OSDN Git Service

PR42288 emit empty .debug_aranges section.
[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 /* True if the compilation unit has location entries that reference
437    debug strings.  */
438 static GTY(()) bool debug_str_hash_forced = false;
439
440 static GTY(()) int dw2_string_counter;
441 static GTY(()) unsigned long dwarf2out_cfi_label_num;
442
443 /* True if the compilation unit places functions in more than one section.  */
444 static GTY(()) bool have_multiple_function_sections = false;
445
446 /* Whether the default text and cold text sections have been used at all.  */
447
448 static GTY(()) bool text_section_used = false;
449 static GTY(()) bool cold_text_section_used = false;
450
451 /* The default cold text section.  */
452 static GTY(()) section *cold_text_section;
453
454 /* Forward declarations for functions defined in this file.  */
455
456 static char *stripattributes (const char *);
457 static const char *dwarf_cfi_name (unsigned);
458 static dw_cfi_ref new_cfi (void);
459 static void add_cfi (cfi_vec *, dw_cfi_ref);
460 static void add_fde_cfi (const char *, dw_cfi_ref);
461 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
462 static void lookup_cfa (dw_cfa_location *);
463 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
464 static void initial_return_save (rtx);
465 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
466                                           HOST_WIDE_INT);
467 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
468 static void output_cfi_directive (dw_cfi_ref);
469 static void output_call_frame_info (int);
470 static void dwarf2out_note_section_used (void);
471 static bool clobbers_queued_reg_save (const_rtx);
472 static void dwarf2out_frame_debug_expr (rtx, const char *);
473
474 /* Support for complex CFA locations.  */
475 static void output_cfa_loc (dw_cfi_ref, int);
476 static void output_cfa_loc_raw (dw_cfi_ref);
477 static void get_cfa_from_loc_descr (dw_cfa_location *,
478                                     struct dw_loc_descr_struct *);
479 static struct dw_loc_descr_struct *build_cfa_loc
480   (dw_cfa_location *, HOST_WIDE_INT);
481 static struct dw_loc_descr_struct *build_cfa_aligned_loc
482   (HOST_WIDE_INT, HOST_WIDE_INT);
483 static void def_cfa_1 (const char *, dw_cfa_location *);
484 static struct dw_loc_descr_struct *mem_loc_descriptor
485   (rtx, enum machine_mode mode, enum machine_mode mem_mode,
486    enum var_init_status);
487
488 /* How to start an assembler comment.  */
489 #ifndef ASM_COMMENT_START
490 #define ASM_COMMENT_START ";#"
491 #endif
492
493 /* Data and reference forms for relocatable data.  */
494 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
495 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
496
497 #ifndef DEBUG_FRAME_SECTION
498 #define DEBUG_FRAME_SECTION     ".debug_frame"
499 #endif
500
501 #ifndef FUNC_BEGIN_LABEL
502 #define FUNC_BEGIN_LABEL        "LFB"
503 #endif
504
505 #ifndef FUNC_END_LABEL
506 #define FUNC_END_LABEL          "LFE"
507 #endif
508
509 #ifndef PROLOGUE_END_LABEL
510 #define PROLOGUE_END_LABEL      "LPE"
511 #endif
512
513 #ifndef EPILOGUE_BEGIN_LABEL
514 #define EPILOGUE_BEGIN_LABEL    "LEB"
515 #endif
516
517 #ifndef FRAME_BEGIN_LABEL
518 #define FRAME_BEGIN_LABEL       "Lframe"
519 #endif
520 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
521 #define CIE_END_LABEL           "LECIE"
522 #define FDE_LABEL               "LSFDE"
523 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
524 #define FDE_END_LABEL           "LEFDE"
525 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
526 #define LINE_NUMBER_END_LABEL   "LELT"
527 #define LN_PROLOG_AS_LABEL      "LASLTP"
528 #define LN_PROLOG_END_LABEL     "LELTP"
529 #define DIE_LABEL_PREFIX        "DW"
530
531 /* The DWARF 2 CFA column which tracks the return address.  Normally this
532    is the column for PC, or the first column after all of the hard
533    registers.  */
534 #ifndef DWARF_FRAME_RETURN_COLUMN
535 #ifdef PC_REGNUM
536 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
537 #else
538 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
539 #endif
540 #endif
541
542 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
543    default, we just provide columns for all registers.  */
544 #ifndef DWARF_FRAME_REGNUM
545 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
546 #endif
547 \f
548 /* Match the base name of a file to the base name of a compilation unit. */
549
550 static int
551 matches_main_base (const char *path)
552 {
553   /* Cache the last query. */
554   static const char *last_path = NULL;
555   static int last_match = 0;
556   if (path != last_path)
557     {
558       const char *base;
559       int length = base_of_path (path, &base);
560       last_path = path;
561       last_match = (length == main_input_baselength
562                     && memcmp (base, main_input_basename, length) == 0);
563     }
564   return last_match;
565 }
566
567 #ifdef DEBUG_DEBUG_STRUCT
568
569 static int
570 dump_struct_debug (tree type, enum debug_info_usage usage,
571                    enum debug_struct_file criterion, int generic,
572                    int matches, int result)
573 {
574   /* Find the type name. */
575   tree type_decl = TYPE_STUB_DECL (type);
576   tree t = type_decl;
577   const char *name = 0;
578   if (TREE_CODE (t) == TYPE_DECL)
579     t = DECL_NAME (t);
580   if (t)
581     name = IDENTIFIER_POINTER (t);
582
583   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
584            criterion,
585            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
586            matches ? "bas" : "hdr",
587            generic ? "gen" : "ord",
588            usage == DINFO_USAGE_DFN ? ";" :
589              usage == DINFO_USAGE_DIR_USE ? "." : "*",
590            result,
591            (void*) type_decl, name);
592   return result;
593 }
594 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
595   dump_struct_debug (type, usage, criterion, generic, matches, result)
596
597 #else
598
599 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
600   (result)
601
602 #endif
603
604 static bool
605 should_emit_struct_debug (tree type, enum debug_info_usage usage)
606 {
607   enum debug_struct_file criterion;
608   tree type_decl;
609   bool generic = lang_hooks.types.generic_p (type);
610
611   if (generic)
612     criterion = debug_struct_generic[usage];
613   else
614     criterion = debug_struct_ordinary[usage];
615
616   if (criterion == DINFO_STRUCT_FILE_NONE)
617     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
618   if (criterion == DINFO_STRUCT_FILE_ANY)
619     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
620
621   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
622
623   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
624     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
625
626   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
627     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
628   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
629 }
630 \f
631 /* Hook used by __throw.  */
632
633 rtx
634 expand_builtin_dwarf_sp_column (void)
635 {
636   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
637   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
638 }
639
640 /* Return a pointer to a copy of the section string name S with all
641    attributes stripped off, and an asterisk prepended (for assemble_name).  */
642
643 static inline char *
644 stripattributes (const char *s)
645 {
646   char *stripped = XNEWVEC (char, strlen (s) + 2);
647   char *p = stripped;
648
649   *p++ = '*';
650
651   while (*s && *s != ',')
652     *p++ = *s++;
653
654   *p = '\0';
655   return stripped;
656 }
657
658 /* MEM is a memory reference for the register size table, each element of
659    which has mode MODE.  Initialize column C as a return address column.  */
660
661 static void
662 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
663 {
664   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
665   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
666   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
667 }
668
669 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
670
671 static inline HOST_WIDE_INT
672 div_data_align (HOST_WIDE_INT off)
673 {
674   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
675   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
676   return r;
677 }
678
679 /* Return true if we need a signed version of a given opcode
680    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
681
682 static inline bool
683 need_data_align_sf_opcode (HOST_WIDE_INT off)
684 {
685   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
686 }
687
688 /* Generate code to initialize the register size table.  */
689
690 void
691 expand_builtin_init_dwarf_reg_sizes (tree address)
692 {
693   unsigned int i;
694   enum machine_mode mode = TYPE_MODE (char_type_node);
695   rtx addr = expand_normal (address);
696   rtx mem = gen_rtx_MEM (BLKmode, addr);
697   bool wrote_return_column = false;
698
699   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
700     {
701       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
702
703       if (rnum < DWARF_FRAME_REGISTERS)
704         {
705           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
706           enum machine_mode save_mode = reg_raw_mode[i];
707           HOST_WIDE_INT size;
708
709           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
710             save_mode = choose_hard_reg_mode (i, 1, true);
711           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
712             {
713               if (save_mode == VOIDmode)
714                 continue;
715               wrote_return_column = true;
716             }
717           size = GET_MODE_SIZE (save_mode);
718           if (offset < 0)
719             continue;
720
721           emit_move_insn (adjust_address (mem, mode, offset),
722                           gen_int_mode (size, mode));
723         }
724     }
725
726   if (!wrote_return_column)
727     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
728
729 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
730   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
731 #endif
732
733   targetm.init_dwarf_reg_sizes_extra (address);
734 }
735
736 /* Convert a DWARF call frame info. operation to its string name */
737
738 static const char *
739 dwarf_cfi_name (unsigned int cfi_opc)
740 {
741   switch (cfi_opc)
742     {
743     case DW_CFA_advance_loc:
744       return "DW_CFA_advance_loc";
745     case DW_CFA_offset:
746       return "DW_CFA_offset";
747     case DW_CFA_restore:
748       return "DW_CFA_restore";
749     case DW_CFA_nop:
750       return "DW_CFA_nop";
751     case DW_CFA_set_loc:
752       return "DW_CFA_set_loc";
753     case DW_CFA_advance_loc1:
754       return "DW_CFA_advance_loc1";
755     case DW_CFA_advance_loc2:
756       return "DW_CFA_advance_loc2";
757     case DW_CFA_advance_loc4:
758       return "DW_CFA_advance_loc4";
759     case DW_CFA_offset_extended:
760       return "DW_CFA_offset_extended";
761     case DW_CFA_restore_extended:
762       return "DW_CFA_restore_extended";
763     case DW_CFA_undefined:
764       return "DW_CFA_undefined";
765     case DW_CFA_same_value:
766       return "DW_CFA_same_value";
767     case DW_CFA_register:
768       return "DW_CFA_register";
769     case DW_CFA_remember_state:
770       return "DW_CFA_remember_state";
771     case DW_CFA_restore_state:
772       return "DW_CFA_restore_state";
773     case DW_CFA_def_cfa:
774       return "DW_CFA_def_cfa";
775     case DW_CFA_def_cfa_register:
776       return "DW_CFA_def_cfa_register";
777     case DW_CFA_def_cfa_offset:
778       return "DW_CFA_def_cfa_offset";
779
780     /* DWARF 3 */
781     case DW_CFA_def_cfa_expression:
782       return "DW_CFA_def_cfa_expression";
783     case DW_CFA_expression:
784       return "DW_CFA_expression";
785     case DW_CFA_offset_extended_sf:
786       return "DW_CFA_offset_extended_sf";
787     case DW_CFA_def_cfa_sf:
788       return "DW_CFA_def_cfa_sf";
789     case DW_CFA_def_cfa_offset_sf:
790       return "DW_CFA_def_cfa_offset_sf";
791
792     /* SGI/MIPS specific */
793     case DW_CFA_MIPS_advance_loc8:
794       return "DW_CFA_MIPS_advance_loc8";
795
796     /* GNU extensions */
797     case DW_CFA_GNU_window_save:
798       return "DW_CFA_GNU_window_save";
799     case DW_CFA_GNU_args_size:
800       return "DW_CFA_GNU_args_size";
801     case DW_CFA_GNU_negative_offset_extended:
802       return "DW_CFA_GNU_negative_offset_extended";
803
804     default:
805       return "DW_CFA_<unknown>";
806     }
807 }
808
809 /* Return a pointer to a newly allocated Call Frame Instruction.  */
810
811 static inline dw_cfi_ref
812 new_cfi (void)
813 {
814   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
815
816   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
817   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
818
819   return cfi;
820 }
821
822 /* Add a Call Frame Instruction to list of instructions.  */
823
824 static inline void
825 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
826 {
827   dw_fde_ref fde = current_fde ();
828
829   /* When DRAP is used, CFA is defined with an expression.  Redefine
830      CFA may lead to a different CFA value.   */
831   /* ??? Of course, this heuristic fails when we're annotating epilogues,
832      because of course we'll always want to redefine the CFA back to the
833      stack pointer on the way out.  Where should we move this check?  */
834   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
835     switch (cfi->dw_cfi_opc)
836       {
837         case DW_CFA_def_cfa_register:
838         case DW_CFA_def_cfa_offset:
839         case DW_CFA_def_cfa_offset_sf:
840         case DW_CFA_def_cfa:
841         case DW_CFA_def_cfa_sf:
842           gcc_unreachable ();
843
844         default:
845           break;
846       }
847
848   VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
849 }
850
851 /* Generate a new label for the CFI info to refer to.  FORCE is true
852    if a label needs to be output even when using .cfi_* directives.  */
853
854 char *
855 dwarf2out_cfi_label (bool force)
856 {
857   static char label[20];
858
859   if (!force && dwarf2out_do_cfi_asm ())
860     {
861       /* In this case, we will be emitting the asm directive instead of
862          the label, so just return a placeholder to keep the rest of the
863          interfaces happy.  */
864       strcpy (label, "<do not output>");
865     }
866   else
867     {
868       int num = dwarf2out_cfi_label_num++;
869       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
870       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
871     }
872
873   return label;
874 }
875
876 /* True if remember_state should be emitted before following CFI directive.  */
877 static bool emit_cfa_remember;
878
879 /* True if any CFI directives were emitted at the current insn.  */
880 static bool any_cfis_emitted;
881
882 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
883    or to the CIE if LABEL is NULL.  */
884
885 static void
886 add_fde_cfi (const char *label, dw_cfi_ref cfi)
887 {
888   cfi_vec *vec;
889
890   if (cie_cfi_vec == NULL)
891     cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
892
893   vec = &cie_cfi_vec;
894
895   if (emit_cfa_remember)
896     {
897       dw_cfi_ref cfi_remember;
898
899       /* Emit the state save.  */
900       emit_cfa_remember = false;
901       cfi_remember = new_cfi ();
902       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
903       add_fde_cfi (label, cfi_remember);
904     }
905
906   if (dwarf2out_do_cfi_asm ())
907     {
908       if (label)
909         {
910           dw_fde_ref fde = current_fde ();
911
912           gcc_assert (fde != NULL);
913
914           /* We still have to add the cfi to the list so that lookup_cfa
915              works later on.  When -g2 and above we even need to force
916              emitting of CFI labels and add to list a DW_CFA_set_loc for
917              convert_cfa_to_fb_loc_list purposes.  If we're generating
918              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
919              convert_cfa_to_fb_loc_list.  */
920           if (dwarf_version == 2
921               && debug_info_level > DINFO_LEVEL_TERSE
922               && (write_symbols == DWARF2_DEBUG
923                   || write_symbols == VMS_AND_DWARF2_DEBUG))
924             {
925               switch (cfi->dw_cfi_opc)
926                 {
927                 case DW_CFA_def_cfa_offset:
928                 case DW_CFA_def_cfa_offset_sf:
929                 case DW_CFA_def_cfa_register:
930                 case DW_CFA_def_cfa:
931                 case DW_CFA_def_cfa_sf:
932                 case DW_CFA_def_cfa_expression:
933                 case DW_CFA_restore_state:
934                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
935                     label = dwarf2out_cfi_label (true);
936
937                   if (fde->dw_fde_current_label == NULL
938                       || strcmp (label, fde->dw_fde_current_label) != 0)
939                     {
940                       dw_cfi_ref xcfi;
941
942                       label = xstrdup (label);
943
944                       /* Set the location counter to the new label.  */
945                       xcfi = new_cfi ();
946                       /* It doesn't metter whether DW_CFA_set_loc
947                          or DW_CFA_advance_loc4 is added here, those aren't
948                          emitted into assembly, only looked up by
949                          convert_cfa_to_fb_loc_list.  */
950                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
951                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
952                       add_cfi (&fde->dw_fde_cfi, xcfi);
953                       fde->dw_fde_current_label = label;
954                     }
955                   break;
956                 default:
957                   break;
958                 }
959             }
960
961           output_cfi_directive (cfi);
962
963           vec = &fde->dw_fde_cfi;
964           any_cfis_emitted = true;
965         }
966       /* ??? If this is a CFI for the CIE, we don't emit.  This
967          assumes that the standard CIE contents that the assembler
968          uses matches the standard CIE contents that the compiler
969          uses.  This is probably a bad assumption.  I'm not quite
970          sure how to address this for now.  */
971     }
972   else if (label)
973     {
974       dw_fde_ref fde = current_fde ();
975
976       gcc_assert (fde != NULL);
977
978       if (*label == 0)
979         label = dwarf2out_cfi_label (false);
980
981       if (fde->dw_fde_current_label == NULL
982           || strcmp (label, fde->dw_fde_current_label) != 0)
983         {
984           dw_cfi_ref xcfi;
985
986           label = xstrdup (label);
987
988           /* Set the location counter to the new label.  */
989           xcfi = new_cfi ();
990           /* If we have a current label, advance from there, otherwise
991              set the location directly using set_loc.  */
992           xcfi->dw_cfi_opc = fde->dw_fde_current_label
993                              ? DW_CFA_advance_loc4
994                              : DW_CFA_set_loc;
995           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
996           add_cfi (&fde->dw_fde_cfi, xcfi);
997
998           fde->dw_fde_current_label = label;
999         }
1000
1001       vec = &fde->dw_fde_cfi;
1002       any_cfis_emitted = true;
1003     }
1004
1005   add_cfi (vec, cfi);
1006 }
1007
1008 /* Subroutine of lookup_cfa.  */
1009
1010 static void
1011 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1012 {
1013   switch (cfi->dw_cfi_opc)
1014     {
1015     case DW_CFA_def_cfa_offset:
1016     case DW_CFA_def_cfa_offset_sf:
1017       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1018       break;
1019     case DW_CFA_def_cfa_register:
1020       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1021       break;
1022     case DW_CFA_def_cfa:
1023     case DW_CFA_def_cfa_sf:
1024       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1025       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1026       break;
1027     case DW_CFA_def_cfa_expression:
1028       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1029       break;
1030
1031     case DW_CFA_remember_state:
1032       gcc_assert (!remember->in_use);
1033       *remember = *loc;
1034       remember->in_use = 1;
1035       break;
1036     case DW_CFA_restore_state:
1037       gcc_assert (remember->in_use);
1038       *loc = *remember;
1039       remember->in_use = 0;
1040       break;
1041
1042     default:
1043       break;
1044     }
1045 }
1046
1047 /* Find the previous value for the CFA.  */
1048
1049 static void
1050 lookup_cfa (dw_cfa_location *loc)
1051 {
1052   int ix;
1053   dw_cfi_ref cfi;
1054   dw_fde_ref fde;
1055   dw_cfa_location remember;
1056
1057   memset (loc, 0, sizeof (*loc));
1058   loc->reg = INVALID_REGNUM;
1059   remember = *loc;
1060
1061   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1062     lookup_cfa_1 (cfi, loc, &remember);
1063
1064   fde = current_fde ();
1065   if (fde)
1066     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1067       lookup_cfa_1 (cfi, loc, &remember);
1068 }
1069
1070 /* The current rule for calculating the DWARF2 canonical frame address.  */
1071 static dw_cfa_location cfa;
1072
1073 /* The register used for saving registers to the stack, and its offset
1074    from the CFA.  */
1075 static dw_cfa_location cfa_store;
1076
1077 /* The current save location around an epilogue.  */
1078 static dw_cfa_location cfa_remember;
1079
1080 /* The running total of the size of arguments pushed onto the stack.  */
1081 static HOST_WIDE_INT args_size;
1082
1083 /* The last args_size we actually output.  */
1084 static HOST_WIDE_INT old_args_size;
1085
1086 /* Entry point to update the canonical frame address (CFA).
1087    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1088    calculated from REG+OFFSET.  */
1089
1090 void
1091 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1092 {
1093   dw_cfa_location loc;
1094   loc.indirect = 0;
1095   loc.base_offset = 0;
1096   loc.reg = reg;
1097   loc.offset = offset;
1098   def_cfa_1 (label, &loc);
1099 }
1100
1101 /* Determine if two dw_cfa_location structures define the same data.  */
1102
1103 static bool
1104 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1105 {
1106   return (loc1->reg == loc2->reg
1107           && loc1->offset == loc2->offset
1108           && loc1->indirect == loc2->indirect
1109           && (loc1->indirect == 0
1110               || loc1->base_offset == loc2->base_offset));
1111 }
1112
1113 /* This routine does the actual work.  The CFA is now calculated from
1114    the dw_cfa_location structure.  */
1115
1116 static void
1117 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1118 {
1119   dw_cfi_ref cfi;
1120   dw_cfa_location old_cfa, loc;
1121
1122   cfa = *loc_p;
1123   loc = *loc_p;
1124
1125   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1126     cfa_store.offset = loc.offset;
1127
1128   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1129   lookup_cfa (&old_cfa);
1130
1131   /* If nothing changed, no need to issue any call frame instructions.  */
1132   if (cfa_equal_p (&loc, &old_cfa))
1133     return;
1134
1135   cfi = new_cfi ();
1136
1137   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1138     {
1139       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1140          the CFA register did not change but the offset did.  The data
1141          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1142          in the assembler via the .cfi_def_cfa_offset directive.  */
1143       if (loc.offset < 0)
1144         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1145       else
1146         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1147       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1148     }
1149
1150 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1151   else if (loc.offset == old_cfa.offset
1152            && old_cfa.reg != INVALID_REGNUM
1153            && !loc.indirect
1154            && !old_cfa.indirect)
1155     {
1156       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1157          indicating the CFA register has changed to <register> but the
1158          offset has not changed.  */
1159       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1160       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1161     }
1162 #endif
1163
1164   else if (loc.indirect == 0)
1165     {
1166       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1167          indicating the CFA register has changed to <register> with
1168          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1169          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1170          directive.  */
1171       if (loc.offset < 0)
1172         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1173       else
1174         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1175       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1176       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1177     }
1178   else
1179     {
1180       /* Construct a DW_CFA_def_cfa_expression instruction to
1181          calculate the CFA using a full location expression since no
1182          register-offset pair is available.  */
1183       struct dw_loc_descr_struct *loc_list;
1184
1185       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1186       loc_list = build_cfa_loc (&loc, 0);
1187       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1188     }
1189
1190   add_fde_cfi (label, cfi);
1191 }
1192
1193 /* Add the CFI for saving a register.  REG is the CFA column number.
1194    LABEL is passed to add_fde_cfi.
1195    If SREG is -1, the register is saved at OFFSET from the CFA;
1196    otherwise it is saved in SREG.  */
1197
1198 static void
1199 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1200 {
1201   dw_cfi_ref cfi = new_cfi ();
1202   dw_fde_ref fde = current_fde ();
1203
1204   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1205
1206   /* When stack is aligned, store REG using DW_CFA_expression with
1207      FP.  */
1208   if (fde
1209       && fde->stack_realign
1210       && sreg == INVALID_REGNUM)
1211     {
1212       cfi->dw_cfi_opc = DW_CFA_expression;
1213       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1214       cfi->dw_cfi_oprnd2.dw_cfi_loc
1215         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1216     }
1217   else if (sreg == INVALID_REGNUM)
1218     {
1219       if (need_data_align_sf_opcode (offset))
1220         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1221       else if (reg & ~0x3f)
1222         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1223       else
1224         cfi->dw_cfi_opc = DW_CFA_offset;
1225       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1226     }
1227   else if (sreg == reg)
1228     cfi->dw_cfi_opc = DW_CFA_same_value;
1229   else
1230     {
1231       cfi->dw_cfi_opc = DW_CFA_register;
1232       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1233     }
1234
1235   add_fde_cfi (label, cfi);
1236 }
1237
1238 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1239    This CFI tells the unwinder that it needs to restore the window registers
1240    from the previous frame's window save area.
1241
1242    ??? Perhaps we should note in the CIE where windows are saved (instead of
1243    assuming 0(cfa)) and what registers are in the window.  */
1244
1245 void
1246 dwarf2out_window_save (const char *label)
1247 {
1248   dw_cfi_ref cfi = new_cfi ();
1249
1250   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1251   add_fde_cfi (label, cfi);
1252 }
1253
1254 /* Entry point for saving a register to the stack.  REG is the GCC register
1255    number.  LABEL and OFFSET are passed to reg_save.  */
1256
1257 void
1258 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1259 {
1260   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1261 }
1262
1263 /* Entry point for saving the return address in the stack.
1264    LABEL and OFFSET are passed to reg_save.  */
1265
1266 void
1267 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1268 {
1269   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1270 }
1271
1272 /* Entry point for saving the return address in a register.
1273    LABEL and SREG are passed to reg_save.  */
1274
1275 void
1276 dwarf2out_return_reg (const char *label, unsigned int sreg)
1277 {
1278   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1279 }
1280
1281 /* Record the initial position of the return address.  RTL is
1282    INCOMING_RETURN_ADDR_RTX.  */
1283
1284 static void
1285 initial_return_save (rtx rtl)
1286 {
1287   unsigned int reg = INVALID_REGNUM;
1288   HOST_WIDE_INT offset = 0;
1289
1290   switch (GET_CODE (rtl))
1291     {
1292     case REG:
1293       /* RA is in a register.  */
1294       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1295       break;
1296
1297     case MEM:
1298       /* RA is on the stack.  */
1299       rtl = XEXP (rtl, 0);
1300       switch (GET_CODE (rtl))
1301         {
1302         case REG:
1303           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1304           offset = 0;
1305           break;
1306
1307         case PLUS:
1308           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1309           offset = INTVAL (XEXP (rtl, 1));
1310           break;
1311
1312         case MINUS:
1313           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1314           offset = -INTVAL (XEXP (rtl, 1));
1315           break;
1316
1317         default:
1318           gcc_unreachable ();
1319         }
1320
1321       break;
1322
1323     case PLUS:
1324       /* The return address is at some offset from any value we can
1325          actually load.  For instance, on the SPARC it is in %i7+8. Just
1326          ignore the offset for now; it doesn't matter for unwinding frames.  */
1327       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1328       initial_return_save (XEXP (rtl, 0));
1329       return;
1330
1331     default:
1332       gcc_unreachable ();
1333     }
1334
1335   if (reg != DWARF_FRAME_RETURN_COLUMN)
1336     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1337 }
1338
1339 /* Given a SET, calculate the amount of stack adjustment it
1340    contains.  */
1341
1342 static HOST_WIDE_INT
1343 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1344                      HOST_WIDE_INT cur_offset)
1345 {
1346   const_rtx src = SET_SRC (pattern);
1347   const_rtx dest = SET_DEST (pattern);
1348   HOST_WIDE_INT offset = 0;
1349   enum rtx_code code;
1350
1351   if (dest == stack_pointer_rtx)
1352     {
1353       code = GET_CODE (src);
1354
1355       /* Assume (set (reg sp) (reg whatever)) sets args_size
1356          level to 0.  */
1357       if (code == REG && src != stack_pointer_rtx)
1358         {
1359           offset = -cur_args_size;
1360 #ifndef STACK_GROWS_DOWNWARD
1361           offset = -offset;
1362 #endif
1363           return offset - cur_offset;
1364         }
1365
1366       if (! (code == PLUS || code == MINUS)
1367           || XEXP (src, 0) != stack_pointer_rtx
1368           || !CONST_INT_P (XEXP (src, 1)))
1369         return 0;
1370
1371       /* (set (reg sp) (plus (reg sp) (const_int))) */
1372       offset = INTVAL (XEXP (src, 1));
1373       if (code == PLUS)
1374         offset = -offset;
1375       return offset;
1376     }
1377
1378   if (MEM_P (src) && !MEM_P (dest))
1379     dest = src;
1380   if (MEM_P (dest))
1381     {
1382       /* (set (mem (pre_dec (reg sp))) (foo)) */
1383       src = XEXP (dest, 0);
1384       code = GET_CODE (src);
1385
1386       switch (code)
1387         {
1388         case PRE_MODIFY:
1389         case POST_MODIFY:
1390           if (XEXP (src, 0) == stack_pointer_rtx)
1391             {
1392               rtx val = XEXP (XEXP (src, 1), 1);
1393               /* We handle only adjustments by constant amount.  */
1394               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1395                           && CONST_INT_P (val));
1396               offset = -INTVAL (val);
1397               break;
1398             }
1399           return 0;
1400
1401         case PRE_DEC:
1402         case POST_DEC:
1403           if (XEXP (src, 0) == stack_pointer_rtx)
1404             {
1405               offset = GET_MODE_SIZE (GET_MODE (dest));
1406               break;
1407             }
1408           return 0;
1409
1410         case PRE_INC:
1411         case POST_INC:
1412           if (XEXP (src, 0) == stack_pointer_rtx)
1413             {
1414               offset = -GET_MODE_SIZE (GET_MODE (dest));
1415               break;
1416             }
1417           return 0;
1418
1419         default:
1420           return 0;
1421         }
1422     }
1423   else
1424     return 0;
1425
1426   return offset;
1427 }
1428
1429 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1430    indexed by INSN_UID.  */
1431
1432 static HOST_WIDE_INT *barrier_args_size;
1433
1434 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1435
1436 static HOST_WIDE_INT
1437 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1438                              VEC (rtx, heap) **next)
1439 {
1440   HOST_WIDE_INT offset = 0;
1441   int i;
1442
1443   if (! RTX_FRAME_RELATED_P (insn))
1444     {
1445       if (prologue_epilogue_contains (insn))
1446         /* Nothing */;
1447       else if (GET_CODE (PATTERN (insn)) == SET)
1448         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1449       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1450                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1451         {
1452           /* There may be stack adjustments inside compound insns.  Search
1453              for them.  */
1454           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1455             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1456               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1457                                              cur_args_size, offset);
1458         }
1459     }
1460   else
1461     {
1462       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1463
1464       if (expr)
1465         {
1466           expr = XEXP (expr, 0);
1467           if (GET_CODE (expr) == PARALLEL
1468               || GET_CODE (expr) == SEQUENCE)
1469             for (i = 1; i < XVECLEN (expr, 0); i++)
1470               {
1471                 rtx elem = XVECEXP (expr, 0, i);
1472
1473                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1474                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1475               }
1476         }
1477     }
1478
1479 #ifndef STACK_GROWS_DOWNWARD
1480   offset = -offset;
1481 #endif
1482
1483   cur_args_size += offset;
1484   if (cur_args_size < 0)
1485     cur_args_size = 0;
1486
1487   if (JUMP_P (insn))
1488     {
1489       rtx dest = JUMP_LABEL (insn);
1490
1491       if (dest)
1492         {
1493           if (barrier_args_size [INSN_UID (dest)] < 0)
1494             {
1495               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1496               VEC_safe_push (rtx, heap, *next, dest);
1497             }
1498         }
1499     }
1500
1501   return cur_args_size;
1502 }
1503
1504 /* Walk the whole function and compute args_size on BARRIERs.  */
1505
1506 static void
1507 compute_barrier_args_size (void)
1508 {
1509   int max_uid = get_max_uid (), i;
1510   rtx insn;
1511   VEC (rtx, heap) *worklist, *next, *tmp;
1512
1513   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1514   for (i = 0; i < max_uid; i++)
1515     barrier_args_size[i] = -1;
1516
1517   worklist = VEC_alloc (rtx, heap, 20);
1518   next = VEC_alloc (rtx, heap, 20);
1519   insn = get_insns ();
1520   barrier_args_size[INSN_UID (insn)] = 0;
1521   VEC_quick_push (rtx, worklist, insn);
1522   for (;;)
1523     {
1524       while (!VEC_empty (rtx, worklist))
1525         {
1526           rtx prev, body, first_insn;
1527           HOST_WIDE_INT cur_args_size;
1528
1529           first_insn = insn = VEC_pop (rtx, worklist);
1530           cur_args_size = barrier_args_size[INSN_UID (insn)];
1531           prev = prev_nonnote_insn (insn);
1532           if (prev && BARRIER_P (prev))
1533             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1534
1535           for (; insn; insn = NEXT_INSN (insn))
1536             {
1537               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1538                 continue;
1539               if (BARRIER_P (insn))
1540                 break;
1541
1542               if (LABEL_P (insn))
1543                 {
1544                   if (insn == first_insn)
1545                     continue;
1546                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1547                     {
1548                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1549                       continue;
1550                     }
1551                   else
1552                     {
1553                       /* The insns starting with this label have been
1554                          already scanned or are in the worklist.  */
1555                       break;
1556                     }
1557                 }
1558
1559               body = PATTERN (insn);
1560               if (GET_CODE (body) == SEQUENCE)
1561                 {
1562                   HOST_WIDE_INT dest_args_size = cur_args_size;
1563                   for (i = 1; i < XVECLEN (body, 0); i++)
1564                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1565                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1566                       dest_args_size
1567                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1568                                                        dest_args_size, &next);
1569                     else
1570                       cur_args_size
1571                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1572                                                        cur_args_size, &next);
1573
1574                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1575                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1576                                                  dest_args_size, &next);
1577                   else
1578                     cur_args_size
1579                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1580                                                      cur_args_size, &next);
1581                 }
1582               else
1583                 cur_args_size
1584                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1585             }
1586         }
1587
1588       if (VEC_empty (rtx, next))
1589         break;
1590
1591       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1592       tmp = next;
1593       next = worklist;
1594       worklist = tmp;
1595       VEC_truncate (rtx, next, 0);
1596     }
1597
1598   VEC_free (rtx, heap, worklist);
1599   VEC_free (rtx, heap, next);
1600 }
1601
1602 /* Add a CFI to update the running total of the size of arguments
1603    pushed onto the stack.  */
1604
1605 static void
1606 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1607 {
1608   dw_cfi_ref cfi;
1609
1610   if (size == old_args_size)
1611     return;
1612
1613   old_args_size = size;
1614
1615   cfi = new_cfi ();
1616   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1617   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1618   add_fde_cfi (label, cfi);
1619 }
1620
1621 /* Record a stack adjustment of OFFSET bytes.  */
1622
1623 static void
1624 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1625 {
1626   if (cfa.reg == STACK_POINTER_REGNUM)
1627     cfa.offset += offset;
1628
1629   if (cfa_store.reg == STACK_POINTER_REGNUM)
1630     cfa_store.offset += offset;
1631
1632   if (ACCUMULATE_OUTGOING_ARGS)
1633     return;
1634
1635 #ifndef STACK_GROWS_DOWNWARD
1636   offset = -offset;
1637 #endif
1638
1639   args_size += offset;
1640   if (args_size < 0)
1641     args_size = 0;
1642
1643   def_cfa_1 (label, &cfa);
1644   if (flag_asynchronous_unwind_tables)
1645     dwarf2out_args_size (label, args_size);
1646 }
1647
1648 /* Check INSN to see if it looks like a push or a stack adjustment, and
1649    make a note of it if it does.  EH uses this information to find out
1650    how much extra space it needs to pop off the stack.  */
1651
1652 static void
1653 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1654 {
1655   HOST_WIDE_INT offset;
1656   const char *label;
1657   int i;
1658
1659   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1660      with this function.  Proper support would require all frame-related
1661      insns to be marked, and to be able to handle saving state around
1662      epilogues textually in the middle of the function.  */
1663   if (prologue_epilogue_contains (insn))
1664     return;
1665
1666   /* If INSN is an instruction from target of an annulled branch, the
1667      effects are for the target only and so current argument size
1668      shouldn't change at all.  */
1669   if (final_sequence
1670       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1671       && INSN_FROM_TARGET_P (insn))
1672     return;
1673
1674   /* If only calls can throw, and we have a frame pointer,
1675      save up adjustments until we see the CALL_INSN.  */
1676   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1677     {
1678       if (CALL_P (insn) && !after_p)
1679         {
1680           /* Extract the size of the args from the CALL rtx itself.  */
1681           insn = PATTERN (insn);
1682           if (GET_CODE (insn) == PARALLEL)
1683             insn = XVECEXP (insn, 0, 0);
1684           if (GET_CODE (insn) == SET)
1685             insn = SET_SRC (insn);
1686           gcc_assert (GET_CODE (insn) == CALL);
1687           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1688         }
1689       return;
1690     }
1691
1692   if (CALL_P (insn) && !after_p)
1693     {
1694       if (!flag_asynchronous_unwind_tables)
1695         dwarf2out_args_size ("", args_size);
1696       return;
1697     }
1698   else if (BARRIER_P (insn))
1699     {
1700       /* Don't call compute_barrier_args_size () if the only
1701          BARRIER is at the end of function.  */
1702       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1703         compute_barrier_args_size ();
1704       if (barrier_args_size == NULL)
1705         offset = 0;
1706       else
1707         {
1708           offset = barrier_args_size[INSN_UID (insn)];
1709           if (offset < 0)
1710             offset = 0;
1711         }
1712
1713       offset -= args_size;
1714 #ifndef STACK_GROWS_DOWNWARD
1715       offset = -offset;
1716 #endif
1717     }
1718   else if (GET_CODE (PATTERN (insn)) == SET)
1719     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1720   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1721            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1722     {
1723       /* There may be stack adjustments inside compound insns.  Search
1724          for them.  */
1725       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1726         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1727           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1728                                          args_size, offset);
1729     }
1730   else
1731     return;
1732
1733   if (offset == 0)
1734     return;
1735
1736   label = dwarf2out_cfi_label (false);
1737   dwarf2out_stack_adjust (offset, label);
1738 }
1739
1740 /* We delay emitting a register save until either (a) we reach the end
1741    of the prologue or (b) the register is clobbered.  This clusters
1742    register saves so that there are fewer pc advances.  */
1743
1744 struct GTY(()) queued_reg_save {
1745   struct queued_reg_save *next;
1746   rtx reg;
1747   HOST_WIDE_INT cfa_offset;
1748   rtx saved_reg;
1749 };
1750
1751 static GTY(()) struct queued_reg_save *queued_reg_saves;
1752
1753 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1754 struct GTY(()) reg_saved_in_data {
1755   rtx orig_reg;
1756   rtx saved_in_reg;
1757 };
1758
1759 /* A list of registers saved in other registers.
1760    The list intentionally has a small maximum capacity of 4; if your
1761    port needs more than that, you might consider implementing a
1762    more efficient data structure.  */
1763 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1764 static GTY(()) size_t num_regs_saved_in_regs;
1765
1766 static const char *last_reg_save_label;
1767
1768 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1769    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1770
1771 static void
1772 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1773 {
1774   struct queued_reg_save *q;
1775
1776   /* Duplicates waste space, but it's also necessary to remove them
1777      for correctness, since the queue gets output in reverse
1778      order.  */
1779   for (q = queued_reg_saves; q != NULL; q = q->next)
1780     if (REGNO (q->reg) == REGNO (reg))
1781       break;
1782
1783   if (q == NULL)
1784     {
1785       q = ggc_alloc_queued_reg_save ();
1786       q->next = queued_reg_saves;
1787       queued_reg_saves = q;
1788     }
1789
1790   q->reg = reg;
1791   q->cfa_offset = offset;
1792   q->saved_reg = sreg;
1793
1794   last_reg_save_label = label;
1795 }
1796
1797 /* Output all the entries in QUEUED_REG_SAVES.  */
1798
1799 void
1800 dwarf2out_flush_queued_reg_saves (void)
1801 {
1802   struct queued_reg_save *q;
1803
1804   for (q = queued_reg_saves; q; q = q->next)
1805     {
1806       size_t i;
1807       unsigned int reg, sreg;
1808
1809       for (i = 0; i < num_regs_saved_in_regs; i++)
1810         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1811           break;
1812       if (q->saved_reg && i == num_regs_saved_in_regs)
1813         {
1814           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1815           num_regs_saved_in_regs++;
1816         }
1817       if (i != num_regs_saved_in_regs)
1818         {
1819           regs_saved_in_regs[i].orig_reg = q->reg;
1820           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1821         }
1822
1823       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1824       if (q->saved_reg)
1825         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1826       else
1827         sreg = INVALID_REGNUM;
1828       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1829     }
1830
1831   queued_reg_saves = NULL;
1832   last_reg_save_label = NULL;
1833 }
1834
1835 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1836    location for?  Or, does it clobber a register which we've previously
1837    said that some other register is saved in, and for which we now
1838    have a new location for?  */
1839
1840 static bool
1841 clobbers_queued_reg_save (const_rtx insn)
1842 {
1843   struct queued_reg_save *q;
1844
1845   for (q = queued_reg_saves; q; q = q->next)
1846     {
1847       size_t i;
1848       if (modified_in_p (q->reg, insn))
1849         return true;
1850       for (i = 0; i < num_regs_saved_in_regs; i++)
1851         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1852             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1853           return true;
1854     }
1855
1856   return false;
1857 }
1858
1859 /* Entry point for saving the first register into the second.  */
1860
1861 void
1862 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1863 {
1864   size_t i;
1865   unsigned int regno, sregno;
1866
1867   for (i = 0; i < num_regs_saved_in_regs; i++)
1868     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1869       break;
1870   if (i == num_regs_saved_in_regs)
1871     {
1872       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1873       num_regs_saved_in_regs++;
1874     }
1875   regs_saved_in_regs[i].orig_reg = reg;
1876   regs_saved_in_regs[i].saved_in_reg = sreg;
1877
1878   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1879   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1880   reg_save (label, regno, sregno, 0);
1881 }
1882
1883 /* What register, if any, is currently saved in REG?  */
1884
1885 static rtx
1886 reg_saved_in (rtx reg)
1887 {
1888   unsigned int regn = REGNO (reg);
1889   size_t i;
1890   struct queued_reg_save *q;
1891
1892   for (q = queued_reg_saves; q; q = q->next)
1893     if (q->saved_reg && regn == REGNO (q->saved_reg))
1894       return q->reg;
1895
1896   for (i = 0; i < num_regs_saved_in_regs; i++)
1897     if (regs_saved_in_regs[i].saved_in_reg
1898         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1899       return regs_saved_in_regs[i].orig_reg;
1900
1901   return NULL_RTX;
1902 }
1903
1904
1905 /* A temporary register holding an integral value used in adjusting SP
1906    or setting up the store_reg.  The "offset" field holds the integer
1907    value, not an offset.  */
1908 static dw_cfa_location cfa_temp;
1909
1910 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1911
1912 static void
1913 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1914 {
1915   memset (&cfa, 0, sizeof (cfa));
1916
1917   switch (GET_CODE (pat))
1918     {
1919     case PLUS:
1920       cfa.reg = REGNO (XEXP (pat, 0));
1921       cfa.offset = INTVAL (XEXP (pat, 1));
1922       break;
1923
1924     case REG:
1925       cfa.reg = REGNO (pat);
1926       break;
1927
1928     case MEM:
1929       cfa.indirect = 1;
1930       pat = XEXP (pat, 0);
1931       if (GET_CODE (pat) == PLUS)
1932         {
1933           cfa.base_offset = INTVAL (XEXP (pat, 1));
1934           pat = XEXP (pat, 0);
1935         }
1936       cfa.reg = REGNO (pat);
1937       break;
1938
1939     default:
1940       /* Recurse and define an expression.  */
1941       gcc_unreachable ();
1942     }
1943
1944   def_cfa_1 (label, &cfa);
1945 }
1946
1947 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1948
1949 static void
1950 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1951 {
1952   rtx src, dest;
1953
1954   gcc_assert (GET_CODE (pat) == SET);
1955   dest = XEXP (pat, 0);
1956   src = XEXP (pat, 1);
1957
1958   switch (GET_CODE (src))
1959     {
1960     case PLUS:
1961       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1962       cfa.offset -= INTVAL (XEXP (src, 1));
1963       break;
1964
1965     case REG:
1966         break;
1967
1968     default:
1969         gcc_unreachable ();
1970     }
1971
1972   cfa.reg = REGNO (dest);
1973   gcc_assert (cfa.indirect == 0);
1974
1975   def_cfa_1 (label, &cfa);
1976 }
1977
1978 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1979
1980 static void
1981 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1982 {
1983   HOST_WIDE_INT offset;
1984   rtx src, addr, span;
1985
1986   src = XEXP (set, 1);
1987   addr = XEXP (set, 0);
1988   gcc_assert (MEM_P (addr));
1989   addr = XEXP (addr, 0);
1990
1991   /* As documented, only consider extremely simple addresses.  */
1992   switch (GET_CODE (addr))
1993     {
1994     case REG:
1995       gcc_assert (REGNO (addr) == cfa.reg);
1996       offset = -cfa.offset;
1997       break;
1998     case PLUS:
1999       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2000       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2001       break;
2002     default:
2003       gcc_unreachable ();
2004     }
2005
2006   span = targetm.dwarf_register_span (src);
2007
2008   /* ??? We'd like to use queue_reg_save, but we need to come up with
2009      a different flushing heuristic for epilogues.  */
2010   if (!span)
2011     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2012   else
2013     {
2014       /* We have a PARALLEL describing where the contents of SRC live.
2015          Queue register saves for each piece of the PARALLEL.  */
2016       int par_index;
2017       int limit;
2018       HOST_WIDE_INT span_offset = offset;
2019
2020       gcc_assert (GET_CODE (span) == PARALLEL);
2021
2022       limit = XVECLEN (span, 0);
2023       for (par_index = 0; par_index < limit; par_index++)
2024         {
2025           rtx elem = XVECEXP (span, 0, par_index);
2026
2027           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2028                     INVALID_REGNUM, span_offset);
2029           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2030         }
2031     }
2032 }
2033
2034 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2035
2036 static void
2037 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2038 {
2039   rtx src, dest;
2040   unsigned sregno, dregno;
2041
2042   src = XEXP (set, 1);
2043   dest = XEXP (set, 0);
2044
2045   if (src == pc_rtx)
2046     sregno = DWARF_FRAME_RETURN_COLUMN;
2047   else
2048     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2049
2050   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2051
2052   /* ??? We'd like to use queue_reg_save, but we need to come up with
2053      a different flushing heuristic for epilogues.  */
2054   reg_save (label, sregno, dregno, 0);
2055 }
2056
2057 /* Helper function to get mode of MEM's address.  */
2058
2059 static inline enum machine_mode
2060 get_address_mode (rtx mem)
2061 {
2062   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
2063   if (mode != VOIDmode)
2064     return mode;
2065   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
2066 }
2067
2068 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2069
2070 static void
2071 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2072 {
2073   rtx src, dest, span;
2074   dw_cfi_ref cfi = new_cfi ();
2075
2076   dest = SET_DEST (set);
2077   src = SET_SRC (set);
2078
2079   gcc_assert (REG_P (src));
2080   gcc_assert (MEM_P (dest));
2081
2082   span = targetm.dwarf_register_span (src);
2083   gcc_assert (!span);
2084
2085   cfi->dw_cfi_opc = DW_CFA_expression;
2086   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2087   cfi->dw_cfi_oprnd2.dw_cfi_loc
2088     = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
2089                           GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
2090
2091   /* ??? We'd like to use queue_reg_save, were the interface different,
2092      and, as above, we could manage flushing for epilogues.  */
2093   add_fde_cfi (label, cfi);
2094 }
2095
2096 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2097
2098 static void
2099 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2100 {
2101   dw_cfi_ref cfi = new_cfi ();
2102   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2103
2104   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2105   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2106
2107   add_fde_cfi (label, cfi);
2108 }
2109
2110 /* Record call frame debugging information for an expression EXPR,
2111    which either sets SP or FP (adjusting how we calculate the frame
2112    address) or saves a register to the stack or another register.
2113    LABEL indicates the address of EXPR.
2114
2115    This function encodes a state machine mapping rtxes to actions on
2116    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2117    users need not read the source code.
2118
2119   The High-Level Picture
2120
2121   Changes in the register we use to calculate the CFA: Currently we
2122   assume that if you copy the CFA register into another register, we
2123   should take the other one as the new CFA register; this seems to
2124   work pretty well.  If it's wrong for some target, it's simple
2125   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2126
2127   Changes in the register we use for saving registers to the stack:
2128   This is usually SP, but not always.  Again, we deduce that if you
2129   copy SP into another register (and SP is not the CFA register),
2130   then the new register is the one we will be using for register
2131   saves.  This also seems to work.
2132
2133   Register saves: There's not much guesswork about this one; if
2134   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2135   register save, and the register used to calculate the destination
2136   had better be the one we think we're using for this purpose.
2137   It's also assumed that a copy from a call-saved register to another
2138   register is saving that register if RTX_FRAME_RELATED_P is set on
2139   that instruction.  If the copy is from a call-saved register to
2140   the *same* register, that means that the register is now the same
2141   value as in the caller.
2142
2143   Except: If the register being saved is the CFA register, and the
2144   offset is nonzero, we are saving the CFA, so we assume we have to
2145   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2146   the intent is to save the value of SP from the previous frame.
2147
2148   In addition, if a register has previously been saved to a different
2149   register,
2150
2151   Invariants / Summaries of Rules
2152
2153   cfa          current rule for calculating the CFA.  It usually
2154                consists of a register and an offset.
2155   cfa_store    register used by prologue code to save things to the stack
2156                cfa_store.offset is the offset from the value of
2157                cfa_store.reg to the actual CFA
2158   cfa_temp     register holding an integral value.  cfa_temp.offset
2159                stores the value, which will be used to adjust the
2160                stack pointer.  cfa_temp is also used like cfa_store,
2161                to track stores to the stack via fp or a temp reg.
2162
2163   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2164                with cfa.reg as the first operand changes the cfa.reg and its
2165                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2166                cfa_temp.offset.
2167
2168   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2169                expression yielding a constant.  This sets cfa_temp.reg
2170                and cfa_temp.offset.
2171
2172   Rule 5:      Create a new register cfa_store used to save items to the
2173                stack.
2174
2175   Rules 10-14: Save a register to the stack.  Define offset as the
2176                difference of the original location and cfa_store's
2177                location (or cfa_temp's location if cfa_temp is used).
2178
2179   Rules 16-20: If AND operation happens on sp in prologue, we assume
2180                stack is realigned.  We will use a group of DW_OP_XXX
2181                expressions to represent the location of the stored
2182                register instead of CFA+offset.
2183
2184   The Rules
2185
2186   "{a,b}" indicates a choice of a xor b.
2187   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2188
2189   Rule 1:
2190   (set <reg1> <reg2>:cfa.reg)
2191   effects: cfa.reg = <reg1>
2192            cfa.offset unchanged
2193            cfa_temp.reg = <reg1>
2194            cfa_temp.offset = cfa.offset
2195
2196   Rule 2:
2197   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2198                               {<const_int>,<reg>:cfa_temp.reg}))
2199   effects: cfa.reg = sp if fp used
2200            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2201            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2202              if cfa_store.reg==sp
2203
2204   Rule 3:
2205   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2206   effects: cfa.reg = fp
2207            cfa_offset += +/- <const_int>
2208
2209   Rule 4:
2210   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2211   constraints: <reg1> != fp
2212                <reg1> != sp
2213   effects: cfa.reg = <reg1>
2214            cfa_temp.reg = <reg1>
2215            cfa_temp.offset = cfa.offset
2216
2217   Rule 5:
2218   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2219   constraints: <reg1> != fp
2220                <reg1> != sp
2221   effects: cfa_store.reg = <reg1>
2222            cfa_store.offset = cfa.offset - cfa_temp.offset
2223
2224   Rule 6:
2225   (set <reg> <const_int>)
2226   effects: cfa_temp.reg = <reg>
2227            cfa_temp.offset = <const_int>
2228
2229   Rule 7:
2230   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2231   effects: cfa_temp.reg = <reg1>
2232            cfa_temp.offset |= <const_int>
2233
2234   Rule 8:
2235   (set <reg> (high <exp>))
2236   effects: none
2237
2238   Rule 9:
2239   (set <reg> (lo_sum <exp> <const_int>))
2240   effects: cfa_temp.reg = <reg>
2241            cfa_temp.offset = <const_int>
2242
2243   Rule 10:
2244   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2245   effects: cfa_store.offset -= <const_int>
2246            cfa.offset = cfa_store.offset if cfa.reg == sp
2247            cfa.reg = sp
2248            cfa.base_offset = -cfa_store.offset
2249
2250   Rule 11:
2251   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2252   effects: cfa_store.offset += -/+ mode_size(mem)
2253            cfa.offset = cfa_store.offset if cfa.reg == sp
2254            cfa.reg = sp
2255            cfa.base_offset = -cfa_store.offset
2256
2257   Rule 12:
2258   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2259
2260        <reg2>)
2261   effects: cfa.reg = <reg1>
2262            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2263
2264   Rule 13:
2265   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2266   effects: cfa.reg = <reg1>
2267            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2268
2269   Rule 14:
2270   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2271   effects: cfa.reg = <reg1>
2272            cfa.base_offset = -cfa_temp.offset
2273            cfa_temp.offset -= mode_size(mem)
2274
2275   Rule 15:
2276   (set <reg> {unspec, unspec_volatile})
2277   effects: target-dependent
2278
2279   Rule 16:
2280   (set sp (and: sp <const_int>))
2281   constraints: cfa_store.reg == sp
2282   effects: current_fde.stack_realign = 1
2283            cfa_store.offset = 0
2284            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2285
2286   Rule 17:
2287   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2288   effects: cfa_store.offset += -/+ mode_size(mem)
2289
2290   Rule 18:
2291   (set (mem ({pre_inc, pre_dec} sp)) fp)
2292   constraints: fde->stack_realign == 1
2293   effects: cfa_store.offset = 0
2294            cfa.reg != HARD_FRAME_POINTER_REGNUM
2295
2296   Rule 19:
2297   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2298   constraints: fde->stack_realign == 1
2299                && cfa.offset == 0
2300                && cfa.indirect == 0
2301                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2302   effects: Use DW_CFA_def_cfa_expression to define cfa
2303            cfa.reg == fde->drap_reg  */
2304
2305 static void
2306 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2307 {
2308   rtx src, dest, span;
2309   HOST_WIDE_INT offset;
2310   dw_fde_ref fde;
2311
2312   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2313      the PARALLEL independently. The first element is always processed if
2314      it is a SET. This is for backward compatibility.   Other elements
2315      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2316      flag is set in them.  */
2317   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2318     {
2319       int par_index;
2320       int limit = XVECLEN (expr, 0);
2321       rtx elem;
2322
2323       /* PARALLELs have strict read-modify-write semantics, so we
2324          ought to evaluate every rvalue before changing any lvalue.
2325          It's cumbersome to do that in general, but there's an
2326          easy approximation that is enough for all current users:
2327          handle register saves before register assignments.  */
2328       if (GET_CODE (expr) == PARALLEL)
2329         for (par_index = 0; par_index < limit; par_index++)
2330           {
2331             elem = XVECEXP (expr, 0, par_index);
2332             if (GET_CODE (elem) == SET
2333                 && MEM_P (SET_DEST (elem))
2334                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2335               dwarf2out_frame_debug_expr (elem, label);
2336           }
2337
2338       for (par_index = 0; par_index < limit; par_index++)
2339         {
2340           elem = XVECEXP (expr, 0, par_index);
2341           if (GET_CODE (elem) == SET
2342               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2343               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2344             dwarf2out_frame_debug_expr (elem, label);
2345           else if (GET_CODE (elem) == SET
2346                    && par_index != 0
2347                    && !RTX_FRAME_RELATED_P (elem))
2348             {
2349               /* Stack adjustment combining might combine some post-prologue
2350                  stack adjustment into a prologue stack adjustment.  */
2351               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2352
2353               if (offset != 0)
2354                 dwarf2out_stack_adjust (offset, label);
2355             }
2356         }
2357       return;
2358     }
2359
2360   gcc_assert (GET_CODE (expr) == SET);
2361
2362   src = SET_SRC (expr);
2363   dest = SET_DEST (expr);
2364
2365   if (REG_P (src))
2366     {
2367       rtx rsi = reg_saved_in (src);
2368       if (rsi)
2369         src = rsi;
2370     }
2371
2372   fde = current_fde ();
2373
2374   switch (GET_CODE (dest))
2375     {
2376     case REG:
2377       switch (GET_CODE (src))
2378         {
2379           /* Setting FP from SP.  */
2380         case REG:
2381           if (cfa.reg == (unsigned) REGNO (src))
2382             {
2383               /* Rule 1 */
2384               /* Update the CFA rule wrt SP or FP.  Make sure src is
2385                  relative to the current CFA register.
2386
2387                  We used to require that dest be either SP or FP, but the
2388                  ARM copies SP to a temporary register, and from there to
2389                  FP.  So we just rely on the backends to only set
2390                  RTX_FRAME_RELATED_P on appropriate insns.  */
2391               cfa.reg = REGNO (dest);
2392               cfa_temp.reg = cfa.reg;
2393               cfa_temp.offset = cfa.offset;
2394             }
2395           else
2396             {
2397               /* Saving a register in a register.  */
2398               gcc_assert (!fixed_regs [REGNO (dest)]
2399                           /* For the SPARC and its register window.  */
2400                           || (DWARF_FRAME_REGNUM (REGNO (src))
2401                               == DWARF_FRAME_RETURN_COLUMN));
2402
2403               /* After stack is aligned, we can only save SP in FP
2404                  if drap register is used.  In this case, we have
2405                  to restore stack pointer with the CFA value and we
2406                  don't generate this DWARF information.  */
2407               if (fde
2408                   && fde->stack_realign
2409                   && REGNO (src) == STACK_POINTER_REGNUM)
2410                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2411                             && fde->drap_reg != INVALID_REGNUM
2412                             && cfa.reg != REGNO (src));
2413               else
2414                 queue_reg_save (label, src, dest, 0);
2415             }
2416           break;
2417
2418         case PLUS:
2419         case MINUS:
2420         case LO_SUM:
2421           if (dest == stack_pointer_rtx)
2422             {
2423               /* Rule 2 */
2424               /* Adjusting SP.  */
2425               switch (GET_CODE (XEXP (src, 1)))
2426                 {
2427                 case CONST_INT:
2428                   offset = INTVAL (XEXP (src, 1));
2429                   break;
2430                 case REG:
2431                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2432                               == cfa_temp.reg);
2433                   offset = cfa_temp.offset;
2434                   break;
2435                 default:
2436                   gcc_unreachable ();
2437                 }
2438
2439               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2440                 {
2441                   /* Restoring SP from FP in the epilogue.  */
2442                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2443                   cfa.reg = STACK_POINTER_REGNUM;
2444                 }
2445               else if (GET_CODE (src) == LO_SUM)
2446                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2447                 ;
2448               else
2449                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2450
2451               if (GET_CODE (src) != MINUS)
2452                 offset = -offset;
2453               if (cfa.reg == STACK_POINTER_REGNUM)
2454                 cfa.offset += offset;
2455               if (cfa_store.reg == STACK_POINTER_REGNUM)
2456                 cfa_store.offset += offset;
2457             }
2458           else if (dest == hard_frame_pointer_rtx)
2459             {
2460               /* Rule 3 */
2461               /* Either setting the FP from an offset of the SP,
2462                  or adjusting the FP */
2463               gcc_assert (frame_pointer_needed);
2464
2465               gcc_assert (REG_P (XEXP (src, 0))
2466                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2467                           && CONST_INT_P (XEXP (src, 1)));
2468               offset = INTVAL (XEXP (src, 1));
2469               if (GET_CODE (src) != MINUS)
2470                 offset = -offset;
2471               cfa.offset += offset;
2472               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2473             }
2474           else
2475             {
2476               gcc_assert (GET_CODE (src) != MINUS);
2477
2478               /* Rule 4 */
2479               if (REG_P (XEXP (src, 0))
2480                   && REGNO (XEXP (src, 0)) == cfa.reg
2481                   && CONST_INT_P (XEXP (src, 1)))
2482                 {
2483                   /* Setting a temporary CFA register that will be copied
2484                      into the FP later on.  */
2485                   offset = - INTVAL (XEXP (src, 1));
2486                   cfa.offset += offset;
2487                   cfa.reg = REGNO (dest);
2488                   /* Or used to save regs to the stack.  */
2489                   cfa_temp.reg = cfa.reg;
2490                   cfa_temp.offset = cfa.offset;
2491                 }
2492
2493               /* Rule 5 */
2494               else if (REG_P (XEXP (src, 0))
2495                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2496                        && XEXP (src, 1) == stack_pointer_rtx)
2497                 {
2498                   /* Setting a scratch register that we will use instead
2499                      of SP for saving registers to the stack.  */
2500                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2501                   cfa_store.reg = REGNO (dest);
2502                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2503                 }
2504
2505               /* Rule 9 */
2506               else if (GET_CODE (src) == LO_SUM
2507                        && CONST_INT_P (XEXP (src, 1)))
2508                 {
2509                   cfa_temp.reg = REGNO (dest);
2510                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2511                 }
2512               else
2513                 gcc_unreachable ();
2514             }
2515           break;
2516
2517           /* Rule 6 */
2518         case CONST_INT:
2519           cfa_temp.reg = REGNO (dest);
2520           cfa_temp.offset = INTVAL (src);
2521           break;
2522
2523           /* Rule 7 */
2524         case IOR:
2525           gcc_assert (REG_P (XEXP (src, 0))
2526                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2527                       && CONST_INT_P (XEXP (src, 1)));
2528
2529           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2530             cfa_temp.reg = REGNO (dest);
2531           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2532           break;
2533
2534           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2535              which will fill in all of the bits.  */
2536           /* Rule 8 */
2537         case HIGH:
2538           break;
2539
2540           /* Rule 15 */
2541         case UNSPEC:
2542         case UNSPEC_VOLATILE:
2543           gcc_assert (targetm.dwarf_handle_frame_unspec);
2544           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2545           return;
2546
2547           /* Rule 16 */
2548         case AND:
2549           /* If this AND operation happens on stack pointer in prologue,
2550              we assume the stack is realigned and we extract the
2551              alignment.  */
2552           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2553             {
2554               /* We interpret reg_save differently with stack_realign set.
2555                  Thus we must flush whatever we have queued first.  */
2556               dwarf2out_flush_queued_reg_saves ();
2557
2558               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2559               fde->stack_realign = 1;
2560               fde->stack_realignment = INTVAL (XEXP (src, 1));
2561               cfa_store.offset = 0;
2562
2563               if (cfa.reg != STACK_POINTER_REGNUM
2564                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2565                 fde->drap_reg = cfa.reg;
2566             }
2567           return;
2568
2569         default:
2570           gcc_unreachable ();
2571         }
2572
2573       def_cfa_1 (label, &cfa);
2574       break;
2575
2576     case MEM:
2577
2578       /* Saving a register to the stack.  Make sure dest is relative to the
2579          CFA register.  */
2580       switch (GET_CODE (XEXP (dest, 0)))
2581         {
2582           /* Rule 10 */
2583           /* With a push.  */
2584         case PRE_MODIFY:
2585           /* We can't handle variable size modifications.  */
2586           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2587                       == CONST_INT);
2588           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2589
2590           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2591                       && cfa_store.reg == STACK_POINTER_REGNUM);
2592
2593           cfa_store.offset += offset;
2594           if (cfa.reg == STACK_POINTER_REGNUM)
2595             cfa.offset = cfa_store.offset;
2596
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 /* Create a label for an indirect string node, ensuring it is going to
7680    be output, unless its reference count goes down to zero.  */
7681
7682 static inline void
7683 gen_label_for_indirect_string (struct indirect_string_node *node)
7684 {
7685   char label[32];
7686
7687   if (node->label)
7688     return;
7689
7690   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7691   ++dw2_string_counter;
7692   node->label = xstrdup (label);
7693 }
7694
7695 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7696    debug string STR.  */
7697
7698 static inline rtx
7699 get_debug_string_label (const char *str)
7700 {
7701   struct indirect_string_node *node = find_AT_string (str);
7702
7703   debug_str_hash_forced = true;
7704
7705   gen_label_for_indirect_string (node);
7706
7707   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7708 }
7709
7710 static inline const char *
7711 AT_string (dw_attr_ref a)
7712 {
7713   gcc_assert (a && AT_class (a) == dw_val_class_str);
7714   return a->dw_attr_val.v.val_str->str;
7715 }
7716
7717 /* Find out whether a string should be output inline in DIE
7718    or out-of-line in .debug_str section.  */
7719
7720 static enum dwarf_form
7721 AT_string_form (dw_attr_ref a)
7722 {
7723   struct indirect_string_node *node;
7724   unsigned int len;
7725
7726   gcc_assert (a && AT_class (a) == dw_val_class_str);
7727
7728   node = a->dw_attr_val.v.val_str;
7729   if (node->form)
7730     return node->form;
7731
7732   len = strlen (node->str) + 1;
7733
7734   /* If the string is shorter or equal to the size of the reference, it is
7735      always better to put it inline.  */
7736   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7737     return node->form = DW_FORM_string;
7738
7739   /* If we cannot expect the linker to merge strings in .debug_str
7740      section, only put it into .debug_str if it is worth even in this
7741      single module.  */
7742   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7743       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7744       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7745     return node->form = DW_FORM_string;
7746
7747   gen_label_for_indirect_string (node);
7748
7749   return node->form = DW_FORM_strp;
7750 }
7751
7752 /* Add a DIE reference attribute value to a DIE.  */
7753
7754 static inline void
7755 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7756 {
7757   dw_attr_node attr;
7758
7759 #ifdef ENABLE_CHECKING
7760   gcc_assert (targ_die != NULL);
7761 #else
7762   /* With LTO we can end up trying to reference something we didn't create
7763      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7764   if (targ_die == NULL)
7765     return;
7766 #endif
7767
7768   attr.dw_attr = attr_kind;
7769   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7770   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7771   attr.dw_attr_val.v.val_die_ref.external = 0;
7772   add_dwarf_attr (die, &attr);
7773 }
7774
7775 /* Add an AT_specification attribute to a DIE, and also make the back
7776    pointer from the specification to the definition.  */
7777
7778 static inline void
7779 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7780 {
7781   add_AT_die_ref (die, DW_AT_specification, targ_die);
7782   gcc_assert (!targ_die->die_definition);
7783   targ_die->die_definition = die;
7784 }
7785
7786 static inline dw_die_ref
7787 AT_ref (dw_attr_ref a)
7788 {
7789   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7790   return a->dw_attr_val.v.val_die_ref.die;
7791 }
7792
7793 static inline int
7794 AT_ref_external (dw_attr_ref a)
7795 {
7796   if (a && AT_class (a) == dw_val_class_die_ref)
7797     return a->dw_attr_val.v.val_die_ref.external;
7798
7799   return 0;
7800 }
7801
7802 static inline void
7803 set_AT_ref_external (dw_attr_ref a, int i)
7804 {
7805   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7806   a->dw_attr_val.v.val_die_ref.external = i;
7807 }
7808
7809 /* Add an FDE reference attribute value to a DIE.  */
7810
7811 static inline void
7812 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7813 {
7814   dw_attr_node attr;
7815
7816   attr.dw_attr = attr_kind;
7817   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7818   attr.dw_attr_val.v.val_fde_index = targ_fde;
7819   add_dwarf_attr (die, &attr);
7820 }
7821
7822 /* Add a location description attribute value to a DIE.  */
7823
7824 static inline void
7825 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7826 {
7827   dw_attr_node attr;
7828
7829   attr.dw_attr = attr_kind;
7830   attr.dw_attr_val.val_class = dw_val_class_loc;
7831   attr.dw_attr_val.v.val_loc = loc;
7832   add_dwarf_attr (die, &attr);
7833 }
7834
7835 static inline dw_loc_descr_ref
7836 AT_loc (dw_attr_ref a)
7837 {
7838   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7839   return a->dw_attr_val.v.val_loc;
7840 }
7841
7842 static inline void
7843 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7844 {
7845   dw_attr_node attr;
7846
7847   attr.dw_attr = attr_kind;
7848   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7849   attr.dw_attr_val.v.val_loc_list = loc_list;
7850   add_dwarf_attr (die, &attr);
7851   have_location_lists = true;
7852 }
7853
7854 static inline dw_loc_list_ref
7855 AT_loc_list (dw_attr_ref a)
7856 {
7857   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7858   return a->dw_attr_val.v.val_loc_list;
7859 }
7860
7861 static inline dw_loc_list_ref *
7862 AT_loc_list_ptr (dw_attr_ref a)
7863 {
7864   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7865   return &a->dw_attr_val.v.val_loc_list;
7866 }
7867
7868 /* Add an address constant attribute value to a DIE.  */
7869
7870 static inline void
7871 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7872 {
7873   dw_attr_node attr;
7874
7875   attr.dw_attr = attr_kind;
7876   attr.dw_attr_val.val_class = dw_val_class_addr;
7877   attr.dw_attr_val.v.val_addr = addr;
7878   add_dwarf_attr (die, &attr);
7879 }
7880
7881 /* Get the RTX from to an address DIE attribute.  */
7882
7883 static inline rtx
7884 AT_addr (dw_attr_ref a)
7885 {
7886   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7887   return a->dw_attr_val.v.val_addr;
7888 }
7889
7890 /* Add a file attribute value to a DIE.  */
7891
7892 static inline void
7893 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7894              struct dwarf_file_data *fd)
7895 {
7896   dw_attr_node attr;
7897
7898   attr.dw_attr = attr_kind;
7899   attr.dw_attr_val.val_class = dw_val_class_file;
7900   attr.dw_attr_val.v.val_file = fd;
7901   add_dwarf_attr (die, &attr);
7902 }
7903
7904 /* Get the dwarf_file_data from a file DIE attribute.  */
7905
7906 static inline struct dwarf_file_data *
7907 AT_file (dw_attr_ref a)
7908 {
7909   gcc_assert (a && AT_class (a) == dw_val_class_file);
7910   return a->dw_attr_val.v.val_file;
7911 }
7912
7913 /* Add a vms delta attribute value to a DIE.  */
7914
7915 static inline void
7916 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7917                   const char *lbl1, const char *lbl2)
7918 {
7919   dw_attr_node attr;
7920
7921   attr.dw_attr = attr_kind;
7922   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7923   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7924   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7925   add_dwarf_attr (die, &attr);
7926 }
7927
7928 /* Add a label identifier attribute value to a DIE.  */
7929
7930 static inline void
7931 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7932 {
7933   dw_attr_node attr;
7934
7935   attr.dw_attr = attr_kind;
7936   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7937   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7938   add_dwarf_attr (die, &attr);
7939 }
7940
7941 /* Add a section offset attribute value to a DIE, an offset into the
7942    debug_line section.  */
7943
7944 static inline void
7945 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7946                 const char *label)
7947 {
7948   dw_attr_node attr;
7949
7950   attr.dw_attr = attr_kind;
7951   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7952   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7953   add_dwarf_attr (die, &attr);
7954 }
7955
7956 /* Add a section offset attribute value to a DIE, an offset into the
7957    debug_macinfo section.  */
7958
7959 static inline void
7960 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7961                const char *label)
7962 {
7963   dw_attr_node attr;
7964
7965   attr.dw_attr = attr_kind;
7966   attr.dw_attr_val.val_class = dw_val_class_macptr;
7967   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7968   add_dwarf_attr (die, &attr);
7969 }
7970
7971 /* Add an offset attribute value to a DIE.  */
7972
7973 static inline void
7974 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7975                unsigned HOST_WIDE_INT offset)
7976 {
7977   dw_attr_node attr;
7978
7979   attr.dw_attr = attr_kind;
7980   attr.dw_attr_val.val_class = dw_val_class_offset;
7981   attr.dw_attr_val.v.val_offset = offset;
7982   add_dwarf_attr (die, &attr);
7983 }
7984
7985 /* Add an range_list attribute value to a DIE.  */
7986
7987 static void
7988 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7989                    long unsigned int offset)
7990 {
7991   dw_attr_node attr;
7992
7993   attr.dw_attr = attr_kind;
7994   attr.dw_attr_val.val_class = dw_val_class_range_list;
7995   attr.dw_attr_val.v.val_offset = offset;
7996   add_dwarf_attr (die, &attr);
7997 }
7998
7999 /* Return the start label of a delta attribute.  */
8000
8001 static inline const char *
8002 AT_vms_delta1 (dw_attr_ref a)
8003 {
8004   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
8005   return a->dw_attr_val.v.val_vms_delta.lbl1;
8006 }
8007
8008 /* Return the end label of a delta attribute.  */
8009
8010 static inline const char *
8011 AT_vms_delta2 (dw_attr_ref a)
8012 {
8013   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
8014   return a->dw_attr_val.v.val_vms_delta.lbl2;
8015 }
8016
8017 static inline const char *
8018 AT_lbl (dw_attr_ref a)
8019 {
8020   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
8021                     || AT_class (a) == dw_val_class_lineptr
8022                     || AT_class (a) == dw_val_class_macptr));
8023   return a->dw_attr_val.v.val_lbl_id;
8024 }
8025
8026 /* Get the attribute of type attr_kind.  */
8027
8028 static dw_attr_ref
8029 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8030 {
8031   dw_attr_ref a;
8032   unsigned ix;
8033   dw_die_ref spec = NULL;
8034
8035   if (! die)
8036     return NULL;
8037
8038   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8039     if (a->dw_attr == attr_kind)
8040       return a;
8041     else if (a->dw_attr == DW_AT_specification
8042              || a->dw_attr == DW_AT_abstract_origin)
8043       spec = AT_ref (a);
8044
8045   if (spec)
8046     return get_AT (spec, attr_kind);
8047
8048   return NULL;
8049 }
8050
8051 /* Return the "low pc" attribute value, typically associated with a subprogram
8052    DIE.  Return null if the "low pc" attribute is either not present, or if it
8053    cannot be represented as an assembler label identifier.  */
8054
8055 static inline const char *
8056 get_AT_low_pc (dw_die_ref die)
8057 {
8058   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8059
8060   return a ? AT_lbl (a) : NULL;
8061 }
8062
8063 /* Return the "high pc" attribute value, typically associated with a subprogram
8064    DIE.  Return null if the "high pc" attribute is either not present, or if it
8065    cannot be represented as an assembler label identifier.  */
8066
8067 static inline const char *
8068 get_AT_hi_pc (dw_die_ref die)
8069 {
8070   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8071
8072   return a ? AT_lbl (a) : NULL;
8073 }
8074
8075 /* Return the value of the string attribute designated by ATTR_KIND, or
8076    NULL if it is not present.  */
8077
8078 static inline const char *
8079 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8080 {
8081   dw_attr_ref a = get_AT (die, attr_kind);
8082
8083   return a ? AT_string (a) : NULL;
8084 }
8085
8086 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
8087    if it is not present.  */
8088
8089 static inline int
8090 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
8091 {
8092   dw_attr_ref a = get_AT (die, attr_kind);
8093
8094   return a ? AT_flag (a) : 0;
8095 }
8096
8097 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
8098    if it is not present.  */
8099
8100 static inline unsigned
8101 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
8102 {
8103   dw_attr_ref a = get_AT (die, attr_kind);
8104
8105   return a ? AT_unsigned (a) : 0;
8106 }
8107
8108 static inline dw_die_ref
8109 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
8110 {
8111   dw_attr_ref a = get_AT (die, attr_kind);
8112
8113   return a ? AT_ref (a) : NULL;
8114 }
8115
8116 static inline struct dwarf_file_data *
8117 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
8118 {
8119   dw_attr_ref a = get_AT (die, attr_kind);
8120
8121   return a ? AT_file (a) : NULL;
8122 }
8123
8124 /* Return TRUE if the language is C++.  */
8125
8126 static inline bool
8127 is_cxx (void)
8128 {
8129   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8130
8131   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
8132 }
8133
8134 /* Return TRUE if the language is Fortran.  */
8135
8136 static inline bool
8137 is_fortran (void)
8138 {
8139   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8140
8141   return (lang == DW_LANG_Fortran77
8142           || lang == DW_LANG_Fortran90
8143           || lang == DW_LANG_Fortran95);
8144 }
8145
8146 /* Return TRUE if the language is Ada.  */
8147
8148 static inline bool
8149 is_ada (void)
8150 {
8151   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8152
8153   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
8154 }
8155
8156 /* Remove the specified attribute if present.  */
8157
8158 static void
8159 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8160 {
8161   dw_attr_ref a;
8162   unsigned ix;
8163
8164   if (! die)
8165     return;
8166
8167   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8168     if (a->dw_attr == attr_kind)
8169       {
8170         if (AT_class (a) == dw_val_class_str)
8171           if (a->dw_attr_val.v.val_str->refcount)
8172             a->dw_attr_val.v.val_str->refcount--;
8173
8174         /* VEC_ordered_remove should help reduce the number of abbrevs
8175            that are needed.  */
8176         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8177         return;
8178       }
8179 }
8180
8181 /* Remove CHILD from its parent.  PREV must have the property that
8182    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8183
8184 static void
8185 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8186 {
8187   gcc_assert (child->die_parent == prev->die_parent);
8188   gcc_assert (prev->die_sib == child);
8189   if (prev == child)
8190     {
8191       gcc_assert (child->die_parent->die_child == child);
8192       prev = NULL;
8193     }
8194   else
8195     prev->die_sib = child->die_sib;
8196   if (child->die_parent->die_child == child)
8197     child->die_parent->die_child = prev;
8198 }
8199
8200 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8201    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8202
8203 static void
8204 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8205 {
8206   dw_die_ref parent = old_child->die_parent;
8207
8208   gcc_assert (parent == prev->die_parent);
8209   gcc_assert (prev->die_sib == old_child);
8210
8211   new_child->die_parent = parent;
8212   if (prev == old_child)
8213     {
8214       gcc_assert (parent->die_child == old_child);
8215       new_child->die_sib = new_child;
8216     }
8217   else
8218     {
8219       prev->die_sib = new_child;
8220       new_child->die_sib = old_child->die_sib;
8221     }
8222   if (old_child->die_parent->die_child == old_child)
8223     old_child->die_parent->die_child = new_child;
8224 }
8225
8226 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8227
8228 static void
8229 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8230 {
8231   dw_die_ref c;
8232   new_parent->die_child = old_parent->die_child;
8233   old_parent->die_child = NULL;
8234   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8235 }
8236
8237 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8238    matches TAG.  */
8239
8240 static void
8241 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8242 {
8243   dw_die_ref c;
8244
8245   c = die->die_child;
8246   if (c) do {
8247     dw_die_ref prev = c;
8248     c = c->die_sib;
8249     while (c->die_tag == tag)
8250       {
8251         remove_child_with_prev (c, prev);
8252         /* Might have removed every child.  */
8253         if (c == c->die_sib)
8254           return;
8255         c = c->die_sib;
8256       }
8257   } while (c != die->die_child);
8258 }
8259
8260 /* Add a CHILD_DIE as the last child of DIE.  */
8261
8262 static void
8263 add_child_die (dw_die_ref die, dw_die_ref child_die)
8264 {
8265   /* FIXME this should probably be an assert.  */
8266   if (! die || ! child_die)
8267     return;
8268   gcc_assert (die != child_die);
8269
8270   child_die->die_parent = die;
8271   if (die->die_child)
8272     {
8273       child_die->die_sib = die->die_child->die_sib;
8274       die->die_child->die_sib = child_die;
8275     }
8276   else
8277     child_die->die_sib = child_die;
8278   die->die_child = child_die;
8279 }
8280
8281 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8282    is the specification, to the end of PARENT's list of children.
8283    This is done by removing and re-adding it.  */
8284
8285 static void
8286 splice_child_die (dw_die_ref parent, dw_die_ref child)
8287 {
8288   dw_die_ref p;
8289
8290   /* We want the declaration DIE from inside the class, not the
8291      specification DIE at toplevel.  */
8292   if (child->die_parent != parent)
8293     {
8294       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8295
8296       if (tmp)
8297         child = tmp;
8298     }
8299
8300   gcc_assert (child->die_parent == parent
8301               || (child->die_parent
8302                   == get_AT_ref (parent, DW_AT_specification)));
8303
8304   for (p = child->die_parent->die_child; ; p = p->die_sib)
8305     if (p->die_sib == child)
8306       {
8307         remove_child_with_prev (child, p);
8308         break;
8309       }
8310
8311   add_child_die (parent, child);
8312 }
8313
8314 /* Return a pointer to a newly created DIE node.  */
8315
8316 static inline dw_die_ref
8317 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8318 {
8319   dw_die_ref die = ggc_alloc_cleared_die_node ();
8320
8321   die->die_tag = tag_value;
8322
8323   if (parent_die != NULL)
8324     add_child_die (parent_die, die);
8325   else
8326     {
8327       limbo_die_node *limbo_node;
8328
8329       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8330       limbo_node->die = die;
8331       limbo_node->created_for = t;
8332       limbo_node->next = limbo_die_list;
8333       limbo_die_list = limbo_node;
8334     }
8335
8336   return die;
8337 }
8338
8339 /* Return the DIE associated with the given type specifier.  */
8340
8341 static inline dw_die_ref
8342 lookup_type_die (tree type)
8343 {
8344   return TYPE_SYMTAB_DIE (type);
8345 }
8346
8347 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8348    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8349    anonymous type instead the one of the naming typedef.  */
8350
8351 static inline dw_die_ref
8352 strip_naming_typedef (tree type, dw_die_ref type_die)
8353 {
8354   if (type
8355       && TREE_CODE (type) == RECORD_TYPE
8356       && type_die
8357       && type_die->die_tag == DW_TAG_typedef
8358       && is_naming_typedef_decl (TYPE_NAME (type)))
8359     type_die = get_AT_ref (type_die, DW_AT_type);
8360   return type_die;
8361 }
8362
8363 /* Like lookup_type_die, but if type is an anonymous type named by a
8364    typedef[1], return the DIE of the anonymous type instead the one of
8365    the naming typedef.  This is because in gen_typedef_die, we did
8366    equate the anonymous struct named by the typedef with the DIE of
8367    the naming typedef. So by default, lookup_type_die on an anonymous
8368    struct yields the DIE of the naming typedef.
8369
8370    [1]: Read the comment of is_naming_typedef_decl to learn about what
8371    a naming typedef is.  */
8372
8373 static inline dw_die_ref
8374 lookup_type_die_strip_naming_typedef (tree type)
8375 {
8376   dw_die_ref die = lookup_type_die (type);
8377   return strip_naming_typedef (type, die);
8378 }
8379
8380 /* Equate a DIE to a given type specifier.  */
8381
8382 static inline void
8383 equate_type_number_to_die (tree type, dw_die_ref type_die)
8384 {
8385   TYPE_SYMTAB_DIE (type) = type_die;
8386 }
8387
8388 /* Returns a hash value for X (which really is a die_struct).  */
8389
8390 static hashval_t
8391 decl_die_table_hash (const void *x)
8392 {
8393   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8394 }
8395
8396 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8397
8398 static int
8399 decl_die_table_eq (const void *x, const void *y)
8400 {
8401   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8402 }
8403
8404 /* Return the DIE associated with a given declaration.  */
8405
8406 static inline dw_die_ref
8407 lookup_decl_die (tree decl)
8408 {
8409   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8410 }
8411
8412 /* Returns a hash value for X (which really is a var_loc_list).  */
8413
8414 static hashval_t
8415 decl_loc_table_hash (const void *x)
8416 {
8417   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8418 }
8419
8420 /* Return nonzero if decl_id of var_loc_list X is the same as
8421    UID of decl *Y.  */
8422
8423 static int
8424 decl_loc_table_eq (const void *x, const void *y)
8425 {
8426   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8427 }
8428
8429 /* Return the var_loc list associated with a given declaration.  */
8430
8431 static inline var_loc_list *
8432 lookup_decl_loc (const_tree decl)
8433 {
8434   if (!decl_loc_table)
8435     return NULL;
8436   return (var_loc_list *)
8437     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8438 }
8439
8440 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8441
8442 static hashval_t
8443 cached_dw_loc_list_table_hash (const void *x)
8444 {
8445   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8446 }
8447
8448 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8449    UID of decl *Y.  */
8450
8451 static int
8452 cached_dw_loc_list_table_eq (const void *x, const void *y)
8453 {
8454   return (((const cached_dw_loc_list *) x)->decl_id
8455           == DECL_UID ((const_tree) y));
8456 }
8457
8458 /* Equate a DIE to a particular declaration.  */
8459
8460 static void
8461 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8462 {
8463   unsigned int decl_id = DECL_UID (decl);
8464   void **slot;
8465
8466   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8467   *slot = decl_die;
8468   decl_die->decl_id = decl_id;
8469 }
8470
8471 /* Return how many bits covers PIECE EXPR_LIST.  */
8472
8473 static int
8474 decl_piece_bitsize (rtx piece)
8475 {
8476   int ret = (int) GET_MODE (piece);
8477   if (ret)
8478     return ret;
8479   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8480               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8481   return INTVAL (XEXP (XEXP (piece, 0), 0));
8482 }
8483
8484 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8485
8486 static rtx *
8487 decl_piece_varloc_ptr (rtx piece)
8488 {
8489   if ((int) GET_MODE (piece))
8490     return &XEXP (piece, 0);
8491   else
8492     return &XEXP (XEXP (piece, 0), 1);
8493 }
8494
8495 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8496    Next is the chain of following piece nodes.  */
8497
8498 static rtx
8499 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8500 {
8501   if (bitsize <= (int) MAX_MACHINE_MODE)
8502     return alloc_EXPR_LIST (bitsize, loc_note, next);
8503   else
8504     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8505                                                GEN_INT (bitsize),
8506                                                loc_note), next);
8507 }
8508
8509 /* Return rtx that should be stored into loc field for
8510    LOC_NOTE and BITPOS/BITSIZE.  */
8511
8512 static rtx
8513 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8514                       HOST_WIDE_INT bitsize)
8515 {
8516   if (bitsize != -1)
8517     {
8518       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8519       if (bitpos != 0)
8520         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8521     }
8522   return loc_note;
8523 }
8524
8525 /* This function either modifies location piece list *DEST in
8526    place (if SRC and INNER is NULL), or copies location piece list
8527    *SRC to *DEST while modifying it.  Location BITPOS is modified
8528    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8529    not copied and if needed some padding around it is added.
8530    When modifying in place, DEST should point to EXPR_LIST where
8531    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8532    to the start of the whole list and INNER points to the EXPR_LIST
8533    where earlier pieces cover PIECE_BITPOS bits.  */
8534
8535 static void
8536 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8537                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8538                    HOST_WIDE_INT bitsize, rtx loc_note)
8539 {
8540   int diff;
8541   bool copy = inner != NULL;
8542
8543   if (copy)
8544     {
8545       /* First copy all nodes preceeding the current bitpos.  */
8546       while (src != inner)
8547         {
8548           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8549                                    decl_piece_bitsize (*src), NULL_RTX);
8550           dest = &XEXP (*dest, 1);
8551           src = &XEXP (*src, 1);
8552         }
8553     }
8554   /* Add padding if needed.  */
8555   if (bitpos != piece_bitpos)
8556     {
8557       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8558                                copy ? NULL_RTX : *dest);
8559       dest = &XEXP (*dest, 1);
8560     }
8561   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8562     {
8563       gcc_assert (!copy);
8564       /* A piece with correct bitpos and bitsize already exist,
8565          just update the location for it and return.  */
8566       *decl_piece_varloc_ptr (*dest) = loc_note;
8567       return;
8568     }
8569   /* Add the piece that changed.  */
8570   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8571   dest = &XEXP (*dest, 1);
8572   /* Skip over pieces that overlap it.  */
8573   diff = bitpos - piece_bitpos + bitsize;
8574   if (!copy)
8575     src = dest;
8576   while (diff > 0 && *src)
8577     {
8578       rtx piece = *src;
8579       diff -= decl_piece_bitsize (piece);
8580       if (copy)
8581         src = &XEXP (piece, 1);
8582       else
8583         {
8584           *src = XEXP (piece, 1);
8585           free_EXPR_LIST_node (piece);
8586         }
8587     }
8588   /* Add padding if needed.  */
8589   if (diff < 0 && *src)
8590     {
8591       if (!copy)
8592         dest = src;
8593       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8594       dest = &XEXP (*dest, 1);
8595     }
8596   if (!copy)
8597     return;
8598   /* Finally copy all nodes following it.  */
8599   while (*src)
8600     {
8601       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8602                                decl_piece_bitsize (*src), NULL_RTX);
8603       dest = &XEXP (*dest, 1);
8604       src = &XEXP (*src, 1);
8605     }
8606 }
8607
8608 /* Add a variable location node to the linked list for DECL.  */
8609
8610 static struct var_loc_node *
8611 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8612 {
8613   unsigned int decl_id;
8614   var_loc_list *temp;
8615   void **slot;
8616   struct var_loc_node *loc = NULL;
8617   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8618
8619   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8620     {
8621       tree realdecl = DECL_DEBUG_EXPR (decl);
8622       if (realdecl && handled_component_p (realdecl))
8623         {
8624           HOST_WIDE_INT maxsize;
8625           tree innerdecl;
8626           innerdecl
8627             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8628           if (!DECL_P (innerdecl)
8629               || DECL_IGNORED_P (innerdecl)
8630               || TREE_STATIC (innerdecl)
8631               || bitsize <= 0
8632               || bitpos + bitsize > 256
8633               || bitsize != maxsize)
8634             return NULL;
8635           decl = innerdecl;
8636         }
8637     }
8638
8639   decl_id = DECL_UID (decl);
8640   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8641   if (*slot == NULL)
8642     {
8643       temp = ggc_alloc_cleared_var_loc_list ();
8644       temp->decl_id = decl_id;
8645       *slot = temp;
8646     }
8647   else
8648     temp = (var_loc_list *) *slot;
8649
8650   if (temp->last)
8651     {
8652       struct var_loc_node *last = temp->last, *unused = NULL;
8653       rtx *piece_loc = NULL, last_loc_note;
8654       int piece_bitpos = 0;
8655       if (last->next)
8656         {
8657           last = last->next;
8658           gcc_assert (last->next == NULL);
8659         }
8660       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8661         {
8662           piece_loc = &last->loc;
8663           do
8664             {
8665               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8666               if (piece_bitpos + cur_bitsize > bitpos)
8667                 break;
8668               piece_bitpos += cur_bitsize;
8669               piece_loc = &XEXP (*piece_loc, 1);
8670             }
8671           while (*piece_loc);
8672         }
8673       /* TEMP->LAST here is either pointer to the last but one or
8674          last element in the chained list, LAST is pointer to the
8675          last element.  */
8676       if (label && strcmp (last->label, label) == 0)
8677         {
8678           /* For SRA optimized variables if there weren't any real
8679              insns since last note, just modify the last node.  */
8680           if (piece_loc != NULL)
8681             {
8682               adjust_piece_list (piece_loc, NULL, NULL,
8683                                  bitpos, piece_bitpos, bitsize, loc_note);
8684               return NULL;
8685             }
8686           /* If the last note doesn't cover any instructions, remove it.  */
8687           if (temp->last != last)
8688             {
8689               temp->last->next = NULL;
8690               unused = last;
8691               last = temp->last;
8692               gcc_assert (strcmp (last->label, label) != 0);
8693             }
8694           else
8695             {
8696               gcc_assert (temp->first == temp->last);
8697               memset (temp->last, '\0', sizeof (*temp->last));
8698               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8699               return temp->last;
8700             }
8701         }
8702       if (bitsize == -1 && NOTE_P (last->loc))
8703         last_loc_note = last->loc;
8704       else if (piece_loc != NULL
8705                && *piece_loc != NULL_RTX
8706                && piece_bitpos == bitpos
8707                && decl_piece_bitsize (*piece_loc) == bitsize)
8708         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8709       else
8710         last_loc_note = NULL_RTX;
8711       /* If the current location is the same as the end of the list,
8712          and either both or neither of the locations is uninitialized,
8713          we have nothing to do.  */
8714       if (last_loc_note == NULL_RTX
8715           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8716                             NOTE_VAR_LOCATION_LOC (loc_note)))
8717           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8718                != NOTE_VAR_LOCATION_STATUS (loc_note))
8719               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8720                    == VAR_INIT_STATUS_UNINITIALIZED)
8721                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8722                       == VAR_INIT_STATUS_UNINITIALIZED))))
8723         {
8724           /* Add LOC to the end of list and update LAST.  If the last
8725              element of the list has been removed above, reuse its
8726              memory for the new node, otherwise allocate a new one.  */
8727           if (unused)
8728             {
8729               loc = unused;
8730               memset (loc, '\0', sizeof (*loc));
8731             }
8732           else
8733             loc = ggc_alloc_cleared_var_loc_node ();
8734           if (bitsize == -1 || piece_loc == NULL)
8735             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8736           else
8737             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8738                                bitpos, piece_bitpos, bitsize, loc_note);
8739           last->next = loc;
8740           /* Ensure TEMP->LAST will point either to the new last but one
8741              element of the chain, or to the last element in it.  */
8742           if (last != temp->last)
8743             temp->last = last;
8744         }
8745       else if (unused)
8746         ggc_free (unused);
8747     }
8748   else
8749     {
8750       loc = ggc_alloc_cleared_var_loc_node ();
8751       temp->first = loc;
8752       temp->last = loc;
8753       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8754     }
8755   return loc;
8756 }
8757 \f
8758 /* Keep track of the number of spaces used to indent the
8759    output of the debugging routines that print the structure of
8760    the DIE internal representation.  */
8761 static int print_indent;
8762
8763 /* Indent the line the number of spaces given by print_indent.  */
8764
8765 static inline void
8766 print_spaces (FILE *outfile)
8767 {
8768   fprintf (outfile, "%*s", print_indent, "");
8769 }
8770
8771 /* Print a type signature in hex.  */
8772
8773 static inline void
8774 print_signature (FILE *outfile, char *sig)
8775 {
8776   int i;
8777
8778   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8779     fprintf (outfile, "%02x", sig[i] & 0xff);
8780 }
8781
8782 /* Print the information associated with a given DIE, and its children.
8783    This routine is a debugging aid only.  */
8784
8785 static void
8786 print_die (dw_die_ref die, FILE *outfile)
8787 {
8788   dw_attr_ref a;
8789   dw_die_ref c;
8790   unsigned ix;
8791
8792   print_spaces (outfile);
8793   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8794            die->die_offset, dwarf_tag_name (die->die_tag),
8795            (void*) die);
8796   print_spaces (outfile);
8797   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8798   fprintf (outfile, " offset: %ld", die->die_offset);
8799   fprintf (outfile, " mark: %d\n", die->die_mark);
8800
8801   if (use_debug_types && die->die_id.die_type_node)
8802     {
8803       print_spaces (outfile);
8804       fprintf (outfile, "  signature: ");
8805       print_signature (outfile, die->die_id.die_type_node->signature);
8806       fprintf (outfile, "\n");
8807     }
8808
8809   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8810     {
8811       print_spaces (outfile);
8812       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8813
8814       switch (AT_class (a))
8815         {
8816         case dw_val_class_addr:
8817           fprintf (outfile, "address");
8818           break;
8819         case dw_val_class_offset:
8820           fprintf (outfile, "offset");
8821           break;
8822         case dw_val_class_loc:
8823           fprintf (outfile, "location descriptor");
8824           break;
8825         case dw_val_class_loc_list:
8826           fprintf (outfile, "location list -> label:%s",
8827                    AT_loc_list (a)->ll_symbol);
8828           break;
8829         case dw_val_class_range_list:
8830           fprintf (outfile, "range list");
8831           break;
8832         case dw_val_class_const:
8833           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8834           break;
8835         case dw_val_class_unsigned_const:
8836           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8837           break;
8838         case dw_val_class_const_double:
8839           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8840                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8841                    a->dw_attr_val.v.val_double.high,
8842                    a->dw_attr_val.v.val_double.low);
8843           break;
8844         case dw_val_class_vec:
8845           fprintf (outfile, "floating-point or vector constant");
8846           break;
8847         case dw_val_class_flag:
8848           fprintf (outfile, "%u", AT_flag (a));
8849           break;
8850         case dw_val_class_die_ref:
8851           if (AT_ref (a) != NULL)
8852             {
8853               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8854                 {
8855                   fprintf (outfile, "die -> signature: ");
8856                   print_signature (outfile,
8857                                    AT_ref (a)->die_id.die_type_node->signature);
8858                 }
8859               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8860                 fprintf (outfile, "die -> label: %s",
8861                          AT_ref (a)->die_id.die_symbol);
8862               else
8863                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8864               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8865             }
8866           else
8867             fprintf (outfile, "die -> <null>");
8868           break;
8869         case dw_val_class_vms_delta:
8870           fprintf (outfile, "delta: @slotcount(%s-%s)",
8871                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8872           break;
8873         case dw_val_class_lbl_id:
8874         case dw_val_class_lineptr:
8875         case dw_val_class_macptr:
8876           fprintf (outfile, "label: %s", AT_lbl (a));
8877           break;
8878         case dw_val_class_str:
8879           if (AT_string (a) != NULL)
8880             fprintf (outfile, "\"%s\"", AT_string (a));
8881           else
8882             fprintf (outfile, "<null>");
8883           break;
8884         case dw_val_class_file:
8885           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8886                    AT_file (a)->emitted_number);
8887           break;
8888         case dw_val_class_data8:
8889           {
8890             int i;
8891
8892             for (i = 0; i < 8; i++)
8893               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8894             break;
8895           }
8896         default:
8897           break;
8898         }
8899
8900       fprintf (outfile, "\n");
8901     }
8902
8903   if (die->die_child != NULL)
8904     {
8905       print_indent += 4;
8906       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8907       print_indent -= 4;
8908     }
8909   if (print_indent == 0)
8910     fprintf (outfile, "\n");
8911 }
8912
8913 /* Print the information collected for a given DIE.  */
8914
8915 DEBUG_FUNCTION void
8916 debug_dwarf_die (dw_die_ref die)
8917 {
8918   print_die (die, stderr);
8919 }
8920
8921 /* Print all DWARF information collected for the compilation unit.
8922    This routine is a debugging aid only.  */
8923
8924 DEBUG_FUNCTION void
8925 debug_dwarf (void)
8926 {
8927   print_indent = 0;
8928   print_die (comp_unit_die (), stderr);
8929 }
8930 \f
8931 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8932    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8933    DIE that marks the start of the DIEs for this include file.  */
8934
8935 static dw_die_ref
8936 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8937 {
8938   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8939   dw_die_ref new_unit = gen_compile_unit_die (filename);
8940
8941   new_unit->die_sib = old_unit;
8942   return new_unit;
8943 }
8944
8945 /* Close an include-file CU and reopen the enclosing one.  */
8946
8947 static dw_die_ref
8948 pop_compile_unit (dw_die_ref old_unit)
8949 {
8950   dw_die_ref new_unit = old_unit->die_sib;
8951
8952   old_unit->die_sib = NULL;
8953   return new_unit;
8954 }
8955
8956 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8957 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8958
8959 /* Calculate the checksum of a location expression.  */
8960
8961 static inline void
8962 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8963 {
8964   int tem;
8965
8966   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8967   CHECKSUM (tem);
8968   CHECKSUM (loc->dw_loc_oprnd1);
8969   CHECKSUM (loc->dw_loc_oprnd2);
8970 }
8971
8972 /* Calculate the checksum of an attribute.  */
8973
8974 static void
8975 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8976 {
8977   dw_loc_descr_ref loc;
8978   rtx r;
8979
8980   CHECKSUM (at->dw_attr);
8981
8982   /* We don't care that this was compiled with a different compiler
8983      snapshot; if the output is the same, that's what matters.  */
8984   if (at->dw_attr == DW_AT_producer)
8985     return;
8986
8987   switch (AT_class (at))
8988     {
8989     case dw_val_class_const:
8990       CHECKSUM (at->dw_attr_val.v.val_int);
8991       break;
8992     case dw_val_class_unsigned_const:
8993       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8994       break;
8995     case dw_val_class_const_double:
8996       CHECKSUM (at->dw_attr_val.v.val_double);
8997       break;
8998     case dw_val_class_vec:
8999       CHECKSUM (at->dw_attr_val.v.val_vec);
9000       break;
9001     case dw_val_class_flag:
9002       CHECKSUM (at->dw_attr_val.v.val_flag);
9003       break;
9004     case dw_val_class_str:
9005       CHECKSUM_STRING (AT_string (at));
9006       break;
9007
9008     case dw_val_class_addr:
9009       r = AT_addr (at);
9010       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9011       CHECKSUM_STRING (XSTR (r, 0));
9012       break;
9013
9014     case dw_val_class_offset:
9015       CHECKSUM (at->dw_attr_val.v.val_offset);
9016       break;
9017
9018     case dw_val_class_loc:
9019       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9020         loc_checksum (loc, ctx);
9021       break;
9022
9023     case dw_val_class_die_ref:
9024       die_checksum (AT_ref (at), ctx, mark);
9025       break;
9026
9027     case dw_val_class_fde_ref:
9028     case dw_val_class_vms_delta:
9029     case dw_val_class_lbl_id:
9030     case dw_val_class_lineptr:
9031     case dw_val_class_macptr:
9032       break;
9033
9034     case dw_val_class_file:
9035       CHECKSUM_STRING (AT_file (at)->filename);
9036       break;
9037
9038     case dw_val_class_data8:
9039       CHECKSUM (at->dw_attr_val.v.val_data8);
9040       break;
9041
9042     default:
9043       break;
9044     }
9045 }
9046
9047 /* Calculate the checksum of a DIE.  */
9048
9049 static void
9050 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9051 {
9052   dw_die_ref c;
9053   dw_attr_ref a;
9054   unsigned ix;
9055
9056   /* To avoid infinite recursion.  */
9057   if (die->die_mark)
9058     {
9059       CHECKSUM (die->die_mark);
9060       return;
9061     }
9062   die->die_mark = ++(*mark);
9063
9064   CHECKSUM (die->die_tag);
9065
9066   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9067     attr_checksum (a, ctx, mark);
9068
9069   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
9070 }
9071
9072 #undef CHECKSUM
9073 #undef CHECKSUM_STRING
9074
9075 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
9076 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9077 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
9078 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
9079 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
9080 #define CHECKSUM_ATTR(FOO) \
9081   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
9082
9083 /* Calculate the checksum of a number in signed LEB128 format.  */
9084
9085 static void
9086 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
9087 {
9088   unsigned char byte;
9089   bool more;
9090
9091   while (1)
9092     {
9093       byte = (value & 0x7f);
9094       value >>= 7;
9095       more = !((value == 0 && (byte & 0x40) == 0)
9096                 || (value == -1 && (byte & 0x40) != 0));
9097       if (more)
9098         byte |= 0x80;
9099       CHECKSUM (byte);
9100       if (!more)
9101         break;
9102     }
9103 }
9104
9105 /* Calculate the checksum of a number in unsigned LEB128 format.  */
9106
9107 static void
9108 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
9109 {
9110   while (1)
9111     {
9112       unsigned char byte = (value & 0x7f);
9113       value >>= 7;
9114       if (value != 0)
9115         /* More bytes to follow.  */
9116         byte |= 0x80;
9117       CHECKSUM (byte);
9118       if (value == 0)
9119         break;
9120     }
9121 }
9122
9123 /* Checksum the context of the DIE.  This adds the names of any
9124    surrounding namespaces or structures to the checksum.  */
9125
9126 static void
9127 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
9128 {
9129   const char *name;
9130   dw_die_ref spec;
9131   int tag = die->die_tag;
9132
9133   if (tag != DW_TAG_namespace
9134       && tag != DW_TAG_structure_type
9135       && tag != DW_TAG_class_type)
9136     return;
9137
9138   name = get_AT_string (die, DW_AT_name);
9139
9140   spec = get_AT_ref (die, DW_AT_specification);
9141   if (spec != NULL)
9142     die = spec;
9143
9144   if (die->die_parent != NULL)
9145     checksum_die_context (die->die_parent, ctx);
9146
9147   CHECKSUM_ULEB128 ('C');
9148   CHECKSUM_ULEB128 (tag);
9149   if (name != NULL)
9150     CHECKSUM_STRING (name);
9151 }
9152
9153 /* Calculate the checksum of a location expression.  */
9154
9155 static inline void
9156 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9157 {
9158   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
9159      were emitted as a DW_FORM_sdata instead of a location expression.  */
9160   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
9161     {
9162       CHECKSUM_ULEB128 (DW_FORM_sdata);
9163       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
9164       return;
9165     }
9166
9167   /* Otherwise, just checksum the raw location expression.  */
9168   while (loc != NULL)
9169     {
9170       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9171       CHECKSUM (loc->dw_loc_oprnd1);
9172       CHECKSUM (loc->dw_loc_oprnd2);
9173       loc = loc->dw_loc_next;
9174     }
9175 }
9176
9177 /* Calculate the checksum of an attribute.  */
9178
9179 static void
9180 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9181                        struct md5_ctx *ctx, int *mark)
9182 {
9183   dw_loc_descr_ref loc;
9184   rtx r;
9185
9186   if (AT_class (at) == dw_val_class_die_ref)
9187     {
9188       dw_die_ref target_die = AT_ref (at);
9189
9190       /* For pointer and reference types, we checksum only the (qualified)
9191          name of the target type (if there is a name).  For friend entries,
9192          we checksum only the (qualified) name of the target type or function.
9193          This allows the checksum to remain the same whether the target type
9194          is complete or not.  */
9195       if ((at->dw_attr == DW_AT_type
9196            && (tag == DW_TAG_pointer_type
9197                || tag == DW_TAG_reference_type
9198                || tag == DW_TAG_rvalue_reference_type
9199                || tag == DW_TAG_ptr_to_member_type))
9200           || (at->dw_attr == DW_AT_friend
9201               && tag == DW_TAG_friend))
9202         {
9203           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9204
9205           if (name_attr != NULL)
9206             {
9207               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9208
9209               if (decl == NULL)
9210                 decl = target_die;
9211               CHECKSUM_ULEB128 ('N');
9212               CHECKSUM_ULEB128 (at->dw_attr);
9213               if (decl->die_parent != NULL)
9214                 checksum_die_context (decl->die_parent, ctx);
9215               CHECKSUM_ULEB128 ('E');
9216               CHECKSUM_STRING (AT_string (name_attr));
9217               return;
9218             }
9219         }
9220
9221       /* For all other references to another DIE, we check to see if the
9222          target DIE has already been visited.  If it has, we emit a
9223          backward reference; if not, we descend recursively.  */
9224       if (target_die->die_mark > 0)
9225         {
9226           CHECKSUM_ULEB128 ('R');
9227           CHECKSUM_ULEB128 (at->dw_attr);
9228           CHECKSUM_ULEB128 (target_die->die_mark);
9229         }
9230       else
9231         {
9232           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9233
9234           if (decl == NULL)
9235             decl = target_die;
9236           target_die->die_mark = ++(*mark);
9237           CHECKSUM_ULEB128 ('T');
9238           CHECKSUM_ULEB128 (at->dw_attr);
9239           if (decl->die_parent != NULL)
9240             checksum_die_context (decl->die_parent, ctx);
9241           die_checksum_ordered (target_die, ctx, mark);
9242         }
9243       return;
9244     }
9245
9246   CHECKSUM_ULEB128 ('A');
9247   CHECKSUM_ULEB128 (at->dw_attr);
9248
9249   switch (AT_class (at))
9250     {
9251     case dw_val_class_const:
9252       CHECKSUM_ULEB128 (DW_FORM_sdata);
9253       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9254       break;
9255
9256     case dw_val_class_unsigned_const:
9257       CHECKSUM_ULEB128 (DW_FORM_sdata);
9258       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9259       break;
9260
9261     case dw_val_class_const_double:
9262       CHECKSUM_ULEB128 (DW_FORM_block);
9263       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9264       CHECKSUM (at->dw_attr_val.v.val_double);
9265       break;
9266
9267     case dw_val_class_vec:
9268       CHECKSUM_ULEB128 (DW_FORM_block);
9269       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9270       CHECKSUM (at->dw_attr_val.v.val_vec);
9271       break;
9272
9273     case dw_val_class_flag:
9274       CHECKSUM_ULEB128 (DW_FORM_flag);
9275       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9276       break;
9277
9278     case dw_val_class_str:
9279       CHECKSUM_ULEB128 (DW_FORM_string);
9280       CHECKSUM_STRING (AT_string (at));
9281       break;
9282
9283     case dw_val_class_addr:
9284       r = AT_addr (at);
9285       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9286       CHECKSUM_ULEB128 (DW_FORM_string);
9287       CHECKSUM_STRING (XSTR (r, 0));
9288       break;
9289
9290     case dw_val_class_offset:
9291       CHECKSUM_ULEB128 (DW_FORM_sdata);
9292       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9293       break;
9294
9295     case dw_val_class_loc:
9296       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9297         loc_checksum_ordered (loc, ctx);
9298       break;
9299
9300     case dw_val_class_fde_ref:
9301     case dw_val_class_lbl_id:
9302     case dw_val_class_lineptr:
9303     case dw_val_class_macptr:
9304       break;
9305
9306     case dw_val_class_file:
9307       CHECKSUM_ULEB128 (DW_FORM_string);
9308       CHECKSUM_STRING (AT_file (at)->filename);
9309       break;
9310
9311     case dw_val_class_data8:
9312       CHECKSUM (at->dw_attr_val.v.val_data8);
9313       break;
9314
9315     default:
9316       break;
9317     }
9318 }
9319
9320 struct checksum_attributes
9321 {
9322   dw_attr_ref at_name;
9323   dw_attr_ref at_type;
9324   dw_attr_ref at_friend;
9325   dw_attr_ref at_accessibility;
9326   dw_attr_ref at_address_class;
9327   dw_attr_ref at_allocated;
9328   dw_attr_ref at_artificial;
9329   dw_attr_ref at_associated;
9330   dw_attr_ref at_binary_scale;
9331   dw_attr_ref at_bit_offset;
9332   dw_attr_ref at_bit_size;
9333   dw_attr_ref at_bit_stride;
9334   dw_attr_ref at_byte_size;
9335   dw_attr_ref at_byte_stride;
9336   dw_attr_ref at_const_value;
9337   dw_attr_ref at_containing_type;
9338   dw_attr_ref at_count;
9339   dw_attr_ref at_data_location;
9340   dw_attr_ref at_data_member_location;
9341   dw_attr_ref at_decimal_scale;
9342   dw_attr_ref at_decimal_sign;
9343   dw_attr_ref at_default_value;
9344   dw_attr_ref at_digit_count;
9345   dw_attr_ref at_discr;
9346   dw_attr_ref at_discr_list;
9347   dw_attr_ref at_discr_value;
9348   dw_attr_ref at_encoding;
9349   dw_attr_ref at_endianity;
9350   dw_attr_ref at_explicit;
9351   dw_attr_ref at_is_optional;
9352   dw_attr_ref at_location;
9353   dw_attr_ref at_lower_bound;
9354   dw_attr_ref at_mutable;
9355   dw_attr_ref at_ordering;
9356   dw_attr_ref at_picture_string;
9357   dw_attr_ref at_prototyped;
9358   dw_attr_ref at_small;
9359   dw_attr_ref at_segment;
9360   dw_attr_ref at_string_length;
9361   dw_attr_ref at_threads_scaled;
9362   dw_attr_ref at_upper_bound;
9363   dw_attr_ref at_use_location;
9364   dw_attr_ref at_use_UTF8;
9365   dw_attr_ref at_variable_parameter;
9366   dw_attr_ref at_virtuality;
9367   dw_attr_ref at_visibility;
9368   dw_attr_ref at_vtable_elem_location;
9369 };
9370
9371 /* Collect the attributes that we will want to use for the checksum.  */
9372
9373 static void
9374 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9375 {
9376   dw_attr_ref a;
9377   unsigned ix;
9378
9379   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9380     {
9381       switch (a->dw_attr)
9382         {
9383         case DW_AT_name:
9384           attrs->at_name = a;
9385           break;
9386         case DW_AT_type:
9387           attrs->at_type = a;
9388           break;
9389         case DW_AT_friend:
9390           attrs->at_friend = a;
9391           break;
9392         case DW_AT_accessibility:
9393           attrs->at_accessibility = a;
9394           break;
9395         case DW_AT_address_class:
9396           attrs->at_address_class = a;
9397           break;
9398         case DW_AT_allocated:
9399           attrs->at_allocated = a;
9400           break;
9401         case DW_AT_artificial:
9402           attrs->at_artificial = a;
9403           break;
9404         case DW_AT_associated:
9405           attrs->at_associated = a;
9406           break;
9407         case DW_AT_binary_scale:
9408           attrs->at_binary_scale = a;
9409           break;
9410         case DW_AT_bit_offset:
9411           attrs->at_bit_offset = a;
9412           break;
9413         case DW_AT_bit_size:
9414           attrs->at_bit_size = a;
9415           break;
9416         case DW_AT_bit_stride:
9417           attrs->at_bit_stride = a;
9418           break;
9419         case DW_AT_byte_size:
9420           attrs->at_byte_size = a;
9421           break;
9422         case DW_AT_byte_stride:
9423           attrs->at_byte_stride = a;
9424           break;
9425         case DW_AT_const_value:
9426           attrs->at_const_value = a;
9427           break;
9428         case DW_AT_containing_type:
9429           attrs->at_containing_type = a;
9430           break;
9431         case DW_AT_count:
9432           attrs->at_count = a;
9433           break;
9434         case DW_AT_data_location:
9435           attrs->at_data_location = a;
9436           break;
9437         case DW_AT_data_member_location:
9438           attrs->at_data_member_location = a;
9439           break;
9440         case DW_AT_decimal_scale:
9441           attrs->at_decimal_scale = a;
9442           break;
9443         case DW_AT_decimal_sign:
9444           attrs->at_decimal_sign = a;
9445           break;
9446         case DW_AT_default_value:
9447           attrs->at_default_value = a;
9448           break;
9449         case DW_AT_digit_count:
9450           attrs->at_digit_count = a;
9451           break;
9452         case DW_AT_discr:
9453           attrs->at_discr = a;
9454           break;
9455         case DW_AT_discr_list:
9456           attrs->at_discr_list = a;
9457           break;
9458         case DW_AT_discr_value:
9459           attrs->at_discr_value = a;
9460           break;
9461         case DW_AT_encoding:
9462           attrs->at_encoding = a;
9463           break;
9464         case DW_AT_endianity:
9465           attrs->at_endianity = a;
9466           break;
9467         case DW_AT_explicit:
9468           attrs->at_explicit = a;
9469           break;
9470         case DW_AT_is_optional:
9471           attrs->at_is_optional = a;
9472           break;
9473         case DW_AT_location:
9474           attrs->at_location = a;
9475           break;
9476         case DW_AT_lower_bound:
9477           attrs->at_lower_bound = a;
9478           break;
9479         case DW_AT_mutable:
9480           attrs->at_mutable = a;
9481           break;
9482         case DW_AT_ordering:
9483           attrs->at_ordering = a;
9484           break;
9485         case DW_AT_picture_string:
9486           attrs->at_picture_string = a;
9487           break;
9488         case DW_AT_prototyped:
9489           attrs->at_prototyped = a;
9490           break;
9491         case DW_AT_small:
9492           attrs->at_small = a;
9493           break;
9494         case DW_AT_segment:
9495           attrs->at_segment = a;
9496           break;
9497         case DW_AT_string_length:
9498           attrs->at_string_length = a;
9499           break;
9500         case DW_AT_threads_scaled:
9501           attrs->at_threads_scaled = a;
9502           break;
9503         case DW_AT_upper_bound:
9504           attrs->at_upper_bound = a;
9505           break;
9506         case DW_AT_use_location:
9507           attrs->at_use_location = a;
9508           break;
9509         case DW_AT_use_UTF8:
9510           attrs->at_use_UTF8 = a;
9511           break;
9512         case DW_AT_variable_parameter:
9513           attrs->at_variable_parameter = a;
9514           break;
9515         case DW_AT_virtuality:
9516           attrs->at_virtuality = a;
9517           break;
9518         case DW_AT_visibility:
9519           attrs->at_visibility = a;
9520           break;
9521         case DW_AT_vtable_elem_location:
9522           attrs->at_vtable_elem_location = a;
9523           break;
9524         default:
9525           break;
9526         }
9527     }
9528 }
9529
9530 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9531
9532 static void
9533 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9534 {
9535   dw_die_ref c;
9536   dw_die_ref decl;
9537   struct checksum_attributes attrs;
9538
9539   CHECKSUM_ULEB128 ('D');
9540   CHECKSUM_ULEB128 (die->die_tag);
9541
9542   memset (&attrs, 0, sizeof (attrs));
9543
9544   decl = get_AT_ref (die, DW_AT_specification);
9545   if (decl != NULL)
9546     collect_checksum_attributes (&attrs, decl);
9547   collect_checksum_attributes (&attrs, die);
9548
9549   CHECKSUM_ATTR (attrs.at_name);
9550   CHECKSUM_ATTR (attrs.at_accessibility);
9551   CHECKSUM_ATTR (attrs.at_address_class);
9552   CHECKSUM_ATTR (attrs.at_allocated);
9553   CHECKSUM_ATTR (attrs.at_artificial);
9554   CHECKSUM_ATTR (attrs.at_associated);
9555   CHECKSUM_ATTR (attrs.at_binary_scale);
9556   CHECKSUM_ATTR (attrs.at_bit_offset);
9557   CHECKSUM_ATTR (attrs.at_bit_size);
9558   CHECKSUM_ATTR (attrs.at_bit_stride);
9559   CHECKSUM_ATTR (attrs.at_byte_size);
9560   CHECKSUM_ATTR (attrs.at_byte_stride);
9561   CHECKSUM_ATTR (attrs.at_const_value);
9562   CHECKSUM_ATTR (attrs.at_containing_type);
9563   CHECKSUM_ATTR (attrs.at_count);
9564   CHECKSUM_ATTR (attrs.at_data_location);
9565   CHECKSUM_ATTR (attrs.at_data_member_location);
9566   CHECKSUM_ATTR (attrs.at_decimal_scale);
9567   CHECKSUM_ATTR (attrs.at_decimal_sign);
9568   CHECKSUM_ATTR (attrs.at_default_value);
9569   CHECKSUM_ATTR (attrs.at_digit_count);
9570   CHECKSUM_ATTR (attrs.at_discr);
9571   CHECKSUM_ATTR (attrs.at_discr_list);
9572   CHECKSUM_ATTR (attrs.at_discr_value);
9573   CHECKSUM_ATTR (attrs.at_encoding);
9574   CHECKSUM_ATTR (attrs.at_endianity);
9575   CHECKSUM_ATTR (attrs.at_explicit);
9576   CHECKSUM_ATTR (attrs.at_is_optional);
9577   CHECKSUM_ATTR (attrs.at_location);
9578   CHECKSUM_ATTR (attrs.at_lower_bound);
9579   CHECKSUM_ATTR (attrs.at_mutable);
9580   CHECKSUM_ATTR (attrs.at_ordering);
9581   CHECKSUM_ATTR (attrs.at_picture_string);
9582   CHECKSUM_ATTR (attrs.at_prototyped);
9583   CHECKSUM_ATTR (attrs.at_small);
9584   CHECKSUM_ATTR (attrs.at_segment);
9585   CHECKSUM_ATTR (attrs.at_string_length);
9586   CHECKSUM_ATTR (attrs.at_threads_scaled);
9587   CHECKSUM_ATTR (attrs.at_upper_bound);
9588   CHECKSUM_ATTR (attrs.at_use_location);
9589   CHECKSUM_ATTR (attrs.at_use_UTF8);
9590   CHECKSUM_ATTR (attrs.at_variable_parameter);
9591   CHECKSUM_ATTR (attrs.at_virtuality);
9592   CHECKSUM_ATTR (attrs.at_visibility);
9593   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9594   CHECKSUM_ATTR (attrs.at_type);
9595   CHECKSUM_ATTR (attrs.at_friend);
9596
9597   /* Checksum the child DIEs, except for nested types and member functions.  */
9598   c = die->die_child;
9599   if (c) do {
9600     dw_attr_ref name_attr;
9601
9602     c = c->die_sib;
9603     name_attr = get_AT (c, DW_AT_name);
9604     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9605         && name_attr != NULL)
9606       {
9607         CHECKSUM_ULEB128 ('S');
9608         CHECKSUM_ULEB128 (c->die_tag);
9609         CHECKSUM_STRING (AT_string (name_attr));
9610       }
9611     else
9612       {
9613         /* Mark this DIE so it gets processed when unmarking.  */
9614         if (c->die_mark == 0)
9615           c->die_mark = -1;
9616         die_checksum_ordered (c, ctx, mark);
9617       }
9618   } while (c != die->die_child);
9619
9620   CHECKSUM_ULEB128 (0);
9621 }
9622
9623 #undef CHECKSUM
9624 #undef CHECKSUM_STRING
9625 #undef CHECKSUM_ATTR
9626 #undef CHECKSUM_LEB128
9627 #undef CHECKSUM_ULEB128
9628
9629 /* Generate the type signature for DIE.  This is computed by generating an
9630    MD5 checksum over the DIE's tag, its relevant attributes, and its
9631    children.  Attributes that are references to other DIEs are processed
9632    by recursion, using the MARK field to prevent infinite recursion.
9633    If the DIE is nested inside a namespace or another type, we also
9634    need to include that context in the signature.  The lower 64 bits
9635    of the resulting MD5 checksum comprise the signature.  */
9636
9637 static void
9638 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9639 {
9640   int mark;
9641   const char *name;
9642   unsigned char checksum[16];
9643   struct md5_ctx ctx;
9644   dw_die_ref decl;
9645
9646   name = get_AT_string (die, DW_AT_name);
9647   decl = get_AT_ref (die, DW_AT_specification);
9648
9649   /* First, compute a signature for just the type name (and its surrounding
9650      context, if any.  This is stored in the type unit DIE for link-time
9651      ODR (one-definition rule) checking.  */
9652
9653   if (is_cxx() && name != NULL)
9654     {
9655       md5_init_ctx (&ctx);
9656
9657       /* Checksum the names of surrounding namespaces and structures.  */
9658       if (decl != NULL && decl->die_parent != NULL)
9659         checksum_die_context (decl->die_parent, &ctx);
9660
9661       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9662       md5_process_bytes (name, strlen (name) + 1, &ctx);
9663       md5_finish_ctx (&ctx, checksum);
9664
9665       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9666     }
9667
9668   /* Next, compute the complete type signature.  */
9669
9670   md5_init_ctx (&ctx);
9671   mark = 1;
9672   die->die_mark = mark;
9673
9674   /* Checksum the names of surrounding namespaces and structures.  */
9675   if (decl != NULL && decl->die_parent != NULL)
9676     checksum_die_context (decl->die_parent, &ctx);
9677
9678   /* Checksum the DIE and its children.  */
9679   die_checksum_ordered (die, &ctx, &mark);
9680   unmark_all_dies (die);
9681   md5_finish_ctx (&ctx, checksum);
9682
9683   /* Store the signature in the type node and link the type DIE and the
9684      type node together.  */
9685   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9686           DWARF_TYPE_SIGNATURE_SIZE);
9687   die->die_id.die_type_node = type_node;
9688   type_node->type_die = die;
9689
9690   /* If the DIE is a specification, link its declaration to the type node
9691      as well.  */
9692   if (decl != NULL)
9693     decl->die_id.die_type_node = type_node;
9694 }
9695
9696 /* Do the location expressions look same?  */
9697 static inline int
9698 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9699 {
9700   return loc1->dw_loc_opc == loc2->dw_loc_opc
9701          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9702          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9703 }
9704
9705 /* Do the values look the same?  */
9706 static int
9707 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9708 {
9709   dw_loc_descr_ref loc1, loc2;
9710   rtx r1, r2;
9711
9712   if (v1->val_class != v2->val_class)
9713     return 0;
9714
9715   switch (v1->val_class)
9716     {
9717     case dw_val_class_const:
9718       return v1->v.val_int == v2->v.val_int;
9719     case dw_val_class_unsigned_const:
9720       return v1->v.val_unsigned == v2->v.val_unsigned;
9721     case dw_val_class_const_double:
9722       return v1->v.val_double.high == v2->v.val_double.high
9723              && v1->v.val_double.low == v2->v.val_double.low;
9724     case dw_val_class_vec:
9725       if (v1->v.val_vec.length != v2->v.val_vec.length
9726           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9727         return 0;
9728       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9729                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9730         return 0;
9731       return 1;
9732     case dw_val_class_flag:
9733       return v1->v.val_flag == v2->v.val_flag;
9734     case dw_val_class_str:
9735       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9736
9737     case dw_val_class_addr:
9738       r1 = v1->v.val_addr;
9739       r2 = v2->v.val_addr;
9740       if (GET_CODE (r1) != GET_CODE (r2))
9741         return 0;
9742       return !rtx_equal_p (r1, r2);
9743
9744     case dw_val_class_offset:
9745       return v1->v.val_offset == v2->v.val_offset;
9746
9747     case dw_val_class_loc:
9748       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9749            loc1 && loc2;
9750            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9751         if (!same_loc_p (loc1, loc2, mark))
9752           return 0;
9753       return !loc1 && !loc2;
9754
9755     case dw_val_class_die_ref:
9756       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9757
9758     case dw_val_class_fde_ref:
9759     case dw_val_class_vms_delta:
9760     case dw_val_class_lbl_id:
9761     case dw_val_class_lineptr:
9762     case dw_val_class_macptr:
9763       return 1;
9764
9765     case dw_val_class_file:
9766       return v1->v.val_file == v2->v.val_file;
9767
9768     case dw_val_class_data8:
9769       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9770
9771     default:
9772       return 1;
9773     }
9774 }
9775
9776 /* Do the attributes look the same?  */
9777
9778 static int
9779 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9780 {
9781   if (at1->dw_attr != at2->dw_attr)
9782     return 0;
9783
9784   /* We don't care that this was compiled with a different compiler
9785      snapshot; if the output is the same, that's what matters. */
9786   if (at1->dw_attr == DW_AT_producer)
9787     return 1;
9788
9789   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9790 }
9791
9792 /* Do the dies look the same?  */
9793
9794 static int
9795 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9796 {
9797   dw_die_ref c1, c2;
9798   dw_attr_ref a1;
9799   unsigned ix;
9800
9801   /* To avoid infinite recursion.  */
9802   if (die1->die_mark)
9803     return die1->die_mark == die2->die_mark;
9804   die1->die_mark = die2->die_mark = ++(*mark);
9805
9806   if (die1->die_tag != die2->die_tag)
9807     return 0;
9808
9809   if (VEC_length (dw_attr_node, die1->die_attr)
9810       != VEC_length (dw_attr_node, die2->die_attr))
9811     return 0;
9812
9813   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9814     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9815       return 0;
9816
9817   c1 = die1->die_child;
9818   c2 = die2->die_child;
9819   if (! c1)
9820     {
9821       if (c2)
9822         return 0;
9823     }
9824   else
9825     for (;;)
9826       {
9827         if (!same_die_p (c1, c2, mark))
9828           return 0;
9829         c1 = c1->die_sib;
9830         c2 = c2->die_sib;
9831         if (c1 == die1->die_child)
9832           {
9833             if (c2 == die2->die_child)
9834               break;
9835             else
9836               return 0;
9837           }
9838     }
9839
9840   return 1;
9841 }
9842
9843 /* Do the dies look the same?  Wrapper around same_die_p.  */
9844
9845 static int
9846 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9847 {
9848   int mark = 0;
9849   int ret = same_die_p (die1, die2, &mark);
9850
9851   unmark_all_dies (die1);
9852   unmark_all_dies (die2);
9853
9854   return ret;
9855 }
9856
9857 /* The prefix to attach to symbols on DIEs in the current comdat debug
9858    info section.  */
9859 static char *comdat_symbol_id;
9860
9861 /* The index of the current symbol within the current comdat CU.  */
9862 static unsigned int comdat_symbol_number;
9863
9864 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9865    children, and set comdat_symbol_id accordingly.  */
9866
9867 static void
9868 compute_section_prefix (dw_die_ref unit_die)
9869 {
9870   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9871   const char *base = die_name ? lbasename (die_name) : "anonymous";
9872   char *name = XALLOCAVEC (char, strlen (base) + 64);
9873   char *p;
9874   int i, mark;
9875   unsigned char checksum[16];
9876   struct md5_ctx ctx;
9877
9878   /* Compute the checksum of the DIE, then append part of it as hex digits to
9879      the name filename of the unit.  */
9880
9881   md5_init_ctx (&ctx);
9882   mark = 0;
9883   die_checksum (unit_die, &ctx, &mark);
9884   unmark_all_dies (unit_die);
9885   md5_finish_ctx (&ctx, checksum);
9886
9887   sprintf (name, "%s.", base);
9888   clean_symbol_name (name);
9889
9890   p = name + strlen (name);
9891   for (i = 0; i < 4; i++)
9892     {
9893       sprintf (p, "%.2x", checksum[i]);
9894       p += 2;
9895     }
9896
9897   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9898   comdat_symbol_number = 0;
9899 }
9900
9901 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9902
9903 static int
9904 is_type_die (dw_die_ref die)
9905 {
9906   switch (die->die_tag)
9907     {
9908     case DW_TAG_array_type:
9909     case DW_TAG_class_type:
9910     case DW_TAG_interface_type:
9911     case DW_TAG_enumeration_type:
9912     case DW_TAG_pointer_type:
9913     case DW_TAG_reference_type:
9914     case DW_TAG_rvalue_reference_type:
9915     case DW_TAG_string_type:
9916     case DW_TAG_structure_type:
9917     case DW_TAG_subroutine_type:
9918     case DW_TAG_union_type:
9919     case DW_TAG_ptr_to_member_type:
9920     case DW_TAG_set_type:
9921     case DW_TAG_subrange_type:
9922     case DW_TAG_base_type:
9923     case DW_TAG_const_type:
9924     case DW_TAG_file_type:
9925     case DW_TAG_packed_type:
9926     case DW_TAG_volatile_type:
9927     case DW_TAG_typedef:
9928       return 1;
9929     default:
9930       return 0;
9931     }
9932 }
9933
9934 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9935    Basically, we want to choose the bits that are likely to be shared between
9936    compilations (types) and leave out the bits that are specific to individual
9937    compilations (functions).  */
9938
9939 static int
9940 is_comdat_die (dw_die_ref c)
9941 {
9942   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9943      we do for stabs.  The advantage is a greater likelihood of sharing between
9944      objects that don't include headers in the same order (and therefore would
9945      put the base types in a different comdat).  jason 8/28/00 */
9946
9947   if (c->die_tag == DW_TAG_base_type)
9948     return 0;
9949
9950   if (c->die_tag == DW_TAG_pointer_type
9951       || c->die_tag == DW_TAG_reference_type
9952       || c->die_tag == DW_TAG_rvalue_reference_type
9953       || c->die_tag == DW_TAG_const_type
9954       || c->die_tag == DW_TAG_volatile_type)
9955     {
9956       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9957
9958       return t ? is_comdat_die (t) : 0;
9959     }
9960
9961   return is_type_die (c);
9962 }
9963
9964 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9965    compilation unit.  */
9966
9967 static int
9968 is_symbol_die (dw_die_ref c)
9969 {
9970   return (is_type_die (c)
9971           || is_declaration_die (c)
9972           || c->die_tag == DW_TAG_namespace
9973           || c->die_tag == DW_TAG_module);
9974 }
9975
9976 /* Returns true iff C is a compile-unit DIE.  */
9977
9978 static inline bool
9979 is_cu_die (dw_die_ref c)
9980 {
9981   return c && c->die_tag == DW_TAG_compile_unit;
9982 }
9983
9984 static char *
9985 gen_internal_sym (const char *prefix)
9986 {
9987   char buf[256];
9988
9989   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9990   return xstrdup (buf);
9991 }
9992
9993 /* Assign symbols to all worthy DIEs under DIE.  */
9994
9995 static void
9996 assign_symbol_names (dw_die_ref die)
9997 {
9998   dw_die_ref c;
9999
10000   if (is_symbol_die (die))
10001     {
10002       if (comdat_symbol_id)
10003         {
10004           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
10005
10006           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
10007                    comdat_symbol_id, comdat_symbol_number++);
10008           die->die_id.die_symbol = xstrdup (p);
10009         }
10010       else
10011         die->die_id.die_symbol = gen_internal_sym ("LDIE");
10012     }
10013
10014   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
10015 }
10016
10017 struct cu_hash_table_entry
10018 {
10019   dw_die_ref cu;
10020   unsigned min_comdat_num, max_comdat_num;
10021   struct cu_hash_table_entry *next;
10022 };
10023
10024 /* Routines to manipulate hash table of CUs.  */
10025 static hashval_t
10026 htab_cu_hash (const void *of)
10027 {
10028   const struct cu_hash_table_entry *const entry =
10029     (const struct cu_hash_table_entry *) of;
10030
10031   return htab_hash_string (entry->cu->die_id.die_symbol);
10032 }
10033
10034 static int
10035 htab_cu_eq (const void *of1, const void *of2)
10036 {
10037   const struct cu_hash_table_entry *const entry1 =
10038     (const struct cu_hash_table_entry *) of1;
10039   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10040
10041   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
10042 }
10043
10044 static void
10045 htab_cu_del (void *what)
10046 {
10047   struct cu_hash_table_entry *next,
10048     *entry = (struct cu_hash_table_entry *) what;
10049
10050   while (entry)
10051     {
10052       next = entry->next;
10053       free (entry);
10054       entry = next;
10055     }
10056 }
10057
10058 /* Check whether we have already seen this CU and set up SYM_NUM
10059    accordingly.  */
10060 static int
10061 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
10062 {
10063   struct cu_hash_table_entry dummy;
10064   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
10065
10066   dummy.max_comdat_num = 0;
10067
10068   slot = (struct cu_hash_table_entry **)
10069     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10070         INSERT);
10071   entry = *slot;
10072
10073   for (; entry; last = entry, entry = entry->next)
10074     {
10075       if (same_die_p_wrap (cu, entry->cu))
10076         break;
10077     }
10078
10079   if (entry)
10080     {
10081       *sym_num = entry->min_comdat_num;
10082       return 1;
10083     }
10084
10085   entry = XCNEW (struct cu_hash_table_entry);
10086   entry->cu = cu;
10087   entry->min_comdat_num = *sym_num = last->max_comdat_num;
10088   entry->next = *slot;
10089   *slot = entry;
10090
10091   return 0;
10092 }
10093
10094 /* Record SYM_NUM to record of CU in HTABLE.  */
10095 static void
10096 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
10097 {
10098   struct cu_hash_table_entry **slot, *entry;
10099
10100   slot = (struct cu_hash_table_entry **)
10101     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10102         NO_INSERT);
10103   entry = *slot;
10104
10105   entry->max_comdat_num = sym_num;
10106 }
10107
10108 /* Traverse the DIE (which is always comp_unit_die), and set up
10109    additional compilation units for each of the include files we see
10110    bracketed by BINCL/EINCL.  */
10111
10112 static void
10113 break_out_includes (dw_die_ref die)
10114 {
10115   dw_die_ref c;
10116   dw_die_ref unit = NULL;
10117   limbo_die_node *node, **pnode;
10118   htab_t cu_hash_table;
10119
10120   c = die->die_child;
10121   if (c) do {
10122     dw_die_ref prev = c;
10123     c = c->die_sib;
10124     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
10125            || (unit && is_comdat_die (c)))
10126       {
10127         dw_die_ref next = c->die_sib;
10128
10129         /* This DIE is for a secondary CU; remove it from the main one.  */
10130         remove_child_with_prev (c, prev);
10131
10132         if (c->die_tag == DW_TAG_GNU_BINCL)
10133           unit = push_new_compile_unit (unit, c);
10134         else if (c->die_tag == DW_TAG_GNU_EINCL)
10135           unit = pop_compile_unit (unit);
10136         else
10137           add_child_die (unit, c);
10138         c = next;
10139         if (c == die->die_child)
10140           break;
10141       }
10142   } while (c != die->die_child);
10143
10144 #if 0
10145   /* We can only use this in debugging, since the frontend doesn't check
10146      to make sure that we leave every include file we enter.  */
10147   gcc_assert (!unit);
10148 #endif
10149
10150   assign_symbol_names (die);
10151   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
10152   for (node = limbo_die_list, pnode = &limbo_die_list;
10153        node;
10154        node = node->next)
10155     {
10156       int is_dupl;
10157
10158       compute_section_prefix (node->die);
10159       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
10160                         &comdat_symbol_number);
10161       assign_symbol_names (node->die);
10162       if (is_dupl)
10163         *pnode = node->next;
10164       else
10165         {
10166           pnode = &node->next;
10167           record_comdat_symbol_number (node->die, cu_hash_table,
10168                 comdat_symbol_number);
10169         }
10170     }
10171   htab_delete (cu_hash_table);
10172 }
10173
10174 /* Return non-zero if this DIE is a declaration.  */
10175
10176 static int
10177 is_declaration_die (dw_die_ref die)
10178 {
10179   dw_attr_ref a;
10180   unsigned ix;
10181
10182   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10183     if (a->dw_attr == DW_AT_declaration)
10184       return 1;
10185
10186   return 0;
10187 }
10188
10189 /* Return non-zero if this DIE is nested inside a subprogram.  */
10190
10191 static int
10192 is_nested_in_subprogram (dw_die_ref die)
10193 {
10194   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10195
10196   if (decl == NULL)
10197     decl = die;
10198   return local_scope_p (decl);
10199 }
10200
10201 /* Return non-zero if this is a type DIE that should be moved to a
10202    COMDAT .debug_types section.  */
10203
10204 static int
10205 should_move_die_to_comdat (dw_die_ref die)
10206 {
10207   switch (die->die_tag)
10208     {
10209     case DW_TAG_class_type:
10210     case DW_TAG_structure_type:
10211     case DW_TAG_enumeration_type:
10212     case DW_TAG_union_type:
10213       /* Don't move declarations, inlined instances, or types nested in a
10214          subprogram.  */
10215       if (is_declaration_die (die)
10216           || get_AT (die, DW_AT_abstract_origin)
10217           || is_nested_in_subprogram (die))
10218         return 0;
10219       return 1;
10220     case DW_TAG_array_type:
10221     case DW_TAG_interface_type:
10222     case DW_TAG_pointer_type:
10223     case DW_TAG_reference_type:
10224     case DW_TAG_rvalue_reference_type:
10225     case DW_TAG_string_type:
10226     case DW_TAG_subroutine_type:
10227     case DW_TAG_ptr_to_member_type:
10228     case DW_TAG_set_type:
10229     case DW_TAG_subrange_type:
10230     case DW_TAG_base_type:
10231     case DW_TAG_const_type:
10232     case DW_TAG_file_type:
10233     case DW_TAG_packed_type:
10234     case DW_TAG_volatile_type:
10235     case DW_TAG_typedef:
10236     default:
10237       return 0;
10238     }
10239 }
10240
10241 /* Make a clone of DIE.  */
10242
10243 static dw_die_ref
10244 clone_die (dw_die_ref die)
10245 {
10246   dw_die_ref clone;
10247   dw_attr_ref a;
10248   unsigned ix;
10249
10250   clone = ggc_alloc_cleared_die_node ();
10251   clone->die_tag = die->die_tag;
10252
10253   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10254     add_dwarf_attr (clone, a);
10255
10256   return clone;
10257 }
10258
10259 /* Make a clone of the tree rooted at DIE.  */
10260
10261 static dw_die_ref
10262 clone_tree (dw_die_ref die)
10263 {
10264   dw_die_ref c;
10265   dw_die_ref clone = clone_die (die);
10266
10267   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10268
10269   return clone;
10270 }
10271
10272 /* Make a clone of DIE as a declaration.  */
10273
10274 static dw_die_ref
10275 clone_as_declaration (dw_die_ref die)
10276 {
10277   dw_die_ref clone;
10278   dw_die_ref decl;
10279   dw_attr_ref a;
10280   unsigned ix;
10281
10282   /* If the DIE is already a declaration, just clone it.  */
10283   if (is_declaration_die (die))
10284     return clone_die (die);
10285
10286   /* If the DIE is a specification, just clone its declaration DIE.  */
10287   decl = get_AT_ref (die, DW_AT_specification);
10288   if (decl != NULL)
10289     return clone_die (decl);
10290
10291   clone = ggc_alloc_cleared_die_node ();
10292   clone->die_tag = die->die_tag;
10293
10294   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10295     {
10296       /* We don't want to copy over all attributes.
10297          For example we don't want DW_AT_byte_size because otherwise we will no
10298          longer have a declaration and GDB will treat it as a definition.  */
10299
10300       switch (a->dw_attr)
10301         {
10302         case DW_AT_artificial:
10303         case DW_AT_containing_type:
10304         case DW_AT_external:
10305         case DW_AT_name:
10306         case DW_AT_type:
10307         case DW_AT_virtuality:
10308         case DW_AT_linkage_name:
10309         case DW_AT_MIPS_linkage_name:
10310           add_dwarf_attr (clone, a);
10311           break;
10312         case DW_AT_byte_size:
10313         default:
10314           break;
10315         }
10316     }
10317
10318   if (die->die_id.die_type_node)
10319     add_AT_die_ref (clone, DW_AT_signature, die);
10320
10321   add_AT_flag (clone, DW_AT_declaration, 1);
10322   return clone;
10323 }
10324
10325 /* Copy the declaration context to the new compile unit DIE.  This includes
10326    any surrounding namespace or type declarations.  If the DIE has an
10327    AT_specification attribute, it also includes attributes and children
10328    attached to the specification.  */
10329
10330 static void
10331 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10332 {
10333   dw_die_ref decl;
10334   dw_die_ref new_decl;
10335
10336   decl = get_AT_ref (die, DW_AT_specification);
10337   if (decl == NULL)
10338     decl = die;
10339   else
10340     {
10341       unsigned ix;
10342       dw_die_ref c;
10343       dw_attr_ref a;
10344
10345       /* Copy the type node pointer from the new DIE to the original
10346          declaration DIE so we can forward references later.  */
10347       decl->die_id.die_type_node = die->die_id.die_type_node;
10348
10349       remove_AT (die, DW_AT_specification);
10350
10351       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10352         {
10353           if (a->dw_attr != DW_AT_name
10354               && a->dw_attr != DW_AT_declaration
10355               && a->dw_attr != DW_AT_external)
10356             add_dwarf_attr (die, a);
10357         }
10358
10359       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10360     }
10361
10362   if (decl->die_parent != NULL
10363       && decl->die_parent->die_tag != DW_TAG_compile_unit
10364       && decl->die_parent->die_tag != DW_TAG_type_unit)
10365     {
10366       new_decl = copy_ancestor_tree (unit, decl, NULL);
10367       if (new_decl != NULL)
10368         {
10369           remove_AT (new_decl, DW_AT_signature);
10370           add_AT_specification (die, new_decl);
10371         }
10372     }
10373 }
10374
10375 /* Generate the skeleton ancestor tree for the given NODE, then clone
10376    the DIE and add the clone into the tree.  */
10377
10378 static void
10379 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10380 {
10381   if (node->new_die != NULL)
10382     return;
10383
10384   node->new_die = clone_as_declaration (node->old_die);
10385
10386   if (node->parent != NULL)
10387     {
10388       generate_skeleton_ancestor_tree (node->parent);
10389       add_child_die (node->parent->new_die, node->new_die);
10390     }
10391 }
10392
10393 /* Generate a skeleton tree of DIEs containing any declarations that are
10394    found in the original tree.  We traverse the tree looking for declaration
10395    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10396
10397 static void
10398 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10399 {
10400   skeleton_chain_node node;
10401   dw_die_ref c;
10402   dw_die_ref first;
10403   dw_die_ref prev = NULL;
10404   dw_die_ref next = NULL;
10405
10406   node.parent = parent;
10407
10408   first = c = parent->old_die->die_child;
10409   if (c)
10410     next = c->die_sib;
10411   if (c) do {
10412     if (prev == NULL || prev->die_sib == c)
10413       prev = c;
10414     c = next;
10415     next = (c == first ? NULL : c->die_sib);
10416     node.old_die = c;
10417     node.new_die = NULL;
10418     if (is_declaration_die (c))
10419       {
10420         /* Clone the existing DIE, move the original to the skeleton
10421            tree (which is in the main CU), and put the clone, with
10422            all the original's children, where the original came from.  */
10423         dw_die_ref clone = clone_die (c);
10424         move_all_children (c, clone);
10425
10426         replace_child (c, clone, prev);
10427         generate_skeleton_ancestor_tree (parent);
10428         add_child_die (parent->new_die, c);
10429         node.new_die = c;
10430         c = clone;
10431       }
10432     generate_skeleton_bottom_up (&node);
10433   } while (next != NULL);
10434 }
10435
10436 /* Wrapper function for generate_skeleton_bottom_up.  */
10437
10438 static dw_die_ref
10439 generate_skeleton (dw_die_ref die)
10440 {
10441   skeleton_chain_node node;
10442
10443   node.old_die = die;
10444   node.new_die = NULL;
10445   node.parent = NULL;
10446
10447   /* If this type definition is nested inside another type,
10448      always leave at least a declaration in its place.  */
10449   if (die->die_parent != NULL && is_type_die (die->die_parent))
10450     node.new_die = clone_as_declaration (die);
10451
10452   generate_skeleton_bottom_up (&node);
10453   return node.new_die;
10454 }
10455
10456 /* Remove the DIE from its parent, possibly replacing it with a cloned
10457    declaration.  The original DIE will be moved to a new compile unit
10458    so that existing references to it follow it to the new location.  If
10459    any of the original DIE's descendants is a declaration, we need to
10460    replace the original DIE with a skeleton tree and move the
10461    declarations back into the skeleton tree.  */
10462
10463 static dw_die_ref
10464 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10465 {
10466   dw_die_ref skeleton;
10467
10468   skeleton = generate_skeleton (child);
10469   if (skeleton == NULL)
10470     remove_child_with_prev (child, prev);
10471   else
10472     {
10473       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10474       replace_child (child, skeleton, prev);
10475     }
10476
10477   return skeleton;
10478 }
10479
10480 /* Traverse the DIE and set up additional .debug_types sections for each
10481    type worthy of being placed in a COMDAT section.  */
10482
10483 static void
10484 break_out_comdat_types (dw_die_ref die)
10485 {
10486   dw_die_ref c;
10487   dw_die_ref first;
10488   dw_die_ref prev = NULL;
10489   dw_die_ref next = NULL;
10490   dw_die_ref unit = NULL;
10491
10492   first = c = die->die_child;
10493   if (c)
10494     next = c->die_sib;
10495   if (c) do {
10496     if (prev == NULL || prev->die_sib == c)
10497       prev = c;
10498     c = next;
10499     next = (c == first ? NULL : c->die_sib);
10500     if (should_move_die_to_comdat (c))
10501       {
10502         dw_die_ref replacement;
10503         comdat_type_node_ref type_node;
10504
10505         /* Create a new type unit DIE as the root for the new tree, and
10506            add it to the list of comdat types.  */
10507         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10508         add_AT_unsigned (unit, DW_AT_language,
10509                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10510         type_node = ggc_alloc_cleared_comdat_type_node ();
10511         type_node->root_die = unit;
10512         type_node->next = comdat_type_list;
10513         comdat_type_list = type_node;
10514
10515         /* Generate the type signature.  */
10516         generate_type_signature (c, type_node);
10517
10518         /* Copy the declaration context, attributes, and children of the
10519            declaration into the new compile unit DIE.  */
10520         copy_declaration_context (unit, c);
10521
10522         /* Remove this DIE from the main CU.  */
10523         replacement = remove_child_or_replace_with_skeleton (c, prev);
10524
10525         /* Break out nested types into their own type units.  */
10526         break_out_comdat_types (c);
10527
10528         /* Add the DIE to the new compunit.  */
10529         add_child_die (unit, c);
10530
10531         if (replacement != NULL)
10532           c = replacement;
10533       }
10534     else if (c->die_tag == DW_TAG_namespace
10535              || c->die_tag == DW_TAG_class_type
10536              || c->die_tag == DW_TAG_structure_type
10537              || c->die_tag == DW_TAG_union_type)
10538       {
10539         /* Look for nested types that can be broken out.  */
10540         break_out_comdat_types (c);
10541       }
10542   } while (next != NULL);
10543 }
10544
10545 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10546
10547 struct decl_table_entry
10548 {
10549   dw_die_ref orig;
10550   dw_die_ref copy;
10551 };
10552
10553 /* Routines to manipulate hash table of copied declarations.  */
10554
10555 static hashval_t
10556 htab_decl_hash (const void *of)
10557 {
10558   const struct decl_table_entry *const entry =
10559     (const struct decl_table_entry *) of;
10560
10561   return htab_hash_pointer (entry->orig);
10562 }
10563
10564 static int
10565 htab_decl_eq (const void *of1, const void *of2)
10566 {
10567   const struct decl_table_entry *const entry1 =
10568     (const struct decl_table_entry *) of1;
10569   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10570
10571   return entry1->orig == entry2;
10572 }
10573
10574 static void
10575 htab_decl_del (void *what)
10576 {
10577   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10578
10579   free (entry);
10580 }
10581
10582 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10583    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10584    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10585    to check if the ancestor has already been copied into UNIT.  */
10586
10587 static dw_die_ref
10588 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10589 {
10590   dw_die_ref parent = die->die_parent;
10591   dw_die_ref new_parent = unit;
10592   dw_die_ref copy;
10593   void **slot = NULL;
10594   struct decl_table_entry *entry = NULL;
10595
10596   if (decl_table)
10597     {
10598       /* Check if the entry has already been copied to UNIT.  */
10599       slot = htab_find_slot_with_hash (decl_table, die,
10600                                        htab_hash_pointer (die), INSERT);
10601       if (*slot != HTAB_EMPTY_ENTRY)
10602         {
10603           entry = (struct decl_table_entry *) *slot;
10604           return entry->copy;
10605         }
10606
10607       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10608       entry = XCNEW (struct decl_table_entry);
10609       entry->orig = die;
10610       entry->copy = NULL;
10611       *slot = entry;
10612     }
10613
10614   if (parent != NULL)
10615     {
10616       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10617       if (spec != NULL)
10618         parent = spec;
10619       if (parent->die_tag != DW_TAG_compile_unit
10620           && parent->die_tag != DW_TAG_type_unit)
10621         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10622     }
10623
10624   copy = clone_as_declaration (die);
10625   add_child_die (new_parent, copy);
10626
10627   if (decl_table != NULL)
10628     {
10629       /* Record the pointer to the copy.  */
10630       entry->copy = copy;
10631     }
10632
10633   return copy;
10634 }
10635
10636 /* Walk the DIE and its children, looking for references to incomplete
10637    or trivial types that are unmarked (i.e., that are not in the current
10638    type_unit).  */
10639
10640 static void
10641 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10642 {
10643   dw_die_ref c;
10644   dw_attr_ref a;
10645   unsigned ix;
10646
10647   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10648     {
10649       if (AT_class (a) == dw_val_class_die_ref)
10650         {
10651           dw_die_ref targ = AT_ref (a);
10652           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10653           void **slot;
10654           struct decl_table_entry *entry;
10655
10656           if (targ->die_mark != 0 || type_node != NULL)
10657             continue;
10658
10659           slot = htab_find_slot_with_hash (decl_table, targ,
10660                                            htab_hash_pointer (targ), INSERT);
10661
10662           if (*slot != HTAB_EMPTY_ENTRY)
10663             {
10664               /* TARG has already been copied, so we just need to
10665                  modify the reference to point to the copy.  */
10666               entry = (struct decl_table_entry *) *slot;
10667               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10668             }
10669           else
10670             {
10671               dw_die_ref parent = unit;
10672               dw_die_ref copy = clone_tree (targ);
10673
10674               /* Make sure the cloned tree is marked as part of the
10675                  type unit.  */
10676               mark_dies (copy);
10677
10678               /* Record in DECL_TABLE that TARG has been copied.
10679                  Need to do this now, before the recursive call,
10680                  because DECL_TABLE may be expanded and SLOT
10681                  would no longer be a valid pointer.  */
10682               entry = XCNEW (struct decl_table_entry);
10683               entry->orig = targ;
10684               entry->copy = copy;
10685               *slot = entry;
10686
10687               /* If TARG has surrounding context, copy its ancestor tree
10688                  into the new type unit.  */
10689               if (targ->die_parent != NULL
10690                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10691                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10692                 parent = copy_ancestor_tree (unit, targ->die_parent,
10693                                              decl_table);
10694
10695               add_child_die (parent, copy);
10696               a->dw_attr_val.v.val_die_ref.die = copy;
10697
10698               /* Make sure the newly-copied DIE is walked.  If it was
10699                  installed in a previously-added context, it won't
10700                  get visited otherwise.  */
10701               if (parent != unit)
10702                 {
10703                   /* Find the highest point of the newly-added tree,
10704                      mark each node along the way, and walk from there.  */
10705                   parent->die_mark = 1;
10706                   while (parent->die_parent
10707                          && parent->die_parent->die_mark == 0)
10708                     {
10709                       parent = parent->die_parent;
10710                       parent->die_mark = 1;
10711                     }
10712                   copy_decls_walk (unit, parent, decl_table);
10713                 }
10714             }
10715         }
10716     }
10717
10718   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10719 }
10720
10721 /* Copy declarations for "unworthy" types into the new comdat section.
10722    Incomplete types, modified types, and certain other types aren't broken
10723    out into comdat sections of their own, so they don't have a signature,
10724    and we need to copy the declaration into the same section so that we
10725    don't have an external reference.  */
10726
10727 static void
10728 copy_decls_for_unworthy_types (dw_die_ref unit)
10729 {
10730   htab_t decl_table;
10731
10732   mark_dies (unit);
10733   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10734   copy_decls_walk (unit, unit, decl_table);
10735   htab_delete (decl_table);
10736   unmark_dies (unit);
10737 }
10738
10739 /* Traverse the DIE and add a sibling attribute if it may have the
10740    effect of speeding up access to siblings.  To save some space,
10741    avoid generating sibling attributes for DIE's without children.  */
10742
10743 static void
10744 add_sibling_attributes (dw_die_ref die)
10745 {
10746   dw_die_ref c;
10747
10748   if (! die->die_child)
10749     return;
10750
10751   if (die->die_parent && die != die->die_parent->die_child)
10752     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10753
10754   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10755 }
10756
10757 /* Output all location lists for the DIE and its children.  */
10758
10759 static void
10760 output_location_lists (dw_die_ref die)
10761 {
10762   dw_die_ref c;
10763   dw_attr_ref a;
10764   unsigned ix;
10765
10766   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10767     if (AT_class (a) == dw_val_class_loc_list)
10768       output_loc_list (AT_loc_list (a));
10769
10770   FOR_EACH_CHILD (die, c, output_location_lists (c));
10771 }
10772
10773 /* The format of each DIE (and its attribute value pairs) is encoded in an
10774    abbreviation table.  This routine builds the abbreviation table and assigns
10775    a unique abbreviation id for each abbreviation entry.  The children of each
10776    die are visited recursively.  */
10777
10778 static void
10779 build_abbrev_table (dw_die_ref die)
10780 {
10781   unsigned long abbrev_id;
10782   unsigned int n_alloc;
10783   dw_die_ref c;
10784   dw_attr_ref a;
10785   unsigned ix;
10786
10787   /* Scan the DIE references, and mark as external any that refer to
10788      DIEs from other CUs (i.e. those which are not marked).  */
10789   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10790     if (AT_class (a) == dw_val_class_die_ref
10791         && AT_ref (a)->die_mark == 0)
10792       {
10793         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10794         set_AT_ref_external (a, 1);
10795       }
10796
10797   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10798     {
10799       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10800       dw_attr_ref die_a, abbrev_a;
10801       unsigned ix;
10802       bool ok = true;
10803
10804       if (abbrev->die_tag != die->die_tag)
10805         continue;
10806       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10807         continue;
10808
10809       if (VEC_length (dw_attr_node, abbrev->die_attr)
10810           != VEC_length (dw_attr_node, die->die_attr))
10811         continue;
10812
10813       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10814         {
10815           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10816           if ((abbrev_a->dw_attr != die_a->dw_attr)
10817               || (value_format (abbrev_a) != value_format (die_a)))
10818             {
10819               ok = false;
10820               break;
10821             }
10822         }
10823       if (ok)
10824         break;
10825     }
10826
10827   if (abbrev_id >= abbrev_die_table_in_use)
10828     {
10829       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10830         {
10831           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10832           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10833                                             n_alloc);
10834
10835           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10836                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10837           abbrev_die_table_allocated = n_alloc;
10838         }
10839
10840       ++abbrev_die_table_in_use;
10841       abbrev_die_table[abbrev_id] = die;
10842     }
10843
10844   die->die_abbrev = abbrev_id;
10845   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10846 }
10847 \f
10848 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10849
10850 static int
10851 constant_size (unsigned HOST_WIDE_INT value)
10852 {
10853   int log;
10854
10855   if (value == 0)
10856     log = 0;
10857   else
10858     log = floor_log2 (value);
10859
10860   log = log / 8;
10861   log = 1 << (floor_log2 (log) + 1);
10862
10863   return log;
10864 }
10865
10866 /* Return the size of a DIE as it is represented in the
10867    .debug_info section.  */
10868
10869 static unsigned long
10870 size_of_die (dw_die_ref die)
10871 {
10872   unsigned long size = 0;
10873   dw_attr_ref a;
10874   unsigned ix;
10875
10876   size += size_of_uleb128 (die->die_abbrev);
10877   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10878     {
10879       switch (AT_class (a))
10880         {
10881         case dw_val_class_addr:
10882           size += DWARF2_ADDR_SIZE;
10883           break;
10884         case dw_val_class_offset:
10885           size += DWARF_OFFSET_SIZE;
10886           break;
10887         case dw_val_class_loc:
10888           {
10889             unsigned long lsize = size_of_locs (AT_loc (a));
10890
10891             /* Block length.  */
10892             if (dwarf_version >= 4)
10893               size += size_of_uleb128 (lsize);
10894             else
10895               size += constant_size (lsize);
10896             size += lsize;
10897           }
10898           break;
10899         case dw_val_class_loc_list:
10900           size += DWARF_OFFSET_SIZE;
10901           break;
10902         case dw_val_class_range_list:
10903           size += DWARF_OFFSET_SIZE;
10904           break;
10905         case dw_val_class_const:
10906           size += size_of_sleb128 (AT_int (a));
10907           break;
10908         case dw_val_class_unsigned_const:
10909           size += constant_size (AT_unsigned (a));
10910           break;
10911         case dw_val_class_const_double:
10912           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10913           if (HOST_BITS_PER_WIDE_INT >= 64)
10914             size++; /* block */
10915           break;
10916         case dw_val_class_vec:
10917           size += constant_size (a->dw_attr_val.v.val_vec.length
10918                                  * a->dw_attr_val.v.val_vec.elt_size)
10919                   + a->dw_attr_val.v.val_vec.length
10920                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10921           break;
10922         case dw_val_class_flag:
10923           if (dwarf_version >= 4)
10924             /* Currently all add_AT_flag calls pass in 1 as last argument,
10925                so DW_FORM_flag_present can be used.  If that ever changes,
10926                we'll need to use DW_FORM_flag and have some optimization
10927                in build_abbrev_table that will change those to
10928                DW_FORM_flag_present if it is set to 1 in all DIEs using
10929                the same abbrev entry.  */
10930             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10931           else
10932             size += 1;
10933           break;
10934         case dw_val_class_die_ref:
10935           if (AT_ref_external (a))
10936             {
10937               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10938                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10939                  is sized by target address length, whereas in DWARF3
10940                  it's always sized as an offset.  */
10941               if (use_debug_types)
10942                 size += DWARF_TYPE_SIGNATURE_SIZE;
10943               else if (dwarf_version == 2)
10944                 size += DWARF2_ADDR_SIZE;
10945               else
10946                 size += DWARF_OFFSET_SIZE;
10947             }
10948           else
10949             size += DWARF_OFFSET_SIZE;
10950           break;
10951         case dw_val_class_fde_ref:
10952           size += DWARF_OFFSET_SIZE;
10953           break;
10954         case dw_val_class_lbl_id:
10955           size += DWARF2_ADDR_SIZE;
10956           break;
10957         case dw_val_class_lineptr:
10958         case dw_val_class_macptr:
10959           size += DWARF_OFFSET_SIZE;
10960           break;
10961         case dw_val_class_str:
10962           if (AT_string_form (a) == DW_FORM_strp)
10963             size += DWARF_OFFSET_SIZE;
10964           else
10965             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10966           break;
10967         case dw_val_class_file:
10968           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10969           break;
10970         case dw_val_class_data8:
10971           size += 8;
10972           break;
10973         case dw_val_class_vms_delta:
10974           size += DWARF_OFFSET_SIZE;
10975           break;
10976         default:
10977           gcc_unreachable ();
10978         }
10979     }
10980
10981   return size;
10982 }
10983
10984 /* Size the debugging information associated with a given DIE.  Visits the
10985    DIE's children recursively.  Updates the global variable next_die_offset, on
10986    each time through.  Uses the current value of next_die_offset to update the
10987    die_offset field in each DIE.  */
10988
10989 static void
10990 calc_die_sizes (dw_die_ref die)
10991 {
10992   dw_die_ref c;
10993
10994   gcc_assert (die->die_offset == 0
10995               || (unsigned long int) die->die_offset == next_die_offset);
10996   die->die_offset = next_die_offset;
10997   next_die_offset += size_of_die (die);
10998
10999   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
11000
11001   if (die->die_child != NULL)
11002     /* Count the null byte used to terminate sibling lists.  */
11003     next_die_offset += 1;
11004 }
11005
11006 /* Size just the base type children at the start of the CU.
11007    This is needed because build_abbrev needs to size locs
11008    and sizing of type based stack ops needs to know die_offset
11009    values for the base types.  */
11010
11011 static void
11012 calc_base_type_die_sizes (void)
11013 {
11014   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11015   unsigned int i;
11016   dw_die_ref base_type;
11017 #if ENABLE_ASSERT_CHECKING
11018   dw_die_ref prev = comp_unit_die ()->die_child;
11019 #endif
11020
11021   die_offset += size_of_die (comp_unit_die ());
11022   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
11023     {
11024 #if ENABLE_ASSERT_CHECKING
11025       gcc_assert (base_type->die_offset == 0
11026                   && prev->die_sib == base_type
11027                   && base_type->die_child == NULL
11028                   && base_type->die_abbrev);
11029       prev = base_type;
11030 #endif
11031       base_type->die_offset = die_offset;
11032       die_offset += size_of_die (base_type);
11033     }
11034 }
11035
11036 /* Set the marks for a die and its children.  We do this so
11037    that we know whether or not a reference needs to use FORM_ref_addr; only
11038    DIEs in the same CU will be marked.  We used to clear out the offset
11039    and use that as the flag, but ran into ordering problems.  */
11040
11041 static void
11042 mark_dies (dw_die_ref die)
11043 {
11044   dw_die_ref c;
11045
11046   gcc_assert (!die->die_mark);
11047
11048   die->die_mark = 1;
11049   FOR_EACH_CHILD (die, c, mark_dies (c));
11050 }
11051
11052 /* Clear the marks for a die and its children.  */
11053
11054 static void
11055 unmark_dies (dw_die_ref die)
11056 {
11057   dw_die_ref c;
11058
11059   if (! use_debug_types)
11060     gcc_assert (die->die_mark);
11061
11062   die->die_mark = 0;
11063   FOR_EACH_CHILD (die, c, unmark_dies (c));
11064 }
11065
11066 /* Clear the marks for a die, its children and referred dies.  */
11067
11068 static void
11069 unmark_all_dies (dw_die_ref die)
11070 {
11071   dw_die_ref c;
11072   dw_attr_ref a;
11073   unsigned ix;
11074
11075   if (!die->die_mark)
11076     return;
11077   die->die_mark = 0;
11078
11079   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
11080
11081   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11082     if (AT_class (a) == dw_val_class_die_ref)
11083       unmark_all_dies (AT_ref (a));
11084 }
11085
11086 /* Return the size of the .debug_pubnames or .debug_pubtypes table
11087    generated for the compilation unit.  */
11088
11089 static unsigned long
11090 size_of_pubnames (VEC (pubname_entry, gc) * names)
11091 {
11092   unsigned long size;
11093   unsigned i;
11094   pubname_ref p;
11095
11096   size = DWARF_PUBNAMES_HEADER_SIZE;
11097   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
11098     if (names != pubtype_table
11099         || p->die->die_offset != 0
11100         || !flag_eliminate_unused_debug_types)
11101       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
11102
11103   size += DWARF_OFFSET_SIZE;
11104   return size;
11105 }
11106
11107 /* Return the size of the information in the .debug_aranges section.  */
11108
11109 static unsigned long
11110 size_of_aranges (void)
11111 {
11112   unsigned long size;
11113
11114   size = DWARF_ARANGES_HEADER_SIZE;
11115
11116   /* Count the address/length pair for this compilation unit.  */
11117   if (text_section_used)
11118     size += 2 * DWARF2_ADDR_SIZE;
11119   if (cold_text_section_used)
11120     size += 2 * DWARF2_ADDR_SIZE;
11121   if (have_multiple_function_sections)
11122     {
11123       unsigned fde_idx = 0;
11124
11125       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11126         {
11127           dw_fde_ref fde = &fde_table[fde_idx];
11128
11129           if (!fde->in_std_section)
11130             size += 2 * DWARF2_ADDR_SIZE;
11131           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11132             size += 2 * DWARF2_ADDR_SIZE;
11133         }
11134     }
11135
11136   /* Count the two zero words used to terminated the address range table.  */
11137   size += 2 * DWARF2_ADDR_SIZE;
11138   return size;
11139 }
11140 \f
11141 /* Select the encoding of an attribute value.  */
11142
11143 static enum dwarf_form
11144 value_format (dw_attr_ref a)
11145 {
11146   switch (a->dw_attr_val.val_class)
11147     {
11148     case dw_val_class_addr:
11149       /* Only very few attributes allow DW_FORM_addr.  */
11150       switch (a->dw_attr)
11151         {
11152         case DW_AT_low_pc:
11153         case DW_AT_high_pc:
11154         case DW_AT_entry_pc:
11155         case DW_AT_trampoline:
11156           return DW_FORM_addr;
11157         default:
11158           break;
11159         }
11160       switch (DWARF2_ADDR_SIZE)
11161         {
11162         case 1:
11163           return DW_FORM_data1;
11164         case 2:
11165           return DW_FORM_data2;
11166         case 4:
11167           return DW_FORM_data4;
11168         case 8:
11169           return DW_FORM_data8;
11170         default:
11171           gcc_unreachable ();
11172         }
11173     case dw_val_class_range_list:
11174     case dw_val_class_loc_list:
11175       if (dwarf_version >= 4)
11176         return DW_FORM_sec_offset;
11177       /* FALLTHRU */
11178     case dw_val_class_vms_delta:
11179     case dw_val_class_offset:
11180       switch (DWARF_OFFSET_SIZE)
11181         {
11182         case 4:
11183           return DW_FORM_data4;
11184         case 8:
11185           return DW_FORM_data8;
11186         default:
11187           gcc_unreachable ();
11188         }
11189     case dw_val_class_loc:
11190       if (dwarf_version >= 4)
11191         return DW_FORM_exprloc;
11192       switch (constant_size (size_of_locs (AT_loc (a))))
11193         {
11194         case 1:
11195           return DW_FORM_block1;
11196         case 2:
11197           return DW_FORM_block2;
11198         default:
11199           gcc_unreachable ();
11200         }
11201     case dw_val_class_const:
11202       return DW_FORM_sdata;
11203     case dw_val_class_unsigned_const:
11204       switch (constant_size (AT_unsigned (a)))
11205         {
11206         case 1:
11207           return DW_FORM_data1;
11208         case 2:
11209           return DW_FORM_data2;
11210         case 4:
11211           return DW_FORM_data4;
11212         case 8:
11213           return DW_FORM_data8;
11214         default:
11215           gcc_unreachable ();
11216         }
11217     case dw_val_class_const_double:
11218       switch (HOST_BITS_PER_WIDE_INT)
11219         {
11220         case 8:
11221           return DW_FORM_data2;
11222         case 16:
11223           return DW_FORM_data4;
11224         case 32:
11225           return DW_FORM_data8;
11226         case 64:
11227         default:
11228           return DW_FORM_block1;
11229         }
11230     case dw_val_class_vec:
11231       switch (constant_size (a->dw_attr_val.v.val_vec.length
11232                              * a->dw_attr_val.v.val_vec.elt_size))
11233         {
11234         case 1:
11235           return DW_FORM_block1;
11236         case 2:
11237           return DW_FORM_block2;
11238         case 4:
11239           return DW_FORM_block4;
11240         default:
11241           gcc_unreachable ();
11242         }
11243     case dw_val_class_flag:
11244       if (dwarf_version >= 4)
11245         {
11246           /* Currently all add_AT_flag calls pass in 1 as last argument,
11247              so DW_FORM_flag_present can be used.  If that ever changes,
11248              we'll need to use DW_FORM_flag and have some optimization
11249              in build_abbrev_table that will change those to
11250              DW_FORM_flag_present if it is set to 1 in all DIEs using
11251              the same abbrev entry.  */
11252           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11253           return DW_FORM_flag_present;
11254         }
11255       return DW_FORM_flag;
11256     case dw_val_class_die_ref:
11257       if (AT_ref_external (a))
11258         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11259       else
11260         return DW_FORM_ref;
11261     case dw_val_class_fde_ref:
11262       return DW_FORM_data;
11263     case dw_val_class_lbl_id:
11264       return DW_FORM_addr;
11265     case dw_val_class_lineptr:
11266     case dw_val_class_macptr:
11267       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11268     case dw_val_class_str:
11269       return AT_string_form (a);
11270     case dw_val_class_file:
11271       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11272         {
11273         case 1:
11274           return DW_FORM_data1;
11275         case 2:
11276           return DW_FORM_data2;
11277         case 4:
11278           return DW_FORM_data4;
11279         default:
11280           gcc_unreachable ();
11281         }
11282
11283     case dw_val_class_data8:
11284       return DW_FORM_data8;
11285
11286     default:
11287       gcc_unreachable ();
11288     }
11289 }
11290
11291 /* Output the encoding of an attribute value.  */
11292
11293 static void
11294 output_value_format (dw_attr_ref a)
11295 {
11296   enum dwarf_form form = value_format (a);
11297
11298   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11299 }
11300
11301 /* Output the .debug_abbrev section which defines the DIE abbreviation
11302    table.  */
11303
11304 static void
11305 output_abbrev_section (void)
11306 {
11307   unsigned long abbrev_id;
11308
11309   if (abbrev_die_table_in_use == 1)
11310     return;
11311
11312   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11313     {
11314       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11315       unsigned ix;
11316       dw_attr_ref a_attr;
11317
11318       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11319       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11320                                    dwarf_tag_name (abbrev->die_tag));
11321
11322       if (abbrev->die_child != NULL)
11323         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11324       else
11325         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11326
11327       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11328            ix++)
11329         {
11330           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11331                                        dwarf_attr_name (a_attr->dw_attr));
11332           output_value_format (a_attr);
11333         }
11334
11335       dw2_asm_output_data (1, 0, NULL);
11336       dw2_asm_output_data (1, 0, NULL);
11337     }
11338
11339   /* Terminate the table.  */
11340   dw2_asm_output_data (1, 0, NULL);
11341 }
11342
11343 /* Output a symbol we can use to refer to this DIE from another CU.  */
11344
11345 static inline void
11346 output_die_symbol (dw_die_ref die)
11347 {
11348   char *sym = die->die_id.die_symbol;
11349
11350   if (sym == 0)
11351     return;
11352
11353   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11354     /* We make these global, not weak; if the target doesn't support
11355        .linkonce, it doesn't support combining the sections, so debugging
11356        will break.  */
11357     targetm.asm_out.globalize_label (asm_out_file, sym);
11358
11359   ASM_OUTPUT_LABEL (asm_out_file, sym);
11360 }
11361
11362 /* Return a new location list, given the begin and end range, and the
11363    expression.  */
11364
11365 static inline dw_loc_list_ref
11366 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11367               const char *section)
11368 {
11369   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11370
11371   retlist->begin = begin;
11372   retlist->end = end;
11373   retlist->expr = expr;
11374   retlist->section = section;
11375
11376   return retlist;
11377 }
11378
11379 /* Generate a new internal symbol for this location list node, if it
11380    hasn't got one yet.  */
11381
11382 static inline void
11383 gen_llsym (dw_loc_list_ref list)
11384 {
11385   gcc_assert (!list->ll_symbol);
11386   list->ll_symbol = gen_internal_sym ("LLST");
11387 }
11388
11389 /* Output the location list given to us.  */
11390
11391 static void
11392 output_loc_list (dw_loc_list_ref list_head)
11393 {
11394   dw_loc_list_ref curr = list_head;
11395
11396   if (list_head->emitted)
11397     return;
11398   list_head->emitted = true;
11399
11400   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11401
11402   /* Walk the location list, and output each range + expression.  */
11403   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11404     {
11405       unsigned long size;
11406       /* Don't output an entry that starts and ends at the same address.  */
11407       if (strcmp (curr->begin, curr->end) == 0)
11408         continue;
11409       if (!have_multiple_function_sections)
11410         {
11411           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11412                                 "Location list begin address (%s)",
11413                                 list_head->ll_symbol);
11414           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11415                                 "Location list end address (%s)",
11416                                 list_head->ll_symbol);
11417         }
11418       else
11419         {
11420           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11421                                "Location list begin address (%s)",
11422                                list_head->ll_symbol);
11423           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11424                                "Location list end address (%s)",
11425                                list_head->ll_symbol);
11426         }
11427       size = size_of_locs (curr->expr);
11428
11429       /* Output the block length for this list of location operations.  */
11430       gcc_assert (size <= 0xffff);
11431       dw2_asm_output_data (2, size, "%s", "Location expression size");
11432
11433       output_loc_sequence (curr->expr, -1);
11434     }
11435
11436   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11437                        "Location list terminator begin (%s)",
11438                        list_head->ll_symbol);
11439   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11440                        "Location list terminator end (%s)",
11441                        list_head->ll_symbol);
11442 }
11443
11444 /* Output a type signature.  */
11445
11446 static inline void
11447 output_signature (const char *sig, const char *name)
11448 {
11449   int i;
11450
11451   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11452     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11453 }
11454
11455 /* Output the DIE and its attributes.  Called recursively to generate
11456    the definitions of each child DIE.  */
11457
11458 static void
11459 output_die (dw_die_ref die)
11460 {
11461   dw_attr_ref a;
11462   dw_die_ref c;
11463   unsigned long size;
11464   unsigned ix;
11465
11466   /* If someone in another CU might refer to us, set up a symbol for
11467      them to point to.  */
11468   if (! use_debug_types && die->die_id.die_symbol)
11469     output_die_symbol (die);
11470
11471   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11472                                (unsigned long)die->die_offset,
11473                                dwarf_tag_name (die->die_tag));
11474
11475   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11476     {
11477       const char *name = dwarf_attr_name (a->dw_attr);
11478
11479       switch (AT_class (a))
11480         {
11481         case dw_val_class_addr:
11482           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11483           break;
11484
11485         case dw_val_class_offset:
11486           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11487                                "%s", name);
11488           break;
11489
11490         case dw_val_class_range_list:
11491           {
11492             char *p = strchr (ranges_section_label, '\0');
11493
11494             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11495                      a->dw_attr_val.v.val_offset);
11496             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11497                                    debug_ranges_section, "%s", name);
11498             *p = '\0';
11499           }
11500           break;
11501
11502         case dw_val_class_loc:
11503           size = size_of_locs (AT_loc (a));
11504
11505           /* Output the block length for this list of location operations.  */
11506           if (dwarf_version >= 4)
11507             dw2_asm_output_data_uleb128 (size, "%s", name);
11508           else
11509             dw2_asm_output_data (constant_size (size), size, "%s", name);
11510
11511           output_loc_sequence (AT_loc (a), -1);
11512           break;
11513
11514         case dw_val_class_const:
11515           /* ??? It would be slightly more efficient to use a scheme like is
11516              used for unsigned constants below, but gdb 4.x does not sign
11517              extend.  Gdb 5.x does sign extend.  */
11518           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11519           break;
11520
11521         case dw_val_class_unsigned_const:
11522           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11523                                AT_unsigned (a), "%s", name);
11524           break;
11525
11526         case dw_val_class_const_double:
11527           {
11528             unsigned HOST_WIDE_INT first, second;
11529
11530             if (HOST_BITS_PER_WIDE_INT >= 64)
11531               dw2_asm_output_data (1,
11532                                    2 * HOST_BITS_PER_WIDE_INT
11533                                    / HOST_BITS_PER_CHAR,
11534                                    NULL);
11535
11536             if (WORDS_BIG_ENDIAN)
11537               {
11538                 first = a->dw_attr_val.v.val_double.high;
11539                 second = a->dw_attr_val.v.val_double.low;
11540               }
11541             else
11542               {
11543                 first = a->dw_attr_val.v.val_double.low;
11544                 second = a->dw_attr_val.v.val_double.high;
11545               }
11546
11547             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11548                                  first, name);
11549             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11550                                  second, NULL);
11551           }
11552           break;
11553
11554         case dw_val_class_vec:
11555           {
11556             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11557             unsigned int len = a->dw_attr_val.v.val_vec.length;
11558             unsigned int i;
11559             unsigned char *p;
11560
11561             dw2_asm_output_data (constant_size (len * elt_size),
11562                                  len * elt_size, "%s", name);
11563             if (elt_size > sizeof (HOST_WIDE_INT))
11564               {
11565                 elt_size /= 2;
11566                 len *= 2;
11567               }
11568             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11569                  i < len;
11570                  i++, p += elt_size)
11571               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11572                                    "fp or vector constant word %u", i);
11573             break;
11574           }
11575
11576         case dw_val_class_flag:
11577           if (dwarf_version >= 4)
11578             {
11579               /* Currently all add_AT_flag calls pass in 1 as last argument,
11580                  so DW_FORM_flag_present can be used.  If that ever changes,
11581                  we'll need to use DW_FORM_flag and have some optimization
11582                  in build_abbrev_table that will change those to
11583                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11584                  the same abbrev entry.  */
11585               gcc_assert (AT_flag (a) == 1);
11586               if (flag_debug_asm)
11587                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11588                          ASM_COMMENT_START, name);
11589               break;
11590             }
11591           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11592           break;
11593
11594         case dw_val_class_loc_list:
11595           {
11596             char *sym = AT_loc_list (a)->ll_symbol;
11597
11598             gcc_assert (sym);
11599             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11600                                    "%s", name);
11601           }
11602           break;
11603
11604         case dw_val_class_die_ref:
11605           if (AT_ref_external (a))
11606             {
11607               if (use_debug_types)
11608                 {
11609                   comdat_type_node_ref type_node =
11610                     AT_ref (a)->die_id.die_type_node;
11611
11612                   gcc_assert (type_node);
11613                   output_signature (type_node->signature, name);
11614                 }
11615               else
11616                 {
11617                   char *sym = AT_ref (a)->die_id.die_symbol;
11618                   int size;
11619
11620                   gcc_assert (sym);
11621                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11622                      length, whereas in DWARF3 it's always sized as an
11623                      offset.  */
11624                   if (dwarf_version == 2)
11625                     size = DWARF2_ADDR_SIZE;
11626                   else
11627                     size = DWARF_OFFSET_SIZE;
11628                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11629                                          name);
11630                 }
11631             }
11632           else
11633             {
11634               gcc_assert (AT_ref (a)->die_offset);
11635               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11636                                    "%s", name);
11637             }
11638           break;
11639
11640         case dw_val_class_fde_ref:
11641           {
11642             char l1[20];
11643
11644             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11645                                          a->dw_attr_val.v.val_fde_index * 2);
11646             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11647                                    "%s", name);
11648           }
11649           break;
11650
11651         case dw_val_class_vms_delta:
11652           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11653                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11654                                     "%s", name);
11655           break;
11656
11657         case dw_val_class_lbl_id:
11658           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11659           break;
11660
11661         case dw_val_class_lineptr:
11662           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11663                                  debug_line_section, "%s", name);
11664           break;
11665
11666         case dw_val_class_macptr:
11667           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11668                                  debug_macinfo_section, "%s", name);
11669           break;
11670
11671         case dw_val_class_str:
11672           if (AT_string_form (a) == DW_FORM_strp)
11673             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11674                                    a->dw_attr_val.v.val_str->label,
11675                                    debug_str_section,
11676                                    "%s: \"%s\"", name, AT_string (a));
11677           else
11678             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11679           break;
11680
11681         case dw_val_class_file:
11682           {
11683             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11684
11685             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11686                                  a->dw_attr_val.v.val_file->filename);
11687             break;
11688           }
11689
11690         case dw_val_class_data8:
11691           {
11692             int i;
11693
11694             for (i = 0; i < 8; i++)
11695               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11696                                    i == 0 ? "%s" : NULL, name);
11697             break;
11698           }
11699
11700         default:
11701           gcc_unreachable ();
11702         }
11703     }
11704
11705   FOR_EACH_CHILD (die, c, output_die (c));
11706
11707   /* Add null byte to terminate sibling list.  */
11708   if (die->die_child != NULL)
11709     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11710                          (unsigned long) die->die_offset);
11711 }
11712
11713 /* Output the compilation unit that appears at the beginning of the
11714    .debug_info section, and precedes the DIE descriptions.  */
11715
11716 static void
11717 output_compilation_unit_header (void)
11718 {
11719   int ver = dwarf_version;
11720
11721   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11722     dw2_asm_output_data (4, 0xffffffff,
11723       "Initial length escape value indicating 64-bit DWARF extension");
11724   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11725                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11726                        "Length of Compilation Unit Info");
11727   dw2_asm_output_data (2, ver, "DWARF version number");
11728   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11729                          debug_abbrev_section,
11730                          "Offset Into Abbrev. Section");
11731   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11732 }
11733
11734 /* Output the compilation unit DIE and its children.  */
11735
11736 static void
11737 output_comp_unit (dw_die_ref die, int output_if_empty)
11738 {
11739   const char *secname;
11740   char *oldsym, *tmp;
11741
11742   /* Unless we are outputting main CU, we may throw away empty ones.  */
11743   if (!output_if_empty && die->die_child == NULL)
11744     return;
11745
11746   /* Even if there are no children of this DIE, we must output the information
11747      about the compilation unit.  Otherwise, on an empty translation unit, we
11748      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11749      will then complain when examining the file.  First mark all the DIEs in
11750      this CU so we know which get local refs.  */
11751   mark_dies (die);
11752
11753   build_abbrev_table (die);
11754
11755   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11756   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11757   calc_die_sizes (die);
11758
11759   oldsym = die->die_id.die_symbol;
11760   if (oldsym)
11761     {
11762       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11763
11764       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11765       secname = tmp;
11766       die->die_id.die_symbol = NULL;
11767       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11768     }
11769   else
11770     {
11771       switch_to_section (debug_info_section);
11772       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11773       info_section_emitted = true;
11774     }
11775
11776   /* Output debugging information.  */
11777   output_compilation_unit_header ();
11778   output_die (die);
11779
11780   /* Leave the marks on the main CU, so we can check them in
11781      output_pubnames.  */
11782   if (oldsym)
11783     {
11784       unmark_dies (die);
11785       die->die_id.die_symbol = oldsym;
11786     }
11787 }
11788
11789 /* Output a comdat type unit DIE and its children.  */
11790
11791 static void
11792 output_comdat_type_unit (comdat_type_node *node)
11793 {
11794   const char *secname;
11795   char *tmp;
11796   int i;
11797 #if defined (OBJECT_FORMAT_ELF)
11798   tree comdat_key;
11799 #endif
11800
11801   /* First mark all the DIEs in this CU so we know which get local refs.  */
11802   mark_dies (node->root_die);
11803
11804   build_abbrev_table (node->root_die);
11805
11806   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11807   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11808   calc_die_sizes (node->root_die);
11809
11810 #if defined (OBJECT_FORMAT_ELF)
11811   secname = ".debug_types";
11812   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11813   sprintf (tmp, "wt.");
11814   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11815     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11816   comdat_key = get_identifier (tmp);
11817   targetm.asm_out.named_section (secname,
11818                                  SECTION_DEBUG | SECTION_LINKONCE,
11819                                  comdat_key);
11820 #else
11821   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11822   sprintf (tmp, ".gnu.linkonce.wt.");
11823   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11824     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11825   secname = tmp;
11826   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11827 #endif
11828
11829   /* Output debugging information.  */
11830   output_compilation_unit_header ();
11831   output_signature (node->signature, "Type Signature");
11832   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11833                        "Offset to Type DIE");
11834   output_die (node->root_die);
11835
11836   unmark_dies (node->root_die);
11837 }
11838
11839 /* Return the DWARF2/3 pubname associated with a decl.  */
11840
11841 static const char *
11842 dwarf2_name (tree decl, int scope)
11843 {
11844   if (DECL_NAMELESS (decl))
11845     return NULL;
11846   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11847 }
11848
11849 /* Add a new entry to .debug_pubnames if appropriate.  */
11850
11851 static void
11852 add_pubname_string (const char *str, dw_die_ref die)
11853 {
11854   if (targetm.want_debug_pub_sections)
11855     {
11856       pubname_entry e;
11857
11858       e.die = die;
11859       e.name = xstrdup (str);
11860       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11861     }
11862 }
11863
11864 static void
11865 add_pubname (tree decl, dw_die_ref die)
11866 {
11867   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11868     {
11869       const char *name = dwarf2_name (decl, 1);
11870       if (name)
11871         add_pubname_string (name, die);
11872     }
11873 }
11874
11875 /* Add a new entry to .debug_pubtypes if appropriate.  */
11876
11877 static void
11878 add_pubtype (tree decl, dw_die_ref die)
11879 {
11880   pubname_entry e;
11881
11882   if (!targetm.want_debug_pub_sections)
11883     return;
11884
11885   e.name = NULL;
11886   if ((TREE_PUBLIC (decl)
11887        || is_cu_die (die->die_parent))
11888       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11889     {
11890       e.die = die;
11891       if (TYPE_P (decl))
11892         {
11893           if (TYPE_NAME (decl))
11894             {
11895               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11896                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11897               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11898                        && DECL_NAME (TYPE_NAME (decl)))
11899                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11900               else
11901                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11902             }
11903         }
11904       else
11905         {
11906           e.name = dwarf2_name (decl, 1);
11907           if (e.name)
11908             e.name = xstrdup (e.name);
11909         }
11910
11911       /* If we don't have a name for the type, there's no point in adding
11912          it to the table.  */
11913       if (e.name && e.name[0] != '\0')
11914         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11915     }
11916 }
11917
11918 /* Output the public names table used to speed up access to externally
11919    visible names; or the public types table used to find type definitions.  */
11920
11921 static void
11922 output_pubnames (VEC (pubname_entry, gc) * names)
11923 {
11924   unsigned i;
11925   unsigned long pubnames_length = size_of_pubnames (names);
11926   pubname_ref pub;
11927
11928   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11929     dw2_asm_output_data (4, 0xffffffff,
11930       "Initial length escape value indicating 64-bit DWARF extension");
11931   if (names == pubname_table)
11932     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11933                          "Length of Public Names Info");
11934   else
11935     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11936                          "Length of Public Type Names Info");
11937   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11938   dw2_asm_output_data (2, 2, "DWARF Version");
11939   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11940                          debug_info_section,
11941                          "Offset of Compilation Unit Info");
11942   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11943                        "Compilation Unit Length");
11944
11945   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11946     {
11947       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11948       if (names == pubname_table)
11949         gcc_assert (pub->die->die_mark);
11950
11951       if (names != pubtype_table
11952           || pub->die->die_offset != 0
11953           || !flag_eliminate_unused_debug_types)
11954         {
11955           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11956                                "DIE offset");
11957
11958           dw2_asm_output_nstring (pub->name, -1, "external name");
11959         }
11960     }
11961
11962   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11963 }
11964
11965 /* Output the information that goes into the .debug_aranges table.
11966    Namely, define the beginning and ending address range of the
11967    text section generated for this compilation unit.  */
11968
11969 static void
11970 output_aranges (unsigned long aranges_length)
11971 {
11972   unsigned i;
11973
11974   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11975     dw2_asm_output_data (4, 0xffffffff,
11976       "Initial length escape value indicating 64-bit DWARF extension");
11977   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11978                        "Length of Address Ranges Info");
11979   /* Version number for aranges is still 2, even in DWARF3.  */
11980   dw2_asm_output_data (2, 2, "DWARF Version");
11981   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11982                          debug_info_section,
11983                          "Offset of Compilation Unit Info");
11984   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11985   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11986
11987   /* We need to align to twice the pointer size here.  */
11988   if (DWARF_ARANGES_PAD_SIZE)
11989     {
11990       /* Pad using a 2 byte words so that padding is correct for any
11991          pointer size.  */
11992       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11993                            2 * DWARF2_ADDR_SIZE);
11994       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11995         dw2_asm_output_data (2, 0, NULL);
11996     }
11997
11998   /* It is necessary not to output these entries if the sections were
11999      not used; if the sections were not used, the length will be 0 and
12000      the address may end up as 0 if the section is discarded by ld
12001      --gc-sections, leaving an invalid (0, 0) entry that can be
12002      confused with the terminator.  */
12003   if (text_section_used)
12004     {
12005       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
12006       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
12007                             text_section_label, "Length");
12008     }
12009   if (cold_text_section_used)
12010     {
12011       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
12012                            "Address");
12013       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
12014                             cold_text_section_label, "Length");
12015     }
12016
12017   if (have_multiple_function_sections)
12018     {
12019       unsigned fde_idx = 0;
12020
12021       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
12022         {
12023           dw_fde_ref fde = &fde_table[fde_idx];
12024
12025           if (!fde->in_std_section)
12026             {
12027               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
12028                                    "Address");
12029               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
12030                                     fde->dw_fde_begin, "Length");
12031             }
12032           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
12033             {
12034               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
12035                                    "Address");
12036               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
12037                                     fde->dw_fde_second_begin, "Length");
12038             }
12039         }
12040     }
12041
12042   /* Output the terminator words.  */
12043   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12044   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12045 }
12046
12047 /* Add a new entry to .debug_ranges.  Return the offset at which it
12048    was placed.  */
12049
12050 static unsigned int
12051 add_ranges_num (int num)
12052 {
12053   unsigned int in_use = ranges_table_in_use;
12054
12055   if (in_use == ranges_table_allocated)
12056     {
12057       ranges_table_allocated += RANGES_TABLE_INCREMENT;
12058       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
12059                                     ranges_table_allocated);
12060       memset (ranges_table + ranges_table_in_use, 0,
12061               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
12062     }
12063
12064   ranges_table[in_use].num = num;
12065   ranges_table_in_use = in_use + 1;
12066
12067   return in_use * 2 * DWARF2_ADDR_SIZE;
12068 }
12069
12070 /* Add a new entry to .debug_ranges corresponding to a block, or a
12071    range terminator if BLOCK is NULL.  */
12072
12073 static unsigned int
12074 add_ranges (const_tree block)
12075 {
12076   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
12077 }
12078
12079 /* Add a new entry to .debug_ranges corresponding to a pair of
12080    labels.  */
12081
12082 static void
12083 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
12084                       bool *added)
12085 {
12086   unsigned int in_use = ranges_by_label_in_use;
12087   unsigned int offset;
12088
12089   if (in_use == ranges_by_label_allocated)
12090     {
12091       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
12092       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
12093                                        ranges_by_label,
12094                                        ranges_by_label_allocated);
12095       memset (ranges_by_label + ranges_by_label_in_use, 0,
12096               RANGES_TABLE_INCREMENT
12097               * sizeof (struct dw_ranges_by_label_struct));
12098     }
12099
12100   ranges_by_label[in_use].begin = begin;
12101   ranges_by_label[in_use].end = end;
12102   ranges_by_label_in_use = in_use + 1;
12103
12104   offset = add_ranges_num (-(int)in_use - 1);
12105   if (!*added)
12106     {
12107       add_AT_range_list (die, DW_AT_ranges, offset);
12108       *added = true;
12109     }
12110 }
12111
12112 static void
12113 output_ranges (void)
12114 {
12115   unsigned i;
12116   static const char *const start_fmt = "Offset %#x";
12117   const char *fmt = start_fmt;
12118
12119   for (i = 0; i < ranges_table_in_use; i++)
12120     {
12121       int block_num = ranges_table[i].num;
12122
12123       if (block_num > 0)
12124         {
12125           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12126           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12127
12128           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12129           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12130
12131           /* If all code is in the text section, then the compilation
12132              unit base address defaults to DW_AT_low_pc, which is the
12133              base of the text section.  */
12134           if (!have_multiple_function_sections)
12135             {
12136               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12137                                     text_section_label,
12138                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12139               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12140                                     text_section_label, NULL);
12141             }
12142
12143           /* Otherwise, the compilation unit base address is zero,
12144              which allows us to use absolute addresses, and not worry
12145              about whether the target supports cross-section
12146              arithmetic.  */
12147           else
12148             {
12149               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12150                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12151               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12152             }
12153
12154           fmt = NULL;
12155         }
12156
12157       /* Negative block_num stands for an index into ranges_by_label.  */
12158       else if (block_num < 0)
12159         {
12160           int lab_idx = - block_num - 1;
12161
12162           if (!have_multiple_function_sections)
12163             {
12164               gcc_unreachable ();
12165 #if 0
12166               /* If we ever use add_ranges_by_labels () for a single
12167                  function section, all we have to do is to take out
12168                  the #if 0 above.  */
12169               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12170                                     ranges_by_label[lab_idx].begin,
12171                                     text_section_label,
12172                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12173               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12174                                     ranges_by_label[lab_idx].end,
12175                                     text_section_label, NULL);
12176 #endif
12177             }
12178           else
12179             {
12180               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12181                                    ranges_by_label[lab_idx].begin,
12182                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12183               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12184                                    ranges_by_label[lab_idx].end,
12185                                    NULL);
12186             }
12187         }
12188       else
12189         {
12190           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12191           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12192           fmt = start_fmt;
12193         }
12194     }
12195 }
12196
12197 /* Data structure containing information about input files.  */
12198 struct file_info
12199 {
12200   const char *path;     /* Complete file name.  */
12201   const char *fname;    /* File name part.  */
12202   int length;           /* Length of entire string.  */
12203   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12204   int dir_idx;          /* Index in directory table.  */
12205 };
12206
12207 /* Data structure containing information about directories with source
12208    files.  */
12209 struct dir_info
12210 {
12211   const char *path;     /* Path including directory name.  */
12212   int length;           /* Path length.  */
12213   int prefix;           /* Index of directory entry which is a prefix.  */
12214   int count;            /* Number of files in this directory.  */
12215   int dir_idx;          /* Index of directory used as base.  */
12216 };
12217
12218 /* Callback function for file_info comparison.  We sort by looking at
12219    the directories in the path.  */
12220
12221 static int
12222 file_info_cmp (const void *p1, const void *p2)
12223 {
12224   const struct file_info *const s1 = (const struct file_info *) p1;
12225   const struct file_info *const s2 = (const struct file_info *) p2;
12226   const unsigned char *cp1;
12227   const unsigned char *cp2;
12228
12229   /* Take care of file names without directories.  We need to make sure that
12230      we return consistent values to qsort since some will get confused if
12231      we return the same value when identical operands are passed in opposite
12232      orders.  So if neither has a directory, return 0 and otherwise return
12233      1 or -1 depending on which one has the directory.  */
12234   if ((s1->path == s1->fname || s2->path == s2->fname))
12235     return (s2->path == s2->fname) - (s1->path == s1->fname);
12236
12237   cp1 = (const unsigned char *) s1->path;
12238   cp2 = (const unsigned char *) s2->path;
12239
12240   while (1)
12241     {
12242       ++cp1;
12243       ++cp2;
12244       /* Reached the end of the first path?  If so, handle like above.  */
12245       if ((cp1 == (const unsigned char *) s1->fname)
12246           || (cp2 == (const unsigned char *) s2->fname))
12247         return ((cp2 == (const unsigned char *) s2->fname)
12248                 - (cp1 == (const unsigned char *) s1->fname));
12249
12250       /* Character of current path component the same?  */
12251       else if (*cp1 != *cp2)
12252         return *cp1 - *cp2;
12253     }
12254 }
12255
12256 struct file_name_acquire_data
12257 {
12258   struct file_info *files;
12259   int used_files;
12260   int max_files;
12261 };
12262
12263 /* Traversal function for the hash table.  */
12264
12265 static int
12266 file_name_acquire (void ** slot, void *data)
12267 {
12268   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12269   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12270   struct file_info *fi;
12271   const char *f;
12272
12273   gcc_assert (fnad->max_files >= d->emitted_number);
12274
12275   if (! d->emitted_number)
12276     return 1;
12277
12278   gcc_assert (fnad->max_files != fnad->used_files);
12279
12280   fi = fnad->files + fnad->used_files++;
12281
12282   /* Skip all leading "./".  */
12283   f = d->filename;
12284   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12285     f += 2;
12286
12287   /* Create a new array entry.  */
12288   fi->path = f;
12289   fi->length = strlen (f);
12290   fi->file_idx = d;
12291
12292   /* Search for the file name part.  */
12293   f = strrchr (f, DIR_SEPARATOR);
12294 #if defined (DIR_SEPARATOR_2)
12295   {
12296     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12297
12298     if (g != NULL)
12299       {
12300         if (f == NULL || f < g)
12301           f = g;
12302       }
12303   }
12304 #endif
12305
12306   fi->fname = f == NULL ? fi->path : f + 1;
12307   return 1;
12308 }
12309
12310 /* Output the directory table and the file name table.  We try to minimize
12311    the total amount of memory needed.  A heuristic is used to avoid large
12312    slowdowns with many input files.  */
12313
12314 static void
12315 output_file_names (void)
12316 {
12317   struct file_name_acquire_data fnad;
12318   int numfiles;
12319   struct file_info *files;
12320   struct dir_info *dirs;
12321   int *saved;
12322   int *savehere;
12323   int *backmap;
12324   int ndirs;
12325   int idx_offset;
12326   int i;
12327
12328   if (!last_emitted_file)
12329     {
12330       dw2_asm_output_data (1, 0, "End directory table");
12331       dw2_asm_output_data (1, 0, "End file name table");
12332       return;
12333     }
12334
12335   numfiles = last_emitted_file->emitted_number;
12336
12337   /* Allocate the various arrays we need.  */
12338   files = XALLOCAVEC (struct file_info, numfiles);
12339   dirs = XALLOCAVEC (struct dir_info, numfiles);
12340
12341   fnad.files = files;
12342   fnad.used_files = 0;
12343   fnad.max_files = numfiles;
12344   htab_traverse (file_table, file_name_acquire, &fnad);
12345   gcc_assert (fnad.used_files == fnad.max_files);
12346
12347   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12348
12349   /* Find all the different directories used.  */
12350   dirs[0].path = files[0].path;
12351   dirs[0].length = files[0].fname - files[0].path;
12352   dirs[0].prefix = -1;
12353   dirs[0].count = 1;
12354   dirs[0].dir_idx = 0;
12355   files[0].dir_idx = 0;
12356   ndirs = 1;
12357
12358   for (i = 1; i < numfiles; i++)
12359     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12360         && memcmp (dirs[ndirs - 1].path, files[i].path,
12361                    dirs[ndirs - 1].length) == 0)
12362       {
12363         /* Same directory as last entry.  */
12364         files[i].dir_idx = ndirs - 1;
12365         ++dirs[ndirs - 1].count;
12366       }
12367     else
12368       {
12369         int j;
12370
12371         /* This is a new directory.  */
12372         dirs[ndirs].path = files[i].path;
12373         dirs[ndirs].length = files[i].fname - files[i].path;
12374         dirs[ndirs].count = 1;
12375         dirs[ndirs].dir_idx = ndirs;
12376         files[i].dir_idx = ndirs;
12377
12378         /* Search for a prefix.  */
12379         dirs[ndirs].prefix = -1;
12380         for (j = 0; j < ndirs; j++)
12381           if (dirs[j].length < dirs[ndirs].length
12382               && dirs[j].length > 1
12383               && (dirs[ndirs].prefix == -1
12384                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12385               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12386             dirs[ndirs].prefix = j;
12387
12388         ++ndirs;
12389       }
12390
12391   /* Now to the actual work.  We have to find a subset of the directories which
12392      allow expressing the file name using references to the directory table
12393      with the least amount of characters.  We do not do an exhaustive search
12394      where we would have to check out every combination of every single
12395      possible prefix.  Instead we use a heuristic which provides nearly optimal
12396      results in most cases and never is much off.  */
12397   saved = XALLOCAVEC (int, ndirs);
12398   savehere = XALLOCAVEC (int, ndirs);
12399
12400   memset (saved, '\0', ndirs * sizeof (saved[0]));
12401   for (i = 0; i < ndirs; i++)
12402     {
12403       int j;
12404       int total;
12405
12406       /* We can always save some space for the current directory.  But this
12407          does not mean it will be enough to justify adding the directory.  */
12408       savehere[i] = dirs[i].length;
12409       total = (savehere[i] - saved[i]) * dirs[i].count;
12410
12411       for (j = i + 1; j < ndirs; j++)
12412         {
12413           savehere[j] = 0;
12414           if (saved[j] < dirs[i].length)
12415             {
12416               /* Determine whether the dirs[i] path is a prefix of the
12417                  dirs[j] path.  */
12418               int k;
12419
12420               k = dirs[j].prefix;
12421               while (k != -1 && k != (int) i)
12422                 k = dirs[k].prefix;
12423
12424               if (k == (int) i)
12425                 {
12426                   /* Yes it is.  We can possibly save some memory by
12427                      writing the filenames in dirs[j] relative to
12428                      dirs[i].  */
12429                   savehere[j] = dirs[i].length;
12430                   total += (savehere[j] - saved[j]) * dirs[j].count;
12431                 }
12432             }
12433         }
12434
12435       /* Check whether we can save enough to justify adding the dirs[i]
12436          directory.  */
12437       if (total > dirs[i].length + 1)
12438         {
12439           /* It's worthwhile adding.  */
12440           for (j = i; j < ndirs; j++)
12441             if (savehere[j] > 0)
12442               {
12443                 /* Remember how much we saved for this directory so far.  */
12444                 saved[j] = savehere[j];
12445
12446                 /* Remember the prefix directory.  */
12447                 dirs[j].dir_idx = i;
12448               }
12449         }
12450     }
12451
12452   /* Emit the directory name table.  */
12453   idx_offset = dirs[0].length > 0 ? 1 : 0;
12454   for (i = 1 - idx_offset; i < ndirs; i++)
12455     dw2_asm_output_nstring (dirs[i].path,
12456                             dirs[i].length
12457                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12458                             "Directory Entry: %#x", i + idx_offset);
12459
12460   dw2_asm_output_data (1, 0, "End directory table");
12461
12462   /* We have to emit them in the order of emitted_number since that's
12463      used in the debug info generation.  To do this efficiently we
12464      generate a back-mapping of the indices first.  */
12465   backmap = XALLOCAVEC (int, numfiles);
12466   for (i = 0; i < numfiles; i++)
12467     backmap[files[i].file_idx->emitted_number - 1] = i;
12468
12469   /* Now write all the file names.  */
12470   for (i = 0; i < numfiles; i++)
12471     {
12472       int file_idx = backmap[i];
12473       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12474
12475 #ifdef VMS_DEBUGGING_INFO
12476 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12477
12478       /* Setting these fields can lead to debugger miscomparisons,
12479          but VMS Debug requires them to be set correctly.  */
12480
12481       int ver;
12482       long long cdt;
12483       long siz;
12484       int maxfilelen = strlen (files[file_idx].path)
12485                                + dirs[dir_idx].length
12486                                + MAX_VMS_VERSION_LEN + 1;
12487       char *filebuf = XALLOCAVEC (char, maxfilelen);
12488
12489       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12490       snprintf (filebuf, maxfilelen, "%s;%d",
12491                 files[file_idx].path + dirs[dir_idx].length, ver);
12492
12493       dw2_asm_output_nstring
12494         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12495
12496       /* Include directory index.  */
12497       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12498
12499       /* Modification time.  */
12500       dw2_asm_output_data_uleb128
12501         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12502           ? cdt : 0,
12503          NULL);
12504
12505       /* File length in bytes.  */
12506       dw2_asm_output_data_uleb128
12507         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12508           ? siz : 0,
12509          NULL);
12510 #else
12511       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12512                               "File Entry: %#x", (unsigned) i + 1);
12513
12514       /* Include directory index.  */
12515       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12516
12517       /* Modification time.  */
12518       dw2_asm_output_data_uleb128 (0, NULL);
12519
12520       /* File length in bytes.  */
12521       dw2_asm_output_data_uleb128 (0, NULL);
12522 #endif /* VMS_DEBUGGING_INFO */
12523     }
12524
12525   dw2_asm_output_data (1, 0, "End file name table");
12526 }
12527
12528
12529 /* Output one line number table into the .debug_line section.  */
12530
12531 static void
12532 output_one_line_info_table (dw_line_info_table *table)
12533 {
12534   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12535   unsigned int current_line = 1;
12536   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12537   dw_line_info_entry *ent;
12538   size_t i;
12539
12540   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12541     {
12542       switch (ent->opcode)
12543         {
12544         case LI_set_address:
12545           /* ??? Unfortunately, we have little choice here currently, and
12546              must always use the most general form.  GCC does not know the
12547              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12548              ports do have length attributes which will give an upper bound
12549              on the address range.  We could perhaps use length attributes
12550              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12551           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12552
12553           /* This can handle any delta.  This takes
12554              4+DWARF2_ADDR_SIZE bytes.  */
12555           dw2_asm_output_data (1, 0, "set address %s", line_label);
12556           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12557           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12558           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12559           break;
12560
12561         case LI_set_line:
12562           if (ent->val == current_line)
12563             {
12564               /* We still need to start a new row, so output a copy insn.  */
12565               dw2_asm_output_data (1, DW_LNS_copy,
12566                                    "copy line %u", current_line);
12567             }
12568           else
12569             {
12570               int line_offset = ent->val - current_line;
12571               int line_delta = line_offset - DWARF_LINE_BASE;
12572
12573               current_line = ent->val;
12574               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12575                 {
12576                   /* This can handle deltas from -10 to 234, using the current
12577                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12578                      This takes 1 byte.  */
12579                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12580                                        "line %u", current_line);
12581                 }
12582               else
12583                 {
12584                   /* This can handle any delta.  This takes at least 4 bytes,
12585                      depending on the value being encoded.  */
12586                   dw2_asm_output_data (1, DW_LNS_advance_line,
12587                                        "advance to line %u", current_line);
12588                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12589                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12590                 }
12591             }
12592           break;
12593
12594         case LI_set_file:
12595           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12596           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12597           break;
12598
12599         case LI_set_column:
12600           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12601           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12602           break;
12603
12604         case LI_negate_stmt:
12605           current_is_stmt = !current_is_stmt;
12606           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12607                                "is_stmt %d", current_is_stmt);
12608           break;
12609
12610         case LI_set_prologue_end:
12611           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12612                                "set prologue end");
12613           break;
12614           
12615         case LI_set_epilogue_begin:
12616           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12617                                "set epilogue begin");
12618           break;
12619
12620         case LI_set_discriminator:
12621           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12622           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12623           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12624           dw2_asm_output_data_uleb128 (ent->val, NULL);
12625           break;
12626         }
12627     }
12628
12629   /* Emit debug info for the address of the end of the table.  */
12630   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12631   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12632   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12633   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12634
12635   dw2_asm_output_data (1, 0, "end sequence");
12636   dw2_asm_output_data_uleb128 (1, NULL);
12637   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12638 }
12639
12640 /* Output the source line number correspondence information.  This
12641    information goes into the .debug_line section.  */
12642
12643 static void
12644 output_line_info (void)
12645 {
12646   char l1[20], l2[20], p1[20], p2[20];
12647   int ver = dwarf_version;
12648   bool saw_one = false;
12649   int opc;
12650
12651   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12652   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12653   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12654   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12655
12656   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12657     dw2_asm_output_data (4, 0xffffffff,
12658       "Initial length escape value indicating 64-bit DWARF extension");
12659   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12660                         "Length of Source Line Info");
12661   ASM_OUTPUT_LABEL (asm_out_file, l1);
12662
12663   dw2_asm_output_data (2, ver, "DWARF Version");
12664   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12665   ASM_OUTPUT_LABEL (asm_out_file, p1);
12666
12667   /* Define the architecture-dependent minimum instruction length (in bytes).
12668      In this implementation of DWARF, this field is used for information
12669      purposes only.  Since GCC generates assembly language, we have no
12670      a priori knowledge of how many instruction bytes are generated for each
12671      source line, and therefore can use only the DW_LNE_set_address and
12672      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12673      this as '1', which is "correct enough" for all architectures,
12674      and don't let the target override.  */
12675   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12676
12677   if (ver >= 4)
12678     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12679                          "Maximum Operations Per Instruction");
12680   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12681                        "Default is_stmt_start flag");
12682   dw2_asm_output_data (1, DWARF_LINE_BASE,
12683                        "Line Base Value (Special Opcodes)");
12684   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12685                        "Line Range Value (Special Opcodes)");
12686   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12687                        "Special Opcode Base");
12688
12689   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12690     {
12691       int n_op_args;
12692       switch (opc)
12693         {
12694         case DW_LNS_advance_pc:
12695         case DW_LNS_advance_line:
12696         case DW_LNS_set_file:
12697         case DW_LNS_set_column:
12698         case DW_LNS_fixed_advance_pc:
12699         case DW_LNS_set_isa:
12700           n_op_args = 1;
12701           break;
12702         default:
12703           n_op_args = 0;
12704           break;
12705         }
12706
12707       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12708                            opc, n_op_args);
12709     }
12710
12711   /* Write out the information about the files we use.  */
12712   output_file_names ();
12713   ASM_OUTPUT_LABEL (asm_out_file, p2);
12714
12715   if (separate_line_info)
12716     {
12717       dw_line_info_table *table;
12718       size_t i;
12719
12720       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12721         if (table->in_use)
12722           {
12723             output_one_line_info_table (table);
12724             saw_one = true;
12725           }
12726     }
12727   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12728     {
12729       output_one_line_info_table (cold_text_section_line_info);
12730       saw_one = true;
12731     }
12732
12733   /* ??? Some Darwin linkers crash on a .debug_line section with no
12734      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12735      sufficient -- the address column must also be initialized.
12736      Make sure to output at least one set_address/end_sequence pair,
12737      choosing .text since that section is always present.  */
12738   if (text_section_line_info->in_use || !saw_one)
12739     output_one_line_info_table (text_section_line_info);
12740
12741   /* Output the marker for the end of the line number info.  */
12742   ASM_OUTPUT_LABEL (asm_out_file, l2);
12743 }
12744 \f
12745 /* Given a pointer to a tree node for some base type, return a pointer to
12746    a DIE that describes the given type.
12747
12748    This routine must only be called for GCC type nodes that correspond to
12749    Dwarf base (fundamental) types.  */
12750
12751 static dw_die_ref
12752 base_type_die (tree type)
12753 {
12754   dw_die_ref base_type_result;
12755   enum dwarf_type encoding;
12756
12757   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12758     return 0;
12759
12760   /* If this is a subtype that should not be emitted as a subrange type,
12761      use the base type.  See subrange_type_for_debug_p.  */
12762   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12763     type = TREE_TYPE (type);
12764
12765   switch (TREE_CODE (type))
12766     {
12767     case INTEGER_TYPE:
12768       if ((dwarf_version >= 4 || !dwarf_strict)
12769           && TYPE_NAME (type)
12770           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12771           && DECL_IS_BUILTIN (TYPE_NAME (type))
12772           && DECL_NAME (TYPE_NAME (type)))
12773         {
12774           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12775           if (strcmp (name, "char16_t") == 0
12776               || strcmp (name, "char32_t") == 0)
12777             {
12778               encoding = DW_ATE_UTF;
12779               break;
12780             }
12781         }
12782       if (TYPE_STRING_FLAG (type))
12783         {
12784           if (TYPE_UNSIGNED (type))
12785             encoding = DW_ATE_unsigned_char;
12786           else
12787             encoding = DW_ATE_signed_char;
12788         }
12789       else if (TYPE_UNSIGNED (type))
12790         encoding = DW_ATE_unsigned;
12791       else
12792         encoding = DW_ATE_signed;
12793       break;
12794
12795     case REAL_TYPE:
12796       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12797         {
12798           if (dwarf_version >= 3 || !dwarf_strict)
12799             encoding = DW_ATE_decimal_float;
12800           else
12801             encoding = DW_ATE_lo_user;
12802         }
12803       else
12804         encoding = DW_ATE_float;
12805       break;
12806
12807     case FIXED_POINT_TYPE:
12808       if (!(dwarf_version >= 3 || !dwarf_strict))
12809         encoding = DW_ATE_lo_user;
12810       else if (TYPE_UNSIGNED (type))
12811         encoding = DW_ATE_unsigned_fixed;
12812       else
12813         encoding = DW_ATE_signed_fixed;
12814       break;
12815
12816       /* Dwarf2 doesn't know anything about complex ints, so use
12817          a user defined type for it.  */
12818     case COMPLEX_TYPE:
12819       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12820         encoding = DW_ATE_complex_float;
12821       else
12822         encoding = DW_ATE_lo_user;
12823       break;
12824
12825     case BOOLEAN_TYPE:
12826       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12827       encoding = DW_ATE_boolean;
12828       break;
12829
12830     default:
12831       /* No other TREE_CODEs are Dwarf fundamental types.  */
12832       gcc_unreachable ();
12833     }
12834
12835   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12836
12837   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12838                    int_size_in_bytes (type));
12839   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12840
12841   return base_type_result;
12842 }
12843
12844 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12845    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12846
12847 static inline int
12848 is_base_type (tree type)
12849 {
12850   switch (TREE_CODE (type))
12851     {
12852     case ERROR_MARK:
12853     case VOID_TYPE:
12854     case INTEGER_TYPE:
12855     case REAL_TYPE:
12856     case FIXED_POINT_TYPE:
12857     case COMPLEX_TYPE:
12858     case BOOLEAN_TYPE:
12859       return 1;
12860
12861     case ARRAY_TYPE:
12862     case RECORD_TYPE:
12863     case UNION_TYPE:
12864     case QUAL_UNION_TYPE:
12865     case ENUMERAL_TYPE:
12866     case FUNCTION_TYPE:
12867     case METHOD_TYPE:
12868     case POINTER_TYPE:
12869     case REFERENCE_TYPE:
12870     case NULLPTR_TYPE:
12871     case OFFSET_TYPE:
12872     case LANG_TYPE:
12873     case VECTOR_TYPE:
12874       return 0;
12875
12876     default:
12877       gcc_unreachable ();
12878     }
12879
12880   return 0;
12881 }
12882
12883 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12884    node, return the size in bits for the type if it is a constant, or else
12885    return the alignment for the type if the type's size is not constant, or
12886    else return BITS_PER_WORD if the type actually turns out to be an
12887    ERROR_MARK node.  */
12888
12889 static inline unsigned HOST_WIDE_INT
12890 simple_type_size_in_bits (const_tree type)
12891 {
12892   if (TREE_CODE (type) == ERROR_MARK)
12893     return BITS_PER_WORD;
12894   else if (TYPE_SIZE (type) == NULL_TREE)
12895     return 0;
12896   else if (host_integerp (TYPE_SIZE (type), 1))
12897     return tree_low_cst (TYPE_SIZE (type), 1);
12898   else
12899     return TYPE_ALIGN (type);
12900 }
12901
12902 /* Similarly, but return a double_int instead of UHWI.  */
12903
12904 static inline double_int
12905 double_int_type_size_in_bits (const_tree type)
12906 {
12907   if (TREE_CODE (type) == ERROR_MARK)
12908     return uhwi_to_double_int (BITS_PER_WORD);
12909   else if (TYPE_SIZE (type) == NULL_TREE)
12910     return double_int_zero;
12911   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12912     return tree_to_double_int (TYPE_SIZE (type));
12913   else
12914     return uhwi_to_double_int (TYPE_ALIGN (type));
12915 }
12916
12917 /*  Given a pointer to a tree node for a subrange type, return a pointer
12918     to a DIE that describes the given type.  */
12919
12920 static dw_die_ref
12921 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12922 {
12923   dw_die_ref subrange_die;
12924   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12925
12926   if (context_die == NULL)
12927     context_die = comp_unit_die ();
12928
12929   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12930
12931   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12932     {
12933       /* The size of the subrange type and its base type do not match,
12934          so we need to generate a size attribute for the subrange type.  */
12935       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12936     }
12937
12938   if (low)
12939     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12940   if (high)
12941     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12942
12943   return subrange_die;
12944 }
12945
12946 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12947    entry that chains various modifiers in front of the given type.  */
12948
12949 static dw_die_ref
12950 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12951                    dw_die_ref context_die)
12952 {
12953   enum tree_code code = TREE_CODE (type);
12954   dw_die_ref mod_type_die;
12955   dw_die_ref sub_die = NULL;
12956   tree item_type = NULL;
12957   tree qualified_type;
12958   tree name, low, high;
12959
12960   if (code == ERROR_MARK)
12961     return NULL;
12962
12963   /* See if we already have the appropriately qualified variant of
12964      this type.  */
12965   qualified_type
12966     = get_qualified_type (type,
12967                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12968                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12969
12970   if (qualified_type == sizetype
12971       && TYPE_NAME (qualified_type)
12972       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12973     {
12974       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12975
12976       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12977                            && TYPE_PRECISION (t)
12978                            == TYPE_PRECISION (qualified_type)
12979                            && TYPE_UNSIGNED (t)
12980                            == TYPE_UNSIGNED (qualified_type));
12981       qualified_type = t;
12982     }
12983
12984   /* If we do, then we can just use its DIE, if it exists.  */
12985   if (qualified_type)
12986     {
12987       mod_type_die = lookup_type_die (qualified_type);
12988       if (mod_type_die)
12989         return mod_type_die;
12990     }
12991
12992   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12993
12994   /* Handle C typedef types.  */
12995   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12996       && !DECL_ARTIFICIAL (name))
12997     {
12998       tree dtype = TREE_TYPE (name);
12999
13000       if (qualified_type == dtype)
13001         {
13002           /* For a named type, use the typedef.  */
13003           gen_type_die (qualified_type, context_die);
13004           return lookup_type_die (qualified_type);
13005         }
13006       else if (is_const_type < TYPE_READONLY (dtype)
13007                || is_volatile_type < TYPE_VOLATILE (dtype)
13008                || (is_const_type <= TYPE_READONLY (dtype)
13009                    && is_volatile_type <= TYPE_VOLATILE (dtype)
13010                    && DECL_ORIGINAL_TYPE (name) != type))
13011         /* cv-unqualified version of named type.  Just use the unnamed
13012            type to which it refers.  */
13013         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13014                                   is_const_type, is_volatile_type,
13015                                   context_die);
13016       /* Else cv-qualified version of named type; fall through.  */
13017     }
13018
13019   if (is_const_type
13020       /* If both is_const_type and is_volatile_type, prefer the path
13021          which leads to a qualified type.  */
13022       && (!is_volatile_type
13023           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13024           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13025     {
13026       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13027       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13028     }
13029   else if (is_volatile_type)
13030     {
13031       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13032       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13033     }
13034   else if (code == POINTER_TYPE)
13035     {
13036       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13037       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13038                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13039       item_type = TREE_TYPE (type);
13040       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13041         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13042                          TYPE_ADDR_SPACE (item_type));
13043     }
13044   else if (code == REFERENCE_TYPE)
13045     {
13046       if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
13047         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13048                                 type);
13049       else
13050         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13051       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13052                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13053       item_type = TREE_TYPE (type);
13054       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13055         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13056                          TYPE_ADDR_SPACE (item_type));
13057     }
13058   else if (code == INTEGER_TYPE
13059            && TREE_TYPE (type) != NULL_TREE
13060            && subrange_type_for_debug_p (type, &low, &high))
13061     {
13062       mod_type_die = subrange_type_die (type, low, high, context_die);
13063       item_type = TREE_TYPE (type);
13064     }
13065   else if (is_base_type (type))
13066     mod_type_die = base_type_die (type);
13067   else
13068     {
13069       gen_type_die (type, context_die);
13070
13071       /* We have to get the type_main_variant here (and pass that to the
13072          `lookup_type_die' routine) because the ..._TYPE node we have
13073          might simply be a *copy* of some original type node (where the
13074          copy was created to help us keep track of typedef names) and
13075          that copy might have a different TYPE_UID from the original
13076          ..._TYPE node.  */
13077       if (TREE_CODE (type) != VECTOR_TYPE)
13078         return lookup_type_die (type_main_variant (type));
13079       else
13080         /* Vectors have the debugging information in the type,
13081            not the main variant.  */
13082         return lookup_type_die (type);
13083     }
13084
13085   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13086      don't output a DW_TAG_typedef, since there isn't one in the
13087      user's program; just attach a DW_AT_name to the type.
13088      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13089      if the base type already has the same name.  */
13090   if (name
13091       && ((TREE_CODE (name) != TYPE_DECL
13092            && (qualified_type == TYPE_MAIN_VARIANT (type)
13093                || (!is_const_type && !is_volatile_type)))
13094           || (TREE_CODE (name) == TYPE_DECL
13095               && TREE_TYPE (name) == qualified_type
13096               && DECL_NAME (name))))
13097     {
13098       if (TREE_CODE (name) == TYPE_DECL)
13099         /* Could just call add_name_and_src_coords_attributes here,
13100            but since this is a builtin type it doesn't have any
13101            useful source coordinates anyway.  */
13102         name = DECL_NAME (name);
13103       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13104       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13105     }
13106   /* This probably indicates a bug.  */
13107   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13108     add_name_attribute (mod_type_die, "__unknown__");
13109
13110   if (qualified_type)
13111     equate_type_number_to_die (qualified_type, mod_type_die);
13112
13113   if (item_type)
13114     /* We must do this after the equate_type_number_to_die call, in case
13115        this is a recursive type.  This ensures that the modified_type_die
13116        recursion will terminate even if the type is recursive.  Recursive
13117        types are possible in Ada.  */
13118     sub_die = modified_type_die (item_type,
13119                                  TYPE_READONLY (item_type),
13120                                  TYPE_VOLATILE (item_type),
13121                                  context_die);
13122
13123   if (sub_die != NULL)
13124     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13125
13126   return mod_type_die;
13127 }
13128
13129 /* Generate DIEs for the generic parameters of T.
13130    T must be either a generic type or a generic function.
13131    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13132
13133 static void
13134 gen_generic_params_dies (tree t)
13135 {
13136   tree parms, args;
13137   int parms_num, i;
13138   dw_die_ref die = NULL;
13139
13140   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13141     return;
13142
13143   if (TYPE_P (t))
13144     die = lookup_type_die (t);
13145   else if (DECL_P (t))
13146     die = lookup_decl_die (t);
13147
13148   gcc_assert (die);
13149
13150   parms = lang_hooks.get_innermost_generic_parms (t);
13151   if (!parms)
13152     /* T has no generic parameter. It means T is neither a generic type
13153        or function. End of story.  */
13154     return;
13155
13156   parms_num = TREE_VEC_LENGTH (parms);
13157   args = lang_hooks.get_innermost_generic_args (t);
13158   for (i = 0; i < parms_num; i++)
13159     {
13160       tree parm, arg, arg_pack_elems;
13161
13162       parm = TREE_VEC_ELT (parms, i);
13163       arg = TREE_VEC_ELT (args, i);
13164       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13165       gcc_assert (parm && TREE_VALUE (parm) && arg);
13166
13167       if (parm && TREE_VALUE (parm) && arg)
13168         {
13169           /* If PARM represents a template parameter pack,
13170              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13171              by DW_TAG_template_*_parameter DIEs for the argument
13172              pack elements of ARG. Note that ARG would then be
13173              an argument pack.  */
13174           if (arg_pack_elems)
13175             template_parameter_pack_die (TREE_VALUE (parm),
13176                                          arg_pack_elems,
13177                                          die);
13178           else
13179             generic_parameter_die (TREE_VALUE (parm), arg,
13180                                    true /* Emit DW_AT_name */, die);
13181         }
13182     }
13183 }
13184
13185 /* Create and return a DIE for PARM which should be
13186    the representation of a generic type parameter.
13187    For instance, in the C++ front end, PARM would be a template parameter.
13188    ARG is the argument to PARM.
13189    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13190    name of the PARM.
13191    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13192    as a child node.  */
13193
13194 static dw_die_ref
13195 generic_parameter_die (tree parm, tree arg,
13196                        bool emit_name_p,
13197                        dw_die_ref parent_die)
13198 {
13199   dw_die_ref tmpl_die = NULL;
13200   const char *name = NULL;
13201
13202   if (!parm || !DECL_NAME (parm) || !arg)
13203     return NULL;
13204
13205   /* We support non-type generic parameters and arguments,
13206      type generic parameters and arguments, as well as
13207      generic generic parameters (a.k.a. template template parameters in C++)
13208      and arguments.  */
13209   if (TREE_CODE (parm) == PARM_DECL)
13210     /* PARM is a nontype generic parameter  */
13211     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13212   else if (TREE_CODE (parm) == TYPE_DECL)
13213     /* PARM is a type generic parameter.  */
13214     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13215   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13216     /* PARM is a generic generic parameter.
13217        Its DIE is a GNU extension. It shall have a
13218        DW_AT_name attribute to represent the name of the template template
13219        parameter, and a DW_AT_GNU_template_name attribute to represent the
13220        name of the template template argument.  */
13221     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13222                         parent_die, parm);
13223   else
13224     gcc_unreachable ();
13225
13226   if (tmpl_die)
13227     {
13228       tree tmpl_type;
13229
13230       /* If PARM is a generic parameter pack, it means we are
13231          emitting debug info for a template argument pack element.
13232          In other terms, ARG is a template argument pack element.
13233          In that case, we don't emit any DW_AT_name attribute for
13234          the die.  */
13235       if (emit_name_p)
13236         {
13237           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13238           gcc_assert (name);
13239           add_AT_string (tmpl_die, DW_AT_name, name);
13240         }
13241
13242       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13243         {
13244           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13245              TMPL_DIE should have a child DW_AT_type attribute that is set
13246              to the type of the argument to PARM, which is ARG.
13247              If PARM is a type generic parameter, TMPL_DIE should have a
13248              child DW_AT_type that is set to ARG.  */
13249           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13250           add_type_attribute (tmpl_die, tmpl_type, 0,
13251                               TREE_THIS_VOLATILE (tmpl_type),
13252                               parent_die);
13253         }
13254       else
13255         {
13256           /* So TMPL_DIE is a DIE representing a
13257              a generic generic template parameter, a.k.a template template
13258              parameter in C++ and arg is a template.  */
13259
13260           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13261              to the name of the argument.  */
13262           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13263           if (name)
13264             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13265         }
13266
13267       if (TREE_CODE (parm) == PARM_DECL)
13268         /* So PARM is a non-type generic parameter.
13269            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13270            attribute of TMPL_DIE which value represents the value
13271            of ARG.
13272            We must be careful here:
13273            The value of ARG might reference some function decls.
13274            We might currently be emitting debug info for a generic
13275            type and types are emitted before function decls, we don't
13276            know if the function decls referenced by ARG will actually be
13277            emitted after cgraph computations.
13278            So must defer the generation of the DW_AT_const_value to
13279            after cgraph is ready.  */
13280         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13281     }
13282
13283   return tmpl_die;
13284 }
13285
13286 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13287    PARM_PACK must be a template parameter pack. The returned DIE
13288    will be child DIE of PARENT_DIE.  */
13289
13290 static dw_die_ref
13291 template_parameter_pack_die (tree parm_pack,
13292                              tree parm_pack_args,
13293                              dw_die_ref parent_die)
13294 {
13295   dw_die_ref die;
13296   int j;
13297
13298   gcc_assert (parent_die && parm_pack);
13299
13300   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13301   add_name_and_src_coords_attributes (die, parm_pack);
13302   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13303     generic_parameter_die (parm_pack,
13304                            TREE_VEC_ELT (parm_pack_args, j),
13305                            false /* Don't emit DW_AT_name */,
13306                            die);
13307   return die;
13308 }
13309
13310 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13311    an enumerated type.  */
13312
13313 static inline int
13314 type_is_enum (const_tree type)
13315 {
13316   return TREE_CODE (type) == ENUMERAL_TYPE;
13317 }
13318
13319 /* Return the DBX register number described by a given RTL node.  */
13320
13321 static unsigned int
13322 dbx_reg_number (const_rtx rtl)
13323 {
13324   unsigned regno = REGNO (rtl);
13325
13326   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13327
13328 #ifdef LEAF_REG_REMAP
13329   if (current_function_uses_only_leaf_regs)
13330     {
13331       int leaf_reg = LEAF_REG_REMAP (regno);
13332       if (leaf_reg != -1)
13333         regno = (unsigned) leaf_reg;
13334     }
13335 #endif
13336
13337   return DBX_REGISTER_NUMBER (regno);
13338 }
13339
13340 /* Optionally add a DW_OP_piece term to a location description expression.
13341    DW_OP_piece is only added if the location description expression already
13342    doesn't end with DW_OP_piece.  */
13343
13344 static void
13345 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13346 {
13347   dw_loc_descr_ref loc;
13348
13349   if (*list_head != NULL)
13350     {
13351       /* Find the end of the chain.  */
13352       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13353         ;
13354
13355       if (loc->dw_loc_opc != DW_OP_piece)
13356         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13357     }
13358 }
13359
13360 /* Return a location descriptor that designates a machine register or
13361    zero if there is none.  */
13362
13363 static dw_loc_descr_ref
13364 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13365 {
13366   rtx regs;
13367
13368   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13369     return 0;
13370
13371   /* We only use "frame base" when we're sure we're talking about the
13372      post-prologue local stack frame.  We do this by *not* running
13373      register elimination until this point, and recognizing the special
13374      argument pointer and soft frame pointer rtx's.
13375      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13376   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13377       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13378     {
13379       dw_loc_descr_ref result = NULL;
13380
13381       if (dwarf_version >= 4 || !dwarf_strict)
13382         {
13383           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13384                                        initialized);
13385           if (result)
13386             add_loc_descr (&result,
13387                            new_loc_descr (DW_OP_stack_value, 0, 0));
13388         }
13389       return result;
13390     }
13391
13392   regs = targetm.dwarf_register_span (rtl);
13393
13394   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13395     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13396   else
13397     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13398 }
13399
13400 /* Return a location descriptor that designates a machine register for
13401    a given hard register number.  */
13402
13403 static dw_loc_descr_ref
13404 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13405 {
13406   dw_loc_descr_ref reg_loc_descr;
13407
13408   if (regno <= 31)
13409     reg_loc_descr
13410       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13411   else
13412     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13413
13414   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13415     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13416
13417   return reg_loc_descr;
13418 }
13419
13420 /* Given an RTL of a register, return a location descriptor that
13421    designates a value that spans more than one register.  */
13422
13423 static dw_loc_descr_ref
13424 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13425                              enum var_init_status initialized)
13426 {
13427   int nregs, size, i;
13428   unsigned reg;
13429   dw_loc_descr_ref loc_result = NULL;
13430
13431   reg = REGNO (rtl);
13432 #ifdef LEAF_REG_REMAP
13433   if (current_function_uses_only_leaf_regs)
13434     {
13435       int leaf_reg = LEAF_REG_REMAP (reg);
13436       if (leaf_reg != -1)
13437         reg = (unsigned) leaf_reg;
13438     }
13439 #endif
13440   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13441   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13442
13443   /* Simple, contiguous registers.  */
13444   if (regs == NULL_RTX)
13445     {
13446       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13447
13448       loc_result = NULL;
13449       while (nregs--)
13450         {
13451           dw_loc_descr_ref t;
13452
13453           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13454                                       VAR_INIT_STATUS_INITIALIZED);
13455           add_loc_descr (&loc_result, t);
13456           add_loc_descr_op_piece (&loc_result, size);
13457           ++reg;
13458         }
13459       return loc_result;
13460     }
13461
13462   /* Now onto stupid register sets in non contiguous locations.  */
13463
13464   gcc_assert (GET_CODE (regs) == PARALLEL);
13465
13466   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13467   loc_result = NULL;
13468
13469   for (i = 0; i < XVECLEN (regs, 0); ++i)
13470     {
13471       dw_loc_descr_ref t;
13472
13473       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13474                                   VAR_INIT_STATUS_INITIALIZED);
13475       add_loc_descr (&loc_result, t);
13476       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13477       add_loc_descr_op_piece (&loc_result, size);
13478     }
13479
13480   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13481     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13482   return loc_result;
13483 }
13484
13485 /* Return a location descriptor that designates a constant.  */
13486
13487 static dw_loc_descr_ref
13488 int_loc_descriptor (HOST_WIDE_INT i)
13489 {
13490   enum dwarf_location_atom op;
13491
13492   /* Pick the smallest representation of a constant, rather than just
13493      defaulting to the LEB encoding.  */
13494   if (i >= 0)
13495     {
13496       if (i <= 31)
13497         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13498       else if (i <= 0xff)
13499         op = DW_OP_const1u;
13500       else if (i <= 0xffff)
13501         op = DW_OP_const2u;
13502       else if (HOST_BITS_PER_WIDE_INT == 32
13503                || i <= 0xffffffff)
13504         op = DW_OP_const4u;
13505       else
13506         op = DW_OP_constu;
13507     }
13508   else
13509     {
13510       if (i >= -0x80)
13511         op = DW_OP_const1s;
13512       else if (i >= -0x8000)
13513         op = DW_OP_const2s;
13514       else if (HOST_BITS_PER_WIDE_INT == 32
13515                || i >= -0x80000000)
13516         op = DW_OP_const4s;
13517       else
13518         op = DW_OP_consts;
13519     }
13520
13521   return new_loc_descr (op, i, 0);
13522 }
13523
13524 /* Return loc description representing "address" of integer value.
13525    This can appear only as toplevel expression.  */
13526
13527 static dw_loc_descr_ref
13528 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13529 {
13530   int litsize;
13531   dw_loc_descr_ref loc_result = NULL;
13532
13533   if (!(dwarf_version >= 4 || !dwarf_strict))
13534     return NULL;
13535
13536   if (i >= 0)
13537     {
13538       if (i <= 31)
13539         litsize = 1;
13540       else if (i <= 0xff)
13541         litsize = 2;
13542       else if (i <= 0xffff)
13543         litsize = 3;
13544       else if (HOST_BITS_PER_WIDE_INT == 32
13545                || i <= 0xffffffff)
13546         litsize = 5;
13547       else
13548         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13549     }
13550   else
13551     {
13552       if (i >= -0x80)
13553         litsize = 2;
13554       else if (i >= -0x8000)
13555         litsize = 3;
13556       else if (HOST_BITS_PER_WIDE_INT == 32
13557                || i >= -0x80000000)
13558         litsize = 5;
13559       else
13560         litsize = 1 + size_of_sleb128 (i);
13561     }
13562   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13563      is more compact.  For DW_OP_stack_value we need:
13564      litsize + 1 (DW_OP_stack_value)
13565      and for DW_OP_implicit_value:
13566      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13567   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13568     {
13569       loc_result = int_loc_descriptor (i);
13570       add_loc_descr (&loc_result,
13571                      new_loc_descr (DW_OP_stack_value, 0, 0));
13572       return loc_result;
13573     }
13574
13575   loc_result = new_loc_descr (DW_OP_implicit_value,
13576                               size, 0);
13577   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13578   loc_result->dw_loc_oprnd2.v.val_int = i;
13579   return loc_result;
13580 }
13581
13582 /* Return a location descriptor that designates a base+offset location.  */
13583
13584 static dw_loc_descr_ref
13585 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13586                  enum var_init_status initialized)
13587 {
13588   unsigned int regno;
13589   dw_loc_descr_ref result;
13590   dw_fde_ref fde = current_fde ();
13591
13592   /* We only use "frame base" when we're sure we're talking about the
13593      post-prologue local stack frame.  We do this by *not* running
13594      register elimination until this point, and recognizing the special
13595      argument pointer and soft frame pointer rtx's.  */
13596   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13597     {
13598       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13599
13600       if (elim != reg)
13601         {
13602           if (GET_CODE (elim) == PLUS)
13603             {
13604               offset += INTVAL (XEXP (elim, 1));
13605               elim = XEXP (elim, 0);
13606             }
13607           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13608                        && (elim == hard_frame_pointer_rtx
13609                            || elim == stack_pointer_rtx))
13610                       || elim == (frame_pointer_needed
13611                                   ? hard_frame_pointer_rtx
13612                                   : stack_pointer_rtx));
13613
13614           /* If drap register is used to align stack, use frame
13615              pointer + offset to access stack variables.  If stack
13616              is aligned without drap, use stack pointer + offset to
13617              access stack variables.  */
13618           if (crtl->stack_realign_tried
13619               && reg == frame_pointer_rtx)
13620             {
13621               int base_reg
13622                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13623                                       ? HARD_FRAME_POINTER_REGNUM
13624                                       : REGNO (elim));
13625               return new_reg_loc_descr (base_reg, offset);
13626             }
13627
13628           offset += frame_pointer_fb_offset;
13629           return new_loc_descr (DW_OP_fbreg, offset, 0);
13630         }
13631     }
13632   else if (!optimize
13633            && fde
13634            && (fde->drap_reg == REGNO (reg)
13635                || fde->vdrap_reg == REGNO (reg)))
13636     {
13637       /* Use cfa+offset to represent the location of arguments passed
13638          on the stack when drap is used to align stack.
13639          Only do this when not optimizing, for optimized code var-tracking
13640          is supposed to track where the arguments live and the register
13641          used as vdrap or drap in some spot might be used for something
13642          else in other part of the routine.  */
13643       return new_loc_descr (DW_OP_fbreg, offset, 0);
13644     }
13645
13646   regno = dbx_reg_number (reg);
13647   if (regno <= 31)
13648     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13649                             offset, 0);
13650   else
13651     result = new_loc_descr (DW_OP_bregx, regno, offset);
13652
13653   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13654     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13655
13656   return result;
13657 }
13658
13659 /* Return true if this RTL expression describes a base+offset calculation.  */
13660
13661 static inline int
13662 is_based_loc (const_rtx rtl)
13663 {
13664   return (GET_CODE (rtl) == PLUS
13665           && ((REG_P (XEXP (rtl, 0))
13666                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13667                && CONST_INT_P (XEXP (rtl, 1)))));
13668 }
13669
13670 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13671    failed.  */
13672
13673 static dw_loc_descr_ref
13674 tls_mem_loc_descriptor (rtx mem)
13675 {
13676   tree base;
13677   dw_loc_descr_ref loc_result;
13678
13679   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13680     return NULL;
13681
13682   base = get_base_address (MEM_EXPR (mem));
13683   if (base == NULL
13684       || TREE_CODE (base) != VAR_DECL
13685       || !DECL_THREAD_LOCAL_P (base))
13686     return NULL;
13687
13688   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13689   if (loc_result == NULL)
13690     return NULL;
13691
13692   if (INTVAL (MEM_OFFSET (mem)))
13693     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13694
13695   return loc_result;
13696 }
13697
13698 /* Output debug info about reason why we failed to expand expression as dwarf
13699    expression.  */
13700
13701 static void
13702 expansion_failed (tree expr, rtx rtl, char const *reason)
13703 {
13704   if (dump_file && (dump_flags & TDF_DETAILS))
13705     {
13706       fprintf (dump_file, "Failed to expand as dwarf: ");
13707       if (expr)
13708         print_generic_expr (dump_file, expr, dump_flags);
13709       if (rtl)
13710         {
13711           fprintf (dump_file, "\n");
13712           print_rtl (dump_file, rtl);
13713         }
13714       fprintf (dump_file, "\nReason: %s\n", reason);
13715     }
13716 }
13717
13718 /* Helper function for const_ok_for_output, called either directly
13719    or via for_each_rtx.  */
13720
13721 static int
13722 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13723 {
13724   rtx rtl = *rtlp;
13725
13726   if (GET_CODE (rtl) == UNSPEC)
13727     {
13728       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13729          we can't express it in the debug info.  */
13730 #ifdef ENABLE_CHECKING
13731       /* Don't complain about TLS UNSPECs, those are just too hard to
13732          delegitimize.  */
13733       if (XVECLEN (rtl, 0) != 1
13734           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13735           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13736           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13737           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13738         inform (current_function_decl
13739                 ? DECL_SOURCE_LOCATION (current_function_decl)
13740                 : UNKNOWN_LOCATION,
13741 #if NUM_UNSPEC_VALUES > 0
13742                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13743                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13744                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13745                 XINT (rtl, 1));
13746 #else
13747                 "non-delegitimized UNSPEC %d found in variable location",
13748                 XINT (rtl, 1));
13749 #endif
13750 #endif
13751       expansion_failed (NULL_TREE, rtl,
13752                         "UNSPEC hasn't been delegitimized.\n");
13753       return 1;
13754     }
13755
13756   if (GET_CODE (rtl) != SYMBOL_REF)
13757     return 0;
13758
13759   if (CONSTANT_POOL_ADDRESS_P (rtl))
13760     {
13761       bool marked;
13762       get_pool_constant_mark (rtl, &marked);
13763       /* If all references to this pool constant were optimized away,
13764          it was not output and thus we can't represent it.  */
13765       if (!marked)
13766         {
13767           expansion_failed (NULL_TREE, rtl,
13768                             "Constant was removed from constant pool.\n");
13769           return 1;
13770         }
13771     }
13772
13773   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13774     return 1;
13775
13776   /* Avoid references to external symbols in debug info, on several targets
13777      the linker might even refuse to link when linking a shared library,
13778      and in many other cases the relocations for .debug_info/.debug_loc are
13779      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13780      to be defined within the same shared library or executable are fine.  */
13781   if (SYMBOL_REF_EXTERNAL_P (rtl))
13782     {
13783       tree decl = SYMBOL_REF_DECL (rtl);
13784
13785       if (decl == NULL || !targetm.binds_local_p (decl))
13786         {
13787           expansion_failed (NULL_TREE, rtl,
13788                             "Symbol not defined in current TU.\n");
13789           return 1;
13790         }
13791     }
13792
13793   return 0;
13794 }
13795
13796 /* Return true if constant RTL can be emitted in DW_OP_addr or
13797    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13798    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13799
13800 static bool
13801 const_ok_for_output (rtx rtl)
13802 {
13803   if (GET_CODE (rtl) == SYMBOL_REF)
13804     return const_ok_for_output_1 (&rtl, NULL) == 0;
13805
13806   if (GET_CODE (rtl) == CONST)
13807     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13808
13809   return true;
13810 }
13811
13812 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13813    if possible, NULL otherwise.  */
13814
13815 static dw_die_ref
13816 base_type_for_mode (enum machine_mode mode, bool unsignedp)
13817 {
13818   dw_die_ref type_die;
13819   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13820
13821   if (type == NULL)
13822     return NULL;
13823   switch (TREE_CODE (type))
13824     {
13825     case INTEGER_TYPE:
13826     case REAL_TYPE:
13827       break;
13828     default:
13829       return NULL;
13830     }
13831   type_die = lookup_type_die (type);
13832   if (!type_die)
13833     type_die = modified_type_die (type, false, false, comp_unit_die ());
13834   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13835     return NULL;
13836   return type_die;
13837 }
13838
13839 /* The following routine converts the RTL for a variable or parameter
13840    (resident in memory) into an equivalent Dwarf representation of a
13841    mechanism for getting the address of that same variable onto the top of a
13842    hypothetical "address evaluation" stack.
13843
13844    When creating memory location descriptors, we are effectively transforming
13845    the RTL for a memory-resident object into its Dwarf postfix expression
13846    equivalent.  This routine recursively descends an RTL tree, turning
13847    it into Dwarf postfix code as it goes.
13848
13849    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13850
13851    MEM_MODE is the mode of the memory reference, needed to handle some
13852    autoincrement addressing modes.
13853
13854    Return 0 if we can't represent the location.  */
13855
13856 static dw_loc_descr_ref
13857 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13858                     enum machine_mode mem_mode,
13859                     enum var_init_status initialized)
13860 {
13861   dw_loc_descr_ref mem_loc_result = NULL;
13862   enum dwarf_location_atom op;
13863   dw_loc_descr_ref op0, op1;
13864
13865   if (mode == VOIDmode)
13866     mode = GET_MODE (rtl);
13867
13868   /* Note that for a dynamically sized array, the location we will generate a
13869      description of here will be the lowest numbered location which is
13870      actually within the array.  That's *not* necessarily the same as the
13871      zeroth element of the array.  */
13872
13873   rtl = targetm.delegitimize_address (rtl);
13874
13875   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13876     return NULL;
13877
13878   switch (GET_CODE (rtl))
13879     {
13880     case POST_INC:
13881     case POST_DEC:
13882     case POST_MODIFY:
13883       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13884
13885     case SUBREG:
13886       /* The case of a subreg may arise when we have a local (register)
13887          variable or a formal (register) parameter which doesn't quite fill
13888          up an entire register.  For now, just assume that it is
13889          legitimate to make the Dwarf info refer to the whole register which
13890          contains the given subreg.  */
13891       if (!subreg_lowpart_p (rtl))
13892         break;
13893       if (GET_MODE_CLASS (mode) == MODE_INT
13894           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
13895           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13896           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
13897         {
13898           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
13899                                                GET_MODE (SUBREG_REG (rtl)),
13900                                                mem_mode, initialized);
13901           break;
13902         }
13903       if (dwarf_strict)
13904         break;
13905       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
13906         break;
13907       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
13908           && (GET_MODE_CLASS (mode) != MODE_INT
13909               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
13910         break;
13911       else
13912         {
13913           dw_die_ref type_die;
13914           dw_loc_descr_ref cvt;
13915
13916           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
13917                                                GET_MODE (SUBREG_REG (rtl)),
13918                                                mode, initialized);
13919           if (mem_loc_result == NULL)
13920             break;
13921           type_die = base_type_for_mode (mode, 0);
13922           if (type_die == NULL)
13923             {
13924               mem_loc_result = NULL;
13925               break;
13926             }
13927           if (GET_MODE_SIZE (mode)
13928               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
13929             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13930           else
13931             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13932           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13933           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13934           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13935           add_loc_descr (&mem_loc_result, cvt);
13936         }
13937       break;
13938
13939     case REG:
13940       if (GET_MODE_CLASS (mode) != MODE_INT
13941           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13942         {
13943           dw_die_ref type_die;
13944
13945           if (dwarf_strict)
13946             break;
13947           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13948             break;
13949           type_die = base_type_for_mode (mode, 0);
13950           if (type_die == NULL)
13951             break;
13952           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13953                                           dbx_reg_number (rtl), 0);
13954           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13955           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13956           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13957           break;
13958         }
13959       /* Whenever a register number forms a part of the description of the
13960          method for calculating the (dynamic) address of a memory resident
13961          object, DWARF rules require the register number be referred to as
13962          a "base register".  This distinction is not based in any way upon
13963          what category of register the hardware believes the given register
13964          belongs to.  This is strictly DWARF terminology we're dealing with
13965          here. Note that in cases where the location of a memory-resident
13966          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13967          OP_CONST (0)) the actual DWARF location descriptor that we generate
13968          may just be OP_BASEREG (basereg).  This may look deceptively like
13969          the object in question was allocated to a register (rather than in
13970          memory) so DWARF consumers need to be aware of the subtle
13971          distinction between OP_REG and OP_BASEREG.  */
13972       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13973         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13974       else if (stack_realign_drap
13975                && crtl->drap_reg
13976                && crtl->args.internal_arg_pointer == rtl
13977                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13978         {
13979           /* If RTL is internal_arg_pointer, which has been optimized
13980              out, use DRAP instead.  */
13981           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13982                                             VAR_INIT_STATUS_INITIALIZED);
13983         }
13984       break;
13985
13986     case SIGN_EXTEND:
13987     case ZERO_EXTEND:
13988       gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
13989       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13990                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13991       if (op0 == 0)
13992         break;
13993       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13994         {
13995           int shift = DWARF2_ADDR_SIZE
13996                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13997           shift *= BITS_PER_UNIT;
13998           if (GET_CODE (rtl) == SIGN_EXTEND)
13999             op = DW_OP_shra;
14000           else
14001             op = DW_OP_shr;
14002           mem_loc_result = op0;
14003           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14004           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14005           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14006           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14007         }
14008       else if (!dwarf_strict)
14009         {
14010           dw_die_ref type_die1, type_die2;
14011           dw_loc_descr_ref cvt;
14012
14013           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14014                                           GET_CODE (rtl) == ZERO_EXTEND);
14015           if (type_die1 == NULL)
14016             break;
14017           type_die2 = base_type_for_mode (mode, 0);
14018           if (type_die2 == NULL)
14019             break;
14020           mem_loc_result = op0;
14021           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14022           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14023           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14024           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14025           add_loc_descr (&mem_loc_result, cvt);
14026           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14027           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14028           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14029           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14030           add_loc_descr (&mem_loc_result, cvt);
14031         }
14032       break;
14033
14034     case MEM:
14035       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14036                                            get_address_mode (rtl), mode,
14037                                            VAR_INIT_STATUS_INITIALIZED);
14038       if (mem_loc_result == NULL)
14039         mem_loc_result = tls_mem_loc_descriptor (rtl);
14040       if (mem_loc_result != 0)
14041         {
14042           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14043               || GET_MODE_CLASS (mode) != MODE_INT)
14044             {
14045               dw_die_ref type_die;
14046               dw_loc_descr_ref deref;
14047
14048               if (dwarf_strict)
14049                 return NULL;
14050               type_die = base_type_for_mode (mode, 0);
14051               if (type_die == NULL)
14052                 return NULL;
14053               deref = new_loc_descr (DW_OP_GNU_deref_type,
14054                                      GET_MODE_SIZE (mode), 0);
14055               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14056               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14057               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14058               add_loc_descr (&mem_loc_result, deref);
14059             }
14060           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14061             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14062           else
14063             add_loc_descr (&mem_loc_result,
14064                            new_loc_descr (DW_OP_deref_size,
14065                                           GET_MODE_SIZE (mode), 0));
14066         }
14067       else
14068         {
14069           rtx new_rtl = avoid_constant_pool_reference (rtl);
14070           if (new_rtl != rtl)
14071             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
14072         }
14073       break;
14074
14075     case LO_SUM:
14076       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14077
14078     case LABEL_REF:
14079       /* Some ports can transform a symbol ref into a label ref, because
14080          the symbol ref is too far away and has to be dumped into a constant
14081          pool.  */
14082     case CONST:
14083     case SYMBOL_REF:
14084       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14085           || GET_MODE_CLASS (mode) != MODE_INT)
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         {
14325           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14326           break;
14327         }
14328       if (!dwarf_strict
14329           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
14330               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
14331         {
14332           dw_die_ref type_die = base_type_for_mode (mode, 0);
14333           if (type_die == NULL)
14334             return NULL;
14335           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
14336                                           INTVAL (rtl));
14337           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14338           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14339           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14340           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14341             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14342           else
14343             {
14344               mem_loc_result->dw_loc_oprnd2.val_class
14345                 = dw_val_class_const_double;
14346               mem_loc_result->dw_loc_oprnd2.v.val_double
14347                 = shwi_to_double_int (INTVAL (rtl));
14348             }
14349         }
14350       break;
14351
14352     case CONST_DOUBLE:
14353       if (!dwarf_strict)
14354         {
14355           dw_die_ref type_die;
14356
14357           /* Note that a CONST_DOUBLE rtx could represent either an integer
14358              or a floating-point constant.  A CONST_DOUBLE is used whenever
14359              the constant requires more than one word in order to be
14360              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14361           if (mode == VOIDmode
14362               || (GET_MODE (rtl) == VOIDmode
14363                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
14364             break;
14365           type_die = base_type_for_mode (mode, 0);
14366           if (type_die == NULL)
14367             return NULL;
14368           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14369           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14370           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14371           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14372           if (SCALAR_FLOAT_MODE_P (mode))
14373             {
14374               unsigned int length = GET_MODE_SIZE (mode);
14375               unsigned char *array
14376                   = (unsigned char*) ggc_alloc_atomic (length);
14377
14378               insert_float (rtl, array);
14379               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14380               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14381               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14382               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14383             }
14384           else
14385             {
14386               mem_loc_result->dw_loc_oprnd2.val_class
14387                 = dw_val_class_const_double;
14388               mem_loc_result->dw_loc_oprnd2.v.val_double
14389                 = rtx_to_double_int (rtl);
14390             }
14391         }
14392       break;
14393
14394     case EQ:
14395       op = DW_OP_eq;
14396       goto do_scompare;
14397
14398     case GE:
14399       op = DW_OP_ge;
14400       goto do_scompare;
14401
14402     case GT:
14403       op = DW_OP_gt;
14404       goto do_scompare;
14405
14406     case LE:
14407       op = DW_OP_le;
14408       goto do_scompare;
14409
14410     case LT:
14411       op = DW_OP_lt;
14412       goto do_scompare;
14413
14414     case NE:
14415       op = DW_OP_ne;
14416       goto do_scompare;
14417
14418     do_scompare:
14419       {
14420         enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14421
14422         if (op_mode == VOIDmode)
14423           op_mode = GET_MODE (XEXP (rtl, 1));
14424         if (op_mode == VOIDmode)
14425           break;
14426
14427         if (dwarf_strict
14428             && (GET_MODE_CLASS (op_mode) != MODE_INT
14429                 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
14430           break;
14431
14432         op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14433                                   VAR_INIT_STATUS_INITIALIZED);
14434         op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14435                                   VAR_INIT_STATUS_INITIALIZED);
14436
14437         if (op0 == 0 || op1 == 0)
14438           break;
14439
14440         if (GET_MODE_CLASS (op_mode) == MODE_INT
14441             && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14442           {
14443             int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14444             shift *= BITS_PER_UNIT;
14445             /* For eq/ne, if the operands are known to be zero-extended,
14446                there is no need to do the fancy shifting up.  */
14447             if (op == DW_OP_eq || op == DW_OP_ne)
14448               {
14449                 dw_loc_descr_ref last0, last1;
14450                 for (last0 = op0;
14451                      last0->dw_loc_next != NULL;
14452                      last0 = last0->dw_loc_next)
14453                   ;
14454                 for (last1 = op1;
14455                      last1->dw_loc_next != NULL;
14456                      last1 = last1->dw_loc_next)
14457                   ;
14458                 /* deref_size zero extends, and for constants we can check
14459                    whether they are zero extended or not.  */
14460                 if (((last0->dw_loc_opc == DW_OP_deref_size
14461                       && last0->dw_loc_oprnd1.v.val_int
14462                          <= GET_MODE_SIZE (op_mode))
14463                      || (CONST_INT_P (XEXP (rtl, 0))
14464                          && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14465                              == (INTVAL (XEXP (rtl, 0))
14466                                  & GET_MODE_MASK (op_mode))))
14467                     && ((last1->dw_loc_opc == DW_OP_deref_size
14468                          && last1->dw_loc_oprnd1.v.val_int
14469                             <= GET_MODE_SIZE (op_mode))
14470                         || (CONST_INT_P (XEXP (rtl, 1))
14471                             && (unsigned HOST_WIDE_INT)
14472                                INTVAL (XEXP (rtl, 1))
14473                                == (INTVAL (XEXP (rtl, 1))
14474                                    & GET_MODE_MASK (op_mode)))))
14475                   goto do_compare;
14476               }
14477             add_loc_descr (&op0, int_loc_descriptor (shift));
14478             add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14479             if (CONST_INT_P (XEXP (rtl, 1)))
14480               op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14481             else
14482               {
14483                 add_loc_descr (&op1, int_loc_descriptor (shift));
14484                 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14485               }
14486           }
14487       }
14488
14489     do_compare:
14490       mem_loc_result = op0;
14491       add_loc_descr (&mem_loc_result, op1);
14492       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14493       if (STORE_FLAG_VALUE != 1)
14494         {
14495           add_loc_descr (&mem_loc_result,
14496                          int_loc_descriptor (STORE_FLAG_VALUE));
14497           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14498         }
14499       break;
14500
14501     case GEU:
14502       op = DW_OP_ge;
14503       goto do_ucompare;
14504
14505     case GTU:
14506       op = DW_OP_gt;
14507       goto do_ucompare;
14508
14509     case LEU:
14510       op = DW_OP_le;
14511       goto do_ucompare;
14512
14513     case LTU:
14514       op = DW_OP_lt;
14515       goto do_ucompare;
14516
14517     do_ucompare:
14518       {
14519         enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14520
14521         if (op_mode == VOIDmode)
14522           op_mode = GET_MODE (XEXP (rtl, 1));
14523         if (op_mode == VOIDmode)
14524           break;
14525         if (GET_MODE_CLASS (op_mode) != MODE_INT)
14526           break;
14527
14528         if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14529           break;
14530
14531         if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14532             break;
14533
14534         op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14535                                   VAR_INIT_STATUS_INITIALIZED);
14536         op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14537                                   VAR_INIT_STATUS_INITIALIZED);
14538
14539         if (op0 == 0 || op1 == 0)
14540           break;
14541
14542         if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14543           {
14544             HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14545             dw_loc_descr_ref last0, last1;
14546             for (last0 = op0;
14547                  last0->dw_loc_next != NULL;
14548                  last0 = last0->dw_loc_next)
14549               ;
14550             for (last1 = op1;
14551                  last1->dw_loc_next != NULL;
14552                  last1 = last1->dw_loc_next)
14553               ;
14554             if (CONST_INT_P (XEXP (rtl, 0)))
14555               op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14556             /* deref_size zero extends, so no need to mask it again.  */
14557             else if (last0->dw_loc_opc != DW_OP_deref_size
14558                      || last0->dw_loc_oprnd1.v.val_int
14559                         > GET_MODE_SIZE (op_mode))
14560               {
14561                 add_loc_descr (&op0, int_loc_descriptor (mask));
14562                 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14563               }
14564             if (CONST_INT_P (XEXP (rtl, 1)))
14565               op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14566             /* deref_size zero extends, so no need to mask it again.  */
14567             else if (last1->dw_loc_opc != DW_OP_deref_size
14568                      || last1->dw_loc_oprnd1.v.val_int
14569                         > GET_MODE_SIZE (op_mode))
14570               {
14571                 add_loc_descr (&op1, int_loc_descriptor (mask));
14572                 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14573               }
14574           }
14575         else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14576           {
14577             HOST_WIDE_INT bias = 1;
14578             bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14579             add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14580             if (CONST_INT_P (XEXP (rtl, 1)))
14581               op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14582                                         + INTVAL (XEXP (rtl, 1)));
14583             else
14584               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14585                                                   bias, 0));
14586           }
14587         else
14588           {
14589             dw_die_ref type_die = base_type_for_mode (mode, 1);
14590             dw_loc_descr_ref cvt;
14591
14592             if (type_die == NULL)
14593               break;
14594             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14595             cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14596             cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14597             cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14598             add_loc_descr (&op0, cvt);
14599             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14600             cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14601             cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14602             cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14603             add_loc_descr (&op1, cvt);
14604           }
14605       }
14606       goto do_compare;
14607
14608     case UMIN:
14609     case UMAX:
14610       if (GET_MODE_CLASS (mode) != MODE_INT)
14611         break;
14612       /* FALLTHRU */
14613     case SMIN:
14614     case SMAX:
14615       if (dwarf_strict
14616           && (GET_MODE_CLASS (mode) != MODE_INT
14617               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14618         break;
14619
14620       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14621                                 VAR_INIT_STATUS_INITIALIZED);
14622       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14623                                 VAR_INIT_STATUS_INITIALIZED);
14624
14625       if (op0 == 0 || op1 == 0)
14626         break;
14627
14628       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14629       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14630       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14631       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14632         {
14633           if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14634             {
14635               HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14636               add_loc_descr (&op0, int_loc_descriptor (mask));
14637               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14638               add_loc_descr (&op1, int_loc_descriptor (mask));
14639               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14640             }
14641           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14642             {
14643               HOST_WIDE_INT bias = 1;
14644               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14645               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14646               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14647             }
14648           else
14649             {
14650               dw_die_ref type_die = base_type_for_mode (mode, 1);
14651               dw_loc_descr_ref cvt;
14652
14653               if (type_die == NULL)
14654                 break;
14655               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14656               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14657               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14658               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14659               add_loc_descr (&op0, cvt);
14660               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14661               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14662               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14663               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14664               add_loc_descr (&op1, cvt);
14665             }
14666         }
14667       else if (GET_MODE_CLASS (mode) == MODE_INT
14668                && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14669         {
14670           int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode);
14671           shift *= BITS_PER_UNIT;
14672           add_loc_descr (&op0, int_loc_descriptor (shift));
14673           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14674           add_loc_descr (&op1, int_loc_descriptor (shift));
14675           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14676         }
14677
14678       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14679         op = DW_OP_lt;
14680       else
14681         op = DW_OP_gt;
14682       mem_loc_result = op0;
14683       add_loc_descr (&mem_loc_result, op1);
14684       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14685       {
14686         dw_loc_descr_ref bra_node, drop_node;
14687
14688         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14689         add_loc_descr (&mem_loc_result, bra_node);
14690         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14691         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14692         add_loc_descr (&mem_loc_result, drop_node);
14693         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14694         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14695       }
14696       break;
14697
14698     case ZERO_EXTRACT:
14699     case SIGN_EXTRACT:
14700       if (CONST_INT_P (XEXP (rtl, 1))
14701           && CONST_INT_P (XEXP (rtl, 2))
14702           && ((unsigned) INTVAL (XEXP (rtl, 1))
14703               + (unsigned) INTVAL (XEXP (rtl, 2))
14704               <= GET_MODE_BITSIZE (mode))
14705           && GET_MODE_CLASS (mode) == MODE_INT
14706           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14707           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14708         {
14709           int shift, size;
14710           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14711                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14712           if (op0 == 0)
14713             break;
14714           if (GET_CODE (rtl) == SIGN_EXTRACT)
14715             op = DW_OP_shra;
14716           else
14717             op = DW_OP_shr;
14718           mem_loc_result = op0;
14719           size = INTVAL (XEXP (rtl, 1));
14720           shift = INTVAL (XEXP (rtl, 2));
14721           if (BITS_BIG_ENDIAN)
14722             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14723                     - shift - size;
14724           if (shift + size != (int) DWARF2_ADDR_SIZE)
14725             {
14726               add_loc_descr (&mem_loc_result,
14727                              int_loc_descriptor (DWARF2_ADDR_SIZE
14728                                                  - shift - size));
14729               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14730             }
14731           if (size != (int) DWARF2_ADDR_SIZE)
14732             {
14733               add_loc_descr (&mem_loc_result,
14734                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14735               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14736             }
14737         }
14738       break;
14739
14740     case IF_THEN_ELSE:
14741       {
14742         dw_loc_descr_ref op2, bra_node, drop_node;
14743         op0 = mem_loc_descriptor (XEXP (rtl, 0),
14744                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
14745                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
14746                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
14747         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14748                                   VAR_INIT_STATUS_INITIALIZED);
14749         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14750                                   VAR_INIT_STATUS_INITIALIZED);
14751         if (op0 == NULL || op1 == NULL || op2 == NULL)
14752           break;
14753
14754         mem_loc_result = op1;
14755         add_loc_descr (&mem_loc_result, op2);
14756         add_loc_descr (&mem_loc_result, op0);
14757         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14758         add_loc_descr (&mem_loc_result, bra_node);
14759         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14760         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14761         add_loc_descr (&mem_loc_result, drop_node);
14762         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14763         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14764       }
14765       break;
14766
14767     case FLOAT_EXTEND:
14768     case FLOAT_TRUNCATE:
14769     case FLOAT:
14770     case UNSIGNED_FLOAT:
14771     case FIX:
14772     case UNSIGNED_FIX:
14773       if (!dwarf_strict)
14774         {
14775           dw_die_ref type_die;
14776           dw_loc_descr_ref cvt;
14777
14778           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14779                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14780           if (op0 == NULL)
14781             break;
14782           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
14783               && (GET_CODE (rtl) == UNSIGNED_FLOAT
14784                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14785                      <= DWARF2_ADDR_SIZE))
14786             {
14787               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14788                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
14789               if (type_die == NULL)
14790                 break;
14791               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14792               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14793               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14794               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14795               add_loc_descr (&op0, cvt);
14796             }
14797           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14798           if (type_die == NULL)
14799             break;
14800           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14801           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14802           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14803           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14804           add_loc_descr (&op0, cvt);
14805           if (GET_MODE_CLASS (mode) == MODE_INT
14806               && (GET_CODE (rtl) == UNSIGNED_FIX
14807                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14808             {
14809               enum machine_mode outer_mode = mode;
14810               if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14811                 {
14812                   outer_mode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
14813                                               MODE_INT, 0);
14814                   if (outer_mode == BLKmode
14815                       || GET_MODE_SIZE (outer_mode) != DWARF2_ADDR_SIZE)
14816                     break;
14817                 }
14818               type_die = base_type_for_mode (outer_mode, 0);
14819               if (type_die == NULL)
14820                 break;
14821               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14822               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14823               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14824               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14825               add_loc_descr (&op0, cvt);
14826             }
14827           mem_loc_result = op0;
14828         }
14829       break;
14830
14831     case COMPARE:
14832     case ROTATE:
14833     case ROTATERT:
14834     case TRUNCATE:
14835       /* In theory, we could implement the above.  */
14836       /* DWARF cannot represent the unsigned compare operations
14837          natively.  */
14838     case SS_MULT:
14839     case US_MULT:
14840     case SS_DIV:
14841     case US_DIV:
14842     case SS_PLUS:
14843     case US_PLUS:
14844     case SS_MINUS:
14845     case US_MINUS:
14846     case SS_NEG:
14847     case US_NEG:
14848     case SS_ABS:
14849     case SS_ASHIFT:
14850     case US_ASHIFT:
14851     case SS_TRUNCATE:
14852     case US_TRUNCATE:
14853     case UDIV:
14854     case UNORDERED:
14855     case ORDERED:
14856     case UNEQ:
14857     case UNGE:
14858     case UNGT:
14859     case UNLE:
14860     case UNLT:
14861     case LTGT:
14862     case FRACT_CONVERT:
14863     case UNSIGNED_FRACT_CONVERT:
14864     case SAT_FRACT:
14865     case UNSIGNED_SAT_FRACT:
14866     case SQRT:
14867     case BSWAP:
14868     case FFS:
14869     case CLZ:
14870     case CTZ:
14871     case POPCOUNT:
14872     case PARITY:
14873     case ASM_OPERANDS:
14874     case VEC_MERGE:
14875     case VEC_SELECT:
14876     case VEC_CONCAT:
14877     case VEC_DUPLICATE:
14878     case UNSPEC:
14879     case HIGH:
14880     case FMA:
14881     case STRICT_LOW_PART:
14882     case CONST_VECTOR:
14883     case CONST_FIXED:
14884       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14885          can't express it in the debug info.  This can happen e.g. with some
14886          TLS UNSPECs.  */
14887       break;
14888
14889     case CONST_STRING:
14890       resolve_one_addr (&rtl, NULL);
14891       goto symref;
14892
14893     default:
14894 #ifdef ENABLE_CHECKING
14895       print_rtl (stderr, rtl);
14896       gcc_unreachable ();
14897 #else
14898       break;
14899 #endif
14900     }
14901
14902   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14903     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14904
14905   return mem_loc_result;
14906 }
14907
14908 /* Return a descriptor that describes the concatenation of two locations.
14909    This is typically a complex variable.  */
14910
14911 static dw_loc_descr_ref
14912 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14913 {
14914   dw_loc_descr_ref cc_loc_result = NULL;
14915   dw_loc_descr_ref x0_ref
14916     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14917   dw_loc_descr_ref x1_ref
14918     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14919
14920   if (x0_ref == 0 || x1_ref == 0)
14921     return 0;
14922
14923   cc_loc_result = x0_ref;
14924   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14925
14926   add_loc_descr (&cc_loc_result, x1_ref);
14927   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14928
14929   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14930     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14931
14932   return cc_loc_result;
14933 }
14934
14935 /* Return a descriptor that describes the concatenation of N
14936    locations.  */
14937
14938 static dw_loc_descr_ref
14939 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14940 {
14941   unsigned int i;
14942   dw_loc_descr_ref cc_loc_result = NULL;
14943   unsigned int n = XVECLEN (concatn, 0);
14944
14945   for (i = 0; i < n; ++i)
14946     {
14947       dw_loc_descr_ref ref;
14948       rtx x = XVECEXP (concatn, 0, i);
14949
14950       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14951       if (ref == NULL)
14952         return NULL;
14953
14954       add_loc_descr (&cc_loc_result, ref);
14955       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14956     }
14957
14958   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14959     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14960
14961   return cc_loc_result;
14962 }
14963
14964 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14965    for DEBUG_IMPLICIT_PTR RTL.  */
14966
14967 static dw_loc_descr_ref
14968 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14969 {
14970   dw_loc_descr_ref ret;
14971   dw_die_ref ref;
14972
14973   if (dwarf_strict)
14974     return NULL;
14975   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14976               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14977               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14978   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14979   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14980   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14981   if (ref)
14982     {
14983       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14984       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14985       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14986     }
14987   else
14988     {
14989       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14990       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14991     }
14992   return ret;
14993 }
14994
14995 /* Output a proper Dwarf location descriptor for a variable or parameter
14996    which is either allocated in a register or in a memory location.  For a
14997    register, we just generate an OP_REG and the register number.  For a
14998    memory location we provide a Dwarf postfix expression describing how to
14999    generate the (dynamic) address of the object onto the address stack.
15000
15001    MODE is mode of the decl if this loc_descriptor is going to be used in
15002    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15003    allowed, VOIDmode otherwise.
15004
15005    If we don't know how to describe it, return 0.  */
15006
15007 static dw_loc_descr_ref
15008 loc_descriptor (rtx rtl, enum machine_mode mode,
15009                 enum var_init_status initialized)
15010 {
15011   dw_loc_descr_ref loc_result = NULL;
15012
15013   switch (GET_CODE (rtl))
15014     {
15015     case SUBREG:
15016       /* The case of a subreg may arise when we have a local (register)
15017          variable or a formal (register) parameter which doesn't quite fill
15018          up an entire register.  For now, just assume that it is
15019          legitimate to make the Dwarf info refer to the whole register which
15020          contains the given subreg.  */
15021       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15022         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
15023       else
15024         goto do_default;
15025       break;
15026
15027     case REG:
15028       loc_result = reg_loc_descriptor (rtl, initialized);
15029       break;
15030
15031     case MEM:
15032       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15033                                        GET_MODE (rtl), initialized);
15034       if (loc_result == NULL)
15035         loc_result = tls_mem_loc_descriptor (rtl);
15036       if (loc_result == NULL)
15037         {
15038           rtx new_rtl = avoid_constant_pool_reference (rtl);
15039           if (new_rtl != rtl)
15040             loc_result = loc_descriptor (new_rtl, mode, initialized);
15041         }
15042       break;
15043
15044     case CONCAT:
15045       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15046                                           initialized);
15047       break;
15048
15049     case CONCATN:
15050       loc_result = concatn_loc_descriptor (rtl, initialized);
15051       break;
15052
15053     case VAR_LOCATION:
15054       /* Single part.  */
15055       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15056         {
15057           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15058           if (GET_CODE (loc) == EXPR_LIST)
15059             loc = XEXP (loc, 0);
15060           loc_result = loc_descriptor (loc, mode, initialized);
15061           break;
15062         }
15063
15064       rtl = XEXP (rtl, 1);
15065       /* FALLTHRU */
15066
15067     case PARALLEL:
15068       {
15069         rtvec par_elems = XVEC (rtl, 0);
15070         int num_elem = GET_NUM_ELEM (par_elems);
15071         enum machine_mode mode;
15072         int i;
15073
15074         /* Create the first one, so we have something to add to.  */
15075         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15076                                      VOIDmode, initialized);
15077         if (loc_result == NULL)
15078           return NULL;
15079         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15080         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15081         for (i = 1; i < num_elem; i++)
15082           {
15083             dw_loc_descr_ref temp;
15084
15085             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15086                                    VOIDmode, initialized);
15087             if (temp == NULL)
15088               return NULL;
15089             add_loc_descr (&loc_result, temp);
15090             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15091             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15092           }
15093       }
15094       break;
15095
15096     case CONST_INT:
15097       if (mode != VOIDmode && mode != BLKmode)
15098         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15099                                                     INTVAL (rtl));
15100       break;
15101
15102     case CONST_DOUBLE:
15103       if (mode == VOIDmode)
15104         mode = GET_MODE (rtl);
15105
15106       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15107         {
15108           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15109
15110           /* Note that a CONST_DOUBLE rtx could represent either an integer
15111              or a floating-point constant.  A CONST_DOUBLE is used whenever
15112              the constant requires more than one word in order to be
15113              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15114           loc_result = new_loc_descr (DW_OP_implicit_value,
15115                                       GET_MODE_SIZE (mode), 0);
15116           if (SCALAR_FLOAT_MODE_P (mode))
15117             {
15118               unsigned int length = GET_MODE_SIZE (mode);
15119               unsigned char *array
15120                   = (unsigned char*) ggc_alloc_atomic (length);
15121
15122               insert_float (rtl, array);
15123               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15124               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15125               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15126               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15127             }
15128           else
15129             {
15130               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15131               loc_result->dw_loc_oprnd2.v.val_double
15132                 = rtx_to_double_int (rtl);
15133             }
15134         }
15135       break;
15136
15137     case CONST_VECTOR:
15138       if (mode == VOIDmode)
15139         mode = GET_MODE (rtl);
15140
15141       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15142         {
15143           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15144           unsigned int length = CONST_VECTOR_NUNITS (rtl);
15145           unsigned char *array = (unsigned char *)
15146             ggc_alloc_atomic (length * elt_size);
15147           unsigned int i;
15148           unsigned char *p;
15149
15150           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15151           switch (GET_MODE_CLASS (mode))
15152             {
15153             case MODE_VECTOR_INT:
15154               for (i = 0, p = array; i < length; i++, p += elt_size)
15155                 {
15156                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15157                   double_int val = rtx_to_double_int (elt);
15158
15159                   if (elt_size <= sizeof (HOST_WIDE_INT))
15160                     insert_int (double_int_to_shwi (val), elt_size, p);
15161                   else
15162                     {
15163                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15164                       insert_double (val, p);
15165                     }
15166                 }
15167               break;
15168
15169             case MODE_VECTOR_FLOAT:
15170               for (i = 0, p = array; i < length; i++, p += elt_size)
15171                 {
15172                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15173                   insert_float (elt, p);
15174                 }
15175               break;
15176
15177             default:
15178               gcc_unreachable ();
15179             }
15180
15181           loc_result = new_loc_descr (DW_OP_implicit_value,
15182                                       length * elt_size, 0);
15183           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15184           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15185           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15186           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15187         }
15188       break;
15189
15190     case CONST:
15191       if (mode == VOIDmode
15192           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
15193           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
15194           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15195         {
15196           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15197           break;
15198         }
15199       /* FALLTHROUGH */
15200     case SYMBOL_REF:
15201       if (!const_ok_for_output (rtl))
15202         break;
15203     case LABEL_REF:
15204       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15205           && (dwarf_version >= 4 || !dwarf_strict))
15206         {
15207           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15208           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15209           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15210           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15211           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15212         }
15213       break;
15214
15215     case DEBUG_IMPLICIT_PTR:
15216       loc_result = implicit_ptr_descriptor (rtl, 0);
15217       break;
15218
15219     case PLUS:
15220       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15221           && CONST_INT_P (XEXP (rtl, 1)))
15222         {
15223           loc_result
15224             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15225           break;
15226         }
15227       /* FALLTHRU */
15228     do_default:
15229     default:
15230       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
15231            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15232            && dwarf_version >= 4)
15233           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15234         {
15235           /* Value expression.  */
15236           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15237           if (loc_result)
15238             add_loc_descr (&loc_result,
15239                            new_loc_descr (DW_OP_stack_value, 0, 0));
15240         }
15241       break;
15242     }
15243
15244   return loc_result;
15245 }
15246
15247 /* We need to figure out what section we should use as the base for the
15248    address ranges where a given location is valid.
15249    1. If this particular DECL has a section associated with it, use that.
15250    2. If this function has a section associated with it, use that.
15251    3. Otherwise, use the text section.
15252    XXX: If you split a variable across multiple sections, we won't notice.  */
15253
15254 static const char *
15255 secname_for_decl (const_tree decl)
15256 {
15257   const char *secname;
15258
15259   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
15260     {
15261       tree sectree = DECL_SECTION_NAME (decl);
15262       secname = TREE_STRING_POINTER (sectree);
15263     }
15264   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15265     {
15266       tree sectree = DECL_SECTION_NAME (current_function_decl);
15267       secname = TREE_STRING_POINTER (sectree);
15268     }
15269   else if (cfun && in_cold_section_p)
15270     secname = crtl->subsections.cold_section_label;
15271   else
15272     secname = text_section_label;
15273
15274   return secname;
15275 }
15276
15277 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
15278
15279 static bool
15280 decl_by_reference_p (tree decl)
15281 {
15282   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15283            || TREE_CODE (decl) == VAR_DECL)
15284           && DECL_BY_REFERENCE (decl));
15285 }
15286
15287 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15288    for VARLOC.  */
15289
15290 static dw_loc_descr_ref
15291 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15292                enum var_init_status initialized)
15293 {
15294   int have_address = 0;
15295   dw_loc_descr_ref descr;
15296   enum machine_mode mode;
15297
15298   if (want_address != 2)
15299     {
15300       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15301       /* Single part.  */
15302       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15303         {
15304           varloc = PAT_VAR_LOCATION_LOC (varloc);
15305           if (GET_CODE (varloc) == EXPR_LIST)
15306             varloc = XEXP (varloc, 0);
15307           mode = GET_MODE (varloc);
15308           if (MEM_P (varloc))
15309             {
15310               rtx addr = XEXP (varloc, 0);
15311               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15312                                           mode, initialized);
15313               if (descr)
15314                 have_address = 1;
15315               else
15316                 {
15317                   rtx x = avoid_constant_pool_reference (varloc);
15318                   if (x != varloc)
15319                     descr = mem_loc_descriptor (x, mode, VOIDmode,
15320                                                 initialized);
15321                 }
15322             }
15323           else
15324             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15325         }
15326       else
15327         return 0;
15328     }
15329   else
15330     {
15331       if (GET_CODE (varloc) == VAR_LOCATION)
15332         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15333       else
15334         mode = DECL_MODE (loc);
15335       descr = loc_descriptor (varloc, mode, initialized);
15336       have_address = 1;
15337     }
15338
15339   if (!descr)
15340     return 0;
15341
15342   if (want_address == 2 && !have_address
15343       && (dwarf_version >= 4 || !dwarf_strict))
15344     {
15345       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15346         {
15347           expansion_failed (loc, NULL_RTX,
15348                             "DWARF address size mismatch");
15349           return 0;
15350         }
15351       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15352       have_address = 1;
15353     }
15354   /* Show if we can't fill the request for an address.  */
15355   if (want_address && !have_address)
15356     {
15357       expansion_failed (loc, NULL_RTX,
15358                         "Want address and only have value");
15359       return 0;
15360     }
15361
15362   /* If we've got an address and don't want one, dereference.  */
15363   if (!want_address && have_address)
15364     {
15365       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15366       enum dwarf_location_atom op;
15367
15368       if (size > DWARF2_ADDR_SIZE || size == -1)
15369         {
15370           expansion_failed (loc, NULL_RTX,
15371                             "DWARF address size mismatch");
15372           return 0;
15373         }
15374       else if (size == DWARF2_ADDR_SIZE)
15375         op = DW_OP_deref;
15376       else
15377         op = DW_OP_deref_size;
15378
15379       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15380     }
15381
15382   return descr;
15383 }
15384
15385 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15386    if it is not possible.  */
15387
15388 static dw_loc_descr_ref
15389 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15390 {
15391   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15392     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15393   else if (dwarf_version >= 3 || !dwarf_strict)
15394     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15395   else
15396     return NULL;
15397 }
15398
15399 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15400    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15401
15402 static dw_loc_descr_ref
15403 dw_sra_loc_expr (tree decl, rtx loc)
15404 {
15405   rtx p;
15406   unsigned int padsize = 0;
15407   dw_loc_descr_ref descr, *descr_tail;
15408   unsigned HOST_WIDE_INT decl_size;
15409   rtx varloc;
15410   enum var_init_status initialized;
15411
15412   if (DECL_SIZE (decl) == NULL
15413       || !host_integerp (DECL_SIZE (decl), 1))
15414     return NULL;
15415
15416   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15417   descr = NULL;
15418   descr_tail = &descr;
15419
15420   for (p = loc; p; p = XEXP (p, 1))
15421     {
15422       unsigned int bitsize = decl_piece_bitsize (p);
15423       rtx loc_note = *decl_piece_varloc_ptr (p);
15424       dw_loc_descr_ref cur_descr;
15425       dw_loc_descr_ref *tail, last = NULL;
15426       unsigned int opsize = 0;
15427
15428       if (loc_note == NULL_RTX
15429           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15430         {
15431           padsize += bitsize;
15432           continue;
15433         }
15434       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15435       varloc = NOTE_VAR_LOCATION (loc_note);
15436       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15437       if (cur_descr == NULL)
15438         {
15439           padsize += bitsize;
15440           continue;
15441         }
15442
15443       /* Check that cur_descr either doesn't use
15444          DW_OP_*piece operations, or their sum is equal
15445          to bitsize.  Otherwise we can't embed it.  */
15446       for (tail = &cur_descr; *tail != NULL;
15447            tail = &(*tail)->dw_loc_next)
15448         if ((*tail)->dw_loc_opc == DW_OP_piece)
15449           {
15450             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15451                       * BITS_PER_UNIT;
15452             last = *tail;
15453           }
15454         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15455           {
15456             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15457             last = *tail;
15458           }
15459
15460       if (last != NULL && opsize != bitsize)
15461         {
15462           padsize += bitsize;
15463           continue;
15464         }
15465
15466       /* If there is a hole, add DW_OP_*piece after empty DWARF
15467          expression, which means that those bits are optimized out.  */
15468       if (padsize)
15469         {
15470           if (padsize > decl_size)
15471             return NULL;
15472           decl_size -= padsize;
15473           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15474           if (*descr_tail == NULL)
15475             return NULL;
15476           descr_tail = &(*descr_tail)->dw_loc_next;
15477           padsize = 0;
15478         }
15479       *descr_tail = cur_descr;
15480       descr_tail = tail;
15481       if (bitsize > decl_size)
15482         return NULL;
15483       decl_size -= bitsize;
15484       if (last == NULL)
15485         {
15486           HOST_WIDE_INT offset = 0;
15487           if (GET_CODE (varloc) == VAR_LOCATION
15488               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15489             {
15490               varloc = PAT_VAR_LOCATION_LOC (varloc);
15491               if (GET_CODE (varloc) == EXPR_LIST)
15492                 varloc = XEXP (varloc, 0);
15493             }
15494           do 
15495             {
15496               if (GET_CODE (varloc) == CONST
15497                   || GET_CODE (varloc) == SIGN_EXTEND
15498                   || GET_CODE (varloc) == ZERO_EXTEND)
15499                 varloc = XEXP (varloc, 0);
15500               else if (GET_CODE (varloc) == SUBREG)
15501                 varloc = SUBREG_REG (varloc);
15502               else
15503                 break;
15504             }
15505           while (1);
15506           /* DW_OP_bit_size offset should be zero for register
15507              or implicit location descriptions and empty location
15508              descriptions, but for memory addresses needs big endian
15509              adjustment.  */
15510           if (MEM_P (varloc))
15511             {
15512               unsigned HOST_WIDE_INT memsize
15513                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15514               if (memsize != bitsize)
15515                 {
15516                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15517                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15518                     return NULL;
15519                   if (memsize < bitsize)
15520                     return NULL;
15521                   if (BITS_BIG_ENDIAN)
15522                     offset = memsize - bitsize;
15523                 }
15524             }
15525
15526           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15527           if (*descr_tail == NULL)
15528             return NULL;
15529           descr_tail = &(*descr_tail)->dw_loc_next;
15530         }
15531     }
15532
15533   /* If there were any non-empty expressions, add padding till the end of
15534      the decl.  */
15535   if (descr != NULL && decl_size != 0)
15536     {
15537       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15538       if (*descr_tail == NULL)
15539         return NULL;
15540     }
15541   return descr;
15542 }
15543
15544 /* Return the dwarf representation of the location list LOC_LIST of
15545    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15546    function.  */
15547
15548 static dw_loc_list_ref
15549 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15550 {
15551   const char *endname, *secname;
15552   rtx varloc;
15553   enum var_init_status initialized;
15554   struct var_loc_node *node;
15555   dw_loc_descr_ref descr;
15556   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15557   dw_loc_list_ref list = NULL;
15558   dw_loc_list_ref *listp = &list;
15559
15560   /* Now that we know what section we are using for a base,
15561      actually construct the list of locations.
15562      The first location information is what is passed to the
15563      function that creates the location list, and the remaining
15564      locations just get added on to that list.
15565      Note that we only know the start address for a location
15566      (IE location changes), so to build the range, we use
15567      the range [current location start, next location start].
15568      This means we have to special case the last node, and generate
15569      a range of [last location start, end of function label].  */
15570
15571   secname = secname_for_decl (decl);
15572
15573   for (node = loc_list->first; node; node = node->next)
15574     if (GET_CODE (node->loc) == EXPR_LIST
15575         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15576       {
15577         if (GET_CODE (node->loc) == EXPR_LIST)
15578           {
15579             /* This requires DW_OP_{,bit_}piece, which is not usable
15580                inside DWARF expressions.  */
15581             if (want_address != 2)
15582               continue;
15583             descr = dw_sra_loc_expr (decl, node->loc);
15584             if (descr == NULL)
15585               continue;
15586           }
15587         else
15588           {
15589             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15590             varloc = NOTE_VAR_LOCATION (node->loc);
15591             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15592           }
15593         if (descr)
15594           {
15595             bool range_across_switch = false;
15596             /* If section switch happens in between node->label
15597                and node->next->label (or end of function) and
15598                we can't emit it as a single entry list,
15599                emit two ranges, first one ending at the end
15600                of first partition and second one starting at the
15601                beginning of second partition.  */
15602             if (node == loc_list->last_before_switch
15603                 && (node != loc_list->first || loc_list->first->next)
15604                 && current_function_decl)
15605               {
15606                 endname = current_fde ()->dw_fde_end;
15607                 range_across_switch = true;
15608               }
15609             /* The variable has a location between NODE->LABEL and
15610                NODE->NEXT->LABEL.  */
15611             else if (node->next)
15612               endname = node->next->label;
15613             /* If the variable has a location at the last label
15614                it keeps its location until the end of function.  */
15615             else if (!current_function_decl)
15616               endname = text_end_label;
15617             else
15618               {
15619                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15620                                              current_function_funcdef_no);
15621                 endname = ggc_strdup (label_id);
15622               }
15623
15624             *listp = new_loc_list (descr, node->label, endname, secname);
15625             listp = &(*listp)->dw_loc_next;
15626
15627             if (range_across_switch)
15628               {
15629                 if (GET_CODE (node->loc) == EXPR_LIST)
15630                   descr = dw_sra_loc_expr (decl, node->loc);
15631                 else
15632                   {
15633                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15634                     varloc = NOTE_VAR_LOCATION (node->loc);
15635                     descr = dw_loc_list_1 (decl, varloc, want_address,
15636                                            initialized);
15637                   }
15638                 gcc_assert (descr);
15639                 /* The variable has a location between NODE->LABEL and
15640                    NODE->NEXT->LABEL.  */
15641                 if (node->next)
15642                   endname = node->next->label;
15643                 else
15644                   endname = current_fde ()->dw_fde_second_end;
15645                 *listp = new_loc_list (descr,
15646                                        current_fde ()->dw_fde_second_begin,
15647                                        endname, secname);
15648                 listp = &(*listp)->dw_loc_next;
15649               }
15650           }
15651       }
15652
15653   /* Try to avoid the overhead of a location list emitting a location
15654      expression instead, but only if we didn't have more than one
15655      location entry in the first place.  If some entries were not
15656      representable, we don't want to pretend a single entry that was
15657      applies to the entire scope in which the variable is
15658      available.  */
15659   if (list && loc_list->first->next)
15660     gen_llsym (list);
15661
15662   return list;
15663 }
15664
15665 /* Return if the loc_list has only single element and thus can be represented
15666    as location description.   */
15667
15668 static bool
15669 single_element_loc_list_p (dw_loc_list_ref list)
15670 {
15671   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15672   return !list->ll_symbol;
15673 }
15674
15675 /* To each location in list LIST add loc descr REF.  */
15676
15677 static void
15678 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15679 {
15680   dw_loc_descr_ref copy;
15681   add_loc_descr (&list->expr, ref);
15682   list = list->dw_loc_next;
15683   while (list)
15684     {
15685       copy = ggc_alloc_dw_loc_descr_node ();
15686       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15687       add_loc_descr (&list->expr, copy);
15688       while (copy->dw_loc_next)
15689         {
15690           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15691           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15692           copy->dw_loc_next = new_copy;
15693           copy = new_copy;
15694         }
15695       list = list->dw_loc_next;
15696     }
15697 }
15698
15699 /* Given two lists RET and LIST
15700    produce location list that is result of adding expression in LIST
15701    to expression in RET on each possition in program.
15702    Might be destructive on both RET and LIST.
15703
15704    TODO: We handle only simple cases of RET or LIST having at most one
15705    element. General case would inolve sorting the lists in program order
15706    and merging them that will need some additional work.
15707    Adding that will improve quality of debug info especially for SRA-ed
15708    structures.  */
15709
15710 static void
15711 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15712 {
15713   if (!list)
15714     return;
15715   if (!*ret)
15716     {
15717       *ret = list;
15718       return;
15719     }
15720   if (!list->dw_loc_next)
15721     {
15722       add_loc_descr_to_each (*ret, list->expr);
15723       return;
15724     }
15725   if (!(*ret)->dw_loc_next)
15726     {
15727       add_loc_descr_to_each (list, (*ret)->expr);
15728       *ret = list;
15729       return;
15730     }
15731   expansion_failed (NULL_TREE, NULL_RTX,
15732                     "Don't know how to merge two non-trivial"
15733                     " location lists.\n");
15734   *ret = NULL;
15735   return;
15736 }
15737
15738 /* LOC is constant expression.  Try a luck, look it up in constant
15739    pool and return its loc_descr of its address.  */
15740
15741 static dw_loc_descr_ref
15742 cst_pool_loc_descr (tree loc)
15743 {
15744   /* Get an RTL for this, if something has been emitted.  */
15745   rtx rtl = lookup_constant_def (loc);
15746
15747   if (!rtl || !MEM_P (rtl))
15748     {
15749       gcc_assert (!rtl);
15750       return 0;
15751     }
15752   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15753
15754   /* TODO: We might get more coverage if we was actually delaying expansion
15755      of all expressions till end of compilation when constant pools are fully
15756      populated.  */
15757   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15758     {
15759       expansion_failed (loc, NULL_RTX,
15760                         "CST value in contant pool but not marked.");
15761       return 0;
15762     }
15763   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15764                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15765 }
15766
15767 /* Return dw_loc_list representing address of addr_expr LOC
15768    by looking for innder INDIRECT_REF expression and turing it
15769    into simple arithmetics.  */
15770
15771 static dw_loc_list_ref
15772 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15773 {
15774   tree obj, offset;
15775   HOST_WIDE_INT bitsize, bitpos, bytepos;
15776   enum machine_mode mode;
15777   int volatilep;
15778   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15779   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15780
15781   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15782                              &bitsize, &bitpos, &offset, &mode,
15783                              &unsignedp, &volatilep, false);
15784   STRIP_NOPS (obj);
15785   if (bitpos % BITS_PER_UNIT)
15786     {
15787       expansion_failed (loc, NULL_RTX, "bitfield access");
15788       return 0;
15789     }
15790   if (!INDIRECT_REF_P (obj))
15791     {
15792       expansion_failed (obj,
15793                         NULL_RTX, "no indirect ref in inner refrence");
15794       return 0;
15795     }
15796   if (!offset && !bitpos)
15797     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15798   else if (toplev
15799            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15800            && (dwarf_version >= 4 || !dwarf_strict))
15801     {
15802       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15803       if (!list_ret)
15804         return 0;
15805       if (offset)
15806         {
15807           /* Variable offset.  */
15808           list_ret1 = loc_list_from_tree (offset, 0);
15809           if (list_ret1 == 0)
15810             return 0;
15811           add_loc_list (&list_ret, list_ret1);
15812           if (!list_ret)
15813             return 0;
15814           add_loc_descr_to_each (list_ret,
15815                                  new_loc_descr (DW_OP_plus, 0, 0));
15816         }
15817       bytepos = bitpos / BITS_PER_UNIT;
15818       if (bytepos > 0)
15819         add_loc_descr_to_each (list_ret,
15820                                new_loc_descr (DW_OP_plus_uconst,
15821                                               bytepos, 0));
15822       else if (bytepos < 0)
15823         loc_list_plus_const (list_ret, bytepos);
15824       add_loc_descr_to_each (list_ret,
15825                              new_loc_descr (DW_OP_stack_value, 0, 0));
15826     }
15827   return list_ret;
15828 }
15829
15830
15831 /* Generate Dwarf location list representing LOC.
15832    If WANT_ADDRESS is false, expression computing LOC will be computed
15833    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15834    if WANT_ADDRESS is 2, expression computing address useable in location
15835      will be returned (i.e. DW_OP_reg can be used
15836      to refer to register values).  */
15837
15838 static dw_loc_list_ref
15839 loc_list_from_tree (tree loc, int want_address)
15840 {
15841   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15842   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15843   int have_address = 0;
15844   enum dwarf_location_atom op;
15845
15846   /* ??? Most of the time we do not take proper care for sign/zero
15847      extending the values properly.  Hopefully this won't be a real
15848      problem...  */
15849
15850   switch (TREE_CODE (loc))
15851     {
15852     case ERROR_MARK:
15853       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15854       return 0;
15855
15856     case PLACEHOLDER_EXPR:
15857       /* This case involves extracting fields from an object to determine the
15858          position of other fields.  We don't try to encode this here.  The
15859          only user of this is Ada, which encodes the needed information using
15860          the names of types.  */
15861       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15862       return 0;
15863
15864     case CALL_EXPR:
15865       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15866       /* There are no opcodes for these operations.  */
15867       return 0;
15868
15869     case PREINCREMENT_EXPR:
15870     case PREDECREMENT_EXPR:
15871     case POSTINCREMENT_EXPR:
15872     case POSTDECREMENT_EXPR:
15873       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15874       /* There are no opcodes for these operations.  */
15875       return 0;
15876
15877     case ADDR_EXPR:
15878       /* If we already want an address, see if there is INDIRECT_REF inside
15879          e.g. for &this->field.  */
15880       if (want_address)
15881         {
15882           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15883                        (loc, want_address == 2);
15884           if (list_ret)
15885             have_address = 1;
15886           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15887                    && (ret = cst_pool_loc_descr (loc)))
15888             have_address = 1;
15889         }
15890         /* Otherwise, process the argument and look for the address.  */
15891       if (!list_ret && !ret)
15892         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15893       else
15894         {
15895           if (want_address)
15896             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15897           return NULL;
15898         }
15899       break;
15900
15901     case VAR_DECL:
15902       if (DECL_THREAD_LOCAL_P (loc))
15903         {
15904           rtx rtl;
15905           enum dwarf_location_atom first_op;
15906           enum dwarf_location_atom second_op;
15907           bool dtprel = false;
15908
15909           if (targetm.have_tls)
15910             {
15911               /* If this is not defined, we have no way to emit the
15912                  data.  */
15913               if (!targetm.asm_out.output_dwarf_dtprel)
15914                 return 0;
15915
15916                /* The way DW_OP_GNU_push_tls_address is specified, we
15917                   can only look up addresses of objects in the current
15918                   module.  We used DW_OP_addr as first op, but that's
15919                   wrong, because DW_OP_addr is relocated by the debug
15920                   info consumer, while DW_OP_GNU_push_tls_address
15921                   operand shouldn't be.  */
15922               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15923                 return 0;
15924               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15925               dtprel = true;
15926               second_op = DW_OP_GNU_push_tls_address;
15927             }
15928           else
15929             {
15930               if (!targetm.emutls.debug_form_tls_address
15931                   || !(dwarf_version >= 3 || !dwarf_strict))
15932                 return 0;
15933               /* We stuffed the control variable into the DECL_VALUE_EXPR
15934                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15935                  no longer appear in gimple code.  We used the control
15936                  variable in specific so that we could pick it up here.  */
15937               loc = DECL_VALUE_EXPR (loc);
15938               first_op = DW_OP_addr;
15939               second_op = DW_OP_form_tls_address;
15940             }
15941
15942           rtl = rtl_for_decl_location (loc);
15943           if (rtl == NULL_RTX)
15944             return 0;
15945
15946           if (!MEM_P (rtl))
15947             return 0;
15948           rtl = XEXP (rtl, 0);
15949           if (! CONSTANT_P (rtl))
15950             return 0;
15951
15952           ret = new_loc_descr (first_op, 0, 0);
15953           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15954           ret->dw_loc_oprnd1.v.val_addr = rtl;
15955           ret->dtprel = dtprel;
15956
15957           ret1 = new_loc_descr (second_op, 0, 0);
15958           add_loc_descr (&ret, ret1);
15959
15960           have_address = 1;
15961           break;
15962         }
15963       /* FALLTHRU */
15964
15965     case PARM_DECL:
15966     case RESULT_DECL:
15967       if (DECL_HAS_VALUE_EXPR_P (loc))
15968         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15969                                    want_address);
15970       /* FALLTHRU */
15971
15972     case FUNCTION_DECL:
15973       {
15974         rtx rtl;
15975         var_loc_list *loc_list = lookup_decl_loc (loc);
15976
15977         if (loc_list && loc_list->first)
15978           {
15979             list_ret = dw_loc_list (loc_list, loc, want_address);
15980             have_address = want_address != 0;
15981             break;
15982           }
15983         rtl = rtl_for_decl_location (loc);
15984         if (rtl == NULL_RTX)
15985           {
15986             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15987             return 0;
15988           }
15989         else if (CONST_INT_P (rtl))
15990           {
15991             HOST_WIDE_INT val = INTVAL (rtl);
15992             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15993               val &= GET_MODE_MASK (DECL_MODE (loc));
15994             ret = int_loc_descriptor (val);
15995           }
15996         else if (GET_CODE (rtl) == CONST_STRING)
15997           {
15998             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15999             return 0;
16000           }
16001         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16002           {
16003             ret = new_loc_descr (DW_OP_addr, 0, 0);
16004             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16005             ret->dw_loc_oprnd1.v.val_addr = rtl;
16006           }
16007         else
16008           {
16009             enum machine_mode mode, mem_mode;
16010
16011             /* Certain constructs can only be represented at top-level.  */
16012             if (want_address == 2)
16013               {
16014                 ret = loc_descriptor (rtl, VOIDmode,
16015                                       VAR_INIT_STATUS_INITIALIZED);
16016                 have_address = 1;
16017               }
16018             else
16019               {
16020                 mode = GET_MODE (rtl);
16021                 mem_mode = VOIDmode;
16022                 if (MEM_P (rtl))
16023                   {
16024                     mem_mode = mode;
16025                     mode = get_address_mode (rtl);
16026                     rtl = XEXP (rtl, 0);
16027                     have_address = 1;
16028                   }
16029                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16030                                           VAR_INIT_STATUS_INITIALIZED);
16031               }
16032             if (!ret)
16033               expansion_failed (loc, rtl,
16034                                 "failed to produce loc descriptor for rtl");
16035           }
16036       }
16037       break;
16038
16039     case MEM_REF:
16040       /* ??? FIXME.  */
16041       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16042         return 0;
16043       /* Fallthru.  */
16044     case INDIRECT_REF:
16045       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16046       have_address = 1;
16047       break;
16048
16049     case COMPOUND_EXPR:
16050       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
16051
16052     CASE_CONVERT:
16053     case VIEW_CONVERT_EXPR:
16054     case SAVE_EXPR:
16055     case MODIFY_EXPR:
16056       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
16057
16058     case COMPONENT_REF:
16059     case BIT_FIELD_REF:
16060     case ARRAY_REF:
16061     case ARRAY_RANGE_REF:
16062     case REALPART_EXPR:
16063     case IMAGPART_EXPR:
16064       {
16065         tree obj, offset;
16066         HOST_WIDE_INT bitsize, bitpos, bytepos;
16067         enum machine_mode mode;
16068         int volatilep;
16069         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16070
16071         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16072                                    &unsignedp, &volatilep, false);
16073
16074         gcc_assert (obj != loc);
16075
16076         list_ret = loc_list_from_tree (obj,
16077                                        want_address == 2
16078                                        && !bitpos && !offset ? 2 : 1);
16079         /* TODO: We can extract value of the small expression via shifting even
16080            for nonzero bitpos.  */
16081         if (list_ret == 0)
16082           return 0;
16083         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16084           {
16085             expansion_failed (loc, NULL_RTX,
16086                               "bitfield access");
16087             return 0;
16088           }
16089
16090         if (offset != NULL_TREE)
16091           {
16092             /* Variable offset.  */
16093             list_ret1 = loc_list_from_tree (offset, 0);
16094             if (list_ret1 == 0)
16095               return 0;
16096             add_loc_list (&list_ret, list_ret1);
16097             if (!list_ret)
16098               return 0;
16099             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16100           }
16101
16102         bytepos = bitpos / BITS_PER_UNIT;
16103         if (bytepos > 0)
16104           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16105         else if (bytepos < 0)
16106           loc_list_plus_const (list_ret, bytepos);
16107
16108         have_address = 1;
16109         break;
16110       }
16111
16112     case INTEGER_CST:
16113       if ((want_address || !host_integerp (loc, 0))
16114           && (ret = cst_pool_loc_descr (loc)))
16115         have_address = 1;
16116       else if (want_address == 2
16117                && host_integerp (loc, 0)
16118                && (ret = address_of_int_loc_descriptor
16119                            (int_size_in_bytes (TREE_TYPE (loc)),
16120                             tree_low_cst (loc, 0))))
16121         have_address = 1;
16122       else if (host_integerp (loc, 0))
16123         ret = int_loc_descriptor (tree_low_cst (loc, 0));
16124       else
16125         {
16126           expansion_failed (loc, NULL_RTX,
16127                             "Integer operand is not host integer");
16128           return 0;
16129         }
16130       break;
16131
16132     case CONSTRUCTOR:
16133     case REAL_CST:
16134     case STRING_CST:
16135     case COMPLEX_CST:
16136       if ((ret = cst_pool_loc_descr (loc)))
16137         have_address = 1;
16138       else
16139       /* We can construct small constants here using int_loc_descriptor.  */
16140         expansion_failed (loc, NULL_RTX,
16141                           "constructor or constant not in constant pool");
16142       break;
16143
16144     case TRUTH_AND_EXPR:
16145     case TRUTH_ANDIF_EXPR:
16146     case BIT_AND_EXPR:
16147       op = DW_OP_and;
16148       goto do_binop;
16149
16150     case TRUTH_XOR_EXPR:
16151     case BIT_XOR_EXPR:
16152       op = DW_OP_xor;
16153       goto do_binop;
16154
16155     case TRUTH_OR_EXPR:
16156     case TRUTH_ORIF_EXPR:
16157     case BIT_IOR_EXPR:
16158       op = DW_OP_or;
16159       goto do_binop;
16160
16161     case FLOOR_DIV_EXPR:
16162     case CEIL_DIV_EXPR:
16163     case ROUND_DIV_EXPR:
16164     case TRUNC_DIV_EXPR:
16165       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16166         return 0;
16167       op = DW_OP_div;
16168       goto do_binop;
16169
16170     case MINUS_EXPR:
16171       op = DW_OP_minus;
16172       goto do_binop;
16173
16174     case FLOOR_MOD_EXPR:
16175     case CEIL_MOD_EXPR:
16176     case ROUND_MOD_EXPR:
16177     case TRUNC_MOD_EXPR:
16178       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16179         {
16180           op = DW_OP_mod;
16181           goto do_binop;
16182         }
16183       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16184       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16185       if (list_ret == 0 || list_ret1 == 0)
16186         return 0;
16187
16188       add_loc_list (&list_ret, list_ret1);
16189       if (list_ret == 0)
16190         return 0;
16191       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16192       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16193       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16194       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16195       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16196       break;
16197
16198     case MULT_EXPR:
16199       op = DW_OP_mul;
16200       goto do_binop;
16201
16202     case LSHIFT_EXPR:
16203       op = DW_OP_shl;
16204       goto do_binop;
16205
16206     case RSHIFT_EXPR:
16207       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16208       goto do_binop;
16209
16210     case POINTER_PLUS_EXPR:
16211     case PLUS_EXPR:
16212       if (host_integerp (TREE_OPERAND (loc, 1), 0))
16213         {
16214           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16215           if (list_ret == 0)
16216             return 0;
16217
16218           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
16219           break;
16220         }
16221
16222       op = DW_OP_plus;
16223       goto do_binop;
16224
16225     case LE_EXPR:
16226       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16227         return 0;
16228
16229       op = DW_OP_le;
16230       goto do_binop;
16231
16232     case GE_EXPR:
16233       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16234         return 0;
16235
16236       op = DW_OP_ge;
16237       goto do_binop;
16238
16239     case LT_EXPR:
16240       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16241         return 0;
16242
16243       op = DW_OP_lt;
16244       goto do_binop;
16245
16246     case GT_EXPR:
16247       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16248         return 0;
16249
16250       op = DW_OP_gt;
16251       goto do_binop;
16252
16253     case EQ_EXPR:
16254       op = DW_OP_eq;
16255       goto do_binop;
16256
16257     case NE_EXPR:
16258       op = DW_OP_ne;
16259       goto do_binop;
16260
16261     do_binop:
16262       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16263       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16264       if (list_ret == 0 || list_ret1 == 0)
16265         return 0;
16266
16267       add_loc_list (&list_ret, list_ret1);
16268       if (list_ret == 0)
16269         return 0;
16270       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16271       break;
16272
16273     case TRUTH_NOT_EXPR:
16274     case BIT_NOT_EXPR:
16275       op = DW_OP_not;
16276       goto do_unop;
16277
16278     case ABS_EXPR:
16279       op = DW_OP_abs;
16280       goto do_unop;
16281
16282     case NEGATE_EXPR:
16283       op = DW_OP_neg;
16284       goto do_unop;
16285
16286     do_unop:
16287       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16288       if (list_ret == 0)
16289         return 0;
16290
16291       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16292       break;
16293
16294     case MIN_EXPR:
16295     case MAX_EXPR:
16296       {
16297         const enum tree_code code =
16298           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16299
16300         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16301                       build2 (code, integer_type_node,
16302                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16303                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16304       }
16305
16306       /* ... fall through ...  */
16307
16308     case COND_EXPR:
16309       {
16310         dw_loc_descr_ref lhs
16311           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
16312         dw_loc_list_ref rhs
16313           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
16314         dw_loc_descr_ref bra_node, jump_node, tmp;
16315
16316         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16317         if (list_ret == 0 || lhs == 0 || rhs == 0)
16318           return 0;
16319
16320         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16321         add_loc_descr_to_each (list_ret, bra_node);
16322
16323         add_loc_list (&list_ret, rhs);
16324         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16325         add_loc_descr_to_each (list_ret, jump_node);
16326
16327         add_loc_descr_to_each (list_ret, lhs);
16328         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16329         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16330
16331         /* ??? Need a node to point the skip at.  Use a nop.  */
16332         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16333         add_loc_descr_to_each (list_ret, tmp);
16334         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16335         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16336       }
16337       break;
16338
16339     case FIX_TRUNC_EXPR:
16340       return 0;
16341
16342     default:
16343       /* Leave front-end specific codes as simply unknown.  This comes
16344          up, for instance, with the C STMT_EXPR.  */
16345       if ((unsigned int) TREE_CODE (loc)
16346           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16347         {
16348           expansion_failed (loc, NULL_RTX,
16349                             "language specific tree node");
16350           return 0;
16351         }
16352
16353 #ifdef ENABLE_CHECKING
16354       /* Otherwise this is a generic code; we should just lists all of
16355          these explicitly.  We forgot one.  */
16356       gcc_unreachable ();
16357 #else
16358       /* In a release build, we want to degrade gracefully: better to
16359          generate incomplete debugging information than to crash.  */
16360       return NULL;
16361 #endif
16362     }
16363
16364   if (!ret && !list_ret)
16365     return 0;
16366
16367   if (want_address == 2 && !have_address
16368       && (dwarf_version >= 4 || !dwarf_strict))
16369     {
16370       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16371         {
16372           expansion_failed (loc, NULL_RTX,
16373                             "DWARF address size mismatch");
16374           return 0;
16375         }
16376       if (ret)
16377         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16378       else
16379         add_loc_descr_to_each (list_ret,
16380                                new_loc_descr (DW_OP_stack_value, 0, 0));
16381       have_address = 1;
16382     }
16383   /* Show if we can't fill the request for an address.  */
16384   if (want_address && !have_address)
16385     {
16386       expansion_failed (loc, NULL_RTX,
16387                         "Want address and only have value");
16388       return 0;
16389     }
16390
16391   gcc_assert (!ret || !list_ret);
16392
16393   /* If we've got an address and don't want one, dereference.  */
16394   if (!want_address && have_address)
16395     {
16396       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16397
16398       if (size > DWARF2_ADDR_SIZE || size == -1)
16399         {
16400           expansion_failed (loc, NULL_RTX,
16401                             "DWARF address size mismatch");
16402           return 0;
16403         }
16404       else if (size == DWARF2_ADDR_SIZE)
16405         op = DW_OP_deref;
16406       else
16407         op = DW_OP_deref_size;
16408
16409       if (ret)
16410         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16411       else
16412         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16413     }
16414   if (ret)
16415     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16416
16417   return list_ret;
16418 }
16419
16420 /* Same as above but return only single location expression.  */
16421 static dw_loc_descr_ref
16422 loc_descriptor_from_tree (tree loc, int want_address)
16423 {
16424   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
16425   if (!ret)
16426     return NULL;
16427   if (ret->dw_loc_next)
16428     {
16429       expansion_failed (loc, NULL_RTX,
16430                         "Location list where only loc descriptor needed");
16431       return NULL;
16432     }
16433   return ret->expr;
16434 }
16435
16436 /* Given a value, round it up to the lowest multiple of `boundary'
16437    which is not less than the value itself.  */
16438
16439 static inline HOST_WIDE_INT
16440 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16441 {
16442   return (((value + boundary - 1) / boundary) * boundary);
16443 }
16444
16445 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16446    pointer to the declared type for the relevant field variable, or return
16447    `integer_type_node' if the given node turns out to be an
16448    ERROR_MARK node.  */
16449
16450 static inline tree
16451 field_type (const_tree decl)
16452 {
16453   tree type;
16454
16455   if (TREE_CODE (decl) == ERROR_MARK)
16456     return integer_type_node;
16457
16458   type = DECL_BIT_FIELD_TYPE (decl);
16459   if (type == NULL_TREE)
16460     type = TREE_TYPE (decl);
16461
16462   return type;
16463 }
16464
16465 /* Given a pointer to a tree node, return the alignment in bits for
16466    it, or else return BITS_PER_WORD if the node actually turns out to
16467    be an ERROR_MARK node.  */
16468
16469 static inline unsigned
16470 simple_type_align_in_bits (const_tree type)
16471 {
16472   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16473 }
16474
16475 static inline unsigned
16476 simple_decl_align_in_bits (const_tree decl)
16477 {
16478   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16479 }
16480
16481 /* Return the result of rounding T up to ALIGN.  */
16482
16483 static inline double_int
16484 round_up_to_align (double_int t, unsigned int align)
16485 {
16486   double_int alignd = uhwi_to_double_int (align);
16487   t = double_int_add (t, alignd);
16488   t = double_int_add (t, double_int_minus_one);
16489   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16490   t = double_int_mul (t, alignd);
16491   return t;
16492 }
16493
16494 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16495    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16496    or return 0 if we are unable to determine what that offset is, either
16497    because the argument turns out to be a pointer to an ERROR_MARK node, or
16498    because the offset is actually variable.  (We can't handle the latter case
16499    just yet).  */
16500
16501 static HOST_WIDE_INT
16502 field_byte_offset (const_tree decl)
16503 {
16504   double_int object_offset_in_bits;
16505   double_int object_offset_in_bytes;
16506   double_int bitpos_int;
16507
16508   if (TREE_CODE (decl) == ERROR_MARK)
16509     return 0;
16510
16511   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16512
16513   /* We cannot yet cope with fields whose positions are variable, so
16514      for now, when we see such things, we simply return 0.  Someday, we may
16515      be able to handle such cases, but it will be damn difficult.  */
16516   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16517     return 0;
16518
16519   bitpos_int = tree_to_double_int (bit_position (decl));
16520
16521 #ifdef PCC_BITFIELD_TYPE_MATTERS
16522   if (PCC_BITFIELD_TYPE_MATTERS)
16523     {
16524       tree type;
16525       tree field_size_tree;
16526       double_int deepest_bitpos;
16527       double_int field_size_in_bits;
16528       unsigned int type_align_in_bits;
16529       unsigned int decl_align_in_bits;
16530       double_int type_size_in_bits;
16531
16532       type = field_type (decl);
16533       type_size_in_bits = double_int_type_size_in_bits (type);
16534       type_align_in_bits = simple_type_align_in_bits (type);
16535
16536       field_size_tree = DECL_SIZE (decl);
16537
16538       /* The size could be unspecified if there was an error, or for
16539          a flexible array member.  */
16540       if (!field_size_tree)
16541         field_size_tree = bitsize_zero_node;
16542
16543       /* If the size of the field is not constant, use the type size.  */
16544       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16545         field_size_in_bits = tree_to_double_int (field_size_tree);
16546       else
16547         field_size_in_bits = type_size_in_bits;
16548
16549       decl_align_in_bits = simple_decl_align_in_bits (decl);
16550
16551       /* The GCC front-end doesn't make any attempt to keep track of the
16552          starting bit offset (relative to the start of the containing
16553          structure type) of the hypothetical "containing object" for a
16554          bit-field.  Thus, when computing the byte offset value for the
16555          start of the "containing object" of a bit-field, we must deduce
16556          this information on our own. This can be rather tricky to do in
16557          some cases.  For example, handling the following structure type
16558          definition when compiling for an i386/i486 target (which only
16559          aligns long long's to 32-bit boundaries) can be very tricky:
16560
16561          struct S { int field1; long long field2:31; };
16562
16563          Fortunately, there is a simple rule-of-thumb which can be used
16564          in such cases.  When compiling for an i386/i486, GCC will
16565          allocate 8 bytes for the structure shown above.  It decides to
16566          do this based upon one simple rule for bit-field allocation.
16567          GCC allocates each "containing object" for each bit-field at
16568          the first (i.e. lowest addressed) legitimate alignment boundary
16569          (based upon the required minimum alignment for the declared
16570          type of the field) which it can possibly use, subject to the
16571          condition that there is still enough available space remaining
16572          in the containing object (when allocated at the selected point)
16573          to fully accommodate all of the bits of the bit-field itself.
16574
16575          This simple rule makes it obvious why GCC allocates 8 bytes for
16576          each object of the structure type shown above.  When looking
16577          for a place to allocate the "containing object" for `field2',
16578          the compiler simply tries to allocate a 64-bit "containing
16579          object" at each successive 32-bit boundary (starting at zero)
16580          until it finds a place to allocate that 64- bit field such that
16581          at least 31 contiguous (and previously unallocated) bits remain
16582          within that selected 64 bit field.  (As it turns out, for the
16583          example above, the compiler finds it is OK to allocate the
16584          "containing object" 64-bit field at bit-offset zero within the
16585          structure type.)
16586
16587          Here we attempt to work backwards from the limited set of facts
16588          we're given, and we try to deduce from those facts, where GCC
16589          must have believed that the containing object started (within
16590          the structure type). The value we deduce is then used (by the
16591          callers of this routine) to generate DW_AT_location and
16592          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16593          the case of DW_AT_location, regular fields as well).  */
16594
16595       /* Figure out the bit-distance from the start of the structure to
16596          the "deepest" bit of the bit-field.  */
16597       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16598
16599       /* This is the tricky part.  Use some fancy footwork to deduce
16600          where the lowest addressed bit of the containing object must
16601          be.  */
16602       object_offset_in_bits
16603         = double_int_sub (deepest_bitpos, type_size_in_bits);
16604
16605       /* Round up to type_align by default.  This works best for
16606          bitfields.  */
16607       object_offset_in_bits
16608         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16609
16610       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16611         {
16612           object_offset_in_bits
16613             = double_int_sub (deepest_bitpos, type_size_in_bits);
16614
16615           /* Round up to decl_align instead.  */
16616           object_offset_in_bits
16617             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16618         }
16619     }
16620   else
16621 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16622     object_offset_in_bits = bitpos_int;
16623
16624   object_offset_in_bytes
16625     = double_int_div (object_offset_in_bits,
16626                       uhwi_to_double_int (BITS_PER_UNIT), true,
16627                       TRUNC_DIV_EXPR);
16628   return double_int_to_shwi (object_offset_in_bytes);
16629 }
16630 \f
16631 /* The following routines define various Dwarf attributes and any data
16632    associated with them.  */
16633
16634 /* Add a location description attribute value to a DIE.
16635
16636    This emits location attributes suitable for whole variables and
16637    whole parameters.  Note that the location attributes for struct fields are
16638    generated by the routine `data_member_location_attribute' below.  */
16639
16640 static inline void
16641 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16642                              dw_loc_list_ref descr)
16643 {
16644   if (descr == 0)
16645     return;
16646   if (single_element_loc_list_p (descr))
16647     add_AT_loc (die, attr_kind, descr->expr);
16648   else
16649     add_AT_loc_list (die, attr_kind, descr);
16650 }
16651
16652 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16653
16654 static void
16655 add_accessibility_attribute (dw_die_ref die, tree decl)
16656 {
16657   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16658      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16659      the default has always been DW_ACCESS_public.  */
16660   if (TREE_PROTECTED (decl))
16661     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16662   else if (TREE_PRIVATE (decl))
16663     {
16664       if (dwarf_version == 2
16665           || die->die_parent == NULL
16666           || die->die_parent->die_tag != DW_TAG_class_type)
16667         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16668     }
16669   else if (dwarf_version > 2
16670            && die->die_parent
16671            && die->die_parent->die_tag == DW_TAG_class_type)
16672     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16673 }
16674
16675 /* Attach the specialized form of location attribute used for data members of
16676    struct and union types.  In the special case of a FIELD_DECL node which
16677    represents a bit-field, the "offset" part of this special location
16678    descriptor must indicate the distance in bytes from the lowest-addressed
16679    byte of the containing struct or union type to the lowest-addressed byte of
16680    the "containing object" for the bit-field.  (See the `field_byte_offset'
16681    function above).
16682
16683    For any given bit-field, the "containing object" is a hypothetical object
16684    (of some integral or enum type) within which the given bit-field lives.  The
16685    type of this hypothetical "containing object" is always the same as the
16686    declared type of the individual bit-field itself (for GCC anyway... the
16687    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16688    bytes) of the hypothetical "containing object" which will be given in the
16689    DW_AT_byte_size attribute for this bit-field.  (See the
16690    `byte_size_attribute' function below.)  It is also used when calculating the
16691    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16692    function below.)  */
16693
16694 static void
16695 add_data_member_location_attribute (dw_die_ref die, tree decl)
16696 {
16697   HOST_WIDE_INT offset;
16698   dw_loc_descr_ref loc_descr = 0;
16699
16700   if (TREE_CODE (decl) == TREE_BINFO)
16701     {
16702       /* We're working on the TAG_inheritance for a base class.  */
16703       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16704         {
16705           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16706              aren't at a fixed offset from all (sub)objects of the same
16707              type.  We need to extract the appropriate offset from our
16708              vtable.  The following dwarf expression means
16709
16710                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16711
16712              This is specific to the V3 ABI, of course.  */
16713
16714           dw_loc_descr_ref tmp;
16715
16716           /* Make a copy of the object address.  */
16717           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16718           add_loc_descr (&loc_descr, tmp);
16719
16720           /* Extract the vtable address.  */
16721           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16722           add_loc_descr (&loc_descr, tmp);
16723
16724           /* Calculate the address of the offset.  */
16725           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16726           gcc_assert (offset < 0);
16727
16728           tmp = int_loc_descriptor (-offset);
16729           add_loc_descr (&loc_descr, tmp);
16730           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16731           add_loc_descr (&loc_descr, tmp);
16732
16733           /* Extract the offset.  */
16734           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16735           add_loc_descr (&loc_descr, tmp);
16736
16737           /* Add it to the object address.  */
16738           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16739           add_loc_descr (&loc_descr, tmp);
16740         }
16741       else
16742         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16743     }
16744   else
16745     offset = field_byte_offset (decl);
16746
16747   if (! loc_descr)
16748     {
16749       if (dwarf_version > 2)
16750         {
16751           /* Don't need to output a location expression, just the constant. */
16752           if (offset < 0)
16753             add_AT_int (die, DW_AT_data_member_location, offset);
16754           else
16755             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16756           return;
16757         }
16758       else
16759         {
16760           enum dwarf_location_atom op;
16761
16762           /* The DWARF2 standard says that we should assume that the structure
16763              address is already on the stack, so we can specify a structure
16764              field address by using DW_OP_plus_uconst.  */
16765
16766 #ifdef MIPS_DEBUGGING_INFO
16767           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16768              operator correctly.  It works only if we leave the offset on the
16769              stack.  */
16770           op = DW_OP_constu;
16771 #else
16772           op = DW_OP_plus_uconst;
16773 #endif
16774
16775           loc_descr = new_loc_descr (op, offset, 0);
16776         }
16777     }
16778
16779   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16780 }
16781
16782 /* Writes integer values to dw_vec_const array.  */
16783
16784 static void
16785 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16786 {
16787   while (size != 0)
16788     {
16789       *dest++ = val & 0xff;
16790       val >>= 8;
16791       --size;
16792     }
16793 }
16794
16795 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16796
16797 static HOST_WIDE_INT
16798 extract_int (const unsigned char *src, unsigned int size)
16799 {
16800   HOST_WIDE_INT val = 0;
16801
16802   src += size;
16803   while (size != 0)
16804     {
16805       val <<= 8;
16806       val |= *--src & 0xff;
16807       --size;
16808     }
16809   return val;
16810 }
16811
16812 /* Writes double_int values to dw_vec_const array.  */
16813
16814 static void
16815 insert_double (double_int val, unsigned char *dest)
16816 {
16817   unsigned char *p0 = dest;
16818   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16819
16820   if (WORDS_BIG_ENDIAN)
16821     {
16822       p0 = p1;
16823       p1 = dest;
16824     }
16825
16826   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16827   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16828 }
16829
16830 /* Writes floating point values to dw_vec_const array.  */
16831
16832 static void
16833 insert_float (const_rtx rtl, unsigned char *array)
16834 {
16835   REAL_VALUE_TYPE rv;
16836   long val[4];
16837   int i;
16838
16839   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16840   real_to_target (val, &rv, GET_MODE (rtl));
16841
16842   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16843   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16844     {
16845       insert_int (val[i], 4, array);
16846       array += 4;
16847     }
16848 }
16849
16850 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16851    does not have a "location" either in memory or in a register.  These
16852    things can arise in GNU C when a constant is passed as an actual parameter
16853    to an inlined function.  They can also arise in C++ where declared
16854    constants do not necessarily get memory "homes".  */
16855
16856 static bool
16857 add_const_value_attribute (dw_die_ref die, rtx rtl)
16858 {
16859   switch (GET_CODE (rtl))
16860     {
16861     case CONST_INT:
16862       {
16863         HOST_WIDE_INT val = INTVAL (rtl);
16864
16865         if (val < 0)
16866           add_AT_int (die, DW_AT_const_value, val);
16867         else
16868           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16869       }
16870       return true;
16871
16872     case CONST_DOUBLE:
16873       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16874          floating-point constant.  A CONST_DOUBLE is used whenever the
16875          constant requires more than one word in order to be adequately
16876          represented.  */
16877       {
16878         enum machine_mode mode = GET_MODE (rtl);
16879
16880         if (SCALAR_FLOAT_MODE_P (mode))
16881           {
16882             unsigned int length = GET_MODE_SIZE (mode);
16883             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16884
16885             insert_float (rtl, array);
16886             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16887           }
16888         else
16889           add_AT_double (die, DW_AT_const_value,
16890                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16891       }
16892       return true;
16893
16894     case CONST_VECTOR:
16895       {
16896         enum machine_mode mode = GET_MODE (rtl);
16897         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16898         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16899         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16900           (length * elt_size);
16901         unsigned int i;
16902         unsigned char *p;
16903
16904         switch (GET_MODE_CLASS (mode))
16905           {
16906           case MODE_VECTOR_INT:
16907             for (i = 0, p = array; i < length; i++, p += elt_size)
16908               {
16909                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16910                 double_int val = rtx_to_double_int (elt);
16911
16912                 if (elt_size <= sizeof (HOST_WIDE_INT))
16913                   insert_int (double_int_to_shwi (val), elt_size, p);
16914                 else
16915                   {
16916                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16917                     insert_double (val, p);
16918                   }
16919               }
16920             break;
16921
16922           case MODE_VECTOR_FLOAT:
16923             for (i = 0, p = array; i < length; i++, p += elt_size)
16924               {
16925                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16926                 insert_float (elt, p);
16927               }
16928             break;
16929
16930           default:
16931             gcc_unreachable ();
16932           }
16933
16934         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16935       }
16936       return true;
16937
16938     case CONST_STRING:
16939       if (dwarf_version >= 4 || !dwarf_strict)
16940         {
16941           dw_loc_descr_ref loc_result;
16942           resolve_one_addr (&rtl, NULL);
16943         rtl_addr:
16944           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16945           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16946           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16947           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16948           add_AT_loc (die, DW_AT_location, loc_result);
16949           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16950           return true;
16951         }
16952       return false;
16953
16954     case CONST:
16955       if (CONSTANT_P (XEXP (rtl, 0)))
16956         return add_const_value_attribute (die, XEXP (rtl, 0));
16957       /* FALLTHROUGH */
16958     case SYMBOL_REF:
16959       if (!const_ok_for_output (rtl))
16960         return false;
16961     case LABEL_REF:
16962       if (dwarf_version >= 4 || !dwarf_strict)
16963         goto rtl_addr;
16964       return false;
16965
16966     case PLUS:
16967       /* In cases where an inlined instance of an inline function is passed
16968          the address of an `auto' variable (which is local to the caller) we
16969          can get a situation where the DECL_RTL of the artificial local
16970          variable (for the inlining) which acts as a stand-in for the
16971          corresponding formal parameter (of the inline function) will look
16972          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16973          exactly a compile-time constant expression, but it isn't the address
16974          of the (artificial) local variable either.  Rather, it represents the
16975          *value* which the artificial local variable always has during its
16976          lifetime.  We currently have no way to represent such quasi-constant
16977          values in Dwarf, so for now we just punt and generate nothing.  */
16978       return false;
16979
16980     case HIGH:
16981     case CONST_FIXED:
16982       return false;
16983
16984     case MEM:
16985       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16986           && MEM_READONLY_P (rtl)
16987           && GET_MODE (rtl) == BLKmode)
16988         {
16989           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16990           return true;
16991         }
16992       return false;
16993
16994     default:
16995       /* No other kinds of rtx should be possible here.  */
16996       gcc_unreachable ();
16997     }
16998   return false;
16999 }
17000
17001 /* Determine whether the evaluation of EXPR references any variables
17002    or functions which aren't otherwise used (and therefore may not be
17003    output).  */
17004 static tree
17005 reference_to_unused (tree * tp, int * walk_subtrees,
17006                      void * data ATTRIBUTE_UNUSED)
17007 {
17008   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17009     *walk_subtrees = 0;
17010
17011   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17012       && ! TREE_ASM_WRITTEN (*tp))
17013     return *tp;
17014   /* ???  The C++ FE emits debug information for using decls, so
17015      putting gcc_unreachable here falls over.  See PR31899.  For now
17016      be conservative.  */
17017   else if (!cgraph_global_info_ready
17018            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17019     return *tp;
17020   else if (TREE_CODE (*tp) == VAR_DECL)
17021     {
17022       struct varpool_node *node = varpool_get_node (*tp);
17023       if (!node || !node->needed)
17024         return *tp;
17025     }
17026   else if (TREE_CODE (*tp) == FUNCTION_DECL
17027            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17028     {
17029       /* The call graph machinery must have finished analyzing,
17030          optimizing and gimplifying the CU by now.
17031          So if *TP has no call graph node associated
17032          to it, it means *TP will not be emitted.  */
17033       if (!cgraph_get_node (*tp))
17034         return *tp;
17035     }
17036   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17037     return *tp;
17038
17039   return NULL_TREE;
17040 }
17041
17042 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17043    for use in a later add_const_value_attribute call.  */
17044
17045 static rtx
17046 rtl_for_decl_init (tree init, tree type)
17047 {
17048   rtx rtl = NULL_RTX;
17049
17050   STRIP_NOPS (init);
17051
17052   /* If a variable is initialized with a string constant without embedded
17053      zeros, build CONST_STRING.  */
17054   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17055     {
17056       tree enttype = TREE_TYPE (type);
17057       tree domain = TYPE_DOMAIN (type);
17058       enum machine_mode mode = TYPE_MODE (enttype);
17059
17060       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17061           && domain
17062           && integer_zerop (TYPE_MIN_VALUE (domain))
17063           && compare_tree_int (TYPE_MAX_VALUE (domain),
17064                                TREE_STRING_LENGTH (init) - 1) == 0
17065           && ((size_t) TREE_STRING_LENGTH (init)
17066               == strlen (TREE_STRING_POINTER (init)) + 1))
17067         {
17068           rtl = gen_rtx_CONST_STRING (VOIDmode,
17069                                       ggc_strdup (TREE_STRING_POINTER (init)));
17070           rtl = gen_rtx_MEM (BLKmode, rtl);
17071           MEM_READONLY_P (rtl) = 1;
17072         }
17073     }
17074   /* Other aggregates, and complex values, could be represented using
17075      CONCAT: FIXME!  */
17076   else if (AGGREGATE_TYPE_P (type)
17077            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17078                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17079            || TREE_CODE (type) == COMPLEX_TYPE)
17080     ;
17081   /* Vectors only work if their mode is supported by the target.
17082      FIXME: generic vectors ought to work too.  */
17083   else if (TREE_CODE (type) == VECTOR_TYPE
17084            && !VECTOR_MODE_P (TYPE_MODE (type)))
17085     ;
17086   /* If the initializer is something that we know will expand into an
17087      immediate RTL constant, expand it now.  We must be careful not to
17088      reference variables which won't be output.  */
17089   else if (initializer_constant_valid_p (init, type)
17090            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17091     {
17092       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17093          possible.  */
17094       if (TREE_CODE (type) == VECTOR_TYPE)
17095         switch (TREE_CODE (init))
17096           {
17097           case VECTOR_CST:
17098             break;
17099           case CONSTRUCTOR:
17100             if (TREE_CONSTANT (init))
17101               {
17102                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
17103                 bool constant_p = true;
17104                 tree value;
17105                 unsigned HOST_WIDE_INT ix;
17106
17107                 /* Even when ctor is constant, it might contain non-*_CST
17108                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17109                    belong into VECTOR_CST nodes.  */
17110                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17111                   if (!CONSTANT_CLASS_P (value))
17112                     {
17113                       constant_p = false;
17114                       break;
17115                     }
17116
17117                 if (constant_p)
17118                   {
17119                     init = build_vector_from_ctor (type, elts);
17120                     break;
17121                   }
17122               }
17123             /* FALLTHRU */
17124
17125           default:
17126             return NULL;
17127           }
17128
17129       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17130
17131       /* If expand_expr returns a MEM, it wasn't immediate.  */
17132       gcc_assert (!rtl || !MEM_P (rtl));
17133     }
17134
17135   return rtl;
17136 }
17137
17138 /* Generate RTL for the variable DECL to represent its location.  */
17139
17140 static rtx
17141 rtl_for_decl_location (tree decl)
17142 {
17143   rtx rtl;
17144
17145   /* Here we have to decide where we are going to say the parameter "lives"
17146      (as far as the debugger is concerned).  We only have a couple of
17147      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17148
17149      DECL_RTL normally indicates where the parameter lives during most of the
17150      activation of the function.  If optimization is enabled however, this
17151      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17152      that the parameter doesn't really live anywhere (as far as the code
17153      generation parts of GCC are concerned) during most of the function's
17154      activation.  That will happen (for example) if the parameter is never
17155      referenced within the function.
17156
17157      We could just generate a location descriptor here for all non-NULL
17158      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17159      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17160      where DECL_RTL is NULL or is a pseudo-reg.
17161
17162      Note however that we can only get away with using DECL_INCOMING_RTL as
17163      a backup substitute for DECL_RTL in certain limited cases.  In cases
17164      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17165      we can be sure that the parameter was passed using the same type as it is
17166      declared to have within the function, and that its DECL_INCOMING_RTL
17167      points us to a place where a value of that type is passed.
17168
17169      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17170      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17171      because in these cases DECL_INCOMING_RTL points us to a value of some
17172      type which is *different* from the type of the parameter itself.  Thus,
17173      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17174      such cases, the debugger would end up (for example) trying to fetch a
17175      `float' from a place which actually contains the first part of a
17176      `double'.  That would lead to really incorrect and confusing
17177      output at debug-time.
17178
17179      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17180      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17181      are a couple of exceptions however.  On little-endian machines we can
17182      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17183      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17184      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17185      when (on a little-endian machine) a non-prototyped function has a
17186      parameter declared to be of type `short' or `char'.  In such cases,
17187      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17188      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17189      passed `int' value.  If the debugger then uses that address to fetch
17190      a `short' or a `char' (on a little-endian machine) the result will be
17191      the correct data, so we allow for such exceptional cases below.
17192
17193      Note that our goal here is to describe the place where the given formal
17194      parameter lives during most of the function's activation (i.e. between the
17195      end of the prologue and the start of the epilogue).  We'll do that as best
17196      as we can. Note however that if the given formal parameter is modified
17197      sometime during the execution of the function, then a stack backtrace (at
17198      debug-time) will show the function as having been called with the *new*
17199      value rather than the value which was originally passed in.  This happens
17200      rarely enough that it is not a major problem, but it *is* a problem, and
17201      I'd like to fix it.
17202
17203      A future version of dwarf2out.c may generate two additional attributes for
17204      any given DW_TAG_formal_parameter DIE which will describe the "passed
17205      type" and the "passed location" for the given formal parameter in addition
17206      to the attributes we now generate to indicate the "declared type" and the
17207      "active location" for each parameter.  This additional set of attributes
17208      could be used by debuggers for stack backtraces. Separately, note that
17209      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17210      This happens (for example) for inlined-instances of inline function formal
17211      parameters which are never referenced.  This really shouldn't be
17212      happening.  All PARM_DECL nodes should get valid non-NULL
17213      DECL_INCOMING_RTL values.  FIXME.  */
17214
17215   /* Use DECL_RTL as the "location" unless we find something better.  */
17216   rtl = DECL_RTL_IF_SET (decl);
17217
17218   /* When generating abstract instances, ignore everything except
17219      constants, symbols living in memory, and symbols living in
17220      fixed registers.  */
17221   if (! reload_completed)
17222     {
17223       if (rtl
17224           && (CONSTANT_P (rtl)
17225               || (MEM_P (rtl)
17226                   && CONSTANT_P (XEXP (rtl, 0)))
17227               || (REG_P (rtl)
17228                   && TREE_CODE (decl) == VAR_DECL
17229                   && TREE_STATIC (decl))))
17230         {
17231           rtl = targetm.delegitimize_address (rtl);
17232           return rtl;
17233         }
17234       rtl = NULL_RTX;
17235     }
17236   else if (TREE_CODE (decl) == PARM_DECL)
17237     {
17238       if (rtl == NULL_RTX
17239           || is_pseudo_reg (rtl)
17240           || (MEM_P (rtl)
17241               && is_pseudo_reg (XEXP (rtl, 0))
17242               && DECL_INCOMING_RTL (decl)
17243               && MEM_P (DECL_INCOMING_RTL (decl))
17244               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17245         {
17246           tree declared_type = TREE_TYPE (decl);
17247           tree passed_type = DECL_ARG_TYPE (decl);
17248           enum machine_mode dmode = TYPE_MODE (declared_type);
17249           enum machine_mode pmode = TYPE_MODE (passed_type);
17250
17251           /* This decl represents a formal parameter which was optimized out.
17252              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17253              all cases where (rtl == NULL_RTX) just below.  */
17254           if (dmode == pmode)
17255             rtl = DECL_INCOMING_RTL (decl);
17256           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17257                    && SCALAR_INT_MODE_P (dmode)
17258                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17259                    && DECL_INCOMING_RTL (decl))
17260             {
17261               rtx inc = DECL_INCOMING_RTL (decl);
17262               if (REG_P (inc))
17263                 rtl = inc;
17264               else if (MEM_P (inc))
17265                 {
17266                   if (BYTES_BIG_ENDIAN)
17267                     rtl = adjust_address_nv (inc, dmode,
17268                                              GET_MODE_SIZE (pmode)
17269                                              - GET_MODE_SIZE (dmode));
17270                   else
17271                     rtl = inc;
17272                 }
17273             }
17274         }
17275
17276       /* If the parm was passed in registers, but lives on the stack, then
17277          make a big endian correction if the mode of the type of the
17278          parameter is not the same as the mode of the rtl.  */
17279       /* ??? This is the same series of checks that are made in dbxout.c before
17280          we reach the big endian correction code there.  It isn't clear if all
17281          of these checks are necessary here, but keeping them all is the safe
17282          thing to do.  */
17283       else if (MEM_P (rtl)
17284                && XEXP (rtl, 0) != const0_rtx
17285                && ! CONSTANT_P (XEXP (rtl, 0))
17286                /* Not passed in memory.  */
17287                && !MEM_P (DECL_INCOMING_RTL (decl))
17288                /* Not passed by invisible reference.  */
17289                && (!REG_P (XEXP (rtl, 0))
17290                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17291                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17292 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17293                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17294 #endif
17295                      )
17296                /* Big endian correction check.  */
17297                && BYTES_BIG_ENDIAN
17298                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17299                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17300                    < UNITS_PER_WORD))
17301         {
17302           int offset = (UNITS_PER_WORD
17303                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17304
17305           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17306                              plus_constant (XEXP (rtl, 0), offset));
17307         }
17308     }
17309   else if (TREE_CODE (decl) == VAR_DECL
17310            && rtl
17311            && MEM_P (rtl)
17312            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17313            && BYTES_BIG_ENDIAN)
17314     {
17315       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17316       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17317
17318       /* If a variable is declared "register" yet is smaller than
17319          a register, then if we store the variable to memory, it
17320          looks like we're storing a register-sized value, when in
17321          fact we are not.  We need to adjust the offset of the
17322          storage location to reflect the actual value's bytes,
17323          else gdb will not be able to display it.  */
17324       if (rsize > dsize)
17325         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17326                            plus_constant (XEXP (rtl, 0), rsize-dsize));
17327     }
17328
17329   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17330      and will have been substituted directly into all expressions that use it.
17331      C does not have such a concept, but C++ and other languages do.  */
17332   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17333     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17334
17335   if (rtl)
17336     rtl = targetm.delegitimize_address (rtl);
17337
17338   /* If we don't look past the constant pool, we risk emitting a
17339      reference to a constant pool entry that isn't referenced from
17340      code, and thus is not emitted.  */
17341   if (rtl)
17342     rtl = avoid_constant_pool_reference (rtl);
17343
17344   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17345      in the current CU, resolve_addr will remove the expression referencing
17346      it.  */
17347   if (rtl == NULL_RTX
17348       && TREE_CODE (decl) == VAR_DECL
17349       && !DECL_EXTERNAL (decl)
17350       && TREE_STATIC (decl)
17351       && DECL_NAME (decl)
17352       && !DECL_HARD_REGISTER (decl)
17353       && DECL_MODE (decl) != VOIDmode)
17354     {
17355       rtl = make_decl_rtl_for_debug (decl);
17356       if (!MEM_P (rtl)
17357           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17358           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17359         rtl = NULL_RTX;
17360     }
17361
17362   return rtl;
17363 }
17364
17365 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17366    returned.  If so, the decl for the COMMON block is returned, and the
17367    value is the offset into the common block for the symbol.  */
17368
17369 static tree
17370 fortran_common (tree decl, HOST_WIDE_INT *value)
17371 {
17372   tree val_expr, cvar;
17373   enum machine_mode mode;
17374   HOST_WIDE_INT bitsize, bitpos;
17375   tree offset;
17376   int volatilep = 0, unsignedp = 0;
17377
17378   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17379      it does not have a value (the offset into the common area), or if it
17380      is thread local (as opposed to global) then it isn't common, and shouldn't
17381      be handled as such.  */
17382   if (TREE_CODE (decl) != VAR_DECL
17383       || !TREE_STATIC (decl)
17384       || !DECL_HAS_VALUE_EXPR_P (decl)
17385       || !is_fortran ())
17386     return NULL_TREE;
17387
17388   val_expr = DECL_VALUE_EXPR (decl);
17389   if (TREE_CODE (val_expr) != COMPONENT_REF)
17390     return NULL_TREE;
17391
17392   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17393                               &mode, &unsignedp, &volatilep, true);
17394
17395   if (cvar == NULL_TREE
17396       || TREE_CODE (cvar) != VAR_DECL
17397       || DECL_ARTIFICIAL (cvar)
17398       || !TREE_PUBLIC (cvar))
17399     return NULL_TREE;
17400
17401   *value = 0;
17402   if (offset != NULL)
17403     {
17404       if (!host_integerp (offset, 0))
17405         return NULL_TREE;
17406       *value = tree_low_cst (offset, 0);
17407     }
17408   if (bitpos != 0)
17409     *value += bitpos / BITS_PER_UNIT;
17410
17411   return cvar;
17412 }
17413
17414 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17415    data attribute for a variable or a parameter.  We generate the
17416    DW_AT_const_value attribute only in those cases where the given variable
17417    or parameter does not have a true "location" either in memory or in a
17418    register.  This can happen (for example) when a constant is passed as an
17419    actual argument in a call to an inline function.  (It's possible that
17420    these things can crop up in other ways also.)  Note that one type of
17421    constant value which can be passed into an inlined function is a constant
17422    pointer.  This can happen for example if an actual argument in an inlined
17423    function call evaluates to a compile-time constant address.
17424
17425    CACHE_P is true if it is worth caching the location list for DECL,
17426    so that future calls can reuse it rather than regenerate it from scratch.
17427    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17428    since we will need to refer to them each time the function is inlined.  */
17429
17430 static bool
17431 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
17432                                        enum dwarf_attribute attr)
17433 {
17434   rtx rtl;
17435   dw_loc_list_ref list;
17436   var_loc_list *loc_list;
17437   cached_dw_loc_list *cache;
17438   void **slot;
17439
17440   if (TREE_CODE (decl) == ERROR_MARK)
17441     return false;
17442
17443   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17444               || TREE_CODE (decl) == RESULT_DECL);
17445
17446   /* Try to get some constant RTL for this decl, and use that as the value of
17447      the location.  */
17448
17449   rtl = rtl_for_decl_location (decl);
17450   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17451       && add_const_value_attribute (die, rtl))
17452     return true;
17453
17454   /* See if we have single element location list that is equivalent to
17455      a constant value.  That way we are better to use add_const_value_attribute
17456      rather than expanding constant value equivalent.  */
17457   loc_list = lookup_decl_loc (decl);
17458   if (loc_list
17459       && loc_list->first
17460       && loc_list->first->next == NULL
17461       && NOTE_P (loc_list->first->loc)
17462       && NOTE_VAR_LOCATION (loc_list->first->loc)
17463       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17464     {
17465       struct var_loc_node *node;
17466
17467       node = loc_list->first;
17468       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17469       if (GET_CODE (rtl) == EXPR_LIST)
17470         rtl = XEXP (rtl, 0);
17471       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17472           && add_const_value_attribute (die, rtl))
17473          return true;
17474     }
17475   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17476      list several times.  See if we've already cached the contents.  */
17477   list = NULL;
17478   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17479     cache_p = false;
17480   if (cache_p)
17481     {
17482       cache = (cached_dw_loc_list *)
17483         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
17484       if (cache)
17485         list = cache->loc_list;
17486     }
17487   if (list == NULL)
17488     {
17489       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17490       /* It is usually worth caching this result if the decl is from
17491          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17492       if (cache_p && list && list->dw_loc_next)
17493         {
17494           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17495                                            DECL_UID (decl), INSERT);
17496           cache = ggc_alloc_cleared_cached_dw_loc_list ();
17497           cache->decl_id = DECL_UID (decl);
17498           cache->loc_list = list;
17499           *slot = cache;
17500         }
17501     }
17502   if (list)
17503     {
17504       add_AT_location_description (die, attr, list);
17505       return true;
17506     }
17507   /* None of that worked, so it must not really have a location;
17508      try adding a constant value attribute from the DECL_INITIAL.  */
17509   return tree_add_const_value_attribute_for_decl (die, decl);
17510 }
17511
17512 /* Add VARIABLE and DIE into deferred locations list.  */
17513
17514 static void
17515 defer_location (tree variable, dw_die_ref die)
17516 {
17517   deferred_locations entry;
17518   entry.variable = variable;
17519   entry.die = die;
17520   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17521 }
17522
17523 /* Helper function for tree_add_const_value_attribute.  Natively encode
17524    initializer INIT into an array.  Return true if successful.  */
17525
17526 static bool
17527 native_encode_initializer (tree init, unsigned char *array, int size)
17528 {
17529   tree type;
17530
17531   if (init == NULL_TREE)
17532     return false;
17533
17534   STRIP_NOPS (init);
17535   switch (TREE_CODE (init))
17536     {
17537     case STRING_CST:
17538       type = TREE_TYPE (init);
17539       if (TREE_CODE (type) == ARRAY_TYPE)
17540         {
17541           tree enttype = TREE_TYPE (type);
17542           enum machine_mode mode = TYPE_MODE (enttype);
17543
17544           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17545             return false;
17546           if (int_size_in_bytes (type) != size)
17547             return false;
17548           if (size > TREE_STRING_LENGTH (init))
17549             {
17550               memcpy (array, TREE_STRING_POINTER (init),
17551                       TREE_STRING_LENGTH (init));
17552               memset (array + TREE_STRING_LENGTH (init),
17553                       '\0', size - TREE_STRING_LENGTH (init));
17554             }
17555           else
17556             memcpy (array, TREE_STRING_POINTER (init), size);
17557           return true;
17558         }
17559       return false;
17560     case CONSTRUCTOR:
17561       type = TREE_TYPE (init);
17562       if (int_size_in_bytes (type) != size)
17563         return false;
17564       if (TREE_CODE (type) == ARRAY_TYPE)
17565         {
17566           HOST_WIDE_INT min_index;
17567           unsigned HOST_WIDE_INT cnt;
17568           int curpos = 0, fieldsize;
17569           constructor_elt *ce;
17570
17571           if (TYPE_DOMAIN (type) == NULL_TREE
17572               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17573             return false;
17574
17575           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17576           if (fieldsize <= 0)
17577             return false;
17578
17579           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17580           memset (array, '\0', size);
17581           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17582             {
17583               tree val = ce->value;
17584               tree index = ce->index;
17585               int pos = curpos;
17586               if (index && TREE_CODE (index) == RANGE_EXPR)
17587                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17588                       * fieldsize;
17589               else if (index)
17590                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17591
17592               if (val)
17593                 {
17594                   STRIP_NOPS (val);
17595                   if (!native_encode_initializer (val, array + pos, fieldsize))
17596                     return false;
17597                 }
17598               curpos = pos + fieldsize;
17599               if (index && TREE_CODE (index) == RANGE_EXPR)
17600                 {
17601                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17602                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17603                   while (count > 0)
17604                     {
17605                       if (val)
17606                         memcpy (array + curpos, array + pos, fieldsize);
17607                       curpos += fieldsize;
17608                     }
17609                 }
17610               gcc_assert (curpos <= size);
17611             }
17612           return true;
17613         }
17614       else if (TREE_CODE (type) == RECORD_TYPE
17615                || TREE_CODE (type) == UNION_TYPE)
17616         {
17617           tree field = NULL_TREE;
17618           unsigned HOST_WIDE_INT cnt;
17619           constructor_elt *ce;
17620
17621           if (int_size_in_bytes (type) != size)
17622             return false;
17623
17624           if (TREE_CODE (type) == RECORD_TYPE)
17625             field = TYPE_FIELDS (type);
17626
17627           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17628             {
17629               tree val = ce->value;
17630               int pos, fieldsize;
17631
17632               if (ce->index != 0)
17633                 field = ce->index;
17634
17635               if (val)
17636                 STRIP_NOPS (val);
17637
17638               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17639                 return false;
17640
17641               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17642                   && TYPE_DOMAIN (TREE_TYPE (field))
17643                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17644                 return false;
17645               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17646                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17647                 return false;
17648               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17649               pos = int_byte_position (field);
17650               gcc_assert (pos + fieldsize <= size);
17651               if (val
17652                   && !native_encode_initializer (val, array + pos, fieldsize))
17653                 return false;
17654             }
17655           return true;
17656         }
17657       return false;
17658     case VIEW_CONVERT_EXPR:
17659     case NON_LVALUE_EXPR:
17660       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17661     default:
17662       return native_encode_expr (init, array, size) == size;
17663     }
17664 }
17665
17666 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17667    attribute is the const value T.  */
17668
17669 static bool
17670 tree_add_const_value_attribute (dw_die_ref die, tree t)
17671 {
17672   tree init;
17673   tree type = TREE_TYPE (t);
17674   rtx rtl;
17675
17676   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17677     return false;
17678
17679   init = t;
17680   gcc_assert (!DECL_P (init));
17681
17682   rtl = rtl_for_decl_init (init, type);
17683   if (rtl)
17684     return add_const_value_attribute (die, rtl);
17685   /* If the host and target are sane, try harder.  */
17686   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17687            && initializer_constant_valid_p (init, type))
17688     {
17689       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17690       if (size > 0 && (int) size == size)
17691         {
17692           unsigned char *array = (unsigned char *)
17693             ggc_alloc_cleared_atomic (size);
17694
17695           if (native_encode_initializer (init, array, size))
17696             {
17697               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17698               return true;
17699             }
17700         }
17701     }
17702   return false;
17703 }
17704
17705 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17706    attribute is the const value of T, where T is an integral constant
17707    variable with static storage duration
17708    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17709
17710 static bool
17711 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17712 {
17713
17714   if (!decl
17715       || (TREE_CODE (decl) != VAR_DECL
17716           && TREE_CODE (decl) != CONST_DECL))
17717     return false;
17718
17719     if (TREE_READONLY (decl)
17720         && ! TREE_THIS_VOLATILE (decl)
17721         && DECL_INITIAL (decl))
17722       /* OK */;
17723     else
17724       return false;
17725
17726   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17727   if (get_AT (var_die, DW_AT_const_value))
17728     return false;
17729
17730   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17731 }
17732
17733 /* Convert the CFI instructions for the current function into a
17734    location list.  This is used for DW_AT_frame_base when we targeting
17735    a dwarf2 consumer that does not support the dwarf3
17736    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17737    expressions.  */
17738
17739 static dw_loc_list_ref
17740 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17741 {
17742   int ix;
17743   dw_fde_ref fde;
17744   dw_loc_list_ref list, *list_tail;
17745   dw_cfi_ref cfi;
17746   dw_cfa_location last_cfa, next_cfa;
17747   const char *start_label, *last_label, *section;
17748   dw_cfa_location remember;
17749
17750   fde = current_fde ();
17751   gcc_assert (fde != NULL);
17752
17753   section = secname_for_decl (current_function_decl);
17754   list_tail = &list;
17755   list = NULL;
17756
17757   memset (&next_cfa, 0, sizeof (next_cfa));
17758   next_cfa.reg = INVALID_REGNUM;
17759   remember = next_cfa;
17760
17761   start_label = fde->dw_fde_begin;
17762
17763   /* ??? Bald assumption that the CIE opcode list does not contain
17764      advance opcodes.  */
17765   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
17766     lookup_cfa_1 (cfi, &next_cfa, &remember);
17767
17768   last_cfa = next_cfa;
17769   last_label = start_label;
17770
17771   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17772     {
17773       /* If the first partition contained no CFI adjustments, the
17774          CIE opcodes apply to the whole first partition.  */
17775       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17776                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17777       list_tail =&(*list_tail)->dw_loc_next;
17778       start_label = last_label = fde->dw_fde_second_begin;
17779     }
17780
17781   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
17782     {
17783       switch (cfi->dw_cfi_opc)
17784         {
17785         case DW_CFA_set_loc:
17786         case DW_CFA_advance_loc1:
17787         case DW_CFA_advance_loc2:
17788         case DW_CFA_advance_loc4:
17789           if (!cfa_equal_p (&last_cfa, &next_cfa))
17790             {
17791               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17792                                          start_label, last_label, section);
17793
17794               list_tail = &(*list_tail)->dw_loc_next;
17795               last_cfa = next_cfa;
17796               start_label = last_label;
17797             }
17798           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17799           break;
17800
17801         case DW_CFA_advance_loc:
17802           /* The encoding is complex enough that we should never emit this.  */
17803           gcc_unreachable ();
17804
17805         default:
17806           lookup_cfa_1 (cfi, &next_cfa, &remember);
17807           break;
17808         }
17809       if (ix + 1 == fde->dw_fde_switch_cfi_index)
17810         {
17811           if (!cfa_equal_p (&last_cfa, &next_cfa))
17812             {
17813               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17814                                          start_label, last_label, section);
17815
17816               list_tail = &(*list_tail)->dw_loc_next;
17817               last_cfa = next_cfa;
17818               start_label = last_label;
17819             }
17820           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17821                                      start_label, fde->dw_fde_end, section);
17822           list_tail = &(*list_tail)->dw_loc_next;
17823           start_label = last_label = fde->dw_fde_second_begin;
17824         }
17825     }
17826
17827   if (!cfa_equal_p (&last_cfa, &next_cfa))
17828     {
17829       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17830                                  start_label, last_label, section);
17831       list_tail = &(*list_tail)->dw_loc_next;
17832       start_label = last_label;
17833     }
17834
17835   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17836                              start_label,
17837                              fde->dw_fde_second_begin
17838                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17839                              section);
17840
17841   if (list && list->dw_loc_next)
17842     gen_llsym (list);
17843
17844   return list;
17845 }
17846
17847 /* Compute a displacement from the "steady-state frame pointer" to the
17848    frame base (often the same as the CFA), and store it in
17849    frame_pointer_fb_offset.  OFFSET is added to the displacement
17850    before the latter is negated.  */
17851
17852 static void
17853 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17854 {
17855   rtx reg, elim;
17856
17857 #ifdef FRAME_POINTER_CFA_OFFSET
17858   reg = frame_pointer_rtx;
17859   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17860 #else
17861   reg = arg_pointer_rtx;
17862   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17863 #endif
17864
17865   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17866   if (GET_CODE (elim) == PLUS)
17867     {
17868       offset += INTVAL (XEXP (elim, 1));
17869       elim = XEXP (elim, 0);
17870     }
17871
17872   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17873                && (elim == hard_frame_pointer_rtx
17874                    || elim == stack_pointer_rtx))
17875               || elim == (frame_pointer_needed
17876                           ? hard_frame_pointer_rtx
17877                           : stack_pointer_rtx));
17878
17879   frame_pointer_fb_offset = -offset;
17880 }
17881
17882 /* Generate a DW_AT_name attribute given some string value to be included as
17883    the value of the attribute.  */
17884
17885 static void
17886 add_name_attribute (dw_die_ref die, const char *name_string)
17887 {
17888   if (name_string != NULL && *name_string != 0)
17889     {
17890       if (demangle_name_func)
17891         name_string = (*demangle_name_func) (name_string);
17892
17893       add_AT_string (die, DW_AT_name, name_string);
17894     }
17895 }
17896
17897 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
17898    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
17899    of TYPE accordingly.
17900
17901    ??? This is a temporary measure until after we're able to generate
17902    regular DWARF for the complex Ada type system.  */
17903
17904 static void 
17905 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
17906                                      dw_die_ref context_die)
17907 {
17908   tree dtype;
17909   dw_die_ref dtype_die;
17910
17911   if (!lang_hooks.types.descriptive_type)
17912     return;
17913
17914   dtype = lang_hooks.types.descriptive_type (type);
17915   if (!dtype)
17916     return;
17917
17918   dtype_die = lookup_type_die (dtype);
17919   if (!dtype_die)
17920     {
17921       gen_type_die (dtype, context_die);
17922       dtype_die = lookup_type_die (dtype);
17923       gcc_assert (dtype_die);
17924     }
17925
17926   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
17927 }
17928
17929 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17930
17931 static void
17932 add_comp_dir_attribute (dw_die_ref die)
17933 {
17934   const char *wd = get_src_pwd ();
17935   char *wd1;
17936
17937   if (wd == NULL)
17938     return;
17939
17940   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17941     {
17942       int wdlen;
17943
17944       wdlen = strlen (wd);
17945       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17946       strcpy (wd1, wd);
17947       wd1 [wdlen] = DIR_SEPARATOR;
17948       wd1 [wdlen + 1] = 0;
17949       wd = wd1;
17950     }
17951
17952     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17953 }
17954
17955 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17956    default.  */
17957
17958 static int
17959 lower_bound_default (void)
17960 {
17961   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17962     {
17963     case DW_LANG_C:
17964     case DW_LANG_C89:
17965     case DW_LANG_C99:
17966     case DW_LANG_C_plus_plus:
17967     case DW_LANG_ObjC:
17968     case DW_LANG_ObjC_plus_plus:
17969     case DW_LANG_Java:
17970       return 0;
17971     case DW_LANG_Fortran77:
17972     case DW_LANG_Fortran90:
17973     case DW_LANG_Fortran95:
17974       return 1;
17975     case DW_LANG_UPC:
17976     case DW_LANG_D:
17977     case DW_LANG_Python:
17978       return dwarf_version >= 4 ? 0 : -1;
17979     case DW_LANG_Ada95:
17980     case DW_LANG_Ada83:
17981     case DW_LANG_Cobol74:
17982     case DW_LANG_Cobol85:
17983     case DW_LANG_Pascal83:
17984     case DW_LANG_Modula2:
17985     case DW_LANG_PLI:
17986       return dwarf_version >= 4 ? 1 : -1;
17987     default:
17988       return -1;
17989     }
17990 }
17991
17992 /* Given a tree node describing an array bound (either lower or upper) output
17993    a representation for that bound.  */
17994
17995 static void
17996 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17997 {
17998   switch (TREE_CODE (bound))
17999     {
18000     case ERROR_MARK:
18001       return;
18002
18003     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
18004     case INTEGER_CST:
18005       {
18006         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
18007         int dflt;
18008
18009         /* Use the default if possible.  */
18010         if (bound_attr == DW_AT_lower_bound
18011             && host_integerp (bound, 0)
18012             && (dflt = lower_bound_default ()) != -1
18013             && tree_low_cst (bound, 0) == dflt)
18014           ;
18015
18016         /* Otherwise represent the bound as an unsigned value with the
18017            precision of its type.  The precision and signedness of the
18018            type will be necessary to re-interpret it unambiguously.  */
18019         else if (prec < HOST_BITS_PER_WIDE_INT)
18020           {
18021             unsigned HOST_WIDE_INT mask
18022               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
18023             add_AT_unsigned (subrange_die, bound_attr,
18024                              TREE_INT_CST_LOW (bound) & mask);
18025           }
18026         else if (prec == HOST_BITS_PER_WIDE_INT
18027                  || TREE_INT_CST_HIGH (bound) == 0)
18028           add_AT_unsigned (subrange_die, bound_attr,
18029                            TREE_INT_CST_LOW (bound));
18030         else
18031           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
18032                          TREE_INT_CST_LOW (bound));
18033       }
18034       break;
18035
18036     CASE_CONVERT:
18037     case VIEW_CONVERT_EXPR:
18038       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
18039       break;
18040
18041     case SAVE_EXPR:
18042       break;
18043
18044     case VAR_DECL:
18045     case PARM_DECL:
18046     case RESULT_DECL:
18047       {
18048         dw_die_ref decl_die = lookup_decl_die (bound);
18049
18050         /* ??? Can this happen, or should the variable have been bound
18051            first?  Probably it can, since I imagine that we try to create
18052            the types of parameters in the order in which they exist in
18053            the list, and won't have created a forward reference to a
18054            later parameter.  */
18055         if (decl_die != NULL)
18056           {
18057             add_AT_die_ref (subrange_die, bound_attr, decl_die);
18058             break;
18059           }
18060       }
18061       /* FALLTHRU */
18062
18063     default:
18064       {
18065         /* Otherwise try to create a stack operation procedure to
18066            evaluate the value of the array bound.  */
18067
18068         dw_die_ref ctx, decl_die;
18069         dw_loc_list_ref list;
18070
18071         list = loc_list_from_tree (bound, 2);
18072         if (list == NULL || single_element_loc_list_p (list))
18073           {
18074             /* If DW_AT_*bound is not a reference nor constant, it is
18075                a DWARF expression rather than location description.
18076                For that loc_list_from_tree (bound, 0) is needed.
18077                If that fails to give a single element list,
18078                fall back to outputting this as a reference anyway.  */
18079             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
18080             if (list2 && single_element_loc_list_p (list2))
18081               {
18082                 add_AT_loc (subrange_die, bound_attr, list2->expr);
18083                 break;
18084               }
18085           }
18086         if (list == NULL)
18087           break;
18088
18089         if (current_function_decl == 0)
18090           ctx = comp_unit_die ();
18091         else
18092           ctx = lookup_decl_die (current_function_decl);
18093
18094         decl_die = new_die (DW_TAG_variable, ctx, bound);
18095         add_AT_flag (decl_die, DW_AT_artificial, 1);
18096         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
18097         add_AT_location_description (decl_die, DW_AT_location, list);
18098         add_AT_die_ref (subrange_die, bound_attr, decl_die);
18099         break;
18100       }
18101     }
18102 }
18103
18104 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18105    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18106    Note that the block of subscript information for an array type also
18107    includes information about the element type of the given array type.  */
18108
18109 static void
18110 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18111 {
18112   unsigned dimension_number;
18113   tree lower, upper;
18114   dw_die_ref subrange_die;
18115
18116   for (dimension_number = 0;
18117        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18118        type = TREE_TYPE (type), dimension_number++)
18119     {
18120       tree domain = TYPE_DOMAIN (type);
18121
18122       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18123         break;
18124
18125       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18126          and (in GNU C only) variable bounds.  Handle all three forms
18127          here.  */
18128       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18129       if (domain)
18130         {
18131           /* We have an array type with specified bounds.  */
18132           lower = TYPE_MIN_VALUE (domain);
18133           upper = TYPE_MAX_VALUE (domain);
18134
18135           /* Define the index type.  */
18136           if (TREE_TYPE (domain))
18137             {
18138               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18139                  TREE_TYPE field.  We can't emit debug info for this
18140                  because it is an unnamed integral type.  */
18141               if (TREE_CODE (domain) == INTEGER_TYPE
18142                   && TYPE_NAME (domain) == NULL_TREE
18143                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18144                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18145                 ;
18146               else
18147                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
18148                                     type_die);
18149             }
18150
18151           /* ??? If upper is NULL, the array has unspecified length,
18152              but it does have a lower bound.  This happens with Fortran
18153                dimension arr(N:*)
18154              Since the debugger is definitely going to need to know N
18155              to produce useful results, go ahead and output the lower
18156              bound solo, and hope the debugger can cope.  */
18157
18158           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
18159           if (upper)
18160             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
18161         }
18162
18163       /* Otherwise we have an array type with an unspecified length.  The
18164          DWARF-2 spec does not say how to handle this; let's just leave out the
18165          bounds.  */
18166     }
18167 }
18168
18169 static void
18170 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18171 {
18172   unsigned size;
18173
18174   switch (TREE_CODE (tree_node))
18175     {
18176     case ERROR_MARK:
18177       size = 0;
18178       break;
18179     case ENUMERAL_TYPE:
18180     case RECORD_TYPE:
18181     case UNION_TYPE:
18182     case QUAL_UNION_TYPE:
18183       size = int_size_in_bytes (tree_node);
18184       break;
18185     case FIELD_DECL:
18186       /* For a data member of a struct or union, the DW_AT_byte_size is
18187          generally given as the number of bytes normally allocated for an
18188          object of the *declared* type of the member itself.  This is true
18189          even for bit-fields.  */
18190       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
18191       break;
18192     default:
18193       gcc_unreachable ();
18194     }
18195
18196   /* Note that `size' might be -1 when we get to this point.  If it is, that
18197      indicates that the byte size of the entity in question is variable.  We
18198      have no good way of expressing this fact in Dwarf at the present time,
18199      so just let the -1 pass on through.  */
18200   add_AT_unsigned (die, DW_AT_byte_size, size);
18201 }
18202
18203 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18204    which specifies the distance in bits from the highest order bit of the
18205    "containing object" for the bit-field to the highest order bit of the
18206    bit-field itself.
18207
18208    For any given bit-field, the "containing object" is a hypothetical object
18209    (of some integral or enum type) within which the given bit-field lives.  The
18210    type of this hypothetical "containing object" is always the same as the
18211    declared type of the individual bit-field itself.  The determination of the
18212    exact location of the "containing object" for a bit-field is rather
18213    complicated.  It's handled by the `field_byte_offset' function (above).
18214
18215    Note that it is the size (in bytes) of the hypothetical "containing object"
18216    which will be given in the DW_AT_byte_size attribute for this bit-field.
18217    (See `byte_size_attribute' above).  */
18218
18219 static inline void
18220 add_bit_offset_attribute (dw_die_ref die, tree decl)
18221 {
18222   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
18223   tree type = DECL_BIT_FIELD_TYPE (decl);
18224   HOST_WIDE_INT bitpos_int;
18225   HOST_WIDE_INT highest_order_object_bit_offset;
18226   HOST_WIDE_INT highest_order_field_bit_offset;
18227   HOST_WIDE_INT bit_offset;
18228
18229   /* Must be a field and a bit field.  */
18230   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
18231
18232   /* We can't yet handle bit-fields whose offsets are variable, so if we
18233      encounter such things, just return without generating any attribute
18234      whatsoever.  Likewise for variable or too large size.  */
18235   if (! host_integerp (bit_position (decl), 0)
18236       || ! host_integerp (DECL_SIZE (decl), 1))
18237     return;
18238
18239   bitpos_int = int_bit_position (decl);
18240
18241   /* Note that the bit offset is always the distance (in bits) from the
18242      highest-order bit of the "containing object" to the highest-order bit of
18243      the bit-field itself.  Since the "high-order end" of any object or field
18244      is different on big-endian and little-endian machines, the computation
18245      below must take account of these differences.  */
18246   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18247   highest_order_field_bit_offset = bitpos_int;
18248
18249   if (! BYTES_BIG_ENDIAN)
18250     {
18251       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
18252       highest_order_object_bit_offset += simple_type_size_in_bits (type);
18253     }
18254
18255   bit_offset
18256     = (! BYTES_BIG_ENDIAN
18257        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18258        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18259
18260   if (bit_offset < 0)
18261     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18262   else
18263     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18264 }
18265
18266 /* For a FIELD_DECL node which represents a bit field, output an attribute
18267    which specifies the length in bits of the given field.  */
18268
18269 static inline void
18270 add_bit_size_attribute (dw_die_ref die, tree decl)
18271 {
18272   /* Must be a field and a bit field.  */
18273   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18274               && DECL_BIT_FIELD_TYPE (decl));
18275
18276   if (host_integerp (DECL_SIZE (decl), 1))
18277     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
18278 }
18279
18280 /* If the compiled language is ANSI C, then add a 'prototyped'
18281    attribute, if arg types are given for the parameters of a function.  */
18282
18283 static inline void
18284 add_prototyped_attribute (dw_die_ref die, tree func_type)
18285 {
18286   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
18287       && prototype_p (func_type))
18288     add_AT_flag (die, DW_AT_prototyped, 1);
18289 }
18290
18291 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18292    by looking in either the type declaration or object declaration
18293    equate table.  */
18294
18295 static inline dw_die_ref
18296 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18297 {
18298   dw_die_ref origin_die = NULL;
18299
18300   if (TREE_CODE (origin) != FUNCTION_DECL)
18301     {
18302       /* We may have gotten separated from the block for the inlined
18303          function, if we're in an exception handler or some such; make
18304          sure that the abstract function has been written out.
18305
18306          Doing this for nested functions is wrong, however; functions are
18307          distinct units, and our context might not even be inline.  */
18308       tree fn = origin;
18309
18310       if (TYPE_P (fn))
18311         fn = TYPE_STUB_DECL (fn);
18312
18313       fn = decl_function_context (fn);
18314       if (fn)
18315         dwarf2out_abstract_function (fn);
18316     }
18317
18318   if (DECL_P (origin))
18319     origin_die = lookup_decl_die (origin);
18320   else if (TYPE_P (origin))
18321     origin_die = lookup_type_die (origin);
18322
18323   /* XXX: Functions that are never lowered don't always have correct block
18324      trees (in the case of java, they simply have no block tree, in some other
18325      languages).  For these functions, there is nothing we can really do to
18326      output correct debug info for inlined functions in all cases.  Rather
18327      than die, we'll just produce deficient debug info now, in that we will
18328      have variables without a proper abstract origin.  In the future, when all
18329      functions are lowered, we should re-add a gcc_assert (origin_die)
18330      here.  */
18331
18332   if (origin_die)
18333     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18334   return origin_die;
18335 }
18336
18337 /* We do not currently support the pure_virtual attribute.  */
18338
18339 static inline void
18340 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18341 {
18342   if (DECL_VINDEX (func_decl))
18343     {
18344       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18345
18346       if (host_integerp (DECL_VINDEX (func_decl), 0))
18347         add_AT_loc (die, DW_AT_vtable_elem_location,
18348                     new_loc_descr (DW_OP_constu,
18349                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
18350                                    0));
18351
18352       /* GNU extension: Record what type this method came from originally.  */
18353       if (debug_info_level > DINFO_LEVEL_TERSE
18354           && DECL_CONTEXT (func_decl))
18355         add_AT_die_ref (die, DW_AT_containing_type,
18356                         lookup_type_die (DECL_CONTEXT (func_decl)));
18357     }
18358 }
18359 \f
18360 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18361    given decl.  This used to be a vendor extension until after DWARF 4
18362    standardized it.  */
18363
18364 static void
18365 add_linkage_attr (dw_die_ref die, tree decl)
18366 {
18367   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18368
18369   /* Mimic what assemble_name_raw does with a leading '*'.  */
18370   if (name[0] == '*')
18371     name = &name[1];
18372
18373   if (dwarf_version >= 4)
18374     add_AT_string (die, DW_AT_linkage_name, name);
18375   else
18376     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18377 }
18378
18379 /* Add source coordinate attributes for the given decl.  */
18380
18381 static void
18382 add_src_coords_attributes (dw_die_ref die, tree decl)
18383 {
18384   expanded_location s;
18385
18386   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
18387     return;
18388   s = expand_location (DECL_SOURCE_LOCATION (decl));
18389   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18390   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18391 }
18392
18393 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18394
18395 static void
18396 add_linkage_name (dw_die_ref die, tree decl)
18397 {
18398   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18399        && TREE_PUBLIC (decl)
18400        && !DECL_ABSTRACT (decl)
18401        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18402        && die->die_tag != DW_TAG_member)
18403     {
18404       /* Defer until we have an assembler name set.  */
18405       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18406         {
18407           limbo_die_node *asm_name;
18408
18409           asm_name = ggc_alloc_cleared_limbo_die_node ();
18410           asm_name->die = die;
18411           asm_name->created_for = decl;
18412           asm_name->next = deferred_asm_name;
18413           deferred_asm_name = asm_name;
18414         }
18415       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18416         add_linkage_attr (die, decl);
18417     }
18418 }
18419
18420 /* Add a DW_AT_name attribute and source coordinate attribute for the
18421    given decl, but only if it actually has a name.  */
18422
18423 static void
18424 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18425 {
18426   tree decl_name;
18427
18428   decl_name = DECL_NAME (decl);
18429   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18430     {
18431       const char *name = dwarf2_name (decl, 0);
18432       if (name)
18433         add_name_attribute (die, name);
18434       if (! DECL_ARTIFICIAL (decl))
18435         add_src_coords_attributes (die, decl);
18436
18437       add_linkage_name (die, decl);
18438     }
18439
18440 #ifdef VMS_DEBUGGING_INFO
18441   /* Get the function's name, as described by its RTL.  This may be different
18442      from the DECL_NAME name used in the source file.  */
18443   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18444     {
18445       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18446                    XEXP (DECL_RTL (decl), 0));
18447       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
18448     }
18449 #endif /* VMS_DEBUGGING_INFO */
18450 }
18451
18452 #ifdef VMS_DEBUGGING_INFO
18453 /* Output the debug main pointer die for VMS */
18454
18455 void
18456 dwarf2out_vms_debug_main_pointer (void)
18457 {
18458   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18459   dw_die_ref die;
18460
18461   /* Allocate the VMS debug main subprogram die.  */
18462   die = ggc_alloc_cleared_die_node ();
18463   die->die_tag = DW_TAG_subprogram;
18464   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18465   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18466                                current_function_funcdef_no);
18467   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18468
18469   /* Make it the first child of comp_unit_die ().  */
18470   die->die_parent = comp_unit_die ();
18471   if (comp_unit_die ()->die_child)
18472     {
18473       die->die_sib = comp_unit_die ()->die_child->die_sib;
18474       comp_unit_die ()->die_child->die_sib = die;
18475     }
18476   else
18477     {
18478       die->die_sib = die;
18479       comp_unit_die ()->die_child = die;
18480     }
18481 }
18482 #endif /* VMS_DEBUGGING_INFO */
18483
18484 /* Push a new declaration scope.  */
18485
18486 static void
18487 push_decl_scope (tree scope)
18488 {
18489   VEC_safe_push (tree, gc, decl_scope_table, scope);
18490 }
18491
18492 /* Pop a declaration scope.  */
18493
18494 static inline void
18495 pop_decl_scope (void)
18496 {
18497   VEC_pop (tree, decl_scope_table);
18498 }
18499
18500 /* Return the DIE for the scope that immediately contains this type.
18501    Non-named types get global scope.  Named types nested in other
18502    types get their containing scope if it's open, or global scope
18503    otherwise.  All other types (i.e. function-local named types) get
18504    the current active scope.  */
18505
18506 static dw_die_ref
18507 scope_die_for (tree t, dw_die_ref context_die)
18508 {
18509   dw_die_ref scope_die = NULL;
18510   tree containing_scope;
18511   int i;
18512
18513   /* Non-types always go in the current scope.  */
18514   gcc_assert (TYPE_P (t));
18515
18516   containing_scope = TYPE_CONTEXT (t);
18517
18518   /* Use the containing namespace if it was passed in (for a declaration).  */
18519   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18520     {
18521       if (context_die == lookup_decl_die (containing_scope))
18522         /* OK */;
18523       else
18524         containing_scope = NULL_TREE;
18525     }
18526
18527   /* Ignore function type "scopes" from the C frontend.  They mean that
18528      a tagged type is local to a parmlist of a function declarator, but
18529      that isn't useful to DWARF.  */
18530   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18531     containing_scope = NULL_TREE;
18532
18533   if (SCOPE_FILE_SCOPE_P (containing_scope))
18534     scope_die = comp_unit_die ();
18535   else if (TYPE_P (containing_scope))
18536     {
18537       /* For types, we can just look up the appropriate DIE.  But
18538          first we check to see if we're in the middle of emitting it
18539          so we know where the new DIE should go.  */
18540       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18541         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18542           break;
18543
18544       if (i < 0)
18545         {
18546           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18547                       || TREE_ASM_WRITTEN (containing_scope));
18548           /*We are not in the middle of emitting the type
18549             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18550           scope_die = lookup_type_die (containing_scope);
18551
18552           /* If none of the current dies are suitable, we get file scope.  */
18553           if (scope_die == NULL)
18554             scope_die = comp_unit_die ();
18555         }
18556       else
18557         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18558     }
18559   else
18560     scope_die = context_die;
18561
18562   return scope_die;
18563 }
18564
18565 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18566
18567 static inline int
18568 local_scope_p (dw_die_ref context_die)
18569 {
18570   for (; context_die; context_die = context_die->die_parent)
18571     if (context_die->die_tag == DW_TAG_inlined_subroutine
18572         || context_die->die_tag == DW_TAG_subprogram)
18573       return 1;
18574
18575   return 0;
18576 }
18577
18578 /* Returns nonzero if CONTEXT_DIE is a class.  */
18579
18580 static inline int
18581 class_scope_p (dw_die_ref context_die)
18582 {
18583   return (context_die
18584           && (context_die->die_tag == DW_TAG_structure_type
18585               || context_die->die_tag == DW_TAG_class_type
18586               || context_die->die_tag == DW_TAG_interface_type
18587               || context_die->die_tag == DW_TAG_union_type));
18588 }
18589
18590 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18591    whether or not to treat a DIE in this context as a declaration.  */
18592
18593 static inline int
18594 class_or_namespace_scope_p (dw_die_ref context_die)
18595 {
18596   return (class_scope_p (context_die)
18597           || (context_die && context_die->die_tag == DW_TAG_namespace));
18598 }
18599
18600 /* Many forms of DIEs require a "type description" attribute.  This
18601    routine locates the proper "type descriptor" die for the type given
18602    by 'type', and adds a DW_AT_type attribute below the given die.  */
18603
18604 static void
18605 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18606                     int decl_volatile, dw_die_ref context_die)
18607 {
18608   enum tree_code code  = TREE_CODE (type);
18609   dw_die_ref type_die  = NULL;
18610
18611   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18612      or fixed-point type, use the inner type.  This is because we have no
18613      support for unnamed types in base_type_die.  This can happen if this is
18614      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18615   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18616       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18617     type = TREE_TYPE (type), code = TREE_CODE (type);
18618
18619   if (code == ERROR_MARK
18620       /* Handle a special case.  For functions whose return type is void, we
18621          generate *no* type attribute.  (Note that no object may have type
18622          `void', so this only applies to function return types).  */
18623       || code == VOID_TYPE)
18624     return;
18625
18626   type_die = modified_type_die (type,
18627                                 decl_const || TYPE_READONLY (type),
18628                                 decl_volatile || TYPE_VOLATILE (type),
18629                                 context_die);
18630
18631   if (type_die != NULL)
18632     add_AT_die_ref (object_die, DW_AT_type, type_die);
18633 }
18634
18635 /* Given an object die, add the calling convention attribute for the
18636    function call type.  */
18637 static void
18638 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18639 {
18640   enum dwarf_calling_convention value = DW_CC_normal;
18641
18642   value = ((enum dwarf_calling_convention)
18643            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18644
18645   if (is_fortran ()
18646       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18647     {
18648       /* DWARF 2 doesn't provide a way to identify a program's source-level
18649         entry point.  DW_AT_calling_convention attributes are only meant
18650         to describe functions' calling conventions.  However, lacking a
18651         better way to signal the Fortran main program, we used this for 
18652         a long time, following existing custom.  Now, DWARF 4 has 
18653         DW_AT_main_subprogram, which we add below, but some tools still
18654         rely on the old way, which we thus keep.  */
18655       value = DW_CC_program;
18656
18657       if (dwarf_version >= 4 || !dwarf_strict)
18658         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18659     }
18660
18661   /* Only add the attribute if the backend requests it, and
18662      is not DW_CC_normal.  */
18663   if (value && (value != DW_CC_normal))
18664     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18665 }
18666
18667 /* Given a tree pointer to a struct, class, union, or enum type node, return
18668    a pointer to the (string) tag name for the given type, or zero if the type
18669    was declared without a tag.  */
18670
18671 static const char *
18672 type_tag (const_tree type)
18673 {
18674   const char *name = 0;
18675
18676   if (TYPE_NAME (type) != 0)
18677     {
18678       tree t = 0;
18679
18680       /* Find the IDENTIFIER_NODE for the type name.  */
18681       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18682           && !TYPE_NAMELESS (type))
18683         t = TYPE_NAME (type);
18684
18685       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18686          a TYPE_DECL node, regardless of whether or not a `typedef' was
18687          involved.  */
18688       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18689                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18690         {
18691           /* We want to be extra verbose.  Don't call dwarf_name if
18692              DECL_NAME isn't set.  The default hook for decl_printable_name
18693              doesn't like that, and in this context it's correct to return
18694              0, instead of "<anonymous>" or the like.  */
18695           if (DECL_NAME (TYPE_NAME (type))
18696               && !DECL_NAMELESS (TYPE_NAME (type)))
18697             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18698         }
18699
18700       /* Now get the name as a string, or invent one.  */
18701       if (!name && t != 0)
18702         name = IDENTIFIER_POINTER (t);
18703     }
18704
18705   return (name == 0 || *name == '\0') ? 0 : name;
18706 }
18707
18708 /* Return the type associated with a data member, make a special check
18709    for bit field types.  */
18710
18711 static inline tree
18712 member_declared_type (const_tree member)
18713 {
18714   return (DECL_BIT_FIELD_TYPE (member)
18715           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18716 }
18717
18718 /* Get the decl's label, as described by its RTL. This may be different
18719    from the DECL_NAME name used in the source file.  */
18720
18721 #if 0
18722 static const char *
18723 decl_start_label (tree decl)
18724 {
18725   rtx x;
18726   const char *fnname;
18727
18728   x = DECL_RTL (decl);
18729   gcc_assert (MEM_P (x));
18730
18731   x = XEXP (x, 0);
18732   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18733
18734   fnname = XSTR (x, 0);
18735   return fnname;
18736 }
18737 #endif
18738 \f
18739 /* These routines generate the internal representation of the DIE's for
18740    the compilation unit.  Debugging information is collected by walking
18741    the declaration trees passed in from dwarf2out_decl().  */
18742
18743 static void
18744 gen_array_type_die (tree type, dw_die_ref context_die)
18745 {
18746   dw_die_ref scope_die = scope_die_for (type, context_die);
18747   dw_die_ref array_die;
18748
18749   /* GNU compilers represent multidimensional array types as sequences of one
18750      dimensional array types whose element types are themselves array types.
18751      We sometimes squish that down to a single array_type DIE with multiple
18752      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18753      say that we are allowed to do this kind of compression in C, because
18754      there is no difference between an array of arrays and a multidimensional
18755      array.  We don't do this for Ada to remain as close as possible to the
18756      actual representation, which is especially important against the language
18757      flexibilty wrt arrays of variable size.  */
18758
18759   bool collapse_nested_arrays = !is_ada ();
18760   tree element_type;
18761
18762   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18763      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18764   if (TYPE_STRING_FLAG (type)
18765       && TREE_CODE (type) == ARRAY_TYPE
18766       && is_fortran ()
18767       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18768     {
18769       HOST_WIDE_INT size;
18770
18771       array_die = new_die (DW_TAG_string_type, scope_die, type);
18772       add_name_attribute (array_die, type_tag (type));
18773       equate_type_number_to_die (type, array_die);
18774       size = int_size_in_bytes (type);
18775       if (size >= 0)
18776         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18777       else if (TYPE_DOMAIN (type) != NULL_TREE
18778                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18779                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18780         {
18781           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18782           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18783
18784           size = int_size_in_bytes (TREE_TYPE (szdecl));
18785           if (loc && size > 0)
18786             {
18787               add_AT_location_description (array_die, DW_AT_string_length, loc);
18788               if (size != DWARF2_ADDR_SIZE)
18789                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18790             }
18791         }
18792       return;
18793     }
18794
18795   /* ??? The SGI dwarf reader fails for array of array of enum types
18796      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18797      array type comes before the outer array type.  We thus call gen_type_die
18798      before we new_die and must prevent nested array types collapsing for this
18799      target.  */
18800
18801 #ifdef MIPS_DEBUGGING_INFO
18802   gen_type_die (TREE_TYPE (type), context_die);
18803   collapse_nested_arrays = false;
18804 #endif
18805
18806   array_die = new_die (DW_TAG_array_type, scope_die, type);
18807   add_name_attribute (array_die, type_tag (type));
18808   add_gnat_descriptive_type_attribute (array_die, type, context_die);
18809   equate_type_number_to_die (type, array_die);
18810
18811   if (TREE_CODE (type) == VECTOR_TYPE)
18812     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18813
18814   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18815   if (is_fortran ()
18816       && TREE_CODE (type) == ARRAY_TYPE
18817       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18818       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18819     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18820
18821 #if 0
18822   /* We default the array ordering.  SDB will probably do
18823      the right things even if DW_AT_ordering is not present.  It's not even
18824      an issue until we start to get into multidimensional arrays anyway.  If
18825      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18826      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18827      and when we find out that we need to put these in, we will only do so
18828      for multidimensional arrays.  */
18829   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18830 #endif
18831
18832 #ifdef MIPS_DEBUGGING_INFO
18833   /* The SGI compilers handle arrays of unknown bound by setting
18834      AT_declaration and not emitting any subrange DIEs.  */
18835   if (TREE_CODE (type) == ARRAY_TYPE
18836       && ! TYPE_DOMAIN (type))
18837     add_AT_flag (array_die, DW_AT_declaration, 1);
18838   else
18839 #endif
18840   if (TREE_CODE (type) == VECTOR_TYPE)
18841     {
18842       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18843       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18844       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18845       add_bound_info (subrange_die, DW_AT_upper_bound,
18846                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18847     }
18848   else
18849     add_subscript_info (array_die, type, collapse_nested_arrays);
18850
18851   /* Add representation of the type of the elements of this array type and
18852      emit the corresponding DIE if we haven't done it already.  */
18853   element_type = TREE_TYPE (type);
18854   if (collapse_nested_arrays)
18855     while (TREE_CODE (element_type) == ARRAY_TYPE)
18856       {
18857         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18858           break;
18859         element_type = TREE_TYPE (element_type);
18860       }
18861
18862 #ifndef MIPS_DEBUGGING_INFO
18863   gen_type_die (element_type, context_die);
18864 #endif
18865
18866   add_type_attribute (array_die, element_type, 0, 0, context_die);
18867
18868   if (get_AT (array_die, DW_AT_name))
18869     add_pubtype (type, array_die);
18870 }
18871
18872 static dw_loc_descr_ref
18873 descr_info_loc (tree val, tree base_decl)
18874 {
18875   HOST_WIDE_INT size;
18876   dw_loc_descr_ref loc, loc2;
18877   enum dwarf_location_atom op;
18878
18879   if (val == base_decl)
18880     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18881
18882   switch (TREE_CODE (val))
18883     {
18884     CASE_CONVERT:
18885       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18886     case VAR_DECL:
18887       return loc_descriptor_from_tree (val, 0);
18888     case INTEGER_CST:
18889       if (host_integerp (val, 0))
18890         return int_loc_descriptor (tree_low_cst (val, 0));
18891       break;
18892     case INDIRECT_REF:
18893       size = int_size_in_bytes (TREE_TYPE (val));
18894       if (size < 0)
18895         break;
18896       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18897       if (!loc)
18898         break;
18899       if (size == DWARF2_ADDR_SIZE)
18900         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18901       else
18902         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18903       return loc;
18904     case POINTER_PLUS_EXPR:
18905     case PLUS_EXPR:
18906       if (host_integerp (TREE_OPERAND (val, 1), 1)
18907           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18908              < 16384)
18909         {
18910           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18911           if (!loc)
18912             break;
18913           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18914         }
18915       else
18916         {
18917           op = DW_OP_plus;
18918         do_binop:
18919           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18920           if (!loc)
18921             break;
18922           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18923           if (!loc2)
18924             break;
18925           add_loc_descr (&loc, loc2);
18926           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18927         }
18928       return loc;
18929     case MINUS_EXPR:
18930       op = DW_OP_minus;
18931       goto do_binop;
18932     case MULT_EXPR:
18933       op = DW_OP_mul;
18934       goto do_binop;
18935     case EQ_EXPR:
18936       op = DW_OP_eq;
18937       goto do_binop;
18938     case NE_EXPR:
18939       op = DW_OP_ne;
18940       goto do_binop;
18941     default:
18942       break;
18943     }
18944   return NULL;
18945 }
18946
18947 static void
18948 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18949                       tree val, tree base_decl)
18950 {
18951   dw_loc_descr_ref loc;
18952
18953   if (host_integerp (val, 0))
18954     {
18955       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18956       return;
18957     }
18958
18959   loc = descr_info_loc (val, base_decl);
18960   if (!loc)
18961     return;
18962
18963   add_AT_loc (die, attr, loc);
18964 }
18965
18966 /* This routine generates DIE for array with hidden descriptor, details
18967    are filled into *info by a langhook.  */
18968
18969 static void
18970 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18971                           dw_die_ref context_die)
18972 {
18973   dw_die_ref scope_die = scope_die_for (type, context_die);
18974   dw_die_ref array_die;
18975   int dim;
18976
18977   array_die = new_die (DW_TAG_array_type, scope_die, type);
18978   add_name_attribute (array_die, type_tag (type));
18979   equate_type_number_to_die (type, array_die);
18980
18981   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18982   if (is_fortran ()
18983       && info->ndimensions >= 2)
18984     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18985
18986   if (info->data_location)
18987     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18988                           info->base_decl);
18989   if (info->associated)
18990     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18991                           info->base_decl);
18992   if (info->allocated)
18993     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18994                           info->base_decl);
18995
18996   for (dim = 0; dim < info->ndimensions; dim++)
18997     {
18998       dw_die_ref subrange_die
18999         = new_die (DW_TAG_subrange_type, array_die, NULL);
19000
19001       if (info->dimen[dim].lower_bound)
19002         {
19003           /* If it is the default value, omit it.  */
19004           int dflt;
19005
19006           if (host_integerp (info->dimen[dim].lower_bound, 0)
19007               && (dflt = lower_bound_default ()) != -1
19008               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
19009             ;
19010           else
19011             add_descr_info_field (subrange_die, DW_AT_lower_bound,
19012                                   info->dimen[dim].lower_bound,
19013                                   info->base_decl);
19014         }
19015       if (info->dimen[dim].upper_bound)
19016         add_descr_info_field (subrange_die, DW_AT_upper_bound,
19017                               info->dimen[dim].upper_bound,
19018                               info->base_decl);
19019       if (info->dimen[dim].stride)
19020         add_descr_info_field (subrange_die, DW_AT_byte_stride,
19021                               info->dimen[dim].stride,
19022                               info->base_decl);
19023     }
19024
19025   gen_type_die (info->element_type, context_die);
19026   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
19027
19028   if (get_AT (array_die, DW_AT_name))
19029     add_pubtype (type, array_die);
19030 }
19031
19032 #if 0
19033 static void
19034 gen_entry_point_die (tree decl, dw_die_ref context_die)
19035 {
19036   tree origin = decl_ultimate_origin (decl);
19037   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19038
19039   if (origin != NULL)
19040     add_abstract_origin_attribute (decl_die, origin);
19041   else
19042     {
19043       add_name_and_src_coords_attributes (decl_die, decl);
19044       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19045                           0, 0, context_die);
19046     }
19047
19048   if (DECL_ABSTRACT (decl))
19049     equate_decl_number_to_die (decl, decl_die);
19050   else
19051     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19052 }
19053 #endif
19054
19055 /* Walk through the list of incomplete types again, trying once more to
19056    emit full debugging info for them.  */
19057
19058 static void
19059 retry_incomplete_types (void)
19060 {
19061   int i;
19062
19063   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
19064     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
19065                                   DINFO_USAGE_DIR_USE))
19066       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
19067 }
19068
19069 /* Determine what tag to use for a record type.  */
19070
19071 static enum dwarf_tag
19072 record_type_tag (tree type)
19073 {
19074   if (! lang_hooks.types.classify_record)
19075     return DW_TAG_structure_type;
19076
19077   switch (lang_hooks.types.classify_record (type))
19078     {
19079     case RECORD_IS_STRUCT:
19080       return DW_TAG_structure_type;
19081
19082     case RECORD_IS_CLASS:
19083       return DW_TAG_class_type;
19084
19085     case RECORD_IS_INTERFACE:
19086       if (dwarf_version >= 3 || !dwarf_strict)
19087         return DW_TAG_interface_type;
19088       return DW_TAG_structure_type;
19089
19090     default:
19091       gcc_unreachable ();
19092     }
19093 }
19094
19095 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19096    include all of the information about the enumeration values also. Each
19097    enumerated type name/value is listed as a child of the enumerated type
19098    DIE.  */
19099
19100 static dw_die_ref
19101 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19102 {
19103   dw_die_ref type_die = lookup_type_die (type);
19104
19105   if (type_die == NULL)
19106     {
19107       type_die = new_die (DW_TAG_enumeration_type,
19108                           scope_die_for (type, context_die), type);
19109       equate_type_number_to_die (type, type_die);
19110       add_name_attribute (type_die, type_tag (type));
19111       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19112       if (dwarf_version >= 4 || !dwarf_strict)
19113         {
19114           if (ENUM_IS_SCOPED (type))
19115             add_AT_flag (type_die, DW_AT_enum_class, 1);
19116           if (ENUM_IS_OPAQUE (type))
19117             add_AT_flag (type_die, DW_AT_declaration, 1);
19118         }
19119     }
19120   else if (! TYPE_SIZE (type))
19121     return type_die;
19122   else
19123     remove_AT (type_die, DW_AT_declaration);
19124
19125   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19126      given enum type is incomplete, do not generate the DW_AT_byte_size
19127      attribute or the DW_AT_element_list attribute.  */
19128   if (TYPE_SIZE (type))
19129     {
19130       tree link;
19131
19132       TREE_ASM_WRITTEN (type) = 1;
19133       add_byte_size_attribute (type_die, type);
19134       if (TYPE_STUB_DECL (type) != NULL_TREE)
19135         {
19136           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19137           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19138         }
19139
19140       /* If the first reference to this type was as the return type of an
19141          inline function, then it may not have a parent.  Fix this now.  */
19142       if (type_die->die_parent == NULL)
19143         add_child_die (scope_die_for (type, context_die), type_die);
19144
19145       for (link = TYPE_VALUES (type);
19146            link != NULL; link = TREE_CHAIN (link))
19147         {
19148           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19149           tree value = TREE_VALUE (link);
19150
19151           add_name_attribute (enum_die,
19152                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19153
19154           if (TREE_CODE (value) == CONST_DECL)
19155             value = DECL_INITIAL (value);
19156
19157           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
19158             /* DWARF2 does not provide a way of indicating whether or
19159                not enumeration constants are signed or unsigned.  GDB
19160                always assumes the values are signed, so we output all
19161                values as if they were signed.  That means that
19162                enumeration constants with very large unsigned values
19163                will appear to have negative values in the debugger.  */
19164             add_AT_int (enum_die, DW_AT_const_value,
19165                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
19166         }
19167     }
19168   else
19169     add_AT_flag (type_die, DW_AT_declaration, 1);
19170
19171   if (get_AT (type_die, DW_AT_name))
19172     add_pubtype (type, type_die);
19173
19174   return type_die;
19175 }
19176
19177 /* Generate a DIE to represent either a real live formal parameter decl or to
19178    represent just the type of some formal parameter position in some function
19179    type.
19180
19181    Note that this routine is a bit unusual because its argument may be a
19182    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19183    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19184    node.  If it's the former then this function is being called to output a
19185    DIE to represent a formal parameter object (or some inlining thereof).  If
19186    it's the latter, then this function is only being called to output a
19187    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19188    argument type of some subprogram type.
19189    If EMIT_NAME_P is true, name and source coordinate attributes
19190    are emitted.  */
19191
19192 static dw_die_ref
19193 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19194                           dw_die_ref context_die)
19195 {
19196   tree node_or_origin = node ? node : origin;
19197   tree ultimate_origin;
19198   dw_die_ref parm_die
19199     = new_die (DW_TAG_formal_parameter, context_die, node);
19200
19201   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19202     {
19203     case tcc_declaration:
19204       ultimate_origin = decl_ultimate_origin (node_or_origin);
19205       if (node || ultimate_origin)
19206         origin = ultimate_origin;
19207       if (origin != NULL)
19208         add_abstract_origin_attribute (parm_die, origin);
19209       else if (emit_name_p)
19210         add_name_and_src_coords_attributes (parm_die, node);
19211       if (origin == NULL
19212           || (! DECL_ABSTRACT (node_or_origin)
19213               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19214                                            decl_function_context
19215                                                             (node_or_origin))))
19216         {
19217           tree type = TREE_TYPE (node_or_origin);
19218           if (decl_by_reference_p (node_or_origin))
19219             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
19220                                 context_die);
19221           else
19222             add_type_attribute (parm_die, type,
19223                                 TREE_READONLY (node_or_origin),
19224                                 TREE_THIS_VOLATILE (node_or_origin),
19225                                 context_die);
19226         }
19227       if (origin == NULL && DECL_ARTIFICIAL (node))
19228         add_AT_flag (parm_die, DW_AT_artificial, 1);
19229
19230       if (node && node != origin)
19231         equate_decl_number_to_die (node, parm_die);
19232       if (! DECL_ABSTRACT (node_or_origin))
19233         add_location_or_const_value_attribute (parm_die, node_or_origin,
19234                                                node == NULL, DW_AT_location);
19235
19236       break;
19237
19238     case tcc_type:
19239       /* We were called with some kind of a ..._TYPE node.  */
19240       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
19241       break;
19242
19243     default:
19244       gcc_unreachable ();
19245     }
19246
19247   return parm_die;
19248 }
19249
19250 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19251    children DW_TAG_formal_parameter DIEs representing the arguments of the
19252    parameter pack.
19253
19254    PARM_PACK must be a function parameter pack.
19255    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19256    must point to the subsequent arguments of the function PACK_ARG belongs to.
19257    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19258    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19259    following the last one for which a DIE was generated.  */
19260
19261 static dw_die_ref
19262 gen_formal_parameter_pack_die  (tree parm_pack,
19263                                 tree pack_arg,
19264                                 dw_die_ref subr_die,
19265                                 tree *next_arg)
19266 {
19267   tree arg;
19268   dw_die_ref parm_pack_die;
19269
19270   gcc_assert (parm_pack
19271               && lang_hooks.function_parameter_pack_p (parm_pack)
19272               && subr_die);
19273
19274   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19275   add_src_coords_attributes (parm_pack_die, parm_pack);
19276
19277   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19278     {
19279       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19280                                                                  parm_pack))
19281         break;
19282       gen_formal_parameter_die (arg, NULL,
19283                                 false /* Don't emit name attribute.  */,
19284                                 parm_pack_die);
19285     }
19286   if (next_arg)
19287     *next_arg = arg;
19288   return parm_pack_die;
19289 }
19290
19291 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19292    at the end of an (ANSI prototyped) formal parameters list.  */
19293
19294 static void
19295 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19296 {
19297   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19298 }
19299
19300 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19301    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19302    parameters as specified in some function type specification (except for
19303    those which appear as part of a function *definition*).  */
19304
19305 static void
19306 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19307 {
19308   tree link;
19309   tree formal_type = NULL;
19310   tree first_parm_type;
19311   tree arg;
19312
19313   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19314     {
19315       arg = DECL_ARGUMENTS (function_or_method_type);
19316       function_or_method_type = TREE_TYPE (function_or_method_type);
19317     }
19318   else
19319     arg = NULL_TREE;
19320
19321   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19322
19323   /* Make our first pass over the list of formal parameter types and output a
19324      DW_TAG_formal_parameter DIE for each one.  */
19325   for (link = first_parm_type; link; )
19326     {
19327       dw_die_ref parm_die;
19328
19329       formal_type = TREE_VALUE (link);
19330       if (formal_type == void_type_node)
19331         break;
19332
19333       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19334       parm_die = gen_formal_parameter_die (formal_type, NULL,
19335                                            true /* Emit name attribute.  */,
19336                                            context_die);
19337       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19338           && link == first_parm_type)
19339         {
19340           add_AT_flag (parm_die, DW_AT_artificial, 1);
19341           if (dwarf_version >= 3 || !dwarf_strict)
19342             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19343         }
19344       else if (arg && DECL_ARTIFICIAL (arg))
19345         add_AT_flag (parm_die, DW_AT_artificial, 1);
19346
19347       link = TREE_CHAIN (link);
19348       if (arg)
19349         arg = DECL_CHAIN (arg);
19350     }
19351
19352   /* If this function type has an ellipsis, add a
19353      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19354   if (formal_type != void_type_node)
19355     gen_unspecified_parameters_die (function_or_method_type, context_die);
19356
19357   /* Make our second (and final) pass over the list of formal parameter types
19358      and output DIEs to represent those types (as necessary).  */
19359   for (link = TYPE_ARG_TYPES (function_or_method_type);
19360        link && TREE_VALUE (link);
19361        link = TREE_CHAIN (link))
19362     gen_type_die (TREE_VALUE (link), context_die);
19363 }
19364
19365 /* We want to generate the DIE for TYPE so that we can generate the
19366    die for MEMBER, which has been defined; we will need to refer back
19367    to the member declaration nested within TYPE.  If we're trying to
19368    generate minimal debug info for TYPE, processing TYPE won't do the
19369    trick; we need to attach the member declaration by hand.  */
19370
19371 static void
19372 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19373 {
19374   gen_type_die (type, context_die);
19375
19376   /* If we're trying to avoid duplicate debug info, we may not have
19377      emitted the member decl for this function.  Emit it now.  */
19378   if (TYPE_STUB_DECL (type)
19379       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19380       && ! lookup_decl_die (member))
19381     {
19382       dw_die_ref type_die;
19383       gcc_assert (!decl_ultimate_origin (member));
19384
19385       push_decl_scope (type);
19386       type_die = lookup_type_die_strip_naming_typedef (type);
19387       if (TREE_CODE (member) == FUNCTION_DECL)
19388         gen_subprogram_die (member, type_die);
19389       else if (TREE_CODE (member) == FIELD_DECL)
19390         {
19391           /* Ignore the nameless fields that are used to skip bits but handle
19392              C++ anonymous unions and structs.  */
19393           if (DECL_NAME (member) != NULL_TREE
19394               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19395               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19396             {
19397               gen_type_die (member_declared_type (member), type_die);
19398               gen_field_die (member, type_die);
19399             }
19400         }
19401       else
19402         gen_variable_die (member, NULL_TREE, type_die);
19403
19404       pop_decl_scope ();
19405     }
19406 }
19407
19408 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19409    may later generate inlined and/or out-of-line instances of.  */
19410
19411 static void
19412 dwarf2out_abstract_function (tree decl)
19413 {
19414   dw_die_ref old_die;
19415   tree save_fn;
19416   tree context;
19417   int was_abstract;
19418   htab_t old_decl_loc_table;
19419   htab_t old_cached_dw_loc_list_table;
19420   int old_call_site_count, old_tail_call_site_count;
19421   struct call_arg_loc_node *old_call_arg_locations;
19422
19423   /* Make sure we have the actual abstract inline, not a clone.  */
19424   decl = DECL_ORIGIN (decl);
19425
19426   old_die = lookup_decl_die (decl);
19427   if (old_die && get_AT (old_die, DW_AT_inline))
19428     /* We've already generated the abstract instance.  */
19429     return;
19430
19431   /* We can be called while recursively when seeing block defining inlined subroutine
19432      DIE.  Be sure to not clobber the outer location table nor use it or we would
19433      get locations in abstract instantces.  */
19434   old_decl_loc_table = decl_loc_table;
19435   decl_loc_table = NULL;
19436   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
19437   cached_dw_loc_list_table = NULL;
19438   old_call_arg_locations = call_arg_locations;
19439   call_arg_locations = NULL;
19440   old_call_site_count = call_site_count;
19441   call_site_count = -1;
19442   old_tail_call_site_count = tail_call_site_count;
19443   tail_call_site_count = -1;
19444
19445   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
19446      we don't get confused by DECL_ABSTRACT.  */
19447   if (debug_info_level > DINFO_LEVEL_TERSE)
19448     {
19449       context = decl_class_context (decl);
19450       if (context)
19451         gen_type_die_for_member
19452           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
19453     }
19454
19455   /* Pretend we've just finished compiling this function.  */
19456   save_fn = current_function_decl;
19457   current_function_decl = decl;
19458   push_cfun (DECL_STRUCT_FUNCTION (decl));
19459
19460   was_abstract = DECL_ABSTRACT (decl);
19461   set_decl_abstract_flags (decl, 1);
19462   dwarf2out_decl (decl);
19463   if (! was_abstract)
19464     set_decl_abstract_flags (decl, 0);
19465
19466   current_function_decl = save_fn;
19467   decl_loc_table = old_decl_loc_table;
19468   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
19469   call_arg_locations = old_call_arg_locations;
19470   call_site_count = old_call_site_count;
19471   tail_call_site_count = old_tail_call_site_count;
19472   pop_cfun ();
19473 }
19474
19475 /* Helper function of premark_used_types() which gets called through
19476    htab_traverse.
19477
19478    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19479    marked as unused by prune_unused_types.  */
19480
19481 static int
19482 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
19483 {
19484   tree type;
19485   dw_die_ref die;
19486
19487   type = (tree) *slot;
19488   die = lookup_type_die (type);
19489   if (die != NULL)
19490     die->die_perennial_p = 1;
19491   return 1;
19492 }
19493
19494 /* Helper function of premark_types_used_by_global_vars which gets called
19495    through htab_traverse.
19496
19497    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19498    marked as unused by prune_unused_types. The DIE of the type is marked
19499    only if the global variable using the type will actually be emitted.  */
19500
19501 static int
19502 premark_types_used_by_global_vars_helper (void **slot,
19503                                           void *data ATTRIBUTE_UNUSED)
19504 {
19505   struct types_used_by_vars_entry *entry;
19506   dw_die_ref die;
19507
19508   entry = (struct types_used_by_vars_entry *) *slot;
19509   gcc_assert (entry->type != NULL
19510               && entry->var_decl != NULL);
19511   die = lookup_type_die (entry->type);
19512   if (die)
19513     {
19514       /* Ask cgraph if the global variable really is to be emitted.
19515          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19516       struct varpool_node *node = varpool_get_node (entry->var_decl);
19517       if (node && node->needed)
19518         {
19519           die->die_perennial_p = 1;
19520           /* Keep the parent DIEs as well.  */
19521           while ((die = die->die_parent) && die->die_perennial_p == 0)
19522             die->die_perennial_p = 1;
19523         }
19524     }
19525   return 1;
19526 }
19527
19528 /* Mark all members of used_types_hash as perennial.  */
19529
19530 static void
19531 premark_used_types (void)
19532 {
19533   if (cfun && cfun->used_types_hash)
19534     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19535 }
19536
19537 /* Mark all members of types_used_by_vars_entry as perennial.  */
19538
19539 static void
19540 premark_types_used_by_global_vars (void)
19541 {
19542   if (types_used_by_vars_hash)
19543     htab_traverse (types_used_by_vars_hash,
19544                    premark_types_used_by_global_vars_helper, NULL);
19545 }
19546
19547 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19548    for CA_LOC call arg loc node.  */
19549
19550 static dw_die_ref
19551 gen_call_site_die (tree decl, dw_die_ref subr_die,
19552                    struct call_arg_loc_node *ca_loc)
19553 {
19554   dw_die_ref stmt_die = NULL, die;
19555   tree block = ca_loc->block;
19556
19557   while (block
19558          && block != DECL_INITIAL (decl)
19559          && TREE_CODE (block) == BLOCK)
19560     {
19561       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19562         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19563       if (stmt_die)
19564         break;
19565       block = BLOCK_SUPERCONTEXT (block);
19566     }
19567   if (stmt_die == NULL)
19568     stmt_die = subr_die;
19569   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19570   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19571   if (ca_loc->tail_call_p)
19572     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19573   if (ca_loc->symbol_ref)
19574     {
19575       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19576       if (tdie)
19577         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19578       else
19579         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19580     }
19581   return die;
19582 }
19583
19584 /* Generate a DIE to represent a declared function (either file-scope or
19585    block-local).  */
19586
19587 static void
19588 gen_subprogram_die (tree decl, dw_die_ref context_die)
19589 {
19590   tree origin = decl_ultimate_origin (decl);
19591   dw_die_ref subr_die;
19592   tree outer_scope;
19593   dw_die_ref old_die = lookup_decl_die (decl);
19594   int declaration = (current_function_decl != decl
19595                      || class_or_namespace_scope_p (context_die));
19596
19597   premark_used_types ();
19598
19599   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19600      started to generate the abstract instance of an inline, decided to output
19601      its containing class, and proceeded to emit the declaration of the inline
19602      from the member list for the class.  If so, DECLARATION takes priority;
19603      we'll get back to the abstract instance when done with the class.  */
19604
19605   /* The class-scope declaration DIE must be the primary DIE.  */
19606   if (origin && declaration && class_or_namespace_scope_p (context_die))
19607     {
19608       origin = NULL;
19609       gcc_assert (!old_die);
19610     }
19611
19612   /* Now that the C++ front end lazily declares artificial member fns, we
19613      might need to retrofit the declaration into its class.  */
19614   if (!declaration && !origin && !old_die
19615       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19616       && !class_or_namespace_scope_p (context_die)
19617       && debug_info_level > DINFO_LEVEL_TERSE)
19618     old_die = force_decl_die (decl);
19619
19620   if (origin != NULL)
19621     {
19622       gcc_assert (!declaration || local_scope_p (context_die));
19623
19624       /* Fixup die_parent for the abstract instance of a nested
19625          inline function.  */
19626       if (old_die && old_die->die_parent == NULL)
19627         add_child_die (context_die, old_die);
19628
19629       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19630       add_abstract_origin_attribute (subr_die, origin);
19631     }
19632   else if (old_die)
19633     {
19634       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19635       struct dwarf_file_data * file_index = lookup_filename (s.file);
19636
19637       if (!get_AT_flag (old_die, DW_AT_declaration)
19638           /* We can have a normal definition following an inline one in the
19639              case of redefinition of GNU C extern inlines.
19640              It seems reasonable to use AT_specification in this case.  */
19641           && !get_AT (old_die, DW_AT_inline))
19642         {
19643           /* Detect and ignore this case, where we are trying to output
19644              something we have already output.  */
19645           return;
19646         }
19647
19648       /* If the definition comes from the same place as the declaration,
19649          maybe use the old DIE.  We always want the DIE for this function
19650          that has the *_pc attributes to be under comp_unit_die so the
19651          debugger can find it.  We also need to do this for abstract
19652          instances of inlines, since the spec requires the out-of-line copy
19653          to have the same parent.  For local class methods, this doesn't
19654          apply; we just use the old DIE.  */
19655       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19656           && (DECL_ARTIFICIAL (decl)
19657               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19658                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19659                       == (unsigned) s.line))))
19660         {
19661           subr_die = old_die;
19662
19663           /* Clear out the declaration attribute and the formal parameters.
19664              Do not remove all children, because it is possible that this
19665              declaration die was forced using force_decl_die(). In such
19666              cases die that forced declaration die (e.g. TAG_imported_module)
19667              is one of the children that we do not want to remove.  */
19668           remove_AT (subr_die, DW_AT_declaration);
19669           remove_AT (subr_die, DW_AT_object_pointer);
19670           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19671         }
19672       else
19673         {
19674           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19675           add_AT_specification (subr_die, old_die);
19676           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19677             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19678           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19679             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19680         }
19681     }
19682   else
19683     {
19684       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19685
19686       if (TREE_PUBLIC (decl))
19687         add_AT_flag (subr_die, DW_AT_external, 1);
19688
19689       add_name_and_src_coords_attributes (subr_die, decl);
19690       if (debug_info_level > DINFO_LEVEL_TERSE)
19691         {
19692           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19693           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19694                               0, 0, context_die);
19695         }
19696
19697       add_pure_or_virtual_attribute (subr_die, decl);
19698       if (DECL_ARTIFICIAL (decl))
19699         add_AT_flag (subr_die, DW_AT_artificial, 1);
19700
19701       add_accessibility_attribute (subr_die, decl);
19702     }
19703
19704   if (declaration)
19705     {
19706       if (!old_die || !get_AT (old_die, DW_AT_inline))
19707         {
19708           add_AT_flag (subr_die, DW_AT_declaration, 1);
19709
19710           /* If this is an explicit function declaration then generate
19711              a DW_AT_explicit attribute.  */
19712           if (lang_hooks.decls.function_decl_explicit_p (decl)
19713               && (dwarf_version >= 3 || !dwarf_strict))
19714             add_AT_flag (subr_die, DW_AT_explicit, 1);
19715
19716           /* The first time we see a member function, it is in the context of
19717              the class to which it belongs.  We make sure of this by emitting
19718              the class first.  The next time is the definition, which is
19719              handled above.  The two may come from the same source text.
19720
19721              Note that force_decl_die() forces function declaration die. It is
19722              later reused to represent definition.  */
19723           equate_decl_number_to_die (decl, subr_die);
19724         }
19725     }
19726   else if (DECL_ABSTRACT (decl))
19727     {
19728       if (DECL_DECLARED_INLINE_P (decl))
19729         {
19730           if (cgraph_function_possibly_inlined_p (decl))
19731             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19732           else
19733             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19734         }
19735       else
19736         {
19737           if (cgraph_function_possibly_inlined_p (decl))
19738             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19739           else
19740             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19741         }
19742
19743       if (DECL_DECLARED_INLINE_P (decl)
19744           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19745         add_AT_flag (subr_die, DW_AT_artificial, 1);
19746
19747       equate_decl_number_to_die (decl, subr_die);
19748     }
19749   else if (!DECL_EXTERNAL (decl))
19750     {
19751       HOST_WIDE_INT cfa_fb_offset;
19752
19753       if (!old_die || !get_AT (old_die, DW_AT_inline))
19754         equate_decl_number_to_die (decl, subr_die);
19755
19756       if (!flag_reorder_blocks_and_partition)
19757         {
19758           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19759           if (fde->dw_fde_begin)
19760             {
19761               /* We have already generated the labels.  */
19762               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19763               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19764             }
19765           else
19766             {
19767               /* Create start/end labels and add the range.  */
19768               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19769               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19770                                            current_function_funcdef_no);
19771               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19772               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19773                                            current_function_funcdef_no);
19774               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19775             }
19776
19777 #if VMS_DEBUGGING_INFO
19778       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19779          Section 2.3 Prologue and Epilogue Attributes:
19780          When a breakpoint is set on entry to a function, it is generally
19781          desirable for execution to be suspended, not on the very first
19782          instruction of the function, but rather at a point after the
19783          function's frame has been set up, after any language defined local
19784          declaration processing has been completed, and before execution of
19785          the first statement of the function begins. Debuggers generally
19786          cannot properly determine where this point is.  Similarly for a
19787          breakpoint set on exit from a function. The prologue and epilogue
19788          attributes allow a compiler to communicate the location(s) to use.  */
19789
19790       {
19791         if (fde->dw_fde_vms_end_prologue)
19792           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19793             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19794
19795         if (fde->dw_fde_vms_begin_epilogue)
19796           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19797             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19798       }
19799 #endif
19800
19801           add_pubname (decl, subr_die);
19802         }
19803       else
19804         {  /* Generate pubnames entries for the split function code
19805               ranges.  */
19806           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19807
19808           if (fde->dw_fde_second_begin)
19809             {
19810               if (dwarf_version >= 3 || !dwarf_strict)
19811                 {
19812                   /* We should use ranges for non-contiguous code section 
19813                      addresses.  Use the actual code range for the initial
19814                      section, since the HOT/COLD labels might precede an 
19815                      alignment offset.  */
19816                   bool range_list_added = false;
19817                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19818                                         fde->dw_fde_end, &range_list_added);
19819                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19820                                         fde->dw_fde_second_end,
19821                                         &range_list_added);
19822                   add_pubname (decl, subr_die);
19823                   if (range_list_added)
19824                     add_ranges (NULL);
19825                 }
19826               else
19827                 {
19828                   /* There is no real support in DW2 for this .. so we make
19829                      a work-around.  First, emit the pub name for the segment
19830                      containing the function label.  Then make and emit a
19831                      simplified subprogram DIE for the second segment with the
19832                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19833                      linkage name for the second die so that gdb will find both
19834                      sections when given "b foo".  */
19835                   const char *name = NULL;
19836                   tree decl_name = DECL_NAME (decl);
19837                   dw_die_ref seg_die;
19838
19839                   /* Do the 'primary' section.   */
19840                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19841                                  fde->dw_fde_begin);
19842                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19843                                  fde->dw_fde_end);
19844                   /* Add it.   */
19845                   add_pubname (decl, subr_die);
19846
19847                   /* Build a minimal DIE for the secondary section.  */
19848                   seg_die = new_die (DW_TAG_subprogram,
19849                                      subr_die->die_parent, decl);
19850
19851                   if (TREE_PUBLIC (decl))
19852                     add_AT_flag (seg_die, DW_AT_external, 1);
19853
19854                   if (decl_name != NULL 
19855                       && IDENTIFIER_POINTER (decl_name) != NULL)
19856                     {
19857                       name = dwarf2_name (decl, 1);
19858                       if (! DECL_ARTIFICIAL (decl))
19859                         add_src_coords_attributes (seg_die, decl);
19860
19861                       add_linkage_name (seg_die, decl);
19862                     }
19863                   gcc_assert (name != NULL);
19864                   add_pure_or_virtual_attribute (seg_die, decl);
19865                   if (DECL_ARTIFICIAL (decl))
19866                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19867
19868                   name = concat ("__second_sect_of_", name, NULL); 
19869                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19870                                  fde->dw_fde_second_begin);
19871                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19872                                  fde->dw_fde_second_end);
19873                   add_name_attribute (seg_die, name);
19874                   add_pubname_string (name, seg_die);
19875                 }
19876             }
19877           else
19878             {
19879               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19880               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19881               add_pubname (decl, subr_die);
19882             }
19883         }
19884
19885 #ifdef MIPS_DEBUGGING_INFO
19886       /* Add a reference to the FDE for this routine.  */
19887       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19888 #endif
19889
19890       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19891
19892       /* We define the "frame base" as the function's CFA.  This is more
19893          convenient for several reasons: (1) It's stable across the prologue
19894          and epilogue, which makes it better than just a frame pointer,
19895          (2) With dwarf3, there exists a one-byte encoding that allows us
19896          to reference the .debug_frame data by proxy, but failing that,
19897          (3) We can at least reuse the code inspection and interpretation
19898          code that determines the CFA position at various points in the
19899          function.  */
19900       if (dwarf_version >= 3)
19901         {
19902           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19903           add_AT_loc (subr_die, DW_AT_frame_base, op);
19904         }
19905       else
19906         {
19907           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19908           if (list->dw_loc_next)
19909             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19910           else
19911             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19912         }
19913
19914       /* Compute a displacement from the "steady-state frame pointer" to
19915          the CFA.  The former is what all stack slots and argument slots
19916          will reference in the rtl; the later is what we've told the
19917          debugger about.  We'll need to adjust all frame_base references
19918          by this displacement.  */
19919       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19920
19921       if (cfun->static_chain_decl)
19922         add_AT_location_description (subr_die, DW_AT_static_link,
19923                  loc_list_from_tree (cfun->static_chain_decl, 2));
19924     }
19925
19926   /* Generate child dies for template paramaters.  */
19927   if (debug_info_level > DINFO_LEVEL_TERSE)
19928     gen_generic_params_dies (decl);
19929
19930   /* Now output descriptions of the arguments for this function. This gets
19931      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19932      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19933      `...' at the end of the formal parameter list.  In order to find out if
19934      there was a trailing ellipsis or not, we must instead look at the type
19935      associated with the FUNCTION_DECL.  This will be a node of type
19936      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19937      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19938      an ellipsis at the end.  */
19939
19940   /* In the case where we are describing a mere function declaration, all we
19941      need to do here (and all we *can* do here) is to describe the *types* of
19942      its formal parameters.  */
19943   if (debug_info_level <= DINFO_LEVEL_TERSE)
19944     ;
19945   else if (declaration)
19946     gen_formal_types_die (decl, subr_die);
19947   else
19948     {
19949       /* Generate DIEs to represent all known formal parameters.  */
19950       tree parm = DECL_ARGUMENTS (decl);
19951       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19952       tree generic_decl_parm = generic_decl
19953                                 ? DECL_ARGUMENTS (generic_decl)
19954                                 : NULL;
19955
19956       /* Now we want to walk the list of parameters of the function and
19957          emit their relevant DIEs.
19958
19959          We consider the case of DECL being an instance of a generic function
19960          as well as it being a normal function.
19961
19962          If DECL is an instance of a generic function we walk the
19963          parameters of the generic function declaration _and_ the parameters of
19964          DECL itself. This is useful because we want to emit specific DIEs for
19965          function parameter packs and those are declared as part of the
19966          generic function declaration. In that particular case,
19967          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19968          That DIE has children DIEs representing the set of arguments
19969          of the pack. Note that the set of pack arguments can be empty.
19970          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19971          children DIE.
19972
19973          Otherwise, we just consider the parameters of DECL.  */
19974       while (generic_decl_parm || parm)
19975         {
19976           if (generic_decl_parm
19977               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19978             gen_formal_parameter_pack_die (generic_decl_parm,
19979                                            parm, subr_die,
19980                                            &parm);
19981           else if (parm)
19982             {
19983               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19984
19985               if (parm == DECL_ARGUMENTS (decl)
19986                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19987                   && parm_die
19988                   && (dwarf_version >= 3 || !dwarf_strict))
19989                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19990
19991               parm = DECL_CHAIN (parm);
19992             }
19993
19994           if (generic_decl_parm)
19995             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19996         }
19997
19998       /* Decide whether we need an unspecified_parameters DIE at the end.
19999          There are 2 more cases to do this for: 1) the ansi ... declaration -
20000          this is detectable when the end of the arg list is not a
20001          void_type_node 2) an unprototyped function declaration (not a
20002          definition).  This just means that we have no info about the
20003          parameters at all.  */
20004       if (prototype_p (TREE_TYPE (decl)))
20005         {
20006           /* This is the prototyped case, check for....  */
20007           if (stdarg_p (TREE_TYPE (decl)))
20008             gen_unspecified_parameters_die (decl, subr_die);
20009         }
20010       else if (DECL_INITIAL (decl) == NULL_TREE)
20011         gen_unspecified_parameters_die (decl, subr_die);
20012     }
20013
20014   /* Output Dwarf info for all of the stuff within the body of the function
20015      (if it has one - it may be just a declaration).  */
20016   outer_scope = DECL_INITIAL (decl);
20017
20018   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20019      a function.  This BLOCK actually represents the outermost binding contour
20020      for the function, i.e. the contour in which the function's formal
20021      parameters and labels get declared. Curiously, it appears that the front
20022      end doesn't actually put the PARM_DECL nodes for the current function onto
20023      the BLOCK_VARS list for this outer scope, but are strung off of the
20024      DECL_ARGUMENTS list for the function instead.
20025
20026      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20027      the LABEL_DECL nodes for the function however, and we output DWARF info
20028      for those in decls_for_scope.  Just within the `outer_scope' there will be
20029      a BLOCK node representing the function's outermost pair of curly braces,
20030      and any blocks used for the base and member initializers of a C++
20031      constructor function.  */
20032   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
20033     {
20034       int call_site_note_count = 0;
20035       int tail_call_site_note_count = 0;
20036
20037       /* Emit a DW_TAG_variable DIE for a named return value.  */
20038       if (DECL_NAME (DECL_RESULT (decl)))
20039         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
20040
20041       current_function_has_inlines = 0;
20042       decls_for_scope (outer_scope, subr_die, 0);
20043
20044       if (call_arg_locations && !dwarf_strict)
20045         {
20046           struct call_arg_loc_node *ca_loc;
20047           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20048             {
20049               dw_die_ref die = NULL;
20050               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20051               rtx arg, next_arg;
20052
20053               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
20054                    arg; arg = next_arg)
20055                 {
20056                   dw_loc_descr_ref reg, val;
20057                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20058                   dw_die_ref cdie;
20059
20060                   next_arg = XEXP (arg, 1);
20061                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20062                       && next_arg
20063                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20064                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20065                       && REGNO (XEXP (XEXP (arg, 0), 0))
20066                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20067                     next_arg = XEXP (next_arg, 1);
20068                   if (mode == VOIDmode)
20069                     {
20070                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20071                       if (mode == VOIDmode)
20072                         mode = GET_MODE (XEXP (arg, 0));
20073                     }
20074                   if (mode == VOIDmode || mode == BLKmode)
20075                     continue;
20076                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20077                     {
20078                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20079                       tloc = XEXP (XEXP (arg, 0), 1);
20080                       continue;
20081                     }
20082                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20083                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20084                     {
20085                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20086                       tlocc = XEXP (XEXP (arg, 0), 1);
20087                       continue;
20088                     }
20089                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20090                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20091                                               VAR_INIT_STATUS_INITIALIZED);
20092                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20093                     {
20094                       rtx mem = XEXP (XEXP (arg, 0), 0);
20095                       reg = mem_loc_descriptor (XEXP (mem, 0),
20096                                                 get_address_mode (mem),
20097                                                 GET_MODE (mem),
20098                                                 VAR_INIT_STATUS_INITIALIZED);
20099                     }
20100                   else
20101                     continue;
20102                   if (reg == NULL)
20103                     continue;
20104                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20105                                             VOIDmode,
20106                                             VAR_INIT_STATUS_INITIALIZED);
20107                   if (val == NULL)
20108                     continue;
20109                   if (die == NULL)
20110                     die = gen_call_site_die (decl, subr_die, ca_loc);
20111                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20112                                   NULL_TREE);           
20113                   add_AT_loc (cdie, DW_AT_location, reg);
20114                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20115                   if (next_arg != XEXP (arg, 1))
20116                     {
20117                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20118                       if (mode == VOIDmode)
20119                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20120                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20121                                                             0), 1),
20122                                                 mode, VOIDmode,
20123                                                 VAR_INIT_STATUS_INITIALIZED);
20124                       if (val != NULL)
20125                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20126                     }
20127                 }
20128               if (die == NULL
20129                   && (ca_loc->symbol_ref || tloc))
20130                 die = gen_call_site_die (decl, subr_die, ca_loc);
20131               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20132                 {
20133                   dw_loc_descr_ref tval = NULL;
20134
20135                   if (tloc != NULL_RTX)
20136                     tval = mem_loc_descriptor (tloc,
20137                                                GET_MODE (tloc) == VOIDmode
20138                                                ? Pmode : GET_MODE (tloc),
20139                                                VOIDmode,
20140                                                VAR_INIT_STATUS_INITIALIZED);
20141                   if (tval)
20142                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20143                   else if (tlocc != NULL_RTX)
20144                     {
20145                       tval = mem_loc_descriptor (tlocc,
20146                                                  GET_MODE (tlocc) == VOIDmode
20147                                                  ? Pmode : GET_MODE (tlocc),
20148                                                  VOIDmode,
20149                                                  VAR_INIT_STATUS_INITIALIZED);
20150                       if (tval)
20151                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20152                                     tval);
20153                     }
20154                 }
20155               if (die != NULL)
20156                 {
20157                   call_site_note_count++;
20158                   if (ca_loc->tail_call_p)
20159                     tail_call_site_note_count++;
20160                 }
20161             }
20162         }
20163       call_arg_locations = NULL;
20164       call_arg_loc_last = NULL;
20165       if (tail_call_site_count >= 0
20166           && tail_call_site_count == tail_call_site_note_count
20167           && !dwarf_strict)
20168         {
20169           if (call_site_count >= 0
20170               && call_site_count == call_site_note_count)
20171             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20172           else
20173             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20174         }
20175       call_site_count = -1;
20176       tail_call_site_count = -1;
20177     }
20178   /* Add the calling convention attribute if requested.  */
20179   add_calling_convention_attribute (subr_die, decl);
20180
20181 }
20182
20183 /* Returns a hash value for X (which really is a die_struct).  */
20184
20185 static hashval_t
20186 common_block_die_table_hash (const void *x)
20187 {
20188   const_dw_die_ref d = (const_dw_die_ref) x;
20189   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20190 }
20191
20192 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20193    as decl_id and die_parent of die_struct Y.  */
20194
20195 static int
20196 common_block_die_table_eq (const void *x, const void *y)
20197 {
20198   const_dw_die_ref d = (const_dw_die_ref) x;
20199   const_dw_die_ref e = (const_dw_die_ref) y;
20200   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
20201 }
20202
20203 /* Generate a DIE to represent a declared data object.
20204    Either DECL or ORIGIN must be non-null.  */
20205
20206 static void
20207 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20208 {
20209   HOST_WIDE_INT off;
20210   tree com_decl;
20211   tree decl_or_origin = decl ? decl : origin;
20212   tree ultimate_origin;
20213   dw_die_ref var_die;
20214   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20215   dw_die_ref origin_die;
20216   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20217                       || class_or_namespace_scope_p (context_die));
20218   bool specialization_p = false;
20219
20220   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20221   if (decl || ultimate_origin)
20222     origin = ultimate_origin;
20223   com_decl = fortran_common (decl_or_origin, &off);
20224
20225   /* Symbol in common gets emitted as a child of the common block, in the form
20226      of a data member.  */
20227   if (com_decl)
20228     {
20229       dw_die_ref com_die;
20230       dw_loc_list_ref loc;
20231       die_node com_die_arg;
20232
20233       var_die = lookup_decl_die (decl_or_origin);
20234       if (var_die)
20235         {
20236           if (get_AT (var_die, DW_AT_location) == NULL)
20237             {
20238               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
20239               if (loc)
20240                 {
20241                   if (off)
20242                     {
20243                       /* Optimize the common case.  */
20244                       if (single_element_loc_list_p (loc)
20245                           && loc->expr->dw_loc_opc == DW_OP_addr
20246                           && loc->expr->dw_loc_next == NULL
20247                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20248                              == SYMBOL_REF)
20249                         loc->expr->dw_loc_oprnd1.v.val_addr
20250                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20251                         else
20252                           loc_list_plus_const (loc, off);
20253                     }
20254                   add_AT_location_description (var_die, DW_AT_location, loc);
20255                   remove_AT (var_die, DW_AT_declaration);
20256                 }
20257             }
20258           return;
20259         }
20260
20261       if (common_block_die_table == NULL)
20262         common_block_die_table
20263           = htab_create_ggc (10, common_block_die_table_hash,
20264                              common_block_die_table_eq, NULL);
20265
20266       com_die_arg.decl_id = DECL_UID (com_decl);
20267       com_die_arg.die_parent = context_die;
20268       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
20269       loc = loc_list_from_tree (com_decl, 2);
20270       if (com_die == NULL)
20271         {
20272           const char *cnam
20273             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20274           void **slot;
20275
20276           com_die = new_die (DW_TAG_common_block, context_die, decl);
20277           add_name_and_src_coords_attributes (com_die, com_decl);
20278           if (loc)
20279             {
20280               add_AT_location_description (com_die, DW_AT_location, loc);
20281               /* Avoid sharing the same loc descriptor between
20282                  DW_TAG_common_block and DW_TAG_variable.  */
20283               loc = loc_list_from_tree (com_decl, 2);
20284             }
20285           else if (DECL_EXTERNAL (decl))
20286             add_AT_flag (com_die, DW_AT_declaration, 1);
20287           add_pubname_string (cnam, com_die); /* ??? needed? */
20288           com_die->decl_id = DECL_UID (com_decl);
20289           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
20290           *slot = (void *) com_die;
20291         }
20292       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20293         {
20294           add_AT_location_description (com_die, DW_AT_location, loc);
20295           loc = loc_list_from_tree (com_decl, 2);
20296           remove_AT (com_die, DW_AT_declaration);
20297         }
20298       var_die = new_die (DW_TAG_variable, com_die, decl);
20299       add_name_and_src_coords_attributes (var_die, decl);
20300       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
20301                           TREE_THIS_VOLATILE (decl), context_die);
20302       add_AT_flag (var_die, DW_AT_external, 1);
20303       if (loc)
20304         {
20305           if (off)
20306             {
20307               /* Optimize the common case.  */
20308               if (single_element_loc_list_p (loc)
20309                   && loc->expr->dw_loc_opc == DW_OP_addr
20310                   && loc->expr->dw_loc_next == NULL
20311                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20312                 loc->expr->dw_loc_oprnd1.v.val_addr
20313                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20314               else
20315                 loc_list_plus_const (loc, off);
20316             }
20317           add_AT_location_description (var_die, DW_AT_location, loc);
20318         }
20319       else if (DECL_EXTERNAL (decl))
20320         add_AT_flag (var_die, DW_AT_declaration, 1);
20321       equate_decl_number_to_die (decl, var_die);
20322       return;
20323     }
20324
20325   /* If the compiler emitted a definition for the DECL declaration
20326      and if we already emitted a DIE for it, don't emit a second
20327      DIE for it again. Allow re-declarations of DECLs that are
20328      inside functions, though.  */
20329   if (old_die && declaration && !local_scope_p (context_die))
20330     return;
20331
20332   /* For static data members, the declaration in the class is supposed
20333      to have DW_TAG_member tag; the specification should still be
20334      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20335   if (declaration && class_scope_p (context_die))
20336     var_die = new_die (DW_TAG_member, context_die, decl);
20337   else
20338     var_die = new_die (DW_TAG_variable, context_die, decl);
20339
20340   origin_die = NULL;
20341   if (origin != NULL)
20342     origin_die = add_abstract_origin_attribute (var_die, origin);
20343
20344   /* Loop unrolling can create multiple blocks that refer to the same
20345      static variable, so we must test for the DW_AT_declaration flag.
20346
20347      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
20348      copy decls and set the DECL_ABSTRACT flag on them instead of
20349      sharing them.
20350
20351      ??? Duplicated blocks have been rewritten to use .debug_ranges.
20352
20353      ??? The declare_in_namespace support causes us to get two DIEs for one
20354      variable, both of which are declarations.  We want to avoid considering
20355      one to be a specification, so we must test that this DIE is not a
20356      declaration.  */
20357   else if (old_die && TREE_STATIC (decl) && ! declaration
20358            && get_AT_flag (old_die, DW_AT_declaration) == 1)
20359     {
20360       /* This is a definition of a C++ class level static.  */
20361       add_AT_specification (var_die, old_die);
20362       specialization_p = true;
20363       if (DECL_NAME (decl))
20364         {
20365           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20366           struct dwarf_file_data * file_index = lookup_filename (s.file);
20367
20368           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20369             add_AT_file (var_die, DW_AT_decl_file, file_index);
20370
20371           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20372             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
20373
20374           if (old_die->die_tag == DW_TAG_member)
20375             add_linkage_name (var_die, decl);
20376         }
20377     }
20378   else
20379     add_name_and_src_coords_attributes (var_die, decl);
20380
20381   if ((origin == NULL && !specialization_p)
20382       || (origin != NULL
20383           && !DECL_ABSTRACT (decl_or_origin)
20384           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
20385                                        decl_function_context
20386                                                         (decl_or_origin))))
20387     {
20388       tree type = TREE_TYPE (decl_or_origin);
20389
20390       if (decl_by_reference_p (decl_or_origin))
20391         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
20392       else
20393         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
20394                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
20395     }
20396
20397   if (origin == NULL && !specialization_p)
20398     {
20399       if (TREE_PUBLIC (decl))
20400         add_AT_flag (var_die, DW_AT_external, 1);
20401
20402       if (DECL_ARTIFICIAL (decl))
20403         add_AT_flag (var_die, DW_AT_artificial, 1);
20404
20405       add_accessibility_attribute (var_die, decl);
20406     }
20407
20408   if (declaration)
20409     add_AT_flag (var_die, DW_AT_declaration, 1);
20410
20411   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
20412     equate_decl_number_to_die (decl, var_die);
20413
20414   if (! declaration
20415       && (! DECL_ABSTRACT (decl_or_origin)
20416           /* Local static vars are shared between all clones/inlines,
20417              so emit DW_AT_location on the abstract DIE if DECL_RTL is
20418              already set.  */
20419           || (TREE_CODE (decl_or_origin) == VAR_DECL
20420               && TREE_STATIC (decl_or_origin)
20421               && DECL_RTL_SET_P (decl_or_origin)))
20422       /* When abstract origin already has DW_AT_location attribute, no need
20423          to add it again.  */
20424       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
20425     {
20426       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
20427           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
20428         defer_location (decl_or_origin, var_die);
20429       else
20430         add_location_or_const_value_attribute (var_die, decl_or_origin,
20431                                                decl == NULL, DW_AT_location);
20432       add_pubname (decl_or_origin, var_die);
20433     }
20434   else
20435     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
20436 }
20437
20438 /* Generate a DIE to represent a named constant.  */
20439
20440 static void
20441 gen_const_die (tree decl, dw_die_ref context_die)
20442 {
20443   dw_die_ref const_die;
20444   tree type = TREE_TYPE (decl);
20445
20446   const_die = new_die (DW_TAG_constant, context_die, decl);
20447   add_name_and_src_coords_attributes (const_die, decl);
20448   add_type_attribute (const_die, type, 1, 0, context_die);
20449   if (TREE_PUBLIC (decl))
20450     add_AT_flag (const_die, DW_AT_external, 1);
20451   if (DECL_ARTIFICIAL (decl))
20452     add_AT_flag (const_die, DW_AT_artificial, 1);
20453   tree_add_const_value_attribute_for_decl (const_die, decl);
20454 }
20455
20456 /* Generate a DIE to represent a label identifier.  */
20457
20458 static void
20459 gen_label_die (tree decl, dw_die_ref context_die)
20460 {
20461   tree origin = decl_ultimate_origin (decl);
20462   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
20463   rtx insn;
20464   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20465
20466   if (origin != NULL)
20467     add_abstract_origin_attribute (lbl_die, origin);
20468   else
20469     add_name_and_src_coords_attributes (lbl_die, decl);
20470
20471   if (DECL_ABSTRACT (decl))
20472     equate_decl_number_to_die (decl, lbl_die);
20473   else
20474     {
20475       insn = DECL_RTL_IF_SET (decl);
20476
20477       /* Deleted labels are programmer specified labels which have been
20478          eliminated because of various optimizations.  We still emit them
20479          here so that it is possible to put breakpoints on them.  */
20480       if (insn
20481           && (LABEL_P (insn)
20482               || ((NOTE_P (insn)
20483                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
20484         {
20485           /* When optimization is enabled (via -O) some parts of the compiler
20486              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
20487              represent source-level labels which were explicitly declared by
20488              the user.  This really shouldn't be happening though, so catch
20489              it if it ever does happen.  */
20490           gcc_assert (!INSN_DELETED_P (insn));
20491
20492           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
20493           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
20494         }
20495     }
20496 }
20497
20498 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
20499    attributes to the DIE for a block STMT, to describe where the inlined
20500    function was called from.  This is similar to add_src_coords_attributes.  */
20501
20502 static inline void
20503 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
20504 {
20505   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
20506
20507   if (dwarf_version >= 3 || !dwarf_strict)
20508     {
20509       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
20510       add_AT_unsigned (die, DW_AT_call_line, s.line);
20511     }
20512 }
20513
20514
20515 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
20516    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
20517
20518 static inline void
20519 add_high_low_attributes (tree stmt, dw_die_ref die)
20520 {
20521   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20522
20523   if (BLOCK_FRAGMENT_CHAIN (stmt)
20524       && (dwarf_version >= 3 || !dwarf_strict))
20525     {
20526       tree chain;
20527
20528       if (inlined_function_outer_scope_p (stmt))
20529         {
20530           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20531                                        BLOCK_NUMBER (stmt));
20532           add_AT_lbl_id (die, DW_AT_entry_pc, label);
20533         }
20534
20535       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
20536
20537       chain = BLOCK_FRAGMENT_CHAIN (stmt);
20538       do
20539         {
20540           add_ranges (chain);
20541           chain = BLOCK_FRAGMENT_CHAIN (chain);
20542         }
20543       while (chain);
20544       add_ranges (NULL);
20545     }
20546   else
20547     {
20548       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20549                                    BLOCK_NUMBER (stmt));
20550       add_AT_lbl_id (die, DW_AT_low_pc, label);
20551       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
20552                                    BLOCK_NUMBER (stmt));
20553       add_AT_lbl_id (die, DW_AT_high_pc, label);
20554     }
20555 }
20556
20557 /* Generate a DIE for a lexical block.  */
20558
20559 static void
20560 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
20561 {
20562   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
20563
20564   if (call_arg_locations)
20565     {
20566       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20567         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20568                                BLOCK_NUMBER (stmt) + 1);
20569       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20570     }
20571
20572   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20573     add_high_low_attributes (stmt, stmt_die);
20574
20575   decls_for_scope (stmt, stmt_die, depth);
20576 }
20577
20578 /* Generate a DIE for an inlined subprogram.  */
20579
20580 static void
20581 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20582 {
20583   tree decl;
20584
20585   /* The instance of function that is effectively being inlined shall not
20586      be abstract.  */
20587   gcc_assert (! BLOCK_ABSTRACT (stmt));
20588
20589   decl = block_ultimate_origin (stmt);
20590
20591   /* Emit info for the abstract instance first, if we haven't yet.  We
20592      must emit this even if the block is abstract, otherwise when we
20593      emit the block below (or elsewhere), we may end up trying to emit
20594      a die whose origin die hasn't been emitted, and crashing.  */
20595   dwarf2out_abstract_function (decl);
20596
20597   if (! BLOCK_ABSTRACT (stmt))
20598     {
20599       dw_die_ref subr_die
20600         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20601
20602       if (call_arg_locations)
20603         {
20604           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20605             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20606                                    BLOCK_NUMBER (stmt) + 1);
20607           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20608         }
20609       add_abstract_origin_attribute (subr_die, decl);
20610       if (TREE_ASM_WRITTEN (stmt))
20611         add_high_low_attributes (stmt, subr_die);
20612       add_call_src_coords_attributes (stmt, subr_die);
20613
20614       decls_for_scope (stmt, subr_die, depth);
20615       current_function_has_inlines = 1;
20616     }
20617 }
20618
20619 /* Generate a DIE for a field in a record, or structure.  */
20620
20621 static void
20622 gen_field_die (tree decl, dw_die_ref context_die)
20623 {
20624   dw_die_ref decl_die;
20625
20626   if (TREE_TYPE (decl) == error_mark_node)
20627     return;
20628
20629   decl_die = new_die (DW_TAG_member, context_die, decl);
20630   add_name_and_src_coords_attributes (decl_die, decl);
20631   add_type_attribute (decl_die, member_declared_type (decl),
20632                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20633                       context_die);
20634
20635   if (DECL_BIT_FIELD_TYPE (decl))
20636     {
20637       add_byte_size_attribute (decl_die, decl);
20638       add_bit_size_attribute (decl_die, decl);
20639       add_bit_offset_attribute (decl_die, decl);
20640     }
20641
20642   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20643     add_data_member_location_attribute (decl_die, decl);
20644
20645   if (DECL_ARTIFICIAL (decl))
20646     add_AT_flag (decl_die, DW_AT_artificial, 1);
20647
20648   add_accessibility_attribute (decl_die, decl);
20649
20650   /* Equate decl number to die, so that we can look up this decl later on.  */
20651   equate_decl_number_to_die (decl, decl_die);
20652 }
20653
20654 #if 0
20655 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20656    Use modified_type_die instead.
20657    We keep this code here just in case these types of DIEs may be needed to
20658    represent certain things in other languages (e.g. Pascal) someday.  */
20659
20660 static void
20661 gen_pointer_type_die (tree type, dw_die_ref context_die)
20662 {
20663   dw_die_ref ptr_die
20664     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20665
20666   equate_type_number_to_die (type, ptr_die);
20667   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20668   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20669 }
20670
20671 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20672    Use modified_type_die instead.
20673    We keep this code here just in case these types of DIEs may be needed to
20674    represent certain things in other languages (e.g. Pascal) someday.  */
20675
20676 static void
20677 gen_reference_type_die (tree type, dw_die_ref context_die)
20678 {
20679   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20680
20681   if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
20682     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20683   else
20684     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20685
20686   equate_type_number_to_die (type, ref_die);
20687   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20688   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20689 }
20690 #endif
20691
20692 /* Generate a DIE for a pointer to a member type.  */
20693
20694 static void
20695 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20696 {
20697   dw_die_ref ptr_die
20698     = new_die (DW_TAG_ptr_to_member_type,
20699                scope_die_for (type, context_die), type);
20700
20701   equate_type_number_to_die (type, ptr_die);
20702   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20703                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20704   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20705 }
20706
20707 /* Generate the DIE for the compilation unit.  */
20708
20709 static dw_die_ref
20710 gen_compile_unit_die (const char *filename)
20711 {
20712   dw_die_ref die;
20713   char producer[250];
20714   const char *language_string = lang_hooks.name;
20715   int language;
20716
20717   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20718
20719   if (filename)
20720     {
20721       add_name_attribute (die, filename);
20722       /* Don't add cwd for <built-in>.  */
20723       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20724         add_comp_dir_attribute (die);
20725     }
20726
20727   sprintf (producer, "%s %s", language_string, version_string);
20728
20729 #ifdef MIPS_DEBUGGING_INFO
20730   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20731      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20732      not appear in the producer string, the debugger reaches the conclusion
20733      that the object file is stripped and has no debugging information.
20734      To get the MIPS/SGI debugger to believe that there is debugging
20735      information in the object file, we add a -g to the producer string.  */
20736   if (debug_info_level > DINFO_LEVEL_TERSE)
20737     strcat (producer, " -g");
20738 #endif
20739
20740   add_AT_string (die, DW_AT_producer, producer);
20741
20742   /* If our producer is LTO try to figure out a common language to use
20743      from the global list of translation units.  */
20744   if (strcmp (language_string, "GNU GIMPLE") == 0)
20745     {
20746       unsigned i;
20747       tree t;
20748       const char *common_lang = NULL;
20749
20750       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20751         {
20752           if (!TRANSLATION_UNIT_LANGUAGE (t))
20753             continue;
20754           if (!common_lang)
20755             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20756           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20757             ;
20758           else if (strncmp (common_lang, "GNU C", 5) == 0
20759                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20760             /* Mixing C and C++ is ok, use C++ in that case.  */
20761             common_lang = "GNU C++";
20762           else
20763             {
20764               /* Fall back to C.  */
20765               common_lang = NULL;
20766               break;
20767             }
20768         }
20769
20770       if (common_lang)
20771         language_string = common_lang;
20772     }
20773
20774   language = DW_LANG_C89;
20775   if (strcmp (language_string, "GNU C++") == 0)
20776     language = DW_LANG_C_plus_plus;
20777   else if (strcmp (language_string, "GNU F77") == 0)
20778     language = DW_LANG_Fortran77;
20779   else if (strcmp (language_string, "GNU Pascal") == 0)
20780     language = DW_LANG_Pascal83;
20781   else if (dwarf_version >= 3 || !dwarf_strict)
20782     {
20783       if (strcmp (language_string, "GNU Ada") == 0)
20784         language = DW_LANG_Ada95;
20785       else if (strcmp (language_string, "GNU Fortran") == 0)
20786         language = DW_LANG_Fortran95;
20787       else if (strcmp (language_string, "GNU Java") == 0)
20788         language = DW_LANG_Java;
20789       else if (strcmp (language_string, "GNU Objective-C") == 0)
20790         language = DW_LANG_ObjC;
20791       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20792         language = DW_LANG_ObjC_plus_plus;
20793     }
20794
20795   add_AT_unsigned (die, DW_AT_language, language);
20796
20797   switch (language)
20798     {
20799     case DW_LANG_Fortran77:
20800     case DW_LANG_Fortran90:
20801     case DW_LANG_Fortran95:
20802       /* Fortran has case insensitive identifiers and the front-end
20803          lowercases everything.  */
20804       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20805       break;
20806     default:
20807       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20808       break;
20809     }
20810   return die;
20811 }
20812
20813 /* Generate the DIE for a base class.  */
20814
20815 static void
20816 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20817 {
20818   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20819
20820   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20821   add_data_member_location_attribute (die, binfo);
20822
20823   if (BINFO_VIRTUAL_P (binfo))
20824     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20825
20826   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20827      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20828      the default has always been DW_ACCESS_private.  */
20829   if (access == access_public_node)
20830     {
20831       if (dwarf_version == 2
20832           || context_die->die_tag == DW_TAG_class_type)
20833       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20834     }
20835   else if (access == access_protected_node)
20836     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20837   else if (dwarf_version > 2
20838            && context_die->die_tag != DW_TAG_class_type)
20839     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20840 }
20841
20842 /* Generate a DIE for a class member.  */
20843
20844 static void
20845 gen_member_die (tree type, dw_die_ref context_die)
20846 {
20847   tree member;
20848   tree binfo = TYPE_BINFO (type);
20849   dw_die_ref child;
20850
20851   /* If this is not an incomplete type, output descriptions of each of its
20852      members. Note that as we output the DIEs necessary to represent the
20853      members of this record or union type, we will also be trying to output
20854      DIEs to represent the *types* of those members. However the `type'
20855      function (above) will specifically avoid generating type DIEs for member
20856      types *within* the list of member DIEs for this (containing) type except
20857      for those types (of members) which are explicitly marked as also being
20858      members of this (containing) type themselves.  The g++ front- end can
20859      force any given type to be treated as a member of some other (containing)
20860      type by setting the TYPE_CONTEXT of the given (member) type to point to
20861      the TREE node representing the appropriate (containing) type.  */
20862
20863   /* First output info about the base classes.  */
20864   if (binfo)
20865     {
20866       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20867       int i;
20868       tree base;
20869
20870       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20871         gen_inheritance_die (base,
20872                              (accesses ? VEC_index (tree, accesses, i)
20873                               : access_public_node), context_die);
20874     }
20875
20876   /* Now output info about the data members and type members.  */
20877   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20878     {
20879       /* If we thought we were generating minimal debug info for TYPE
20880          and then changed our minds, some of the member declarations
20881          may have already been defined.  Don't define them again, but
20882          do put them in the right order.  */
20883
20884       child = lookup_decl_die (member);
20885       if (child)
20886         splice_child_die (context_die, child);
20887       else
20888         gen_decl_die (member, NULL, context_die);
20889     }
20890
20891   /* Now output info about the function members (if any).  */
20892   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20893     {
20894       /* Don't include clones in the member list.  */
20895       if (DECL_ABSTRACT_ORIGIN (member))
20896         continue;
20897
20898       child = lookup_decl_die (member);
20899       if (child)
20900         splice_child_die (context_die, child);
20901       else
20902         gen_decl_die (member, NULL, context_die);
20903     }
20904 }
20905
20906 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20907    is set, we pretend that the type was never defined, so we only get the
20908    member DIEs needed by later specification DIEs.  */
20909
20910 static void
20911 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20912                                 enum debug_info_usage usage)
20913 {
20914   dw_die_ref type_die = lookup_type_die (type);
20915   dw_die_ref scope_die = 0;
20916   int nested = 0;
20917   int complete = (TYPE_SIZE (type)
20918                   && (! TYPE_STUB_DECL (type)
20919                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20920   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20921   complete = complete && should_emit_struct_debug (type, usage);
20922
20923   if (type_die && ! complete)
20924     return;
20925
20926   if (TYPE_CONTEXT (type) != NULL_TREE
20927       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20928           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20929     nested = 1;
20930
20931   scope_die = scope_die_for (type, context_die);
20932
20933   if (! type_die || (nested && is_cu_die (scope_die)))
20934     /* First occurrence of type or toplevel definition of nested class.  */
20935     {
20936       dw_die_ref old_die = type_die;
20937
20938       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20939                           ? record_type_tag (type) : DW_TAG_union_type,
20940                           scope_die, type);
20941       equate_type_number_to_die (type, type_die);
20942       if (old_die)
20943         add_AT_specification (type_die, old_die);
20944       else
20945         {
20946           add_name_attribute (type_die, type_tag (type));
20947           add_gnat_descriptive_type_attribute (type_die, type, context_die);
20948         }
20949     }
20950   else
20951     remove_AT (type_die, DW_AT_declaration);
20952
20953   /* Generate child dies for template paramaters.  */
20954   if (debug_info_level > DINFO_LEVEL_TERSE
20955       && COMPLETE_TYPE_P (type))
20956     schedule_generic_params_dies_gen (type);
20957
20958   /* If this type has been completed, then give it a byte_size attribute and
20959      then give a list of members.  */
20960   if (complete && !ns_decl)
20961     {
20962       /* Prevent infinite recursion in cases where the type of some member of
20963          this type is expressed in terms of this type itself.  */
20964       TREE_ASM_WRITTEN (type) = 1;
20965       add_byte_size_attribute (type_die, type);
20966       if (TYPE_STUB_DECL (type) != NULL_TREE)
20967         {
20968           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20969           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20970         }
20971
20972       /* If the first reference to this type was as the return type of an
20973          inline function, then it may not have a parent.  Fix this now.  */
20974       if (type_die->die_parent == NULL)
20975         add_child_die (scope_die, type_die);
20976
20977       push_decl_scope (type);
20978       gen_member_die (type, type_die);
20979       pop_decl_scope ();
20980
20981       /* GNU extension: Record what type our vtable lives in.  */
20982       if (TYPE_VFIELD (type))
20983         {
20984           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20985
20986           gen_type_die (vtype, context_die);
20987           add_AT_die_ref (type_die, DW_AT_containing_type,
20988                           lookup_type_die (vtype));
20989         }
20990     }
20991   else
20992     {
20993       add_AT_flag (type_die, DW_AT_declaration, 1);
20994
20995       /* We don't need to do this for function-local types.  */
20996       if (TYPE_STUB_DECL (type)
20997           && ! decl_function_context (TYPE_STUB_DECL (type)))
20998         VEC_safe_push (tree, gc, incomplete_types, type);
20999     }
21000
21001   if (get_AT (type_die, DW_AT_name))
21002     add_pubtype (type, type_die);
21003 }
21004
21005 /* Generate a DIE for a subroutine _type_.  */
21006
21007 static void
21008 gen_subroutine_type_die (tree type, dw_die_ref context_die)
21009 {
21010   tree return_type = TREE_TYPE (type);
21011   dw_die_ref subr_die
21012     = new_die (DW_TAG_subroutine_type,
21013                scope_die_for (type, context_die), type);
21014
21015   equate_type_number_to_die (type, subr_die);
21016   add_prototyped_attribute (subr_die, type);
21017   add_type_attribute (subr_die, return_type, 0, 0, context_die);
21018   gen_formal_types_die (type, subr_die);
21019
21020   if (get_AT (subr_die, DW_AT_name))
21021     add_pubtype (type, subr_die);
21022 }
21023
21024 /* Generate a DIE for a type definition.  */
21025
21026 static void
21027 gen_typedef_die (tree decl, dw_die_ref context_die)
21028 {
21029   dw_die_ref type_die;
21030   tree origin;
21031
21032   if (TREE_ASM_WRITTEN (decl))
21033     return;
21034
21035   TREE_ASM_WRITTEN (decl) = 1;
21036   type_die = new_die (DW_TAG_typedef, context_die, decl);
21037   origin = decl_ultimate_origin (decl);
21038   if (origin != NULL)
21039     add_abstract_origin_attribute (type_die, origin);
21040   else
21041     {
21042       tree type;
21043
21044       add_name_and_src_coords_attributes (type_die, decl);
21045       if (DECL_ORIGINAL_TYPE (decl))
21046         {
21047           type = DECL_ORIGINAL_TYPE (decl);
21048
21049           gcc_assert (type != TREE_TYPE (decl));
21050           equate_type_number_to_die (TREE_TYPE (decl), type_die);
21051         }
21052       else
21053         {
21054           type = TREE_TYPE (decl);
21055
21056           if (is_naming_typedef_decl (TYPE_NAME (type)))
21057             {
21058               /* Here, we are in the case of decl being a typedef naming
21059                  an anonymous type, e.g:
21060                      typedef struct {...} foo;
21061                  In that case TREE_TYPE (decl) is not a typedef variant
21062                  type and TYPE_NAME of the anonymous type is set to the
21063                  TYPE_DECL of the typedef. This construct is emitted by
21064                  the C++ FE.
21065
21066                  TYPE is the anonymous struct named by the typedef
21067                  DECL. As we need the DW_AT_type attribute of the
21068                  DW_TAG_typedef to point to the DIE of TYPE, let's
21069                  generate that DIE right away. add_type_attribute
21070                  called below will then pick (via lookup_type_die) that
21071                  anonymous struct DIE.  */
21072               if (!TREE_ASM_WRITTEN (type))
21073                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
21074
21075               /* This is a GNU Extension.  We are adding a
21076                  DW_AT_linkage_name attribute to the DIE of the
21077                  anonymous struct TYPE.  The value of that attribute
21078                  is the name of the typedef decl naming the anonymous
21079                  struct.  This greatly eases the work of consumers of
21080                  this debug info.  */
21081               add_linkage_attr (lookup_type_die (type), decl);
21082             }
21083         }
21084
21085       add_type_attribute (type_die, type, TREE_READONLY (decl),
21086                           TREE_THIS_VOLATILE (decl), context_die);
21087
21088       if (is_naming_typedef_decl (decl))
21089         /* We want that all subsequent calls to lookup_type_die with
21090            TYPE in argument yield the DW_TAG_typedef we have just
21091            created.  */
21092         equate_type_number_to_die (type, type_die);
21093
21094       add_accessibility_attribute (type_die, decl);
21095     }
21096
21097   if (DECL_ABSTRACT (decl))
21098     equate_decl_number_to_die (decl, type_die);
21099
21100   if (get_AT (type_die, DW_AT_name))
21101     add_pubtype (decl, type_die);
21102 }
21103
21104 /* Generate a DIE for a struct, class, enum or union type.  */
21105
21106 static void
21107 gen_tagged_type_die (tree type,
21108                      dw_die_ref context_die,
21109                      enum debug_info_usage usage)
21110 {
21111   int need_pop;
21112
21113   if (type == NULL_TREE
21114       || !is_tagged_type (type))
21115     return;
21116
21117   /* If this is a nested type whose containing class hasn't been written
21118      out yet, writing it out will cover this one, too.  This does not apply
21119      to instantiations of member class templates; they need to be added to
21120      the containing class as they are generated.  FIXME: This hurts the
21121      idea of combining type decls from multiple TUs, since we can't predict
21122      what set of template instantiations we'll get.  */
21123   if (TYPE_CONTEXT (type)
21124       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21125       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
21126     {
21127       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
21128
21129       if (TREE_ASM_WRITTEN (type))
21130         return;
21131
21132       /* If that failed, attach ourselves to the stub.  */
21133       push_decl_scope (TYPE_CONTEXT (type));
21134       context_die = lookup_type_die (TYPE_CONTEXT (type));
21135       need_pop = 1;
21136     }
21137   else if (TYPE_CONTEXT (type) != NULL_TREE
21138            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
21139     {
21140       /* If this type is local to a function that hasn't been written
21141          out yet, use a NULL context for now; it will be fixed up in
21142          decls_for_scope.  */
21143       context_die = lookup_decl_die (TYPE_CONTEXT (type));
21144       /* A declaration DIE doesn't count; nested types need to go in the
21145          specification.  */
21146       if (context_die && is_declaration_die (context_die))
21147         context_die = NULL;
21148       need_pop = 0;
21149     }
21150   else
21151     {
21152       context_die = declare_in_namespace (type, context_die);
21153       need_pop = 0;
21154     }
21155
21156   if (TREE_CODE (type) == ENUMERAL_TYPE)
21157     {
21158       /* This might have been written out by the call to
21159          declare_in_namespace.  */
21160       if (!TREE_ASM_WRITTEN (type))
21161         gen_enumeration_type_die (type, context_die);
21162     }
21163   else
21164     gen_struct_or_union_type_die (type, context_die, usage);
21165
21166   if (need_pop)
21167     pop_decl_scope ();
21168
21169   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
21170      it up if it is ever completed.  gen_*_type_die will set it for us
21171      when appropriate.  */
21172 }
21173
21174 /* Generate a type description DIE.  */
21175
21176 static void
21177 gen_type_die_with_usage (tree type, dw_die_ref context_die,
21178                          enum debug_info_usage usage)
21179 {
21180   struct array_descr_info info;
21181
21182   if (type == NULL_TREE || type == error_mark_node)
21183     return;
21184
21185   if (TYPE_NAME (type) != NULL_TREE
21186       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21187       && is_redundant_typedef (TYPE_NAME (type))
21188       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
21189     /* The DECL of this type is a typedef we don't want to emit debug
21190        info for but we want debug info for its underlying typedef.
21191        This can happen for e.g, the injected-class-name of a C++
21192        type.  */
21193     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
21194
21195   /* If TYPE is a typedef type variant, let's generate debug info
21196      for the parent typedef which TYPE is a type of.  */
21197   if (typedef_variant_p (type))
21198     {
21199       if (TREE_ASM_WRITTEN (type))
21200         return;
21201
21202       /* Prevent broken recursion; we can't hand off to the same type.  */
21203       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
21204
21205       /* Use the DIE of the containing namespace as the parent DIE of
21206          the type description DIE we want to generate.  */
21207       if (DECL_CONTEXT (TYPE_NAME (type))
21208           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21209         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21210
21211       TREE_ASM_WRITTEN (type) = 1;
21212
21213       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21214       return;
21215     }
21216
21217   /* If type is an anonymous tagged type named by a typedef, let's
21218      generate debug info for the typedef.  */
21219   if (is_naming_typedef_decl (TYPE_NAME (type)))
21220     {
21221       /* Use the DIE of the containing namespace as the parent DIE of
21222          the type description DIE we want to generate.  */
21223       if (DECL_CONTEXT (TYPE_NAME (type))
21224           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21225         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21226       
21227       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21228       return;
21229     }
21230
21231   /* If this is an array type with hidden descriptor, handle it first.  */
21232   if (!TREE_ASM_WRITTEN (type)
21233       && lang_hooks.types.get_array_descr_info
21234       && lang_hooks.types.get_array_descr_info (type, &info)
21235       && (dwarf_version >= 3 || !dwarf_strict))
21236     {
21237       gen_descr_array_type_die (type, &info, context_die);
21238       TREE_ASM_WRITTEN (type) = 1;
21239       return;
21240     }
21241
21242   /* We are going to output a DIE to represent the unqualified version
21243      of this type (i.e. without any const or volatile qualifiers) so
21244      get the main variant (i.e. the unqualified version) of this type
21245      now.  (Vectors are special because the debugging info is in the
21246      cloned type itself).  */
21247   if (TREE_CODE (type) != VECTOR_TYPE)
21248     type = type_main_variant (type);
21249
21250   if (TREE_ASM_WRITTEN (type))
21251     return;
21252
21253   switch (TREE_CODE (type))
21254     {
21255     case ERROR_MARK:
21256       break;
21257
21258     case POINTER_TYPE:
21259     case REFERENCE_TYPE:
21260       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
21261          ensures that the gen_type_die recursion will terminate even if the
21262          type is recursive.  Recursive types are possible in Ada.  */
21263       /* ??? We could perhaps do this for all types before the switch
21264          statement.  */
21265       TREE_ASM_WRITTEN (type) = 1;
21266
21267       /* For these types, all that is required is that we output a DIE (or a
21268          set of DIEs) to represent the "basis" type.  */
21269       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21270                                 DINFO_USAGE_IND_USE);
21271       break;
21272
21273     case OFFSET_TYPE:
21274       /* This code is used for C++ pointer-to-data-member types.
21275          Output a description of the relevant class type.  */
21276       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
21277                                         DINFO_USAGE_IND_USE);
21278
21279       /* Output a description of the type of the object pointed to.  */
21280       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21281                                         DINFO_USAGE_IND_USE);
21282
21283       /* Now output a DIE to represent this pointer-to-data-member type
21284          itself.  */
21285       gen_ptr_to_mbr_type_die (type, context_die);
21286       break;
21287
21288     case FUNCTION_TYPE:
21289       /* Force out return type (in case it wasn't forced out already).  */
21290       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21291                                         DINFO_USAGE_DIR_USE);
21292       gen_subroutine_type_die (type, context_die);
21293       break;
21294
21295     case METHOD_TYPE:
21296       /* Force out return type (in case it wasn't forced out already).  */
21297       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21298                                         DINFO_USAGE_DIR_USE);
21299       gen_subroutine_type_die (type, context_die);
21300       break;
21301
21302     case ARRAY_TYPE:
21303       gen_array_type_die (type, context_die);
21304       break;
21305
21306     case VECTOR_TYPE:
21307       gen_array_type_die (type, context_die);
21308       break;
21309
21310     case ENUMERAL_TYPE:
21311     case RECORD_TYPE:
21312     case UNION_TYPE:
21313     case QUAL_UNION_TYPE:
21314       gen_tagged_type_die (type, context_die, usage);
21315       return;
21316
21317     case VOID_TYPE:
21318     case INTEGER_TYPE:
21319     case REAL_TYPE:
21320     case FIXED_POINT_TYPE:
21321     case COMPLEX_TYPE:
21322     case BOOLEAN_TYPE:
21323       /* No DIEs needed for fundamental types.  */
21324       break;
21325
21326     case NULLPTR_TYPE:
21327     case LANG_TYPE:
21328       /* Just use DW_TAG_unspecified_type.  */
21329       {
21330         dw_die_ref type_die = lookup_type_die (type);
21331         if (type_die == NULL)
21332           {
21333             tree name = TYPE_NAME (type);
21334             if (TREE_CODE (name) == TYPE_DECL)
21335               name = DECL_NAME (name);
21336             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
21337             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
21338             equate_type_number_to_die (type, type_die);
21339           }
21340       }
21341       break;
21342
21343     default:
21344       gcc_unreachable ();
21345     }
21346
21347   TREE_ASM_WRITTEN (type) = 1;
21348 }
21349
21350 static void
21351 gen_type_die (tree type, dw_die_ref context_die)
21352 {
21353   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
21354 }
21355
21356 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
21357    things which are local to the given block.  */
21358
21359 static void
21360 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
21361 {
21362   int must_output_die = 0;
21363   bool inlined_func;
21364
21365   /* Ignore blocks that are NULL.  */
21366   if (stmt == NULL_TREE)
21367     return;
21368
21369   inlined_func = inlined_function_outer_scope_p (stmt);
21370
21371   /* If the block is one fragment of a non-contiguous block, do not
21372      process the variables, since they will have been done by the
21373      origin block.  Do process subblocks.  */
21374   if (BLOCK_FRAGMENT_ORIGIN (stmt))
21375     {
21376       tree sub;
21377
21378       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
21379         gen_block_die (sub, context_die, depth + 1);
21380
21381       return;
21382     }
21383
21384   /* Determine if we need to output any Dwarf DIEs at all to represent this
21385      block.  */
21386   if (inlined_func)
21387     /* The outer scopes for inlinings *must* always be represented.  We
21388        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
21389     must_output_die = 1;
21390   else
21391     {
21392       /* Determine if this block directly contains any "significant"
21393          local declarations which we will need to output DIEs for.  */
21394       if (debug_info_level > DINFO_LEVEL_TERSE)
21395         /* We are not in terse mode so *any* local declaration counts
21396            as being a "significant" one.  */
21397         must_output_die = ((BLOCK_VARS (stmt) != NULL
21398                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
21399                            && (TREE_USED (stmt)
21400                                || TREE_ASM_WRITTEN (stmt)
21401                                || BLOCK_ABSTRACT (stmt)));
21402       else if ((TREE_USED (stmt)
21403                 || TREE_ASM_WRITTEN (stmt)
21404                 || BLOCK_ABSTRACT (stmt))
21405                && !dwarf2out_ignore_block (stmt))
21406         must_output_die = 1;
21407     }
21408
21409   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
21410      DIE for any block which contains no significant local declarations at
21411      all.  Rather, in such cases we just call `decls_for_scope' so that any
21412      needed Dwarf info for any sub-blocks will get properly generated. Note
21413      that in terse mode, our definition of what constitutes a "significant"
21414      local declaration gets restricted to include only inlined function
21415      instances and local (nested) function definitions.  */
21416   if (must_output_die)
21417     {
21418       if (inlined_func)
21419         {
21420           /* If STMT block is abstract, that means we have been called
21421              indirectly from dwarf2out_abstract_function.
21422              That function rightfully marks the descendent blocks (of
21423              the abstract function it is dealing with) as being abstract,
21424              precisely to prevent us from emitting any
21425              DW_TAG_inlined_subroutine DIE as a descendent
21426              of an abstract function instance. So in that case, we should
21427              not call gen_inlined_subroutine_die.
21428
21429              Later though, when cgraph asks dwarf2out to emit info
21430              for the concrete instance of the function decl into which
21431              the concrete instance of STMT got inlined, the later will lead
21432              to the generation of a DW_TAG_inlined_subroutine DIE.  */
21433           if (! BLOCK_ABSTRACT (stmt))
21434             gen_inlined_subroutine_die (stmt, context_die, depth);
21435         }
21436       else
21437         gen_lexical_block_die (stmt, context_die, depth);
21438     }
21439   else
21440     decls_for_scope (stmt, context_die, depth);
21441 }
21442
21443 /* Process variable DECL (or variable with origin ORIGIN) within
21444    block STMT and add it to CONTEXT_DIE.  */
21445 static void
21446 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21447 {
21448   dw_die_ref die;
21449   tree decl_or_origin = decl ? decl : origin;
21450
21451   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21452     die = lookup_decl_die (decl_or_origin);
21453   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21454            && TYPE_DECL_IS_STUB (decl_or_origin))
21455     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21456   else
21457     die = NULL;
21458
21459   if (die != NULL && die->die_parent == NULL)
21460     add_child_die (context_die, die);
21461   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21462     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21463                                          stmt, context_die);
21464   else
21465     gen_decl_die (decl, origin, context_die);
21466 }
21467
21468 /* Generate all of the decls declared within a given scope and (recursively)
21469    all of its sub-blocks.  */
21470
21471 static void
21472 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
21473 {
21474   tree decl;
21475   unsigned int i;
21476   tree subblocks;
21477
21478   /* Ignore NULL blocks.  */
21479   if (stmt == NULL_TREE)
21480     return;
21481
21482   /* Output the DIEs to represent all of the data objects and typedefs
21483      declared directly within this block but not within any nested
21484      sub-blocks.  Also, nested function and tag DIEs have been
21485      generated with a parent of NULL; fix that up now.  */
21486   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21487     process_scope_var (stmt, decl, NULL_TREE, context_die);
21488   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21489     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21490                        context_die);
21491
21492   /* If we're at -g1, we're not interested in subblocks.  */
21493   if (debug_info_level <= DINFO_LEVEL_TERSE)
21494     return;
21495
21496   /* Output the DIEs to represent all sub-blocks (and the items declared
21497      therein) of this block.  */
21498   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21499        subblocks != NULL;
21500        subblocks = BLOCK_CHAIN (subblocks))
21501     gen_block_die (subblocks, context_die, depth + 1);
21502 }
21503
21504 /* Is this a typedef we can avoid emitting?  */
21505
21506 static inline int
21507 is_redundant_typedef (const_tree decl)
21508 {
21509   if (TYPE_DECL_IS_STUB (decl))
21510     return 1;
21511
21512   if (DECL_ARTIFICIAL (decl)
21513       && DECL_CONTEXT (decl)
21514       && is_tagged_type (DECL_CONTEXT (decl))
21515       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21516       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21517     /* Also ignore the artificial member typedef for the class name.  */
21518     return 1;
21519
21520   return 0;
21521 }
21522
21523 /* Return TRUE if TYPE is a typedef that names a type for linkage
21524    purposes. This kind of typedefs is produced by the C++ FE for
21525    constructs like:
21526
21527    typedef struct {...} foo;
21528
21529    In that case, there is no typedef variant type produced for foo.
21530    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21531    struct type.  */
21532
21533 static bool
21534 is_naming_typedef_decl (const_tree decl)
21535 {
21536   if (decl == NULL_TREE
21537       || TREE_CODE (decl) != TYPE_DECL
21538       || !is_tagged_type (TREE_TYPE (decl))
21539       || DECL_IS_BUILTIN (decl)
21540       || is_redundant_typedef (decl)
21541       /* It looks like Ada produces TYPE_DECLs that are very similar
21542          to C++ naming typedefs but that have different
21543          semantics. Let's be specific to c++ for now.  */
21544       || !is_cxx ())
21545     return FALSE;
21546
21547   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21548           && TYPE_NAME (TREE_TYPE (decl)) == decl
21549           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21550               != TYPE_NAME (TREE_TYPE (decl))));
21551 }
21552
21553 /* Returns the DIE for a context.  */
21554
21555 static inline dw_die_ref
21556 get_context_die (tree context)
21557 {
21558   if (context)
21559     {
21560       /* Find die that represents this context.  */
21561       if (TYPE_P (context))
21562         {
21563           context = TYPE_MAIN_VARIANT (context);
21564           return strip_naming_typedef (context, force_type_die (context));
21565         }
21566       else
21567         return force_decl_die (context);
21568     }
21569   return comp_unit_die ();
21570 }
21571
21572 /* Returns the DIE for decl.  A DIE will always be returned.  */
21573
21574 static dw_die_ref
21575 force_decl_die (tree decl)
21576 {
21577   dw_die_ref decl_die;
21578   unsigned saved_external_flag;
21579   tree save_fn = NULL_TREE;
21580   decl_die = lookup_decl_die (decl);
21581   if (!decl_die)
21582     {
21583       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21584
21585       decl_die = lookup_decl_die (decl);
21586       if (decl_die)
21587         return decl_die;
21588
21589       switch (TREE_CODE (decl))
21590         {
21591         case FUNCTION_DECL:
21592           /* Clear current_function_decl, so that gen_subprogram_die thinks
21593              that this is a declaration. At this point, we just want to force
21594              declaration die.  */
21595           save_fn = current_function_decl;
21596           current_function_decl = NULL_TREE;
21597           gen_subprogram_die (decl, context_die);
21598           current_function_decl = save_fn;
21599           break;
21600
21601         case VAR_DECL:
21602           /* Set external flag to force declaration die. Restore it after
21603            gen_decl_die() call.  */
21604           saved_external_flag = DECL_EXTERNAL (decl);
21605           DECL_EXTERNAL (decl) = 1;
21606           gen_decl_die (decl, NULL, context_die);
21607           DECL_EXTERNAL (decl) = saved_external_flag;
21608           break;
21609
21610         case NAMESPACE_DECL:
21611           if (dwarf_version >= 3 || !dwarf_strict)
21612             dwarf2out_decl (decl);
21613           else
21614             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21615             decl_die = comp_unit_die ();
21616           break;
21617
21618         case TRANSLATION_UNIT_DECL:
21619           decl_die = comp_unit_die ();
21620           break;
21621
21622         default:
21623           gcc_unreachable ();
21624         }
21625
21626       /* We should be able to find the DIE now.  */
21627       if (!decl_die)
21628         decl_die = lookup_decl_die (decl);
21629       gcc_assert (decl_die);
21630     }
21631
21632   return decl_die;
21633 }
21634
21635 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21636    always returned.  */
21637
21638 static dw_die_ref
21639 force_type_die (tree type)
21640 {
21641   dw_die_ref type_die;
21642
21643   type_die = lookup_type_die (type);
21644   if (!type_die)
21645     {
21646       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21647
21648       type_die = modified_type_die (type, TYPE_READONLY (type),
21649                                     TYPE_VOLATILE (type), context_die);
21650       gcc_assert (type_die);
21651     }
21652   return type_die;
21653 }
21654
21655 /* Force out any required namespaces to be able to output DECL,
21656    and return the new context_die for it, if it's changed.  */
21657
21658 static dw_die_ref
21659 setup_namespace_context (tree thing, dw_die_ref context_die)
21660 {
21661   tree context = (DECL_P (thing)
21662                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21663   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21664     /* Force out the namespace.  */
21665     context_die = force_decl_die (context);
21666
21667   return context_die;
21668 }
21669
21670 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21671    type) within its namespace, if appropriate.
21672
21673    For compatibility with older debuggers, namespace DIEs only contain
21674    declarations; all definitions are emitted at CU scope.  */
21675
21676 static dw_die_ref
21677 declare_in_namespace (tree thing, dw_die_ref context_die)
21678 {
21679   dw_die_ref ns_context;
21680
21681   if (debug_info_level <= DINFO_LEVEL_TERSE)
21682     return context_die;
21683
21684   /* If this decl is from an inlined function, then don't try to emit it in its
21685      namespace, as we will get confused.  It would have already been emitted
21686      when the abstract instance of the inline function was emitted anyways.  */
21687   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21688     return context_die;
21689
21690   ns_context = setup_namespace_context (thing, context_die);
21691
21692   if (ns_context != context_die)
21693     {
21694       if (is_fortran ())
21695         return ns_context;
21696       if (DECL_P (thing))
21697         gen_decl_die (thing, NULL, ns_context);
21698       else
21699         gen_type_die (thing, ns_context);
21700     }
21701   return context_die;
21702 }
21703
21704 /* Generate a DIE for a namespace or namespace alias.  */
21705
21706 static void
21707 gen_namespace_die (tree decl, dw_die_ref context_die)
21708 {
21709   dw_die_ref namespace_die;
21710
21711   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21712      they are an alias of.  */
21713   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21714     {
21715       /* Output a real namespace or module.  */
21716       context_die = setup_namespace_context (decl, comp_unit_die ());
21717       namespace_die = new_die (is_fortran ()
21718                                ? DW_TAG_module : DW_TAG_namespace,
21719                                context_die, decl);
21720       /* For Fortran modules defined in different CU don't add src coords.  */
21721       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21722         {
21723           const char *name = dwarf2_name (decl, 0);
21724           if (name)
21725             add_name_attribute (namespace_die, name);
21726         }
21727       else
21728         add_name_and_src_coords_attributes (namespace_die, decl);
21729       if (DECL_EXTERNAL (decl))
21730         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21731       equate_decl_number_to_die (decl, namespace_die);
21732     }
21733   else
21734     {
21735       /* Output a namespace alias.  */
21736
21737       /* Force out the namespace we are an alias of, if necessary.  */
21738       dw_die_ref origin_die
21739         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21740
21741       if (DECL_FILE_SCOPE_P (decl)
21742           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21743         context_die = setup_namespace_context (decl, comp_unit_die ());
21744       /* Now create the namespace alias DIE.  */
21745       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21746       add_name_and_src_coords_attributes (namespace_die, decl);
21747       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21748       equate_decl_number_to_die (decl, namespace_die);
21749     }
21750 }
21751
21752 /* Generate Dwarf debug information for a decl described by DECL.
21753    The return value is currently only meaningful for PARM_DECLs,
21754    for all other decls it returns NULL.  */
21755
21756 static dw_die_ref
21757 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21758 {
21759   tree decl_or_origin = decl ? decl : origin;
21760   tree class_origin = NULL, ultimate_origin;
21761
21762   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21763     return NULL;
21764
21765   switch (TREE_CODE (decl_or_origin))
21766     {
21767     case ERROR_MARK:
21768       break;
21769
21770     case CONST_DECL:
21771       if (!is_fortran () && !is_ada ())
21772         {
21773           /* The individual enumerators of an enum type get output when we output
21774              the Dwarf representation of the relevant enum type itself.  */
21775           break;
21776         }
21777
21778       /* Emit its type.  */
21779       gen_type_die (TREE_TYPE (decl), context_die);
21780
21781       /* And its containing namespace.  */
21782       context_die = declare_in_namespace (decl, context_die);
21783
21784       gen_const_die (decl, context_die);
21785       break;
21786
21787     case FUNCTION_DECL:
21788       /* Don't output any DIEs to represent mere function declarations,
21789          unless they are class members or explicit block externs.  */
21790       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21791           && DECL_FILE_SCOPE_P (decl_or_origin)
21792           && (current_function_decl == NULL_TREE
21793               || DECL_ARTIFICIAL (decl_or_origin)))
21794         break;
21795
21796 #if 0
21797       /* FIXME */
21798       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21799          on local redeclarations of global functions.  That seems broken.  */
21800       if (current_function_decl != decl)
21801         /* This is only a declaration.  */;
21802 #endif
21803
21804       /* If we're emitting a clone, emit info for the abstract instance.  */
21805       if (origin || DECL_ORIGIN (decl) != decl)
21806         dwarf2out_abstract_function (origin
21807                                      ? DECL_ORIGIN (origin)
21808                                      : DECL_ABSTRACT_ORIGIN (decl));
21809
21810       /* If we're emitting an out-of-line copy of an inline function,
21811          emit info for the abstract instance and set up to refer to it.  */
21812       else if (cgraph_function_possibly_inlined_p (decl)
21813                && ! DECL_ABSTRACT (decl)
21814                && ! class_or_namespace_scope_p (context_die)
21815                /* dwarf2out_abstract_function won't emit a die if this is just
21816                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21817                   that case, because that works only if we have a die.  */
21818                && DECL_INITIAL (decl) != NULL_TREE)
21819         {
21820           dwarf2out_abstract_function (decl);
21821           set_decl_origin_self (decl);
21822         }
21823
21824       /* Otherwise we're emitting the primary DIE for this decl.  */
21825       else if (debug_info_level > DINFO_LEVEL_TERSE)
21826         {
21827           /* Before we describe the FUNCTION_DECL itself, make sure that we
21828              have its containing type.  */
21829           if (!origin)
21830             origin = decl_class_context (decl);
21831           if (origin != NULL_TREE)
21832             gen_type_die (origin, context_die);
21833
21834           /* And its return type.  */
21835           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21836
21837           /* And its virtual context.  */
21838           if (DECL_VINDEX (decl) != NULL_TREE)
21839             gen_type_die (DECL_CONTEXT (decl), context_die);
21840
21841           /* Make sure we have a member DIE for decl.  */
21842           if (origin != NULL_TREE)
21843             gen_type_die_for_member (origin, decl, context_die);
21844
21845           /* And its containing namespace.  */
21846           context_die = declare_in_namespace (decl, context_die);
21847         }
21848
21849       /* Now output a DIE to represent the function itself.  */
21850       if (decl)
21851         gen_subprogram_die (decl, context_die);
21852       break;
21853
21854     case TYPE_DECL:
21855       /* If we are in terse mode, don't generate any DIEs to represent any
21856          actual typedefs.  */
21857       if (debug_info_level <= DINFO_LEVEL_TERSE)
21858         break;
21859
21860       /* In the special case of a TYPE_DECL node representing the declaration
21861          of some type tag, if the given TYPE_DECL is marked as having been
21862          instantiated from some other (original) TYPE_DECL node (e.g. one which
21863          was generated within the original definition of an inline function) we
21864          used to generate a special (abbreviated) DW_TAG_structure_type,
21865          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21866          should be actually referencing those DIEs, as variable DIEs with that
21867          type would be emitted already in the abstract origin, so it was always
21868          removed during unused type prunning.  Don't add anything in this
21869          case.  */
21870       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21871         break;
21872
21873       if (is_redundant_typedef (decl))
21874         gen_type_die (TREE_TYPE (decl), context_die);
21875       else
21876         /* Output a DIE to represent the typedef itself.  */
21877         gen_typedef_die (decl, context_die);
21878       break;
21879
21880     case LABEL_DECL:
21881       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21882         gen_label_die (decl, context_die);
21883       break;
21884
21885     case VAR_DECL:
21886     case RESULT_DECL:
21887       /* If we are in terse mode, don't generate any DIEs to represent any
21888          variable declarations or definitions.  */
21889       if (debug_info_level <= DINFO_LEVEL_TERSE)
21890         break;
21891
21892       /* Output any DIEs that are needed to specify the type of this data
21893          object.  */
21894       if (decl_by_reference_p (decl_or_origin))
21895         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21896       else
21897         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21898
21899       /* And its containing type.  */
21900       class_origin = decl_class_context (decl_or_origin);
21901       if (class_origin != NULL_TREE)
21902         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21903
21904       /* And its containing namespace.  */
21905       context_die = declare_in_namespace (decl_or_origin, context_die);
21906
21907       /* Now output the DIE to represent the data object itself.  This gets
21908          complicated because of the possibility that the VAR_DECL really
21909          represents an inlined instance of a formal parameter for an inline
21910          function.  */
21911       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21912       if (ultimate_origin != NULL_TREE
21913           && TREE_CODE (ultimate_origin) == PARM_DECL)
21914         gen_formal_parameter_die (decl, origin,
21915                                   true /* Emit name attribute.  */,
21916                                   context_die);
21917       else
21918         gen_variable_die (decl, origin, context_die);
21919       break;
21920
21921     case FIELD_DECL:
21922       /* Ignore the nameless fields that are used to skip bits but handle C++
21923          anonymous unions and structs.  */
21924       if (DECL_NAME (decl) != NULL_TREE
21925           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21926           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21927         {
21928           gen_type_die (member_declared_type (decl), context_die);
21929           gen_field_die (decl, context_die);
21930         }
21931       break;
21932
21933     case PARM_DECL:
21934       if (DECL_BY_REFERENCE (decl_or_origin))
21935         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21936       else
21937         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21938       return gen_formal_parameter_die (decl, origin,
21939                                        true /* Emit name attribute.  */,
21940                                        context_die);
21941
21942     case NAMESPACE_DECL:
21943     case IMPORTED_DECL:
21944       if (dwarf_version >= 3 || !dwarf_strict)
21945         gen_namespace_die (decl, context_die);
21946       break;
21947
21948     default:
21949       /* Probably some frontend-internal decl.  Assume we don't care.  */
21950       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21951       break;
21952     }
21953
21954   return NULL;
21955 }
21956 \f
21957 /* Output debug information for global decl DECL.  Called from toplev.c after
21958    compilation proper has finished.  */
21959
21960 static void
21961 dwarf2out_global_decl (tree decl)
21962 {
21963   /* Output DWARF2 information for file-scope tentative data object
21964      declarations, file-scope (extern) function declarations (which
21965      had no corresponding body) and file-scope tagged type declarations
21966      and definitions which have not yet been forced out.  */
21967   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21968     dwarf2out_decl (decl);
21969 }
21970
21971 /* Output debug information for type decl DECL.  Called from toplev.c
21972    and from language front ends (to record built-in types).  */
21973 static void
21974 dwarf2out_type_decl (tree decl, int local)
21975 {
21976   if (!local)
21977     dwarf2out_decl (decl);
21978 }
21979
21980 /* Output debug information for imported module or decl DECL.
21981    NAME is non-NULL name in the lexical block if the decl has been renamed.
21982    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21983    that DECL belongs to.
21984    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21985 static void
21986 dwarf2out_imported_module_or_decl_1 (tree decl,
21987                                      tree name,
21988                                      tree lexical_block,
21989                                      dw_die_ref lexical_block_die)
21990 {
21991   expanded_location xloc;
21992   dw_die_ref imported_die = NULL;
21993   dw_die_ref at_import_die;
21994
21995   if (TREE_CODE (decl) == IMPORTED_DECL)
21996     {
21997       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21998       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21999       gcc_assert (decl);
22000     }
22001   else
22002     xloc = expand_location (input_location);
22003
22004   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
22005     {
22006       at_import_die = force_type_die (TREE_TYPE (decl));
22007       /* For namespace N { typedef void T; } using N::T; base_type_die
22008          returns NULL, but DW_TAG_imported_declaration requires
22009          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
22010       if (!at_import_die)
22011         {
22012           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
22013           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
22014           at_import_die = lookup_type_die (TREE_TYPE (decl));
22015           gcc_assert (at_import_die);
22016         }
22017     }
22018   else
22019     {
22020       at_import_die = lookup_decl_die (decl);
22021       if (!at_import_die)
22022         {
22023           /* If we're trying to avoid duplicate debug info, we may not have
22024              emitted the member decl for this field.  Emit it now.  */
22025           if (TREE_CODE (decl) == FIELD_DECL)
22026             {
22027               tree type = DECL_CONTEXT (decl);
22028
22029               if (TYPE_CONTEXT (type)
22030                   && TYPE_P (TYPE_CONTEXT (type))
22031                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
22032                                                 DINFO_USAGE_DIR_USE))
22033                 return;
22034               gen_type_die_for_member (type, decl,
22035                                        get_context_die (TYPE_CONTEXT (type)));
22036             }
22037           at_import_die = force_decl_die (decl);
22038         }
22039     }
22040
22041   if (TREE_CODE (decl) == NAMESPACE_DECL)
22042     {
22043       if (dwarf_version >= 3 || !dwarf_strict)
22044         imported_die = new_die (DW_TAG_imported_module,
22045                                 lexical_block_die,
22046                                 lexical_block);
22047       else
22048         return;
22049     }
22050   else
22051     imported_die = new_die (DW_TAG_imported_declaration,
22052                             lexical_block_die,
22053                             lexical_block);
22054
22055   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
22056   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
22057   if (name)
22058     add_AT_string (imported_die, DW_AT_name,
22059                    IDENTIFIER_POINTER (name));
22060   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
22061 }
22062
22063 /* Output debug information for imported module or decl DECL.
22064    NAME is non-NULL name in context if the decl has been renamed.
22065    CHILD is true if decl is one of the renamed decls as part of
22066    importing whole module.  */
22067
22068 static void
22069 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
22070                                    bool child)
22071 {
22072   /* dw_die_ref at_import_die;  */
22073   dw_die_ref scope_die;
22074
22075   if (debug_info_level <= DINFO_LEVEL_TERSE)
22076     return;
22077
22078   gcc_assert (decl);
22079
22080   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
22081      We need decl DIE for reference and scope die. First, get DIE for the decl
22082      itself.  */
22083
22084   /* Get the scope die for decl context. Use comp_unit_die for global module
22085      or decl. If die is not found for non globals, force new die.  */
22086   if (context
22087       && TYPE_P (context)
22088       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
22089     return;
22090
22091   if (!(dwarf_version >= 3 || !dwarf_strict))
22092     return;
22093
22094   scope_die = get_context_die (context);
22095
22096   if (child)
22097     {
22098       gcc_assert (scope_die->die_child);
22099       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
22100       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
22101       scope_die = scope_die->die_child;
22102     }
22103
22104   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
22105   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
22106
22107 }
22108
22109 /* Write the debugging output for DECL.  */
22110
22111 void
22112 dwarf2out_decl (tree decl)
22113 {
22114   dw_die_ref context_die = comp_unit_die ();
22115
22116   switch (TREE_CODE (decl))
22117     {
22118     case ERROR_MARK:
22119       return;
22120
22121     case FUNCTION_DECL:
22122       /* What we would really like to do here is to filter out all mere
22123          file-scope declarations of file-scope functions which are never
22124          referenced later within this translation unit (and keep all of ones
22125          that *are* referenced later on) but we aren't clairvoyant, so we have
22126          no idea which functions will be referenced in the future (i.e. later
22127          on within the current translation unit). So here we just ignore all
22128          file-scope function declarations which are not also definitions.  If
22129          and when the debugger needs to know something about these functions,
22130          it will have to hunt around and find the DWARF information associated
22131          with the definition of the function.
22132
22133          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
22134          nodes represent definitions and which ones represent mere
22135          declarations.  We have to check DECL_INITIAL instead. That's because
22136          the C front-end supports some weird semantics for "extern inline"
22137          function definitions.  These can get inlined within the current
22138          translation unit (and thus, we need to generate Dwarf info for their
22139          abstract instances so that the Dwarf info for the concrete inlined
22140          instances can have something to refer to) but the compiler never
22141          generates any out-of-lines instances of such things (despite the fact
22142          that they *are* definitions).
22143
22144          The important point is that the C front-end marks these "extern
22145          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
22146          them anyway. Note that the C++ front-end also plays some similar games
22147          for inline function definitions appearing within include files which
22148          also contain `#pragma interface' pragmas.  */
22149       if (DECL_INITIAL (decl) == NULL_TREE)
22150         return;
22151
22152       /* If we're a nested function, initially use a parent of NULL; if we're
22153          a plain function, this will be fixed up in decls_for_scope.  If
22154          we're a method, it will be ignored, since we already have a DIE.  */
22155       if (decl_function_context (decl)
22156           /* But if we're in terse mode, we don't care about scope.  */
22157           && debug_info_level > DINFO_LEVEL_TERSE)
22158         context_die = NULL;
22159       break;
22160
22161     case VAR_DECL:
22162       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
22163          declaration and if the declaration was never even referenced from
22164          within this entire compilation unit.  We suppress these DIEs in
22165          order to save space in the .debug section (by eliminating entries
22166          which are probably useless).  Note that we must not suppress
22167          block-local extern declarations (whether used or not) because that
22168          would screw-up the debugger's name lookup mechanism and cause it to
22169          miss things which really ought to be in scope at a given point.  */
22170       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
22171         return;
22172
22173       /* For local statics lookup proper context die.  */
22174       if (TREE_STATIC (decl) && decl_function_context (decl))
22175         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22176
22177       /* If we are in terse mode, don't generate any DIEs to represent any
22178          variable declarations or definitions.  */
22179       if (debug_info_level <= DINFO_LEVEL_TERSE)
22180         return;
22181       break;
22182
22183     case CONST_DECL:
22184       if (debug_info_level <= DINFO_LEVEL_TERSE)
22185         return;
22186       if (!is_fortran () && !is_ada ())
22187         return;
22188       if (TREE_STATIC (decl) && decl_function_context (decl))
22189         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22190       break;
22191
22192     case NAMESPACE_DECL:
22193     case IMPORTED_DECL:
22194       if (debug_info_level <= DINFO_LEVEL_TERSE)
22195         return;
22196       if (lookup_decl_die (decl) != NULL)
22197         return;
22198       break;
22199
22200     case TYPE_DECL:
22201       /* Don't emit stubs for types unless they are needed by other DIEs.  */
22202       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
22203         return;
22204
22205       /* Don't bother trying to generate any DIEs to represent any of the
22206          normal built-in types for the language we are compiling.  */
22207       if (DECL_IS_BUILTIN (decl))
22208         return;
22209
22210       /* If we are in terse mode, don't generate any DIEs for types.  */
22211       if (debug_info_level <= DINFO_LEVEL_TERSE)
22212         return;
22213
22214       /* If we're a function-scope tag, initially use a parent of NULL;
22215          this will be fixed up in decls_for_scope.  */
22216       if (decl_function_context (decl))
22217         context_die = NULL;
22218
22219       break;
22220
22221     default:
22222       return;
22223     }
22224
22225   gen_decl_die (decl, NULL, context_die);
22226 }
22227
22228 /* Write the debugging output for DECL.  */
22229
22230 static void
22231 dwarf2out_function_decl (tree decl)
22232 {
22233   dwarf2out_decl (decl);
22234   call_arg_locations = NULL;
22235   call_arg_loc_last = NULL;
22236   call_site_count = -1;
22237   tail_call_site_count = -1;
22238   VEC_free (dw_die_ref, heap, block_map);
22239   htab_empty (decl_loc_table);
22240   htab_empty (cached_dw_loc_list_table);
22241 }
22242
22243 /* Output a marker (i.e. a label) for the beginning of the generated code for
22244    a lexical block.  */
22245
22246 static void
22247 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22248                        unsigned int blocknum)
22249 {
22250   switch_to_section (current_function_section ());
22251   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22252 }
22253
22254 /* Output a marker (i.e. a label) for the end of the generated code for a
22255    lexical block.  */
22256
22257 static void
22258 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22259 {
22260   switch_to_section (current_function_section ());
22261   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22262 }
22263
22264 /* Returns nonzero if it is appropriate not to emit any debugging
22265    information for BLOCK, because it doesn't contain any instructions.
22266
22267    Don't allow this for blocks with nested functions or local classes
22268    as we would end up with orphans, and in the presence of scheduling
22269    we may end up calling them anyway.  */
22270
22271 static bool
22272 dwarf2out_ignore_block (const_tree block)
22273 {
22274   tree decl;
22275   unsigned int i;
22276
22277   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22278     if (TREE_CODE (decl) == FUNCTION_DECL
22279         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22280       return 0;
22281   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22282     {
22283       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22284       if (TREE_CODE (decl) == FUNCTION_DECL
22285           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22286       return 0;
22287     }
22288
22289   return 1;
22290 }
22291
22292 /* Hash table routines for file_hash.  */
22293
22294 static int
22295 file_table_eq (const void *p1_p, const void *p2_p)
22296 {
22297   const struct dwarf_file_data *const p1 =
22298     (const struct dwarf_file_data *) p1_p;
22299   const char *const p2 = (const char *) p2_p;
22300   return filename_cmp (p1->filename, p2) == 0;
22301 }
22302
22303 static hashval_t
22304 file_table_hash (const void *p_p)
22305 {
22306   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
22307   return htab_hash_string (p->filename);
22308 }
22309
22310 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22311    dwarf2out.c) and return its "index".  The index of each (known) filename is
22312    just a unique number which is associated with only that one filename.  We
22313    need such numbers for the sake of generating labels (in the .debug_sfnames
22314    section) and references to those files numbers (in the .debug_srcinfo
22315    and.debug_macinfo sections).  If the filename given as an argument is not
22316    found in our current list, add it to the list and assign it the next
22317    available unique index number.  In order to speed up searches, we remember
22318    the index of the filename was looked up last.  This handles the majority of
22319    all searches.  */
22320
22321 static struct dwarf_file_data *
22322 lookup_filename (const char *file_name)
22323 {
22324   void ** slot;
22325   struct dwarf_file_data * created;
22326
22327   /* Check to see if the file name that was searched on the previous
22328      call matches this file name.  If so, return the index.  */
22329   if (file_table_last_lookup
22330       && (file_name == file_table_last_lookup->filename
22331           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
22332     return file_table_last_lookup;
22333
22334   /* Didn't match the previous lookup, search the table.  */
22335   slot = htab_find_slot_with_hash (file_table, file_name,
22336                                    htab_hash_string (file_name), INSERT);
22337   if (*slot)
22338     return (struct dwarf_file_data *) *slot;
22339
22340   created = ggc_alloc_dwarf_file_data ();
22341   created->filename = file_name;
22342   created->emitted_number = 0;
22343   *slot = created;
22344   return created;
22345 }
22346
22347 /* If the assembler will construct the file table, then translate the compiler
22348    internal file table number into the assembler file table number, and emit
22349    a .file directive if we haven't already emitted one yet.  The file table
22350    numbers are different because we prune debug info for unused variables and
22351    types, which may include filenames.  */
22352
22353 static int
22354 maybe_emit_file (struct dwarf_file_data * fd)
22355 {
22356   if (! fd->emitted_number)
22357     {
22358       if (last_emitted_file)
22359         fd->emitted_number = last_emitted_file->emitted_number + 1;
22360       else
22361         fd->emitted_number = 1;
22362       last_emitted_file = fd;
22363
22364       if (DWARF2_ASM_LINE_DEBUG_INFO)
22365         {
22366           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22367           output_quoted_string (asm_out_file,
22368                                 remap_debug_filename (fd->filename));
22369           fputc ('\n', asm_out_file);
22370         }
22371     }
22372
22373   return fd->emitted_number;
22374 }
22375
22376 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22377    That generation should happen after function debug info has been
22378    generated. The value of the attribute is the constant value of ARG.  */
22379
22380 static void
22381 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22382 {
22383   die_arg_entry entry;
22384
22385   if (!die || !arg)
22386     return;
22387
22388   if (!tmpl_value_parm_die_table)
22389     tmpl_value_parm_die_table
22390       = VEC_alloc (die_arg_entry, gc, 32);
22391
22392   entry.die = die;
22393   entry.arg = arg;
22394   VEC_safe_push (die_arg_entry, gc,
22395                  tmpl_value_parm_die_table,
22396                  &entry);
22397 }
22398
22399 /* Return TRUE if T is an instance of generic type, FALSE
22400    otherwise.  */
22401
22402 static bool
22403 generic_type_p (tree t)
22404 {
22405   if (t == NULL_TREE || !TYPE_P (t))
22406     return false;
22407   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22408 }
22409
22410 /* Schedule the generation of the generic parameter dies for the
22411   instance of generic type T. The proper generation itself is later
22412   done by gen_scheduled_generic_parms_dies. */
22413
22414 static void
22415 schedule_generic_params_dies_gen (tree t)
22416 {
22417   if (!generic_type_p (t))
22418     return;
22419
22420   if (generic_type_instances == NULL)
22421     generic_type_instances = VEC_alloc (tree, gc, 256);
22422
22423   VEC_safe_push (tree, gc, generic_type_instances, t);
22424 }
22425
22426 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22427    by append_entry_to_tmpl_value_parm_die_table. This function must
22428    be called after function DIEs have been generated.  */
22429
22430 static void
22431 gen_remaining_tmpl_value_param_die_attribute (void)
22432 {
22433   if (tmpl_value_parm_die_table)
22434     {
22435       unsigned i;
22436       die_arg_entry *e;
22437
22438       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
22439         tree_add_const_value_attribute (e->die, e->arg);
22440     }
22441 }
22442
22443 /* Generate generic parameters DIEs for instances of generic types
22444    that have been previously scheduled by
22445    schedule_generic_params_dies_gen. This function must be called
22446    after all the types of the CU have been laid out.  */
22447
22448 static void
22449 gen_scheduled_generic_parms_dies (void)
22450 {
22451   unsigned i;
22452   tree t;
22453
22454   if (generic_type_instances == NULL)
22455     return;
22456   
22457   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
22458     gen_generic_params_dies (t);
22459 }
22460
22461
22462 /* Replace DW_AT_name for the decl with name.  */
22463
22464 static void
22465 dwarf2out_set_name (tree decl, tree name)
22466 {
22467   dw_die_ref die;
22468   dw_attr_ref attr;
22469   const char *dname;
22470
22471   die = TYPE_SYMTAB_DIE (decl);
22472   if (!die)
22473     return;
22474
22475   dname = dwarf2_name (name, 0);
22476   if (!dname)
22477     return;
22478
22479   attr = get_AT (die, DW_AT_name);
22480   if (attr)
22481     {
22482       struct indirect_string_node *node;
22483
22484       node = find_AT_string (dname);
22485       /* replace the string.  */
22486       attr->dw_attr_val.v.val_str = node;
22487     }
22488
22489   else
22490     add_name_attribute (die, dname);
22491 }
22492
22493 /* Called by the final INSN scan whenever we see a var location.  We
22494    use it to drop labels in the right places, and throw the location in
22495    our lookup table.  */
22496
22497 static void
22498 dwarf2out_var_location (rtx loc_note)
22499 {
22500   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22501   struct var_loc_node *newloc;
22502   rtx next_real;
22503   static const char *last_label;
22504   static const char *last_postcall_label;
22505   static bool last_in_cold_section_p;
22506   tree decl;
22507   bool var_loc_p;
22508
22509   if (!NOTE_P (loc_note))
22510     {
22511       if (CALL_P (loc_note))
22512         {
22513           call_site_count++;
22514           if (SIBLING_CALL_P (loc_note))
22515             tail_call_site_count++;
22516         }
22517       return;
22518     }
22519
22520   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22521   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22522     return;
22523
22524   next_real = next_real_insn (loc_note);
22525
22526   /* If there are no instructions which would be affected by this note,
22527      don't do anything.  */
22528   if (var_loc_p
22529       && next_real == NULL_RTX
22530       && !NOTE_DURING_CALL_P (loc_note))
22531     return;
22532
22533   if (next_real == NULL_RTX)
22534     next_real = get_last_insn ();
22535
22536   /* If there were any real insns between note we processed last time
22537      and this note (or if it is the first note), clear
22538      last_{,postcall_}label so that they are not reused this time.  */
22539   if (last_var_location_insn == NULL_RTX
22540       || last_var_location_insn != next_real
22541       || last_in_cold_section_p != in_cold_section_p)
22542     {
22543       last_label = NULL;
22544       last_postcall_label = NULL;
22545     }
22546
22547   if (var_loc_p)
22548     {
22549       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22550       newloc = add_var_loc_to_decl (decl, loc_note,
22551                                     NOTE_DURING_CALL_P (loc_note)
22552                                     ? last_postcall_label : last_label);
22553       if (newloc == NULL)
22554         return;
22555     }
22556   else
22557     {
22558       decl = NULL_TREE;
22559       newloc = NULL;
22560     }
22561
22562   /* If there were no real insns between note we processed last time
22563      and this note, use the label we emitted last time.  Otherwise
22564      create a new label and emit it.  */
22565   if (last_label == NULL)
22566     {
22567       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22568       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22569       loclabel_num++;
22570       last_label = ggc_strdup (loclabel);
22571     }
22572
22573   if (!var_loc_p)
22574     {
22575       struct call_arg_loc_node *ca_loc
22576         = ggc_alloc_cleared_call_arg_loc_node ();
22577       rtx prev = prev_real_insn (loc_note), x;
22578       ca_loc->call_arg_loc_note = loc_note;
22579       ca_loc->next = NULL;
22580       ca_loc->label = last_label;
22581       gcc_assert (prev
22582                   && (CALL_P (prev)
22583                       || (NONJUMP_INSN_P (prev)
22584                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22585                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22586       if (!CALL_P (prev))
22587         prev = XVECEXP (PATTERN (prev), 0, 0);
22588       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22589       x = PATTERN (prev);
22590       if (GET_CODE (x) == PARALLEL)
22591         x = XVECEXP (x, 0, 0);
22592       if (GET_CODE (x) == SET)
22593         x = SET_SRC (x);
22594       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22595         {
22596           x = XEXP (XEXP (x, 0), 0);
22597           if (GET_CODE (x) == SYMBOL_REF
22598               && SYMBOL_REF_DECL (x)
22599               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22600             ca_loc->symbol_ref = x;
22601         }
22602       ca_loc->block = insn_scope (prev);
22603       if (call_arg_locations)
22604         call_arg_loc_last->next = ca_loc;
22605       else
22606         call_arg_locations = ca_loc;
22607       call_arg_loc_last = ca_loc;
22608     }
22609   else if (!NOTE_DURING_CALL_P (loc_note))
22610     newloc->label = last_label;
22611   else
22612     {
22613       if (!last_postcall_label)
22614         {
22615           sprintf (loclabel, "%s-1", last_label);
22616           last_postcall_label = ggc_strdup (loclabel);
22617         }
22618       newloc->label = last_postcall_label;
22619     }
22620
22621   last_var_location_insn = next_real;
22622   last_in_cold_section_p = in_cold_section_p;
22623 }
22624
22625 /* Note in one location list that text section has changed.  */
22626
22627 static int
22628 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22629 {
22630   var_loc_list *list = (var_loc_list *) *slot;
22631   if (list->first)
22632     list->last_before_switch
22633       = list->last->next ? list->last->next : list->last;
22634   return 1;
22635 }
22636
22637 /* Note in all location lists that text section has changed.  */
22638
22639 static void
22640 var_location_switch_text_section (void)
22641 {
22642   if (decl_loc_table == NULL)
22643     return;
22644
22645   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22646 }
22647
22648 /* Create a new line number table.  */
22649
22650 static dw_line_info_table *
22651 new_line_info_table (void)
22652 {
22653   dw_line_info_table *table;
22654
22655   table = ggc_alloc_cleared_dw_line_info_table_struct ();
22656   table->file_num = 1;
22657   table->line_num = 1;
22658   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22659
22660   return table;
22661 }
22662
22663 /* Lookup the "current" table into which we emit line info, so
22664    that we don't have to do it for every source line.  */
22665
22666 static void
22667 set_cur_line_info_table (section *sec)
22668 {
22669   dw_line_info_table *table;
22670
22671   if (sec == text_section)
22672     table = text_section_line_info;
22673   else if (sec == cold_text_section)
22674     {
22675       table = cold_text_section_line_info;
22676       if (!table)
22677         {
22678           cold_text_section_line_info = table = new_line_info_table ();
22679           table->end_label = cold_end_label;
22680         }
22681     }
22682   else
22683     {
22684       const char *end_label;
22685
22686       if (flag_reorder_blocks_and_partition)
22687         {
22688           if (in_cold_section_p)
22689             end_label = crtl->subsections.cold_section_end_label;
22690           else
22691             end_label = crtl->subsections.hot_section_end_label;
22692         }
22693       else
22694         {
22695           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22696           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22697                                        current_function_funcdef_no);
22698           end_label = ggc_strdup (label);
22699         }
22700
22701       table = new_line_info_table ();
22702       table->end_label = end_label;
22703
22704       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
22705     }
22706
22707   cur_line_info_table = table;
22708 }
22709
22710
22711 /* We need to reset the locations at the beginning of each
22712    function. We can't do this in the end_function hook, because the
22713    declarations that use the locations won't have been output when
22714    that hook is called.  Also compute have_multiple_function_sections here.  */
22715
22716 static void
22717 dwarf2out_begin_function (tree fun)
22718 {
22719   section *sec = function_section (fun);
22720
22721   if (sec != text_section)
22722     have_multiple_function_sections = true;
22723
22724   if (flag_reorder_blocks_and_partition && !cold_text_section)
22725     {
22726       gcc_assert (current_function_decl == fun);
22727       cold_text_section = unlikely_text_section ();
22728       switch_to_section (cold_text_section);
22729       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22730       switch_to_section (sec);
22731     }
22732
22733   dwarf2out_note_section_used ();
22734   call_site_count = 0;
22735   tail_call_site_count = 0;
22736
22737   set_cur_line_info_table (sec);
22738 }
22739
22740 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22741
22742 static void
22743 push_dw_line_info_entry (dw_line_info_table *table,
22744                          enum dw_line_info_opcode opcode, unsigned int val)
22745 {
22746   dw_line_info_entry e;
22747   e.opcode = opcode;
22748   e.val = val;
22749   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
22750 }
22751
22752 /* Output a label to mark the beginning of a source code line entry
22753    and record information relating to this source line, in
22754    'line_info_table' for later output of the .debug_line section.  */
22755 /* ??? The discriminator parameter ought to be unsigned.  */
22756
22757 static void
22758 dwarf2out_source_line (unsigned int line, const char *filename,
22759                        int discriminator, bool is_stmt)
22760 {
22761   unsigned int file_num;
22762   dw_line_info_table *table;
22763
22764   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
22765     return;
22766
22767   /* The discriminator column was added in dwarf4.  Simplify the below
22768      by simply removing it if we're not supposed to output it.  */
22769   if (dwarf_version < 4 && dwarf_strict)
22770     discriminator = 0;
22771
22772   table = cur_line_info_table;
22773   file_num = maybe_emit_file (lookup_filename (filename));
22774
22775   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22776      the debugger has used the second (possibly duplicate) line number
22777      at the beginning of the function to mark the end of the prologue.
22778      We could eliminate any other duplicates within the function.  For
22779      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22780      that second line number entry.  */
22781   /* Recall that this end-of-prologue indication is *not* the same thing
22782      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22783      to which the hook corresponds, follows the last insn that was 
22784      emitted by gen_prologue.  What we need is to preceed the first insn
22785      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22786      insn that corresponds to something the user wrote.  These may be
22787      very different locations once scheduling is enabled.  */
22788
22789   if (0 && file_num == table->file_num
22790       && line == table->line_num
22791       && discriminator == table->discrim_num
22792       && is_stmt == table->is_stmt)
22793     return;
22794
22795   switch_to_section (current_function_section ());
22796
22797   /* If requested, emit something human-readable.  */
22798   if (flag_debug_asm)
22799     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22800
22801   if (DWARF2_ASM_LINE_DEBUG_INFO)
22802     {
22803       /* Emit the .loc directive understood by GNU as.  */
22804       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22805       if (is_stmt != table->is_stmt)
22806         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22807       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22808         fprintf (asm_out_file, " discriminator %d", discriminator);
22809       fputc ('\n', asm_out_file);
22810     }
22811   else
22812     {
22813       unsigned int label_num = ++line_info_label_num;
22814
22815       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22816
22817       push_dw_line_info_entry (table, LI_set_address, label_num);
22818       if (file_num != table->file_num)
22819         push_dw_line_info_entry (table, LI_set_file, file_num);
22820       if (discriminator != table->discrim_num)
22821         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22822       if (is_stmt != table->is_stmt)
22823         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22824       push_dw_line_info_entry (table, LI_set_line, line);
22825     }
22826
22827   table->file_num = file_num;
22828   table->line_num = line;
22829   table->discrim_num = discriminator;
22830   table->is_stmt = is_stmt;
22831   table->in_use = true;
22832 }
22833
22834 /* Record the beginning of a new source file.  */
22835
22836 static void
22837 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22838 {
22839   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22840     {
22841       /* Record the beginning of the file for break_out_includes.  */
22842       dw_die_ref bincl_die;
22843
22844       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22845       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22846     }
22847
22848   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22849     {
22850       macinfo_entry e;
22851       e.code = DW_MACINFO_start_file;
22852       e.lineno = lineno;
22853       e.info = xstrdup (filename);
22854       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22855     }
22856 }
22857
22858 /* Record the end of a source file.  */
22859
22860 static void
22861 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22862 {
22863   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22864     /* Record the end of the file for break_out_includes.  */
22865     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22866
22867   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22868     {
22869       macinfo_entry e;
22870       e.code = DW_MACINFO_end_file;
22871       e.lineno = lineno;
22872       e.info = NULL;
22873       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22874     }
22875 }
22876
22877 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22878    the tail part of the directive line, i.e. the part which is past the
22879    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22880
22881 static void
22882 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22883                   const char *buffer ATTRIBUTE_UNUSED)
22884 {
22885   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22886     {
22887       macinfo_entry e;
22888       e.code = DW_MACINFO_define;
22889       e.lineno = lineno;
22890       e.info = xstrdup (buffer);;
22891       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22892     }
22893 }
22894
22895 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22896    the tail part of the directive line, i.e. the part which is past the
22897    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22898
22899 static void
22900 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22901                  const char *buffer ATTRIBUTE_UNUSED)
22902 {
22903   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22904     {
22905       macinfo_entry e;
22906       e.code = DW_MACINFO_undef;
22907       e.lineno = lineno;
22908       e.info = xstrdup (buffer);;
22909       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22910     }
22911 }
22912
22913 static void
22914 output_macinfo (void)
22915 {
22916   unsigned i;
22917   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22918   macinfo_entry *ref;
22919
22920   if (! length)
22921     return;
22922
22923   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22924     {
22925       switch (ref->code)
22926         {
22927           case DW_MACINFO_start_file:
22928             {
22929               int file_num = maybe_emit_file (lookup_filename (ref->info));
22930               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22931               dw2_asm_output_data_uleb128 
22932                         (ref->lineno, "Included from line number %lu", 
22933                                                 (unsigned long)ref->lineno);
22934               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22935             }
22936             break;
22937           case DW_MACINFO_end_file:
22938             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22939             break;
22940           case DW_MACINFO_define:
22941             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22942             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22943                                                 (unsigned long)ref->lineno);
22944             dw2_asm_output_nstring (ref->info, -1, "The macro");
22945             break;
22946           case DW_MACINFO_undef:
22947             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22948             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22949                                                 (unsigned long)ref->lineno);
22950             dw2_asm_output_nstring (ref->info, -1, "The macro");
22951             break;
22952           default:
22953            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22954              ASM_COMMENT_START, (unsigned long)ref->code);
22955           break;
22956         }
22957     }
22958 }
22959
22960 /* Set up for Dwarf output at the start of compilation.  */
22961
22962 static void
22963 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22964 {
22965   /* Allocate the file_table.  */
22966   file_table = htab_create_ggc (50, file_table_hash,
22967                                 file_table_eq, NULL);
22968
22969   /* Allocate the decl_die_table.  */
22970   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22971                                     decl_die_table_eq, NULL);
22972
22973   /* Allocate the decl_loc_table.  */
22974   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22975                                     decl_loc_table_eq, NULL);
22976
22977   /* Allocate the cached_dw_loc_list_table.  */
22978   cached_dw_loc_list_table
22979     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22980                        cached_dw_loc_list_table_eq, NULL);
22981
22982   /* Allocate the initial hunk of the decl_scope_table.  */
22983   decl_scope_table = VEC_alloc (tree, gc, 256);
22984
22985   /* Allocate the initial hunk of the abbrev_die_table.  */
22986   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22987     (ABBREV_DIE_TABLE_INCREMENT);
22988   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22989   /* Zero-th entry is allocated, but unused.  */
22990   abbrev_die_table_in_use = 1;
22991
22992   /* Allocate the pubtypes and pubnames vectors.  */
22993   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22994   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22995
22996   incomplete_types = VEC_alloc (tree, gc, 64);
22997
22998   used_rtx_array = VEC_alloc (rtx, gc, 32);
22999
23000   debug_info_section = get_section (DEBUG_INFO_SECTION,
23001                                     SECTION_DEBUG, NULL);
23002   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23003                                       SECTION_DEBUG, NULL);
23004   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23005                                        SECTION_DEBUG, NULL);
23006   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
23007                                        SECTION_DEBUG, NULL);
23008   debug_line_section = get_section (DEBUG_LINE_SECTION,
23009                                     SECTION_DEBUG, NULL);
23010   debug_loc_section = get_section (DEBUG_LOC_SECTION,
23011                                    SECTION_DEBUG, NULL);
23012   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23013                                         SECTION_DEBUG, NULL);
23014   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23015                                         SECTION_DEBUG, NULL);
23016   debug_str_section = get_section (DEBUG_STR_SECTION,
23017                                    DEBUG_STR_SECTION_FLAGS, NULL);
23018   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23019                                       SECTION_DEBUG, NULL);
23020   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23021                                      SECTION_DEBUG, NULL);
23022
23023   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23024   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23025                                DEBUG_ABBREV_SECTION_LABEL, 0);
23026   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23027   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23028                                COLD_TEXT_SECTION_LABEL, 0);
23029   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23030
23031   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23032                                DEBUG_INFO_SECTION_LABEL, 0);
23033   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23034                                DEBUG_LINE_SECTION_LABEL, 0);
23035   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23036                                DEBUG_RANGES_SECTION_LABEL, 0);
23037   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23038                                DEBUG_MACINFO_SECTION_LABEL, 0);
23039
23040   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23041     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
23042
23043   switch_to_section (text_section);
23044   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23045
23046   /* Make sure the line number table for .text always exists.  */
23047   text_section_line_info = new_line_info_table ();
23048   text_section_line_info->end_label = text_end_label;
23049 }
23050
23051 /* Called before cgraph_optimize starts outputtting functions, variables
23052    and toplevel asms into assembly.  */
23053
23054 static void
23055 dwarf2out_assembly_start (void)
23056 {
23057   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23058       && dwarf2out_do_cfi_asm ()
23059       && (!(flag_unwind_tables || flag_exceptions)
23060           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
23061     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23062 }
23063
23064 /* A helper function for dwarf2out_finish called through
23065    htab_traverse.  Emit one queued .debug_str string.  */
23066
23067 static int
23068 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23069 {
23070   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23071
23072   if (node->label && node->refcount)
23073     {
23074       switch_to_section (debug_str_section);
23075       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23076       assemble_string (node->str, strlen (node->str) + 1);
23077     }
23078
23079   return 1;
23080 }
23081
23082 #if ENABLE_ASSERT_CHECKING
23083 /* Verify that all marks are clear.  */
23084
23085 static void
23086 verify_marks_clear (dw_die_ref die)
23087 {
23088   dw_die_ref c;
23089
23090   gcc_assert (! die->die_mark);
23091   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23092 }
23093 #endif /* ENABLE_ASSERT_CHECKING */
23094
23095 /* Clear the marks for a die and its children.
23096    Be cool if the mark isn't set.  */
23097
23098 static void
23099 prune_unmark_dies (dw_die_ref die)
23100 {
23101   dw_die_ref c;
23102
23103   if (die->die_mark)
23104     die->die_mark = 0;
23105   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23106 }
23107
23108 /* Given DIE that we're marking as used, find any other dies
23109    it references as attributes and mark them as used.  */
23110
23111 static void
23112 prune_unused_types_walk_attribs (dw_die_ref die)
23113 {
23114   dw_attr_ref a;
23115   unsigned ix;
23116
23117   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23118     {
23119       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23120         {
23121           /* A reference to another DIE.
23122              Make sure that it will get emitted.
23123              If it was broken out into a comdat group, don't follow it.  */
23124           if (! use_debug_types
23125               || a->dw_attr == DW_AT_specification
23126               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
23127             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23128         }
23129       /* Set the string's refcount to 0 so that prune_unused_types_mark
23130          accounts properly for it.  */
23131       if (AT_class (a) == dw_val_class_str)
23132         a->dw_attr_val.v.val_str->refcount = 0;
23133     }
23134 }
23135
23136 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23137
23138 static void
23139 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23140 {
23141   dw_die_ref c;
23142
23143   if (die == NULL || die->die_child == NULL)
23144     return;
23145   c = die->die_child;
23146   do
23147     {
23148       switch (c->die_tag)
23149         {
23150         case DW_TAG_template_type_param:
23151         case DW_TAG_template_value_param:
23152         case DW_TAG_GNU_template_template_param:
23153         case DW_TAG_GNU_template_parameter_pack:
23154           prune_unused_types_mark (c, 1);
23155           break;
23156         default:
23157           break;
23158         }
23159       c = c->die_sib;
23160     } while (c && c != die->die_child);
23161 }
23162
23163 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23164    to DIE's children.  */
23165
23166 static void
23167 prune_unused_types_mark (dw_die_ref die, int dokids)
23168 {
23169   dw_die_ref c;
23170
23171   if (die->die_mark == 0)
23172     {
23173       /* We haven't done this node yet.  Mark it as used.  */
23174       die->die_mark = 1;
23175       /* If this is the DIE of a generic type instantiation,
23176          mark the children DIEs that describe its generic parms and
23177          args.  */
23178       prune_unused_types_mark_generic_parms_dies (die);
23179
23180       /* We also have to mark its parents as used.
23181          (But we don't want to mark our parents' kids due to this.)  */
23182       if (die->die_parent)
23183         prune_unused_types_mark (die->die_parent, 0);
23184
23185       /* Mark any referenced nodes.  */
23186       prune_unused_types_walk_attribs (die);
23187
23188       /* If this node is a specification,
23189          also mark the definition, if it exists.  */
23190       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23191         prune_unused_types_mark (die->die_definition, 1);
23192     }
23193
23194   if (dokids && die->die_mark != 2)
23195     {
23196       /* We need to walk the children, but haven't done so yet.
23197          Remember that we've walked the kids.  */
23198       die->die_mark = 2;
23199
23200       /* If this is an array type, we need to make sure our
23201          kids get marked, even if they're types.  If we're
23202          breaking out types into comdat sections, do this
23203          for all type definitions.  */
23204       if (die->die_tag == DW_TAG_array_type
23205           || (use_debug_types
23206               && is_type_die (die) && ! is_declaration_die (die)))
23207         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23208       else
23209         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23210     }
23211 }
23212
23213 /* For local classes, look if any static member functions were emitted
23214    and if so, mark them.  */
23215
23216 static void
23217 prune_unused_types_walk_local_classes (dw_die_ref die)
23218 {
23219   dw_die_ref c;
23220
23221   if (die->die_mark == 2)
23222     return;
23223
23224   switch (die->die_tag)
23225     {
23226     case DW_TAG_structure_type:
23227     case DW_TAG_union_type:
23228     case DW_TAG_class_type:
23229       break;
23230
23231     case DW_TAG_subprogram:
23232       if (!get_AT_flag (die, DW_AT_declaration)
23233           || die->die_definition != NULL)
23234         prune_unused_types_mark (die, 1);
23235       return;
23236
23237     default:
23238       return;
23239     }
23240
23241   /* Mark children.  */
23242   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23243 }
23244
23245 /* Walk the tree DIE and mark types that we actually use.  */
23246
23247 static void
23248 prune_unused_types_walk (dw_die_ref die)
23249 {
23250   dw_die_ref c;
23251
23252   /* Don't do anything if this node is already marked and
23253      children have been marked as well.  */
23254   if (die->die_mark == 2)
23255     return;
23256
23257   switch (die->die_tag)
23258     {
23259     case DW_TAG_structure_type:
23260     case DW_TAG_union_type:
23261     case DW_TAG_class_type:
23262       if (die->die_perennial_p)
23263         break;
23264
23265       for (c = die->die_parent; c; c = c->die_parent)
23266         if (c->die_tag == DW_TAG_subprogram)
23267           break;
23268
23269       /* Finding used static member functions inside of classes
23270          is needed just for local classes, because for other classes
23271          static member function DIEs with DW_AT_specification
23272          are emitted outside of the DW_TAG_*_type.  If we ever change
23273          it, we'd need to call this even for non-local classes.  */
23274       if (c)
23275         prune_unused_types_walk_local_classes (die);
23276
23277       /* It's a type node --- don't mark it.  */
23278       return;
23279
23280     case DW_TAG_const_type:
23281     case DW_TAG_packed_type:
23282     case DW_TAG_pointer_type:
23283     case DW_TAG_reference_type:
23284     case DW_TAG_rvalue_reference_type:
23285     case DW_TAG_volatile_type:
23286     case DW_TAG_typedef:
23287     case DW_TAG_array_type:
23288     case DW_TAG_interface_type:
23289     case DW_TAG_friend:
23290     case DW_TAG_variant_part:
23291     case DW_TAG_enumeration_type:
23292     case DW_TAG_subroutine_type:
23293     case DW_TAG_string_type:
23294     case DW_TAG_set_type:
23295     case DW_TAG_subrange_type:
23296     case DW_TAG_ptr_to_member_type:
23297     case DW_TAG_file_type:
23298       if (die->die_perennial_p)
23299         break;
23300
23301       /* It's a type node --- don't mark it.  */
23302       return;
23303
23304     default:
23305       /* Mark everything else.  */
23306       break;
23307   }
23308
23309   if (die->die_mark == 0)
23310     {
23311       die->die_mark = 1;
23312
23313       /* Now, mark any dies referenced from here.  */
23314       prune_unused_types_walk_attribs (die);
23315     }
23316
23317   die->die_mark = 2;
23318
23319   /* Mark children.  */
23320   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23321 }
23322
23323 /* Increment the string counts on strings referred to from DIE's
23324    attributes.  */
23325
23326 static void
23327 prune_unused_types_update_strings (dw_die_ref die)
23328 {
23329   dw_attr_ref a;
23330   unsigned ix;
23331
23332   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23333     if (AT_class (a) == dw_val_class_str)
23334       {
23335         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23336         s->refcount++;
23337         /* Avoid unnecessarily putting strings that are used less than
23338            twice in the hash table.  */
23339         if (s->refcount
23340             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23341           {
23342             void ** slot;
23343             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
23344                                              htab_hash_string (s->str),
23345                                              INSERT);
23346             gcc_assert (*slot == NULL);
23347             *slot = s;
23348           }
23349       }
23350 }
23351
23352 /* Remove from the tree DIE any dies that aren't marked.  */
23353
23354 static void
23355 prune_unused_types_prune (dw_die_ref die)
23356 {
23357   dw_die_ref c;
23358
23359   gcc_assert (die->die_mark);
23360   prune_unused_types_update_strings (die);
23361
23362   if (! die->die_child)
23363     return;
23364
23365   c = die->die_child;
23366   do {
23367     dw_die_ref prev = c;
23368     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23369       if (c == die->die_child)
23370         {
23371           /* No marked children between 'prev' and the end of the list.  */
23372           if (prev == c)
23373             /* No marked children at all.  */
23374             die->die_child = NULL;
23375           else
23376             {
23377               prev->die_sib = c->die_sib;
23378               die->die_child = prev;
23379             }
23380           return;
23381         }
23382
23383     if (c != prev->die_sib)
23384       prev->die_sib = c;
23385     prune_unused_types_prune (c);
23386   } while (c != die->die_child);
23387 }
23388
23389 /* A helper function for dwarf2out_finish called through
23390    htab_traverse.  Clear .debug_str strings that we haven't already
23391    decided to emit.  */
23392
23393 static int
23394 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23395 {
23396   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23397
23398   if (!node->label || !node->refcount)
23399     htab_clear_slot (debug_str_hash, h);
23400
23401   return 1;
23402 }
23403
23404 /* Remove dies representing declarations that we never use.  */
23405
23406 static void
23407 prune_unused_types (void)
23408 {
23409   unsigned int i;
23410   limbo_die_node *node;
23411   comdat_type_node *ctnode;
23412   pubname_ref pub;
23413   dw_die_ref base_type;
23414
23415 #if ENABLE_ASSERT_CHECKING
23416   /* All the marks should already be clear.  */
23417   verify_marks_clear (comp_unit_die ());
23418   for (node = limbo_die_list; node; node = node->next)
23419     verify_marks_clear (node->die);
23420   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23421     verify_marks_clear (ctnode->root_die);
23422 #endif /* ENABLE_ASSERT_CHECKING */
23423
23424   /* Mark types that are used in global variables.  */
23425   premark_types_used_by_global_vars ();
23426
23427   /* Set the mark on nodes that are actually used.  */
23428   prune_unused_types_walk (comp_unit_die ());
23429   for (node = limbo_die_list; node; node = node->next)
23430     prune_unused_types_walk (node->die);
23431   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23432     {
23433       prune_unused_types_walk (ctnode->root_die);
23434       prune_unused_types_mark (ctnode->type_die, 1);
23435     }
23436
23437   /* Also set the mark on nodes referenced from the
23438      pubname_table.  */
23439   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
23440     prune_unused_types_mark (pub->die, 1);
23441   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23442     prune_unused_types_mark (base_type, 1);
23443
23444   /* Get rid of nodes that aren't marked; and update the string counts.  */
23445   if (debug_str_hash && debug_str_hash_forced)
23446     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
23447   else if (debug_str_hash)
23448     htab_empty (debug_str_hash);
23449   prune_unused_types_prune (comp_unit_die ());
23450   for (node = limbo_die_list; node; node = node->next)
23451     prune_unused_types_prune (node->die);
23452   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23453     prune_unused_types_prune (ctnode->root_die);
23454
23455   /* Leave the marks clear.  */
23456   prune_unmark_dies (comp_unit_die ());
23457   for (node = limbo_die_list; node; node = node->next)
23458     prune_unmark_dies (node->die);
23459   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23460     prune_unmark_dies (ctnode->root_die);
23461 }
23462
23463 /* Set the parameter to true if there are any relative pathnames in
23464    the file table.  */
23465 static int
23466 file_table_relative_p (void ** slot, void *param)
23467 {
23468   bool *p = (bool *) param;
23469   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
23470   if (!IS_ABSOLUTE_PATH (d->filename))
23471     {
23472       *p = true;
23473       return 0;
23474     }
23475   return 1;
23476 }
23477
23478 /* Routines to manipulate hash table of comdat type units.  */
23479
23480 static hashval_t
23481 htab_ct_hash (const void *of)
23482 {
23483   hashval_t h;
23484   const comdat_type_node *const type_node = (const comdat_type_node *) of;
23485
23486   memcpy (&h, type_node->signature, sizeof (h));
23487   return h;
23488 }
23489
23490 static int
23491 htab_ct_eq (const void *of1, const void *of2)
23492 {
23493   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
23494   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
23495
23496   return (! memcmp (type_node_1->signature, type_node_2->signature,
23497                     DWARF_TYPE_SIGNATURE_SIZE));
23498 }
23499
23500 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23501    to the location it would have been added, should we know its
23502    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23503    probably improve compactness of debug info, removing equivalent
23504    abbrevs, and hide any differences caused by deferring the
23505    computation of the assembler name, triggered by e.g. PCH.  */
23506
23507 static inline void
23508 move_linkage_attr (dw_die_ref die)
23509 {
23510   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
23511   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
23512
23513   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23514               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23515
23516   while (--ix > 0)
23517     {
23518       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
23519
23520       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23521         break;
23522     }
23523
23524   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
23525     {
23526       VEC_pop (dw_attr_node, die->die_attr);
23527       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
23528     }
23529 }
23530
23531 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23532    referenced from typed stack ops and count how often they are used.  */
23533
23534 static void
23535 mark_base_types (dw_loc_descr_ref loc)
23536 {
23537   dw_die_ref base_type = NULL;
23538
23539   for (; loc; loc = loc->dw_loc_next)
23540     {
23541       switch (loc->dw_loc_opc)
23542         {
23543         case DW_OP_GNU_regval_type:
23544         case DW_OP_GNU_deref_type:
23545           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23546           break;
23547         case DW_OP_GNU_const_type:
23548         case DW_OP_GNU_convert:
23549         case DW_OP_GNU_reinterpret:
23550           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23551           break;
23552         case DW_OP_GNU_entry_value:
23553           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23554           continue;
23555         default:
23556           continue;
23557         }
23558       gcc_assert (base_type->die_parent == comp_unit_die ());
23559       if (base_type->die_mark)
23560         base_type->die_mark++;
23561       else
23562         {
23563           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
23564           base_type->die_mark = 1;
23565         }
23566     }
23567 }
23568
23569 /* Comparison function for sorting marked base types.  */
23570
23571 static int
23572 base_type_cmp (const void *x, const void *y)
23573 {
23574   dw_die_ref dx = *(const dw_die_ref *) x;
23575   dw_die_ref dy = *(const dw_die_ref *) y;
23576   unsigned int byte_size1, byte_size2;
23577   unsigned int encoding1, encoding2;
23578   if (dx->die_mark > dy->die_mark)
23579     return -1;
23580   if (dx->die_mark < dy->die_mark)
23581     return 1;
23582   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23583   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23584   if (byte_size1 < byte_size2)
23585     return 1;
23586   if (byte_size1 > byte_size2)
23587     return -1;
23588   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23589   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23590   if (encoding1 < encoding2)
23591     return 1;
23592   if (encoding1 > encoding2)
23593     return -1;
23594   return 0;
23595 }
23596
23597 /* Move base types marked by mark_base_types as early as possible
23598    in the CU, sorted by decreasing usage count both to make the
23599    uleb128 references as small as possible and to make sure they
23600    will have die_offset already computed by calc_die_sizes when
23601    sizes of typed stack loc ops is computed.  */
23602
23603 static void
23604 move_marked_base_types (void)
23605 {
23606   unsigned int i;
23607   dw_die_ref base_type, die, c;
23608
23609   if (VEC_empty (dw_die_ref, base_types))
23610     return;
23611
23612   /* Sort by decreasing usage count, they will be added again in that
23613      order later on.  */
23614   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
23615   die = comp_unit_die ();
23616   c = die->die_child;
23617   do
23618     {
23619       dw_die_ref prev = c;
23620       c = c->die_sib;
23621       while (c->die_mark)
23622         {
23623           remove_child_with_prev (c, prev);
23624           /* As base types got marked, there must be at least
23625              one node other than DW_TAG_base_type.  */
23626           gcc_assert (c != c->die_sib);
23627           c = c->die_sib;
23628         }
23629     }
23630   while (c != die->die_child);
23631   gcc_assert (die->die_child);
23632   c = die->die_child;
23633   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23634     {
23635       base_type->die_mark = 0;
23636       base_type->die_sib = c->die_sib;
23637       c->die_sib = base_type;
23638       c = base_type;
23639     }
23640 }
23641
23642 /* Helper function for resolve_addr, attempt to resolve
23643    one CONST_STRING, return non-zero if not successful.  Similarly verify that
23644    SYMBOL_REFs refer to variables emitted in the current CU.  */
23645
23646 static int
23647 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23648 {
23649   rtx rtl = *addr;
23650
23651   if (GET_CODE (rtl) == CONST_STRING)
23652     {
23653       size_t len = strlen (XSTR (rtl, 0)) + 1;
23654       tree t = build_string (len, XSTR (rtl, 0));
23655       tree tlen = size_int (len - 1);
23656       TREE_TYPE (t)
23657         = build_array_type (char_type_node, build_index_type (tlen));
23658       rtl = lookup_constant_def (t);
23659       if (!rtl || !MEM_P (rtl))
23660         return 1;
23661       rtl = XEXP (rtl, 0);
23662       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
23663       *addr = rtl;
23664       return 0;
23665     }
23666
23667   if (GET_CODE (rtl) == SYMBOL_REF
23668       && SYMBOL_REF_DECL (rtl))
23669     {
23670       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23671         {
23672           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23673             return 1;
23674         }
23675       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23676         return 1;
23677     }
23678
23679   if (GET_CODE (rtl) == CONST
23680       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23681     return 1;
23682
23683   return 0;
23684 }
23685
23686 /* Helper function for resolve_addr, handle one location
23687    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23688    the location list couldn't be resolved.  */
23689
23690 static bool
23691 resolve_addr_in_expr (dw_loc_descr_ref loc)
23692 {
23693   for (; loc; loc = loc->dw_loc_next)
23694     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
23695          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23696         || (loc->dw_loc_opc == DW_OP_implicit_value
23697             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23698             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
23699       return false;
23700     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
23701              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23702       {
23703         dw_die_ref ref
23704           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23705         if (ref == NULL)
23706           return false;
23707         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23708         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23709         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23710       }
23711   return true;
23712 }
23713
23714 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23715    an address in .rodata section if the string literal is emitted there,
23716    or remove the containing location list or replace DW_AT_const_value
23717    with DW_AT_location and empty location expression, if it isn't found
23718    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23719    to something that has been emitted in the current CU.  */
23720
23721 static void
23722 resolve_addr (dw_die_ref die)
23723 {
23724   dw_die_ref c;
23725   dw_attr_ref a;
23726   dw_loc_list_ref *curr, *start, loc;
23727   unsigned ix;
23728
23729   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23730     switch (AT_class (a))
23731       {
23732       case dw_val_class_loc_list:
23733         start = curr = AT_loc_list_ptr (a);
23734         loc = *curr;
23735         gcc_assert (loc);
23736         /* The same list can be referenced more than once.  See if we have
23737            already recorded the result from a previous pass.  */
23738         if (loc->replaced)
23739           *curr = loc->dw_loc_next;
23740         else if (!loc->resolved_addr)
23741           {
23742             /* As things stand, we do not expect or allow one die to
23743                reference a suffix of another die's location list chain.
23744                References must be identical or completely separate.
23745                There is therefore no need to cache the result of this
23746                pass on any list other than the first; doing so
23747                would lead to unnecessary writes.  */
23748             while (*curr)
23749               {
23750                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23751                 if (!resolve_addr_in_expr ((*curr)->expr))
23752                   {
23753                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23754                     if (next && (*curr)->ll_symbol)
23755                       {
23756                         gcc_assert (!next->ll_symbol);
23757                         next->ll_symbol = (*curr)->ll_symbol;
23758                       }
23759                     *curr = next;
23760                   }
23761                 else
23762                   {
23763                     mark_base_types ((*curr)->expr);
23764                     curr = &(*curr)->dw_loc_next;
23765                   }
23766               }
23767             if (loc == *start)
23768               loc->resolved_addr = 1;
23769             else
23770               {
23771                 loc->replaced = 1;
23772                 loc->dw_loc_next = *start;
23773               }
23774           }
23775         if (!*start)
23776           {
23777             remove_AT (die, a->dw_attr);
23778             ix--;
23779           }
23780         break;
23781       case dw_val_class_loc:
23782         if (!resolve_addr_in_expr (AT_loc (a)))
23783           {
23784             remove_AT (die, a->dw_attr);
23785             ix--;
23786           }
23787         else
23788           mark_base_types (AT_loc (a));
23789         break;
23790       case dw_val_class_addr:
23791         if (a->dw_attr == DW_AT_const_value
23792             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23793           {
23794             remove_AT (die, a->dw_attr);
23795             ix--;
23796           }
23797         if (die->die_tag == DW_TAG_GNU_call_site
23798             && a->dw_attr == DW_AT_abstract_origin)
23799           {
23800             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23801             dw_die_ref tdie = lookup_decl_die (tdecl);
23802             if (tdie == NULL
23803                 && DECL_EXTERNAL (tdecl)
23804                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23805               {
23806                 force_decl_die (tdecl);
23807                 tdie = lookup_decl_die (tdecl);
23808               }
23809             if (tdie)
23810               {
23811                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23812                 a->dw_attr_val.v.val_die_ref.die = tdie;
23813                 a->dw_attr_val.v.val_die_ref.external = 0;
23814               }
23815             else
23816               {
23817                 remove_AT (die, a->dw_attr);
23818                 ix--;
23819               }
23820           }
23821         break;
23822       default:
23823         break;
23824       }
23825
23826   FOR_EACH_CHILD (die, c, resolve_addr (c));
23827 }
23828 \f
23829 /* Helper routines for optimize_location_lists.
23830    This pass tries to share identical local lists in .debug_loc
23831    section.  */
23832
23833 /* Iteratively hash operands of LOC opcode.  */
23834
23835 static inline hashval_t
23836 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23837 {
23838   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23839   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23840
23841   switch (loc->dw_loc_opc)
23842     {
23843     case DW_OP_const4u:
23844     case DW_OP_const8u:
23845       if (loc->dtprel)
23846         goto hash_addr;
23847       /* FALLTHRU */
23848     case DW_OP_const1u:
23849     case DW_OP_const1s:
23850     case DW_OP_const2u:
23851     case DW_OP_const2s:
23852     case DW_OP_const4s:
23853     case DW_OP_const8s:
23854     case DW_OP_constu:
23855     case DW_OP_consts:
23856     case DW_OP_pick:
23857     case DW_OP_plus_uconst:
23858     case DW_OP_breg0:
23859     case DW_OP_breg1:
23860     case DW_OP_breg2:
23861     case DW_OP_breg3:
23862     case DW_OP_breg4:
23863     case DW_OP_breg5:
23864     case DW_OP_breg6:
23865     case DW_OP_breg7:
23866     case DW_OP_breg8:
23867     case DW_OP_breg9:
23868     case DW_OP_breg10:
23869     case DW_OP_breg11:
23870     case DW_OP_breg12:
23871     case DW_OP_breg13:
23872     case DW_OP_breg14:
23873     case DW_OP_breg15:
23874     case DW_OP_breg16:
23875     case DW_OP_breg17:
23876     case DW_OP_breg18:
23877     case DW_OP_breg19:
23878     case DW_OP_breg20:
23879     case DW_OP_breg21:
23880     case DW_OP_breg22:
23881     case DW_OP_breg23:
23882     case DW_OP_breg24:
23883     case DW_OP_breg25:
23884     case DW_OP_breg26:
23885     case DW_OP_breg27:
23886     case DW_OP_breg28:
23887     case DW_OP_breg29:
23888     case DW_OP_breg30:
23889     case DW_OP_breg31:
23890     case DW_OP_regx:
23891     case DW_OP_fbreg:
23892     case DW_OP_piece:
23893     case DW_OP_deref_size:
23894     case DW_OP_xderef_size:
23895       hash = iterative_hash_object (val1->v.val_int, hash);
23896       break;
23897     case DW_OP_skip:
23898     case DW_OP_bra:
23899       {
23900         int offset;
23901
23902         gcc_assert (val1->val_class == dw_val_class_loc);
23903         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23904         hash = iterative_hash_object (offset, hash);
23905       }
23906       break;
23907     case DW_OP_implicit_value:
23908       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23909       switch (val2->val_class)
23910         {
23911         case dw_val_class_const:
23912           hash = iterative_hash_object (val2->v.val_int, hash);
23913           break;
23914         case dw_val_class_vec:
23915           {
23916             unsigned int elt_size = val2->v.val_vec.elt_size;
23917             unsigned int len = val2->v.val_vec.length;
23918
23919             hash = iterative_hash_object (elt_size, hash);
23920             hash = iterative_hash_object (len, hash);
23921             hash = iterative_hash (val2->v.val_vec.array,
23922                                    len * elt_size, hash);
23923           }
23924           break;
23925         case dw_val_class_const_double:
23926           hash = iterative_hash_object (val2->v.val_double.low, hash);
23927           hash = iterative_hash_object (val2->v.val_double.high, hash);
23928           break;
23929         case dw_val_class_addr:
23930           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23931           break;
23932         default:
23933           gcc_unreachable ();
23934         }
23935       break;
23936     case DW_OP_bregx:
23937     case DW_OP_bit_piece:
23938       hash = iterative_hash_object (val1->v.val_int, hash);
23939       hash = iterative_hash_object (val2->v.val_int, hash);
23940       break;
23941     case DW_OP_addr:
23942     hash_addr:
23943       if (loc->dtprel)
23944         {
23945           unsigned char dtprel = 0xd1;
23946           hash = iterative_hash_object (dtprel, hash);
23947         }
23948       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23949       break;
23950     case DW_OP_GNU_implicit_pointer:
23951       hash = iterative_hash_object (val2->v.val_int, hash);
23952       break;
23953     case DW_OP_GNU_entry_value:
23954       hash = hash_loc_operands (val1->v.val_loc, hash);
23955       break;
23956     case DW_OP_GNU_regval_type:
23957     case DW_OP_GNU_deref_type:
23958       {
23959         unsigned int byte_size
23960           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23961         unsigned int encoding
23962           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23963         hash = iterative_hash_object (val1->v.val_int, hash);
23964         hash = iterative_hash_object (byte_size, hash);
23965         hash = iterative_hash_object (encoding, hash);
23966       }
23967       break;
23968     case DW_OP_GNU_convert:
23969     case DW_OP_GNU_reinterpret:
23970     case DW_OP_GNU_const_type:
23971       {
23972         unsigned int byte_size
23973           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23974         unsigned int encoding
23975           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23976         hash = iterative_hash_object (byte_size, hash);
23977         hash = iterative_hash_object (encoding, hash);
23978         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23979           break;
23980         hash = iterative_hash_object (val2->val_class, hash);
23981         switch (val2->val_class)
23982           {
23983           case dw_val_class_const:
23984             hash = iterative_hash_object (val2->v.val_int, hash);
23985             break;
23986           case dw_val_class_vec:
23987             {
23988               unsigned int elt_size = val2->v.val_vec.elt_size;
23989               unsigned int len = val2->v.val_vec.length;
23990
23991               hash = iterative_hash_object (elt_size, hash);
23992               hash = iterative_hash_object (len, hash);
23993               hash = iterative_hash (val2->v.val_vec.array,
23994                                      len * elt_size, hash);
23995             }
23996             break;
23997           case dw_val_class_const_double:
23998             hash = iterative_hash_object (val2->v.val_double.low, hash);
23999             hash = iterative_hash_object (val2->v.val_double.high, hash);
24000             break;
24001           default:
24002             gcc_unreachable ();
24003           }
24004       }
24005       break;
24006
24007     default:
24008       /* Other codes have no operands.  */
24009       break;
24010     }
24011   return hash;
24012 }
24013
24014 /* Iteratively hash the whole DWARF location expression LOC.  */
24015
24016 static inline hashval_t
24017 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
24018 {
24019   dw_loc_descr_ref l;
24020   bool sizes_computed = false;
24021   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24022   size_of_locs (loc);
24023
24024   for (l = loc; l != NULL; l = l->dw_loc_next)
24025     {
24026       enum dwarf_location_atom opc = l->dw_loc_opc;
24027       hash = iterative_hash_object (opc, hash);
24028       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24029         {
24030           size_of_locs (loc);
24031           sizes_computed = true;
24032         }
24033       hash = hash_loc_operands (l, hash);
24034     }
24035   return hash;
24036 }
24037
24038 /* Compute hash of the whole location list LIST_HEAD.  */
24039
24040 static inline void
24041 hash_loc_list (dw_loc_list_ref list_head)
24042 {
24043   dw_loc_list_ref curr = list_head;
24044   hashval_t hash = 0;
24045
24046   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24047     {
24048       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
24049       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
24050       if (curr->section)
24051         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
24052                                hash);
24053       hash = hash_locs (curr->expr, hash);
24054     }
24055   list_head->hash = hash;
24056 }
24057
24058 /* Return true if X and Y opcodes have the same operands.  */
24059
24060 static inline bool
24061 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24062 {
24063   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24064   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24065   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24066   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24067
24068   switch (x->dw_loc_opc)
24069     {
24070     case DW_OP_const4u:
24071     case DW_OP_const8u:
24072       if (x->dtprel)
24073         goto hash_addr;
24074       /* FALLTHRU */
24075     case DW_OP_const1u:
24076     case DW_OP_const1s:
24077     case DW_OP_const2u:
24078     case DW_OP_const2s:
24079     case DW_OP_const4s:
24080     case DW_OP_const8s:
24081     case DW_OP_constu:
24082     case DW_OP_consts:
24083     case DW_OP_pick:
24084     case DW_OP_plus_uconst:
24085     case DW_OP_breg0:
24086     case DW_OP_breg1:
24087     case DW_OP_breg2:
24088     case DW_OP_breg3:
24089     case DW_OP_breg4:
24090     case DW_OP_breg5:
24091     case DW_OP_breg6:
24092     case DW_OP_breg7:
24093     case DW_OP_breg8:
24094     case DW_OP_breg9:
24095     case DW_OP_breg10:
24096     case DW_OP_breg11:
24097     case DW_OP_breg12:
24098     case DW_OP_breg13:
24099     case DW_OP_breg14:
24100     case DW_OP_breg15:
24101     case DW_OP_breg16:
24102     case DW_OP_breg17:
24103     case DW_OP_breg18:
24104     case DW_OP_breg19:
24105     case DW_OP_breg20:
24106     case DW_OP_breg21:
24107     case DW_OP_breg22:
24108     case DW_OP_breg23:
24109     case DW_OP_breg24:
24110     case DW_OP_breg25:
24111     case DW_OP_breg26:
24112     case DW_OP_breg27:
24113     case DW_OP_breg28:
24114     case DW_OP_breg29:
24115     case DW_OP_breg30:
24116     case DW_OP_breg31:
24117     case DW_OP_regx:
24118     case DW_OP_fbreg:
24119     case DW_OP_piece:
24120     case DW_OP_deref_size:
24121     case DW_OP_xderef_size:
24122       return valx1->v.val_int == valy1->v.val_int;
24123     case DW_OP_skip:
24124     case DW_OP_bra:
24125       gcc_assert (valx1->val_class == dw_val_class_loc
24126                   && valy1->val_class == dw_val_class_loc
24127                   && x->dw_loc_addr == y->dw_loc_addr);
24128       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24129     case DW_OP_implicit_value:
24130       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24131           || valx2->val_class != valy2->val_class)
24132         return false;
24133       switch (valx2->val_class)
24134         {
24135         case dw_val_class_const:
24136           return valx2->v.val_int == valy2->v.val_int;
24137         case dw_val_class_vec:
24138           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24139                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24140                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24141                             valx2->v.val_vec.elt_size
24142                             * valx2->v.val_vec.length) == 0;
24143         case dw_val_class_const_double:
24144           return valx2->v.val_double.low == valy2->v.val_double.low
24145                  && valx2->v.val_double.high == valy2->v.val_double.high;
24146         case dw_val_class_addr:
24147           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24148         default:
24149           gcc_unreachable ();
24150         }
24151     case DW_OP_bregx:
24152     case DW_OP_bit_piece:
24153       return valx1->v.val_int == valy1->v.val_int
24154              && valx2->v.val_int == valy2->v.val_int;
24155     case DW_OP_addr:
24156     hash_addr:
24157       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24158     case DW_OP_GNU_implicit_pointer:
24159       return valx1->val_class == dw_val_class_die_ref
24160              && valx1->val_class == valy1->val_class
24161              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24162              && valx2->v.val_int == valy2->v.val_int;
24163     case DW_OP_GNU_entry_value:
24164       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24165     case DW_OP_GNU_const_type:
24166       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24167           || valx2->val_class != valy2->val_class)
24168         return false;
24169       switch (valx2->val_class)
24170         {
24171         case dw_val_class_const:
24172           return valx2->v.val_int == valy2->v.val_int;
24173         case dw_val_class_vec:
24174           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24175                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24176                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24177                             valx2->v.val_vec.elt_size
24178                             * valx2->v.val_vec.length) == 0;
24179         case dw_val_class_const_double:
24180           return valx2->v.val_double.low == valy2->v.val_double.low
24181                  && valx2->v.val_double.high == valy2->v.val_double.high;
24182         default:
24183           gcc_unreachable ();
24184         }
24185     case DW_OP_GNU_regval_type:
24186     case DW_OP_GNU_deref_type:
24187       return valx1->v.val_int == valy1->v.val_int
24188              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24189     case DW_OP_GNU_convert:
24190     case DW_OP_GNU_reinterpret:
24191       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24192     default:
24193       /* Other codes have no operands.  */
24194       return true;
24195     }
24196 }
24197
24198 /* Return true if DWARF location expressions X and Y are the same.  */
24199
24200 static inline bool
24201 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24202 {
24203   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24204     if (x->dw_loc_opc != y->dw_loc_opc
24205         || x->dtprel != y->dtprel
24206         || !compare_loc_operands (x, y))
24207       break;
24208   return x == NULL && y == NULL;
24209 }
24210
24211 /* Return precomputed hash of location list X.  */
24212
24213 static hashval_t
24214 loc_list_hash (const void *x)
24215 {
24216   return ((const struct dw_loc_list_struct *) x)->hash;
24217 }
24218
24219 /* Return 1 if location lists X and Y are the same.  */
24220
24221 static int
24222 loc_list_eq (const void *x, const void *y)
24223 {
24224   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
24225   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
24226   if (a == b)
24227     return 1;
24228   if (a->hash != b->hash)
24229     return 0;
24230   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24231     if (strcmp (a->begin, b->begin) != 0
24232         || strcmp (a->end, b->end) != 0
24233         || (a->section == NULL) != (b->section == NULL)
24234         || (a->section && strcmp (a->section, b->section) != 0)
24235         || !compare_locs (a->expr, b->expr))
24236       break;
24237   return a == NULL && b == NULL;
24238 }
24239
24240 /* Recursively optimize location lists referenced from DIE
24241    children and share them whenever possible.  */
24242
24243 static void
24244 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
24245 {
24246   dw_die_ref c;
24247   dw_attr_ref a;
24248   unsigned ix;
24249   void **slot;
24250
24251   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24252     if (AT_class (a) == dw_val_class_loc_list)
24253       {
24254         dw_loc_list_ref list = AT_loc_list (a);
24255         /* TODO: perform some optimizations here, before hashing
24256            it and storing into the hash table.  */
24257         hash_loc_list (list);
24258         slot = htab_find_slot_with_hash (htab, list, list->hash,
24259                                          INSERT);
24260         if (*slot == NULL)
24261           *slot = (void *) list;
24262         else
24263           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
24264       }
24265
24266   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24267 }
24268
24269 /* Optimize location lists referenced from DIE
24270    children and share them whenever possible.  */
24271
24272 static void
24273 optimize_location_lists (dw_die_ref die)
24274 {
24275   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
24276   optimize_location_lists_1 (die, htab);
24277   htab_delete (htab);
24278 }
24279 \f
24280 /* Output stuff that dwarf requires at the end of every file,
24281    and generate the DWARF-2 debugging info.  */
24282
24283 static void
24284 dwarf2out_finish (const char *filename)
24285 {
24286   limbo_die_node *node, *next_node;
24287   comdat_type_node *ctnode;
24288   htab_t comdat_type_table;
24289   unsigned int i;
24290
24291   gen_scheduled_generic_parms_dies ();
24292   gen_remaining_tmpl_value_param_die_attribute ();
24293
24294   /* Add the name for the main input file now.  We delayed this from
24295      dwarf2out_init to avoid complications with PCH.  */
24296   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24297   if (!IS_ABSOLUTE_PATH (filename))
24298     add_comp_dir_attribute (comp_unit_die ());
24299   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24300     {
24301       bool p = false;
24302       htab_traverse (file_table, file_table_relative_p, &p);
24303       if (p)
24304         add_comp_dir_attribute (comp_unit_die ());
24305     }
24306
24307   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
24308     {
24309       add_location_or_const_value_attribute (
24310         VEC_index (deferred_locations, deferred_locations_list, i)->die,
24311         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
24312         false,
24313         DW_AT_location);
24314     }
24315
24316   /* Traverse the limbo die list, and add parent/child links.  The only
24317      dies without parents that should be here are concrete instances of
24318      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24319      For concrete instances, we can get the parent die from the abstract
24320      instance.  */
24321   for (node = limbo_die_list; node; node = next_node)
24322     {
24323       dw_die_ref die = node->die;
24324       next_node = node->next;
24325
24326       if (die->die_parent == NULL)
24327         {
24328           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24329
24330           if (origin)
24331             add_child_die (origin->die_parent, die);
24332           else if (is_cu_die (die))
24333             ;
24334           else if (seen_error ())
24335             /* It's OK to be confused by errors in the input.  */
24336             add_child_die (comp_unit_die (), die);
24337           else
24338             {
24339               /* In certain situations, the lexical block containing a
24340                  nested function can be optimized away, which results
24341                  in the nested function die being orphaned.  Likewise
24342                  with the return type of that nested function.  Force
24343                  this to be a child of the containing function.
24344
24345                  It may happen that even the containing function got fully
24346                  inlined and optimized out.  In that case we are lost and
24347                  assign the empty child.  This should not be big issue as
24348                  the function is likely unreachable too.  */
24349               tree context = NULL_TREE;
24350
24351               gcc_assert (node->created_for);
24352
24353               if (DECL_P (node->created_for))
24354                 context = DECL_CONTEXT (node->created_for);
24355               else if (TYPE_P (node->created_for))
24356                 context = TYPE_CONTEXT (node->created_for);
24357
24358               gcc_assert (context
24359                           && (TREE_CODE (context) == FUNCTION_DECL
24360                               || TREE_CODE (context) == NAMESPACE_DECL));
24361
24362               origin = lookup_decl_die (context);
24363               if (origin)
24364                 add_child_die (origin, die);
24365               else
24366                 add_child_die (comp_unit_die (), die);
24367             }
24368         }
24369     }
24370
24371   limbo_die_list = NULL;
24372
24373 #if ENABLE_ASSERT_CHECKING
24374   {
24375     dw_die_ref die = comp_unit_die (), c;
24376     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24377   }
24378 #endif
24379   resolve_addr (comp_unit_die ());
24380   move_marked_base_types ();
24381
24382   for (node = deferred_asm_name; node; node = node->next)
24383     {
24384       tree decl = node->created_for;
24385       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24386         {
24387           add_linkage_attr (node->die, decl);
24388           move_linkage_attr (node->die);
24389         }
24390     }
24391
24392   deferred_asm_name = NULL;
24393
24394   /* Walk through the list of incomplete types again, trying once more to
24395      emit full debugging info for them.  */
24396   retry_incomplete_types ();
24397
24398   if (flag_eliminate_unused_debug_types)
24399     prune_unused_types ();
24400
24401   /* Generate separate CUs for each of the include files we've seen.
24402      They will go into limbo_die_list.  */
24403   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
24404     break_out_includes (comp_unit_die ());
24405
24406   /* Generate separate COMDAT sections for type DIEs. */
24407   if (use_debug_types)
24408     {
24409       break_out_comdat_types (comp_unit_die ());
24410
24411       /* Each new type_unit DIE was added to the limbo die list when created.
24412          Since these have all been added to comdat_type_list, clear the
24413          limbo die list.  */
24414       limbo_die_list = NULL;
24415
24416       /* For each new comdat type unit, copy declarations for incomplete
24417          types to make the new unit self-contained (i.e., no direct
24418          references to the main compile unit).  */
24419       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24420         copy_decls_for_unworthy_types (ctnode->root_die);
24421       copy_decls_for_unworthy_types (comp_unit_die ());
24422
24423       /* In the process of copying declarations from one unit to another,
24424          we may have left some declarations behind that are no longer
24425          referenced.  Prune them.  */
24426       prune_unused_types ();
24427     }
24428
24429   /* Traverse the DIE's and add add sibling attributes to those DIE's
24430      that have children.  */
24431   add_sibling_attributes (comp_unit_die ());
24432   for (node = limbo_die_list; node; node = node->next)
24433     add_sibling_attributes (node->die);
24434   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24435     add_sibling_attributes (ctnode->root_die);
24436
24437   /* Output a terminator label for the .text section.  */
24438   switch_to_section (text_section);
24439   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24440   if (cold_text_section)
24441     {
24442       switch_to_section (cold_text_section);
24443       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24444     }
24445
24446   /* We can only use the low/high_pc attributes if all of the code was
24447      in .text.  */
24448   if (!have_multiple_function_sections 
24449       || (dwarf_version < 3 && dwarf_strict))
24450     {
24451       /* Don't add if the CU has no associated code.  */
24452       if (text_section_used)
24453         {
24454           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
24455           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
24456         }
24457     }
24458   else
24459     {
24460       unsigned fde_idx = 0;
24461       bool range_list_added = false;
24462
24463       if (text_section_used)
24464         add_ranges_by_labels (comp_unit_die (), text_section_label,
24465                               text_end_label, &range_list_added);
24466       if (cold_text_section_used)
24467         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
24468                               cold_end_label, &range_list_added);
24469
24470       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
24471         {
24472           dw_fde_ref fde = &fde_table[fde_idx];
24473
24474           if (!fde->in_std_section)
24475             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
24476                                   fde->dw_fde_end, &range_list_added);
24477           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24478             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
24479                                   fde->dw_fde_second_end, &range_list_added);
24480         }
24481
24482       if (range_list_added)
24483         {
24484           /* We need to give .debug_loc and .debug_ranges an appropriate
24485              "base address".  Use zero so that these addresses become
24486              absolute.  Historically, we've emitted the unexpected
24487              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24488              Emit both to give time for other tools to adapt.  */
24489           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
24490           if (! dwarf_strict && dwarf_version < 4)
24491             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
24492
24493           add_ranges (NULL);
24494         }
24495     }
24496
24497   if (debug_info_level >= DINFO_LEVEL_NORMAL)
24498     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
24499                     debug_line_section_label);
24500
24501   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24502     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
24503
24504   if (have_location_lists)
24505     optimize_location_lists (comp_unit_die ());
24506
24507   /* Output all of the compilation units.  We put the main one last so that
24508      the offsets are available to output_pubnames.  */
24509   for (node = limbo_die_list; node; node = node->next)
24510     output_comp_unit (node->die, 0);
24511
24512   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
24513   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24514     {
24515       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
24516
24517       /* Don't output duplicate types.  */
24518       if (*slot != HTAB_EMPTY_ENTRY)
24519         continue;
24520
24521       /* Add a pointer to the line table for the main compilation unit
24522          so that the debugger can make sense of DW_AT_decl_file
24523          attributes.  */
24524       if (debug_info_level >= DINFO_LEVEL_NORMAL)
24525         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24526                         debug_line_section_label);
24527
24528       output_comdat_type_unit (ctnode);
24529       *slot = ctnode;
24530     }
24531   htab_delete (comdat_type_table);
24532
24533   /* Output the main compilation unit if non-empty or if .debug_macinfo
24534      will be emitted.  */
24535   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
24536
24537   /* Output the abbreviation table.  */
24538   switch_to_section (debug_abbrev_section);
24539   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24540   output_abbrev_section ();
24541
24542   /* Output location list section if necessary.  */
24543   if (have_location_lists)
24544     {
24545       /* Output the location lists info.  */
24546       switch_to_section (debug_loc_section);
24547       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
24548                                    DEBUG_LOC_SECTION_LABEL, 0);
24549       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24550       output_location_lists (comp_unit_die ());
24551     }
24552
24553   /* Output public names table if necessary.  */
24554   if (!VEC_empty (pubname_entry, pubname_table))
24555     {
24556       gcc_assert (info_section_emitted);
24557       switch_to_section (debug_pubnames_section);
24558       output_pubnames (pubname_table);
24559     }
24560
24561   /* Output public types table if necessary.  */
24562   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24563      It shouldn't hurt to emit it always, since pure DWARF2 consumers
24564      simply won't look for the section.  */
24565   if (!VEC_empty (pubname_entry, pubtype_table))
24566     {
24567       bool empty = false;
24568       
24569       if (flag_eliminate_unused_debug_types)
24570         {
24571           /* The pubtypes table might be emptied by pruning unused items.  */
24572           unsigned i;
24573           pubname_ref p;
24574           empty = true;
24575           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
24576             if (p->die->die_offset != 0)
24577               {
24578                 empty = false;
24579                 break;
24580               }
24581         }
24582       if (!empty)
24583         {
24584           gcc_assert (info_section_emitted);
24585           switch_to_section (debug_pubtypes_section);
24586           output_pubnames (pubtype_table);
24587         }
24588     }
24589
24590   /* Output the address range information if a CU (.debug_info section)
24591      was emitted.  We output an empty table even if we had no functions
24592      to put in it.  This because the consumer has no way to tell the
24593      difference between an empty table that we omitted and failure to
24594      generate a table that would have contained data.  */
24595   if (info_section_emitted)
24596     {
24597       unsigned long aranges_length = size_of_aranges ();
24598
24599       switch_to_section (debug_aranges_section);
24600       output_aranges (aranges_length);
24601     }
24602
24603   /* Output ranges section if necessary.  */
24604   if (ranges_table_in_use)
24605     {
24606       switch_to_section (debug_ranges_section);
24607       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24608       output_ranges ();
24609     }
24610
24611   /* Output the source line correspondence table.  We must do this
24612      even if there is no line information.  Otherwise, on an empty
24613      translation unit, we will generate a present, but empty,
24614      .debug_info section.  IRIX 6.5 `nm' will then complain when
24615      examining the file.  This is done late so that any filenames
24616      used by the debug_info section are marked as 'used'.  */
24617   switch_to_section (debug_line_section);
24618   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24619   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24620     output_line_info ();
24621
24622   /* Have to end the macro section.  */
24623   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24624     {
24625       switch_to_section (debug_macinfo_section);
24626       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24627       if (!VEC_empty (macinfo_entry, macinfo_table))
24628         output_macinfo ();
24629       dw2_asm_output_data (1, 0, "End compilation unit");
24630     }
24631
24632   /* If we emitted any DW_FORM_strp form attribute, output the string
24633      table too.  */
24634   if (debug_str_hash)
24635     htab_traverse (debug_str_hash, output_indirect_string, NULL);
24636 }
24637
24638 #include "gt-dwarf2out.h"