OSDN Git Service

Remove deprecated DW_FORM_sig8 define.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95 #include "cfglayout.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
126 #endif
127
128 /* Map register numbers held in the call frame info that gcc has
129    collected using DWARF_FRAME_REGNUM to those that should be output in
130    .debug_frame and .eh_frame.  */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
134
135 /* Save the result of dwarf2out_do_frame across PCH.  */
136 static GTY(()) bool saved_do_cfi_asm = 0;
137
138 /* Decide whether we want to emit frame unwind information for the current
139    translation unit.  */
140
141 int
142 dwarf2out_do_frame (void)
143 {
144   /* We want to emit correct CFA location expressions or lists, so we
145      have to return true if we're going to output debug info, even if
146      we're not going to output frame or unwind info.  */
147   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148     return true;
149
150   if (saved_do_cfi_asm)
151     return true;
152
153   if (targetm.debug_unwind_info () == UI_DWARF2)
154     return true;
155
156   if ((flag_unwind_tables || flag_exceptions)
157       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158     return true;
159
160   return false;
161 }
162
163 /* Decide whether to emit frame unwind via assembler directives.  */
164
165 int
166 dwarf2out_do_cfi_asm (void)
167 {
168   int enc;
169
170 #ifdef MIPS_DEBUGGING_INFO
171   return false;
172 #endif
173   if (saved_do_cfi_asm)
174     return true;
175   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176     return false;
177   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178     return false;
179
180   /* Make sure the personality encoding is one the assembler can support.
181      In particular, aligned addresses can't be handled.  */
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187     return false;
188
189   /* If we can't get the assembler to emit only .debug_frame, and we don't need
190      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
191   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192       && !flag_unwind_tables && !flag_exceptions
193       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194     return false;
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
234
235 /* Personality decl of current unit.  Used only when assembler does not support
236    personality CFI.  */
237 static GTY(()) rtx current_unit_personality;
238
239 /* How to start an assembler comment.  */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
243
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247
248 /* Call frames are described using a sequence of Call Frame
249    Information instructions.  The register number, offset
250    and address fields are provided as possible operands;
251    their use is selected by the opcode field.  */
252
253 enum dw_cfi_oprnd_type {
254   dw_cfi_oprnd_unused,
255   dw_cfi_oprnd_reg_num,
256   dw_cfi_oprnd_offset,
257   dw_cfi_oprnd_addr,
258   dw_cfi_oprnd_loc
259 };
260
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 }
267 dw_cfi_oprnd;
268
269 typedef struct GTY(()) dw_cfi_struct {
270   dw_cfi_ref dw_cfi_next;
271   enum dwarf_call_frame_info dw_cfi_opc;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd1;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd2;
276 }
277 dw_cfi_node;
278
279 /* This is how we define the location of the CFA. We use to handle it
280    as REG + OFFSET all the time,  but now it can be more complex.
281    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
282    Instead of passing around REG and OFFSET, we pass a copy
283    of this structure.  */
284 typedef struct cfa_loc {
285   HOST_WIDE_INT offset;
286   HOST_WIDE_INT base_offset;
287   unsigned int reg;
288   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
289   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
290 } dw_cfa_location;
291
292 /* All call frame descriptions (FDE's) in the GCC generated DWARF
293    refer to a single Common Information Entry (CIE), defined at
294    the beginning of the .debug_frame section.  This use of a single
295    CIE obviates the need to keep track of multiple CIE's
296    in the DWARF generation routines below.  */
297
298 typedef struct GTY(()) dw_fde_struct {
299   tree decl;
300   const char *dw_fde_begin;
301   const char *dw_fde_current_label;
302   const char *dw_fde_end;
303   const char *dw_fde_vms_end_prologue;
304   const char *dw_fde_vms_begin_epilogue;
305   const char *dw_fde_hot_section_label;
306   const char *dw_fde_hot_section_end_label;
307   const char *dw_fde_unlikely_section_label;
308   const char *dw_fde_unlikely_section_end_label;
309   dw_cfi_ref dw_fde_cfi;
310   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
311   HOST_WIDE_INT stack_realignment;
312   unsigned funcdef_number;
313   /* Dynamic realign argument pointer register.  */
314   unsigned int drap_reg;
315   /* Virtual dynamic realign argument pointer register.  */
316   unsigned int vdrap_reg;
317   /* These 3 flags are copied from rtl_data in function.h.  */
318   unsigned all_throwers_are_sibcalls : 1;
319   unsigned uses_eh_lsda : 1;
320   unsigned nothrow : 1;
321   /* Whether we did stack realign in this call frame.  */
322   unsigned stack_realign : 1;
323   /* Whether dynamic realign argument pointer register has been saved.  */
324   unsigned drap_reg_saved: 1;
325   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
326   unsigned in_std_section : 1;
327   /* True iff dw_fde_unlikely_section_label is in text_section or
328      cold_text_section.  */
329   unsigned cold_in_std_section : 1;
330   /* True iff switched sections.  */
331   unsigned dw_fde_switched_sections : 1;
332   /* True iff switching from cold to hot section.  */
333   unsigned dw_fde_switched_cold_to_hot : 1;
334 }
335 dw_fde_node;
336
337 /* Maximum size (in bytes) of an artificially generated label.  */
338 #define MAX_ARTIFICIAL_LABEL_BYTES      30
339
340 /* The size of addresses as they appear in the Dwarf 2 data.
341    Some architectures use word addresses to refer to code locations,
342    but Dwarf 2 info always uses byte addresses.  On such machines,
343    Dwarf 2 addresses need to be larger than the architecture's
344    pointers.  */
345 #ifndef DWARF2_ADDR_SIZE
346 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
347 #endif
348
349 /* The size in bytes of a DWARF field indicating an offset or length
350    relative to a debug info section, specified to be 4 bytes in the
351    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
352    as PTR_SIZE.  */
353
354 #ifndef DWARF_OFFSET_SIZE
355 #define DWARF_OFFSET_SIZE 4
356 #endif
357
358 /* The size in bytes of a DWARF 4 type signature.  */
359
360 #ifndef DWARF_TYPE_SIGNATURE_SIZE
361 #define DWARF_TYPE_SIGNATURE_SIZE 8
362 #endif
363
364 /* According to the (draft) DWARF 3 specification, the initial length
365    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
366    bytes are 0xffffffff, followed by the length stored in the next 8
367    bytes.
368
369    However, the SGI/MIPS ABI uses an initial length which is equal to
370    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
371
372 #ifndef DWARF_INITIAL_LENGTH_SIZE
373 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
374 #endif
375
376 /* Round SIZE up to the nearest BOUNDARY.  */
377 #define DWARF_ROUND(SIZE,BOUNDARY) \
378   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
379
380 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
381 #ifndef DWARF_CIE_DATA_ALIGNMENT
382 #ifdef STACK_GROWS_DOWNWARD
383 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
384 #else
385 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
386 #endif
387 #endif
388
389 /* CIE identifier.  */
390 #if HOST_BITS_PER_WIDE_INT >= 64
391 #define DWARF_CIE_ID \
392   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
393 #else
394 #define DWARF_CIE_ID DW_CIE_ID
395 #endif
396
397 /* A pointer to the base of a table that contains frame description
398    information for each routine.  */
399 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
400
401 /* Number of elements currently allocated for fde_table.  */
402 static GTY(()) unsigned fde_table_allocated;
403
404 /* Number of elements in fde_table currently in use.  */
405 static GTY(()) unsigned fde_table_in_use;
406
407 /* Size (in elements) of increments by which we may expand the
408    fde_table.  */
409 #define FDE_TABLE_INCREMENT 256
410
411 /* Get the current fde_table entry we should use.  */
412
413 static inline dw_fde_ref
414 current_fde (void)
415 {
416   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
417 }
418
419 /* A list of call frame insns for the CIE.  */
420 static GTY(()) dw_cfi_ref cie_cfi_head;
421
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423    attribute that accelerates the lookup of the FDE associated
424    with the subprogram.  This variable holds the table index of the FDE
425    associated with the current function (body) definition.  */
426 static unsigned current_funcdef_fde;
427
428 struct GTY(()) indirect_string_node {
429   const char *str;
430   unsigned int refcount;
431   enum dwarf_form form;
432   char *label;
433 };
434
435 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
436
437 /* True if the compilation unit has location entries that reference
438    debug strings.  */
439 static GTY(()) bool debug_str_hash_forced = false;
440
441 static GTY(()) int dw2_string_counter;
442 static GTY(()) unsigned long dwarf2out_cfi_label_num;
443
444 /* True if the compilation unit places functions in more than one section.  */
445 static GTY(()) bool have_multiple_function_sections = false;
446
447 /* Whether the default text and cold text sections have been used at all.  */
448
449 static GTY(()) bool text_section_used = false;
450 static GTY(()) bool cold_text_section_used = false;
451
452 /* The default cold text section.  */
453 static GTY(()) section *cold_text_section;
454
455 /* Forward declarations for functions defined in this file.  */
456
457 static char *stripattributes (const char *);
458 static const char *dwarf_cfi_name (unsigned);
459 static dw_cfi_ref new_cfi (void);
460 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
461 static void add_fde_cfi (const char *, dw_cfi_ref);
462 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
463 static void lookup_cfa (dw_cfa_location *);
464 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
465 static void initial_return_save (rtx);
466 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
467                                           HOST_WIDE_INT);
468 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
469 static void output_cfi_directive (dw_cfi_ref);
470 static void output_call_frame_info (int);
471 static void dwarf2out_note_section_used (void);
472 static bool clobbers_queued_reg_save (const_rtx);
473 static void dwarf2out_frame_debug_expr (rtx, const char *);
474
475 /* Support for complex CFA locations.  */
476 static void output_cfa_loc (dw_cfi_ref, int);
477 static void output_cfa_loc_raw (dw_cfi_ref);
478 static void get_cfa_from_loc_descr (dw_cfa_location *,
479                                     struct dw_loc_descr_struct *);
480 static struct dw_loc_descr_struct *build_cfa_loc
481   (dw_cfa_location *, HOST_WIDE_INT);
482 static struct dw_loc_descr_struct *build_cfa_aligned_loc
483   (HOST_WIDE_INT, HOST_WIDE_INT);
484 static void def_cfa_1 (const char *, dw_cfa_location *);
485 static struct dw_loc_descr_struct *mem_loc_descriptor
486   (rtx, enum machine_mode mode, 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_next = NULL;
817   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
818   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
819
820   return cfi;
821 }
822
823 /* Add a Call Frame Instruction to list of instructions.  */
824
825 static inline void
826 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
827 {
828   dw_cfi_ref *p;
829   dw_fde_ref fde = current_fde ();
830
831   /* When DRAP is used, CFA is defined with an expression.  Redefine
832      CFA may lead to a different CFA value.   */
833   /* ??? Of course, this heuristic fails when we're annotating epilogues,
834      because of course we'll always want to redefine the CFA back to the
835      stack pointer on the way out.  Where should we move this check?  */
836   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
837     switch (cfi->dw_cfi_opc)
838       {
839         case DW_CFA_def_cfa_register:
840         case DW_CFA_def_cfa_offset:
841         case DW_CFA_def_cfa_offset_sf:
842         case DW_CFA_def_cfa:
843         case DW_CFA_def_cfa_sf:
844           gcc_unreachable ();
845
846         default:
847           break;
848       }
849
850   /* Find the end of the chain.  */
851   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
852     ;
853
854   *p = cfi;
855 }
856
857 /* Generate a new label for the CFI info to refer to.  FORCE is true
858    if a label needs to be output even when using .cfi_* directives.  */
859
860 char *
861 dwarf2out_cfi_label (bool force)
862 {
863   static char label[20];
864
865   if (!force && dwarf2out_do_cfi_asm ())
866     {
867       /* In this case, we will be emitting the asm directive instead of
868          the label, so just return a placeholder to keep the rest of the
869          interfaces happy.  */
870       strcpy (label, "<do not output>");
871     }
872   else
873     {
874       int num = dwarf2out_cfi_label_num++;
875       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
876       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
877     }
878
879   return label;
880 }
881
882 /* True if remember_state should be emitted before following CFI directive.  */
883 static bool emit_cfa_remember;
884
885 /* True if any CFI directives were emitted at the current insn.  */
886 static bool any_cfis_emitted;
887
888 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
889    or to the CIE if LABEL is NULL.  */
890
891 static void
892 add_fde_cfi (const char *label, dw_cfi_ref cfi)
893 {
894   dw_cfi_ref *list_head;
895
896   if (emit_cfa_remember)
897     {
898       dw_cfi_ref cfi_remember;
899
900       /* Emit the state save.  */
901       emit_cfa_remember = false;
902       cfi_remember = new_cfi ();
903       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
904       add_fde_cfi (label, cfi_remember);
905     }
906
907   list_head = &cie_cfi_head;
908
909   if (dwarf2out_do_cfi_asm ())
910     {
911       if (label)
912         {
913           dw_fde_ref fde = current_fde ();
914
915           gcc_assert (fde != NULL);
916
917           /* We still have to add the cfi to the list so that lookup_cfa
918              works later on.  When -g2 and above we even need to force
919              emitting of CFI labels and add to list a DW_CFA_set_loc for
920              convert_cfa_to_fb_loc_list purposes.  If we're generating
921              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
922              convert_cfa_to_fb_loc_list.  */
923           if (dwarf_version == 2
924               && debug_info_level > DINFO_LEVEL_TERSE
925               && (write_symbols == DWARF2_DEBUG
926                   || write_symbols == VMS_AND_DWARF2_DEBUG))
927             {
928               switch (cfi->dw_cfi_opc)
929                 {
930                 case DW_CFA_def_cfa_offset:
931                 case DW_CFA_def_cfa_offset_sf:
932                 case DW_CFA_def_cfa_register:
933                 case DW_CFA_def_cfa:
934                 case DW_CFA_def_cfa_sf:
935                 case DW_CFA_def_cfa_expression:
936                 case DW_CFA_restore_state:
937                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
938                     label = dwarf2out_cfi_label (true);
939
940                   if (fde->dw_fde_current_label == NULL
941                       || strcmp (label, fde->dw_fde_current_label) != 0)
942                     {
943                       dw_cfi_ref xcfi;
944
945                       label = xstrdup (label);
946
947                       /* Set the location counter to the new label.  */
948                       xcfi = new_cfi ();
949                       /* It doesn't metter whether DW_CFA_set_loc
950                          or DW_CFA_advance_loc4 is added here, those aren't
951                          emitted into assembly, only looked up by
952                          convert_cfa_to_fb_loc_list.  */
953                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
954                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
955                       add_cfi (&fde->dw_fde_cfi, xcfi);
956                       fde->dw_fde_current_label = label;
957                     }
958                   break;
959                 default:
960                   break;
961                 }
962             }
963
964           output_cfi_directive (cfi);
965
966           list_head = &fde->dw_fde_cfi;
967           any_cfis_emitted = true;
968         }
969       /* ??? If this is a CFI for the CIE, we don't emit.  This
970          assumes that the standard CIE contents that the assembler
971          uses matches the standard CIE contents that the compiler
972          uses.  This is probably a bad assumption.  I'm not quite
973          sure how to address this for now.  */
974     }
975   else if (label)
976     {
977       dw_fde_ref fde = current_fde ();
978
979       gcc_assert (fde != NULL);
980
981       if (*label == 0)
982         label = dwarf2out_cfi_label (false);
983
984       if (fde->dw_fde_current_label == NULL
985           || strcmp (label, fde->dw_fde_current_label) != 0)
986         {
987           dw_cfi_ref xcfi;
988
989           label = xstrdup (label);
990
991           /* Set the location counter to the new label.  */
992           xcfi = new_cfi ();
993           /* If we have a current label, advance from there, otherwise
994              set the location directly using set_loc.  */
995           xcfi->dw_cfi_opc = fde->dw_fde_current_label
996                              ? DW_CFA_advance_loc4
997                              : DW_CFA_set_loc;
998           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
999           add_cfi (&fde->dw_fde_cfi, xcfi);
1000
1001           fde->dw_fde_current_label = label;
1002         }
1003
1004       list_head = &fde->dw_fde_cfi;
1005       any_cfis_emitted = true;
1006     }
1007
1008   add_cfi (list_head, cfi);
1009 }
1010
1011 /* Subroutine of lookup_cfa.  */
1012
1013 static void
1014 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1015 {
1016   switch (cfi->dw_cfi_opc)
1017     {
1018     case DW_CFA_def_cfa_offset:
1019     case DW_CFA_def_cfa_offset_sf:
1020       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1021       break;
1022     case DW_CFA_def_cfa_register:
1023       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1024       break;
1025     case DW_CFA_def_cfa:
1026     case DW_CFA_def_cfa_sf:
1027       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1028       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1029       break;
1030     case DW_CFA_def_cfa_expression:
1031       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1032       break;
1033
1034     case DW_CFA_remember_state:
1035       gcc_assert (!remember->in_use);
1036       *remember = *loc;
1037       remember->in_use = 1;
1038       break;
1039     case DW_CFA_restore_state:
1040       gcc_assert (remember->in_use);
1041       *loc = *remember;
1042       remember->in_use = 0;
1043       break;
1044
1045     default:
1046       break;
1047     }
1048 }
1049
1050 /* Find the previous value for the CFA.  */
1051
1052 static void
1053 lookup_cfa (dw_cfa_location *loc)
1054 {
1055   dw_cfi_ref cfi;
1056   dw_fde_ref fde;
1057   dw_cfa_location remember;
1058
1059   memset (loc, 0, sizeof (*loc));
1060   loc->reg = INVALID_REGNUM;
1061   remember = *loc;
1062
1063   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1064     lookup_cfa_1 (cfi, loc, &remember);
1065
1066   fde = current_fde ();
1067   if (fde)
1068     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1069       lookup_cfa_1 (cfi, loc, &remember);
1070 }
1071
1072 /* The current rule for calculating the DWARF2 canonical frame address.  */
1073 static dw_cfa_location cfa;
1074
1075 /* The register used for saving registers to the stack, and its offset
1076    from the CFA.  */
1077 static dw_cfa_location cfa_store;
1078
1079 /* The current save location around an epilogue.  */
1080 static dw_cfa_location cfa_remember;
1081
1082 /* The running total of the size of arguments pushed onto the stack.  */
1083 static HOST_WIDE_INT args_size;
1084
1085 /* The last args_size we actually output.  */
1086 static HOST_WIDE_INT old_args_size;
1087
1088 /* Entry point to update the canonical frame address (CFA).
1089    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1090    calculated from REG+OFFSET.  */
1091
1092 void
1093 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1094 {
1095   dw_cfa_location loc;
1096   loc.indirect = 0;
1097   loc.base_offset = 0;
1098   loc.reg = reg;
1099   loc.offset = offset;
1100   def_cfa_1 (label, &loc);
1101 }
1102
1103 /* Determine if two dw_cfa_location structures define the same data.  */
1104
1105 static bool
1106 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1107 {
1108   return (loc1->reg == loc2->reg
1109           && loc1->offset == loc2->offset
1110           && loc1->indirect == loc2->indirect
1111           && (loc1->indirect == 0
1112               || loc1->base_offset == loc2->base_offset));
1113 }
1114
1115 /* This routine does the actual work.  The CFA is now calculated from
1116    the dw_cfa_location structure.  */
1117
1118 static void
1119 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1120 {
1121   dw_cfi_ref cfi;
1122   dw_cfa_location old_cfa, loc;
1123
1124   cfa = *loc_p;
1125   loc = *loc_p;
1126
1127   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1128     cfa_store.offset = loc.offset;
1129
1130   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1131   lookup_cfa (&old_cfa);
1132
1133   /* If nothing changed, no need to issue any call frame instructions.  */
1134   if (cfa_equal_p (&loc, &old_cfa))
1135     return;
1136
1137   cfi = new_cfi ();
1138
1139   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1140     {
1141       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1142          the CFA register did not change but the offset did.  The data
1143          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1144          in the assembler via the .cfi_def_cfa_offset directive.  */
1145       if (loc.offset < 0)
1146         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1147       else
1148         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1149       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1150     }
1151
1152 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1153   else if (loc.offset == old_cfa.offset
1154            && old_cfa.reg != INVALID_REGNUM
1155            && !loc.indirect
1156            && !old_cfa.indirect)
1157     {
1158       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1159          indicating the CFA register has changed to <register> but the
1160          offset has not changed.  */
1161       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1162       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1163     }
1164 #endif
1165
1166   else if (loc.indirect == 0)
1167     {
1168       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1169          indicating the CFA register has changed to <register> with
1170          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1171          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1172          directive.  */
1173       if (loc.offset < 0)
1174         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1175       else
1176         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1177       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1178       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1179     }
1180   else
1181     {
1182       /* Construct a DW_CFA_def_cfa_expression instruction to
1183          calculate the CFA using a full location expression since no
1184          register-offset pair is available.  */
1185       struct dw_loc_descr_struct *loc_list;
1186
1187       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1188       loc_list = build_cfa_loc (&loc, 0);
1189       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1190     }
1191
1192   add_fde_cfi (label, cfi);
1193 }
1194
1195 /* Add the CFI for saving a register.  REG is the CFA column number.
1196    LABEL is passed to add_fde_cfi.
1197    If SREG is -1, the register is saved at OFFSET from the CFA;
1198    otherwise it is saved in SREG.  */
1199
1200 static void
1201 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1202 {
1203   dw_cfi_ref cfi = new_cfi ();
1204   dw_fde_ref fde = current_fde ();
1205
1206   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1207
1208   /* When stack is aligned, store REG using DW_CFA_expression with
1209      FP.  */
1210   if (fde
1211       && fde->stack_realign
1212       && sreg == INVALID_REGNUM)
1213     {
1214       cfi->dw_cfi_opc = DW_CFA_expression;
1215       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1216       cfi->dw_cfi_oprnd2.dw_cfi_loc
1217         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1218     }
1219   else if (sreg == INVALID_REGNUM)
1220     {
1221       if (need_data_align_sf_opcode (offset))
1222         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1223       else if (reg & ~0x3f)
1224         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1225       else
1226         cfi->dw_cfi_opc = DW_CFA_offset;
1227       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1228     }
1229   else if (sreg == reg)
1230     cfi->dw_cfi_opc = DW_CFA_same_value;
1231   else
1232     {
1233       cfi->dw_cfi_opc = DW_CFA_register;
1234       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1235     }
1236
1237   add_fde_cfi (label, cfi);
1238 }
1239
1240 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1241    This CFI tells the unwinder that it needs to restore the window registers
1242    from the previous frame's window save area.
1243
1244    ??? Perhaps we should note in the CIE where windows are saved (instead of
1245    assuming 0(cfa)) and what registers are in the window.  */
1246
1247 void
1248 dwarf2out_window_save (const char *label)
1249 {
1250   dw_cfi_ref cfi = new_cfi ();
1251
1252   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1253   add_fde_cfi (label, cfi);
1254 }
1255
1256 /* Entry point for saving a register to the stack.  REG is the GCC register
1257    number.  LABEL and OFFSET are passed to reg_save.  */
1258
1259 void
1260 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1261 {
1262   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1263 }
1264
1265 /* Entry point for saving the return address in the stack.
1266    LABEL and OFFSET are passed to reg_save.  */
1267
1268 void
1269 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1270 {
1271   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1272 }
1273
1274 /* Entry point for saving the return address in a register.
1275    LABEL and SREG are passed to reg_save.  */
1276
1277 void
1278 dwarf2out_return_reg (const char *label, unsigned int sreg)
1279 {
1280   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1281 }
1282
1283 /* Record the initial position of the return address.  RTL is
1284    INCOMING_RETURN_ADDR_RTX.  */
1285
1286 static void
1287 initial_return_save (rtx rtl)
1288 {
1289   unsigned int reg = INVALID_REGNUM;
1290   HOST_WIDE_INT offset = 0;
1291
1292   switch (GET_CODE (rtl))
1293     {
1294     case REG:
1295       /* RA is in a register.  */
1296       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1297       break;
1298
1299     case MEM:
1300       /* RA is on the stack.  */
1301       rtl = XEXP (rtl, 0);
1302       switch (GET_CODE (rtl))
1303         {
1304         case REG:
1305           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1306           offset = 0;
1307           break;
1308
1309         case PLUS:
1310           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1311           offset = INTVAL (XEXP (rtl, 1));
1312           break;
1313
1314         case MINUS:
1315           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1316           offset = -INTVAL (XEXP (rtl, 1));
1317           break;
1318
1319         default:
1320           gcc_unreachable ();
1321         }
1322
1323       break;
1324
1325     case PLUS:
1326       /* The return address is at some offset from any value we can
1327          actually load.  For instance, on the SPARC it is in %i7+8. Just
1328          ignore the offset for now; it doesn't matter for unwinding frames.  */
1329       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1330       initial_return_save (XEXP (rtl, 0));
1331       return;
1332
1333     default:
1334       gcc_unreachable ();
1335     }
1336
1337   if (reg != DWARF_FRAME_RETURN_COLUMN)
1338     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1339 }
1340
1341 /* Given a SET, calculate the amount of stack adjustment it
1342    contains.  */
1343
1344 static HOST_WIDE_INT
1345 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1346                      HOST_WIDE_INT cur_offset)
1347 {
1348   const_rtx src = SET_SRC (pattern);
1349   const_rtx dest = SET_DEST (pattern);
1350   HOST_WIDE_INT offset = 0;
1351   enum rtx_code code;
1352
1353   if (dest == stack_pointer_rtx)
1354     {
1355       code = GET_CODE (src);
1356
1357       /* Assume (set (reg sp) (reg whatever)) sets args_size
1358          level to 0.  */
1359       if (code == REG && src != stack_pointer_rtx)
1360         {
1361           offset = -cur_args_size;
1362 #ifndef STACK_GROWS_DOWNWARD
1363           offset = -offset;
1364 #endif
1365           return offset - cur_offset;
1366         }
1367
1368       if (! (code == PLUS || code == MINUS)
1369           || XEXP (src, 0) != stack_pointer_rtx
1370           || !CONST_INT_P (XEXP (src, 1)))
1371         return 0;
1372
1373       /* (set (reg sp) (plus (reg sp) (const_int))) */
1374       offset = INTVAL (XEXP (src, 1));
1375       if (code == PLUS)
1376         offset = -offset;
1377       return offset;
1378     }
1379
1380   if (MEM_P (src) && !MEM_P (dest))
1381     dest = src;
1382   if (MEM_P (dest))
1383     {
1384       /* (set (mem (pre_dec (reg sp))) (foo)) */
1385       src = XEXP (dest, 0);
1386       code = GET_CODE (src);
1387
1388       switch (code)
1389         {
1390         case PRE_MODIFY:
1391         case POST_MODIFY:
1392           if (XEXP (src, 0) == stack_pointer_rtx)
1393             {
1394               rtx val = XEXP (XEXP (src, 1), 1);
1395               /* We handle only adjustments by constant amount.  */
1396               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1397                           && CONST_INT_P (val));
1398               offset = -INTVAL (val);
1399               break;
1400             }
1401           return 0;
1402
1403         case PRE_DEC:
1404         case POST_DEC:
1405           if (XEXP (src, 0) == stack_pointer_rtx)
1406             {
1407               offset = GET_MODE_SIZE (GET_MODE (dest));
1408               break;
1409             }
1410           return 0;
1411
1412         case PRE_INC:
1413         case POST_INC:
1414           if (XEXP (src, 0) == stack_pointer_rtx)
1415             {
1416               offset = -GET_MODE_SIZE (GET_MODE (dest));
1417               break;
1418             }
1419           return 0;
1420
1421         default:
1422           return 0;
1423         }
1424     }
1425   else
1426     return 0;
1427
1428   return offset;
1429 }
1430
1431 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1432    indexed by INSN_UID.  */
1433
1434 static HOST_WIDE_INT *barrier_args_size;
1435
1436 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1437
1438 static HOST_WIDE_INT
1439 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1440                              VEC (rtx, heap) **next)
1441 {
1442   HOST_WIDE_INT offset = 0;
1443   int i;
1444
1445   if (! RTX_FRAME_RELATED_P (insn))
1446     {
1447       if (prologue_epilogue_contains (insn))
1448         /* Nothing */;
1449       else if (GET_CODE (PATTERN (insn)) == SET)
1450         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1451       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1452                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1453         {
1454           /* There may be stack adjustments inside compound insns.  Search
1455              for them.  */
1456           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1457             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1458               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1459                                              cur_args_size, offset);
1460         }
1461     }
1462   else
1463     {
1464       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1465
1466       if (expr)
1467         {
1468           expr = XEXP (expr, 0);
1469           if (GET_CODE (expr) == PARALLEL
1470               || GET_CODE (expr) == SEQUENCE)
1471             for (i = 1; i < XVECLEN (expr, 0); i++)
1472               {
1473                 rtx elem = XVECEXP (expr, 0, i);
1474
1475                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1476                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1477               }
1478         }
1479     }
1480
1481 #ifndef STACK_GROWS_DOWNWARD
1482   offset = -offset;
1483 #endif
1484
1485   cur_args_size += offset;
1486   if (cur_args_size < 0)
1487     cur_args_size = 0;
1488
1489   if (JUMP_P (insn))
1490     {
1491       rtx dest = JUMP_LABEL (insn);
1492
1493       if (dest)
1494         {
1495           if (barrier_args_size [INSN_UID (dest)] < 0)
1496             {
1497               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1498               VEC_safe_push (rtx, heap, *next, dest);
1499             }
1500         }
1501     }
1502
1503   return cur_args_size;
1504 }
1505
1506 /* Walk the whole function and compute args_size on BARRIERs.  */
1507
1508 static void
1509 compute_barrier_args_size (void)
1510 {
1511   int max_uid = get_max_uid (), i;
1512   rtx insn;
1513   VEC (rtx, heap) *worklist, *next, *tmp;
1514
1515   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1516   for (i = 0; i < max_uid; i++)
1517     barrier_args_size[i] = -1;
1518
1519   worklist = VEC_alloc (rtx, heap, 20);
1520   next = VEC_alloc (rtx, heap, 20);
1521   insn = get_insns ();
1522   barrier_args_size[INSN_UID (insn)] = 0;
1523   VEC_quick_push (rtx, worklist, insn);
1524   for (;;)
1525     {
1526       while (!VEC_empty (rtx, worklist))
1527         {
1528           rtx prev, body, first_insn;
1529           HOST_WIDE_INT cur_args_size;
1530
1531           first_insn = insn = VEC_pop (rtx, worklist);
1532           cur_args_size = barrier_args_size[INSN_UID (insn)];
1533           prev = prev_nonnote_insn (insn);
1534           if (prev && BARRIER_P (prev))
1535             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1536
1537           for (; insn; insn = NEXT_INSN (insn))
1538             {
1539               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1540                 continue;
1541               if (BARRIER_P (insn))
1542                 break;
1543
1544               if (LABEL_P (insn))
1545                 {
1546                   if (insn == first_insn)
1547                     continue;
1548                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1549                     {
1550                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1551                       continue;
1552                     }
1553                   else
1554                     {
1555                       /* The insns starting with this label have been
1556                          already scanned or are in the worklist.  */
1557                       break;
1558                     }
1559                 }
1560
1561               body = PATTERN (insn);
1562               if (GET_CODE (body) == SEQUENCE)
1563                 {
1564                   HOST_WIDE_INT dest_args_size = cur_args_size;
1565                   for (i = 1; i < XVECLEN (body, 0); i++)
1566                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1567                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1568                       dest_args_size
1569                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1570                                                        dest_args_size, &next);
1571                     else
1572                       cur_args_size
1573                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1574                                                        cur_args_size, &next);
1575
1576                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1577                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1578                                                  dest_args_size, &next);
1579                   else
1580                     cur_args_size
1581                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1582                                                      cur_args_size, &next);
1583                 }
1584               else
1585                 cur_args_size
1586                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1587             }
1588         }
1589
1590       if (VEC_empty (rtx, next))
1591         break;
1592
1593       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1594       tmp = next;
1595       next = worklist;
1596       worklist = tmp;
1597       VEC_truncate (rtx, next, 0);
1598     }
1599
1600   VEC_free (rtx, heap, worklist);
1601   VEC_free (rtx, heap, next);
1602 }
1603
1604 /* Add a CFI to update the running total of the size of arguments
1605    pushed onto the stack.  */
1606
1607 static void
1608 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1609 {
1610   dw_cfi_ref cfi;
1611
1612   if (size == old_args_size)
1613     return;
1614
1615   old_args_size = size;
1616
1617   cfi = new_cfi ();
1618   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1619   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1620   add_fde_cfi (label, cfi);
1621 }
1622
1623 /* Record a stack adjustment of OFFSET bytes.  */
1624
1625 static void
1626 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1627 {
1628   if (cfa.reg == STACK_POINTER_REGNUM)
1629     cfa.offset += offset;
1630
1631   if (cfa_store.reg == STACK_POINTER_REGNUM)
1632     cfa_store.offset += offset;
1633
1634   if (ACCUMULATE_OUTGOING_ARGS)
1635     return;
1636
1637 #ifndef STACK_GROWS_DOWNWARD
1638   offset = -offset;
1639 #endif
1640
1641   args_size += offset;
1642   if (args_size < 0)
1643     args_size = 0;
1644
1645   def_cfa_1 (label, &cfa);
1646   if (flag_asynchronous_unwind_tables)
1647     dwarf2out_args_size (label, args_size);
1648 }
1649
1650 /* Check INSN to see if it looks like a push or a stack adjustment, and
1651    make a note of it if it does.  EH uses this information to find out
1652    how much extra space it needs to pop off the stack.  */
1653
1654 static void
1655 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1656 {
1657   HOST_WIDE_INT offset;
1658   const char *label;
1659   int i;
1660
1661   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1662      with this function.  Proper support would require all frame-related
1663      insns to be marked, and to be able to handle saving state around
1664      epilogues textually in the middle of the function.  */
1665   if (prologue_epilogue_contains (insn))
1666     return;
1667
1668   /* If INSN is an instruction from target of an annulled branch, the
1669      effects are for the target only and so current argument size
1670      shouldn't change at all.  */
1671   if (final_sequence
1672       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1673       && INSN_FROM_TARGET_P (insn))
1674     return;
1675
1676   /* If only calls can throw, and we have a frame pointer,
1677      save up adjustments until we see the CALL_INSN.  */
1678   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1679     {
1680       if (CALL_P (insn) && !after_p)
1681         {
1682           /* Extract the size of the args from the CALL rtx itself.  */
1683           insn = PATTERN (insn);
1684           if (GET_CODE (insn) == PARALLEL)
1685             insn = XVECEXP (insn, 0, 0);
1686           if (GET_CODE (insn) == SET)
1687             insn = SET_SRC (insn);
1688           gcc_assert (GET_CODE (insn) == CALL);
1689           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1690         }
1691       return;
1692     }
1693
1694   if (CALL_P (insn) && !after_p)
1695     {
1696       if (!flag_asynchronous_unwind_tables)
1697         dwarf2out_args_size ("", args_size);
1698       return;
1699     }
1700   else if (BARRIER_P (insn))
1701     {
1702       /* Don't call compute_barrier_args_size () if the only
1703          BARRIER is at the end of function.  */
1704       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1705         compute_barrier_args_size ();
1706       if (barrier_args_size == NULL)
1707         offset = 0;
1708       else
1709         {
1710           offset = barrier_args_size[INSN_UID (insn)];
1711           if (offset < 0)
1712             offset = 0;
1713         }
1714
1715       offset -= args_size;
1716 #ifndef STACK_GROWS_DOWNWARD
1717       offset = -offset;
1718 #endif
1719     }
1720   else if (GET_CODE (PATTERN (insn)) == SET)
1721     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1722   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1723            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1724     {
1725       /* There may be stack adjustments inside compound insns.  Search
1726          for them.  */
1727       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1728         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1729           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1730                                          args_size, offset);
1731     }
1732   else
1733     return;
1734
1735   if (offset == 0)
1736     return;
1737
1738   label = dwarf2out_cfi_label (false);
1739   dwarf2out_stack_adjust (offset, label);
1740 }
1741
1742 /* We delay emitting a register save until either (a) we reach the end
1743    of the prologue or (b) the register is clobbered.  This clusters
1744    register saves so that there are fewer pc advances.  */
1745
1746 struct GTY(()) queued_reg_save {
1747   struct queued_reg_save *next;
1748   rtx reg;
1749   HOST_WIDE_INT cfa_offset;
1750   rtx saved_reg;
1751 };
1752
1753 static GTY(()) struct queued_reg_save *queued_reg_saves;
1754
1755 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1756 struct GTY(()) reg_saved_in_data {
1757   rtx orig_reg;
1758   rtx saved_in_reg;
1759 };
1760
1761 /* A list of registers saved in other registers.
1762    The list intentionally has a small maximum capacity of 4; if your
1763    port needs more than that, you might consider implementing a
1764    more efficient data structure.  */
1765 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1766 static GTY(()) size_t num_regs_saved_in_regs;
1767
1768 static const char *last_reg_save_label;
1769
1770 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1771    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1772
1773 static void
1774 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1775 {
1776   struct queued_reg_save *q;
1777
1778   /* Duplicates waste space, but it's also necessary to remove them
1779      for correctness, since the queue gets output in reverse
1780      order.  */
1781   for (q = queued_reg_saves; q != NULL; q = q->next)
1782     if (REGNO (q->reg) == REGNO (reg))
1783       break;
1784
1785   if (q == NULL)
1786     {
1787       q = ggc_alloc_queued_reg_save ();
1788       q->next = queued_reg_saves;
1789       queued_reg_saves = q;
1790     }
1791
1792   q->reg = reg;
1793   q->cfa_offset = offset;
1794   q->saved_reg = sreg;
1795
1796   last_reg_save_label = label;
1797 }
1798
1799 /* Output all the entries in QUEUED_REG_SAVES.  */
1800
1801 void
1802 dwarf2out_flush_queued_reg_saves (void)
1803 {
1804   struct queued_reg_save *q;
1805
1806   for (q = queued_reg_saves; q; q = q->next)
1807     {
1808       size_t i;
1809       unsigned int reg, sreg;
1810
1811       for (i = 0; i < num_regs_saved_in_regs; i++)
1812         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1813           break;
1814       if (q->saved_reg && i == num_regs_saved_in_regs)
1815         {
1816           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1817           num_regs_saved_in_regs++;
1818         }
1819       if (i != num_regs_saved_in_regs)
1820         {
1821           regs_saved_in_regs[i].orig_reg = q->reg;
1822           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1823         }
1824
1825       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1826       if (q->saved_reg)
1827         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1828       else
1829         sreg = INVALID_REGNUM;
1830       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1831     }
1832
1833   queued_reg_saves = NULL;
1834   last_reg_save_label = NULL;
1835 }
1836
1837 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1838    location for?  Or, does it clobber a register which we've previously
1839    said that some other register is saved in, and for which we now
1840    have a new location for?  */
1841
1842 static bool
1843 clobbers_queued_reg_save (const_rtx insn)
1844 {
1845   struct queued_reg_save *q;
1846
1847   for (q = queued_reg_saves; q; q = q->next)
1848     {
1849       size_t i;
1850       if (modified_in_p (q->reg, insn))
1851         return true;
1852       for (i = 0; i < num_regs_saved_in_regs; i++)
1853         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1854             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1855           return true;
1856     }
1857
1858   return false;
1859 }
1860
1861 /* Entry point for saving the first register into the second.  */
1862
1863 void
1864 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1865 {
1866   size_t i;
1867   unsigned int regno, sregno;
1868
1869   for (i = 0; i < num_regs_saved_in_regs; i++)
1870     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1871       break;
1872   if (i == num_regs_saved_in_regs)
1873     {
1874       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1875       num_regs_saved_in_regs++;
1876     }
1877   regs_saved_in_regs[i].orig_reg = reg;
1878   regs_saved_in_regs[i].saved_in_reg = sreg;
1879
1880   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1881   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1882   reg_save (label, regno, sregno, 0);
1883 }
1884
1885 /* What register, if any, is currently saved in REG?  */
1886
1887 static rtx
1888 reg_saved_in (rtx reg)
1889 {
1890   unsigned int regn = REGNO (reg);
1891   size_t i;
1892   struct queued_reg_save *q;
1893
1894   for (q = queued_reg_saves; q; q = q->next)
1895     if (q->saved_reg && regn == REGNO (q->saved_reg))
1896       return q->reg;
1897
1898   for (i = 0; i < num_regs_saved_in_regs; i++)
1899     if (regs_saved_in_regs[i].saved_in_reg
1900         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1901       return regs_saved_in_regs[i].orig_reg;
1902
1903   return NULL_RTX;
1904 }
1905
1906
1907 /* A temporary register holding an integral value used in adjusting SP
1908    or setting up the store_reg.  The "offset" field holds the integer
1909    value, not an offset.  */
1910 static dw_cfa_location cfa_temp;
1911
1912 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1913
1914 static void
1915 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1916 {
1917   memset (&cfa, 0, sizeof (cfa));
1918
1919   switch (GET_CODE (pat))
1920     {
1921     case PLUS:
1922       cfa.reg = REGNO (XEXP (pat, 0));
1923       cfa.offset = INTVAL (XEXP (pat, 1));
1924       break;
1925
1926     case REG:
1927       cfa.reg = REGNO (pat);
1928       break;
1929
1930     case MEM:
1931       cfa.indirect = 1;
1932       pat = XEXP (pat, 0);
1933       if (GET_CODE (pat) == PLUS)
1934         {
1935           cfa.base_offset = INTVAL (XEXP (pat, 1));
1936           pat = XEXP (pat, 0);
1937         }
1938       cfa.reg = REGNO (pat);
1939       break;
1940
1941     default:
1942       /* Recurse and define an expression.  */
1943       gcc_unreachable ();
1944     }
1945
1946   def_cfa_1 (label, &cfa);
1947 }
1948
1949 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1950
1951 static void
1952 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1953 {
1954   rtx src, dest;
1955
1956   gcc_assert (GET_CODE (pat) == SET);
1957   dest = XEXP (pat, 0);
1958   src = XEXP (pat, 1);
1959
1960   switch (GET_CODE (src))
1961     {
1962     case PLUS:
1963       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1964       cfa.offset -= INTVAL (XEXP (src, 1));
1965       break;
1966
1967     case REG:
1968         break;
1969
1970     default:
1971         gcc_unreachable ();
1972     }
1973
1974   cfa.reg = REGNO (dest);
1975   gcc_assert (cfa.indirect == 0);
1976
1977   def_cfa_1 (label, &cfa);
1978 }
1979
1980 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1981
1982 static void
1983 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1984 {
1985   HOST_WIDE_INT offset;
1986   rtx src, addr, span;
1987
1988   src = XEXP (set, 1);
1989   addr = XEXP (set, 0);
1990   gcc_assert (MEM_P (addr));
1991   addr = XEXP (addr, 0);
1992
1993   /* As documented, only consider extremely simple addresses.  */
1994   switch (GET_CODE (addr))
1995     {
1996     case REG:
1997       gcc_assert (REGNO (addr) == cfa.reg);
1998       offset = -cfa.offset;
1999       break;
2000     case PLUS:
2001       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2002       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2003       break;
2004     default:
2005       gcc_unreachable ();
2006     }
2007
2008   span = targetm.dwarf_register_span (src);
2009
2010   /* ??? We'd like to use queue_reg_save, but we need to come up with
2011      a different flushing heuristic for epilogues.  */
2012   if (!span)
2013     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2014   else
2015     {
2016       /* We have a PARALLEL describing where the contents of SRC live.
2017          Queue register saves for each piece of the PARALLEL.  */
2018       int par_index;
2019       int limit;
2020       HOST_WIDE_INT span_offset = offset;
2021
2022       gcc_assert (GET_CODE (span) == PARALLEL);
2023
2024       limit = XVECLEN (span, 0);
2025       for (par_index = 0; par_index < limit; par_index++)
2026         {
2027           rtx elem = XVECEXP (span, 0, par_index);
2028
2029           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2030                     INVALID_REGNUM, span_offset);
2031           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2032         }
2033     }
2034 }
2035
2036 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2037
2038 static void
2039 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2040 {
2041   rtx src, dest;
2042   unsigned sregno, dregno;
2043
2044   src = XEXP (set, 1);
2045   dest = XEXP (set, 0);
2046
2047   if (src == pc_rtx)
2048     sregno = DWARF_FRAME_RETURN_COLUMN;
2049   else
2050     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2051
2052   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2053
2054   /* ??? We'd like to use queue_reg_save, but we need to come up with
2055      a different flushing heuristic for epilogues.  */
2056   reg_save (label, sregno, dregno, 0);
2057 }
2058
2059 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2060
2061 static void
2062 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2063 {
2064   rtx src, dest, span;
2065   dw_cfi_ref cfi = new_cfi ();
2066
2067   dest = SET_DEST (set);
2068   src = SET_SRC (set);
2069
2070   gcc_assert (REG_P (src));
2071   gcc_assert (MEM_P (dest));
2072
2073   span = targetm.dwarf_register_span (src);
2074   gcc_assert (!span);
2075
2076   cfi->dw_cfi_opc = DW_CFA_expression;
2077   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2078   cfi->dw_cfi_oprnd2.dw_cfi_loc
2079     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2080                           VAR_INIT_STATUS_INITIALIZED);
2081
2082   /* ??? We'd like to use queue_reg_save, were the interface different,
2083      and, as above, we could manage flushing for epilogues.  */
2084   add_fde_cfi (label, cfi);
2085 }
2086
2087 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2088
2089 static void
2090 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2091 {
2092   dw_cfi_ref cfi = new_cfi ();
2093   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2094
2095   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2096   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2097
2098   add_fde_cfi (label, cfi);
2099 }
2100
2101 /* Record call frame debugging information for an expression EXPR,
2102    which either sets SP or FP (adjusting how we calculate the frame
2103    address) or saves a register to the stack or another register.
2104    LABEL indicates the address of EXPR.
2105
2106    This function encodes a state machine mapping rtxes to actions on
2107    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2108    users need not read the source code.
2109
2110   The High-Level Picture
2111
2112   Changes in the register we use to calculate the CFA: Currently we
2113   assume that if you copy the CFA register into another register, we
2114   should take the other one as the new CFA register; this seems to
2115   work pretty well.  If it's wrong for some target, it's simple
2116   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2117
2118   Changes in the register we use for saving registers to the stack:
2119   This is usually SP, but not always.  Again, we deduce that if you
2120   copy SP into another register (and SP is not the CFA register),
2121   then the new register is the one we will be using for register
2122   saves.  This also seems to work.
2123
2124   Register saves: There's not much guesswork about this one; if
2125   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2126   register save, and the register used to calculate the destination
2127   had better be the one we think we're using for this purpose.
2128   It's also assumed that a copy from a call-saved register to another
2129   register is saving that register if RTX_FRAME_RELATED_P is set on
2130   that instruction.  If the copy is from a call-saved register to
2131   the *same* register, that means that the register is now the same
2132   value as in the caller.
2133
2134   Except: If the register being saved is the CFA register, and the
2135   offset is nonzero, we are saving the CFA, so we assume we have to
2136   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2137   the intent is to save the value of SP from the previous frame.
2138
2139   In addition, if a register has previously been saved to a different
2140   register,
2141
2142   Invariants / Summaries of Rules
2143
2144   cfa          current rule for calculating the CFA.  It usually
2145                consists of a register and an offset.
2146   cfa_store    register used by prologue code to save things to the stack
2147                cfa_store.offset is the offset from the value of
2148                cfa_store.reg to the actual CFA
2149   cfa_temp     register holding an integral value.  cfa_temp.offset
2150                stores the value, which will be used to adjust the
2151                stack pointer.  cfa_temp is also used like cfa_store,
2152                to track stores to the stack via fp or a temp reg.
2153
2154   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2155                with cfa.reg as the first operand changes the cfa.reg and its
2156                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2157                cfa_temp.offset.
2158
2159   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2160                expression yielding a constant.  This sets cfa_temp.reg
2161                and cfa_temp.offset.
2162
2163   Rule 5:      Create a new register cfa_store used to save items to the
2164                stack.
2165
2166   Rules 10-14: Save a register to the stack.  Define offset as the
2167                difference of the original location and cfa_store's
2168                location (or cfa_temp's location if cfa_temp is used).
2169
2170   Rules 16-20: If AND operation happens on sp in prologue, we assume
2171                stack is realigned.  We will use a group of DW_OP_XXX
2172                expressions to represent the location of the stored
2173                register instead of CFA+offset.
2174
2175   The Rules
2176
2177   "{a,b}" indicates a choice of a xor b.
2178   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2179
2180   Rule 1:
2181   (set <reg1> <reg2>:cfa.reg)
2182   effects: cfa.reg = <reg1>
2183            cfa.offset unchanged
2184            cfa_temp.reg = <reg1>
2185            cfa_temp.offset = cfa.offset
2186
2187   Rule 2:
2188   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2189                               {<const_int>,<reg>:cfa_temp.reg}))
2190   effects: cfa.reg = sp if fp used
2191            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2192            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2193              if cfa_store.reg==sp
2194
2195   Rule 3:
2196   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2197   effects: cfa.reg = fp
2198            cfa_offset += +/- <const_int>
2199
2200   Rule 4:
2201   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2202   constraints: <reg1> != fp
2203                <reg1> != sp
2204   effects: cfa.reg = <reg1>
2205            cfa_temp.reg = <reg1>
2206            cfa_temp.offset = cfa.offset
2207
2208   Rule 5:
2209   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2210   constraints: <reg1> != fp
2211                <reg1> != sp
2212   effects: cfa_store.reg = <reg1>
2213            cfa_store.offset = cfa.offset - cfa_temp.offset
2214
2215   Rule 6:
2216   (set <reg> <const_int>)
2217   effects: cfa_temp.reg = <reg>
2218            cfa_temp.offset = <const_int>
2219
2220   Rule 7:
2221   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2222   effects: cfa_temp.reg = <reg1>
2223            cfa_temp.offset |= <const_int>
2224
2225   Rule 8:
2226   (set <reg> (high <exp>))
2227   effects: none
2228
2229   Rule 9:
2230   (set <reg> (lo_sum <exp> <const_int>))
2231   effects: cfa_temp.reg = <reg>
2232            cfa_temp.offset = <const_int>
2233
2234   Rule 10:
2235   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2236   effects: cfa_store.offset -= <const_int>
2237            cfa.offset = cfa_store.offset if cfa.reg == sp
2238            cfa.reg = sp
2239            cfa.base_offset = -cfa_store.offset
2240
2241   Rule 11:
2242   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2243   effects: cfa_store.offset += -/+ mode_size(mem)
2244            cfa.offset = cfa_store.offset if cfa.reg == sp
2245            cfa.reg = sp
2246            cfa.base_offset = -cfa_store.offset
2247
2248   Rule 12:
2249   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2250
2251        <reg2>)
2252   effects: cfa.reg = <reg1>
2253            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2254
2255   Rule 13:
2256   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2257   effects: cfa.reg = <reg1>
2258            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2259
2260   Rule 14:
2261   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2262   effects: cfa.reg = <reg1>
2263            cfa.base_offset = -cfa_temp.offset
2264            cfa_temp.offset -= mode_size(mem)
2265
2266   Rule 15:
2267   (set <reg> {unspec, unspec_volatile})
2268   effects: target-dependent
2269
2270   Rule 16:
2271   (set sp (and: sp <const_int>))
2272   constraints: cfa_store.reg == sp
2273   effects: current_fde.stack_realign = 1
2274            cfa_store.offset = 0
2275            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2276
2277   Rule 17:
2278   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2279   effects: cfa_store.offset += -/+ mode_size(mem)
2280
2281   Rule 18:
2282   (set (mem ({pre_inc, pre_dec} sp)) fp)
2283   constraints: fde->stack_realign == 1
2284   effects: cfa_store.offset = 0
2285            cfa.reg != HARD_FRAME_POINTER_REGNUM
2286
2287   Rule 19:
2288   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2289   constraints: fde->stack_realign == 1
2290                && cfa.offset == 0
2291                && cfa.indirect == 0
2292                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2293   effects: Use DW_CFA_def_cfa_expression to define cfa
2294            cfa.reg == fde->drap_reg  */
2295
2296 static void
2297 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2298 {
2299   rtx src, dest, span;
2300   HOST_WIDE_INT offset;
2301   dw_fde_ref fde;
2302
2303   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2304      the PARALLEL independently. The first element is always processed if
2305      it is a SET. This is for backward compatibility.   Other elements
2306      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2307      flag is set in them.  */
2308   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2309     {
2310       int par_index;
2311       int limit = XVECLEN (expr, 0);
2312       rtx elem;
2313
2314       /* PARALLELs have strict read-modify-write semantics, so we
2315          ought to evaluate every rvalue before changing any lvalue.
2316          It's cumbersome to do that in general, but there's an
2317          easy approximation that is enough for all current users:
2318          handle register saves before register assignments.  */
2319       if (GET_CODE (expr) == PARALLEL)
2320         for (par_index = 0; par_index < limit; par_index++)
2321           {
2322             elem = XVECEXP (expr, 0, par_index);
2323             if (GET_CODE (elem) == SET
2324                 && MEM_P (SET_DEST (elem))
2325                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2326               dwarf2out_frame_debug_expr (elem, label);
2327           }
2328
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)) || GET_CODE (expr) == SEQUENCE)
2334               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2335             dwarf2out_frame_debug_expr (elem, label);
2336           else if (GET_CODE (elem) == SET
2337                    && par_index != 0
2338                    && !RTX_FRAME_RELATED_P (elem))
2339             {
2340               /* Stack adjustment combining might combine some post-prologue
2341                  stack adjustment into a prologue stack adjustment.  */
2342               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2343
2344               if (offset != 0)
2345                 dwarf2out_stack_adjust (offset, label);
2346             }
2347         }
2348       return;
2349     }
2350
2351   gcc_assert (GET_CODE (expr) == SET);
2352
2353   src = SET_SRC (expr);
2354   dest = SET_DEST (expr);
2355
2356   if (REG_P (src))
2357     {
2358       rtx rsi = reg_saved_in (src);
2359       if (rsi)
2360         src = rsi;
2361     }
2362
2363   fde = current_fde ();
2364
2365   switch (GET_CODE (dest))
2366     {
2367     case REG:
2368       switch (GET_CODE (src))
2369         {
2370           /* Setting FP from SP.  */
2371         case REG:
2372           if (cfa.reg == (unsigned) REGNO (src))
2373             {
2374               /* Rule 1 */
2375               /* Update the CFA rule wrt SP or FP.  Make sure src is
2376                  relative to the current CFA register.
2377
2378                  We used to require that dest be either SP or FP, but the
2379                  ARM copies SP to a temporary register, and from there to
2380                  FP.  So we just rely on the backends to only set
2381                  RTX_FRAME_RELATED_P on appropriate insns.  */
2382               cfa.reg = REGNO (dest);
2383               cfa_temp.reg = cfa.reg;
2384               cfa_temp.offset = cfa.offset;
2385             }
2386           else
2387             {
2388               /* Saving a register in a register.  */
2389               gcc_assert (!fixed_regs [REGNO (dest)]
2390                           /* For the SPARC and its register window.  */
2391                           || (DWARF_FRAME_REGNUM (REGNO (src))
2392                               == DWARF_FRAME_RETURN_COLUMN));
2393
2394               /* After stack is aligned, we can only save SP in FP
2395                  if drap register is used.  In this case, we have
2396                  to restore stack pointer with the CFA value and we
2397                  don't generate this DWARF information.  */
2398               if (fde
2399                   && fde->stack_realign
2400                   && REGNO (src) == STACK_POINTER_REGNUM)
2401                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2402                             && fde->drap_reg != INVALID_REGNUM
2403                             && cfa.reg != REGNO (src));
2404               else
2405                 queue_reg_save (label, src, dest, 0);
2406             }
2407           break;
2408
2409         case PLUS:
2410         case MINUS:
2411         case LO_SUM:
2412           if (dest == stack_pointer_rtx)
2413             {
2414               /* Rule 2 */
2415               /* Adjusting SP.  */
2416               switch (GET_CODE (XEXP (src, 1)))
2417                 {
2418                 case CONST_INT:
2419                   offset = INTVAL (XEXP (src, 1));
2420                   break;
2421                 case REG:
2422                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2423                               == cfa_temp.reg);
2424                   offset = cfa_temp.offset;
2425                   break;
2426                 default:
2427                   gcc_unreachable ();
2428                 }
2429
2430               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2431                 {
2432                   /* Restoring SP from FP in the epilogue.  */
2433                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2434                   cfa.reg = STACK_POINTER_REGNUM;
2435                 }
2436               else if (GET_CODE (src) == LO_SUM)
2437                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2438                 ;
2439               else
2440                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2441
2442               if (GET_CODE (src) != MINUS)
2443                 offset = -offset;
2444               if (cfa.reg == STACK_POINTER_REGNUM)
2445                 cfa.offset += offset;
2446               if (cfa_store.reg == STACK_POINTER_REGNUM)
2447                 cfa_store.offset += offset;
2448             }
2449           else if (dest == hard_frame_pointer_rtx)
2450             {
2451               /* Rule 3 */
2452               /* Either setting the FP from an offset of the SP,
2453                  or adjusting the FP */
2454               gcc_assert (frame_pointer_needed);
2455
2456               gcc_assert (REG_P (XEXP (src, 0))
2457                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2458                           && CONST_INT_P (XEXP (src, 1)));
2459               offset = INTVAL (XEXP (src, 1));
2460               if (GET_CODE (src) != MINUS)
2461                 offset = -offset;
2462               cfa.offset += offset;
2463               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2464             }
2465           else
2466             {
2467               gcc_assert (GET_CODE (src) != MINUS);
2468
2469               /* Rule 4 */
2470               if (REG_P (XEXP (src, 0))
2471                   && REGNO (XEXP (src, 0)) == cfa.reg
2472                   && CONST_INT_P (XEXP (src, 1)))
2473                 {
2474                   /* Setting a temporary CFA register that will be copied
2475                      into the FP later on.  */
2476                   offset = - INTVAL (XEXP (src, 1));
2477                   cfa.offset += offset;
2478                   cfa.reg = REGNO (dest);
2479                   /* Or used to save regs to the stack.  */
2480                   cfa_temp.reg = cfa.reg;
2481                   cfa_temp.offset = cfa.offset;
2482                 }
2483
2484               /* Rule 5 */
2485               else if (REG_P (XEXP (src, 0))
2486                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2487                        && XEXP (src, 1) == stack_pointer_rtx)
2488                 {
2489                   /* Setting a scratch register that we will use instead
2490                      of SP for saving registers to the stack.  */
2491                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2492                   cfa_store.reg = REGNO (dest);
2493                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2494                 }
2495
2496               /* Rule 9 */
2497               else if (GET_CODE (src) == LO_SUM
2498                        && CONST_INT_P (XEXP (src, 1)))
2499                 {
2500                   cfa_temp.reg = REGNO (dest);
2501                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2502                 }
2503               else
2504                 gcc_unreachable ();
2505             }
2506           break;
2507
2508           /* Rule 6 */
2509         case CONST_INT:
2510           cfa_temp.reg = REGNO (dest);
2511           cfa_temp.offset = INTVAL (src);
2512           break;
2513
2514           /* Rule 7 */
2515         case IOR:
2516           gcc_assert (REG_P (XEXP (src, 0))
2517                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2518                       && CONST_INT_P (XEXP (src, 1)));
2519
2520           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2521             cfa_temp.reg = REGNO (dest);
2522           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2523           break;
2524
2525           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2526              which will fill in all of the bits.  */
2527           /* Rule 8 */
2528         case HIGH:
2529           break;
2530
2531           /* Rule 15 */
2532         case UNSPEC:
2533         case UNSPEC_VOLATILE:
2534           gcc_assert (targetm.dwarf_handle_frame_unspec);
2535           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2536           return;
2537
2538           /* Rule 16 */
2539         case AND:
2540           /* If this AND operation happens on stack pointer in prologue,
2541              we assume the stack is realigned and we extract the
2542              alignment.  */
2543           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2544             {
2545               /* We interpret reg_save differently with stack_realign set.
2546                  Thus we must flush whatever we have queued first.  */
2547               dwarf2out_flush_queued_reg_saves ();
2548
2549               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2550               fde->stack_realign = 1;
2551               fde->stack_realignment = INTVAL (XEXP (src, 1));
2552               cfa_store.offset = 0;
2553
2554               if (cfa.reg != STACK_POINTER_REGNUM
2555                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2556                 fde->drap_reg = cfa.reg;
2557             }
2558           return;
2559
2560         default:
2561           gcc_unreachable ();
2562         }
2563
2564       def_cfa_1 (label, &cfa);
2565       break;
2566
2567     case MEM:
2568
2569       /* Saving a register to the stack.  Make sure dest is relative to the
2570          CFA register.  */
2571       switch (GET_CODE (XEXP (dest, 0)))
2572         {
2573           /* Rule 10 */
2574           /* With a push.  */
2575         case PRE_MODIFY:
2576           /* We can't handle variable size modifications.  */
2577           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2578                       == CONST_INT);
2579           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2580
2581           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2582                       && cfa_store.reg == STACK_POINTER_REGNUM);
2583
2584           cfa_store.offset += offset;
2585           if (cfa.reg == STACK_POINTER_REGNUM)
2586             cfa.offset = cfa_store.offset;
2587
2588           offset = -cfa_store.offset;
2589           break;
2590
2591           /* Rule 11 */
2592         case PRE_INC:
2593         case PRE_DEC:
2594         case POST_DEC:
2595           offset = GET_MODE_SIZE (GET_MODE (dest));
2596           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2597             offset = -offset;
2598
2599           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2600                        == STACK_POINTER_REGNUM)
2601                       && cfa_store.reg == STACK_POINTER_REGNUM);
2602
2603           cfa_store.offset += offset;
2604
2605           /* Rule 18: If stack is aligned, we will use FP as a
2606              reference to represent the address of the stored
2607              regiser.  */
2608           if (fde
2609               && fde->stack_realign
2610               && src == hard_frame_pointer_rtx)
2611             {
2612               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2613               cfa_store.offset = 0;
2614             }
2615
2616           if (cfa.reg == STACK_POINTER_REGNUM)
2617             cfa.offset = cfa_store.offset;
2618
2619           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2620             offset += -cfa_store.offset;
2621           else
2622             offset = -cfa_store.offset;
2623           break;
2624
2625           /* Rule 12 */
2626           /* With an offset.  */
2627         case PLUS:
2628         case MINUS:
2629         case LO_SUM:
2630           {
2631             int regno;
2632
2633             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2634                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2635             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2636             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2637               offset = -offset;
2638
2639             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2640
2641             if (cfa.reg == (unsigned) regno)
2642               offset -= cfa.offset;
2643             else if (cfa_store.reg == (unsigned) regno)
2644               offset -= cfa_store.offset;
2645             else
2646               {
2647                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2648                 offset -= cfa_temp.offset;
2649               }
2650           }
2651           break;
2652
2653           /* Rule 13 */
2654           /* Without an offset.  */
2655         case REG:
2656           {
2657             int regno = REGNO (XEXP (dest, 0));
2658
2659             if (cfa.reg == (unsigned) regno)
2660               offset = -cfa.offset;
2661             else if (cfa_store.reg == (unsigned) regno)
2662               offset = -cfa_store.offset;
2663             else
2664               {
2665                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2666                 offset = -cfa_temp.offset;
2667               }
2668           }
2669           break;
2670
2671           /* Rule 14 */
2672         case POST_INC:
2673           gcc_assert (cfa_temp.reg
2674                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2675           offset = -cfa_temp.offset;
2676           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2677           break;
2678
2679         default:
2680           gcc_unreachable ();
2681         }
2682
2683         /* Rule 17 */
2684         /* If the source operand of this MEM operation is not a
2685            register, basically the source is return address.  Here
2686            we only care how much stack grew and we don't save it.  */
2687       if (!REG_P (src))
2688         break;
2689
2690       if (REGNO (src) != STACK_POINTER_REGNUM
2691           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2692           && (unsigned) REGNO (src) == cfa.reg)
2693         {
2694           /* We're storing the current CFA reg into the stack.  */
2695
2696           if (cfa.offset == 0)
2697             {
2698               /* Rule 19 */
2699               /* If stack is aligned, putting CFA reg into stack means
2700                  we can no longer use reg + offset to represent CFA.
2701                  Here we use DW_CFA_def_cfa_expression instead.  The
2702                  result of this expression equals to the original CFA
2703                  value.  */
2704               if (fde
2705                   && fde->stack_realign
2706                   && cfa.indirect == 0
2707                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2708                 {
2709                   dw_cfa_location cfa_exp;
2710
2711                   gcc_assert (fde->drap_reg == cfa.reg);
2712
2713                   cfa_exp.indirect = 1;
2714                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2715                   cfa_exp.base_offset = offset;
2716                   cfa_exp.offset = 0;
2717
2718                   fde->drap_reg_saved = 1;
2719
2720                   def_cfa_1 (label, &cfa_exp);
2721                   break;
2722                 }
2723
2724               /* If the source register is exactly the CFA, assume
2725                  we're saving SP like any other register; this happens
2726                  on the ARM.  */
2727               def_cfa_1 (label, &cfa);
2728               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2729               break;
2730             }
2731           else
2732             {
2733               /* Otherwise, we'll need to look in the stack to
2734                  calculate the CFA.  */
2735               rtx x = XEXP (dest, 0);
2736
2737               if (!REG_P (x))
2738                 x = XEXP (x, 0);
2739               gcc_assert (REG_P (x));
2740
2741               cfa.reg = REGNO (x);
2742               cfa.base_offset = offset;
2743               cfa.indirect = 1;
2744               def_cfa_1 (label, &cfa);
2745               break;
2746             }
2747         }
2748
2749       def_cfa_1 (label, &cfa);
2750       {
2751         span = targetm.dwarf_register_span (src);
2752
2753         if (!span)
2754           queue_reg_save (label, src, NULL_RTX, offset);
2755         else
2756           {
2757             /* We have a PARALLEL describing where the contents of SRC
2758                live.  Queue register saves for each piece of the
2759                PARALLEL.  */
2760             int par_index;
2761             int limit;
2762             HOST_WIDE_INT span_offset = offset;
2763
2764             gcc_assert (GET_CODE (span) == PARALLEL);
2765
2766             limit = XVECLEN (span, 0);
2767             for (par_index = 0; par_index < limit; par_index++)
2768               {
2769                 rtx elem = XVECEXP (span, 0, par_index);
2770
2771                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2772                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2773               }
2774           }
2775       }
2776       break;
2777
2778     default:
2779       gcc_unreachable ();
2780     }
2781 }
2782
2783 /* Record call frame debugging information for INSN, which either
2784    sets SP or FP (adjusting how we calculate the frame address) or saves a
2785    register to the stack.  If INSN is NULL_RTX, initialize our state.
2786
2787    If AFTER_P is false, we're being called before the insn is emitted,
2788    otherwise after.  Call instructions get invoked twice.  */
2789
2790 void
2791 dwarf2out_frame_debug (rtx insn, bool after_p)
2792 {
2793   const char *label;
2794   rtx note, n;
2795   bool handled_one = false;
2796
2797   if (insn == NULL_RTX)
2798     {
2799       size_t i;
2800
2801       /* Flush any queued register saves.  */
2802       dwarf2out_flush_queued_reg_saves ();
2803
2804       /* Set up state for generating call frame debug info.  */
2805       lookup_cfa (&cfa);
2806       gcc_assert (cfa.reg
2807                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2808
2809       cfa.reg = STACK_POINTER_REGNUM;
2810       cfa_store = cfa;
2811       cfa_temp.reg = -1;
2812       cfa_temp.offset = 0;
2813
2814       for (i = 0; i < num_regs_saved_in_regs; i++)
2815         {
2816           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2817           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2818         }
2819       num_regs_saved_in_regs = 0;
2820
2821       if (barrier_args_size)
2822         {
2823           XDELETEVEC (barrier_args_size);
2824           barrier_args_size = NULL;
2825         }
2826       return;
2827     }
2828
2829   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2830     dwarf2out_flush_queued_reg_saves ();
2831
2832   if (!RTX_FRAME_RELATED_P (insn))
2833     {
2834       /* ??? This should be done unconditionally since stack adjustments
2835          matter if the stack pointer is not the CFA register anymore but
2836          is still used to save registers.  */
2837       if (!ACCUMULATE_OUTGOING_ARGS)
2838         dwarf2out_notice_stack_adjust (insn, after_p);
2839       return;
2840     }
2841
2842   label = dwarf2out_cfi_label (false);
2843   any_cfis_emitted = false;
2844
2845   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2846     switch (REG_NOTE_KIND (note))
2847       {
2848       case REG_FRAME_RELATED_EXPR:
2849         insn = XEXP (note, 0);
2850         goto found;
2851
2852       case REG_CFA_DEF_CFA:
2853         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2854         handled_one = true;
2855         break;
2856
2857       case REG_CFA_ADJUST_CFA:
2858         n = XEXP (note, 0);
2859         if (n == NULL)
2860           {
2861             n = PATTERN (insn);
2862             if (GET_CODE (n) == PARALLEL)
2863               n = XVECEXP (n, 0, 0);
2864           }
2865         dwarf2out_frame_debug_adjust_cfa (n, label);
2866         handled_one = true;
2867         break;
2868
2869       case REG_CFA_OFFSET:
2870         n = XEXP (note, 0);
2871         if (n == NULL)
2872           n = single_set (insn);
2873         dwarf2out_frame_debug_cfa_offset (n, label);
2874         handled_one = true;
2875         break;
2876
2877       case REG_CFA_REGISTER:
2878         n = XEXP (note, 0);
2879         if (n == NULL)
2880           {
2881             n = PATTERN (insn);
2882             if (GET_CODE (n) == PARALLEL)
2883               n = XVECEXP (n, 0, 0);
2884           }
2885         dwarf2out_frame_debug_cfa_register (n, label);
2886         handled_one = true;
2887         break;
2888
2889       case REG_CFA_EXPRESSION:
2890         n = XEXP (note, 0);
2891         if (n == NULL)
2892           n = single_set (insn);
2893         dwarf2out_frame_debug_cfa_expression (n, label);
2894         handled_one = true;
2895         break;
2896
2897       case REG_CFA_RESTORE:
2898         n = XEXP (note, 0);
2899         if (n == NULL)
2900           {
2901             n = PATTERN (insn);
2902             if (GET_CODE (n) == PARALLEL)
2903               n = XVECEXP (n, 0, 0);
2904             n = XEXP (n, 0);
2905           }
2906         dwarf2out_frame_debug_cfa_restore (n, label);
2907         handled_one = true;
2908         break;
2909
2910       case REG_CFA_SET_VDRAP:
2911         n = XEXP (note, 0);
2912         if (REG_P (n))
2913           {
2914             dw_fde_ref fde = current_fde ();
2915             if (fde)
2916               {
2917                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2918                 if (REG_P (n))
2919                   fde->vdrap_reg = REGNO (n);
2920               }
2921           }
2922         handled_one = true;
2923         break;
2924
2925       default:
2926         break;
2927       }
2928   if (handled_one)
2929     {
2930       if (any_cfis_emitted)
2931         dwarf2out_flush_queued_reg_saves ();
2932       return;
2933     }
2934
2935   insn = PATTERN (insn);
2936  found:
2937   dwarf2out_frame_debug_expr (insn, label);
2938
2939   /* Check again.  A parallel can save and update the same register.
2940      We could probably check just once, here, but this is safer than
2941      removing the check above.  */
2942   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2943     dwarf2out_flush_queued_reg_saves ();
2944 }
2945
2946 /* Determine if we need to save and restore CFI information around this
2947    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2948    we do need to save/restore, then emit the save now, and insert a
2949    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2950
2951 void
2952 dwarf2out_cfi_begin_epilogue (rtx insn)
2953 {
2954   bool saw_frp = false;
2955   rtx i;
2956
2957   /* Scan forward to the return insn, noticing if there are possible
2958      frame related insns.  */
2959   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2960     {
2961       if (!INSN_P (i))
2962         continue;
2963
2964       /* Look for both regular and sibcalls to end the block.  */
2965       if (returnjump_p (i))
2966         break;
2967       if (CALL_P (i) && SIBLING_CALL_P (i))
2968         break;
2969
2970       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2971         {
2972           int idx;
2973           rtx seq = PATTERN (i);
2974
2975           if (returnjump_p (XVECEXP (seq, 0, 0)))
2976             break;
2977           if (CALL_P (XVECEXP (seq, 0, 0))
2978               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2979             break;
2980
2981           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2982             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2983               saw_frp = true;
2984         }
2985
2986       if (RTX_FRAME_RELATED_P (i))
2987         saw_frp = true;
2988     }
2989
2990   /* If the port doesn't emit epilogue unwind info, we don't need a
2991      save/restore pair.  */
2992   if (!saw_frp)
2993     return;
2994
2995   /* Otherwise, search forward to see if the return insn was the last
2996      basic block of the function.  If so, we don't need save/restore.  */
2997   gcc_assert (i != NULL);
2998   i = next_real_insn (i);
2999   if (i == NULL)
3000     return;
3001
3002   /* Insert the restore before that next real insn in the stream, and before
3003      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3004      properly nested.  This should be after any label or alignment.  This
3005      will be pushed into the CFI stream by the function below.  */
3006   while (1)
3007     {
3008       rtx p = PREV_INSN (i);
3009       if (!NOTE_P (p))
3010         break;
3011       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3012         break;
3013       i = p;
3014     }
3015   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3016
3017   emit_cfa_remember = true;
3018
3019   /* And emulate the state save.  */
3020   gcc_assert (!cfa_remember.in_use);
3021   cfa_remember = cfa;
3022   cfa_remember.in_use = 1;
3023 }
3024
3025 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3026    required.  */
3027
3028 void
3029 dwarf2out_frame_debug_restore_state (void)
3030 {
3031   dw_cfi_ref cfi = new_cfi ();
3032   const char *label = dwarf2out_cfi_label (false);
3033
3034   cfi->dw_cfi_opc = DW_CFA_restore_state;
3035   add_fde_cfi (label, cfi);
3036
3037   gcc_assert (cfa_remember.in_use);
3038   cfa = cfa_remember;
3039   cfa_remember.in_use = 0;
3040 }
3041
3042 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3043 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3044  (enum dwarf_call_frame_info cfi);
3045
3046 static enum dw_cfi_oprnd_type
3047 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3048 {
3049   switch (cfi)
3050     {
3051     case DW_CFA_nop:
3052     case DW_CFA_GNU_window_save:
3053     case DW_CFA_remember_state:
3054     case DW_CFA_restore_state:
3055       return dw_cfi_oprnd_unused;
3056
3057     case DW_CFA_set_loc:
3058     case DW_CFA_advance_loc1:
3059     case DW_CFA_advance_loc2:
3060     case DW_CFA_advance_loc4:
3061     case DW_CFA_MIPS_advance_loc8:
3062       return dw_cfi_oprnd_addr;
3063
3064     case DW_CFA_offset:
3065     case DW_CFA_offset_extended:
3066     case DW_CFA_def_cfa:
3067     case DW_CFA_offset_extended_sf:
3068     case DW_CFA_def_cfa_sf:
3069     case DW_CFA_restore:
3070     case DW_CFA_restore_extended:
3071     case DW_CFA_undefined:
3072     case DW_CFA_same_value:
3073     case DW_CFA_def_cfa_register:
3074     case DW_CFA_register:
3075     case DW_CFA_expression:
3076       return dw_cfi_oprnd_reg_num;
3077
3078     case DW_CFA_def_cfa_offset:
3079     case DW_CFA_GNU_args_size:
3080     case DW_CFA_def_cfa_offset_sf:
3081       return dw_cfi_oprnd_offset;
3082
3083     case DW_CFA_def_cfa_expression:
3084       return dw_cfi_oprnd_loc;
3085
3086     default:
3087       gcc_unreachable ();
3088     }
3089 }
3090
3091 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3092 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3093  (enum dwarf_call_frame_info cfi);
3094
3095 static enum dw_cfi_oprnd_type
3096 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3097 {
3098   switch (cfi)
3099     {
3100     case DW_CFA_def_cfa:
3101     case DW_CFA_def_cfa_sf:
3102     case DW_CFA_offset:
3103     case DW_CFA_offset_extended_sf:
3104     case DW_CFA_offset_extended:
3105       return dw_cfi_oprnd_offset;
3106
3107     case DW_CFA_register:
3108       return dw_cfi_oprnd_reg_num;
3109
3110     case DW_CFA_expression:
3111       return dw_cfi_oprnd_loc;
3112
3113     default:
3114       return dw_cfi_oprnd_unused;
3115     }
3116 }
3117
3118 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3119    switch to the data section instead, and write out a synthetic start label
3120    for collect2 the first time around.  */
3121
3122 static void
3123 switch_to_eh_frame_section (bool back)
3124 {
3125   tree label;
3126
3127 #ifdef EH_FRAME_SECTION_NAME
3128   if (eh_frame_section == 0)
3129     {
3130       int flags;
3131
3132       if (EH_TABLES_CAN_BE_READ_ONLY)
3133         {
3134           int fde_encoding;
3135           int per_encoding;
3136           int lsda_encoding;
3137
3138           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3139                                                        /*global=*/0);
3140           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3141                                                        /*global=*/1);
3142           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3143                                                         /*global=*/0);
3144           flags = ((! flag_pic
3145                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3146                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3147                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3148                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3149                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3150                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3151                    ? 0 : SECTION_WRITE);
3152         }
3153       else
3154         flags = SECTION_WRITE;
3155       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3156     }
3157 #endif /* EH_FRAME_SECTION_NAME */
3158
3159   if (eh_frame_section)
3160     switch_to_section (eh_frame_section);
3161   else
3162     {
3163       /* We have no special eh_frame section.  Put the information in
3164          the data section and emit special labels to guide collect2.  */
3165       switch_to_section (data_section);
3166
3167       if (!back)
3168         {
3169           label = get_file_function_name ("F");
3170           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3171           targetm.asm_out.globalize_label (asm_out_file,
3172                                            IDENTIFIER_POINTER (label));
3173           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3174         }
3175     }
3176 }
3177
3178 /* Switch [BACK] to the eh or debug frame table section, depending on
3179    FOR_EH.  */
3180
3181 static void
3182 switch_to_frame_table_section (int for_eh, bool back)
3183 {
3184   if (for_eh)
3185     switch_to_eh_frame_section (back);
3186   else
3187     {
3188       if (!debug_frame_section)
3189         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3190                                            SECTION_DEBUG, NULL);
3191       switch_to_section (debug_frame_section);
3192     }
3193 }
3194
3195 /* Output a Call Frame Information opcode and its operand(s).  */
3196
3197 static void
3198 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3199 {
3200   unsigned long r;
3201   HOST_WIDE_INT off;
3202
3203   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3204     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3205                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3206                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3207                          ((unsigned HOST_WIDE_INT)
3208                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3209   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3210     {
3211       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3212       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3213                            "DW_CFA_offset, column %#lx", r);
3214       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3215       dw2_asm_output_data_uleb128 (off, NULL);
3216     }
3217   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3218     {
3219       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3220       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3221                            "DW_CFA_restore, column %#lx", r);
3222     }
3223   else
3224     {
3225       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3226                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3227
3228       switch (cfi->dw_cfi_opc)
3229         {
3230         case DW_CFA_set_loc:
3231           if (for_eh)
3232             dw2_asm_output_encoded_addr_rtx (
3233                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3234                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3235                 false, NULL);
3236           else
3237             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3238                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3239           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3240           break;
3241
3242         case DW_CFA_advance_loc1:
3243           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3244                                 fde->dw_fde_current_label, NULL);
3245           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3246           break;
3247
3248         case DW_CFA_advance_loc2:
3249           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3250                                 fde->dw_fde_current_label, NULL);
3251           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3252           break;
3253
3254         case DW_CFA_advance_loc4:
3255           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3256                                 fde->dw_fde_current_label, NULL);
3257           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3258           break;
3259
3260         case DW_CFA_MIPS_advance_loc8:
3261           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3262                                 fde->dw_fde_current_label, NULL);
3263           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3264           break;
3265
3266         case DW_CFA_offset_extended:
3267           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3268           dw2_asm_output_data_uleb128 (r, NULL);
3269           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3270           dw2_asm_output_data_uleb128 (off, NULL);
3271           break;
3272
3273         case DW_CFA_def_cfa:
3274           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3275           dw2_asm_output_data_uleb128 (r, NULL);
3276           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3277           break;
3278
3279         case DW_CFA_offset_extended_sf:
3280           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3281           dw2_asm_output_data_uleb128 (r, NULL);
3282           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3283           dw2_asm_output_data_sleb128 (off, NULL);
3284           break;
3285
3286         case DW_CFA_def_cfa_sf:
3287           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3288           dw2_asm_output_data_uleb128 (r, NULL);
3289           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3290           dw2_asm_output_data_sleb128 (off, NULL);
3291           break;
3292
3293         case DW_CFA_restore_extended:
3294         case DW_CFA_undefined:
3295         case DW_CFA_same_value:
3296         case DW_CFA_def_cfa_register:
3297           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3298           dw2_asm_output_data_uleb128 (r, NULL);
3299           break;
3300
3301         case DW_CFA_register:
3302           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3303           dw2_asm_output_data_uleb128 (r, NULL);
3304           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3305           dw2_asm_output_data_uleb128 (r, NULL);
3306           break;
3307
3308         case DW_CFA_def_cfa_offset:
3309         case DW_CFA_GNU_args_size:
3310           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3311           break;
3312
3313         case DW_CFA_def_cfa_offset_sf:
3314           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3315           dw2_asm_output_data_sleb128 (off, NULL);
3316           break;
3317
3318         case DW_CFA_GNU_window_save:
3319           break;
3320
3321         case DW_CFA_def_cfa_expression:
3322         case DW_CFA_expression:
3323           output_cfa_loc (cfi, for_eh);
3324           break;
3325
3326         case DW_CFA_GNU_negative_offset_extended:
3327           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3328           gcc_unreachable ();
3329
3330         default:
3331           break;
3332         }
3333     }
3334 }
3335
3336 /* Similar, but do it via assembler directives instead.  */
3337
3338 static void
3339 output_cfi_directive (dw_cfi_ref cfi)
3340 {
3341   unsigned long r, r2;
3342
3343   switch (cfi->dw_cfi_opc)
3344     {
3345     case DW_CFA_advance_loc:
3346     case DW_CFA_advance_loc1:
3347     case DW_CFA_advance_loc2:
3348     case DW_CFA_advance_loc4:
3349     case DW_CFA_MIPS_advance_loc8:
3350     case DW_CFA_set_loc:
3351       /* Should only be created by add_fde_cfi in a code path not
3352          followed when emitting via directives.  The assembler is
3353          going to take care of this for us.  */
3354       gcc_unreachable ();
3355
3356     case DW_CFA_offset:
3357     case DW_CFA_offset_extended:
3358     case DW_CFA_offset_extended_sf:
3359       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3360       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3361                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3362       break;
3363
3364     case DW_CFA_restore:
3365     case DW_CFA_restore_extended:
3366       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3367       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3368       break;
3369
3370     case DW_CFA_undefined:
3371       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3372       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3373       break;
3374
3375     case DW_CFA_same_value:
3376       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3377       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3378       break;
3379
3380     case DW_CFA_def_cfa:
3381     case DW_CFA_def_cfa_sf:
3382       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3384                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3385       break;
3386
3387     case DW_CFA_def_cfa_register:
3388       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3389       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3390       break;
3391
3392     case DW_CFA_register:
3393       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3394       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3395       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3396       break;
3397
3398     case DW_CFA_def_cfa_offset:
3399     case DW_CFA_def_cfa_offset_sf:
3400       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3401                HOST_WIDE_INT_PRINT_DEC"\n",
3402                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3403       break;
3404
3405     case DW_CFA_remember_state:
3406       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3407       break;
3408     case DW_CFA_restore_state:
3409       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3410       break;
3411
3412     case DW_CFA_GNU_args_size:
3413       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3414       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415       if (flag_debug_asm)
3416         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3417                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3418       fputc ('\n', asm_out_file);
3419       break;
3420
3421     case DW_CFA_GNU_window_save:
3422       fprintf (asm_out_file, "\t.cfi_window_save\n");
3423       break;
3424
3425     case DW_CFA_def_cfa_expression:
3426     case DW_CFA_expression:
3427       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3428       output_cfa_loc_raw (cfi);
3429       fputc ('\n', asm_out_file);
3430       break;
3431
3432     default:
3433       gcc_unreachable ();
3434     }
3435 }
3436
3437 DEF_VEC_P (dw_cfi_ref);
3438 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3439
3440 /* Output CFIs to bring current FDE to the same state as after executing
3441    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3442    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3443    other arguments to pass to output_cfi.  */
3444
3445 static void
3446 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3447 {
3448   struct dw_cfi_struct cfi_buf;
3449   dw_cfi_ref cfi2;
3450   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3451   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3452   unsigned int len, idx;
3453
3454   for (;; cfi = cfi->dw_cfi_next)
3455     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3456       {
3457       case DW_CFA_advance_loc:
3458       case DW_CFA_advance_loc1:
3459       case DW_CFA_advance_loc2:
3460       case DW_CFA_advance_loc4:
3461       case DW_CFA_MIPS_advance_loc8:
3462       case DW_CFA_set_loc:
3463         /* All advances should be ignored.  */
3464         break;
3465       case DW_CFA_remember_state:
3466         {
3467           dw_cfi_ref args_size = cfi_args_size;
3468
3469           /* Skip everything between .cfi_remember_state and
3470              .cfi_restore_state.  */
3471           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3472             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3473               break;
3474             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3475               args_size = cfi2;
3476             else
3477               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3478
3479           if (cfi2 == NULL)
3480             goto flush_all;
3481           else
3482             {
3483               cfi = cfi2;
3484               cfi_args_size = args_size;
3485             }
3486           break;
3487         }
3488       case DW_CFA_GNU_args_size:
3489         cfi_args_size = cfi;
3490         break;
3491       case DW_CFA_GNU_window_save:
3492         goto flush_all;
3493       case DW_CFA_offset:
3494       case DW_CFA_offset_extended:
3495       case DW_CFA_offset_extended_sf:
3496       case DW_CFA_restore:
3497       case DW_CFA_restore_extended:
3498       case DW_CFA_undefined:
3499       case DW_CFA_same_value:
3500       case DW_CFA_register:
3501       case DW_CFA_val_offset:
3502       case DW_CFA_val_offset_sf:
3503       case DW_CFA_expression:
3504       case DW_CFA_val_expression:
3505       case DW_CFA_GNU_negative_offset_extended:
3506         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3507           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3508                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3509         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3510         break;
3511       case DW_CFA_def_cfa:
3512       case DW_CFA_def_cfa_sf:
3513       case DW_CFA_def_cfa_expression:
3514         cfi_cfa = cfi;
3515         cfi_cfa_offset = cfi;
3516         break;
3517       case DW_CFA_def_cfa_register:
3518         cfi_cfa = cfi;
3519         break;
3520       case DW_CFA_def_cfa_offset:
3521       case DW_CFA_def_cfa_offset_sf:
3522         cfi_cfa_offset = cfi;
3523         break;
3524       case DW_CFA_nop:
3525         gcc_assert (cfi == NULL);
3526       flush_all:
3527         len = VEC_length (dw_cfi_ref, regs);
3528         for (idx = 0; idx < len; idx++)
3529           {
3530             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3531             if (cfi2 != NULL
3532                 && cfi2->dw_cfi_opc != DW_CFA_restore
3533                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3534               {
3535                 if (do_cfi_asm)
3536                   output_cfi_directive (cfi2);
3537                 else
3538                   output_cfi (cfi2, fde, for_eh);
3539               }
3540           }
3541         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3542           {
3543             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3544             cfi_buf = *cfi_cfa;
3545             switch (cfi_cfa_offset->dw_cfi_opc)
3546               {
3547               case DW_CFA_def_cfa_offset:
3548                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3549                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3550                 break;
3551               case DW_CFA_def_cfa_offset_sf:
3552                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3553                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3554                 break;
3555               case DW_CFA_def_cfa:
3556               case DW_CFA_def_cfa_sf:
3557                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3558                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3559                 break;
3560               default:
3561                 gcc_unreachable ();
3562               }
3563             cfi_cfa = &cfi_buf;
3564           }
3565         else if (cfi_cfa_offset)
3566           cfi_cfa = cfi_cfa_offset;
3567         if (cfi_cfa)
3568           {
3569             if (do_cfi_asm)
3570               output_cfi_directive (cfi_cfa);
3571             else
3572               output_cfi (cfi_cfa, fde, for_eh);
3573           }
3574         cfi_cfa = NULL;
3575         cfi_cfa_offset = NULL;
3576         if (cfi_args_size
3577             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3578           {
3579             if (do_cfi_asm)
3580               output_cfi_directive (cfi_args_size);
3581             else
3582               output_cfi (cfi_args_size, fde, for_eh);
3583           }
3584         cfi_args_size = NULL;
3585         if (cfi == NULL)
3586           {
3587             VEC_free (dw_cfi_ref, heap, regs);
3588             return;
3589           }
3590         else if (do_cfi_asm)
3591           output_cfi_directive (cfi);
3592         else
3593           output_cfi (cfi, fde, for_eh);
3594         break;
3595       default:
3596         gcc_unreachable ();
3597     }
3598 }
3599
3600 /* Output one FDE.  */
3601
3602 static void
3603 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3604             char *section_start_label, int fde_encoding, char *augmentation,
3605             bool any_lsda_needed, int lsda_encoding)
3606 {
3607   const char *begin, *end;
3608   static unsigned int j;
3609   char l1[20], l2[20];
3610   dw_cfi_ref cfi;
3611
3612   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3613                                      /* empty */ 0);
3614   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3615                                   for_eh + j);
3616   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3617   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3618   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3619     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3620                          " indicating 64-bit DWARF extension");
3621   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3622                         "FDE Length");
3623   ASM_OUTPUT_LABEL (asm_out_file, l1);
3624
3625   if (for_eh)
3626     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3627   else
3628     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3629                            debug_frame_section, "FDE CIE offset");
3630
3631   if (!fde->dw_fde_switched_sections)
3632     {
3633       begin = fde->dw_fde_begin;
3634       end = fde->dw_fde_end;
3635     }
3636   else
3637     {
3638       /* For the first section, prefer dw_fde_begin over
3639          dw_fde_{hot,cold}_section_label, as the latter
3640          might be separated from the real start of the
3641          function by alignment padding.  */
3642       if (!second)
3643         begin = fde->dw_fde_begin;
3644       else if (fde->dw_fde_switched_cold_to_hot)
3645         begin = fde->dw_fde_hot_section_label;
3646       else
3647         begin = fde->dw_fde_unlikely_section_label;
3648       if (second ^ fde->dw_fde_switched_cold_to_hot)
3649         end = fde->dw_fde_unlikely_section_end_label;
3650       else
3651         end = fde->dw_fde_hot_section_end_label;
3652     }
3653
3654   if (for_eh)
3655     {
3656       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3657       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3658       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3659                                        "FDE initial location");
3660       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3661                             end, begin, "FDE address range");
3662     }
3663   else
3664     {
3665       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3666       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3667     }
3668
3669   if (augmentation[0])
3670     {
3671       if (any_lsda_needed)
3672         {
3673           int size = size_of_encoded_value (lsda_encoding);
3674
3675           if (lsda_encoding == DW_EH_PE_aligned)
3676             {
3677               int offset = (  4         /* Length */
3678                             + 4         /* CIE offset */
3679                             + 2 * size_of_encoded_value (fde_encoding)
3680                             + 1         /* Augmentation size */ );
3681               int pad = -offset & (PTR_SIZE - 1);
3682
3683               size += pad;
3684               gcc_assert (size_of_uleb128 (size) == 1);
3685             }
3686
3687           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3688
3689           if (fde->uses_eh_lsda)
3690             {
3691               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3692                                            fde->funcdef_number);
3693               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3694                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3695                                                false,
3696                                                "Language Specific Data Area");
3697             }
3698           else
3699             {
3700               if (lsda_encoding == DW_EH_PE_aligned)
3701                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3702               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3703                                    "Language Specific Data Area (none)");
3704             }
3705         }
3706       else
3707         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3708     }
3709
3710   /* Loop through the Call Frame Instructions associated with
3711      this FDE.  */
3712   fde->dw_fde_current_label = begin;
3713   if (!fde->dw_fde_switched_sections)
3714     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3715       output_cfi (cfi, fde, for_eh);
3716   else if (!second)
3717     {
3718       if (fde->dw_fde_switch_cfi)
3719         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3720           {
3721             output_cfi (cfi, fde, for_eh);
3722             if (cfi == fde->dw_fde_switch_cfi)
3723               break;
3724           }
3725     }
3726   else
3727     {
3728       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3729
3730       if (fde->dw_fde_switch_cfi)
3731         {
3732           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3733           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3734           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3735           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3736         }
3737       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3738         output_cfi (cfi, fde, for_eh);
3739     }
3740
3741   /* If we are to emit a ref/link from function bodies to their frame tables,
3742      do it now.  This is typically performed to make sure that tables
3743      associated with functions are dragged with them and not discarded in
3744      garbage collecting links. We need to do this on a per function basis to
3745      cope with -ffunction-sections.  */
3746
3747 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3748   /* Switch to the function section, emit the ref to the tables, and
3749      switch *back* into the table section.  */
3750   switch_to_section (function_section (fde->decl));
3751   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3752   switch_to_frame_table_section (for_eh, true);
3753 #endif
3754
3755   /* Pad the FDE out to an address sized boundary.  */
3756   ASM_OUTPUT_ALIGN (asm_out_file,
3757                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3758   ASM_OUTPUT_LABEL (asm_out_file, l2);
3759
3760   j += 2;
3761 }
3762
3763 /* Return true if frame description entry FDE is needed for EH.  */
3764
3765 static bool
3766 fde_needed_for_eh_p (dw_fde_ref fde)
3767 {
3768   if (flag_asynchronous_unwind_tables)
3769     return true;
3770
3771   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3772     return true;
3773
3774   if (fde->uses_eh_lsda)
3775     return true;
3776
3777   /* If exceptions are enabled, we have collected nothrow info.  */
3778   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3779     return false;
3780
3781   return true;
3782 }
3783
3784 /* Output the call frame information used to record information
3785    that relates to calculating the frame pointer, and records the
3786    location of saved registers.  */
3787
3788 static void
3789 output_call_frame_info (int for_eh)
3790 {
3791   unsigned int i;
3792   dw_fde_ref fde;
3793   dw_cfi_ref cfi;
3794   char l1[20], l2[20], section_start_label[20];
3795   bool any_lsda_needed = false;
3796   char augmentation[6];
3797   int augmentation_size;
3798   int fde_encoding = DW_EH_PE_absptr;
3799   int per_encoding = DW_EH_PE_absptr;
3800   int lsda_encoding = DW_EH_PE_absptr;
3801   int return_reg;
3802   rtx personality = NULL;
3803   int dw_cie_version;
3804
3805   /* Don't emit a CIE if there won't be any FDEs.  */
3806   if (fde_table_in_use == 0)
3807     return;
3808
3809   /* Nothing to do if the assembler's doing it all.  */
3810   if (dwarf2out_do_cfi_asm ())
3811     return;
3812
3813   /* If we don't have any functions we'll want to unwind out of, don't emit
3814      any EH unwind information.  If we make FDEs linkonce, we may have to
3815      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3816      want to avoid having an FDE kept around when the function it refers to
3817      is discarded.  Example where this matters: a primary function template
3818      in C++ requires EH information, an explicit specialization doesn't.  */
3819   if (for_eh)
3820     {
3821       bool any_eh_needed = false;
3822
3823       for (i = 0; i < fde_table_in_use; i++)
3824         if (fde_table[i].uses_eh_lsda)
3825           any_eh_needed = any_lsda_needed = true;
3826         else if (fde_needed_for_eh_p (&fde_table[i]))
3827           any_eh_needed = true;
3828         else if (TARGET_USES_WEAK_UNWIND_INFO)
3829           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3830                                              1, 1);
3831
3832       if (!any_eh_needed)
3833         return;
3834     }
3835
3836   /* We're going to be generating comments, so turn on app.  */
3837   if (flag_debug_asm)
3838     app_enable ();
3839
3840   /* Switch to the proper frame section, first time.  */
3841   switch_to_frame_table_section (for_eh, false);
3842
3843   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3844   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3845
3846   /* Output the CIE.  */
3847   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3848   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3849   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3850     dw2_asm_output_data (4, 0xffffffff,
3851       "Initial length escape value indicating 64-bit DWARF extension");
3852   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3853                         "Length of Common Information Entry");
3854   ASM_OUTPUT_LABEL (asm_out_file, l1);
3855
3856   /* Now that the CIE pointer is PC-relative for EH,
3857      use 0 to identify the CIE.  */
3858   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3859                        (for_eh ? 0 : DWARF_CIE_ID),
3860                        "CIE Identifier Tag");
3861
3862   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3863      use CIE version 1, unless that would produce incorrect results
3864      due to overflowing the return register column.  */
3865   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3866   dw_cie_version = 1;
3867   if (return_reg >= 256 || dwarf_version > 2)
3868     dw_cie_version = 3;
3869   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3870
3871   augmentation[0] = 0;
3872   augmentation_size = 0;
3873
3874   personality = current_unit_personality;
3875   if (for_eh)
3876     {
3877       char *p;
3878
3879       /* Augmentation:
3880          z      Indicates that a uleb128 is present to size the
3881                 augmentation section.
3882          L      Indicates the encoding (and thus presence) of
3883                 an LSDA pointer in the FDE augmentation.
3884          R      Indicates a non-default pointer encoding for
3885                 FDE code pointers.
3886          P      Indicates the presence of an encoding + language
3887                 personality routine in the CIE augmentation.  */
3888
3889       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3890       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3891       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3892
3893       p = augmentation + 1;
3894       if (personality)
3895         {
3896           *p++ = 'P';
3897           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3898           assemble_external_libcall (personality);
3899         }
3900       if (any_lsda_needed)
3901         {
3902           *p++ = 'L';
3903           augmentation_size += 1;
3904         }
3905       if (fde_encoding != DW_EH_PE_absptr)
3906         {
3907           *p++ = 'R';
3908           augmentation_size += 1;
3909         }
3910       if (p > augmentation + 1)
3911         {
3912           augmentation[0] = 'z';
3913           *p = '\0';
3914         }
3915
3916       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3917       if (personality && per_encoding == DW_EH_PE_aligned)
3918         {
3919           int offset = (  4             /* Length */
3920                         + 4             /* CIE Id */
3921                         + 1             /* CIE version */
3922                         + strlen (augmentation) + 1     /* Augmentation */
3923                         + size_of_uleb128 (1)           /* Code alignment */
3924                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3925                         + 1             /* RA column */
3926                         + 1             /* Augmentation size */
3927                         + 1             /* Personality encoding */ );
3928           int pad = -offset & (PTR_SIZE - 1);
3929
3930           augmentation_size += pad;
3931
3932           /* Augmentations should be small, so there's scarce need to
3933              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3934           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3935         }
3936     }
3937
3938   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3939   if (dw_cie_version >= 4)
3940     {
3941       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3942       dw2_asm_output_data (1, 0, "CIE Segment Size");
3943     }
3944   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3945   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3946                                "CIE Data Alignment Factor");
3947
3948   if (dw_cie_version == 1)
3949     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3950   else
3951     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3952
3953   if (augmentation[0])
3954     {
3955       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3956       if (personality)
3957         {
3958           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3959                                eh_data_format_name (per_encoding));
3960           dw2_asm_output_encoded_addr_rtx (per_encoding,
3961                                            personality,
3962                                            true, NULL);
3963         }
3964
3965       if (any_lsda_needed)
3966         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3967                              eh_data_format_name (lsda_encoding));
3968
3969       if (fde_encoding != DW_EH_PE_absptr)
3970         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3971                              eh_data_format_name (fde_encoding));
3972     }
3973
3974   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3975     output_cfi (cfi, NULL, for_eh);
3976
3977   /* Pad the CIE out to an address sized boundary.  */
3978   ASM_OUTPUT_ALIGN (asm_out_file,
3979                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3980   ASM_OUTPUT_LABEL (asm_out_file, l2);
3981
3982   /* Loop through all of the FDE's.  */
3983   for (i = 0; i < fde_table_in_use; i++)
3984     {
3985       unsigned int k;
3986       fde = &fde_table[i];
3987
3988       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3989       if (for_eh && !fde_needed_for_eh_p (fde))
3990         continue;
3991
3992       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3993         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3994                     augmentation, any_lsda_needed, lsda_encoding);
3995     }
3996
3997   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3998     dw2_asm_output_data (4, 0, "End of Table");
3999 #ifdef MIPS_DEBUGGING_INFO
4000   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4001      get a value of 0.  Putting .align 0 after the label fixes it.  */
4002   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4003 #endif
4004
4005   /* Turn off app to make assembly quicker.  */
4006   if (flag_debug_asm)
4007     app_disable ();
4008 }
4009
4010 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4011
4012 static void
4013 dwarf2out_do_cfi_startproc (bool second)
4014 {
4015   int enc;
4016   rtx ref;
4017   rtx personality = get_personality_function (current_function_decl);
4018
4019   fprintf (asm_out_file, "\t.cfi_startproc\n");
4020
4021   if (personality)
4022     {
4023       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4024       ref = personality;
4025
4026       /* ??? The GAS support isn't entirely consistent.  We have to
4027          handle indirect support ourselves, but PC-relative is done
4028          in the assembler.  Further, the assembler can't handle any
4029          of the weirder relocation types.  */
4030       if (enc & DW_EH_PE_indirect)
4031         ref = dw2_force_const_mem (ref, true);
4032
4033       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4034       output_addr_const (asm_out_file, ref);
4035       fputc ('\n', asm_out_file);
4036     }
4037
4038   if (crtl->uses_eh_lsda)
4039     {
4040       char lab[20];
4041
4042       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4043       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4044                                    current_function_funcdef_no);
4045       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4046       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4047
4048       if (enc & DW_EH_PE_indirect)
4049         ref = dw2_force_const_mem (ref, true);
4050
4051       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4052       output_addr_const (asm_out_file, ref);
4053       fputc ('\n', asm_out_file);
4054     }
4055 }
4056
4057 /* Output a marker (i.e. a label) for the beginning of a function, before
4058    the prologue.  */
4059
4060 void
4061 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4062                           const char *file ATTRIBUTE_UNUSED)
4063 {
4064   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4065   char * dup_label;
4066   dw_fde_ref fde;
4067   section *fnsec;
4068   bool do_frame;
4069
4070   current_function_func_begin_label = NULL;
4071
4072   do_frame = dwarf2out_do_frame ();
4073
4074   /* ??? current_function_func_begin_label is also used by except.c for
4075      call-site information.  We must emit this label if it might be used.  */
4076   if (!do_frame
4077       && (!flag_exceptions
4078           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4079     return;
4080
4081   fnsec = function_section (current_function_decl);
4082   switch_to_section (fnsec);
4083   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4084                                current_function_funcdef_no);
4085   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4086                           current_function_funcdef_no);
4087   dup_label = xstrdup (label);
4088   current_function_func_begin_label = dup_label;
4089
4090   /* We can elide the fde allocation if we're not emitting debug info.  */
4091   if (!do_frame)
4092     return;
4093
4094   /* Expand the fde table if necessary.  */
4095   if (fde_table_in_use == fde_table_allocated)
4096     {
4097       fde_table_allocated += FDE_TABLE_INCREMENT;
4098       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4099       memset (fde_table + fde_table_in_use, 0,
4100               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4101     }
4102
4103   /* Record the FDE associated with this function.  */
4104   current_funcdef_fde = fde_table_in_use;
4105
4106   /* Add the new FDE at the end of the fde_table.  */
4107   fde = &fde_table[fde_table_in_use++];
4108   fde->decl = current_function_decl;
4109   fde->dw_fde_begin = dup_label;
4110   fde->dw_fde_current_label = dup_label;
4111   fde->dw_fde_hot_section_label = NULL;
4112   fde->dw_fde_hot_section_end_label = NULL;
4113   fde->dw_fde_unlikely_section_label = NULL;
4114   fde->dw_fde_unlikely_section_end_label = NULL;
4115   fde->dw_fde_switched_sections = 0;
4116   fde->dw_fde_switched_cold_to_hot = 0;
4117   fde->dw_fde_end = NULL;
4118   fde->dw_fde_vms_end_prologue = NULL;
4119   fde->dw_fde_vms_begin_epilogue = NULL;
4120   fde->dw_fde_cfi = NULL;
4121   fde->dw_fde_switch_cfi = NULL;
4122   fde->funcdef_number = current_function_funcdef_no;
4123   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4124   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4125   fde->nothrow = crtl->nothrow;
4126   fde->drap_reg = INVALID_REGNUM;
4127   fde->vdrap_reg = INVALID_REGNUM;
4128   if (flag_reorder_blocks_and_partition)
4129     {
4130       section *unlikelysec;
4131       if (first_function_block_is_cold)
4132         fde->in_std_section = 1;
4133       else
4134         fde->in_std_section
4135           = (fnsec == text_section
4136              || (cold_text_section && fnsec == cold_text_section));
4137       unlikelysec = unlikely_text_section ();
4138       fde->cold_in_std_section
4139         = (unlikelysec == text_section
4140            || (cold_text_section && unlikelysec == cold_text_section));
4141     }
4142   else
4143     {
4144       fde->in_std_section
4145         = (fnsec == text_section
4146            || (cold_text_section && fnsec == cold_text_section));
4147       fde->cold_in_std_section = 0;
4148     }
4149
4150   args_size = old_args_size = 0;
4151
4152   /* We only want to output line number information for the genuine dwarf2
4153      prologue case, not the eh frame case.  */
4154 #ifdef DWARF2_DEBUGGING_INFO
4155   if (file)
4156     dwarf2out_source_line (line, file, 0, true);
4157 #endif
4158
4159   if (dwarf2out_do_cfi_asm ())
4160     dwarf2out_do_cfi_startproc (false);
4161   else
4162     {
4163       rtx personality = get_personality_function (current_function_decl);
4164       if (!current_unit_personality)
4165         current_unit_personality = personality;
4166
4167       /* We cannot keep a current personality per function as without CFI
4168          asm, at the point where we emit the CFI data, there is no current
4169          function anymore.  */
4170       if (personality && current_unit_personality != personality)
4171         sorry ("multiple EH personalities are supported only with assemblers "
4172                "supporting .cfi_personality directive");
4173     }
4174 }
4175
4176 /* Output a marker (i.e. a label) for the end of the generated code
4177    for a function prologue.  This gets called *after* the prologue code has
4178    been generated.  */
4179
4180 void
4181 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4182                         const char *file ATTRIBUTE_UNUSED)
4183 {
4184   dw_fde_ref fde;
4185   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4186
4187   /* Output a label to mark the endpoint of the code generated for this
4188      function.  */
4189   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4190                                current_function_funcdef_no);
4191   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4192                           current_function_funcdef_no);
4193   fde = &fde_table[fde_table_in_use - 1];
4194   fde->dw_fde_vms_end_prologue = xstrdup (label);
4195 }
4196
4197 /* Output a marker (i.e. a label) for the beginning of the generated code
4198    for a function epilogue.  This gets called *before* the prologue code has
4199    been generated.  */
4200
4201 void
4202 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4203                           const char *file ATTRIBUTE_UNUSED)
4204 {
4205   dw_fde_ref fde;
4206   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4207
4208   fde = &fde_table[fde_table_in_use - 1];
4209   if (fde->dw_fde_vms_begin_epilogue)
4210     return;
4211
4212   /* Output a label to mark the endpoint of the code generated for this
4213      function.  */
4214   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4215                                current_function_funcdef_no);
4216   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4217                           current_function_funcdef_no);
4218   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4219 }
4220
4221 /* Output a marker (i.e. a label) for the absolute end of the generated code
4222    for a function definition.  This gets called *after* the epilogue code has
4223    been generated.  */
4224
4225 void
4226 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4227                         const char *file ATTRIBUTE_UNUSED)
4228 {
4229   dw_fde_ref fde;
4230   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4231
4232   last_var_location_insn = NULL_RTX;
4233
4234   if (dwarf2out_do_cfi_asm ())
4235     fprintf (asm_out_file, "\t.cfi_endproc\n");
4236
4237   /* Output a label to mark the endpoint of the code generated for this
4238      function.  */
4239   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4240                                current_function_funcdef_no);
4241   ASM_OUTPUT_LABEL (asm_out_file, label);
4242   fde = current_fde ();
4243   gcc_assert (fde != NULL);
4244   fde->dw_fde_end = xstrdup (label);
4245 }
4246
4247 void
4248 dwarf2out_frame_init (void)
4249 {
4250   /* Allocate the initial hunk of the fde_table.  */
4251   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4252   fde_table_allocated = FDE_TABLE_INCREMENT;
4253   fde_table_in_use = 0;
4254
4255   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4256      sake of lookup_cfa.  */
4257
4258   /* On entry, the Canonical Frame Address is at SP.  */
4259   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4260
4261   if (targetm.debug_unwind_info () == UI_DWARF2
4262       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4263     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4264 }
4265
4266 void
4267 dwarf2out_frame_finish (void)
4268 {
4269   /* Output call frame information.  */
4270   if (targetm.debug_unwind_info () == UI_DWARF2)
4271     output_call_frame_info (0);
4272
4273   /* Output another copy for the unwinder.  */
4274   if ((flag_unwind_tables || flag_exceptions)
4275       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4276     output_call_frame_info (1);
4277 }
4278
4279 /* Note that the current function section is being used for code.  */
4280
4281 static void
4282 dwarf2out_note_section_used (void)
4283 {
4284   section *sec = current_function_section ();
4285   if (sec == text_section)
4286     text_section_used = true;
4287   else if (sec == cold_text_section)
4288     cold_text_section_used = true;
4289 }
4290
4291 void
4292 dwarf2out_switch_text_section (void)
4293 {
4294   dw_fde_ref fde = current_fde ();
4295
4296   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4297
4298   fde->dw_fde_switched_sections = 1;
4299   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4300
4301   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4302   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4303   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4304   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4305   have_multiple_function_sections = true;
4306
4307   /* Reset the current label on switching text sections, so that we
4308      don't attempt to advance_loc4 between labels in different sections.  */
4309   fde->dw_fde_current_label = NULL;
4310
4311   /* There is no need to mark used sections when not debugging.  */
4312   if (cold_text_section != NULL)
4313     dwarf2out_note_section_used ();
4314
4315   if (dwarf2out_do_cfi_asm ())
4316     fprintf (asm_out_file, "\t.cfi_endproc\n");
4317
4318   /* Now do the real section switch.  */
4319   switch_to_section (current_function_section ());
4320
4321   if (dwarf2out_do_cfi_asm ())
4322     {
4323       dwarf2out_do_cfi_startproc (true);
4324       /* As this is a different FDE, insert all current CFI instructions
4325          again.  */
4326       output_cfis (fde->dw_fde_cfi, true, fde, true);
4327     }
4328   else
4329     {
4330       dw_cfi_ref cfi = fde->dw_fde_cfi;
4331
4332       cfi = fde->dw_fde_cfi;
4333       if (cfi)
4334         while (cfi->dw_cfi_next != NULL)
4335           cfi = cfi->dw_cfi_next;
4336       fde->dw_fde_switch_cfi = cfi;
4337     }
4338 }
4339 \f
4340 /* And now, the subset of the debugging information support code necessary
4341    for emitting location expressions.  */
4342
4343 /* Data about a single source file.  */
4344 struct GTY(()) dwarf_file_data {
4345   const char * filename;
4346   int emitted_number;
4347 };
4348
4349 typedef struct dw_val_struct *dw_val_ref;
4350 typedef struct die_struct *dw_die_ref;
4351 typedef const struct die_struct *const_dw_die_ref;
4352 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4353 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4354
4355 typedef struct GTY(()) deferred_locations_struct
4356 {
4357   tree variable;
4358   dw_die_ref die;
4359 } deferred_locations;
4360
4361 DEF_VEC_O(deferred_locations);
4362 DEF_VEC_ALLOC_O(deferred_locations,gc);
4363
4364 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4365
4366 DEF_VEC_P(dw_die_ref);
4367 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4368
4369 /* Each DIE may have a series of attribute/value pairs.  Values
4370    can take on several forms.  The forms that are used in this
4371    implementation are listed below.  */
4372
4373 enum dw_val_class
4374 {
4375   dw_val_class_addr,
4376   dw_val_class_offset,
4377   dw_val_class_loc,
4378   dw_val_class_loc_list,
4379   dw_val_class_range_list,
4380   dw_val_class_const,
4381   dw_val_class_unsigned_const,
4382   dw_val_class_const_double,
4383   dw_val_class_vec,
4384   dw_val_class_flag,
4385   dw_val_class_die_ref,
4386   dw_val_class_fde_ref,
4387   dw_val_class_lbl_id,
4388   dw_val_class_lineptr,
4389   dw_val_class_str,
4390   dw_val_class_macptr,
4391   dw_val_class_file,
4392   dw_val_class_data8,
4393   dw_val_class_decl_ref,
4394   dw_val_class_vms_delta
4395 };
4396
4397 /* Describe a floating point constant value, or a vector constant value.  */
4398
4399 typedef struct GTY(()) dw_vec_struct {
4400   unsigned char * GTY((length ("%h.length"))) array;
4401   unsigned length;
4402   unsigned elt_size;
4403 }
4404 dw_vec_const;
4405
4406 /* The dw_val_node describes an attribute's value, as it is
4407    represented internally.  */
4408
4409 typedef struct GTY(()) dw_val_struct {
4410   enum dw_val_class val_class;
4411   union dw_val_struct_union
4412     {
4413       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4414       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4415       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4416       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4417       HOST_WIDE_INT GTY ((default)) val_int;
4418       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4419       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4420       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4421       struct dw_val_die_union
4422         {
4423           dw_die_ref die;
4424           int external;
4425         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4426       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4427       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4428       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4429       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4430       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4431       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4432       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4433       struct dw_val_vms_delta_union
4434         {
4435           char * lbl1;
4436           char * lbl2;
4437         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4438     }
4439   GTY ((desc ("%1.val_class"))) v;
4440 }
4441 dw_val_node;
4442
4443 /* Locations in memory are described using a sequence of stack machine
4444    operations.  */
4445
4446 typedef struct GTY(()) dw_loc_descr_struct {
4447   dw_loc_descr_ref dw_loc_next;
4448   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4449   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4450      from DW_OP_addr with a dtp-relative symbol relocation.  */
4451   unsigned int dtprel : 1;
4452   int dw_loc_addr;
4453   dw_val_node dw_loc_oprnd1;
4454   dw_val_node dw_loc_oprnd2;
4455 }
4456 dw_loc_descr_node;
4457
4458 /* Location lists are ranges + location descriptions for that range,
4459    so you can track variables that are in different places over
4460    their entire life.  */
4461 typedef struct GTY(()) dw_loc_list_struct {
4462   dw_loc_list_ref dw_loc_next;
4463   const char *begin; /* Label for begin address of range */
4464   const char *end;  /* Label for end address of range */
4465   char *ll_symbol; /* Label for beginning of location list.
4466                       Only on head of list */
4467   const char *section; /* Section this loclist is relative to */
4468   dw_loc_descr_ref expr;
4469   hashval_t hash;
4470   bool emitted;
4471 } dw_loc_list_node;
4472
4473 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4474
4475 /* Convert a DWARF stack opcode into its string name.  */
4476
4477 static const char *
4478 dwarf_stack_op_name (unsigned int op)
4479 {
4480   switch (op)
4481     {
4482     case DW_OP_addr:
4483       return "DW_OP_addr";
4484     case DW_OP_deref:
4485       return "DW_OP_deref";
4486     case DW_OP_const1u:
4487       return "DW_OP_const1u";
4488     case DW_OP_const1s:
4489       return "DW_OP_const1s";
4490     case DW_OP_const2u:
4491       return "DW_OP_const2u";
4492     case DW_OP_const2s:
4493       return "DW_OP_const2s";
4494     case DW_OP_const4u:
4495       return "DW_OP_const4u";
4496     case DW_OP_const4s:
4497       return "DW_OP_const4s";
4498     case DW_OP_const8u:
4499       return "DW_OP_const8u";
4500     case DW_OP_const8s:
4501       return "DW_OP_const8s";
4502     case DW_OP_constu:
4503       return "DW_OP_constu";
4504     case DW_OP_consts:
4505       return "DW_OP_consts";
4506     case DW_OP_dup:
4507       return "DW_OP_dup";
4508     case DW_OP_drop:
4509       return "DW_OP_drop";
4510     case DW_OP_over:
4511       return "DW_OP_over";
4512     case DW_OP_pick:
4513       return "DW_OP_pick";
4514     case DW_OP_swap:
4515       return "DW_OP_swap";
4516     case DW_OP_rot:
4517       return "DW_OP_rot";
4518     case DW_OP_xderef:
4519       return "DW_OP_xderef";
4520     case DW_OP_abs:
4521       return "DW_OP_abs";
4522     case DW_OP_and:
4523       return "DW_OP_and";
4524     case DW_OP_div:
4525       return "DW_OP_div";
4526     case DW_OP_minus:
4527       return "DW_OP_minus";
4528     case DW_OP_mod:
4529       return "DW_OP_mod";
4530     case DW_OP_mul:
4531       return "DW_OP_mul";
4532     case DW_OP_neg:
4533       return "DW_OP_neg";
4534     case DW_OP_not:
4535       return "DW_OP_not";
4536     case DW_OP_or:
4537       return "DW_OP_or";
4538     case DW_OP_plus:
4539       return "DW_OP_plus";
4540     case DW_OP_plus_uconst:
4541       return "DW_OP_plus_uconst";
4542     case DW_OP_shl:
4543       return "DW_OP_shl";
4544     case DW_OP_shr:
4545       return "DW_OP_shr";
4546     case DW_OP_shra:
4547       return "DW_OP_shra";
4548     case DW_OP_xor:
4549       return "DW_OP_xor";
4550     case DW_OP_bra:
4551       return "DW_OP_bra";
4552     case DW_OP_eq:
4553       return "DW_OP_eq";
4554     case DW_OP_ge:
4555       return "DW_OP_ge";
4556     case DW_OP_gt:
4557       return "DW_OP_gt";
4558     case DW_OP_le:
4559       return "DW_OP_le";
4560     case DW_OP_lt:
4561       return "DW_OP_lt";
4562     case DW_OP_ne:
4563       return "DW_OP_ne";
4564     case DW_OP_skip:
4565       return "DW_OP_skip";
4566     case DW_OP_lit0:
4567       return "DW_OP_lit0";
4568     case DW_OP_lit1:
4569       return "DW_OP_lit1";
4570     case DW_OP_lit2:
4571       return "DW_OP_lit2";
4572     case DW_OP_lit3:
4573       return "DW_OP_lit3";
4574     case DW_OP_lit4:
4575       return "DW_OP_lit4";
4576     case DW_OP_lit5:
4577       return "DW_OP_lit5";
4578     case DW_OP_lit6:
4579       return "DW_OP_lit6";
4580     case DW_OP_lit7:
4581       return "DW_OP_lit7";
4582     case DW_OP_lit8:
4583       return "DW_OP_lit8";
4584     case DW_OP_lit9:
4585       return "DW_OP_lit9";
4586     case DW_OP_lit10:
4587       return "DW_OP_lit10";
4588     case DW_OP_lit11:
4589       return "DW_OP_lit11";
4590     case DW_OP_lit12:
4591       return "DW_OP_lit12";
4592     case DW_OP_lit13:
4593       return "DW_OP_lit13";
4594     case DW_OP_lit14:
4595       return "DW_OP_lit14";
4596     case DW_OP_lit15:
4597       return "DW_OP_lit15";
4598     case DW_OP_lit16:
4599       return "DW_OP_lit16";
4600     case DW_OP_lit17:
4601       return "DW_OP_lit17";
4602     case DW_OP_lit18:
4603       return "DW_OP_lit18";
4604     case DW_OP_lit19:
4605       return "DW_OP_lit19";
4606     case DW_OP_lit20:
4607       return "DW_OP_lit20";
4608     case DW_OP_lit21:
4609       return "DW_OP_lit21";
4610     case DW_OP_lit22:
4611       return "DW_OP_lit22";
4612     case DW_OP_lit23:
4613       return "DW_OP_lit23";
4614     case DW_OP_lit24:
4615       return "DW_OP_lit24";
4616     case DW_OP_lit25:
4617       return "DW_OP_lit25";
4618     case DW_OP_lit26:
4619       return "DW_OP_lit26";
4620     case DW_OP_lit27:
4621       return "DW_OP_lit27";
4622     case DW_OP_lit28:
4623       return "DW_OP_lit28";
4624     case DW_OP_lit29:
4625       return "DW_OP_lit29";
4626     case DW_OP_lit30:
4627       return "DW_OP_lit30";
4628     case DW_OP_lit31:
4629       return "DW_OP_lit31";
4630     case DW_OP_reg0:
4631       return "DW_OP_reg0";
4632     case DW_OP_reg1:
4633       return "DW_OP_reg1";
4634     case DW_OP_reg2:
4635       return "DW_OP_reg2";
4636     case DW_OP_reg3:
4637       return "DW_OP_reg3";
4638     case DW_OP_reg4:
4639       return "DW_OP_reg4";
4640     case DW_OP_reg5:
4641       return "DW_OP_reg5";
4642     case DW_OP_reg6:
4643       return "DW_OP_reg6";
4644     case DW_OP_reg7:
4645       return "DW_OP_reg7";
4646     case DW_OP_reg8:
4647       return "DW_OP_reg8";
4648     case DW_OP_reg9:
4649       return "DW_OP_reg9";
4650     case DW_OP_reg10:
4651       return "DW_OP_reg10";
4652     case DW_OP_reg11:
4653       return "DW_OP_reg11";
4654     case DW_OP_reg12:
4655       return "DW_OP_reg12";
4656     case DW_OP_reg13:
4657       return "DW_OP_reg13";
4658     case DW_OP_reg14:
4659       return "DW_OP_reg14";
4660     case DW_OP_reg15:
4661       return "DW_OP_reg15";
4662     case DW_OP_reg16:
4663       return "DW_OP_reg16";
4664     case DW_OP_reg17:
4665       return "DW_OP_reg17";
4666     case DW_OP_reg18:
4667       return "DW_OP_reg18";
4668     case DW_OP_reg19:
4669       return "DW_OP_reg19";
4670     case DW_OP_reg20:
4671       return "DW_OP_reg20";
4672     case DW_OP_reg21:
4673       return "DW_OP_reg21";
4674     case DW_OP_reg22:
4675       return "DW_OP_reg22";
4676     case DW_OP_reg23:
4677       return "DW_OP_reg23";
4678     case DW_OP_reg24:
4679       return "DW_OP_reg24";
4680     case DW_OP_reg25:
4681       return "DW_OP_reg25";
4682     case DW_OP_reg26:
4683       return "DW_OP_reg26";
4684     case DW_OP_reg27:
4685       return "DW_OP_reg27";
4686     case DW_OP_reg28:
4687       return "DW_OP_reg28";
4688     case DW_OP_reg29:
4689       return "DW_OP_reg29";
4690     case DW_OP_reg30:
4691       return "DW_OP_reg30";
4692     case DW_OP_reg31:
4693       return "DW_OP_reg31";
4694     case DW_OP_breg0:
4695       return "DW_OP_breg0";
4696     case DW_OP_breg1:
4697       return "DW_OP_breg1";
4698     case DW_OP_breg2:
4699       return "DW_OP_breg2";
4700     case DW_OP_breg3:
4701       return "DW_OP_breg3";
4702     case DW_OP_breg4:
4703       return "DW_OP_breg4";
4704     case DW_OP_breg5:
4705       return "DW_OP_breg5";
4706     case DW_OP_breg6:
4707       return "DW_OP_breg6";
4708     case DW_OP_breg7:
4709       return "DW_OP_breg7";
4710     case DW_OP_breg8:
4711       return "DW_OP_breg8";
4712     case DW_OP_breg9:
4713       return "DW_OP_breg9";
4714     case DW_OP_breg10:
4715       return "DW_OP_breg10";
4716     case DW_OP_breg11:
4717       return "DW_OP_breg11";
4718     case DW_OP_breg12:
4719       return "DW_OP_breg12";
4720     case DW_OP_breg13:
4721       return "DW_OP_breg13";
4722     case DW_OP_breg14:
4723       return "DW_OP_breg14";
4724     case DW_OP_breg15:
4725       return "DW_OP_breg15";
4726     case DW_OP_breg16:
4727       return "DW_OP_breg16";
4728     case DW_OP_breg17:
4729       return "DW_OP_breg17";
4730     case DW_OP_breg18:
4731       return "DW_OP_breg18";
4732     case DW_OP_breg19:
4733       return "DW_OP_breg19";
4734     case DW_OP_breg20:
4735       return "DW_OP_breg20";
4736     case DW_OP_breg21:
4737       return "DW_OP_breg21";
4738     case DW_OP_breg22:
4739       return "DW_OP_breg22";
4740     case DW_OP_breg23:
4741       return "DW_OP_breg23";
4742     case DW_OP_breg24:
4743       return "DW_OP_breg24";
4744     case DW_OP_breg25:
4745       return "DW_OP_breg25";
4746     case DW_OP_breg26:
4747       return "DW_OP_breg26";
4748     case DW_OP_breg27:
4749       return "DW_OP_breg27";
4750     case DW_OP_breg28:
4751       return "DW_OP_breg28";
4752     case DW_OP_breg29:
4753       return "DW_OP_breg29";
4754     case DW_OP_breg30:
4755       return "DW_OP_breg30";
4756     case DW_OP_breg31:
4757       return "DW_OP_breg31";
4758     case DW_OP_regx:
4759       return "DW_OP_regx";
4760     case DW_OP_fbreg:
4761       return "DW_OP_fbreg";
4762     case DW_OP_bregx:
4763       return "DW_OP_bregx";
4764     case DW_OP_piece:
4765       return "DW_OP_piece";
4766     case DW_OP_deref_size:
4767       return "DW_OP_deref_size";
4768     case DW_OP_xderef_size:
4769       return "DW_OP_xderef_size";
4770     case DW_OP_nop:
4771       return "DW_OP_nop";
4772
4773     case DW_OP_push_object_address:
4774       return "DW_OP_push_object_address";
4775     case DW_OP_call2:
4776       return "DW_OP_call2";
4777     case DW_OP_call4:
4778       return "DW_OP_call4";
4779     case DW_OP_call_ref:
4780       return "DW_OP_call_ref";
4781     case DW_OP_implicit_value:
4782       return "DW_OP_implicit_value";
4783     case DW_OP_stack_value:
4784       return "DW_OP_stack_value";
4785     case DW_OP_form_tls_address:
4786       return "DW_OP_form_tls_address";
4787     case DW_OP_call_frame_cfa:
4788       return "DW_OP_call_frame_cfa";
4789     case DW_OP_bit_piece:
4790       return "DW_OP_bit_piece";
4791
4792     case DW_OP_GNU_push_tls_address:
4793       return "DW_OP_GNU_push_tls_address";
4794     case DW_OP_GNU_uninit:
4795       return "DW_OP_GNU_uninit";
4796     case DW_OP_GNU_encoded_addr:
4797       return "DW_OP_GNU_encoded_addr";
4798     case DW_OP_GNU_implicit_pointer:
4799       return "DW_OP_GNU_implicit_pointer";
4800     case DW_OP_GNU_entry_value:
4801       return "DW_OP_GNU_entry_value";
4802
4803     default:
4804       return "OP_<unknown>";
4805     }
4806 }
4807
4808 /* Return a pointer to a newly allocated location description.  Location
4809    descriptions are simple expression terms that can be strung
4810    together to form more complicated location (address) descriptions.  */
4811
4812 static inline dw_loc_descr_ref
4813 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4814                unsigned HOST_WIDE_INT oprnd2)
4815 {
4816   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4817
4818   descr->dw_loc_opc = op;
4819   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4820   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4821   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4822   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4823
4824   return descr;
4825 }
4826
4827 /* Return a pointer to a newly allocated location description for
4828    REG and OFFSET.  */
4829
4830 static inline dw_loc_descr_ref
4831 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4832 {
4833   if (reg <= 31)
4834     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4835                           offset, 0);
4836   else
4837     return new_loc_descr (DW_OP_bregx, reg, offset);
4838 }
4839
4840 /* Add a location description term to a location description expression.  */
4841
4842 static inline void
4843 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4844 {
4845   dw_loc_descr_ref *d;
4846
4847   /* Find the end of the chain.  */
4848   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4849     ;
4850
4851   *d = descr;
4852 }
4853
4854 /* Add a constant OFFSET to a location expression.  */
4855
4856 static void
4857 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4858 {
4859   dw_loc_descr_ref loc;
4860   HOST_WIDE_INT *p;
4861
4862   gcc_assert (*list_head != NULL);
4863
4864   if (!offset)
4865     return;
4866
4867   /* Find the end of the chain.  */
4868   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4869     ;
4870
4871   p = NULL;
4872   if (loc->dw_loc_opc == DW_OP_fbreg
4873       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4874     p = &loc->dw_loc_oprnd1.v.val_int;
4875   else if (loc->dw_loc_opc == DW_OP_bregx)
4876     p = &loc->dw_loc_oprnd2.v.val_int;
4877
4878   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4879      offset.  Don't optimize if an signed integer overflow would happen.  */
4880   if (p != NULL
4881       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4882           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4883     *p += offset;
4884
4885   else if (offset > 0)
4886     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4887
4888   else
4889     {
4890       loc->dw_loc_next = int_loc_descriptor (-offset);
4891       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4892     }
4893 }
4894
4895 /* Add a constant OFFSET to a location list.  */
4896
4897 static void
4898 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4899 {
4900   dw_loc_list_ref d;
4901   for (d = list_head; d != NULL; d = d->dw_loc_next)
4902     loc_descr_plus_const (&d->expr, offset);
4903 }
4904
4905 #define DWARF_REF_SIZE  \
4906   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4907
4908 static unsigned long size_of_locs (dw_loc_descr_ref);
4909
4910 /* Return the size of a location descriptor.  */
4911
4912 static unsigned long
4913 size_of_loc_descr (dw_loc_descr_ref loc)
4914 {
4915   unsigned long size = 1;
4916
4917   switch (loc->dw_loc_opc)
4918     {
4919     case DW_OP_addr:
4920       size += DWARF2_ADDR_SIZE;
4921       break;
4922     case DW_OP_const1u:
4923     case DW_OP_const1s:
4924       size += 1;
4925       break;
4926     case DW_OP_const2u:
4927     case DW_OP_const2s:
4928       size += 2;
4929       break;
4930     case DW_OP_const4u:
4931     case DW_OP_const4s:
4932       size += 4;
4933       break;
4934     case DW_OP_const8u:
4935     case DW_OP_const8s:
4936       size += 8;
4937       break;
4938     case DW_OP_constu:
4939       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4940       break;
4941     case DW_OP_consts:
4942       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4943       break;
4944     case DW_OP_pick:
4945       size += 1;
4946       break;
4947     case DW_OP_plus_uconst:
4948       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4949       break;
4950     case DW_OP_skip:
4951     case DW_OP_bra:
4952       size += 2;
4953       break;
4954     case DW_OP_breg0:
4955     case DW_OP_breg1:
4956     case DW_OP_breg2:
4957     case DW_OP_breg3:
4958     case DW_OP_breg4:
4959     case DW_OP_breg5:
4960     case DW_OP_breg6:
4961     case DW_OP_breg7:
4962     case DW_OP_breg8:
4963     case DW_OP_breg9:
4964     case DW_OP_breg10:
4965     case DW_OP_breg11:
4966     case DW_OP_breg12:
4967     case DW_OP_breg13:
4968     case DW_OP_breg14:
4969     case DW_OP_breg15:
4970     case DW_OP_breg16:
4971     case DW_OP_breg17:
4972     case DW_OP_breg18:
4973     case DW_OP_breg19:
4974     case DW_OP_breg20:
4975     case DW_OP_breg21:
4976     case DW_OP_breg22:
4977     case DW_OP_breg23:
4978     case DW_OP_breg24:
4979     case DW_OP_breg25:
4980     case DW_OP_breg26:
4981     case DW_OP_breg27:
4982     case DW_OP_breg28:
4983     case DW_OP_breg29:
4984     case DW_OP_breg30:
4985     case DW_OP_breg31:
4986       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4987       break;
4988     case DW_OP_regx:
4989       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4990       break;
4991     case DW_OP_fbreg:
4992       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4993       break;
4994     case DW_OP_bregx:
4995       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4997       break;
4998     case DW_OP_piece:
4999       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5000       break;
5001     case DW_OP_bit_piece:
5002       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5003       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5004       break;
5005     case DW_OP_deref_size:
5006     case DW_OP_xderef_size:
5007       size += 1;
5008       break;
5009     case DW_OP_call2:
5010       size += 2;
5011       break;
5012     case DW_OP_call4:
5013       size += 4;
5014       break;
5015     case DW_OP_call_ref:
5016       size += DWARF_REF_SIZE;
5017       break;
5018     case DW_OP_implicit_value:
5019       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5020               + loc->dw_loc_oprnd1.v.val_unsigned;
5021       break;
5022     case DW_OP_GNU_implicit_pointer:
5023       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5024       break;
5025     case DW_OP_GNU_entry_value:
5026       {
5027         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5028         size += size_of_uleb128 (op_size) + op_size;
5029         break;
5030       }
5031     default:
5032       break;
5033     }
5034
5035   return size;
5036 }
5037
5038 /* Return the size of a series of location descriptors.  */
5039
5040 static unsigned long
5041 size_of_locs (dw_loc_descr_ref loc)
5042 {
5043   dw_loc_descr_ref l;
5044   unsigned long size;
5045
5046   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5047      field, to avoid writing to a PCH file.  */
5048   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5049     {
5050       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5051         break;
5052       size += size_of_loc_descr (l);
5053     }
5054   if (! l)
5055     return size;
5056
5057   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5058     {
5059       l->dw_loc_addr = size;
5060       size += size_of_loc_descr (l);
5061     }
5062
5063   return size;
5064 }
5065
5066 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5067 static void get_ref_die_offset_label (char *, dw_die_ref);
5068 static void output_loc_sequence (dw_loc_descr_ref, int);
5069
5070 /* Output location description stack opcode's operands (if any).
5071    The for_eh_or_skip parameter controls whether register numbers are
5072    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5073    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5074    info).  This should be suppressed for the cases that have not been converted
5075    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5076
5077 static void
5078 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5079 {
5080   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5081   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5082
5083   switch (loc->dw_loc_opc)
5084     {
5085 #ifdef DWARF2_DEBUGGING_INFO
5086     case DW_OP_const2u:
5087     case DW_OP_const2s:
5088       dw2_asm_output_data (2, val1->v.val_int, NULL);
5089       break;
5090     case DW_OP_const4u:
5091       if (loc->dtprel)
5092         {
5093           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5094           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5095                                                val1->v.val_addr);
5096           fputc ('\n', asm_out_file);
5097           break;
5098         }
5099       /* FALLTHRU */
5100     case DW_OP_const4s:
5101       dw2_asm_output_data (4, val1->v.val_int, NULL);
5102       break;
5103     case DW_OP_const8u:
5104       if (loc->dtprel)
5105         {
5106           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5107           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5108                                                val1->v.val_addr);
5109           fputc ('\n', asm_out_file);
5110           break;
5111         }
5112       /* FALLTHRU */
5113     case DW_OP_const8s:
5114       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5115       dw2_asm_output_data (8, val1->v.val_int, NULL);
5116       break;
5117     case DW_OP_skip:
5118     case DW_OP_bra:
5119       {
5120         int offset;
5121
5122         gcc_assert (val1->val_class == dw_val_class_loc);
5123         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5124
5125         dw2_asm_output_data (2, offset, NULL);
5126       }
5127       break;
5128     case DW_OP_implicit_value:
5129       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5130       switch (val2->val_class)
5131         {
5132         case dw_val_class_const:
5133           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5134           break;
5135         case dw_val_class_vec:
5136           {
5137             unsigned int elt_size = val2->v.val_vec.elt_size;
5138             unsigned int len = val2->v.val_vec.length;
5139             unsigned int i;
5140             unsigned char *p;
5141
5142             if (elt_size > sizeof (HOST_WIDE_INT))
5143               {
5144                 elt_size /= 2;
5145                 len *= 2;
5146               }
5147             for (i = 0, p = val2->v.val_vec.array;
5148                  i < len;
5149                  i++, p += elt_size)
5150               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5151                                    "fp or vector constant word %u", i);
5152           }
5153           break;
5154         case dw_val_class_const_double:
5155           {
5156             unsigned HOST_WIDE_INT first, second;
5157
5158             if (WORDS_BIG_ENDIAN)
5159               {
5160                 first = val2->v.val_double.high;
5161                 second = val2->v.val_double.low;
5162               }
5163             else
5164               {
5165                 first = val2->v.val_double.low;
5166                 second = val2->v.val_double.high;
5167               }
5168             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5169                                  first, NULL);
5170             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5171                                  second, NULL);
5172           }
5173           break;
5174         case dw_val_class_addr:
5175           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5176           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5177           break;
5178         default:
5179           gcc_unreachable ();
5180         }
5181       break;
5182 #else
5183     case DW_OP_const2u:
5184     case DW_OP_const2s:
5185     case DW_OP_const4u:
5186     case DW_OP_const4s:
5187     case DW_OP_const8u:
5188     case DW_OP_const8s:
5189     case DW_OP_skip:
5190     case DW_OP_bra:
5191     case DW_OP_implicit_value:
5192       /* We currently don't make any attempt to make sure these are
5193          aligned properly like we do for the main unwind info, so
5194          don't support emitting things larger than a byte if we're
5195          only doing unwinding.  */
5196       gcc_unreachable ();
5197 #endif
5198     case DW_OP_const1u:
5199     case DW_OP_const1s:
5200       dw2_asm_output_data (1, val1->v.val_int, NULL);
5201       break;
5202     case DW_OP_constu:
5203       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5204       break;
5205     case DW_OP_consts:
5206       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5207       break;
5208     case DW_OP_pick:
5209       dw2_asm_output_data (1, val1->v.val_int, NULL);
5210       break;
5211     case DW_OP_plus_uconst:
5212       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5213       break;
5214     case DW_OP_breg0:
5215     case DW_OP_breg1:
5216     case DW_OP_breg2:
5217     case DW_OP_breg3:
5218     case DW_OP_breg4:
5219     case DW_OP_breg5:
5220     case DW_OP_breg6:
5221     case DW_OP_breg7:
5222     case DW_OP_breg8:
5223     case DW_OP_breg9:
5224     case DW_OP_breg10:
5225     case DW_OP_breg11:
5226     case DW_OP_breg12:
5227     case DW_OP_breg13:
5228     case DW_OP_breg14:
5229     case DW_OP_breg15:
5230     case DW_OP_breg16:
5231     case DW_OP_breg17:
5232     case DW_OP_breg18:
5233     case DW_OP_breg19:
5234     case DW_OP_breg20:
5235     case DW_OP_breg21:
5236     case DW_OP_breg22:
5237     case DW_OP_breg23:
5238     case DW_OP_breg24:
5239     case DW_OP_breg25:
5240     case DW_OP_breg26:
5241     case DW_OP_breg27:
5242     case DW_OP_breg28:
5243     case DW_OP_breg29:
5244     case DW_OP_breg30:
5245     case DW_OP_breg31:
5246       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5247       break;
5248     case DW_OP_regx:
5249       {
5250         unsigned r = val1->v.val_unsigned;
5251         if (for_eh_or_skip >= 0)
5252           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5253         gcc_assert (size_of_uleb128 (r) 
5254                     == size_of_uleb128 (val1->v.val_unsigned));
5255         dw2_asm_output_data_uleb128 (r, NULL);  
5256       }
5257       break;
5258     case DW_OP_fbreg:
5259       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5260       break;
5261     case DW_OP_bregx:
5262       {
5263         unsigned r = val1->v.val_unsigned;
5264         if (for_eh_or_skip >= 0)
5265           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5266         gcc_assert (size_of_uleb128 (r) 
5267                     == size_of_uleb128 (val1->v.val_unsigned));
5268         dw2_asm_output_data_uleb128 (r, NULL);  
5269         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5270       }
5271       break;
5272     case DW_OP_piece:
5273       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5274       break;
5275     case DW_OP_bit_piece:
5276       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5277       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5278       break;
5279     case DW_OP_deref_size:
5280     case DW_OP_xderef_size:
5281       dw2_asm_output_data (1, val1->v.val_int, NULL);
5282       break;
5283
5284     case DW_OP_addr:
5285       if (loc->dtprel)
5286         {
5287           if (targetm.asm_out.output_dwarf_dtprel)
5288             {
5289               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5290                                                    DWARF2_ADDR_SIZE,
5291                                                    val1->v.val_addr);
5292               fputc ('\n', asm_out_file);
5293             }
5294           else
5295             gcc_unreachable ();
5296         }
5297       else
5298         {
5299 #ifdef DWARF2_DEBUGGING_INFO
5300           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5301 #else
5302           gcc_unreachable ();
5303 #endif
5304         }
5305       break;
5306
5307     case DW_OP_GNU_implicit_pointer:
5308       {
5309         char label[MAX_ARTIFICIAL_LABEL_BYTES
5310                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5311         gcc_assert (val1->val_class == dw_val_class_die_ref);
5312         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5313         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5314         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5315       }
5316       break;
5317
5318     case DW_OP_GNU_entry_value:
5319       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5320       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5321       break;
5322
5323     default:
5324       /* Other codes have no operands.  */
5325       break;
5326     }
5327 }
5328
5329 /* Output a sequence of location operations.  
5330    The for_eh_or_skip parameter controls whether register numbers are
5331    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5332    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5333    info).  This should be suppressed for the cases that have not been converted
5334    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5335
5336 static void
5337 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5338 {
5339   for (; loc != NULL; loc = loc->dw_loc_next)
5340     {
5341       enum dwarf_location_atom opc = loc->dw_loc_opc;
5342       /* Output the opcode.  */
5343       if (for_eh_or_skip >= 0 
5344           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5345         {
5346           unsigned r = (opc - DW_OP_breg0);
5347           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5348           gcc_assert (r <= 31);
5349           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5350         }
5351       else if (for_eh_or_skip >= 0 
5352                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5353         {
5354           unsigned r = (opc - DW_OP_reg0);
5355           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5356           gcc_assert (r <= 31);
5357           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5358         }
5359
5360       dw2_asm_output_data (1, opc,
5361                              "%s", dwarf_stack_op_name (opc));
5362
5363       /* Output the operand(s) (if any).  */
5364       output_loc_operands (loc, for_eh_or_skip);
5365     }
5366 }
5367
5368 /* Output location description stack opcode's operands (if any).
5369    The output is single bytes on a line, suitable for .cfi_escape.  */
5370
5371 static void
5372 output_loc_operands_raw (dw_loc_descr_ref loc)
5373 {
5374   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5375   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5376
5377   switch (loc->dw_loc_opc)
5378     {
5379     case DW_OP_addr:
5380     case DW_OP_implicit_value:
5381       /* We cannot output addresses in .cfi_escape, only bytes.  */
5382       gcc_unreachable ();
5383
5384     case DW_OP_const1u:
5385     case DW_OP_const1s:
5386     case DW_OP_pick:
5387     case DW_OP_deref_size:
5388     case DW_OP_xderef_size:
5389       fputc (',', asm_out_file);
5390       dw2_asm_output_data_raw (1, val1->v.val_int);
5391       break;
5392
5393     case DW_OP_const2u:
5394     case DW_OP_const2s:
5395       fputc (',', asm_out_file);
5396       dw2_asm_output_data_raw (2, val1->v.val_int);
5397       break;
5398
5399     case DW_OP_const4u:
5400     case DW_OP_const4s:
5401       fputc (',', asm_out_file);
5402       dw2_asm_output_data_raw (4, val1->v.val_int);
5403       break;
5404
5405     case DW_OP_const8u:
5406     case DW_OP_const8s:
5407       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5408       fputc (',', asm_out_file);
5409       dw2_asm_output_data_raw (8, val1->v.val_int);
5410       break;
5411
5412     case DW_OP_skip:
5413     case DW_OP_bra:
5414       {
5415         int offset;
5416
5417         gcc_assert (val1->val_class == dw_val_class_loc);
5418         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5419
5420         fputc (',', asm_out_file);
5421         dw2_asm_output_data_raw (2, offset);
5422       }
5423       break;
5424
5425     case DW_OP_regx:
5426       {
5427         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5428         gcc_assert (size_of_uleb128 (r) 
5429                     == size_of_uleb128 (val1->v.val_unsigned));
5430         fputc (',', asm_out_file);
5431         dw2_asm_output_data_uleb128_raw (r);
5432       }
5433       break;
5434       
5435     case DW_OP_constu:
5436     case DW_OP_plus_uconst:
5437     case DW_OP_piece:
5438       fputc (',', asm_out_file);
5439       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5440       break;
5441
5442     case DW_OP_bit_piece:
5443       fputc (',', asm_out_file);
5444       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5445       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5446       break;
5447
5448     case DW_OP_consts:
5449     case DW_OP_breg0:
5450     case DW_OP_breg1:
5451     case DW_OP_breg2:
5452     case DW_OP_breg3:
5453     case DW_OP_breg4:
5454     case DW_OP_breg5:
5455     case DW_OP_breg6:
5456     case DW_OP_breg7:
5457     case DW_OP_breg8:
5458     case DW_OP_breg9:
5459     case DW_OP_breg10:
5460     case DW_OP_breg11:
5461     case DW_OP_breg12:
5462     case DW_OP_breg13:
5463     case DW_OP_breg14:
5464     case DW_OP_breg15:
5465     case DW_OP_breg16:
5466     case DW_OP_breg17:
5467     case DW_OP_breg18:
5468     case DW_OP_breg19:
5469     case DW_OP_breg20:
5470     case DW_OP_breg21:
5471     case DW_OP_breg22:
5472     case DW_OP_breg23:
5473     case DW_OP_breg24:
5474     case DW_OP_breg25:
5475     case DW_OP_breg26:
5476     case DW_OP_breg27:
5477     case DW_OP_breg28:
5478     case DW_OP_breg29:
5479     case DW_OP_breg30:
5480     case DW_OP_breg31:
5481     case DW_OP_fbreg:
5482       fputc (',', asm_out_file);
5483       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5484       break;
5485
5486     case DW_OP_bregx:
5487       {
5488         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5489         gcc_assert (size_of_uleb128 (r) 
5490                     == size_of_uleb128 (val1->v.val_unsigned));
5491         fputc (',', asm_out_file);
5492         dw2_asm_output_data_uleb128_raw (r);
5493         fputc (',', asm_out_file);
5494         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5495       }
5496       break;
5497
5498     case DW_OP_GNU_implicit_pointer:
5499     case DW_OP_GNU_entry_value:
5500       gcc_unreachable ();
5501       break;
5502
5503     default:
5504       /* Other codes have no operands.  */
5505       break;
5506     }
5507 }
5508
5509 static void
5510 output_loc_sequence_raw (dw_loc_descr_ref loc)
5511 {
5512   while (1)
5513     {
5514       enum dwarf_location_atom opc = loc->dw_loc_opc;
5515       /* Output the opcode.  */
5516       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5517         {
5518           unsigned r = (opc - DW_OP_breg0);
5519           r = DWARF2_FRAME_REG_OUT (r, 1);
5520           gcc_assert (r <= 31);
5521           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5522         }
5523       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5524         {
5525           unsigned r = (opc - DW_OP_reg0);
5526           r = DWARF2_FRAME_REG_OUT (r, 1);
5527           gcc_assert (r <= 31);
5528           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5529         }
5530       /* Output the opcode.  */
5531       fprintf (asm_out_file, "%#x", opc);
5532       output_loc_operands_raw (loc);
5533
5534       if (!loc->dw_loc_next)
5535         break;
5536       loc = loc->dw_loc_next;
5537
5538       fputc (',', asm_out_file);
5539     }
5540 }
5541
5542 /* This routine will generate the correct assembly data for a location
5543    description based on a cfi entry with a complex address.  */
5544
5545 static void
5546 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5547 {
5548   dw_loc_descr_ref loc;
5549   unsigned long size;
5550
5551   if (cfi->dw_cfi_opc == DW_CFA_expression)
5552     {
5553       unsigned r = 
5554         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5555       dw2_asm_output_data (1, r, NULL);
5556       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5557     }
5558   else
5559     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5560
5561   /* Output the size of the block.  */
5562   size = size_of_locs (loc);
5563   dw2_asm_output_data_uleb128 (size, NULL);
5564
5565   /* Now output the operations themselves.  */
5566   output_loc_sequence (loc, for_eh);
5567 }
5568
5569 /* Similar, but used for .cfi_escape.  */
5570
5571 static void
5572 output_cfa_loc_raw (dw_cfi_ref cfi)
5573 {
5574   dw_loc_descr_ref loc;
5575   unsigned long size;
5576
5577   if (cfi->dw_cfi_opc == DW_CFA_expression)
5578     {
5579       unsigned r = 
5580         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5581       fprintf (asm_out_file, "%#x,", r);
5582       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5583     }
5584   else
5585     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5586
5587   /* Output the size of the block.  */
5588   size = size_of_locs (loc);
5589   dw2_asm_output_data_uleb128_raw (size);
5590   fputc (',', asm_out_file);
5591
5592   /* Now output the operations themselves.  */
5593   output_loc_sequence_raw (loc);
5594 }
5595
5596 /* This function builds a dwarf location descriptor sequence from a
5597    dw_cfa_location, adding the given OFFSET to the result of the
5598    expression.  */
5599
5600 static struct dw_loc_descr_struct *
5601 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5602 {
5603   struct dw_loc_descr_struct *head, *tmp;
5604
5605   offset += cfa->offset;
5606
5607   if (cfa->indirect)
5608     {
5609       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5610       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5611       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5612       add_loc_descr (&head, tmp);
5613       if (offset != 0)
5614         {
5615           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5616           add_loc_descr (&head, tmp);
5617         }
5618     }
5619   else
5620     head = new_reg_loc_descr (cfa->reg, offset);
5621
5622   return head;
5623 }
5624
5625 /* This function builds a dwarf location descriptor sequence for
5626    the address at OFFSET from the CFA when stack is aligned to
5627    ALIGNMENT byte.  */
5628
5629 static struct dw_loc_descr_struct *
5630 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5631 {
5632   struct dw_loc_descr_struct *head;
5633   unsigned int dwarf_fp
5634     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5635
5636  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5637   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5638     {
5639       head = new_reg_loc_descr (dwarf_fp, 0);
5640       add_loc_descr (&head, int_loc_descriptor (alignment));
5641       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5642       loc_descr_plus_const (&head, offset);
5643     }
5644   else
5645     head = new_reg_loc_descr (dwarf_fp, offset);
5646   return head;
5647 }
5648
5649 /* This function fills in aa dw_cfa_location structure from a dwarf location
5650    descriptor sequence.  */
5651
5652 static void
5653 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5654 {
5655   struct dw_loc_descr_struct *ptr;
5656   cfa->offset = 0;
5657   cfa->base_offset = 0;
5658   cfa->indirect = 0;
5659   cfa->reg = -1;
5660
5661   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5662     {
5663       enum dwarf_location_atom op = ptr->dw_loc_opc;
5664
5665       switch (op)
5666         {
5667         case DW_OP_reg0:
5668         case DW_OP_reg1:
5669         case DW_OP_reg2:
5670         case DW_OP_reg3:
5671         case DW_OP_reg4:
5672         case DW_OP_reg5:
5673         case DW_OP_reg6:
5674         case DW_OP_reg7:
5675         case DW_OP_reg8:
5676         case DW_OP_reg9:
5677         case DW_OP_reg10:
5678         case DW_OP_reg11:
5679         case DW_OP_reg12:
5680         case DW_OP_reg13:
5681         case DW_OP_reg14:
5682         case DW_OP_reg15:
5683         case DW_OP_reg16:
5684         case DW_OP_reg17:
5685         case DW_OP_reg18:
5686         case DW_OP_reg19:
5687         case DW_OP_reg20:
5688         case DW_OP_reg21:
5689         case DW_OP_reg22:
5690         case DW_OP_reg23:
5691         case DW_OP_reg24:
5692         case DW_OP_reg25:
5693         case DW_OP_reg26:
5694         case DW_OP_reg27:
5695         case DW_OP_reg28:
5696         case DW_OP_reg29:
5697         case DW_OP_reg30:
5698         case DW_OP_reg31:
5699           cfa->reg = op - DW_OP_reg0;
5700           break;
5701         case DW_OP_regx:
5702           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5703           break;
5704         case DW_OP_breg0:
5705         case DW_OP_breg1:
5706         case DW_OP_breg2:
5707         case DW_OP_breg3:
5708         case DW_OP_breg4:
5709         case DW_OP_breg5:
5710         case DW_OP_breg6:
5711         case DW_OP_breg7:
5712         case DW_OP_breg8:
5713         case DW_OP_breg9:
5714         case DW_OP_breg10:
5715         case DW_OP_breg11:
5716         case DW_OP_breg12:
5717         case DW_OP_breg13:
5718         case DW_OP_breg14:
5719         case DW_OP_breg15:
5720         case DW_OP_breg16:
5721         case DW_OP_breg17:
5722         case DW_OP_breg18:
5723         case DW_OP_breg19:
5724         case DW_OP_breg20:
5725         case DW_OP_breg21:
5726         case DW_OP_breg22:
5727         case DW_OP_breg23:
5728         case DW_OP_breg24:
5729         case DW_OP_breg25:
5730         case DW_OP_breg26:
5731         case DW_OP_breg27:
5732         case DW_OP_breg28:
5733         case DW_OP_breg29:
5734         case DW_OP_breg30:
5735         case DW_OP_breg31:
5736           cfa->reg = op - DW_OP_breg0;
5737           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5738           break;
5739         case DW_OP_bregx:
5740           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5741           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5742           break;
5743         case DW_OP_deref:
5744           cfa->indirect = 1;
5745           break;
5746         case DW_OP_plus_uconst:
5747           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5748           break;
5749         default:
5750           internal_error ("DW_LOC_OP %s not implemented",
5751                           dwarf_stack_op_name (ptr->dw_loc_opc));
5752         }
5753     }
5754 }
5755 \f
5756 /* And now, the support for symbolic debugging information.  */
5757
5758 /* .debug_str support.  */
5759 static int output_indirect_string (void **, void *);
5760
5761 static void dwarf2out_init (const char *);
5762 static void dwarf2out_finish (const char *);
5763 static void dwarf2out_assembly_start (void);
5764 static void dwarf2out_define (unsigned int, const char *);
5765 static void dwarf2out_undef (unsigned int, const char *);
5766 static void dwarf2out_start_source_file (unsigned, const char *);
5767 static void dwarf2out_end_source_file (unsigned);
5768 static void dwarf2out_function_decl (tree);
5769 static void dwarf2out_begin_block (unsigned, unsigned);
5770 static void dwarf2out_end_block (unsigned, unsigned);
5771 static bool dwarf2out_ignore_block (const_tree);
5772 static void dwarf2out_global_decl (tree);
5773 static void dwarf2out_type_decl (tree, int);
5774 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5775 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5776                                                  dw_die_ref);
5777 static void dwarf2out_abstract_function (tree);
5778 static void dwarf2out_var_location (rtx);
5779 static void dwarf2out_begin_function (tree);
5780 static void dwarf2out_set_name (tree, tree);
5781
5782 /* The debug hooks structure.  */
5783
5784 const struct gcc_debug_hooks dwarf2_debug_hooks =
5785 {
5786   dwarf2out_init,
5787   dwarf2out_finish,
5788   dwarf2out_assembly_start,
5789   dwarf2out_define,
5790   dwarf2out_undef,
5791   dwarf2out_start_source_file,
5792   dwarf2out_end_source_file,
5793   dwarf2out_begin_block,
5794   dwarf2out_end_block,
5795   dwarf2out_ignore_block,
5796   dwarf2out_source_line,
5797   dwarf2out_begin_prologue,
5798 #if VMS_DEBUGGING_INFO
5799   dwarf2out_vms_end_prologue,
5800   dwarf2out_vms_begin_epilogue,
5801 #else
5802   debug_nothing_int_charstar,
5803   debug_nothing_int_charstar,
5804 #endif
5805   dwarf2out_end_epilogue,
5806   dwarf2out_begin_function,
5807   debug_nothing_int,            /* end_function */
5808   dwarf2out_function_decl,      /* function_decl */
5809   dwarf2out_global_decl,
5810   dwarf2out_type_decl,          /* type_decl */
5811   dwarf2out_imported_module_or_decl,
5812   debug_nothing_tree,           /* deferred_inline_function */
5813   /* The DWARF 2 backend tries to reduce debugging bloat by not
5814      emitting the abstract description of inline functions until
5815      something tries to reference them.  */
5816   dwarf2out_abstract_function,  /* outlining_inline_function */
5817   debug_nothing_rtx,            /* label */
5818   debug_nothing_int,            /* handle_pch */
5819   dwarf2out_var_location,
5820   dwarf2out_switch_text_section,
5821   dwarf2out_set_name,
5822   1,                            /* start_end_main_source_file */
5823   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5824 };
5825 \f
5826 /* NOTE: In the comments in this file, many references are made to
5827    "Debugging Information Entries".  This term is abbreviated as `DIE'
5828    throughout the remainder of this file.  */
5829
5830 /* An internal representation of the DWARF output is built, and then
5831    walked to generate the DWARF debugging info.  The walk of the internal
5832    representation is done after the entire program has been compiled.
5833    The types below are used to describe the internal representation.  */
5834
5835 /* Various DIE's use offsets relative to the beginning of the
5836    .debug_info section to refer to each other.  */
5837
5838 typedef long int dw_offset;
5839
5840 /* Define typedefs here to avoid circular dependencies.  */
5841
5842 typedef struct dw_attr_struct *dw_attr_ref;
5843 typedef struct dw_line_info_struct *dw_line_info_ref;
5844 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5845 typedef struct pubname_struct *pubname_ref;
5846 typedef struct dw_ranges_struct *dw_ranges_ref;
5847 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5848 typedef struct comdat_type_struct *comdat_type_node_ref;
5849
5850 /* Each entry in the line_info_table maintains the file and
5851    line number associated with the label generated for that
5852    entry.  The label gives the PC value associated with
5853    the line number entry.  */
5854
5855 typedef struct GTY(()) dw_line_info_struct {
5856   unsigned long dw_file_num;
5857   unsigned long dw_line_num;
5858 }
5859 dw_line_info_entry;
5860
5861 /* Line information for functions in separate sections; each one gets its
5862    own sequence.  */
5863 typedef struct GTY(()) dw_separate_line_info_struct {
5864   unsigned long dw_file_num;
5865   unsigned long dw_line_num;
5866   unsigned long function;
5867 }
5868 dw_separate_line_info_entry;
5869
5870 /* Each DIE attribute has a field specifying the attribute kind,
5871    a link to the next attribute in the chain, and an attribute value.
5872    Attributes are typically linked below the DIE they modify.  */
5873
5874 typedef struct GTY(()) dw_attr_struct {
5875   enum dwarf_attribute dw_attr;
5876   dw_val_node dw_attr_val;
5877 }
5878 dw_attr_node;
5879
5880 DEF_VEC_O(dw_attr_node);
5881 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5882
5883 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5884    The children of each node form a circular list linked by
5885    die_sib.  die_child points to the node *before* the "first" child node.  */
5886
5887 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5888   union die_symbol_or_type_node
5889     {
5890       char * GTY ((tag ("0"))) die_symbol;
5891       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5892     }
5893   GTY ((desc ("dwarf_version >= 4"))) die_id;
5894   VEC(dw_attr_node,gc) * die_attr;
5895   dw_die_ref die_parent;
5896   dw_die_ref die_child;
5897   dw_die_ref die_sib;
5898   dw_die_ref die_definition; /* ref from a specification to its definition */
5899   dw_offset die_offset;
5900   unsigned long die_abbrev;
5901   int die_mark;
5902   /* Die is used and must not be pruned as unused.  */
5903   int die_perennial_p;
5904   unsigned int decl_id;
5905   enum dwarf_tag die_tag;
5906 }
5907 die_node;
5908
5909 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5910 #define FOR_EACH_CHILD(die, c, expr) do {       \
5911   c = die->die_child;                           \
5912   if (c) do {                                   \
5913     c = c->die_sib;                             \
5914     expr;                                       \
5915   } while (c != die->die_child);                \
5916 } while (0)
5917
5918 /* The pubname structure */
5919
5920 typedef struct GTY(()) pubname_struct {
5921   dw_die_ref die;
5922   const char *name;
5923 }
5924 pubname_entry;
5925
5926 DEF_VEC_O(pubname_entry);
5927 DEF_VEC_ALLOC_O(pubname_entry, gc);
5928
5929 struct GTY(()) dw_ranges_struct {
5930   /* If this is positive, it's a block number, otherwise it's a
5931      bitwise-negated index into dw_ranges_by_label.  */
5932   int num;
5933 };
5934
5935 /* A structure to hold a macinfo entry.  */
5936
5937 typedef struct GTY(()) macinfo_struct {
5938   unsigned HOST_WIDE_INT code;
5939   unsigned HOST_WIDE_INT lineno;
5940   const char *info;
5941 }
5942 macinfo_entry;
5943
5944 DEF_VEC_O(macinfo_entry);
5945 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5946
5947 struct GTY(()) dw_ranges_by_label_struct {
5948   const char *begin;
5949   const char *end;
5950 };
5951
5952 /* The comdat type node structure.  */
5953 typedef struct GTY(()) comdat_type_struct
5954 {
5955   dw_die_ref root_die;
5956   dw_die_ref type_die;
5957   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5958   struct comdat_type_struct *next;
5959 }
5960 comdat_type_node;
5961
5962 /* The limbo die list structure.  */
5963 typedef struct GTY(()) limbo_die_struct {
5964   dw_die_ref die;
5965   tree created_for;
5966   struct limbo_die_struct *next;
5967 }
5968 limbo_die_node;
5969
5970 typedef struct skeleton_chain_struct
5971 {
5972   dw_die_ref old_die;
5973   dw_die_ref new_die;
5974   struct skeleton_chain_struct *parent;
5975 }
5976 skeleton_chain_node;
5977
5978 /* How to start an assembler comment.  */
5979 #ifndef ASM_COMMENT_START
5980 #define ASM_COMMENT_START ";#"
5981 #endif
5982
5983 /* Define a macro which returns nonzero for a TYPE_DECL which was
5984    implicitly generated for a tagged type.
5985
5986    Note that unlike the gcc front end (which generates a NULL named
5987    TYPE_DECL node for each complete tagged type, each array type, and
5988    each function type node created) the g++ front end generates a
5989    _named_ TYPE_DECL node for each tagged type node created.
5990    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5991    generate a DW_TAG_typedef DIE for them.  */
5992
5993 #define TYPE_DECL_IS_STUB(decl)                         \
5994   (DECL_NAME (decl) == NULL_TREE                        \
5995    || (DECL_ARTIFICIAL (decl)                           \
5996        && is_tagged_type (TREE_TYPE (decl))             \
5997        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5998            /* This is necessary for stub decls that     \
5999               appear in nested inline functions.  */    \
6000            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6001                && (decl_ultimate_origin (decl)          \
6002                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6003
6004 /* Information concerning the compilation unit's programming
6005    language, and compiler version.  */
6006
6007 /* Fixed size portion of the DWARF compilation unit header.  */
6008 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6009   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6010
6011 /* Fixed size portion of the DWARF comdat type unit header.  */
6012 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6013   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6014    + DWARF_OFFSET_SIZE)
6015
6016 /* Fixed size portion of public names info.  */
6017 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6018
6019 /* Fixed size portion of the address range info.  */
6020 #define DWARF_ARANGES_HEADER_SIZE                                       \
6021   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6022                 DWARF2_ADDR_SIZE * 2)                                   \
6023    - DWARF_INITIAL_LENGTH_SIZE)
6024
6025 /* Size of padding portion in the address range info.  It must be
6026    aligned to twice the pointer size.  */
6027 #define DWARF_ARANGES_PAD_SIZE \
6028   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6029                 DWARF2_ADDR_SIZE * 2)                              \
6030    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6031
6032 /* Use assembler line directives if available.  */
6033 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6034 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6035 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6036 #else
6037 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6038 #endif
6039 #endif
6040
6041 /* Minimum line offset in a special line info. opcode.
6042    This value was chosen to give a reasonable range of values.  */
6043 #define DWARF_LINE_BASE  -10
6044
6045 /* First special line opcode - leave room for the standard opcodes.  */
6046 #define DWARF_LINE_OPCODE_BASE  10
6047
6048 /* Range of line offsets in a special line info. opcode.  */
6049 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6050
6051 /* Flag that indicates the initial value of the is_stmt_start flag.
6052    In the present implementation, we do not mark any lines as
6053    the beginning of a source statement, because that information
6054    is not made available by the GCC front-end.  */
6055 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6056
6057 /* Maximum number of operations per instruction bundle.  */
6058 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6059 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6060 #endif
6061
6062 /* This location is used by calc_die_sizes() to keep track
6063    the offset of each DIE within the .debug_info section.  */
6064 static unsigned long next_die_offset;
6065
6066 /* Record the root of the DIE's built for the current compilation unit.  */
6067 static GTY(()) dw_die_ref single_comp_unit_die;
6068
6069 /* A list of type DIEs that have been separated into comdat sections.  */
6070 static GTY(()) comdat_type_node *comdat_type_list;
6071
6072 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6073 static GTY(()) limbo_die_node *limbo_die_list;
6074
6075 /* A list of DIEs for which we may have to generate
6076    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6077 static GTY(()) limbo_die_node *deferred_asm_name;
6078
6079 /* Filenames referenced by this compilation unit.  */
6080 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6081
6082 /* A hash table of references to DIE's that describe declarations.
6083    The key is a DECL_UID() which is a unique number identifying each decl.  */
6084 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6085
6086 /* A hash table of references to DIE's that describe COMMON blocks.
6087    The key is DECL_UID() ^ die_parent.  */
6088 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6089
6090 typedef struct GTY(()) die_arg_entry_struct {
6091     dw_die_ref die;
6092     tree arg;
6093 } die_arg_entry;
6094
6095 DEF_VEC_O(die_arg_entry);
6096 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6097
6098 /* Node of the variable location list.  */
6099 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6100   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6101      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6102      in mode of the EXPR_LIST node and first EXPR_LIST operand
6103      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6104      location or NULL for padding.  For larger bitsizes,
6105      mode is 0 and first operand is a CONCAT with bitsize
6106      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6107      NULL as second operand.  */
6108   rtx GTY (()) loc;
6109   const char * GTY (()) label;
6110   struct var_loc_node * GTY (()) next;
6111 };
6112
6113 /* Variable location list.  */
6114 struct GTY (()) var_loc_list_def {
6115   struct var_loc_node * GTY (()) first;
6116
6117   /* Pointer to the last but one or last element of the
6118      chained list.  If the list is empty, both first and
6119      last are NULL, if the list contains just one node
6120      or the last node certainly is not redundant, it points
6121      to the last node, otherwise points to the last but one.
6122      Do not mark it for GC because it is marked through the chain.  */
6123   struct var_loc_node * GTY ((skip ("%h"))) last;
6124
6125   /* DECL_UID of the variable decl.  */
6126   unsigned int decl_id;
6127 };
6128 typedef struct var_loc_list_def var_loc_list;
6129
6130 /* Call argument location list.  */
6131 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6132   rtx GTY (()) call_arg_loc_note;
6133   const char * GTY (()) label;
6134   tree GTY (()) block;
6135   bool tail_call_p;
6136   rtx GTY (()) symbol_ref;
6137   struct call_arg_loc_node * GTY (()) next;
6138 };
6139
6140
6141 /* Table of decl location linked lists.  */
6142 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6143
6144 /* Head and tail of call_arg_loc chain.  */
6145 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6146 static struct call_arg_loc_node *call_arg_loc_last;
6147
6148 /* Number of call sites in the current function.  */
6149 static int call_site_count = -1;
6150 /* Number of tail call sites in the current function.  */
6151 static int tail_call_site_count = -1;
6152
6153 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6154    DIEs.  */
6155 static VEC (dw_die_ref, heap) *block_map;
6156
6157 /* A pointer to the base of a list of references to DIE's that
6158    are uniquely identified by their tag, presence/absence of
6159    children DIE's, and list of attribute/value pairs.  */
6160 static GTY((length ("abbrev_die_table_allocated")))
6161   dw_die_ref *abbrev_die_table;
6162
6163 /* Number of elements currently allocated for abbrev_die_table.  */
6164 static GTY(()) unsigned abbrev_die_table_allocated;
6165
6166 /* Number of elements in type_die_table currently in use.  */
6167 static GTY(()) unsigned abbrev_die_table_in_use;
6168
6169 /* Size (in elements) of increments by which we may expand the
6170    abbrev_die_table.  */
6171 #define ABBREV_DIE_TABLE_INCREMENT 256
6172
6173 /* A pointer to the base of a table that contains line information
6174    for each source code line in .text in the compilation unit.  */
6175 static GTY((length ("line_info_table_allocated")))
6176      dw_line_info_ref line_info_table;
6177
6178 /* Number of elements currently allocated for line_info_table.  */
6179 static GTY(()) unsigned line_info_table_allocated;
6180
6181 /* Number of elements in line_info_table currently in use.  */
6182 static GTY(()) unsigned line_info_table_in_use;
6183
6184 /* A pointer to the base of a table that contains line information
6185    for each source code line outside of .text in the compilation unit.  */
6186 static GTY ((length ("separate_line_info_table_allocated")))
6187      dw_separate_line_info_ref separate_line_info_table;
6188
6189 /* Number of elements currently allocated for separate_line_info_table.  */
6190 static GTY(()) unsigned separate_line_info_table_allocated;
6191
6192 /* Number of elements in separate_line_info_table currently in use.  */
6193 static GTY(()) unsigned separate_line_info_table_in_use;
6194
6195 /* Size (in elements) of increments by which we may expand the
6196    line_info_table.  */
6197 #define LINE_INFO_TABLE_INCREMENT 1024
6198
6199 /* A flag to tell pubnames/types export if there is an info section to
6200    refer to.  */
6201 static bool info_section_emitted;
6202
6203 /* A pointer to the base of a table that contains a list of publicly
6204    accessible names.  */
6205 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6206
6207 /* A pointer to the base of a table that contains a list of publicly
6208    accessible types.  */
6209 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6210
6211 /* A pointer to the base of a table that contains a list of macro
6212    defines/undefines (and file start/end markers).  */
6213 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6214
6215 /* Array of dies for which we should generate .debug_arange info.  */
6216 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6217
6218 /* Number of elements currently allocated for arange_table.  */
6219 static GTY(()) unsigned arange_table_allocated;
6220
6221 /* Number of elements in arange_table currently in use.  */
6222 static GTY(()) unsigned arange_table_in_use;
6223
6224 /* Size (in elements) of increments by which we may expand the
6225    arange_table.  */
6226 #define ARANGE_TABLE_INCREMENT 64
6227
6228 /* Array of dies for which we should generate .debug_ranges info.  */
6229 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6230
6231 /* Number of elements currently allocated for ranges_table.  */
6232 static GTY(()) unsigned ranges_table_allocated;
6233
6234 /* Number of elements in ranges_table currently in use.  */
6235 static GTY(()) unsigned ranges_table_in_use;
6236
6237 /* Array of pairs of labels referenced in ranges_table.  */
6238 static GTY ((length ("ranges_by_label_allocated")))
6239      dw_ranges_by_label_ref ranges_by_label;
6240
6241 /* Number of elements currently allocated for ranges_by_label.  */
6242 static GTY(()) unsigned ranges_by_label_allocated;
6243
6244 /* Number of elements in ranges_by_label currently in use.  */
6245 static GTY(()) unsigned ranges_by_label_in_use;
6246
6247 /* Size (in elements) of increments by which we may expand the
6248    ranges_table.  */
6249 #define RANGES_TABLE_INCREMENT 64
6250
6251 /* Whether we have location lists that need outputting */
6252 static GTY(()) bool have_location_lists;
6253
6254 /* Unique label counter.  */
6255 static GTY(()) unsigned int loclabel_num;
6256
6257 /* Unique label counter for point-of-call tables.  */
6258 static GTY(()) unsigned int poc_label_num;
6259
6260 /* Record whether the function being analyzed contains inlined functions.  */
6261 static int current_function_has_inlines;
6262
6263 /* The last file entry emitted by maybe_emit_file().  */
6264 static GTY(()) struct dwarf_file_data * last_emitted_file;
6265
6266 /* Number of internal labels generated by gen_internal_sym().  */
6267 static GTY(()) int label_num;
6268
6269 /* Cached result of previous call to lookup_filename.  */
6270 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6271
6272 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6273
6274 /* Instances of generic types for which we need to generate debug
6275    info that describe their generic parameters and arguments. That
6276    generation needs to happen once all types are properly laid out so
6277    we do it at the end of compilation.  */
6278 static GTY(()) VEC(tree,gc) *generic_type_instances;
6279
6280 /* Offset from the "steady-state frame pointer" to the frame base,
6281    within the current function.  */
6282 static HOST_WIDE_INT frame_pointer_fb_offset;
6283
6284 /* Forward declarations for functions defined in this file.  */
6285
6286 static int is_pseudo_reg (const_rtx);
6287 static tree type_main_variant (tree);
6288 static int is_tagged_type (const_tree);
6289 static const char *dwarf_tag_name (unsigned);
6290 static const char *dwarf_attr_name (unsigned);
6291 static const char *dwarf_form_name (unsigned);
6292 static tree decl_ultimate_origin (const_tree);
6293 static tree decl_class_context (tree);
6294 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6295 static inline enum dw_val_class AT_class (dw_attr_ref);
6296 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6297 static inline unsigned AT_flag (dw_attr_ref);
6298 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6299 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6300 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6301 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6302 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6303                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6304 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6305                                unsigned int, unsigned char *);
6306 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6307 static hashval_t debug_str_do_hash (const void *);
6308 static int debug_str_eq (const void *, const void *);
6309 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6310 static inline const char *AT_string (dw_attr_ref);
6311 static enum dwarf_form AT_string_form (dw_attr_ref);
6312 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6313 static void add_AT_specification (dw_die_ref, dw_die_ref);
6314 static inline dw_die_ref AT_ref (dw_attr_ref);
6315 static inline int AT_ref_external (dw_attr_ref);
6316 static inline void set_AT_ref_external (dw_attr_ref, int);
6317 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6318 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6319 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6320 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6321                              dw_loc_list_ref);
6322 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6323 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6324 static inline rtx AT_addr (dw_attr_ref);
6325 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6326 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6327 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6328 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6329                            unsigned HOST_WIDE_INT);
6330 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6331                                unsigned long);
6332 static inline const char *AT_lbl (dw_attr_ref);
6333 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6334 static const char *get_AT_low_pc (dw_die_ref);
6335 static const char *get_AT_hi_pc (dw_die_ref);
6336 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6337 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6338 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6339 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6340 static bool is_cxx (void);
6341 static bool is_fortran (void);
6342 static bool is_ada (void);
6343 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6344 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6345 static void add_child_die (dw_die_ref, dw_die_ref);
6346 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6347 static dw_die_ref lookup_type_die (tree);
6348 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6349 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6350 static void equate_type_number_to_die (tree, dw_die_ref);
6351 static hashval_t decl_die_table_hash (const void *);
6352 static int decl_die_table_eq (const void *, const void *);
6353 static dw_die_ref lookup_decl_die (tree);
6354 static hashval_t common_block_die_table_hash (const void *);
6355 static int common_block_die_table_eq (const void *, const void *);
6356 static hashval_t decl_loc_table_hash (const void *);
6357 static int decl_loc_table_eq (const void *, const void *);
6358 static var_loc_list *lookup_decl_loc (const_tree);
6359 static void equate_decl_number_to_die (tree, dw_die_ref);
6360 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6361 static void print_spaces (FILE *);
6362 static void print_die (dw_die_ref, FILE *);
6363 static void print_dwarf_line_table (FILE *);
6364 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6365 static dw_die_ref pop_compile_unit (dw_die_ref);
6366 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6367 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6368 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6369 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6370 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6371 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6372 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6373                                    struct md5_ctx *, int *);
6374 struct checksum_attributes;
6375 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6376 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6377 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6378 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6379 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6380 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6381 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6382 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6383 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6384 static void compute_section_prefix (dw_die_ref);
6385 static int is_type_die (dw_die_ref);
6386 static int is_comdat_die (dw_die_ref);
6387 static int is_symbol_die (dw_die_ref);
6388 static void assign_symbol_names (dw_die_ref);
6389 static void break_out_includes (dw_die_ref);
6390 static int is_declaration_die (dw_die_ref);
6391 static int should_move_die_to_comdat (dw_die_ref);
6392 static dw_die_ref clone_as_declaration (dw_die_ref);
6393 static dw_die_ref clone_die (dw_die_ref);
6394 static dw_die_ref clone_tree (dw_die_ref);
6395 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6396 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6397 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6398 static dw_die_ref generate_skeleton (dw_die_ref);
6399 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6400                                                          dw_die_ref);
6401 static void break_out_comdat_types (dw_die_ref);
6402 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6403 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6404 static void copy_decls_for_unworthy_types (dw_die_ref);
6405
6406 static hashval_t htab_cu_hash (const void *);
6407 static int htab_cu_eq (const void *, const void *);
6408 static void htab_cu_del (void *);
6409 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6410 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6411 static void add_sibling_attributes (dw_die_ref);
6412 static void build_abbrev_table (dw_die_ref);
6413 static void output_location_lists (dw_die_ref);
6414 static int constant_size (unsigned HOST_WIDE_INT);
6415 static unsigned long size_of_die (dw_die_ref);
6416 static void calc_die_sizes (dw_die_ref);
6417 static void mark_dies (dw_die_ref);
6418 static void unmark_dies (dw_die_ref);
6419 static void unmark_all_dies (dw_die_ref);
6420 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6421 static unsigned long size_of_aranges (void);
6422 static enum dwarf_form value_format (dw_attr_ref);
6423 static void output_value_format (dw_attr_ref);
6424 static void output_abbrev_section (void);
6425 static void output_die_symbol (dw_die_ref);
6426 static void output_die (dw_die_ref);
6427 static void output_compilation_unit_header (void);
6428 static void output_comp_unit (dw_die_ref, int);
6429 static void output_comdat_type_unit (comdat_type_node *);
6430 static const char *dwarf2_name (tree, int);
6431 static void add_pubname (tree, dw_die_ref);
6432 static void add_pubname_string (const char *, dw_die_ref);
6433 static void add_pubtype (tree, dw_die_ref);
6434 static void output_pubnames (VEC (pubname_entry,gc) *);
6435 static void add_arange (tree, dw_die_ref);
6436 static void output_aranges (void);
6437 static unsigned int add_ranges_num (int);
6438 static unsigned int add_ranges (const_tree);
6439 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6440                                   bool *);
6441 static void output_ranges (void);
6442 static void output_line_info (void);
6443 static void output_file_names (void);
6444 static dw_die_ref base_type_die (tree);
6445 static int is_base_type (tree);
6446 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6447 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6448 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6449 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6450 static int type_is_enum (const_tree);
6451 static unsigned int dbx_reg_number (const_rtx);
6452 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6453 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6454 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6455                                                 enum var_init_status);
6456 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6457                                                      enum var_init_status);
6458 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6459                                          enum var_init_status);
6460 static int is_based_loc (const_rtx);
6461 static int resolve_one_addr (rtx *, void *);
6462 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6463                                                enum var_init_status);
6464 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6465                                         enum var_init_status);
6466 static dw_loc_list_ref loc_list_from_tree (tree, int);
6467 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6468 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6469 static tree field_type (const_tree);
6470 static unsigned int simple_type_align_in_bits (const_tree);
6471 static unsigned int simple_decl_align_in_bits (const_tree);
6472 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6473 static HOST_WIDE_INT field_byte_offset (const_tree);
6474 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6475                                          dw_loc_list_ref);
6476 static void add_data_member_location_attribute (dw_die_ref, tree);
6477 static bool add_const_value_attribute (dw_die_ref, rtx);
6478 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6479 static void insert_double (double_int, unsigned char *);
6480 static void insert_float (const_rtx, unsigned char *);
6481 static rtx rtl_for_decl_location (tree);
6482 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6483                                                    enum dwarf_attribute);
6484 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6485 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6486 static void add_name_attribute (dw_die_ref, const char *);
6487 static void add_comp_dir_attribute (dw_die_ref);
6488 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6489 static void add_subscript_info (dw_die_ref, tree, bool);
6490 static void add_byte_size_attribute (dw_die_ref, tree);
6491 static void add_bit_offset_attribute (dw_die_ref, tree);
6492 static void add_bit_size_attribute (dw_die_ref, tree);
6493 static void add_prototyped_attribute (dw_die_ref, tree);
6494 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6495 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6496 static void add_src_coords_attributes (dw_die_ref, tree);
6497 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6498 static void push_decl_scope (tree);
6499 static void pop_decl_scope (void);
6500 static dw_die_ref scope_die_for (tree, dw_die_ref);
6501 static inline int local_scope_p (dw_die_ref);
6502 static inline int class_scope_p (dw_die_ref);
6503 static inline int class_or_namespace_scope_p (dw_die_ref);
6504 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6505 static void add_calling_convention_attribute (dw_die_ref, tree);
6506 static const char *type_tag (const_tree);
6507 static tree member_declared_type (const_tree);
6508 #if 0
6509 static const char *decl_start_label (tree);
6510 #endif
6511 static void gen_array_type_die (tree, dw_die_ref);
6512 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6513 #if 0
6514 static void gen_entry_point_die (tree, dw_die_ref);
6515 #endif
6516 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6517 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6518 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6519 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6520 static void gen_formal_types_die (tree, dw_die_ref);
6521 static void gen_subprogram_die (tree, dw_die_ref);
6522 static void gen_variable_die (tree, tree, dw_die_ref);
6523 static void gen_const_die (tree, dw_die_ref);
6524 static void gen_label_die (tree, dw_die_ref);
6525 static void gen_lexical_block_die (tree, dw_die_ref, int);
6526 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6527 static void gen_field_die (tree, dw_die_ref);
6528 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6529 static dw_die_ref gen_compile_unit_die (const char *);
6530 static void gen_inheritance_die (tree, tree, dw_die_ref);
6531 static void gen_member_die (tree, dw_die_ref);
6532 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6533                                                 enum debug_info_usage);
6534 static void gen_subroutine_type_die (tree, dw_die_ref);
6535 static void gen_typedef_die (tree, dw_die_ref);
6536 static void gen_type_die (tree, dw_die_ref);
6537 static void gen_block_die (tree, dw_die_ref, int);
6538 static void decls_for_scope (tree, dw_die_ref, int);
6539 static int is_redundant_typedef (const_tree);
6540 static bool is_naming_typedef_decl (const_tree);
6541 static inline dw_die_ref get_context_die (tree);
6542 static void gen_namespace_die (tree, dw_die_ref);
6543 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6544 static dw_die_ref force_decl_die (tree);
6545 static dw_die_ref force_type_die (tree);
6546 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6547 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6548 static struct dwarf_file_data * lookup_filename (const char *);
6549 static void retry_incomplete_types (void);
6550 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6551 static void gen_generic_params_dies (tree);
6552 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6553 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6554 static void splice_child_die (dw_die_ref, dw_die_ref);
6555 static int file_info_cmp (const void *, const void *);
6556 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6557                                      const char *, const char *);
6558 static void output_loc_list (dw_loc_list_ref);
6559 static char *gen_internal_sym (const char *);
6560
6561 static void prune_unmark_dies (dw_die_ref);
6562 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6563 static void prune_unused_types_mark (dw_die_ref, int);
6564 static void prune_unused_types_walk (dw_die_ref);
6565 static void prune_unused_types_walk_attribs (dw_die_ref);
6566 static void prune_unused_types_prune (dw_die_ref);
6567 static void prune_unused_types (void);
6568 static int maybe_emit_file (struct dwarf_file_data *fd);
6569 static inline const char *AT_vms_delta1 (dw_attr_ref);
6570 static inline const char *AT_vms_delta2 (dw_attr_ref);
6571 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6572                                      const char *, const char *);
6573 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6574 static void gen_remaining_tmpl_value_param_die_attribute (void);
6575 static bool generic_type_p (tree);
6576 static void schedule_generic_params_dies_gen (tree t);
6577 static void gen_scheduled_generic_parms_dies (void);
6578
6579 /* Section names used to hold DWARF debugging information.  */
6580 #ifndef DEBUG_INFO_SECTION
6581 #define DEBUG_INFO_SECTION      ".debug_info"
6582 #endif
6583 #ifndef DEBUG_ABBREV_SECTION
6584 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6585 #endif
6586 #ifndef DEBUG_ARANGES_SECTION
6587 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6588 #endif
6589 #ifndef DEBUG_MACINFO_SECTION
6590 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6591 #endif
6592 #ifndef DEBUG_LINE_SECTION
6593 #define DEBUG_LINE_SECTION      ".debug_line"
6594 #endif
6595 #ifndef DEBUG_LOC_SECTION
6596 #define DEBUG_LOC_SECTION       ".debug_loc"
6597 #endif
6598 #ifndef DEBUG_PUBNAMES_SECTION
6599 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6600 #endif
6601 #ifndef DEBUG_PUBTYPES_SECTION
6602 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6603 #endif
6604 #ifndef DEBUG_STR_SECTION
6605 #define DEBUG_STR_SECTION       ".debug_str"
6606 #endif
6607 #ifndef DEBUG_RANGES_SECTION
6608 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6609 #endif
6610
6611 /* Standard ELF section names for compiled code and data.  */
6612 #ifndef TEXT_SECTION_NAME
6613 #define TEXT_SECTION_NAME       ".text"
6614 #endif
6615
6616 /* Section flags for .debug_str section.  */
6617 #define DEBUG_STR_SECTION_FLAGS \
6618   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6619    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6620    : SECTION_DEBUG)
6621
6622 /* Labels we insert at beginning sections we can reference instead of
6623    the section names themselves.  */
6624
6625 #ifndef TEXT_SECTION_LABEL
6626 #define TEXT_SECTION_LABEL              "Ltext"
6627 #endif
6628 #ifndef COLD_TEXT_SECTION_LABEL
6629 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6630 #endif
6631 #ifndef DEBUG_LINE_SECTION_LABEL
6632 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6633 #endif
6634 #ifndef DEBUG_INFO_SECTION_LABEL
6635 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6636 #endif
6637 #ifndef DEBUG_ABBREV_SECTION_LABEL
6638 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6639 #endif
6640 #ifndef DEBUG_LOC_SECTION_LABEL
6641 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6642 #endif
6643 #ifndef DEBUG_RANGES_SECTION_LABEL
6644 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6645 #endif
6646 #ifndef DEBUG_MACINFO_SECTION_LABEL
6647 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6648 #endif
6649
6650
6651 /* Definitions of defaults for formats and names of various special
6652    (artificial) labels which may be generated within this file (when the -g
6653    options is used and DWARF2_DEBUGGING_INFO is in effect.
6654    If necessary, these may be overridden from within the tm.h file, but
6655    typically, overriding these defaults is unnecessary.  */
6656
6657 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6658 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6659 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6660 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6661 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6662 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6663 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6664 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6665 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6666 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6667
6668 #ifndef TEXT_END_LABEL
6669 #define TEXT_END_LABEL          "Letext"
6670 #endif
6671 #ifndef COLD_END_LABEL
6672 #define COLD_END_LABEL          "Letext_cold"
6673 #endif
6674 #ifndef BLOCK_BEGIN_LABEL
6675 #define BLOCK_BEGIN_LABEL       "LBB"
6676 #endif
6677 #ifndef BLOCK_END_LABEL
6678 #define BLOCK_END_LABEL         "LBE"
6679 #endif
6680 #ifndef LINE_CODE_LABEL
6681 #define LINE_CODE_LABEL         "LM"
6682 #endif
6683 #ifndef SEPARATE_LINE_CODE_LABEL
6684 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6685 #endif
6686
6687 \f
6688 /* Return the root of the DIE's built for the current compilation unit.  */
6689 static dw_die_ref
6690 comp_unit_die (void)
6691 {
6692   if (!single_comp_unit_die)
6693     single_comp_unit_die = gen_compile_unit_die (NULL);
6694   return single_comp_unit_die;
6695 }
6696
6697 /* We allow a language front-end to designate a function that is to be
6698    called to "demangle" any name before it is put into a DIE.  */
6699
6700 static const char *(*demangle_name_func) (const char *);
6701
6702 void
6703 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6704 {
6705   demangle_name_func = func;
6706 }
6707
6708 /* Test if rtl node points to a pseudo register.  */
6709
6710 static inline int
6711 is_pseudo_reg (const_rtx rtl)
6712 {
6713   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6714           || (GET_CODE (rtl) == SUBREG
6715               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6716 }
6717
6718 /* Return a reference to a type, with its const and volatile qualifiers
6719    removed.  */
6720
6721 static inline tree
6722 type_main_variant (tree type)
6723 {
6724   type = TYPE_MAIN_VARIANT (type);
6725
6726   /* ??? There really should be only one main variant among any group of
6727      variants of a given type (and all of the MAIN_VARIANT values for all
6728      members of the group should point to that one type) but sometimes the C
6729      front-end messes this up for array types, so we work around that bug
6730      here.  */
6731   if (TREE_CODE (type) == ARRAY_TYPE)
6732     while (type != TYPE_MAIN_VARIANT (type))
6733       type = TYPE_MAIN_VARIANT (type);
6734
6735   return type;
6736 }
6737
6738 /* Return nonzero if the given type node represents a tagged type.  */
6739
6740 static inline int
6741 is_tagged_type (const_tree type)
6742 {
6743   enum tree_code code = TREE_CODE (type);
6744
6745   return (code == RECORD_TYPE || code == UNION_TYPE
6746           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6747 }
6748
6749 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6750
6751 static void
6752 get_ref_die_offset_label (char *label, dw_die_ref ref)
6753 {
6754   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6755 }
6756
6757 /* Convert a DIE tag into its string name.  */
6758
6759 static const char *
6760 dwarf_tag_name (unsigned int tag)
6761 {
6762   switch (tag)
6763     {
6764     case DW_TAG_padding:
6765       return "DW_TAG_padding";
6766     case DW_TAG_array_type:
6767       return "DW_TAG_array_type";
6768     case DW_TAG_class_type:
6769       return "DW_TAG_class_type";
6770     case DW_TAG_entry_point:
6771       return "DW_TAG_entry_point";
6772     case DW_TAG_enumeration_type:
6773       return "DW_TAG_enumeration_type";
6774     case DW_TAG_formal_parameter:
6775       return "DW_TAG_formal_parameter";
6776     case DW_TAG_imported_declaration:
6777       return "DW_TAG_imported_declaration";
6778     case DW_TAG_label:
6779       return "DW_TAG_label";
6780     case DW_TAG_lexical_block:
6781       return "DW_TAG_lexical_block";
6782     case DW_TAG_member:
6783       return "DW_TAG_member";
6784     case DW_TAG_pointer_type:
6785       return "DW_TAG_pointer_type";
6786     case DW_TAG_reference_type:
6787       return "DW_TAG_reference_type";
6788     case DW_TAG_compile_unit:
6789       return "DW_TAG_compile_unit";
6790     case DW_TAG_string_type:
6791       return "DW_TAG_string_type";
6792     case DW_TAG_structure_type:
6793       return "DW_TAG_structure_type";
6794     case DW_TAG_subroutine_type:
6795       return "DW_TAG_subroutine_type";
6796     case DW_TAG_typedef:
6797       return "DW_TAG_typedef";
6798     case DW_TAG_union_type:
6799       return "DW_TAG_union_type";
6800     case DW_TAG_unspecified_parameters:
6801       return "DW_TAG_unspecified_parameters";
6802     case DW_TAG_variant:
6803       return "DW_TAG_variant";
6804     case DW_TAG_common_block:
6805       return "DW_TAG_common_block";
6806     case DW_TAG_common_inclusion:
6807       return "DW_TAG_common_inclusion";
6808     case DW_TAG_inheritance:
6809       return "DW_TAG_inheritance";
6810     case DW_TAG_inlined_subroutine:
6811       return "DW_TAG_inlined_subroutine";
6812     case DW_TAG_module:
6813       return "DW_TAG_module";
6814     case DW_TAG_ptr_to_member_type:
6815       return "DW_TAG_ptr_to_member_type";
6816     case DW_TAG_set_type:
6817       return "DW_TAG_set_type";
6818     case DW_TAG_subrange_type:
6819       return "DW_TAG_subrange_type";
6820     case DW_TAG_with_stmt:
6821       return "DW_TAG_with_stmt";
6822     case DW_TAG_access_declaration:
6823       return "DW_TAG_access_declaration";
6824     case DW_TAG_base_type:
6825       return "DW_TAG_base_type";
6826     case DW_TAG_catch_block:
6827       return "DW_TAG_catch_block";
6828     case DW_TAG_const_type:
6829       return "DW_TAG_const_type";
6830     case DW_TAG_constant:
6831       return "DW_TAG_constant";
6832     case DW_TAG_enumerator:
6833       return "DW_TAG_enumerator";
6834     case DW_TAG_file_type:
6835       return "DW_TAG_file_type";
6836     case DW_TAG_friend:
6837       return "DW_TAG_friend";
6838     case DW_TAG_namelist:
6839       return "DW_TAG_namelist";
6840     case DW_TAG_namelist_item:
6841       return "DW_TAG_namelist_item";
6842     case DW_TAG_packed_type:
6843       return "DW_TAG_packed_type";
6844     case DW_TAG_subprogram:
6845       return "DW_TAG_subprogram";
6846     case DW_TAG_template_type_param:
6847       return "DW_TAG_template_type_param";
6848     case DW_TAG_template_value_param:
6849       return "DW_TAG_template_value_param";
6850     case DW_TAG_thrown_type:
6851       return "DW_TAG_thrown_type";
6852     case DW_TAG_try_block:
6853       return "DW_TAG_try_block";
6854     case DW_TAG_variant_part:
6855       return "DW_TAG_variant_part";
6856     case DW_TAG_variable:
6857       return "DW_TAG_variable";
6858     case DW_TAG_volatile_type:
6859       return "DW_TAG_volatile_type";
6860     case DW_TAG_dwarf_procedure:
6861       return "DW_TAG_dwarf_procedure";
6862     case DW_TAG_restrict_type:
6863       return "DW_TAG_restrict_type";
6864     case DW_TAG_interface_type:
6865       return "DW_TAG_interface_type";
6866     case DW_TAG_namespace:
6867       return "DW_TAG_namespace";
6868     case DW_TAG_imported_module:
6869       return "DW_TAG_imported_module";
6870     case DW_TAG_unspecified_type:
6871       return "DW_TAG_unspecified_type";
6872     case DW_TAG_partial_unit:
6873       return "DW_TAG_partial_unit";
6874     case DW_TAG_imported_unit:
6875       return "DW_TAG_imported_unit";
6876     case DW_TAG_condition:
6877       return "DW_TAG_condition";
6878     case DW_TAG_shared_type:
6879       return "DW_TAG_shared_type";
6880     case DW_TAG_type_unit:
6881       return "DW_TAG_type_unit";
6882     case DW_TAG_rvalue_reference_type:
6883       return "DW_TAG_rvalue_reference_type";
6884     case DW_TAG_template_alias:
6885       return "DW_TAG_template_alias";
6886     case DW_TAG_GNU_template_parameter_pack:
6887       return "DW_TAG_GNU_template_parameter_pack";
6888     case DW_TAG_GNU_formal_parameter_pack:
6889       return "DW_TAG_GNU_formal_parameter_pack";
6890     case DW_TAG_MIPS_loop:
6891       return "DW_TAG_MIPS_loop";
6892     case DW_TAG_format_label:
6893       return "DW_TAG_format_label";
6894     case DW_TAG_function_template:
6895       return "DW_TAG_function_template";
6896     case DW_TAG_class_template:
6897       return "DW_TAG_class_template";
6898     case DW_TAG_GNU_BINCL:
6899       return "DW_TAG_GNU_BINCL";
6900     case DW_TAG_GNU_EINCL:
6901       return "DW_TAG_GNU_EINCL";
6902     case DW_TAG_GNU_template_template_param:
6903       return "DW_TAG_GNU_template_template_param";
6904     case DW_TAG_GNU_call_site:
6905       return "DW_TAG_GNU_call_site";
6906     case DW_TAG_GNU_call_site_parameter:
6907       return "DW_TAG_GNU_call_site_parameter";
6908     default:
6909       return "DW_TAG_<unknown>";
6910     }
6911 }
6912
6913 /* Convert a DWARF attribute code into its string name.  */
6914
6915 static const char *
6916 dwarf_attr_name (unsigned int attr)
6917 {
6918   switch (attr)
6919     {
6920     case DW_AT_sibling:
6921       return "DW_AT_sibling";
6922     case DW_AT_location:
6923       return "DW_AT_location";
6924     case DW_AT_name:
6925       return "DW_AT_name";
6926     case DW_AT_ordering:
6927       return "DW_AT_ordering";
6928     case DW_AT_subscr_data:
6929       return "DW_AT_subscr_data";
6930     case DW_AT_byte_size:
6931       return "DW_AT_byte_size";
6932     case DW_AT_bit_offset:
6933       return "DW_AT_bit_offset";
6934     case DW_AT_bit_size:
6935       return "DW_AT_bit_size";
6936     case DW_AT_element_list:
6937       return "DW_AT_element_list";
6938     case DW_AT_stmt_list:
6939       return "DW_AT_stmt_list";
6940     case DW_AT_low_pc:
6941       return "DW_AT_low_pc";
6942     case DW_AT_high_pc:
6943       return "DW_AT_high_pc";
6944     case DW_AT_language:
6945       return "DW_AT_language";
6946     case DW_AT_member:
6947       return "DW_AT_member";
6948     case DW_AT_discr:
6949       return "DW_AT_discr";
6950     case DW_AT_discr_value:
6951       return "DW_AT_discr_value";
6952     case DW_AT_visibility:
6953       return "DW_AT_visibility";
6954     case DW_AT_import:
6955       return "DW_AT_import";
6956     case DW_AT_string_length:
6957       return "DW_AT_string_length";
6958     case DW_AT_common_reference:
6959       return "DW_AT_common_reference";
6960     case DW_AT_comp_dir:
6961       return "DW_AT_comp_dir";
6962     case DW_AT_const_value:
6963       return "DW_AT_const_value";
6964     case DW_AT_containing_type:
6965       return "DW_AT_containing_type";
6966     case DW_AT_default_value:
6967       return "DW_AT_default_value";
6968     case DW_AT_inline:
6969       return "DW_AT_inline";
6970     case DW_AT_is_optional:
6971       return "DW_AT_is_optional";
6972     case DW_AT_lower_bound:
6973       return "DW_AT_lower_bound";
6974     case DW_AT_producer:
6975       return "DW_AT_producer";
6976     case DW_AT_prototyped:
6977       return "DW_AT_prototyped";
6978     case DW_AT_return_addr:
6979       return "DW_AT_return_addr";
6980     case DW_AT_start_scope:
6981       return "DW_AT_start_scope";
6982     case DW_AT_bit_stride:
6983       return "DW_AT_bit_stride";
6984     case DW_AT_upper_bound:
6985       return "DW_AT_upper_bound";
6986     case DW_AT_abstract_origin:
6987       return "DW_AT_abstract_origin";
6988     case DW_AT_accessibility:
6989       return "DW_AT_accessibility";
6990     case DW_AT_address_class:
6991       return "DW_AT_address_class";
6992     case DW_AT_artificial:
6993       return "DW_AT_artificial";
6994     case DW_AT_base_types:
6995       return "DW_AT_base_types";
6996     case DW_AT_calling_convention:
6997       return "DW_AT_calling_convention";
6998     case DW_AT_count:
6999       return "DW_AT_count";
7000     case DW_AT_data_member_location:
7001       return "DW_AT_data_member_location";
7002     case DW_AT_decl_column:
7003       return "DW_AT_decl_column";
7004     case DW_AT_decl_file:
7005       return "DW_AT_decl_file";
7006     case DW_AT_decl_line:
7007       return "DW_AT_decl_line";
7008     case DW_AT_declaration:
7009       return "DW_AT_declaration";
7010     case DW_AT_discr_list:
7011       return "DW_AT_discr_list";
7012     case DW_AT_encoding:
7013       return "DW_AT_encoding";
7014     case DW_AT_external:
7015       return "DW_AT_external";
7016     case DW_AT_explicit:
7017       return "DW_AT_explicit";
7018     case DW_AT_frame_base:
7019       return "DW_AT_frame_base";
7020     case DW_AT_friend:
7021       return "DW_AT_friend";
7022     case DW_AT_identifier_case:
7023       return "DW_AT_identifier_case";
7024     case DW_AT_macro_info:
7025       return "DW_AT_macro_info";
7026     case DW_AT_namelist_items:
7027       return "DW_AT_namelist_items";
7028     case DW_AT_priority:
7029       return "DW_AT_priority";
7030     case DW_AT_segment:
7031       return "DW_AT_segment";
7032     case DW_AT_specification:
7033       return "DW_AT_specification";
7034     case DW_AT_static_link:
7035       return "DW_AT_static_link";
7036     case DW_AT_type:
7037       return "DW_AT_type";
7038     case DW_AT_use_location:
7039       return "DW_AT_use_location";
7040     case DW_AT_variable_parameter:
7041       return "DW_AT_variable_parameter";
7042     case DW_AT_virtuality:
7043       return "DW_AT_virtuality";
7044     case DW_AT_vtable_elem_location:
7045       return "DW_AT_vtable_elem_location";
7046
7047     case DW_AT_allocated:
7048       return "DW_AT_allocated";
7049     case DW_AT_associated:
7050       return "DW_AT_associated";
7051     case DW_AT_data_location:
7052       return "DW_AT_data_location";
7053     case DW_AT_byte_stride:
7054       return "DW_AT_byte_stride";
7055     case DW_AT_entry_pc:
7056       return "DW_AT_entry_pc";
7057     case DW_AT_use_UTF8:
7058       return "DW_AT_use_UTF8";
7059     case DW_AT_extension:
7060       return "DW_AT_extension";
7061     case DW_AT_ranges:
7062       return "DW_AT_ranges";
7063     case DW_AT_trampoline:
7064       return "DW_AT_trampoline";
7065     case DW_AT_call_column:
7066       return "DW_AT_call_column";
7067     case DW_AT_call_file:
7068       return "DW_AT_call_file";
7069     case DW_AT_call_line:
7070       return "DW_AT_call_line";
7071     case DW_AT_object_pointer:
7072       return "DW_AT_object_pointer";
7073
7074     case DW_AT_signature:
7075       return "DW_AT_signature";
7076     case DW_AT_main_subprogram:
7077       return "DW_AT_main_subprogram";
7078     case DW_AT_data_bit_offset:
7079       return "DW_AT_data_bit_offset";
7080     case DW_AT_const_expr:
7081       return "DW_AT_const_expr";
7082     case DW_AT_enum_class:
7083       return "DW_AT_enum_class";
7084     case DW_AT_linkage_name:
7085       return "DW_AT_linkage_name";
7086
7087     case DW_AT_MIPS_fde:
7088       return "DW_AT_MIPS_fde";
7089     case DW_AT_MIPS_loop_begin:
7090       return "DW_AT_MIPS_loop_begin";
7091     case DW_AT_MIPS_tail_loop_begin:
7092       return "DW_AT_MIPS_tail_loop_begin";
7093     case DW_AT_MIPS_epilog_begin:
7094       return "DW_AT_MIPS_epilog_begin";
7095 #if VMS_DEBUGGING_INFO
7096     case DW_AT_HP_prologue:
7097       return "DW_AT_HP_prologue";
7098 #else
7099     case DW_AT_MIPS_loop_unroll_factor:
7100       return "DW_AT_MIPS_loop_unroll_factor";
7101 #endif
7102     case DW_AT_MIPS_software_pipeline_depth:
7103       return "DW_AT_MIPS_software_pipeline_depth";
7104     case DW_AT_MIPS_linkage_name:
7105       return "DW_AT_MIPS_linkage_name";
7106 #if VMS_DEBUGGING_INFO
7107     case DW_AT_HP_epilogue:
7108       return "DW_AT_HP_epilogue";
7109 #else
7110     case DW_AT_MIPS_stride:
7111       return "DW_AT_MIPS_stride";
7112 #endif
7113     case DW_AT_MIPS_abstract_name:
7114       return "DW_AT_MIPS_abstract_name";
7115     case DW_AT_MIPS_clone_origin:
7116       return "DW_AT_MIPS_clone_origin";
7117     case DW_AT_MIPS_has_inlines:
7118       return "DW_AT_MIPS_has_inlines";
7119
7120     case DW_AT_sf_names:
7121       return "DW_AT_sf_names";
7122     case DW_AT_src_info:
7123       return "DW_AT_src_info";
7124     case DW_AT_mac_info:
7125       return "DW_AT_mac_info";
7126     case DW_AT_src_coords:
7127       return "DW_AT_src_coords";
7128     case DW_AT_body_begin:
7129       return "DW_AT_body_begin";
7130     case DW_AT_body_end:
7131       return "DW_AT_body_end";
7132     case DW_AT_GNU_vector:
7133       return "DW_AT_GNU_vector";
7134     case DW_AT_GNU_guarded_by:
7135       return "DW_AT_GNU_guarded_by";
7136     case DW_AT_GNU_pt_guarded_by:
7137       return "DW_AT_GNU_pt_guarded_by";
7138     case DW_AT_GNU_guarded:
7139       return "DW_AT_GNU_guarded";
7140     case DW_AT_GNU_pt_guarded:
7141       return "DW_AT_GNU_pt_guarded";
7142     case DW_AT_GNU_locks_excluded:
7143       return "DW_AT_GNU_locks_excluded";
7144     case DW_AT_GNU_exclusive_locks_required:
7145       return "DW_AT_GNU_exclusive_locks_required";
7146     case DW_AT_GNU_shared_locks_required:
7147       return "DW_AT_GNU_shared_locks_required";
7148     case DW_AT_GNU_odr_signature:
7149       return "DW_AT_GNU_odr_signature";
7150     case DW_AT_GNU_template_name:
7151       return "DW_AT_GNU_template_name";
7152     case DW_AT_GNU_call_site_value:
7153       return "DW_AT_GNU_call_site_value";
7154     case DW_AT_GNU_call_site_data_value:
7155       return "DW_AT_GNU_call_site_data_value";
7156     case DW_AT_GNU_call_site_target:
7157       return "DW_AT_GNU_call_site_target";
7158     case DW_AT_GNU_call_site_target_clobbered:
7159       return "DW_AT_GNU_call_site_target_clobbered";
7160     case DW_AT_GNU_tail_call:
7161       return "DW_AT_GNU_tail_call";
7162     case DW_AT_GNU_all_tail_call_sites:
7163       return "DW_AT_GNU_all_tail_call_sites";
7164     case DW_AT_GNU_all_call_sites:
7165       return "DW_AT_GNU_all_call_sites";
7166     case DW_AT_GNU_all_source_call_sites:
7167       return "DW_AT_GNU_all_source_call_sites";
7168
7169     case DW_AT_VMS_rtnbeg_pd_address:
7170       return "DW_AT_VMS_rtnbeg_pd_address";
7171
7172     default:
7173       return "DW_AT_<unknown>";
7174     }
7175 }
7176
7177 /* Convert a DWARF value form code into its string name.  */
7178
7179 static const char *
7180 dwarf_form_name (unsigned int form)
7181 {
7182   switch (form)
7183     {
7184     case DW_FORM_addr:
7185       return "DW_FORM_addr";
7186     case DW_FORM_block2:
7187       return "DW_FORM_block2";
7188     case DW_FORM_block4:
7189       return "DW_FORM_block4";
7190     case DW_FORM_data2:
7191       return "DW_FORM_data2";
7192     case DW_FORM_data4:
7193       return "DW_FORM_data4";
7194     case DW_FORM_data8:
7195       return "DW_FORM_data8";
7196     case DW_FORM_string:
7197       return "DW_FORM_string";
7198     case DW_FORM_block:
7199       return "DW_FORM_block";
7200     case DW_FORM_block1:
7201       return "DW_FORM_block1";
7202     case DW_FORM_data1:
7203       return "DW_FORM_data1";
7204     case DW_FORM_flag:
7205       return "DW_FORM_flag";
7206     case DW_FORM_sdata:
7207       return "DW_FORM_sdata";
7208     case DW_FORM_strp:
7209       return "DW_FORM_strp";
7210     case DW_FORM_udata:
7211       return "DW_FORM_udata";
7212     case DW_FORM_ref_addr:
7213       return "DW_FORM_ref_addr";
7214     case DW_FORM_ref1:
7215       return "DW_FORM_ref1";
7216     case DW_FORM_ref2:
7217       return "DW_FORM_ref2";
7218     case DW_FORM_ref4:
7219       return "DW_FORM_ref4";
7220     case DW_FORM_ref8:
7221       return "DW_FORM_ref8";
7222     case DW_FORM_ref_udata:
7223       return "DW_FORM_ref_udata";
7224     case DW_FORM_indirect:
7225       return "DW_FORM_indirect";
7226     case DW_FORM_sec_offset:
7227       return "DW_FORM_sec_offset";
7228     case DW_FORM_exprloc:
7229       return "DW_FORM_exprloc";
7230     case DW_FORM_flag_present:
7231       return "DW_FORM_flag_present";
7232     case DW_FORM_ref_sig8:
7233       return "DW_FORM_ref_sig8";
7234     default:
7235       return "DW_FORM_<unknown>";
7236     }
7237 }
7238 \f
7239 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7240    instance of an inlined instance of a decl which is local to an inline
7241    function, so we have to trace all of the way back through the origin chain
7242    to find out what sort of node actually served as the original seed for the
7243    given block.  */
7244
7245 static tree
7246 decl_ultimate_origin (const_tree decl)
7247 {
7248   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7249     return NULL_TREE;
7250
7251   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7252      nodes in the function to point to themselves; ignore that if
7253      we're trying to output the abstract instance of this function.  */
7254   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7255     return NULL_TREE;
7256
7257   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7258      most distant ancestor, this should never happen.  */
7259   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7260
7261   return DECL_ABSTRACT_ORIGIN (decl);
7262 }
7263
7264 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7265    of a virtual function may refer to a base class, so we check the 'this'
7266    parameter.  */
7267
7268 static tree
7269 decl_class_context (tree decl)
7270 {
7271   tree context = NULL_TREE;
7272
7273   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7274     context = DECL_CONTEXT (decl);
7275   else
7276     context = TYPE_MAIN_VARIANT
7277       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7278
7279   if (context && !TYPE_P (context))
7280     context = NULL_TREE;
7281
7282   return context;
7283 }
7284 \f
7285 /* Add an attribute/value pair to a DIE.  */
7286
7287 static inline void
7288 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7289 {
7290   /* Maybe this should be an assert?  */
7291   if (die == NULL)
7292     return;
7293
7294   if (die->die_attr == NULL)
7295     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7296   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7297 }
7298
7299 static inline enum dw_val_class
7300 AT_class (dw_attr_ref a)
7301 {
7302   return a->dw_attr_val.val_class;
7303 }
7304
7305 /* Add a flag value attribute to a DIE.  */
7306
7307 static inline void
7308 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7309 {
7310   dw_attr_node attr;
7311
7312   attr.dw_attr = attr_kind;
7313   attr.dw_attr_val.val_class = dw_val_class_flag;
7314   attr.dw_attr_val.v.val_flag = flag;
7315   add_dwarf_attr (die, &attr);
7316 }
7317
7318 static inline unsigned
7319 AT_flag (dw_attr_ref a)
7320 {
7321   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7322   return a->dw_attr_val.v.val_flag;
7323 }
7324
7325 /* Add a signed integer attribute value to a DIE.  */
7326
7327 static inline void
7328 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7329 {
7330   dw_attr_node attr;
7331
7332   attr.dw_attr = attr_kind;
7333   attr.dw_attr_val.val_class = dw_val_class_const;
7334   attr.dw_attr_val.v.val_int = int_val;
7335   add_dwarf_attr (die, &attr);
7336 }
7337
7338 static inline HOST_WIDE_INT
7339 AT_int (dw_attr_ref a)
7340 {
7341   gcc_assert (a && AT_class (a) == dw_val_class_const);
7342   return a->dw_attr_val.v.val_int;
7343 }
7344
7345 /* Add an unsigned integer attribute value to a DIE.  */
7346
7347 static inline void
7348 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7349                  unsigned HOST_WIDE_INT unsigned_val)
7350 {
7351   dw_attr_node attr;
7352
7353   attr.dw_attr = attr_kind;
7354   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7355   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7356   add_dwarf_attr (die, &attr);
7357 }
7358
7359 static inline unsigned HOST_WIDE_INT
7360 AT_unsigned (dw_attr_ref a)
7361 {
7362   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7363   return a->dw_attr_val.v.val_unsigned;
7364 }
7365
7366 /* Add an unsigned double integer attribute value to a DIE.  */
7367
7368 static inline void
7369 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7370                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7371 {
7372   dw_attr_node attr;
7373
7374   attr.dw_attr = attr_kind;
7375   attr.dw_attr_val.val_class = dw_val_class_const_double;
7376   attr.dw_attr_val.v.val_double.high = high;
7377   attr.dw_attr_val.v.val_double.low = low;
7378   add_dwarf_attr (die, &attr);
7379 }
7380
7381 /* Add a floating point attribute value to a DIE and return it.  */
7382
7383 static inline void
7384 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7385             unsigned int length, unsigned int elt_size, unsigned char *array)
7386 {
7387   dw_attr_node attr;
7388
7389   attr.dw_attr = attr_kind;
7390   attr.dw_attr_val.val_class = dw_val_class_vec;
7391   attr.dw_attr_val.v.val_vec.length = length;
7392   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7393   attr.dw_attr_val.v.val_vec.array = array;
7394   add_dwarf_attr (die, &attr);
7395 }
7396
7397 /* Add an 8-byte data attribute value to a DIE.  */
7398
7399 static inline void
7400 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7401               unsigned char data8[8])
7402 {
7403   dw_attr_node attr;
7404
7405   attr.dw_attr = attr_kind;
7406   attr.dw_attr_val.val_class = dw_val_class_data8;
7407   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7408   add_dwarf_attr (die, &attr);
7409 }
7410
7411 /* Hash and equality functions for debug_str_hash.  */
7412
7413 static hashval_t
7414 debug_str_do_hash (const void *x)
7415 {
7416   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7417 }
7418
7419 static int
7420 debug_str_eq (const void *x1, const void *x2)
7421 {
7422   return strcmp ((((const struct indirect_string_node *)x1)->str),
7423                  (const char *)x2) == 0;
7424 }
7425
7426 /* Add STR to the indirect string hash table.  */
7427
7428 static struct indirect_string_node *
7429 find_AT_string (const char *str)
7430 {
7431   struct indirect_string_node *node;
7432   void **slot;
7433
7434   if (! debug_str_hash)
7435     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7436                                       debug_str_eq, NULL);
7437
7438   slot = htab_find_slot_with_hash (debug_str_hash, str,
7439                                    htab_hash_string (str), INSERT);
7440   if (*slot == NULL)
7441     {
7442       node = ggc_alloc_cleared_indirect_string_node ();
7443       node->str = ggc_strdup (str);
7444       *slot = node;
7445     }
7446   else
7447     node = (struct indirect_string_node *) *slot;
7448
7449   node->refcount++;
7450   return node;
7451 }
7452
7453 /* Add a string attribute value to a DIE.  */
7454
7455 static inline void
7456 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7457 {
7458   dw_attr_node attr;
7459   struct indirect_string_node *node;
7460
7461   node = find_AT_string (str);
7462
7463   attr.dw_attr = attr_kind;
7464   attr.dw_attr_val.val_class = dw_val_class_str;
7465   attr.dw_attr_val.v.val_str = node;
7466   add_dwarf_attr (die, &attr);
7467 }
7468
7469 /* Create a label for an indirect string node, ensuring it is going to
7470    be output, unless its reference count goes down to zero.  */
7471
7472 static inline void
7473 gen_label_for_indirect_string (struct indirect_string_node *node)
7474 {
7475   char label[32];
7476
7477   if (node->label)
7478     return;
7479
7480   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7481   ++dw2_string_counter;
7482   node->label = xstrdup (label);
7483 }
7484
7485 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7486    debug string STR.  */
7487
7488 static inline rtx
7489 get_debug_string_label (const char *str)
7490 {
7491   struct indirect_string_node *node = find_AT_string (str);
7492
7493   debug_str_hash_forced = true;
7494
7495   gen_label_for_indirect_string (node);
7496
7497   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7498 }
7499
7500 static inline const char *
7501 AT_string (dw_attr_ref a)
7502 {
7503   gcc_assert (a && AT_class (a) == dw_val_class_str);
7504   return a->dw_attr_val.v.val_str->str;
7505 }
7506
7507 /* Find out whether a string should be output inline in DIE
7508    or out-of-line in .debug_str section.  */
7509
7510 static enum dwarf_form
7511 AT_string_form (dw_attr_ref a)
7512 {
7513   struct indirect_string_node *node;
7514   unsigned int len;
7515
7516   gcc_assert (a && AT_class (a) == dw_val_class_str);
7517
7518   node = a->dw_attr_val.v.val_str;
7519   if (node->form)
7520     return node->form;
7521
7522   len = strlen (node->str) + 1;
7523
7524   /* If the string is shorter or equal to the size of the reference, it is
7525      always better to put it inline.  */
7526   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7527     return node->form = DW_FORM_string;
7528
7529   /* If we cannot expect the linker to merge strings in .debug_str
7530      section, only put it into .debug_str if it is worth even in this
7531      single module.  */
7532   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7533       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7534       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7535     return node->form = DW_FORM_string;
7536
7537   gen_label_for_indirect_string (node);
7538
7539   return node->form = DW_FORM_strp;
7540 }
7541
7542 /* Add a DIE reference attribute value to a DIE.  */
7543
7544 static inline void
7545 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7546 {
7547   dw_attr_node attr;
7548
7549 #ifdef ENABLE_CHECKING
7550   gcc_assert (targ_die != NULL);
7551 #else
7552   /* With LTO we can end up trying to reference something we didn't create
7553      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7554   if (targ_die == NULL)
7555     return;
7556 #endif
7557
7558   attr.dw_attr = attr_kind;
7559   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7560   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7561   attr.dw_attr_val.v.val_die_ref.external = 0;
7562   add_dwarf_attr (die, &attr);
7563 }
7564
7565 /* Add an AT_specification attribute to a DIE, and also make the back
7566    pointer from the specification to the definition.  */
7567
7568 static inline void
7569 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7570 {
7571   add_AT_die_ref (die, DW_AT_specification, targ_die);
7572   gcc_assert (!targ_die->die_definition);
7573   targ_die->die_definition = die;
7574 }
7575
7576 static inline dw_die_ref
7577 AT_ref (dw_attr_ref a)
7578 {
7579   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7580   return a->dw_attr_val.v.val_die_ref.die;
7581 }
7582
7583 static inline int
7584 AT_ref_external (dw_attr_ref a)
7585 {
7586   if (a && AT_class (a) == dw_val_class_die_ref)
7587     return a->dw_attr_val.v.val_die_ref.external;
7588
7589   return 0;
7590 }
7591
7592 static inline void
7593 set_AT_ref_external (dw_attr_ref a, int i)
7594 {
7595   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7596   a->dw_attr_val.v.val_die_ref.external = i;
7597 }
7598
7599 /* Add an FDE reference attribute value to a DIE.  */
7600
7601 static inline void
7602 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7603 {
7604   dw_attr_node attr;
7605
7606   attr.dw_attr = attr_kind;
7607   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7608   attr.dw_attr_val.v.val_fde_index = targ_fde;
7609   add_dwarf_attr (die, &attr);
7610 }
7611
7612 /* Add a location description attribute value to a DIE.  */
7613
7614 static inline void
7615 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7616 {
7617   dw_attr_node attr;
7618
7619   attr.dw_attr = attr_kind;
7620   attr.dw_attr_val.val_class = dw_val_class_loc;
7621   attr.dw_attr_val.v.val_loc = loc;
7622   add_dwarf_attr (die, &attr);
7623 }
7624
7625 static inline dw_loc_descr_ref
7626 AT_loc (dw_attr_ref a)
7627 {
7628   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7629   return a->dw_attr_val.v.val_loc;
7630 }
7631
7632 static inline void
7633 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7634 {
7635   dw_attr_node attr;
7636
7637   attr.dw_attr = attr_kind;
7638   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7639   attr.dw_attr_val.v.val_loc_list = loc_list;
7640   add_dwarf_attr (die, &attr);
7641   have_location_lists = true;
7642 }
7643
7644 static inline dw_loc_list_ref
7645 AT_loc_list (dw_attr_ref a)
7646 {
7647   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7648   return a->dw_attr_val.v.val_loc_list;
7649 }
7650
7651 static inline dw_loc_list_ref *
7652 AT_loc_list_ptr (dw_attr_ref a)
7653 {
7654   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7655   return &a->dw_attr_val.v.val_loc_list;
7656 }
7657
7658 /* Add an address constant attribute value to a DIE.  */
7659
7660 static inline void
7661 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7662 {
7663   dw_attr_node attr;
7664
7665   attr.dw_attr = attr_kind;
7666   attr.dw_attr_val.val_class = dw_val_class_addr;
7667   attr.dw_attr_val.v.val_addr = addr;
7668   add_dwarf_attr (die, &attr);
7669 }
7670
7671 /* Get the RTX from to an address DIE attribute.  */
7672
7673 static inline rtx
7674 AT_addr (dw_attr_ref a)
7675 {
7676   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7677   return a->dw_attr_val.v.val_addr;
7678 }
7679
7680 /* Add a file attribute value to a DIE.  */
7681
7682 static inline void
7683 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7684              struct dwarf_file_data *fd)
7685 {
7686   dw_attr_node attr;
7687
7688   attr.dw_attr = attr_kind;
7689   attr.dw_attr_val.val_class = dw_val_class_file;
7690   attr.dw_attr_val.v.val_file = fd;
7691   add_dwarf_attr (die, &attr);
7692 }
7693
7694 /* Get the dwarf_file_data from a file DIE attribute.  */
7695
7696 static inline struct dwarf_file_data *
7697 AT_file (dw_attr_ref a)
7698 {
7699   gcc_assert (a && AT_class (a) == dw_val_class_file);
7700   return a->dw_attr_val.v.val_file;
7701 }
7702
7703 /* Add a vms delta attribute value to a DIE.  */
7704
7705 static inline void
7706 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7707                   const char *lbl1, const char *lbl2)
7708 {
7709   dw_attr_node attr;
7710
7711   attr.dw_attr = attr_kind;
7712   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7713   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7714   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7715   add_dwarf_attr (die, &attr);
7716 }
7717
7718 /* Add a label identifier attribute value to a DIE.  */
7719
7720 static inline void
7721 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7722 {
7723   dw_attr_node attr;
7724
7725   attr.dw_attr = attr_kind;
7726   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7727   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7728   add_dwarf_attr (die, &attr);
7729 }
7730
7731 /* Add a section offset attribute value to a DIE, an offset into the
7732    debug_line section.  */
7733
7734 static inline void
7735 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7736                 const char *label)
7737 {
7738   dw_attr_node attr;
7739
7740   attr.dw_attr = attr_kind;
7741   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7742   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7743   add_dwarf_attr (die, &attr);
7744 }
7745
7746 /* Add a section offset attribute value to a DIE, an offset into the
7747    debug_macinfo section.  */
7748
7749 static inline void
7750 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7751                const char *label)
7752 {
7753   dw_attr_node attr;
7754
7755   attr.dw_attr = attr_kind;
7756   attr.dw_attr_val.val_class = dw_val_class_macptr;
7757   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7758   add_dwarf_attr (die, &attr);
7759 }
7760
7761 /* Add an offset attribute value to a DIE.  */
7762
7763 static inline void
7764 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7765                unsigned HOST_WIDE_INT offset)
7766 {
7767   dw_attr_node attr;
7768
7769   attr.dw_attr = attr_kind;
7770   attr.dw_attr_val.val_class = dw_val_class_offset;
7771   attr.dw_attr_val.v.val_offset = offset;
7772   add_dwarf_attr (die, &attr);
7773 }
7774
7775 /* Add an range_list attribute value to a DIE.  */
7776
7777 static void
7778 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7779                    long unsigned int offset)
7780 {
7781   dw_attr_node attr;
7782
7783   attr.dw_attr = attr_kind;
7784   attr.dw_attr_val.val_class = dw_val_class_range_list;
7785   attr.dw_attr_val.v.val_offset = offset;
7786   add_dwarf_attr (die, &attr);
7787 }
7788
7789 /* Return the start label of a delta attribute.  */
7790
7791 static inline const char *
7792 AT_vms_delta1 (dw_attr_ref a)
7793 {
7794   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7795   return a->dw_attr_val.v.val_vms_delta.lbl1;
7796 }
7797
7798 /* Return the end label of a delta attribute.  */
7799
7800 static inline const char *
7801 AT_vms_delta2 (dw_attr_ref a)
7802 {
7803   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7804   return a->dw_attr_val.v.val_vms_delta.lbl2;
7805 }
7806
7807 static inline const char *
7808 AT_lbl (dw_attr_ref a)
7809 {
7810   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7811                     || AT_class (a) == dw_val_class_lineptr
7812                     || AT_class (a) == dw_val_class_macptr));
7813   return a->dw_attr_val.v.val_lbl_id;
7814 }
7815
7816 /* Get the attribute of type attr_kind.  */
7817
7818 static dw_attr_ref
7819 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7820 {
7821   dw_attr_ref a;
7822   unsigned ix;
7823   dw_die_ref spec = NULL;
7824
7825   if (! die)
7826     return NULL;
7827
7828   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7829     if (a->dw_attr == attr_kind)
7830       return a;
7831     else if (a->dw_attr == DW_AT_specification
7832              || a->dw_attr == DW_AT_abstract_origin)
7833       spec = AT_ref (a);
7834
7835   if (spec)
7836     return get_AT (spec, attr_kind);
7837
7838   return NULL;
7839 }
7840
7841 /* Return the "low pc" attribute value, typically associated with a subprogram
7842    DIE.  Return null if the "low pc" attribute is either not present, or if it
7843    cannot be represented as an assembler label identifier.  */
7844
7845 static inline const char *
7846 get_AT_low_pc (dw_die_ref die)
7847 {
7848   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7849
7850   return a ? AT_lbl (a) : NULL;
7851 }
7852
7853 /* Return the "high pc" attribute value, typically associated with a subprogram
7854    DIE.  Return null if the "high pc" attribute is either not present, or if it
7855    cannot be represented as an assembler label identifier.  */
7856
7857 static inline const char *
7858 get_AT_hi_pc (dw_die_ref die)
7859 {
7860   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7861
7862   return a ? AT_lbl (a) : NULL;
7863 }
7864
7865 /* Return the value of the string attribute designated by ATTR_KIND, or
7866    NULL if it is not present.  */
7867
7868 static inline const char *
7869 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7870 {
7871   dw_attr_ref a = get_AT (die, attr_kind);
7872
7873   return a ? AT_string (a) : NULL;
7874 }
7875
7876 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7877    if it is not present.  */
7878
7879 static inline int
7880 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7881 {
7882   dw_attr_ref a = get_AT (die, attr_kind);
7883
7884   return a ? AT_flag (a) : 0;
7885 }
7886
7887 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7888    if it is not present.  */
7889
7890 static inline unsigned
7891 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7892 {
7893   dw_attr_ref a = get_AT (die, attr_kind);
7894
7895   return a ? AT_unsigned (a) : 0;
7896 }
7897
7898 static inline dw_die_ref
7899 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7900 {
7901   dw_attr_ref a = get_AT (die, attr_kind);
7902
7903   return a ? AT_ref (a) : NULL;
7904 }
7905
7906 static inline struct dwarf_file_data *
7907 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7908 {
7909   dw_attr_ref a = get_AT (die, attr_kind);
7910
7911   return a ? AT_file (a) : NULL;
7912 }
7913
7914 /* Return TRUE if the language is C++.  */
7915
7916 static inline bool
7917 is_cxx (void)
7918 {
7919   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7920
7921   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7922 }
7923
7924 /* Return TRUE if the language is Fortran.  */
7925
7926 static inline bool
7927 is_fortran (void)
7928 {
7929   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7930
7931   return (lang == DW_LANG_Fortran77
7932           || lang == DW_LANG_Fortran90
7933           || lang == DW_LANG_Fortran95);
7934 }
7935
7936 /* Return TRUE if the language is Ada.  */
7937
7938 static inline bool
7939 is_ada (void)
7940 {
7941   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7942
7943   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7944 }
7945
7946 /* Remove the specified attribute if present.  */
7947
7948 static void
7949 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7950 {
7951   dw_attr_ref a;
7952   unsigned ix;
7953
7954   if (! die)
7955     return;
7956
7957   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7958     if (a->dw_attr == attr_kind)
7959       {
7960         if (AT_class (a) == dw_val_class_str)
7961           if (a->dw_attr_val.v.val_str->refcount)
7962             a->dw_attr_val.v.val_str->refcount--;
7963
7964         /* VEC_ordered_remove should help reduce the number of abbrevs
7965            that are needed.  */
7966         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7967         return;
7968       }
7969 }
7970
7971 /* Remove CHILD from its parent.  PREV must have the property that
7972    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7973
7974 static void
7975 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7976 {
7977   gcc_assert (child->die_parent == prev->die_parent);
7978   gcc_assert (prev->die_sib == child);
7979   if (prev == child)
7980     {
7981       gcc_assert (child->die_parent->die_child == child);
7982       prev = NULL;
7983     }
7984   else
7985     prev->die_sib = child->die_sib;
7986   if (child->die_parent->die_child == child)
7987     child->die_parent->die_child = prev;
7988 }
7989
7990 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7991    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7992
7993 static void
7994 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7995 {
7996   dw_die_ref parent = old_child->die_parent;
7997
7998   gcc_assert (parent == prev->die_parent);
7999   gcc_assert (prev->die_sib == old_child);
8000
8001   new_child->die_parent = parent;
8002   if (prev == old_child)
8003     {
8004       gcc_assert (parent->die_child == old_child);
8005       new_child->die_sib = new_child;
8006     }
8007   else
8008     {
8009       prev->die_sib = new_child;
8010       new_child->die_sib = old_child->die_sib;
8011     }
8012   if (old_child->die_parent->die_child == old_child)
8013     old_child->die_parent->die_child = new_child;
8014 }
8015
8016 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8017
8018 static void
8019 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8020 {
8021   dw_die_ref c;
8022   new_parent->die_child = old_parent->die_child;
8023   old_parent->die_child = NULL;
8024   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8025 }
8026
8027 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8028    matches TAG.  */
8029
8030 static void
8031 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8032 {
8033   dw_die_ref c;
8034
8035   c = die->die_child;
8036   if (c) do {
8037     dw_die_ref prev = c;
8038     c = c->die_sib;
8039     while (c->die_tag == tag)
8040       {
8041         remove_child_with_prev (c, prev);
8042         /* Might have removed every child.  */
8043         if (c == c->die_sib)
8044           return;
8045         c = c->die_sib;
8046       }
8047   } while (c != die->die_child);
8048 }
8049
8050 /* Add a CHILD_DIE as the last child of DIE.  */
8051
8052 static void
8053 add_child_die (dw_die_ref die, dw_die_ref child_die)
8054 {
8055   /* FIXME this should probably be an assert.  */
8056   if (! die || ! child_die)
8057     return;
8058   gcc_assert (die != child_die);
8059
8060   child_die->die_parent = die;
8061   if (die->die_child)
8062     {
8063       child_die->die_sib = die->die_child->die_sib;
8064       die->die_child->die_sib = child_die;
8065     }
8066   else
8067     child_die->die_sib = child_die;
8068   die->die_child = child_die;
8069 }
8070
8071 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8072    is the specification, to the end of PARENT's list of children.
8073    This is done by removing and re-adding it.  */
8074
8075 static void
8076 splice_child_die (dw_die_ref parent, dw_die_ref child)
8077 {
8078   dw_die_ref p;
8079
8080   /* We want the declaration DIE from inside the class, not the
8081      specification DIE at toplevel.  */
8082   if (child->die_parent != parent)
8083     {
8084       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8085
8086       if (tmp)
8087         child = tmp;
8088     }
8089
8090   gcc_assert (child->die_parent == parent
8091               || (child->die_parent
8092                   == get_AT_ref (parent, DW_AT_specification)));
8093
8094   for (p = child->die_parent->die_child; ; p = p->die_sib)
8095     if (p->die_sib == child)
8096       {
8097         remove_child_with_prev (child, p);
8098         break;
8099       }
8100
8101   add_child_die (parent, child);
8102 }
8103
8104 /* Return a pointer to a newly created DIE node.  */
8105
8106 static inline dw_die_ref
8107 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8108 {
8109   dw_die_ref die = ggc_alloc_cleared_die_node ();
8110
8111   die->die_tag = tag_value;
8112
8113   if (parent_die != NULL)
8114     add_child_die (parent_die, die);
8115   else
8116     {
8117       limbo_die_node *limbo_node;
8118
8119       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8120       limbo_node->die = die;
8121       limbo_node->created_for = t;
8122       limbo_node->next = limbo_die_list;
8123       limbo_die_list = limbo_node;
8124     }
8125
8126   return die;
8127 }
8128
8129 /* Return the DIE associated with the given type specifier.  */
8130
8131 static inline dw_die_ref
8132 lookup_type_die (tree type)
8133 {
8134   return TYPE_SYMTAB_DIE (type);
8135 }
8136
8137 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8138    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8139    anonymous type instead the one of the naming typedef.  */
8140
8141 static inline dw_die_ref
8142 strip_naming_typedef (tree type, dw_die_ref type_die)
8143 {
8144   if (type
8145       && TREE_CODE (type) == RECORD_TYPE
8146       && type_die
8147       && type_die->die_tag == DW_TAG_typedef
8148       && is_naming_typedef_decl (TYPE_NAME (type)))
8149     type_die = get_AT_ref (type_die, DW_AT_type);
8150   return type_die;
8151 }
8152
8153 /* Like lookup_type_die, but if type is an anonymous type named by a
8154    typedef[1], return the DIE of the anonymous type instead the one of
8155    the naming typedef.  This is because in gen_typedef_die, we did
8156    equate the anonymous struct named by the typedef with the DIE of
8157    the naming typedef. So by default, lookup_type_die on an anonymous
8158    struct yields the DIE of the naming typedef.
8159
8160    [1]: Read the comment of is_naming_typedef_decl to learn about what
8161    a naming typedef is.  */
8162
8163 static inline dw_die_ref
8164 lookup_type_die_strip_naming_typedef (tree type)
8165 {
8166   dw_die_ref die = lookup_type_die (type);
8167   return strip_naming_typedef (type, die);
8168 }
8169
8170 /* Equate a DIE to a given type specifier.  */
8171
8172 static inline void
8173 equate_type_number_to_die (tree type, dw_die_ref type_die)
8174 {
8175   TYPE_SYMTAB_DIE (type) = type_die;
8176 }
8177
8178 /* Returns a hash value for X (which really is a die_struct).  */
8179
8180 static hashval_t
8181 decl_die_table_hash (const void *x)
8182 {
8183   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8184 }
8185
8186 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8187
8188 static int
8189 decl_die_table_eq (const void *x, const void *y)
8190 {
8191   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8192 }
8193
8194 /* Return the DIE associated with a given declaration.  */
8195
8196 static inline dw_die_ref
8197 lookup_decl_die (tree decl)
8198 {
8199   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8200 }
8201
8202 /* Returns a hash value for X (which really is a var_loc_list).  */
8203
8204 static hashval_t
8205 decl_loc_table_hash (const void *x)
8206 {
8207   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8208 }
8209
8210 /* Return nonzero if decl_id of var_loc_list X is the same as
8211    UID of decl *Y.  */
8212
8213 static int
8214 decl_loc_table_eq (const void *x, const void *y)
8215 {
8216   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8217 }
8218
8219 /* Return the var_loc list associated with a given declaration.  */
8220
8221 static inline var_loc_list *
8222 lookup_decl_loc (const_tree decl)
8223 {
8224   if (!decl_loc_table)
8225     return NULL;
8226   return (var_loc_list *)
8227     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8228 }
8229
8230 /* Equate a DIE to a particular declaration.  */
8231
8232 static void
8233 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8234 {
8235   unsigned int decl_id = DECL_UID (decl);
8236   void **slot;
8237
8238   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8239   *slot = decl_die;
8240   decl_die->decl_id = decl_id;
8241 }
8242
8243 /* Return how many bits covers PIECE EXPR_LIST.  */
8244
8245 static int
8246 decl_piece_bitsize (rtx piece)
8247 {
8248   int ret = (int) GET_MODE (piece);
8249   if (ret)
8250     return ret;
8251   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8252               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8253   return INTVAL (XEXP (XEXP (piece, 0), 0));
8254 }
8255
8256 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8257
8258 static rtx *
8259 decl_piece_varloc_ptr (rtx piece)
8260 {
8261   if ((int) GET_MODE (piece))
8262     return &XEXP (piece, 0);
8263   else
8264     return &XEXP (XEXP (piece, 0), 1);
8265 }
8266
8267 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8268    Next is the chain of following piece nodes.  */
8269
8270 static rtx
8271 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8272 {
8273   if (bitsize <= (int) MAX_MACHINE_MODE)
8274     return alloc_EXPR_LIST (bitsize, loc_note, next);
8275   else
8276     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8277                                                GEN_INT (bitsize),
8278                                                loc_note), next);
8279 }
8280
8281 /* Return rtx that should be stored into loc field for
8282    LOC_NOTE and BITPOS/BITSIZE.  */
8283
8284 static rtx
8285 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8286                       HOST_WIDE_INT bitsize)
8287 {
8288   if (bitsize != -1)
8289     {
8290       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8291       if (bitpos != 0)
8292         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8293     }
8294   return loc_note;
8295 }
8296
8297 /* This function either modifies location piece list *DEST in
8298    place (if SRC and INNER is NULL), or copies location piece list
8299    *SRC to *DEST while modifying it.  Location BITPOS is modified
8300    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8301    not copied and if needed some padding around it is added.
8302    When modifying in place, DEST should point to EXPR_LIST where
8303    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8304    to the start of the whole list and INNER points to the EXPR_LIST
8305    where earlier pieces cover PIECE_BITPOS bits.  */
8306
8307 static void
8308 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8309                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8310                    HOST_WIDE_INT bitsize, rtx loc_note)
8311 {
8312   int diff;
8313   bool copy = inner != NULL;
8314
8315   if (copy)
8316     {
8317       /* First copy all nodes preceeding the current bitpos.  */
8318       while (src != inner)
8319         {
8320           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8321                                    decl_piece_bitsize (*src), NULL_RTX);
8322           dest = &XEXP (*dest, 1);
8323           src = &XEXP (*src, 1);
8324         }
8325     }
8326   /* Add padding if needed.  */
8327   if (bitpos != piece_bitpos)
8328     {
8329       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8330                                copy ? NULL_RTX : *dest);
8331       dest = &XEXP (*dest, 1);
8332     }
8333   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8334     {
8335       gcc_assert (!copy);
8336       /* A piece with correct bitpos and bitsize already exist,
8337          just update the location for it and return.  */
8338       *decl_piece_varloc_ptr (*dest) = loc_note;
8339       return;
8340     }
8341   /* Add the piece that changed.  */
8342   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8343   dest = &XEXP (*dest, 1);
8344   /* Skip over pieces that overlap it.  */
8345   diff = bitpos - piece_bitpos + bitsize;
8346   if (!copy)
8347     src = dest;
8348   while (diff > 0 && *src)
8349     {
8350       rtx piece = *src;
8351       diff -= decl_piece_bitsize (piece);
8352       if (copy)
8353         src = &XEXP (piece, 1);
8354       else
8355         {
8356           *src = XEXP (piece, 1);
8357           free_EXPR_LIST_node (piece);
8358         }
8359     }
8360   /* Add padding if needed.  */
8361   if (diff < 0 && *src)
8362     {
8363       if (!copy)
8364         dest = src;
8365       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8366       dest = &XEXP (*dest, 1);
8367     }
8368   if (!copy)
8369     return;
8370   /* Finally copy all nodes following it.  */
8371   while (*src)
8372     {
8373       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8374                                decl_piece_bitsize (*src), NULL_RTX);
8375       dest = &XEXP (*dest, 1);
8376       src = &XEXP (*src, 1);
8377     }
8378 }
8379
8380 /* Add a variable location node to the linked list for DECL.  */
8381
8382 static struct var_loc_node *
8383 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8384 {
8385   unsigned int decl_id;
8386   var_loc_list *temp;
8387   void **slot;
8388   struct var_loc_node *loc = NULL;
8389   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8390
8391   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8392     {
8393       tree realdecl = DECL_DEBUG_EXPR (decl);
8394       if (realdecl && handled_component_p (realdecl))
8395         {
8396           HOST_WIDE_INT maxsize;
8397           tree innerdecl;
8398           innerdecl
8399             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8400           if (!DECL_P (innerdecl)
8401               || DECL_IGNORED_P (innerdecl)
8402               || TREE_STATIC (innerdecl)
8403               || bitsize <= 0
8404               || bitpos + bitsize > 256
8405               || bitsize != maxsize)
8406             return NULL;
8407           decl = innerdecl;
8408         }
8409     }
8410
8411   decl_id = DECL_UID (decl);
8412   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8413   if (*slot == NULL)
8414     {
8415       temp = ggc_alloc_cleared_var_loc_list ();
8416       temp->decl_id = decl_id;
8417       *slot = temp;
8418     }
8419   else
8420     temp = (var_loc_list *) *slot;
8421
8422   if (temp->last)
8423     {
8424       struct var_loc_node *last = temp->last, *unused = NULL;
8425       rtx *piece_loc = NULL, last_loc_note;
8426       int piece_bitpos = 0;
8427       if (last->next)
8428         {
8429           last = last->next;
8430           gcc_assert (last->next == NULL);
8431         }
8432       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8433         {
8434           piece_loc = &last->loc;
8435           do
8436             {
8437               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8438               if (piece_bitpos + cur_bitsize > bitpos)
8439                 break;
8440               piece_bitpos += cur_bitsize;
8441               piece_loc = &XEXP (*piece_loc, 1);
8442             }
8443           while (*piece_loc);
8444         }
8445       /* TEMP->LAST here is either pointer to the last but one or
8446          last element in the chained list, LAST is pointer to the
8447          last element.  */
8448       if (label && strcmp (last->label, label) == 0)
8449         {
8450           /* For SRA optimized variables if there weren't any real
8451              insns since last note, just modify the last node.  */
8452           if (piece_loc != NULL)
8453             {
8454               adjust_piece_list (piece_loc, NULL, NULL,
8455                                  bitpos, piece_bitpos, bitsize, loc_note);
8456               return NULL;
8457             }
8458           /* If the last note doesn't cover any instructions, remove it.  */
8459           if (temp->last != last)
8460             {
8461               temp->last->next = NULL;
8462               unused = last;
8463               last = temp->last;
8464               gcc_assert (strcmp (last->label, label) != 0);
8465             }
8466           else
8467             {
8468               gcc_assert (temp->first == temp->last);
8469               memset (temp->last, '\0', sizeof (*temp->last));
8470               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8471               return temp->last;
8472             }
8473         }
8474       if (bitsize == -1 && NOTE_P (last->loc))
8475         last_loc_note = last->loc;
8476       else if (piece_loc != NULL
8477                && *piece_loc != NULL_RTX
8478                && piece_bitpos == bitpos
8479                && decl_piece_bitsize (*piece_loc) == bitsize)
8480         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8481       else
8482         last_loc_note = NULL_RTX;
8483       /* If the current location is the same as the end of the list,
8484          and either both or neither of the locations is uninitialized,
8485          we have nothing to do.  */
8486       if (last_loc_note == NULL_RTX
8487           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8488                             NOTE_VAR_LOCATION_LOC (loc_note)))
8489           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8490                != NOTE_VAR_LOCATION_STATUS (loc_note))
8491               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8492                    == VAR_INIT_STATUS_UNINITIALIZED)
8493                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8494                       == VAR_INIT_STATUS_UNINITIALIZED))))
8495         {
8496           /* Add LOC to the end of list and update LAST.  If the last
8497              element of the list has been removed above, reuse its
8498              memory for the new node, otherwise allocate a new one.  */
8499           if (unused)
8500             {
8501               loc = unused;
8502               memset (loc, '\0', sizeof (*loc));
8503             }
8504           else
8505             loc = ggc_alloc_cleared_var_loc_node ();
8506           if (bitsize == -1 || piece_loc == NULL)
8507             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8508           else
8509             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8510                                bitpos, piece_bitpos, bitsize, loc_note);
8511           last->next = loc;
8512           /* Ensure TEMP->LAST will point either to the new last but one
8513              element of the chain, or to the last element in it.  */
8514           if (last != temp->last)
8515             temp->last = last;
8516         }
8517       else if (unused)
8518         ggc_free (unused);
8519     }
8520   else
8521     {
8522       loc = ggc_alloc_cleared_var_loc_node ();
8523       temp->first = loc;
8524       temp->last = loc;
8525       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8526     }
8527   return loc;
8528 }
8529 \f
8530 /* Keep track of the number of spaces used to indent the
8531    output of the debugging routines that print the structure of
8532    the DIE internal representation.  */
8533 static int print_indent;
8534
8535 /* Indent the line the number of spaces given by print_indent.  */
8536
8537 static inline void
8538 print_spaces (FILE *outfile)
8539 {
8540   fprintf (outfile, "%*s", print_indent, "");
8541 }
8542
8543 /* Print a type signature in hex.  */
8544
8545 static inline void
8546 print_signature (FILE *outfile, char *sig)
8547 {
8548   int i;
8549
8550   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8551     fprintf (outfile, "%02x", sig[i] & 0xff);
8552 }
8553
8554 /* Print the information associated with a given DIE, and its children.
8555    This routine is a debugging aid only.  */
8556
8557 static void
8558 print_die (dw_die_ref die, FILE *outfile)
8559 {
8560   dw_attr_ref a;
8561   dw_die_ref c;
8562   unsigned ix;
8563
8564   print_spaces (outfile);
8565   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8566            die->die_offset, dwarf_tag_name (die->die_tag),
8567            (void*) die);
8568   print_spaces (outfile);
8569   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8570   fprintf (outfile, " offset: %ld", die->die_offset);
8571   fprintf (outfile, " mark: %d\n", die->die_mark);
8572
8573   if (dwarf_version >= 4 && die->die_id.die_type_node)
8574     {
8575       print_spaces (outfile);
8576       fprintf (outfile, "  signature: ");
8577       print_signature (outfile, die->die_id.die_type_node->signature);
8578       fprintf (outfile, "\n");
8579     }
8580
8581   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8582     {
8583       print_spaces (outfile);
8584       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8585
8586       switch (AT_class (a))
8587         {
8588         case dw_val_class_addr:
8589           fprintf (outfile, "address");
8590           break;
8591         case dw_val_class_offset:
8592           fprintf (outfile, "offset");
8593           break;
8594         case dw_val_class_loc:
8595           fprintf (outfile, "location descriptor");
8596           break;
8597         case dw_val_class_loc_list:
8598           fprintf (outfile, "location list -> label:%s",
8599                    AT_loc_list (a)->ll_symbol);
8600           break;
8601         case dw_val_class_range_list:
8602           fprintf (outfile, "range list");
8603           break;
8604         case dw_val_class_const:
8605           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8606           break;
8607         case dw_val_class_unsigned_const:
8608           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8609           break;
8610         case dw_val_class_const_double:
8611           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8612                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8613                    a->dw_attr_val.v.val_double.high,
8614                    a->dw_attr_val.v.val_double.low);
8615           break;
8616         case dw_val_class_vec:
8617           fprintf (outfile, "floating-point or vector constant");
8618           break;
8619         case dw_val_class_flag:
8620           fprintf (outfile, "%u", AT_flag (a));
8621           break;
8622         case dw_val_class_die_ref:
8623           if (AT_ref (a) != NULL)
8624             {
8625               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8626                 {
8627                   fprintf (outfile, "die -> signature: ");
8628                   print_signature (outfile,
8629                                    AT_ref (a)->die_id.die_type_node->signature);
8630                 }
8631               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8632                 fprintf (outfile, "die -> label: %s",
8633                          AT_ref (a)->die_id.die_symbol);
8634               else
8635                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8636               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8637             }
8638           else
8639             fprintf (outfile, "die -> <null>");
8640           break;
8641         case dw_val_class_vms_delta:
8642           fprintf (outfile, "delta: @slotcount(%s-%s)",
8643                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8644           break;
8645         case dw_val_class_lbl_id:
8646         case dw_val_class_lineptr:
8647         case dw_val_class_macptr:
8648           fprintf (outfile, "label: %s", AT_lbl (a));
8649           break;
8650         case dw_val_class_str:
8651           if (AT_string (a) != NULL)
8652             fprintf (outfile, "\"%s\"", AT_string (a));
8653           else
8654             fprintf (outfile, "<null>");
8655           break;
8656         case dw_val_class_file:
8657           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8658                    AT_file (a)->emitted_number);
8659           break;
8660         case dw_val_class_data8:
8661           {
8662             int i;
8663
8664             for (i = 0; i < 8; i++)
8665               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8666             break;
8667           }
8668         default:
8669           break;
8670         }
8671
8672       fprintf (outfile, "\n");
8673     }
8674
8675   if (die->die_child != NULL)
8676     {
8677       print_indent += 4;
8678       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8679       print_indent -= 4;
8680     }
8681   if (print_indent == 0)
8682     fprintf (outfile, "\n");
8683 }
8684
8685 /* Print the contents of the source code line number correspondence table.
8686    This routine is a debugging aid only.  */
8687
8688 static void
8689 print_dwarf_line_table (FILE *outfile)
8690 {
8691   unsigned i;
8692   dw_line_info_ref line_info;
8693
8694   fprintf (outfile, "\n\nDWARF source line information\n");
8695   for (i = 1; i < line_info_table_in_use; i++)
8696     {
8697       line_info = &line_info_table[i];
8698       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8699                line_info->dw_file_num,
8700                line_info->dw_line_num);
8701     }
8702
8703   fprintf (outfile, "\n\n");
8704 }
8705
8706 /* Print the information collected for a given DIE.  */
8707
8708 DEBUG_FUNCTION void
8709 debug_dwarf_die (dw_die_ref die)
8710 {
8711   print_die (die, stderr);
8712 }
8713
8714 /* Print all DWARF information collected for the compilation unit.
8715    This routine is a debugging aid only.  */
8716
8717 DEBUG_FUNCTION void
8718 debug_dwarf (void)
8719 {
8720   print_indent = 0;
8721   print_die (comp_unit_die (), stderr);
8722   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8723     print_dwarf_line_table (stderr);
8724 }
8725 \f
8726 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8727    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8728    DIE that marks the start of the DIEs for this include file.  */
8729
8730 static dw_die_ref
8731 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8732 {
8733   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8734   dw_die_ref new_unit = gen_compile_unit_die (filename);
8735
8736   new_unit->die_sib = old_unit;
8737   return new_unit;
8738 }
8739
8740 /* Close an include-file CU and reopen the enclosing one.  */
8741
8742 static dw_die_ref
8743 pop_compile_unit (dw_die_ref old_unit)
8744 {
8745   dw_die_ref new_unit = old_unit->die_sib;
8746
8747   old_unit->die_sib = NULL;
8748   return new_unit;
8749 }
8750
8751 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8752 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8753
8754 /* Calculate the checksum of a location expression.  */
8755
8756 static inline void
8757 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8758 {
8759   int tem;
8760
8761   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8762   CHECKSUM (tem);
8763   CHECKSUM (loc->dw_loc_oprnd1);
8764   CHECKSUM (loc->dw_loc_oprnd2);
8765 }
8766
8767 /* Calculate the checksum of an attribute.  */
8768
8769 static void
8770 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8771 {
8772   dw_loc_descr_ref loc;
8773   rtx r;
8774
8775   CHECKSUM (at->dw_attr);
8776
8777   /* We don't care that this was compiled with a different compiler
8778      snapshot; if the output is the same, that's what matters.  */
8779   if (at->dw_attr == DW_AT_producer)
8780     return;
8781
8782   switch (AT_class (at))
8783     {
8784     case dw_val_class_const:
8785       CHECKSUM (at->dw_attr_val.v.val_int);
8786       break;
8787     case dw_val_class_unsigned_const:
8788       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8789       break;
8790     case dw_val_class_const_double:
8791       CHECKSUM (at->dw_attr_val.v.val_double);
8792       break;
8793     case dw_val_class_vec:
8794       CHECKSUM (at->dw_attr_val.v.val_vec);
8795       break;
8796     case dw_val_class_flag:
8797       CHECKSUM (at->dw_attr_val.v.val_flag);
8798       break;
8799     case dw_val_class_str:
8800       CHECKSUM_STRING (AT_string (at));
8801       break;
8802
8803     case dw_val_class_addr:
8804       r = AT_addr (at);
8805       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8806       CHECKSUM_STRING (XSTR (r, 0));
8807       break;
8808
8809     case dw_val_class_offset:
8810       CHECKSUM (at->dw_attr_val.v.val_offset);
8811       break;
8812
8813     case dw_val_class_loc:
8814       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8815         loc_checksum (loc, ctx);
8816       break;
8817
8818     case dw_val_class_die_ref:
8819       die_checksum (AT_ref (at), ctx, mark);
8820       break;
8821
8822     case dw_val_class_fde_ref:
8823     case dw_val_class_vms_delta:
8824     case dw_val_class_lbl_id:
8825     case dw_val_class_lineptr:
8826     case dw_val_class_macptr:
8827       break;
8828
8829     case dw_val_class_file:
8830       CHECKSUM_STRING (AT_file (at)->filename);
8831       break;
8832
8833     case dw_val_class_data8:
8834       CHECKSUM (at->dw_attr_val.v.val_data8);
8835       break;
8836
8837     default:
8838       break;
8839     }
8840 }
8841
8842 /* Calculate the checksum of a DIE.  */
8843
8844 static void
8845 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8846 {
8847   dw_die_ref c;
8848   dw_attr_ref a;
8849   unsigned ix;
8850
8851   /* To avoid infinite recursion.  */
8852   if (die->die_mark)
8853     {
8854       CHECKSUM (die->die_mark);
8855       return;
8856     }
8857   die->die_mark = ++(*mark);
8858
8859   CHECKSUM (die->die_tag);
8860
8861   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8862     attr_checksum (a, ctx, mark);
8863
8864   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8865 }
8866
8867 #undef CHECKSUM
8868 #undef CHECKSUM_STRING
8869
8870 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8871 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8872 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8873 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8874 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8875 #define CHECKSUM_ATTR(FOO) \
8876   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8877
8878 /* Calculate the checksum of a number in signed LEB128 format.  */
8879
8880 static void
8881 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8882 {
8883   unsigned char byte;
8884   bool more;
8885
8886   while (1)
8887     {
8888       byte = (value & 0x7f);
8889       value >>= 7;
8890       more = !((value == 0 && (byte & 0x40) == 0)
8891                 || (value == -1 && (byte & 0x40) != 0));
8892       if (more)
8893         byte |= 0x80;
8894       CHECKSUM (byte);
8895       if (!more)
8896         break;
8897     }
8898 }
8899
8900 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8901
8902 static void
8903 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8904 {
8905   while (1)
8906     {
8907       unsigned char byte = (value & 0x7f);
8908       value >>= 7;
8909       if (value != 0)
8910         /* More bytes to follow.  */
8911         byte |= 0x80;
8912       CHECKSUM (byte);
8913       if (value == 0)
8914         break;
8915     }
8916 }
8917
8918 /* Checksum the context of the DIE.  This adds the names of any
8919    surrounding namespaces or structures to the checksum.  */
8920
8921 static void
8922 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8923 {
8924   const char *name;
8925   dw_die_ref spec;
8926   int tag = die->die_tag;
8927
8928   if (tag != DW_TAG_namespace
8929       && tag != DW_TAG_structure_type
8930       && tag != DW_TAG_class_type)
8931     return;
8932
8933   name = get_AT_string (die, DW_AT_name);
8934
8935   spec = get_AT_ref (die, DW_AT_specification);
8936   if (spec != NULL)
8937     die = spec;
8938
8939   if (die->die_parent != NULL)
8940     checksum_die_context (die->die_parent, ctx);
8941
8942   CHECKSUM_ULEB128 ('C');
8943   CHECKSUM_ULEB128 (tag);
8944   if (name != NULL)
8945     CHECKSUM_STRING (name);
8946 }
8947
8948 /* Calculate the checksum of a location expression.  */
8949
8950 static inline void
8951 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8952 {
8953   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8954      were emitted as a DW_FORM_sdata instead of a location expression.  */
8955   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8956     {
8957       CHECKSUM_ULEB128 (DW_FORM_sdata);
8958       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8959       return;
8960     }
8961
8962   /* Otherwise, just checksum the raw location expression.  */
8963   while (loc != NULL)
8964     {
8965       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8966       CHECKSUM (loc->dw_loc_oprnd1);
8967       CHECKSUM (loc->dw_loc_oprnd2);
8968       loc = loc->dw_loc_next;
8969     }
8970 }
8971
8972 /* Calculate the checksum of an attribute.  */
8973
8974 static void
8975 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8976                        struct md5_ctx *ctx, int *mark)
8977 {
8978   dw_loc_descr_ref loc;
8979   rtx r;
8980
8981   if (AT_class (at) == dw_val_class_die_ref)
8982     {
8983       dw_die_ref target_die = AT_ref (at);
8984
8985       /* For pointer and reference types, we checksum only the (qualified)
8986          name of the target type (if there is a name).  For friend entries,
8987          we checksum only the (qualified) name of the target type or function.
8988          This allows the checksum to remain the same whether the target type
8989          is complete or not.  */
8990       if ((at->dw_attr == DW_AT_type
8991            && (tag == DW_TAG_pointer_type
8992                || tag == DW_TAG_reference_type
8993                || tag == DW_TAG_rvalue_reference_type
8994                || tag == DW_TAG_ptr_to_member_type))
8995           || (at->dw_attr == DW_AT_friend
8996               && tag == DW_TAG_friend))
8997         {
8998           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8999
9000           if (name_attr != NULL)
9001             {
9002               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9003
9004               if (decl == NULL)
9005                 decl = target_die;
9006               CHECKSUM_ULEB128 ('N');
9007               CHECKSUM_ULEB128 (at->dw_attr);
9008               if (decl->die_parent != NULL)
9009                 checksum_die_context (decl->die_parent, ctx);
9010               CHECKSUM_ULEB128 ('E');
9011               CHECKSUM_STRING (AT_string (name_attr));
9012               return;
9013             }
9014         }
9015
9016       /* For all other references to another DIE, we check to see if the
9017          target DIE has already been visited.  If it has, we emit a
9018          backward reference; if not, we descend recursively.  */
9019       if (target_die->die_mark > 0)
9020         {
9021           CHECKSUM_ULEB128 ('R');
9022           CHECKSUM_ULEB128 (at->dw_attr);
9023           CHECKSUM_ULEB128 (target_die->die_mark);
9024         }
9025       else
9026         {
9027           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9028
9029           if (decl == NULL)
9030             decl = target_die;
9031           target_die->die_mark = ++(*mark);
9032           CHECKSUM_ULEB128 ('T');
9033           CHECKSUM_ULEB128 (at->dw_attr);
9034           if (decl->die_parent != NULL)
9035             checksum_die_context (decl->die_parent, ctx);
9036           die_checksum_ordered (target_die, ctx, mark);
9037         }
9038       return;
9039     }
9040
9041   CHECKSUM_ULEB128 ('A');
9042   CHECKSUM_ULEB128 (at->dw_attr);
9043
9044   switch (AT_class (at))
9045     {
9046     case dw_val_class_const:
9047       CHECKSUM_ULEB128 (DW_FORM_sdata);
9048       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9049       break;
9050
9051     case dw_val_class_unsigned_const:
9052       CHECKSUM_ULEB128 (DW_FORM_sdata);
9053       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9054       break;
9055
9056     case dw_val_class_const_double:
9057       CHECKSUM_ULEB128 (DW_FORM_block);
9058       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9059       CHECKSUM (at->dw_attr_val.v.val_double);
9060       break;
9061
9062     case dw_val_class_vec:
9063       CHECKSUM_ULEB128 (DW_FORM_block);
9064       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9065       CHECKSUM (at->dw_attr_val.v.val_vec);
9066       break;
9067
9068     case dw_val_class_flag:
9069       CHECKSUM_ULEB128 (DW_FORM_flag);
9070       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9071       break;
9072
9073     case dw_val_class_str:
9074       CHECKSUM_ULEB128 (DW_FORM_string);
9075       CHECKSUM_STRING (AT_string (at));
9076       break;
9077
9078     case dw_val_class_addr:
9079       r = AT_addr (at);
9080       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9081       CHECKSUM_ULEB128 (DW_FORM_string);
9082       CHECKSUM_STRING (XSTR (r, 0));
9083       break;
9084
9085     case dw_val_class_offset:
9086       CHECKSUM_ULEB128 (DW_FORM_sdata);
9087       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9088       break;
9089
9090     case dw_val_class_loc:
9091       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9092         loc_checksum_ordered (loc, ctx);
9093       break;
9094
9095     case dw_val_class_fde_ref:
9096     case dw_val_class_lbl_id:
9097     case dw_val_class_lineptr:
9098     case dw_val_class_macptr:
9099       break;
9100
9101     case dw_val_class_file:
9102       CHECKSUM_ULEB128 (DW_FORM_string);
9103       CHECKSUM_STRING (AT_file (at)->filename);
9104       break;
9105
9106     case dw_val_class_data8:
9107       CHECKSUM (at->dw_attr_val.v.val_data8);
9108       break;
9109
9110     default:
9111       break;
9112     }
9113 }
9114
9115 struct checksum_attributes
9116 {
9117   dw_attr_ref at_name;
9118   dw_attr_ref at_type;
9119   dw_attr_ref at_friend;
9120   dw_attr_ref at_accessibility;
9121   dw_attr_ref at_address_class;
9122   dw_attr_ref at_allocated;
9123   dw_attr_ref at_artificial;
9124   dw_attr_ref at_associated;
9125   dw_attr_ref at_binary_scale;
9126   dw_attr_ref at_bit_offset;
9127   dw_attr_ref at_bit_size;
9128   dw_attr_ref at_bit_stride;
9129   dw_attr_ref at_byte_size;
9130   dw_attr_ref at_byte_stride;
9131   dw_attr_ref at_const_value;
9132   dw_attr_ref at_containing_type;
9133   dw_attr_ref at_count;
9134   dw_attr_ref at_data_location;
9135   dw_attr_ref at_data_member_location;
9136   dw_attr_ref at_decimal_scale;
9137   dw_attr_ref at_decimal_sign;
9138   dw_attr_ref at_default_value;
9139   dw_attr_ref at_digit_count;
9140   dw_attr_ref at_discr;
9141   dw_attr_ref at_discr_list;
9142   dw_attr_ref at_discr_value;
9143   dw_attr_ref at_encoding;
9144   dw_attr_ref at_endianity;
9145   dw_attr_ref at_explicit;
9146   dw_attr_ref at_is_optional;
9147   dw_attr_ref at_location;
9148   dw_attr_ref at_lower_bound;
9149   dw_attr_ref at_mutable;
9150   dw_attr_ref at_ordering;
9151   dw_attr_ref at_picture_string;
9152   dw_attr_ref at_prototyped;
9153   dw_attr_ref at_small;
9154   dw_attr_ref at_segment;
9155   dw_attr_ref at_string_length;
9156   dw_attr_ref at_threads_scaled;
9157   dw_attr_ref at_upper_bound;
9158   dw_attr_ref at_use_location;
9159   dw_attr_ref at_use_UTF8;
9160   dw_attr_ref at_variable_parameter;
9161   dw_attr_ref at_virtuality;
9162   dw_attr_ref at_visibility;
9163   dw_attr_ref at_vtable_elem_location;
9164 };
9165
9166 /* Collect the attributes that we will want to use for the checksum.  */
9167
9168 static void
9169 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9170 {
9171   dw_attr_ref a;
9172   unsigned ix;
9173
9174   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9175     {
9176       switch (a->dw_attr)
9177         {
9178         case DW_AT_name:
9179           attrs->at_name = a;
9180           break;
9181         case DW_AT_type:
9182           attrs->at_type = a;
9183           break;
9184         case DW_AT_friend:
9185           attrs->at_friend = a;
9186           break;
9187         case DW_AT_accessibility:
9188           attrs->at_accessibility = a;
9189           break;
9190         case DW_AT_address_class:
9191           attrs->at_address_class = a;
9192           break;
9193         case DW_AT_allocated:
9194           attrs->at_allocated = a;
9195           break;
9196         case DW_AT_artificial:
9197           attrs->at_artificial = a;
9198           break;
9199         case DW_AT_associated:
9200           attrs->at_associated = a;
9201           break;
9202         case DW_AT_binary_scale:
9203           attrs->at_binary_scale = a;
9204           break;
9205         case DW_AT_bit_offset:
9206           attrs->at_bit_offset = a;
9207           break;
9208         case DW_AT_bit_size:
9209           attrs->at_bit_size = a;
9210           break;
9211         case DW_AT_bit_stride:
9212           attrs->at_bit_stride = a;
9213           break;
9214         case DW_AT_byte_size:
9215           attrs->at_byte_size = a;
9216           break;
9217         case DW_AT_byte_stride:
9218           attrs->at_byte_stride = a;
9219           break;
9220         case DW_AT_const_value:
9221           attrs->at_const_value = a;
9222           break;
9223         case DW_AT_containing_type:
9224           attrs->at_containing_type = a;
9225           break;
9226         case DW_AT_count:
9227           attrs->at_count = a;
9228           break;
9229         case DW_AT_data_location:
9230           attrs->at_data_location = a;
9231           break;
9232         case DW_AT_data_member_location:
9233           attrs->at_data_member_location = a;
9234           break;
9235         case DW_AT_decimal_scale:
9236           attrs->at_decimal_scale = a;
9237           break;
9238         case DW_AT_decimal_sign:
9239           attrs->at_decimal_sign = a;
9240           break;
9241         case DW_AT_default_value:
9242           attrs->at_default_value = a;
9243           break;
9244         case DW_AT_digit_count:
9245           attrs->at_digit_count = a;
9246           break;
9247         case DW_AT_discr:
9248           attrs->at_discr = a;
9249           break;
9250         case DW_AT_discr_list:
9251           attrs->at_discr_list = a;
9252           break;
9253         case DW_AT_discr_value:
9254           attrs->at_discr_value = a;
9255           break;
9256         case DW_AT_encoding:
9257           attrs->at_encoding = a;
9258           break;
9259         case DW_AT_endianity:
9260           attrs->at_endianity = a;
9261           break;
9262         case DW_AT_explicit:
9263           attrs->at_explicit = a;
9264           break;
9265         case DW_AT_is_optional:
9266           attrs->at_is_optional = a;
9267           break;
9268         case DW_AT_location:
9269           attrs->at_location = a;
9270           break;
9271         case DW_AT_lower_bound:
9272           attrs->at_lower_bound = a;
9273           break;
9274         case DW_AT_mutable:
9275           attrs->at_mutable = a;
9276           break;
9277         case DW_AT_ordering:
9278           attrs->at_ordering = a;
9279           break;
9280         case DW_AT_picture_string:
9281           attrs->at_picture_string = a;
9282           break;
9283         case DW_AT_prototyped:
9284           attrs->at_prototyped = a;
9285           break;
9286         case DW_AT_small:
9287           attrs->at_small = a;
9288           break;
9289         case DW_AT_segment:
9290           attrs->at_segment = a;
9291           break;
9292         case DW_AT_string_length:
9293           attrs->at_string_length = a;
9294           break;
9295         case DW_AT_threads_scaled:
9296           attrs->at_threads_scaled = a;
9297           break;
9298         case DW_AT_upper_bound:
9299           attrs->at_upper_bound = a;
9300           break;
9301         case DW_AT_use_location:
9302           attrs->at_use_location = a;
9303           break;
9304         case DW_AT_use_UTF8:
9305           attrs->at_use_UTF8 = a;
9306           break;
9307         case DW_AT_variable_parameter:
9308           attrs->at_variable_parameter = a;
9309           break;
9310         case DW_AT_virtuality:
9311           attrs->at_virtuality = a;
9312           break;
9313         case DW_AT_visibility:
9314           attrs->at_visibility = a;
9315           break;
9316         case DW_AT_vtable_elem_location:
9317           attrs->at_vtable_elem_location = a;
9318           break;
9319         default:
9320           break;
9321         }
9322     }
9323 }
9324
9325 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9326
9327 static void
9328 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9329 {
9330   dw_die_ref c;
9331   dw_die_ref decl;
9332   struct checksum_attributes attrs;
9333
9334   CHECKSUM_ULEB128 ('D');
9335   CHECKSUM_ULEB128 (die->die_tag);
9336
9337   memset (&attrs, 0, sizeof (attrs));
9338
9339   decl = get_AT_ref (die, DW_AT_specification);
9340   if (decl != NULL)
9341     collect_checksum_attributes (&attrs, decl);
9342   collect_checksum_attributes (&attrs, die);
9343
9344   CHECKSUM_ATTR (attrs.at_name);
9345   CHECKSUM_ATTR (attrs.at_accessibility);
9346   CHECKSUM_ATTR (attrs.at_address_class);
9347   CHECKSUM_ATTR (attrs.at_allocated);
9348   CHECKSUM_ATTR (attrs.at_artificial);
9349   CHECKSUM_ATTR (attrs.at_associated);
9350   CHECKSUM_ATTR (attrs.at_binary_scale);
9351   CHECKSUM_ATTR (attrs.at_bit_offset);
9352   CHECKSUM_ATTR (attrs.at_bit_size);
9353   CHECKSUM_ATTR (attrs.at_bit_stride);
9354   CHECKSUM_ATTR (attrs.at_byte_size);
9355   CHECKSUM_ATTR (attrs.at_byte_stride);
9356   CHECKSUM_ATTR (attrs.at_const_value);
9357   CHECKSUM_ATTR (attrs.at_containing_type);
9358   CHECKSUM_ATTR (attrs.at_count);
9359   CHECKSUM_ATTR (attrs.at_data_location);
9360   CHECKSUM_ATTR (attrs.at_data_member_location);
9361   CHECKSUM_ATTR (attrs.at_decimal_scale);
9362   CHECKSUM_ATTR (attrs.at_decimal_sign);
9363   CHECKSUM_ATTR (attrs.at_default_value);
9364   CHECKSUM_ATTR (attrs.at_digit_count);
9365   CHECKSUM_ATTR (attrs.at_discr);
9366   CHECKSUM_ATTR (attrs.at_discr_list);
9367   CHECKSUM_ATTR (attrs.at_discr_value);
9368   CHECKSUM_ATTR (attrs.at_encoding);
9369   CHECKSUM_ATTR (attrs.at_endianity);
9370   CHECKSUM_ATTR (attrs.at_explicit);
9371   CHECKSUM_ATTR (attrs.at_is_optional);
9372   CHECKSUM_ATTR (attrs.at_location);
9373   CHECKSUM_ATTR (attrs.at_lower_bound);
9374   CHECKSUM_ATTR (attrs.at_mutable);
9375   CHECKSUM_ATTR (attrs.at_ordering);
9376   CHECKSUM_ATTR (attrs.at_picture_string);
9377   CHECKSUM_ATTR (attrs.at_prototyped);
9378   CHECKSUM_ATTR (attrs.at_small);
9379   CHECKSUM_ATTR (attrs.at_segment);
9380   CHECKSUM_ATTR (attrs.at_string_length);
9381   CHECKSUM_ATTR (attrs.at_threads_scaled);
9382   CHECKSUM_ATTR (attrs.at_upper_bound);
9383   CHECKSUM_ATTR (attrs.at_use_location);
9384   CHECKSUM_ATTR (attrs.at_use_UTF8);
9385   CHECKSUM_ATTR (attrs.at_variable_parameter);
9386   CHECKSUM_ATTR (attrs.at_virtuality);
9387   CHECKSUM_ATTR (attrs.at_visibility);
9388   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9389   CHECKSUM_ATTR (attrs.at_type);
9390   CHECKSUM_ATTR (attrs.at_friend);
9391
9392   /* Checksum the child DIEs, except for nested types and member functions.  */
9393   c = die->die_child;
9394   if (c) do {
9395     dw_attr_ref name_attr;
9396
9397     c = c->die_sib;
9398     name_attr = get_AT (c, DW_AT_name);
9399     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9400         && name_attr != NULL)
9401       {
9402         CHECKSUM_ULEB128 ('S');
9403         CHECKSUM_ULEB128 (c->die_tag);
9404         CHECKSUM_STRING (AT_string (name_attr));
9405       }
9406     else
9407       {
9408         /* Mark this DIE so it gets processed when unmarking.  */
9409         if (c->die_mark == 0)
9410           c->die_mark = -1;
9411         die_checksum_ordered (c, ctx, mark);
9412       }
9413   } while (c != die->die_child);
9414
9415   CHECKSUM_ULEB128 (0);
9416 }
9417
9418 #undef CHECKSUM
9419 #undef CHECKSUM_STRING
9420 #undef CHECKSUM_ATTR
9421 #undef CHECKSUM_LEB128
9422 #undef CHECKSUM_ULEB128
9423
9424 /* Generate the type signature for DIE.  This is computed by generating an
9425    MD5 checksum over the DIE's tag, its relevant attributes, and its
9426    children.  Attributes that are references to other DIEs are processed
9427    by recursion, using the MARK field to prevent infinite recursion.
9428    If the DIE is nested inside a namespace or another type, we also
9429    need to include that context in the signature.  The lower 64 bits
9430    of the resulting MD5 checksum comprise the signature.  */
9431
9432 static void
9433 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9434 {
9435   int mark;
9436   const char *name;
9437   unsigned char checksum[16];
9438   struct md5_ctx ctx;
9439   dw_die_ref decl;
9440
9441   name = get_AT_string (die, DW_AT_name);
9442   decl = get_AT_ref (die, DW_AT_specification);
9443
9444   /* First, compute a signature for just the type name (and its surrounding
9445      context, if any.  This is stored in the type unit DIE for link-time
9446      ODR (one-definition rule) checking.  */
9447
9448   if (is_cxx() && name != NULL)
9449     {
9450       md5_init_ctx (&ctx);
9451
9452       /* Checksum the names of surrounding namespaces and structures.  */
9453       if (decl != NULL && decl->die_parent != NULL)
9454         checksum_die_context (decl->die_parent, &ctx);
9455
9456       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9457       md5_process_bytes (name, strlen (name) + 1, &ctx);
9458       md5_finish_ctx (&ctx, checksum);
9459
9460       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9461     }
9462
9463   /* Next, compute the complete type signature.  */
9464
9465   md5_init_ctx (&ctx);
9466   mark = 1;
9467   die->die_mark = mark;
9468
9469   /* Checksum the names of surrounding namespaces and structures.  */
9470   if (decl != NULL && decl->die_parent != NULL)
9471     checksum_die_context (decl->die_parent, &ctx);
9472
9473   /* Checksum the DIE and its children.  */
9474   die_checksum_ordered (die, &ctx, &mark);
9475   unmark_all_dies (die);
9476   md5_finish_ctx (&ctx, checksum);
9477
9478   /* Store the signature in the type node and link the type DIE and the
9479      type node together.  */
9480   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9481           DWARF_TYPE_SIGNATURE_SIZE);
9482   die->die_id.die_type_node = type_node;
9483   type_node->type_die = die;
9484
9485   /* If the DIE is a specification, link its declaration to the type node
9486      as well.  */
9487   if (decl != NULL)
9488     decl->die_id.die_type_node = type_node;
9489 }
9490
9491 /* Do the location expressions look same?  */
9492 static inline int
9493 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9494 {
9495   return loc1->dw_loc_opc == loc2->dw_loc_opc
9496          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9497          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9498 }
9499
9500 /* Do the values look the same?  */
9501 static int
9502 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9503 {
9504   dw_loc_descr_ref loc1, loc2;
9505   rtx r1, r2;
9506
9507   if (v1->val_class != v2->val_class)
9508     return 0;
9509
9510   switch (v1->val_class)
9511     {
9512     case dw_val_class_const:
9513       return v1->v.val_int == v2->v.val_int;
9514     case dw_val_class_unsigned_const:
9515       return v1->v.val_unsigned == v2->v.val_unsigned;
9516     case dw_val_class_const_double:
9517       return v1->v.val_double.high == v2->v.val_double.high
9518              && v1->v.val_double.low == v2->v.val_double.low;
9519     case dw_val_class_vec:
9520       if (v1->v.val_vec.length != v2->v.val_vec.length
9521           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9522         return 0;
9523       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9524                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9525         return 0;
9526       return 1;
9527     case dw_val_class_flag:
9528       return v1->v.val_flag == v2->v.val_flag;
9529     case dw_val_class_str:
9530       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9531
9532     case dw_val_class_addr:
9533       r1 = v1->v.val_addr;
9534       r2 = v2->v.val_addr;
9535       if (GET_CODE (r1) != GET_CODE (r2))
9536         return 0;
9537       return !rtx_equal_p (r1, r2);
9538
9539     case dw_val_class_offset:
9540       return v1->v.val_offset == v2->v.val_offset;
9541
9542     case dw_val_class_loc:
9543       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9544            loc1 && loc2;
9545            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9546         if (!same_loc_p (loc1, loc2, mark))
9547           return 0;
9548       return !loc1 && !loc2;
9549
9550     case dw_val_class_die_ref:
9551       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9552
9553     case dw_val_class_fde_ref:
9554     case dw_val_class_vms_delta:
9555     case dw_val_class_lbl_id:
9556     case dw_val_class_lineptr:
9557     case dw_val_class_macptr:
9558       return 1;
9559
9560     case dw_val_class_file:
9561       return v1->v.val_file == v2->v.val_file;
9562
9563     case dw_val_class_data8:
9564       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9565
9566     default:
9567       return 1;
9568     }
9569 }
9570
9571 /* Do the attributes look the same?  */
9572
9573 static int
9574 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9575 {
9576   if (at1->dw_attr != at2->dw_attr)
9577     return 0;
9578
9579   /* We don't care that this was compiled with a different compiler
9580      snapshot; if the output is the same, that's what matters. */
9581   if (at1->dw_attr == DW_AT_producer)
9582     return 1;
9583
9584   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9585 }
9586
9587 /* Do the dies look the same?  */
9588
9589 static int
9590 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9591 {
9592   dw_die_ref c1, c2;
9593   dw_attr_ref a1;
9594   unsigned ix;
9595
9596   /* To avoid infinite recursion.  */
9597   if (die1->die_mark)
9598     return die1->die_mark == die2->die_mark;
9599   die1->die_mark = die2->die_mark = ++(*mark);
9600
9601   if (die1->die_tag != die2->die_tag)
9602     return 0;
9603
9604   if (VEC_length (dw_attr_node, die1->die_attr)
9605       != VEC_length (dw_attr_node, die2->die_attr))
9606     return 0;
9607
9608   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9609     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9610       return 0;
9611
9612   c1 = die1->die_child;
9613   c2 = die2->die_child;
9614   if (! c1)
9615     {
9616       if (c2)
9617         return 0;
9618     }
9619   else
9620     for (;;)
9621       {
9622         if (!same_die_p (c1, c2, mark))
9623           return 0;
9624         c1 = c1->die_sib;
9625         c2 = c2->die_sib;
9626         if (c1 == die1->die_child)
9627           {
9628             if (c2 == die2->die_child)
9629               break;
9630             else
9631               return 0;
9632           }
9633     }
9634
9635   return 1;
9636 }
9637
9638 /* Do the dies look the same?  Wrapper around same_die_p.  */
9639
9640 static int
9641 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9642 {
9643   int mark = 0;
9644   int ret = same_die_p (die1, die2, &mark);
9645
9646   unmark_all_dies (die1);
9647   unmark_all_dies (die2);
9648
9649   return ret;
9650 }
9651
9652 /* The prefix to attach to symbols on DIEs in the current comdat debug
9653    info section.  */
9654 static char *comdat_symbol_id;
9655
9656 /* The index of the current symbol within the current comdat CU.  */
9657 static unsigned int comdat_symbol_number;
9658
9659 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9660    children, and set comdat_symbol_id accordingly.  */
9661
9662 static void
9663 compute_section_prefix (dw_die_ref unit_die)
9664 {
9665   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9666   const char *base = die_name ? lbasename (die_name) : "anonymous";
9667   char *name = XALLOCAVEC (char, strlen (base) + 64);
9668   char *p;
9669   int i, mark;
9670   unsigned char checksum[16];
9671   struct md5_ctx ctx;
9672
9673   /* Compute the checksum of the DIE, then append part of it as hex digits to
9674      the name filename of the unit.  */
9675
9676   md5_init_ctx (&ctx);
9677   mark = 0;
9678   die_checksum (unit_die, &ctx, &mark);
9679   unmark_all_dies (unit_die);
9680   md5_finish_ctx (&ctx, checksum);
9681
9682   sprintf (name, "%s.", base);
9683   clean_symbol_name (name);
9684
9685   p = name + strlen (name);
9686   for (i = 0; i < 4; i++)
9687     {
9688       sprintf (p, "%.2x", checksum[i]);
9689       p += 2;
9690     }
9691
9692   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9693   comdat_symbol_number = 0;
9694 }
9695
9696 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9697
9698 static int
9699 is_type_die (dw_die_ref die)
9700 {
9701   switch (die->die_tag)
9702     {
9703     case DW_TAG_array_type:
9704     case DW_TAG_class_type:
9705     case DW_TAG_interface_type:
9706     case DW_TAG_enumeration_type:
9707     case DW_TAG_pointer_type:
9708     case DW_TAG_reference_type:
9709     case DW_TAG_rvalue_reference_type:
9710     case DW_TAG_string_type:
9711     case DW_TAG_structure_type:
9712     case DW_TAG_subroutine_type:
9713     case DW_TAG_union_type:
9714     case DW_TAG_ptr_to_member_type:
9715     case DW_TAG_set_type:
9716     case DW_TAG_subrange_type:
9717     case DW_TAG_base_type:
9718     case DW_TAG_const_type:
9719     case DW_TAG_file_type:
9720     case DW_TAG_packed_type:
9721     case DW_TAG_volatile_type:
9722     case DW_TAG_typedef:
9723       return 1;
9724     default:
9725       return 0;
9726     }
9727 }
9728
9729 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9730    Basically, we want to choose the bits that are likely to be shared between
9731    compilations (types) and leave out the bits that are specific to individual
9732    compilations (functions).  */
9733
9734 static int
9735 is_comdat_die (dw_die_ref c)
9736 {
9737   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9738      we do for stabs.  The advantage is a greater likelihood of sharing between
9739      objects that don't include headers in the same order (and therefore would
9740      put the base types in a different comdat).  jason 8/28/00 */
9741
9742   if (c->die_tag == DW_TAG_base_type)
9743     return 0;
9744
9745   if (c->die_tag == DW_TAG_pointer_type
9746       || c->die_tag == DW_TAG_reference_type
9747       || c->die_tag == DW_TAG_rvalue_reference_type
9748       || c->die_tag == DW_TAG_const_type
9749       || c->die_tag == DW_TAG_volatile_type)
9750     {
9751       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9752
9753       return t ? is_comdat_die (t) : 0;
9754     }
9755
9756   return is_type_die (c);
9757 }
9758
9759 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9760    compilation unit.  */
9761
9762 static int
9763 is_symbol_die (dw_die_ref c)
9764 {
9765   return (is_type_die (c)
9766           || is_declaration_die (c)
9767           || c->die_tag == DW_TAG_namespace
9768           || c->die_tag == DW_TAG_module);
9769 }
9770
9771 /* Returns true iff C is a compile-unit DIE.  */
9772
9773 static inline bool
9774 is_cu_die (dw_die_ref c)
9775 {
9776   return c && c->die_tag == DW_TAG_compile_unit;
9777 }
9778
9779 static char *
9780 gen_internal_sym (const char *prefix)
9781 {
9782   char buf[256];
9783
9784   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9785   return xstrdup (buf);
9786 }
9787
9788 /* Assign symbols to all worthy DIEs under DIE.  */
9789
9790 static void
9791 assign_symbol_names (dw_die_ref die)
9792 {
9793   dw_die_ref c;
9794
9795   if (is_symbol_die (die))
9796     {
9797       if (comdat_symbol_id)
9798         {
9799           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9800
9801           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9802                    comdat_symbol_id, comdat_symbol_number++);
9803           die->die_id.die_symbol = xstrdup (p);
9804         }
9805       else
9806         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9807     }
9808
9809   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9810 }
9811
9812 struct cu_hash_table_entry
9813 {
9814   dw_die_ref cu;
9815   unsigned min_comdat_num, max_comdat_num;
9816   struct cu_hash_table_entry *next;
9817 };
9818
9819 /* Routines to manipulate hash table of CUs.  */
9820 static hashval_t
9821 htab_cu_hash (const void *of)
9822 {
9823   const struct cu_hash_table_entry *const entry =
9824     (const struct cu_hash_table_entry *) of;
9825
9826   return htab_hash_string (entry->cu->die_id.die_symbol);
9827 }
9828
9829 static int
9830 htab_cu_eq (const void *of1, const void *of2)
9831 {
9832   const struct cu_hash_table_entry *const entry1 =
9833     (const struct cu_hash_table_entry *) of1;
9834   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9835
9836   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9837 }
9838
9839 static void
9840 htab_cu_del (void *what)
9841 {
9842   struct cu_hash_table_entry *next,
9843     *entry = (struct cu_hash_table_entry *) what;
9844
9845   while (entry)
9846     {
9847       next = entry->next;
9848       free (entry);
9849       entry = next;
9850     }
9851 }
9852
9853 /* Check whether we have already seen this CU and set up SYM_NUM
9854    accordingly.  */
9855 static int
9856 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9857 {
9858   struct cu_hash_table_entry dummy;
9859   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9860
9861   dummy.max_comdat_num = 0;
9862
9863   slot = (struct cu_hash_table_entry **)
9864     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9865         INSERT);
9866   entry = *slot;
9867
9868   for (; entry; last = entry, entry = entry->next)
9869     {
9870       if (same_die_p_wrap (cu, entry->cu))
9871         break;
9872     }
9873
9874   if (entry)
9875     {
9876       *sym_num = entry->min_comdat_num;
9877       return 1;
9878     }
9879
9880   entry = XCNEW (struct cu_hash_table_entry);
9881   entry->cu = cu;
9882   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9883   entry->next = *slot;
9884   *slot = entry;
9885
9886   return 0;
9887 }
9888
9889 /* Record SYM_NUM to record of CU in HTABLE.  */
9890 static void
9891 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9892 {
9893   struct cu_hash_table_entry **slot, *entry;
9894
9895   slot = (struct cu_hash_table_entry **)
9896     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9897         NO_INSERT);
9898   entry = *slot;
9899
9900   entry->max_comdat_num = sym_num;
9901 }
9902
9903 /* Traverse the DIE (which is always comp_unit_die), and set up
9904    additional compilation units for each of the include files we see
9905    bracketed by BINCL/EINCL.  */
9906
9907 static void
9908 break_out_includes (dw_die_ref die)
9909 {
9910   dw_die_ref c;
9911   dw_die_ref unit = NULL;
9912   limbo_die_node *node, **pnode;
9913   htab_t cu_hash_table;
9914
9915   c = die->die_child;
9916   if (c) do {
9917     dw_die_ref prev = c;
9918     c = c->die_sib;
9919     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9920            || (unit && is_comdat_die (c)))
9921       {
9922         dw_die_ref next = c->die_sib;
9923
9924         /* This DIE is for a secondary CU; remove it from the main one.  */
9925         remove_child_with_prev (c, prev);
9926
9927         if (c->die_tag == DW_TAG_GNU_BINCL)
9928           unit = push_new_compile_unit (unit, c);
9929         else if (c->die_tag == DW_TAG_GNU_EINCL)
9930           unit = pop_compile_unit (unit);
9931         else
9932           add_child_die (unit, c);
9933         c = next;
9934         if (c == die->die_child)
9935           break;
9936       }
9937   } while (c != die->die_child);
9938
9939 #if 0
9940   /* We can only use this in debugging, since the frontend doesn't check
9941      to make sure that we leave every include file we enter.  */
9942   gcc_assert (!unit);
9943 #endif
9944
9945   assign_symbol_names (die);
9946   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9947   for (node = limbo_die_list, pnode = &limbo_die_list;
9948        node;
9949        node = node->next)
9950     {
9951       int is_dupl;
9952
9953       compute_section_prefix (node->die);
9954       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9955                         &comdat_symbol_number);
9956       assign_symbol_names (node->die);
9957       if (is_dupl)
9958         *pnode = node->next;
9959       else
9960         {
9961           pnode = &node->next;
9962           record_comdat_symbol_number (node->die, cu_hash_table,
9963                 comdat_symbol_number);
9964         }
9965     }
9966   htab_delete (cu_hash_table);
9967 }
9968
9969 /* Return non-zero if this DIE is a declaration.  */
9970
9971 static int
9972 is_declaration_die (dw_die_ref die)
9973 {
9974   dw_attr_ref a;
9975   unsigned ix;
9976
9977   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9978     if (a->dw_attr == DW_AT_declaration)
9979       return 1;
9980
9981   return 0;
9982 }
9983
9984 /* Return non-zero if this DIE is nested inside a subprogram.  */
9985
9986 static int
9987 is_nested_in_subprogram (dw_die_ref die)
9988 {
9989   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9990
9991   if (decl == NULL)
9992     decl = die;
9993   return local_scope_p (decl);
9994 }
9995
9996 /* Return non-zero if this is a type DIE that should be moved to a
9997    COMDAT .debug_types section.  */
9998
9999 static int
10000 should_move_die_to_comdat (dw_die_ref die)
10001 {
10002   switch (die->die_tag)
10003     {
10004     case DW_TAG_class_type:
10005     case DW_TAG_structure_type:
10006     case DW_TAG_enumeration_type:
10007     case DW_TAG_union_type:
10008       /* Don't move declarations, inlined instances, or types nested in a
10009          subprogram.  */
10010       if (is_declaration_die (die)
10011           || get_AT (die, DW_AT_abstract_origin)
10012           || is_nested_in_subprogram (die))
10013         return 0;
10014       return 1;
10015     case DW_TAG_array_type:
10016     case DW_TAG_interface_type:
10017     case DW_TAG_pointer_type:
10018     case DW_TAG_reference_type:
10019     case DW_TAG_rvalue_reference_type:
10020     case DW_TAG_string_type:
10021     case DW_TAG_subroutine_type:
10022     case DW_TAG_ptr_to_member_type:
10023     case DW_TAG_set_type:
10024     case DW_TAG_subrange_type:
10025     case DW_TAG_base_type:
10026     case DW_TAG_const_type:
10027     case DW_TAG_file_type:
10028     case DW_TAG_packed_type:
10029     case DW_TAG_volatile_type:
10030     case DW_TAG_typedef:
10031     default:
10032       return 0;
10033     }
10034 }
10035
10036 /* Make a clone of DIE.  */
10037
10038 static dw_die_ref
10039 clone_die (dw_die_ref die)
10040 {
10041   dw_die_ref clone;
10042   dw_attr_ref a;
10043   unsigned ix;
10044
10045   clone = ggc_alloc_cleared_die_node ();
10046   clone->die_tag = die->die_tag;
10047
10048   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10049     add_dwarf_attr (clone, a);
10050
10051   return clone;
10052 }
10053
10054 /* Make a clone of the tree rooted at DIE.  */
10055
10056 static dw_die_ref
10057 clone_tree (dw_die_ref die)
10058 {
10059   dw_die_ref c;
10060   dw_die_ref clone = clone_die (die);
10061
10062   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10063
10064   return clone;
10065 }
10066
10067 /* Make a clone of DIE as a declaration.  */
10068
10069 static dw_die_ref
10070 clone_as_declaration (dw_die_ref die)
10071 {
10072   dw_die_ref clone;
10073   dw_die_ref decl;
10074   dw_attr_ref a;
10075   unsigned ix;
10076
10077   /* If the DIE is already a declaration, just clone it.  */
10078   if (is_declaration_die (die))
10079     return clone_die (die);
10080
10081   /* If the DIE is a specification, just clone its declaration DIE.  */
10082   decl = get_AT_ref (die, DW_AT_specification);
10083   if (decl != NULL)
10084     return clone_die (decl);
10085
10086   clone = ggc_alloc_cleared_die_node ();
10087   clone->die_tag = die->die_tag;
10088
10089   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10090     {
10091       /* We don't want to copy over all attributes.
10092          For example we don't want DW_AT_byte_size because otherwise we will no
10093          longer have a declaration and GDB will treat it as a definition.  */
10094
10095       switch (a->dw_attr)
10096         {
10097         case DW_AT_artificial:
10098         case DW_AT_containing_type:
10099         case DW_AT_external:
10100         case DW_AT_name:
10101         case DW_AT_type:
10102         case DW_AT_virtuality:
10103         case DW_AT_linkage_name:
10104         case DW_AT_MIPS_linkage_name:
10105           add_dwarf_attr (clone, a);
10106           break;
10107         case DW_AT_byte_size:
10108         default:
10109           break;
10110         }
10111     }
10112
10113   if (die->die_id.die_type_node)
10114     add_AT_die_ref (clone, DW_AT_signature, die);
10115
10116   add_AT_flag (clone, DW_AT_declaration, 1);
10117   return clone;
10118 }
10119
10120 /* Copy the declaration context to the new compile unit DIE.  This includes
10121    any surrounding namespace or type declarations.  If the DIE has an
10122    AT_specification attribute, it also includes attributes and children
10123    attached to the specification.  */
10124
10125 static void
10126 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10127 {
10128   dw_die_ref decl;
10129   dw_die_ref new_decl;
10130
10131   decl = get_AT_ref (die, DW_AT_specification);
10132   if (decl == NULL)
10133     decl = die;
10134   else
10135     {
10136       unsigned ix;
10137       dw_die_ref c;
10138       dw_attr_ref a;
10139
10140       /* Copy the type node pointer from the new DIE to the original
10141          declaration DIE so we can forward references later.  */
10142       decl->die_id.die_type_node = die->die_id.die_type_node;
10143
10144       remove_AT (die, DW_AT_specification);
10145
10146       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10147         {
10148           if (a->dw_attr != DW_AT_name
10149               && a->dw_attr != DW_AT_declaration
10150               && a->dw_attr != DW_AT_external)
10151             add_dwarf_attr (die, a);
10152         }
10153
10154       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10155     }
10156
10157   if (decl->die_parent != NULL
10158       && decl->die_parent->die_tag != DW_TAG_compile_unit
10159       && decl->die_parent->die_tag != DW_TAG_type_unit)
10160     {
10161       new_decl = copy_ancestor_tree (unit, decl, NULL);
10162       if (new_decl != NULL)
10163         {
10164           remove_AT (new_decl, DW_AT_signature);
10165           add_AT_specification (die, new_decl);
10166         }
10167     }
10168 }
10169
10170 /* Generate the skeleton ancestor tree for the given NODE, then clone
10171    the DIE and add the clone into the tree.  */
10172
10173 static void
10174 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10175 {
10176   if (node->new_die != NULL)
10177     return;
10178
10179   node->new_die = clone_as_declaration (node->old_die);
10180
10181   if (node->parent != NULL)
10182     {
10183       generate_skeleton_ancestor_tree (node->parent);
10184       add_child_die (node->parent->new_die, node->new_die);
10185     }
10186 }
10187
10188 /* Generate a skeleton tree of DIEs containing any declarations that are
10189    found in the original tree.  We traverse the tree looking for declaration
10190    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10191
10192 static void
10193 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10194 {
10195   skeleton_chain_node node;
10196   dw_die_ref c;
10197   dw_die_ref first;
10198   dw_die_ref prev = NULL;
10199   dw_die_ref next = NULL;
10200
10201   node.parent = parent;
10202
10203   first = c = parent->old_die->die_child;
10204   if (c)
10205     next = c->die_sib;
10206   if (c) do {
10207     if (prev == NULL || prev->die_sib == c)
10208       prev = c;
10209     c = next;
10210     next = (c == first ? NULL : c->die_sib);
10211     node.old_die = c;
10212     node.new_die = NULL;
10213     if (is_declaration_die (c))
10214       {
10215         /* Clone the existing DIE, move the original to the skeleton
10216            tree (which is in the main CU), and put the clone, with
10217            all the original's children, where the original came from.  */
10218         dw_die_ref clone = clone_die (c);
10219         move_all_children (c, clone);
10220
10221         replace_child (c, clone, prev);
10222         generate_skeleton_ancestor_tree (parent);
10223         add_child_die (parent->new_die, c);
10224         node.new_die = c;
10225         c = clone;
10226       }
10227     generate_skeleton_bottom_up (&node);
10228   } while (next != NULL);
10229 }
10230
10231 /* Wrapper function for generate_skeleton_bottom_up.  */
10232
10233 static dw_die_ref
10234 generate_skeleton (dw_die_ref die)
10235 {
10236   skeleton_chain_node node;
10237
10238   node.old_die = die;
10239   node.new_die = NULL;
10240   node.parent = NULL;
10241
10242   /* If this type definition is nested inside another type,
10243      always leave at least a declaration in its place.  */
10244   if (die->die_parent != NULL && is_type_die (die->die_parent))
10245     node.new_die = clone_as_declaration (die);
10246
10247   generate_skeleton_bottom_up (&node);
10248   return node.new_die;
10249 }
10250
10251 /* Remove the DIE from its parent, possibly replacing it with a cloned
10252    declaration.  The original DIE will be moved to a new compile unit
10253    so that existing references to it follow it to the new location.  If
10254    any of the original DIE's descendants is a declaration, we need to
10255    replace the original DIE with a skeleton tree and move the
10256    declarations back into the skeleton tree.  */
10257
10258 static dw_die_ref
10259 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10260 {
10261   dw_die_ref skeleton;
10262
10263   skeleton = generate_skeleton (child);
10264   if (skeleton == NULL)
10265     remove_child_with_prev (child, prev);
10266   else
10267     {
10268       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10269       replace_child (child, skeleton, prev);
10270     }
10271
10272   return skeleton;
10273 }
10274
10275 /* Traverse the DIE and set up additional .debug_types sections for each
10276    type worthy of being placed in a COMDAT section.  */
10277
10278 static void
10279 break_out_comdat_types (dw_die_ref die)
10280 {
10281   dw_die_ref c;
10282   dw_die_ref first;
10283   dw_die_ref prev = NULL;
10284   dw_die_ref next = NULL;
10285   dw_die_ref unit = NULL;
10286
10287   first = c = die->die_child;
10288   if (c)
10289     next = c->die_sib;
10290   if (c) do {
10291     if (prev == NULL || prev->die_sib == c)
10292       prev = c;
10293     c = next;
10294     next = (c == first ? NULL : c->die_sib);
10295     if (should_move_die_to_comdat (c))
10296       {
10297         dw_die_ref replacement;
10298         comdat_type_node_ref type_node;
10299
10300         /* Create a new type unit DIE as the root for the new tree, and
10301            add it to the list of comdat types.  */
10302         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10303         add_AT_unsigned (unit, DW_AT_language,
10304                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10305         type_node = ggc_alloc_cleared_comdat_type_node ();
10306         type_node->root_die = unit;
10307         type_node->next = comdat_type_list;
10308         comdat_type_list = type_node;
10309
10310         /* Generate the type signature.  */
10311         generate_type_signature (c, type_node);
10312
10313         /* Copy the declaration context, attributes, and children of the
10314            declaration into the new compile unit DIE.  */
10315         copy_declaration_context (unit, c);
10316
10317         /* Remove this DIE from the main CU.  */
10318         replacement = remove_child_or_replace_with_skeleton (c, prev);
10319
10320         /* Break out nested types into their own type units.  */
10321         break_out_comdat_types (c);
10322
10323         /* Add the DIE to the new compunit.  */
10324         add_child_die (unit, c);
10325
10326         if (replacement != NULL)
10327           c = replacement;
10328       }
10329     else if (c->die_tag == DW_TAG_namespace
10330              || c->die_tag == DW_TAG_class_type
10331              || c->die_tag == DW_TAG_structure_type
10332              || c->die_tag == DW_TAG_union_type)
10333       {
10334         /* Look for nested types that can be broken out.  */
10335         break_out_comdat_types (c);
10336       }
10337   } while (next != NULL);
10338 }
10339
10340 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10341
10342 struct decl_table_entry
10343 {
10344   dw_die_ref orig;
10345   dw_die_ref copy;
10346 };
10347
10348 /* Routines to manipulate hash table of copied declarations.  */
10349
10350 static hashval_t
10351 htab_decl_hash (const void *of)
10352 {
10353   const struct decl_table_entry *const entry =
10354     (const struct decl_table_entry *) of;
10355
10356   return htab_hash_pointer (entry->orig);
10357 }
10358
10359 static int
10360 htab_decl_eq (const void *of1, const void *of2)
10361 {
10362   const struct decl_table_entry *const entry1 =
10363     (const struct decl_table_entry *) of1;
10364   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10365
10366   return entry1->orig == entry2;
10367 }
10368
10369 static void
10370 htab_decl_del (void *what)
10371 {
10372   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10373
10374   free (entry);
10375 }
10376
10377 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10378    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10379    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10380    to check if the ancestor has already been copied into UNIT.  */
10381
10382 static dw_die_ref
10383 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10384 {
10385   dw_die_ref parent = die->die_parent;
10386   dw_die_ref new_parent = unit;
10387   dw_die_ref copy;
10388   void **slot = NULL;
10389   struct decl_table_entry *entry = NULL;
10390
10391   if (decl_table)
10392     {
10393       /* Check if the entry has already been copied to UNIT.  */
10394       slot = htab_find_slot_with_hash (decl_table, die,
10395                                        htab_hash_pointer (die), INSERT);
10396       if (*slot != HTAB_EMPTY_ENTRY)
10397         {
10398           entry = (struct decl_table_entry *) *slot;
10399           return entry->copy;
10400         }
10401
10402       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10403       entry = XCNEW (struct decl_table_entry);
10404       entry->orig = die;
10405       entry->copy = NULL;
10406       *slot = entry;
10407     }
10408
10409   if (parent != NULL)
10410     {
10411       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10412       if (spec != NULL)
10413         parent = spec;
10414       if (parent->die_tag != DW_TAG_compile_unit
10415           && parent->die_tag != DW_TAG_type_unit)
10416         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10417     }
10418
10419   copy = clone_as_declaration (die);
10420   add_child_die (new_parent, copy);
10421
10422   if (decl_table != NULL)
10423     {
10424       /* Record the pointer to the copy.  */
10425       entry->copy = copy;
10426     }
10427
10428   return copy;
10429 }
10430
10431 /* Walk the DIE and its children, looking for references to incomplete
10432    or trivial types that are unmarked (i.e., that are not in the current
10433    type_unit).  */
10434
10435 static void
10436 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10437 {
10438   dw_die_ref c;
10439   dw_attr_ref a;
10440   unsigned ix;
10441
10442   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10443     {
10444       if (AT_class (a) == dw_val_class_die_ref)
10445         {
10446           dw_die_ref targ = AT_ref (a);
10447           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10448           void **slot;
10449           struct decl_table_entry *entry;
10450
10451           if (targ->die_mark != 0 || type_node != NULL)
10452             continue;
10453
10454           slot = htab_find_slot_with_hash (decl_table, targ,
10455                                            htab_hash_pointer (targ), INSERT);
10456
10457           if (*slot != HTAB_EMPTY_ENTRY)
10458             {
10459               /* TARG has already been copied, so we just need to
10460                  modify the reference to point to the copy.  */
10461               entry = (struct decl_table_entry *) *slot;
10462               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10463             }
10464           else
10465             {
10466               dw_die_ref parent = unit;
10467               dw_die_ref copy = clone_tree (targ);
10468
10469               /* Make sure the cloned tree is marked as part of the
10470                  type unit.  */
10471               mark_dies (copy);
10472
10473               /* Record in DECL_TABLE that TARG has been copied.
10474                  Need to do this now, before the recursive call,
10475                  because DECL_TABLE may be expanded and SLOT
10476                  would no longer be a valid pointer.  */
10477               entry = XCNEW (struct decl_table_entry);
10478               entry->orig = targ;
10479               entry->copy = copy;
10480               *slot = entry;
10481
10482               /* If TARG has surrounding context, copy its ancestor tree
10483                  into the new type unit.  */
10484               if (targ->die_parent != NULL
10485                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10486                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10487                 parent = copy_ancestor_tree (unit, targ->die_parent,
10488                                              decl_table);
10489
10490               add_child_die (parent, copy);
10491               a->dw_attr_val.v.val_die_ref.die = copy;
10492
10493               /* Make sure the newly-copied DIE is walked.  If it was
10494                  installed in a previously-added context, it won't
10495                  get visited otherwise.  */
10496               if (parent != unit)
10497                 {
10498                   /* Find the highest point of the newly-added tree,
10499                      mark each node along the way, and walk from there.  */
10500                   parent->die_mark = 1;
10501                   while (parent->die_parent
10502                          && parent->die_parent->die_mark == 0)
10503                     {
10504                       parent = parent->die_parent;
10505                       parent->die_mark = 1;
10506                     }
10507                   copy_decls_walk (unit, parent, decl_table);
10508                 }
10509             }
10510         }
10511     }
10512
10513   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10514 }
10515
10516 /* Copy declarations for "unworthy" types into the new comdat section.
10517    Incomplete types, modified types, and certain other types aren't broken
10518    out into comdat sections of their own, so they don't have a signature,
10519    and we need to copy the declaration into the same section so that we
10520    don't have an external reference.  */
10521
10522 static void
10523 copy_decls_for_unworthy_types (dw_die_ref unit)
10524 {
10525   htab_t decl_table;
10526
10527   mark_dies (unit);
10528   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10529   copy_decls_walk (unit, unit, decl_table);
10530   htab_delete (decl_table);
10531   unmark_dies (unit);
10532 }
10533
10534 /* Traverse the DIE and add a sibling attribute if it may have the
10535    effect of speeding up access to siblings.  To save some space,
10536    avoid generating sibling attributes for DIE's without children.  */
10537
10538 static void
10539 add_sibling_attributes (dw_die_ref die)
10540 {
10541   dw_die_ref c;
10542
10543   if (! die->die_child)
10544     return;
10545
10546   if (die->die_parent && die != die->die_parent->die_child)
10547     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10548
10549   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10550 }
10551
10552 /* Output all location lists for the DIE and its children.  */
10553
10554 static void
10555 output_location_lists (dw_die_ref die)
10556 {
10557   dw_die_ref c;
10558   dw_attr_ref a;
10559   unsigned ix;
10560
10561   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10562     if (AT_class (a) == dw_val_class_loc_list)
10563       output_loc_list (AT_loc_list (a));
10564
10565   FOR_EACH_CHILD (die, c, output_location_lists (c));
10566 }
10567
10568 /* The format of each DIE (and its attribute value pairs) is encoded in an
10569    abbreviation table.  This routine builds the abbreviation table and assigns
10570    a unique abbreviation id for each abbreviation entry.  The children of each
10571    die are visited recursively.  */
10572
10573 static void
10574 build_abbrev_table (dw_die_ref die)
10575 {
10576   unsigned long abbrev_id;
10577   unsigned int n_alloc;
10578   dw_die_ref c;
10579   dw_attr_ref a;
10580   unsigned ix;
10581
10582   /* Scan the DIE references, and mark as external any that refer to
10583      DIEs from other CUs (i.e. those which are not marked).  */
10584   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10585     if (AT_class (a) == dw_val_class_die_ref
10586         && AT_ref (a)->die_mark == 0)
10587       {
10588         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10589         set_AT_ref_external (a, 1);
10590       }
10591
10592   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10593     {
10594       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10595       dw_attr_ref die_a, abbrev_a;
10596       unsigned ix;
10597       bool ok = true;
10598
10599       if (abbrev->die_tag != die->die_tag)
10600         continue;
10601       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10602         continue;
10603
10604       if (VEC_length (dw_attr_node, abbrev->die_attr)
10605           != VEC_length (dw_attr_node, die->die_attr))
10606         continue;
10607
10608       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10609         {
10610           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10611           if ((abbrev_a->dw_attr != die_a->dw_attr)
10612               || (value_format (abbrev_a) != value_format (die_a)))
10613             {
10614               ok = false;
10615               break;
10616             }
10617         }
10618       if (ok)
10619         break;
10620     }
10621
10622   if (abbrev_id >= abbrev_die_table_in_use)
10623     {
10624       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10625         {
10626           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10627           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10628                                             n_alloc);
10629
10630           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10631                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10632           abbrev_die_table_allocated = n_alloc;
10633         }
10634
10635       ++abbrev_die_table_in_use;
10636       abbrev_die_table[abbrev_id] = die;
10637     }
10638
10639   die->die_abbrev = abbrev_id;
10640   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10641 }
10642 \f
10643 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10644
10645 static int
10646 constant_size (unsigned HOST_WIDE_INT value)
10647 {
10648   int log;
10649
10650   if (value == 0)
10651     log = 0;
10652   else
10653     log = floor_log2 (value);
10654
10655   log = log / 8;
10656   log = 1 << (floor_log2 (log) + 1);
10657
10658   return log;
10659 }
10660
10661 /* Return the size of a DIE as it is represented in the
10662    .debug_info section.  */
10663
10664 static unsigned long
10665 size_of_die (dw_die_ref die)
10666 {
10667   unsigned long size = 0;
10668   dw_attr_ref a;
10669   unsigned ix;
10670
10671   size += size_of_uleb128 (die->die_abbrev);
10672   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10673     {
10674       switch (AT_class (a))
10675         {
10676         case dw_val_class_addr:
10677           size += DWARF2_ADDR_SIZE;
10678           break;
10679         case dw_val_class_offset:
10680           size += DWARF_OFFSET_SIZE;
10681           break;
10682         case dw_val_class_loc:
10683           {
10684             unsigned long lsize = size_of_locs (AT_loc (a));
10685
10686             /* Block length.  */
10687             if (dwarf_version >= 4)
10688               size += size_of_uleb128 (lsize);
10689             else
10690               size += constant_size (lsize);
10691             size += lsize;
10692           }
10693           break;
10694         case dw_val_class_loc_list:
10695           size += DWARF_OFFSET_SIZE;
10696           break;
10697         case dw_val_class_range_list:
10698           size += DWARF_OFFSET_SIZE;
10699           break;
10700         case dw_val_class_const:
10701           size += size_of_sleb128 (AT_int (a));
10702           break;
10703         case dw_val_class_unsigned_const:
10704           size += constant_size (AT_unsigned (a));
10705           break;
10706         case dw_val_class_const_double:
10707           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10708           if (HOST_BITS_PER_WIDE_INT >= 64)
10709             size++; /* block */
10710           break;
10711         case dw_val_class_vec:
10712           size += constant_size (a->dw_attr_val.v.val_vec.length
10713                                  * a->dw_attr_val.v.val_vec.elt_size)
10714                   + a->dw_attr_val.v.val_vec.length
10715                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10716           break;
10717         case dw_val_class_flag:
10718           if (dwarf_version >= 4)
10719             /* Currently all add_AT_flag calls pass in 1 as last argument,
10720                so DW_FORM_flag_present can be used.  If that ever changes,
10721                we'll need to use DW_FORM_flag and have some optimization
10722                in build_abbrev_table that will change those to
10723                DW_FORM_flag_present if it is set to 1 in all DIEs using
10724                the same abbrev entry.  */
10725             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10726           else
10727             size += 1;
10728           break;
10729         case dw_val_class_die_ref:
10730           if (AT_ref_external (a))
10731             {
10732               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10733                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10734                  is sized by target address length, whereas in DWARF3
10735                  it's always sized as an offset.  */
10736               if (dwarf_version >= 4)
10737                 size += DWARF_TYPE_SIGNATURE_SIZE;
10738               else if (dwarf_version == 2)
10739                 size += DWARF2_ADDR_SIZE;
10740               else
10741                 size += DWARF_OFFSET_SIZE;
10742             }
10743           else
10744             size += DWARF_OFFSET_SIZE;
10745           break;
10746         case dw_val_class_fde_ref:
10747           size += DWARF_OFFSET_SIZE;
10748           break;
10749         case dw_val_class_lbl_id:
10750           size += DWARF2_ADDR_SIZE;
10751           break;
10752         case dw_val_class_lineptr:
10753         case dw_val_class_macptr:
10754           size += DWARF_OFFSET_SIZE;
10755           break;
10756         case dw_val_class_str:
10757           if (AT_string_form (a) == DW_FORM_strp)
10758             size += DWARF_OFFSET_SIZE;
10759           else
10760             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10761           break;
10762         case dw_val_class_file:
10763           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10764           break;
10765         case dw_val_class_data8:
10766           size += 8;
10767           break;
10768         case dw_val_class_vms_delta:
10769           size += DWARF_OFFSET_SIZE;
10770           break;
10771         default:
10772           gcc_unreachable ();
10773         }
10774     }
10775
10776   return size;
10777 }
10778
10779 /* Size the debugging information associated with a given DIE.  Visits the
10780    DIE's children recursively.  Updates the global variable next_die_offset, on
10781    each time through.  Uses the current value of next_die_offset to update the
10782    die_offset field in each DIE.  */
10783
10784 static void
10785 calc_die_sizes (dw_die_ref die)
10786 {
10787   dw_die_ref c;
10788
10789   die->die_offset = next_die_offset;
10790   next_die_offset += size_of_die (die);
10791
10792   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10793
10794   if (die->die_child != NULL)
10795     /* Count the null byte used to terminate sibling lists.  */
10796     next_die_offset += 1;
10797 }
10798
10799 /* Set the marks for a die and its children.  We do this so
10800    that we know whether or not a reference needs to use FORM_ref_addr; only
10801    DIEs in the same CU will be marked.  We used to clear out the offset
10802    and use that as the flag, but ran into ordering problems.  */
10803
10804 static void
10805 mark_dies (dw_die_ref die)
10806 {
10807   dw_die_ref c;
10808
10809   gcc_assert (!die->die_mark);
10810
10811   die->die_mark = 1;
10812   FOR_EACH_CHILD (die, c, mark_dies (c));
10813 }
10814
10815 /* Clear the marks for a die and its children.  */
10816
10817 static void
10818 unmark_dies (dw_die_ref die)
10819 {
10820   dw_die_ref c;
10821
10822   if (dwarf_version < 4)
10823     gcc_assert (die->die_mark);
10824
10825   die->die_mark = 0;
10826   FOR_EACH_CHILD (die, c, unmark_dies (c));
10827 }
10828
10829 /* Clear the marks for a die, its children and referred dies.  */
10830
10831 static void
10832 unmark_all_dies (dw_die_ref die)
10833 {
10834   dw_die_ref c;
10835   dw_attr_ref a;
10836   unsigned ix;
10837
10838   if (!die->die_mark)
10839     return;
10840   die->die_mark = 0;
10841
10842   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10843
10844   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10845     if (AT_class (a) == dw_val_class_die_ref)
10846       unmark_all_dies (AT_ref (a));
10847 }
10848
10849 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10850    generated for the compilation unit.  */
10851
10852 static unsigned long
10853 size_of_pubnames (VEC (pubname_entry, gc) * names)
10854 {
10855   unsigned long size;
10856   unsigned i;
10857   pubname_ref p;
10858
10859   size = DWARF_PUBNAMES_HEADER_SIZE;
10860   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10861     if (names != pubtype_table
10862         || p->die->die_offset != 0
10863         || !flag_eliminate_unused_debug_types)
10864       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10865
10866   size += DWARF_OFFSET_SIZE;
10867   return size;
10868 }
10869
10870 /* Return the size of the information in the .debug_aranges section.  */
10871
10872 static unsigned long
10873 size_of_aranges (void)
10874 {
10875   unsigned long size;
10876
10877   size = DWARF_ARANGES_HEADER_SIZE;
10878
10879   /* Count the address/length pair for this compilation unit.  */
10880   if (text_section_used)
10881     size += 2 * DWARF2_ADDR_SIZE;
10882   if (cold_text_section_used)
10883     size += 2 * DWARF2_ADDR_SIZE;
10884   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10885
10886   /* Count the two zero words used to terminated the address range table.  */
10887   size += 2 * DWARF2_ADDR_SIZE;
10888   return size;
10889 }
10890 \f
10891 /* Select the encoding of an attribute value.  */
10892
10893 static enum dwarf_form
10894 value_format (dw_attr_ref a)
10895 {
10896   switch (a->dw_attr_val.val_class)
10897     {
10898     case dw_val_class_addr:
10899       /* Only very few attributes allow DW_FORM_addr.  */
10900       switch (a->dw_attr)
10901         {
10902         case DW_AT_low_pc:
10903         case DW_AT_high_pc:
10904         case DW_AT_entry_pc:
10905         case DW_AT_trampoline:
10906           return DW_FORM_addr;
10907         default:
10908           break;
10909         }
10910       switch (DWARF2_ADDR_SIZE)
10911         {
10912         case 1:
10913           return DW_FORM_data1;
10914         case 2:
10915           return DW_FORM_data2;
10916         case 4:
10917           return DW_FORM_data4;
10918         case 8:
10919           return DW_FORM_data8;
10920         default:
10921           gcc_unreachable ();
10922         }
10923     case dw_val_class_range_list:
10924     case dw_val_class_loc_list:
10925       if (dwarf_version >= 4)
10926         return DW_FORM_sec_offset;
10927       /* FALLTHRU */
10928     case dw_val_class_vms_delta:
10929     case dw_val_class_offset:
10930       switch (DWARF_OFFSET_SIZE)
10931         {
10932         case 4:
10933           return DW_FORM_data4;
10934         case 8:
10935           return DW_FORM_data8;
10936         default:
10937           gcc_unreachable ();
10938         }
10939     case dw_val_class_loc:
10940       if (dwarf_version >= 4)
10941         return DW_FORM_exprloc;
10942       switch (constant_size (size_of_locs (AT_loc (a))))
10943         {
10944         case 1:
10945           return DW_FORM_block1;
10946         case 2:
10947           return DW_FORM_block2;
10948         default:
10949           gcc_unreachable ();
10950         }
10951     case dw_val_class_const:
10952       return DW_FORM_sdata;
10953     case dw_val_class_unsigned_const:
10954       switch (constant_size (AT_unsigned (a)))
10955         {
10956         case 1:
10957           return DW_FORM_data1;
10958         case 2:
10959           return DW_FORM_data2;
10960         case 4:
10961           return DW_FORM_data4;
10962         case 8:
10963           return DW_FORM_data8;
10964         default:
10965           gcc_unreachable ();
10966         }
10967     case dw_val_class_const_double:
10968       switch (HOST_BITS_PER_WIDE_INT)
10969         {
10970         case 8:
10971           return DW_FORM_data2;
10972         case 16:
10973           return DW_FORM_data4;
10974         case 32:
10975           return DW_FORM_data8;
10976         case 64:
10977         default:
10978           return DW_FORM_block1;
10979         }
10980     case dw_val_class_vec:
10981       switch (constant_size (a->dw_attr_val.v.val_vec.length
10982                              * a->dw_attr_val.v.val_vec.elt_size))
10983         {
10984         case 1:
10985           return DW_FORM_block1;
10986         case 2:
10987           return DW_FORM_block2;
10988         case 4:
10989           return DW_FORM_block4;
10990         default:
10991           gcc_unreachable ();
10992         }
10993     case dw_val_class_flag:
10994       if (dwarf_version >= 4)
10995         {
10996           /* Currently all add_AT_flag calls pass in 1 as last argument,
10997              so DW_FORM_flag_present can be used.  If that ever changes,
10998              we'll need to use DW_FORM_flag and have some optimization
10999              in build_abbrev_table that will change those to
11000              DW_FORM_flag_present if it is set to 1 in all DIEs using
11001              the same abbrev entry.  */
11002           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11003           return DW_FORM_flag_present;
11004         }
11005       return DW_FORM_flag;
11006     case dw_val_class_die_ref:
11007       if (AT_ref_external (a))
11008         return dwarf_version >= 4 ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11009       else
11010         return DW_FORM_ref;
11011     case dw_val_class_fde_ref:
11012       return DW_FORM_data;
11013     case dw_val_class_lbl_id:
11014       return DW_FORM_addr;
11015     case dw_val_class_lineptr:
11016     case dw_val_class_macptr:
11017       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11018     case dw_val_class_str:
11019       return AT_string_form (a);
11020     case dw_val_class_file:
11021       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11022         {
11023         case 1:
11024           return DW_FORM_data1;
11025         case 2:
11026           return DW_FORM_data2;
11027         case 4:
11028           return DW_FORM_data4;
11029         default:
11030           gcc_unreachable ();
11031         }
11032
11033     case dw_val_class_data8:
11034       return DW_FORM_data8;
11035
11036     default:
11037       gcc_unreachable ();
11038     }
11039 }
11040
11041 /* Output the encoding of an attribute value.  */
11042
11043 static void
11044 output_value_format (dw_attr_ref a)
11045 {
11046   enum dwarf_form form = value_format (a);
11047
11048   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11049 }
11050
11051 /* Output the .debug_abbrev section which defines the DIE abbreviation
11052    table.  */
11053
11054 static void
11055 output_abbrev_section (void)
11056 {
11057   unsigned long abbrev_id;
11058
11059   if (abbrev_die_table_in_use == 1)
11060     return;
11061
11062   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11063     {
11064       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11065       unsigned ix;
11066       dw_attr_ref a_attr;
11067
11068       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11069       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11070                                    dwarf_tag_name (abbrev->die_tag));
11071
11072       if (abbrev->die_child != NULL)
11073         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11074       else
11075         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11076
11077       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11078            ix++)
11079         {
11080           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11081                                        dwarf_attr_name (a_attr->dw_attr));
11082           output_value_format (a_attr);
11083         }
11084
11085       dw2_asm_output_data (1, 0, NULL);
11086       dw2_asm_output_data (1, 0, NULL);
11087     }
11088
11089   /* Terminate the table.  */
11090   dw2_asm_output_data (1, 0, NULL);
11091 }
11092
11093 /* Output a symbol we can use to refer to this DIE from another CU.  */
11094
11095 static inline void
11096 output_die_symbol (dw_die_ref die)
11097 {
11098   char *sym = die->die_id.die_symbol;
11099
11100   if (sym == 0)
11101     return;
11102
11103   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11104     /* We make these global, not weak; if the target doesn't support
11105        .linkonce, it doesn't support combining the sections, so debugging
11106        will break.  */
11107     targetm.asm_out.globalize_label (asm_out_file, sym);
11108
11109   ASM_OUTPUT_LABEL (asm_out_file, sym);
11110 }
11111
11112 /* Return a new location list, given the begin and end range, and the
11113    expression.  */
11114
11115 static inline dw_loc_list_ref
11116 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11117               const char *section)
11118 {
11119   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11120
11121   retlist->begin = begin;
11122   retlist->end = end;
11123   retlist->expr = expr;
11124   retlist->section = section;
11125
11126   return retlist;
11127 }
11128
11129 /* Generate a new internal symbol for this location list node, if it
11130    hasn't got one yet.  */
11131
11132 static inline void
11133 gen_llsym (dw_loc_list_ref list)
11134 {
11135   gcc_assert (!list->ll_symbol);
11136   list->ll_symbol = gen_internal_sym ("LLST");
11137 }
11138
11139 /* Output the location list given to us.  */
11140
11141 static void
11142 output_loc_list (dw_loc_list_ref list_head)
11143 {
11144   dw_loc_list_ref curr = list_head;
11145
11146   if (list_head->emitted)
11147     return;
11148   list_head->emitted = true;
11149
11150   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11151
11152   /* Walk the location list, and output each range + expression.  */
11153   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11154     {
11155       unsigned long size;
11156       /* Don't output an entry that starts and ends at the same address.  */
11157       if (strcmp (curr->begin, curr->end) == 0)
11158         continue;
11159       if (!have_multiple_function_sections)
11160         {
11161           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11162                                 "Location list begin address (%s)",
11163                                 list_head->ll_symbol);
11164           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11165                                 "Location list end address (%s)",
11166                                 list_head->ll_symbol);
11167         }
11168       else
11169         {
11170           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11171                                "Location list begin address (%s)",
11172                                list_head->ll_symbol);
11173           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11174                                "Location list end address (%s)",
11175                                list_head->ll_symbol);
11176         }
11177       size = size_of_locs (curr->expr);
11178
11179       /* Output the block length for this list of location operations.  */
11180       gcc_assert (size <= 0xffff);
11181       dw2_asm_output_data (2, size, "%s", "Location expression size");
11182
11183       output_loc_sequence (curr->expr, -1);
11184     }
11185
11186   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11187                        "Location list terminator begin (%s)",
11188                        list_head->ll_symbol);
11189   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11190                        "Location list terminator end (%s)",
11191                        list_head->ll_symbol);
11192 }
11193
11194 /* Output a type signature.  */
11195
11196 static inline void
11197 output_signature (const char *sig, const char *name)
11198 {
11199   int i;
11200
11201   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11202     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11203 }
11204
11205 /* Output the DIE and its attributes.  Called recursively to generate
11206    the definitions of each child DIE.  */
11207
11208 static void
11209 output_die (dw_die_ref die)
11210 {
11211   dw_attr_ref a;
11212   dw_die_ref c;
11213   unsigned long size;
11214   unsigned ix;
11215
11216   /* If someone in another CU might refer to us, set up a symbol for
11217      them to point to.  */
11218   if (dwarf_version < 4 && die->die_id.die_symbol)
11219     output_die_symbol (die);
11220
11221   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11222                                (unsigned long)die->die_offset,
11223                                dwarf_tag_name (die->die_tag));
11224
11225   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11226     {
11227       const char *name = dwarf_attr_name (a->dw_attr);
11228
11229       switch (AT_class (a))
11230         {
11231         case dw_val_class_addr:
11232           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11233           break;
11234
11235         case dw_val_class_offset:
11236           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11237                                "%s", name);
11238           break;
11239
11240         case dw_val_class_range_list:
11241           {
11242             char *p = strchr (ranges_section_label, '\0');
11243
11244             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11245                      a->dw_attr_val.v.val_offset);
11246             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11247                                    debug_ranges_section, "%s", name);
11248             *p = '\0';
11249           }
11250           break;
11251
11252         case dw_val_class_loc:
11253           size = size_of_locs (AT_loc (a));
11254
11255           /* Output the block length for this list of location operations.  */
11256           if (dwarf_version >= 4)
11257             dw2_asm_output_data_uleb128 (size, "%s", name);
11258           else
11259             dw2_asm_output_data (constant_size (size), size, "%s", name);
11260
11261           output_loc_sequence (AT_loc (a), -1);
11262           break;
11263
11264         case dw_val_class_const:
11265           /* ??? It would be slightly more efficient to use a scheme like is
11266              used for unsigned constants below, but gdb 4.x does not sign
11267              extend.  Gdb 5.x does sign extend.  */
11268           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11269           break;
11270
11271         case dw_val_class_unsigned_const:
11272           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11273                                AT_unsigned (a), "%s", name);
11274           break;
11275
11276         case dw_val_class_const_double:
11277           {
11278             unsigned HOST_WIDE_INT first, second;
11279
11280             if (HOST_BITS_PER_WIDE_INT >= 64)
11281               dw2_asm_output_data (1,
11282                                    2 * HOST_BITS_PER_WIDE_INT
11283                                    / HOST_BITS_PER_CHAR,
11284                                    NULL);
11285
11286             if (WORDS_BIG_ENDIAN)
11287               {
11288                 first = a->dw_attr_val.v.val_double.high;
11289                 second = a->dw_attr_val.v.val_double.low;
11290               }
11291             else
11292               {
11293                 first = a->dw_attr_val.v.val_double.low;
11294                 second = a->dw_attr_val.v.val_double.high;
11295               }
11296
11297             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11298                                  first, name);
11299             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11300                                  second, NULL);
11301           }
11302           break;
11303
11304         case dw_val_class_vec:
11305           {
11306             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11307             unsigned int len = a->dw_attr_val.v.val_vec.length;
11308             unsigned int i;
11309             unsigned char *p;
11310
11311             dw2_asm_output_data (constant_size (len * elt_size),
11312                                  len * elt_size, "%s", name);
11313             if (elt_size > sizeof (HOST_WIDE_INT))
11314               {
11315                 elt_size /= 2;
11316                 len *= 2;
11317               }
11318             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11319                  i < len;
11320                  i++, p += elt_size)
11321               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11322                                    "fp or vector constant word %u", i);
11323             break;
11324           }
11325
11326         case dw_val_class_flag:
11327           if (dwarf_version >= 4)
11328             {
11329               /* Currently all add_AT_flag calls pass in 1 as last argument,
11330                  so DW_FORM_flag_present can be used.  If that ever changes,
11331                  we'll need to use DW_FORM_flag and have some optimization
11332                  in build_abbrev_table that will change those to
11333                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11334                  the same abbrev entry.  */
11335               gcc_assert (AT_flag (a) == 1);
11336               if (flag_debug_asm)
11337                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11338                          ASM_COMMENT_START, name);
11339               break;
11340             }
11341           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11342           break;
11343
11344         case dw_val_class_loc_list:
11345           {
11346             char *sym = AT_loc_list (a)->ll_symbol;
11347
11348             gcc_assert (sym);
11349             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11350                                    "%s", name);
11351           }
11352           break;
11353
11354         case dw_val_class_die_ref:
11355           if (AT_ref_external (a))
11356             {
11357               if (dwarf_version >= 4)
11358                 {
11359                   comdat_type_node_ref type_node =
11360                     AT_ref (a)->die_id.die_type_node;
11361
11362                   gcc_assert (type_node);
11363                   output_signature (type_node->signature, name);
11364                 }
11365               else
11366                 {
11367                   char *sym = AT_ref (a)->die_id.die_symbol;
11368                   int size;
11369
11370                   gcc_assert (sym);
11371                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11372                      length, whereas in DWARF3 it's always sized as an
11373                      offset.  */
11374                   if (dwarf_version == 2)
11375                     size = DWARF2_ADDR_SIZE;
11376                   else
11377                     size = DWARF_OFFSET_SIZE;
11378                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11379                                          name);
11380                 }
11381             }
11382           else
11383             {
11384               gcc_assert (AT_ref (a)->die_offset);
11385               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11386                                    "%s", name);
11387             }
11388           break;
11389
11390         case dw_val_class_fde_ref:
11391           {
11392             char l1[20];
11393
11394             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11395                                          a->dw_attr_val.v.val_fde_index * 2);
11396             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11397                                    "%s", name);
11398           }
11399           break;
11400
11401         case dw_val_class_vms_delta:
11402           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11403                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11404                                     "%s", name);
11405           break;
11406
11407         case dw_val_class_lbl_id:
11408           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11409           break;
11410
11411         case dw_val_class_lineptr:
11412           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11413                                  debug_line_section, "%s", name);
11414           break;
11415
11416         case dw_val_class_macptr:
11417           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11418                                  debug_macinfo_section, "%s", name);
11419           break;
11420
11421         case dw_val_class_str:
11422           if (AT_string_form (a) == DW_FORM_strp)
11423             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11424                                    a->dw_attr_val.v.val_str->label,
11425                                    debug_str_section,
11426                                    "%s: \"%s\"", name, AT_string (a));
11427           else
11428             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11429           break;
11430
11431         case dw_val_class_file:
11432           {
11433             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11434
11435             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11436                                  a->dw_attr_val.v.val_file->filename);
11437             break;
11438           }
11439
11440         case dw_val_class_data8:
11441           {
11442             int i;
11443
11444             for (i = 0; i < 8; i++)
11445               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11446                                    i == 0 ? "%s" : NULL, name);
11447             break;
11448           }
11449
11450         default:
11451           gcc_unreachable ();
11452         }
11453     }
11454
11455   FOR_EACH_CHILD (die, c, output_die (c));
11456
11457   /* Add null byte to terminate sibling list.  */
11458   if (die->die_child != NULL)
11459     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11460                          (unsigned long) die->die_offset);
11461 }
11462
11463 /* Output the compilation unit that appears at the beginning of the
11464    .debug_info section, and precedes the DIE descriptions.  */
11465
11466 static void
11467 output_compilation_unit_header (void)
11468 {
11469   int ver = dwarf_version;
11470
11471   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11472     dw2_asm_output_data (4, 0xffffffff,
11473       "Initial length escape value indicating 64-bit DWARF extension");
11474   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11475                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11476                        "Length of Compilation Unit Info");
11477   dw2_asm_output_data (2, ver, "DWARF version number");
11478   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11479                          debug_abbrev_section,
11480                          "Offset Into Abbrev. Section");
11481   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11482 }
11483
11484 /* Output the compilation unit DIE and its children.  */
11485
11486 static void
11487 output_comp_unit (dw_die_ref die, int output_if_empty)
11488 {
11489   const char *secname;
11490   char *oldsym, *tmp;
11491
11492   /* Unless we are outputting main CU, we may throw away empty ones.  */
11493   if (!output_if_empty && die->die_child == NULL)
11494     return;
11495
11496   /* Even if there are no children of this DIE, we must output the information
11497      about the compilation unit.  Otherwise, on an empty translation unit, we
11498      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11499      will then complain when examining the file.  First mark all the DIEs in
11500      this CU so we know which get local refs.  */
11501   mark_dies (die);
11502
11503   build_abbrev_table (die);
11504
11505   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11506   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11507   calc_die_sizes (die);
11508
11509   oldsym = die->die_id.die_symbol;
11510   if (oldsym)
11511     {
11512       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11513
11514       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11515       secname = tmp;
11516       die->die_id.die_symbol = NULL;
11517       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11518     }
11519   else
11520     {
11521       switch_to_section (debug_info_section);
11522       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11523       info_section_emitted = true;
11524     }
11525
11526   /* Output debugging information.  */
11527   output_compilation_unit_header ();
11528   output_die (die);
11529
11530   /* Leave the marks on the main CU, so we can check them in
11531      output_pubnames.  */
11532   if (oldsym)
11533     {
11534       unmark_dies (die);
11535       die->die_id.die_symbol = oldsym;
11536     }
11537 }
11538
11539 /* Output a comdat type unit DIE and its children.  */
11540
11541 static void
11542 output_comdat_type_unit (comdat_type_node *node)
11543 {
11544   const char *secname;
11545   char *tmp;
11546   int i;
11547 #if defined (OBJECT_FORMAT_ELF)
11548   tree comdat_key;
11549 #endif
11550
11551   /* First mark all the DIEs in this CU so we know which get local refs.  */
11552   mark_dies (node->root_die);
11553
11554   build_abbrev_table (node->root_die);
11555
11556   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11557   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11558   calc_die_sizes (node->root_die);
11559
11560 #if defined (OBJECT_FORMAT_ELF)
11561   secname = ".debug_types";
11562   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11563   sprintf (tmp, "wt.");
11564   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11565     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11566   comdat_key = get_identifier (tmp);
11567   targetm.asm_out.named_section (secname,
11568                                  SECTION_DEBUG | SECTION_LINKONCE,
11569                                  comdat_key);
11570 #else
11571   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11572   sprintf (tmp, ".gnu.linkonce.wt.");
11573   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11574     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11575   secname = tmp;
11576   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11577 #endif
11578
11579   /* Output debugging information.  */
11580   output_compilation_unit_header ();
11581   output_signature (node->signature, "Type Signature");
11582   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11583                        "Offset to Type DIE");
11584   output_die (node->root_die);
11585
11586   unmark_dies (node->root_die);
11587 }
11588
11589 /* Return the DWARF2/3 pubname associated with a decl.  */
11590
11591 static const char *
11592 dwarf2_name (tree decl, int scope)
11593 {
11594   if (DECL_NAMELESS (decl))
11595     return NULL;
11596   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11597 }
11598
11599 /* Add a new entry to .debug_pubnames if appropriate.  */
11600
11601 static void
11602 add_pubname_string (const char *str, dw_die_ref die)
11603 {
11604   if (targetm.want_debug_pub_sections)
11605     {
11606       pubname_entry e;
11607
11608       e.die = die;
11609       e.name = xstrdup (str);
11610       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11611     }
11612 }
11613
11614 static void
11615 add_pubname (tree decl, dw_die_ref die)
11616 {
11617   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11618     {
11619       const char *name = dwarf2_name (decl, 1);
11620       if (name)
11621         add_pubname_string (name, die);
11622     }
11623 }
11624
11625 /* Add a new entry to .debug_pubtypes if appropriate.  */
11626
11627 static void
11628 add_pubtype (tree decl, dw_die_ref die)
11629 {
11630   pubname_entry e;
11631
11632   if (!targetm.want_debug_pub_sections)
11633     return;
11634
11635   e.name = NULL;
11636   if ((TREE_PUBLIC (decl)
11637        || is_cu_die (die->die_parent))
11638       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11639     {
11640       e.die = die;
11641       if (TYPE_P (decl))
11642         {
11643           if (TYPE_NAME (decl))
11644             {
11645               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11646                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11647               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11648                        && DECL_NAME (TYPE_NAME (decl)))
11649                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11650               else
11651                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11652             }
11653         }
11654       else
11655         {
11656           e.name = dwarf2_name (decl, 1);
11657           if (e.name)
11658             e.name = xstrdup (e.name);
11659         }
11660
11661       /* If we don't have a name for the type, there's no point in adding
11662          it to the table.  */
11663       if (e.name && e.name[0] != '\0')
11664         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11665     }
11666 }
11667
11668 /* Output the public names table used to speed up access to externally
11669    visible names; or the public types table used to find type definitions.  */
11670
11671 static void
11672 output_pubnames (VEC (pubname_entry, gc) * names)
11673 {
11674   unsigned i;
11675   unsigned long pubnames_length = size_of_pubnames (names);
11676   pubname_ref pub;
11677
11678   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11679     dw2_asm_output_data (4, 0xffffffff,
11680       "Initial length escape value indicating 64-bit DWARF extension");
11681   if (names == pubname_table)
11682     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11683                          "Length of Public Names Info");
11684   else
11685     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11686                          "Length of Public Type Names Info");
11687   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11688   dw2_asm_output_data (2, 2, "DWARF Version");
11689   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11690                          debug_info_section,
11691                          "Offset of Compilation Unit Info");
11692   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11693                        "Compilation Unit Length");
11694
11695   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11696     {
11697       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11698       if (names == pubname_table)
11699         gcc_assert (pub->die->die_mark);
11700
11701       if (names != pubtype_table
11702           || pub->die->die_offset != 0
11703           || !flag_eliminate_unused_debug_types)
11704         {
11705           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11706                                "DIE offset");
11707
11708           dw2_asm_output_nstring (pub->name, -1, "external name");
11709         }
11710     }
11711
11712   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11713 }
11714
11715 /* Add a new entry to .debug_aranges if appropriate.  */
11716
11717 static void
11718 add_arange (tree decl, dw_die_ref die)
11719 {
11720   if (! DECL_SECTION_NAME (decl))
11721     return;
11722
11723   if (arange_table_in_use == arange_table_allocated)
11724     {
11725       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11726       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11727                                     arange_table_allocated);
11728       memset (arange_table + arange_table_in_use, 0,
11729               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11730     }
11731
11732   arange_table[arange_table_in_use++] = die;
11733 }
11734
11735 /* Output the information that goes into the .debug_aranges table.
11736    Namely, define the beginning and ending address range of the
11737    text section generated for this compilation unit.  */
11738
11739 static void
11740 output_aranges (void)
11741 {
11742   unsigned i;
11743   unsigned long aranges_length = size_of_aranges ();
11744
11745   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11746     dw2_asm_output_data (4, 0xffffffff,
11747       "Initial length escape value indicating 64-bit DWARF extension");
11748   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11749                        "Length of Address Ranges Info");
11750   /* Version number for aranges is still 2, even in DWARF3.  */
11751   dw2_asm_output_data (2, 2, "DWARF Version");
11752   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11753                          debug_info_section,
11754                          "Offset of Compilation Unit Info");
11755   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11756   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11757
11758   /* We need to align to twice the pointer size here.  */
11759   if (DWARF_ARANGES_PAD_SIZE)
11760     {
11761       /* Pad using a 2 byte words so that padding is correct for any
11762          pointer size.  */
11763       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11764                            2 * DWARF2_ADDR_SIZE);
11765       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11766         dw2_asm_output_data (2, 0, NULL);
11767     }
11768
11769   /* It is necessary not to output these entries if the sections were
11770      not used; if the sections were not used, the length will be 0 and
11771      the address may end up as 0 if the section is discarded by ld
11772      --gc-sections, leaving an invalid (0, 0) entry that can be
11773      confused with the terminator.  */
11774   if (text_section_used)
11775     {
11776       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11777       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11778                             text_section_label, "Length");
11779     }
11780   if (cold_text_section_used)
11781     {
11782       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11783                            "Address");
11784       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11785                             cold_text_section_label, "Length");
11786     }
11787
11788   for (i = 0; i < arange_table_in_use; i++)
11789     {
11790       dw_die_ref die = arange_table[i];
11791
11792       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11793       gcc_assert (die->die_mark);
11794
11795       if (die->die_tag == DW_TAG_subprogram)
11796         {
11797           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11798                                "Address");
11799           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11800                                 get_AT_low_pc (die), "Length");
11801         }
11802       else
11803         {
11804           /* A static variable; extract the symbol from DW_AT_location.
11805              Note that this code isn't currently hit, as we only emit
11806              aranges for functions (jason 9/23/99).  */
11807           dw_attr_ref a = get_AT (die, DW_AT_location);
11808           dw_loc_descr_ref loc;
11809
11810           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11811
11812           loc = AT_loc (a);
11813           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11814
11815           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11816                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11817           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11818                                get_AT_unsigned (die, DW_AT_byte_size),
11819                                "Length");
11820         }
11821     }
11822
11823   /* Output the terminator words.  */
11824   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11825   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11826 }
11827
11828 /* Add a new entry to .debug_ranges.  Return the offset at which it
11829    was placed.  */
11830
11831 static unsigned int
11832 add_ranges_num (int num)
11833 {
11834   unsigned int in_use = ranges_table_in_use;
11835
11836   if (in_use == ranges_table_allocated)
11837     {
11838       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11839       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11840                                     ranges_table_allocated);
11841       memset (ranges_table + ranges_table_in_use, 0,
11842               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11843     }
11844
11845   ranges_table[in_use].num = num;
11846   ranges_table_in_use = in_use + 1;
11847
11848   return in_use * 2 * DWARF2_ADDR_SIZE;
11849 }
11850
11851 /* Add a new entry to .debug_ranges corresponding to a block, or a
11852    range terminator if BLOCK is NULL.  */
11853
11854 static unsigned int
11855 add_ranges (const_tree block)
11856 {
11857   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11858 }
11859
11860 /* Add a new entry to .debug_ranges corresponding to a pair of
11861    labels.  */
11862
11863 static void
11864 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11865                       bool *added)
11866 {
11867   unsigned int in_use = ranges_by_label_in_use;
11868   unsigned int offset;
11869
11870   if (in_use == ranges_by_label_allocated)
11871     {
11872       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11873       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11874                                        ranges_by_label,
11875                                        ranges_by_label_allocated);
11876       memset (ranges_by_label + ranges_by_label_in_use, 0,
11877               RANGES_TABLE_INCREMENT
11878               * sizeof (struct dw_ranges_by_label_struct));
11879     }
11880
11881   ranges_by_label[in_use].begin = begin;
11882   ranges_by_label[in_use].end = end;
11883   ranges_by_label_in_use = in_use + 1;
11884
11885   offset = add_ranges_num (-(int)in_use - 1);
11886   if (!*added)
11887     {
11888       add_AT_range_list (die, DW_AT_ranges, offset);
11889       *added = true;
11890     }
11891 }
11892
11893 static void
11894 output_ranges (void)
11895 {
11896   unsigned i;
11897   static const char *const start_fmt = "Offset %#x";
11898   const char *fmt = start_fmt;
11899
11900   for (i = 0; i < ranges_table_in_use; i++)
11901     {
11902       int block_num = ranges_table[i].num;
11903
11904       if (block_num > 0)
11905         {
11906           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11907           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11908
11909           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11910           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11911
11912           /* If all code is in the text section, then the compilation
11913              unit base address defaults to DW_AT_low_pc, which is the
11914              base of the text section.  */
11915           if (!have_multiple_function_sections)
11916             {
11917               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11918                                     text_section_label,
11919                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11920               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11921                                     text_section_label, NULL);
11922             }
11923
11924           /* Otherwise, the compilation unit base address is zero,
11925              which allows us to use absolute addresses, and not worry
11926              about whether the target supports cross-section
11927              arithmetic.  */
11928           else
11929             {
11930               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11931                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11932               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11933             }
11934
11935           fmt = NULL;
11936         }
11937
11938       /* Negative block_num stands for an index into ranges_by_label.  */
11939       else if (block_num < 0)
11940         {
11941           int lab_idx = - block_num - 1;
11942
11943           if (!have_multiple_function_sections)
11944             {
11945               gcc_unreachable ();
11946 #if 0
11947               /* If we ever use add_ranges_by_labels () for a single
11948                  function section, all we have to do is to take out
11949                  the #if 0 above.  */
11950               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11951                                     ranges_by_label[lab_idx].begin,
11952                                     text_section_label,
11953                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11954               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11955                                     ranges_by_label[lab_idx].end,
11956                                     text_section_label, NULL);
11957 #endif
11958             }
11959           else
11960             {
11961               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11962                                    ranges_by_label[lab_idx].begin,
11963                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11964               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11965                                    ranges_by_label[lab_idx].end,
11966                                    NULL);
11967             }
11968         }
11969       else
11970         {
11971           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11972           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11973           fmt = start_fmt;
11974         }
11975     }
11976 }
11977
11978 /* Data structure containing information about input files.  */
11979 struct file_info
11980 {
11981   const char *path;     /* Complete file name.  */
11982   const char *fname;    /* File name part.  */
11983   int length;           /* Length of entire string.  */
11984   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11985   int dir_idx;          /* Index in directory table.  */
11986 };
11987
11988 /* Data structure containing information about directories with source
11989    files.  */
11990 struct dir_info
11991 {
11992   const char *path;     /* Path including directory name.  */
11993   int length;           /* Path length.  */
11994   int prefix;           /* Index of directory entry which is a prefix.  */
11995   int count;            /* Number of files in this directory.  */
11996   int dir_idx;          /* Index of directory used as base.  */
11997 };
11998
11999 /* Callback function for file_info comparison.  We sort by looking at
12000    the directories in the path.  */
12001
12002 static int
12003 file_info_cmp (const void *p1, const void *p2)
12004 {
12005   const struct file_info *const s1 = (const struct file_info *) p1;
12006   const struct file_info *const s2 = (const struct file_info *) p2;
12007   const unsigned char *cp1;
12008   const unsigned char *cp2;
12009
12010   /* Take care of file names without directories.  We need to make sure that
12011      we return consistent values to qsort since some will get confused if
12012      we return the same value when identical operands are passed in opposite
12013      orders.  So if neither has a directory, return 0 and otherwise return
12014      1 or -1 depending on which one has the directory.  */
12015   if ((s1->path == s1->fname || s2->path == s2->fname))
12016     return (s2->path == s2->fname) - (s1->path == s1->fname);
12017
12018   cp1 = (const unsigned char *) s1->path;
12019   cp2 = (const unsigned char *) s2->path;
12020
12021   while (1)
12022     {
12023       ++cp1;
12024       ++cp2;
12025       /* Reached the end of the first path?  If so, handle like above.  */
12026       if ((cp1 == (const unsigned char *) s1->fname)
12027           || (cp2 == (const unsigned char *) s2->fname))
12028         return ((cp2 == (const unsigned char *) s2->fname)
12029                 - (cp1 == (const unsigned char *) s1->fname));
12030
12031       /* Character of current path component the same?  */
12032       else if (*cp1 != *cp2)
12033         return *cp1 - *cp2;
12034     }
12035 }
12036
12037 struct file_name_acquire_data
12038 {
12039   struct file_info *files;
12040   int used_files;
12041   int max_files;
12042 };
12043
12044 /* Traversal function for the hash table.  */
12045
12046 static int
12047 file_name_acquire (void ** slot, void *data)
12048 {
12049   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12050   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12051   struct file_info *fi;
12052   const char *f;
12053
12054   gcc_assert (fnad->max_files >= d->emitted_number);
12055
12056   if (! d->emitted_number)
12057     return 1;
12058
12059   gcc_assert (fnad->max_files != fnad->used_files);
12060
12061   fi = fnad->files + fnad->used_files++;
12062
12063   /* Skip all leading "./".  */
12064   f = d->filename;
12065   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12066     f += 2;
12067
12068   /* Create a new array entry.  */
12069   fi->path = f;
12070   fi->length = strlen (f);
12071   fi->file_idx = d;
12072
12073   /* Search for the file name part.  */
12074   f = strrchr (f, DIR_SEPARATOR);
12075 #if defined (DIR_SEPARATOR_2)
12076   {
12077     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12078
12079     if (g != NULL)
12080       {
12081         if (f == NULL || f < g)
12082           f = g;
12083       }
12084   }
12085 #endif
12086
12087   fi->fname = f == NULL ? fi->path : f + 1;
12088   return 1;
12089 }
12090
12091 /* Output the directory table and the file name table.  We try to minimize
12092    the total amount of memory needed.  A heuristic is used to avoid large
12093    slowdowns with many input files.  */
12094
12095 static void
12096 output_file_names (void)
12097 {
12098   struct file_name_acquire_data fnad;
12099   int numfiles;
12100   struct file_info *files;
12101   struct dir_info *dirs;
12102   int *saved;
12103   int *savehere;
12104   int *backmap;
12105   int ndirs;
12106   int idx_offset;
12107   int i;
12108
12109   if (!last_emitted_file)
12110     {
12111       dw2_asm_output_data (1, 0, "End directory table");
12112       dw2_asm_output_data (1, 0, "End file name table");
12113       return;
12114     }
12115
12116   numfiles = last_emitted_file->emitted_number;
12117
12118   /* Allocate the various arrays we need.  */
12119   files = XALLOCAVEC (struct file_info, numfiles);
12120   dirs = XALLOCAVEC (struct dir_info, numfiles);
12121
12122   fnad.files = files;
12123   fnad.used_files = 0;
12124   fnad.max_files = numfiles;
12125   htab_traverse (file_table, file_name_acquire, &fnad);
12126   gcc_assert (fnad.used_files == fnad.max_files);
12127
12128   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12129
12130   /* Find all the different directories used.  */
12131   dirs[0].path = files[0].path;
12132   dirs[0].length = files[0].fname - files[0].path;
12133   dirs[0].prefix = -1;
12134   dirs[0].count = 1;
12135   dirs[0].dir_idx = 0;
12136   files[0].dir_idx = 0;
12137   ndirs = 1;
12138
12139   for (i = 1; i < numfiles; i++)
12140     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12141         && memcmp (dirs[ndirs - 1].path, files[i].path,
12142                    dirs[ndirs - 1].length) == 0)
12143       {
12144         /* Same directory as last entry.  */
12145         files[i].dir_idx = ndirs - 1;
12146         ++dirs[ndirs - 1].count;
12147       }
12148     else
12149       {
12150         int j;
12151
12152         /* This is a new directory.  */
12153         dirs[ndirs].path = files[i].path;
12154         dirs[ndirs].length = files[i].fname - files[i].path;
12155         dirs[ndirs].count = 1;
12156         dirs[ndirs].dir_idx = ndirs;
12157         files[i].dir_idx = ndirs;
12158
12159         /* Search for a prefix.  */
12160         dirs[ndirs].prefix = -1;
12161         for (j = 0; j < ndirs; j++)
12162           if (dirs[j].length < dirs[ndirs].length
12163               && dirs[j].length > 1
12164               && (dirs[ndirs].prefix == -1
12165                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12166               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12167             dirs[ndirs].prefix = j;
12168
12169         ++ndirs;
12170       }
12171
12172   /* Now to the actual work.  We have to find a subset of the directories which
12173      allow expressing the file name using references to the directory table
12174      with the least amount of characters.  We do not do an exhaustive search
12175      where we would have to check out every combination of every single
12176      possible prefix.  Instead we use a heuristic which provides nearly optimal
12177      results in most cases and never is much off.  */
12178   saved = XALLOCAVEC (int, ndirs);
12179   savehere = XALLOCAVEC (int, ndirs);
12180
12181   memset (saved, '\0', ndirs * sizeof (saved[0]));
12182   for (i = 0; i < ndirs; i++)
12183     {
12184       int j;
12185       int total;
12186
12187       /* We can always save some space for the current directory.  But this
12188          does not mean it will be enough to justify adding the directory.  */
12189       savehere[i] = dirs[i].length;
12190       total = (savehere[i] - saved[i]) * dirs[i].count;
12191
12192       for (j = i + 1; j < ndirs; j++)
12193         {
12194           savehere[j] = 0;
12195           if (saved[j] < dirs[i].length)
12196             {
12197               /* Determine whether the dirs[i] path is a prefix of the
12198                  dirs[j] path.  */
12199               int k;
12200
12201               k = dirs[j].prefix;
12202               while (k != -1 && k != (int) i)
12203                 k = dirs[k].prefix;
12204
12205               if (k == (int) i)
12206                 {
12207                   /* Yes it is.  We can possibly save some memory by
12208                      writing the filenames in dirs[j] relative to
12209                      dirs[i].  */
12210                   savehere[j] = dirs[i].length;
12211                   total += (savehere[j] - saved[j]) * dirs[j].count;
12212                 }
12213             }
12214         }
12215
12216       /* Check whether we can save enough to justify adding the dirs[i]
12217          directory.  */
12218       if (total > dirs[i].length + 1)
12219         {
12220           /* It's worthwhile adding.  */
12221           for (j = i; j < ndirs; j++)
12222             if (savehere[j] > 0)
12223               {
12224                 /* Remember how much we saved for this directory so far.  */
12225                 saved[j] = savehere[j];
12226
12227                 /* Remember the prefix directory.  */
12228                 dirs[j].dir_idx = i;
12229               }
12230         }
12231     }
12232
12233   /* Emit the directory name table.  */
12234   idx_offset = dirs[0].length > 0 ? 1 : 0;
12235   for (i = 1 - idx_offset; i < ndirs; i++)
12236     dw2_asm_output_nstring (dirs[i].path,
12237                             dirs[i].length
12238                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12239                             "Directory Entry: %#x", i + idx_offset);
12240
12241   dw2_asm_output_data (1, 0, "End directory table");
12242
12243   /* We have to emit them in the order of emitted_number since that's
12244      used in the debug info generation.  To do this efficiently we
12245      generate a back-mapping of the indices first.  */
12246   backmap = XALLOCAVEC (int, numfiles);
12247   for (i = 0; i < numfiles; i++)
12248     backmap[files[i].file_idx->emitted_number - 1] = i;
12249
12250   /* Now write all the file names.  */
12251   for (i = 0; i < numfiles; i++)
12252     {
12253       int file_idx = backmap[i];
12254       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12255
12256 #ifdef VMS_DEBUGGING_INFO
12257 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12258
12259       /* Setting these fields can lead to debugger miscomparisons,
12260          but VMS Debug requires them to be set correctly.  */
12261
12262       int ver;
12263       long long cdt;
12264       long siz;
12265       int maxfilelen = strlen (files[file_idx].path)
12266                                + dirs[dir_idx].length
12267                                + MAX_VMS_VERSION_LEN + 1;
12268       char *filebuf = XALLOCAVEC (char, maxfilelen);
12269
12270       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12271       snprintf (filebuf, maxfilelen, "%s;%d",
12272                 files[file_idx].path + dirs[dir_idx].length, ver);
12273
12274       dw2_asm_output_nstring
12275         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12276
12277       /* Include directory index.  */
12278       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12279
12280       /* Modification time.  */
12281       dw2_asm_output_data_uleb128
12282         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12283           ? cdt : 0,
12284          NULL);
12285
12286       /* File length in bytes.  */
12287       dw2_asm_output_data_uleb128
12288         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12289           ? siz : 0,
12290          NULL);
12291 #else
12292       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12293                               "File Entry: %#x", (unsigned) i + 1);
12294
12295       /* Include directory index.  */
12296       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12297
12298       /* Modification time.  */
12299       dw2_asm_output_data_uleb128 (0, NULL);
12300
12301       /* File length in bytes.  */
12302       dw2_asm_output_data_uleb128 (0, NULL);
12303 #endif /* VMS_DEBUGGING_INFO */
12304     }
12305
12306   dw2_asm_output_data (1, 0, "End file name table");
12307 }
12308
12309
12310 /* Output the source line number correspondence information.  This
12311    information goes into the .debug_line section.  */
12312
12313 static void
12314 output_line_info (void)
12315 {
12316   char l1[20], l2[20], p1[20], p2[20];
12317   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12318   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12319   unsigned opc;
12320   unsigned n_op_args;
12321   unsigned long lt_index;
12322   unsigned long current_line;
12323   long line_offset;
12324   long line_delta;
12325   unsigned long current_file;
12326   unsigned long function;
12327   int ver = dwarf_version;
12328
12329   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12330   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12331   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12332   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12333
12334   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12335     dw2_asm_output_data (4, 0xffffffff,
12336       "Initial length escape value indicating 64-bit DWARF extension");
12337   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12338                         "Length of Source Line Info");
12339   ASM_OUTPUT_LABEL (asm_out_file, l1);
12340
12341   dw2_asm_output_data (2, ver, "DWARF Version");
12342   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12343   ASM_OUTPUT_LABEL (asm_out_file, p1);
12344
12345   /* Define the architecture-dependent minimum instruction length (in
12346    bytes).  In this implementation of DWARF, this field is used for
12347    information purposes only.  Since GCC generates assembly language,
12348    we have no a priori knowledge of how many instruction bytes are
12349    generated for each source line, and therefore can use only the
12350    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12351    commands.  Accordingly, we fix this as `1', which is "correct
12352    enough" for all architectures, and don't let the target override.  */
12353   dw2_asm_output_data (1, 1,
12354                        "Minimum Instruction Length");
12355
12356   if (ver >= 4)
12357     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12358                          "Maximum Operations Per Instruction");
12359   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12360                        "Default is_stmt_start flag");
12361   dw2_asm_output_data (1, DWARF_LINE_BASE,
12362                        "Line Base Value (Special Opcodes)");
12363   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12364                        "Line Range Value (Special Opcodes)");
12365   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12366                        "Special Opcode Base");
12367
12368   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12369     {
12370       switch (opc)
12371         {
12372         case DW_LNS_advance_pc:
12373         case DW_LNS_advance_line:
12374         case DW_LNS_set_file:
12375         case DW_LNS_set_column:
12376         case DW_LNS_fixed_advance_pc:
12377           n_op_args = 1;
12378           break;
12379         default:
12380           n_op_args = 0;
12381           break;
12382         }
12383
12384       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12385                            opc, n_op_args);
12386     }
12387
12388   /* Write out the information about the files we use.  */
12389   output_file_names ();
12390   ASM_OUTPUT_LABEL (asm_out_file, p2);
12391
12392   /* We used to set the address register to the first location in the text
12393      section here, but that didn't accomplish anything since we already
12394      have a line note for the opening brace of the first function.  */
12395
12396   /* Generate the line number to PC correspondence table, encoded as
12397      a series of state machine operations.  */
12398   current_file = 1;
12399   current_line = 1;
12400
12401   if (cfun && in_cold_section_p)
12402     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12403   else
12404     strcpy (prev_line_label, text_section_label);
12405   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12406     {
12407       dw_line_info_ref line_info = &line_info_table[lt_index];
12408
12409 #if 0
12410       /* Disable this optimization for now; GDB wants to see two line notes
12411          at the beginning of a function so it can find the end of the
12412          prologue.  */
12413
12414       /* Don't emit anything for redundant notes.  Just updating the
12415          address doesn't accomplish anything, because we already assume
12416          that anything after the last address is this line.  */
12417       if (line_info->dw_line_num == current_line
12418           && line_info->dw_file_num == current_file)
12419         continue;
12420 #endif
12421
12422       /* Emit debug info for the address of the current line.
12423
12424          Unfortunately, we have little choice here currently, and must always
12425          use the most general form.  GCC does not know the address delta
12426          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12427          attributes which will give an upper bound on the address range.  We
12428          could perhaps use length attributes to determine when it is safe to
12429          use DW_LNS_fixed_advance_pc.  */
12430
12431       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12432       if (0)
12433         {
12434           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12435           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12436                                "DW_LNS_fixed_advance_pc");
12437           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12438         }
12439       else
12440         {
12441           /* This can handle any delta.  This takes
12442              4+DWARF2_ADDR_SIZE bytes.  */
12443           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12444           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12445           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12446           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12447         }
12448
12449       strcpy (prev_line_label, line_label);
12450
12451       /* Emit debug info for the source file of the current line, if
12452          different from the previous line.  */
12453       if (line_info->dw_file_num != current_file)
12454         {
12455           current_file = line_info->dw_file_num;
12456           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12457           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12458         }
12459
12460       /* Emit debug info for the current line number, choosing the encoding
12461          that uses the least amount of space.  */
12462       if (line_info->dw_line_num != current_line)
12463         {
12464           line_offset = line_info->dw_line_num - current_line;
12465           line_delta = line_offset - DWARF_LINE_BASE;
12466           current_line = line_info->dw_line_num;
12467           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12468             /* This can handle deltas from -10 to 234, using the current
12469                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12470                takes 1 byte.  */
12471             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12472                                  "line %lu", current_line);
12473           else
12474             {
12475               /* This can handle any delta.  This takes at least 4 bytes,
12476                  depending on the value being encoded.  */
12477               dw2_asm_output_data (1, DW_LNS_advance_line,
12478                                    "advance to line %lu", current_line);
12479               dw2_asm_output_data_sleb128 (line_offset, NULL);
12480               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12481             }
12482         }
12483       else
12484         /* We still need to start a new row, so output a copy insn.  */
12485         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12486     }
12487
12488   /* Emit debug info for the address of the end of the function.  */
12489   if (0)
12490     {
12491       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12492                            "DW_LNS_fixed_advance_pc");
12493       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12494     }
12495   else
12496     {
12497       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12498       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12499       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12500       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12501     }
12502
12503   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12504   dw2_asm_output_data_uleb128 (1, NULL);
12505   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12506
12507   function = 0;
12508   current_file = 1;
12509   current_line = 1;
12510   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12511     {
12512       dw_separate_line_info_ref line_info
12513         = &separate_line_info_table[lt_index];
12514
12515 #if 0
12516       /* Don't emit anything for redundant notes.  */
12517       if (line_info->dw_line_num == current_line
12518           && line_info->dw_file_num == current_file
12519           && line_info->function == function)
12520         goto cont;
12521 #endif
12522
12523       /* Emit debug info for the address of the current line.  If this is
12524          a new function, or the first line of a function, then we need
12525          to handle it differently.  */
12526       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12527                                    lt_index);
12528       if (function != line_info->function)
12529         {
12530           function = line_info->function;
12531
12532           /* Set the address register to the first line in the function.  */
12533           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12534           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12535           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12536           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12537         }
12538       else
12539         {
12540           /* ??? See the DW_LNS_advance_pc comment above.  */
12541           if (0)
12542             {
12543               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12544                                    "DW_LNS_fixed_advance_pc");
12545               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12546             }
12547           else
12548             {
12549               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12550               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12551               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12552               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12553             }
12554         }
12555
12556       strcpy (prev_line_label, line_label);
12557
12558       /* Emit debug info for the source file of the current line, if
12559          different from the previous line.  */
12560       if (line_info->dw_file_num != current_file)
12561         {
12562           current_file = line_info->dw_file_num;
12563           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12564           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12565         }
12566
12567       /* Emit debug info for the current line number, choosing the encoding
12568          that uses the least amount of space.  */
12569       if (line_info->dw_line_num != current_line)
12570         {
12571           line_offset = line_info->dw_line_num - current_line;
12572           line_delta = line_offset - DWARF_LINE_BASE;
12573           current_line = line_info->dw_line_num;
12574           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12575             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12576                                  "line %lu", current_line);
12577           else
12578             {
12579               dw2_asm_output_data (1, DW_LNS_advance_line,
12580                                    "advance to line %lu", current_line);
12581               dw2_asm_output_data_sleb128 (line_offset, NULL);
12582               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12583             }
12584         }
12585       else
12586         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12587
12588 #if 0
12589     cont:
12590 #endif
12591
12592       lt_index++;
12593
12594       /* If we're done with a function, end its sequence.  */
12595       if (lt_index == separate_line_info_table_in_use
12596           || separate_line_info_table[lt_index].function != function)
12597         {
12598           current_file = 1;
12599           current_line = 1;
12600
12601           /* Emit debug info for the address of the end of the function.  */
12602           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12603           if (0)
12604             {
12605               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12606                                    "DW_LNS_fixed_advance_pc");
12607               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12608             }
12609           else
12610             {
12611               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12612               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12613               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12614               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12615             }
12616
12617           /* Output the marker for the end of this sequence.  */
12618           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12619           dw2_asm_output_data_uleb128 (1, NULL);
12620           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12621         }
12622     }
12623
12624   /* Output the marker for the end of the line number info.  */
12625   ASM_OUTPUT_LABEL (asm_out_file, l2);
12626 }
12627 \f
12628 /* Given a pointer to a tree node for some base type, return a pointer to
12629    a DIE that describes the given type.
12630
12631    This routine must only be called for GCC type nodes that correspond to
12632    Dwarf base (fundamental) types.  */
12633
12634 static dw_die_ref
12635 base_type_die (tree type)
12636 {
12637   dw_die_ref base_type_result;
12638   enum dwarf_type encoding;
12639
12640   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12641     return 0;
12642
12643   /* If this is a subtype that should not be emitted as a subrange type,
12644      use the base type.  See subrange_type_for_debug_p.  */
12645   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12646     type = TREE_TYPE (type);
12647
12648   switch (TREE_CODE (type))
12649     {
12650     case INTEGER_TYPE:
12651       if ((dwarf_version >= 4 || !dwarf_strict)
12652           && TYPE_NAME (type)
12653           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12654           && DECL_IS_BUILTIN (TYPE_NAME (type))
12655           && DECL_NAME (TYPE_NAME (type)))
12656         {
12657           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12658           if (strcmp (name, "char16_t") == 0
12659               || strcmp (name, "char32_t") == 0)
12660             {
12661               encoding = DW_ATE_UTF;
12662               break;
12663             }
12664         }
12665       if (TYPE_STRING_FLAG (type))
12666         {
12667           if (TYPE_UNSIGNED (type))
12668             encoding = DW_ATE_unsigned_char;
12669           else
12670             encoding = DW_ATE_signed_char;
12671         }
12672       else if (TYPE_UNSIGNED (type))
12673         encoding = DW_ATE_unsigned;
12674       else
12675         encoding = DW_ATE_signed;
12676       break;
12677
12678     case REAL_TYPE:
12679       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12680         {
12681           if (dwarf_version >= 3 || !dwarf_strict)
12682             encoding = DW_ATE_decimal_float;
12683           else
12684             encoding = DW_ATE_lo_user;
12685         }
12686       else
12687         encoding = DW_ATE_float;
12688       break;
12689
12690     case FIXED_POINT_TYPE:
12691       if (!(dwarf_version >= 3 || !dwarf_strict))
12692         encoding = DW_ATE_lo_user;
12693       else if (TYPE_UNSIGNED (type))
12694         encoding = DW_ATE_unsigned_fixed;
12695       else
12696         encoding = DW_ATE_signed_fixed;
12697       break;
12698
12699       /* Dwarf2 doesn't know anything about complex ints, so use
12700          a user defined type for it.  */
12701     case COMPLEX_TYPE:
12702       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12703         encoding = DW_ATE_complex_float;
12704       else
12705         encoding = DW_ATE_lo_user;
12706       break;
12707
12708     case BOOLEAN_TYPE:
12709       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12710       encoding = DW_ATE_boolean;
12711       break;
12712
12713     default:
12714       /* No other TREE_CODEs are Dwarf fundamental types.  */
12715       gcc_unreachable ();
12716     }
12717
12718   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12719
12720   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12721                    int_size_in_bytes (type));
12722   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12723
12724   return base_type_result;
12725 }
12726
12727 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12728    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12729
12730 static inline int
12731 is_base_type (tree type)
12732 {
12733   switch (TREE_CODE (type))
12734     {
12735     case ERROR_MARK:
12736     case VOID_TYPE:
12737     case INTEGER_TYPE:
12738     case REAL_TYPE:
12739     case FIXED_POINT_TYPE:
12740     case COMPLEX_TYPE:
12741     case BOOLEAN_TYPE:
12742       return 1;
12743
12744     case ARRAY_TYPE:
12745     case RECORD_TYPE:
12746     case UNION_TYPE:
12747     case QUAL_UNION_TYPE:
12748     case ENUMERAL_TYPE:
12749     case FUNCTION_TYPE:
12750     case METHOD_TYPE:
12751     case POINTER_TYPE:
12752     case REFERENCE_TYPE:
12753     case NULLPTR_TYPE:
12754     case OFFSET_TYPE:
12755     case LANG_TYPE:
12756     case VECTOR_TYPE:
12757       return 0;
12758
12759     default:
12760       gcc_unreachable ();
12761     }
12762
12763   return 0;
12764 }
12765
12766 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12767    node, return the size in bits for the type if it is a constant, or else
12768    return the alignment for the type if the type's size is not constant, or
12769    else return BITS_PER_WORD if the type actually turns out to be an
12770    ERROR_MARK node.  */
12771
12772 static inline unsigned HOST_WIDE_INT
12773 simple_type_size_in_bits (const_tree type)
12774 {
12775   if (TREE_CODE (type) == ERROR_MARK)
12776     return BITS_PER_WORD;
12777   else if (TYPE_SIZE (type) == NULL_TREE)
12778     return 0;
12779   else if (host_integerp (TYPE_SIZE (type), 1))
12780     return tree_low_cst (TYPE_SIZE (type), 1);
12781   else
12782     return TYPE_ALIGN (type);
12783 }
12784
12785 /* Similarly, but return a double_int instead of UHWI.  */
12786
12787 static inline double_int
12788 double_int_type_size_in_bits (const_tree type)
12789 {
12790   if (TREE_CODE (type) == ERROR_MARK)
12791     return uhwi_to_double_int (BITS_PER_WORD);
12792   else if (TYPE_SIZE (type) == NULL_TREE)
12793     return double_int_zero;
12794   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12795     return tree_to_double_int (TYPE_SIZE (type));
12796   else
12797     return uhwi_to_double_int (TYPE_ALIGN (type));
12798 }
12799
12800 /*  Given a pointer to a tree node for a subrange type, return a pointer
12801     to a DIE that describes the given type.  */
12802
12803 static dw_die_ref
12804 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12805 {
12806   dw_die_ref subrange_die;
12807   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12808
12809   if (context_die == NULL)
12810     context_die = comp_unit_die ();
12811
12812   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12813
12814   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12815     {
12816       /* The size of the subrange type and its base type do not match,
12817          so we need to generate a size attribute for the subrange type.  */
12818       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12819     }
12820
12821   if (low)
12822     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12823   if (high)
12824     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12825
12826   return subrange_die;
12827 }
12828
12829 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12830    entry that chains various modifiers in front of the given type.  */
12831
12832 static dw_die_ref
12833 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12834                    dw_die_ref context_die)
12835 {
12836   enum tree_code code = TREE_CODE (type);
12837   dw_die_ref mod_type_die;
12838   dw_die_ref sub_die = NULL;
12839   tree item_type = NULL;
12840   tree qualified_type;
12841   tree name, low, high;
12842
12843   if (code == ERROR_MARK)
12844     return NULL;
12845
12846   /* See if we already have the appropriately qualified variant of
12847      this type.  */
12848   qualified_type
12849     = get_qualified_type (type,
12850                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12851                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12852
12853   if (qualified_type == sizetype
12854       && TYPE_NAME (qualified_type)
12855       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12856     {
12857       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12858
12859       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12860                            && TYPE_PRECISION (t)
12861                            == TYPE_PRECISION (qualified_type)
12862                            && TYPE_UNSIGNED (t)
12863                            == TYPE_UNSIGNED (qualified_type));
12864       qualified_type = t;
12865     }
12866
12867   /* If we do, then we can just use its DIE, if it exists.  */
12868   if (qualified_type)
12869     {
12870       mod_type_die = lookup_type_die (qualified_type);
12871       if (mod_type_die)
12872         return mod_type_die;
12873     }
12874
12875   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12876
12877   /* Handle C typedef types.  */
12878   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12879       && !DECL_ARTIFICIAL (name))
12880     {
12881       tree dtype = TREE_TYPE (name);
12882
12883       if (qualified_type == dtype)
12884         {
12885           /* For a named type, use the typedef.  */
12886           gen_type_die (qualified_type, context_die);
12887           return lookup_type_die (qualified_type);
12888         }
12889       else if (is_const_type < TYPE_READONLY (dtype)
12890                || is_volatile_type < TYPE_VOLATILE (dtype)
12891                || (is_const_type <= TYPE_READONLY (dtype)
12892                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12893                    && DECL_ORIGINAL_TYPE (name) != type))
12894         /* cv-unqualified version of named type.  Just use the unnamed
12895            type to which it refers.  */
12896         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12897                                   is_const_type, is_volatile_type,
12898                                   context_die);
12899       /* Else cv-qualified version of named type; fall through.  */
12900     }
12901
12902   if (is_const_type
12903       /* If both is_const_type and is_volatile_type, prefer the path
12904          which leads to a qualified type.  */
12905       && (!is_volatile_type
12906           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12907           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12908     {
12909       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12910       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12911     }
12912   else if (is_volatile_type)
12913     {
12914       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12915       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12916     }
12917   else if (code == POINTER_TYPE)
12918     {
12919       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12920       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12921                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12922       item_type = TREE_TYPE (type);
12923       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12924         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12925                          TYPE_ADDR_SPACE (item_type));
12926     }
12927   else if (code == REFERENCE_TYPE)
12928     {
12929       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12930         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12931                                 type);
12932       else
12933         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12934       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12935                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12936       item_type = TREE_TYPE (type);
12937       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12938         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12939                          TYPE_ADDR_SPACE (item_type));
12940     }
12941   else if (code == INTEGER_TYPE
12942            && TREE_TYPE (type) != NULL_TREE
12943            && subrange_type_for_debug_p (type, &low, &high))
12944     {
12945       mod_type_die = subrange_type_die (type, low, high, context_die);
12946       item_type = TREE_TYPE (type);
12947     }
12948   else if (is_base_type (type))
12949     mod_type_die = base_type_die (type);
12950   else
12951     {
12952       gen_type_die (type, context_die);
12953
12954       /* We have to get the type_main_variant here (and pass that to the
12955          `lookup_type_die' routine) because the ..._TYPE node we have
12956          might simply be a *copy* of some original type node (where the
12957          copy was created to help us keep track of typedef names) and
12958          that copy might have a different TYPE_UID from the original
12959          ..._TYPE node.  */
12960       if (TREE_CODE (type) != VECTOR_TYPE)
12961         return lookup_type_die (type_main_variant (type));
12962       else
12963         /* Vectors have the debugging information in the type,
12964            not the main variant.  */
12965         return lookup_type_die (type);
12966     }
12967
12968   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12969      don't output a DW_TAG_typedef, since there isn't one in the
12970      user's program; just attach a DW_AT_name to the type.
12971      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12972      if the base type already has the same name.  */
12973   if (name
12974       && ((TREE_CODE (name) != TYPE_DECL
12975            && (qualified_type == TYPE_MAIN_VARIANT (type)
12976                || (!is_const_type && !is_volatile_type)))
12977           || (TREE_CODE (name) == TYPE_DECL
12978               && TREE_TYPE (name) == qualified_type
12979               && DECL_NAME (name))))
12980     {
12981       if (TREE_CODE (name) == TYPE_DECL)
12982         /* Could just call add_name_and_src_coords_attributes here,
12983            but since this is a builtin type it doesn't have any
12984            useful source coordinates anyway.  */
12985         name = DECL_NAME (name);
12986       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12987     }
12988   /* This probably indicates a bug.  */
12989   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12990     add_name_attribute (mod_type_die, "__unknown__");
12991
12992   if (qualified_type)
12993     equate_type_number_to_die (qualified_type, mod_type_die);
12994
12995   if (item_type)
12996     /* We must do this after the equate_type_number_to_die call, in case
12997        this is a recursive type.  This ensures that the modified_type_die
12998        recursion will terminate even if the type is recursive.  Recursive
12999        types are possible in Ada.  */
13000     sub_die = modified_type_die (item_type,
13001                                  TYPE_READONLY (item_type),
13002                                  TYPE_VOLATILE (item_type),
13003                                  context_die);
13004
13005   if (sub_die != NULL)
13006     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13007
13008   return mod_type_die;
13009 }
13010
13011 /* Generate DIEs for the generic parameters of T.
13012    T must be either a generic type or a generic function.
13013    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13014
13015 static void
13016 gen_generic_params_dies (tree t)
13017 {
13018   tree parms, args;
13019   int parms_num, i;
13020   dw_die_ref die = NULL;
13021
13022   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13023     return;
13024
13025   if (TYPE_P (t))
13026     die = lookup_type_die (t);
13027   else if (DECL_P (t))
13028     die = lookup_decl_die (t);
13029
13030   gcc_assert (die);
13031
13032   parms = lang_hooks.get_innermost_generic_parms (t);
13033   if (!parms)
13034     /* T has no generic parameter. It means T is neither a generic type
13035        or function. End of story.  */
13036     return;
13037
13038   parms_num = TREE_VEC_LENGTH (parms);
13039   args = lang_hooks.get_innermost_generic_args (t);
13040   for (i = 0; i < parms_num; i++)
13041     {
13042       tree parm, arg, arg_pack_elems;
13043
13044       parm = TREE_VEC_ELT (parms, i);
13045       arg = TREE_VEC_ELT (args, i);
13046       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13047       gcc_assert (parm && TREE_VALUE (parm) && arg);
13048
13049       if (parm && TREE_VALUE (parm) && arg)
13050         {
13051           /* If PARM represents a template parameter pack,
13052              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13053              by DW_TAG_template_*_parameter DIEs for the argument
13054              pack elements of ARG. Note that ARG would then be
13055              an argument pack.  */
13056           if (arg_pack_elems)
13057             template_parameter_pack_die (TREE_VALUE (parm),
13058                                          arg_pack_elems,
13059                                          die);
13060           else
13061             generic_parameter_die (TREE_VALUE (parm), arg,
13062                                    true /* Emit DW_AT_name */, die);
13063         }
13064     }
13065 }
13066
13067 /* Create and return a DIE for PARM which should be
13068    the representation of a generic type parameter.
13069    For instance, in the C++ front end, PARM would be a template parameter.
13070    ARG is the argument to PARM.
13071    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13072    name of the PARM.
13073    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13074    as a child node.  */
13075
13076 static dw_die_ref
13077 generic_parameter_die (tree parm, tree arg,
13078                        bool emit_name_p,
13079                        dw_die_ref parent_die)
13080 {
13081   dw_die_ref tmpl_die = NULL;
13082   const char *name = NULL;
13083
13084   if (!parm || !DECL_NAME (parm) || !arg)
13085     return NULL;
13086
13087   /* We support non-type generic parameters and arguments,
13088      type generic parameters and arguments, as well as
13089      generic generic parameters (a.k.a. template template parameters in C++)
13090      and arguments.  */
13091   if (TREE_CODE (parm) == PARM_DECL)
13092     /* PARM is a nontype generic parameter  */
13093     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13094   else if (TREE_CODE (parm) == TYPE_DECL)
13095     /* PARM is a type generic parameter.  */
13096     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13097   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13098     /* PARM is a generic generic parameter.
13099        Its DIE is a GNU extension. It shall have a
13100        DW_AT_name attribute to represent the name of the template template
13101        parameter, and a DW_AT_GNU_template_name attribute to represent the
13102        name of the template template argument.  */
13103     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13104                         parent_die, parm);
13105   else
13106     gcc_unreachable ();
13107
13108   if (tmpl_die)
13109     {
13110       tree tmpl_type;
13111
13112       /* If PARM is a generic parameter pack, it means we are
13113          emitting debug info for a template argument pack element.
13114          In other terms, ARG is a template argument pack element.
13115          In that case, we don't emit any DW_AT_name attribute for
13116          the die.  */
13117       if (emit_name_p)
13118         {
13119           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13120           gcc_assert (name);
13121           add_AT_string (tmpl_die, DW_AT_name, name);
13122         }
13123
13124       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13125         {
13126           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13127              TMPL_DIE should have a child DW_AT_type attribute that is set
13128              to the type of the argument to PARM, which is ARG.
13129              If PARM is a type generic parameter, TMPL_DIE should have a
13130              child DW_AT_type that is set to ARG.  */
13131           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13132           add_type_attribute (tmpl_die, tmpl_type, 0,
13133                               TREE_THIS_VOLATILE (tmpl_type),
13134                               parent_die);
13135         }
13136       else
13137         {
13138           /* So TMPL_DIE is a DIE representing a
13139              a generic generic template parameter, a.k.a template template
13140              parameter in C++ and arg is a template.  */
13141
13142           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13143              to the name of the argument.  */
13144           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13145           if (name)
13146             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13147         }
13148
13149       if (TREE_CODE (parm) == PARM_DECL)
13150         /* So PARM is a non-type generic parameter.
13151            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13152            attribute of TMPL_DIE which value represents the value
13153            of ARG.
13154            We must be careful here:
13155            The value of ARG might reference some function decls.
13156            We might currently be emitting debug info for a generic
13157            type and types are emitted before function decls, we don't
13158            know if the function decls referenced by ARG will actually be
13159            emitted after cgraph computations.
13160            So must defer the generation of the DW_AT_const_value to
13161            after cgraph is ready.  */
13162         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13163     }
13164
13165   return tmpl_die;
13166 }
13167
13168 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13169    PARM_PACK must be a template parameter pack. The returned DIE
13170    will be child DIE of PARENT_DIE.  */
13171
13172 static dw_die_ref
13173 template_parameter_pack_die (tree parm_pack,
13174                              tree parm_pack_args,
13175                              dw_die_ref parent_die)
13176 {
13177   dw_die_ref die;
13178   int j;
13179
13180   gcc_assert (parent_die && parm_pack);
13181
13182   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13183   add_name_and_src_coords_attributes (die, parm_pack);
13184   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13185     generic_parameter_die (parm_pack,
13186                            TREE_VEC_ELT (parm_pack_args, j),
13187                            false /* Don't emit DW_AT_name */,
13188                            die);
13189   return die;
13190 }
13191
13192 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13193    an enumerated type.  */
13194
13195 static inline int
13196 type_is_enum (const_tree type)
13197 {
13198   return TREE_CODE (type) == ENUMERAL_TYPE;
13199 }
13200
13201 /* Return the DBX register number described by a given RTL node.  */
13202
13203 static unsigned int
13204 dbx_reg_number (const_rtx rtl)
13205 {
13206   unsigned regno = REGNO (rtl);
13207
13208   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13209
13210 #ifdef LEAF_REG_REMAP
13211   if (current_function_uses_only_leaf_regs)
13212     {
13213       int leaf_reg = LEAF_REG_REMAP (regno);
13214       if (leaf_reg != -1)
13215         regno = (unsigned) leaf_reg;
13216     }
13217 #endif
13218
13219   return DBX_REGISTER_NUMBER (regno);
13220 }
13221
13222 /* Optionally add a DW_OP_piece term to a location description expression.
13223    DW_OP_piece is only added if the location description expression already
13224    doesn't end with DW_OP_piece.  */
13225
13226 static void
13227 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13228 {
13229   dw_loc_descr_ref loc;
13230
13231   if (*list_head != NULL)
13232     {
13233       /* Find the end of the chain.  */
13234       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13235         ;
13236
13237       if (loc->dw_loc_opc != DW_OP_piece)
13238         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13239     }
13240 }
13241
13242 /* Return a location descriptor that designates a machine register or
13243    zero if there is none.  */
13244
13245 static dw_loc_descr_ref
13246 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13247 {
13248   rtx regs;
13249
13250   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13251     return 0;
13252
13253   /* We only use "frame base" when we're sure we're talking about the
13254      post-prologue local stack frame.  We do this by *not* running
13255      register elimination until this point, and recognizing the special
13256      argument pointer and soft frame pointer rtx's.
13257      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13258   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13259       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13260     {
13261       dw_loc_descr_ref result = NULL;
13262
13263       if (dwarf_version >= 4 || !dwarf_strict)
13264         {
13265           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13266           if (result)
13267             add_loc_descr (&result,
13268                            new_loc_descr (DW_OP_stack_value, 0, 0));
13269         }
13270       return result;
13271     }
13272
13273   regs = targetm.dwarf_register_span (rtl);
13274
13275   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13276     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13277   else
13278     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13279 }
13280
13281 /* Return a location descriptor that designates a machine register for
13282    a given hard register number.  */
13283
13284 static dw_loc_descr_ref
13285 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13286 {
13287   dw_loc_descr_ref reg_loc_descr;
13288
13289   if (regno <= 31)
13290     reg_loc_descr
13291       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13292   else
13293     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13294
13295   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13296     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13297
13298   return reg_loc_descr;
13299 }
13300
13301 /* Given an RTL of a register, return a location descriptor that
13302    designates a value that spans more than one register.  */
13303
13304 static dw_loc_descr_ref
13305 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13306                              enum var_init_status initialized)
13307 {
13308   int nregs, size, i;
13309   unsigned reg;
13310   dw_loc_descr_ref loc_result = NULL;
13311
13312   reg = REGNO (rtl);
13313 #ifdef LEAF_REG_REMAP
13314   if (current_function_uses_only_leaf_regs)
13315     {
13316       int leaf_reg = LEAF_REG_REMAP (reg);
13317       if (leaf_reg != -1)
13318         reg = (unsigned) leaf_reg;
13319     }
13320 #endif
13321   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13322   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13323
13324   /* Simple, contiguous registers.  */
13325   if (regs == NULL_RTX)
13326     {
13327       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13328
13329       loc_result = NULL;
13330       while (nregs--)
13331         {
13332           dw_loc_descr_ref t;
13333
13334           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13335                                       VAR_INIT_STATUS_INITIALIZED);
13336           add_loc_descr (&loc_result, t);
13337           add_loc_descr_op_piece (&loc_result, size);
13338           ++reg;
13339         }
13340       return loc_result;
13341     }
13342
13343   /* Now onto stupid register sets in non contiguous locations.  */
13344
13345   gcc_assert (GET_CODE (regs) == PARALLEL);
13346
13347   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13348   loc_result = NULL;
13349
13350   for (i = 0; i < XVECLEN (regs, 0); ++i)
13351     {
13352       dw_loc_descr_ref t;
13353
13354       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13355                                   VAR_INIT_STATUS_INITIALIZED);
13356       add_loc_descr (&loc_result, t);
13357       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13358       add_loc_descr_op_piece (&loc_result, size);
13359     }
13360
13361   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13362     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13363   return loc_result;
13364 }
13365
13366 /* Return a location descriptor that designates a constant.  */
13367
13368 static dw_loc_descr_ref
13369 int_loc_descriptor (HOST_WIDE_INT i)
13370 {
13371   enum dwarf_location_atom op;
13372
13373   /* Pick the smallest representation of a constant, rather than just
13374      defaulting to the LEB encoding.  */
13375   if (i >= 0)
13376     {
13377       if (i <= 31)
13378         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13379       else if (i <= 0xff)
13380         op = DW_OP_const1u;
13381       else if (i <= 0xffff)
13382         op = DW_OP_const2u;
13383       else if (HOST_BITS_PER_WIDE_INT == 32
13384                || i <= 0xffffffff)
13385         op = DW_OP_const4u;
13386       else
13387         op = DW_OP_constu;
13388     }
13389   else
13390     {
13391       if (i >= -0x80)
13392         op = DW_OP_const1s;
13393       else if (i >= -0x8000)
13394         op = DW_OP_const2s;
13395       else if (HOST_BITS_PER_WIDE_INT == 32
13396                || i >= -0x80000000)
13397         op = DW_OP_const4s;
13398       else
13399         op = DW_OP_consts;
13400     }
13401
13402   return new_loc_descr (op, i, 0);
13403 }
13404
13405 /* Return loc description representing "address" of integer value.
13406    This can appear only as toplevel expression.  */
13407
13408 static dw_loc_descr_ref
13409 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13410 {
13411   int litsize;
13412   dw_loc_descr_ref loc_result = NULL;
13413
13414   if (!(dwarf_version >= 4 || !dwarf_strict))
13415     return NULL;
13416
13417   if (i >= 0)
13418     {
13419       if (i <= 31)
13420         litsize = 1;
13421       else if (i <= 0xff)
13422         litsize = 2;
13423       else if (i <= 0xffff)
13424         litsize = 3;
13425       else if (HOST_BITS_PER_WIDE_INT == 32
13426                || i <= 0xffffffff)
13427         litsize = 5;
13428       else
13429         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13430     }
13431   else
13432     {
13433       if (i >= -0x80)
13434         litsize = 2;
13435       else if (i >= -0x8000)
13436         litsize = 3;
13437       else if (HOST_BITS_PER_WIDE_INT == 32
13438                || i >= -0x80000000)
13439         litsize = 5;
13440       else
13441         litsize = 1 + size_of_sleb128 (i);
13442     }
13443   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13444      is more compact.  For DW_OP_stack_value we need:
13445      litsize + 1 (DW_OP_stack_value)
13446      and for DW_OP_implicit_value:
13447      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13448   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13449     {
13450       loc_result = int_loc_descriptor (i);
13451       add_loc_descr (&loc_result,
13452                      new_loc_descr (DW_OP_stack_value, 0, 0));
13453       return loc_result;
13454     }
13455
13456   loc_result = new_loc_descr (DW_OP_implicit_value,
13457                               size, 0);
13458   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13459   loc_result->dw_loc_oprnd2.v.val_int = i;
13460   return loc_result;
13461 }
13462
13463 /* Return a location descriptor that designates a base+offset location.  */
13464
13465 static dw_loc_descr_ref
13466 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13467                  enum var_init_status initialized)
13468 {
13469   unsigned int regno;
13470   dw_loc_descr_ref result;
13471   dw_fde_ref fde = current_fde ();
13472
13473   /* We only use "frame base" when we're sure we're talking about the
13474      post-prologue local stack frame.  We do this by *not* running
13475      register elimination until this point, and recognizing the special
13476      argument pointer and soft frame pointer rtx's.  */
13477   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13478     {
13479       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13480
13481       if (elim != reg)
13482         {
13483           if (GET_CODE (elim) == PLUS)
13484             {
13485               offset += INTVAL (XEXP (elim, 1));
13486               elim = XEXP (elim, 0);
13487             }
13488           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13489                        && (elim == hard_frame_pointer_rtx
13490                            || elim == stack_pointer_rtx))
13491                       || elim == (frame_pointer_needed
13492                                   ? hard_frame_pointer_rtx
13493                                   : stack_pointer_rtx));
13494
13495           /* If drap register is used to align stack, use frame
13496              pointer + offset to access stack variables.  If stack
13497              is aligned without drap, use stack pointer + offset to
13498              access stack variables.  */
13499           if (crtl->stack_realign_tried
13500               && reg == frame_pointer_rtx)
13501             {
13502               int base_reg
13503                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13504                                       ? HARD_FRAME_POINTER_REGNUM
13505                                       : STACK_POINTER_REGNUM);
13506               return new_reg_loc_descr (base_reg, offset);
13507             }
13508
13509           offset += frame_pointer_fb_offset;
13510           return new_loc_descr (DW_OP_fbreg, offset, 0);
13511         }
13512     }
13513   else if (!optimize
13514            && fde
13515            && (fde->drap_reg == REGNO (reg)
13516                || fde->vdrap_reg == REGNO (reg)))
13517     {
13518       /* Use cfa+offset to represent the location of arguments passed
13519          on the stack when drap is used to align stack.
13520          Only do this when not optimizing, for optimized code var-tracking
13521          is supposed to track where the arguments live and the register
13522          used as vdrap or drap in some spot might be used for something
13523          else in other part of the routine.  */
13524       return new_loc_descr (DW_OP_fbreg, offset, 0);
13525     }
13526
13527   regno = dbx_reg_number (reg);
13528   if (regno <= 31)
13529     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13530                             offset, 0);
13531   else
13532     result = new_loc_descr (DW_OP_bregx, regno, offset);
13533
13534   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13535     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13536
13537   return result;
13538 }
13539
13540 /* Return true if this RTL expression describes a base+offset calculation.  */
13541
13542 static inline int
13543 is_based_loc (const_rtx rtl)
13544 {
13545   return (GET_CODE (rtl) == PLUS
13546           && ((REG_P (XEXP (rtl, 0))
13547                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13548                && CONST_INT_P (XEXP (rtl, 1)))));
13549 }
13550
13551 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13552    failed.  */
13553
13554 static dw_loc_descr_ref
13555 tls_mem_loc_descriptor (rtx mem)
13556 {
13557   tree base;
13558   dw_loc_descr_ref loc_result;
13559
13560   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13561     return NULL;
13562
13563   base = get_base_address (MEM_EXPR (mem));
13564   if (base == NULL
13565       || TREE_CODE (base) != VAR_DECL
13566       || !DECL_THREAD_LOCAL_P (base))
13567     return NULL;
13568
13569   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13570   if (loc_result == NULL)
13571     return NULL;
13572
13573   if (INTVAL (MEM_OFFSET (mem)))
13574     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13575
13576   return loc_result;
13577 }
13578
13579 /* Output debug info about reason why we failed to expand expression as dwarf
13580    expression.  */
13581
13582 static void
13583 expansion_failed (tree expr, rtx rtl, char const *reason)
13584 {
13585   if (dump_file && (dump_flags & TDF_DETAILS))
13586     {
13587       fprintf (dump_file, "Failed to expand as dwarf: ");
13588       if (expr)
13589         print_generic_expr (dump_file, expr, dump_flags);
13590       if (rtl)
13591         {
13592           fprintf (dump_file, "\n");
13593           print_rtl (dump_file, rtl);
13594         }
13595       fprintf (dump_file, "\nReason: %s\n", reason);
13596     }
13597 }
13598
13599 /* Helper function for const_ok_for_output, called either directly
13600    or via for_each_rtx.  */
13601
13602 static int
13603 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13604 {
13605   rtx rtl = *rtlp;
13606
13607   if (GET_CODE (rtl) == UNSPEC)
13608     {
13609       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13610          we can't express it in the debug info.  */
13611 #ifdef ENABLE_CHECKING
13612       /* Don't complain about TLS UNSPECs, those are just too hard to
13613          delegitimize.  */
13614       if (XVECLEN (rtl, 0) != 1
13615           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13616           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13617           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13618           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13619         inform (current_function_decl
13620                 ? DECL_SOURCE_LOCATION (current_function_decl)
13621                 : UNKNOWN_LOCATION,
13622 #if NUM_UNSPEC_VALUES > 0
13623                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13624                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13625                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13626                 XINT (rtl, 1));
13627 #else
13628                 "non-delegitimized UNSPEC %d found in variable location",
13629                 XINT (rtl, 1));
13630 #endif
13631 #endif
13632       expansion_failed (NULL_TREE, rtl,
13633                         "UNSPEC hasn't been delegitimized.\n");
13634       return 1;
13635     }
13636
13637   if (GET_CODE (rtl) != SYMBOL_REF)
13638     return 0;
13639
13640   if (CONSTANT_POOL_ADDRESS_P (rtl))
13641     {
13642       bool marked;
13643       get_pool_constant_mark (rtl, &marked);
13644       /* If all references to this pool constant were optimized away,
13645          it was not output and thus we can't represent it.  */
13646       if (!marked)
13647         {
13648           expansion_failed (NULL_TREE, rtl,
13649                             "Constant was removed from constant pool.\n");
13650           return 1;
13651         }
13652     }
13653
13654   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13655     return 1;
13656
13657   /* Avoid references to external symbols in debug info, on several targets
13658      the linker might even refuse to link when linking a shared library,
13659      and in many other cases the relocations for .debug_info/.debug_loc are
13660      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13661      to be defined within the same shared library or executable are fine.  */
13662   if (SYMBOL_REF_EXTERNAL_P (rtl))
13663     {
13664       tree decl = SYMBOL_REF_DECL (rtl);
13665
13666       if (decl == NULL || !targetm.binds_local_p (decl))
13667         {
13668           expansion_failed (NULL_TREE, rtl,
13669                             "Symbol not defined in current TU.\n");
13670           return 1;
13671         }
13672     }
13673
13674   return 0;
13675 }
13676
13677 /* Return true if constant RTL can be emitted in DW_OP_addr or
13678    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13679    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13680
13681 static bool
13682 const_ok_for_output (rtx rtl)
13683 {
13684   if (GET_CODE (rtl) == SYMBOL_REF)
13685     return const_ok_for_output_1 (&rtl, NULL) == 0;
13686
13687   if (GET_CODE (rtl) == CONST)
13688     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13689
13690   return true;
13691 }
13692
13693 /* The following routine converts the RTL for a variable or parameter
13694    (resident in memory) into an equivalent Dwarf representation of a
13695    mechanism for getting the address of that same variable onto the top of a
13696    hypothetical "address evaluation" stack.
13697
13698    When creating memory location descriptors, we are effectively transforming
13699    the RTL for a memory-resident object into its Dwarf postfix expression
13700    equivalent.  This routine recursively descends an RTL tree, turning
13701    it into Dwarf postfix code as it goes.
13702
13703    MODE is the mode of the memory reference, needed to handle some
13704    autoincrement addressing modes.
13705
13706    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13707    location list for RTL.
13708
13709    Return 0 if we can't represent the location.  */
13710
13711 static dw_loc_descr_ref
13712 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13713                     enum var_init_status initialized)
13714 {
13715   dw_loc_descr_ref mem_loc_result = NULL;
13716   enum dwarf_location_atom op;
13717   dw_loc_descr_ref op0, op1;
13718
13719   /* Note that for a dynamically sized array, the location we will generate a
13720      description of here will be the lowest numbered location which is
13721      actually within the array.  That's *not* necessarily the same as the
13722      zeroth element of the array.  */
13723
13724   rtl = targetm.delegitimize_address (rtl);
13725
13726   switch (GET_CODE (rtl))
13727     {
13728     case POST_INC:
13729     case POST_DEC:
13730     case POST_MODIFY:
13731       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13732
13733     case SUBREG:
13734       /* The case of a subreg may arise when we have a local (register)
13735          variable or a formal (register) parameter which doesn't quite fill
13736          up an entire register.  For now, just assume that it is
13737          legitimate to make the Dwarf info refer to the whole register which
13738          contains the given subreg.  */
13739       if (!subreg_lowpart_p (rtl))
13740         break;
13741       rtl = SUBREG_REG (rtl);
13742       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13743         break;
13744       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13745         break;
13746       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13747       break;
13748
13749     case REG:
13750       /* Whenever a register number forms a part of the description of the
13751          method for calculating the (dynamic) address of a memory resident
13752          object, DWARF rules require the register number be referred to as
13753          a "base register".  This distinction is not based in any way upon
13754          what category of register the hardware believes the given register
13755          belongs to.  This is strictly DWARF terminology we're dealing with
13756          here. Note that in cases where the location of a memory-resident
13757          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13758          OP_CONST (0)) the actual DWARF location descriptor that we generate
13759          may just be OP_BASEREG (basereg).  This may look deceptively like
13760          the object in question was allocated to a register (rather than in
13761          memory) so DWARF consumers need to be aware of the subtle
13762          distinction between OP_REG and OP_BASEREG.  */
13763       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13764         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13765       else if (stack_realign_drap
13766                && crtl->drap_reg
13767                && crtl->args.internal_arg_pointer == rtl
13768                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13769         {
13770           /* If RTL is internal_arg_pointer, which has been optimized
13771              out, use DRAP instead.  */
13772           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13773                                             VAR_INIT_STATUS_INITIALIZED);
13774         }
13775       break;
13776
13777     case SIGN_EXTEND:
13778     case ZERO_EXTEND:
13779       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13780                                 VAR_INIT_STATUS_INITIALIZED);
13781       if (op0 == 0)
13782         break;
13783       else
13784         {
13785           int shift = DWARF2_ADDR_SIZE
13786                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13787           shift *= BITS_PER_UNIT;
13788           if (GET_CODE (rtl) == SIGN_EXTEND)
13789             op = DW_OP_shra;
13790           else
13791             op = DW_OP_shr;
13792           mem_loc_result = op0;
13793           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13794           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13795           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13796           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13797         }
13798       break;
13799
13800     case MEM:
13801       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13802                                            VAR_INIT_STATUS_INITIALIZED);
13803       if (mem_loc_result == NULL)
13804         mem_loc_result = tls_mem_loc_descriptor (rtl);
13805       if (mem_loc_result != 0)
13806         {
13807           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13808             {
13809               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13810               return 0;
13811             }
13812           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13813             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13814           else
13815             add_loc_descr (&mem_loc_result,
13816                            new_loc_descr (DW_OP_deref_size,
13817                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13818         }
13819       else
13820         {
13821           rtx new_rtl = avoid_constant_pool_reference (rtl);
13822           if (new_rtl != rtl)
13823             return mem_loc_descriptor (new_rtl, mode, initialized);
13824         }
13825       break;
13826
13827     case LO_SUM:
13828          rtl = XEXP (rtl, 1);
13829
13830       /* ... fall through ...  */
13831
13832     case LABEL_REF:
13833       /* Some ports can transform a symbol ref into a label ref, because
13834          the symbol ref is too far away and has to be dumped into a constant
13835          pool.  */
13836     case CONST:
13837     case SYMBOL_REF:
13838       if (GET_CODE (rtl) == SYMBOL_REF
13839           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13840         {
13841           dw_loc_descr_ref temp;
13842
13843           /* If this is not defined, we have no way to emit the data.  */
13844           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13845             break;
13846
13847           /* We used to emit DW_OP_addr here, but that's wrong, since
13848              DW_OP_addr should be relocated by the debug info consumer,
13849              while DW_OP_GNU_push_tls_address operand should not.  */
13850           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13851                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13852           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13853           temp->dw_loc_oprnd1.v.val_addr = rtl;
13854           temp->dtprel = true;
13855
13856           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13857           add_loc_descr (&mem_loc_result, temp);
13858
13859           break;
13860         }
13861
13862       if (!const_ok_for_output (rtl))
13863         break;
13864
13865     symref:
13866       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13867       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13868       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13869       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13870       break;
13871
13872     case CONCAT:
13873     case CONCATN:
13874     case VAR_LOCATION:
13875     case DEBUG_IMPLICIT_PTR:
13876       expansion_failed (NULL_TREE, rtl,
13877                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13878       return 0;
13879
13880     case ENTRY_VALUE:
13881       if (dwarf_strict)
13882         return NULL;
13883       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13884       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13885       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13886         mem_loc_result->dw_loc_oprnd1.v.val_loc
13887           = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13888                                     VAR_INIT_STATUS_INITIALIZED);
13889       else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13890         {
13891           dw_loc_descr_ref ref
13892             = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13893                                   VAR_INIT_STATUS_INITIALIZED);
13894           if (ref == NULL)
13895             return NULL;
13896           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13897         }
13898       else
13899         gcc_unreachable ();
13900       return mem_loc_result;
13901
13902     case PRE_MODIFY:
13903       /* Extract the PLUS expression nested inside and fall into
13904          PLUS code below.  */
13905       rtl = XEXP (rtl, 1);
13906       goto plus;
13907
13908     case PRE_INC:
13909     case PRE_DEC:
13910       /* Turn these into a PLUS expression and fall into the PLUS code
13911          below.  */
13912       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13913                           GEN_INT (GET_CODE (rtl) == PRE_INC
13914                                    ? GET_MODE_UNIT_SIZE (mode)
13915                                    : -GET_MODE_UNIT_SIZE (mode)));
13916
13917       /* ... fall through ...  */
13918
13919     case PLUS:
13920     plus:
13921       if (is_based_loc (rtl))
13922         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13923                                           INTVAL (XEXP (rtl, 1)),
13924                                           VAR_INIT_STATUS_INITIALIZED);
13925       else
13926         {
13927           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13928                                                VAR_INIT_STATUS_INITIALIZED);
13929           if (mem_loc_result == 0)
13930             break;
13931
13932           if (CONST_INT_P (XEXP (rtl, 1)))
13933             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13934           else
13935             {
13936               dw_loc_descr_ref mem_loc_result2
13937                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13938                                       VAR_INIT_STATUS_INITIALIZED);
13939               if (mem_loc_result2 == 0)
13940                 break;
13941               add_loc_descr (&mem_loc_result, mem_loc_result2);
13942               add_loc_descr (&mem_loc_result,
13943                              new_loc_descr (DW_OP_plus, 0, 0));
13944             }
13945         }
13946       break;
13947
13948     /* If a pseudo-reg is optimized away, it is possible for it to
13949        be replaced with a MEM containing a multiply or shift.  */
13950     case MINUS:
13951       op = DW_OP_minus;
13952       goto do_binop;
13953
13954     case MULT:
13955       op = DW_OP_mul;
13956       goto do_binop;
13957
13958     case DIV:
13959       op = DW_OP_div;
13960       goto do_binop;
13961
13962     case UMOD:
13963       op = DW_OP_mod;
13964       goto do_binop;
13965
13966     case ASHIFT:
13967       op = DW_OP_shl;
13968       goto do_binop;
13969
13970     case ASHIFTRT:
13971       op = DW_OP_shra;
13972       goto do_binop;
13973
13974     case LSHIFTRT:
13975       op = DW_OP_shr;
13976       goto do_binop;
13977
13978     case AND:
13979       op = DW_OP_and;
13980       goto do_binop;
13981
13982     case IOR:
13983       op = DW_OP_or;
13984       goto do_binop;
13985
13986     case XOR:
13987       op = DW_OP_xor;
13988       goto do_binop;
13989
13990     do_binop:
13991       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13992                                 VAR_INIT_STATUS_INITIALIZED);
13993       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13994                                 VAR_INIT_STATUS_INITIALIZED);
13995
13996       if (op0 == 0 || op1 == 0)
13997         break;
13998
13999       mem_loc_result = op0;
14000       add_loc_descr (&mem_loc_result, op1);
14001       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14002       break;
14003
14004     case MOD:
14005       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14006                                 VAR_INIT_STATUS_INITIALIZED);
14007       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14008                                 VAR_INIT_STATUS_INITIALIZED);
14009
14010       if (op0 == 0 || op1 == 0)
14011         break;
14012
14013       mem_loc_result = op0;
14014       add_loc_descr (&mem_loc_result, op1);
14015       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14016       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14017       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14018       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14019       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14020       break;
14021
14022     case NOT:
14023       op = DW_OP_not;
14024       goto do_unop;
14025
14026     case ABS:
14027       op = DW_OP_abs;
14028       goto do_unop;
14029
14030     case NEG:
14031       op = DW_OP_neg;
14032       goto do_unop;
14033
14034     do_unop:
14035       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14036                                 VAR_INIT_STATUS_INITIALIZED);
14037
14038       if (op0 == 0)
14039         break;
14040
14041       mem_loc_result = op0;
14042       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14043       break;
14044
14045     case CONST_INT:
14046       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14047       break;
14048
14049     case EQ:
14050       op = DW_OP_eq;
14051       goto do_scompare;
14052
14053     case GE:
14054       op = DW_OP_ge;
14055       goto do_scompare;
14056
14057     case GT:
14058       op = DW_OP_gt;
14059       goto do_scompare;
14060
14061     case LE:
14062       op = DW_OP_le;
14063       goto do_scompare;
14064
14065     case LT:
14066       op = DW_OP_lt;
14067       goto do_scompare;
14068
14069     case NE:
14070       op = DW_OP_ne;
14071       goto do_scompare;
14072
14073     do_scompare:
14074       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14075           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14076         break;
14077       else
14078         {
14079           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14080
14081           if (op_mode == VOIDmode)
14082             op_mode = GET_MODE (XEXP (rtl, 1));
14083           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14084             break;
14085
14086           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14087                                     VAR_INIT_STATUS_INITIALIZED);
14088           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14089                                     VAR_INIT_STATUS_INITIALIZED);
14090
14091           if (op0 == 0 || op1 == 0)
14092             break;
14093
14094           if (op_mode != VOIDmode
14095               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14096             {
14097               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14098               shift *= BITS_PER_UNIT;
14099               /* For eq/ne, if the operands are known to be zero-extended,
14100                  there is no need to do the fancy shifting up.  */
14101               if (op == DW_OP_eq || op == DW_OP_ne)
14102                 {
14103                   dw_loc_descr_ref last0, last1;
14104                   for (last0 = op0;
14105                        last0->dw_loc_next != NULL;
14106                        last0 = last0->dw_loc_next)
14107                     ;
14108                   for (last1 = op1;
14109                        last1->dw_loc_next != NULL;
14110                        last1 = last1->dw_loc_next)
14111                     ;
14112                   /* deref_size zero extends, and for constants we can check
14113                      whether they are zero extended or not.  */
14114                   if (((last0->dw_loc_opc == DW_OP_deref_size
14115                         && last0->dw_loc_oprnd1.v.val_int
14116                            <= GET_MODE_SIZE (op_mode))
14117                        || (CONST_INT_P (XEXP (rtl, 0))
14118                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14119                                == (INTVAL (XEXP (rtl, 0))
14120                                    & GET_MODE_MASK (op_mode))))
14121                       && ((last1->dw_loc_opc == DW_OP_deref_size
14122                            && last1->dw_loc_oprnd1.v.val_int
14123                               <= GET_MODE_SIZE (op_mode))
14124                           || (CONST_INT_P (XEXP (rtl, 1))
14125                               && (unsigned HOST_WIDE_INT)
14126                                  INTVAL (XEXP (rtl, 1))
14127                                  == (INTVAL (XEXP (rtl, 1))
14128                                      & GET_MODE_MASK (op_mode)))))
14129                     goto do_compare;
14130                 }
14131               add_loc_descr (&op0, int_loc_descriptor (shift));
14132               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14133               if (CONST_INT_P (XEXP (rtl, 1)))
14134                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14135               else
14136                 {
14137                   add_loc_descr (&op1, int_loc_descriptor (shift));
14138                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14139                 }
14140             }
14141         }
14142
14143     do_compare:
14144       mem_loc_result = op0;
14145       add_loc_descr (&mem_loc_result, op1);
14146       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14147       if (STORE_FLAG_VALUE != 1)
14148         {
14149           add_loc_descr (&mem_loc_result,
14150                          int_loc_descriptor (STORE_FLAG_VALUE));
14151           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14152         }
14153       break;
14154
14155     case GEU:
14156       op = DW_OP_ge;
14157       goto do_ucompare;
14158
14159     case GTU:
14160       op = DW_OP_gt;
14161       goto do_ucompare;
14162
14163     case LEU:
14164       op = DW_OP_le;
14165       goto do_ucompare;
14166
14167     case LTU:
14168       op = DW_OP_lt;
14169       goto do_ucompare;
14170
14171     do_ucompare:
14172       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14173           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14174         break;
14175       else
14176         {
14177           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14178
14179           if (op_mode == VOIDmode)
14180             op_mode = GET_MODE (XEXP (rtl, 1));
14181           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14182             break;
14183
14184           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14185                                     VAR_INIT_STATUS_INITIALIZED);
14186           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14187                                     VAR_INIT_STATUS_INITIALIZED);
14188
14189           if (op0 == 0 || op1 == 0)
14190             break;
14191
14192           if (op_mode != VOIDmode
14193               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14194             {
14195               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14196               dw_loc_descr_ref last0, last1;
14197               for (last0 = op0;
14198                    last0->dw_loc_next != NULL;
14199                    last0 = last0->dw_loc_next)
14200                 ;
14201               for (last1 = op1;
14202                    last1->dw_loc_next != NULL;
14203                    last1 = last1->dw_loc_next)
14204                 ;
14205               if (CONST_INT_P (XEXP (rtl, 0)))
14206                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14207               /* deref_size zero extends, so no need to mask it again.  */
14208               else if (last0->dw_loc_opc != DW_OP_deref_size
14209                        || last0->dw_loc_oprnd1.v.val_int
14210                           > GET_MODE_SIZE (op_mode))
14211                 {
14212                   add_loc_descr (&op0, int_loc_descriptor (mask));
14213                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14214                 }
14215               if (CONST_INT_P (XEXP (rtl, 1)))
14216                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14217               /* deref_size zero extends, so no need to mask it again.  */
14218               else if (last1->dw_loc_opc != DW_OP_deref_size
14219                        || last1->dw_loc_oprnd1.v.val_int
14220                           > GET_MODE_SIZE (op_mode))
14221                 {
14222                   add_loc_descr (&op1, int_loc_descriptor (mask));
14223                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14224                 }
14225             }
14226           else
14227             {
14228               HOST_WIDE_INT bias = 1;
14229               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14230               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14231               if (CONST_INT_P (XEXP (rtl, 1)))
14232                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14233                                           + INTVAL (XEXP (rtl, 1)));
14234               else
14235                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14236                                                     bias, 0));
14237             }
14238         }
14239       goto do_compare;
14240
14241     case SMIN:
14242     case SMAX:
14243     case UMIN:
14244     case UMAX:
14245       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14246           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14247           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14248         break;
14249
14250       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14251                                 VAR_INIT_STATUS_INITIALIZED);
14252       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14253                                 VAR_INIT_STATUS_INITIALIZED);
14254
14255       if (op0 == 0 || op1 == 0)
14256         break;
14257
14258       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14259       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14260       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14261       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14262         {
14263           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14264             {
14265               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14266               add_loc_descr (&op0, int_loc_descriptor (mask));
14267               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14268               add_loc_descr (&op1, int_loc_descriptor (mask));
14269               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14270             }
14271           else
14272             {
14273               HOST_WIDE_INT bias = 1;
14274               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14275               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14276               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14277             }
14278         }
14279       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14280         {
14281           int shift = DWARF2_ADDR_SIZE
14282                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14283           shift *= BITS_PER_UNIT;
14284           add_loc_descr (&op0, int_loc_descriptor (shift));
14285           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14286           add_loc_descr (&op1, int_loc_descriptor (shift));
14287           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14288         }
14289
14290       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14291         op = DW_OP_lt;
14292       else
14293         op = DW_OP_gt;
14294       mem_loc_result = op0;
14295       add_loc_descr (&mem_loc_result, op1);
14296       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14297       {
14298         dw_loc_descr_ref bra_node, drop_node;
14299
14300         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14301         add_loc_descr (&mem_loc_result, bra_node);
14302         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14303         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14304         add_loc_descr (&mem_loc_result, drop_node);
14305         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14306         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14307       }
14308       break;
14309
14310     case ZERO_EXTRACT:
14311     case SIGN_EXTRACT:
14312       if (CONST_INT_P (XEXP (rtl, 1))
14313           && CONST_INT_P (XEXP (rtl, 2))
14314           && ((unsigned) INTVAL (XEXP (rtl, 1))
14315               + (unsigned) INTVAL (XEXP (rtl, 2))
14316               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14317           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14318           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14319         {
14320           int shift, size;
14321           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14322                                     VAR_INIT_STATUS_INITIALIZED);
14323           if (op0 == 0)
14324             break;
14325           if (GET_CODE (rtl) == SIGN_EXTRACT)
14326             op = DW_OP_shra;
14327           else
14328             op = DW_OP_shr;
14329           mem_loc_result = op0;
14330           size = INTVAL (XEXP (rtl, 1));
14331           shift = INTVAL (XEXP (rtl, 2));
14332           if (BITS_BIG_ENDIAN)
14333             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14334                     - shift - size;
14335           if (shift + size != (int) DWARF2_ADDR_SIZE)
14336             {
14337               add_loc_descr (&mem_loc_result,
14338                              int_loc_descriptor (DWARF2_ADDR_SIZE
14339                                                  - shift - size));
14340               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14341             }
14342           if (size != (int) DWARF2_ADDR_SIZE)
14343             {
14344               add_loc_descr (&mem_loc_result,
14345                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14346               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14347             }
14348         }
14349       break;
14350
14351     case IF_THEN_ELSE:
14352       {
14353         dw_loc_descr_ref op2, bra_node, drop_node;
14354         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14355                                   VAR_INIT_STATUS_INITIALIZED);
14356         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14357                                   VAR_INIT_STATUS_INITIALIZED);
14358         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14359                                   VAR_INIT_STATUS_INITIALIZED);
14360         if (op0 == NULL || op1 == NULL || op2 == NULL)
14361           break;
14362
14363         mem_loc_result = op1;
14364         add_loc_descr (&mem_loc_result, op2);
14365         add_loc_descr (&mem_loc_result, op0);
14366         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14367         add_loc_descr (&mem_loc_result, bra_node);
14368         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14369         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14370         add_loc_descr (&mem_loc_result, drop_node);
14371         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14372         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14373       }
14374       break;
14375
14376     case COMPARE:
14377     case ROTATE:
14378     case ROTATERT:
14379     case TRUNCATE:
14380       /* In theory, we could implement the above.  */
14381       /* DWARF cannot represent the unsigned compare operations
14382          natively.  */
14383     case SS_MULT:
14384     case US_MULT:
14385     case SS_DIV:
14386     case US_DIV:
14387     case SS_PLUS:
14388     case US_PLUS:
14389     case SS_MINUS:
14390     case US_MINUS:
14391     case SS_NEG:
14392     case US_NEG:
14393     case SS_ABS:
14394     case SS_ASHIFT:
14395     case US_ASHIFT:
14396     case SS_TRUNCATE:
14397     case US_TRUNCATE:
14398     case UDIV:
14399     case UNORDERED:
14400     case ORDERED:
14401     case UNEQ:
14402     case UNGE:
14403     case UNGT:
14404     case UNLE:
14405     case UNLT:
14406     case LTGT:
14407     case FLOAT_EXTEND:
14408     case FLOAT_TRUNCATE:
14409     case FLOAT:
14410     case UNSIGNED_FLOAT:
14411     case FIX:
14412     case UNSIGNED_FIX:
14413     case FRACT_CONVERT:
14414     case UNSIGNED_FRACT_CONVERT:
14415     case SAT_FRACT:
14416     case UNSIGNED_SAT_FRACT:
14417     case SQRT:
14418     case BSWAP:
14419     case FFS:
14420     case CLZ:
14421     case CTZ:
14422     case POPCOUNT:
14423     case PARITY:
14424     case ASM_OPERANDS:
14425     case VEC_MERGE:
14426     case VEC_SELECT:
14427     case VEC_CONCAT:
14428     case VEC_DUPLICATE:
14429     case UNSPEC:
14430     case HIGH:
14431       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14432          can't express it in the debug info.  This can happen e.g. with some
14433          TLS UNSPECs.  */
14434       break;
14435
14436     case CONST_STRING:
14437       resolve_one_addr (&rtl, NULL);
14438       goto symref;
14439
14440     default:
14441 #ifdef ENABLE_CHECKING
14442       print_rtl (stderr, rtl);
14443       gcc_unreachable ();
14444 #else
14445       break;
14446 #endif
14447     }
14448
14449   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14450     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14451
14452   return mem_loc_result;
14453 }
14454
14455 /* Return a descriptor that describes the concatenation of two locations.
14456    This is typically a complex variable.  */
14457
14458 static dw_loc_descr_ref
14459 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14460 {
14461   dw_loc_descr_ref cc_loc_result = NULL;
14462   dw_loc_descr_ref x0_ref
14463     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14464   dw_loc_descr_ref x1_ref
14465     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14466
14467   if (x0_ref == 0 || x1_ref == 0)
14468     return 0;
14469
14470   cc_loc_result = x0_ref;
14471   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14472
14473   add_loc_descr (&cc_loc_result, x1_ref);
14474   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14475
14476   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14477     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14478
14479   return cc_loc_result;
14480 }
14481
14482 /* Return a descriptor that describes the concatenation of N
14483    locations.  */
14484
14485 static dw_loc_descr_ref
14486 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14487 {
14488   unsigned int i;
14489   dw_loc_descr_ref cc_loc_result = NULL;
14490   unsigned int n = XVECLEN (concatn, 0);
14491
14492   for (i = 0; i < n; ++i)
14493     {
14494       dw_loc_descr_ref ref;
14495       rtx x = XVECEXP (concatn, 0, i);
14496
14497       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14498       if (ref == NULL)
14499         return NULL;
14500
14501       add_loc_descr (&cc_loc_result, ref);
14502       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14503     }
14504
14505   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14506     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14507
14508   return cc_loc_result;
14509 }
14510
14511 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14512    for DEBUG_IMPLICIT_PTR RTL.  */
14513
14514 static dw_loc_descr_ref
14515 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14516 {
14517   dw_loc_descr_ref ret;
14518   dw_die_ref ref;
14519
14520   if (dwarf_strict)
14521     return NULL;
14522   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14523               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14524               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14525   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14526   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14527   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14528   if (ref)
14529     {
14530       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14531       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14532       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14533     }
14534   else
14535     {
14536       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14537       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14538     }
14539   return ret;
14540 }
14541
14542 /* Output a proper Dwarf location descriptor for a variable or parameter
14543    which is either allocated in a register or in a memory location.  For a
14544    register, we just generate an OP_REG and the register number.  For a
14545    memory location we provide a Dwarf postfix expression describing how to
14546    generate the (dynamic) address of the object onto the address stack.
14547
14548    MODE is mode of the decl if this loc_descriptor is going to be used in
14549    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14550    allowed, VOIDmode otherwise.
14551
14552    If we don't know how to describe it, return 0.  */
14553
14554 static dw_loc_descr_ref
14555 loc_descriptor (rtx rtl, enum machine_mode mode,
14556                 enum var_init_status initialized)
14557 {
14558   dw_loc_descr_ref loc_result = NULL;
14559
14560   switch (GET_CODE (rtl))
14561     {
14562     case SUBREG:
14563       /* The case of a subreg may arise when we have a local (register)
14564          variable or a formal (register) parameter which doesn't quite fill
14565          up an entire register.  For now, just assume that it is
14566          legitimate to make the Dwarf info refer to the whole register which
14567          contains the given subreg.  */
14568       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14569       break;
14570
14571     case REG:
14572       loc_result = reg_loc_descriptor (rtl, initialized);
14573       break;
14574
14575     case MEM:
14576       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14577                                        initialized);
14578       if (loc_result == NULL)
14579         loc_result = tls_mem_loc_descriptor (rtl);
14580       if (loc_result == NULL)
14581         {
14582           rtx new_rtl = avoid_constant_pool_reference (rtl);
14583           if (new_rtl != rtl)
14584             loc_result = loc_descriptor (new_rtl, mode, initialized);
14585         }
14586       break;
14587
14588     case CONCAT:
14589       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14590                                           initialized);
14591       break;
14592
14593     case CONCATN:
14594       loc_result = concatn_loc_descriptor (rtl, initialized);
14595       break;
14596
14597     case VAR_LOCATION:
14598       /* Single part.  */
14599       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14600         {
14601           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14602           if (GET_CODE (loc) == EXPR_LIST)
14603             loc = XEXP (loc, 0);
14604           loc_result = loc_descriptor (loc, mode, initialized);
14605           break;
14606         }
14607
14608       rtl = XEXP (rtl, 1);
14609       /* FALLTHRU */
14610
14611     case PARALLEL:
14612       {
14613         rtvec par_elems = XVEC (rtl, 0);
14614         int num_elem = GET_NUM_ELEM (par_elems);
14615         enum machine_mode mode;
14616         int i;
14617
14618         /* Create the first one, so we have something to add to.  */
14619         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14620                                      VOIDmode, initialized);
14621         if (loc_result == NULL)
14622           return NULL;
14623         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14624         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14625         for (i = 1; i < num_elem; i++)
14626           {
14627             dw_loc_descr_ref temp;
14628
14629             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14630                                    VOIDmode, initialized);
14631             if (temp == NULL)
14632               return NULL;
14633             add_loc_descr (&loc_result, temp);
14634             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14635             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14636           }
14637       }
14638       break;
14639
14640     case CONST_INT:
14641       if (mode != VOIDmode && mode != BLKmode)
14642         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14643                                                     INTVAL (rtl));
14644       break;
14645
14646     case CONST_DOUBLE:
14647       if (mode == VOIDmode)
14648         mode = GET_MODE (rtl);
14649
14650       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14651         {
14652           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14653
14654           /* Note that a CONST_DOUBLE rtx could represent either an integer
14655              or a floating-point constant.  A CONST_DOUBLE is used whenever
14656              the constant requires more than one word in order to be
14657              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14658           loc_result = new_loc_descr (DW_OP_implicit_value,
14659                                       GET_MODE_SIZE (mode), 0);
14660           if (SCALAR_FLOAT_MODE_P (mode))
14661             {
14662               unsigned int length = GET_MODE_SIZE (mode);
14663               unsigned char *array
14664                   = (unsigned char*) ggc_alloc_atomic (length);
14665
14666               insert_float (rtl, array);
14667               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14668               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14669               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14670               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14671             }
14672           else
14673             {
14674               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14675               loc_result->dw_loc_oprnd2.v.val_double
14676                 = rtx_to_double_int (rtl);
14677             }
14678         }
14679       break;
14680
14681     case CONST_VECTOR:
14682       if (mode == VOIDmode)
14683         mode = GET_MODE (rtl);
14684
14685       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14686         {
14687           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14688           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14689           unsigned char *array = (unsigned char *)
14690             ggc_alloc_atomic (length * elt_size);
14691           unsigned int i;
14692           unsigned char *p;
14693
14694           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14695           switch (GET_MODE_CLASS (mode))
14696             {
14697             case MODE_VECTOR_INT:
14698               for (i = 0, p = array; i < length; i++, p += elt_size)
14699                 {
14700                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14701                   double_int val = rtx_to_double_int (elt);
14702
14703                   if (elt_size <= sizeof (HOST_WIDE_INT))
14704                     insert_int (double_int_to_shwi (val), elt_size, p);
14705                   else
14706                     {
14707                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14708                       insert_double (val, p);
14709                     }
14710                 }
14711               break;
14712
14713             case MODE_VECTOR_FLOAT:
14714               for (i = 0, p = array; i < length; i++, p += elt_size)
14715                 {
14716                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14717                   insert_float (elt, p);
14718                 }
14719               break;
14720
14721             default:
14722               gcc_unreachable ();
14723             }
14724
14725           loc_result = new_loc_descr (DW_OP_implicit_value,
14726                                       length * elt_size, 0);
14727           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14728           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14729           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14730           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14731         }
14732       break;
14733
14734     case CONST:
14735       if (mode == VOIDmode
14736           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14737           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14738           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14739         {
14740           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14741           break;
14742         }
14743       /* FALLTHROUGH */
14744     case SYMBOL_REF:
14745       if (!const_ok_for_output (rtl))
14746         break;
14747     case LABEL_REF:
14748       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14749           && (dwarf_version >= 4 || !dwarf_strict))
14750         {
14751           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14752           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14753           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14754           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14755           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14756         }
14757       break;
14758
14759     case DEBUG_IMPLICIT_PTR:
14760       loc_result = implicit_ptr_descriptor (rtl, 0);
14761       break;
14762
14763     case PLUS:
14764       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14765           && CONST_INT_P (XEXP (rtl, 1)))
14766         {
14767           loc_result
14768             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14769           break;
14770         }
14771       /* FALLTHRU */
14772     default:
14773       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14774           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14775           && (dwarf_version >= 4 || !dwarf_strict))
14776         {
14777           /* Value expression.  */
14778           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14779           if (loc_result)
14780             add_loc_descr (&loc_result,
14781                            new_loc_descr (DW_OP_stack_value, 0, 0));
14782         }
14783       break;
14784     }
14785
14786   return loc_result;
14787 }
14788
14789 /* We need to figure out what section we should use as the base for the
14790    address ranges where a given location is valid.
14791    1. If this particular DECL has a section associated with it, use that.
14792    2. If this function has a section associated with it, use that.
14793    3. Otherwise, use the text section.
14794    XXX: If you split a variable across multiple sections, we won't notice.  */
14795
14796 static const char *
14797 secname_for_decl (const_tree decl)
14798 {
14799   const char *secname;
14800
14801   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14802     {
14803       tree sectree = DECL_SECTION_NAME (decl);
14804       secname = TREE_STRING_POINTER (sectree);
14805     }
14806   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14807     {
14808       tree sectree = DECL_SECTION_NAME (current_function_decl);
14809       secname = TREE_STRING_POINTER (sectree);
14810     }
14811   else if (cfun && in_cold_section_p)
14812     secname = crtl->subsections.cold_section_label;
14813   else
14814     secname = text_section_label;
14815
14816   return secname;
14817 }
14818
14819 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14820
14821 static bool
14822 decl_by_reference_p (tree decl)
14823 {
14824   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14825            || TREE_CODE (decl) == VAR_DECL)
14826           && DECL_BY_REFERENCE (decl));
14827 }
14828
14829 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14830    for VARLOC.  */
14831
14832 static dw_loc_descr_ref
14833 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14834                enum var_init_status initialized)
14835 {
14836   int have_address = 0;
14837   dw_loc_descr_ref descr;
14838   enum machine_mode mode;
14839
14840   if (want_address != 2)
14841     {
14842       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14843       /* Single part.  */
14844       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14845         {
14846           varloc = PAT_VAR_LOCATION_LOC (varloc);
14847           if (GET_CODE (varloc) == EXPR_LIST)
14848             varloc = XEXP (varloc, 0);
14849           mode = GET_MODE (varloc);
14850           if (MEM_P (varloc))
14851             {
14852               rtx addr = XEXP (varloc, 0);
14853               descr = mem_loc_descriptor (addr, mode, initialized);
14854               if (descr)
14855                 have_address = 1;
14856               else
14857                 {
14858                   rtx x = avoid_constant_pool_reference (varloc);
14859                   if (x != varloc)
14860                     descr = mem_loc_descriptor (x, mode, initialized);
14861                 }
14862             }
14863           else
14864             descr = mem_loc_descriptor (varloc, mode, initialized);
14865         }
14866       else
14867         return 0;
14868     }
14869   else
14870     {
14871       if (GET_CODE (varloc) == VAR_LOCATION)
14872         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14873       else
14874         mode = DECL_MODE (loc);
14875       descr = loc_descriptor (varloc, mode, initialized);
14876       have_address = 1;
14877     }
14878
14879   if (!descr)
14880     return 0;
14881
14882   if (want_address == 2 && !have_address
14883       && (dwarf_version >= 4 || !dwarf_strict))
14884     {
14885       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14886         {
14887           expansion_failed (loc, NULL_RTX,
14888                             "DWARF address size mismatch");
14889           return 0;
14890         }
14891       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14892       have_address = 1;
14893     }
14894   /* Show if we can't fill the request for an address.  */
14895   if (want_address && !have_address)
14896     {
14897       expansion_failed (loc, NULL_RTX,
14898                         "Want address and only have value");
14899       return 0;
14900     }
14901
14902   /* If we've got an address and don't want one, dereference.  */
14903   if (!want_address && have_address)
14904     {
14905       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14906       enum dwarf_location_atom op;
14907
14908       if (size > DWARF2_ADDR_SIZE || size == -1)
14909         {
14910           expansion_failed (loc, NULL_RTX,
14911                             "DWARF address size mismatch");
14912           return 0;
14913         }
14914       else if (size == DWARF2_ADDR_SIZE)
14915         op = DW_OP_deref;
14916       else
14917         op = DW_OP_deref_size;
14918
14919       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14920     }
14921
14922   return descr;
14923 }
14924
14925 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14926    if it is not possible.  */
14927
14928 static dw_loc_descr_ref
14929 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14930 {
14931   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14932     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14933   else if (dwarf_version >= 3 || !dwarf_strict)
14934     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14935   else
14936     return NULL;
14937 }
14938
14939 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14940    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14941
14942 static dw_loc_descr_ref
14943 dw_sra_loc_expr (tree decl, rtx loc)
14944 {
14945   rtx p;
14946   unsigned int padsize = 0;
14947   dw_loc_descr_ref descr, *descr_tail;
14948   unsigned HOST_WIDE_INT decl_size;
14949   rtx varloc;
14950   enum var_init_status initialized;
14951
14952   if (DECL_SIZE (decl) == NULL
14953       || !host_integerp (DECL_SIZE (decl), 1))
14954     return NULL;
14955
14956   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14957   descr = NULL;
14958   descr_tail = &descr;
14959
14960   for (p = loc; p; p = XEXP (p, 1))
14961     {
14962       unsigned int bitsize = decl_piece_bitsize (p);
14963       rtx loc_note = *decl_piece_varloc_ptr (p);
14964       dw_loc_descr_ref cur_descr;
14965       dw_loc_descr_ref *tail, last = NULL;
14966       unsigned int opsize = 0;
14967
14968       if (loc_note == NULL_RTX
14969           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14970         {
14971           padsize += bitsize;
14972           continue;
14973         }
14974       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14975       varloc = NOTE_VAR_LOCATION (loc_note);
14976       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14977       if (cur_descr == NULL)
14978         {
14979           padsize += bitsize;
14980           continue;
14981         }
14982
14983       /* Check that cur_descr either doesn't use
14984          DW_OP_*piece operations, or their sum is equal
14985          to bitsize.  Otherwise we can't embed it.  */
14986       for (tail = &cur_descr; *tail != NULL;
14987            tail = &(*tail)->dw_loc_next)
14988         if ((*tail)->dw_loc_opc == DW_OP_piece)
14989           {
14990             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14991                       * BITS_PER_UNIT;
14992             last = *tail;
14993           }
14994         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14995           {
14996             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14997             last = *tail;
14998           }
14999
15000       if (last != NULL && opsize != bitsize)
15001         {
15002           padsize += bitsize;
15003           continue;
15004         }
15005
15006       /* If there is a hole, add DW_OP_*piece after empty DWARF
15007          expression, which means that those bits are optimized out.  */
15008       if (padsize)
15009         {
15010           if (padsize > decl_size)
15011             return NULL;
15012           decl_size -= padsize;
15013           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15014           if (*descr_tail == NULL)
15015             return NULL;
15016           descr_tail = &(*descr_tail)->dw_loc_next;
15017           padsize = 0;
15018         }
15019       *descr_tail = cur_descr;
15020       descr_tail = tail;
15021       if (bitsize > decl_size)
15022         return NULL;
15023       decl_size -= bitsize;
15024       if (last == NULL)
15025         {
15026           HOST_WIDE_INT offset = 0;
15027           if (GET_CODE (varloc) == VAR_LOCATION
15028               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15029             {
15030               varloc = PAT_VAR_LOCATION_LOC (varloc);
15031               if (GET_CODE (varloc) == EXPR_LIST)
15032                 varloc = XEXP (varloc, 0);
15033             }
15034           do 
15035             {
15036               if (GET_CODE (varloc) == CONST
15037                   || GET_CODE (varloc) == SIGN_EXTEND
15038                   || GET_CODE (varloc) == ZERO_EXTEND)
15039                 varloc = XEXP (varloc, 0);
15040               else if (GET_CODE (varloc) == SUBREG)
15041                 varloc = SUBREG_REG (varloc);
15042               else
15043                 break;
15044             }
15045           while (1);
15046           /* DW_OP_bit_size offset should be zero for register
15047              or implicit location descriptions and empty location
15048              descriptions, but for memory addresses needs big endian
15049              adjustment.  */
15050           if (MEM_P (varloc))
15051             {
15052               unsigned HOST_WIDE_INT memsize
15053                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15054               if (memsize != bitsize)
15055                 {
15056                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15057                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15058                     return NULL;
15059                   if (memsize < bitsize)
15060                     return NULL;
15061                   if (BITS_BIG_ENDIAN)
15062                     offset = memsize - bitsize;
15063                 }
15064             }
15065
15066           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15067           if (*descr_tail == NULL)
15068             return NULL;
15069           descr_tail = &(*descr_tail)->dw_loc_next;
15070         }
15071     }
15072
15073   /* If there were any non-empty expressions, add padding till the end of
15074      the decl.  */
15075   if (descr != NULL && decl_size != 0)
15076     {
15077       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15078       if (*descr_tail == NULL)
15079         return NULL;
15080     }
15081   return descr;
15082 }
15083
15084 /* Return the dwarf representation of the location list LOC_LIST of
15085    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15086    function.  */
15087
15088 static dw_loc_list_ref
15089 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15090 {
15091   const char *endname, *secname;
15092   rtx varloc;
15093   enum var_init_status initialized;
15094   struct var_loc_node *node;
15095   dw_loc_descr_ref descr;
15096   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15097   dw_loc_list_ref list = NULL;
15098   dw_loc_list_ref *listp = &list;
15099
15100   /* Now that we know what section we are using for a base,
15101      actually construct the list of locations.
15102      The first location information is what is passed to the
15103      function that creates the location list, and the remaining
15104      locations just get added on to that list.
15105      Note that we only know the start address for a location
15106      (IE location changes), so to build the range, we use
15107      the range [current location start, next location start].
15108      This means we have to special case the last node, and generate
15109      a range of [last location start, end of function label].  */
15110
15111   secname = secname_for_decl (decl);
15112
15113   for (node = loc_list->first; node; node = node->next)
15114     if (GET_CODE (node->loc) == EXPR_LIST
15115         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15116       {
15117         if (GET_CODE (node->loc) == EXPR_LIST)
15118           {
15119             /* This requires DW_OP_{,bit_}piece, which is not usable
15120                inside DWARF expressions.  */
15121             if (want_address != 2)
15122               continue;
15123             descr = dw_sra_loc_expr (decl, node->loc);
15124             if (descr == NULL)
15125               continue;
15126           }
15127         else
15128           {
15129             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15130             varloc = NOTE_VAR_LOCATION (node->loc);
15131             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15132           }
15133         if (descr)
15134           {
15135             /* The variable has a location between NODE->LABEL and
15136                NODE->NEXT->LABEL.  */
15137             if (node->next)
15138               endname = node->next->label;
15139             /* If the variable has a location at the last label
15140                it keeps its location until the end of function.  */
15141             else if (!current_function_decl)
15142               endname = text_end_label;
15143             else
15144               {
15145                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15146                                              current_function_funcdef_no);
15147                 endname = ggc_strdup (label_id);
15148               }
15149
15150             *listp = new_loc_list (descr, node->label, endname, secname);
15151             listp = &(*listp)->dw_loc_next;
15152           }
15153       }
15154
15155   /* Try to avoid the overhead of a location list emitting a location
15156      expression instead, but only if we didn't have more than one
15157      location entry in the first place.  If some entries were not
15158      representable, we don't want to pretend a single entry that was
15159      applies to the entire scope in which the variable is
15160      available.  */
15161   if (list && loc_list->first->next)
15162     gen_llsym (list);
15163
15164   return list;
15165 }
15166
15167 /* Return if the loc_list has only single element and thus can be represented
15168    as location description.   */
15169
15170 static bool
15171 single_element_loc_list_p (dw_loc_list_ref list)
15172 {
15173   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15174   return !list->ll_symbol;
15175 }
15176
15177 /* To each location in list LIST add loc descr REF.  */
15178
15179 static void
15180 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15181 {
15182   dw_loc_descr_ref copy;
15183   add_loc_descr (&list->expr, ref);
15184   list = list->dw_loc_next;
15185   while (list)
15186     {
15187       copy = ggc_alloc_dw_loc_descr_node ();
15188       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15189       add_loc_descr (&list->expr, copy);
15190       while (copy->dw_loc_next)
15191         {
15192           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15193           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15194           copy->dw_loc_next = new_copy;
15195           copy = new_copy;
15196         }
15197       list = list->dw_loc_next;
15198     }
15199 }
15200
15201 /* Given two lists RET and LIST
15202    produce location list that is result of adding expression in LIST
15203    to expression in RET on each possition in program.
15204    Might be destructive on both RET and LIST.
15205
15206    TODO: We handle only simple cases of RET or LIST having at most one
15207    element. General case would inolve sorting the lists in program order
15208    and merging them that will need some additional work.
15209    Adding that will improve quality of debug info especially for SRA-ed
15210    structures.  */
15211
15212 static void
15213 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15214 {
15215   if (!list)
15216     return;
15217   if (!*ret)
15218     {
15219       *ret = list;
15220       return;
15221     }
15222   if (!list->dw_loc_next)
15223     {
15224       add_loc_descr_to_each (*ret, list->expr);
15225       return;
15226     }
15227   if (!(*ret)->dw_loc_next)
15228     {
15229       add_loc_descr_to_each (list, (*ret)->expr);
15230       *ret = list;
15231       return;
15232     }
15233   expansion_failed (NULL_TREE, NULL_RTX,
15234                     "Don't know how to merge two non-trivial"
15235                     " location lists.\n");
15236   *ret = NULL;
15237   return;
15238 }
15239
15240 /* LOC is constant expression.  Try a luck, look it up in constant
15241    pool and return its loc_descr of its address.  */
15242
15243 static dw_loc_descr_ref
15244 cst_pool_loc_descr (tree loc)
15245 {
15246   /* Get an RTL for this, if something has been emitted.  */
15247   rtx rtl = lookup_constant_def (loc);
15248   enum machine_mode mode;
15249
15250   if (!rtl || !MEM_P (rtl))
15251     {
15252       gcc_assert (!rtl);
15253       return 0;
15254     }
15255   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15256
15257   /* TODO: We might get more coverage if we was actually delaying expansion
15258      of all expressions till end of compilation when constant pools are fully
15259      populated.  */
15260   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15261     {
15262       expansion_failed (loc, NULL_RTX,
15263                         "CST value in contant pool but not marked.");
15264       return 0;
15265     }
15266   mode = GET_MODE (rtl);
15267   rtl = XEXP (rtl, 0);
15268   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15269 }
15270
15271 /* Return dw_loc_list representing address of addr_expr LOC
15272    by looking for innder INDIRECT_REF expression and turing it
15273    into simple arithmetics.  */
15274
15275 static dw_loc_list_ref
15276 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15277 {
15278   tree obj, offset;
15279   HOST_WIDE_INT bitsize, bitpos, bytepos;
15280   enum machine_mode mode;
15281   int volatilep;
15282   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15283   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15284
15285   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15286                              &bitsize, &bitpos, &offset, &mode,
15287                              &unsignedp, &volatilep, false);
15288   STRIP_NOPS (obj);
15289   if (bitpos % BITS_PER_UNIT)
15290     {
15291       expansion_failed (loc, NULL_RTX, "bitfield access");
15292       return 0;
15293     }
15294   if (!INDIRECT_REF_P (obj))
15295     {
15296       expansion_failed (obj,
15297                         NULL_RTX, "no indirect ref in inner refrence");
15298       return 0;
15299     }
15300   if (!offset && !bitpos)
15301     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15302   else if (toplev
15303            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15304            && (dwarf_version >= 4 || !dwarf_strict))
15305     {
15306       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15307       if (!list_ret)
15308         return 0;
15309       if (offset)
15310         {
15311           /* Variable offset.  */
15312           list_ret1 = loc_list_from_tree (offset, 0);
15313           if (list_ret1 == 0)
15314             return 0;
15315           add_loc_list (&list_ret, list_ret1);
15316           if (!list_ret)
15317             return 0;
15318           add_loc_descr_to_each (list_ret,
15319                                  new_loc_descr (DW_OP_plus, 0, 0));
15320         }
15321       bytepos = bitpos / BITS_PER_UNIT;
15322       if (bytepos > 0)
15323         add_loc_descr_to_each (list_ret,
15324                                new_loc_descr (DW_OP_plus_uconst,
15325                                               bytepos, 0));
15326       else if (bytepos < 0)
15327         loc_list_plus_const (list_ret, bytepos);
15328       add_loc_descr_to_each (list_ret,
15329                              new_loc_descr (DW_OP_stack_value, 0, 0));
15330     }
15331   return list_ret;
15332 }
15333
15334
15335 /* Generate Dwarf location list representing LOC.
15336    If WANT_ADDRESS is false, expression computing LOC will be computed
15337    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15338    if WANT_ADDRESS is 2, expression computing address useable in location
15339      will be returned (i.e. DW_OP_reg can be used
15340      to refer to register values).  */
15341
15342 static dw_loc_list_ref
15343 loc_list_from_tree (tree loc, int want_address)
15344 {
15345   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15346   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15347   int have_address = 0;
15348   enum dwarf_location_atom op;
15349
15350   /* ??? Most of the time we do not take proper care for sign/zero
15351      extending the values properly.  Hopefully this won't be a real
15352      problem...  */
15353
15354   switch (TREE_CODE (loc))
15355     {
15356     case ERROR_MARK:
15357       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15358       return 0;
15359
15360     case PLACEHOLDER_EXPR:
15361       /* This case involves extracting fields from an object to determine the
15362          position of other fields.  We don't try to encode this here.  The
15363          only user of this is Ada, which encodes the needed information using
15364          the names of types.  */
15365       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15366       return 0;
15367
15368     case CALL_EXPR:
15369       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15370       /* There are no opcodes for these operations.  */
15371       return 0;
15372
15373     case PREINCREMENT_EXPR:
15374     case PREDECREMENT_EXPR:
15375     case POSTINCREMENT_EXPR:
15376     case POSTDECREMENT_EXPR:
15377       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15378       /* There are no opcodes for these operations.  */
15379       return 0;
15380
15381     case ADDR_EXPR:
15382       /* If we already want an address, see if there is INDIRECT_REF inside
15383          e.g. for &this->field.  */
15384       if (want_address)
15385         {
15386           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15387                        (loc, want_address == 2);
15388           if (list_ret)
15389             have_address = 1;
15390           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15391                    && (ret = cst_pool_loc_descr (loc)))
15392             have_address = 1;
15393         }
15394         /* Otherwise, process the argument and look for the address.  */
15395       if (!list_ret && !ret)
15396         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15397       else
15398         {
15399           if (want_address)
15400             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15401           return NULL;
15402         }
15403       break;
15404
15405     case VAR_DECL:
15406       if (DECL_THREAD_LOCAL_P (loc))
15407         {
15408           rtx rtl;
15409           enum dwarf_location_atom first_op;
15410           enum dwarf_location_atom second_op;
15411           bool dtprel = false;
15412
15413           if (targetm.have_tls)
15414             {
15415               /* If this is not defined, we have no way to emit the
15416                  data.  */
15417               if (!targetm.asm_out.output_dwarf_dtprel)
15418                 return 0;
15419
15420                /* The way DW_OP_GNU_push_tls_address is specified, we
15421                   can only look up addresses of objects in the current
15422                   module.  We used DW_OP_addr as first op, but that's
15423                   wrong, because DW_OP_addr is relocated by the debug
15424                   info consumer, while DW_OP_GNU_push_tls_address
15425                   operand shouldn't be.  */
15426               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15427                 return 0;
15428               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15429               dtprel = true;
15430               second_op = DW_OP_GNU_push_tls_address;
15431             }
15432           else
15433             {
15434               if (!targetm.emutls.debug_form_tls_address
15435                   || !(dwarf_version >= 3 || !dwarf_strict))
15436                 return 0;
15437               /* We stuffed the control variable into the DECL_VALUE_EXPR
15438                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15439                  no longer appear in gimple code.  We used the control
15440                  variable in specific so that we could pick it up here.  */
15441               loc = DECL_VALUE_EXPR (loc);
15442               first_op = DW_OP_addr;
15443               second_op = DW_OP_form_tls_address;
15444             }
15445
15446           rtl = rtl_for_decl_location (loc);
15447           if (rtl == NULL_RTX)
15448             return 0;
15449
15450           if (!MEM_P (rtl))
15451             return 0;
15452           rtl = XEXP (rtl, 0);
15453           if (! CONSTANT_P (rtl))
15454             return 0;
15455
15456           ret = new_loc_descr (first_op, 0, 0);
15457           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15458           ret->dw_loc_oprnd1.v.val_addr = rtl;
15459           ret->dtprel = dtprel;
15460
15461           ret1 = new_loc_descr (second_op, 0, 0);
15462           add_loc_descr (&ret, ret1);
15463
15464           have_address = 1;
15465           break;
15466         }
15467       /* FALLTHRU */
15468
15469     case PARM_DECL:
15470     case RESULT_DECL:
15471       if (DECL_HAS_VALUE_EXPR_P (loc))
15472         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15473                                    want_address);
15474       /* FALLTHRU */
15475
15476     case FUNCTION_DECL:
15477       {
15478         rtx rtl;
15479         var_loc_list *loc_list = lookup_decl_loc (loc);
15480
15481         if (loc_list && loc_list->first)
15482           {
15483             list_ret = dw_loc_list (loc_list, loc, want_address);
15484             have_address = want_address != 0;
15485             break;
15486           }
15487         rtl = rtl_for_decl_location (loc);
15488         if (rtl == NULL_RTX)
15489           {
15490             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15491             return 0;
15492           }
15493         else if (CONST_INT_P (rtl))
15494           {
15495             HOST_WIDE_INT val = INTVAL (rtl);
15496             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15497               val &= GET_MODE_MASK (DECL_MODE (loc));
15498             ret = int_loc_descriptor (val);
15499           }
15500         else if (GET_CODE (rtl) == CONST_STRING)
15501           {
15502             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15503             return 0;
15504           }
15505         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15506           {
15507             ret = new_loc_descr (DW_OP_addr, 0, 0);
15508             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15509             ret->dw_loc_oprnd1.v.val_addr = rtl;
15510           }
15511         else
15512           {
15513             enum machine_mode mode;
15514
15515             /* Certain constructs can only be represented at top-level.  */
15516             if (want_address == 2)
15517               {
15518                 ret = loc_descriptor (rtl, VOIDmode,
15519                                       VAR_INIT_STATUS_INITIALIZED);
15520                 have_address = 1;
15521               }
15522             else
15523               {
15524                 mode = GET_MODE (rtl);
15525                 if (MEM_P (rtl))
15526                   {
15527                     rtl = XEXP (rtl, 0);
15528                     have_address = 1;
15529                   }
15530                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15531               }
15532             if (!ret)
15533               expansion_failed (loc, rtl,
15534                                 "failed to produce loc descriptor for rtl");
15535           }
15536       }
15537       break;
15538
15539     case MEM_REF:
15540       /* ??? FIXME.  */
15541       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15542         return 0;
15543       /* Fallthru.  */
15544     case INDIRECT_REF:
15545       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15546       have_address = 1;
15547       break;
15548
15549     case COMPOUND_EXPR:
15550       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15551
15552     CASE_CONVERT:
15553     case VIEW_CONVERT_EXPR:
15554     case SAVE_EXPR:
15555     case MODIFY_EXPR:
15556       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15557
15558     case COMPONENT_REF:
15559     case BIT_FIELD_REF:
15560     case ARRAY_REF:
15561     case ARRAY_RANGE_REF:
15562     case REALPART_EXPR:
15563     case IMAGPART_EXPR:
15564       {
15565         tree obj, offset;
15566         HOST_WIDE_INT bitsize, bitpos, bytepos;
15567         enum machine_mode mode;
15568         int volatilep;
15569         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15570
15571         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15572                                    &unsignedp, &volatilep, false);
15573
15574         gcc_assert (obj != loc);
15575
15576         list_ret = loc_list_from_tree (obj,
15577                                        want_address == 2
15578                                        && !bitpos && !offset ? 2 : 1);
15579         /* TODO: We can extract value of the small expression via shifting even
15580            for nonzero bitpos.  */
15581         if (list_ret == 0)
15582           return 0;
15583         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15584           {
15585             expansion_failed (loc, NULL_RTX,
15586                               "bitfield access");
15587             return 0;
15588           }
15589
15590         if (offset != NULL_TREE)
15591           {
15592             /* Variable offset.  */
15593             list_ret1 = loc_list_from_tree (offset, 0);
15594             if (list_ret1 == 0)
15595               return 0;
15596             add_loc_list (&list_ret, list_ret1);
15597             if (!list_ret)
15598               return 0;
15599             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15600           }
15601
15602         bytepos = bitpos / BITS_PER_UNIT;
15603         if (bytepos > 0)
15604           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15605         else if (bytepos < 0)
15606           loc_list_plus_const (list_ret, bytepos);
15607
15608         have_address = 1;
15609         break;
15610       }
15611
15612     case INTEGER_CST:
15613       if ((want_address || !host_integerp (loc, 0))
15614           && (ret = cst_pool_loc_descr (loc)))
15615         have_address = 1;
15616       else if (want_address == 2
15617                && host_integerp (loc, 0)
15618                && (ret = address_of_int_loc_descriptor
15619                            (int_size_in_bytes (TREE_TYPE (loc)),
15620                             tree_low_cst (loc, 0))))
15621         have_address = 1;
15622       else if (host_integerp (loc, 0))
15623         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15624       else
15625         {
15626           expansion_failed (loc, NULL_RTX,
15627                             "Integer operand is not host integer");
15628           return 0;
15629         }
15630       break;
15631
15632     case CONSTRUCTOR:
15633     case REAL_CST:
15634     case STRING_CST:
15635     case COMPLEX_CST:
15636       if ((ret = cst_pool_loc_descr (loc)))
15637         have_address = 1;
15638       else
15639       /* We can construct small constants here using int_loc_descriptor.  */
15640         expansion_failed (loc, NULL_RTX,
15641                           "constructor or constant not in constant pool");
15642       break;
15643
15644     case TRUTH_AND_EXPR:
15645     case TRUTH_ANDIF_EXPR:
15646     case BIT_AND_EXPR:
15647       op = DW_OP_and;
15648       goto do_binop;
15649
15650     case TRUTH_XOR_EXPR:
15651     case BIT_XOR_EXPR:
15652       op = DW_OP_xor;
15653       goto do_binop;
15654
15655     case TRUTH_OR_EXPR:
15656     case TRUTH_ORIF_EXPR:
15657     case BIT_IOR_EXPR:
15658       op = DW_OP_or;
15659       goto do_binop;
15660
15661     case FLOOR_DIV_EXPR:
15662     case CEIL_DIV_EXPR:
15663     case ROUND_DIV_EXPR:
15664     case TRUNC_DIV_EXPR:
15665       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15666         return 0;
15667       op = DW_OP_div;
15668       goto do_binop;
15669
15670     case MINUS_EXPR:
15671       op = DW_OP_minus;
15672       goto do_binop;
15673
15674     case FLOOR_MOD_EXPR:
15675     case CEIL_MOD_EXPR:
15676     case ROUND_MOD_EXPR:
15677     case TRUNC_MOD_EXPR:
15678       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15679         {
15680           op = DW_OP_mod;
15681           goto do_binop;
15682         }
15683       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15684       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15685       if (list_ret == 0 || list_ret1 == 0)
15686         return 0;
15687
15688       add_loc_list (&list_ret, list_ret1);
15689       if (list_ret == 0)
15690         return 0;
15691       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15692       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15693       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15694       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15695       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15696       break;
15697
15698     case MULT_EXPR:
15699       op = DW_OP_mul;
15700       goto do_binop;
15701
15702     case LSHIFT_EXPR:
15703       op = DW_OP_shl;
15704       goto do_binop;
15705
15706     case RSHIFT_EXPR:
15707       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15708       goto do_binop;
15709
15710     case POINTER_PLUS_EXPR:
15711     case PLUS_EXPR:
15712       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15713         {
15714           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15715           if (list_ret == 0)
15716             return 0;
15717
15718           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15719           break;
15720         }
15721
15722       op = DW_OP_plus;
15723       goto do_binop;
15724
15725     case LE_EXPR:
15726       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15727         return 0;
15728
15729       op = DW_OP_le;
15730       goto do_binop;
15731
15732     case GE_EXPR:
15733       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15734         return 0;
15735
15736       op = DW_OP_ge;
15737       goto do_binop;
15738
15739     case LT_EXPR:
15740       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15741         return 0;
15742
15743       op = DW_OP_lt;
15744       goto do_binop;
15745
15746     case GT_EXPR:
15747       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15748         return 0;
15749
15750       op = DW_OP_gt;
15751       goto do_binop;
15752
15753     case EQ_EXPR:
15754       op = DW_OP_eq;
15755       goto do_binop;
15756
15757     case NE_EXPR:
15758       op = DW_OP_ne;
15759       goto do_binop;
15760
15761     do_binop:
15762       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15763       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15764       if (list_ret == 0 || list_ret1 == 0)
15765         return 0;
15766
15767       add_loc_list (&list_ret, list_ret1);
15768       if (list_ret == 0)
15769         return 0;
15770       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15771       break;
15772
15773     case TRUTH_NOT_EXPR:
15774     case BIT_NOT_EXPR:
15775       op = DW_OP_not;
15776       goto do_unop;
15777
15778     case ABS_EXPR:
15779       op = DW_OP_abs;
15780       goto do_unop;
15781
15782     case NEGATE_EXPR:
15783       op = DW_OP_neg;
15784       goto do_unop;
15785
15786     do_unop:
15787       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15788       if (list_ret == 0)
15789         return 0;
15790
15791       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15792       break;
15793
15794     case MIN_EXPR:
15795     case MAX_EXPR:
15796       {
15797         const enum tree_code code =
15798           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15799
15800         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15801                       build2 (code, integer_type_node,
15802                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15803                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15804       }
15805
15806       /* ... fall through ...  */
15807
15808     case COND_EXPR:
15809       {
15810         dw_loc_descr_ref lhs
15811           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15812         dw_loc_list_ref rhs
15813           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15814         dw_loc_descr_ref bra_node, jump_node, tmp;
15815
15816         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15817         if (list_ret == 0 || lhs == 0 || rhs == 0)
15818           return 0;
15819
15820         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15821         add_loc_descr_to_each (list_ret, bra_node);
15822
15823         add_loc_list (&list_ret, rhs);
15824         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15825         add_loc_descr_to_each (list_ret, jump_node);
15826
15827         add_loc_descr_to_each (list_ret, lhs);
15828         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15829         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15830
15831         /* ??? Need a node to point the skip at.  Use a nop.  */
15832         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15833         add_loc_descr_to_each (list_ret, tmp);
15834         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15835         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15836       }
15837       break;
15838
15839     case FIX_TRUNC_EXPR:
15840       return 0;
15841
15842     default:
15843       /* Leave front-end specific codes as simply unknown.  This comes
15844          up, for instance, with the C STMT_EXPR.  */
15845       if ((unsigned int) TREE_CODE (loc)
15846           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15847         {
15848           expansion_failed (loc, NULL_RTX,
15849                             "language specific tree node");
15850           return 0;
15851         }
15852
15853 #ifdef ENABLE_CHECKING
15854       /* Otherwise this is a generic code; we should just lists all of
15855          these explicitly.  We forgot one.  */
15856       gcc_unreachable ();
15857 #else
15858       /* In a release build, we want to degrade gracefully: better to
15859          generate incomplete debugging information than to crash.  */
15860       return NULL;
15861 #endif
15862     }
15863
15864   if (!ret && !list_ret)
15865     return 0;
15866
15867   if (want_address == 2 && !have_address
15868       && (dwarf_version >= 4 || !dwarf_strict))
15869     {
15870       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15871         {
15872           expansion_failed (loc, NULL_RTX,
15873                             "DWARF address size mismatch");
15874           return 0;
15875         }
15876       if (ret)
15877         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15878       else
15879         add_loc_descr_to_each (list_ret,
15880                                new_loc_descr (DW_OP_stack_value, 0, 0));
15881       have_address = 1;
15882     }
15883   /* Show if we can't fill the request for an address.  */
15884   if (want_address && !have_address)
15885     {
15886       expansion_failed (loc, NULL_RTX,
15887                         "Want address and only have value");
15888       return 0;
15889     }
15890
15891   gcc_assert (!ret || !list_ret);
15892
15893   /* If we've got an address and don't want one, dereference.  */
15894   if (!want_address && have_address)
15895     {
15896       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15897
15898       if (size > DWARF2_ADDR_SIZE || size == -1)
15899         {
15900           expansion_failed (loc, NULL_RTX,
15901                             "DWARF address size mismatch");
15902           return 0;
15903         }
15904       else if (size == DWARF2_ADDR_SIZE)
15905         op = DW_OP_deref;
15906       else
15907         op = DW_OP_deref_size;
15908
15909       if (ret)
15910         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15911       else
15912         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15913     }
15914   if (ret)
15915     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15916
15917   return list_ret;
15918 }
15919
15920 /* Same as above but return only single location expression.  */
15921 static dw_loc_descr_ref
15922 loc_descriptor_from_tree (tree loc, int want_address)
15923 {
15924   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15925   if (!ret)
15926     return NULL;
15927   if (ret->dw_loc_next)
15928     {
15929       expansion_failed (loc, NULL_RTX,
15930                         "Location list where only loc descriptor needed");
15931       return NULL;
15932     }
15933   return ret->expr;
15934 }
15935
15936 /* Given a value, round it up to the lowest multiple of `boundary'
15937    which is not less than the value itself.  */
15938
15939 static inline HOST_WIDE_INT
15940 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15941 {
15942   return (((value + boundary - 1) / boundary) * boundary);
15943 }
15944
15945 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15946    pointer to the declared type for the relevant field variable, or return
15947    `integer_type_node' if the given node turns out to be an
15948    ERROR_MARK node.  */
15949
15950 static inline tree
15951 field_type (const_tree decl)
15952 {
15953   tree type;
15954
15955   if (TREE_CODE (decl) == ERROR_MARK)
15956     return integer_type_node;
15957
15958   type = DECL_BIT_FIELD_TYPE (decl);
15959   if (type == NULL_TREE)
15960     type = TREE_TYPE (decl);
15961
15962   return type;
15963 }
15964
15965 /* Given a pointer to a tree node, return the alignment in bits for
15966    it, or else return BITS_PER_WORD if the node actually turns out to
15967    be an ERROR_MARK node.  */
15968
15969 static inline unsigned
15970 simple_type_align_in_bits (const_tree type)
15971 {
15972   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15973 }
15974
15975 static inline unsigned
15976 simple_decl_align_in_bits (const_tree decl)
15977 {
15978   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15979 }
15980
15981 /* Return the result of rounding T up to ALIGN.  */
15982
15983 static inline double_int
15984 round_up_to_align (double_int t, unsigned int align)
15985 {
15986   double_int alignd = uhwi_to_double_int (align);
15987   t = double_int_add (t, alignd);
15988   t = double_int_add (t, double_int_minus_one);
15989   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15990   t = double_int_mul (t, alignd);
15991   return t;
15992 }
15993
15994 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15995    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15996    or return 0 if we are unable to determine what that offset is, either
15997    because the argument turns out to be a pointer to an ERROR_MARK node, or
15998    because the offset is actually variable.  (We can't handle the latter case
15999    just yet).  */
16000
16001 static HOST_WIDE_INT
16002 field_byte_offset (const_tree decl)
16003 {
16004   double_int object_offset_in_bits;
16005   double_int object_offset_in_bytes;
16006   double_int bitpos_int;
16007
16008   if (TREE_CODE (decl) == ERROR_MARK)
16009     return 0;
16010
16011   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16012
16013   /* We cannot yet cope with fields whose positions are variable, so
16014      for now, when we see such things, we simply return 0.  Someday, we may
16015      be able to handle such cases, but it will be damn difficult.  */
16016   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16017     return 0;
16018
16019   bitpos_int = tree_to_double_int (bit_position (decl));
16020
16021 #ifdef PCC_BITFIELD_TYPE_MATTERS
16022   if (PCC_BITFIELD_TYPE_MATTERS)
16023     {
16024       tree type;
16025       tree field_size_tree;
16026       double_int deepest_bitpos;
16027       double_int field_size_in_bits;
16028       unsigned int type_align_in_bits;
16029       unsigned int decl_align_in_bits;
16030       double_int type_size_in_bits;
16031
16032       type = field_type (decl);
16033       type_size_in_bits = double_int_type_size_in_bits (type);
16034       type_align_in_bits = simple_type_align_in_bits (type);
16035
16036       field_size_tree = DECL_SIZE (decl);
16037
16038       /* The size could be unspecified if there was an error, or for
16039          a flexible array member.  */
16040       if (!field_size_tree)
16041         field_size_tree = bitsize_zero_node;
16042
16043       /* If the size of the field is not constant, use the type size.  */
16044       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16045         field_size_in_bits = tree_to_double_int (field_size_tree);
16046       else
16047         field_size_in_bits = type_size_in_bits;
16048
16049       decl_align_in_bits = simple_decl_align_in_bits (decl);
16050
16051       /* The GCC front-end doesn't make any attempt to keep track of the
16052          starting bit offset (relative to the start of the containing
16053          structure type) of the hypothetical "containing object" for a
16054          bit-field.  Thus, when computing the byte offset value for the
16055          start of the "containing object" of a bit-field, we must deduce
16056          this information on our own. This can be rather tricky to do in
16057          some cases.  For example, handling the following structure type
16058          definition when compiling for an i386/i486 target (which only
16059          aligns long long's to 32-bit boundaries) can be very tricky:
16060
16061          struct S { int field1; long long field2:31; };
16062
16063          Fortunately, there is a simple rule-of-thumb which can be used
16064          in such cases.  When compiling for an i386/i486, GCC will
16065          allocate 8 bytes for the structure shown above.  It decides to
16066          do this based upon one simple rule for bit-field allocation.
16067          GCC allocates each "containing object" for each bit-field at
16068          the first (i.e. lowest addressed) legitimate alignment boundary
16069          (based upon the required minimum alignment for the declared
16070          type of the field) which it can possibly use, subject to the
16071          condition that there is still enough available space remaining
16072          in the containing object (when allocated at the selected point)
16073          to fully accommodate all of the bits of the bit-field itself.
16074
16075          This simple rule makes it obvious why GCC allocates 8 bytes for
16076          each object of the structure type shown above.  When looking
16077          for a place to allocate the "containing object" for `field2',
16078          the compiler simply tries to allocate a 64-bit "containing
16079          object" at each successive 32-bit boundary (starting at zero)
16080          until it finds a place to allocate that 64- bit field such that
16081          at least 31 contiguous (and previously unallocated) bits remain
16082          within that selected 64 bit field.  (As it turns out, for the
16083          example above, the compiler finds it is OK to allocate the
16084          "containing object" 64-bit field at bit-offset zero within the
16085          structure type.)
16086
16087          Here we attempt to work backwards from the limited set of facts
16088          we're given, and we try to deduce from those facts, where GCC
16089          must have believed that the containing object started (within
16090          the structure type). The value we deduce is then used (by the
16091          callers of this routine) to generate DW_AT_location and
16092          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16093          the case of DW_AT_location, regular fields as well).  */
16094
16095       /* Figure out the bit-distance from the start of the structure to
16096          the "deepest" bit of the bit-field.  */
16097       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16098
16099       /* This is the tricky part.  Use some fancy footwork to deduce
16100          where the lowest addressed bit of the containing object must
16101          be.  */
16102       object_offset_in_bits
16103         = double_int_sub (deepest_bitpos, type_size_in_bits);
16104
16105       /* Round up to type_align by default.  This works best for
16106          bitfields.  */
16107       object_offset_in_bits
16108         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16109
16110       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16111         {
16112           object_offset_in_bits
16113             = double_int_sub (deepest_bitpos, type_size_in_bits);
16114
16115           /* Round up to decl_align instead.  */
16116           object_offset_in_bits
16117             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16118         }
16119     }
16120   else
16121 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16122     object_offset_in_bits = bitpos_int;
16123
16124   object_offset_in_bytes
16125     = double_int_div (object_offset_in_bits,
16126                       uhwi_to_double_int (BITS_PER_UNIT), true,
16127                       TRUNC_DIV_EXPR);
16128   return double_int_to_shwi (object_offset_in_bytes);
16129 }
16130 \f
16131 /* The following routines define various Dwarf attributes and any data
16132    associated with them.  */
16133
16134 /* Add a location description attribute value to a DIE.
16135
16136    This emits location attributes suitable for whole variables and
16137    whole parameters.  Note that the location attributes for struct fields are
16138    generated by the routine `data_member_location_attribute' below.  */
16139
16140 static inline void
16141 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16142                              dw_loc_list_ref descr)
16143 {
16144   if (descr == 0)
16145     return;
16146   if (single_element_loc_list_p (descr))
16147     add_AT_loc (die, attr_kind, descr->expr);
16148   else
16149     add_AT_loc_list (die, attr_kind, descr);
16150 }
16151
16152 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16153
16154 static void
16155 add_accessibility_attribute (dw_die_ref die, tree decl)
16156 {
16157   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16158      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16159      the default has always been DW_ACCESS_public.  */
16160   if (TREE_PROTECTED (decl))
16161     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16162   else if (TREE_PRIVATE (decl))
16163     {
16164       if (dwarf_version == 2
16165           || die->die_parent == NULL
16166           || die->die_parent->die_tag != DW_TAG_class_type)
16167         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16168     }
16169   else if (dwarf_version > 2
16170            && die->die_parent
16171            && die->die_parent->die_tag == DW_TAG_class_type)
16172     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16173 }
16174
16175 /* Attach the specialized form of location attribute used for data members of
16176    struct and union types.  In the special case of a FIELD_DECL node which
16177    represents a bit-field, the "offset" part of this special location
16178    descriptor must indicate the distance in bytes from the lowest-addressed
16179    byte of the containing struct or union type to the lowest-addressed byte of
16180    the "containing object" for the bit-field.  (See the `field_byte_offset'
16181    function above).
16182
16183    For any given bit-field, the "containing object" is a hypothetical object
16184    (of some integral or enum type) within which the given bit-field lives.  The
16185    type of this hypothetical "containing object" is always the same as the
16186    declared type of the individual bit-field itself (for GCC anyway... the
16187    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16188    bytes) of the hypothetical "containing object" which will be given in the
16189    DW_AT_byte_size attribute for this bit-field.  (See the
16190    `byte_size_attribute' function below.)  It is also used when calculating the
16191    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16192    function below.)  */
16193
16194 static void
16195 add_data_member_location_attribute (dw_die_ref die, tree decl)
16196 {
16197   HOST_WIDE_INT offset;
16198   dw_loc_descr_ref loc_descr = 0;
16199
16200   if (TREE_CODE (decl) == TREE_BINFO)
16201     {
16202       /* We're working on the TAG_inheritance for a base class.  */
16203       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16204         {
16205           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16206              aren't at a fixed offset from all (sub)objects of the same
16207              type.  We need to extract the appropriate offset from our
16208              vtable.  The following dwarf expression means
16209
16210                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16211
16212              This is specific to the V3 ABI, of course.  */
16213
16214           dw_loc_descr_ref tmp;
16215
16216           /* Make a copy of the object address.  */
16217           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16218           add_loc_descr (&loc_descr, tmp);
16219
16220           /* Extract the vtable address.  */
16221           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16222           add_loc_descr (&loc_descr, tmp);
16223
16224           /* Calculate the address of the offset.  */
16225           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16226           gcc_assert (offset < 0);
16227
16228           tmp = int_loc_descriptor (-offset);
16229           add_loc_descr (&loc_descr, tmp);
16230           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16231           add_loc_descr (&loc_descr, tmp);
16232
16233           /* Extract the offset.  */
16234           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16235           add_loc_descr (&loc_descr, tmp);
16236
16237           /* Add it to the object address.  */
16238           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16239           add_loc_descr (&loc_descr, tmp);
16240         }
16241       else
16242         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16243     }
16244   else
16245     offset = field_byte_offset (decl);
16246
16247   if (! loc_descr)
16248     {
16249       if (dwarf_version > 2)
16250         {
16251           /* Don't need to output a location expression, just the constant. */
16252           if (offset < 0)
16253             add_AT_int (die, DW_AT_data_member_location, offset);
16254           else
16255             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16256           return;
16257         }
16258       else
16259         {
16260           enum dwarf_location_atom op;
16261
16262           /* The DWARF2 standard says that we should assume that the structure
16263              address is already on the stack, so we can specify a structure
16264              field address by using DW_OP_plus_uconst.  */
16265
16266 #ifdef MIPS_DEBUGGING_INFO
16267           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16268              operator correctly.  It works only if we leave the offset on the
16269              stack.  */
16270           op = DW_OP_constu;
16271 #else
16272           op = DW_OP_plus_uconst;
16273 #endif
16274
16275           loc_descr = new_loc_descr (op, offset, 0);
16276         }
16277     }
16278
16279   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16280 }
16281
16282 /* Writes integer values to dw_vec_const array.  */
16283
16284 static void
16285 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16286 {
16287   while (size != 0)
16288     {
16289       *dest++ = val & 0xff;
16290       val >>= 8;
16291       --size;
16292     }
16293 }
16294
16295 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16296
16297 static HOST_WIDE_INT
16298 extract_int (const unsigned char *src, unsigned int size)
16299 {
16300   HOST_WIDE_INT val = 0;
16301
16302   src += size;
16303   while (size != 0)
16304     {
16305       val <<= 8;
16306       val |= *--src & 0xff;
16307       --size;
16308     }
16309   return val;
16310 }
16311
16312 /* Writes double_int values to dw_vec_const array.  */
16313
16314 static void
16315 insert_double (double_int val, unsigned char *dest)
16316 {
16317   unsigned char *p0 = dest;
16318   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16319
16320   if (WORDS_BIG_ENDIAN)
16321     {
16322       p0 = p1;
16323       p1 = dest;
16324     }
16325
16326   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16327   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16328 }
16329
16330 /* Writes floating point values to dw_vec_const array.  */
16331
16332 static void
16333 insert_float (const_rtx rtl, unsigned char *array)
16334 {
16335   REAL_VALUE_TYPE rv;
16336   long val[4];
16337   int i;
16338
16339   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16340   real_to_target (val, &rv, GET_MODE (rtl));
16341
16342   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16343   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16344     {
16345       insert_int (val[i], 4, array);
16346       array += 4;
16347     }
16348 }
16349
16350 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16351    does not have a "location" either in memory or in a register.  These
16352    things can arise in GNU C when a constant is passed as an actual parameter
16353    to an inlined function.  They can also arise in C++ where declared
16354    constants do not necessarily get memory "homes".  */
16355
16356 static bool
16357 add_const_value_attribute (dw_die_ref die, rtx rtl)
16358 {
16359   switch (GET_CODE (rtl))
16360     {
16361     case CONST_INT:
16362       {
16363         HOST_WIDE_INT val = INTVAL (rtl);
16364
16365         if (val < 0)
16366           add_AT_int (die, DW_AT_const_value, val);
16367         else
16368           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16369       }
16370       return true;
16371
16372     case CONST_DOUBLE:
16373       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16374          floating-point constant.  A CONST_DOUBLE is used whenever the
16375          constant requires more than one word in order to be adequately
16376          represented.  */
16377       {
16378         enum machine_mode mode = GET_MODE (rtl);
16379
16380         if (SCALAR_FLOAT_MODE_P (mode))
16381           {
16382             unsigned int length = GET_MODE_SIZE (mode);
16383             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16384
16385             insert_float (rtl, array);
16386             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16387           }
16388         else
16389           add_AT_double (die, DW_AT_const_value,
16390                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16391       }
16392       return true;
16393
16394     case CONST_VECTOR:
16395       {
16396         enum machine_mode mode = GET_MODE (rtl);
16397         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16398         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16399         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16400           (length * elt_size);
16401         unsigned int i;
16402         unsigned char *p;
16403
16404         switch (GET_MODE_CLASS (mode))
16405           {
16406           case MODE_VECTOR_INT:
16407             for (i = 0, p = array; i < length; i++, p += elt_size)
16408               {
16409                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16410                 double_int val = rtx_to_double_int (elt);
16411
16412                 if (elt_size <= sizeof (HOST_WIDE_INT))
16413                   insert_int (double_int_to_shwi (val), elt_size, p);
16414                 else
16415                   {
16416                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16417                     insert_double (val, p);
16418                   }
16419               }
16420             break;
16421
16422           case MODE_VECTOR_FLOAT:
16423             for (i = 0, p = array; i < length; i++, p += elt_size)
16424               {
16425                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16426                 insert_float (elt, p);
16427               }
16428             break;
16429
16430           default:
16431             gcc_unreachable ();
16432           }
16433
16434         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16435       }
16436       return true;
16437
16438     case CONST_STRING:
16439       if (dwarf_version >= 4 || !dwarf_strict)
16440         {
16441           dw_loc_descr_ref loc_result;
16442           resolve_one_addr (&rtl, NULL);
16443         rtl_addr:
16444           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16445           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16446           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16447           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16448           add_AT_loc (die, DW_AT_location, loc_result);
16449           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16450           return true;
16451         }
16452       return false;
16453
16454     case CONST:
16455       if (CONSTANT_P (XEXP (rtl, 0)))
16456         return add_const_value_attribute (die, XEXP (rtl, 0));
16457       /* FALLTHROUGH */
16458     case SYMBOL_REF:
16459       if (!const_ok_for_output (rtl))
16460         return false;
16461     case LABEL_REF:
16462       if (dwarf_version >= 4 || !dwarf_strict)
16463         goto rtl_addr;
16464       return false;
16465
16466     case PLUS:
16467       /* In cases where an inlined instance of an inline function is passed
16468          the address of an `auto' variable (which is local to the caller) we
16469          can get a situation where the DECL_RTL of the artificial local
16470          variable (for the inlining) which acts as a stand-in for the
16471          corresponding formal parameter (of the inline function) will look
16472          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16473          exactly a compile-time constant expression, but it isn't the address
16474          of the (artificial) local variable either.  Rather, it represents the
16475          *value* which the artificial local variable always has during its
16476          lifetime.  We currently have no way to represent such quasi-constant
16477          values in Dwarf, so for now we just punt and generate nothing.  */
16478       return false;
16479
16480     case HIGH:
16481     case CONST_FIXED:
16482       return false;
16483
16484     case MEM:
16485       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16486           && MEM_READONLY_P (rtl)
16487           && GET_MODE (rtl) == BLKmode)
16488         {
16489           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16490           return true;
16491         }
16492       return false;
16493
16494     default:
16495       /* No other kinds of rtx should be possible here.  */
16496       gcc_unreachable ();
16497     }
16498   return false;
16499 }
16500
16501 /* Determine whether the evaluation of EXPR references any variables
16502    or functions which aren't otherwise used (and therefore may not be
16503    output).  */
16504 static tree
16505 reference_to_unused (tree * tp, int * walk_subtrees,
16506                      void * data ATTRIBUTE_UNUSED)
16507 {
16508   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16509     *walk_subtrees = 0;
16510
16511   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16512       && ! TREE_ASM_WRITTEN (*tp))
16513     return *tp;
16514   /* ???  The C++ FE emits debug information for using decls, so
16515      putting gcc_unreachable here falls over.  See PR31899.  For now
16516      be conservative.  */
16517   else if (!cgraph_global_info_ready
16518            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16519     return *tp;
16520   else if (TREE_CODE (*tp) == VAR_DECL)
16521     {
16522       struct varpool_node *node = varpool_get_node (*tp);
16523       if (!node || !node->needed)
16524         return *tp;
16525     }
16526   else if (TREE_CODE (*tp) == FUNCTION_DECL
16527            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16528     {
16529       /* The call graph machinery must have finished analyzing,
16530          optimizing and gimplifying the CU by now.
16531          So if *TP has no call graph node associated
16532          to it, it means *TP will not be emitted.  */
16533       if (!cgraph_get_node (*tp))
16534         return *tp;
16535     }
16536   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16537     return *tp;
16538
16539   return NULL_TREE;
16540 }
16541
16542 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16543    for use in a later add_const_value_attribute call.  */
16544
16545 static rtx
16546 rtl_for_decl_init (tree init, tree type)
16547 {
16548   rtx rtl = NULL_RTX;
16549
16550   STRIP_NOPS (init);
16551
16552   /* If a variable is initialized with a string constant without embedded
16553      zeros, build CONST_STRING.  */
16554   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16555     {
16556       tree enttype = TREE_TYPE (type);
16557       tree domain = TYPE_DOMAIN (type);
16558       enum machine_mode mode = TYPE_MODE (enttype);
16559
16560       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16561           && domain
16562           && integer_zerop (TYPE_MIN_VALUE (domain))
16563           && compare_tree_int (TYPE_MAX_VALUE (domain),
16564                                TREE_STRING_LENGTH (init) - 1) == 0
16565           && ((size_t) TREE_STRING_LENGTH (init)
16566               == strlen (TREE_STRING_POINTER (init)) + 1))
16567         {
16568           rtl = gen_rtx_CONST_STRING (VOIDmode,
16569                                       ggc_strdup (TREE_STRING_POINTER (init)));
16570           rtl = gen_rtx_MEM (BLKmode, rtl);
16571           MEM_READONLY_P (rtl) = 1;
16572         }
16573     }
16574   /* Other aggregates, and complex values, could be represented using
16575      CONCAT: FIXME!  */
16576   else if (AGGREGATE_TYPE_P (type)
16577            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16578                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16579            || TREE_CODE (type) == COMPLEX_TYPE)
16580     ;
16581   /* Vectors only work if their mode is supported by the target.
16582      FIXME: generic vectors ought to work too.  */
16583   else if (TREE_CODE (type) == VECTOR_TYPE
16584            && !VECTOR_MODE_P (TYPE_MODE (type)))
16585     ;
16586   /* If the initializer is something that we know will expand into an
16587      immediate RTL constant, expand it now.  We must be careful not to
16588      reference variables which won't be output.  */
16589   else if (initializer_constant_valid_p (init, type)
16590            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16591     {
16592       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16593          possible.  */
16594       if (TREE_CODE (type) == VECTOR_TYPE)
16595         switch (TREE_CODE (init))
16596           {
16597           case VECTOR_CST:
16598             break;
16599           case CONSTRUCTOR:
16600             if (TREE_CONSTANT (init))
16601               {
16602                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16603                 bool constant_p = true;
16604                 tree value;
16605                 unsigned HOST_WIDE_INT ix;
16606
16607                 /* Even when ctor is constant, it might contain non-*_CST
16608                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16609                    belong into VECTOR_CST nodes.  */
16610                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16611                   if (!CONSTANT_CLASS_P (value))
16612                     {
16613                       constant_p = false;
16614                       break;
16615                     }
16616
16617                 if (constant_p)
16618                   {
16619                     init = build_vector_from_ctor (type, elts);
16620                     break;
16621                   }
16622               }
16623             /* FALLTHRU */
16624
16625           default:
16626             return NULL;
16627           }
16628
16629       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16630
16631       /* If expand_expr returns a MEM, it wasn't immediate.  */
16632       gcc_assert (!rtl || !MEM_P (rtl));
16633     }
16634
16635   return rtl;
16636 }
16637
16638 /* Generate RTL for the variable DECL to represent its location.  */
16639
16640 static rtx
16641 rtl_for_decl_location (tree decl)
16642 {
16643   rtx rtl;
16644
16645   /* Here we have to decide where we are going to say the parameter "lives"
16646      (as far as the debugger is concerned).  We only have a couple of
16647      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16648
16649      DECL_RTL normally indicates where the parameter lives during most of the
16650      activation of the function.  If optimization is enabled however, this
16651      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16652      that the parameter doesn't really live anywhere (as far as the code
16653      generation parts of GCC are concerned) during most of the function's
16654      activation.  That will happen (for example) if the parameter is never
16655      referenced within the function.
16656
16657      We could just generate a location descriptor here for all non-NULL
16658      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16659      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16660      where DECL_RTL is NULL or is a pseudo-reg.
16661
16662      Note however that we can only get away with using DECL_INCOMING_RTL as
16663      a backup substitute for DECL_RTL in certain limited cases.  In cases
16664      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16665      we can be sure that the parameter was passed using the same type as it is
16666      declared to have within the function, and that its DECL_INCOMING_RTL
16667      points us to a place where a value of that type is passed.
16668
16669      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16670      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16671      because in these cases DECL_INCOMING_RTL points us to a value of some
16672      type which is *different* from the type of the parameter itself.  Thus,
16673      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16674      such cases, the debugger would end up (for example) trying to fetch a
16675      `float' from a place which actually contains the first part of a
16676      `double'.  That would lead to really incorrect and confusing
16677      output at debug-time.
16678
16679      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16680      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16681      are a couple of exceptions however.  On little-endian machines we can
16682      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16683      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16684      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16685      when (on a little-endian machine) a non-prototyped function has a
16686      parameter declared to be of type `short' or `char'.  In such cases,
16687      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16688      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16689      passed `int' value.  If the debugger then uses that address to fetch
16690      a `short' or a `char' (on a little-endian machine) the result will be
16691      the correct data, so we allow for such exceptional cases below.
16692
16693      Note that our goal here is to describe the place where the given formal
16694      parameter lives during most of the function's activation (i.e. between the
16695      end of the prologue and the start of the epilogue).  We'll do that as best
16696      as we can. Note however that if the given formal parameter is modified
16697      sometime during the execution of the function, then a stack backtrace (at
16698      debug-time) will show the function as having been called with the *new*
16699      value rather than the value which was originally passed in.  This happens
16700      rarely enough that it is not a major problem, but it *is* a problem, and
16701      I'd like to fix it.
16702
16703      A future version of dwarf2out.c may generate two additional attributes for
16704      any given DW_TAG_formal_parameter DIE which will describe the "passed
16705      type" and the "passed location" for the given formal parameter in addition
16706      to the attributes we now generate to indicate the "declared type" and the
16707      "active location" for each parameter.  This additional set of attributes
16708      could be used by debuggers for stack backtraces. Separately, note that
16709      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16710      This happens (for example) for inlined-instances of inline function formal
16711      parameters which are never referenced.  This really shouldn't be
16712      happening.  All PARM_DECL nodes should get valid non-NULL
16713      DECL_INCOMING_RTL values.  FIXME.  */
16714
16715   /* Use DECL_RTL as the "location" unless we find something better.  */
16716   rtl = DECL_RTL_IF_SET (decl);
16717
16718   /* When generating abstract instances, ignore everything except
16719      constants, symbols living in memory, and symbols living in
16720      fixed registers.  */
16721   if (! reload_completed)
16722     {
16723       if (rtl
16724           && (CONSTANT_P (rtl)
16725               || (MEM_P (rtl)
16726                   && CONSTANT_P (XEXP (rtl, 0)))
16727               || (REG_P (rtl)
16728                   && TREE_CODE (decl) == VAR_DECL
16729                   && TREE_STATIC (decl))))
16730         {
16731           rtl = targetm.delegitimize_address (rtl);
16732           return rtl;
16733         }
16734       rtl = NULL_RTX;
16735     }
16736   else if (TREE_CODE (decl) == PARM_DECL)
16737     {
16738       if (rtl == NULL_RTX
16739           || is_pseudo_reg (rtl)
16740           || (MEM_P (rtl)
16741               && is_pseudo_reg (XEXP (rtl, 0))
16742               && DECL_INCOMING_RTL (decl)
16743               && MEM_P (DECL_INCOMING_RTL (decl))
16744               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16745         {
16746           tree declared_type = TREE_TYPE (decl);
16747           tree passed_type = DECL_ARG_TYPE (decl);
16748           enum machine_mode dmode = TYPE_MODE (declared_type);
16749           enum machine_mode pmode = TYPE_MODE (passed_type);
16750
16751           /* This decl represents a formal parameter which was optimized out.
16752              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16753              all cases where (rtl == NULL_RTX) just below.  */
16754           if (dmode == pmode)
16755             rtl = DECL_INCOMING_RTL (decl);
16756           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16757                    && SCALAR_INT_MODE_P (dmode)
16758                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16759                    && DECL_INCOMING_RTL (decl))
16760             {
16761               rtx inc = DECL_INCOMING_RTL (decl);
16762               if (REG_P (inc))
16763                 rtl = inc;
16764               else if (MEM_P (inc))
16765                 {
16766                   if (BYTES_BIG_ENDIAN)
16767                     rtl = adjust_address_nv (inc, dmode,
16768                                              GET_MODE_SIZE (pmode)
16769                                              - GET_MODE_SIZE (dmode));
16770                   else
16771                     rtl = inc;
16772                 }
16773             }
16774         }
16775
16776       /* If the parm was passed in registers, but lives on the stack, then
16777          make a big endian correction if the mode of the type of the
16778          parameter is not the same as the mode of the rtl.  */
16779       /* ??? This is the same series of checks that are made in dbxout.c before
16780          we reach the big endian correction code there.  It isn't clear if all
16781          of these checks are necessary here, but keeping them all is the safe
16782          thing to do.  */
16783       else if (MEM_P (rtl)
16784                && XEXP (rtl, 0) != const0_rtx
16785                && ! CONSTANT_P (XEXP (rtl, 0))
16786                /* Not passed in memory.  */
16787                && !MEM_P (DECL_INCOMING_RTL (decl))
16788                /* Not passed by invisible reference.  */
16789                && (!REG_P (XEXP (rtl, 0))
16790                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16791                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16792 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16793                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16794 #endif
16795                      )
16796                /* Big endian correction check.  */
16797                && BYTES_BIG_ENDIAN
16798                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16799                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16800                    < UNITS_PER_WORD))
16801         {
16802           int offset = (UNITS_PER_WORD
16803                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16804
16805           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16806                              plus_constant (XEXP (rtl, 0), offset));
16807         }
16808     }
16809   else if (TREE_CODE (decl) == VAR_DECL
16810            && rtl
16811            && MEM_P (rtl)
16812            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16813            && BYTES_BIG_ENDIAN)
16814     {
16815       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16816       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16817
16818       /* If a variable is declared "register" yet is smaller than
16819          a register, then if we store the variable to memory, it
16820          looks like we're storing a register-sized value, when in
16821          fact we are not.  We need to adjust the offset of the
16822          storage location to reflect the actual value's bytes,
16823          else gdb will not be able to display it.  */
16824       if (rsize > dsize)
16825         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16826                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16827     }
16828
16829   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16830      and will have been substituted directly into all expressions that use it.
16831      C does not have such a concept, but C++ and other languages do.  */
16832   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16833     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16834
16835   if (rtl)
16836     rtl = targetm.delegitimize_address (rtl);
16837
16838   /* If we don't look past the constant pool, we risk emitting a
16839      reference to a constant pool entry that isn't referenced from
16840      code, and thus is not emitted.  */
16841   if (rtl)
16842     rtl = avoid_constant_pool_reference (rtl);
16843
16844   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16845      in the current CU, resolve_addr will remove the expression referencing
16846      it.  */
16847   if (rtl == NULL_RTX
16848       && TREE_CODE (decl) == VAR_DECL
16849       && !DECL_EXTERNAL (decl)
16850       && TREE_STATIC (decl)
16851       && DECL_NAME (decl)
16852       && !DECL_HARD_REGISTER (decl)
16853       && DECL_MODE (decl) != VOIDmode)
16854     {
16855       rtl = make_decl_rtl_for_debug (decl);
16856       if (!MEM_P (rtl)
16857           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16858           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16859         rtl = NULL_RTX;
16860     }
16861
16862   return rtl;
16863 }
16864
16865 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16866    returned.  If so, the decl for the COMMON block is returned, and the
16867    value is the offset into the common block for the symbol.  */
16868
16869 static tree
16870 fortran_common (tree decl, HOST_WIDE_INT *value)
16871 {
16872   tree val_expr, cvar;
16873   enum machine_mode mode;
16874   HOST_WIDE_INT bitsize, bitpos;
16875   tree offset;
16876   int volatilep = 0, unsignedp = 0;
16877
16878   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16879      it does not have a value (the offset into the common area), or if it
16880      is thread local (as opposed to global) then it isn't common, and shouldn't
16881      be handled as such.  */
16882   if (TREE_CODE (decl) != VAR_DECL
16883       || !TREE_STATIC (decl)
16884       || !DECL_HAS_VALUE_EXPR_P (decl)
16885       || !is_fortran ())
16886     return NULL_TREE;
16887
16888   val_expr = DECL_VALUE_EXPR (decl);
16889   if (TREE_CODE (val_expr) != COMPONENT_REF)
16890     return NULL_TREE;
16891
16892   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16893                               &mode, &unsignedp, &volatilep, true);
16894
16895   if (cvar == NULL_TREE
16896       || TREE_CODE (cvar) != VAR_DECL
16897       || DECL_ARTIFICIAL (cvar)
16898       || !TREE_PUBLIC (cvar))
16899     return NULL_TREE;
16900
16901   *value = 0;
16902   if (offset != NULL)
16903     {
16904       if (!host_integerp (offset, 0))
16905         return NULL_TREE;
16906       *value = tree_low_cst (offset, 0);
16907     }
16908   if (bitpos != 0)
16909     *value += bitpos / BITS_PER_UNIT;
16910
16911   return cvar;
16912 }
16913
16914 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16915    data attribute for a variable or a parameter.  We generate the
16916    DW_AT_const_value attribute only in those cases where the given variable
16917    or parameter does not have a true "location" either in memory or in a
16918    register.  This can happen (for example) when a constant is passed as an
16919    actual argument in a call to an inline function.  (It's possible that
16920    these things can crop up in other ways also.)  Note that one type of
16921    constant value which can be passed into an inlined function is a constant
16922    pointer.  This can happen for example if an actual argument in an inlined
16923    function call evaluates to a compile-time constant address.  */
16924
16925 static bool
16926 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16927                                        enum dwarf_attribute attr)
16928 {
16929   rtx rtl;
16930   dw_loc_list_ref list;
16931   var_loc_list *loc_list;
16932
16933   if (TREE_CODE (decl) == ERROR_MARK)
16934     return false;
16935
16936   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16937               || TREE_CODE (decl) == RESULT_DECL);
16938
16939   /* Try to get some constant RTL for this decl, and use that as the value of
16940      the location.  */
16941
16942   rtl = rtl_for_decl_location (decl);
16943   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16944       && add_const_value_attribute (die, rtl))
16945     return true;
16946
16947   /* See if we have single element location list that is equivalent to
16948      a constant value.  That way we are better to use add_const_value_attribute
16949      rather than expanding constant value equivalent.  */
16950   loc_list = lookup_decl_loc (decl);
16951   if (loc_list
16952       && loc_list->first
16953       && loc_list->first->next == NULL
16954       && NOTE_P (loc_list->first->loc)
16955       && NOTE_VAR_LOCATION (loc_list->first->loc)
16956       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16957     {
16958       struct var_loc_node *node;
16959
16960       node = loc_list->first;
16961       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16962       if (GET_CODE (rtl) == EXPR_LIST)
16963         rtl = XEXP (rtl, 0);
16964       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16965           && add_const_value_attribute (die, rtl))
16966          return true;
16967     }
16968   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16969   if (list)
16970     {
16971       add_AT_location_description (die, attr, list);
16972       return true;
16973     }
16974   /* None of that worked, so it must not really have a location;
16975      try adding a constant value attribute from the DECL_INITIAL.  */
16976   return tree_add_const_value_attribute_for_decl (die, decl);
16977 }
16978
16979 /* Add VARIABLE and DIE into deferred locations list.  */
16980
16981 static void
16982 defer_location (tree variable, dw_die_ref die)
16983 {
16984   deferred_locations entry;
16985   entry.variable = variable;
16986   entry.die = die;
16987   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16988 }
16989
16990 /* Helper function for tree_add_const_value_attribute.  Natively encode
16991    initializer INIT into an array.  Return true if successful.  */
16992
16993 static bool
16994 native_encode_initializer (tree init, unsigned char *array, int size)
16995 {
16996   tree type;
16997
16998   if (init == NULL_TREE)
16999     return false;
17000
17001   STRIP_NOPS (init);
17002   switch (TREE_CODE (init))
17003     {
17004     case STRING_CST:
17005       type = TREE_TYPE (init);
17006       if (TREE_CODE (type) == ARRAY_TYPE)
17007         {
17008           tree enttype = TREE_TYPE (type);
17009           enum machine_mode mode = TYPE_MODE (enttype);
17010
17011           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17012             return false;
17013           if (int_size_in_bytes (type) != size)
17014             return false;
17015           if (size > TREE_STRING_LENGTH (init))
17016             {
17017               memcpy (array, TREE_STRING_POINTER (init),
17018                       TREE_STRING_LENGTH (init));
17019               memset (array + TREE_STRING_LENGTH (init),
17020                       '\0', size - TREE_STRING_LENGTH (init));
17021             }
17022           else
17023             memcpy (array, TREE_STRING_POINTER (init), size);
17024           return true;
17025         }
17026       return false;
17027     case CONSTRUCTOR:
17028       type = TREE_TYPE (init);
17029       if (int_size_in_bytes (type) != size)
17030         return false;
17031       if (TREE_CODE (type) == ARRAY_TYPE)
17032         {
17033           HOST_WIDE_INT min_index;
17034           unsigned HOST_WIDE_INT cnt;
17035           int curpos = 0, fieldsize;
17036           constructor_elt *ce;
17037
17038           if (TYPE_DOMAIN (type) == NULL_TREE
17039               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17040             return false;
17041
17042           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17043           if (fieldsize <= 0)
17044             return false;
17045
17046           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17047           memset (array, '\0', size);
17048           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17049             {
17050               tree val = ce->value;
17051               tree index = ce->index;
17052               int pos = curpos;
17053               if (index && TREE_CODE (index) == RANGE_EXPR)
17054                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17055                       * fieldsize;
17056               else if (index)
17057                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17058
17059               if (val)
17060                 {
17061                   STRIP_NOPS (val);
17062                   if (!native_encode_initializer (val, array + pos, fieldsize))
17063                     return false;
17064                 }
17065               curpos = pos + fieldsize;
17066               if (index && TREE_CODE (index) == RANGE_EXPR)
17067                 {
17068                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17069                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17070                   while (count > 0)
17071                     {
17072                       if (val)
17073                         memcpy (array + curpos, array + pos, fieldsize);
17074                       curpos += fieldsize;
17075                     }
17076                 }
17077               gcc_assert (curpos <= size);
17078             }
17079           return true;
17080         }
17081       else if (TREE_CODE (type) == RECORD_TYPE
17082                || TREE_CODE (type) == UNION_TYPE)
17083         {
17084           tree field = NULL_TREE;
17085           unsigned HOST_WIDE_INT cnt;
17086           constructor_elt *ce;
17087
17088           if (int_size_in_bytes (type) != size)
17089             return false;
17090
17091           if (TREE_CODE (type) == RECORD_TYPE)
17092             field = TYPE_FIELDS (type);
17093
17094           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17095             {
17096               tree val = ce->value;
17097               int pos, fieldsize;
17098
17099               if (ce->index != 0)
17100                 field = ce->index;
17101
17102               if (val)
17103                 STRIP_NOPS (val);
17104
17105               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17106                 return false;
17107
17108               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17109                   && TYPE_DOMAIN (TREE_TYPE (field))
17110                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17111                 return false;
17112               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17113                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17114                 return false;
17115               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17116               pos = int_byte_position (field);
17117               gcc_assert (pos + fieldsize <= size);
17118               if (val
17119                   && !native_encode_initializer (val, array + pos, fieldsize))
17120                 return false;
17121             }
17122           return true;
17123         }
17124       return false;
17125     case VIEW_CONVERT_EXPR:
17126     case NON_LVALUE_EXPR:
17127       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17128     default:
17129       return native_encode_expr (init, array, size) == size;
17130     }
17131 }
17132
17133 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17134    attribute is the const value T.  */
17135
17136 static bool
17137 tree_add_const_value_attribute (dw_die_ref die, tree t)
17138 {
17139   tree init;
17140   tree type = TREE_TYPE (t);
17141   rtx rtl;
17142
17143   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17144     return false;
17145
17146   init = t;
17147   gcc_assert (!DECL_P (init));
17148
17149   rtl = rtl_for_decl_init (init, type);
17150   if (rtl)
17151     return add_const_value_attribute (die, rtl);
17152   /* If the host and target are sane, try harder.  */
17153   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17154            && initializer_constant_valid_p (init, type))
17155     {
17156       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17157       if (size > 0 && (int) size == size)
17158         {
17159           unsigned char *array = (unsigned char *)
17160             ggc_alloc_cleared_atomic (size);
17161
17162           if (native_encode_initializer (init, array, size))
17163             {
17164               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17165               return true;
17166             }
17167         }
17168     }
17169   return false;
17170 }
17171
17172 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17173    attribute is the const value of T, where T is an integral constant
17174    variable with static storage duration
17175    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17176
17177 static bool
17178 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17179 {
17180
17181   if (!decl
17182       || (TREE_CODE (decl) != VAR_DECL
17183           && TREE_CODE (decl) != CONST_DECL))
17184     return false;
17185
17186     if (TREE_READONLY (decl)
17187         && ! TREE_THIS_VOLATILE (decl)
17188         && DECL_INITIAL (decl))
17189       /* OK */;
17190     else
17191       return false;
17192
17193   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17194   if (get_AT (var_die, DW_AT_const_value))
17195     return false;
17196
17197   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17198 }
17199
17200 /* Convert the CFI instructions for the current function into a
17201    location list.  This is used for DW_AT_frame_base when we targeting
17202    a dwarf2 consumer that does not support the dwarf3
17203    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17204    expressions.  */
17205
17206 static dw_loc_list_ref
17207 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17208 {
17209   dw_fde_ref fde;
17210   dw_loc_list_ref list, *list_tail;
17211   dw_cfi_ref cfi;
17212   dw_cfa_location last_cfa, next_cfa;
17213   const char *start_label, *last_label, *section;
17214   dw_cfa_location remember;
17215
17216   fde = current_fde ();
17217   gcc_assert (fde != NULL);
17218
17219   section = secname_for_decl (current_function_decl);
17220   list_tail = &list;
17221   list = NULL;
17222
17223   memset (&next_cfa, 0, sizeof (next_cfa));
17224   next_cfa.reg = INVALID_REGNUM;
17225   remember = next_cfa;
17226
17227   start_label = fde->dw_fde_begin;
17228
17229   /* ??? Bald assumption that the CIE opcode list does not contain
17230      advance opcodes.  */
17231   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17232     lookup_cfa_1 (cfi, &next_cfa, &remember);
17233
17234   last_cfa = next_cfa;
17235   last_label = start_label;
17236
17237   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17238     switch (cfi->dw_cfi_opc)
17239       {
17240       case DW_CFA_set_loc:
17241       case DW_CFA_advance_loc1:
17242       case DW_CFA_advance_loc2:
17243       case DW_CFA_advance_loc4:
17244         if (!cfa_equal_p (&last_cfa, &next_cfa))
17245           {
17246             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17247                                        start_label, last_label, section);
17248
17249             list_tail = &(*list_tail)->dw_loc_next;
17250             last_cfa = next_cfa;
17251             start_label = last_label;
17252           }
17253         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17254         break;
17255
17256       case DW_CFA_advance_loc:
17257         /* The encoding is complex enough that we should never emit this.  */
17258         gcc_unreachable ();
17259
17260       default:
17261         lookup_cfa_1 (cfi, &next_cfa, &remember);
17262         break;
17263       }
17264
17265   if (!cfa_equal_p (&last_cfa, &next_cfa))
17266     {
17267       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17268                                  start_label, last_label, section);
17269       list_tail = &(*list_tail)->dw_loc_next;
17270       start_label = last_label;
17271     }
17272
17273   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17274                              start_label, fde->dw_fde_end, section);
17275
17276   if (list && list->dw_loc_next)
17277     gen_llsym (list);
17278
17279   return list;
17280 }
17281
17282 /* Compute a displacement from the "steady-state frame pointer" to the
17283    frame base (often the same as the CFA), and store it in
17284    frame_pointer_fb_offset.  OFFSET is added to the displacement
17285    before the latter is negated.  */
17286
17287 static void
17288 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17289 {
17290   rtx reg, elim;
17291
17292 #ifdef FRAME_POINTER_CFA_OFFSET
17293   reg = frame_pointer_rtx;
17294   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17295 #else
17296   reg = arg_pointer_rtx;
17297   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17298 #endif
17299
17300   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17301   if (GET_CODE (elim) == PLUS)
17302     {
17303       offset += INTVAL (XEXP (elim, 1));
17304       elim = XEXP (elim, 0);
17305     }
17306
17307   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17308                && (elim == hard_frame_pointer_rtx
17309                    || elim == stack_pointer_rtx))
17310               || elim == (frame_pointer_needed
17311                           ? hard_frame_pointer_rtx
17312                           : stack_pointer_rtx));
17313
17314   frame_pointer_fb_offset = -offset;
17315 }
17316
17317 /* Generate a DW_AT_name attribute given some string value to be included as
17318    the value of the attribute.  */
17319
17320 static void
17321 add_name_attribute (dw_die_ref die, const char *name_string)
17322 {
17323   if (name_string != NULL && *name_string != 0)
17324     {
17325       if (demangle_name_func)
17326         name_string = (*demangle_name_func) (name_string);
17327
17328       add_AT_string (die, DW_AT_name, name_string);
17329     }
17330 }
17331
17332 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17333
17334 static void
17335 add_comp_dir_attribute (dw_die_ref die)
17336 {
17337   const char *wd = get_src_pwd ();
17338   char *wd1;
17339
17340   if (wd == NULL)
17341     return;
17342
17343   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17344     {
17345       int wdlen;
17346
17347       wdlen = strlen (wd);
17348       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17349       strcpy (wd1, wd);
17350       wd1 [wdlen] = DIR_SEPARATOR;
17351       wd1 [wdlen + 1] = 0;
17352       wd = wd1;
17353     }
17354
17355     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17356 }
17357
17358 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17359    default.  */
17360
17361 static int
17362 lower_bound_default (void)
17363 {
17364   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17365     {
17366     case DW_LANG_C:
17367     case DW_LANG_C89:
17368     case DW_LANG_C99:
17369     case DW_LANG_C_plus_plus:
17370     case DW_LANG_ObjC:
17371     case DW_LANG_ObjC_plus_plus:
17372     case DW_LANG_Java:
17373       return 0;
17374     case DW_LANG_Fortran77:
17375     case DW_LANG_Fortran90:
17376     case DW_LANG_Fortran95:
17377       return 1;
17378     case DW_LANG_UPC:
17379     case DW_LANG_D:
17380     case DW_LANG_Python:
17381       return dwarf_version >= 4 ? 0 : -1;
17382     case DW_LANG_Ada95:
17383     case DW_LANG_Ada83:
17384     case DW_LANG_Cobol74:
17385     case DW_LANG_Cobol85:
17386     case DW_LANG_Pascal83:
17387     case DW_LANG_Modula2:
17388     case DW_LANG_PLI:
17389       return dwarf_version >= 4 ? 1 : -1;
17390     default:
17391       return -1;
17392     }
17393 }
17394
17395 /* Given a tree node describing an array bound (either lower or upper) output
17396    a representation for that bound.  */
17397
17398 static void
17399 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17400 {
17401   switch (TREE_CODE (bound))
17402     {
17403     case ERROR_MARK:
17404       return;
17405
17406     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17407     case INTEGER_CST:
17408       {
17409         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17410         int dflt;
17411
17412         /* Use the default if possible.  */
17413         if (bound_attr == DW_AT_lower_bound
17414             && host_integerp (bound, 0)
17415             && (dflt = lower_bound_default ()) != -1
17416             && tree_low_cst (bound, 0) == dflt)
17417           ;
17418
17419         /* Otherwise represent the bound as an unsigned value with the
17420            precision of its type.  The precision and signedness of the
17421            type will be necessary to re-interpret it unambiguously.  */
17422         else if (prec < HOST_BITS_PER_WIDE_INT)
17423           {
17424             unsigned HOST_WIDE_INT mask
17425               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17426             add_AT_unsigned (subrange_die, bound_attr,
17427                              TREE_INT_CST_LOW (bound) & mask);
17428           }
17429         else if (prec == HOST_BITS_PER_WIDE_INT
17430                  || TREE_INT_CST_HIGH (bound) == 0)
17431           add_AT_unsigned (subrange_die, bound_attr,
17432                            TREE_INT_CST_LOW (bound));
17433         else
17434           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17435                          TREE_INT_CST_LOW (bound));
17436       }
17437       break;
17438
17439     CASE_CONVERT:
17440     case VIEW_CONVERT_EXPR:
17441       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17442       break;
17443
17444     case SAVE_EXPR:
17445       break;
17446
17447     case VAR_DECL:
17448     case PARM_DECL:
17449     case RESULT_DECL:
17450       {
17451         dw_die_ref decl_die = lookup_decl_die (bound);
17452
17453         /* ??? Can this happen, or should the variable have been bound
17454            first?  Probably it can, since I imagine that we try to create
17455            the types of parameters in the order in which they exist in
17456            the list, and won't have created a forward reference to a
17457            later parameter.  */
17458         if (decl_die != NULL)
17459           {
17460             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17461             break;
17462           }
17463       }
17464       /* FALLTHRU */
17465
17466     default:
17467       {
17468         /* Otherwise try to create a stack operation procedure to
17469            evaluate the value of the array bound.  */
17470
17471         dw_die_ref ctx, decl_die;
17472         dw_loc_list_ref list;
17473
17474         list = loc_list_from_tree (bound, 2);
17475         if (list == NULL || single_element_loc_list_p (list))
17476           {
17477             /* If DW_AT_*bound is not a reference nor constant, it is
17478                a DWARF expression rather than location description.
17479                For that loc_list_from_tree (bound, 0) is needed.
17480                If that fails to give a single element list,
17481                fall back to outputting this as a reference anyway.  */
17482             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17483             if (list2 && single_element_loc_list_p (list2))
17484               {
17485                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17486                 break;
17487               }
17488           }
17489         if (list == NULL)
17490           break;
17491
17492         if (current_function_decl == 0)
17493           ctx = comp_unit_die ();
17494         else
17495           ctx = lookup_decl_die (current_function_decl);
17496
17497         decl_die = new_die (DW_TAG_variable, ctx, bound);
17498         add_AT_flag (decl_die, DW_AT_artificial, 1);
17499         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17500         add_AT_location_description (decl_die, DW_AT_location, list);
17501         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17502         break;
17503       }
17504     }
17505 }
17506
17507 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17508    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17509    Note that the block of subscript information for an array type also
17510    includes information about the element type of the given array type.  */
17511
17512 static void
17513 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17514 {
17515   unsigned dimension_number;
17516   tree lower, upper;
17517   dw_die_ref subrange_die;
17518
17519   for (dimension_number = 0;
17520        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17521        type = TREE_TYPE (type), dimension_number++)
17522     {
17523       tree domain = TYPE_DOMAIN (type);
17524
17525       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17526         break;
17527
17528       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17529          and (in GNU C only) variable bounds.  Handle all three forms
17530          here.  */
17531       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17532       if (domain)
17533         {
17534           /* We have an array type with specified bounds.  */
17535           lower = TYPE_MIN_VALUE (domain);
17536           upper = TYPE_MAX_VALUE (domain);
17537
17538           /* Define the index type.  */
17539           if (TREE_TYPE (domain))
17540             {
17541               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17542                  TREE_TYPE field.  We can't emit debug info for this
17543                  because it is an unnamed integral type.  */
17544               if (TREE_CODE (domain) == INTEGER_TYPE
17545                   && TYPE_NAME (domain) == NULL_TREE
17546                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17547                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17548                 ;
17549               else
17550                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17551                                     type_die);
17552             }
17553
17554           /* ??? If upper is NULL, the array has unspecified length,
17555              but it does have a lower bound.  This happens with Fortran
17556                dimension arr(N:*)
17557              Since the debugger is definitely going to need to know N
17558              to produce useful results, go ahead and output the lower
17559              bound solo, and hope the debugger can cope.  */
17560
17561           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17562           if (upper)
17563             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17564         }
17565
17566       /* Otherwise we have an array type with an unspecified length.  The
17567          DWARF-2 spec does not say how to handle this; let's just leave out the
17568          bounds.  */
17569     }
17570 }
17571
17572 static void
17573 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17574 {
17575   unsigned size;
17576
17577   switch (TREE_CODE (tree_node))
17578     {
17579     case ERROR_MARK:
17580       size = 0;
17581       break;
17582     case ENUMERAL_TYPE:
17583     case RECORD_TYPE:
17584     case UNION_TYPE:
17585     case QUAL_UNION_TYPE:
17586       size = int_size_in_bytes (tree_node);
17587       break;
17588     case FIELD_DECL:
17589       /* For a data member of a struct or union, the DW_AT_byte_size is
17590          generally given as the number of bytes normally allocated for an
17591          object of the *declared* type of the member itself.  This is true
17592          even for bit-fields.  */
17593       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17594       break;
17595     default:
17596       gcc_unreachable ();
17597     }
17598
17599   /* Note that `size' might be -1 when we get to this point.  If it is, that
17600      indicates that the byte size of the entity in question is variable.  We
17601      have no good way of expressing this fact in Dwarf at the present time,
17602      so just let the -1 pass on through.  */
17603   add_AT_unsigned (die, DW_AT_byte_size, size);
17604 }
17605
17606 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17607    which specifies the distance in bits from the highest order bit of the
17608    "containing object" for the bit-field to the highest order bit of the
17609    bit-field itself.
17610
17611    For any given bit-field, the "containing object" is a hypothetical object
17612    (of some integral or enum type) within which the given bit-field lives.  The
17613    type of this hypothetical "containing object" is always the same as the
17614    declared type of the individual bit-field itself.  The determination of the
17615    exact location of the "containing object" for a bit-field is rather
17616    complicated.  It's handled by the `field_byte_offset' function (above).
17617
17618    Note that it is the size (in bytes) of the hypothetical "containing object"
17619    which will be given in the DW_AT_byte_size attribute for this bit-field.
17620    (See `byte_size_attribute' above).  */
17621
17622 static inline void
17623 add_bit_offset_attribute (dw_die_ref die, tree decl)
17624 {
17625   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17626   tree type = DECL_BIT_FIELD_TYPE (decl);
17627   HOST_WIDE_INT bitpos_int;
17628   HOST_WIDE_INT highest_order_object_bit_offset;
17629   HOST_WIDE_INT highest_order_field_bit_offset;
17630   HOST_WIDE_INT bit_offset;
17631
17632   /* Must be a field and a bit field.  */
17633   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17634
17635   /* We can't yet handle bit-fields whose offsets are variable, so if we
17636      encounter such things, just return without generating any attribute
17637      whatsoever.  Likewise for variable or too large size.  */
17638   if (! host_integerp (bit_position (decl), 0)
17639       || ! host_integerp (DECL_SIZE (decl), 1))
17640     return;
17641
17642   bitpos_int = int_bit_position (decl);
17643
17644   /* Note that the bit offset is always the distance (in bits) from the
17645      highest-order bit of the "containing object" to the highest-order bit of
17646      the bit-field itself.  Since the "high-order end" of any object or field
17647      is different on big-endian and little-endian machines, the computation
17648      below must take account of these differences.  */
17649   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17650   highest_order_field_bit_offset = bitpos_int;
17651
17652   if (! BYTES_BIG_ENDIAN)
17653     {
17654       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17655       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17656     }
17657
17658   bit_offset
17659     = (! BYTES_BIG_ENDIAN
17660        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17661        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17662
17663   if (bit_offset < 0)
17664     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17665   else
17666     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17667 }
17668
17669 /* For a FIELD_DECL node which represents a bit field, output an attribute
17670    which specifies the length in bits of the given field.  */
17671
17672 static inline void
17673 add_bit_size_attribute (dw_die_ref die, tree decl)
17674 {
17675   /* Must be a field and a bit field.  */
17676   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17677               && DECL_BIT_FIELD_TYPE (decl));
17678
17679   if (host_integerp (DECL_SIZE (decl), 1))
17680     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17681 }
17682
17683 /* If the compiled language is ANSI C, then add a 'prototyped'
17684    attribute, if arg types are given for the parameters of a function.  */
17685
17686 static inline void
17687 add_prototyped_attribute (dw_die_ref die, tree func_type)
17688 {
17689   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17690       && prototype_p (func_type))
17691     add_AT_flag (die, DW_AT_prototyped, 1);
17692 }
17693
17694 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17695    by looking in either the type declaration or object declaration
17696    equate table.  */
17697
17698 static inline dw_die_ref
17699 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17700 {
17701   dw_die_ref origin_die = NULL;
17702
17703   if (TREE_CODE (origin) != FUNCTION_DECL)
17704     {
17705       /* We may have gotten separated from the block for the inlined
17706          function, if we're in an exception handler or some such; make
17707          sure that the abstract function has been written out.
17708
17709          Doing this for nested functions is wrong, however; functions are
17710          distinct units, and our context might not even be inline.  */
17711       tree fn = origin;
17712
17713       if (TYPE_P (fn))
17714         fn = TYPE_STUB_DECL (fn);
17715
17716       fn = decl_function_context (fn);
17717       if (fn)
17718         dwarf2out_abstract_function (fn);
17719     }
17720
17721   if (DECL_P (origin))
17722     origin_die = lookup_decl_die (origin);
17723   else if (TYPE_P (origin))
17724     origin_die = lookup_type_die (origin);
17725
17726   /* XXX: Functions that are never lowered don't always have correct block
17727      trees (in the case of java, they simply have no block tree, in some other
17728      languages).  For these functions, there is nothing we can really do to
17729      output correct debug info for inlined functions in all cases.  Rather
17730      than die, we'll just produce deficient debug info now, in that we will
17731      have variables without a proper abstract origin.  In the future, when all
17732      functions are lowered, we should re-add a gcc_assert (origin_die)
17733      here.  */
17734
17735   if (origin_die)
17736     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17737   return origin_die;
17738 }
17739
17740 /* We do not currently support the pure_virtual attribute.  */
17741
17742 static inline void
17743 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17744 {
17745   if (DECL_VINDEX (func_decl))
17746     {
17747       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17748
17749       if (host_integerp (DECL_VINDEX (func_decl), 0))
17750         add_AT_loc (die, DW_AT_vtable_elem_location,
17751                     new_loc_descr (DW_OP_constu,
17752                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17753                                    0));
17754
17755       /* GNU extension: Record what type this method came from originally.  */
17756       if (debug_info_level > DINFO_LEVEL_TERSE
17757           && DECL_CONTEXT (func_decl))
17758         add_AT_die_ref (die, DW_AT_containing_type,
17759                         lookup_type_die (DECL_CONTEXT (func_decl)));
17760     }
17761 }
17762 \f
17763 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17764    given decl.  This used to be a vendor extension until after DWARF 4
17765    standardized it.  */
17766
17767 static void
17768 add_linkage_attr (dw_die_ref die, tree decl)
17769 {
17770   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17771
17772   /* Mimic what assemble_name_raw does with a leading '*'.  */
17773   if (name[0] == '*')
17774     name = &name[1];
17775
17776   if (dwarf_version >= 4)
17777     add_AT_string (die, DW_AT_linkage_name, name);
17778   else
17779     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17780 }
17781
17782 /* Add source coordinate attributes for the given decl.  */
17783
17784 static void
17785 add_src_coords_attributes (dw_die_ref die, tree decl)
17786 {
17787   expanded_location s;
17788
17789   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17790     return;
17791   s = expand_location (DECL_SOURCE_LOCATION (decl));
17792   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17793   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17794 }
17795
17796 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17797
17798 static void
17799 add_linkage_name (dw_die_ref die, tree decl)
17800 {
17801   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17802        && TREE_PUBLIC (decl)
17803        && !DECL_ABSTRACT (decl)
17804        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17805        && die->die_tag != DW_TAG_member)
17806     {
17807       /* Defer until we have an assembler name set.  */
17808       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17809         {
17810           limbo_die_node *asm_name;
17811
17812           asm_name = ggc_alloc_cleared_limbo_die_node ();
17813           asm_name->die = die;
17814           asm_name->created_for = decl;
17815           asm_name->next = deferred_asm_name;
17816           deferred_asm_name = asm_name;
17817         }
17818       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17819         add_linkage_attr (die, decl);
17820     }
17821 }
17822
17823 /* Add a DW_AT_name attribute and source coordinate attribute for the
17824    given decl, but only if it actually has a name.  */
17825
17826 static void
17827 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17828 {
17829   tree decl_name;
17830
17831   decl_name = DECL_NAME (decl);
17832   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17833     {
17834       const char *name = dwarf2_name (decl, 0);
17835       if (name)
17836         add_name_attribute (die, name);
17837       if (! DECL_ARTIFICIAL (decl))
17838         add_src_coords_attributes (die, decl);
17839
17840       add_linkage_name (die, decl);
17841     }
17842
17843 #ifdef VMS_DEBUGGING_INFO
17844   /* Get the function's name, as described by its RTL.  This may be different
17845      from the DECL_NAME name used in the source file.  */
17846   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17847     {
17848       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17849                    XEXP (DECL_RTL (decl), 0));
17850       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17851     }
17852 #endif /* VMS_DEBUGGING_INFO */
17853 }
17854
17855 #ifdef VMS_DEBUGGING_INFO
17856 /* Output the debug main pointer die for VMS */
17857
17858 void
17859 dwarf2out_vms_debug_main_pointer (void)
17860 {
17861   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17862   dw_die_ref die;
17863
17864   /* Allocate the VMS debug main subprogram die.  */
17865   die = ggc_alloc_cleared_die_node ();
17866   die->die_tag = DW_TAG_subprogram;
17867   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17868   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17869                                current_function_funcdef_no);
17870   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17871
17872   /* Make it the first child of comp_unit_die ().  */
17873   die->die_parent = comp_unit_die ();
17874   if (comp_unit_die ()->die_child)
17875     {
17876       die->die_sib = comp_unit_die ()->die_child->die_sib;
17877       comp_unit_die ()->die_child->die_sib = die;
17878     }
17879   else
17880     {
17881       die->die_sib = die;
17882       comp_unit_die ()->die_child = die;
17883     }
17884 }
17885 #endif /* VMS_DEBUGGING_INFO */
17886
17887 /* Push a new declaration scope.  */
17888
17889 static void
17890 push_decl_scope (tree scope)
17891 {
17892   VEC_safe_push (tree, gc, decl_scope_table, scope);
17893 }
17894
17895 /* Pop a declaration scope.  */
17896
17897 static inline void
17898 pop_decl_scope (void)
17899 {
17900   VEC_pop (tree, decl_scope_table);
17901 }
17902
17903 /* Return the DIE for the scope that immediately contains this type.
17904    Non-named types get global scope.  Named types nested in other
17905    types get their containing scope if it's open, or global scope
17906    otherwise.  All other types (i.e. function-local named types) get
17907    the current active scope.  */
17908
17909 static dw_die_ref
17910 scope_die_for (tree t, dw_die_ref context_die)
17911 {
17912   dw_die_ref scope_die = NULL;
17913   tree containing_scope;
17914   int i;
17915
17916   /* Non-types always go in the current scope.  */
17917   gcc_assert (TYPE_P (t));
17918
17919   containing_scope = TYPE_CONTEXT (t);
17920
17921   /* Use the containing namespace if it was passed in (for a declaration).  */
17922   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17923     {
17924       if (context_die == lookup_decl_die (containing_scope))
17925         /* OK */;
17926       else
17927         containing_scope = NULL_TREE;
17928     }
17929
17930   /* Ignore function type "scopes" from the C frontend.  They mean that
17931      a tagged type is local to a parmlist of a function declarator, but
17932      that isn't useful to DWARF.  */
17933   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17934     containing_scope = NULL_TREE;
17935
17936   if (SCOPE_FILE_SCOPE_P (containing_scope))
17937     scope_die = comp_unit_die ();
17938   else if (TYPE_P (containing_scope))
17939     {
17940       /* For types, we can just look up the appropriate DIE.  But
17941          first we check to see if we're in the middle of emitting it
17942          so we know where the new DIE should go.  */
17943       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17944         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17945           break;
17946
17947       if (i < 0)
17948         {
17949           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17950                       || TREE_ASM_WRITTEN (containing_scope));
17951           /*We are not in the middle of emitting the type
17952             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17953           scope_die = lookup_type_die (containing_scope);
17954
17955           /* If none of the current dies are suitable, we get file scope.  */
17956           if (scope_die == NULL)
17957             scope_die = comp_unit_die ();
17958         }
17959       else
17960         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17961     }
17962   else
17963     scope_die = context_die;
17964
17965   return scope_die;
17966 }
17967
17968 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17969
17970 static inline int
17971 local_scope_p (dw_die_ref context_die)
17972 {
17973   for (; context_die; context_die = context_die->die_parent)
17974     if (context_die->die_tag == DW_TAG_inlined_subroutine
17975         || context_die->die_tag == DW_TAG_subprogram)
17976       return 1;
17977
17978   return 0;
17979 }
17980
17981 /* Returns nonzero if CONTEXT_DIE is a class.  */
17982
17983 static inline int
17984 class_scope_p (dw_die_ref context_die)
17985 {
17986   return (context_die
17987           && (context_die->die_tag == DW_TAG_structure_type
17988               || context_die->die_tag == DW_TAG_class_type
17989               || context_die->die_tag == DW_TAG_interface_type
17990               || context_die->die_tag == DW_TAG_union_type));
17991 }
17992
17993 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17994    whether or not to treat a DIE in this context as a declaration.  */
17995
17996 static inline int
17997 class_or_namespace_scope_p (dw_die_ref context_die)
17998 {
17999   return (class_scope_p (context_die)
18000           || (context_die && context_die->die_tag == DW_TAG_namespace));
18001 }
18002
18003 /* Many forms of DIEs require a "type description" attribute.  This
18004    routine locates the proper "type descriptor" die for the type given
18005    by 'type', and adds a DW_AT_type attribute below the given die.  */
18006
18007 static void
18008 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18009                     int decl_volatile, dw_die_ref context_die)
18010 {
18011   enum tree_code code  = TREE_CODE (type);
18012   dw_die_ref type_die  = NULL;
18013
18014   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18015      or fixed-point type, use the inner type.  This is because we have no
18016      support for unnamed types in base_type_die.  This can happen if this is
18017      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18018   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18019       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18020     type = TREE_TYPE (type), code = TREE_CODE (type);
18021
18022   if (code == ERROR_MARK
18023       /* Handle a special case.  For functions whose return type is void, we
18024          generate *no* type attribute.  (Note that no object may have type
18025          `void', so this only applies to function return types).  */
18026       || code == VOID_TYPE)
18027     return;
18028
18029   type_die = modified_type_die (type,
18030                                 decl_const || TYPE_READONLY (type),
18031                                 decl_volatile || TYPE_VOLATILE (type),
18032                                 context_die);
18033
18034   if (type_die != NULL)
18035     add_AT_die_ref (object_die, DW_AT_type, type_die);
18036 }
18037
18038 /* Given an object die, add the calling convention attribute for the
18039    function call type.  */
18040 static void
18041 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18042 {
18043   enum dwarf_calling_convention value = DW_CC_normal;
18044
18045   value = ((enum dwarf_calling_convention)
18046            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18047
18048   if (is_fortran ()
18049       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18050     {
18051       /* DWARF 2 doesn't provide a way to identify a program's source-level
18052         entry point.  DW_AT_calling_convention attributes are only meant
18053         to describe functions' calling conventions.  However, lacking a
18054         better way to signal the Fortran main program, we used this for 
18055         a long time, following existing custom.  Now, DWARF 4 has 
18056         DW_AT_main_subprogram, which we add below, but some tools still
18057         rely on the old way, which we thus keep.  */
18058       value = DW_CC_program;
18059
18060       if (dwarf_version >= 4 || !dwarf_strict)
18061         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18062     }
18063
18064   /* Only add the attribute if the backend requests it, and
18065      is not DW_CC_normal.  */
18066   if (value && (value != DW_CC_normal))
18067     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18068 }
18069
18070 /* Given a tree pointer to a struct, class, union, or enum type node, return
18071    a pointer to the (string) tag name for the given type, or zero if the type
18072    was declared without a tag.  */
18073
18074 static const char *
18075 type_tag (const_tree type)
18076 {
18077   const char *name = 0;
18078
18079   if (TYPE_NAME (type) != 0)
18080     {
18081       tree t = 0;
18082
18083       /* Find the IDENTIFIER_NODE for the type name.  */
18084       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18085           && !TYPE_NAMELESS (type))
18086         t = TYPE_NAME (type);
18087
18088       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18089          a TYPE_DECL node, regardless of whether or not a `typedef' was
18090          involved.  */
18091       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18092                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18093         {
18094           /* We want to be extra verbose.  Don't call dwarf_name if
18095              DECL_NAME isn't set.  The default hook for decl_printable_name
18096              doesn't like that, and in this context it's correct to return
18097              0, instead of "<anonymous>" or the like.  */
18098           if (DECL_NAME (TYPE_NAME (type))
18099               && !DECL_NAMELESS (TYPE_NAME (type)))
18100             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18101         }
18102
18103       /* Now get the name as a string, or invent one.  */
18104       if (!name && t != 0)
18105         name = IDENTIFIER_POINTER (t);
18106     }
18107
18108   return (name == 0 || *name == '\0') ? 0 : name;
18109 }
18110
18111 /* Return the type associated with a data member, make a special check
18112    for bit field types.  */
18113
18114 static inline tree
18115 member_declared_type (const_tree member)
18116 {
18117   return (DECL_BIT_FIELD_TYPE (member)
18118           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18119 }
18120
18121 /* Get the decl's label, as described by its RTL. This may be different
18122    from the DECL_NAME name used in the source file.  */
18123
18124 #if 0
18125 static const char *
18126 decl_start_label (tree decl)
18127 {
18128   rtx x;
18129   const char *fnname;
18130
18131   x = DECL_RTL (decl);
18132   gcc_assert (MEM_P (x));
18133
18134   x = XEXP (x, 0);
18135   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18136
18137   fnname = XSTR (x, 0);
18138   return fnname;
18139 }
18140 #endif
18141 \f
18142 /* These routines generate the internal representation of the DIE's for
18143    the compilation unit.  Debugging information is collected by walking
18144    the declaration trees passed in from dwarf2out_decl().  */
18145
18146 static void
18147 gen_array_type_die (tree type, dw_die_ref context_die)
18148 {
18149   dw_die_ref scope_die = scope_die_for (type, context_die);
18150   dw_die_ref array_die;
18151
18152   /* GNU compilers represent multidimensional array types as sequences of one
18153      dimensional array types whose element types are themselves array types.
18154      We sometimes squish that down to a single array_type DIE with multiple
18155      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18156      say that we are allowed to do this kind of compression in C, because
18157      there is no difference between an array of arrays and a multidimensional
18158      array.  We don't do this for Ada to remain as close as possible to the
18159      actual representation, which is especially important against the language
18160      flexibilty wrt arrays of variable size.  */
18161
18162   bool collapse_nested_arrays = !is_ada ();
18163   tree element_type;
18164
18165   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18166      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18167   if (TYPE_STRING_FLAG (type)
18168       && TREE_CODE (type) == ARRAY_TYPE
18169       && is_fortran ()
18170       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18171     {
18172       HOST_WIDE_INT size;
18173
18174       array_die = new_die (DW_TAG_string_type, scope_die, type);
18175       add_name_attribute (array_die, type_tag (type));
18176       equate_type_number_to_die (type, array_die);
18177       size = int_size_in_bytes (type);
18178       if (size >= 0)
18179         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18180       else if (TYPE_DOMAIN (type) != NULL_TREE
18181                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18182                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18183         {
18184           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18185           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18186
18187           size = int_size_in_bytes (TREE_TYPE (szdecl));
18188           if (loc && size > 0)
18189             {
18190               add_AT_location_description (array_die, DW_AT_string_length, loc);
18191               if (size != DWARF2_ADDR_SIZE)
18192                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18193             }
18194         }
18195       return;
18196     }
18197
18198   /* ??? The SGI dwarf reader fails for array of array of enum types
18199      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18200      array type comes before the outer array type.  We thus call gen_type_die
18201      before we new_die and must prevent nested array types collapsing for this
18202      target.  */
18203
18204 #ifdef MIPS_DEBUGGING_INFO
18205   gen_type_die (TREE_TYPE (type), context_die);
18206   collapse_nested_arrays = false;
18207 #endif
18208
18209   array_die = new_die (DW_TAG_array_type, scope_die, type);
18210   add_name_attribute (array_die, type_tag (type));
18211   equate_type_number_to_die (type, array_die);
18212
18213   if (TREE_CODE (type) == VECTOR_TYPE)
18214     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18215
18216   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18217   if (is_fortran ()
18218       && TREE_CODE (type) == ARRAY_TYPE
18219       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18220       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18221     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18222
18223 #if 0
18224   /* We default the array ordering.  SDB will probably do
18225      the right things even if DW_AT_ordering is not present.  It's not even
18226      an issue until we start to get into multidimensional arrays anyway.  If
18227      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18228      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18229      and when we find out that we need to put these in, we will only do so
18230      for multidimensional arrays.  */
18231   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18232 #endif
18233
18234 #ifdef MIPS_DEBUGGING_INFO
18235   /* The SGI compilers handle arrays of unknown bound by setting
18236      AT_declaration and not emitting any subrange DIEs.  */
18237   if (TREE_CODE (type) == ARRAY_TYPE
18238       && ! TYPE_DOMAIN (type))
18239     add_AT_flag (array_die, DW_AT_declaration, 1);
18240   else
18241 #endif
18242   if (TREE_CODE (type) == VECTOR_TYPE)
18243     {
18244       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18245       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18246       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18247       add_bound_info (subrange_die, DW_AT_upper_bound,
18248                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18249     }
18250   else
18251     add_subscript_info (array_die, type, collapse_nested_arrays);
18252
18253   /* Add representation of the type of the elements of this array type and
18254      emit the corresponding DIE if we haven't done it already.  */
18255   element_type = TREE_TYPE (type);
18256   if (collapse_nested_arrays)
18257     while (TREE_CODE (element_type) == ARRAY_TYPE)
18258       {
18259         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18260           break;
18261         element_type = TREE_TYPE (element_type);
18262       }
18263
18264 #ifndef MIPS_DEBUGGING_INFO
18265   gen_type_die (element_type, context_die);
18266 #endif
18267
18268   add_type_attribute (array_die, element_type, 0, 0, context_die);
18269
18270   if (get_AT (array_die, DW_AT_name))
18271     add_pubtype (type, array_die);
18272 }
18273
18274 static dw_loc_descr_ref
18275 descr_info_loc (tree val, tree base_decl)
18276 {
18277   HOST_WIDE_INT size;
18278   dw_loc_descr_ref loc, loc2;
18279   enum dwarf_location_atom op;
18280
18281   if (val == base_decl)
18282     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18283
18284   switch (TREE_CODE (val))
18285     {
18286     CASE_CONVERT:
18287       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18288     case VAR_DECL:
18289       return loc_descriptor_from_tree (val, 0);
18290     case INTEGER_CST:
18291       if (host_integerp (val, 0))
18292         return int_loc_descriptor (tree_low_cst (val, 0));
18293       break;
18294     case INDIRECT_REF:
18295       size = int_size_in_bytes (TREE_TYPE (val));
18296       if (size < 0)
18297         break;
18298       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18299       if (!loc)
18300         break;
18301       if (size == DWARF2_ADDR_SIZE)
18302         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18303       else
18304         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18305       return loc;
18306     case POINTER_PLUS_EXPR:
18307     case PLUS_EXPR:
18308       if (host_integerp (TREE_OPERAND (val, 1), 1)
18309           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18310              < 16384)
18311         {
18312           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18313           if (!loc)
18314             break;
18315           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18316         }
18317       else
18318         {
18319           op = DW_OP_plus;
18320         do_binop:
18321           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18322           if (!loc)
18323             break;
18324           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18325           if (!loc2)
18326             break;
18327           add_loc_descr (&loc, loc2);
18328           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18329         }
18330       return loc;
18331     case MINUS_EXPR:
18332       op = DW_OP_minus;
18333       goto do_binop;
18334     case MULT_EXPR:
18335       op = DW_OP_mul;
18336       goto do_binop;
18337     case EQ_EXPR:
18338       op = DW_OP_eq;
18339       goto do_binop;
18340     case NE_EXPR:
18341       op = DW_OP_ne;
18342       goto do_binop;
18343     default:
18344       break;
18345     }
18346   return NULL;
18347 }
18348
18349 static void
18350 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18351                       tree val, tree base_decl)
18352 {
18353   dw_loc_descr_ref loc;
18354
18355   if (host_integerp (val, 0))
18356     {
18357       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18358       return;
18359     }
18360
18361   loc = descr_info_loc (val, base_decl);
18362   if (!loc)
18363     return;
18364
18365   add_AT_loc (die, attr, loc);
18366 }
18367
18368 /* This routine generates DIE for array with hidden descriptor, details
18369    are filled into *info by a langhook.  */
18370
18371 static void
18372 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18373                           dw_die_ref context_die)
18374 {
18375   dw_die_ref scope_die = scope_die_for (type, context_die);
18376   dw_die_ref array_die;
18377   int dim;
18378
18379   array_die = new_die (DW_TAG_array_type, scope_die, type);
18380   add_name_attribute (array_die, type_tag (type));
18381   equate_type_number_to_die (type, array_die);
18382
18383   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18384   if (is_fortran ()
18385       && info->ndimensions >= 2)
18386     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18387
18388   if (info->data_location)
18389     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18390                           info->base_decl);
18391   if (info->associated)
18392     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18393                           info->base_decl);
18394   if (info->allocated)
18395     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18396                           info->base_decl);
18397
18398   for (dim = 0; dim < info->ndimensions; dim++)
18399     {
18400       dw_die_ref subrange_die
18401         = new_die (DW_TAG_subrange_type, array_die, NULL);
18402
18403       if (info->dimen[dim].lower_bound)
18404         {
18405           /* If it is the default value, omit it.  */
18406           int dflt;
18407
18408           if (host_integerp (info->dimen[dim].lower_bound, 0)
18409               && (dflt = lower_bound_default ()) != -1
18410               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18411             ;
18412           else
18413             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18414                                   info->dimen[dim].lower_bound,
18415                                   info->base_decl);
18416         }
18417       if (info->dimen[dim].upper_bound)
18418         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18419                               info->dimen[dim].upper_bound,
18420                               info->base_decl);
18421       if (info->dimen[dim].stride)
18422         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18423                               info->dimen[dim].stride,
18424                               info->base_decl);
18425     }
18426
18427   gen_type_die (info->element_type, context_die);
18428   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18429
18430   if (get_AT (array_die, DW_AT_name))
18431     add_pubtype (type, array_die);
18432 }
18433
18434 #if 0
18435 static void
18436 gen_entry_point_die (tree decl, dw_die_ref context_die)
18437 {
18438   tree origin = decl_ultimate_origin (decl);
18439   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18440
18441   if (origin != NULL)
18442     add_abstract_origin_attribute (decl_die, origin);
18443   else
18444     {
18445       add_name_and_src_coords_attributes (decl_die, decl);
18446       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18447                           0, 0, context_die);
18448     }
18449
18450   if (DECL_ABSTRACT (decl))
18451     equate_decl_number_to_die (decl, decl_die);
18452   else
18453     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18454 }
18455 #endif
18456
18457 /* Walk through the list of incomplete types again, trying once more to
18458    emit full debugging info for them.  */
18459
18460 static void
18461 retry_incomplete_types (void)
18462 {
18463   int i;
18464
18465   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18466     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18467                                   DINFO_USAGE_DIR_USE))
18468       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18469 }
18470
18471 /* Determine what tag to use for a record type.  */
18472
18473 static enum dwarf_tag
18474 record_type_tag (tree type)
18475 {
18476   if (! lang_hooks.types.classify_record)
18477     return DW_TAG_structure_type;
18478
18479   switch (lang_hooks.types.classify_record (type))
18480     {
18481     case RECORD_IS_STRUCT:
18482       return DW_TAG_structure_type;
18483
18484     case RECORD_IS_CLASS:
18485       return DW_TAG_class_type;
18486
18487     case RECORD_IS_INTERFACE:
18488       if (dwarf_version >= 3 || !dwarf_strict)
18489         return DW_TAG_interface_type;
18490       return DW_TAG_structure_type;
18491
18492     default:
18493       gcc_unreachable ();
18494     }
18495 }
18496
18497 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18498    include all of the information about the enumeration values also. Each
18499    enumerated type name/value is listed as a child of the enumerated type
18500    DIE.  */
18501
18502 static dw_die_ref
18503 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18504 {
18505   dw_die_ref type_die = lookup_type_die (type);
18506
18507   if (type_die == NULL)
18508     {
18509       type_die = new_die (DW_TAG_enumeration_type,
18510                           scope_die_for (type, context_die), type);
18511       equate_type_number_to_die (type, type_die);
18512       add_name_attribute (type_die, type_tag (type));
18513       if (dwarf_version >= 4 || !dwarf_strict)
18514         {
18515           if (ENUM_IS_SCOPED (type))
18516             add_AT_flag (type_die, DW_AT_enum_class, 1);
18517           if (ENUM_IS_OPAQUE (type))
18518             add_AT_flag (type_die, DW_AT_declaration, 1);
18519         }
18520     }
18521   else if (! TYPE_SIZE (type))
18522     return type_die;
18523   else
18524     remove_AT (type_die, DW_AT_declaration);
18525
18526   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18527      given enum type is incomplete, do not generate the DW_AT_byte_size
18528      attribute or the DW_AT_element_list attribute.  */
18529   if (TYPE_SIZE (type))
18530     {
18531       tree link;
18532
18533       TREE_ASM_WRITTEN (type) = 1;
18534       add_byte_size_attribute (type_die, type);
18535       if (TYPE_STUB_DECL (type) != NULL_TREE)
18536         {
18537           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18538           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18539         }
18540
18541       /* If the first reference to this type was as the return type of an
18542          inline function, then it may not have a parent.  Fix this now.  */
18543       if (type_die->die_parent == NULL)
18544         add_child_die (scope_die_for (type, context_die), type_die);
18545
18546       for (link = TYPE_VALUES (type);
18547            link != NULL; link = TREE_CHAIN (link))
18548         {
18549           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18550           tree value = TREE_VALUE (link);
18551
18552           add_name_attribute (enum_die,
18553                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18554
18555           if (TREE_CODE (value) == CONST_DECL)
18556             value = DECL_INITIAL (value);
18557
18558           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18559             /* DWARF2 does not provide a way of indicating whether or
18560                not enumeration constants are signed or unsigned.  GDB
18561                always assumes the values are signed, so we output all
18562                values as if they were signed.  That means that
18563                enumeration constants with very large unsigned values
18564                will appear to have negative values in the debugger.  */
18565             add_AT_int (enum_die, DW_AT_const_value,
18566                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18567         }
18568     }
18569   else
18570     add_AT_flag (type_die, DW_AT_declaration, 1);
18571
18572   if (get_AT (type_die, DW_AT_name))
18573     add_pubtype (type, type_die);
18574
18575   return type_die;
18576 }
18577
18578 /* Generate a DIE to represent either a real live formal parameter decl or to
18579    represent just the type of some formal parameter position in some function
18580    type.
18581
18582    Note that this routine is a bit unusual because its argument may be a
18583    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18584    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18585    node.  If it's the former then this function is being called to output a
18586    DIE to represent a formal parameter object (or some inlining thereof).  If
18587    it's the latter, then this function is only being called to output a
18588    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18589    argument type of some subprogram type.
18590    If EMIT_NAME_P is true, name and source coordinate attributes
18591    are emitted.  */
18592
18593 static dw_die_ref
18594 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18595                           dw_die_ref context_die)
18596 {
18597   tree node_or_origin = node ? node : origin;
18598   tree ultimate_origin;
18599   dw_die_ref parm_die
18600     = new_die (DW_TAG_formal_parameter, context_die, node);
18601
18602   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18603     {
18604     case tcc_declaration:
18605       ultimate_origin = decl_ultimate_origin (node_or_origin);
18606       if (node || ultimate_origin)
18607         origin = ultimate_origin;
18608       if (origin != NULL)
18609         add_abstract_origin_attribute (parm_die, origin);
18610       else if (emit_name_p)
18611         add_name_and_src_coords_attributes (parm_die, node);
18612       if (origin == NULL
18613           || (! DECL_ABSTRACT (node_or_origin)
18614               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18615                                            decl_function_context
18616                                                             (node_or_origin))))
18617         {
18618           tree type = TREE_TYPE (node_or_origin);
18619           if (decl_by_reference_p (node_or_origin))
18620             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18621                                 context_die);
18622           else
18623             add_type_attribute (parm_die, type,
18624                                 TREE_READONLY (node_or_origin),
18625                                 TREE_THIS_VOLATILE (node_or_origin),
18626                                 context_die);
18627         }
18628       if (origin == NULL && DECL_ARTIFICIAL (node))
18629         add_AT_flag (parm_die, DW_AT_artificial, 1);
18630
18631       if (node && node != origin)
18632         equate_decl_number_to_die (node, parm_die);
18633       if (! DECL_ABSTRACT (node_or_origin))
18634         add_location_or_const_value_attribute (parm_die, node_or_origin,
18635                                                DW_AT_location);
18636
18637       break;
18638
18639     case tcc_type:
18640       /* We were called with some kind of a ..._TYPE node.  */
18641       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18642       break;
18643
18644     default:
18645       gcc_unreachable ();
18646     }
18647
18648   return parm_die;
18649 }
18650
18651 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18652    children DW_TAG_formal_parameter DIEs representing the arguments of the
18653    parameter pack.
18654
18655    PARM_PACK must be a function parameter pack.
18656    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18657    must point to the subsequent arguments of the function PACK_ARG belongs to.
18658    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18659    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18660    following the last one for which a DIE was generated.  */
18661
18662 static dw_die_ref
18663 gen_formal_parameter_pack_die  (tree parm_pack,
18664                                 tree pack_arg,
18665                                 dw_die_ref subr_die,
18666                                 tree *next_arg)
18667 {
18668   tree arg;
18669   dw_die_ref parm_pack_die;
18670
18671   gcc_assert (parm_pack
18672               && lang_hooks.function_parameter_pack_p (parm_pack)
18673               && subr_die);
18674
18675   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18676   add_src_coords_attributes (parm_pack_die, parm_pack);
18677
18678   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18679     {
18680       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18681                                                                  parm_pack))
18682         break;
18683       gen_formal_parameter_die (arg, NULL,
18684                                 false /* Don't emit name attribute.  */,
18685                                 parm_pack_die);
18686     }
18687   if (next_arg)
18688     *next_arg = arg;
18689   return parm_pack_die;
18690 }
18691
18692 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18693    at the end of an (ANSI prototyped) formal parameters list.  */
18694
18695 static void
18696 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18697 {
18698   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18699 }
18700
18701 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18702    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18703    parameters as specified in some function type specification (except for
18704    those which appear as part of a function *definition*).  */
18705
18706 static void
18707 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18708 {
18709   tree link;
18710   tree formal_type = NULL;
18711   tree first_parm_type;
18712   tree arg;
18713
18714   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18715     {
18716       arg = DECL_ARGUMENTS (function_or_method_type);
18717       function_or_method_type = TREE_TYPE (function_or_method_type);
18718     }
18719   else
18720     arg = NULL_TREE;
18721
18722   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18723
18724   /* Make our first pass over the list of formal parameter types and output a
18725      DW_TAG_formal_parameter DIE for each one.  */
18726   for (link = first_parm_type; link; )
18727     {
18728       dw_die_ref parm_die;
18729
18730       formal_type = TREE_VALUE (link);
18731       if (formal_type == void_type_node)
18732         break;
18733
18734       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18735       parm_die = gen_formal_parameter_die (formal_type, NULL,
18736                                            true /* Emit name attribute.  */,
18737                                            context_die);
18738       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18739           && link == first_parm_type)
18740         {
18741           add_AT_flag (parm_die, DW_AT_artificial, 1);
18742           if (dwarf_version >= 3 || !dwarf_strict)
18743             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18744         }
18745       else if (arg && DECL_ARTIFICIAL (arg))
18746         add_AT_flag (parm_die, DW_AT_artificial, 1);
18747
18748       link = TREE_CHAIN (link);
18749       if (arg)
18750         arg = DECL_CHAIN (arg);
18751     }
18752
18753   /* If this function type has an ellipsis, add a
18754      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18755   if (formal_type != void_type_node)
18756     gen_unspecified_parameters_die (function_or_method_type, context_die);
18757
18758   /* Make our second (and final) pass over the list of formal parameter types
18759      and output DIEs to represent those types (as necessary).  */
18760   for (link = TYPE_ARG_TYPES (function_or_method_type);
18761        link && TREE_VALUE (link);
18762        link = TREE_CHAIN (link))
18763     gen_type_die (TREE_VALUE (link), context_die);
18764 }
18765
18766 /* We want to generate the DIE for TYPE so that we can generate the
18767    die for MEMBER, which has been defined; we will need to refer back
18768    to the member declaration nested within TYPE.  If we're trying to
18769    generate minimal debug info for TYPE, processing TYPE won't do the
18770    trick; we need to attach the member declaration by hand.  */
18771
18772 static void
18773 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18774 {
18775   gen_type_die (type, context_die);
18776
18777   /* If we're trying to avoid duplicate debug info, we may not have
18778      emitted the member decl for this function.  Emit it now.  */
18779   if (TYPE_STUB_DECL (type)
18780       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18781       && ! lookup_decl_die (member))
18782     {
18783       dw_die_ref type_die;
18784       gcc_assert (!decl_ultimate_origin (member));
18785
18786       push_decl_scope (type);
18787       type_die = lookup_type_die_strip_naming_typedef (type);
18788       if (TREE_CODE (member) == FUNCTION_DECL)
18789         gen_subprogram_die (member, type_die);
18790       else if (TREE_CODE (member) == FIELD_DECL)
18791         {
18792           /* Ignore the nameless fields that are used to skip bits but handle
18793              C++ anonymous unions and structs.  */
18794           if (DECL_NAME (member) != NULL_TREE
18795               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18796               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18797             {
18798               gen_type_die (member_declared_type (member), type_die);
18799               gen_field_die (member, type_die);
18800             }
18801         }
18802       else
18803         gen_variable_die (member, NULL_TREE, type_die);
18804
18805       pop_decl_scope ();
18806     }
18807 }
18808
18809 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18810    may later generate inlined and/or out-of-line instances of.  */
18811
18812 static void
18813 dwarf2out_abstract_function (tree decl)
18814 {
18815   dw_die_ref old_die;
18816   tree save_fn;
18817   tree context;
18818   int was_abstract;
18819   htab_t old_decl_loc_table;
18820   int old_call_site_count, old_tail_call_site_count;
18821   struct call_arg_loc_node *old_call_arg_locations;
18822
18823   /* Make sure we have the actual abstract inline, not a clone.  */
18824   decl = DECL_ORIGIN (decl);
18825
18826   old_die = lookup_decl_die (decl);
18827   if (old_die && get_AT (old_die, DW_AT_inline))
18828     /* We've already generated the abstract instance.  */
18829     return;
18830
18831   /* We can be called while recursively when seeing block defining inlined subroutine
18832      DIE.  Be sure to not clobber the outer location table nor use it or we would
18833      get locations in abstract instantces.  */
18834   old_decl_loc_table = decl_loc_table;
18835   decl_loc_table = NULL;
18836   old_call_arg_locations = call_arg_locations;
18837   call_arg_locations = NULL;
18838   old_call_site_count = call_site_count;
18839   call_site_count = -1;
18840   old_tail_call_site_count = tail_call_site_count;
18841   tail_call_site_count = -1;
18842
18843   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18844      we don't get confused by DECL_ABSTRACT.  */
18845   if (debug_info_level > DINFO_LEVEL_TERSE)
18846     {
18847       context = decl_class_context (decl);
18848       if (context)
18849         gen_type_die_for_member
18850           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18851     }
18852
18853   /* Pretend we've just finished compiling this function.  */
18854   save_fn = current_function_decl;
18855   current_function_decl = decl;
18856   push_cfun (DECL_STRUCT_FUNCTION (decl));
18857
18858   was_abstract = DECL_ABSTRACT (decl);
18859   set_decl_abstract_flags (decl, 1);
18860   dwarf2out_decl (decl);
18861   if (! was_abstract)
18862     set_decl_abstract_flags (decl, 0);
18863
18864   current_function_decl = save_fn;
18865   decl_loc_table = old_decl_loc_table;
18866   call_arg_locations = old_call_arg_locations;
18867   call_site_count = old_call_site_count;
18868   tail_call_site_count = old_tail_call_site_count;
18869   pop_cfun ();
18870 }
18871
18872 /* Helper function of premark_used_types() which gets called through
18873    htab_traverse.
18874
18875    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18876    marked as unused by prune_unused_types.  */
18877
18878 static int
18879 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18880 {
18881   tree type;
18882   dw_die_ref die;
18883
18884   type = (tree) *slot;
18885   die = lookup_type_die (type);
18886   if (die != NULL)
18887     die->die_perennial_p = 1;
18888   return 1;
18889 }
18890
18891 /* Helper function of premark_types_used_by_global_vars which gets called
18892    through htab_traverse.
18893
18894    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18895    marked as unused by prune_unused_types. The DIE of the type is marked
18896    only if the global variable using the type will actually be emitted.  */
18897
18898 static int
18899 premark_types_used_by_global_vars_helper (void **slot,
18900                                           void *data ATTRIBUTE_UNUSED)
18901 {
18902   struct types_used_by_vars_entry *entry;
18903   dw_die_ref die;
18904
18905   entry = (struct types_used_by_vars_entry *) *slot;
18906   gcc_assert (entry->type != NULL
18907               && entry->var_decl != NULL);
18908   die = lookup_type_die (entry->type);
18909   if (die)
18910     {
18911       /* Ask cgraph if the global variable really is to be emitted.
18912          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18913       struct varpool_node *node = varpool_get_node (entry->var_decl);
18914       if (node && node->needed)
18915         {
18916           die->die_perennial_p = 1;
18917           /* Keep the parent DIEs as well.  */
18918           while ((die = die->die_parent) && die->die_perennial_p == 0)
18919             die->die_perennial_p = 1;
18920         }
18921     }
18922   return 1;
18923 }
18924
18925 /* Mark all members of used_types_hash as perennial.  */
18926
18927 static void
18928 premark_used_types (void)
18929 {
18930   if (cfun && cfun->used_types_hash)
18931     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18932 }
18933
18934 /* Mark all members of types_used_by_vars_entry as perennial.  */
18935
18936 static void
18937 premark_types_used_by_global_vars (void)
18938 {
18939   if (types_used_by_vars_hash)
18940     htab_traverse (types_used_by_vars_hash,
18941                    premark_types_used_by_global_vars_helper, NULL);
18942 }
18943
18944 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18945    for CA_LOC call arg loc node.  */
18946
18947 static dw_die_ref
18948 gen_call_site_die (tree decl, dw_die_ref subr_die,
18949                    struct call_arg_loc_node *ca_loc)
18950 {
18951   dw_die_ref stmt_die = NULL, die;
18952   tree block = ca_loc->block;
18953
18954   while (block
18955          && block != DECL_INITIAL (decl)
18956          && TREE_CODE (block) == BLOCK)
18957     {
18958       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
18959         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
18960       if (stmt_die)
18961         break;
18962       block = BLOCK_SUPERCONTEXT (block);
18963     }
18964   if (stmt_die == NULL)
18965     stmt_die = subr_die;
18966   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18967   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18968   if (ca_loc->tail_call_p)
18969     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18970   if (ca_loc->symbol_ref)
18971     {
18972       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18973       if (tdie)
18974         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18975       else
18976         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
18977     }
18978   return die;
18979 }
18980
18981 /* Generate a DIE to represent a declared function (either file-scope or
18982    block-local).  */
18983
18984 static void
18985 gen_subprogram_die (tree decl, dw_die_ref context_die)
18986 {
18987   tree origin = decl_ultimate_origin (decl);
18988   dw_die_ref subr_die;
18989   tree outer_scope;
18990   dw_die_ref old_die = lookup_decl_die (decl);
18991   int declaration = (current_function_decl != decl
18992                      || class_or_namespace_scope_p (context_die));
18993
18994   premark_used_types ();
18995
18996   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18997      started to generate the abstract instance of an inline, decided to output
18998      its containing class, and proceeded to emit the declaration of the inline
18999      from the member list for the class.  If so, DECLARATION takes priority;
19000      we'll get back to the abstract instance when done with the class.  */
19001
19002   /* The class-scope declaration DIE must be the primary DIE.  */
19003   if (origin && declaration && class_or_namespace_scope_p (context_die))
19004     {
19005       origin = NULL;
19006       gcc_assert (!old_die);
19007     }
19008
19009   /* Now that the C++ front end lazily declares artificial member fns, we
19010      might need to retrofit the declaration into its class.  */
19011   if (!declaration && !origin && !old_die
19012       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19013       && !class_or_namespace_scope_p (context_die)
19014       && debug_info_level > DINFO_LEVEL_TERSE)
19015     old_die = force_decl_die (decl);
19016
19017   if (origin != NULL)
19018     {
19019       gcc_assert (!declaration || local_scope_p (context_die));
19020
19021       /* Fixup die_parent for the abstract instance of a nested
19022          inline function.  */
19023       if (old_die && old_die->die_parent == NULL)
19024         add_child_die (context_die, old_die);
19025
19026       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19027       add_abstract_origin_attribute (subr_die, origin);
19028     }
19029   else if (old_die)
19030     {
19031       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19032       struct dwarf_file_data * file_index = lookup_filename (s.file);
19033
19034       if (!get_AT_flag (old_die, DW_AT_declaration)
19035           /* We can have a normal definition following an inline one in the
19036              case of redefinition of GNU C extern inlines.
19037              It seems reasonable to use AT_specification in this case.  */
19038           && !get_AT (old_die, DW_AT_inline))
19039         {
19040           /* Detect and ignore this case, where we are trying to output
19041              something we have already output.  */
19042           return;
19043         }
19044
19045       /* If the definition comes from the same place as the declaration,
19046          maybe use the old DIE.  We always want the DIE for this function
19047          that has the *_pc attributes to be under comp_unit_die so the
19048          debugger can find it.  We also need to do this for abstract
19049          instances of inlines, since the spec requires the out-of-line copy
19050          to have the same parent.  For local class methods, this doesn't
19051          apply; we just use the old DIE.  */
19052       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19053           && (DECL_ARTIFICIAL (decl)
19054               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19055                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19056                       == (unsigned) s.line))))
19057         {
19058           subr_die = old_die;
19059
19060           /* Clear out the declaration attribute and the formal parameters.
19061              Do not remove all children, because it is possible that this
19062              declaration die was forced using force_decl_die(). In such
19063              cases die that forced declaration die (e.g. TAG_imported_module)
19064              is one of the children that we do not want to remove.  */
19065           remove_AT (subr_die, DW_AT_declaration);
19066           remove_AT (subr_die, DW_AT_object_pointer);
19067           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19068         }
19069       else
19070         {
19071           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19072           add_AT_specification (subr_die, old_die);
19073           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19074             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19075           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19076             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19077         }
19078     }
19079   else
19080     {
19081       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19082
19083       if (TREE_PUBLIC (decl))
19084         add_AT_flag (subr_die, DW_AT_external, 1);
19085
19086       add_name_and_src_coords_attributes (subr_die, decl);
19087       if (debug_info_level > DINFO_LEVEL_TERSE)
19088         {
19089           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19090           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19091                               0, 0, context_die);
19092         }
19093
19094       add_pure_or_virtual_attribute (subr_die, decl);
19095       if (DECL_ARTIFICIAL (decl))
19096         add_AT_flag (subr_die, DW_AT_artificial, 1);
19097
19098       add_accessibility_attribute (subr_die, decl);
19099     }
19100
19101   if (declaration)
19102     {
19103       if (!old_die || !get_AT (old_die, DW_AT_inline))
19104         {
19105           add_AT_flag (subr_die, DW_AT_declaration, 1);
19106
19107           /* If this is an explicit function declaration then generate
19108              a DW_AT_explicit attribute.  */
19109           if (lang_hooks.decls.function_decl_explicit_p (decl)
19110               && (dwarf_version >= 3 || !dwarf_strict))
19111             add_AT_flag (subr_die, DW_AT_explicit, 1);
19112
19113           /* The first time we see a member function, it is in the context of
19114              the class to which it belongs.  We make sure of this by emitting
19115              the class first.  The next time is the definition, which is
19116              handled above.  The two may come from the same source text.
19117
19118              Note that force_decl_die() forces function declaration die. It is
19119              later reused to represent definition.  */
19120           equate_decl_number_to_die (decl, subr_die);
19121         }
19122     }
19123   else if (DECL_ABSTRACT (decl))
19124     {
19125       if (DECL_DECLARED_INLINE_P (decl))
19126         {
19127           if (cgraph_function_possibly_inlined_p (decl))
19128             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19129           else
19130             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19131         }
19132       else
19133         {
19134           if (cgraph_function_possibly_inlined_p (decl))
19135             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19136           else
19137             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19138         }
19139
19140       if (DECL_DECLARED_INLINE_P (decl)
19141           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19142         add_AT_flag (subr_die, DW_AT_artificial, 1);
19143
19144       equate_decl_number_to_die (decl, subr_die);
19145     }
19146   else if (!DECL_EXTERNAL (decl))
19147     {
19148       HOST_WIDE_INT cfa_fb_offset;
19149
19150       if (!old_die || !get_AT (old_die, DW_AT_inline))
19151         equate_decl_number_to_die (decl, subr_die);
19152
19153       if (!flag_reorder_blocks_and_partition)
19154         {
19155           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19156           if (fde->dw_fde_begin)
19157             {
19158               /* We have already generated the labels.  */
19159               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19160               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19161             }
19162           else
19163             {
19164               /* Create start/end labels and add the range.  */
19165               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19166               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19167                                            current_function_funcdef_no);
19168               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19169               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19170                                            current_function_funcdef_no);
19171               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19172             }
19173
19174 #if VMS_DEBUGGING_INFO
19175       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19176          Section 2.3 Prologue and Epilogue Attributes:
19177          When a breakpoint is set on entry to a function, it is generally
19178          desirable for execution to be suspended, not on the very first
19179          instruction of the function, but rather at a point after the
19180          function's frame has been set up, after any language defined local
19181          declaration processing has been completed, and before execution of
19182          the first statement of the function begins. Debuggers generally
19183          cannot properly determine where this point is.  Similarly for a
19184          breakpoint set on exit from a function. The prologue and epilogue
19185          attributes allow a compiler to communicate the location(s) to use.  */
19186
19187       {
19188         if (fde->dw_fde_vms_end_prologue)
19189           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19190             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19191
19192         if (fde->dw_fde_vms_begin_epilogue)
19193           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19194             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19195       }
19196 #endif
19197
19198           add_pubname (decl, subr_die);
19199           add_arange (decl, subr_die);
19200         }
19201       else
19202         {  /* Generate pubnames entries for the split function code
19203               ranges.  */
19204           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19205
19206           if (fde->dw_fde_switched_sections)
19207             {
19208               if (dwarf_version >= 3 || !dwarf_strict)
19209                 {
19210                   /* We should use ranges for non-contiguous code section 
19211                      addresses.  Use the actual code range for the initial
19212                      section, since the HOT/COLD labels might precede an 
19213                      alignment offset.  */
19214                   bool range_list_added = false;
19215                   if (fde->in_std_section)
19216                     {
19217                       add_ranges_by_labels (subr_die,
19218                                             fde->dw_fde_begin,
19219                                             fde->dw_fde_end,
19220                                             &range_list_added);
19221                       add_ranges_by_labels (subr_die,
19222                                             fde->dw_fde_unlikely_section_label,
19223                                             fde->dw_fde_unlikely_section_end_label,
19224                                             &range_list_added);
19225                     }
19226                   else
19227                     {
19228                       add_ranges_by_labels (subr_die,
19229                                             fde->dw_fde_begin,
19230                                             fde->dw_fde_end,
19231                                             &range_list_added);
19232                       add_ranges_by_labels (subr_die,
19233                                             fde->dw_fde_hot_section_label,
19234                                             fde->dw_fde_hot_section_end_label,
19235                                             &range_list_added);
19236                     }
19237                   add_pubname (decl, subr_die);
19238                   if (range_list_added)
19239                     add_ranges (NULL);
19240                 }
19241               else
19242                 {
19243                   /* There is no real support in DW2 for this .. so we make
19244                      a work-around.  First, emit the pub name for the segment
19245                      containing the function label.  Then make and emit a
19246                      simplified subprogram DIE for the second segment with the
19247                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19248                      linkage name for the second die so that gdb will find both
19249                      sections when given "b foo".  */
19250                   const char *name = NULL;
19251                   tree decl_name = DECL_NAME (decl);
19252                   dw_die_ref seg_die;
19253
19254                   /* Do the 'primary' section.   */
19255                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19256                                  fde->dw_fde_begin);
19257                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19258                                  fde->dw_fde_end);
19259                   /* Add it.   */
19260                   add_pubname (decl, subr_die);
19261                   add_arange (decl, subr_die);
19262
19263                   /* Build a minimal DIE for the secondary section.  */
19264                   seg_die = new_die (DW_TAG_subprogram,
19265                                      subr_die->die_parent, decl);
19266
19267                   if (TREE_PUBLIC (decl))
19268                     add_AT_flag (seg_die, DW_AT_external, 1);
19269
19270                   if (decl_name != NULL 
19271                       && IDENTIFIER_POINTER (decl_name) != NULL)
19272                     {
19273                       name = dwarf2_name (decl, 1);
19274                       if (! DECL_ARTIFICIAL (decl))
19275                         add_src_coords_attributes (seg_die, decl);
19276
19277                       add_linkage_name (seg_die, decl);
19278                     }
19279                   gcc_assert (name!=NULL);
19280                   add_pure_or_virtual_attribute (seg_die, decl);
19281                   if (DECL_ARTIFICIAL (decl))
19282                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19283
19284                   if (fde->in_std_section)
19285                     {
19286                       name = concat ("__cold_sect_of_", name, NULL); 
19287                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19288                                      fde->dw_fde_unlikely_section_label);
19289                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19290                                      fde->dw_fde_unlikely_section_end_label); 
19291                     }
19292                   else 
19293                     {
19294                       name = concat ("__hot_sect_of_", name, NULL); 
19295                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19296                                      fde->dw_fde_hot_section_label);
19297                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19298                                      fde->dw_fde_hot_section_end_label); 
19299                     }
19300                   add_name_attribute (seg_die, name);
19301                   add_pubname_string (name, seg_die);
19302                   add_arange (decl, seg_die);
19303                 }
19304             }
19305           else
19306             {
19307               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19308               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19309               add_pubname (decl, subr_die);
19310               add_arange (decl, subr_die);
19311             }
19312         }
19313
19314 #ifdef MIPS_DEBUGGING_INFO
19315       /* Add a reference to the FDE for this routine.  */
19316       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19317 #endif
19318
19319       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19320
19321       /* We define the "frame base" as the function's CFA.  This is more
19322          convenient for several reasons: (1) It's stable across the prologue
19323          and epilogue, which makes it better than just a frame pointer,
19324          (2) With dwarf3, there exists a one-byte encoding that allows us
19325          to reference the .debug_frame data by proxy, but failing that,
19326          (3) We can at least reuse the code inspection and interpretation
19327          code that determines the CFA position at various points in the
19328          function.  */
19329       if (dwarf_version >= 3)
19330         {
19331           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19332           add_AT_loc (subr_die, DW_AT_frame_base, op);
19333         }
19334       else
19335         {
19336           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19337           if (list->dw_loc_next)
19338             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19339           else
19340             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19341         }
19342
19343       /* Compute a displacement from the "steady-state frame pointer" to
19344          the CFA.  The former is what all stack slots and argument slots
19345          will reference in the rtl; the later is what we've told the
19346          debugger about.  We'll need to adjust all frame_base references
19347          by this displacement.  */
19348       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19349
19350       if (cfun->static_chain_decl)
19351         add_AT_location_description (subr_die, DW_AT_static_link,
19352                  loc_list_from_tree (cfun->static_chain_decl, 2));
19353     }
19354
19355   /* Generate child dies for template paramaters.  */
19356   if (debug_info_level > DINFO_LEVEL_TERSE)
19357     gen_generic_params_dies (decl);
19358
19359   /* Now output descriptions of the arguments for this function. This gets
19360      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19361      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19362      `...' at the end of the formal parameter list.  In order to find out if
19363      there was a trailing ellipsis or not, we must instead look at the type
19364      associated with the FUNCTION_DECL.  This will be a node of type
19365      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19366      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19367      an ellipsis at the end.  */
19368
19369   /* In the case where we are describing a mere function declaration, all we
19370      need to do here (and all we *can* do here) is to describe the *types* of
19371      its formal parameters.  */
19372   if (debug_info_level <= DINFO_LEVEL_TERSE)
19373     ;
19374   else if (declaration)
19375     gen_formal_types_die (decl, subr_die);
19376   else
19377     {
19378       /* Generate DIEs to represent all known formal parameters.  */
19379       tree parm = DECL_ARGUMENTS (decl);
19380       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19381       tree generic_decl_parm = generic_decl
19382                                 ? DECL_ARGUMENTS (generic_decl)
19383                                 : NULL;
19384
19385       /* Now we want to walk the list of parameters of the function and
19386          emit their relevant DIEs.
19387
19388          We consider the case of DECL being an instance of a generic function
19389          as well as it being a normal function.
19390
19391          If DECL is an instance of a generic function we walk the
19392          parameters of the generic function declaration _and_ the parameters of
19393          DECL itself. This is useful because we want to emit specific DIEs for
19394          function parameter packs and those are declared as part of the
19395          generic function declaration. In that particular case,
19396          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19397          That DIE has children DIEs representing the set of arguments
19398          of the pack. Note that the set of pack arguments can be empty.
19399          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19400          children DIE.
19401
19402          Otherwise, we just consider the parameters of DECL.  */
19403       while (generic_decl_parm || parm)
19404         {
19405           if (generic_decl_parm
19406               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19407             gen_formal_parameter_pack_die (generic_decl_parm,
19408                                            parm, subr_die,
19409                                            &parm);
19410           else if (parm)
19411             {
19412               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19413
19414               if (parm == DECL_ARGUMENTS (decl)
19415                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19416                   && parm_die
19417                   && (dwarf_version >= 3 || !dwarf_strict))
19418                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19419
19420               parm = DECL_CHAIN (parm);
19421             }
19422
19423           if (generic_decl_parm)
19424             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19425         }
19426
19427       /* Decide whether we need an unspecified_parameters DIE at the end.
19428          There are 2 more cases to do this for: 1) the ansi ... declaration -
19429          this is detectable when the end of the arg list is not a
19430          void_type_node 2) an unprototyped function declaration (not a
19431          definition).  This just means that we have no info about the
19432          parameters at all.  */
19433       if (prototype_p (TREE_TYPE (decl)))
19434         {
19435           /* This is the prototyped case, check for....  */
19436           if (stdarg_p (TREE_TYPE (decl)))
19437             gen_unspecified_parameters_die (decl, subr_die);
19438         }
19439       else if (DECL_INITIAL (decl) == NULL_TREE)
19440         gen_unspecified_parameters_die (decl, subr_die);
19441     }
19442
19443   /* Output Dwarf info for all of the stuff within the body of the function
19444      (if it has one - it may be just a declaration).  */
19445   outer_scope = DECL_INITIAL (decl);
19446
19447   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19448      a function.  This BLOCK actually represents the outermost binding contour
19449      for the function, i.e. the contour in which the function's formal
19450      parameters and labels get declared. Curiously, it appears that the front
19451      end doesn't actually put the PARM_DECL nodes for the current function onto
19452      the BLOCK_VARS list for this outer scope, but are strung off of the
19453      DECL_ARGUMENTS list for the function instead.
19454
19455      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19456      the LABEL_DECL nodes for the function however, and we output DWARF info
19457      for those in decls_for_scope.  Just within the `outer_scope' there will be
19458      a BLOCK node representing the function's outermost pair of curly braces,
19459      and any blocks used for the base and member initializers of a C++
19460      constructor function.  */
19461   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19462     {
19463       int call_site_note_count = 0;
19464       int tail_call_site_note_count = 0;
19465
19466       /* Emit a DW_TAG_variable DIE for a named return value.  */
19467       if (DECL_NAME (DECL_RESULT (decl)))
19468         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19469
19470       current_function_has_inlines = 0;
19471       decls_for_scope (outer_scope, subr_die, 0);
19472
19473       if (call_arg_locations && !dwarf_strict)
19474         {
19475           struct call_arg_loc_node *ca_loc;
19476           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19477             {
19478               dw_die_ref die = NULL;
19479               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19480               rtx arg, next_arg;
19481
19482               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19483                    arg; arg = next_arg)
19484                 {
19485                   dw_loc_descr_ref reg, val;
19486                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19487                   dw_die_ref cdie;
19488
19489                   next_arg = XEXP (arg, 1);
19490                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19491                       && next_arg
19492                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19493                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19494                       && REGNO (XEXP (XEXP (arg, 0), 0))
19495                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19496                     next_arg = XEXP (next_arg, 1);
19497                   if (mode == VOIDmode)
19498                     {
19499                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19500                       if (mode == VOIDmode)
19501                         mode = GET_MODE (XEXP (arg, 0));
19502                     }
19503                   if (GET_MODE_CLASS (mode) != MODE_INT
19504                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19505                     continue;
19506                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19507                     {
19508                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19509                       tloc = XEXP (XEXP (arg, 0), 1);
19510                       continue;
19511                     }
19512                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19513                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19514                     {
19515                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19516                       tlocc = XEXP (XEXP (arg, 0), 1);
19517                       continue;
19518                     }
19519                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19520                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19521                                               VAR_INIT_STATUS_INITIALIZED);
19522                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19523                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19524                                                           0), 0), mode,
19525                                               VAR_INIT_STATUS_INITIALIZED);
19526                   else
19527                     continue;
19528                   if (reg == NULL)
19529                     continue;
19530                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19531                                             VAR_INIT_STATUS_INITIALIZED);
19532                   if (val == NULL)
19533                     continue;
19534                   if (die == NULL)
19535                     die = gen_call_site_die (decl, subr_die, ca_loc);
19536                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19537                                   NULL_TREE);           
19538                   add_AT_loc (cdie, DW_AT_location, reg);
19539                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19540                   if (next_arg != XEXP (arg, 1))
19541                     {
19542                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19543                                                             0), 1), VOIDmode,
19544                                                 VAR_INIT_STATUS_INITIALIZED);
19545                       if (val != NULL)
19546                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19547                     }
19548                 }
19549               if (die == NULL
19550                   && (ca_loc->symbol_ref || tloc))
19551                 die = gen_call_site_die (decl, subr_die, ca_loc);
19552               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19553                 {
19554                   dw_loc_descr_ref tval = NULL;
19555
19556                   if (tloc != NULL_RTX)
19557                     tval = mem_loc_descriptor (tloc, VOIDmode,
19558                                                VAR_INIT_STATUS_INITIALIZED);
19559                   if (tval)
19560                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19561                   else if (tlocc != NULL_RTX)
19562                     {
19563                       tval = mem_loc_descriptor (tlocc, VOIDmode,
19564                                                  VAR_INIT_STATUS_INITIALIZED);
19565                       if (tval)
19566                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19567                                     tval);
19568                     }
19569                 }
19570               if (die != NULL)
19571                 {
19572                   call_site_note_count++;
19573                   if (ca_loc->tail_call_p)
19574                     tail_call_site_note_count++;
19575                 }
19576             }
19577         }
19578       call_arg_locations = NULL;
19579       call_arg_loc_last = NULL;
19580       if (tail_call_site_count >= 0
19581           && tail_call_site_count == tail_call_site_note_count
19582           && !dwarf_strict)
19583         {
19584           if (call_site_count >= 0
19585               && call_site_count == call_site_note_count)
19586             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19587           else
19588             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19589         }
19590       call_site_count = -1;
19591       tail_call_site_count = -1;
19592     }
19593   /* Add the calling convention attribute if requested.  */
19594   add_calling_convention_attribute (subr_die, decl);
19595
19596 }
19597
19598 /* Returns a hash value for X (which really is a die_struct).  */
19599
19600 static hashval_t
19601 common_block_die_table_hash (const void *x)
19602 {
19603   const_dw_die_ref d = (const_dw_die_ref) x;
19604   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19605 }
19606
19607 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19608    as decl_id and die_parent of die_struct Y.  */
19609
19610 static int
19611 common_block_die_table_eq (const void *x, const void *y)
19612 {
19613   const_dw_die_ref d = (const_dw_die_ref) x;
19614   const_dw_die_ref e = (const_dw_die_ref) y;
19615   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19616 }
19617
19618 /* Generate a DIE to represent a declared data object.
19619    Either DECL or ORIGIN must be non-null.  */
19620
19621 static void
19622 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19623 {
19624   HOST_WIDE_INT off;
19625   tree com_decl;
19626   tree decl_or_origin = decl ? decl : origin;
19627   tree ultimate_origin;
19628   dw_die_ref var_die;
19629   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19630   dw_die_ref origin_die;
19631   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19632                       || class_or_namespace_scope_p (context_die));
19633   bool specialization_p = false;
19634
19635   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19636   if (decl || ultimate_origin)
19637     origin = ultimate_origin;
19638   com_decl = fortran_common (decl_or_origin, &off);
19639
19640   /* Symbol in common gets emitted as a child of the common block, in the form
19641      of a data member.  */
19642   if (com_decl)
19643     {
19644       dw_die_ref com_die;
19645       dw_loc_list_ref loc;
19646       die_node com_die_arg;
19647
19648       var_die = lookup_decl_die (decl_or_origin);
19649       if (var_die)
19650         {
19651           if (get_AT (var_die, DW_AT_location) == NULL)
19652             {
19653               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19654               if (loc)
19655                 {
19656                   if (off)
19657                     {
19658                       /* Optimize the common case.  */
19659                       if (single_element_loc_list_p (loc)
19660                           && loc->expr->dw_loc_opc == DW_OP_addr
19661                           && loc->expr->dw_loc_next == NULL
19662                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19663                              == SYMBOL_REF)
19664                         loc->expr->dw_loc_oprnd1.v.val_addr
19665                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19666                         else
19667                           loc_list_plus_const (loc, off);
19668                     }
19669                   add_AT_location_description (var_die, DW_AT_location, loc);
19670                   remove_AT (var_die, DW_AT_declaration);
19671                 }
19672             }
19673           return;
19674         }
19675
19676       if (common_block_die_table == NULL)
19677         common_block_die_table
19678           = htab_create_ggc (10, common_block_die_table_hash,
19679                              common_block_die_table_eq, NULL);
19680
19681       com_die_arg.decl_id = DECL_UID (com_decl);
19682       com_die_arg.die_parent = context_die;
19683       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19684       loc = loc_list_from_tree (com_decl, 2);
19685       if (com_die == NULL)
19686         {
19687           const char *cnam
19688             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19689           void **slot;
19690
19691           com_die = new_die (DW_TAG_common_block, context_die, decl);
19692           add_name_and_src_coords_attributes (com_die, com_decl);
19693           if (loc)
19694             {
19695               add_AT_location_description (com_die, DW_AT_location, loc);
19696               /* Avoid sharing the same loc descriptor between
19697                  DW_TAG_common_block and DW_TAG_variable.  */
19698               loc = loc_list_from_tree (com_decl, 2);
19699             }
19700           else if (DECL_EXTERNAL (decl))
19701             add_AT_flag (com_die, DW_AT_declaration, 1);
19702           add_pubname_string (cnam, com_die); /* ??? needed? */
19703           com_die->decl_id = DECL_UID (com_decl);
19704           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19705           *slot = (void *) com_die;
19706         }
19707       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19708         {
19709           add_AT_location_description (com_die, DW_AT_location, loc);
19710           loc = loc_list_from_tree (com_decl, 2);
19711           remove_AT (com_die, DW_AT_declaration);
19712         }
19713       var_die = new_die (DW_TAG_variable, com_die, decl);
19714       add_name_and_src_coords_attributes (var_die, decl);
19715       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19716                           TREE_THIS_VOLATILE (decl), context_die);
19717       add_AT_flag (var_die, DW_AT_external, 1);
19718       if (loc)
19719         {
19720           if (off)
19721             {
19722               /* Optimize the common case.  */
19723               if (single_element_loc_list_p (loc)
19724                   && loc->expr->dw_loc_opc == DW_OP_addr
19725                   && loc->expr->dw_loc_next == NULL
19726                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19727                 loc->expr->dw_loc_oprnd1.v.val_addr
19728                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19729               else
19730                 loc_list_plus_const (loc, off);
19731             }
19732           add_AT_location_description (var_die, DW_AT_location, loc);
19733         }
19734       else if (DECL_EXTERNAL (decl))
19735         add_AT_flag (var_die, DW_AT_declaration, 1);
19736       equate_decl_number_to_die (decl, var_die);
19737       return;
19738     }
19739
19740   /* If the compiler emitted a definition for the DECL declaration
19741      and if we already emitted a DIE for it, don't emit a second
19742      DIE for it again. Allow re-declarations of DECLs that are
19743      inside functions, though.  */
19744   if (old_die && declaration && !local_scope_p (context_die))
19745     return;
19746
19747   /* For static data members, the declaration in the class is supposed
19748      to have DW_TAG_member tag; the specification should still be
19749      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19750   if (declaration && class_scope_p (context_die))
19751     var_die = new_die (DW_TAG_member, context_die, decl);
19752   else
19753     var_die = new_die (DW_TAG_variable, context_die, decl);
19754
19755   origin_die = NULL;
19756   if (origin != NULL)
19757     origin_die = add_abstract_origin_attribute (var_die, origin);
19758
19759   /* Loop unrolling can create multiple blocks that refer to the same
19760      static variable, so we must test for the DW_AT_declaration flag.
19761
19762      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19763      copy decls and set the DECL_ABSTRACT flag on them instead of
19764      sharing them.
19765
19766      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19767
19768      ??? The declare_in_namespace support causes us to get two DIEs for one
19769      variable, both of which are declarations.  We want to avoid considering
19770      one to be a specification, so we must test that this DIE is not a
19771      declaration.  */
19772   else if (old_die && TREE_STATIC (decl) && ! declaration
19773            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19774     {
19775       /* This is a definition of a C++ class level static.  */
19776       add_AT_specification (var_die, old_die);
19777       specialization_p = true;
19778       if (DECL_NAME (decl))
19779         {
19780           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19781           struct dwarf_file_data * file_index = lookup_filename (s.file);
19782
19783           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19784             add_AT_file (var_die, DW_AT_decl_file, file_index);
19785
19786           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19787             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19788
19789           if (old_die->die_tag == DW_TAG_member)
19790             add_linkage_name (var_die, decl);
19791         }
19792     }
19793   else
19794     add_name_and_src_coords_attributes (var_die, decl);
19795
19796   if ((origin == NULL && !specialization_p)
19797       || (origin != NULL
19798           && !DECL_ABSTRACT (decl_or_origin)
19799           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19800                                        decl_function_context
19801                                                         (decl_or_origin))))
19802     {
19803       tree type = TREE_TYPE (decl_or_origin);
19804
19805       if (decl_by_reference_p (decl_or_origin))
19806         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19807       else
19808         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19809                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19810     }
19811
19812   if (origin == NULL && !specialization_p)
19813     {
19814       if (TREE_PUBLIC (decl))
19815         add_AT_flag (var_die, DW_AT_external, 1);
19816
19817       if (DECL_ARTIFICIAL (decl))
19818         add_AT_flag (var_die, DW_AT_artificial, 1);
19819
19820       add_accessibility_attribute (var_die, decl);
19821     }
19822
19823   if (declaration)
19824     add_AT_flag (var_die, DW_AT_declaration, 1);
19825
19826   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19827     equate_decl_number_to_die (decl, var_die);
19828
19829   if (! declaration
19830       && (! DECL_ABSTRACT (decl_or_origin)
19831           /* Local static vars are shared between all clones/inlines,
19832              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19833              already set.  */
19834           || (TREE_CODE (decl_or_origin) == VAR_DECL
19835               && TREE_STATIC (decl_or_origin)
19836               && DECL_RTL_SET_P (decl_or_origin)))
19837       /* When abstract origin already has DW_AT_location attribute, no need
19838          to add it again.  */
19839       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19840     {
19841       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19842           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19843         defer_location (decl_or_origin, var_die);
19844       else
19845         add_location_or_const_value_attribute (var_die,
19846                                                decl_or_origin,
19847                                                DW_AT_location);
19848       add_pubname (decl_or_origin, var_die);
19849     }
19850   else
19851     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19852 }
19853
19854 /* Generate a DIE to represent a named constant.  */
19855
19856 static void
19857 gen_const_die (tree decl, dw_die_ref context_die)
19858 {
19859   dw_die_ref const_die;
19860   tree type = TREE_TYPE (decl);
19861
19862   const_die = new_die (DW_TAG_constant, context_die, decl);
19863   add_name_and_src_coords_attributes (const_die, decl);
19864   add_type_attribute (const_die, type, 1, 0, context_die);
19865   if (TREE_PUBLIC (decl))
19866     add_AT_flag (const_die, DW_AT_external, 1);
19867   if (DECL_ARTIFICIAL (decl))
19868     add_AT_flag (const_die, DW_AT_artificial, 1);
19869   tree_add_const_value_attribute_for_decl (const_die, decl);
19870 }
19871
19872 /* Generate a DIE to represent a label identifier.  */
19873
19874 static void
19875 gen_label_die (tree decl, dw_die_ref context_die)
19876 {
19877   tree origin = decl_ultimate_origin (decl);
19878   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19879   rtx insn;
19880   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19881
19882   if (origin != NULL)
19883     add_abstract_origin_attribute (lbl_die, origin);
19884   else
19885     add_name_and_src_coords_attributes (lbl_die, decl);
19886
19887   if (DECL_ABSTRACT (decl))
19888     equate_decl_number_to_die (decl, lbl_die);
19889   else
19890     {
19891       insn = DECL_RTL_IF_SET (decl);
19892
19893       /* Deleted labels are programmer specified labels which have been
19894          eliminated because of various optimizations.  We still emit them
19895          here so that it is possible to put breakpoints on them.  */
19896       if (insn
19897           && (LABEL_P (insn)
19898               || ((NOTE_P (insn)
19899                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19900         {
19901           /* When optimization is enabled (via -O) some parts of the compiler
19902              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19903              represent source-level labels which were explicitly declared by
19904              the user.  This really shouldn't be happening though, so catch
19905              it if it ever does happen.  */
19906           gcc_assert (!INSN_DELETED_P (insn));
19907
19908           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19909           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19910         }
19911     }
19912 }
19913
19914 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19915    attributes to the DIE for a block STMT, to describe where the inlined
19916    function was called from.  This is similar to add_src_coords_attributes.  */
19917
19918 static inline void
19919 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19920 {
19921   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19922
19923   if (dwarf_version >= 3 || !dwarf_strict)
19924     {
19925       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19926       add_AT_unsigned (die, DW_AT_call_line, s.line);
19927     }
19928 }
19929
19930
19931 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19932    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19933
19934 static inline void
19935 add_high_low_attributes (tree stmt, dw_die_ref die)
19936 {
19937   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19938
19939   if (BLOCK_FRAGMENT_CHAIN (stmt)
19940       && (dwarf_version >= 3 || !dwarf_strict))
19941     {
19942       tree chain;
19943
19944       if (inlined_function_outer_scope_p (stmt))
19945         {
19946           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19947                                        BLOCK_NUMBER (stmt));
19948           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19949         }
19950
19951       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19952
19953       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19954       do
19955         {
19956           add_ranges (chain);
19957           chain = BLOCK_FRAGMENT_CHAIN (chain);
19958         }
19959       while (chain);
19960       add_ranges (NULL);
19961     }
19962   else
19963     {
19964       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19965                                    BLOCK_NUMBER (stmt));
19966       add_AT_lbl_id (die, DW_AT_low_pc, label);
19967       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19968                                    BLOCK_NUMBER (stmt));
19969       add_AT_lbl_id (die, DW_AT_high_pc, label);
19970     }
19971 }
19972
19973 /* Generate a DIE for a lexical block.  */
19974
19975 static void
19976 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19977 {
19978   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19979
19980   if (call_arg_locations)
19981     {
19982       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19983         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19984                                BLOCK_NUMBER (stmt) + 1);
19985       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
19986     }
19987
19988   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19989     add_high_low_attributes (stmt, stmt_die);
19990
19991   decls_for_scope (stmt, stmt_die, depth);
19992 }
19993
19994 /* Generate a DIE for an inlined subprogram.  */
19995
19996 static void
19997 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19998 {
19999   tree decl;
20000
20001   /* The instance of function that is effectively being inlined shall not
20002      be abstract.  */
20003   gcc_assert (! BLOCK_ABSTRACT (stmt));
20004
20005   decl = block_ultimate_origin (stmt);
20006
20007   /* Emit info for the abstract instance first, if we haven't yet.  We
20008      must emit this even if the block is abstract, otherwise when we
20009      emit the block below (or elsewhere), we may end up trying to emit
20010      a die whose origin die hasn't been emitted, and crashing.  */
20011   dwarf2out_abstract_function (decl);
20012
20013   if (! BLOCK_ABSTRACT (stmt))
20014     {
20015       dw_die_ref subr_die
20016         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20017
20018       if (call_arg_locations)
20019         {
20020           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20021             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20022                                    BLOCK_NUMBER (stmt) + 1);
20023           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20024         }
20025       add_abstract_origin_attribute (subr_die, decl);
20026       if (TREE_ASM_WRITTEN (stmt))
20027         add_high_low_attributes (stmt, subr_die);
20028       add_call_src_coords_attributes (stmt, subr_die);
20029
20030       decls_for_scope (stmt, subr_die, depth);
20031       current_function_has_inlines = 1;
20032     }
20033 }
20034
20035 /* Generate a DIE for a field in a record, or structure.  */
20036
20037 static void
20038 gen_field_die (tree decl, dw_die_ref context_die)
20039 {
20040   dw_die_ref decl_die;
20041
20042   if (TREE_TYPE (decl) == error_mark_node)
20043     return;
20044
20045   decl_die = new_die (DW_TAG_member, context_die, decl);
20046   add_name_and_src_coords_attributes (decl_die, decl);
20047   add_type_attribute (decl_die, member_declared_type (decl),
20048                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20049                       context_die);
20050
20051   if (DECL_BIT_FIELD_TYPE (decl))
20052     {
20053       add_byte_size_attribute (decl_die, decl);
20054       add_bit_size_attribute (decl_die, decl);
20055       add_bit_offset_attribute (decl_die, decl);
20056     }
20057
20058   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20059     add_data_member_location_attribute (decl_die, decl);
20060
20061   if (DECL_ARTIFICIAL (decl))
20062     add_AT_flag (decl_die, DW_AT_artificial, 1);
20063
20064   add_accessibility_attribute (decl_die, decl);
20065
20066   /* Equate decl number to die, so that we can look up this decl later on.  */
20067   equate_decl_number_to_die (decl, decl_die);
20068 }
20069
20070 #if 0
20071 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20072    Use modified_type_die instead.
20073    We keep this code here just in case these types of DIEs may be needed to
20074    represent certain things in other languages (e.g. Pascal) someday.  */
20075
20076 static void
20077 gen_pointer_type_die (tree type, dw_die_ref context_die)
20078 {
20079   dw_die_ref ptr_die
20080     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20081
20082   equate_type_number_to_die (type, ptr_die);
20083   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20084   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20085 }
20086
20087 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20088    Use modified_type_die instead.
20089    We keep this code here just in case these types of DIEs may be needed to
20090    represent certain things in other languages (e.g. Pascal) someday.  */
20091
20092 static void
20093 gen_reference_type_die (tree type, dw_die_ref context_die)
20094 {
20095   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20096
20097   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20098     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20099   else
20100     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20101
20102   equate_type_number_to_die (type, ref_die);
20103   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20104   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20105 }
20106 #endif
20107
20108 /* Generate a DIE for a pointer to a member type.  */
20109
20110 static void
20111 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20112 {
20113   dw_die_ref ptr_die
20114     = new_die (DW_TAG_ptr_to_member_type,
20115                scope_die_for (type, context_die), type);
20116
20117   equate_type_number_to_die (type, ptr_die);
20118   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20119                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20120   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20121 }
20122
20123 /* Generate the DIE for the compilation unit.  */
20124
20125 static dw_die_ref
20126 gen_compile_unit_die (const char *filename)
20127 {
20128   dw_die_ref die;
20129   char producer[250];
20130   const char *language_string = lang_hooks.name;
20131   int language;
20132
20133   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20134
20135   if (filename)
20136     {
20137       add_name_attribute (die, filename);
20138       /* Don't add cwd for <built-in>.  */
20139       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20140         add_comp_dir_attribute (die);
20141     }
20142
20143   sprintf (producer, "%s %s", language_string, version_string);
20144
20145 #ifdef MIPS_DEBUGGING_INFO
20146   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20147      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20148      not appear in the producer string, the debugger reaches the conclusion
20149      that the object file is stripped and has no debugging information.
20150      To get the MIPS/SGI debugger to believe that there is debugging
20151      information in the object file, we add a -g to the producer string.  */
20152   if (debug_info_level > DINFO_LEVEL_TERSE)
20153     strcat (producer, " -g");
20154 #endif
20155
20156   add_AT_string (die, DW_AT_producer, producer);
20157
20158   /* If our producer is LTO try to figure out a common language to use
20159      from the global list of translation units.  */
20160   if (strcmp (language_string, "GNU GIMPLE") == 0)
20161     {
20162       unsigned i;
20163       tree t;
20164       const char *common_lang = NULL;
20165
20166       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20167         {
20168           if (!TRANSLATION_UNIT_LANGUAGE (t))
20169             continue;
20170           if (!common_lang)
20171             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20172           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20173             ;
20174           else if (strncmp (common_lang, "GNU C", 5) == 0
20175                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20176             /* Mixing C and C++ is ok, use C++ in that case.  */
20177             common_lang = "GNU C++";
20178           else
20179             {
20180               /* Fall back to C.  */
20181               common_lang = NULL;
20182               break;
20183             }
20184         }
20185
20186       if (common_lang)
20187         language_string = common_lang;
20188     }
20189
20190   language = DW_LANG_C89;
20191   if (strcmp (language_string, "GNU C++") == 0)
20192     language = DW_LANG_C_plus_plus;
20193   else if (strcmp (language_string, "GNU F77") == 0)
20194     language = DW_LANG_Fortran77;
20195   else if (strcmp (language_string, "GNU Pascal") == 0)
20196     language = DW_LANG_Pascal83;
20197   else if (dwarf_version >= 3 || !dwarf_strict)
20198     {
20199       if (strcmp (language_string, "GNU Ada") == 0)
20200         language = DW_LANG_Ada95;
20201       else if (strcmp (language_string, "GNU Fortran") == 0)
20202         language = DW_LANG_Fortran95;
20203       else if (strcmp (language_string, "GNU Java") == 0)
20204         language = DW_LANG_Java;
20205       else if (strcmp (language_string, "GNU Objective-C") == 0)
20206         language = DW_LANG_ObjC;
20207       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20208         language = DW_LANG_ObjC_plus_plus;
20209     }
20210
20211   add_AT_unsigned (die, DW_AT_language, language);
20212
20213   switch (language)
20214     {
20215     case DW_LANG_Fortran77:
20216     case DW_LANG_Fortran90:
20217     case DW_LANG_Fortran95:
20218       /* Fortran has case insensitive identifiers and the front-end
20219          lowercases everything.  */
20220       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20221       break;
20222     default:
20223       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20224       break;
20225     }
20226   return die;
20227 }
20228
20229 /* Generate the DIE for a base class.  */
20230
20231 static void
20232 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20233 {
20234   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20235
20236   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20237   add_data_member_location_attribute (die, binfo);
20238
20239   if (BINFO_VIRTUAL_P (binfo))
20240     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20241
20242   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20243      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20244      the default has always been DW_ACCESS_private.  */
20245   if (access == access_public_node)
20246     {
20247       if (dwarf_version == 2
20248           || context_die->die_tag == DW_TAG_class_type)
20249       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20250     }
20251   else if (access == access_protected_node)
20252     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20253   else if (dwarf_version > 2
20254            && context_die->die_tag != DW_TAG_class_type)
20255     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20256 }
20257
20258 /* Generate a DIE for a class member.  */
20259
20260 static void
20261 gen_member_die (tree type, dw_die_ref context_die)
20262 {
20263   tree member;
20264   tree binfo = TYPE_BINFO (type);
20265   dw_die_ref child;
20266
20267   /* If this is not an incomplete type, output descriptions of each of its
20268      members. Note that as we output the DIEs necessary to represent the
20269      members of this record or union type, we will also be trying to output
20270      DIEs to represent the *types* of those members. However the `type'
20271      function (above) will specifically avoid generating type DIEs for member
20272      types *within* the list of member DIEs for this (containing) type except
20273      for those types (of members) which are explicitly marked as also being
20274      members of this (containing) type themselves.  The g++ front- end can
20275      force any given type to be treated as a member of some other (containing)
20276      type by setting the TYPE_CONTEXT of the given (member) type to point to
20277      the TREE node representing the appropriate (containing) type.  */
20278
20279   /* First output info about the base classes.  */
20280   if (binfo)
20281     {
20282       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20283       int i;
20284       tree base;
20285
20286       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20287         gen_inheritance_die (base,
20288                              (accesses ? VEC_index (tree, accesses, i)
20289                               : access_public_node), context_die);
20290     }
20291
20292   /* Now output info about the data members and type members.  */
20293   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20294     {
20295       /* If we thought we were generating minimal debug info for TYPE
20296          and then changed our minds, some of the member declarations
20297          may have already been defined.  Don't define them again, but
20298          do put them in the right order.  */
20299
20300       child = lookup_decl_die (member);
20301       if (child)
20302         splice_child_die (context_die, child);
20303       else
20304         gen_decl_die (member, NULL, context_die);
20305     }
20306
20307   /* Now output info about the function members (if any).  */
20308   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20309     {
20310       /* Don't include clones in the member list.  */
20311       if (DECL_ABSTRACT_ORIGIN (member))
20312         continue;
20313
20314       child = lookup_decl_die (member);
20315       if (child)
20316         splice_child_die (context_die, child);
20317       else
20318         gen_decl_die (member, NULL, context_die);
20319     }
20320 }
20321
20322 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20323    is set, we pretend that the type was never defined, so we only get the
20324    member DIEs needed by later specification DIEs.  */
20325
20326 static void
20327 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20328                                 enum debug_info_usage usage)
20329 {
20330   dw_die_ref type_die = lookup_type_die (type);
20331   dw_die_ref scope_die = 0;
20332   int nested = 0;
20333   int complete = (TYPE_SIZE (type)
20334                   && (! TYPE_STUB_DECL (type)
20335                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20336   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20337   complete = complete && should_emit_struct_debug (type, usage);
20338
20339   if (type_die && ! complete)
20340     return;
20341
20342   if (TYPE_CONTEXT (type) != NULL_TREE
20343       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20344           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20345     nested = 1;
20346
20347   scope_die = scope_die_for (type, context_die);
20348
20349   if (! type_die || (nested && is_cu_die (scope_die)))
20350     /* First occurrence of type or toplevel definition of nested class.  */
20351     {
20352       dw_die_ref old_die = type_die;
20353
20354       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20355                           ? record_type_tag (type) : DW_TAG_union_type,
20356                           scope_die, type);
20357       equate_type_number_to_die (type, type_die);
20358       if (old_die)
20359         add_AT_specification (type_die, old_die);
20360       else
20361         add_name_attribute (type_die, type_tag (type));
20362     }
20363   else
20364     remove_AT (type_die, DW_AT_declaration);
20365
20366   /* Generate child dies for template paramaters.  */
20367   if (debug_info_level > DINFO_LEVEL_TERSE
20368       && COMPLETE_TYPE_P (type))
20369     schedule_generic_params_dies_gen (type);
20370
20371   /* If this type has been completed, then give it a byte_size attribute and
20372      then give a list of members.  */
20373   if (complete && !ns_decl)
20374     {
20375       /* Prevent infinite recursion in cases where the type of some member of
20376          this type is expressed in terms of this type itself.  */
20377       TREE_ASM_WRITTEN (type) = 1;
20378       add_byte_size_attribute (type_die, type);
20379       if (TYPE_STUB_DECL (type) != NULL_TREE)
20380         {
20381           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20382           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20383         }
20384
20385       /* If the first reference to this type was as the return type of an
20386          inline function, then it may not have a parent.  Fix this now.  */
20387       if (type_die->die_parent == NULL)
20388         add_child_die (scope_die, type_die);
20389
20390       push_decl_scope (type);
20391       gen_member_die (type, type_die);
20392       pop_decl_scope ();
20393
20394       /* GNU extension: Record what type our vtable lives in.  */
20395       if (TYPE_VFIELD (type))
20396         {
20397           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20398
20399           gen_type_die (vtype, context_die);
20400           add_AT_die_ref (type_die, DW_AT_containing_type,
20401                           lookup_type_die (vtype));
20402         }
20403     }
20404   else
20405     {
20406       add_AT_flag (type_die, DW_AT_declaration, 1);
20407
20408       /* We don't need to do this for function-local types.  */
20409       if (TYPE_STUB_DECL (type)
20410           && ! decl_function_context (TYPE_STUB_DECL (type)))
20411         VEC_safe_push (tree, gc, incomplete_types, type);
20412     }
20413
20414   if (get_AT (type_die, DW_AT_name))
20415     add_pubtype (type, type_die);
20416 }
20417
20418 /* Generate a DIE for a subroutine _type_.  */
20419
20420 static void
20421 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20422 {
20423   tree return_type = TREE_TYPE (type);
20424   dw_die_ref subr_die
20425     = new_die (DW_TAG_subroutine_type,
20426                scope_die_for (type, context_die), type);
20427
20428   equate_type_number_to_die (type, subr_die);
20429   add_prototyped_attribute (subr_die, type);
20430   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20431   gen_formal_types_die (type, subr_die);
20432
20433   if (get_AT (subr_die, DW_AT_name))
20434     add_pubtype (type, subr_die);
20435 }
20436
20437 /* Generate a DIE for a type definition.  */
20438
20439 static void
20440 gen_typedef_die (tree decl, dw_die_ref context_die)
20441 {
20442   dw_die_ref type_die;
20443   tree origin;
20444
20445   if (TREE_ASM_WRITTEN (decl))
20446     return;
20447
20448   TREE_ASM_WRITTEN (decl) = 1;
20449   type_die = new_die (DW_TAG_typedef, context_die, decl);
20450   origin = decl_ultimate_origin (decl);
20451   if (origin != NULL)
20452     add_abstract_origin_attribute (type_die, origin);
20453   else
20454     {
20455       tree type;
20456
20457       add_name_and_src_coords_attributes (type_die, decl);
20458       if (DECL_ORIGINAL_TYPE (decl))
20459         {
20460           type = DECL_ORIGINAL_TYPE (decl);
20461
20462           gcc_assert (type != TREE_TYPE (decl));
20463           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20464         }
20465       else
20466         {
20467           type = TREE_TYPE (decl);
20468
20469           if (is_naming_typedef_decl (TYPE_NAME (type)))
20470             {
20471               /* Here, we are in the case of decl being a typedef naming
20472                  an anonymous type, e.g:
20473                      typedef struct {...} foo;
20474                  In that case TREE_TYPE (decl) is not a typedef variant
20475                  type and TYPE_NAME of the anonymous type is set to the
20476                  TYPE_DECL of the typedef. This construct is emitted by
20477                  the C++ FE.
20478
20479                  TYPE is the anonymous struct named by the typedef
20480                  DECL. As we need the DW_AT_type attribute of the
20481                  DW_TAG_typedef to point to the DIE of TYPE, let's
20482                  generate that DIE right away. add_type_attribute
20483                  called below will then pick (via lookup_type_die) that
20484                  anonymous struct DIE.  */
20485               if (!TREE_ASM_WRITTEN (type))
20486                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20487
20488               /* This is a GNU Extension.  We are adding a
20489                  DW_AT_linkage_name attribute to the DIE of the
20490                  anonymous struct TYPE.  The value of that attribute
20491                  is the name of the typedef decl naming the anonymous
20492                  struct.  This greatly eases the work of consumers of
20493                  this debug info.  */
20494               add_linkage_attr (lookup_type_die (type), decl);
20495             }
20496         }
20497
20498       add_type_attribute (type_die, type, TREE_READONLY (decl),
20499                           TREE_THIS_VOLATILE (decl), context_die);
20500
20501       if (is_naming_typedef_decl (decl))
20502         /* We want that all subsequent calls to lookup_type_die with
20503            TYPE in argument yield the DW_TAG_typedef we have just
20504            created.  */
20505         equate_type_number_to_die (type, type_die);
20506
20507       add_accessibility_attribute (type_die, decl);
20508     }
20509
20510   if (DECL_ABSTRACT (decl))
20511     equate_decl_number_to_die (decl, type_die);
20512
20513   if (get_AT (type_die, DW_AT_name))
20514     add_pubtype (decl, type_die);
20515 }
20516
20517 /* Generate a DIE for a struct, class, enum or union type.  */
20518
20519 static void
20520 gen_tagged_type_die (tree type,
20521                      dw_die_ref context_die,
20522                      enum debug_info_usage usage)
20523 {
20524   int need_pop;
20525
20526   if (type == NULL_TREE
20527       || !is_tagged_type (type))
20528     return;
20529
20530   /* If this is a nested type whose containing class hasn't been written
20531      out yet, writing it out will cover this one, too.  This does not apply
20532      to instantiations of member class templates; they need to be added to
20533      the containing class as they are generated.  FIXME: This hurts the
20534      idea of combining type decls from multiple TUs, since we can't predict
20535      what set of template instantiations we'll get.  */
20536   if (TYPE_CONTEXT (type)
20537       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20538       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20539     {
20540       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20541
20542       if (TREE_ASM_WRITTEN (type))
20543         return;
20544
20545       /* If that failed, attach ourselves to the stub.  */
20546       push_decl_scope (TYPE_CONTEXT (type));
20547       context_die = lookup_type_die (TYPE_CONTEXT (type));
20548       need_pop = 1;
20549     }
20550   else if (TYPE_CONTEXT (type) != NULL_TREE
20551            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20552     {
20553       /* If this type is local to a function that hasn't been written
20554          out yet, use a NULL context for now; it will be fixed up in
20555          decls_for_scope.  */
20556       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20557       /* A declaration DIE doesn't count; nested types need to go in the
20558          specification.  */
20559       if (context_die && is_declaration_die (context_die))
20560         context_die = NULL;
20561       need_pop = 0;
20562     }
20563   else
20564     {
20565       context_die = declare_in_namespace (type, context_die);
20566       need_pop = 0;
20567     }
20568
20569   if (TREE_CODE (type) == ENUMERAL_TYPE)
20570     {
20571       /* This might have been written out by the call to
20572          declare_in_namespace.  */
20573       if (!TREE_ASM_WRITTEN (type))
20574         gen_enumeration_type_die (type, context_die);
20575     }
20576   else
20577     gen_struct_or_union_type_die (type, context_die, usage);
20578
20579   if (need_pop)
20580     pop_decl_scope ();
20581
20582   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20583      it up if it is ever completed.  gen_*_type_die will set it for us
20584      when appropriate.  */
20585 }
20586
20587 /* Generate a type description DIE.  */
20588
20589 static void
20590 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20591                          enum debug_info_usage usage)
20592 {
20593   struct array_descr_info info;
20594
20595   if (type == NULL_TREE || type == error_mark_node)
20596     return;
20597
20598   if (TYPE_NAME (type) != NULL_TREE
20599       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20600       && is_redundant_typedef (TYPE_NAME (type))
20601       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20602     /* The DECL of this type is a typedef we don't want to emit debug
20603        info for but we want debug info for its underlying typedef.
20604        This can happen for e.g, the injected-class-name of a C++
20605        type.  */
20606     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20607
20608   /* If TYPE is a typedef type variant, let's generate debug info
20609      for the parent typedef which TYPE is a type of.  */
20610   if (typedef_variant_p (type))
20611     {
20612       if (TREE_ASM_WRITTEN (type))
20613         return;
20614
20615       /* Prevent broken recursion; we can't hand off to the same type.  */
20616       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20617
20618       /* Use the DIE of the containing namespace as the parent DIE of
20619          the type description DIE we want to generate.  */
20620       if (DECL_CONTEXT (TYPE_NAME (type))
20621           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20622         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20623
20624       TREE_ASM_WRITTEN (type) = 1;
20625
20626       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20627       return;
20628     }
20629
20630   /* If type is an anonymous tagged type named by a typedef, let's
20631      generate debug info for the typedef.  */
20632   if (is_naming_typedef_decl (TYPE_NAME (type)))
20633     {
20634       /* Use the DIE of the containing namespace as the parent DIE of
20635          the type description DIE we want to generate.  */
20636       if (DECL_CONTEXT (TYPE_NAME (type))
20637           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20638         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20639       
20640       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20641       return;
20642     }
20643
20644   /* If this is an array type with hidden descriptor, handle it first.  */
20645   if (!TREE_ASM_WRITTEN (type)
20646       && lang_hooks.types.get_array_descr_info
20647       && lang_hooks.types.get_array_descr_info (type, &info)
20648       && (dwarf_version >= 3 || !dwarf_strict))
20649     {
20650       gen_descr_array_type_die (type, &info, context_die);
20651       TREE_ASM_WRITTEN (type) = 1;
20652       return;
20653     }
20654
20655   /* We are going to output a DIE to represent the unqualified version
20656      of this type (i.e. without any const or volatile qualifiers) so
20657      get the main variant (i.e. the unqualified version) of this type
20658      now.  (Vectors are special because the debugging info is in the
20659      cloned type itself).  */
20660   if (TREE_CODE (type) != VECTOR_TYPE)
20661     type = type_main_variant (type);
20662
20663   if (TREE_ASM_WRITTEN (type))
20664     return;
20665
20666   switch (TREE_CODE (type))
20667     {
20668     case ERROR_MARK:
20669       break;
20670
20671     case POINTER_TYPE:
20672     case REFERENCE_TYPE:
20673       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20674          ensures that the gen_type_die recursion will terminate even if the
20675          type is recursive.  Recursive types are possible in Ada.  */
20676       /* ??? We could perhaps do this for all types before the switch
20677          statement.  */
20678       TREE_ASM_WRITTEN (type) = 1;
20679
20680       /* For these types, all that is required is that we output a DIE (or a
20681          set of DIEs) to represent the "basis" type.  */
20682       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20683                                 DINFO_USAGE_IND_USE);
20684       break;
20685
20686     case OFFSET_TYPE:
20687       /* This code is used for C++ pointer-to-data-member types.
20688          Output a description of the relevant class type.  */
20689       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20690                                         DINFO_USAGE_IND_USE);
20691
20692       /* Output a description of the type of the object pointed to.  */
20693       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20694                                         DINFO_USAGE_IND_USE);
20695
20696       /* Now output a DIE to represent this pointer-to-data-member type
20697          itself.  */
20698       gen_ptr_to_mbr_type_die (type, context_die);
20699       break;
20700
20701     case FUNCTION_TYPE:
20702       /* Force out return type (in case it wasn't forced out already).  */
20703       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20704                                         DINFO_USAGE_DIR_USE);
20705       gen_subroutine_type_die (type, context_die);
20706       break;
20707
20708     case METHOD_TYPE:
20709       /* Force out return type (in case it wasn't forced out already).  */
20710       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20711                                         DINFO_USAGE_DIR_USE);
20712       gen_subroutine_type_die (type, context_die);
20713       break;
20714
20715     case ARRAY_TYPE:
20716       gen_array_type_die (type, context_die);
20717       break;
20718
20719     case VECTOR_TYPE:
20720       gen_array_type_die (type, context_die);
20721       break;
20722
20723     case ENUMERAL_TYPE:
20724     case RECORD_TYPE:
20725     case UNION_TYPE:
20726     case QUAL_UNION_TYPE:
20727       gen_tagged_type_die (type, context_die, usage);
20728       return;
20729
20730     case VOID_TYPE:
20731     case INTEGER_TYPE:
20732     case REAL_TYPE:
20733     case FIXED_POINT_TYPE:
20734     case COMPLEX_TYPE:
20735     case BOOLEAN_TYPE:
20736       /* No DIEs needed for fundamental types.  */
20737       break;
20738
20739     case NULLPTR_TYPE:
20740     case LANG_TYPE:
20741       /* Just use DW_TAG_unspecified_type.  */
20742       {
20743         dw_die_ref type_die = lookup_type_die (type);
20744         if (type_die == NULL)
20745           {
20746             tree name = TYPE_NAME (type);
20747             if (TREE_CODE (name) == TYPE_DECL)
20748               name = DECL_NAME (name);
20749             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20750             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20751             equate_type_number_to_die (type, type_die);
20752           }
20753       }
20754       break;
20755
20756     default:
20757       gcc_unreachable ();
20758     }
20759
20760   TREE_ASM_WRITTEN (type) = 1;
20761 }
20762
20763 static void
20764 gen_type_die (tree type, dw_die_ref context_die)
20765 {
20766   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20767 }
20768
20769 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20770    things which are local to the given block.  */
20771
20772 static void
20773 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20774 {
20775   int must_output_die = 0;
20776   bool inlined_func;
20777
20778   /* Ignore blocks that are NULL.  */
20779   if (stmt == NULL_TREE)
20780     return;
20781
20782   inlined_func = inlined_function_outer_scope_p (stmt);
20783
20784   /* If the block is one fragment of a non-contiguous block, do not
20785      process the variables, since they will have been done by the
20786      origin block.  Do process subblocks.  */
20787   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20788     {
20789       tree sub;
20790
20791       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20792         gen_block_die (sub, context_die, depth + 1);
20793
20794       return;
20795     }
20796
20797   /* Determine if we need to output any Dwarf DIEs at all to represent this
20798      block.  */
20799   if (inlined_func)
20800     /* The outer scopes for inlinings *must* always be represented.  We
20801        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20802     must_output_die = 1;
20803   else
20804     {
20805       /* Determine if this block directly contains any "significant"
20806          local declarations which we will need to output DIEs for.  */
20807       if (debug_info_level > DINFO_LEVEL_TERSE)
20808         /* We are not in terse mode so *any* local declaration counts
20809            as being a "significant" one.  */
20810         must_output_die = ((BLOCK_VARS (stmt) != NULL
20811                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20812                            && (TREE_USED (stmt)
20813                                || TREE_ASM_WRITTEN (stmt)
20814                                || BLOCK_ABSTRACT (stmt)));
20815       else if ((TREE_USED (stmt)
20816                 || TREE_ASM_WRITTEN (stmt)
20817                 || BLOCK_ABSTRACT (stmt))
20818                && !dwarf2out_ignore_block (stmt))
20819         must_output_die = 1;
20820     }
20821
20822   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20823      DIE for any block which contains no significant local declarations at
20824      all.  Rather, in such cases we just call `decls_for_scope' so that any
20825      needed Dwarf info for any sub-blocks will get properly generated. Note
20826      that in terse mode, our definition of what constitutes a "significant"
20827      local declaration gets restricted to include only inlined function
20828      instances and local (nested) function definitions.  */
20829   if (must_output_die)
20830     {
20831       if (inlined_func)
20832         {
20833           /* If STMT block is abstract, that means we have been called
20834              indirectly from dwarf2out_abstract_function.
20835              That function rightfully marks the descendent blocks (of
20836              the abstract function it is dealing with) as being abstract,
20837              precisely to prevent us from emitting any
20838              DW_TAG_inlined_subroutine DIE as a descendent
20839              of an abstract function instance. So in that case, we should
20840              not call gen_inlined_subroutine_die.
20841
20842              Later though, when cgraph asks dwarf2out to emit info
20843              for the concrete instance of the function decl into which
20844              the concrete instance of STMT got inlined, the later will lead
20845              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20846           if (! BLOCK_ABSTRACT (stmt))
20847             gen_inlined_subroutine_die (stmt, context_die, depth);
20848         }
20849       else
20850         gen_lexical_block_die (stmt, context_die, depth);
20851     }
20852   else
20853     decls_for_scope (stmt, context_die, depth);
20854 }
20855
20856 /* Process variable DECL (or variable with origin ORIGIN) within
20857    block STMT and add it to CONTEXT_DIE.  */
20858 static void
20859 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20860 {
20861   dw_die_ref die;
20862   tree decl_or_origin = decl ? decl : origin;
20863
20864   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20865     die = lookup_decl_die (decl_or_origin);
20866   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20867            && TYPE_DECL_IS_STUB (decl_or_origin))
20868     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20869   else
20870     die = NULL;
20871
20872   if (die != NULL && die->die_parent == NULL)
20873     add_child_die (context_die, die);
20874   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20875     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20876                                          stmt, context_die);
20877   else
20878     gen_decl_die (decl, origin, context_die);
20879 }
20880
20881 /* Generate all of the decls declared within a given scope and (recursively)
20882    all of its sub-blocks.  */
20883
20884 static void
20885 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20886 {
20887   tree decl;
20888   unsigned int i;
20889   tree subblocks;
20890
20891   /* Ignore NULL blocks.  */
20892   if (stmt == NULL_TREE)
20893     return;
20894
20895   /* Output the DIEs to represent all of the data objects and typedefs
20896      declared directly within this block but not within any nested
20897      sub-blocks.  Also, nested function and tag DIEs have been
20898      generated with a parent of NULL; fix that up now.  */
20899   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20900     process_scope_var (stmt, decl, NULL_TREE, context_die);
20901   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20902     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20903                        context_die);
20904
20905   /* If we're at -g1, we're not interested in subblocks.  */
20906   if (debug_info_level <= DINFO_LEVEL_TERSE)
20907     return;
20908
20909   /* Output the DIEs to represent all sub-blocks (and the items declared
20910      therein) of this block.  */
20911   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20912        subblocks != NULL;
20913        subblocks = BLOCK_CHAIN (subblocks))
20914     gen_block_die (subblocks, context_die, depth + 1);
20915 }
20916
20917 /* Is this a typedef we can avoid emitting?  */
20918
20919 static inline int
20920 is_redundant_typedef (const_tree decl)
20921 {
20922   if (TYPE_DECL_IS_STUB (decl))
20923     return 1;
20924
20925   if (DECL_ARTIFICIAL (decl)
20926       && DECL_CONTEXT (decl)
20927       && is_tagged_type (DECL_CONTEXT (decl))
20928       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20929       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20930     /* Also ignore the artificial member typedef for the class name.  */
20931     return 1;
20932
20933   return 0;
20934 }
20935
20936 /* Return TRUE if TYPE is a typedef that names a type for linkage
20937    purposes. This kind of typedefs is produced by the C++ FE for
20938    constructs like:
20939
20940    typedef struct {...} foo;
20941
20942    In that case, there is no typedef variant type produced for foo.
20943    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20944    struct type.  */
20945
20946 static bool
20947 is_naming_typedef_decl (const_tree decl)
20948 {
20949   if (decl == NULL_TREE
20950       || TREE_CODE (decl) != TYPE_DECL
20951       || !is_tagged_type (TREE_TYPE (decl))
20952       || DECL_IS_BUILTIN (decl)
20953       || is_redundant_typedef (decl)
20954       /* It looks like Ada produces TYPE_DECLs that are very similar
20955          to C++ naming typedefs but that have different
20956          semantics. Let's be specific to c++ for now.  */
20957       || !is_cxx ())
20958     return FALSE;
20959
20960   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20961           && TYPE_NAME (TREE_TYPE (decl)) == decl
20962           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20963               != TYPE_NAME (TREE_TYPE (decl))));
20964 }
20965
20966 /* Returns the DIE for a context.  */
20967
20968 static inline dw_die_ref
20969 get_context_die (tree context)
20970 {
20971   if (context)
20972     {
20973       /* Find die that represents this context.  */
20974       if (TYPE_P (context))
20975         {
20976           context = TYPE_MAIN_VARIANT (context);
20977           return strip_naming_typedef (context, force_type_die (context));
20978         }
20979       else
20980         return force_decl_die (context);
20981     }
20982   return comp_unit_die ();
20983 }
20984
20985 /* Returns the DIE for decl.  A DIE will always be returned.  */
20986
20987 static dw_die_ref
20988 force_decl_die (tree decl)
20989 {
20990   dw_die_ref decl_die;
20991   unsigned saved_external_flag;
20992   tree save_fn = NULL_TREE;
20993   decl_die = lookup_decl_die (decl);
20994   if (!decl_die)
20995     {
20996       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20997
20998       decl_die = lookup_decl_die (decl);
20999       if (decl_die)
21000         return decl_die;
21001
21002       switch (TREE_CODE (decl))
21003         {
21004         case FUNCTION_DECL:
21005           /* Clear current_function_decl, so that gen_subprogram_die thinks
21006              that this is a declaration. At this point, we just want to force
21007              declaration die.  */
21008           save_fn = current_function_decl;
21009           current_function_decl = NULL_TREE;
21010           gen_subprogram_die (decl, context_die);
21011           current_function_decl = save_fn;
21012           break;
21013
21014         case VAR_DECL:
21015           /* Set external flag to force declaration die. Restore it after
21016            gen_decl_die() call.  */
21017           saved_external_flag = DECL_EXTERNAL (decl);
21018           DECL_EXTERNAL (decl) = 1;
21019           gen_decl_die (decl, NULL, context_die);
21020           DECL_EXTERNAL (decl) = saved_external_flag;
21021           break;
21022
21023         case NAMESPACE_DECL:
21024           if (dwarf_version >= 3 || !dwarf_strict)
21025             dwarf2out_decl (decl);
21026           else
21027             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21028             decl_die = comp_unit_die ();
21029           break;
21030
21031         case TRANSLATION_UNIT_DECL:
21032           decl_die = comp_unit_die ();
21033           break;
21034
21035         default:
21036           gcc_unreachable ();
21037         }
21038
21039       /* We should be able to find the DIE now.  */
21040       if (!decl_die)
21041         decl_die = lookup_decl_die (decl);
21042       gcc_assert (decl_die);
21043     }
21044
21045   return decl_die;
21046 }
21047
21048 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21049    always returned.  */
21050
21051 static dw_die_ref
21052 force_type_die (tree type)
21053 {
21054   dw_die_ref type_die;
21055
21056   type_die = lookup_type_die (type);
21057   if (!type_die)
21058     {
21059       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21060
21061       type_die = modified_type_die (type, TYPE_READONLY (type),
21062                                     TYPE_VOLATILE (type), context_die);
21063       gcc_assert (type_die);
21064     }
21065   return type_die;
21066 }
21067
21068 /* Force out any required namespaces to be able to output DECL,
21069    and return the new context_die for it, if it's changed.  */
21070
21071 static dw_die_ref
21072 setup_namespace_context (tree thing, dw_die_ref context_die)
21073 {
21074   tree context = (DECL_P (thing)
21075                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21076   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21077     /* Force out the namespace.  */
21078     context_die = force_decl_die (context);
21079
21080   return context_die;
21081 }
21082
21083 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21084    type) within its namespace, if appropriate.
21085
21086    For compatibility with older debuggers, namespace DIEs only contain
21087    declarations; all definitions are emitted at CU scope.  */
21088
21089 static dw_die_ref
21090 declare_in_namespace (tree thing, dw_die_ref context_die)
21091 {
21092   dw_die_ref ns_context;
21093
21094   if (debug_info_level <= DINFO_LEVEL_TERSE)
21095     return context_die;
21096
21097   /* If this decl is from an inlined function, then don't try to emit it in its
21098      namespace, as we will get confused.  It would have already been emitted
21099      when the abstract instance of the inline function was emitted anyways.  */
21100   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21101     return context_die;
21102
21103   ns_context = setup_namespace_context (thing, context_die);
21104
21105   if (ns_context != context_die)
21106     {
21107       if (is_fortran ())
21108         return ns_context;
21109       if (DECL_P (thing))
21110         gen_decl_die (thing, NULL, ns_context);
21111       else
21112         gen_type_die (thing, ns_context);
21113     }
21114   return context_die;
21115 }
21116
21117 /* Generate a DIE for a namespace or namespace alias.  */
21118
21119 static void
21120 gen_namespace_die (tree decl, dw_die_ref context_die)
21121 {
21122   dw_die_ref namespace_die;
21123
21124   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21125      they are an alias of.  */
21126   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21127     {
21128       /* Output a real namespace or module.  */
21129       context_die = setup_namespace_context (decl, comp_unit_die ());
21130       namespace_die = new_die (is_fortran ()
21131                                ? DW_TAG_module : DW_TAG_namespace,
21132                                context_die, decl);
21133       /* For Fortran modules defined in different CU don't add src coords.  */
21134       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21135         {
21136           const char *name = dwarf2_name (decl, 0);
21137           if (name)
21138             add_name_attribute (namespace_die, name);
21139         }
21140       else
21141         add_name_and_src_coords_attributes (namespace_die, decl);
21142       if (DECL_EXTERNAL (decl))
21143         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21144       equate_decl_number_to_die (decl, namespace_die);
21145     }
21146   else
21147     {
21148       /* Output a namespace alias.  */
21149
21150       /* Force out the namespace we are an alias of, if necessary.  */
21151       dw_die_ref origin_die
21152         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21153
21154       if (DECL_FILE_SCOPE_P (decl)
21155           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21156         context_die = setup_namespace_context (decl, comp_unit_die ());
21157       /* Now create the namespace alias DIE.  */
21158       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21159       add_name_and_src_coords_attributes (namespace_die, decl);
21160       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21161       equate_decl_number_to_die (decl, namespace_die);
21162     }
21163 }
21164
21165 /* Generate Dwarf debug information for a decl described by DECL.
21166    The return value is currently only meaningful for PARM_DECLs,
21167    for all other decls it returns NULL.  */
21168
21169 static dw_die_ref
21170 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21171 {
21172   tree decl_or_origin = decl ? decl : origin;
21173   tree class_origin = NULL, ultimate_origin;
21174
21175   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21176     return NULL;
21177
21178   switch (TREE_CODE (decl_or_origin))
21179     {
21180     case ERROR_MARK:
21181       break;
21182
21183     case CONST_DECL:
21184       if (!is_fortran () && !is_ada ())
21185         {
21186           /* The individual enumerators of an enum type get output when we output
21187              the Dwarf representation of the relevant enum type itself.  */
21188           break;
21189         }
21190
21191       /* Emit its type.  */
21192       gen_type_die (TREE_TYPE (decl), context_die);
21193
21194       /* And its containing namespace.  */
21195       context_die = declare_in_namespace (decl, context_die);
21196
21197       gen_const_die (decl, context_die);
21198       break;
21199
21200     case FUNCTION_DECL:
21201       /* Don't output any DIEs to represent mere function declarations,
21202          unless they are class members or explicit block externs.  */
21203       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21204           && DECL_FILE_SCOPE_P (decl_or_origin)
21205           && (current_function_decl == NULL_TREE
21206               || DECL_ARTIFICIAL (decl_or_origin)))
21207         break;
21208
21209 #if 0
21210       /* FIXME */
21211       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21212          on local redeclarations of global functions.  That seems broken.  */
21213       if (current_function_decl != decl)
21214         /* This is only a declaration.  */;
21215 #endif
21216
21217       /* If we're emitting a clone, emit info for the abstract instance.  */
21218       if (origin || DECL_ORIGIN (decl) != decl)
21219         dwarf2out_abstract_function (origin
21220                                      ? DECL_ORIGIN (origin)
21221                                      : DECL_ABSTRACT_ORIGIN (decl));
21222
21223       /* If we're emitting an out-of-line copy of an inline function,
21224          emit info for the abstract instance and set up to refer to it.  */
21225       else if (cgraph_function_possibly_inlined_p (decl)
21226                && ! DECL_ABSTRACT (decl)
21227                && ! class_or_namespace_scope_p (context_die)
21228                /* dwarf2out_abstract_function won't emit a die if this is just
21229                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21230                   that case, because that works only if we have a die.  */
21231                && DECL_INITIAL (decl) != NULL_TREE)
21232         {
21233           dwarf2out_abstract_function (decl);
21234           set_decl_origin_self (decl);
21235         }
21236
21237       /* Otherwise we're emitting the primary DIE for this decl.  */
21238       else if (debug_info_level > DINFO_LEVEL_TERSE)
21239         {
21240           /* Before we describe the FUNCTION_DECL itself, make sure that we
21241              have its containing type.  */
21242           if (!origin)
21243             origin = decl_class_context (decl);
21244           if (origin != NULL_TREE)
21245             gen_type_die (origin, context_die);
21246
21247           /* And its return type.  */
21248           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21249
21250           /* And its virtual context.  */
21251           if (DECL_VINDEX (decl) != NULL_TREE)
21252             gen_type_die (DECL_CONTEXT (decl), context_die);
21253
21254           /* Make sure we have a member DIE for decl.  */
21255           if (origin != NULL_TREE)
21256             gen_type_die_for_member (origin, decl, context_die);
21257
21258           /* And its containing namespace.  */
21259           context_die = declare_in_namespace (decl, context_die);
21260         }
21261
21262       /* Now output a DIE to represent the function itself.  */
21263       if (decl)
21264         gen_subprogram_die (decl, context_die);
21265       break;
21266
21267     case TYPE_DECL:
21268       /* If we are in terse mode, don't generate any DIEs to represent any
21269          actual typedefs.  */
21270       if (debug_info_level <= DINFO_LEVEL_TERSE)
21271         break;
21272
21273       /* In the special case of a TYPE_DECL node representing the declaration
21274          of some type tag, if the given TYPE_DECL is marked as having been
21275          instantiated from some other (original) TYPE_DECL node (e.g. one which
21276          was generated within the original definition of an inline function) we
21277          used to generate a special (abbreviated) DW_TAG_structure_type,
21278          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21279          should be actually referencing those DIEs, as variable DIEs with that
21280          type would be emitted already in the abstract origin, so it was always
21281          removed during unused type prunning.  Don't add anything in this
21282          case.  */
21283       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21284         break;
21285
21286       if (is_redundant_typedef (decl))
21287         gen_type_die (TREE_TYPE (decl), context_die);
21288       else
21289         /* Output a DIE to represent the typedef itself.  */
21290         gen_typedef_die (decl, context_die);
21291       break;
21292
21293     case LABEL_DECL:
21294       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21295         gen_label_die (decl, context_die);
21296       break;
21297
21298     case VAR_DECL:
21299     case RESULT_DECL:
21300       /* If we are in terse mode, don't generate any DIEs to represent any
21301          variable declarations or definitions.  */
21302       if (debug_info_level <= DINFO_LEVEL_TERSE)
21303         break;
21304
21305       /* Output any DIEs that are needed to specify the type of this data
21306          object.  */
21307       if (decl_by_reference_p (decl_or_origin))
21308         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21309       else
21310         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21311
21312       /* And its containing type.  */
21313       class_origin = decl_class_context (decl_or_origin);
21314       if (class_origin != NULL_TREE)
21315         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21316
21317       /* And its containing namespace.  */
21318       context_die = declare_in_namespace (decl_or_origin, context_die);
21319
21320       /* Now output the DIE to represent the data object itself.  This gets
21321          complicated because of the possibility that the VAR_DECL really
21322          represents an inlined instance of a formal parameter for an inline
21323          function.  */
21324       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21325       if (ultimate_origin != NULL_TREE
21326           && TREE_CODE (ultimate_origin) == PARM_DECL)
21327         gen_formal_parameter_die (decl, origin,
21328                                   true /* Emit name attribute.  */,
21329                                   context_die);
21330       else
21331         gen_variable_die (decl, origin, context_die);
21332       break;
21333
21334     case FIELD_DECL:
21335       /* Ignore the nameless fields that are used to skip bits but handle C++
21336          anonymous unions and structs.  */
21337       if (DECL_NAME (decl) != NULL_TREE
21338           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21339           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21340         {
21341           gen_type_die (member_declared_type (decl), context_die);
21342           gen_field_die (decl, context_die);
21343         }
21344       break;
21345
21346     case PARM_DECL:
21347       if (DECL_BY_REFERENCE (decl_or_origin))
21348         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21349       else
21350         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21351       return gen_formal_parameter_die (decl, origin,
21352                                        true /* Emit name attribute.  */,
21353                                        context_die);
21354
21355     case NAMESPACE_DECL:
21356     case IMPORTED_DECL:
21357       if (dwarf_version >= 3 || !dwarf_strict)
21358         gen_namespace_die (decl, context_die);
21359       break;
21360
21361     default:
21362       /* Probably some frontend-internal decl.  Assume we don't care.  */
21363       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21364       break;
21365     }
21366
21367   return NULL;
21368 }
21369 \f
21370 /* Output debug information for global decl DECL.  Called from toplev.c after
21371    compilation proper has finished.  */
21372
21373 static void
21374 dwarf2out_global_decl (tree decl)
21375 {
21376   /* Output DWARF2 information for file-scope tentative data object
21377      declarations, file-scope (extern) function declarations (which
21378      had no corresponding body) and file-scope tagged type declarations
21379      and definitions which have not yet been forced out.  */
21380   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21381     dwarf2out_decl (decl);
21382 }
21383
21384 /* Output debug information for type decl DECL.  Called from toplev.c
21385    and from language front ends (to record built-in types).  */
21386 static void
21387 dwarf2out_type_decl (tree decl, int local)
21388 {
21389   if (!local)
21390     dwarf2out_decl (decl);
21391 }
21392
21393 /* Output debug information for imported module or decl DECL.
21394    NAME is non-NULL name in the lexical block if the decl has been renamed.
21395    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21396    that DECL belongs to.
21397    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21398 static void
21399 dwarf2out_imported_module_or_decl_1 (tree decl,
21400                                      tree name,
21401                                      tree lexical_block,
21402                                      dw_die_ref lexical_block_die)
21403 {
21404   expanded_location xloc;
21405   dw_die_ref imported_die = NULL;
21406   dw_die_ref at_import_die;
21407
21408   if (TREE_CODE (decl) == IMPORTED_DECL)
21409     {
21410       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21411       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21412       gcc_assert (decl);
21413     }
21414   else
21415     xloc = expand_location (input_location);
21416
21417   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21418     {
21419       at_import_die = force_type_die (TREE_TYPE (decl));
21420       /* For namespace N { typedef void T; } using N::T; base_type_die
21421          returns NULL, but DW_TAG_imported_declaration requires
21422          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21423       if (!at_import_die)
21424         {
21425           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21426           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21427           at_import_die = lookup_type_die (TREE_TYPE (decl));
21428           gcc_assert (at_import_die);
21429         }
21430     }
21431   else
21432     {
21433       at_import_die = lookup_decl_die (decl);
21434       if (!at_import_die)
21435         {
21436           /* If we're trying to avoid duplicate debug info, we may not have
21437              emitted the member decl for this field.  Emit it now.  */
21438           if (TREE_CODE (decl) == FIELD_DECL)
21439             {
21440               tree type = DECL_CONTEXT (decl);
21441
21442               if (TYPE_CONTEXT (type)
21443                   && TYPE_P (TYPE_CONTEXT (type))
21444                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21445                                                 DINFO_USAGE_DIR_USE))
21446                 return;
21447               gen_type_die_for_member (type, decl,
21448                                        get_context_die (TYPE_CONTEXT (type)));
21449             }
21450           at_import_die = force_decl_die (decl);
21451         }
21452     }
21453
21454   if (TREE_CODE (decl) == NAMESPACE_DECL)
21455     {
21456       if (dwarf_version >= 3 || !dwarf_strict)
21457         imported_die = new_die (DW_TAG_imported_module,
21458                                 lexical_block_die,
21459                                 lexical_block);
21460       else
21461         return;
21462     }
21463   else
21464     imported_die = new_die (DW_TAG_imported_declaration,
21465                             lexical_block_die,
21466                             lexical_block);
21467
21468   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21469   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21470   if (name)
21471     add_AT_string (imported_die, DW_AT_name,
21472                    IDENTIFIER_POINTER (name));
21473   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21474 }
21475
21476 /* Output debug information for imported module or decl DECL.
21477    NAME is non-NULL name in context if the decl has been renamed.
21478    CHILD is true if decl is one of the renamed decls as part of
21479    importing whole module.  */
21480
21481 static void
21482 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21483                                    bool child)
21484 {
21485   /* dw_die_ref at_import_die;  */
21486   dw_die_ref scope_die;
21487
21488   if (debug_info_level <= DINFO_LEVEL_TERSE)
21489     return;
21490
21491   gcc_assert (decl);
21492
21493   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21494      We need decl DIE for reference and scope die. First, get DIE for the decl
21495      itself.  */
21496
21497   /* Get the scope die for decl context. Use comp_unit_die for global module
21498      or decl. If die is not found for non globals, force new die.  */
21499   if (context
21500       && TYPE_P (context)
21501       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21502     return;
21503
21504   if (!(dwarf_version >= 3 || !dwarf_strict))
21505     return;
21506
21507   scope_die = get_context_die (context);
21508
21509   if (child)
21510     {
21511       gcc_assert (scope_die->die_child);
21512       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21513       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21514       scope_die = scope_die->die_child;
21515     }
21516
21517   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21518   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21519
21520 }
21521
21522 /* Write the debugging output for DECL.  */
21523
21524 void
21525 dwarf2out_decl (tree decl)
21526 {
21527   dw_die_ref context_die = comp_unit_die ();
21528
21529   switch (TREE_CODE (decl))
21530     {
21531     case ERROR_MARK:
21532       return;
21533
21534     case FUNCTION_DECL:
21535       /* What we would really like to do here is to filter out all mere
21536          file-scope declarations of file-scope functions which are never
21537          referenced later within this translation unit (and keep all of ones
21538          that *are* referenced later on) but we aren't clairvoyant, so we have
21539          no idea which functions will be referenced in the future (i.e. later
21540          on within the current translation unit). So here we just ignore all
21541          file-scope function declarations which are not also definitions.  If
21542          and when the debugger needs to know something about these functions,
21543          it will have to hunt around and find the DWARF information associated
21544          with the definition of the function.
21545
21546          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21547          nodes represent definitions and which ones represent mere
21548          declarations.  We have to check DECL_INITIAL instead. That's because
21549          the C front-end supports some weird semantics for "extern inline"
21550          function definitions.  These can get inlined within the current
21551          translation unit (and thus, we need to generate Dwarf info for their
21552          abstract instances so that the Dwarf info for the concrete inlined
21553          instances can have something to refer to) but the compiler never
21554          generates any out-of-lines instances of such things (despite the fact
21555          that they *are* definitions).
21556
21557          The important point is that the C front-end marks these "extern
21558          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21559          them anyway. Note that the C++ front-end also plays some similar games
21560          for inline function definitions appearing within include files which
21561          also contain `#pragma interface' pragmas.  */
21562       if (DECL_INITIAL (decl) == NULL_TREE)
21563         return;
21564
21565       /* If we're a nested function, initially use a parent of NULL; if we're
21566          a plain function, this will be fixed up in decls_for_scope.  If
21567          we're a method, it will be ignored, since we already have a DIE.  */
21568       if (decl_function_context (decl)
21569           /* But if we're in terse mode, we don't care about scope.  */
21570           && debug_info_level > DINFO_LEVEL_TERSE)
21571         context_die = NULL;
21572       break;
21573
21574     case VAR_DECL:
21575       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21576          declaration and if the declaration was never even referenced from
21577          within this entire compilation unit.  We suppress these DIEs in
21578          order to save space in the .debug section (by eliminating entries
21579          which are probably useless).  Note that we must not suppress
21580          block-local extern declarations (whether used or not) because that
21581          would screw-up the debugger's name lookup mechanism and cause it to
21582          miss things which really ought to be in scope at a given point.  */
21583       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21584         return;
21585
21586       /* For local statics lookup proper context die.  */
21587       if (TREE_STATIC (decl) && decl_function_context (decl))
21588         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21589
21590       /* If we are in terse mode, don't generate any DIEs to represent any
21591          variable declarations or definitions.  */
21592       if (debug_info_level <= DINFO_LEVEL_TERSE)
21593         return;
21594       break;
21595
21596     case CONST_DECL:
21597       if (debug_info_level <= DINFO_LEVEL_TERSE)
21598         return;
21599       if (!is_fortran () && !is_ada ())
21600         return;
21601       if (TREE_STATIC (decl) && decl_function_context (decl))
21602         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21603       break;
21604
21605     case NAMESPACE_DECL:
21606     case IMPORTED_DECL:
21607       if (debug_info_level <= DINFO_LEVEL_TERSE)
21608         return;
21609       if (lookup_decl_die (decl) != NULL)
21610         return;
21611       break;
21612
21613     case TYPE_DECL:
21614       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21615       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21616         return;
21617
21618       /* Don't bother trying to generate any DIEs to represent any of the
21619          normal built-in types for the language we are compiling.  */
21620       if (DECL_IS_BUILTIN (decl))
21621         return;
21622
21623       /* If we are in terse mode, don't generate any DIEs for types.  */
21624       if (debug_info_level <= DINFO_LEVEL_TERSE)
21625         return;
21626
21627       /* If we're a function-scope tag, initially use a parent of NULL;
21628          this will be fixed up in decls_for_scope.  */
21629       if (decl_function_context (decl))
21630         context_die = NULL;
21631
21632       break;
21633
21634     default:
21635       return;
21636     }
21637
21638   gen_decl_die (decl, NULL, context_die);
21639 }
21640
21641 /* Write the debugging output for DECL.  */
21642
21643 static void
21644 dwarf2out_function_decl (tree decl)
21645 {
21646   dwarf2out_decl (decl);
21647   call_arg_locations = NULL;
21648   call_arg_loc_last = NULL;
21649   call_site_count = -1;
21650   tail_call_site_count = -1;
21651   VEC_free (dw_die_ref, heap, block_map);
21652   htab_empty (decl_loc_table);
21653 }
21654
21655 /* Output a marker (i.e. a label) for the beginning of the generated code for
21656    a lexical block.  */
21657
21658 static void
21659 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21660                        unsigned int blocknum)
21661 {
21662   switch_to_section (current_function_section ());
21663   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21664 }
21665
21666 /* Output a marker (i.e. a label) for the end of the generated code for a
21667    lexical block.  */
21668
21669 static void
21670 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21671 {
21672   switch_to_section (current_function_section ());
21673   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21674 }
21675
21676 /* Returns nonzero if it is appropriate not to emit any debugging
21677    information for BLOCK, because it doesn't contain any instructions.
21678
21679    Don't allow this for blocks with nested functions or local classes
21680    as we would end up with orphans, and in the presence of scheduling
21681    we may end up calling them anyway.  */
21682
21683 static bool
21684 dwarf2out_ignore_block (const_tree block)
21685 {
21686   tree decl;
21687   unsigned int i;
21688
21689   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21690     if (TREE_CODE (decl) == FUNCTION_DECL
21691         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21692       return 0;
21693   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21694     {
21695       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21696       if (TREE_CODE (decl) == FUNCTION_DECL
21697           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21698       return 0;
21699     }
21700
21701   return 1;
21702 }
21703
21704 /* Hash table routines for file_hash.  */
21705
21706 static int
21707 file_table_eq (const void *p1_p, const void *p2_p)
21708 {
21709   const struct dwarf_file_data *const p1 =
21710     (const struct dwarf_file_data *) p1_p;
21711   const char *const p2 = (const char *) p2_p;
21712   return filename_cmp (p1->filename, p2) == 0;
21713 }
21714
21715 static hashval_t
21716 file_table_hash (const void *p_p)
21717 {
21718   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21719   return htab_hash_string (p->filename);
21720 }
21721
21722 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21723    dwarf2out.c) and return its "index".  The index of each (known) filename is
21724    just a unique number which is associated with only that one filename.  We
21725    need such numbers for the sake of generating labels (in the .debug_sfnames
21726    section) and references to those files numbers (in the .debug_srcinfo
21727    and.debug_macinfo sections).  If the filename given as an argument is not
21728    found in our current list, add it to the list and assign it the next
21729    available unique index number.  In order to speed up searches, we remember
21730    the index of the filename was looked up last.  This handles the majority of
21731    all searches.  */
21732
21733 static struct dwarf_file_data *
21734 lookup_filename (const char *file_name)
21735 {
21736   void ** slot;
21737   struct dwarf_file_data * created;
21738
21739   /* Check to see if the file name that was searched on the previous
21740      call matches this file name.  If so, return the index.  */
21741   if (file_table_last_lookup
21742       && (file_name == file_table_last_lookup->filename
21743           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21744     return file_table_last_lookup;
21745
21746   /* Didn't match the previous lookup, search the table.  */
21747   slot = htab_find_slot_with_hash (file_table, file_name,
21748                                    htab_hash_string (file_name), INSERT);
21749   if (*slot)
21750     return (struct dwarf_file_data *) *slot;
21751
21752   created = ggc_alloc_dwarf_file_data ();
21753   created->filename = file_name;
21754   created->emitted_number = 0;
21755   *slot = created;
21756   return created;
21757 }
21758
21759 /* If the assembler will construct the file table, then translate the compiler
21760    internal file table number into the assembler file table number, and emit
21761    a .file directive if we haven't already emitted one yet.  The file table
21762    numbers are different because we prune debug info for unused variables and
21763    types, which may include filenames.  */
21764
21765 static int
21766 maybe_emit_file (struct dwarf_file_data * fd)
21767 {
21768   if (! fd->emitted_number)
21769     {
21770       if (last_emitted_file)
21771         fd->emitted_number = last_emitted_file->emitted_number + 1;
21772       else
21773         fd->emitted_number = 1;
21774       last_emitted_file = fd;
21775
21776       if (DWARF2_ASM_LINE_DEBUG_INFO)
21777         {
21778           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21779           output_quoted_string (asm_out_file,
21780                                 remap_debug_filename (fd->filename));
21781           fputc ('\n', asm_out_file);
21782         }
21783     }
21784
21785   return fd->emitted_number;
21786 }
21787
21788 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21789    That generation should happen after function debug info has been
21790    generated. The value of the attribute is the constant value of ARG.  */
21791
21792 static void
21793 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21794 {
21795   die_arg_entry entry;
21796
21797   if (!die || !arg)
21798     return;
21799
21800   if (!tmpl_value_parm_die_table)
21801     tmpl_value_parm_die_table
21802       = VEC_alloc (die_arg_entry, gc, 32);
21803
21804   entry.die = die;
21805   entry.arg = arg;
21806   VEC_safe_push (die_arg_entry, gc,
21807                  tmpl_value_parm_die_table,
21808                  &entry);
21809 }
21810
21811 /* Return TRUE if T is an instance of generic type, FALSE
21812    otherwise.  */
21813
21814 static bool
21815 generic_type_p (tree t)
21816 {
21817   if (t == NULL_TREE || !TYPE_P (t))
21818     return false;
21819   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21820 }
21821
21822 /* Schedule the generation of the generic parameter dies for the
21823   instance of generic type T. The proper generation itself is later
21824   done by gen_scheduled_generic_parms_dies. */
21825
21826 static void
21827 schedule_generic_params_dies_gen (tree t)
21828 {
21829   if (!generic_type_p (t))
21830     return;
21831
21832   if (generic_type_instances == NULL)
21833     generic_type_instances = VEC_alloc (tree, gc, 256);
21834
21835   VEC_safe_push (tree, gc, generic_type_instances, t);
21836 }
21837
21838 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21839    by append_entry_to_tmpl_value_parm_die_table. This function must
21840    be called after function DIEs have been generated.  */
21841
21842 static void
21843 gen_remaining_tmpl_value_param_die_attribute (void)
21844 {
21845   if (tmpl_value_parm_die_table)
21846     {
21847       unsigned i;
21848       die_arg_entry *e;
21849
21850       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21851         tree_add_const_value_attribute (e->die, e->arg);
21852     }
21853 }
21854
21855 /* Generate generic parameters DIEs for instances of generic types
21856    that have been previously scheduled by
21857    schedule_generic_params_dies_gen. This function must be called
21858    after all the types of the CU have been laid out.  */
21859
21860 static void
21861 gen_scheduled_generic_parms_dies (void)
21862 {
21863   unsigned i;
21864   tree t;
21865
21866   if (generic_type_instances == NULL)
21867     return;
21868   
21869   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21870     gen_generic_params_dies (t);
21871 }
21872
21873
21874 /* Replace DW_AT_name for the decl with name.  */
21875
21876 static void
21877 dwarf2out_set_name (tree decl, tree name)
21878 {
21879   dw_die_ref die;
21880   dw_attr_ref attr;
21881   const char *dname;
21882
21883   die = TYPE_SYMTAB_DIE (decl);
21884   if (!die)
21885     return;
21886
21887   dname = dwarf2_name (name, 0);
21888   if (!dname)
21889     return;
21890
21891   attr = get_AT (die, DW_AT_name);
21892   if (attr)
21893     {
21894       struct indirect_string_node *node;
21895
21896       node = find_AT_string (dname);
21897       /* replace the string.  */
21898       attr->dw_attr_val.v.val_str = node;
21899     }
21900
21901   else
21902     add_name_attribute (die, dname);
21903 }
21904
21905 /* Called by the final INSN scan whenever we see a var location.  We
21906    use it to drop labels in the right places, and throw the location in
21907    our lookup table.  */
21908
21909 static void
21910 dwarf2out_var_location (rtx loc_note)
21911 {
21912   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21913   struct var_loc_node *newloc;
21914   rtx next_real;
21915   static const char *last_label;
21916   static const char *last_postcall_label;
21917   static bool last_in_cold_section_p;
21918   tree decl;
21919   bool var_loc_p;
21920
21921   if (!NOTE_P (loc_note))
21922     {
21923       if (CALL_P (loc_note))
21924         {
21925           call_site_count++;
21926           if (SIBLING_CALL_P (loc_note))
21927             tail_call_site_count++;
21928         }
21929       return;
21930     }
21931
21932   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21933   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21934     return;
21935
21936   next_real = next_real_insn (loc_note);
21937
21938   /* If there are no instructions which would be affected by this note,
21939      don't do anything.  */
21940   if (var_loc_p
21941       && next_real == NULL_RTX
21942       && !NOTE_DURING_CALL_P (loc_note))
21943     return;
21944
21945   if (next_real == NULL_RTX)
21946     next_real = get_last_insn ();
21947
21948   /* If there were any real insns between note we processed last time
21949      and this note (or if it is the first note), clear
21950      last_{,postcall_}label so that they are not reused this time.  */
21951   if (last_var_location_insn == NULL_RTX
21952       || last_var_location_insn != next_real
21953       || last_in_cold_section_p != in_cold_section_p)
21954     {
21955       last_label = NULL;
21956       last_postcall_label = NULL;
21957     }
21958
21959   if (var_loc_p)
21960     {
21961       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21962       newloc = add_var_loc_to_decl (decl, loc_note,
21963                                     NOTE_DURING_CALL_P (loc_note)
21964                                     ? last_postcall_label : last_label);
21965       if (newloc == NULL)
21966         return;
21967     }
21968   else
21969     {
21970       decl = NULL_TREE;
21971       newloc = NULL;
21972     }
21973
21974   /* If there were no real insns between note we processed last time
21975      and this note, use the label we emitted last time.  Otherwise
21976      create a new label and emit it.  */
21977   if (last_label == NULL)
21978     {
21979       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21980       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21981       loclabel_num++;
21982       last_label = ggc_strdup (loclabel);
21983     }
21984
21985   if (!var_loc_p)
21986     {
21987       struct call_arg_loc_node *ca_loc
21988         = ggc_alloc_cleared_call_arg_loc_node ();
21989       rtx prev = prev_real_insn (loc_note), x;
21990       ca_loc->call_arg_loc_note = loc_note;
21991       ca_loc->next = NULL;
21992       ca_loc->label = last_label;
21993       gcc_assert (prev
21994                   && (CALL_P (prev)
21995                       || (NONJUMP_INSN_P (prev)
21996                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21997                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21998       if (!CALL_P (prev))
21999         prev = XVECEXP (PATTERN (prev), 0, 0);
22000       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22001       x = PATTERN (prev);
22002       if (GET_CODE (x) == PARALLEL)
22003         x = XVECEXP (x, 0, 0);
22004       if (GET_CODE (x) == SET)
22005         x = SET_SRC (x);
22006       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22007         {
22008           x = XEXP (XEXP (x, 0), 0);
22009           if (GET_CODE (x) == SYMBOL_REF
22010               && SYMBOL_REF_DECL (x)
22011               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22012             ca_loc->symbol_ref = x;
22013         }
22014       ca_loc->block = insn_scope (prev);
22015       if (call_arg_locations)
22016         call_arg_loc_last->next = ca_loc;
22017       else
22018         call_arg_locations = ca_loc;
22019       call_arg_loc_last = ca_loc;
22020     }
22021   else if (!NOTE_DURING_CALL_P (loc_note))
22022     newloc->label = last_label;
22023   else
22024     {
22025       if (!last_postcall_label)
22026         {
22027           sprintf (loclabel, "%s-1", last_label);
22028           last_postcall_label = ggc_strdup (loclabel);
22029         }
22030       newloc->label = last_postcall_label;
22031     }
22032
22033   last_var_location_insn = next_real;
22034   last_in_cold_section_p = in_cold_section_p;
22035 }
22036
22037 /* We need to reset the locations at the beginning of each
22038    function. We can't do this in the end_function hook, because the
22039    declarations that use the locations won't have been output when
22040    that hook is called.  Also compute have_multiple_function_sections here.  */
22041
22042 static void
22043 dwarf2out_begin_function (tree fun)
22044 {
22045   if (function_section (fun) != text_section)
22046     have_multiple_function_sections = true;
22047   else if (flag_reorder_blocks_and_partition && !cold_text_section)
22048     {
22049       gcc_assert (current_function_decl == fun);
22050       cold_text_section = unlikely_text_section ();
22051       switch_to_section (cold_text_section);
22052       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22053       switch_to_section (current_function_section ());
22054     }
22055
22056   dwarf2out_note_section_used ();
22057   call_site_count = 0;
22058   tail_call_site_count = 0;
22059 }
22060
22061 /* Output a label to mark the beginning of a source code line entry
22062    and record information relating to this source line, in
22063    'line_info_table' for later output of the .debug_line section.  */
22064
22065 static void
22066 dwarf2out_source_line (unsigned int line, const char *filename,
22067                        int discriminator, bool is_stmt)
22068 {
22069   static bool last_is_stmt = true;
22070
22071   if (debug_info_level >= DINFO_LEVEL_NORMAL
22072       && line != 0)
22073     {
22074       int file_num = maybe_emit_file (lookup_filename (filename));
22075
22076       switch_to_section (current_function_section ());
22077
22078       /* If requested, emit something human-readable.  */
22079       if (flag_debug_asm)
22080         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22081                  filename, line);
22082
22083       if (DWARF2_ASM_LINE_DEBUG_INFO)
22084         {
22085           /* Emit the .loc directive understood by GNU as.  */
22086           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22087           if (is_stmt != last_is_stmt)
22088             {
22089               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22090               last_is_stmt = is_stmt;
22091             }
22092           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22093             fprintf (asm_out_file, " discriminator %d", discriminator);
22094           fputc ('\n', asm_out_file);
22095
22096           /* Indicate that line number info exists.  */
22097           line_info_table_in_use++;
22098         }
22099       else if (function_section (current_function_decl) != text_section)
22100         {
22101           dw_separate_line_info_ref line_info;
22102           targetm.asm_out.internal_label (asm_out_file,
22103                                           SEPARATE_LINE_CODE_LABEL,
22104                                           separate_line_info_table_in_use);
22105
22106           /* Expand the line info table if necessary.  */
22107           if (separate_line_info_table_in_use
22108               == separate_line_info_table_allocated)
22109             {
22110               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22111               separate_line_info_table
22112                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22113                                  separate_line_info_table,
22114                                  separate_line_info_table_allocated);
22115               memset (separate_line_info_table
22116                        + separate_line_info_table_in_use,
22117                       0,
22118                       (LINE_INFO_TABLE_INCREMENT
22119                        * sizeof (dw_separate_line_info_entry)));
22120             }
22121
22122           /* Add the new entry at the end of the line_info_table.  */
22123           line_info
22124             = &separate_line_info_table[separate_line_info_table_in_use++];
22125           line_info->dw_file_num = file_num;
22126           line_info->dw_line_num = line;
22127           line_info->function = current_function_funcdef_no;
22128         }
22129       else
22130         {
22131           dw_line_info_ref line_info;
22132
22133           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22134                                      line_info_table_in_use);
22135
22136           /* Expand the line info table if necessary.  */
22137           if (line_info_table_in_use == line_info_table_allocated)
22138             {
22139               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22140               line_info_table
22141                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22142                                  line_info_table_allocated);
22143               memset (line_info_table + line_info_table_in_use, 0,
22144                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22145             }
22146
22147           /* Add the new entry at the end of the line_info_table.  */
22148           line_info = &line_info_table[line_info_table_in_use++];
22149           line_info->dw_file_num = file_num;
22150           line_info->dw_line_num = line;
22151         }
22152     }
22153 }
22154
22155 /* Record the beginning of a new source file.  */
22156
22157 static void
22158 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22159 {
22160   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22161     {
22162       /* Record the beginning of the file for break_out_includes.  */
22163       dw_die_ref bincl_die;
22164
22165       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22166       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22167     }
22168
22169   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22170     {
22171       macinfo_entry e;
22172       e.code = DW_MACINFO_start_file;
22173       e.lineno = lineno;
22174       e.info = xstrdup (filename);
22175       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22176     }
22177 }
22178
22179 /* Record the end of a source file.  */
22180
22181 static void
22182 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22183 {
22184   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22185     /* Record the end of the file for break_out_includes.  */
22186     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22187
22188   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22189     {
22190       macinfo_entry e;
22191       e.code = DW_MACINFO_end_file;
22192       e.lineno = lineno;
22193       e.info = NULL;
22194       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22195     }
22196 }
22197
22198 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22199    the tail part of the directive line, i.e. the part which is past the
22200    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22201
22202 static void
22203 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22204                   const char *buffer ATTRIBUTE_UNUSED)
22205 {
22206   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22207     {
22208       macinfo_entry e;
22209       e.code = DW_MACINFO_define;
22210       e.lineno = lineno;
22211       e.info = xstrdup (buffer);;
22212       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22213     }
22214 }
22215
22216 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22217    the tail part of the directive line, i.e. the part which is past the
22218    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22219
22220 static void
22221 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22222                  const char *buffer ATTRIBUTE_UNUSED)
22223 {
22224   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22225     {
22226       macinfo_entry e;
22227       e.code = DW_MACINFO_undef;
22228       e.lineno = lineno;
22229       e.info = xstrdup (buffer);;
22230       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22231     }
22232 }
22233
22234 static void
22235 output_macinfo (void)
22236 {
22237   unsigned i;
22238   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22239   macinfo_entry *ref;
22240
22241   if (! length)
22242     return;
22243
22244   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22245     {
22246       switch (ref->code)
22247         {
22248           case DW_MACINFO_start_file:
22249             {
22250               int file_num = maybe_emit_file (lookup_filename (ref->info));
22251               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22252               dw2_asm_output_data_uleb128 
22253                         (ref->lineno, "Included from line number %lu", 
22254                                                 (unsigned long)ref->lineno);
22255               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22256             }
22257             break;
22258           case DW_MACINFO_end_file:
22259             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22260             break;
22261           case DW_MACINFO_define:
22262             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22263             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22264                                                 (unsigned long)ref->lineno);
22265             dw2_asm_output_nstring (ref->info, -1, "The macro");
22266             break;
22267           case DW_MACINFO_undef:
22268             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22269             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22270                                                 (unsigned long)ref->lineno);
22271             dw2_asm_output_nstring (ref->info, -1, "The macro");
22272             break;
22273           default:
22274            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22275              ASM_COMMENT_START, (unsigned long)ref->code);
22276           break;
22277         }
22278     }
22279 }
22280
22281 /* Set up for Dwarf output at the start of compilation.  */
22282
22283 static void
22284 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22285 {
22286   /* Allocate the file_table.  */
22287   file_table = htab_create_ggc (50, file_table_hash,
22288                                 file_table_eq, NULL);
22289
22290   /* Allocate the decl_die_table.  */
22291   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22292                                     decl_die_table_eq, NULL);
22293
22294   /* Allocate the decl_loc_table.  */
22295   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22296                                     decl_loc_table_eq, NULL);
22297
22298   /* Allocate the initial hunk of the decl_scope_table.  */
22299   decl_scope_table = VEC_alloc (tree, gc, 256);
22300
22301   /* Allocate the initial hunk of the abbrev_die_table.  */
22302   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22303     (ABBREV_DIE_TABLE_INCREMENT);
22304   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22305   /* Zero-th entry is allocated, but unused.  */
22306   abbrev_die_table_in_use = 1;
22307
22308   /* Allocate the initial hunk of the line_info_table.  */
22309   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22310     (LINE_INFO_TABLE_INCREMENT);
22311   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22312
22313   /* Zero-th entry is allocated, but unused.  */
22314   line_info_table_in_use = 1;
22315
22316   /* Allocate the pubtypes and pubnames vectors.  */
22317   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22318   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22319
22320   incomplete_types = VEC_alloc (tree, gc, 64);
22321
22322   used_rtx_array = VEC_alloc (rtx, gc, 32);
22323
22324   debug_info_section = get_section (DEBUG_INFO_SECTION,
22325                                     SECTION_DEBUG, NULL);
22326   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22327                                       SECTION_DEBUG, NULL);
22328   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22329                                        SECTION_DEBUG, NULL);
22330   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22331                                        SECTION_DEBUG, NULL);
22332   debug_line_section = get_section (DEBUG_LINE_SECTION,
22333                                     SECTION_DEBUG, NULL);
22334   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22335                                    SECTION_DEBUG, NULL);
22336   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22337                                         SECTION_DEBUG, NULL);
22338   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22339                                         SECTION_DEBUG, NULL);
22340   debug_str_section = get_section (DEBUG_STR_SECTION,
22341                                    DEBUG_STR_SECTION_FLAGS, NULL);
22342   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22343                                       SECTION_DEBUG, NULL);
22344   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22345                                      SECTION_DEBUG, NULL);
22346
22347   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22348   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22349                                DEBUG_ABBREV_SECTION_LABEL, 0);
22350   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22351   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22352                                COLD_TEXT_SECTION_LABEL, 0);
22353   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22354
22355   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22356                                DEBUG_INFO_SECTION_LABEL, 0);
22357   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22358                                DEBUG_LINE_SECTION_LABEL, 0);
22359   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22360                                DEBUG_RANGES_SECTION_LABEL, 0);
22361   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22362                                DEBUG_MACINFO_SECTION_LABEL, 0);
22363
22364   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22365     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22366
22367   switch_to_section (text_section);
22368   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22369 }
22370
22371 /* Called before cgraph_optimize starts outputtting functions, variables
22372    and toplevel asms into assembly.  */
22373
22374 static void
22375 dwarf2out_assembly_start (void)
22376 {
22377   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22378       && dwarf2out_do_cfi_asm ()
22379       && (!(flag_unwind_tables || flag_exceptions)
22380           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22381     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22382 }
22383
22384 /* A helper function for dwarf2out_finish called through
22385    htab_traverse.  Emit one queued .debug_str string.  */
22386
22387 static int
22388 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22389 {
22390   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22391
22392   if (node->label && node->refcount)
22393     {
22394       switch_to_section (debug_str_section);
22395       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22396       assemble_string (node->str, strlen (node->str) + 1);
22397     }
22398
22399   return 1;
22400 }
22401
22402 #if ENABLE_ASSERT_CHECKING
22403 /* Verify that all marks are clear.  */
22404
22405 static void
22406 verify_marks_clear (dw_die_ref die)
22407 {
22408   dw_die_ref c;
22409
22410   gcc_assert (! die->die_mark);
22411   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22412 }
22413 #endif /* ENABLE_ASSERT_CHECKING */
22414
22415 /* Clear the marks for a die and its children.
22416    Be cool if the mark isn't set.  */
22417
22418 static void
22419 prune_unmark_dies (dw_die_ref die)
22420 {
22421   dw_die_ref c;
22422
22423   if (die->die_mark)
22424     die->die_mark = 0;
22425   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22426 }
22427
22428 /* Given DIE that we're marking as used, find any other dies
22429    it references as attributes and mark them as used.  */
22430
22431 static void
22432 prune_unused_types_walk_attribs (dw_die_ref die)
22433 {
22434   dw_attr_ref a;
22435   unsigned ix;
22436
22437   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22438     {
22439       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22440         {
22441           /* A reference to another DIE.
22442              Make sure that it will get emitted.
22443              If it was broken out into a comdat group, don't follow it.  */
22444           if (dwarf_version < 4
22445               || a->dw_attr == DW_AT_specification
22446               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22447             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22448         }
22449       /* Set the string's refcount to 0 so that prune_unused_types_mark
22450          accounts properly for it.  */
22451       if (AT_class (a) == dw_val_class_str)
22452         a->dw_attr_val.v.val_str->refcount = 0;
22453     }
22454 }
22455
22456 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22457
22458 static void
22459 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22460 {
22461   dw_die_ref c;
22462
22463   if (die == NULL || die->die_child == NULL)
22464     return;
22465   c = die->die_child;
22466   do
22467     {
22468       switch (c->die_tag)
22469         {
22470         case DW_TAG_template_type_param:
22471         case DW_TAG_template_value_param:
22472         case DW_TAG_GNU_template_template_param:
22473         case DW_TAG_GNU_template_parameter_pack:
22474           prune_unused_types_mark (c, 1);
22475           break;
22476         default:
22477           break;
22478         }
22479       c = c->die_sib;
22480     } while (c && c != die->die_child);
22481 }
22482
22483 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22484    to DIE's children.  */
22485
22486 static void
22487 prune_unused_types_mark (dw_die_ref die, int dokids)
22488 {
22489   dw_die_ref c;
22490
22491   if (die->die_mark == 0)
22492     {
22493       /* We haven't done this node yet.  Mark it as used.  */
22494       die->die_mark = 1;
22495       /* If this is the DIE of a generic type instantiation,
22496          mark the children DIEs that describe its generic parms and
22497          args.  */
22498       prune_unused_types_mark_generic_parms_dies (die);
22499
22500       /* We also have to mark its parents as used.
22501          (But we don't want to mark our parents' kids due to this.)  */
22502       if (die->die_parent)
22503         prune_unused_types_mark (die->die_parent, 0);
22504
22505       /* Mark any referenced nodes.  */
22506       prune_unused_types_walk_attribs (die);
22507
22508       /* If this node is a specification,
22509          also mark the definition, if it exists.  */
22510       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22511         prune_unused_types_mark (die->die_definition, 1);
22512     }
22513
22514   if (dokids && die->die_mark != 2)
22515     {
22516       /* We need to walk the children, but haven't done so yet.
22517          Remember that we've walked the kids.  */
22518       die->die_mark = 2;
22519
22520       /* If this is an array type, we need to make sure our
22521          kids get marked, even if they're types.  If we're
22522          breaking out types into comdat sections, do this
22523          for all type definitions.  */
22524       if (die->die_tag == DW_TAG_array_type
22525           || (dwarf_version >= 4
22526               && is_type_die (die) && ! is_declaration_die (die)))
22527         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22528       else
22529         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22530     }
22531 }
22532
22533 /* For local classes, look if any static member functions were emitted
22534    and if so, mark them.  */
22535
22536 static void
22537 prune_unused_types_walk_local_classes (dw_die_ref die)
22538 {
22539   dw_die_ref c;
22540
22541   if (die->die_mark == 2)
22542     return;
22543
22544   switch (die->die_tag)
22545     {
22546     case DW_TAG_structure_type:
22547     case DW_TAG_union_type:
22548     case DW_TAG_class_type:
22549       break;
22550
22551     case DW_TAG_subprogram:
22552       if (!get_AT_flag (die, DW_AT_declaration)
22553           || die->die_definition != NULL)
22554         prune_unused_types_mark (die, 1);
22555       return;
22556
22557     default:
22558       return;
22559     }
22560
22561   /* Mark children.  */
22562   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22563 }
22564
22565 /* Walk the tree DIE and mark types that we actually use.  */
22566
22567 static void
22568 prune_unused_types_walk (dw_die_ref die)
22569 {
22570   dw_die_ref c;
22571
22572   /* Don't do anything if this node is already marked and
22573      children have been marked as well.  */
22574   if (die->die_mark == 2)
22575     return;
22576
22577   switch (die->die_tag)
22578     {
22579     case DW_TAG_structure_type:
22580     case DW_TAG_union_type:
22581     case DW_TAG_class_type:
22582       if (die->die_perennial_p)
22583         break;
22584
22585       for (c = die->die_parent; c; c = c->die_parent)
22586         if (c->die_tag == DW_TAG_subprogram)
22587           break;
22588
22589       /* Finding used static member functions inside of classes
22590          is needed just for local classes, because for other classes
22591          static member function DIEs with DW_AT_specification
22592          are emitted outside of the DW_TAG_*_type.  If we ever change
22593          it, we'd need to call this even for non-local classes.  */
22594       if (c)
22595         prune_unused_types_walk_local_classes (die);
22596
22597       /* It's a type node --- don't mark it.  */
22598       return;
22599
22600     case DW_TAG_const_type:
22601     case DW_TAG_packed_type:
22602     case DW_TAG_pointer_type:
22603     case DW_TAG_reference_type:
22604     case DW_TAG_rvalue_reference_type:
22605     case DW_TAG_volatile_type:
22606     case DW_TAG_typedef:
22607     case DW_TAG_array_type:
22608     case DW_TAG_interface_type:
22609     case DW_TAG_friend:
22610     case DW_TAG_variant_part:
22611     case DW_TAG_enumeration_type:
22612     case DW_TAG_subroutine_type:
22613     case DW_TAG_string_type:
22614     case DW_TAG_set_type:
22615     case DW_TAG_subrange_type:
22616     case DW_TAG_ptr_to_member_type:
22617     case DW_TAG_file_type:
22618       if (die->die_perennial_p)
22619         break;
22620
22621       /* It's a type node --- don't mark it.  */
22622       return;
22623
22624     default:
22625       /* Mark everything else.  */
22626       break;
22627   }
22628
22629   if (die->die_mark == 0)
22630     {
22631       die->die_mark = 1;
22632
22633       /* Now, mark any dies referenced from here.  */
22634       prune_unused_types_walk_attribs (die);
22635     }
22636
22637   die->die_mark = 2;
22638
22639   /* Mark children.  */
22640   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22641 }
22642
22643 /* Increment the string counts on strings referred to from DIE's
22644    attributes.  */
22645
22646 static void
22647 prune_unused_types_update_strings (dw_die_ref die)
22648 {
22649   dw_attr_ref a;
22650   unsigned ix;
22651
22652   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22653     if (AT_class (a) == dw_val_class_str)
22654       {
22655         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22656         s->refcount++;
22657         /* Avoid unnecessarily putting strings that are used less than
22658            twice in the hash table.  */
22659         if (s->refcount
22660             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22661           {
22662             void ** slot;
22663             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22664                                              htab_hash_string (s->str),
22665                                              INSERT);
22666             gcc_assert (*slot == NULL);
22667             *slot = s;
22668           }
22669       }
22670 }
22671
22672 /* Remove from the tree DIE any dies that aren't marked.  */
22673
22674 static void
22675 prune_unused_types_prune (dw_die_ref die)
22676 {
22677   dw_die_ref c;
22678
22679   gcc_assert (die->die_mark);
22680   prune_unused_types_update_strings (die);
22681
22682   if (! die->die_child)
22683     return;
22684
22685   c = die->die_child;
22686   do {
22687     dw_die_ref prev = c;
22688     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22689       if (c == die->die_child)
22690         {
22691           /* No marked children between 'prev' and the end of the list.  */
22692           if (prev == c)
22693             /* No marked children at all.  */
22694             die->die_child = NULL;
22695           else
22696             {
22697               prev->die_sib = c->die_sib;
22698               die->die_child = prev;
22699             }
22700           return;
22701         }
22702
22703     if (c != prev->die_sib)
22704       prev->die_sib = c;
22705     prune_unused_types_prune (c);
22706   } while (c != die->die_child);
22707 }
22708
22709 /* A helper function for dwarf2out_finish called through
22710    htab_traverse.  Clear .debug_str strings that we haven't already
22711    decided to emit.  */
22712
22713 static int
22714 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22715 {
22716   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22717
22718   if (!node->label || !node->refcount)
22719     htab_clear_slot (debug_str_hash, h);
22720
22721   return 1;
22722 }
22723
22724 /* Remove dies representing declarations that we never use.  */
22725
22726 static void
22727 prune_unused_types (void)
22728 {
22729   unsigned int i;
22730   limbo_die_node *node;
22731   comdat_type_node *ctnode;
22732   pubname_ref pub;
22733
22734 #if ENABLE_ASSERT_CHECKING
22735   /* All the marks should already be clear.  */
22736   verify_marks_clear (comp_unit_die ());
22737   for (node = limbo_die_list; node; node = node->next)
22738     verify_marks_clear (node->die);
22739   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22740     verify_marks_clear (ctnode->root_die);
22741 #endif /* ENABLE_ASSERT_CHECKING */
22742
22743   /* Mark types that are used in global variables.  */
22744   premark_types_used_by_global_vars ();
22745
22746   /* Set the mark on nodes that are actually used.  */
22747   prune_unused_types_walk (comp_unit_die ());
22748   for (node = limbo_die_list; node; node = node->next)
22749     prune_unused_types_walk (node->die);
22750   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22751     {
22752       prune_unused_types_walk (ctnode->root_die);
22753       prune_unused_types_mark (ctnode->type_die, 1);
22754     }
22755
22756   /* Also set the mark on nodes referenced from the
22757      pubname_table or arange_table.  */
22758   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22759     prune_unused_types_mark (pub->die, 1);
22760   for (i = 0; i < arange_table_in_use; i++)
22761     prune_unused_types_mark (arange_table[i], 1);
22762
22763   /* Get rid of nodes that aren't marked; and update the string counts.  */
22764   if (debug_str_hash && debug_str_hash_forced)
22765     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22766   else if (debug_str_hash)
22767     htab_empty (debug_str_hash);
22768   prune_unused_types_prune (comp_unit_die ());
22769   for (node = limbo_die_list; node; node = node->next)
22770     prune_unused_types_prune (node->die);
22771   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22772     prune_unused_types_prune (ctnode->root_die);
22773
22774   /* Leave the marks clear.  */
22775   prune_unmark_dies (comp_unit_die ());
22776   for (node = limbo_die_list; node; node = node->next)
22777     prune_unmark_dies (node->die);
22778   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22779     prune_unmark_dies (ctnode->root_die);
22780 }
22781
22782 /* Set the parameter to true if there are any relative pathnames in
22783    the file table.  */
22784 static int
22785 file_table_relative_p (void ** slot, void *param)
22786 {
22787   bool *p = (bool *) param;
22788   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22789   if (!IS_ABSOLUTE_PATH (d->filename))
22790     {
22791       *p = true;
22792       return 0;
22793     }
22794   return 1;
22795 }
22796
22797 /* Routines to manipulate hash table of comdat type units.  */
22798
22799 static hashval_t
22800 htab_ct_hash (const void *of)
22801 {
22802   hashval_t h;
22803   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22804
22805   memcpy (&h, type_node->signature, sizeof (h));
22806   return h;
22807 }
22808
22809 static int
22810 htab_ct_eq (const void *of1, const void *of2)
22811 {
22812   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22813   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22814
22815   return (! memcmp (type_node_1->signature, type_node_2->signature,
22816                     DWARF_TYPE_SIGNATURE_SIZE));
22817 }
22818
22819 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22820    to the location it would have been added, should we know its
22821    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22822    probably improve compactness of debug info, removing equivalent
22823    abbrevs, and hide any differences caused by deferring the
22824    computation of the assembler name, triggered by e.g. PCH.  */
22825
22826 static inline void
22827 move_linkage_attr (dw_die_ref die)
22828 {
22829   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22830   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22831
22832   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22833               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22834
22835   while (--ix > 0)
22836     {
22837       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22838
22839       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22840         break;
22841     }
22842
22843   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22844     {
22845       VEC_pop (dw_attr_node, die->die_attr);
22846       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22847     }
22848 }
22849
22850 /* Helper function for resolve_addr, attempt to resolve
22851    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22852    SYMBOL_REFs refer to variables emitted in the current CU.  */
22853
22854 static int
22855 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22856 {
22857   rtx rtl = *addr;
22858
22859   if (GET_CODE (rtl) == CONST_STRING)
22860     {
22861       size_t len = strlen (XSTR (rtl, 0)) + 1;
22862       tree t = build_string (len, XSTR (rtl, 0));
22863       tree tlen = build_int_cst (NULL_TREE, len - 1);
22864       TREE_TYPE (t)
22865         = build_array_type (char_type_node, build_index_type (tlen));
22866       rtl = lookup_constant_def (t);
22867       if (!rtl || !MEM_P (rtl))
22868         return 1;
22869       rtl = XEXP (rtl, 0);
22870       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22871       *addr = rtl;
22872       return 0;
22873     }
22874
22875   if (GET_CODE (rtl) == SYMBOL_REF
22876       && SYMBOL_REF_DECL (rtl))
22877     {
22878       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22879         {
22880           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22881             return 1;
22882         }
22883       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22884         return 1;
22885     }
22886
22887   if (GET_CODE (rtl) == CONST
22888       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22889     return 1;
22890
22891   return 0;
22892 }
22893
22894 /* Helper function for resolve_addr, handle one location
22895    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22896    the location list couldn't be resolved.  */
22897
22898 static bool
22899 resolve_addr_in_expr (dw_loc_descr_ref loc)
22900 {
22901   for (; loc; loc = loc->dw_loc_next)
22902     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22903          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22904         || (loc->dw_loc_opc == DW_OP_implicit_value
22905             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22906             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22907       return false;
22908     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22909              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22910       {
22911         dw_die_ref ref
22912           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22913         if (ref == NULL)
22914           return false;
22915         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22916         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22917         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22918       }
22919   return true;
22920 }
22921
22922 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22923    an address in .rodata section if the string literal is emitted there,
22924    or remove the containing location list or replace DW_AT_const_value
22925    with DW_AT_location and empty location expression, if it isn't found
22926    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22927    to something that has been emitted in the current CU.  */
22928
22929 static void
22930 resolve_addr (dw_die_ref die)
22931 {
22932   dw_die_ref c;
22933   dw_attr_ref a;
22934   dw_loc_list_ref *curr;
22935   unsigned ix;
22936
22937   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22938     switch (AT_class (a))
22939       {
22940       case dw_val_class_loc_list:
22941         curr = AT_loc_list_ptr (a);
22942         while (*curr)
22943           {
22944             if (!resolve_addr_in_expr ((*curr)->expr))
22945               {
22946                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22947                 if (next && (*curr)->ll_symbol)
22948                   {
22949                     gcc_assert (!next->ll_symbol);
22950                     next->ll_symbol = (*curr)->ll_symbol;
22951                   }
22952                 *curr = next;
22953               }
22954             else
22955               curr = &(*curr)->dw_loc_next;
22956           }
22957         if (!AT_loc_list (a))
22958           {
22959             remove_AT (die, a->dw_attr);
22960             ix--;
22961           }
22962         break;
22963       case dw_val_class_loc:
22964         if (!resolve_addr_in_expr (AT_loc (a)))
22965           {
22966             remove_AT (die, a->dw_attr);
22967             ix--;
22968           }
22969         break;
22970       case dw_val_class_addr:
22971         if (a->dw_attr == DW_AT_const_value
22972             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22973           {
22974             remove_AT (die, a->dw_attr);
22975             ix--;
22976           }
22977         if (die->die_tag == DW_TAG_GNU_call_site
22978             && a->dw_attr == DW_AT_abstract_origin)
22979           {
22980             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22981             dw_die_ref tdie = lookup_decl_die (tdecl);
22982             if (tdie == NULL && DECL_EXTERNAL (tdecl))
22983               {
22984                 force_decl_die (tdecl);
22985                 tdie = lookup_decl_die (tdecl);
22986               }
22987             if (tdie)
22988               {
22989                 a->dw_attr_val.val_class = dw_val_class_die_ref;
22990                 a->dw_attr_val.v.val_die_ref.die = tdie;
22991                 a->dw_attr_val.v.val_die_ref.external = 0;
22992               }
22993             else
22994               {
22995                 remove_AT (die, a->dw_attr);
22996                 ix--;
22997               }
22998           }
22999         break;
23000       default:
23001         break;
23002       }
23003
23004   FOR_EACH_CHILD (die, c, resolve_addr (c));
23005 }
23006 \f
23007 /* Helper routines for optimize_location_lists.
23008    This pass tries to share identical local lists in .debug_loc
23009    section.  */
23010
23011 /* Iteratively hash operands of LOC opcode.  */
23012
23013 static inline hashval_t
23014 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23015 {
23016   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23017   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23018
23019   switch (loc->dw_loc_opc)
23020     {
23021     case DW_OP_const4u:
23022     case DW_OP_const8u:
23023       if (loc->dtprel)
23024         goto hash_addr;
23025       /* FALLTHRU */
23026     case DW_OP_const1u:
23027     case DW_OP_const1s:
23028     case DW_OP_const2u:
23029     case DW_OP_const2s:
23030     case DW_OP_const4s:
23031     case DW_OP_const8s:
23032     case DW_OP_constu:
23033     case DW_OP_consts:
23034     case DW_OP_pick:
23035     case DW_OP_plus_uconst:
23036     case DW_OP_breg0:
23037     case DW_OP_breg1:
23038     case DW_OP_breg2:
23039     case DW_OP_breg3:
23040     case DW_OP_breg4:
23041     case DW_OP_breg5:
23042     case DW_OP_breg6:
23043     case DW_OP_breg7:
23044     case DW_OP_breg8:
23045     case DW_OP_breg9:
23046     case DW_OP_breg10:
23047     case DW_OP_breg11:
23048     case DW_OP_breg12:
23049     case DW_OP_breg13:
23050     case DW_OP_breg14:
23051     case DW_OP_breg15:
23052     case DW_OP_breg16:
23053     case DW_OP_breg17:
23054     case DW_OP_breg18:
23055     case DW_OP_breg19:
23056     case DW_OP_breg20:
23057     case DW_OP_breg21:
23058     case DW_OP_breg22:
23059     case DW_OP_breg23:
23060     case DW_OP_breg24:
23061     case DW_OP_breg25:
23062     case DW_OP_breg26:
23063     case DW_OP_breg27:
23064     case DW_OP_breg28:
23065     case DW_OP_breg29:
23066     case DW_OP_breg30:
23067     case DW_OP_breg31:
23068     case DW_OP_regx:
23069     case DW_OP_fbreg:
23070     case DW_OP_piece:
23071     case DW_OP_deref_size:
23072     case DW_OP_xderef_size:
23073       hash = iterative_hash_object (val1->v.val_int, hash);
23074       break;
23075     case DW_OP_skip:
23076     case DW_OP_bra:
23077       {
23078         int offset;
23079
23080         gcc_assert (val1->val_class == dw_val_class_loc);
23081         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23082         hash = iterative_hash_object (offset, hash);
23083       }
23084       break;
23085     case DW_OP_implicit_value:
23086       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23087       switch (val2->val_class)
23088         {
23089         case dw_val_class_const:
23090           hash = iterative_hash_object (val2->v.val_int, hash);
23091           break;
23092         case dw_val_class_vec:
23093           {
23094             unsigned int elt_size = val2->v.val_vec.elt_size;
23095             unsigned int len = val2->v.val_vec.length;
23096
23097             hash = iterative_hash_object (elt_size, hash);
23098             hash = iterative_hash_object (len, hash);
23099             hash = iterative_hash (val2->v.val_vec.array,
23100                                    len * elt_size, hash);
23101           }
23102           break;
23103         case dw_val_class_const_double:
23104           hash = iterative_hash_object (val2->v.val_double.low, hash);
23105           hash = iterative_hash_object (val2->v.val_double.high, hash);
23106           break;
23107         case dw_val_class_addr:
23108           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23109           break;
23110         default:
23111           gcc_unreachable ();
23112         }
23113       break;
23114     case DW_OP_bregx:
23115     case DW_OP_bit_piece:
23116       hash = iterative_hash_object (val1->v.val_int, hash);
23117       hash = iterative_hash_object (val2->v.val_int, hash);
23118       break;
23119     case DW_OP_addr:
23120     hash_addr:
23121       if (loc->dtprel)
23122         {
23123           unsigned char dtprel = 0xd1;
23124           hash = iterative_hash_object (dtprel, hash);
23125         }
23126       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23127       break;
23128     case DW_OP_GNU_implicit_pointer:
23129       hash = iterative_hash_object (val2->v.val_int, hash);
23130       break;
23131
23132     default:
23133       /* Other codes have no operands.  */
23134       break;
23135     }
23136   return hash;
23137 }
23138
23139 /* Iteratively hash the whole DWARF location expression LOC.  */
23140
23141 static inline hashval_t
23142 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23143 {
23144   dw_loc_descr_ref l;
23145   bool sizes_computed = false;
23146   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23147   size_of_locs (loc);
23148
23149   for (l = loc; l != NULL; l = l->dw_loc_next)
23150     {
23151       enum dwarf_location_atom opc = l->dw_loc_opc;
23152       hash = iterative_hash_object (opc, hash);
23153       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23154         {
23155           size_of_locs (loc);
23156           sizes_computed = true;
23157         }
23158       hash = hash_loc_operands (l, hash);
23159     }
23160   return hash;
23161 }
23162
23163 /* Compute hash of the whole location list LIST_HEAD.  */
23164
23165 static inline void
23166 hash_loc_list (dw_loc_list_ref list_head)
23167 {
23168   dw_loc_list_ref curr = list_head;
23169   hashval_t hash = 0;
23170
23171   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23172     {
23173       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23174       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23175       if (curr->section)
23176         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23177                                hash);
23178       hash = hash_locs (curr->expr, hash);
23179     }
23180   list_head->hash = hash;
23181 }
23182
23183 /* Return true if X and Y opcodes have the same operands.  */
23184
23185 static inline bool
23186 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23187 {
23188   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23189   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23190   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23191   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23192
23193   switch (x->dw_loc_opc)
23194     {
23195     case DW_OP_const4u:
23196     case DW_OP_const8u:
23197       if (x->dtprel)
23198         goto hash_addr;
23199       /* FALLTHRU */
23200     case DW_OP_const1u:
23201     case DW_OP_const1s:
23202     case DW_OP_const2u:
23203     case DW_OP_const2s:
23204     case DW_OP_const4s:
23205     case DW_OP_const8s:
23206     case DW_OP_constu:
23207     case DW_OP_consts:
23208     case DW_OP_pick:
23209     case DW_OP_plus_uconst:
23210     case DW_OP_breg0:
23211     case DW_OP_breg1:
23212     case DW_OP_breg2:
23213     case DW_OP_breg3:
23214     case DW_OP_breg4:
23215     case DW_OP_breg5:
23216     case DW_OP_breg6:
23217     case DW_OP_breg7:
23218     case DW_OP_breg8:
23219     case DW_OP_breg9:
23220     case DW_OP_breg10:
23221     case DW_OP_breg11:
23222     case DW_OP_breg12:
23223     case DW_OP_breg13:
23224     case DW_OP_breg14:
23225     case DW_OP_breg15:
23226     case DW_OP_breg16:
23227     case DW_OP_breg17:
23228     case DW_OP_breg18:
23229     case DW_OP_breg19:
23230     case DW_OP_breg20:
23231     case DW_OP_breg21:
23232     case DW_OP_breg22:
23233     case DW_OP_breg23:
23234     case DW_OP_breg24:
23235     case DW_OP_breg25:
23236     case DW_OP_breg26:
23237     case DW_OP_breg27:
23238     case DW_OP_breg28:
23239     case DW_OP_breg29:
23240     case DW_OP_breg30:
23241     case DW_OP_breg31:
23242     case DW_OP_regx:
23243     case DW_OP_fbreg:
23244     case DW_OP_piece:
23245     case DW_OP_deref_size:
23246     case DW_OP_xderef_size:
23247       return valx1->v.val_int == valy1->v.val_int;
23248     case DW_OP_skip:
23249     case DW_OP_bra:
23250       gcc_assert (valx1->val_class == dw_val_class_loc
23251                   && valy1->val_class == dw_val_class_loc
23252                   && x->dw_loc_addr == y->dw_loc_addr);
23253       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23254     case DW_OP_implicit_value:
23255       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23256           || valx2->val_class != valy2->val_class)
23257         return false;
23258       switch (valx2->val_class)
23259         {
23260         case dw_val_class_const:
23261           return valx2->v.val_int == valy2->v.val_int;
23262         case dw_val_class_vec:
23263           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23264                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23265                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23266                             valx2->v.val_vec.elt_size
23267                             * valx2->v.val_vec.length) == 0;
23268         case dw_val_class_const_double:
23269           return valx2->v.val_double.low == valy2->v.val_double.low
23270                  && valx2->v.val_double.high == valy2->v.val_double.high;
23271         case dw_val_class_addr:
23272           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23273         default:
23274           gcc_unreachable ();
23275         }
23276     case DW_OP_bregx:
23277     case DW_OP_bit_piece:
23278       return valx1->v.val_int == valy1->v.val_int
23279              && valx2->v.val_int == valy2->v.val_int;
23280     case DW_OP_addr:
23281     hash_addr:
23282       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23283     case DW_OP_GNU_implicit_pointer:
23284       return valx1->val_class == dw_val_class_die_ref
23285              && valx1->val_class == valy1->val_class
23286              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23287              && valx2->v.val_int == valy2->v.val_int;
23288     default:
23289       /* Other codes have no operands.  */
23290       return true;
23291     }
23292 }
23293
23294 /* Return true if DWARF location expressions X and Y are the same.  */
23295
23296 static inline bool
23297 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23298 {
23299   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23300     if (x->dw_loc_opc != y->dw_loc_opc
23301         || x->dtprel != y->dtprel
23302         || !compare_loc_operands (x, y))
23303       break;
23304   return x == NULL && y == NULL;
23305 }
23306
23307 /* Return precomputed hash of location list X.  */
23308
23309 static hashval_t
23310 loc_list_hash (const void *x)
23311 {
23312   return ((const struct dw_loc_list_struct *) x)->hash;
23313 }
23314
23315 /* Return 1 if location lists X and Y are the same.  */
23316
23317 static int
23318 loc_list_eq (const void *x, const void *y)
23319 {
23320   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23321   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23322   if (a == b)
23323     return 1;
23324   if (a->hash != b->hash)
23325     return 0;
23326   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23327     if (strcmp (a->begin, b->begin) != 0
23328         || strcmp (a->end, b->end) != 0
23329         || (a->section == NULL) != (b->section == NULL)
23330         || (a->section && strcmp (a->section, b->section) != 0)
23331         || !compare_locs (a->expr, b->expr))
23332       break;
23333   return a == NULL && b == NULL;
23334 }
23335
23336 /* Recursively optimize location lists referenced from DIE
23337    children and share them whenever possible.  */
23338
23339 static void
23340 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23341 {
23342   dw_die_ref c;
23343   dw_attr_ref a;
23344   unsigned ix;
23345   void **slot;
23346
23347   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23348     if (AT_class (a) == dw_val_class_loc_list)
23349       {
23350         dw_loc_list_ref list = AT_loc_list (a);
23351         /* TODO: perform some optimizations here, before hashing
23352            it and storing into the hash table.  */
23353         hash_loc_list (list);
23354         slot = htab_find_slot_with_hash (htab, list, list->hash,
23355                                          INSERT);
23356         if (*slot == NULL)
23357           *slot = (void *) list;
23358         else
23359           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23360       }
23361
23362   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23363 }
23364
23365 /* Optimize location lists referenced from DIE
23366    children and share them whenever possible.  */
23367
23368 static void
23369 optimize_location_lists (dw_die_ref die)
23370 {
23371   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23372   optimize_location_lists_1 (die, htab);
23373   htab_delete (htab);
23374 }
23375 \f
23376 /* Output stuff that dwarf requires at the end of every file,
23377    and generate the DWARF-2 debugging info.  */
23378
23379 static void
23380 dwarf2out_finish (const char *filename)
23381 {
23382   limbo_die_node *node, *next_node;
23383   comdat_type_node *ctnode;
23384   htab_t comdat_type_table;
23385   unsigned int i;
23386
23387   gen_scheduled_generic_parms_dies ();
23388   gen_remaining_tmpl_value_param_die_attribute ();
23389
23390   /* Add the name for the main input file now.  We delayed this from
23391      dwarf2out_init to avoid complications with PCH.  */
23392   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23393   if (!IS_ABSOLUTE_PATH (filename))
23394     add_comp_dir_attribute (comp_unit_die ());
23395   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23396     {
23397       bool p = false;
23398       htab_traverse (file_table, file_table_relative_p, &p);
23399       if (p)
23400         add_comp_dir_attribute (comp_unit_die ());
23401     }
23402
23403   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23404     {
23405       add_location_or_const_value_attribute (
23406         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23407         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23408         DW_AT_location);
23409     }
23410
23411   /* Traverse the limbo die list, and add parent/child links.  The only
23412      dies without parents that should be here are concrete instances of
23413      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23414      For concrete instances, we can get the parent die from the abstract
23415      instance.  */
23416   for (node = limbo_die_list; node; node = next_node)
23417     {
23418       dw_die_ref die = node->die;
23419       next_node = node->next;
23420
23421       if (die->die_parent == NULL)
23422         {
23423           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23424
23425           if (origin)
23426             add_child_die (origin->die_parent, die);
23427           else if (is_cu_die (die))
23428             ;
23429           else if (seen_error ())
23430             /* It's OK to be confused by errors in the input.  */
23431             add_child_die (comp_unit_die (), die);
23432           else
23433             {
23434               /* In certain situations, the lexical block containing a
23435                  nested function can be optimized away, which results
23436                  in the nested function die being orphaned.  Likewise
23437                  with the return type of that nested function.  Force
23438                  this to be a child of the containing function.
23439
23440                  It may happen that even the containing function got fully
23441                  inlined and optimized out.  In that case we are lost and
23442                  assign the empty child.  This should not be big issue as
23443                  the function is likely unreachable too.  */
23444               tree context = NULL_TREE;
23445
23446               gcc_assert (node->created_for);
23447
23448               if (DECL_P (node->created_for))
23449                 context = DECL_CONTEXT (node->created_for);
23450               else if (TYPE_P (node->created_for))
23451                 context = TYPE_CONTEXT (node->created_for);
23452
23453               gcc_assert (context
23454                           && (TREE_CODE (context) == FUNCTION_DECL
23455                               || TREE_CODE (context) == NAMESPACE_DECL));
23456
23457               origin = lookup_decl_die (context);
23458               if (origin)
23459                 add_child_die (origin, die);
23460               else
23461                 add_child_die (comp_unit_die (), die);
23462             }
23463         }
23464     }
23465
23466   limbo_die_list = NULL;
23467
23468   resolve_addr (comp_unit_die ());
23469
23470   for (node = deferred_asm_name; node; node = node->next)
23471     {
23472       tree decl = node->created_for;
23473       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23474         {
23475           add_linkage_attr (node->die, decl);
23476           move_linkage_attr (node->die);
23477         }
23478     }
23479
23480   deferred_asm_name = NULL;
23481
23482   /* Walk through the list of incomplete types again, trying once more to
23483      emit full debugging info for them.  */
23484   retry_incomplete_types ();
23485
23486   if (flag_eliminate_unused_debug_types)
23487     prune_unused_types ();
23488
23489   /* Generate separate CUs for each of the include files we've seen.
23490      They will go into limbo_die_list.  */
23491   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23492     break_out_includes (comp_unit_die ());
23493
23494   /* Generate separate COMDAT sections for type DIEs. */
23495   if (dwarf_version >= 4)
23496     {
23497       break_out_comdat_types (comp_unit_die ());
23498
23499       /* Each new type_unit DIE was added to the limbo die list when created.
23500          Since these have all been added to comdat_type_list, clear the
23501          limbo die list.  */
23502       limbo_die_list = NULL;
23503
23504       /* For each new comdat type unit, copy declarations for incomplete
23505          types to make the new unit self-contained (i.e., no direct
23506          references to the main compile unit).  */
23507       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23508         copy_decls_for_unworthy_types (ctnode->root_die);
23509       copy_decls_for_unworthy_types (comp_unit_die ());
23510
23511       /* In the process of copying declarations from one unit to another,
23512          we may have left some declarations behind that are no longer
23513          referenced.  Prune them.  */
23514       prune_unused_types ();
23515     }
23516
23517   /* Traverse the DIE's and add add sibling attributes to those DIE's
23518      that have children.  */
23519   add_sibling_attributes (comp_unit_die ());
23520   for (node = limbo_die_list; node; node = node->next)
23521     add_sibling_attributes (node->die);
23522   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23523     add_sibling_attributes (ctnode->root_die);
23524
23525   /* Output a terminator label for the .text section.  */
23526   switch_to_section (text_section);
23527   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23528   if (cold_text_section)
23529     {
23530       switch_to_section (cold_text_section);
23531       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23532     }
23533
23534   /* We can only use the low/high_pc attributes if all of the code was
23535      in .text.  */
23536   if (!have_multiple_function_sections 
23537       || (dwarf_version < 3 && dwarf_strict))
23538     {
23539       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23540       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23541     }
23542
23543   else
23544     {
23545       unsigned fde_idx = 0;
23546       bool range_list_added = false;
23547
23548       /* We need to give .debug_loc and .debug_ranges an appropriate
23549          "base address".  Use zero so that these addresses become
23550          absolute.  Historically, we've emitted the unexpected
23551          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23552          Emit both to give time for other tools to adapt.  */
23553       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23554       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23555
23556       if (text_section_used)
23557         add_ranges_by_labels (comp_unit_die (), text_section_label,
23558                               text_end_label, &range_list_added);
23559       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23560         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23561                               cold_end_label, &range_list_added);
23562
23563       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23564         {
23565           dw_fde_ref fde = &fde_table[fde_idx];
23566
23567           if (fde->dw_fde_switched_sections)
23568             {
23569               if (!fde->in_std_section)
23570                 add_ranges_by_labels (comp_unit_die (),
23571                                       fde->dw_fde_hot_section_label,
23572                                       fde->dw_fde_hot_section_end_label,
23573                                       &range_list_added);
23574               if (!fde->cold_in_std_section)
23575                 add_ranges_by_labels (comp_unit_die (),
23576                                       fde->dw_fde_unlikely_section_label,
23577                                       fde->dw_fde_unlikely_section_end_label,
23578                                       &range_list_added);
23579             }
23580           else if (!fde->in_std_section)
23581             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23582                                   fde->dw_fde_end, &range_list_added);
23583         }
23584
23585       if (range_list_added)
23586         add_ranges (NULL);
23587     }
23588
23589   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23590     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23591                     debug_line_section_label);
23592
23593   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23594     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23595
23596   if (have_location_lists)
23597     optimize_location_lists (comp_unit_die ());
23598
23599   /* Output all of the compilation units.  We put the main one last so that
23600      the offsets are available to output_pubnames.  */
23601   for (node = limbo_die_list; node; node = node->next)
23602     output_comp_unit (node->die, 0);
23603
23604   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23605   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23606     {
23607       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23608
23609       /* Don't output duplicate types.  */
23610       if (*slot != HTAB_EMPTY_ENTRY)
23611         continue;
23612
23613       /* Add a pointer to the line table for the main compilation unit
23614          so that the debugger can make sense of DW_AT_decl_file
23615          attributes.  */
23616       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23617         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23618                         debug_line_section_label);
23619
23620       output_comdat_type_unit (ctnode);
23621       *slot = ctnode;
23622     }
23623   htab_delete (comdat_type_table);
23624
23625   /* Output the main compilation unit if non-empty or if .debug_macinfo
23626      will be emitted.  */
23627   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23628
23629   /* Output the abbreviation table.  */
23630   switch_to_section (debug_abbrev_section);
23631   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23632   output_abbrev_section ();
23633
23634   /* Output location list section if necessary.  */
23635   if (have_location_lists)
23636     {
23637       /* Output the location lists info.  */
23638       switch_to_section (debug_loc_section);
23639       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23640                                    DEBUG_LOC_SECTION_LABEL, 0);
23641       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23642       output_location_lists (comp_unit_die ());
23643     }
23644
23645   /* Output public names table if necessary.  */
23646   if (!VEC_empty (pubname_entry, pubname_table))
23647     {
23648       gcc_assert (info_section_emitted);
23649       switch_to_section (debug_pubnames_section);
23650       output_pubnames (pubname_table);
23651     }
23652
23653   /* Output public types table if necessary.  */
23654   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23655      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23656      simply won't look for the section.  */
23657   if (!VEC_empty (pubname_entry, pubtype_table))
23658     {
23659       bool empty = false;
23660       
23661       if (flag_eliminate_unused_debug_types)
23662         {
23663           /* The pubtypes table might be emptied by pruning unused items.  */
23664           unsigned i;
23665           pubname_ref p;
23666           empty = true;
23667           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23668             if (p->die->die_offset != 0)
23669               {
23670                 empty = false;
23671                 break;
23672               }
23673         }
23674       if (!empty)
23675         {
23676           gcc_assert (info_section_emitted);
23677           switch_to_section (debug_pubtypes_section);
23678           output_pubnames (pubtype_table);
23679         }
23680     }
23681
23682   /* Output the address range information.  We only put functions in the arange
23683      table, so don't write it out if we don't have any.  */
23684   if ((text_section_used || cold_text_section_used || arange_table_in_use)
23685       && info_section_emitted)
23686     {
23687       switch_to_section (debug_aranges_section);
23688       output_aranges ();
23689     }
23690
23691   /* Output ranges section if necessary.  */
23692   if (ranges_table_in_use)
23693     {
23694       switch_to_section (debug_ranges_section);
23695       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23696       output_ranges ();
23697     }
23698
23699   /* Output the source line correspondence table.  We must do this
23700      even if there is no line information.  Otherwise, on an empty
23701      translation unit, we will generate a present, but empty,
23702      .debug_info section.  IRIX 6.5 `nm' will then complain when
23703      examining the file.  This is done late so that any filenames
23704      used by the debug_info section are marked as 'used'.  */
23705   switch_to_section (debug_line_section);
23706   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23707   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23708     output_line_info ();
23709
23710   /* Have to end the macro section.  */
23711   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23712     {
23713       switch_to_section (debug_macinfo_section);
23714       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23715       if (!VEC_empty (macinfo_entry, macinfo_table))
23716         output_macinfo ();
23717       dw2_asm_output_data (1, 0, "End compilation unit");
23718     }
23719
23720   /* If we emitted any DW_FORM_strp form attribute, output the string
23721      table too.  */
23722   if (debug_str_hash)
23723     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23724 }
23725
23726 #include "gt-dwarf2out.h"