OSDN Git Service

PR debug/48176
[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} 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 (postinc <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           offset = GET_MODE_SIZE (GET_MODE (dest));
2595           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2596             offset = -offset;
2597
2598           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2599                        == STACK_POINTER_REGNUM)
2600                       && cfa_store.reg == STACK_POINTER_REGNUM);
2601
2602           cfa_store.offset += offset;
2603
2604           /* Rule 18: If stack is aligned, we will use FP as a
2605              reference to represent the address of the stored
2606              regiser.  */
2607           if (fde
2608               && fde->stack_realign
2609               && src == hard_frame_pointer_rtx)
2610             {
2611               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2612               cfa_store.offset = 0;
2613             }
2614
2615           if (cfa.reg == STACK_POINTER_REGNUM)
2616             cfa.offset = cfa_store.offset;
2617
2618           offset = -cfa_store.offset;
2619           break;
2620
2621           /* Rule 12 */
2622           /* With an offset.  */
2623         case PLUS:
2624         case MINUS:
2625         case LO_SUM:
2626           {
2627             int regno;
2628
2629             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2630                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2631             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2632             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2633               offset = -offset;
2634
2635             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2636
2637             if (cfa.reg == (unsigned) regno)
2638               offset -= cfa.offset;
2639             else if (cfa_store.reg == (unsigned) regno)
2640               offset -= cfa_store.offset;
2641             else
2642               {
2643                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2644                 offset -= cfa_temp.offset;
2645               }
2646           }
2647           break;
2648
2649           /* Rule 13 */
2650           /* Without an offset.  */
2651         case REG:
2652           {
2653             int regno = REGNO (XEXP (dest, 0));
2654
2655             if (cfa.reg == (unsigned) regno)
2656               offset = -cfa.offset;
2657             else if (cfa_store.reg == (unsigned) regno)
2658               offset = -cfa_store.offset;
2659             else
2660               {
2661                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2662                 offset = -cfa_temp.offset;
2663               }
2664           }
2665           break;
2666
2667           /* Rule 14 */
2668         case POST_INC:
2669           gcc_assert (cfa_temp.reg
2670                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2671           offset = -cfa_temp.offset;
2672           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2673           break;
2674
2675         default:
2676           gcc_unreachable ();
2677         }
2678
2679         /* Rule 17 */
2680         /* If the source operand of this MEM operation is not a
2681            register, basically the source is return address.  Here
2682            we only care how much stack grew and we don't save it.  */
2683       if (!REG_P (src))
2684         break;
2685
2686       if (REGNO (src) != STACK_POINTER_REGNUM
2687           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2688           && (unsigned) REGNO (src) == cfa.reg)
2689         {
2690           /* We're storing the current CFA reg into the stack.  */
2691
2692           if (cfa.offset == 0)
2693             {
2694               /* Rule 19 */
2695               /* If stack is aligned, putting CFA reg into stack means
2696                  we can no longer use reg + offset to represent CFA.
2697                  Here we use DW_CFA_def_cfa_expression instead.  The
2698                  result of this expression equals to the original CFA
2699                  value.  */
2700               if (fde
2701                   && fde->stack_realign
2702                   && cfa.indirect == 0
2703                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2704                 {
2705                   dw_cfa_location cfa_exp;
2706
2707                   gcc_assert (fde->drap_reg == cfa.reg);
2708
2709                   cfa_exp.indirect = 1;
2710                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2711                   cfa_exp.base_offset = offset;
2712                   cfa_exp.offset = 0;
2713
2714                   fde->drap_reg_saved = 1;
2715
2716                   def_cfa_1 (label, &cfa_exp);
2717                   break;
2718                 }
2719
2720               /* If the source register is exactly the CFA, assume
2721                  we're saving SP like any other register; this happens
2722                  on the ARM.  */
2723               def_cfa_1 (label, &cfa);
2724               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2725               break;
2726             }
2727           else
2728             {
2729               /* Otherwise, we'll need to look in the stack to
2730                  calculate the CFA.  */
2731               rtx x = XEXP (dest, 0);
2732
2733               if (!REG_P (x))
2734                 x = XEXP (x, 0);
2735               gcc_assert (REG_P (x));
2736
2737               cfa.reg = REGNO (x);
2738               cfa.base_offset = offset;
2739               cfa.indirect = 1;
2740               def_cfa_1 (label, &cfa);
2741               break;
2742             }
2743         }
2744
2745       def_cfa_1 (label, &cfa);
2746       {
2747         span = targetm.dwarf_register_span (src);
2748
2749         if (!span)
2750           queue_reg_save (label, src, NULL_RTX, offset);
2751         else
2752           {
2753             /* We have a PARALLEL describing where the contents of SRC
2754                live.  Queue register saves for each piece of the
2755                PARALLEL.  */
2756             int par_index;
2757             int limit;
2758             HOST_WIDE_INT span_offset = offset;
2759
2760             gcc_assert (GET_CODE (span) == PARALLEL);
2761
2762             limit = XVECLEN (span, 0);
2763             for (par_index = 0; par_index < limit; par_index++)
2764               {
2765                 rtx elem = XVECEXP (span, 0, par_index);
2766
2767                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2768                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2769               }
2770           }
2771       }
2772       break;
2773
2774     default:
2775       gcc_unreachable ();
2776     }
2777 }
2778
2779 /* Record call frame debugging information for INSN, which either
2780    sets SP or FP (adjusting how we calculate the frame address) or saves a
2781    register to the stack.  If INSN is NULL_RTX, initialize our state.
2782
2783    If AFTER_P is false, we're being called before the insn is emitted,
2784    otherwise after.  Call instructions get invoked twice.  */
2785
2786 void
2787 dwarf2out_frame_debug (rtx insn, bool after_p)
2788 {
2789   const char *label;
2790   rtx note, n;
2791   bool handled_one = false;
2792
2793   if (insn == NULL_RTX)
2794     {
2795       size_t i;
2796
2797       /* Flush any queued register saves.  */
2798       dwarf2out_flush_queued_reg_saves ();
2799
2800       /* Set up state for generating call frame debug info.  */
2801       lookup_cfa (&cfa);
2802       gcc_assert (cfa.reg
2803                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2804
2805       cfa.reg = STACK_POINTER_REGNUM;
2806       cfa_store = cfa;
2807       cfa_temp.reg = -1;
2808       cfa_temp.offset = 0;
2809
2810       for (i = 0; i < num_regs_saved_in_regs; i++)
2811         {
2812           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2813           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2814         }
2815       num_regs_saved_in_regs = 0;
2816
2817       if (barrier_args_size)
2818         {
2819           XDELETEVEC (barrier_args_size);
2820           barrier_args_size = NULL;
2821         }
2822       return;
2823     }
2824
2825   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2826     dwarf2out_flush_queued_reg_saves ();
2827
2828   if (!RTX_FRAME_RELATED_P (insn))
2829     {
2830       /* ??? This should be done unconditionally since stack adjustments
2831          matter if the stack pointer is not the CFA register anymore but
2832          is still used to save registers.  */
2833       if (!ACCUMULATE_OUTGOING_ARGS)
2834         dwarf2out_notice_stack_adjust (insn, after_p);
2835       return;
2836     }
2837
2838   label = dwarf2out_cfi_label (false);
2839   any_cfis_emitted = false;
2840
2841   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2842     switch (REG_NOTE_KIND (note))
2843       {
2844       case REG_FRAME_RELATED_EXPR:
2845         insn = XEXP (note, 0);
2846         goto found;
2847
2848       case REG_CFA_DEF_CFA:
2849         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2850         handled_one = true;
2851         break;
2852
2853       case REG_CFA_ADJUST_CFA:
2854         n = XEXP (note, 0);
2855         if (n == NULL)
2856           {
2857             n = PATTERN (insn);
2858             if (GET_CODE (n) == PARALLEL)
2859               n = XVECEXP (n, 0, 0);
2860           }
2861         dwarf2out_frame_debug_adjust_cfa (n, label);
2862         handled_one = true;
2863         break;
2864
2865       case REG_CFA_OFFSET:
2866         n = XEXP (note, 0);
2867         if (n == NULL)
2868           n = single_set (insn);
2869         dwarf2out_frame_debug_cfa_offset (n, label);
2870         handled_one = true;
2871         break;
2872
2873       case REG_CFA_REGISTER:
2874         n = XEXP (note, 0);
2875         if (n == NULL)
2876           {
2877             n = PATTERN (insn);
2878             if (GET_CODE (n) == PARALLEL)
2879               n = XVECEXP (n, 0, 0);
2880           }
2881         dwarf2out_frame_debug_cfa_register (n, label);
2882         handled_one = true;
2883         break;
2884
2885       case REG_CFA_EXPRESSION:
2886         n = XEXP (note, 0);
2887         if (n == NULL)
2888           n = single_set (insn);
2889         dwarf2out_frame_debug_cfa_expression (n, label);
2890         handled_one = true;
2891         break;
2892
2893       case REG_CFA_RESTORE:
2894         n = XEXP (note, 0);
2895         if (n == NULL)
2896           {
2897             n = PATTERN (insn);
2898             if (GET_CODE (n) == PARALLEL)
2899               n = XVECEXP (n, 0, 0);
2900             n = XEXP (n, 0);
2901           }
2902         dwarf2out_frame_debug_cfa_restore (n, label);
2903         handled_one = true;
2904         break;
2905
2906       case REG_CFA_SET_VDRAP:
2907         n = XEXP (note, 0);
2908         if (REG_P (n))
2909           {
2910             dw_fde_ref fde = current_fde ();
2911             if (fde)
2912               {
2913                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2914                 if (REG_P (n))
2915                   fde->vdrap_reg = REGNO (n);
2916               }
2917           }
2918         handled_one = true;
2919         break;
2920
2921       default:
2922         break;
2923       }
2924   if (handled_one)
2925     {
2926       if (any_cfis_emitted)
2927         dwarf2out_flush_queued_reg_saves ();
2928       return;
2929     }
2930
2931   insn = PATTERN (insn);
2932  found:
2933   dwarf2out_frame_debug_expr (insn, label);
2934
2935   /* Check again.  A parallel can save and update the same register.
2936      We could probably check just once, here, but this is safer than
2937      removing the check above.  */
2938   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2939     dwarf2out_flush_queued_reg_saves ();
2940 }
2941
2942 /* Determine if we need to save and restore CFI information around this
2943    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2944    we do need to save/restore, then emit the save now, and insert a
2945    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2946
2947 void
2948 dwarf2out_cfi_begin_epilogue (rtx insn)
2949 {
2950   bool saw_frp = false;
2951   rtx i;
2952
2953   /* Scan forward to the return insn, noticing if there are possible
2954      frame related insns.  */
2955   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2956     {
2957       if (!INSN_P (i))
2958         continue;
2959
2960       /* Look for both regular and sibcalls to end the block.  */
2961       if (returnjump_p (i))
2962         break;
2963       if (CALL_P (i) && SIBLING_CALL_P (i))
2964         break;
2965
2966       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2967         {
2968           int idx;
2969           rtx seq = PATTERN (i);
2970
2971           if (returnjump_p (XVECEXP (seq, 0, 0)))
2972             break;
2973           if (CALL_P (XVECEXP (seq, 0, 0))
2974               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2975             break;
2976
2977           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2978             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2979               saw_frp = true;
2980         }
2981
2982       if (RTX_FRAME_RELATED_P (i))
2983         saw_frp = true;
2984     }
2985
2986   /* If the port doesn't emit epilogue unwind info, we don't need a
2987      save/restore pair.  */
2988   if (!saw_frp)
2989     return;
2990
2991   /* Otherwise, search forward to see if the return insn was the last
2992      basic block of the function.  If so, we don't need save/restore.  */
2993   gcc_assert (i != NULL);
2994   i = next_real_insn (i);
2995   if (i == NULL)
2996     return;
2997
2998   /* Insert the restore before that next real insn in the stream, and before
2999      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3000      properly nested.  This should be after any label or alignment.  This
3001      will be pushed into the CFI stream by the function below.  */
3002   while (1)
3003     {
3004       rtx p = PREV_INSN (i);
3005       if (!NOTE_P (p))
3006         break;
3007       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3008         break;
3009       i = p;
3010     }
3011   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3012
3013   emit_cfa_remember = true;
3014
3015   /* And emulate the state save.  */
3016   gcc_assert (!cfa_remember.in_use);
3017   cfa_remember = cfa;
3018   cfa_remember.in_use = 1;
3019 }
3020
3021 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3022    required.  */
3023
3024 void
3025 dwarf2out_frame_debug_restore_state (void)
3026 {
3027   dw_cfi_ref cfi = new_cfi ();
3028   const char *label = dwarf2out_cfi_label (false);
3029
3030   cfi->dw_cfi_opc = DW_CFA_restore_state;
3031   add_fde_cfi (label, cfi);
3032
3033   gcc_assert (cfa_remember.in_use);
3034   cfa = cfa_remember;
3035   cfa_remember.in_use = 0;
3036 }
3037
3038 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3039 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3040  (enum dwarf_call_frame_info cfi);
3041
3042 static enum dw_cfi_oprnd_type
3043 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3044 {
3045   switch (cfi)
3046     {
3047     case DW_CFA_nop:
3048     case DW_CFA_GNU_window_save:
3049     case DW_CFA_remember_state:
3050     case DW_CFA_restore_state:
3051       return dw_cfi_oprnd_unused;
3052
3053     case DW_CFA_set_loc:
3054     case DW_CFA_advance_loc1:
3055     case DW_CFA_advance_loc2:
3056     case DW_CFA_advance_loc4:
3057     case DW_CFA_MIPS_advance_loc8:
3058       return dw_cfi_oprnd_addr;
3059
3060     case DW_CFA_offset:
3061     case DW_CFA_offset_extended:
3062     case DW_CFA_def_cfa:
3063     case DW_CFA_offset_extended_sf:
3064     case DW_CFA_def_cfa_sf:
3065     case DW_CFA_restore:
3066     case DW_CFA_restore_extended:
3067     case DW_CFA_undefined:
3068     case DW_CFA_same_value:
3069     case DW_CFA_def_cfa_register:
3070     case DW_CFA_register:
3071     case DW_CFA_expression:
3072       return dw_cfi_oprnd_reg_num;
3073
3074     case DW_CFA_def_cfa_offset:
3075     case DW_CFA_GNU_args_size:
3076     case DW_CFA_def_cfa_offset_sf:
3077       return dw_cfi_oprnd_offset;
3078
3079     case DW_CFA_def_cfa_expression:
3080       return dw_cfi_oprnd_loc;
3081
3082     default:
3083       gcc_unreachable ();
3084     }
3085 }
3086
3087 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3088 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3089  (enum dwarf_call_frame_info cfi);
3090
3091 static enum dw_cfi_oprnd_type
3092 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3093 {
3094   switch (cfi)
3095     {
3096     case DW_CFA_def_cfa:
3097     case DW_CFA_def_cfa_sf:
3098     case DW_CFA_offset:
3099     case DW_CFA_offset_extended_sf:
3100     case DW_CFA_offset_extended:
3101       return dw_cfi_oprnd_offset;
3102
3103     case DW_CFA_register:
3104       return dw_cfi_oprnd_reg_num;
3105
3106     case DW_CFA_expression:
3107       return dw_cfi_oprnd_loc;
3108
3109     default:
3110       return dw_cfi_oprnd_unused;
3111     }
3112 }
3113
3114 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3115    switch to the data section instead, and write out a synthetic start label
3116    for collect2 the first time around.  */
3117
3118 static void
3119 switch_to_eh_frame_section (bool back)
3120 {
3121   tree label;
3122
3123 #ifdef EH_FRAME_SECTION_NAME
3124   if (eh_frame_section == 0)
3125     {
3126       int flags;
3127
3128       if (EH_TABLES_CAN_BE_READ_ONLY)
3129         {
3130           int fde_encoding;
3131           int per_encoding;
3132           int lsda_encoding;
3133
3134           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3135                                                        /*global=*/0);
3136           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3137                                                        /*global=*/1);
3138           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3139                                                         /*global=*/0);
3140           flags = ((! flag_pic
3141                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3142                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3143                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3144                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3145                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3146                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3147                    ? 0 : SECTION_WRITE);
3148         }
3149       else
3150         flags = SECTION_WRITE;
3151       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3152     }
3153 #endif /* EH_FRAME_SECTION_NAME */
3154
3155   if (eh_frame_section)
3156     switch_to_section (eh_frame_section);
3157   else
3158     {
3159       /* We have no special eh_frame section.  Put the information in
3160          the data section and emit special labels to guide collect2.  */
3161       switch_to_section (data_section);
3162
3163       if (!back)
3164         {
3165           label = get_file_function_name ("F");
3166           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3167           targetm.asm_out.globalize_label (asm_out_file,
3168                                            IDENTIFIER_POINTER (label));
3169           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3170         }
3171     }
3172 }
3173
3174 /* Switch [BACK] to the eh or debug frame table section, depending on
3175    FOR_EH.  */
3176
3177 static void
3178 switch_to_frame_table_section (int for_eh, bool back)
3179 {
3180   if (for_eh)
3181     switch_to_eh_frame_section (back);
3182   else
3183     {
3184       if (!debug_frame_section)
3185         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3186                                            SECTION_DEBUG, NULL);
3187       switch_to_section (debug_frame_section);
3188     }
3189 }
3190
3191 /* Output a Call Frame Information opcode and its operand(s).  */
3192
3193 static void
3194 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3195 {
3196   unsigned long r;
3197   HOST_WIDE_INT off;
3198
3199   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3200     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3201                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3202                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3203                          ((unsigned HOST_WIDE_INT)
3204                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3205   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3206     {
3207       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3208       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3209                            "DW_CFA_offset, column %#lx", r);
3210       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3211       dw2_asm_output_data_uleb128 (off, NULL);
3212     }
3213   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3214     {
3215       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3216       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3217                            "DW_CFA_restore, column %#lx", r);
3218     }
3219   else
3220     {
3221       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3222                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3223
3224       switch (cfi->dw_cfi_opc)
3225         {
3226         case DW_CFA_set_loc:
3227           if (for_eh)
3228             dw2_asm_output_encoded_addr_rtx (
3229                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3230                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3231                 false, NULL);
3232           else
3233             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3234                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3235           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3236           break;
3237
3238         case DW_CFA_advance_loc1:
3239           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3240                                 fde->dw_fde_current_label, NULL);
3241           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3242           break;
3243
3244         case DW_CFA_advance_loc2:
3245           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3246                                 fde->dw_fde_current_label, NULL);
3247           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3248           break;
3249
3250         case DW_CFA_advance_loc4:
3251           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3252                                 fde->dw_fde_current_label, NULL);
3253           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3254           break;
3255
3256         case DW_CFA_MIPS_advance_loc8:
3257           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3258                                 fde->dw_fde_current_label, NULL);
3259           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3260           break;
3261
3262         case DW_CFA_offset_extended:
3263           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3264           dw2_asm_output_data_uleb128 (r, NULL);
3265           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3266           dw2_asm_output_data_uleb128 (off, NULL);
3267           break;
3268
3269         case DW_CFA_def_cfa:
3270           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3271           dw2_asm_output_data_uleb128 (r, NULL);
3272           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3273           break;
3274
3275         case DW_CFA_offset_extended_sf:
3276           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3277           dw2_asm_output_data_uleb128 (r, NULL);
3278           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3279           dw2_asm_output_data_sleb128 (off, NULL);
3280           break;
3281
3282         case DW_CFA_def_cfa_sf:
3283           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3284           dw2_asm_output_data_uleb128 (r, NULL);
3285           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3286           dw2_asm_output_data_sleb128 (off, NULL);
3287           break;
3288
3289         case DW_CFA_restore_extended:
3290         case DW_CFA_undefined:
3291         case DW_CFA_same_value:
3292         case DW_CFA_def_cfa_register:
3293           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3294           dw2_asm_output_data_uleb128 (r, NULL);
3295           break;
3296
3297         case DW_CFA_register:
3298           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3299           dw2_asm_output_data_uleb128 (r, NULL);
3300           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3301           dw2_asm_output_data_uleb128 (r, NULL);
3302           break;
3303
3304         case DW_CFA_def_cfa_offset:
3305         case DW_CFA_GNU_args_size:
3306           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3307           break;
3308
3309         case DW_CFA_def_cfa_offset_sf:
3310           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3311           dw2_asm_output_data_sleb128 (off, NULL);
3312           break;
3313
3314         case DW_CFA_GNU_window_save:
3315           break;
3316
3317         case DW_CFA_def_cfa_expression:
3318         case DW_CFA_expression:
3319           output_cfa_loc (cfi, for_eh);
3320           break;
3321
3322         case DW_CFA_GNU_negative_offset_extended:
3323           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3324           gcc_unreachable ();
3325
3326         default:
3327           break;
3328         }
3329     }
3330 }
3331
3332 /* Similar, but do it via assembler directives instead.  */
3333
3334 static void
3335 output_cfi_directive (dw_cfi_ref cfi)
3336 {
3337   unsigned long r, r2;
3338
3339   switch (cfi->dw_cfi_opc)
3340     {
3341     case DW_CFA_advance_loc:
3342     case DW_CFA_advance_loc1:
3343     case DW_CFA_advance_loc2:
3344     case DW_CFA_advance_loc4:
3345     case DW_CFA_MIPS_advance_loc8:
3346     case DW_CFA_set_loc:
3347       /* Should only be created by add_fde_cfi in a code path not
3348          followed when emitting via directives.  The assembler is
3349          going to take care of this for us.  */
3350       gcc_unreachable ();
3351
3352     case DW_CFA_offset:
3353     case DW_CFA_offset_extended:
3354     case DW_CFA_offset_extended_sf:
3355       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3356       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3357                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3358       break;
3359
3360     case DW_CFA_restore:
3361     case DW_CFA_restore_extended:
3362       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3363       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3364       break;
3365
3366     case DW_CFA_undefined:
3367       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3368       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3369       break;
3370
3371     case DW_CFA_same_value:
3372       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3373       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3374       break;
3375
3376     case DW_CFA_def_cfa:
3377     case DW_CFA_def_cfa_sf:
3378       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3379       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3380                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3381       break;
3382
3383     case DW_CFA_def_cfa_register:
3384       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3385       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3386       break;
3387
3388     case DW_CFA_register:
3389       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3390       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3391       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3392       break;
3393
3394     case DW_CFA_def_cfa_offset:
3395     case DW_CFA_def_cfa_offset_sf:
3396       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3397                HOST_WIDE_INT_PRINT_DEC"\n",
3398                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3399       break;
3400
3401     case DW_CFA_remember_state:
3402       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3403       break;
3404     case DW_CFA_restore_state:
3405       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3406       break;
3407
3408     case DW_CFA_GNU_args_size:
3409       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3410       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3411       if (flag_debug_asm)
3412         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3413                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3414       fputc ('\n', asm_out_file);
3415       break;
3416
3417     case DW_CFA_GNU_window_save:
3418       fprintf (asm_out_file, "\t.cfi_window_save\n");
3419       break;
3420
3421     case DW_CFA_def_cfa_expression:
3422     case DW_CFA_expression:
3423       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3424       output_cfa_loc_raw (cfi);
3425       fputc ('\n', asm_out_file);
3426       break;
3427
3428     default:
3429       gcc_unreachable ();
3430     }
3431 }
3432
3433 DEF_VEC_P (dw_cfi_ref);
3434 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3435
3436 /* Output CFIs to bring current FDE to the same state as after executing
3437    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3438    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3439    other arguments to pass to output_cfi.  */
3440
3441 static void
3442 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3443 {
3444   struct dw_cfi_struct cfi_buf;
3445   dw_cfi_ref cfi2;
3446   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3447   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3448   unsigned int len, idx;
3449
3450   for (;; cfi = cfi->dw_cfi_next)
3451     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3452       {
3453       case DW_CFA_advance_loc:
3454       case DW_CFA_advance_loc1:
3455       case DW_CFA_advance_loc2:
3456       case DW_CFA_advance_loc4:
3457       case DW_CFA_MIPS_advance_loc8:
3458       case DW_CFA_set_loc:
3459         /* All advances should be ignored.  */
3460         break;
3461       case DW_CFA_remember_state:
3462         {
3463           dw_cfi_ref args_size = cfi_args_size;
3464
3465           /* Skip everything between .cfi_remember_state and
3466              .cfi_restore_state.  */
3467           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3468             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3469               break;
3470             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3471               args_size = cfi2;
3472             else
3473               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3474
3475           if (cfi2 == NULL)
3476             goto flush_all;
3477           else
3478             {
3479               cfi = cfi2;
3480               cfi_args_size = args_size;
3481             }
3482           break;
3483         }
3484       case DW_CFA_GNU_args_size:
3485         cfi_args_size = cfi;
3486         break;
3487       case DW_CFA_GNU_window_save:
3488         goto flush_all;
3489       case DW_CFA_offset:
3490       case DW_CFA_offset_extended:
3491       case DW_CFA_offset_extended_sf:
3492       case DW_CFA_restore:
3493       case DW_CFA_restore_extended:
3494       case DW_CFA_undefined:
3495       case DW_CFA_same_value:
3496       case DW_CFA_register:
3497       case DW_CFA_val_offset:
3498       case DW_CFA_val_offset_sf:
3499       case DW_CFA_expression:
3500       case DW_CFA_val_expression:
3501       case DW_CFA_GNU_negative_offset_extended:
3502         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3503           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3504                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3505         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3506         break;
3507       case DW_CFA_def_cfa:
3508       case DW_CFA_def_cfa_sf:
3509       case DW_CFA_def_cfa_expression:
3510         cfi_cfa = cfi;
3511         cfi_cfa_offset = cfi;
3512         break;
3513       case DW_CFA_def_cfa_register:
3514         cfi_cfa = cfi;
3515         break;
3516       case DW_CFA_def_cfa_offset:
3517       case DW_CFA_def_cfa_offset_sf:
3518         cfi_cfa_offset = cfi;
3519         break;
3520       case DW_CFA_nop:
3521         gcc_assert (cfi == NULL);
3522       flush_all:
3523         len = VEC_length (dw_cfi_ref, regs);
3524         for (idx = 0; idx < len; idx++)
3525           {
3526             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3527             if (cfi2 != NULL
3528                 && cfi2->dw_cfi_opc != DW_CFA_restore
3529                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3530               {
3531                 if (do_cfi_asm)
3532                   output_cfi_directive (cfi2);
3533                 else
3534                   output_cfi (cfi2, fde, for_eh);
3535               }
3536           }
3537         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3538           {
3539             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3540             cfi_buf = *cfi_cfa;
3541             switch (cfi_cfa_offset->dw_cfi_opc)
3542               {
3543               case DW_CFA_def_cfa_offset:
3544                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3545                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3546                 break;
3547               case DW_CFA_def_cfa_offset_sf:
3548                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3549                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3550                 break;
3551               case DW_CFA_def_cfa:
3552               case DW_CFA_def_cfa_sf:
3553                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3554                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3555                 break;
3556               default:
3557                 gcc_unreachable ();
3558               }
3559             cfi_cfa = &cfi_buf;
3560           }
3561         else if (cfi_cfa_offset)
3562           cfi_cfa = cfi_cfa_offset;
3563         if (cfi_cfa)
3564           {
3565             if (do_cfi_asm)
3566               output_cfi_directive (cfi_cfa);
3567             else
3568               output_cfi (cfi_cfa, fde, for_eh);
3569           }
3570         cfi_cfa = NULL;
3571         cfi_cfa_offset = NULL;
3572         if (cfi_args_size
3573             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3574           {
3575             if (do_cfi_asm)
3576               output_cfi_directive (cfi_args_size);
3577             else
3578               output_cfi (cfi_args_size, fde, for_eh);
3579           }
3580         cfi_args_size = NULL;
3581         if (cfi == NULL)
3582           {
3583             VEC_free (dw_cfi_ref, heap, regs);
3584             return;
3585           }
3586         else if (do_cfi_asm)
3587           output_cfi_directive (cfi);
3588         else
3589           output_cfi (cfi, fde, for_eh);
3590         break;
3591       default:
3592         gcc_unreachable ();
3593     }
3594 }
3595
3596 /* Output one FDE.  */
3597
3598 static void
3599 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3600             char *section_start_label, int fde_encoding, char *augmentation,
3601             bool any_lsda_needed, int lsda_encoding)
3602 {
3603   const char *begin, *end;
3604   static unsigned int j;
3605   char l1[20], l2[20];
3606   dw_cfi_ref cfi;
3607
3608   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3609                                      /* empty */ 0);
3610   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3611                                   for_eh + j);
3612   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3613   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3614   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3615     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3616                          " indicating 64-bit DWARF extension");
3617   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3618                         "FDE Length");
3619   ASM_OUTPUT_LABEL (asm_out_file, l1);
3620
3621   if (for_eh)
3622     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3623   else
3624     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3625                            debug_frame_section, "FDE CIE offset");
3626
3627   if (!fde->dw_fde_switched_sections)
3628     {
3629       begin = fde->dw_fde_begin;
3630       end = fde->dw_fde_end;
3631     }
3632   else
3633     {
3634       /* For the first section, prefer dw_fde_begin over
3635          dw_fde_{hot,cold}_section_label, as the latter
3636          might be separated from the real start of the
3637          function by alignment padding.  */
3638       if (!second)
3639         begin = fde->dw_fde_begin;
3640       else if (fde->dw_fde_switched_cold_to_hot)
3641         begin = fde->dw_fde_hot_section_label;
3642       else
3643         begin = fde->dw_fde_unlikely_section_label;
3644       if (second ^ fde->dw_fde_switched_cold_to_hot)
3645         end = fde->dw_fde_unlikely_section_end_label;
3646       else
3647         end = fde->dw_fde_hot_section_end_label;
3648     }
3649
3650   if (for_eh)
3651     {
3652       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3653       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3654       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3655                                        "FDE initial location");
3656       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3657                             end, begin, "FDE address range");
3658     }
3659   else
3660     {
3661       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3662       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3663     }
3664
3665   if (augmentation[0])
3666     {
3667       if (any_lsda_needed)
3668         {
3669           int size = size_of_encoded_value (lsda_encoding);
3670
3671           if (lsda_encoding == DW_EH_PE_aligned)
3672             {
3673               int offset = (  4         /* Length */
3674                             + 4         /* CIE offset */
3675                             + 2 * size_of_encoded_value (fde_encoding)
3676                             + 1         /* Augmentation size */ );
3677               int pad = -offset & (PTR_SIZE - 1);
3678
3679               size += pad;
3680               gcc_assert (size_of_uleb128 (size) == 1);
3681             }
3682
3683           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3684
3685           if (fde->uses_eh_lsda)
3686             {
3687               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3688                                            fde->funcdef_number);
3689               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3690                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3691                                                false,
3692                                                "Language Specific Data Area");
3693             }
3694           else
3695             {
3696               if (lsda_encoding == DW_EH_PE_aligned)
3697                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3698               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3699                                    "Language Specific Data Area (none)");
3700             }
3701         }
3702       else
3703         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3704     }
3705
3706   /* Loop through the Call Frame Instructions associated with
3707      this FDE.  */
3708   fde->dw_fde_current_label = begin;
3709   if (!fde->dw_fde_switched_sections)
3710     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3711       output_cfi (cfi, fde, for_eh);
3712   else if (!second)
3713     {
3714       if (fde->dw_fde_switch_cfi)
3715         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3716           {
3717             output_cfi (cfi, fde, for_eh);
3718             if (cfi == fde->dw_fde_switch_cfi)
3719               break;
3720           }
3721     }
3722   else
3723     {
3724       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3725
3726       if (fde->dw_fde_switch_cfi)
3727         {
3728           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3729           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3730           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3731           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3732         }
3733       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3734         output_cfi (cfi, fde, for_eh);
3735     }
3736
3737   /* If we are to emit a ref/link from function bodies to their frame tables,
3738      do it now.  This is typically performed to make sure that tables
3739      associated with functions are dragged with them and not discarded in
3740      garbage collecting links. We need to do this on a per function basis to
3741      cope with -ffunction-sections.  */
3742
3743 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3744   /* Switch to the function section, emit the ref to the tables, and
3745      switch *back* into the table section.  */
3746   switch_to_section (function_section (fde->decl));
3747   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3748   switch_to_frame_table_section (for_eh, true);
3749 #endif
3750
3751   /* Pad the FDE out to an address sized boundary.  */
3752   ASM_OUTPUT_ALIGN (asm_out_file,
3753                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3754   ASM_OUTPUT_LABEL (asm_out_file, l2);
3755
3756   j += 2;
3757 }
3758
3759 /* Return true if frame description entry FDE is needed for EH.  */
3760
3761 static bool
3762 fde_needed_for_eh_p (dw_fde_ref fde)
3763 {
3764   if (flag_asynchronous_unwind_tables)
3765     return true;
3766
3767   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3768     return true;
3769
3770   if (fde->uses_eh_lsda)
3771     return true;
3772
3773   /* If exceptions are enabled, we have collected nothrow info.  */
3774   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3775     return false;
3776
3777   return true;
3778 }
3779
3780 /* Output the call frame information used to record information
3781    that relates to calculating the frame pointer, and records the
3782    location of saved registers.  */
3783
3784 static void
3785 output_call_frame_info (int for_eh)
3786 {
3787   unsigned int i;
3788   dw_fde_ref fde;
3789   dw_cfi_ref cfi;
3790   char l1[20], l2[20], section_start_label[20];
3791   bool any_lsda_needed = false;
3792   char augmentation[6];
3793   int augmentation_size;
3794   int fde_encoding = DW_EH_PE_absptr;
3795   int per_encoding = DW_EH_PE_absptr;
3796   int lsda_encoding = DW_EH_PE_absptr;
3797   int return_reg;
3798   rtx personality = NULL;
3799   int dw_cie_version;
3800
3801   /* Don't emit a CIE if there won't be any FDEs.  */
3802   if (fde_table_in_use == 0)
3803     return;
3804
3805   /* Nothing to do if the assembler's doing it all.  */
3806   if (dwarf2out_do_cfi_asm ())
3807     return;
3808
3809   /* If we don't have any functions we'll want to unwind out of, don't emit
3810      any EH unwind information.  If we make FDEs linkonce, we may have to
3811      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3812      want to avoid having an FDE kept around when the function it refers to
3813      is discarded.  Example where this matters: a primary function template
3814      in C++ requires EH information, an explicit specialization doesn't.  */
3815   if (for_eh)
3816     {
3817       bool any_eh_needed = false;
3818
3819       for (i = 0; i < fde_table_in_use; i++)
3820         if (fde_table[i].uses_eh_lsda)
3821           any_eh_needed = any_lsda_needed = true;
3822         else if (fde_needed_for_eh_p (&fde_table[i]))
3823           any_eh_needed = true;
3824         else if (TARGET_USES_WEAK_UNWIND_INFO)
3825           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3826                                              1, 1);
3827
3828       if (!any_eh_needed)
3829         return;
3830     }
3831
3832   /* We're going to be generating comments, so turn on app.  */
3833   if (flag_debug_asm)
3834     app_enable ();
3835
3836   /* Switch to the proper frame section, first time.  */
3837   switch_to_frame_table_section (for_eh, false);
3838
3839   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3840   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3841
3842   /* Output the CIE.  */
3843   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3844   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3845   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3846     dw2_asm_output_data (4, 0xffffffff,
3847       "Initial length escape value indicating 64-bit DWARF extension");
3848   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3849                         "Length of Common Information Entry");
3850   ASM_OUTPUT_LABEL (asm_out_file, l1);
3851
3852   /* Now that the CIE pointer is PC-relative for EH,
3853      use 0 to identify the CIE.  */
3854   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3855                        (for_eh ? 0 : DWARF_CIE_ID),
3856                        "CIE Identifier Tag");
3857
3858   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3859      use CIE version 1, unless that would produce incorrect results
3860      due to overflowing the return register column.  */
3861   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3862   dw_cie_version = 1;
3863   if (return_reg >= 256 || dwarf_version > 2)
3864     dw_cie_version = 3;
3865   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3866
3867   augmentation[0] = 0;
3868   augmentation_size = 0;
3869
3870   personality = current_unit_personality;
3871   if (for_eh)
3872     {
3873       char *p;
3874
3875       /* Augmentation:
3876          z      Indicates that a uleb128 is present to size the
3877                 augmentation section.
3878          L      Indicates the encoding (and thus presence) of
3879                 an LSDA pointer in the FDE augmentation.
3880          R      Indicates a non-default pointer encoding for
3881                 FDE code pointers.
3882          P      Indicates the presence of an encoding + language
3883                 personality routine in the CIE augmentation.  */
3884
3885       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3886       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3887       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3888
3889       p = augmentation + 1;
3890       if (personality)
3891         {
3892           *p++ = 'P';
3893           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3894           assemble_external_libcall (personality);
3895         }
3896       if (any_lsda_needed)
3897         {
3898           *p++ = 'L';
3899           augmentation_size += 1;
3900         }
3901       if (fde_encoding != DW_EH_PE_absptr)
3902         {
3903           *p++ = 'R';
3904           augmentation_size += 1;
3905         }
3906       if (p > augmentation + 1)
3907         {
3908           augmentation[0] = 'z';
3909           *p = '\0';
3910         }
3911
3912       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3913       if (personality && per_encoding == DW_EH_PE_aligned)
3914         {
3915           int offset = (  4             /* Length */
3916                         + 4             /* CIE Id */
3917                         + 1             /* CIE version */
3918                         + strlen (augmentation) + 1     /* Augmentation */
3919                         + size_of_uleb128 (1)           /* Code alignment */
3920                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3921                         + 1             /* RA column */
3922                         + 1             /* Augmentation size */
3923                         + 1             /* Personality encoding */ );
3924           int pad = -offset & (PTR_SIZE - 1);
3925
3926           augmentation_size += pad;
3927
3928           /* Augmentations should be small, so there's scarce need to
3929              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3930           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3931         }
3932     }
3933
3934   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3935   if (dw_cie_version >= 4)
3936     {
3937       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3938       dw2_asm_output_data (1, 0, "CIE Segment Size");
3939     }
3940   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3941   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3942                                "CIE Data Alignment Factor");
3943
3944   if (dw_cie_version == 1)
3945     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3946   else
3947     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3948
3949   if (augmentation[0])
3950     {
3951       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3952       if (personality)
3953         {
3954           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3955                                eh_data_format_name (per_encoding));
3956           dw2_asm_output_encoded_addr_rtx (per_encoding,
3957                                            personality,
3958                                            true, NULL);
3959         }
3960
3961       if (any_lsda_needed)
3962         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3963                              eh_data_format_name (lsda_encoding));
3964
3965       if (fde_encoding != DW_EH_PE_absptr)
3966         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3967                              eh_data_format_name (fde_encoding));
3968     }
3969
3970   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3971     output_cfi (cfi, NULL, for_eh);
3972
3973   /* Pad the CIE out to an address sized boundary.  */
3974   ASM_OUTPUT_ALIGN (asm_out_file,
3975                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3976   ASM_OUTPUT_LABEL (asm_out_file, l2);
3977
3978   /* Loop through all of the FDE's.  */
3979   for (i = 0; i < fde_table_in_use; i++)
3980     {
3981       unsigned int k;
3982       fde = &fde_table[i];
3983
3984       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3985       if (for_eh && !fde_needed_for_eh_p (fde))
3986         continue;
3987
3988       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3989         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3990                     augmentation, any_lsda_needed, lsda_encoding);
3991     }
3992
3993   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3994     dw2_asm_output_data (4, 0, "End of Table");
3995 #ifdef MIPS_DEBUGGING_INFO
3996   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3997      get a value of 0.  Putting .align 0 after the label fixes it.  */
3998   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3999 #endif
4000
4001   /* Turn off app to make assembly quicker.  */
4002   if (flag_debug_asm)
4003     app_disable ();
4004 }
4005
4006 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4007
4008 static void
4009 dwarf2out_do_cfi_startproc (bool second)
4010 {
4011   int enc;
4012   rtx ref;
4013   rtx personality = get_personality_function (current_function_decl);
4014
4015   fprintf (asm_out_file, "\t.cfi_startproc\n");
4016
4017   if (personality)
4018     {
4019       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4020       ref = personality;
4021
4022       /* ??? The GAS support isn't entirely consistent.  We have to
4023          handle indirect support ourselves, but PC-relative is done
4024          in the assembler.  Further, the assembler can't handle any
4025          of the weirder relocation types.  */
4026       if (enc & DW_EH_PE_indirect)
4027         ref = dw2_force_const_mem (ref, true);
4028
4029       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4030       output_addr_const (asm_out_file, ref);
4031       fputc ('\n', asm_out_file);
4032     }
4033
4034   if (crtl->uses_eh_lsda)
4035     {
4036       char lab[20];
4037
4038       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4039       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4040                                    current_function_funcdef_no);
4041       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4042       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4043
4044       if (enc & DW_EH_PE_indirect)
4045         ref = dw2_force_const_mem (ref, true);
4046
4047       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4048       output_addr_const (asm_out_file, ref);
4049       fputc ('\n', asm_out_file);
4050     }
4051 }
4052
4053 /* Output a marker (i.e. a label) for the beginning of a function, before
4054    the prologue.  */
4055
4056 void
4057 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4058                           const char *file ATTRIBUTE_UNUSED)
4059 {
4060   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4061   char * dup_label;
4062   dw_fde_ref fde;
4063   section *fnsec;
4064   bool do_frame;
4065
4066   current_function_func_begin_label = NULL;
4067
4068   do_frame = dwarf2out_do_frame ();
4069
4070   /* ??? current_function_func_begin_label is also used by except.c for
4071      call-site information.  We must emit this label if it might be used.  */
4072   if (!do_frame
4073       && (!flag_exceptions
4074           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4075     return;
4076
4077   fnsec = function_section (current_function_decl);
4078   switch_to_section (fnsec);
4079   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4080                                current_function_funcdef_no);
4081   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4082                           current_function_funcdef_no);
4083   dup_label = xstrdup (label);
4084   current_function_func_begin_label = dup_label;
4085
4086   /* We can elide the fde allocation if we're not emitting debug info.  */
4087   if (!do_frame)
4088     return;
4089
4090   /* Expand the fde table if necessary.  */
4091   if (fde_table_in_use == fde_table_allocated)
4092     {
4093       fde_table_allocated += FDE_TABLE_INCREMENT;
4094       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4095       memset (fde_table + fde_table_in_use, 0,
4096               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4097     }
4098
4099   /* Record the FDE associated with this function.  */
4100   current_funcdef_fde = fde_table_in_use;
4101
4102   /* Add the new FDE at the end of the fde_table.  */
4103   fde = &fde_table[fde_table_in_use++];
4104   fde->decl = current_function_decl;
4105   fde->dw_fde_begin = dup_label;
4106   fde->dw_fde_current_label = dup_label;
4107   fde->dw_fde_hot_section_label = NULL;
4108   fde->dw_fde_hot_section_end_label = NULL;
4109   fde->dw_fde_unlikely_section_label = NULL;
4110   fde->dw_fde_unlikely_section_end_label = NULL;
4111   fde->dw_fde_switched_sections = 0;
4112   fde->dw_fde_switched_cold_to_hot = 0;
4113   fde->dw_fde_end = NULL;
4114   fde->dw_fde_vms_end_prologue = NULL;
4115   fde->dw_fde_vms_begin_epilogue = NULL;
4116   fde->dw_fde_cfi = NULL;
4117   fde->dw_fde_switch_cfi = NULL;
4118   fde->funcdef_number = current_function_funcdef_no;
4119   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4120   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4121   fde->nothrow = crtl->nothrow;
4122   fde->drap_reg = INVALID_REGNUM;
4123   fde->vdrap_reg = INVALID_REGNUM;
4124   if (flag_reorder_blocks_and_partition)
4125     {
4126       section *unlikelysec;
4127       if (first_function_block_is_cold)
4128         fde->in_std_section = 1;
4129       else
4130         fde->in_std_section
4131           = (fnsec == text_section
4132              || (cold_text_section && fnsec == cold_text_section));
4133       unlikelysec = unlikely_text_section ();
4134       fde->cold_in_std_section
4135         = (unlikelysec == text_section
4136            || (cold_text_section && unlikelysec == cold_text_section));
4137     }
4138   else
4139     {
4140       fde->in_std_section
4141         = (fnsec == text_section
4142            || (cold_text_section && fnsec == cold_text_section));
4143       fde->cold_in_std_section = 0;
4144     }
4145
4146   args_size = old_args_size = 0;
4147
4148   /* We only want to output line number information for the genuine dwarf2
4149      prologue case, not the eh frame case.  */
4150 #ifdef DWARF2_DEBUGGING_INFO
4151   if (file)
4152     dwarf2out_source_line (line, file, 0, true);
4153 #endif
4154
4155   if (dwarf2out_do_cfi_asm ())
4156     dwarf2out_do_cfi_startproc (false);
4157   else
4158     {
4159       rtx personality = get_personality_function (current_function_decl);
4160       if (!current_unit_personality)
4161         current_unit_personality = personality;
4162
4163       /* We cannot keep a current personality per function as without CFI
4164          asm, at the point where we emit the CFI data, there is no current
4165          function anymore.  */
4166       if (personality && current_unit_personality != personality)
4167         sorry ("multiple EH personalities are supported only with assemblers "
4168                "supporting .cfi_personality directive");
4169     }
4170 }
4171
4172 /* Output a marker (i.e. a label) for the end of the generated code
4173    for a function prologue.  This gets called *after* the prologue code has
4174    been generated.  */
4175
4176 void
4177 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4178                         const char *file ATTRIBUTE_UNUSED)
4179 {
4180   dw_fde_ref fde;
4181   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4182
4183   /* Output a label to mark the endpoint of the code generated for this
4184      function.  */
4185   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4186                                current_function_funcdef_no);
4187   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4188                           current_function_funcdef_no);
4189   fde = &fde_table[fde_table_in_use - 1];
4190   fde->dw_fde_vms_end_prologue = xstrdup (label);
4191 }
4192
4193 /* Output a marker (i.e. a label) for the beginning of the generated code
4194    for a function epilogue.  This gets called *before* the prologue code has
4195    been generated.  */
4196
4197 void
4198 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4199                           const char *file ATTRIBUTE_UNUSED)
4200 {
4201   dw_fde_ref fde;
4202   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4203
4204   fde = &fde_table[fde_table_in_use - 1];
4205   if (fde->dw_fde_vms_begin_epilogue)
4206     return;
4207
4208   /* Output a label to mark the endpoint of the code generated for this
4209      function.  */
4210   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4211                                current_function_funcdef_no);
4212   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4213                           current_function_funcdef_no);
4214   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4215 }
4216
4217 /* Output a marker (i.e. a label) for the absolute end of the generated code
4218    for a function definition.  This gets called *after* the epilogue code has
4219    been generated.  */
4220
4221 void
4222 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4223                         const char *file ATTRIBUTE_UNUSED)
4224 {
4225   dw_fde_ref fde;
4226   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4227
4228   last_var_location_insn = NULL_RTX;
4229
4230   if (dwarf2out_do_cfi_asm ())
4231     fprintf (asm_out_file, "\t.cfi_endproc\n");
4232
4233   /* Output a label to mark the endpoint of the code generated for this
4234      function.  */
4235   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4236                                current_function_funcdef_no);
4237   ASM_OUTPUT_LABEL (asm_out_file, label);
4238   fde = current_fde ();
4239   gcc_assert (fde != NULL);
4240   fde->dw_fde_end = xstrdup (label);
4241 }
4242
4243 void
4244 dwarf2out_frame_init (void)
4245 {
4246   /* Allocate the initial hunk of the fde_table.  */
4247   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4248   fde_table_allocated = FDE_TABLE_INCREMENT;
4249   fde_table_in_use = 0;
4250
4251   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4252      sake of lookup_cfa.  */
4253
4254   /* On entry, the Canonical Frame Address is at SP.  */
4255   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4256
4257   if (targetm.debug_unwind_info () == UI_DWARF2
4258       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4259     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4260 }
4261
4262 void
4263 dwarf2out_frame_finish (void)
4264 {
4265   /* Output call frame information.  */
4266   if (targetm.debug_unwind_info () == UI_DWARF2)
4267     output_call_frame_info (0);
4268
4269   /* Output another copy for the unwinder.  */
4270   if ((flag_unwind_tables || flag_exceptions)
4271       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4272     output_call_frame_info (1);
4273 }
4274
4275 /* Note that the current function section is being used for code.  */
4276
4277 static void
4278 dwarf2out_note_section_used (void)
4279 {
4280   section *sec = current_function_section ();
4281   if (sec == text_section)
4282     text_section_used = true;
4283   else if (sec == cold_text_section)
4284     cold_text_section_used = true;
4285 }
4286
4287 void
4288 dwarf2out_switch_text_section (void)
4289 {
4290   dw_fde_ref fde = current_fde ();
4291
4292   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4293
4294   fde->dw_fde_switched_sections = 1;
4295   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4296
4297   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4298   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4299   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4300   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4301   have_multiple_function_sections = true;
4302
4303   /* Reset the current label on switching text sections, so that we
4304      don't attempt to advance_loc4 between labels in different sections.  */
4305   fde->dw_fde_current_label = NULL;
4306
4307   /* There is no need to mark used sections when not debugging.  */
4308   if (cold_text_section != NULL)
4309     dwarf2out_note_section_used ();
4310
4311   if (dwarf2out_do_cfi_asm ())
4312     fprintf (asm_out_file, "\t.cfi_endproc\n");
4313
4314   /* Now do the real section switch.  */
4315   switch_to_section (current_function_section ());
4316
4317   if (dwarf2out_do_cfi_asm ())
4318     {
4319       dwarf2out_do_cfi_startproc (true);
4320       /* As this is a different FDE, insert all current CFI instructions
4321          again.  */
4322       output_cfis (fde->dw_fde_cfi, true, fde, true);
4323     }
4324   else
4325     {
4326       dw_cfi_ref cfi = fde->dw_fde_cfi;
4327
4328       cfi = fde->dw_fde_cfi;
4329       if (cfi)
4330         while (cfi->dw_cfi_next != NULL)
4331           cfi = cfi->dw_cfi_next;
4332       fde->dw_fde_switch_cfi = cfi;
4333     }
4334 }
4335 \f
4336 /* And now, the subset of the debugging information support code necessary
4337    for emitting location expressions.  */
4338
4339 /* Data about a single source file.  */
4340 struct GTY(()) dwarf_file_data {
4341   const char * filename;
4342   int emitted_number;
4343 };
4344
4345 typedef struct dw_val_struct *dw_val_ref;
4346 typedef struct die_struct *dw_die_ref;
4347 typedef const struct die_struct *const_dw_die_ref;
4348 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4349 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4350
4351 typedef struct GTY(()) deferred_locations_struct
4352 {
4353   tree variable;
4354   dw_die_ref die;
4355 } deferred_locations;
4356
4357 DEF_VEC_O(deferred_locations);
4358 DEF_VEC_ALLOC_O(deferred_locations,gc);
4359
4360 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4361
4362 DEF_VEC_P(dw_die_ref);
4363 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4364
4365 /* Each DIE may have a series of attribute/value pairs.  Values
4366    can take on several forms.  The forms that are used in this
4367    implementation are listed below.  */
4368
4369 enum dw_val_class
4370 {
4371   dw_val_class_addr,
4372   dw_val_class_offset,
4373   dw_val_class_loc,
4374   dw_val_class_loc_list,
4375   dw_val_class_range_list,
4376   dw_val_class_const,
4377   dw_val_class_unsigned_const,
4378   dw_val_class_const_double,
4379   dw_val_class_vec,
4380   dw_val_class_flag,
4381   dw_val_class_die_ref,
4382   dw_val_class_fde_ref,
4383   dw_val_class_lbl_id,
4384   dw_val_class_lineptr,
4385   dw_val_class_str,
4386   dw_val_class_macptr,
4387   dw_val_class_file,
4388   dw_val_class_data8,
4389   dw_val_class_decl_ref,
4390   dw_val_class_vms_delta
4391 };
4392
4393 /* Describe a floating point constant value, or a vector constant value.  */
4394
4395 typedef struct GTY(()) dw_vec_struct {
4396   unsigned char * GTY((length ("%h.length"))) array;
4397   unsigned length;
4398   unsigned elt_size;
4399 }
4400 dw_vec_const;
4401
4402 /* The dw_val_node describes an attribute's value, as it is
4403    represented internally.  */
4404
4405 typedef struct GTY(()) dw_val_struct {
4406   enum dw_val_class val_class;
4407   union dw_val_struct_union
4408     {
4409       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4410       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4411       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4412       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4413       HOST_WIDE_INT GTY ((default)) val_int;
4414       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4415       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4416       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4417       struct dw_val_die_union
4418         {
4419           dw_die_ref die;
4420           int external;
4421         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4422       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4423       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4424       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4425       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4426       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4427       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4428       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4429       struct dw_val_vms_delta_union
4430         {
4431           char * lbl1;
4432           char * lbl2;
4433         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4434     }
4435   GTY ((desc ("%1.val_class"))) v;
4436 }
4437 dw_val_node;
4438
4439 /* Locations in memory are described using a sequence of stack machine
4440    operations.  */
4441
4442 typedef struct GTY(()) dw_loc_descr_struct {
4443   dw_loc_descr_ref dw_loc_next;
4444   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4445   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4446      from DW_OP_addr with a dtp-relative symbol relocation.  */
4447   unsigned int dtprel : 1;
4448   int dw_loc_addr;
4449   dw_val_node dw_loc_oprnd1;
4450   dw_val_node dw_loc_oprnd2;
4451 }
4452 dw_loc_descr_node;
4453
4454 /* Location lists are ranges + location descriptions for that range,
4455    so you can track variables that are in different places over
4456    their entire life.  */
4457 typedef struct GTY(()) dw_loc_list_struct {
4458   dw_loc_list_ref dw_loc_next;
4459   const char *begin; /* Label for begin address of range */
4460   const char *end;  /* Label for end address of range */
4461   char *ll_symbol; /* Label for beginning of location list.
4462                       Only on head of list */
4463   const char *section; /* Section this loclist is relative to */
4464   dw_loc_descr_ref expr;
4465   hashval_t hash;
4466   bool emitted;
4467 } dw_loc_list_node;
4468
4469 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4470
4471 /* Convert a DWARF stack opcode into its string name.  */
4472
4473 static const char *
4474 dwarf_stack_op_name (unsigned int op)
4475 {
4476   switch (op)
4477     {
4478     case DW_OP_addr:
4479       return "DW_OP_addr";
4480     case DW_OP_deref:
4481       return "DW_OP_deref";
4482     case DW_OP_const1u:
4483       return "DW_OP_const1u";
4484     case DW_OP_const1s:
4485       return "DW_OP_const1s";
4486     case DW_OP_const2u:
4487       return "DW_OP_const2u";
4488     case DW_OP_const2s:
4489       return "DW_OP_const2s";
4490     case DW_OP_const4u:
4491       return "DW_OP_const4u";
4492     case DW_OP_const4s:
4493       return "DW_OP_const4s";
4494     case DW_OP_const8u:
4495       return "DW_OP_const8u";
4496     case DW_OP_const8s:
4497       return "DW_OP_const8s";
4498     case DW_OP_constu:
4499       return "DW_OP_constu";
4500     case DW_OP_consts:
4501       return "DW_OP_consts";
4502     case DW_OP_dup:
4503       return "DW_OP_dup";
4504     case DW_OP_drop:
4505       return "DW_OP_drop";
4506     case DW_OP_over:
4507       return "DW_OP_over";
4508     case DW_OP_pick:
4509       return "DW_OP_pick";
4510     case DW_OP_swap:
4511       return "DW_OP_swap";
4512     case DW_OP_rot:
4513       return "DW_OP_rot";
4514     case DW_OP_xderef:
4515       return "DW_OP_xderef";
4516     case DW_OP_abs:
4517       return "DW_OP_abs";
4518     case DW_OP_and:
4519       return "DW_OP_and";
4520     case DW_OP_div:
4521       return "DW_OP_div";
4522     case DW_OP_minus:
4523       return "DW_OP_minus";
4524     case DW_OP_mod:
4525       return "DW_OP_mod";
4526     case DW_OP_mul:
4527       return "DW_OP_mul";
4528     case DW_OP_neg:
4529       return "DW_OP_neg";
4530     case DW_OP_not:
4531       return "DW_OP_not";
4532     case DW_OP_or:
4533       return "DW_OP_or";
4534     case DW_OP_plus:
4535       return "DW_OP_plus";
4536     case DW_OP_plus_uconst:
4537       return "DW_OP_plus_uconst";
4538     case DW_OP_shl:
4539       return "DW_OP_shl";
4540     case DW_OP_shr:
4541       return "DW_OP_shr";
4542     case DW_OP_shra:
4543       return "DW_OP_shra";
4544     case DW_OP_xor:
4545       return "DW_OP_xor";
4546     case DW_OP_bra:
4547       return "DW_OP_bra";
4548     case DW_OP_eq:
4549       return "DW_OP_eq";
4550     case DW_OP_ge:
4551       return "DW_OP_ge";
4552     case DW_OP_gt:
4553       return "DW_OP_gt";
4554     case DW_OP_le:
4555       return "DW_OP_le";
4556     case DW_OP_lt:
4557       return "DW_OP_lt";
4558     case DW_OP_ne:
4559       return "DW_OP_ne";
4560     case DW_OP_skip:
4561       return "DW_OP_skip";
4562     case DW_OP_lit0:
4563       return "DW_OP_lit0";
4564     case DW_OP_lit1:
4565       return "DW_OP_lit1";
4566     case DW_OP_lit2:
4567       return "DW_OP_lit2";
4568     case DW_OP_lit3:
4569       return "DW_OP_lit3";
4570     case DW_OP_lit4:
4571       return "DW_OP_lit4";
4572     case DW_OP_lit5:
4573       return "DW_OP_lit5";
4574     case DW_OP_lit6:
4575       return "DW_OP_lit6";
4576     case DW_OP_lit7:
4577       return "DW_OP_lit7";
4578     case DW_OP_lit8:
4579       return "DW_OP_lit8";
4580     case DW_OP_lit9:
4581       return "DW_OP_lit9";
4582     case DW_OP_lit10:
4583       return "DW_OP_lit10";
4584     case DW_OP_lit11:
4585       return "DW_OP_lit11";
4586     case DW_OP_lit12:
4587       return "DW_OP_lit12";
4588     case DW_OP_lit13:
4589       return "DW_OP_lit13";
4590     case DW_OP_lit14:
4591       return "DW_OP_lit14";
4592     case DW_OP_lit15:
4593       return "DW_OP_lit15";
4594     case DW_OP_lit16:
4595       return "DW_OP_lit16";
4596     case DW_OP_lit17:
4597       return "DW_OP_lit17";
4598     case DW_OP_lit18:
4599       return "DW_OP_lit18";
4600     case DW_OP_lit19:
4601       return "DW_OP_lit19";
4602     case DW_OP_lit20:
4603       return "DW_OP_lit20";
4604     case DW_OP_lit21:
4605       return "DW_OP_lit21";
4606     case DW_OP_lit22:
4607       return "DW_OP_lit22";
4608     case DW_OP_lit23:
4609       return "DW_OP_lit23";
4610     case DW_OP_lit24:
4611       return "DW_OP_lit24";
4612     case DW_OP_lit25:
4613       return "DW_OP_lit25";
4614     case DW_OP_lit26:
4615       return "DW_OP_lit26";
4616     case DW_OP_lit27:
4617       return "DW_OP_lit27";
4618     case DW_OP_lit28:
4619       return "DW_OP_lit28";
4620     case DW_OP_lit29:
4621       return "DW_OP_lit29";
4622     case DW_OP_lit30:
4623       return "DW_OP_lit30";
4624     case DW_OP_lit31:
4625       return "DW_OP_lit31";
4626     case DW_OP_reg0:
4627       return "DW_OP_reg0";
4628     case DW_OP_reg1:
4629       return "DW_OP_reg1";
4630     case DW_OP_reg2:
4631       return "DW_OP_reg2";
4632     case DW_OP_reg3:
4633       return "DW_OP_reg3";
4634     case DW_OP_reg4:
4635       return "DW_OP_reg4";
4636     case DW_OP_reg5:
4637       return "DW_OP_reg5";
4638     case DW_OP_reg6:
4639       return "DW_OP_reg6";
4640     case DW_OP_reg7:
4641       return "DW_OP_reg7";
4642     case DW_OP_reg8:
4643       return "DW_OP_reg8";
4644     case DW_OP_reg9:
4645       return "DW_OP_reg9";
4646     case DW_OP_reg10:
4647       return "DW_OP_reg10";
4648     case DW_OP_reg11:
4649       return "DW_OP_reg11";
4650     case DW_OP_reg12:
4651       return "DW_OP_reg12";
4652     case DW_OP_reg13:
4653       return "DW_OP_reg13";
4654     case DW_OP_reg14:
4655       return "DW_OP_reg14";
4656     case DW_OP_reg15:
4657       return "DW_OP_reg15";
4658     case DW_OP_reg16:
4659       return "DW_OP_reg16";
4660     case DW_OP_reg17:
4661       return "DW_OP_reg17";
4662     case DW_OP_reg18:
4663       return "DW_OP_reg18";
4664     case DW_OP_reg19:
4665       return "DW_OP_reg19";
4666     case DW_OP_reg20:
4667       return "DW_OP_reg20";
4668     case DW_OP_reg21:
4669       return "DW_OP_reg21";
4670     case DW_OP_reg22:
4671       return "DW_OP_reg22";
4672     case DW_OP_reg23:
4673       return "DW_OP_reg23";
4674     case DW_OP_reg24:
4675       return "DW_OP_reg24";
4676     case DW_OP_reg25:
4677       return "DW_OP_reg25";
4678     case DW_OP_reg26:
4679       return "DW_OP_reg26";
4680     case DW_OP_reg27:
4681       return "DW_OP_reg27";
4682     case DW_OP_reg28:
4683       return "DW_OP_reg28";
4684     case DW_OP_reg29:
4685       return "DW_OP_reg29";
4686     case DW_OP_reg30:
4687       return "DW_OP_reg30";
4688     case DW_OP_reg31:
4689       return "DW_OP_reg31";
4690     case DW_OP_breg0:
4691       return "DW_OP_breg0";
4692     case DW_OP_breg1:
4693       return "DW_OP_breg1";
4694     case DW_OP_breg2:
4695       return "DW_OP_breg2";
4696     case DW_OP_breg3:
4697       return "DW_OP_breg3";
4698     case DW_OP_breg4:
4699       return "DW_OP_breg4";
4700     case DW_OP_breg5:
4701       return "DW_OP_breg5";
4702     case DW_OP_breg6:
4703       return "DW_OP_breg6";
4704     case DW_OP_breg7:
4705       return "DW_OP_breg7";
4706     case DW_OP_breg8:
4707       return "DW_OP_breg8";
4708     case DW_OP_breg9:
4709       return "DW_OP_breg9";
4710     case DW_OP_breg10:
4711       return "DW_OP_breg10";
4712     case DW_OP_breg11:
4713       return "DW_OP_breg11";
4714     case DW_OP_breg12:
4715       return "DW_OP_breg12";
4716     case DW_OP_breg13:
4717       return "DW_OP_breg13";
4718     case DW_OP_breg14:
4719       return "DW_OP_breg14";
4720     case DW_OP_breg15:
4721       return "DW_OP_breg15";
4722     case DW_OP_breg16:
4723       return "DW_OP_breg16";
4724     case DW_OP_breg17:
4725       return "DW_OP_breg17";
4726     case DW_OP_breg18:
4727       return "DW_OP_breg18";
4728     case DW_OP_breg19:
4729       return "DW_OP_breg19";
4730     case DW_OP_breg20:
4731       return "DW_OP_breg20";
4732     case DW_OP_breg21:
4733       return "DW_OP_breg21";
4734     case DW_OP_breg22:
4735       return "DW_OP_breg22";
4736     case DW_OP_breg23:
4737       return "DW_OP_breg23";
4738     case DW_OP_breg24:
4739       return "DW_OP_breg24";
4740     case DW_OP_breg25:
4741       return "DW_OP_breg25";
4742     case DW_OP_breg26:
4743       return "DW_OP_breg26";
4744     case DW_OP_breg27:
4745       return "DW_OP_breg27";
4746     case DW_OP_breg28:
4747       return "DW_OP_breg28";
4748     case DW_OP_breg29:
4749       return "DW_OP_breg29";
4750     case DW_OP_breg30:
4751       return "DW_OP_breg30";
4752     case DW_OP_breg31:
4753       return "DW_OP_breg31";
4754     case DW_OP_regx:
4755       return "DW_OP_regx";
4756     case DW_OP_fbreg:
4757       return "DW_OP_fbreg";
4758     case DW_OP_bregx:
4759       return "DW_OP_bregx";
4760     case DW_OP_piece:
4761       return "DW_OP_piece";
4762     case DW_OP_deref_size:
4763       return "DW_OP_deref_size";
4764     case DW_OP_xderef_size:
4765       return "DW_OP_xderef_size";
4766     case DW_OP_nop:
4767       return "DW_OP_nop";
4768
4769     case DW_OP_push_object_address:
4770       return "DW_OP_push_object_address";
4771     case DW_OP_call2:
4772       return "DW_OP_call2";
4773     case DW_OP_call4:
4774       return "DW_OP_call4";
4775     case DW_OP_call_ref:
4776       return "DW_OP_call_ref";
4777     case DW_OP_implicit_value:
4778       return "DW_OP_implicit_value";
4779     case DW_OP_stack_value:
4780       return "DW_OP_stack_value";
4781     case DW_OP_form_tls_address:
4782       return "DW_OP_form_tls_address";
4783     case DW_OP_call_frame_cfa:
4784       return "DW_OP_call_frame_cfa";
4785     case DW_OP_bit_piece:
4786       return "DW_OP_bit_piece";
4787
4788     case DW_OP_GNU_push_tls_address:
4789       return "DW_OP_GNU_push_tls_address";
4790     case DW_OP_GNU_uninit:
4791       return "DW_OP_GNU_uninit";
4792     case DW_OP_GNU_encoded_addr:
4793       return "DW_OP_GNU_encoded_addr";
4794     case DW_OP_GNU_implicit_pointer:
4795       return "DW_OP_GNU_implicit_pointer";
4796     case DW_OP_GNU_entry_value:
4797       return "DW_OP_GNU_entry_value";
4798
4799     default:
4800       return "OP_<unknown>";
4801     }
4802 }
4803
4804 /* Return a pointer to a newly allocated location description.  Location
4805    descriptions are simple expression terms that can be strung
4806    together to form more complicated location (address) descriptions.  */
4807
4808 static inline dw_loc_descr_ref
4809 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4810                unsigned HOST_WIDE_INT oprnd2)
4811 {
4812   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4813
4814   descr->dw_loc_opc = op;
4815   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4816   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4817   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4818   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4819
4820   return descr;
4821 }
4822
4823 /* Return a pointer to a newly allocated location description for
4824    REG and OFFSET.  */
4825
4826 static inline dw_loc_descr_ref
4827 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4828 {
4829   if (reg <= 31)
4830     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4831                           offset, 0);
4832   else
4833     return new_loc_descr (DW_OP_bregx, reg, offset);
4834 }
4835
4836 /* Add a location description term to a location description expression.  */
4837
4838 static inline void
4839 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4840 {
4841   dw_loc_descr_ref *d;
4842
4843   /* Find the end of the chain.  */
4844   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4845     ;
4846
4847   *d = descr;
4848 }
4849
4850 /* Add a constant OFFSET to a location expression.  */
4851
4852 static void
4853 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4854 {
4855   dw_loc_descr_ref loc;
4856   HOST_WIDE_INT *p;
4857
4858   gcc_assert (*list_head != NULL);
4859
4860   if (!offset)
4861     return;
4862
4863   /* Find the end of the chain.  */
4864   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4865     ;
4866
4867   p = NULL;
4868   if (loc->dw_loc_opc == DW_OP_fbreg
4869       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4870     p = &loc->dw_loc_oprnd1.v.val_int;
4871   else if (loc->dw_loc_opc == DW_OP_bregx)
4872     p = &loc->dw_loc_oprnd2.v.val_int;
4873
4874   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4875      offset.  Don't optimize if an signed integer overflow would happen.  */
4876   if (p != NULL
4877       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4878           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4879     *p += offset;
4880
4881   else if (offset > 0)
4882     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4883
4884   else
4885     {
4886       loc->dw_loc_next = int_loc_descriptor (-offset);
4887       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4888     }
4889 }
4890
4891 /* Add a constant OFFSET to a location list.  */
4892
4893 static void
4894 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4895 {
4896   dw_loc_list_ref d;
4897   for (d = list_head; d != NULL; d = d->dw_loc_next)
4898     loc_descr_plus_const (&d->expr, offset);
4899 }
4900
4901 #define DWARF_REF_SIZE  \
4902   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4903
4904 static unsigned long size_of_locs (dw_loc_descr_ref);
4905
4906 /* Return the size of a location descriptor.  */
4907
4908 static unsigned long
4909 size_of_loc_descr (dw_loc_descr_ref loc)
4910 {
4911   unsigned long size = 1;
4912
4913   switch (loc->dw_loc_opc)
4914     {
4915     case DW_OP_addr:
4916       size += DWARF2_ADDR_SIZE;
4917       break;
4918     case DW_OP_const1u:
4919     case DW_OP_const1s:
4920       size += 1;
4921       break;
4922     case DW_OP_const2u:
4923     case DW_OP_const2s:
4924       size += 2;
4925       break;
4926     case DW_OP_const4u:
4927     case DW_OP_const4s:
4928       size += 4;
4929       break;
4930     case DW_OP_const8u:
4931     case DW_OP_const8s:
4932       size += 8;
4933       break;
4934     case DW_OP_constu:
4935       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4936       break;
4937     case DW_OP_consts:
4938       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4939       break;
4940     case DW_OP_pick:
4941       size += 1;
4942       break;
4943     case DW_OP_plus_uconst:
4944       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4945       break;
4946     case DW_OP_skip:
4947     case DW_OP_bra:
4948       size += 2;
4949       break;
4950     case DW_OP_breg0:
4951     case DW_OP_breg1:
4952     case DW_OP_breg2:
4953     case DW_OP_breg3:
4954     case DW_OP_breg4:
4955     case DW_OP_breg5:
4956     case DW_OP_breg6:
4957     case DW_OP_breg7:
4958     case DW_OP_breg8:
4959     case DW_OP_breg9:
4960     case DW_OP_breg10:
4961     case DW_OP_breg11:
4962     case DW_OP_breg12:
4963     case DW_OP_breg13:
4964     case DW_OP_breg14:
4965     case DW_OP_breg15:
4966     case DW_OP_breg16:
4967     case DW_OP_breg17:
4968     case DW_OP_breg18:
4969     case DW_OP_breg19:
4970     case DW_OP_breg20:
4971     case DW_OP_breg21:
4972     case DW_OP_breg22:
4973     case DW_OP_breg23:
4974     case DW_OP_breg24:
4975     case DW_OP_breg25:
4976     case DW_OP_breg26:
4977     case DW_OP_breg27:
4978     case DW_OP_breg28:
4979     case DW_OP_breg29:
4980     case DW_OP_breg30:
4981     case DW_OP_breg31:
4982       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4983       break;
4984     case DW_OP_regx:
4985       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4986       break;
4987     case DW_OP_fbreg:
4988       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4989       break;
4990     case DW_OP_bregx:
4991       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4992       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4993       break;
4994     case DW_OP_piece:
4995       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996       break;
4997     case DW_OP_bit_piece:
4998       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4999       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5000       break;
5001     case DW_OP_deref_size:
5002     case DW_OP_xderef_size:
5003       size += 1;
5004       break;
5005     case DW_OP_call2:
5006       size += 2;
5007       break;
5008     case DW_OP_call4:
5009       size += 4;
5010       break;
5011     case DW_OP_call_ref:
5012       size += DWARF_REF_SIZE;
5013       break;
5014     case DW_OP_implicit_value:
5015       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5016               + loc->dw_loc_oprnd1.v.val_unsigned;
5017       break;
5018     case DW_OP_GNU_implicit_pointer:
5019       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5020       break;
5021     case DW_OP_GNU_entry_value:
5022       {
5023         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5024         size += size_of_uleb128 (op_size) + op_size;
5025         break;
5026       }
5027     default:
5028       break;
5029     }
5030
5031   return size;
5032 }
5033
5034 /* Return the size of a series of location descriptors.  */
5035
5036 static unsigned long
5037 size_of_locs (dw_loc_descr_ref loc)
5038 {
5039   dw_loc_descr_ref l;
5040   unsigned long size;
5041
5042   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5043      field, to avoid writing to a PCH file.  */
5044   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5045     {
5046       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5047         break;
5048       size += size_of_loc_descr (l);
5049     }
5050   if (! l)
5051     return size;
5052
5053   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5054     {
5055       l->dw_loc_addr = size;
5056       size += size_of_loc_descr (l);
5057     }
5058
5059   return size;
5060 }
5061
5062 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5063 static void get_ref_die_offset_label (char *, dw_die_ref);
5064 static void output_loc_sequence (dw_loc_descr_ref, int);
5065
5066 /* Output location description stack opcode's operands (if any).
5067    The for_eh_or_skip parameter controls whether register numbers are
5068    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5069    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5070    info).  This should be suppressed for the cases that have not been converted
5071    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5072
5073 static void
5074 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5075 {
5076   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5077   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5078
5079   switch (loc->dw_loc_opc)
5080     {
5081 #ifdef DWARF2_DEBUGGING_INFO
5082     case DW_OP_const2u:
5083     case DW_OP_const2s:
5084       dw2_asm_output_data (2, val1->v.val_int, NULL);
5085       break;
5086     case DW_OP_const4u:
5087       if (loc->dtprel)
5088         {
5089           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5090           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5091                                                val1->v.val_addr);
5092           fputc ('\n', asm_out_file);
5093           break;
5094         }
5095       /* FALLTHRU */
5096     case DW_OP_const4s:
5097       dw2_asm_output_data (4, val1->v.val_int, NULL);
5098       break;
5099     case DW_OP_const8u:
5100       if (loc->dtprel)
5101         {
5102           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5103           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5104                                                val1->v.val_addr);
5105           fputc ('\n', asm_out_file);
5106           break;
5107         }
5108       /* FALLTHRU */
5109     case DW_OP_const8s:
5110       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5111       dw2_asm_output_data (8, val1->v.val_int, NULL);
5112       break;
5113     case DW_OP_skip:
5114     case DW_OP_bra:
5115       {
5116         int offset;
5117
5118         gcc_assert (val1->val_class == dw_val_class_loc);
5119         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5120
5121         dw2_asm_output_data (2, offset, NULL);
5122       }
5123       break;
5124     case DW_OP_implicit_value:
5125       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5126       switch (val2->val_class)
5127         {
5128         case dw_val_class_const:
5129           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5130           break;
5131         case dw_val_class_vec:
5132           {
5133             unsigned int elt_size = val2->v.val_vec.elt_size;
5134             unsigned int len = val2->v.val_vec.length;
5135             unsigned int i;
5136             unsigned char *p;
5137
5138             if (elt_size > sizeof (HOST_WIDE_INT))
5139               {
5140                 elt_size /= 2;
5141                 len *= 2;
5142               }
5143             for (i = 0, p = val2->v.val_vec.array;
5144                  i < len;
5145                  i++, p += elt_size)
5146               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5147                                    "fp or vector constant word %u", i);
5148           }
5149           break;
5150         case dw_val_class_const_double:
5151           {
5152             unsigned HOST_WIDE_INT first, second;
5153
5154             if (WORDS_BIG_ENDIAN)
5155               {
5156                 first = val2->v.val_double.high;
5157                 second = val2->v.val_double.low;
5158               }
5159             else
5160               {
5161                 first = val2->v.val_double.low;
5162                 second = val2->v.val_double.high;
5163               }
5164             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5165                                  first, NULL);
5166             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5167                                  second, NULL);
5168           }
5169           break;
5170         case dw_val_class_addr:
5171           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5172           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5173           break;
5174         default:
5175           gcc_unreachable ();
5176         }
5177       break;
5178 #else
5179     case DW_OP_const2u:
5180     case DW_OP_const2s:
5181     case DW_OP_const4u:
5182     case DW_OP_const4s:
5183     case DW_OP_const8u:
5184     case DW_OP_const8s:
5185     case DW_OP_skip:
5186     case DW_OP_bra:
5187     case DW_OP_implicit_value:
5188       /* We currently don't make any attempt to make sure these are
5189          aligned properly like we do for the main unwind info, so
5190          don't support emitting things larger than a byte if we're
5191          only doing unwinding.  */
5192       gcc_unreachable ();
5193 #endif
5194     case DW_OP_const1u:
5195     case DW_OP_const1s:
5196       dw2_asm_output_data (1, val1->v.val_int, NULL);
5197       break;
5198     case DW_OP_constu:
5199       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5200       break;
5201     case DW_OP_consts:
5202       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5203       break;
5204     case DW_OP_pick:
5205       dw2_asm_output_data (1, val1->v.val_int, NULL);
5206       break;
5207     case DW_OP_plus_uconst:
5208       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5209       break;
5210     case DW_OP_breg0:
5211     case DW_OP_breg1:
5212     case DW_OP_breg2:
5213     case DW_OP_breg3:
5214     case DW_OP_breg4:
5215     case DW_OP_breg5:
5216     case DW_OP_breg6:
5217     case DW_OP_breg7:
5218     case DW_OP_breg8:
5219     case DW_OP_breg9:
5220     case DW_OP_breg10:
5221     case DW_OP_breg11:
5222     case DW_OP_breg12:
5223     case DW_OP_breg13:
5224     case DW_OP_breg14:
5225     case DW_OP_breg15:
5226     case DW_OP_breg16:
5227     case DW_OP_breg17:
5228     case DW_OP_breg18:
5229     case DW_OP_breg19:
5230     case DW_OP_breg20:
5231     case DW_OP_breg21:
5232     case DW_OP_breg22:
5233     case DW_OP_breg23:
5234     case DW_OP_breg24:
5235     case DW_OP_breg25:
5236     case DW_OP_breg26:
5237     case DW_OP_breg27:
5238     case DW_OP_breg28:
5239     case DW_OP_breg29:
5240     case DW_OP_breg30:
5241     case DW_OP_breg31:
5242       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5243       break;
5244     case DW_OP_regx:
5245       {
5246         unsigned r = val1->v.val_unsigned;
5247         if (for_eh_or_skip >= 0)
5248           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5249         gcc_assert (size_of_uleb128 (r) 
5250                     == size_of_uleb128 (val1->v.val_unsigned));
5251         dw2_asm_output_data_uleb128 (r, NULL);  
5252       }
5253       break;
5254     case DW_OP_fbreg:
5255       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5256       break;
5257     case DW_OP_bregx:
5258       {
5259         unsigned r = val1->v.val_unsigned;
5260         if (for_eh_or_skip >= 0)
5261           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5262         gcc_assert (size_of_uleb128 (r) 
5263                     == size_of_uleb128 (val1->v.val_unsigned));
5264         dw2_asm_output_data_uleb128 (r, NULL);  
5265         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5266       }
5267       break;
5268     case DW_OP_piece:
5269       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5270       break;
5271     case DW_OP_bit_piece:
5272       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5273       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5274       break;
5275     case DW_OP_deref_size:
5276     case DW_OP_xderef_size:
5277       dw2_asm_output_data (1, val1->v.val_int, NULL);
5278       break;
5279
5280     case DW_OP_addr:
5281       if (loc->dtprel)
5282         {
5283           if (targetm.asm_out.output_dwarf_dtprel)
5284             {
5285               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5286                                                    DWARF2_ADDR_SIZE,
5287                                                    val1->v.val_addr);
5288               fputc ('\n', asm_out_file);
5289             }
5290           else
5291             gcc_unreachable ();
5292         }
5293       else
5294         {
5295 #ifdef DWARF2_DEBUGGING_INFO
5296           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5297 #else
5298           gcc_unreachable ();
5299 #endif
5300         }
5301       break;
5302
5303     case DW_OP_GNU_implicit_pointer:
5304       {
5305         char label[MAX_ARTIFICIAL_LABEL_BYTES
5306                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5307         gcc_assert (val1->val_class == dw_val_class_die_ref);
5308         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5309         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5310         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5311       }
5312       break;
5313
5314     case DW_OP_GNU_entry_value:
5315       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5316       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5317       break;
5318
5319     default:
5320       /* Other codes have no operands.  */
5321       break;
5322     }
5323 }
5324
5325 /* Output a sequence of location operations.  
5326    The for_eh_or_skip parameter controls whether register numbers are
5327    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5328    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5329    info).  This should be suppressed for the cases that have not been converted
5330    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5331
5332 static void
5333 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5334 {
5335   for (; loc != NULL; loc = loc->dw_loc_next)
5336     {
5337       enum dwarf_location_atom opc = loc->dw_loc_opc;
5338       /* Output the opcode.  */
5339       if (for_eh_or_skip >= 0 
5340           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5341         {
5342           unsigned r = (opc - DW_OP_breg0);
5343           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5344           gcc_assert (r <= 31);
5345           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5346         }
5347       else if (for_eh_or_skip >= 0 
5348                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5349         {
5350           unsigned r = (opc - DW_OP_reg0);
5351           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5352           gcc_assert (r <= 31);
5353           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5354         }
5355
5356       dw2_asm_output_data (1, opc,
5357                              "%s", dwarf_stack_op_name (opc));
5358
5359       /* Output the operand(s) (if any).  */
5360       output_loc_operands (loc, for_eh_or_skip);
5361     }
5362 }
5363
5364 /* Output location description stack opcode's operands (if any).
5365    The output is single bytes on a line, suitable for .cfi_escape.  */
5366
5367 static void
5368 output_loc_operands_raw (dw_loc_descr_ref loc)
5369 {
5370   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5371   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5372
5373   switch (loc->dw_loc_opc)
5374     {
5375     case DW_OP_addr:
5376     case DW_OP_implicit_value:
5377       /* We cannot output addresses in .cfi_escape, only bytes.  */
5378       gcc_unreachable ();
5379
5380     case DW_OP_const1u:
5381     case DW_OP_const1s:
5382     case DW_OP_pick:
5383     case DW_OP_deref_size:
5384     case DW_OP_xderef_size:
5385       fputc (',', asm_out_file);
5386       dw2_asm_output_data_raw (1, val1->v.val_int);
5387       break;
5388
5389     case DW_OP_const2u:
5390     case DW_OP_const2s:
5391       fputc (',', asm_out_file);
5392       dw2_asm_output_data_raw (2, val1->v.val_int);
5393       break;
5394
5395     case DW_OP_const4u:
5396     case DW_OP_const4s:
5397       fputc (',', asm_out_file);
5398       dw2_asm_output_data_raw (4, val1->v.val_int);
5399       break;
5400
5401     case DW_OP_const8u:
5402     case DW_OP_const8s:
5403       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5404       fputc (',', asm_out_file);
5405       dw2_asm_output_data_raw (8, val1->v.val_int);
5406       break;
5407
5408     case DW_OP_skip:
5409     case DW_OP_bra:
5410       {
5411         int offset;
5412
5413         gcc_assert (val1->val_class == dw_val_class_loc);
5414         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5415
5416         fputc (',', asm_out_file);
5417         dw2_asm_output_data_raw (2, offset);
5418       }
5419       break;
5420
5421     case DW_OP_regx:
5422       {
5423         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5424         gcc_assert (size_of_uleb128 (r) 
5425                     == size_of_uleb128 (val1->v.val_unsigned));
5426         fputc (',', asm_out_file);
5427         dw2_asm_output_data_uleb128_raw (r);
5428       }
5429       break;
5430       
5431     case DW_OP_constu:
5432     case DW_OP_plus_uconst:
5433     case DW_OP_piece:
5434       fputc (',', asm_out_file);
5435       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5436       break;
5437
5438     case DW_OP_bit_piece:
5439       fputc (',', asm_out_file);
5440       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5441       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5442       break;
5443
5444     case DW_OP_consts:
5445     case DW_OP_breg0:
5446     case DW_OP_breg1:
5447     case DW_OP_breg2:
5448     case DW_OP_breg3:
5449     case DW_OP_breg4:
5450     case DW_OP_breg5:
5451     case DW_OP_breg6:
5452     case DW_OP_breg7:
5453     case DW_OP_breg8:
5454     case DW_OP_breg9:
5455     case DW_OP_breg10:
5456     case DW_OP_breg11:
5457     case DW_OP_breg12:
5458     case DW_OP_breg13:
5459     case DW_OP_breg14:
5460     case DW_OP_breg15:
5461     case DW_OP_breg16:
5462     case DW_OP_breg17:
5463     case DW_OP_breg18:
5464     case DW_OP_breg19:
5465     case DW_OP_breg20:
5466     case DW_OP_breg21:
5467     case DW_OP_breg22:
5468     case DW_OP_breg23:
5469     case DW_OP_breg24:
5470     case DW_OP_breg25:
5471     case DW_OP_breg26:
5472     case DW_OP_breg27:
5473     case DW_OP_breg28:
5474     case DW_OP_breg29:
5475     case DW_OP_breg30:
5476     case DW_OP_breg31:
5477     case DW_OP_fbreg:
5478       fputc (',', asm_out_file);
5479       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5480       break;
5481
5482     case DW_OP_bregx:
5483       {
5484         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5485         gcc_assert (size_of_uleb128 (r) 
5486                     == size_of_uleb128 (val1->v.val_unsigned));
5487         fputc (',', asm_out_file);
5488         dw2_asm_output_data_uleb128_raw (r);
5489         fputc (',', asm_out_file);
5490         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5491       }
5492       break;
5493
5494     case DW_OP_GNU_implicit_pointer:
5495     case DW_OP_GNU_entry_value:
5496       gcc_unreachable ();
5497       break;
5498
5499     default:
5500       /* Other codes have no operands.  */
5501       break;
5502     }
5503 }
5504
5505 static void
5506 output_loc_sequence_raw (dw_loc_descr_ref loc)
5507 {
5508   while (1)
5509     {
5510       enum dwarf_location_atom opc = loc->dw_loc_opc;
5511       /* Output the opcode.  */
5512       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5513         {
5514           unsigned r = (opc - DW_OP_breg0);
5515           r = DWARF2_FRAME_REG_OUT (r, 1);
5516           gcc_assert (r <= 31);
5517           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5518         }
5519       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5520         {
5521           unsigned r = (opc - DW_OP_reg0);
5522           r = DWARF2_FRAME_REG_OUT (r, 1);
5523           gcc_assert (r <= 31);
5524           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5525         }
5526       /* Output the opcode.  */
5527       fprintf (asm_out_file, "%#x", opc);
5528       output_loc_operands_raw (loc);
5529
5530       if (!loc->dw_loc_next)
5531         break;
5532       loc = loc->dw_loc_next;
5533
5534       fputc (',', asm_out_file);
5535     }
5536 }
5537
5538 /* This routine will generate the correct assembly data for a location
5539    description based on a cfi entry with a complex address.  */
5540
5541 static void
5542 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5543 {
5544   dw_loc_descr_ref loc;
5545   unsigned long size;
5546
5547   if (cfi->dw_cfi_opc == DW_CFA_expression)
5548     {
5549       unsigned r = 
5550         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5551       dw2_asm_output_data (1, r, NULL);
5552       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5553     }
5554   else
5555     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5556
5557   /* Output the size of the block.  */
5558   size = size_of_locs (loc);
5559   dw2_asm_output_data_uleb128 (size, NULL);
5560
5561   /* Now output the operations themselves.  */
5562   output_loc_sequence (loc, for_eh);
5563 }
5564
5565 /* Similar, but used for .cfi_escape.  */
5566
5567 static void
5568 output_cfa_loc_raw (dw_cfi_ref cfi)
5569 {
5570   dw_loc_descr_ref loc;
5571   unsigned long size;
5572
5573   if (cfi->dw_cfi_opc == DW_CFA_expression)
5574     {
5575       unsigned r = 
5576         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5577       fprintf (asm_out_file, "%#x,", r);
5578       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5579     }
5580   else
5581     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5582
5583   /* Output the size of the block.  */
5584   size = size_of_locs (loc);
5585   dw2_asm_output_data_uleb128_raw (size);
5586   fputc (',', asm_out_file);
5587
5588   /* Now output the operations themselves.  */
5589   output_loc_sequence_raw (loc);
5590 }
5591
5592 /* This function builds a dwarf location descriptor sequence from a
5593    dw_cfa_location, adding the given OFFSET to the result of the
5594    expression.  */
5595
5596 static struct dw_loc_descr_struct *
5597 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5598 {
5599   struct dw_loc_descr_struct *head, *tmp;
5600
5601   offset += cfa->offset;
5602
5603   if (cfa->indirect)
5604     {
5605       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5606       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5607       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5608       add_loc_descr (&head, tmp);
5609       if (offset != 0)
5610         {
5611           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5612           add_loc_descr (&head, tmp);
5613         }
5614     }
5615   else
5616     head = new_reg_loc_descr (cfa->reg, offset);
5617
5618   return head;
5619 }
5620
5621 /* This function builds a dwarf location descriptor sequence for
5622    the address at OFFSET from the CFA when stack is aligned to
5623    ALIGNMENT byte.  */
5624
5625 static struct dw_loc_descr_struct *
5626 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5627 {
5628   struct dw_loc_descr_struct *head;
5629   unsigned int dwarf_fp
5630     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5631
5632  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5633   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5634     {
5635       head = new_reg_loc_descr (dwarf_fp, 0);
5636       add_loc_descr (&head, int_loc_descriptor (alignment));
5637       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5638       loc_descr_plus_const (&head, offset);
5639     }
5640   else
5641     head = new_reg_loc_descr (dwarf_fp, offset);
5642   return head;
5643 }
5644
5645 /* This function fills in aa dw_cfa_location structure from a dwarf location
5646    descriptor sequence.  */
5647
5648 static void
5649 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5650 {
5651   struct dw_loc_descr_struct *ptr;
5652   cfa->offset = 0;
5653   cfa->base_offset = 0;
5654   cfa->indirect = 0;
5655   cfa->reg = -1;
5656
5657   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5658     {
5659       enum dwarf_location_atom op = ptr->dw_loc_opc;
5660
5661       switch (op)
5662         {
5663         case DW_OP_reg0:
5664         case DW_OP_reg1:
5665         case DW_OP_reg2:
5666         case DW_OP_reg3:
5667         case DW_OP_reg4:
5668         case DW_OP_reg5:
5669         case DW_OP_reg6:
5670         case DW_OP_reg7:
5671         case DW_OP_reg8:
5672         case DW_OP_reg9:
5673         case DW_OP_reg10:
5674         case DW_OP_reg11:
5675         case DW_OP_reg12:
5676         case DW_OP_reg13:
5677         case DW_OP_reg14:
5678         case DW_OP_reg15:
5679         case DW_OP_reg16:
5680         case DW_OP_reg17:
5681         case DW_OP_reg18:
5682         case DW_OP_reg19:
5683         case DW_OP_reg20:
5684         case DW_OP_reg21:
5685         case DW_OP_reg22:
5686         case DW_OP_reg23:
5687         case DW_OP_reg24:
5688         case DW_OP_reg25:
5689         case DW_OP_reg26:
5690         case DW_OP_reg27:
5691         case DW_OP_reg28:
5692         case DW_OP_reg29:
5693         case DW_OP_reg30:
5694         case DW_OP_reg31:
5695           cfa->reg = op - DW_OP_reg0;
5696           break;
5697         case DW_OP_regx:
5698           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5699           break;
5700         case DW_OP_breg0:
5701         case DW_OP_breg1:
5702         case DW_OP_breg2:
5703         case DW_OP_breg3:
5704         case DW_OP_breg4:
5705         case DW_OP_breg5:
5706         case DW_OP_breg6:
5707         case DW_OP_breg7:
5708         case DW_OP_breg8:
5709         case DW_OP_breg9:
5710         case DW_OP_breg10:
5711         case DW_OP_breg11:
5712         case DW_OP_breg12:
5713         case DW_OP_breg13:
5714         case DW_OP_breg14:
5715         case DW_OP_breg15:
5716         case DW_OP_breg16:
5717         case DW_OP_breg17:
5718         case DW_OP_breg18:
5719         case DW_OP_breg19:
5720         case DW_OP_breg20:
5721         case DW_OP_breg21:
5722         case DW_OP_breg22:
5723         case DW_OP_breg23:
5724         case DW_OP_breg24:
5725         case DW_OP_breg25:
5726         case DW_OP_breg26:
5727         case DW_OP_breg27:
5728         case DW_OP_breg28:
5729         case DW_OP_breg29:
5730         case DW_OP_breg30:
5731         case DW_OP_breg31:
5732           cfa->reg = op - DW_OP_breg0;
5733           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5734           break;
5735         case DW_OP_bregx:
5736           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5737           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5738           break;
5739         case DW_OP_deref:
5740           cfa->indirect = 1;
5741           break;
5742         case DW_OP_plus_uconst:
5743           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5744           break;
5745         default:
5746           internal_error ("DW_LOC_OP %s not implemented",
5747                           dwarf_stack_op_name (ptr->dw_loc_opc));
5748         }
5749     }
5750 }
5751 \f
5752 /* And now, the support for symbolic debugging information.  */
5753
5754 /* .debug_str support.  */
5755 static int output_indirect_string (void **, void *);
5756
5757 static void dwarf2out_init (const char *);
5758 static void dwarf2out_finish (const char *);
5759 static void dwarf2out_assembly_start (void);
5760 static void dwarf2out_define (unsigned int, const char *);
5761 static void dwarf2out_undef (unsigned int, const char *);
5762 static void dwarf2out_start_source_file (unsigned, const char *);
5763 static void dwarf2out_end_source_file (unsigned);
5764 static void dwarf2out_function_decl (tree);
5765 static void dwarf2out_begin_block (unsigned, unsigned);
5766 static void dwarf2out_end_block (unsigned, unsigned);
5767 static bool dwarf2out_ignore_block (const_tree);
5768 static void dwarf2out_global_decl (tree);
5769 static void dwarf2out_type_decl (tree, int);
5770 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5771 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5772                                                  dw_die_ref);
5773 static void dwarf2out_abstract_function (tree);
5774 static void dwarf2out_var_location (rtx);
5775 static void dwarf2out_begin_function (tree);
5776 static void dwarf2out_set_name (tree, tree);
5777
5778 /* The debug hooks structure.  */
5779
5780 const struct gcc_debug_hooks dwarf2_debug_hooks =
5781 {
5782   dwarf2out_init,
5783   dwarf2out_finish,
5784   dwarf2out_assembly_start,
5785   dwarf2out_define,
5786   dwarf2out_undef,
5787   dwarf2out_start_source_file,
5788   dwarf2out_end_source_file,
5789   dwarf2out_begin_block,
5790   dwarf2out_end_block,
5791   dwarf2out_ignore_block,
5792   dwarf2out_source_line,
5793   dwarf2out_begin_prologue,
5794 #if VMS_DEBUGGING_INFO
5795   dwarf2out_vms_end_prologue,
5796   dwarf2out_vms_begin_epilogue,
5797 #else
5798   debug_nothing_int_charstar,
5799   debug_nothing_int_charstar,
5800 #endif
5801   dwarf2out_end_epilogue,
5802   dwarf2out_begin_function,
5803   debug_nothing_int,            /* end_function */
5804   dwarf2out_function_decl,      /* function_decl */
5805   dwarf2out_global_decl,
5806   dwarf2out_type_decl,          /* type_decl */
5807   dwarf2out_imported_module_or_decl,
5808   debug_nothing_tree,           /* deferred_inline_function */
5809   /* The DWARF 2 backend tries to reduce debugging bloat by not
5810      emitting the abstract description of inline functions until
5811      something tries to reference them.  */
5812   dwarf2out_abstract_function,  /* outlining_inline_function */
5813   debug_nothing_rtx,            /* label */
5814   debug_nothing_int,            /* handle_pch */
5815   dwarf2out_var_location,
5816   dwarf2out_switch_text_section,
5817   dwarf2out_set_name,
5818   1,                            /* start_end_main_source_file */
5819   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5820 };
5821 \f
5822 /* NOTE: In the comments in this file, many references are made to
5823    "Debugging Information Entries".  This term is abbreviated as `DIE'
5824    throughout the remainder of this file.  */
5825
5826 /* An internal representation of the DWARF output is built, and then
5827    walked to generate the DWARF debugging info.  The walk of the internal
5828    representation is done after the entire program has been compiled.
5829    The types below are used to describe the internal representation.  */
5830
5831 /* Various DIE's use offsets relative to the beginning of the
5832    .debug_info section to refer to each other.  */
5833
5834 typedef long int dw_offset;
5835
5836 /* Define typedefs here to avoid circular dependencies.  */
5837
5838 typedef struct dw_attr_struct *dw_attr_ref;
5839 typedef struct dw_line_info_struct *dw_line_info_ref;
5840 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5841 typedef struct pubname_struct *pubname_ref;
5842 typedef struct dw_ranges_struct *dw_ranges_ref;
5843 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5844 typedef struct comdat_type_struct *comdat_type_node_ref;
5845
5846 /* Each entry in the line_info_table maintains the file and
5847    line number associated with the label generated for that
5848    entry.  The label gives the PC value associated with
5849    the line number entry.  */
5850
5851 typedef struct GTY(()) dw_line_info_struct {
5852   unsigned long dw_file_num;
5853   unsigned long dw_line_num;
5854 }
5855 dw_line_info_entry;
5856
5857 /* Line information for functions in separate sections; each one gets its
5858    own sequence.  */
5859 typedef struct GTY(()) dw_separate_line_info_struct {
5860   unsigned long dw_file_num;
5861   unsigned long dw_line_num;
5862   unsigned long function;
5863 }
5864 dw_separate_line_info_entry;
5865
5866 /* Each DIE attribute has a field specifying the attribute kind,
5867    a link to the next attribute in the chain, and an attribute value.
5868    Attributes are typically linked below the DIE they modify.  */
5869
5870 typedef struct GTY(()) dw_attr_struct {
5871   enum dwarf_attribute dw_attr;
5872   dw_val_node dw_attr_val;
5873 }
5874 dw_attr_node;
5875
5876 DEF_VEC_O(dw_attr_node);
5877 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5878
5879 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5880    The children of each node form a circular list linked by
5881    die_sib.  die_child points to the node *before* the "first" child node.  */
5882
5883 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5884   union die_symbol_or_type_node
5885     {
5886       char * GTY ((tag ("0"))) die_symbol;
5887       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5888     }
5889   GTY ((desc ("dwarf_version >= 4"))) die_id;
5890   VEC(dw_attr_node,gc) * die_attr;
5891   dw_die_ref die_parent;
5892   dw_die_ref die_child;
5893   dw_die_ref die_sib;
5894   dw_die_ref die_definition; /* ref from a specification to its definition */
5895   dw_offset die_offset;
5896   unsigned long die_abbrev;
5897   int die_mark;
5898   /* Die is used and must not be pruned as unused.  */
5899   int die_perennial_p;
5900   unsigned int decl_id;
5901   enum dwarf_tag die_tag;
5902 }
5903 die_node;
5904
5905 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5906 #define FOR_EACH_CHILD(die, c, expr) do {       \
5907   c = die->die_child;                           \
5908   if (c) do {                                   \
5909     c = c->die_sib;                             \
5910     expr;                                       \
5911   } while (c != die->die_child);                \
5912 } while (0)
5913
5914 /* The pubname structure */
5915
5916 typedef struct GTY(()) pubname_struct {
5917   dw_die_ref die;
5918   const char *name;
5919 }
5920 pubname_entry;
5921
5922 DEF_VEC_O(pubname_entry);
5923 DEF_VEC_ALLOC_O(pubname_entry, gc);
5924
5925 struct GTY(()) dw_ranges_struct {
5926   /* If this is positive, it's a block number, otherwise it's a
5927      bitwise-negated index into dw_ranges_by_label.  */
5928   int num;
5929 };
5930
5931 /* A structure to hold a macinfo entry.  */
5932
5933 typedef struct GTY(()) macinfo_struct {
5934   unsigned HOST_WIDE_INT code;
5935   unsigned HOST_WIDE_INT lineno;
5936   const char *info;
5937 }
5938 macinfo_entry;
5939
5940 DEF_VEC_O(macinfo_entry);
5941 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5942
5943 struct GTY(()) dw_ranges_by_label_struct {
5944   const char *begin;
5945   const char *end;
5946 };
5947
5948 /* The comdat type node structure.  */
5949 typedef struct GTY(()) comdat_type_struct
5950 {
5951   dw_die_ref root_die;
5952   dw_die_ref type_die;
5953   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5954   struct comdat_type_struct *next;
5955 }
5956 comdat_type_node;
5957
5958 /* The limbo die list structure.  */
5959 typedef struct GTY(()) limbo_die_struct {
5960   dw_die_ref die;
5961   tree created_for;
5962   struct limbo_die_struct *next;
5963 }
5964 limbo_die_node;
5965
5966 typedef struct skeleton_chain_struct
5967 {
5968   dw_die_ref old_die;
5969   dw_die_ref new_die;
5970   struct skeleton_chain_struct *parent;
5971 }
5972 skeleton_chain_node;
5973
5974 /* How to start an assembler comment.  */
5975 #ifndef ASM_COMMENT_START
5976 #define ASM_COMMENT_START ";#"
5977 #endif
5978
5979 /* Define a macro which returns nonzero for a TYPE_DECL which was
5980    implicitly generated for a tagged type.
5981
5982    Note that unlike the gcc front end (which generates a NULL named
5983    TYPE_DECL node for each complete tagged type, each array type, and
5984    each function type node created) the g++ front end generates a
5985    _named_ TYPE_DECL node for each tagged type node created.
5986    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5987    generate a DW_TAG_typedef DIE for them.  */
5988
5989 #define TYPE_DECL_IS_STUB(decl)                         \
5990   (DECL_NAME (decl) == NULL_TREE                        \
5991    || (DECL_ARTIFICIAL (decl)                           \
5992        && is_tagged_type (TREE_TYPE (decl))             \
5993        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5994            /* This is necessary for stub decls that     \
5995               appear in nested inline functions.  */    \
5996            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5997                && (decl_ultimate_origin (decl)          \
5998                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5999
6000 /* Information concerning the compilation unit's programming
6001    language, and compiler version.  */
6002
6003 /* Fixed size portion of the DWARF compilation unit header.  */
6004 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6005   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6006
6007 /* Fixed size portion of the DWARF comdat type unit header.  */
6008 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6009   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6010    + DWARF_OFFSET_SIZE)
6011
6012 /* Fixed size portion of public names info.  */
6013 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6014
6015 /* Fixed size portion of the address range info.  */
6016 #define DWARF_ARANGES_HEADER_SIZE                                       \
6017   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6018                 DWARF2_ADDR_SIZE * 2)                                   \
6019    - DWARF_INITIAL_LENGTH_SIZE)
6020
6021 /* Size of padding portion in the address range info.  It must be
6022    aligned to twice the pointer size.  */
6023 #define DWARF_ARANGES_PAD_SIZE \
6024   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6025                 DWARF2_ADDR_SIZE * 2)                              \
6026    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6027
6028 /* Use assembler line directives if available.  */
6029 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6030 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6031 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6032 #else
6033 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6034 #endif
6035 #endif
6036
6037 /* Minimum line offset in a special line info. opcode.
6038    This value was chosen to give a reasonable range of values.  */
6039 #define DWARF_LINE_BASE  -10
6040
6041 /* First special line opcode - leave room for the standard opcodes.  */
6042 #define DWARF_LINE_OPCODE_BASE  10
6043
6044 /* Range of line offsets in a special line info. opcode.  */
6045 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6046
6047 /* Flag that indicates the initial value of the is_stmt_start flag.
6048    In the present implementation, we do not mark any lines as
6049    the beginning of a source statement, because that information
6050    is not made available by the GCC front-end.  */
6051 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6052
6053 /* Maximum number of operations per instruction bundle.  */
6054 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6055 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6056 #endif
6057
6058 /* This location is used by calc_die_sizes() to keep track
6059    the offset of each DIE within the .debug_info section.  */
6060 static unsigned long next_die_offset;
6061
6062 /* Record the root of the DIE's built for the current compilation unit.  */
6063 static GTY(()) dw_die_ref single_comp_unit_die;
6064
6065 /* A list of type DIEs that have been separated into comdat sections.  */
6066 static GTY(()) comdat_type_node *comdat_type_list;
6067
6068 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6069 static GTY(()) limbo_die_node *limbo_die_list;
6070
6071 /* A list of DIEs for which we may have to generate
6072    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6073 static GTY(()) limbo_die_node *deferred_asm_name;
6074
6075 /* Filenames referenced by this compilation unit.  */
6076 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6077
6078 /* A hash table of references to DIE's that describe declarations.
6079    The key is a DECL_UID() which is a unique number identifying each decl.  */
6080 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6081
6082 /* A hash table of references to DIE's that describe COMMON blocks.
6083    The key is DECL_UID() ^ die_parent.  */
6084 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6085
6086 typedef struct GTY(()) die_arg_entry_struct {
6087     dw_die_ref die;
6088     tree arg;
6089 } die_arg_entry;
6090
6091 DEF_VEC_O(die_arg_entry);
6092 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6093
6094 /* Node of the variable location list.  */
6095 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6096   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6097      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6098      in mode of the EXPR_LIST node and first EXPR_LIST operand
6099      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6100      location or NULL for padding.  For larger bitsizes,
6101      mode is 0 and first operand is a CONCAT with bitsize
6102      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6103      NULL as second operand.  */
6104   rtx GTY (()) loc;
6105   const char * GTY (()) label;
6106   struct var_loc_node * GTY (()) next;
6107 };
6108
6109 /* Variable location list.  */
6110 struct GTY (()) var_loc_list_def {
6111   struct var_loc_node * GTY (()) first;
6112
6113   /* Pointer to the last but one or last element of the
6114      chained list.  If the list is empty, both first and
6115      last are NULL, if the list contains just one node
6116      or the last node certainly is not redundant, it points
6117      to the last node, otherwise points to the last but one.
6118      Do not mark it for GC because it is marked through the chain.  */
6119   struct var_loc_node * GTY ((skip ("%h"))) last;
6120
6121   /* DECL_UID of the variable decl.  */
6122   unsigned int decl_id;
6123 };
6124 typedef struct var_loc_list_def var_loc_list;
6125
6126 /* Call argument location list.  */
6127 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6128   rtx GTY (()) call_arg_loc_note;
6129   const char * GTY (()) label;
6130   tree GTY (()) block;
6131   bool tail_call_p;
6132   rtx GTY (()) symbol_ref;
6133   struct call_arg_loc_node * GTY (()) next;
6134 };
6135
6136
6137 /* Table of decl location linked lists.  */
6138 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6139
6140 /* Head and tail of call_arg_loc chain.  */
6141 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6142 static struct call_arg_loc_node *call_arg_loc_last;
6143
6144 /* Number of call sites in the current function.  */
6145 static int call_site_count = -1;
6146 /* Number of tail call sites in the current function.  */
6147 static int tail_call_site_count = -1;
6148
6149 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6150    DIEs.  */
6151 static VEC (dw_die_ref, heap) *block_map;
6152
6153 /* A pointer to the base of a list of references to DIE's that
6154    are uniquely identified by their tag, presence/absence of
6155    children DIE's, and list of attribute/value pairs.  */
6156 static GTY((length ("abbrev_die_table_allocated")))
6157   dw_die_ref *abbrev_die_table;
6158
6159 /* Number of elements currently allocated for abbrev_die_table.  */
6160 static GTY(()) unsigned abbrev_die_table_allocated;
6161
6162 /* Number of elements in type_die_table currently in use.  */
6163 static GTY(()) unsigned abbrev_die_table_in_use;
6164
6165 /* Size (in elements) of increments by which we may expand the
6166    abbrev_die_table.  */
6167 #define ABBREV_DIE_TABLE_INCREMENT 256
6168
6169 /* A pointer to the base of a table that contains line information
6170    for each source code line in .text in the compilation unit.  */
6171 static GTY((length ("line_info_table_allocated")))
6172      dw_line_info_ref line_info_table;
6173
6174 /* Number of elements currently allocated for line_info_table.  */
6175 static GTY(()) unsigned line_info_table_allocated;
6176
6177 /* Number of elements in line_info_table currently in use.  */
6178 static GTY(()) unsigned line_info_table_in_use;
6179
6180 /* A pointer to the base of a table that contains line information
6181    for each source code line outside of .text in the compilation unit.  */
6182 static GTY ((length ("separate_line_info_table_allocated")))
6183      dw_separate_line_info_ref separate_line_info_table;
6184
6185 /* Number of elements currently allocated for separate_line_info_table.  */
6186 static GTY(()) unsigned separate_line_info_table_allocated;
6187
6188 /* Number of elements in separate_line_info_table currently in use.  */
6189 static GTY(()) unsigned separate_line_info_table_in_use;
6190
6191 /* Size (in elements) of increments by which we may expand the
6192    line_info_table.  */
6193 #define LINE_INFO_TABLE_INCREMENT 1024
6194
6195 /* A flag to tell pubnames/types export if there is an info section to
6196    refer to.  */
6197 static bool info_section_emitted;
6198
6199 /* A pointer to the base of a table that contains a list of publicly
6200    accessible names.  */
6201 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6202
6203 /* A pointer to the base of a table that contains a list of publicly
6204    accessible types.  */
6205 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6206
6207 /* A pointer to the base of a table that contains a list of macro
6208    defines/undefines (and file start/end markers).  */
6209 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6210
6211 /* Array of dies for which we should generate .debug_arange info.  */
6212 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6213
6214 /* Number of elements currently allocated for arange_table.  */
6215 static GTY(()) unsigned arange_table_allocated;
6216
6217 /* Number of elements in arange_table currently in use.  */
6218 static GTY(()) unsigned arange_table_in_use;
6219
6220 /* Size (in elements) of increments by which we may expand the
6221    arange_table.  */
6222 #define ARANGE_TABLE_INCREMENT 64
6223
6224 /* Array of dies for which we should generate .debug_ranges info.  */
6225 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6226
6227 /* Number of elements currently allocated for ranges_table.  */
6228 static GTY(()) unsigned ranges_table_allocated;
6229
6230 /* Number of elements in ranges_table currently in use.  */
6231 static GTY(()) unsigned ranges_table_in_use;
6232
6233 /* Array of pairs of labels referenced in ranges_table.  */
6234 static GTY ((length ("ranges_by_label_allocated")))
6235      dw_ranges_by_label_ref ranges_by_label;
6236
6237 /* Number of elements currently allocated for ranges_by_label.  */
6238 static GTY(()) unsigned ranges_by_label_allocated;
6239
6240 /* Number of elements in ranges_by_label currently in use.  */
6241 static GTY(()) unsigned ranges_by_label_in_use;
6242
6243 /* Size (in elements) of increments by which we may expand the
6244    ranges_table.  */
6245 #define RANGES_TABLE_INCREMENT 64
6246
6247 /* Whether we have location lists that need outputting */
6248 static GTY(()) bool have_location_lists;
6249
6250 /* Unique label counter.  */
6251 static GTY(()) unsigned int loclabel_num;
6252
6253 /* Unique label counter for point-of-call tables.  */
6254 static GTY(()) unsigned int poc_label_num;
6255
6256 /* Record whether the function being analyzed contains inlined functions.  */
6257 static int current_function_has_inlines;
6258
6259 /* The last file entry emitted by maybe_emit_file().  */
6260 static GTY(()) struct dwarf_file_data * last_emitted_file;
6261
6262 /* Number of internal labels generated by gen_internal_sym().  */
6263 static GTY(()) int label_num;
6264
6265 /* Cached result of previous call to lookup_filename.  */
6266 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6267
6268 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6269
6270 /* Instances of generic types for which we need to generate debug
6271    info that describe their generic parameters and arguments. That
6272    generation needs to happen once all types are properly laid out so
6273    we do it at the end of compilation.  */
6274 static GTY(()) VEC(tree,gc) *generic_type_instances;
6275
6276 /* Offset from the "steady-state frame pointer" to the frame base,
6277    within the current function.  */
6278 static HOST_WIDE_INT frame_pointer_fb_offset;
6279
6280 /* Forward declarations for functions defined in this file.  */
6281
6282 static int is_pseudo_reg (const_rtx);
6283 static tree type_main_variant (tree);
6284 static int is_tagged_type (const_tree);
6285 static const char *dwarf_tag_name (unsigned);
6286 static const char *dwarf_attr_name (unsigned);
6287 static const char *dwarf_form_name (unsigned);
6288 static tree decl_ultimate_origin (const_tree);
6289 static tree decl_class_context (tree);
6290 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6291 static inline enum dw_val_class AT_class (dw_attr_ref);
6292 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6293 static inline unsigned AT_flag (dw_attr_ref);
6294 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6295 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6296 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6297 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6298 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6299                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6300 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6301                                unsigned int, unsigned char *);
6302 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6303 static hashval_t debug_str_do_hash (const void *);
6304 static int debug_str_eq (const void *, const void *);
6305 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6306 static inline const char *AT_string (dw_attr_ref);
6307 static enum dwarf_form AT_string_form (dw_attr_ref);
6308 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6309 static void add_AT_specification (dw_die_ref, dw_die_ref);
6310 static inline dw_die_ref AT_ref (dw_attr_ref);
6311 static inline int AT_ref_external (dw_attr_ref);
6312 static inline void set_AT_ref_external (dw_attr_ref, int);
6313 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6314 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6315 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6316 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6317                              dw_loc_list_ref);
6318 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6319 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6320 static inline rtx AT_addr (dw_attr_ref);
6321 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6322 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6323 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6324 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6325                            unsigned HOST_WIDE_INT);
6326 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6327                                unsigned long);
6328 static inline const char *AT_lbl (dw_attr_ref);
6329 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6330 static const char *get_AT_low_pc (dw_die_ref);
6331 static const char *get_AT_hi_pc (dw_die_ref);
6332 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6333 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6334 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6335 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6336 static bool is_cxx (void);
6337 static bool is_fortran (void);
6338 static bool is_ada (void);
6339 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6340 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6341 static void add_child_die (dw_die_ref, dw_die_ref);
6342 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6343 static dw_die_ref lookup_type_die (tree);
6344 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6345 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6346 static void equate_type_number_to_die (tree, dw_die_ref);
6347 static hashval_t decl_die_table_hash (const void *);
6348 static int decl_die_table_eq (const void *, const void *);
6349 static dw_die_ref lookup_decl_die (tree);
6350 static hashval_t common_block_die_table_hash (const void *);
6351 static int common_block_die_table_eq (const void *, const void *);
6352 static hashval_t decl_loc_table_hash (const void *);
6353 static int decl_loc_table_eq (const void *, const void *);
6354 static var_loc_list *lookup_decl_loc (const_tree);
6355 static void equate_decl_number_to_die (tree, dw_die_ref);
6356 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6357 static void print_spaces (FILE *);
6358 static void print_die (dw_die_ref, FILE *);
6359 static void print_dwarf_line_table (FILE *);
6360 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6361 static dw_die_ref pop_compile_unit (dw_die_ref);
6362 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6363 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6364 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6365 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6366 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6367 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6368 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6369                                    struct md5_ctx *, int *);
6370 struct checksum_attributes;
6371 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6372 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6373 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6374 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6375 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6376 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6377 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6378 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6379 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6380 static void compute_section_prefix (dw_die_ref);
6381 static int is_type_die (dw_die_ref);
6382 static int is_comdat_die (dw_die_ref);
6383 static int is_symbol_die (dw_die_ref);
6384 static void assign_symbol_names (dw_die_ref);
6385 static void break_out_includes (dw_die_ref);
6386 static int is_declaration_die (dw_die_ref);
6387 static int should_move_die_to_comdat (dw_die_ref);
6388 static dw_die_ref clone_as_declaration (dw_die_ref);
6389 static dw_die_ref clone_die (dw_die_ref);
6390 static dw_die_ref clone_tree (dw_die_ref);
6391 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6392 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6393 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6394 static dw_die_ref generate_skeleton (dw_die_ref);
6395 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6396                                                          dw_die_ref);
6397 static void break_out_comdat_types (dw_die_ref);
6398 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6399 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6400 static void copy_decls_for_unworthy_types (dw_die_ref);
6401
6402 static hashval_t htab_cu_hash (const void *);
6403 static int htab_cu_eq (const void *, const void *);
6404 static void htab_cu_del (void *);
6405 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6406 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6407 static void add_sibling_attributes (dw_die_ref);
6408 static void build_abbrev_table (dw_die_ref);
6409 static void output_location_lists (dw_die_ref);
6410 static int constant_size (unsigned HOST_WIDE_INT);
6411 static unsigned long size_of_die (dw_die_ref);
6412 static void calc_die_sizes (dw_die_ref);
6413 static void mark_dies (dw_die_ref);
6414 static void unmark_dies (dw_die_ref);
6415 static void unmark_all_dies (dw_die_ref);
6416 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6417 static unsigned long size_of_aranges (void);
6418 static enum dwarf_form value_format (dw_attr_ref);
6419 static void output_value_format (dw_attr_ref);
6420 static void output_abbrev_section (void);
6421 static void output_die_symbol (dw_die_ref);
6422 static void output_die (dw_die_ref);
6423 static void output_compilation_unit_header (void);
6424 static void output_comp_unit (dw_die_ref, int);
6425 static void output_comdat_type_unit (comdat_type_node *);
6426 static const char *dwarf2_name (tree, int);
6427 static void add_pubname (tree, dw_die_ref);
6428 static void add_pubname_string (const char *, dw_die_ref);
6429 static void add_pubtype (tree, dw_die_ref);
6430 static void output_pubnames (VEC (pubname_entry,gc) *);
6431 static void add_arange (tree, dw_die_ref);
6432 static void output_aranges (void);
6433 static unsigned int add_ranges_num (int);
6434 static unsigned int add_ranges (const_tree);
6435 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6436                                   bool *);
6437 static void output_ranges (void);
6438 static void output_line_info (void);
6439 static void output_file_names (void);
6440 static dw_die_ref base_type_die (tree);
6441 static int is_base_type (tree);
6442 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6443 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6444 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6445 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6446 static int type_is_enum (const_tree);
6447 static unsigned int dbx_reg_number (const_rtx);
6448 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6449 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6450 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6451                                                 enum var_init_status);
6452 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6453                                                      enum var_init_status);
6454 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6455                                          enum var_init_status);
6456 static int is_based_loc (const_rtx);
6457 static int resolve_one_addr (rtx *, void *);
6458 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6459                                                enum var_init_status);
6460 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6461                                         enum var_init_status);
6462 static dw_loc_list_ref loc_list_from_tree (tree, int);
6463 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6464 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6465 static tree field_type (const_tree);
6466 static unsigned int simple_type_align_in_bits (const_tree);
6467 static unsigned int simple_decl_align_in_bits (const_tree);
6468 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6469 static HOST_WIDE_INT field_byte_offset (const_tree);
6470 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6471                                          dw_loc_list_ref);
6472 static void add_data_member_location_attribute (dw_die_ref, tree);
6473 static bool add_const_value_attribute (dw_die_ref, rtx);
6474 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6475 static void insert_double (double_int, unsigned char *);
6476 static void insert_float (const_rtx, unsigned char *);
6477 static rtx rtl_for_decl_location (tree);
6478 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6479                                                    enum dwarf_attribute);
6480 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6481 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6482 static void add_name_attribute (dw_die_ref, const char *);
6483 static void add_comp_dir_attribute (dw_die_ref);
6484 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6485 static void add_subscript_info (dw_die_ref, tree, bool);
6486 static void add_byte_size_attribute (dw_die_ref, tree);
6487 static void add_bit_offset_attribute (dw_die_ref, tree);
6488 static void add_bit_size_attribute (dw_die_ref, tree);
6489 static void add_prototyped_attribute (dw_die_ref, tree);
6490 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6491 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6492 static void add_src_coords_attributes (dw_die_ref, tree);
6493 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6494 static void push_decl_scope (tree);
6495 static void pop_decl_scope (void);
6496 static dw_die_ref scope_die_for (tree, dw_die_ref);
6497 static inline int local_scope_p (dw_die_ref);
6498 static inline int class_scope_p (dw_die_ref);
6499 static inline int class_or_namespace_scope_p (dw_die_ref);
6500 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6501 static void add_calling_convention_attribute (dw_die_ref, tree);
6502 static const char *type_tag (const_tree);
6503 static tree member_declared_type (const_tree);
6504 #if 0
6505 static const char *decl_start_label (tree);
6506 #endif
6507 static void gen_array_type_die (tree, dw_die_ref);
6508 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6509 #if 0
6510 static void gen_entry_point_die (tree, dw_die_ref);
6511 #endif
6512 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6513 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6514 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6515 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6516 static void gen_formal_types_die (tree, dw_die_ref);
6517 static void gen_subprogram_die (tree, dw_die_ref);
6518 static void gen_variable_die (tree, tree, dw_die_ref);
6519 static void gen_const_die (tree, dw_die_ref);
6520 static void gen_label_die (tree, dw_die_ref);
6521 static void gen_lexical_block_die (tree, dw_die_ref, int);
6522 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6523 static void gen_field_die (tree, dw_die_ref);
6524 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6525 static dw_die_ref gen_compile_unit_die (const char *);
6526 static void gen_inheritance_die (tree, tree, dw_die_ref);
6527 static void gen_member_die (tree, dw_die_ref);
6528 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6529                                                 enum debug_info_usage);
6530 static void gen_subroutine_type_die (tree, dw_die_ref);
6531 static void gen_typedef_die (tree, dw_die_ref);
6532 static void gen_type_die (tree, dw_die_ref);
6533 static void gen_block_die (tree, dw_die_ref, int);
6534 static void decls_for_scope (tree, dw_die_ref, int);
6535 static int is_redundant_typedef (const_tree);
6536 static bool is_naming_typedef_decl (const_tree);
6537 static inline dw_die_ref get_context_die (tree);
6538 static void gen_namespace_die (tree, dw_die_ref);
6539 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6540 static dw_die_ref force_decl_die (tree);
6541 static dw_die_ref force_type_die (tree);
6542 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6543 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6544 static struct dwarf_file_data * lookup_filename (const char *);
6545 static void retry_incomplete_types (void);
6546 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6547 static void gen_generic_params_dies (tree);
6548 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6549 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6550 static void splice_child_die (dw_die_ref, dw_die_ref);
6551 static int file_info_cmp (const void *, const void *);
6552 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6553                                      const char *, const char *);
6554 static void output_loc_list (dw_loc_list_ref);
6555 static char *gen_internal_sym (const char *);
6556
6557 static void prune_unmark_dies (dw_die_ref);
6558 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6559 static void prune_unused_types_mark (dw_die_ref, int);
6560 static void prune_unused_types_walk (dw_die_ref);
6561 static void prune_unused_types_walk_attribs (dw_die_ref);
6562 static void prune_unused_types_prune (dw_die_ref);
6563 static void prune_unused_types (void);
6564 static int maybe_emit_file (struct dwarf_file_data *fd);
6565 static inline const char *AT_vms_delta1 (dw_attr_ref);
6566 static inline const char *AT_vms_delta2 (dw_attr_ref);
6567 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6568                                      const char *, const char *);
6569 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6570 static void gen_remaining_tmpl_value_param_die_attribute (void);
6571 static bool generic_type_p (tree);
6572 static void schedule_generic_params_dies_gen (tree t);
6573 static void gen_scheduled_generic_parms_dies (void);
6574
6575 /* Section names used to hold DWARF debugging information.  */
6576 #ifndef DEBUG_INFO_SECTION
6577 #define DEBUG_INFO_SECTION      ".debug_info"
6578 #endif
6579 #ifndef DEBUG_ABBREV_SECTION
6580 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6581 #endif
6582 #ifndef DEBUG_ARANGES_SECTION
6583 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6584 #endif
6585 #ifndef DEBUG_MACINFO_SECTION
6586 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6587 #endif
6588 #ifndef DEBUG_LINE_SECTION
6589 #define DEBUG_LINE_SECTION      ".debug_line"
6590 #endif
6591 #ifndef DEBUG_LOC_SECTION
6592 #define DEBUG_LOC_SECTION       ".debug_loc"
6593 #endif
6594 #ifndef DEBUG_PUBNAMES_SECTION
6595 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6596 #endif
6597 #ifndef DEBUG_PUBTYPES_SECTION
6598 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6599 #endif
6600 #ifndef DEBUG_STR_SECTION
6601 #define DEBUG_STR_SECTION       ".debug_str"
6602 #endif
6603 #ifndef DEBUG_RANGES_SECTION
6604 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6605 #endif
6606
6607 /* Standard ELF section names for compiled code and data.  */
6608 #ifndef TEXT_SECTION_NAME
6609 #define TEXT_SECTION_NAME       ".text"
6610 #endif
6611
6612 /* Section flags for .debug_str section.  */
6613 #define DEBUG_STR_SECTION_FLAGS \
6614   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6615    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6616    : SECTION_DEBUG)
6617
6618 /* Labels we insert at beginning sections we can reference instead of
6619    the section names themselves.  */
6620
6621 #ifndef TEXT_SECTION_LABEL
6622 #define TEXT_SECTION_LABEL              "Ltext"
6623 #endif
6624 #ifndef COLD_TEXT_SECTION_LABEL
6625 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6626 #endif
6627 #ifndef DEBUG_LINE_SECTION_LABEL
6628 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6629 #endif
6630 #ifndef DEBUG_INFO_SECTION_LABEL
6631 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6632 #endif
6633 #ifndef DEBUG_ABBREV_SECTION_LABEL
6634 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6635 #endif
6636 #ifndef DEBUG_LOC_SECTION_LABEL
6637 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6638 #endif
6639 #ifndef DEBUG_RANGES_SECTION_LABEL
6640 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6641 #endif
6642 #ifndef DEBUG_MACINFO_SECTION_LABEL
6643 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6644 #endif
6645
6646
6647 /* Definitions of defaults for formats and names of various special
6648    (artificial) labels which may be generated within this file (when the -g
6649    options is used and DWARF2_DEBUGGING_INFO is in effect.
6650    If necessary, these may be overridden from within the tm.h file, but
6651    typically, overriding these defaults is unnecessary.  */
6652
6653 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6654 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6655 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6656 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6657 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6658 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6659 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6660 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6661 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6662 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6663
6664 #ifndef TEXT_END_LABEL
6665 #define TEXT_END_LABEL          "Letext"
6666 #endif
6667 #ifndef COLD_END_LABEL
6668 #define COLD_END_LABEL          "Letext_cold"
6669 #endif
6670 #ifndef BLOCK_BEGIN_LABEL
6671 #define BLOCK_BEGIN_LABEL       "LBB"
6672 #endif
6673 #ifndef BLOCK_END_LABEL
6674 #define BLOCK_END_LABEL         "LBE"
6675 #endif
6676 #ifndef LINE_CODE_LABEL
6677 #define LINE_CODE_LABEL         "LM"
6678 #endif
6679 #ifndef SEPARATE_LINE_CODE_LABEL
6680 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6681 #endif
6682
6683 \f
6684 /* Return the root of the DIE's built for the current compilation unit.  */
6685 static dw_die_ref
6686 comp_unit_die (void)
6687 {
6688   if (!single_comp_unit_die)
6689     single_comp_unit_die = gen_compile_unit_die (NULL);
6690   return single_comp_unit_die;
6691 }
6692
6693 /* We allow a language front-end to designate a function that is to be
6694    called to "demangle" any name before it is put into a DIE.  */
6695
6696 static const char *(*demangle_name_func) (const char *);
6697
6698 void
6699 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6700 {
6701   demangle_name_func = func;
6702 }
6703
6704 /* Test if rtl node points to a pseudo register.  */
6705
6706 static inline int
6707 is_pseudo_reg (const_rtx rtl)
6708 {
6709   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6710           || (GET_CODE (rtl) == SUBREG
6711               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6712 }
6713
6714 /* Return a reference to a type, with its const and volatile qualifiers
6715    removed.  */
6716
6717 static inline tree
6718 type_main_variant (tree type)
6719 {
6720   type = TYPE_MAIN_VARIANT (type);
6721
6722   /* ??? There really should be only one main variant among any group of
6723      variants of a given type (and all of the MAIN_VARIANT values for all
6724      members of the group should point to that one type) but sometimes the C
6725      front-end messes this up for array types, so we work around that bug
6726      here.  */
6727   if (TREE_CODE (type) == ARRAY_TYPE)
6728     while (type != TYPE_MAIN_VARIANT (type))
6729       type = TYPE_MAIN_VARIANT (type);
6730
6731   return type;
6732 }
6733
6734 /* Return nonzero if the given type node represents a tagged type.  */
6735
6736 static inline int
6737 is_tagged_type (const_tree type)
6738 {
6739   enum tree_code code = TREE_CODE (type);
6740
6741   return (code == RECORD_TYPE || code == UNION_TYPE
6742           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6743 }
6744
6745 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6746
6747 static void
6748 get_ref_die_offset_label (char *label, dw_die_ref ref)
6749 {
6750   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6751 }
6752
6753 /* Convert a DIE tag into its string name.  */
6754
6755 static const char *
6756 dwarf_tag_name (unsigned int tag)
6757 {
6758   switch (tag)
6759     {
6760     case DW_TAG_padding:
6761       return "DW_TAG_padding";
6762     case DW_TAG_array_type:
6763       return "DW_TAG_array_type";
6764     case DW_TAG_class_type:
6765       return "DW_TAG_class_type";
6766     case DW_TAG_entry_point:
6767       return "DW_TAG_entry_point";
6768     case DW_TAG_enumeration_type:
6769       return "DW_TAG_enumeration_type";
6770     case DW_TAG_formal_parameter:
6771       return "DW_TAG_formal_parameter";
6772     case DW_TAG_imported_declaration:
6773       return "DW_TAG_imported_declaration";
6774     case DW_TAG_label:
6775       return "DW_TAG_label";
6776     case DW_TAG_lexical_block:
6777       return "DW_TAG_lexical_block";
6778     case DW_TAG_member:
6779       return "DW_TAG_member";
6780     case DW_TAG_pointer_type:
6781       return "DW_TAG_pointer_type";
6782     case DW_TAG_reference_type:
6783       return "DW_TAG_reference_type";
6784     case DW_TAG_compile_unit:
6785       return "DW_TAG_compile_unit";
6786     case DW_TAG_string_type:
6787       return "DW_TAG_string_type";
6788     case DW_TAG_structure_type:
6789       return "DW_TAG_structure_type";
6790     case DW_TAG_subroutine_type:
6791       return "DW_TAG_subroutine_type";
6792     case DW_TAG_typedef:
6793       return "DW_TAG_typedef";
6794     case DW_TAG_union_type:
6795       return "DW_TAG_union_type";
6796     case DW_TAG_unspecified_parameters:
6797       return "DW_TAG_unspecified_parameters";
6798     case DW_TAG_variant:
6799       return "DW_TAG_variant";
6800     case DW_TAG_common_block:
6801       return "DW_TAG_common_block";
6802     case DW_TAG_common_inclusion:
6803       return "DW_TAG_common_inclusion";
6804     case DW_TAG_inheritance:
6805       return "DW_TAG_inheritance";
6806     case DW_TAG_inlined_subroutine:
6807       return "DW_TAG_inlined_subroutine";
6808     case DW_TAG_module:
6809       return "DW_TAG_module";
6810     case DW_TAG_ptr_to_member_type:
6811       return "DW_TAG_ptr_to_member_type";
6812     case DW_TAG_set_type:
6813       return "DW_TAG_set_type";
6814     case DW_TAG_subrange_type:
6815       return "DW_TAG_subrange_type";
6816     case DW_TAG_with_stmt:
6817       return "DW_TAG_with_stmt";
6818     case DW_TAG_access_declaration:
6819       return "DW_TAG_access_declaration";
6820     case DW_TAG_base_type:
6821       return "DW_TAG_base_type";
6822     case DW_TAG_catch_block:
6823       return "DW_TAG_catch_block";
6824     case DW_TAG_const_type:
6825       return "DW_TAG_const_type";
6826     case DW_TAG_constant:
6827       return "DW_TAG_constant";
6828     case DW_TAG_enumerator:
6829       return "DW_TAG_enumerator";
6830     case DW_TAG_file_type:
6831       return "DW_TAG_file_type";
6832     case DW_TAG_friend:
6833       return "DW_TAG_friend";
6834     case DW_TAG_namelist:
6835       return "DW_TAG_namelist";
6836     case DW_TAG_namelist_item:
6837       return "DW_TAG_namelist_item";
6838     case DW_TAG_packed_type:
6839       return "DW_TAG_packed_type";
6840     case DW_TAG_subprogram:
6841       return "DW_TAG_subprogram";
6842     case DW_TAG_template_type_param:
6843       return "DW_TAG_template_type_param";
6844     case DW_TAG_template_value_param:
6845       return "DW_TAG_template_value_param";
6846     case DW_TAG_thrown_type:
6847       return "DW_TAG_thrown_type";
6848     case DW_TAG_try_block:
6849       return "DW_TAG_try_block";
6850     case DW_TAG_variant_part:
6851       return "DW_TAG_variant_part";
6852     case DW_TAG_variable:
6853       return "DW_TAG_variable";
6854     case DW_TAG_volatile_type:
6855       return "DW_TAG_volatile_type";
6856     case DW_TAG_dwarf_procedure:
6857       return "DW_TAG_dwarf_procedure";
6858     case DW_TAG_restrict_type:
6859       return "DW_TAG_restrict_type";
6860     case DW_TAG_interface_type:
6861       return "DW_TAG_interface_type";
6862     case DW_TAG_namespace:
6863       return "DW_TAG_namespace";
6864     case DW_TAG_imported_module:
6865       return "DW_TAG_imported_module";
6866     case DW_TAG_unspecified_type:
6867       return "DW_TAG_unspecified_type";
6868     case DW_TAG_partial_unit:
6869       return "DW_TAG_partial_unit";
6870     case DW_TAG_imported_unit:
6871       return "DW_TAG_imported_unit";
6872     case DW_TAG_condition:
6873       return "DW_TAG_condition";
6874     case DW_TAG_shared_type:
6875       return "DW_TAG_shared_type";
6876     case DW_TAG_type_unit:
6877       return "DW_TAG_type_unit";
6878     case DW_TAG_rvalue_reference_type:
6879       return "DW_TAG_rvalue_reference_type";
6880     case DW_TAG_template_alias:
6881       return "DW_TAG_template_alias";
6882     case DW_TAG_GNU_template_parameter_pack:
6883       return "DW_TAG_GNU_template_parameter_pack";
6884     case DW_TAG_GNU_formal_parameter_pack:
6885       return "DW_TAG_GNU_formal_parameter_pack";
6886     case DW_TAG_MIPS_loop:
6887       return "DW_TAG_MIPS_loop";
6888     case DW_TAG_format_label:
6889       return "DW_TAG_format_label";
6890     case DW_TAG_function_template:
6891       return "DW_TAG_function_template";
6892     case DW_TAG_class_template:
6893       return "DW_TAG_class_template";
6894     case DW_TAG_GNU_BINCL:
6895       return "DW_TAG_GNU_BINCL";
6896     case DW_TAG_GNU_EINCL:
6897       return "DW_TAG_GNU_EINCL";
6898     case DW_TAG_GNU_template_template_param:
6899       return "DW_TAG_GNU_template_template_param";
6900     case DW_TAG_GNU_call_site:
6901       return "DW_TAG_GNU_call_site";
6902     case DW_TAG_GNU_call_site_parameter:
6903       return "DW_TAG_GNU_call_site_parameter";
6904     default:
6905       return "DW_TAG_<unknown>";
6906     }
6907 }
6908
6909 /* Convert a DWARF attribute code into its string name.  */
6910
6911 static const char *
6912 dwarf_attr_name (unsigned int attr)
6913 {
6914   switch (attr)
6915     {
6916     case DW_AT_sibling:
6917       return "DW_AT_sibling";
6918     case DW_AT_location:
6919       return "DW_AT_location";
6920     case DW_AT_name:
6921       return "DW_AT_name";
6922     case DW_AT_ordering:
6923       return "DW_AT_ordering";
6924     case DW_AT_subscr_data:
6925       return "DW_AT_subscr_data";
6926     case DW_AT_byte_size:
6927       return "DW_AT_byte_size";
6928     case DW_AT_bit_offset:
6929       return "DW_AT_bit_offset";
6930     case DW_AT_bit_size:
6931       return "DW_AT_bit_size";
6932     case DW_AT_element_list:
6933       return "DW_AT_element_list";
6934     case DW_AT_stmt_list:
6935       return "DW_AT_stmt_list";
6936     case DW_AT_low_pc:
6937       return "DW_AT_low_pc";
6938     case DW_AT_high_pc:
6939       return "DW_AT_high_pc";
6940     case DW_AT_language:
6941       return "DW_AT_language";
6942     case DW_AT_member:
6943       return "DW_AT_member";
6944     case DW_AT_discr:
6945       return "DW_AT_discr";
6946     case DW_AT_discr_value:
6947       return "DW_AT_discr_value";
6948     case DW_AT_visibility:
6949       return "DW_AT_visibility";
6950     case DW_AT_import:
6951       return "DW_AT_import";
6952     case DW_AT_string_length:
6953       return "DW_AT_string_length";
6954     case DW_AT_common_reference:
6955       return "DW_AT_common_reference";
6956     case DW_AT_comp_dir:
6957       return "DW_AT_comp_dir";
6958     case DW_AT_const_value:
6959       return "DW_AT_const_value";
6960     case DW_AT_containing_type:
6961       return "DW_AT_containing_type";
6962     case DW_AT_default_value:
6963       return "DW_AT_default_value";
6964     case DW_AT_inline:
6965       return "DW_AT_inline";
6966     case DW_AT_is_optional:
6967       return "DW_AT_is_optional";
6968     case DW_AT_lower_bound:
6969       return "DW_AT_lower_bound";
6970     case DW_AT_producer:
6971       return "DW_AT_producer";
6972     case DW_AT_prototyped:
6973       return "DW_AT_prototyped";
6974     case DW_AT_return_addr:
6975       return "DW_AT_return_addr";
6976     case DW_AT_start_scope:
6977       return "DW_AT_start_scope";
6978     case DW_AT_bit_stride:
6979       return "DW_AT_bit_stride";
6980     case DW_AT_upper_bound:
6981       return "DW_AT_upper_bound";
6982     case DW_AT_abstract_origin:
6983       return "DW_AT_abstract_origin";
6984     case DW_AT_accessibility:
6985       return "DW_AT_accessibility";
6986     case DW_AT_address_class:
6987       return "DW_AT_address_class";
6988     case DW_AT_artificial:
6989       return "DW_AT_artificial";
6990     case DW_AT_base_types:
6991       return "DW_AT_base_types";
6992     case DW_AT_calling_convention:
6993       return "DW_AT_calling_convention";
6994     case DW_AT_count:
6995       return "DW_AT_count";
6996     case DW_AT_data_member_location:
6997       return "DW_AT_data_member_location";
6998     case DW_AT_decl_column:
6999       return "DW_AT_decl_column";
7000     case DW_AT_decl_file:
7001       return "DW_AT_decl_file";
7002     case DW_AT_decl_line:
7003       return "DW_AT_decl_line";
7004     case DW_AT_declaration:
7005       return "DW_AT_declaration";
7006     case DW_AT_discr_list:
7007       return "DW_AT_discr_list";
7008     case DW_AT_encoding:
7009       return "DW_AT_encoding";
7010     case DW_AT_external:
7011       return "DW_AT_external";
7012     case DW_AT_explicit:
7013       return "DW_AT_explicit";
7014     case DW_AT_frame_base:
7015       return "DW_AT_frame_base";
7016     case DW_AT_friend:
7017       return "DW_AT_friend";
7018     case DW_AT_identifier_case:
7019       return "DW_AT_identifier_case";
7020     case DW_AT_macro_info:
7021       return "DW_AT_macro_info";
7022     case DW_AT_namelist_items:
7023       return "DW_AT_namelist_items";
7024     case DW_AT_priority:
7025       return "DW_AT_priority";
7026     case DW_AT_segment:
7027       return "DW_AT_segment";
7028     case DW_AT_specification:
7029       return "DW_AT_specification";
7030     case DW_AT_static_link:
7031       return "DW_AT_static_link";
7032     case DW_AT_type:
7033       return "DW_AT_type";
7034     case DW_AT_use_location:
7035       return "DW_AT_use_location";
7036     case DW_AT_variable_parameter:
7037       return "DW_AT_variable_parameter";
7038     case DW_AT_virtuality:
7039       return "DW_AT_virtuality";
7040     case DW_AT_vtable_elem_location:
7041       return "DW_AT_vtable_elem_location";
7042
7043     case DW_AT_allocated:
7044       return "DW_AT_allocated";
7045     case DW_AT_associated:
7046       return "DW_AT_associated";
7047     case DW_AT_data_location:
7048       return "DW_AT_data_location";
7049     case DW_AT_byte_stride:
7050       return "DW_AT_byte_stride";
7051     case DW_AT_entry_pc:
7052       return "DW_AT_entry_pc";
7053     case DW_AT_use_UTF8:
7054       return "DW_AT_use_UTF8";
7055     case DW_AT_extension:
7056       return "DW_AT_extension";
7057     case DW_AT_ranges:
7058       return "DW_AT_ranges";
7059     case DW_AT_trampoline:
7060       return "DW_AT_trampoline";
7061     case DW_AT_call_column:
7062       return "DW_AT_call_column";
7063     case DW_AT_call_file:
7064       return "DW_AT_call_file";
7065     case DW_AT_call_line:
7066       return "DW_AT_call_line";
7067     case DW_AT_object_pointer:
7068       return "DW_AT_object_pointer";
7069
7070     case DW_AT_signature:
7071       return "DW_AT_signature";
7072     case DW_AT_main_subprogram:
7073       return "DW_AT_main_subprogram";
7074     case DW_AT_data_bit_offset:
7075       return "DW_AT_data_bit_offset";
7076     case DW_AT_const_expr:
7077       return "DW_AT_const_expr";
7078     case DW_AT_enum_class:
7079       return "DW_AT_enum_class";
7080     case DW_AT_linkage_name:
7081       return "DW_AT_linkage_name";
7082
7083     case DW_AT_MIPS_fde:
7084       return "DW_AT_MIPS_fde";
7085     case DW_AT_MIPS_loop_begin:
7086       return "DW_AT_MIPS_loop_begin";
7087     case DW_AT_MIPS_tail_loop_begin:
7088       return "DW_AT_MIPS_tail_loop_begin";
7089     case DW_AT_MIPS_epilog_begin:
7090       return "DW_AT_MIPS_epilog_begin";
7091 #if VMS_DEBUGGING_INFO
7092     case DW_AT_HP_prologue:
7093       return "DW_AT_HP_prologue";
7094 #else
7095     case DW_AT_MIPS_loop_unroll_factor:
7096       return "DW_AT_MIPS_loop_unroll_factor";
7097 #endif
7098     case DW_AT_MIPS_software_pipeline_depth:
7099       return "DW_AT_MIPS_software_pipeline_depth";
7100     case DW_AT_MIPS_linkage_name:
7101       return "DW_AT_MIPS_linkage_name";
7102 #if VMS_DEBUGGING_INFO
7103     case DW_AT_HP_epilogue:
7104       return "DW_AT_HP_epilogue";
7105 #else
7106     case DW_AT_MIPS_stride:
7107       return "DW_AT_MIPS_stride";
7108 #endif
7109     case DW_AT_MIPS_abstract_name:
7110       return "DW_AT_MIPS_abstract_name";
7111     case DW_AT_MIPS_clone_origin:
7112       return "DW_AT_MIPS_clone_origin";
7113     case DW_AT_MIPS_has_inlines:
7114       return "DW_AT_MIPS_has_inlines";
7115
7116     case DW_AT_sf_names:
7117       return "DW_AT_sf_names";
7118     case DW_AT_src_info:
7119       return "DW_AT_src_info";
7120     case DW_AT_mac_info:
7121       return "DW_AT_mac_info";
7122     case DW_AT_src_coords:
7123       return "DW_AT_src_coords";
7124     case DW_AT_body_begin:
7125       return "DW_AT_body_begin";
7126     case DW_AT_body_end:
7127       return "DW_AT_body_end";
7128     case DW_AT_GNU_vector:
7129       return "DW_AT_GNU_vector";
7130     case DW_AT_GNU_guarded_by:
7131       return "DW_AT_GNU_guarded_by";
7132     case DW_AT_GNU_pt_guarded_by:
7133       return "DW_AT_GNU_pt_guarded_by";
7134     case DW_AT_GNU_guarded:
7135       return "DW_AT_GNU_guarded";
7136     case DW_AT_GNU_pt_guarded:
7137       return "DW_AT_GNU_pt_guarded";
7138     case DW_AT_GNU_locks_excluded:
7139       return "DW_AT_GNU_locks_excluded";
7140     case DW_AT_GNU_exclusive_locks_required:
7141       return "DW_AT_GNU_exclusive_locks_required";
7142     case DW_AT_GNU_shared_locks_required:
7143       return "DW_AT_GNU_shared_locks_required";
7144     case DW_AT_GNU_odr_signature:
7145       return "DW_AT_GNU_odr_signature";
7146     case DW_AT_GNU_template_name:
7147       return "DW_AT_GNU_template_name";
7148     case DW_AT_GNU_call_site_value:
7149       return "DW_AT_GNU_call_site_value";
7150     case DW_AT_GNU_call_site_data_value:
7151       return "DW_AT_GNU_call_site_data_value";
7152     case DW_AT_GNU_call_site_target:
7153       return "DW_AT_GNU_call_site_target";
7154     case DW_AT_GNU_call_site_target_clobbered:
7155       return "DW_AT_GNU_call_site_target_clobbered";
7156     case DW_AT_GNU_tail_call:
7157       return "DW_AT_GNU_tail_call";
7158     case DW_AT_GNU_all_tail_call_sites:
7159       return "DW_AT_GNU_all_tail_call_sites";
7160     case DW_AT_GNU_all_call_sites:
7161       return "DW_AT_GNU_all_call_sites";
7162     case DW_AT_GNU_all_source_call_sites:
7163       return "DW_AT_GNU_all_source_call_sites";
7164
7165     case DW_AT_VMS_rtnbeg_pd_address:
7166       return "DW_AT_VMS_rtnbeg_pd_address";
7167
7168     default:
7169       return "DW_AT_<unknown>";
7170     }
7171 }
7172
7173 /* Convert a DWARF value form code into its string name.  */
7174
7175 static const char *
7176 dwarf_form_name (unsigned int form)
7177 {
7178   switch (form)
7179     {
7180     case DW_FORM_addr:
7181       return "DW_FORM_addr";
7182     case DW_FORM_block2:
7183       return "DW_FORM_block2";
7184     case DW_FORM_block4:
7185       return "DW_FORM_block4";
7186     case DW_FORM_data2:
7187       return "DW_FORM_data2";
7188     case DW_FORM_data4:
7189       return "DW_FORM_data4";
7190     case DW_FORM_data8:
7191       return "DW_FORM_data8";
7192     case DW_FORM_string:
7193       return "DW_FORM_string";
7194     case DW_FORM_block:
7195       return "DW_FORM_block";
7196     case DW_FORM_block1:
7197       return "DW_FORM_block1";
7198     case DW_FORM_data1:
7199       return "DW_FORM_data1";
7200     case DW_FORM_flag:
7201       return "DW_FORM_flag";
7202     case DW_FORM_sdata:
7203       return "DW_FORM_sdata";
7204     case DW_FORM_strp:
7205       return "DW_FORM_strp";
7206     case DW_FORM_udata:
7207       return "DW_FORM_udata";
7208     case DW_FORM_ref_addr:
7209       return "DW_FORM_ref_addr";
7210     case DW_FORM_ref1:
7211       return "DW_FORM_ref1";
7212     case DW_FORM_ref2:
7213       return "DW_FORM_ref2";
7214     case DW_FORM_ref4:
7215       return "DW_FORM_ref4";
7216     case DW_FORM_ref8:
7217       return "DW_FORM_ref8";
7218     case DW_FORM_ref_udata:
7219       return "DW_FORM_ref_udata";
7220     case DW_FORM_indirect:
7221       return "DW_FORM_indirect";
7222     case DW_FORM_sec_offset:
7223       return "DW_FORM_sec_offset";
7224     case DW_FORM_exprloc:
7225       return "DW_FORM_exprloc";
7226     case DW_FORM_flag_present:
7227       return "DW_FORM_flag_present";
7228     case DW_FORM_ref_sig8:
7229       return "DW_FORM_ref_sig8";
7230     default:
7231       return "DW_FORM_<unknown>";
7232     }
7233 }
7234 \f
7235 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7236    instance of an inlined instance of a decl which is local to an inline
7237    function, so we have to trace all of the way back through the origin chain
7238    to find out what sort of node actually served as the original seed for the
7239    given block.  */
7240
7241 static tree
7242 decl_ultimate_origin (const_tree decl)
7243 {
7244   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7245     return NULL_TREE;
7246
7247   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7248      nodes in the function to point to themselves; ignore that if
7249      we're trying to output the abstract instance of this function.  */
7250   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7251     return NULL_TREE;
7252
7253   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7254      most distant ancestor, this should never happen.  */
7255   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7256
7257   return DECL_ABSTRACT_ORIGIN (decl);
7258 }
7259
7260 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7261    of a virtual function may refer to a base class, so we check the 'this'
7262    parameter.  */
7263
7264 static tree
7265 decl_class_context (tree decl)
7266 {
7267   tree context = NULL_TREE;
7268
7269   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7270     context = DECL_CONTEXT (decl);
7271   else
7272     context = TYPE_MAIN_VARIANT
7273       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7274
7275   if (context && !TYPE_P (context))
7276     context = NULL_TREE;
7277
7278   return context;
7279 }
7280 \f
7281 /* Add an attribute/value pair to a DIE.  */
7282
7283 static inline void
7284 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7285 {
7286   /* Maybe this should be an assert?  */
7287   if (die == NULL)
7288     return;
7289
7290   if (die->die_attr == NULL)
7291     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7292   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7293 }
7294
7295 static inline enum dw_val_class
7296 AT_class (dw_attr_ref a)
7297 {
7298   return a->dw_attr_val.val_class;
7299 }
7300
7301 /* Add a flag value attribute to a DIE.  */
7302
7303 static inline void
7304 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7305 {
7306   dw_attr_node attr;
7307
7308   attr.dw_attr = attr_kind;
7309   attr.dw_attr_val.val_class = dw_val_class_flag;
7310   attr.dw_attr_val.v.val_flag = flag;
7311   add_dwarf_attr (die, &attr);
7312 }
7313
7314 static inline unsigned
7315 AT_flag (dw_attr_ref a)
7316 {
7317   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7318   return a->dw_attr_val.v.val_flag;
7319 }
7320
7321 /* Add a signed integer attribute value to a DIE.  */
7322
7323 static inline void
7324 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7325 {
7326   dw_attr_node attr;
7327
7328   attr.dw_attr = attr_kind;
7329   attr.dw_attr_val.val_class = dw_val_class_const;
7330   attr.dw_attr_val.v.val_int = int_val;
7331   add_dwarf_attr (die, &attr);
7332 }
7333
7334 static inline HOST_WIDE_INT
7335 AT_int (dw_attr_ref a)
7336 {
7337   gcc_assert (a && AT_class (a) == dw_val_class_const);
7338   return a->dw_attr_val.v.val_int;
7339 }
7340
7341 /* Add an unsigned integer attribute value to a DIE.  */
7342
7343 static inline void
7344 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7345                  unsigned HOST_WIDE_INT unsigned_val)
7346 {
7347   dw_attr_node attr;
7348
7349   attr.dw_attr = attr_kind;
7350   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7351   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7352   add_dwarf_attr (die, &attr);
7353 }
7354
7355 static inline unsigned HOST_WIDE_INT
7356 AT_unsigned (dw_attr_ref a)
7357 {
7358   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7359   return a->dw_attr_val.v.val_unsigned;
7360 }
7361
7362 /* Add an unsigned double integer attribute value to a DIE.  */
7363
7364 static inline void
7365 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7366                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7367 {
7368   dw_attr_node attr;
7369
7370   attr.dw_attr = attr_kind;
7371   attr.dw_attr_val.val_class = dw_val_class_const_double;
7372   attr.dw_attr_val.v.val_double.high = high;
7373   attr.dw_attr_val.v.val_double.low = low;
7374   add_dwarf_attr (die, &attr);
7375 }
7376
7377 /* Add a floating point attribute value to a DIE and return it.  */
7378
7379 static inline void
7380 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7381             unsigned int length, unsigned int elt_size, unsigned char *array)
7382 {
7383   dw_attr_node attr;
7384
7385   attr.dw_attr = attr_kind;
7386   attr.dw_attr_val.val_class = dw_val_class_vec;
7387   attr.dw_attr_val.v.val_vec.length = length;
7388   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7389   attr.dw_attr_val.v.val_vec.array = array;
7390   add_dwarf_attr (die, &attr);
7391 }
7392
7393 /* Add an 8-byte data attribute value to a DIE.  */
7394
7395 static inline void
7396 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7397               unsigned char data8[8])
7398 {
7399   dw_attr_node attr;
7400
7401   attr.dw_attr = attr_kind;
7402   attr.dw_attr_val.val_class = dw_val_class_data8;
7403   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7404   add_dwarf_attr (die, &attr);
7405 }
7406
7407 /* Hash and equality functions for debug_str_hash.  */
7408
7409 static hashval_t
7410 debug_str_do_hash (const void *x)
7411 {
7412   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7413 }
7414
7415 static int
7416 debug_str_eq (const void *x1, const void *x2)
7417 {
7418   return strcmp ((((const struct indirect_string_node *)x1)->str),
7419                  (const char *)x2) == 0;
7420 }
7421
7422 /* Add STR to the indirect string hash table.  */
7423
7424 static struct indirect_string_node *
7425 find_AT_string (const char *str)
7426 {
7427   struct indirect_string_node *node;
7428   void **slot;
7429
7430   if (! debug_str_hash)
7431     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7432                                       debug_str_eq, NULL);
7433
7434   slot = htab_find_slot_with_hash (debug_str_hash, str,
7435                                    htab_hash_string (str), INSERT);
7436   if (*slot == NULL)
7437     {
7438       node = ggc_alloc_cleared_indirect_string_node ();
7439       node->str = ggc_strdup (str);
7440       *slot = node;
7441     }
7442   else
7443     node = (struct indirect_string_node *) *slot;
7444
7445   node->refcount++;
7446   return node;
7447 }
7448
7449 /* Add a string attribute value to a DIE.  */
7450
7451 static inline void
7452 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7453 {
7454   dw_attr_node attr;
7455   struct indirect_string_node *node;
7456
7457   node = find_AT_string (str);
7458
7459   attr.dw_attr = attr_kind;
7460   attr.dw_attr_val.val_class = dw_val_class_str;
7461   attr.dw_attr_val.v.val_str = node;
7462   add_dwarf_attr (die, &attr);
7463 }
7464
7465 /* Create a label for an indirect string node, ensuring it is going to
7466    be output, unless its reference count goes down to zero.  */
7467
7468 static inline void
7469 gen_label_for_indirect_string (struct indirect_string_node *node)
7470 {
7471   char label[32];
7472
7473   if (node->label)
7474     return;
7475
7476   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7477   ++dw2_string_counter;
7478   node->label = xstrdup (label);
7479 }
7480
7481 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7482    debug string STR.  */
7483
7484 static inline rtx
7485 get_debug_string_label (const char *str)
7486 {
7487   struct indirect_string_node *node = find_AT_string (str);
7488
7489   debug_str_hash_forced = true;
7490
7491   gen_label_for_indirect_string (node);
7492
7493   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7494 }
7495
7496 static inline const char *
7497 AT_string (dw_attr_ref a)
7498 {
7499   gcc_assert (a && AT_class (a) == dw_val_class_str);
7500   return a->dw_attr_val.v.val_str->str;
7501 }
7502
7503 /* Find out whether a string should be output inline in DIE
7504    or out-of-line in .debug_str section.  */
7505
7506 static enum dwarf_form
7507 AT_string_form (dw_attr_ref a)
7508 {
7509   struct indirect_string_node *node;
7510   unsigned int len;
7511
7512   gcc_assert (a && AT_class (a) == dw_val_class_str);
7513
7514   node = a->dw_attr_val.v.val_str;
7515   if (node->form)
7516     return node->form;
7517
7518   len = strlen (node->str) + 1;
7519
7520   /* If the string is shorter or equal to the size of the reference, it is
7521      always better to put it inline.  */
7522   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7523     return node->form = DW_FORM_string;
7524
7525   /* If we cannot expect the linker to merge strings in .debug_str
7526      section, only put it into .debug_str if it is worth even in this
7527      single module.  */
7528   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7529       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7530       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7531     return node->form = DW_FORM_string;
7532
7533   gen_label_for_indirect_string (node);
7534
7535   return node->form = DW_FORM_strp;
7536 }
7537
7538 /* Add a DIE reference attribute value to a DIE.  */
7539
7540 static inline void
7541 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7542 {
7543   dw_attr_node attr;
7544
7545 #ifdef ENABLE_CHECKING
7546   gcc_assert (targ_die != NULL);
7547 #else
7548   /* With LTO we can end up trying to reference something we didn't create
7549      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7550   if (targ_die == NULL)
7551     return;
7552 #endif
7553
7554   attr.dw_attr = attr_kind;
7555   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7556   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7557   attr.dw_attr_val.v.val_die_ref.external = 0;
7558   add_dwarf_attr (die, &attr);
7559 }
7560
7561 /* Add an AT_specification attribute to a DIE, and also make the back
7562    pointer from the specification to the definition.  */
7563
7564 static inline void
7565 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7566 {
7567   add_AT_die_ref (die, DW_AT_specification, targ_die);
7568   gcc_assert (!targ_die->die_definition);
7569   targ_die->die_definition = die;
7570 }
7571
7572 static inline dw_die_ref
7573 AT_ref (dw_attr_ref a)
7574 {
7575   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7576   return a->dw_attr_val.v.val_die_ref.die;
7577 }
7578
7579 static inline int
7580 AT_ref_external (dw_attr_ref a)
7581 {
7582   if (a && AT_class (a) == dw_val_class_die_ref)
7583     return a->dw_attr_val.v.val_die_ref.external;
7584
7585   return 0;
7586 }
7587
7588 static inline void
7589 set_AT_ref_external (dw_attr_ref a, int i)
7590 {
7591   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7592   a->dw_attr_val.v.val_die_ref.external = i;
7593 }
7594
7595 /* Add an FDE reference attribute value to a DIE.  */
7596
7597 static inline void
7598 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7599 {
7600   dw_attr_node attr;
7601
7602   attr.dw_attr = attr_kind;
7603   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7604   attr.dw_attr_val.v.val_fde_index = targ_fde;
7605   add_dwarf_attr (die, &attr);
7606 }
7607
7608 /* Add a location description attribute value to a DIE.  */
7609
7610 static inline void
7611 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7612 {
7613   dw_attr_node attr;
7614
7615   attr.dw_attr = attr_kind;
7616   attr.dw_attr_val.val_class = dw_val_class_loc;
7617   attr.dw_attr_val.v.val_loc = loc;
7618   add_dwarf_attr (die, &attr);
7619 }
7620
7621 static inline dw_loc_descr_ref
7622 AT_loc (dw_attr_ref a)
7623 {
7624   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7625   return a->dw_attr_val.v.val_loc;
7626 }
7627
7628 static inline void
7629 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7630 {
7631   dw_attr_node attr;
7632
7633   attr.dw_attr = attr_kind;
7634   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7635   attr.dw_attr_val.v.val_loc_list = loc_list;
7636   add_dwarf_attr (die, &attr);
7637   have_location_lists = true;
7638 }
7639
7640 static inline dw_loc_list_ref
7641 AT_loc_list (dw_attr_ref a)
7642 {
7643   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7644   return a->dw_attr_val.v.val_loc_list;
7645 }
7646
7647 static inline dw_loc_list_ref *
7648 AT_loc_list_ptr (dw_attr_ref a)
7649 {
7650   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7651   return &a->dw_attr_val.v.val_loc_list;
7652 }
7653
7654 /* Add an address constant attribute value to a DIE.  */
7655
7656 static inline void
7657 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7658 {
7659   dw_attr_node attr;
7660
7661   attr.dw_attr = attr_kind;
7662   attr.dw_attr_val.val_class = dw_val_class_addr;
7663   attr.dw_attr_val.v.val_addr = addr;
7664   add_dwarf_attr (die, &attr);
7665 }
7666
7667 /* Get the RTX from to an address DIE attribute.  */
7668
7669 static inline rtx
7670 AT_addr (dw_attr_ref a)
7671 {
7672   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7673   return a->dw_attr_val.v.val_addr;
7674 }
7675
7676 /* Add a file attribute value to a DIE.  */
7677
7678 static inline void
7679 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7680              struct dwarf_file_data *fd)
7681 {
7682   dw_attr_node attr;
7683
7684   attr.dw_attr = attr_kind;
7685   attr.dw_attr_val.val_class = dw_val_class_file;
7686   attr.dw_attr_val.v.val_file = fd;
7687   add_dwarf_attr (die, &attr);
7688 }
7689
7690 /* Get the dwarf_file_data from a file DIE attribute.  */
7691
7692 static inline struct dwarf_file_data *
7693 AT_file (dw_attr_ref a)
7694 {
7695   gcc_assert (a && AT_class (a) == dw_val_class_file);
7696   return a->dw_attr_val.v.val_file;
7697 }
7698
7699 /* Add a vms delta attribute value to a DIE.  */
7700
7701 static inline void
7702 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7703                   const char *lbl1, const char *lbl2)
7704 {
7705   dw_attr_node attr;
7706
7707   attr.dw_attr = attr_kind;
7708   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7709   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7710   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7711   add_dwarf_attr (die, &attr);
7712 }
7713
7714 /* Add a label identifier attribute value to a DIE.  */
7715
7716 static inline void
7717 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7718 {
7719   dw_attr_node attr;
7720
7721   attr.dw_attr = attr_kind;
7722   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7723   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7724   add_dwarf_attr (die, &attr);
7725 }
7726
7727 /* Add a section offset attribute value to a DIE, an offset into the
7728    debug_line section.  */
7729
7730 static inline void
7731 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7732                 const char *label)
7733 {
7734   dw_attr_node attr;
7735
7736   attr.dw_attr = attr_kind;
7737   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7738   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7739   add_dwarf_attr (die, &attr);
7740 }
7741
7742 /* Add a section offset attribute value to a DIE, an offset into the
7743    debug_macinfo section.  */
7744
7745 static inline void
7746 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7747                const char *label)
7748 {
7749   dw_attr_node attr;
7750
7751   attr.dw_attr = attr_kind;
7752   attr.dw_attr_val.val_class = dw_val_class_macptr;
7753   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7754   add_dwarf_attr (die, &attr);
7755 }
7756
7757 /* Add an offset attribute value to a DIE.  */
7758
7759 static inline void
7760 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7761                unsigned HOST_WIDE_INT offset)
7762 {
7763   dw_attr_node attr;
7764
7765   attr.dw_attr = attr_kind;
7766   attr.dw_attr_val.val_class = dw_val_class_offset;
7767   attr.dw_attr_val.v.val_offset = offset;
7768   add_dwarf_attr (die, &attr);
7769 }
7770
7771 /* Add an range_list attribute value to a DIE.  */
7772
7773 static void
7774 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7775                    long unsigned int offset)
7776 {
7777   dw_attr_node attr;
7778
7779   attr.dw_attr = attr_kind;
7780   attr.dw_attr_val.val_class = dw_val_class_range_list;
7781   attr.dw_attr_val.v.val_offset = offset;
7782   add_dwarf_attr (die, &attr);
7783 }
7784
7785 /* Return the start label of a delta attribute.  */
7786
7787 static inline const char *
7788 AT_vms_delta1 (dw_attr_ref a)
7789 {
7790   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7791   return a->dw_attr_val.v.val_vms_delta.lbl1;
7792 }
7793
7794 /* Return the end label of a delta attribute.  */
7795
7796 static inline const char *
7797 AT_vms_delta2 (dw_attr_ref a)
7798 {
7799   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7800   return a->dw_attr_val.v.val_vms_delta.lbl2;
7801 }
7802
7803 static inline const char *
7804 AT_lbl (dw_attr_ref a)
7805 {
7806   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7807                     || AT_class (a) == dw_val_class_lineptr
7808                     || AT_class (a) == dw_val_class_macptr));
7809   return a->dw_attr_val.v.val_lbl_id;
7810 }
7811
7812 /* Get the attribute of type attr_kind.  */
7813
7814 static dw_attr_ref
7815 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7816 {
7817   dw_attr_ref a;
7818   unsigned ix;
7819   dw_die_ref spec = NULL;
7820
7821   if (! die)
7822     return NULL;
7823
7824   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7825     if (a->dw_attr == attr_kind)
7826       return a;
7827     else if (a->dw_attr == DW_AT_specification
7828              || a->dw_attr == DW_AT_abstract_origin)
7829       spec = AT_ref (a);
7830
7831   if (spec)
7832     return get_AT (spec, attr_kind);
7833
7834   return NULL;
7835 }
7836
7837 /* Return the "low pc" attribute value, typically associated with a subprogram
7838    DIE.  Return null if the "low pc" attribute is either not present, or if it
7839    cannot be represented as an assembler label identifier.  */
7840
7841 static inline const char *
7842 get_AT_low_pc (dw_die_ref die)
7843 {
7844   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7845
7846   return a ? AT_lbl (a) : NULL;
7847 }
7848
7849 /* Return the "high pc" attribute value, typically associated with a subprogram
7850    DIE.  Return null if the "high pc" attribute is either not present, or if it
7851    cannot be represented as an assembler label identifier.  */
7852
7853 static inline const char *
7854 get_AT_hi_pc (dw_die_ref die)
7855 {
7856   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7857
7858   return a ? AT_lbl (a) : NULL;
7859 }
7860
7861 /* Return the value of the string attribute designated by ATTR_KIND, or
7862    NULL if it is not present.  */
7863
7864 static inline const char *
7865 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7866 {
7867   dw_attr_ref a = get_AT (die, attr_kind);
7868
7869   return a ? AT_string (a) : NULL;
7870 }
7871
7872 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7873    if it is not present.  */
7874
7875 static inline int
7876 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7877 {
7878   dw_attr_ref a = get_AT (die, attr_kind);
7879
7880   return a ? AT_flag (a) : 0;
7881 }
7882
7883 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7884    if it is not present.  */
7885
7886 static inline unsigned
7887 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7888 {
7889   dw_attr_ref a = get_AT (die, attr_kind);
7890
7891   return a ? AT_unsigned (a) : 0;
7892 }
7893
7894 static inline dw_die_ref
7895 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7896 {
7897   dw_attr_ref a = get_AT (die, attr_kind);
7898
7899   return a ? AT_ref (a) : NULL;
7900 }
7901
7902 static inline struct dwarf_file_data *
7903 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7904 {
7905   dw_attr_ref a = get_AT (die, attr_kind);
7906
7907   return a ? AT_file (a) : NULL;
7908 }
7909
7910 /* Return TRUE if the language is C++.  */
7911
7912 static inline bool
7913 is_cxx (void)
7914 {
7915   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7916
7917   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7918 }
7919
7920 /* Return TRUE if the language is Fortran.  */
7921
7922 static inline bool
7923 is_fortran (void)
7924 {
7925   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7926
7927   return (lang == DW_LANG_Fortran77
7928           || lang == DW_LANG_Fortran90
7929           || lang == DW_LANG_Fortran95);
7930 }
7931
7932 /* Return TRUE if the language is Ada.  */
7933
7934 static inline bool
7935 is_ada (void)
7936 {
7937   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7938
7939   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7940 }
7941
7942 /* Remove the specified attribute if present.  */
7943
7944 static void
7945 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7946 {
7947   dw_attr_ref a;
7948   unsigned ix;
7949
7950   if (! die)
7951     return;
7952
7953   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7954     if (a->dw_attr == attr_kind)
7955       {
7956         if (AT_class (a) == dw_val_class_str)
7957           if (a->dw_attr_val.v.val_str->refcount)
7958             a->dw_attr_val.v.val_str->refcount--;
7959
7960         /* VEC_ordered_remove should help reduce the number of abbrevs
7961            that are needed.  */
7962         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7963         return;
7964       }
7965 }
7966
7967 /* Remove CHILD from its parent.  PREV must have the property that
7968    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7969
7970 static void
7971 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7972 {
7973   gcc_assert (child->die_parent == prev->die_parent);
7974   gcc_assert (prev->die_sib == child);
7975   if (prev == child)
7976     {
7977       gcc_assert (child->die_parent->die_child == child);
7978       prev = NULL;
7979     }
7980   else
7981     prev->die_sib = child->die_sib;
7982   if (child->die_parent->die_child == child)
7983     child->die_parent->die_child = prev;
7984 }
7985
7986 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7987    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7988
7989 static void
7990 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7991 {
7992   dw_die_ref parent = old_child->die_parent;
7993
7994   gcc_assert (parent == prev->die_parent);
7995   gcc_assert (prev->die_sib == old_child);
7996
7997   new_child->die_parent = parent;
7998   if (prev == old_child)
7999     {
8000       gcc_assert (parent->die_child == old_child);
8001       new_child->die_sib = new_child;
8002     }
8003   else
8004     {
8005       prev->die_sib = new_child;
8006       new_child->die_sib = old_child->die_sib;
8007     }
8008   if (old_child->die_parent->die_child == old_child)
8009     old_child->die_parent->die_child = new_child;
8010 }
8011
8012 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8013
8014 static void
8015 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8016 {
8017   dw_die_ref c;
8018   new_parent->die_child = old_parent->die_child;
8019   old_parent->die_child = NULL;
8020   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8021 }
8022
8023 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8024    matches TAG.  */
8025
8026 static void
8027 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8028 {
8029   dw_die_ref c;
8030
8031   c = die->die_child;
8032   if (c) do {
8033     dw_die_ref prev = c;
8034     c = c->die_sib;
8035     while (c->die_tag == tag)
8036       {
8037         remove_child_with_prev (c, prev);
8038         /* Might have removed every child.  */
8039         if (c == c->die_sib)
8040           return;
8041         c = c->die_sib;
8042       }
8043   } while (c != die->die_child);
8044 }
8045
8046 /* Add a CHILD_DIE as the last child of DIE.  */
8047
8048 static void
8049 add_child_die (dw_die_ref die, dw_die_ref child_die)
8050 {
8051   /* FIXME this should probably be an assert.  */
8052   if (! die || ! child_die)
8053     return;
8054   gcc_assert (die != child_die);
8055
8056   child_die->die_parent = die;
8057   if (die->die_child)
8058     {
8059       child_die->die_sib = die->die_child->die_sib;
8060       die->die_child->die_sib = child_die;
8061     }
8062   else
8063     child_die->die_sib = child_die;
8064   die->die_child = child_die;
8065 }
8066
8067 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8068    is the specification, to the end of PARENT's list of children.
8069    This is done by removing and re-adding it.  */
8070
8071 static void
8072 splice_child_die (dw_die_ref parent, dw_die_ref child)
8073 {
8074   dw_die_ref p;
8075
8076   /* We want the declaration DIE from inside the class, not the
8077      specification DIE at toplevel.  */
8078   if (child->die_parent != parent)
8079     {
8080       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8081
8082       if (tmp)
8083         child = tmp;
8084     }
8085
8086   gcc_assert (child->die_parent == parent
8087               || (child->die_parent
8088                   == get_AT_ref (parent, DW_AT_specification)));
8089
8090   for (p = child->die_parent->die_child; ; p = p->die_sib)
8091     if (p->die_sib == child)
8092       {
8093         remove_child_with_prev (child, p);
8094         break;
8095       }
8096
8097   add_child_die (parent, child);
8098 }
8099
8100 /* Return a pointer to a newly created DIE node.  */
8101
8102 static inline dw_die_ref
8103 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8104 {
8105   dw_die_ref die = ggc_alloc_cleared_die_node ();
8106
8107   die->die_tag = tag_value;
8108
8109   if (parent_die != NULL)
8110     add_child_die (parent_die, die);
8111   else
8112     {
8113       limbo_die_node *limbo_node;
8114
8115       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8116       limbo_node->die = die;
8117       limbo_node->created_for = t;
8118       limbo_node->next = limbo_die_list;
8119       limbo_die_list = limbo_node;
8120     }
8121
8122   return die;
8123 }
8124
8125 /* Return the DIE associated with the given type specifier.  */
8126
8127 static inline dw_die_ref
8128 lookup_type_die (tree type)
8129 {
8130   return TYPE_SYMTAB_DIE (type);
8131 }
8132
8133 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8134    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8135    anonymous type instead the one of the naming typedef.  */
8136
8137 static inline dw_die_ref
8138 strip_naming_typedef (tree type, dw_die_ref type_die)
8139 {
8140   if (type
8141       && TREE_CODE (type) == RECORD_TYPE
8142       && type_die
8143       && type_die->die_tag == DW_TAG_typedef
8144       && is_naming_typedef_decl (TYPE_NAME (type)))
8145     type_die = get_AT_ref (type_die, DW_AT_type);
8146   return type_die;
8147 }
8148
8149 /* Like lookup_type_die, but if type is an anonymous type named by a
8150    typedef[1], return the DIE of the anonymous type instead the one of
8151    the naming typedef.  This is because in gen_typedef_die, we did
8152    equate the anonymous struct named by the typedef with the DIE of
8153    the naming typedef. So by default, lookup_type_die on an anonymous
8154    struct yields the DIE of the naming typedef.
8155
8156    [1]: Read the comment of is_naming_typedef_decl to learn about what
8157    a naming typedef is.  */
8158
8159 static inline dw_die_ref
8160 lookup_type_die_strip_naming_typedef (tree type)
8161 {
8162   dw_die_ref die = lookup_type_die (type);
8163   return strip_naming_typedef (type, die);
8164 }
8165
8166 /* Equate a DIE to a given type specifier.  */
8167
8168 static inline void
8169 equate_type_number_to_die (tree type, dw_die_ref type_die)
8170 {
8171   TYPE_SYMTAB_DIE (type) = type_die;
8172 }
8173
8174 /* Returns a hash value for X (which really is a die_struct).  */
8175
8176 static hashval_t
8177 decl_die_table_hash (const void *x)
8178 {
8179   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8180 }
8181
8182 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8183
8184 static int
8185 decl_die_table_eq (const void *x, const void *y)
8186 {
8187   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8188 }
8189
8190 /* Return the DIE associated with a given declaration.  */
8191
8192 static inline dw_die_ref
8193 lookup_decl_die (tree decl)
8194 {
8195   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8196 }
8197
8198 /* Returns a hash value for X (which really is a var_loc_list).  */
8199
8200 static hashval_t
8201 decl_loc_table_hash (const void *x)
8202 {
8203   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8204 }
8205
8206 /* Return nonzero if decl_id of var_loc_list X is the same as
8207    UID of decl *Y.  */
8208
8209 static int
8210 decl_loc_table_eq (const void *x, const void *y)
8211 {
8212   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8213 }
8214
8215 /* Return the var_loc list associated with a given declaration.  */
8216
8217 static inline var_loc_list *
8218 lookup_decl_loc (const_tree decl)
8219 {
8220   if (!decl_loc_table)
8221     return NULL;
8222   return (var_loc_list *)
8223     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8224 }
8225
8226 /* Equate a DIE to a particular declaration.  */
8227
8228 static void
8229 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8230 {
8231   unsigned int decl_id = DECL_UID (decl);
8232   void **slot;
8233
8234   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8235   *slot = decl_die;
8236   decl_die->decl_id = decl_id;
8237 }
8238
8239 /* Return how many bits covers PIECE EXPR_LIST.  */
8240
8241 static int
8242 decl_piece_bitsize (rtx piece)
8243 {
8244   int ret = (int) GET_MODE (piece);
8245   if (ret)
8246     return ret;
8247   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8248               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8249   return INTVAL (XEXP (XEXP (piece, 0), 0));
8250 }
8251
8252 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8253
8254 static rtx *
8255 decl_piece_varloc_ptr (rtx piece)
8256 {
8257   if ((int) GET_MODE (piece))
8258     return &XEXP (piece, 0);
8259   else
8260     return &XEXP (XEXP (piece, 0), 1);
8261 }
8262
8263 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8264    Next is the chain of following piece nodes.  */
8265
8266 static rtx
8267 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8268 {
8269   if (bitsize <= (int) MAX_MACHINE_MODE)
8270     return alloc_EXPR_LIST (bitsize, loc_note, next);
8271   else
8272     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8273                                                GEN_INT (bitsize),
8274                                                loc_note), next);
8275 }
8276
8277 /* Return rtx that should be stored into loc field for
8278    LOC_NOTE and BITPOS/BITSIZE.  */
8279
8280 static rtx
8281 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8282                       HOST_WIDE_INT bitsize)
8283 {
8284   if (bitsize != -1)
8285     {
8286       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8287       if (bitpos != 0)
8288         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8289     }
8290   return loc_note;
8291 }
8292
8293 /* This function either modifies location piece list *DEST in
8294    place (if SRC and INNER is NULL), or copies location piece list
8295    *SRC to *DEST while modifying it.  Location BITPOS is modified
8296    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8297    not copied and if needed some padding around it is added.
8298    When modifying in place, DEST should point to EXPR_LIST where
8299    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8300    to the start of the whole list and INNER points to the EXPR_LIST
8301    where earlier pieces cover PIECE_BITPOS bits.  */
8302
8303 static void
8304 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8305                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8306                    HOST_WIDE_INT bitsize, rtx loc_note)
8307 {
8308   int diff;
8309   bool copy = inner != NULL;
8310
8311   if (copy)
8312     {
8313       /* First copy all nodes preceeding the current bitpos.  */
8314       while (src != inner)
8315         {
8316           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8317                                    decl_piece_bitsize (*src), NULL_RTX);
8318           dest = &XEXP (*dest, 1);
8319           src = &XEXP (*src, 1);
8320         }
8321     }
8322   /* Add padding if needed.  */
8323   if (bitpos != piece_bitpos)
8324     {
8325       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8326                                copy ? NULL_RTX : *dest);
8327       dest = &XEXP (*dest, 1);
8328     }
8329   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8330     {
8331       gcc_assert (!copy);
8332       /* A piece with correct bitpos and bitsize already exist,
8333          just update the location for it and return.  */
8334       *decl_piece_varloc_ptr (*dest) = loc_note;
8335       return;
8336     }
8337   /* Add the piece that changed.  */
8338   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8339   dest = &XEXP (*dest, 1);
8340   /* Skip over pieces that overlap it.  */
8341   diff = bitpos - piece_bitpos + bitsize;
8342   if (!copy)
8343     src = dest;
8344   while (diff > 0 && *src)
8345     {
8346       rtx piece = *src;
8347       diff -= decl_piece_bitsize (piece);
8348       if (copy)
8349         src = &XEXP (piece, 1);
8350       else
8351         {
8352           *src = XEXP (piece, 1);
8353           free_EXPR_LIST_node (piece);
8354         }
8355     }
8356   /* Add padding if needed.  */
8357   if (diff < 0 && *src)
8358     {
8359       if (!copy)
8360         dest = src;
8361       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8362       dest = &XEXP (*dest, 1);
8363     }
8364   if (!copy)
8365     return;
8366   /* Finally copy all nodes following it.  */
8367   while (*src)
8368     {
8369       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8370                                decl_piece_bitsize (*src), NULL_RTX);
8371       dest = &XEXP (*dest, 1);
8372       src = &XEXP (*src, 1);
8373     }
8374 }
8375
8376 /* Add a variable location node to the linked list for DECL.  */
8377
8378 static struct var_loc_node *
8379 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8380 {
8381   unsigned int decl_id;
8382   var_loc_list *temp;
8383   void **slot;
8384   struct var_loc_node *loc = NULL;
8385   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8386
8387   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8388     {
8389       tree realdecl = DECL_DEBUG_EXPR (decl);
8390       if (realdecl && handled_component_p (realdecl))
8391         {
8392           HOST_WIDE_INT maxsize;
8393           tree innerdecl;
8394           innerdecl
8395             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8396           if (!DECL_P (innerdecl)
8397               || DECL_IGNORED_P (innerdecl)
8398               || TREE_STATIC (innerdecl)
8399               || bitsize <= 0
8400               || bitpos + bitsize > 256
8401               || bitsize != maxsize)
8402             return NULL;
8403           decl = innerdecl;
8404         }
8405     }
8406
8407   decl_id = DECL_UID (decl);
8408   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8409   if (*slot == NULL)
8410     {
8411       temp = ggc_alloc_cleared_var_loc_list ();
8412       temp->decl_id = decl_id;
8413       *slot = temp;
8414     }
8415   else
8416     temp = (var_loc_list *) *slot;
8417
8418   if (temp->last)
8419     {
8420       struct var_loc_node *last = temp->last, *unused = NULL;
8421       rtx *piece_loc = NULL, last_loc_note;
8422       int piece_bitpos = 0;
8423       if (last->next)
8424         {
8425           last = last->next;
8426           gcc_assert (last->next == NULL);
8427         }
8428       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8429         {
8430           piece_loc = &last->loc;
8431           do
8432             {
8433               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8434               if (piece_bitpos + cur_bitsize > bitpos)
8435                 break;
8436               piece_bitpos += cur_bitsize;
8437               piece_loc = &XEXP (*piece_loc, 1);
8438             }
8439           while (*piece_loc);
8440         }
8441       /* TEMP->LAST here is either pointer to the last but one or
8442          last element in the chained list, LAST is pointer to the
8443          last element.  */
8444       if (label && strcmp (last->label, label) == 0)
8445         {
8446           /* For SRA optimized variables if there weren't any real
8447              insns since last note, just modify the last node.  */
8448           if (piece_loc != NULL)
8449             {
8450               adjust_piece_list (piece_loc, NULL, NULL,
8451                                  bitpos, piece_bitpos, bitsize, loc_note);
8452               return NULL;
8453             }
8454           /* If the last note doesn't cover any instructions, remove it.  */
8455           if (temp->last != last)
8456             {
8457               temp->last->next = NULL;
8458               unused = last;
8459               last = temp->last;
8460               gcc_assert (strcmp (last->label, label) != 0);
8461             }
8462           else
8463             {
8464               gcc_assert (temp->first == temp->last);
8465               memset (temp->last, '\0', sizeof (*temp->last));
8466               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8467               return temp->last;
8468             }
8469         }
8470       if (bitsize == -1 && NOTE_P (last->loc))
8471         last_loc_note = last->loc;
8472       else if (piece_loc != NULL
8473                && *piece_loc != NULL_RTX
8474                && piece_bitpos == bitpos
8475                && decl_piece_bitsize (*piece_loc) == bitsize)
8476         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8477       else
8478         last_loc_note = NULL_RTX;
8479       /* If the current location is the same as the end of the list,
8480          and either both or neither of the locations is uninitialized,
8481          we have nothing to do.  */
8482       if (last_loc_note == NULL_RTX
8483           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8484                             NOTE_VAR_LOCATION_LOC (loc_note)))
8485           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8486                != NOTE_VAR_LOCATION_STATUS (loc_note))
8487               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8488                    == VAR_INIT_STATUS_UNINITIALIZED)
8489                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8490                       == VAR_INIT_STATUS_UNINITIALIZED))))
8491         {
8492           /* Add LOC to the end of list and update LAST.  If the last
8493              element of the list has been removed above, reuse its
8494              memory for the new node, otherwise allocate a new one.  */
8495           if (unused)
8496             {
8497               loc = unused;
8498               memset (loc, '\0', sizeof (*loc));
8499             }
8500           else
8501             loc = ggc_alloc_cleared_var_loc_node ();
8502           if (bitsize == -1 || piece_loc == NULL)
8503             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8504           else
8505             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8506                                bitpos, piece_bitpos, bitsize, loc_note);
8507           last->next = loc;
8508           /* Ensure TEMP->LAST will point either to the new last but one
8509              element of the chain, or to the last element in it.  */
8510           if (last != temp->last)
8511             temp->last = last;
8512         }
8513       else if (unused)
8514         ggc_free (unused);
8515     }
8516   else
8517     {
8518       loc = ggc_alloc_cleared_var_loc_node ();
8519       temp->first = loc;
8520       temp->last = loc;
8521       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8522     }
8523   return loc;
8524 }
8525 \f
8526 /* Keep track of the number of spaces used to indent the
8527    output of the debugging routines that print the structure of
8528    the DIE internal representation.  */
8529 static int print_indent;
8530
8531 /* Indent the line the number of spaces given by print_indent.  */
8532
8533 static inline void
8534 print_spaces (FILE *outfile)
8535 {
8536   fprintf (outfile, "%*s", print_indent, "");
8537 }
8538
8539 /* Print a type signature in hex.  */
8540
8541 static inline void
8542 print_signature (FILE *outfile, char *sig)
8543 {
8544   int i;
8545
8546   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8547     fprintf (outfile, "%02x", sig[i] & 0xff);
8548 }
8549
8550 /* Print the information associated with a given DIE, and its children.
8551    This routine is a debugging aid only.  */
8552
8553 static void
8554 print_die (dw_die_ref die, FILE *outfile)
8555 {
8556   dw_attr_ref a;
8557   dw_die_ref c;
8558   unsigned ix;
8559
8560   print_spaces (outfile);
8561   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8562            die->die_offset, dwarf_tag_name (die->die_tag),
8563            (void*) die);
8564   print_spaces (outfile);
8565   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8566   fprintf (outfile, " offset: %ld", die->die_offset);
8567   fprintf (outfile, " mark: %d\n", die->die_mark);
8568
8569   if (dwarf_version >= 4 && die->die_id.die_type_node)
8570     {
8571       print_spaces (outfile);
8572       fprintf (outfile, "  signature: ");
8573       print_signature (outfile, die->die_id.die_type_node->signature);
8574       fprintf (outfile, "\n");
8575     }
8576
8577   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8578     {
8579       print_spaces (outfile);
8580       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8581
8582       switch (AT_class (a))
8583         {
8584         case dw_val_class_addr:
8585           fprintf (outfile, "address");
8586           break;
8587         case dw_val_class_offset:
8588           fprintf (outfile, "offset");
8589           break;
8590         case dw_val_class_loc:
8591           fprintf (outfile, "location descriptor");
8592           break;
8593         case dw_val_class_loc_list:
8594           fprintf (outfile, "location list -> label:%s",
8595                    AT_loc_list (a)->ll_symbol);
8596           break;
8597         case dw_val_class_range_list:
8598           fprintf (outfile, "range list");
8599           break;
8600         case dw_val_class_const:
8601           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8602           break;
8603         case dw_val_class_unsigned_const:
8604           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8605           break;
8606         case dw_val_class_const_double:
8607           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8608                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8609                    a->dw_attr_val.v.val_double.high,
8610                    a->dw_attr_val.v.val_double.low);
8611           break;
8612         case dw_val_class_vec:
8613           fprintf (outfile, "floating-point or vector constant");
8614           break;
8615         case dw_val_class_flag:
8616           fprintf (outfile, "%u", AT_flag (a));
8617           break;
8618         case dw_val_class_die_ref:
8619           if (AT_ref (a) != NULL)
8620             {
8621               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8622                 {
8623                   fprintf (outfile, "die -> signature: ");
8624                   print_signature (outfile,
8625                                    AT_ref (a)->die_id.die_type_node->signature);
8626                 }
8627               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8628                 fprintf (outfile, "die -> label: %s",
8629                          AT_ref (a)->die_id.die_symbol);
8630               else
8631                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8632               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8633             }
8634           else
8635             fprintf (outfile, "die -> <null>");
8636           break;
8637         case dw_val_class_vms_delta:
8638           fprintf (outfile, "delta: @slotcount(%s-%s)",
8639                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8640           break;
8641         case dw_val_class_lbl_id:
8642         case dw_val_class_lineptr:
8643         case dw_val_class_macptr:
8644           fprintf (outfile, "label: %s", AT_lbl (a));
8645           break;
8646         case dw_val_class_str:
8647           if (AT_string (a) != NULL)
8648             fprintf (outfile, "\"%s\"", AT_string (a));
8649           else
8650             fprintf (outfile, "<null>");
8651           break;
8652         case dw_val_class_file:
8653           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8654                    AT_file (a)->emitted_number);
8655           break;
8656         case dw_val_class_data8:
8657           {
8658             int i;
8659
8660             for (i = 0; i < 8; i++)
8661               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8662             break;
8663           }
8664         default:
8665           break;
8666         }
8667
8668       fprintf (outfile, "\n");
8669     }
8670
8671   if (die->die_child != NULL)
8672     {
8673       print_indent += 4;
8674       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8675       print_indent -= 4;
8676     }
8677   if (print_indent == 0)
8678     fprintf (outfile, "\n");
8679 }
8680
8681 /* Print the contents of the source code line number correspondence table.
8682    This routine is a debugging aid only.  */
8683
8684 static void
8685 print_dwarf_line_table (FILE *outfile)
8686 {
8687   unsigned i;
8688   dw_line_info_ref line_info;
8689
8690   fprintf (outfile, "\n\nDWARF source line information\n");
8691   for (i = 1; i < line_info_table_in_use; i++)
8692     {
8693       line_info = &line_info_table[i];
8694       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8695                line_info->dw_file_num,
8696                line_info->dw_line_num);
8697     }
8698
8699   fprintf (outfile, "\n\n");
8700 }
8701
8702 /* Print the information collected for a given DIE.  */
8703
8704 DEBUG_FUNCTION void
8705 debug_dwarf_die (dw_die_ref die)
8706 {
8707   print_die (die, stderr);
8708 }
8709
8710 /* Print all DWARF information collected for the compilation unit.
8711    This routine is a debugging aid only.  */
8712
8713 DEBUG_FUNCTION void
8714 debug_dwarf (void)
8715 {
8716   print_indent = 0;
8717   print_die (comp_unit_die (), stderr);
8718   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8719     print_dwarf_line_table (stderr);
8720 }
8721 \f
8722 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8723    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8724    DIE that marks the start of the DIEs for this include file.  */
8725
8726 static dw_die_ref
8727 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8728 {
8729   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8730   dw_die_ref new_unit = gen_compile_unit_die (filename);
8731
8732   new_unit->die_sib = old_unit;
8733   return new_unit;
8734 }
8735
8736 /* Close an include-file CU and reopen the enclosing one.  */
8737
8738 static dw_die_ref
8739 pop_compile_unit (dw_die_ref old_unit)
8740 {
8741   dw_die_ref new_unit = old_unit->die_sib;
8742
8743   old_unit->die_sib = NULL;
8744   return new_unit;
8745 }
8746
8747 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8748 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8749
8750 /* Calculate the checksum of a location expression.  */
8751
8752 static inline void
8753 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8754 {
8755   int tem;
8756
8757   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8758   CHECKSUM (tem);
8759   CHECKSUM (loc->dw_loc_oprnd1);
8760   CHECKSUM (loc->dw_loc_oprnd2);
8761 }
8762
8763 /* Calculate the checksum of an attribute.  */
8764
8765 static void
8766 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8767 {
8768   dw_loc_descr_ref loc;
8769   rtx r;
8770
8771   CHECKSUM (at->dw_attr);
8772
8773   /* We don't care that this was compiled with a different compiler
8774      snapshot; if the output is the same, that's what matters.  */
8775   if (at->dw_attr == DW_AT_producer)
8776     return;
8777
8778   switch (AT_class (at))
8779     {
8780     case dw_val_class_const:
8781       CHECKSUM (at->dw_attr_val.v.val_int);
8782       break;
8783     case dw_val_class_unsigned_const:
8784       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8785       break;
8786     case dw_val_class_const_double:
8787       CHECKSUM (at->dw_attr_val.v.val_double);
8788       break;
8789     case dw_val_class_vec:
8790       CHECKSUM (at->dw_attr_val.v.val_vec);
8791       break;
8792     case dw_val_class_flag:
8793       CHECKSUM (at->dw_attr_val.v.val_flag);
8794       break;
8795     case dw_val_class_str:
8796       CHECKSUM_STRING (AT_string (at));
8797       break;
8798
8799     case dw_val_class_addr:
8800       r = AT_addr (at);
8801       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8802       CHECKSUM_STRING (XSTR (r, 0));
8803       break;
8804
8805     case dw_val_class_offset:
8806       CHECKSUM (at->dw_attr_val.v.val_offset);
8807       break;
8808
8809     case dw_val_class_loc:
8810       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8811         loc_checksum (loc, ctx);
8812       break;
8813
8814     case dw_val_class_die_ref:
8815       die_checksum (AT_ref (at), ctx, mark);
8816       break;
8817
8818     case dw_val_class_fde_ref:
8819     case dw_val_class_vms_delta:
8820     case dw_val_class_lbl_id:
8821     case dw_val_class_lineptr:
8822     case dw_val_class_macptr:
8823       break;
8824
8825     case dw_val_class_file:
8826       CHECKSUM_STRING (AT_file (at)->filename);
8827       break;
8828
8829     case dw_val_class_data8:
8830       CHECKSUM (at->dw_attr_val.v.val_data8);
8831       break;
8832
8833     default:
8834       break;
8835     }
8836 }
8837
8838 /* Calculate the checksum of a DIE.  */
8839
8840 static void
8841 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8842 {
8843   dw_die_ref c;
8844   dw_attr_ref a;
8845   unsigned ix;
8846
8847   /* To avoid infinite recursion.  */
8848   if (die->die_mark)
8849     {
8850       CHECKSUM (die->die_mark);
8851       return;
8852     }
8853   die->die_mark = ++(*mark);
8854
8855   CHECKSUM (die->die_tag);
8856
8857   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8858     attr_checksum (a, ctx, mark);
8859
8860   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8861 }
8862
8863 #undef CHECKSUM
8864 #undef CHECKSUM_STRING
8865
8866 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8867 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8868 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8869 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8870 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8871 #define CHECKSUM_ATTR(FOO) \
8872   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8873
8874 /* Calculate the checksum of a number in signed LEB128 format.  */
8875
8876 static void
8877 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8878 {
8879   unsigned char byte;
8880   bool more;
8881
8882   while (1)
8883     {
8884       byte = (value & 0x7f);
8885       value >>= 7;
8886       more = !((value == 0 && (byte & 0x40) == 0)
8887                 || (value == -1 && (byte & 0x40) != 0));
8888       if (more)
8889         byte |= 0x80;
8890       CHECKSUM (byte);
8891       if (!more)
8892         break;
8893     }
8894 }
8895
8896 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8897
8898 static void
8899 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8900 {
8901   while (1)
8902     {
8903       unsigned char byte = (value & 0x7f);
8904       value >>= 7;
8905       if (value != 0)
8906         /* More bytes to follow.  */
8907         byte |= 0x80;
8908       CHECKSUM (byte);
8909       if (value == 0)
8910         break;
8911     }
8912 }
8913
8914 /* Checksum the context of the DIE.  This adds the names of any
8915    surrounding namespaces or structures to the checksum.  */
8916
8917 static void
8918 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8919 {
8920   const char *name;
8921   dw_die_ref spec;
8922   int tag = die->die_tag;
8923
8924   if (tag != DW_TAG_namespace
8925       && tag != DW_TAG_structure_type
8926       && tag != DW_TAG_class_type)
8927     return;
8928
8929   name = get_AT_string (die, DW_AT_name);
8930
8931   spec = get_AT_ref (die, DW_AT_specification);
8932   if (spec != NULL)
8933     die = spec;
8934
8935   if (die->die_parent != NULL)
8936     checksum_die_context (die->die_parent, ctx);
8937
8938   CHECKSUM_ULEB128 ('C');
8939   CHECKSUM_ULEB128 (tag);
8940   if (name != NULL)
8941     CHECKSUM_STRING (name);
8942 }
8943
8944 /* Calculate the checksum of a location expression.  */
8945
8946 static inline void
8947 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8948 {
8949   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8950      were emitted as a DW_FORM_sdata instead of a location expression.  */
8951   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8952     {
8953       CHECKSUM_ULEB128 (DW_FORM_sdata);
8954       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8955       return;
8956     }
8957
8958   /* Otherwise, just checksum the raw location expression.  */
8959   while (loc != NULL)
8960     {
8961       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8962       CHECKSUM (loc->dw_loc_oprnd1);
8963       CHECKSUM (loc->dw_loc_oprnd2);
8964       loc = loc->dw_loc_next;
8965     }
8966 }
8967
8968 /* Calculate the checksum of an attribute.  */
8969
8970 static void
8971 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8972                        struct md5_ctx *ctx, int *mark)
8973 {
8974   dw_loc_descr_ref loc;
8975   rtx r;
8976
8977   if (AT_class (at) == dw_val_class_die_ref)
8978     {
8979       dw_die_ref target_die = AT_ref (at);
8980
8981       /* For pointer and reference types, we checksum only the (qualified)
8982          name of the target type (if there is a name).  For friend entries,
8983          we checksum only the (qualified) name of the target type or function.
8984          This allows the checksum to remain the same whether the target type
8985          is complete or not.  */
8986       if ((at->dw_attr == DW_AT_type
8987            && (tag == DW_TAG_pointer_type
8988                || tag == DW_TAG_reference_type
8989                || tag == DW_TAG_rvalue_reference_type
8990                || tag == DW_TAG_ptr_to_member_type))
8991           || (at->dw_attr == DW_AT_friend
8992               && tag == DW_TAG_friend))
8993         {
8994           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8995
8996           if (name_attr != NULL)
8997             {
8998               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8999
9000               if (decl == NULL)
9001                 decl = target_die;
9002               CHECKSUM_ULEB128 ('N');
9003               CHECKSUM_ULEB128 (at->dw_attr);
9004               if (decl->die_parent != NULL)
9005                 checksum_die_context (decl->die_parent, ctx);
9006               CHECKSUM_ULEB128 ('E');
9007               CHECKSUM_STRING (AT_string (name_attr));
9008               return;
9009             }
9010         }
9011
9012       /* For all other references to another DIE, we check to see if the
9013          target DIE has already been visited.  If it has, we emit a
9014          backward reference; if not, we descend recursively.  */
9015       if (target_die->die_mark > 0)
9016         {
9017           CHECKSUM_ULEB128 ('R');
9018           CHECKSUM_ULEB128 (at->dw_attr);
9019           CHECKSUM_ULEB128 (target_die->die_mark);
9020         }
9021       else
9022         {
9023           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9024
9025           if (decl == NULL)
9026             decl = target_die;
9027           target_die->die_mark = ++(*mark);
9028           CHECKSUM_ULEB128 ('T');
9029           CHECKSUM_ULEB128 (at->dw_attr);
9030           if (decl->die_parent != NULL)
9031             checksum_die_context (decl->die_parent, ctx);
9032           die_checksum_ordered (target_die, ctx, mark);
9033         }
9034       return;
9035     }
9036
9037   CHECKSUM_ULEB128 ('A');
9038   CHECKSUM_ULEB128 (at->dw_attr);
9039
9040   switch (AT_class (at))
9041     {
9042     case dw_val_class_const:
9043       CHECKSUM_ULEB128 (DW_FORM_sdata);
9044       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9045       break;
9046
9047     case dw_val_class_unsigned_const:
9048       CHECKSUM_ULEB128 (DW_FORM_sdata);
9049       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9050       break;
9051
9052     case dw_val_class_const_double:
9053       CHECKSUM_ULEB128 (DW_FORM_block);
9054       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9055       CHECKSUM (at->dw_attr_val.v.val_double);
9056       break;
9057
9058     case dw_val_class_vec:
9059       CHECKSUM_ULEB128 (DW_FORM_block);
9060       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9061       CHECKSUM (at->dw_attr_val.v.val_vec);
9062       break;
9063
9064     case dw_val_class_flag:
9065       CHECKSUM_ULEB128 (DW_FORM_flag);
9066       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9067       break;
9068
9069     case dw_val_class_str:
9070       CHECKSUM_ULEB128 (DW_FORM_string);
9071       CHECKSUM_STRING (AT_string (at));
9072       break;
9073
9074     case dw_val_class_addr:
9075       r = AT_addr (at);
9076       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9077       CHECKSUM_ULEB128 (DW_FORM_string);
9078       CHECKSUM_STRING (XSTR (r, 0));
9079       break;
9080
9081     case dw_val_class_offset:
9082       CHECKSUM_ULEB128 (DW_FORM_sdata);
9083       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9084       break;
9085
9086     case dw_val_class_loc:
9087       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9088         loc_checksum_ordered (loc, ctx);
9089       break;
9090
9091     case dw_val_class_fde_ref:
9092     case dw_val_class_lbl_id:
9093     case dw_val_class_lineptr:
9094     case dw_val_class_macptr:
9095       break;
9096
9097     case dw_val_class_file:
9098       CHECKSUM_ULEB128 (DW_FORM_string);
9099       CHECKSUM_STRING (AT_file (at)->filename);
9100       break;
9101
9102     case dw_val_class_data8:
9103       CHECKSUM (at->dw_attr_val.v.val_data8);
9104       break;
9105
9106     default:
9107       break;
9108     }
9109 }
9110
9111 struct checksum_attributes
9112 {
9113   dw_attr_ref at_name;
9114   dw_attr_ref at_type;
9115   dw_attr_ref at_friend;
9116   dw_attr_ref at_accessibility;
9117   dw_attr_ref at_address_class;
9118   dw_attr_ref at_allocated;
9119   dw_attr_ref at_artificial;
9120   dw_attr_ref at_associated;
9121   dw_attr_ref at_binary_scale;
9122   dw_attr_ref at_bit_offset;
9123   dw_attr_ref at_bit_size;
9124   dw_attr_ref at_bit_stride;
9125   dw_attr_ref at_byte_size;
9126   dw_attr_ref at_byte_stride;
9127   dw_attr_ref at_const_value;
9128   dw_attr_ref at_containing_type;
9129   dw_attr_ref at_count;
9130   dw_attr_ref at_data_location;
9131   dw_attr_ref at_data_member_location;
9132   dw_attr_ref at_decimal_scale;
9133   dw_attr_ref at_decimal_sign;
9134   dw_attr_ref at_default_value;
9135   dw_attr_ref at_digit_count;
9136   dw_attr_ref at_discr;
9137   dw_attr_ref at_discr_list;
9138   dw_attr_ref at_discr_value;
9139   dw_attr_ref at_encoding;
9140   dw_attr_ref at_endianity;
9141   dw_attr_ref at_explicit;
9142   dw_attr_ref at_is_optional;
9143   dw_attr_ref at_location;
9144   dw_attr_ref at_lower_bound;
9145   dw_attr_ref at_mutable;
9146   dw_attr_ref at_ordering;
9147   dw_attr_ref at_picture_string;
9148   dw_attr_ref at_prototyped;
9149   dw_attr_ref at_small;
9150   dw_attr_ref at_segment;
9151   dw_attr_ref at_string_length;
9152   dw_attr_ref at_threads_scaled;
9153   dw_attr_ref at_upper_bound;
9154   dw_attr_ref at_use_location;
9155   dw_attr_ref at_use_UTF8;
9156   dw_attr_ref at_variable_parameter;
9157   dw_attr_ref at_virtuality;
9158   dw_attr_ref at_visibility;
9159   dw_attr_ref at_vtable_elem_location;
9160 };
9161
9162 /* Collect the attributes that we will want to use for the checksum.  */
9163
9164 static void
9165 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9166 {
9167   dw_attr_ref a;
9168   unsigned ix;
9169
9170   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9171     {
9172       switch (a->dw_attr)
9173         {
9174         case DW_AT_name:
9175           attrs->at_name = a;
9176           break;
9177         case DW_AT_type:
9178           attrs->at_type = a;
9179           break;
9180         case DW_AT_friend:
9181           attrs->at_friend = a;
9182           break;
9183         case DW_AT_accessibility:
9184           attrs->at_accessibility = a;
9185           break;
9186         case DW_AT_address_class:
9187           attrs->at_address_class = a;
9188           break;
9189         case DW_AT_allocated:
9190           attrs->at_allocated = a;
9191           break;
9192         case DW_AT_artificial:
9193           attrs->at_artificial = a;
9194           break;
9195         case DW_AT_associated:
9196           attrs->at_associated = a;
9197           break;
9198         case DW_AT_binary_scale:
9199           attrs->at_binary_scale = a;
9200           break;
9201         case DW_AT_bit_offset:
9202           attrs->at_bit_offset = a;
9203           break;
9204         case DW_AT_bit_size:
9205           attrs->at_bit_size = a;
9206           break;
9207         case DW_AT_bit_stride:
9208           attrs->at_bit_stride = a;
9209           break;
9210         case DW_AT_byte_size:
9211           attrs->at_byte_size = a;
9212           break;
9213         case DW_AT_byte_stride:
9214           attrs->at_byte_stride = a;
9215           break;
9216         case DW_AT_const_value:
9217           attrs->at_const_value = a;
9218           break;
9219         case DW_AT_containing_type:
9220           attrs->at_containing_type = a;
9221           break;
9222         case DW_AT_count:
9223           attrs->at_count = a;
9224           break;
9225         case DW_AT_data_location:
9226           attrs->at_data_location = a;
9227           break;
9228         case DW_AT_data_member_location:
9229           attrs->at_data_member_location = a;
9230           break;
9231         case DW_AT_decimal_scale:
9232           attrs->at_decimal_scale = a;
9233           break;
9234         case DW_AT_decimal_sign:
9235           attrs->at_decimal_sign = a;
9236           break;
9237         case DW_AT_default_value:
9238           attrs->at_default_value = a;
9239           break;
9240         case DW_AT_digit_count:
9241           attrs->at_digit_count = a;
9242           break;
9243         case DW_AT_discr:
9244           attrs->at_discr = a;
9245           break;
9246         case DW_AT_discr_list:
9247           attrs->at_discr_list = a;
9248           break;
9249         case DW_AT_discr_value:
9250           attrs->at_discr_value = a;
9251           break;
9252         case DW_AT_encoding:
9253           attrs->at_encoding = a;
9254           break;
9255         case DW_AT_endianity:
9256           attrs->at_endianity = a;
9257           break;
9258         case DW_AT_explicit:
9259           attrs->at_explicit = a;
9260           break;
9261         case DW_AT_is_optional:
9262           attrs->at_is_optional = a;
9263           break;
9264         case DW_AT_location:
9265           attrs->at_location = a;
9266           break;
9267         case DW_AT_lower_bound:
9268           attrs->at_lower_bound = a;
9269           break;
9270         case DW_AT_mutable:
9271           attrs->at_mutable = a;
9272           break;
9273         case DW_AT_ordering:
9274           attrs->at_ordering = a;
9275           break;
9276         case DW_AT_picture_string:
9277           attrs->at_picture_string = a;
9278           break;
9279         case DW_AT_prototyped:
9280           attrs->at_prototyped = a;
9281           break;
9282         case DW_AT_small:
9283           attrs->at_small = a;
9284           break;
9285         case DW_AT_segment:
9286           attrs->at_segment = a;
9287           break;
9288         case DW_AT_string_length:
9289           attrs->at_string_length = a;
9290           break;
9291         case DW_AT_threads_scaled:
9292           attrs->at_threads_scaled = a;
9293           break;
9294         case DW_AT_upper_bound:
9295           attrs->at_upper_bound = a;
9296           break;
9297         case DW_AT_use_location:
9298           attrs->at_use_location = a;
9299           break;
9300         case DW_AT_use_UTF8:
9301           attrs->at_use_UTF8 = a;
9302           break;
9303         case DW_AT_variable_parameter:
9304           attrs->at_variable_parameter = a;
9305           break;
9306         case DW_AT_virtuality:
9307           attrs->at_virtuality = a;
9308           break;
9309         case DW_AT_visibility:
9310           attrs->at_visibility = a;
9311           break;
9312         case DW_AT_vtable_elem_location:
9313           attrs->at_vtable_elem_location = a;
9314           break;
9315         default:
9316           break;
9317         }
9318     }
9319 }
9320
9321 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9322
9323 static void
9324 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9325 {
9326   dw_die_ref c;
9327   dw_die_ref decl;
9328   struct checksum_attributes attrs;
9329
9330   CHECKSUM_ULEB128 ('D');
9331   CHECKSUM_ULEB128 (die->die_tag);
9332
9333   memset (&attrs, 0, sizeof (attrs));
9334
9335   decl = get_AT_ref (die, DW_AT_specification);
9336   if (decl != NULL)
9337     collect_checksum_attributes (&attrs, decl);
9338   collect_checksum_attributes (&attrs, die);
9339
9340   CHECKSUM_ATTR (attrs.at_name);
9341   CHECKSUM_ATTR (attrs.at_accessibility);
9342   CHECKSUM_ATTR (attrs.at_address_class);
9343   CHECKSUM_ATTR (attrs.at_allocated);
9344   CHECKSUM_ATTR (attrs.at_artificial);
9345   CHECKSUM_ATTR (attrs.at_associated);
9346   CHECKSUM_ATTR (attrs.at_binary_scale);
9347   CHECKSUM_ATTR (attrs.at_bit_offset);
9348   CHECKSUM_ATTR (attrs.at_bit_size);
9349   CHECKSUM_ATTR (attrs.at_bit_stride);
9350   CHECKSUM_ATTR (attrs.at_byte_size);
9351   CHECKSUM_ATTR (attrs.at_byte_stride);
9352   CHECKSUM_ATTR (attrs.at_const_value);
9353   CHECKSUM_ATTR (attrs.at_containing_type);
9354   CHECKSUM_ATTR (attrs.at_count);
9355   CHECKSUM_ATTR (attrs.at_data_location);
9356   CHECKSUM_ATTR (attrs.at_data_member_location);
9357   CHECKSUM_ATTR (attrs.at_decimal_scale);
9358   CHECKSUM_ATTR (attrs.at_decimal_sign);
9359   CHECKSUM_ATTR (attrs.at_default_value);
9360   CHECKSUM_ATTR (attrs.at_digit_count);
9361   CHECKSUM_ATTR (attrs.at_discr);
9362   CHECKSUM_ATTR (attrs.at_discr_list);
9363   CHECKSUM_ATTR (attrs.at_discr_value);
9364   CHECKSUM_ATTR (attrs.at_encoding);
9365   CHECKSUM_ATTR (attrs.at_endianity);
9366   CHECKSUM_ATTR (attrs.at_explicit);
9367   CHECKSUM_ATTR (attrs.at_is_optional);
9368   CHECKSUM_ATTR (attrs.at_location);
9369   CHECKSUM_ATTR (attrs.at_lower_bound);
9370   CHECKSUM_ATTR (attrs.at_mutable);
9371   CHECKSUM_ATTR (attrs.at_ordering);
9372   CHECKSUM_ATTR (attrs.at_picture_string);
9373   CHECKSUM_ATTR (attrs.at_prototyped);
9374   CHECKSUM_ATTR (attrs.at_small);
9375   CHECKSUM_ATTR (attrs.at_segment);
9376   CHECKSUM_ATTR (attrs.at_string_length);
9377   CHECKSUM_ATTR (attrs.at_threads_scaled);
9378   CHECKSUM_ATTR (attrs.at_upper_bound);
9379   CHECKSUM_ATTR (attrs.at_use_location);
9380   CHECKSUM_ATTR (attrs.at_use_UTF8);
9381   CHECKSUM_ATTR (attrs.at_variable_parameter);
9382   CHECKSUM_ATTR (attrs.at_virtuality);
9383   CHECKSUM_ATTR (attrs.at_visibility);
9384   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9385   CHECKSUM_ATTR (attrs.at_type);
9386   CHECKSUM_ATTR (attrs.at_friend);
9387
9388   /* Checksum the child DIEs, except for nested types and member functions.  */
9389   c = die->die_child;
9390   if (c) do {
9391     dw_attr_ref name_attr;
9392
9393     c = c->die_sib;
9394     name_attr = get_AT (c, DW_AT_name);
9395     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9396         && name_attr != NULL)
9397       {
9398         CHECKSUM_ULEB128 ('S');
9399         CHECKSUM_ULEB128 (c->die_tag);
9400         CHECKSUM_STRING (AT_string (name_attr));
9401       }
9402     else
9403       {
9404         /* Mark this DIE so it gets processed when unmarking.  */
9405         if (c->die_mark == 0)
9406           c->die_mark = -1;
9407         die_checksum_ordered (c, ctx, mark);
9408       }
9409   } while (c != die->die_child);
9410
9411   CHECKSUM_ULEB128 (0);
9412 }
9413
9414 #undef CHECKSUM
9415 #undef CHECKSUM_STRING
9416 #undef CHECKSUM_ATTR
9417 #undef CHECKSUM_LEB128
9418 #undef CHECKSUM_ULEB128
9419
9420 /* Generate the type signature for DIE.  This is computed by generating an
9421    MD5 checksum over the DIE's tag, its relevant attributes, and its
9422    children.  Attributes that are references to other DIEs are processed
9423    by recursion, using the MARK field to prevent infinite recursion.
9424    If the DIE is nested inside a namespace or another type, we also
9425    need to include that context in the signature.  The lower 64 bits
9426    of the resulting MD5 checksum comprise the signature.  */
9427
9428 static void
9429 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9430 {
9431   int mark;
9432   const char *name;
9433   unsigned char checksum[16];
9434   struct md5_ctx ctx;
9435   dw_die_ref decl;
9436
9437   name = get_AT_string (die, DW_AT_name);
9438   decl = get_AT_ref (die, DW_AT_specification);
9439
9440   /* First, compute a signature for just the type name (and its surrounding
9441      context, if any.  This is stored in the type unit DIE for link-time
9442      ODR (one-definition rule) checking.  */
9443
9444   if (is_cxx() && name != NULL)
9445     {
9446       md5_init_ctx (&ctx);
9447
9448       /* Checksum the names of surrounding namespaces and structures.  */
9449       if (decl != NULL && decl->die_parent != NULL)
9450         checksum_die_context (decl->die_parent, &ctx);
9451
9452       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9453       md5_process_bytes (name, strlen (name) + 1, &ctx);
9454       md5_finish_ctx (&ctx, checksum);
9455
9456       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9457     }
9458
9459   /* Next, compute the complete type signature.  */
9460
9461   md5_init_ctx (&ctx);
9462   mark = 1;
9463   die->die_mark = mark;
9464
9465   /* Checksum the names of surrounding namespaces and structures.  */
9466   if (decl != NULL && decl->die_parent != NULL)
9467     checksum_die_context (decl->die_parent, &ctx);
9468
9469   /* Checksum the DIE and its children.  */
9470   die_checksum_ordered (die, &ctx, &mark);
9471   unmark_all_dies (die);
9472   md5_finish_ctx (&ctx, checksum);
9473
9474   /* Store the signature in the type node and link the type DIE and the
9475      type node together.  */
9476   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9477           DWARF_TYPE_SIGNATURE_SIZE);
9478   die->die_id.die_type_node = type_node;
9479   type_node->type_die = die;
9480
9481   /* If the DIE is a specification, link its declaration to the type node
9482      as well.  */
9483   if (decl != NULL)
9484     decl->die_id.die_type_node = type_node;
9485 }
9486
9487 /* Do the location expressions look same?  */
9488 static inline int
9489 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9490 {
9491   return loc1->dw_loc_opc == loc2->dw_loc_opc
9492          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9493          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9494 }
9495
9496 /* Do the values look the same?  */
9497 static int
9498 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9499 {
9500   dw_loc_descr_ref loc1, loc2;
9501   rtx r1, r2;
9502
9503   if (v1->val_class != v2->val_class)
9504     return 0;
9505
9506   switch (v1->val_class)
9507     {
9508     case dw_val_class_const:
9509       return v1->v.val_int == v2->v.val_int;
9510     case dw_val_class_unsigned_const:
9511       return v1->v.val_unsigned == v2->v.val_unsigned;
9512     case dw_val_class_const_double:
9513       return v1->v.val_double.high == v2->v.val_double.high
9514              && v1->v.val_double.low == v2->v.val_double.low;
9515     case dw_val_class_vec:
9516       if (v1->v.val_vec.length != v2->v.val_vec.length
9517           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9518         return 0;
9519       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9520                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9521         return 0;
9522       return 1;
9523     case dw_val_class_flag:
9524       return v1->v.val_flag == v2->v.val_flag;
9525     case dw_val_class_str:
9526       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9527
9528     case dw_val_class_addr:
9529       r1 = v1->v.val_addr;
9530       r2 = v2->v.val_addr;
9531       if (GET_CODE (r1) != GET_CODE (r2))
9532         return 0;
9533       return !rtx_equal_p (r1, r2);
9534
9535     case dw_val_class_offset:
9536       return v1->v.val_offset == v2->v.val_offset;
9537
9538     case dw_val_class_loc:
9539       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9540            loc1 && loc2;
9541            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9542         if (!same_loc_p (loc1, loc2, mark))
9543           return 0;
9544       return !loc1 && !loc2;
9545
9546     case dw_val_class_die_ref:
9547       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9548
9549     case dw_val_class_fde_ref:
9550     case dw_val_class_vms_delta:
9551     case dw_val_class_lbl_id:
9552     case dw_val_class_lineptr:
9553     case dw_val_class_macptr:
9554       return 1;
9555
9556     case dw_val_class_file:
9557       return v1->v.val_file == v2->v.val_file;
9558
9559     case dw_val_class_data8:
9560       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9561
9562     default:
9563       return 1;
9564     }
9565 }
9566
9567 /* Do the attributes look the same?  */
9568
9569 static int
9570 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9571 {
9572   if (at1->dw_attr != at2->dw_attr)
9573     return 0;
9574
9575   /* We don't care that this was compiled with a different compiler
9576      snapshot; if the output is the same, that's what matters. */
9577   if (at1->dw_attr == DW_AT_producer)
9578     return 1;
9579
9580   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9581 }
9582
9583 /* Do the dies look the same?  */
9584
9585 static int
9586 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9587 {
9588   dw_die_ref c1, c2;
9589   dw_attr_ref a1;
9590   unsigned ix;
9591
9592   /* To avoid infinite recursion.  */
9593   if (die1->die_mark)
9594     return die1->die_mark == die2->die_mark;
9595   die1->die_mark = die2->die_mark = ++(*mark);
9596
9597   if (die1->die_tag != die2->die_tag)
9598     return 0;
9599
9600   if (VEC_length (dw_attr_node, die1->die_attr)
9601       != VEC_length (dw_attr_node, die2->die_attr))
9602     return 0;
9603
9604   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9605     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9606       return 0;
9607
9608   c1 = die1->die_child;
9609   c2 = die2->die_child;
9610   if (! c1)
9611     {
9612       if (c2)
9613         return 0;
9614     }
9615   else
9616     for (;;)
9617       {
9618         if (!same_die_p (c1, c2, mark))
9619           return 0;
9620         c1 = c1->die_sib;
9621         c2 = c2->die_sib;
9622         if (c1 == die1->die_child)
9623           {
9624             if (c2 == die2->die_child)
9625               break;
9626             else
9627               return 0;
9628           }
9629     }
9630
9631   return 1;
9632 }
9633
9634 /* Do the dies look the same?  Wrapper around same_die_p.  */
9635
9636 static int
9637 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9638 {
9639   int mark = 0;
9640   int ret = same_die_p (die1, die2, &mark);
9641
9642   unmark_all_dies (die1);
9643   unmark_all_dies (die2);
9644
9645   return ret;
9646 }
9647
9648 /* The prefix to attach to symbols on DIEs in the current comdat debug
9649    info section.  */
9650 static char *comdat_symbol_id;
9651
9652 /* The index of the current symbol within the current comdat CU.  */
9653 static unsigned int comdat_symbol_number;
9654
9655 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9656    children, and set comdat_symbol_id accordingly.  */
9657
9658 static void
9659 compute_section_prefix (dw_die_ref unit_die)
9660 {
9661   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9662   const char *base = die_name ? lbasename (die_name) : "anonymous";
9663   char *name = XALLOCAVEC (char, strlen (base) + 64);
9664   char *p;
9665   int i, mark;
9666   unsigned char checksum[16];
9667   struct md5_ctx ctx;
9668
9669   /* Compute the checksum of the DIE, then append part of it as hex digits to
9670      the name filename of the unit.  */
9671
9672   md5_init_ctx (&ctx);
9673   mark = 0;
9674   die_checksum (unit_die, &ctx, &mark);
9675   unmark_all_dies (unit_die);
9676   md5_finish_ctx (&ctx, checksum);
9677
9678   sprintf (name, "%s.", base);
9679   clean_symbol_name (name);
9680
9681   p = name + strlen (name);
9682   for (i = 0; i < 4; i++)
9683     {
9684       sprintf (p, "%.2x", checksum[i]);
9685       p += 2;
9686     }
9687
9688   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9689   comdat_symbol_number = 0;
9690 }
9691
9692 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9693
9694 static int
9695 is_type_die (dw_die_ref die)
9696 {
9697   switch (die->die_tag)
9698     {
9699     case DW_TAG_array_type:
9700     case DW_TAG_class_type:
9701     case DW_TAG_interface_type:
9702     case DW_TAG_enumeration_type:
9703     case DW_TAG_pointer_type:
9704     case DW_TAG_reference_type:
9705     case DW_TAG_rvalue_reference_type:
9706     case DW_TAG_string_type:
9707     case DW_TAG_structure_type:
9708     case DW_TAG_subroutine_type:
9709     case DW_TAG_union_type:
9710     case DW_TAG_ptr_to_member_type:
9711     case DW_TAG_set_type:
9712     case DW_TAG_subrange_type:
9713     case DW_TAG_base_type:
9714     case DW_TAG_const_type:
9715     case DW_TAG_file_type:
9716     case DW_TAG_packed_type:
9717     case DW_TAG_volatile_type:
9718     case DW_TAG_typedef:
9719       return 1;
9720     default:
9721       return 0;
9722     }
9723 }
9724
9725 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9726    Basically, we want to choose the bits that are likely to be shared between
9727    compilations (types) and leave out the bits that are specific to individual
9728    compilations (functions).  */
9729
9730 static int
9731 is_comdat_die (dw_die_ref c)
9732 {
9733   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9734      we do for stabs.  The advantage is a greater likelihood of sharing between
9735      objects that don't include headers in the same order (and therefore would
9736      put the base types in a different comdat).  jason 8/28/00 */
9737
9738   if (c->die_tag == DW_TAG_base_type)
9739     return 0;
9740
9741   if (c->die_tag == DW_TAG_pointer_type
9742       || c->die_tag == DW_TAG_reference_type
9743       || c->die_tag == DW_TAG_rvalue_reference_type
9744       || c->die_tag == DW_TAG_const_type
9745       || c->die_tag == DW_TAG_volatile_type)
9746     {
9747       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9748
9749       return t ? is_comdat_die (t) : 0;
9750     }
9751
9752   return is_type_die (c);
9753 }
9754
9755 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9756    compilation unit.  */
9757
9758 static int
9759 is_symbol_die (dw_die_ref c)
9760 {
9761   return (is_type_die (c)
9762           || is_declaration_die (c)
9763           || c->die_tag == DW_TAG_namespace
9764           || c->die_tag == DW_TAG_module);
9765 }
9766
9767 /* Returns true iff C is a compile-unit DIE.  */
9768
9769 static inline bool
9770 is_cu_die (dw_die_ref c)
9771 {
9772   return c && c->die_tag == DW_TAG_compile_unit;
9773 }
9774
9775 static char *
9776 gen_internal_sym (const char *prefix)
9777 {
9778   char buf[256];
9779
9780   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9781   return xstrdup (buf);
9782 }
9783
9784 /* Assign symbols to all worthy DIEs under DIE.  */
9785
9786 static void
9787 assign_symbol_names (dw_die_ref die)
9788 {
9789   dw_die_ref c;
9790
9791   if (is_symbol_die (die))
9792     {
9793       if (comdat_symbol_id)
9794         {
9795           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9796
9797           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9798                    comdat_symbol_id, comdat_symbol_number++);
9799           die->die_id.die_symbol = xstrdup (p);
9800         }
9801       else
9802         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9803     }
9804
9805   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9806 }
9807
9808 struct cu_hash_table_entry
9809 {
9810   dw_die_ref cu;
9811   unsigned min_comdat_num, max_comdat_num;
9812   struct cu_hash_table_entry *next;
9813 };
9814
9815 /* Routines to manipulate hash table of CUs.  */
9816 static hashval_t
9817 htab_cu_hash (const void *of)
9818 {
9819   const struct cu_hash_table_entry *const entry =
9820     (const struct cu_hash_table_entry *) of;
9821
9822   return htab_hash_string (entry->cu->die_id.die_symbol);
9823 }
9824
9825 static int
9826 htab_cu_eq (const void *of1, const void *of2)
9827 {
9828   const struct cu_hash_table_entry *const entry1 =
9829     (const struct cu_hash_table_entry *) of1;
9830   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9831
9832   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9833 }
9834
9835 static void
9836 htab_cu_del (void *what)
9837 {
9838   struct cu_hash_table_entry *next,
9839     *entry = (struct cu_hash_table_entry *) what;
9840
9841   while (entry)
9842     {
9843       next = entry->next;
9844       free (entry);
9845       entry = next;
9846     }
9847 }
9848
9849 /* Check whether we have already seen this CU and set up SYM_NUM
9850    accordingly.  */
9851 static int
9852 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9853 {
9854   struct cu_hash_table_entry dummy;
9855   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9856
9857   dummy.max_comdat_num = 0;
9858
9859   slot = (struct cu_hash_table_entry **)
9860     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9861         INSERT);
9862   entry = *slot;
9863
9864   for (; entry; last = entry, entry = entry->next)
9865     {
9866       if (same_die_p_wrap (cu, entry->cu))
9867         break;
9868     }
9869
9870   if (entry)
9871     {
9872       *sym_num = entry->min_comdat_num;
9873       return 1;
9874     }
9875
9876   entry = XCNEW (struct cu_hash_table_entry);
9877   entry->cu = cu;
9878   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9879   entry->next = *slot;
9880   *slot = entry;
9881
9882   return 0;
9883 }
9884
9885 /* Record SYM_NUM to record of CU in HTABLE.  */
9886 static void
9887 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9888 {
9889   struct cu_hash_table_entry **slot, *entry;
9890
9891   slot = (struct cu_hash_table_entry **)
9892     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9893         NO_INSERT);
9894   entry = *slot;
9895
9896   entry->max_comdat_num = sym_num;
9897 }
9898
9899 /* Traverse the DIE (which is always comp_unit_die), and set up
9900    additional compilation units for each of the include files we see
9901    bracketed by BINCL/EINCL.  */
9902
9903 static void
9904 break_out_includes (dw_die_ref die)
9905 {
9906   dw_die_ref c;
9907   dw_die_ref unit = NULL;
9908   limbo_die_node *node, **pnode;
9909   htab_t cu_hash_table;
9910
9911   c = die->die_child;
9912   if (c) do {
9913     dw_die_ref prev = c;
9914     c = c->die_sib;
9915     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9916            || (unit && is_comdat_die (c)))
9917       {
9918         dw_die_ref next = c->die_sib;
9919
9920         /* This DIE is for a secondary CU; remove it from the main one.  */
9921         remove_child_with_prev (c, prev);
9922
9923         if (c->die_tag == DW_TAG_GNU_BINCL)
9924           unit = push_new_compile_unit (unit, c);
9925         else if (c->die_tag == DW_TAG_GNU_EINCL)
9926           unit = pop_compile_unit (unit);
9927         else
9928           add_child_die (unit, c);
9929         c = next;
9930         if (c == die->die_child)
9931           break;
9932       }
9933   } while (c != die->die_child);
9934
9935 #if 0
9936   /* We can only use this in debugging, since the frontend doesn't check
9937      to make sure that we leave every include file we enter.  */
9938   gcc_assert (!unit);
9939 #endif
9940
9941   assign_symbol_names (die);
9942   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9943   for (node = limbo_die_list, pnode = &limbo_die_list;
9944        node;
9945        node = node->next)
9946     {
9947       int is_dupl;
9948
9949       compute_section_prefix (node->die);
9950       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9951                         &comdat_symbol_number);
9952       assign_symbol_names (node->die);
9953       if (is_dupl)
9954         *pnode = node->next;
9955       else
9956         {
9957           pnode = &node->next;
9958           record_comdat_symbol_number (node->die, cu_hash_table,
9959                 comdat_symbol_number);
9960         }
9961     }
9962   htab_delete (cu_hash_table);
9963 }
9964
9965 /* Return non-zero if this DIE is a declaration.  */
9966
9967 static int
9968 is_declaration_die (dw_die_ref die)
9969 {
9970   dw_attr_ref a;
9971   unsigned ix;
9972
9973   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9974     if (a->dw_attr == DW_AT_declaration)
9975       return 1;
9976
9977   return 0;
9978 }
9979
9980 /* Return non-zero if this DIE is nested inside a subprogram.  */
9981
9982 static int
9983 is_nested_in_subprogram (dw_die_ref die)
9984 {
9985   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9986
9987   if (decl == NULL)
9988     decl = die;
9989   return local_scope_p (decl);
9990 }
9991
9992 /* Return non-zero if this is a type DIE that should be moved to a
9993    COMDAT .debug_types section.  */
9994
9995 static int
9996 should_move_die_to_comdat (dw_die_ref die)
9997 {
9998   switch (die->die_tag)
9999     {
10000     case DW_TAG_class_type:
10001     case DW_TAG_structure_type:
10002     case DW_TAG_enumeration_type:
10003     case DW_TAG_union_type:
10004       /* Don't move declarations, inlined instances, or types nested in a
10005          subprogram.  */
10006       if (is_declaration_die (die)
10007           || get_AT (die, DW_AT_abstract_origin)
10008           || is_nested_in_subprogram (die))
10009         return 0;
10010       return 1;
10011     case DW_TAG_array_type:
10012     case DW_TAG_interface_type:
10013     case DW_TAG_pointer_type:
10014     case DW_TAG_reference_type:
10015     case DW_TAG_rvalue_reference_type:
10016     case DW_TAG_string_type:
10017     case DW_TAG_subroutine_type:
10018     case DW_TAG_ptr_to_member_type:
10019     case DW_TAG_set_type:
10020     case DW_TAG_subrange_type:
10021     case DW_TAG_base_type:
10022     case DW_TAG_const_type:
10023     case DW_TAG_file_type:
10024     case DW_TAG_packed_type:
10025     case DW_TAG_volatile_type:
10026     case DW_TAG_typedef:
10027     default:
10028       return 0;
10029     }
10030 }
10031
10032 /* Make a clone of DIE.  */
10033
10034 static dw_die_ref
10035 clone_die (dw_die_ref die)
10036 {
10037   dw_die_ref clone;
10038   dw_attr_ref a;
10039   unsigned ix;
10040
10041   clone = ggc_alloc_cleared_die_node ();
10042   clone->die_tag = die->die_tag;
10043
10044   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10045     add_dwarf_attr (clone, a);
10046
10047   return clone;
10048 }
10049
10050 /* Make a clone of the tree rooted at DIE.  */
10051
10052 static dw_die_ref
10053 clone_tree (dw_die_ref die)
10054 {
10055   dw_die_ref c;
10056   dw_die_ref clone = clone_die (die);
10057
10058   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10059
10060   return clone;
10061 }
10062
10063 /* Make a clone of DIE as a declaration.  */
10064
10065 static dw_die_ref
10066 clone_as_declaration (dw_die_ref die)
10067 {
10068   dw_die_ref clone;
10069   dw_die_ref decl;
10070   dw_attr_ref a;
10071   unsigned ix;
10072
10073   /* If the DIE is already a declaration, just clone it.  */
10074   if (is_declaration_die (die))
10075     return clone_die (die);
10076
10077   /* If the DIE is a specification, just clone its declaration DIE.  */
10078   decl = get_AT_ref (die, DW_AT_specification);
10079   if (decl != NULL)
10080     return clone_die (decl);
10081
10082   clone = ggc_alloc_cleared_die_node ();
10083   clone->die_tag = die->die_tag;
10084
10085   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10086     {
10087       /* We don't want to copy over all attributes.
10088          For example we don't want DW_AT_byte_size because otherwise we will no
10089          longer have a declaration and GDB will treat it as a definition.  */
10090
10091       switch (a->dw_attr)
10092         {
10093         case DW_AT_artificial:
10094         case DW_AT_containing_type:
10095         case DW_AT_external:
10096         case DW_AT_name:
10097         case DW_AT_type:
10098         case DW_AT_virtuality:
10099         case DW_AT_linkage_name:
10100         case DW_AT_MIPS_linkage_name:
10101           add_dwarf_attr (clone, a);
10102           break;
10103         case DW_AT_byte_size:
10104         default:
10105           break;
10106         }
10107     }
10108
10109   if (die->die_id.die_type_node)
10110     add_AT_die_ref (clone, DW_AT_signature, die);
10111
10112   add_AT_flag (clone, DW_AT_declaration, 1);
10113   return clone;
10114 }
10115
10116 /* Copy the declaration context to the new compile unit DIE.  This includes
10117    any surrounding namespace or type declarations.  If the DIE has an
10118    AT_specification attribute, it also includes attributes and children
10119    attached to the specification.  */
10120
10121 static void
10122 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10123 {
10124   dw_die_ref decl;
10125   dw_die_ref new_decl;
10126
10127   decl = get_AT_ref (die, DW_AT_specification);
10128   if (decl == NULL)
10129     decl = die;
10130   else
10131     {
10132       unsigned ix;
10133       dw_die_ref c;
10134       dw_attr_ref a;
10135
10136       /* Copy the type node pointer from the new DIE to the original
10137          declaration DIE so we can forward references later.  */
10138       decl->die_id.die_type_node = die->die_id.die_type_node;
10139
10140       remove_AT (die, DW_AT_specification);
10141
10142       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10143         {
10144           if (a->dw_attr != DW_AT_name
10145               && a->dw_attr != DW_AT_declaration
10146               && a->dw_attr != DW_AT_external)
10147             add_dwarf_attr (die, a);
10148         }
10149
10150       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10151     }
10152
10153   if (decl->die_parent != NULL
10154       && decl->die_parent->die_tag != DW_TAG_compile_unit
10155       && decl->die_parent->die_tag != DW_TAG_type_unit)
10156     {
10157       new_decl = copy_ancestor_tree (unit, decl, NULL);
10158       if (new_decl != NULL)
10159         {
10160           remove_AT (new_decl, DW_AT_signature);
10161           add_AT_specification (die, new_decl);
10162         }
10163     }
10164 }
10165
10166 /* Generate the skeleton ancestor tree for the given NODE, then clone
10167    the DIE and add the clone into the tree.  */
10168
10169 static void
10170 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10171 {
10172   if (node->new_die != NULL)
10173     return;
10174
10175   node->new_die = clone_as_declaration (node->old_die);
10176
10177   if (node->parent != NULL)
10178     {
10179       generate_skeleton_ancestor_tree (node->parent);
10180       add_child_die (node->parent->new_die, node->new_die);
10181     }
10182 }
10183
10184 /* Generate a skeleton tree of DIEs containing any declarations that are
10185    found in the original tree.  We traverse the tree looking for declaration
10186    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10187
10188 static void
10189 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10190 {
10191   skeleton_chain_node node;
10192   dw_die_ref c;
10193   dw_die_ref first;
10194   dw_die_ref prev = NULL;
10195   dw_die_ref next = NULL;
10196
10197   node.parent = parent;
10198
10199   first = c = parent->old_die->die_child;
10200   if (c)
10201     next = c->die_sib;
10202   if (c) do {
10203     if (prev == NULL || prev->die_sib == c)
10204       prev = c;
10205     c = next;
10206     next = (c == first ? NULL : c->die_sib);
10207     node.old_die = c;
10208     node.new_die = NULL;
10209     if (is_declaration_die (c))
10210       {
10211         /* Clone the existing DIE, move the original to the skeleton
10212            tree (which is in the main CU), and put the clone, with
10213            all the original's children, where the original came from.  */
10214         dw_die_ref clone = clone_die (c);
10215         move_all_children (c, clone);
10216
10217         replace_child (c, clone, prev);
10218         generate_skeleton_ancestor_tree (parent);
10219         add_child_die (parent->new_die, c);
10220         node.new_die = c;
10221         c = clone;
10222       }
10223     generate_skeleton_bottom_up (&node);
10224   } while (next != NULL);
10225 }
10226
10227 /* Wrapper function for generate_skeleton_bottom_up.  */
10228
10229 static dw_die_ref
10230 generate_skeleton (dw_die_ref die)
10231 {
10232   skeleton_chain_node node;
10233
10234   node.old_die = die;
10235   node.new_die = NULL;
10236   node.parent = NULL;
10237
10238   /* If this type definition is nested inside another type,
10239      always leave at least a declaration in its place.  */
10240   if (die->die_parent != NULL && is_type_die (die->die_parent))
10241     node.new_die = clone_as_declaration (die);
10242
10243   generate_skeleton_bottom_up (&node);
10244   return node.new_die;
10245 }
10246
10247 /* Remove the DIE from its parent, possibly replacing it with a cloned
10248    declaration.  The original DIE will be moved to a new compile unit
10249    so that existing references to it follow it to the new location.  If
10250    any of the original DIE's descendants is a declaration, we need to
10251    replace the original DIE with a skeleton tree and move the
10252    declarations back into the skeleton tree.  */
10253
10254 static dw_die_ref
10255 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10256 {
10257   dw_die_ref skeleton;
10258
10259   skeleton = generate_skeleton (child);
10260   if (skeleton == NULL)
10261     remove_child_with_prev (child, prev);
10262   else
10263     {
10264       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10265       replace_child (child, skeleton, prev);
10266     }
10267
10268   return skeleton;
10269 }
10270
10271 /* Traverse the DIE and set up additional .debug_types sections for each
10272    type worthy of being placed in a COMDAT section.  */
10273
10274 static void
10275 break_out_comdat_types (dw_die_ref die)
10276 {
10277   dw_die_ref c;
10278   dw_die_ref first;
10279   dw_die_ref prev = NULL;
10280   dw_die_ref next = NULL;
10281   dw_die_ref unit = NULL;
10282
10283   first = c = die->die_child;
10284   if (c)
10285     next = c->die_sib;
10286   if (c) do {
10287     if (prev == NULL || prev->die_sib == c)
10288       prev = c;
10289     c = next;
10290     next = (c == first ? NULL : c->die_sib);
10291     if (should_move_die_to_comdat (c))
10292       {
10293         dw_die_ref replacement;
10294         comdat_type_node_ref type_node;
10295
10296         /* Create a new type unit DIE as the root for the new tree, and
10297            add it to the list of comdat types.  */
10298         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10299         add_AT_unsigned (unit, DW_AT_language,
10300                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10301         type_node = ggc_alloc_cleared_comdat_type_node ();
10302         type_node->root_die = unit;
10303         type_node->next = comdat_type_list;
10304         comdat_type_list = type_node;
10305
10306         /* Generate the type signature.  */
10307         generate_type_signature (c, type_node);
10308
10309         /* Copy the declaration context, attributes, and children of the
10310            declaration into the new compile unit DIE.  */
10311         copy_declaration_context (unit, c);
10312
10313         /* Remove this DIE from the main CU.  */
10314         replacement = remove_child_or_replace_with_skeleton (c, prev);
10315
10316         /* Break out nested types into their own type units.  */
10317         break_out_comdat_types (c);
10318
10319         /* Add the DIE to the new compunit.  */
10320         add_child_die (unit, c);
10321
10322         if (replacement != NULL)
10323           c = replacement;
10324       }
10325     else if (c->die_tag == DW_TAG_namespace
10326              || c->die_tag == DW_TAG_class_type
10327              || c->die_tag == DW_TAG_structure_type
10328              || c->die_tag == DW_TAG_union_type)
10329       {
10330         /* Look for nested types that can be broken out.  */
10331         break_out_comdat_types (c);
10332       }
10333   } while (next != NULL);
10334 }
10335
10336 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10337
10338 struct decl_table_entry
10339 {
10340   dw_die_ref orig;
10341   dw_die_ref copy;
10342 };
10343
10344 /* Routines to manipulate hash table of copied declarations.  */
10345
10346 static hashval_t
10347 htab_decl_hash (const void *of)
10348 {
10349   const struct decl_table_entry *const entry =
10350     (const struct decl_table_entry *) of;
10351
10352   return htab_hash_pointer (entry->orig);
10353 }
10354
10355 static int
10356 htab_decl_eq (const void *of1, const void *of2)
10357 {
10358   const struct decl_table_entry *const entry1 =
10359     (const struct decl_table_entry *) of1;
10360   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10361
10362   return entry1->orig == entry2;
10363 }
10364
10365 static void
10366 htab_decl_del (void *what)
10367 {
10368   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10369
10370   free (entry);
10371 }
10372
10373 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10374    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10375    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10376    to check if the ancestor has already been copied into UNIT.  */
10377
10378 static dw_die_ref
10379 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10380 {
10381   dw_die_ref parent = die->die_parent;
10382   dw_die_ref new_parent = unit;
10383   dw_die_ref copy;
10384   void **slot = NULL;
10385   struct decl_table_entry *entry = NULL;
10386
10387   if (decl_table)
10388     {
10389       /* Check if the entry has already been copied to UNIT.  */
10390       slot = htab_find_slot_with_hash (decl_table, die,
10391                                        htab_hash_pointer (die), INSERT);
10392       if (*slot != HTAB_EMPTY_ENTRY)
10393         {
10394           entry = (struct decl_table_entry *) *slot;
10395           return entry->copy;
10396         }
10397
10398       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10399       entry = XCNEW (struct decl_table_entry);
10400       entry->orig = die;
10401       entry->copy = NULL;
10402       *slot = entry;
10403     }
10404
10405   if (parent != NULL)
10406     {
10407       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10408       if (spec != NULL)
10409         parent = spec;
10410       if (parent->die_tag != DW_TAG_compile_unit
10411           && parent->die_tag != DW_TAG_type_unit)
10412         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10413     }
10414
10415   copy = clone_as_declaration (die);
10416   add_child_die (new_parent, copy);
10417
10418   if (decl_table != NULL)
10419     {
10420       /* Record the pointer to the copy.  */
10421       entry->copy = copy;
10422     }
10423
10424   return copy;
10425 }
10426
10427 /* Walk the DIE and its children, looking for references to incomplete
10428    or trivial types that are unmarked (i.e., that are not in the current
10429    type_unit).  */
10430
10431 static void
10432 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10433 {
10434   dw_die_ref c;
10435   dw_attr_ref a;
10436   unsigned ix;
10437
10438   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10439     {
10440       if (AT_class (a) == dw_val_class_die_ref)
10441         {
10442           dw_die_ref targ = AT_ref (a);
10443           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10444           void **slot;
10445           struct decl_table_entry *entry;
10446
10447           if (targ->die_mark != 0 || type_node != NULL)
10448             continue;
10449
10450           slot = htab_find_slot_with_hash (decl_table, targ,
10451                                            htab_hash_pointer (targ), INSERT);
10452
10453           if (*slot != HTAB_EMPTY_ENTRY)
10454             {
10455               /* TARG has already been copied, so we just need to
10456                  modify the reference to point to the copy.  */
10457               entry = (struct decl_table_entry *) *slot;
10458               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10459             }
10460           else
10461             {
10462               dw_die_ref parent = unit;
10463               dw_die_ref copy = clone_tree (targ);
10464
10465               /* Make sure the cloned tree is marked as part of the
10466                  type unit.  */
10467               mark_dies (copy);
10468
10469               /* Record in DECL_TABLE that TARG has been copied.
10470                  Need to do this now, before the recursive call,
10471                  because DECL_TABLE may be expanded and SLOT
10472                  would no longer be a valid pointer.  */
10473               entry = XCNEW (struct decl_table_entry);
10474               entry->orig = targ;
10475               entry->copy = copy;
10476               *slot = entry;
10477
10478               /* If TARG has surrounding context, copy its ancestor tree
10479                  into the new type unit.  */
10480               if (targ->die_parent != NULL
10481                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10482                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10483                 parent = copy_ancestor_tree (unit, targ->die_parent,
10484                                              decl_table);
10485
10486               add_child_die (parent, copy);
10487               a->dw_attr_val.v.val_die_ref.die = copy;
10488
10489               /* Make sure the newly-copied DIE is walked.  If it was
10490                  installed in a previously-added context, it won't
10491                  get visited otherwise.  */
10492               if (parent != unit)
10493                 {
10494                   /* Find the highest point of the newly-added tree,
10495                      mark each node along the way, and walk from there.  */
10496                   parent->die_mark = 1;
10497                   while (parent->die_parent
10498                          && parent->die_parent->die_mark == 0)
10499                     {
10500                       parent = parent->die_parent;
10501                       parent->die_mark = 1;
10502                     }
10503                   copy_decls_walk (unit, parent, decl_table);
10504                 }
10505             }
10506         }
10507     }
10508
10509   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10510 }
10511
10512 /* Copy declarations for "unworthy" types into the new comdat section.
10513    Incomplete types, modified types, and certain other types aren't broken
10514    out into comdat sections of their own, so they don't have a signature,
10515    and we need to copy the declaration into the same section so that we
10516    don't have an external reference.  */
10517
10518 static void
10519 copy_decls_for_unworthy_types (dw_die_ref unit)
10520 {
10521   htab_t decl_table;
10522
10523   mark_dies (unit);
10524   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10525   copy_decls_walk (unit, unit, decl_table);
10526   htab_delete (decl_table);
10527   unmark_dies (unit);
10528 }
10529
10530 /* Traverse the DIE and add a sibling attribute if it may have the
10531    effect of speeding up access to siblings.  To save some space,
10532    avoid generating sibling attributes for DIE's without children.  */
10533
10534 static void
10535 add_sibling_attributes (dw_die_ref die)
10536 {
10537   dw_die_ref c;
10538
10539   if (! die->die_child)
10540     return;
10541
10542   if (die->die_parent && die != die->die_parent->die_child)
10543     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10544
10545   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10546 }
10547
10548 /* Output all location lists for the DIE and its children.  */
10549
10550 static void
10551 output_location_lists (dw_die_ref die)
10552 {
10553   dw_die_ref c;
10554   dw_attr_ref a;
10555   unsigned ix;
10556
10557   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10558     if (AT_class (a) == dw_val_class_loc_list)
10559       output_loc_list (AT_loc_list (a));
10560
10561   FOR_EACH_CHILD (die, c, output_location_lists (c));
10562 }
10563
10564 /* The format of each DIE (and its attribute value pairs) is encoded in an
10565    abbreviation table.  This routine builds the abbreviation table and assigns
10566    a unique abbreviation id for each abbreviation entry.  The children of each
10567    die are visited recursively.  */
10568
10569 static void
10570 build_abbrev_table (dw_die_ref die)
10571 {
10572   unsigned long abbrev_id;
10573   unsigned int n_alloc;
10574   dw_die_ref c;
10575   dw_attr_ref a;
10576   unsigned ix;
10577
10578   /* Scan the DIE references, and mark as external any that refer to
10579      DIEs from other CUs (i.e. those which are not marked).  */
10580   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10581     if (AT_class (a) == dw_val_class_die_ref
10582         && AT_ref (a)->die_mark == 0)
10583       {
10584         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10585         set_AT_ref_external (a, 1);
10586       }
10587
10588   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10589     {
10590       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10591       dw_attr_ref die_a, abbrev_a;
10592       unsigned ix;
10593       bool ok = true;
10594
10595       if (abbrev->die_tag != die->die_tag)
10596         continue;
10597       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10598         continue;
10599
10600       if (VEC_length (dw_attr_node, abbrev->die_attr)
10601           != VEC_length (dw_attr_node, die->die_attr))
10602         continue;
10603
10604       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10605         {
10606           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10607           if ((abbrev_a->dw_attr != die_a->dw_attr)
10608               || (value_format (abbrev_a) != value_format (die_a)))
10609             {
10610               ok = false;
10611               break;
10612             }
10613         }
10614       if (ok)
10615         break;
10616     }
10617
10618   if (abbrev_id >= abbrev_die_table_in_use)
10619     {
10620       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10621         {
10622           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10623           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10624                                             n_alloc);
10625
10626           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10627                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10628           abbrev_die_table_allocated = n_alloc;
10629         }
10630
10631       ++abbrev_die_table_in_use;
10632       abbrev_die_table[abbrev_id] = die;
10633     }
10634
10635   die->die_abbrev = abbrev_id;
10636   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10637 }
10638 \f
10639 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10640
10641 static int
10642 constant_size (unsigned HOST_WIDE_INT value)
10643 {
10644   int log;
10645
10646   if (value == 0)
10647     log = 0;
10648   else
10649     log = floor_log2 (value);
10650
10651   log = log / 8;
10652   log = 1 << (floor_log2 (log) + 1);
10653
10654   return log;
10655 }
10656
10657 /* Return the size of a DIE as it is represented in the
10658    .debug_info section.  */
10659
10660 static unsigned long
10661 size_of_die (dw_die_ref die)
10662 {
10663   unsigned long size = 0;
10664   dw_attr_ref a;
10665   unsigned ix;
10666
10667   size += size_of_uleb128 (die->die_abbrev);
10668   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10669     {
10670       switch (AT_class (a))
10671         {
10672         case dw_val_class_addr:
10673           size += DWARF2_ADDR_SIZE;
10674           break;
10675         case dw_val_class_offset:
10676           size += DWARF_OFFSET_SIZE;
10677           break;
10678         case dw_val_class_loc:
10679           {
10680             unsigned long lsize = size_of_locs (AT_loc (a));
10681
10682             /* Block length.  */
10683             if (dwarf_version >= 4)
10684               size += size_of_uleb128 (lsize);
10685             else
10686               size += constant_size (lsize);
10687             size += lsize;
10688           }
10689           break;
10690         case dw_val_class_loc_list:
10691           size += DWARF_OFFSET_SIZE;
10692           break;
10693         case dw_val_class_range_list:
10694           size += DWARF_OFFSET_SIZE;
10695           break;
10696         case dw_val_class_const:
10697           size += size_of_sleb128 (AT_int (a));
10698           break;
10699         case dw_val_class_unsigned_const:
10700           size += constant_size (AT_unsigned (a));
10701           break;
10702         case dw_val_class_const_double:
10703           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10704           if (HOST_BITS_PER_WIDE_INT >= 64)
10705             size++; /* block */
10706           break;
10707         case dw_val_class_vec:
10708           size += constant_size (a->dw_attr_val.v.val_vec.length
10709                                  * a->dw_attr_val.v.val_vec.elt_size)
10710                   + a->dw_attr_val.v.val_vec.length
10711                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10712           break;
10713         case dw_val_class_flag:
10714           if (dwarf_version >= 4)
10715             /* Currently all add_AT_flag calls pass in 1 as last argument,
10716                so DW_FORM_flag_present can be used.  If that ever changes,
10717                we'll need to use DW_FORM_flag and have some optimization
10718                in build_abbrev_table that will change those to
10719                DW_FORM_flag_present if it is set to 1 in all DIEs using
10720                the same abbrev entry.  */
10721             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10722           else
10723             size += 1;
10724           break;
10725         case dw_val_class_die_ref:
10726           if (AT_ref_external (a))
10727             {
10728               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10729                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10730                  is sized by target address length, whereas in DWARF3
10731                  it's always sized as an offset.  */
10732               if (dwarf_version >= 4)
10733                 size += DWARF_TYPE_SIGNATURE_SIZE;
10734               else if (dwarf_version == 2)
10735                 size += DWARF2_ADDR_SIZE;
10736               else
10737                 size += DWARF_OFFSET_SIZE;
10738             }
10739           else
10740             size += DWARF_OFFSET_SIZE;
10741           break;
10742         case dw_val_class_fde_ref:
10743           size += DWARF_OFFSET_SIZE;
10744           break;
10745         case dw_val_class_lbl_id:
10746           size += DWARF2_ADDR_SIZE;
10747           break;
10748         case dw_val_class_lineptr:
10749         case dw_val_class_macptr:
10750           size += DWARF_OFFSET_SIZE;
10751           break;
10752         case dw_val_class_str:
10753           if (AT_string_form (a) == DW_FORM_strp)
10754             size += DWARF_OFFSET_SIZE;
10755           else
10756             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10757           break;
10758         case dw_val_class_file:
10759           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10760           break;
10761         case dw_val_class_data8:
10762           size += 8;
10763           break;
10764         case dw_val_class_vms_delta:
10765           size += DWARF_OFFSET_SIZE;
10766           break;
10767         default:
10768           gcc_unreachable ();
10769         }
10770     }
10771
10772   return size;
10773 }
10774
10775 /* Size the debugging information associated with a given DIE.  Visits the
10776    DIE's children recursively.  Updates the global variable next_die_offset, on
10777    each time through.  Uses the current value of next_die_offset to update the
10778    die_offset field in each DIE.  */
10779
10780 static void
10781 calc_die_sizes (dw_die_ref die)
10782 {
10783   dw_die_ref c;
10784
10785   die->die_offset = next_die_offset;
10786   next_die_offset += size_of_die (die);
10787
10788   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10789
10790   if (die->die_child != NULL)
10791     /* Count the null byte used to terminate sibling lists.  */
10792     next_die_offset += 1;
10793 }
10794
10795 /* Set the marks for a die and its children.  We do this so
10796    that we know whether or not a reference needs to use FORM_ref_addr; only
10797    DIEs in the same CU will be marked.  We used to clear out the offset
10798    and use that as the flag, but ran into ordering problems.  */
10799
10800 static void
10801 mark_dies (dw_die_ref die)
10802 {
10803   dw_die_ref c;
10804
10805   gcc_assert (!die->die_mark);
10806
10807   die->die_mark = 1;
10808   FOR_EACH_CHILD (die, c, mark_dies (c));
10809 }
10810
10811 /* Clear the marks for a die and its children.  */
10812
10813 static void
10814 unmark_dies (dw_die_ref die)
10815 {
10816   dw_die_ref c;
10817
10818   if (dwarf_version < 4)
10819     gcc_assert (die->die_mark);
10820
10821   die->die_mark = 0;
10822   FOR_EACH_CHILD (die, c, unmark_dies (c));
10823 }
10824
10825 /* Clear the marks for a die, its children and referred dies.  */
10826
10827 static void
10828 unmark_all_dies (dw_die_ref die)
10829 {
10830   dw_die_ref c;
10831   dw_attr_ref a;
10832   unsigned ix;
10833
10834   if (!die->die_mark)
10835     return;
10836   die->die_mark = 0;
10837
10838   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10839
10840   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10841     if (AT_class (a) == dw_val_class_die_ref)
10842       unmark_all_dies (AT_ref (a));
10843 }
10844
10845 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10846    generated for the compilation unit.  */
10847
10848 static unsigned long
10849 size_of_pubnames (VEC (pubname_entry, gc) * names)
10850 {
10851   unsigned long size;
10852   unsigned i;
10853   pubname_ref p;
10854
10855   size = DWARF_PUBNAMES_HEADER_SIZE;
10856   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10857     if (names != pubtype_table
10858         || p->die->die_offset != 0
10859         || !flag_eliminate_unused_debug_types)
10860       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10861
10862   size += DWARF_OFFSET_SIZE;
10863   return size;
10864 }
10865
10866 /* Return the size of the information in the .debug_aranges section.  */
10867
10868 static unsigned long
10869 size_of_aranges (void)
10870 {
10871   unsigned long size;
10872
10873   size = DWARF_ARANGES_HEADER_SIZE;
10874
10875   /* Count the address/length pair for this compilation unit.  */
10876   if (text_section_used)
10877     size += 2 * DWARF2_ADDR_SIZE;
10878   if (cold_text_section_used)
10879     size += 2 * DWARF2_ADDR_SIZE;
10880   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10881
10882   /* Count the two zero words used to terminated the address range table.  */
10883   size += 2 * DWARF2_ADDR_SIZE;
10884   return size;
10885 }
10886 \f
10887 /* Select the encoding of an attribute value.  */
10888
10889 static enum dwarf_form
10890 value_format (dw_attr_ref a)
10891 {
10892   switch (a->dw_attr_val.val_class)
10893     {
10894     case dw_val_class_addr:
10895       /* Only very few attributes allow DW_FORM_addr.  */
10896       switch (a->dw_attr)
10897         {
10898         case DW_AT_low_pc:
10899         case DW_AT_high_pc:
10900         case DW_AT_entry_pc:
10901         case DW_AT_trampoline:
10902           return DW_FORM_addr;
10903         default:
10904           break;
10905         }
10906       switch (DWARF2_ADDR_SIZE)
10907         {
10908         case 1:
10909           return DW_FORM_data1;
10910         case 2:
10911           return DW_FORM_data2;
10912         case 4:
10913           return DW_FORM_data4;
10914         case 8:
10915           return DW_FORM_data8;
10916         default:
10917           gcc_unreachable ();
10918         }
10919     case dw_val_class_range_list:
10920     case dw_val_class_loc_list:
10921       if (dwarf_version >= 4)
10922         return DW_FORM_sec_offset;
10923       /* FALLTHRU */
10924     case dw_val_class_vms_delta:
10925     case dw_val_class_offset:
10926       switch (DWARF_OFFSET_SIZE)
10927         {
10928         case 4:
10929           return DW_FORM_data4;
10930         case 8:
10931           return DW_FORM_data8;
10932         default:
10933           gcc_unreachable ();
10934         }
10935     case dw_val_class_loc:
10936       if (dwarf_version >= 4)
10937         return DW_FORM_exprloc;
10938       switch (constant_size (size_of_locs (AT_loc (a))))
10939         {
10940         case 1:
10941           return DW_FORM_block1;
10942         case 2:
10943           return DW_FORM_block2;
10944         default:
10945           gcc_unreachable ();
10946         }
10947     case dw_val_class_const:
10948       return DW_FORM_sdata;
10949     case dw_val_class_unsigned_const:
10950       switch (constant_size (AT_unsigned (a)))
10951         {
10952         case 1:
10953           return DW_FORM_data1;
10954         case 2:
10955           return DW_FORM_data2;
10956         case 4:
10957           return DW_FORM_data4;
10958         case 8:
10959           return DW_FORM_data8;
10960         default:
10961           gcc_unreachable ();
10962         }
10963     case dw_val_class_const_double:
10964       switch (HOST_BITS_PER_WIDE_INT)
10965         {
10966         case 8:
10967           return DW_FORM_data2;
10968         case 16:
10969           return DW_FORM_data4;
10970         case 32:
10971           return DW_FORM_data8;
10972         case 64:
10973         default:
10974           return DW_FORM_block1;
10975         }
10976     case dw_val_class_vec:
10977       switch (constant_size (a->dw_attr_val.v.val_vec.length
10978                              * a->dw_attr_val.v.val_vec.elt_size))
10979         {
10980         case 1:
10981           return DW_FORM_block1;
10982         case 2:
10983           return DW_FORM_block2;
10984         case 4:
10985           return DW_FORM_block4;
10986         default:
10987           gcc_unreachable ();
10988         }
10989     case dw_val_class_flag:
10990       if (dwarf_version >= 4)
10991         {
10992           /* Currently all add_AT_flag calls pass in 1 as last argument,
10993              so DW_FORM_flag_present can be used.  If that ever changes,
10994              we'll need to use DW_FORM_flag and have some optimization
10995              in build_abbrev_table that will change those to
10996              DW_FORM_flag_present if it is set to 1 in all DIEs using
10997              the same abbrev entry.  */
10998           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10999           return DW_FORM_flag_present;
11000         }
11001       return DW_FORM_flag;
11002     case dw_val_class_die_ref:
11003       if (AT_ref_external (a))
11004         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
11005       else
11006         return DW_FORM_ref;
11007     case dw_val_class_fde_ref:
11008       return DW_FORM_data;
11009     case dw_val_class_lbl_id:
11010       return DW_FORM_addr;
11011     case dw_val_class_lineptr:
11012     case dw_val_class_macptr:
11013       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11014     case dw_val_class_str:
11015       return AT_string_form (a);
11016     case dw_val_class_file:
11017       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11018         {
11019         case 1:
11020           return DW_FORM_data1;
11021         case 2:
11022           return DW_FORM_data2;
11023         case 4:
11024           return DW_FORM_data4;
11025         default:
11026           gcc_unreachable ();
11027         }
11028
11029     case dw_val_class_data8:
11030       return DW_FORM_data8;
11031
11032     default:
11033       gcc_unreachable ();
11034     }
11035 }
11036
11037 /* Output the encoding of an attribute value.  */
11038
11039 static void
11040 output_value_format (dw_attr_ref a)
11041 {
11042   enum dwarf_form form = value_format (a);
11043
11044   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11045 }
11046
11047 /* Output the .debug_abbrev section which defines the DIE abbreviation
11048    table.  */
11049
11050 static void
11051 output_abbrev_section (void)
11052 {
11053   unsigned long abbrev_id;
11054
11055   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11056     {
11057       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11058       unsigned ix;
11059       dw_attr_ref a_attr;
11060
11061       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11062       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11063                                    dwarf_tag_name (abbrev->die_tag));
11064
11065       if (abbrev->die_child != NULL)
11066         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11067       else
11068         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11069
11070       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11071            ix++)
11072         {
11073           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11074                                        dwarf_attr_name (a_attr->dw_attr));
11075           output_value_format (a_attr);
11076         }
11077
11078       dw2_asm_output_data (1, 0, NULL);
11079       dw2_asm_output_data (1, 0, NULL);
11080     }
11081
11082   /* Terminate the table.  */
11083   dw2_asm_output_data (1, 0, NULL);
11084 }
11085
11086 /* Output a symbol we can use to refer to this DIE from another CU.  */
11087
11088 static inline void
11089 output_die_symbol (dw_die_ref die)
11090 {
11091   char *sym = die->die_id.die_symbol;
11092
11093   if (sym == 0)
11094     return;
11095
11096   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11097     /* We make these global, not weak; if the target doesn't support
11098        .linkonce, it doesn't support combining the sections, so debugging
11099        will break.  */
11100     targetm.asm_out.globalize_label (asm_out_file, sym);
11101
11102   ASM_OUTPUT_LABEL (asm_out_file, sym);
11103 }
11104
11105 /* Return a new location list, given the begin and end range, and the
11106    expression.  */
11107
11108 static inline dw_loc_list_ref
11109 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11110               const char *section)
11111 {
11112   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11113
11114   retlist->begin = begin;
11115   retlist->end = end;
11116   retlist->expr = expr;
11117   retlist->section = section;
11118
11119   return retlist;
11120 }
11121
11122 /* Generate a new internal symbol for this location list node, if it
11123    hasn't got one yet.  */
11124
11125 static inline void
11126 gen_llsym (dw_loc_list_ref list)
11127 {
11128   gcc_assert (!list->ll_symbol);
11129   list->ll_symbol = gen_internal_sym ("LLST");
11130 }
11131
11132 /* Output the location list given to us.  */
11133
11134 static void
11135 output_loc_list (dw_loc_list_ref list_head)
11136 {
11137   dw_loc_list_ref curr = list_head;
11138
11139   if (list_head->emitted)
11140     return;
11141   list_head->emitted = true;
11142
11143   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11144
11145   /* Walk the location list, and output each range + expression.  */
11146   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11147     {
11148       unsigned long size;
11149       /* Don't output an entry that starts and ends at the same address.  */
11150       if (strcmp (curr->begin, curr->end) == 0)
11151         continue;
11152       if (!have_multiple_function_sections)
11153         {
11154           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11155                                 "Location list begin address (%s)",
11156                                 list_head->ll_symbol);
11157           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11158                                 "Location list end address (%s)",
11159                                 list_head->ll_symbol);
11160         }
11161       else
11162         {
11163           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11164                                "Location list begin address (%s)",
11165                                list_head->ll_symbol);
11166           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11167                                "Location list end address (%s)",
11168                                list_head->ll_symbol);
11169         }
11170       size = size_of_locs (curr->expr);
11171
11172       /* Output the block length for this list of location operations.  */
11173       gcc_assert (size <= 0xffff);
11174       dw2_asm_output_data (2, size, "%s", "Location expression size");
11175
11176       output_loc_sequence (curr->expr, -1);
11177     }
11178
11179   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11180                        "Location list terminator begin (%s)",
11181                        list_head->ll_symbol);
11182   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11183                        "Location list terminator end (%s)",
11184                        list_head->ll_symbol);
11185 }
11186
11187 /* Output a type signature.  */
11188
11189 static inline void
11190 output_signature (const char *sig, const char *name)
11191 {
11192   int i;
11193
11194   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11195     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11196 }
11197
11198 /* Output the DIE and its attributes.  Called recursively to generate
11199    the definitions of each child DIE.  */
11200
11201 static void
11202 output_die (dw_die_ref die)
11203 {
11204   dw_attr_ref a;
11205   dw_die_ref c;
11206   unsigned long size;
11207   unsigned ix;
11208
11209   /* If someone in another CU might refer to us, set up a symbol for
11210      them to point to.  */
11211   if (dwarf_version < 4 && die->die_id.die_symbol)
11212     output_die_symbol (die);
11213
11214   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11215                                (unsigned long)die->die_offset,
11216                                dwarf_tag_name (die->die_tag));
11217
11218   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11219     {
11220       const char *name = dwarf_attr_name (a->dw_attr);
11221
11222       switch (AT_class (a))
11223         {
11224         case dw_val_class_addr:
11225           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11226           break;
11227
11228         case dw_val_class_offset:
11229           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11230                                "%s", name);
11231           break;
11232
11233         case dw_val_class_range_list:
11234           {
11235             char *p = strchr (ranges_section_label, '\0');
11236
11237             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11238                      a->dw_attr_val.v.val_offset);
11239             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11240                                    debug_ranges_section, "%s", name);
11241             *p = '\0';
11242           }
11243           break;
11244
11245         case dw_val_class_loc:
11246           size = size_of_locs (AT_loc (a));
11247
11248           /* Output the block length for this list of location operations.  */
11249           if (dwarf_version >= 4)
11250             dw2_asm_output_data_uleb128 (size, "%s", name);
11251           else
11252             dw2_asm_output_data (constant_size (size), size, "%s", name);
11253
11254           output_loc_sequence (AT_loc (a), -1);
11255           break;
11256
11257         case dw_val_class_const:
11258           /* ??? It would be slightly more efficient to use a scheme like is
11259              used for unsigned constants below, but gdb 4.x does not sign
11260              extend.  Gdb 5.x does sign extend.  */
11261           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11262           break;
11263
11264         case dw_val_class_unsigned_const:
11265           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11266                                AT_unsigned (a), "%s", name);
11267           break;
11268
11269         case dw_val_class_const_double:
11270           {
11271             unsigned HOST_WIDE_INT first, second;
11272
11273             if (HOST_BITS_PER_WIDE_INT >= 64)
11274               dw2_asm_output_data (1,
11275                                    2 * HOST_BITS_PER_WIDE_INT
11276                                    / HOST_BITS_PER_CHAR,
11277                                    NULL);
11278
11279             if (WORDS_BIG_ENDIAN)
11280               {
11281                 first = a->dw_attr_val.v.val_double.high;
11282                 second = a->dw_attr_val.v.val_double.low;
11283               }
11284             else
11285               {
11286                 first = a->dw_attr_val.v.val_double.low;
11287                 second = a->dw_attr_val.v.val_double.high;
11288               }
11289
11290             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11291                                  first, name);
11292             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11293                                  second, NULL);
11294           }
11295           break;
11296
11297         case dw_val_class_vec:
11298           {
11299             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11300             unsigned int len = a->dw_attr_val.v.val_vec.length;
11301             unsigned int i;
11302             unsigned char *p;
11303
11304             dw2_asm_output_data (constant_size (len * elt_size),
11305                                  len * elt_size, "%s", name);
11306             if (elt_size > sizeof (HOST_WIDE_INT))
11307               {
11308                 elt_size /= 2;
11309                 len *= 2;
11310               }
11311             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11312                  i < len;
11313                  i++, p += elt_size)
11314               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11315                                    "fp or vector constant word %u", i);
11316             break;
11317           }
11318
11319         case dw_val_class_flag:
11320           if (dwarf_version >= 4)
11321             {
11322               /* Currently all add_AT_flag calls pass in 1 as last argument,
11323                  so DW_FORM_flag_present can be used.  If that ever changes,
11324                  we'll need to use DW_FORM_flag and have some optimization
11325                  in build_abbrev_table that will change those to
11326                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11327                  the same abbrev entry.  */
11328               gcc_assert (AT_flag (a) == 1);
11329               if (flag_debug_asm)
11330                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11331                          ASM_COMMENT_START, name);
11332               break;
11333             }
11334           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11335           break;
11336
11337         case dw_val_class_loc_list:
11338           {
11339             char *sym = AT_loc_list (a)->ll_symbol;
11340
11341             gcc_assert (sym);
11342             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11343                                    "%s", name);
11344           }
11345           break;
11346
11347         case dw_val_class_die_ref:
11348           if (AT_ref_external (a))
11349             {
11350               if (dwarf_version >= 4)
11351                 {
11352                   comdat_type_node_ref type_node =
11353                     AT_ref (a)->die_id.die_type_node;
11354
11355                   gcc_assert (type_node);
11356                   output_signature (type_node->signature, name);
11357                 }
11358               else
11359                 {
11360                   char *sym = AT_ref (a)->die_id.die_symbol;
11361                   int size;
11362
11363                   gcc_assert (sym);
11364                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11365                      length, whereas in DWARF3 it's always sized as an
11366                      offset.  */
11367                   if (dwarf_version == 2)
11368                     size = DWARF2_ADDR_SIZE;
11369                   else
11370                     size = DWARF_OFFSET_SIZE;
11371                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11372                                          name);
11373                 }
11374             }
11375           else
11376             {
11377               gcc_assert (AT_ref (a)->die_offset);
11378               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11379                                    "%s", name);
11380             }
11381           break;
11382
11383         case dw_val_class_fde_ref:
11384           {
11385             char l1[20];
11386
11387             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11388                                          a->dw_attr_val.v.val_fde_index * 2);
11389             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11390                                    "%s", name);
11391           }
11392           break;
11393
11394         case dw_val_class_vms_delta:
11395           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11396                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11397                                     "%s", name);
11398           break;
11399
11400         case dw_val_class_lbl_id:
11401           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11402           break;
11403
11404         case dw_val_class_lineptr:
11405           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11406                                  debug_line_section, "%s", name);
11407           break;
11408
11409         case dw_val_class_macptr:
11410           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11411                                  debug_macinfo_section, "%s", name);
11412           break;
11413
11414         case dw_val_class_str:
11415           if (AT_string_form (a) == DW_FORM_strp)
11416             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11417                                    a->dw_attr_val.v.val_str->label,
11418                                    debug_str_section,
11419                                    "%s: \"%s\"", name, AT_string (a));
11420           else
11421             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11422           break;
11423
11424         case dw_val_class_file:
11425           {
11426             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11427
11428             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11429                                  a->dw_attr_val.v.val_file->filename);
11430             break;
11431           }
11432
11433         case dw_val_class_data8:
11434           {
11435             int i;
11436
11437             for (i = 0; i < 8; i++)
11438               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11439                                    i == 0 ? "%s" : NULL, name);
11440             break;
11441           }
11442
11443         default:
11444           gcc_unreachable ();
11445         }
11446     }
11447
11448   FOR_EACH_CHILD (die, c, output_die (c));
11449
11450   /* Add null byte to terminate sibling list.  */
11451   if (die->die_child != NULL)
11452     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11453                          (unsigned long) die->die_offset);
11454 }
11455
11456 /* Output the compilation unit that appears at the beginning of the
11457    .debug_info section, and precedes the DIE descriptions.  */
11458
11459 static void
11460 output_compilation_unit_header (void)
11461 {
11462   int ver = dwarf_version;
11463
11464   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11465     dw2_asm_output_data (4, 0xffffffff,
11466       "Initial length escape value indicating 64-bit DWARF extension");
11467   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11468                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11469                        "Length of Compilation Unit Info");
11470   dw2_asm_output_data (2, ver, "DWARF version number");
11471   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11472                          debug_abbrev_section,
11473                          "Offset Into Abbrev. Section");
11474   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11475 }
11476
11477 /* Output the compilation unit DIE and its children.  */
11478
11479 static void
11480 output_comp_unit (dw_die_ref die, int output_if_empty)
11481 {
11482   const char *secname;
11483   char *oldsym, *tmp;
11484
11485   /* Unless we are outputting main CU, we may throw away empty ones.  */
11486   if (!output_if_empty && die->die_child == NULL)
11487     return;
11488
11489   /* Even if there are no children of this DIE, we must output the information
11490      about the compilation unit.  Otherwise, on an empty translation unit, we
11491      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11492      will then complain when examining the file.  First mark all the DIEs in
11493      this CU so we know which get local refs.  */
11494   mark_dies (die);
11495
11496   build_abbrev_table (die);
11497
11498   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11499   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11500   calc_die_sizes (die);
11501
11502   oldsym = die->die_id.die_symbol;
11503   if (oldsym)
11504     {
11505       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11506
11507       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11508       secname = tmp;
11509       die->die_id.die_symbol = NULL;
11510       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11511     }
11512   else
11513     {
11514       switch_to_section (debug_info_section);
11515       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11516       info_section_emitted = true;
11517     }
11518
11519   /* Output debugging information.  */
11520   output_compilation_unit_header ();
11521   output_die (die);
11522
11523   /* Leave the marks on the main CU, so we can check them in
11524      output_pubnames.  */
11525   if (oldsym)
11526     {
11527       unmark_dies (die);
11528       die->die_id.die_symbol = oldsym;
11529     }
11530 }
11531
11532 /* Output a comdat type unit DIE and its children.  */
11533
11534 static void
11535 output_comdat_type_unit (comdat_type_node *node)
11536 {
11537   const char *secname;
11538   char *tmp;
11539   int i;
11540 #if defined (OBJECT_FORMAT_ELF)
11541   tree comdat_key;
11542 #endif
11543
11544   /* First mark all the DIEs in this CU so we know which get local refs.  */
11545   mark_dies (node->root_die);
11546
11547   build_abbrev_table (node->root_die);
11548
11549   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11550   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11551   calc_die_sizes (node->root_die);
11552
11553 #if defined (OBJECT_FORMAT_ELF)
11554   secname = ".debug_types";
11555   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11556   sprintf (tmp, "wt.");
11557   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11558     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11559   comdat_key = get_identifier (tmp);
11560   targetm.asm_out.named_section (secname,
11561                                  SECTION_DEBUG | SECTION_LINKONCE,
11562                                  comdat_key);
11563 #else
11564   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11565   sprintf (tmp, ".gnu.linkonce.wt.");
11566   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11567     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11568   secname = tmp;
11569   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11570 #endif
11571
11572   /* Output debugging information.  */
11573   output_compilation_unit_header ();
11574   output_signature (node->signature, "Type Signature");
11575   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11576                        "Offset to Type DIE");
11577   output_die (node->root_die);
11578
11579   unmark_dies (node->root_die);
11580 }
11581
11582 /* Return the DWARF2/3 pubname associated with a decl.  */
11583
11584 static const char *
11585 dwarf2_name (tree decl, int scope)
11586 {
11587   if (DECL_NAMELESS (decl))
11588     return NULL;
11589   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11590 }
11591
11592 /* Add a new entry to .debug_pubnames if appropriate.  */
11593
11594 static void
11595 add_pubname_string (const char *str, dw_die_ref die)
11596 {
11597   if (targetm.want_debug_pub_sections)
11598     {
11599       pubname_entry e;
11600
11601       e.die = die;
11602       e.name = xstrdup (str);
11603       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11604     }
11605 }
11606
11607 static void
11608 add_pubname (tree decl, dw_die_ref die)
11609 {
11610   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11611     {
11612       const char *name = dwarf2_name (decl, 1);
11613       if (name)
11614         add_pubname_string (name, die);
11615     }
11616 }
11617
11618 /* Add a new entry to .debug_pubtypes if appropriate.  */
11619
11620 static void
11621 add_pubtype (tree decl, dw_die_ref die)
11622 {
11623   pubname_entry e;
11624
11625   if (!targetm.want_debug_pub_sections)
11626     return;
11627
11628   e.name = NULL;
11629   if ((TREE_PUBLIC (decl)
11630        || is_cu_die (die->die_parent))
11631       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11632     {
11633       e.die = die;
11634       if (TYPE_P (decl))
11635         {
11636           if (TYPE_NAME (decl))
11637             {
11638               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11639                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11640               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11641                        && DECL_NAME (TYPE_NAME (decl)))
11642                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11643               else
11644                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11645             }
11646         }
11647       else
11648         {
11649           e.name = dwarf2_name (decl, 1);
11650           if (e.name)
11651             e.name = xstrdup (e.name);
11652         }
11653
11654       /* If we don't have a name for the type, there's no point in adding
11655          it to the table.  */
11656       if (e.name && e.name[0] != '\0')
11657         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11658     }
11659 }
11660
11661 /* Output the public names table used to speed up access to externally
11662    visible names; or the public types table used to find type definitions.  */
11663
11664 static void
11665 output_pubnames (VEC (pubname_entry, gc) * names)
11666 {
11667   unsigned i;
11668   unsigned long pubnames_length = size_of_pubnames (names);
11669   pubname_ref pub;
11670
11671   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11672     dw2_asm_output_data (4, 0xffffffff,
11673       "Initial length escape value indicating 64-bit DWARF extension");
11674   if (names == pubname_table)
11675     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11676                          "Length of Public Names Info");
11677   else
11678     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11679                          "Length of Public Type Names Info");
11680   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11681   dw2_asm_output_data (2, 2, "DWARF Version");
11682   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11683                          debug_info_section,
11684                          "Offset of Compilation Unit Info");
11685   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11686                        "Compilation Unit Length");
11687
11688   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11689     {
11690       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11691       if (names == pubname_table)
11692         gcc_assert (pub->die->die_mark);
11693
11694       if (names != pubtype_table
11695           || pub->die->die_offset != 0
11696           || !flag_eliminate_unused_debug_types)
11697         {
11698           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11699                                "DIE offset");
11700
11701           dw2_asm_output_nstring (pub->name, -1, "external name");
11702         }
11703     }
11704
11705   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11706 }
11707
11708 /* Add a new entry to .debug_aranges if appropriate.  */
11709
11710 static void
11711 add_arange (tree decl, dw_die_ref die)
11712 {
11713   if (! DECL_SECTION_NAME (decl))
11714     return;
11715
11716   if (arange_table_in_use == arange_table_allocated)
11717     {
11718       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11719       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11720                                     arange_table_allocated);
11721       memset (arange_table + arange_table_in_use, 0,
11722               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11723     }
11724
11725   arange_table[arange_table_in_use++] = die;
11726 }
11727
11728 /* Output the information that goes into the .debug_aranges table.
11729    Namely, define the beginning and ending address range of the
11730    text section generated for this compilation unit.  */
11731
11732 static void
11733 output_aranges (void)
11734 {
11735   unsigned i;
11736   unsigned long aranges_length = size_of_aranges ();
11737
11738   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11739     dw2_asm_output_data (4, 0xffffffff,
11740       "Initial length escape value indicating 64-bit DWARF extension");
11741   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11742                        "Length of Address Ranges Info");
11743   /* Version number for aranges is still 2, even in DWARF3.  */
11744   dw2_asm_output_data (2, 2, "DWARF Version");
11745   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11746                          debug_info_section,
11747                          "Offset of Compilation Unit Info");
11748   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11749   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11750
11751   /* We need to align to twice the pointer size here.  */
11752   if (DWARF_ARANGES_PAD_SIZE)
11753     {
11754       /* Pad using a 2 byte words so that padding is correct for any
11755          pointer size.  */
11756       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11757                            2 * DWARF2_ADDR_SIZE);
11758       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11759         dw2_asm_output_data (2, 0, NULL);
11760     }
11761
11762   /* It is necessary not to output these entries if the sections were
11763      not used; if the sections were not used, the length will be 0 and
11764      the address may end up as 0 if the section is discarded by ld
11765      --gc-sections, leaving an invalid (0, 0) entry that can be
11766      confused with the terminator.  */
11767   if (text_section_used)
11768     {
11769       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11770       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11771                             text_section_label, "Length");
11772     }
11773   if (cold_text_section_used)
11774     {
11775       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11776                            "Address");
11777       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11778                             cold_text_section_label, "Length");
11779     }
11780
11781   for (i = 0; i < arange_table_in_use; i++)
11782     {
11783       dw_die_ref die = arange_table[i];
11784
11785       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11786       gcc_assert (die->die_mark);
11787
11788       if (die->die_tag == DW_TAG_subprogram)
11789         {
11790           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11791                                "Address");
11792           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11793                                 get_AT_low_pc (die), "Length");
11794         }
11795       else
11796         {
11797           /* A static variable; extract the symbol from DW_AT_location.
11798              Note that this code isn't currently hit, as we only emit
11799              aranges for functions (jason 9/23/99).  */
11800           dw_attr_ref a = get_AT (die, DW_AT_location);
11801           dw_loc_descr_ref loc;
11802
11803           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11804
11805           loc = AT_loc (a);
11806           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11807
11808           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11809                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11810           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11811                                get_AT_unsigned (die, DW_AT_byte_size),
11812                                "Length");
11813         }
11814     }
11815
11816   /* Output the terminator words.  */
11817   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11818   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11819 }
11820
11821 /* Add a new entry to .debug_ranges.  Return the offset at which it
11822    was placed.  */
11823
11824 static unsigned int
11825 add_ranges_num (int num)
11826 {
11827   unsigned int in_use = ranges_table_in_use;
11828
11829   if (in_use == ranges_table_allocated)
11830     {
11831       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11832       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11833                                     ranges_table_allocated);
11834       memset (ranges_table + ranges_table_in_use, 0,
11835               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11836     }
11837
11838   ranges_table[in_use].num = num;
11839   ranges_table_in_use = in_use + 1;
11840
11841   return in_use * 2 * DWARF2_ADDR_SIZE;
11842 }
11843
11844 /* Add a new entry to .debug_ranges corresponding to a block, or a
11845    range terminator if BLOCK is NULL.  */
11846
11847 static unsigned int
11848 add_ranges (const_tree block)
11849 {
11850   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11851 }
11852
11853 /* Add a new entry to .debug_ranges corresponding to a pair of
11854    labels.  */
11855
11856 static void
11857 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11858                       bool *added)
11859 {
11860   unsigned int in_use = ranges_by_label_in_use;
11861   unsigned int offset;
11862
11863   if (in_use == ranges_by_label_allocated)
11864     {
11865       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11866       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11867                                        ranges_by_label,
11868                                        ranges_by_label_allocated);
11869       memset (ranges_by_label + ranges_by_label_in_use, 0,
11870               RANGES_TABLE_INCREMENT
11871               * sizeof (struct dw_ranges_by_label_struct));
11872     }
11873
11874   ranges_by_label[in_use].begin = begin;
11875   ranges_by_label[in_use].end = end;
11876   ranges_by_label_in_use = in_use + 1;
11877
11878   offset = add_ranges_num (-(int)in_use - 1);
11879   if (!*added)
11880     {
11881       add_AT_range_list (die, DW_AT_ranges, offset);
11882       *added = true;
11883     }
11884 }
11885
11886 static void
11887 output_ranges (void)
11888 {
11889   unsigned i;
11890   static const char *const start_fmt = "Offset %#x";
11891   const char *fmt = start_fmt;
11892
11893   for (i = 0; i < ranges_table_in_use; i++)
11894     {
11895       int block_num = ranges_table[i].num;
11896
11897       if (block_num > 0)
11898         {
11899           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11900           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11901
11902           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11903           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11904
11905           /* If all code is in the text section, then the compilation
11906              unit base address defaults to DW_AT_low_pc, which is the
11907              base of the text section.  */
11908           if (!have_multiple_function_sections)
11909             {
11910               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11911                                     text_section_label,
11912                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11913               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11914                                     text_section_label, NULL);
11915             }
11916
11917           /* Otherwise, the compilation unit base address is zero,
11918              which allows us to use absolute addresses, and not worry
11919              about whether the target supports cross-section
11920              arithmetic.  */
11921           else
11922             {
11923               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11924                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11925               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11926             }
11927
11928           fmt = NULL;
11929         }
11930
11931       /* Negative block_num stands for an index into ranges_by_label.  */
11932       else if (block_num < 0)
11933         {
11934           int lab_idx = - block_num - 1;
11935
11936           if (!have_multiple_function_sections)
11937             {
11938               gcc_unreachable ();
11939 #if 0
11940               /* If we ever use add_ranges_by_labels () for a single
11941                  function section, all we have to do is to take out
11942                  the #if 0 above.  */
11943               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11944                                     ranges_by_label[lab_idx].begin,
11945                                     text_section_label,
11946                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11947               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11948                                     ranges_by_label[lab_idx].end,
11949                                     text_section_label, NULL);
11950 #endif
11951             }
11952           else
11953             {
11954               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11955                                    ranges_by_label[lab_idx].begin,
11956                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11957               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11958                                    ranges_by_label[lab_idx].end,
11959                                    NULL);
11960             }
11961         }
11962       else
11963         {
11964           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11965           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11966           fmt = start_fmt;
11967         }
11968     }
11969 }
11970
11971 /* Data structure containing information about input files.  */
11972 struct file_info
11973 {
11974   const char *path;     /* Complete file name.  */
11975   const char *fname;    /* File name part.  */
11976   int length;           /* Length of entire string.  */
11977   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11978   int dir_idx;          /* Index in directory table.  */
11979 };
11980
11981 /* Data structure containing information about directories with source
11982    files.  */
11983 struct dir_info
11984 {
11985   const char *path;     /* Path including directory name.  */
11986   int length;           /* Path length.  */
11987   int prefix;           /* Index of directory entry which is a prefix.  */
11988   int count;            /* Number of files in this directory.  */
11989   int dir_idx;          /* Index of directory used as base.  */
11990 };
11991
11992 /* Callback function for file_info comparison.  We sort by looking at
11993    the directories in the path.  */
11994
11995 static int
11996 file_info_cmp (const void *p1, const void *p2)
11997 {
11998   const struct file_info *const s1 = (const struct file_info *) p1;
11999   const struct file_info *const s2 = (const struct file_info *) p2;
12000   const unsigned char *cp1;
12001   const unsigned char *cp2;
12002
12003   /* Take care of file names without directories.  We need to make sure that
12004      we return consistent values to qsort since some will get confused if
12005      we return the same value when identical operands are passed in opposite
12006      orders.  So if neither has a directory, return 0 and otherwise return
12007      1 or -1 depending on which one has the directory.  */
12008   if ((s1->path == s1->fname || s2->path == s2->fname))
12009     return (s2->path == s2->fname) - (s1->path == s1->fname);
12010
12011   cp1 = (const unsigned char *) s1->path;
12012   cp2 = (const unsigned char *) s2->path;
12013
12014   while (1)
12015     {
12016       ++cp1;
12017       ++cp2;
12018       /* Reached the end of the first path?  If so, handle like above.  */
12019       if ((cp1 == (const unsigned char *) s1->fname)
12020           || (cp2 == (const unsigned char *) s2->fname))
12021         return ((cp2 == (const unsigned char *) s2->fname)
12022                 - (cp1 == (const unsigned char *) s1->fname));
12023
12024       /* Character of current path component the same?  */
12025       else if (*cp1 != *cp2)
12026         return *cp1 - *cp2;
12027     }
12028 }
12029
12030 struct file_name_acquire_data
12031 {
12032   struct file_info *files;
12033   int used_files;
12034   int max_files;
12035 };
12036
12037 /* Traversal function for the hash table.  */
12038
12039 static int
12040 file_name_acquire (void ** slot, void *data)
12041 {
12042   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12043   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12044   struct file_info *fi;
12045   const char *f;
12046
12047   gcc_assert (fnad->max_files >= d->emitted_number);
12048
12049   if (! d->emitted_number)
12050     return 1;
12051
12052   gcc_assert (fnad->max_files != fnad->used_files);
12053
12054   fi = fnad->files + fnad->used_files++;
12055
12056   /* Skip all leading "./".  */
12057   f = d->filename;
12058   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12059     f += 2;
12060
12061   /* Create a new array entry.  */
12062   fi->path = f;
12063   fi->length = strlen (f);
12064   fi->file_idx = d;
12065
12066   /* Search for the file name part.  */
12067   f = strrchr (f, DIR_SEPARATOR);
12068 #if defined (DIR_SEPARATOR_2)
12069   {
12070     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12071
12072     if (g != NULL)
12073       {
12074         if (f == NULL || f < g)
12075           f = g;
12076       }
12077   }
12078 #endif
12079
12080   fi->fname = f == NULL ? fi->path : f + 1;
12081   return 1;
12082 }
12083
12084 /* Output the directory table and the file name table.  We try to minimize
12085    the total amount of memory needed.  A heuristic is used to avoid large
12086    slowdowns with many input files.  */
12087
12088 static void
12089 output_file_names (void)
12090 {
12091   struct file_name_acquire_data fnad;
12092   int numfiles;
12093   struct file_info *files;
12094   struct dir_info *dirs;
12095   int *saved;
12096   int *savehere;
12097   int *backmap;
12098   int ndirs;
12099   int idx_offset;
12100   int i;
12101
12102   if (!last_emitted_file)
12103     {
12104       dw2_asm_output_data (1, 0, "End directory table");
12105       dw2_asm_output_data (1, 0, "End file name table");
12106       return;
12107     }
12108
12109   numfiles = last_emitted_file->emitted_number;
12110
12111   /* Allocate the various arrays we need.  */
12112   files = XALLOCAVEC (struct file_info, numfiles);
12113   dirs = XALLOCAVEC (struct dir_info, numfiles);
12114
12115   fnad.files = files;
12116   fnad.used_files = 0;
12117   fnad.max_files = numfiles;
12118   htab_traverse (file_table, file_name_acquire, &fnad);
12119   gcc_assert (fnad.used_files == fnad.max_files);
12120
12121   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12122
12123   /* Find all the different directories used.  */
12124   dirs[0].path = files[0].path;
12125   dirs[0].length = files[0].fname - files[0].path;
12126   dirs[0].prefix = -1;
12127   dirs[0].count = 1;
12128   dirs[0].dir_idx = 0;
12129   files[0].dir_idx = 0;
12130   ndirs = 1;
12131
12132   for (i = 1; i < numfiles; i++)
12133     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12134         && memcmp (dirs[ndirs - 1].path, files[i].path,
12135                    dirs[ndirs - 1].length) == 0)
12136       {
12137         /* Same directory as last entry.  */
12138         files[i].dir_idx = ndirs - 1;
12139         ++dirs[ndirs - 1].count;
12140       }
12141     else
12142       {
12143         int j;
12144
12145         /* This is a new directory.  */
12146         dirs[ndirs].path = files[i].path;
12147         dirs[ndirs].length = files[i].fname - files[i].path;
12148         dirs[ndirs].count = 1;
12149         dirs[ndirs].dir_idx = ndirs;
12150         files[i].dir_idx = ndirs;
12151
12152         /* Search for a prefix.  */
12153         dirs[ndirs].prefix = -1;
12154         for (j = 0; j < ndirs; j++)
12155           if (dirs[j].length < dirs[ndirs].length
12156               && dirs[j].length > 1
12157               && (dirs[ndirs].prefix == -1
12158                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12159               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12160             dirs[ndirs].prefix = j;
12161
12162         ++ndirs;
12163       }
12164
12165   /* Now to the actual work.  We have to find a subset of the directories which
12166      allow expressing the file name using references to the directory table
12167      with the least amount of characters.  We do not do an exhaustive search
12168      where we would have to check out every combination of every single
12169      possible prefix.  Instead we use a heuristic which provides nearly optimal
12170      results in most cases and never is much off.  */
12171   saved = XALLOCAVEC (int, ndirs);
12172   savehere = XALLOCAVEC (int, ndirs);
12173
12174   memset (saved, '\0', ndirs * sizeof (saved[0]));
12175   for (i = 0; i < ndirs; i++)
12176     {
12177       int j;
12178       int total;
12179
12180       /* We can always save some space for the current directory.  But this
12181          does not mean it will be enough to justify adding the directory.  */
12182       savehere[i] = dirs[i].length;
12183       total = (savehere[i] - saved[i]) * dirs[i].count;
12184
12185       for (j = i + 1; j < ndirs; j++)
12186         {
12187           savehere[j] = 0;
12188           if (saved[j] < dirs[i].length)
12189             {
12190               /* Determine whether the dirs[i] path is a prefix of the
12191                  dirs[j] path.  */
12192               int k;
12193
12194               k = dirs[j].prefix;
12195               while (k != -1 && k != (int) i)
12196                 k = dirs[k].prefix;
12197
12198               if (k == (int) i)
12199                 {
12200                   /* Yes it is.  We can possibly save some memory by
12201                      writing the filenames in dirs[j] relative to
12202                      dirs[i].  */
12203                   savehere[j] = dirs[i].length;
12204                   total += (savehere[j] - saved[j]) * dirs[j].count;
12205                 }
12206             }
12207         }
12208
12209       /* Check whether we can save enough to justify adding the dirs[i]
12210          directory.  */
12211       if (total > dirs[i].length + 1)
12212         {
12213           /* It's worthwhile adding.  */
12214           for (j = i; j < ndirs; j++)
12215             if (savehere[j] > 0)
12216               {
12217                 /* Remember how much we saved for this directory so far.  */
12218                 saved[j] = savehere[j];
12219
12220                 /* Remember the prefix directory.  */
12221                 dirs[j].dir_idx = i;
12222               }
12223         }
12224     }
12225
12226   /* Emit the directory name table.  */
12227   idx_offset = dirs[0].length > 0 ? 1 : 0;
12228   for (i = 1 - idx_offset; i < ndirs; i++)
12229     dw2_asm_output_nstring (dirs[i].path,
12230                             dirs[i].length
12231                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12232                             "Directory Entry: %#x", i + idx_offset);
12233
12234   dw2_asm_output_data (1, 0, "End directory table");
12235
12236   /* We have to emit them in the order of emitted_number since that's
12237      used in the debug info generation.  To do this efficiently we
12238      generate a back-mapping of the indices first.  */
12239   backmap = XALLOCAVEC (int, numfiles);
12240   for (i = 0; i < numfiles; i++)
12241     backmap[files[i].file_idx->emitted_number - 1] = i;
12242
12243   /* Now write all the file names.  */
12244   for (i = 0; i < numfiles; i++)
12245     {
12246       int file_idx = backmap[i];
12247       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12248
12249 #ifdef VMS_DEBUGGING_INFO
12250 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12251
12252       /* Setting these fields can lead to debugger miscomparisons,
12253          but VMS Debug requires them to be set correctly.  */
12254
12255       int ver;
12256       long long cdt;
12257       long siz;
12258       int maxfilelen = strlen (files[file_idx].path)
12259                                + dirs[dir_idx].length
12260                                + MAX_VMS_VERSION_LEN + 1;
12261       char *filebuf = XALLOCAVEC (char, maxfilelen);
12262
12263       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12264       snprintf (filebuf, maxfilelen, "%s;%d",
12265                 files[file_idx].path + dirs[dir_idx].length, ver);
12266
12267       dw2_asm_output_nstring
12268         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12269
12270       /* Include directory index.  */
12271       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12272
12273       /* Modification time.  */
12274       dw2_asm_output_data_uleb128
12275         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12276           ? cdt : 0,
12277          NULL);
12278
12279       /* File length in bytes.  */
12280       dw2_asm_output_data_uleb128
12281         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12282           ? siz : 0,
12283          NULL);
12284 #else
12285       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12286                               "File Entry: %#x", (unsigned) i + 1);
12287
12288       /* Include directory index.  */
12289       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12290
12291       /* Modification time.  */
12292       dw2_asm_output_data_uleb128 (0, NULL);
12293
12294       /* File length in bytes.  */
12295       dw2_asm_output_data_uleb128 (0, NULL);
12296 #endif /* VMS_DEBUGGING_INFO */
12297     }
12298
12299   dw2_asm_output_data (1, 0, "End file name table");
12300 }
12301
12302
12303 /* Output the source line number correspondence information.  This
12304    information goes into the .debug_line section.  */
12305
12306 static void
12307 output_line_info (void)
12308 {
12309   char l1[20], l2[20], p1[20], p2[20];
12310   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12311   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12312   unsigned opc;
12313   unsigned n_op_args;
12314   unsigned long lt_index;
12315   unsigned long current_line;
12316   long line_offset;
12317   long line_delta;
12318   unsigned long current_file;
12319   unsigned long function;
12320   int ver = dwarf_version;
12321
12322   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12323   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12324   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12325   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12326
12327   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12328     dw2_asm_output_data (4, 0xffffffff,
12329       "Initial length escape value indicating 64-bit DWARF extension");
12330   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12331                         "Length of Source Line Info");
12332   ASM_OUTPUT_LABEL (asm_out_file, l1);
12333
12334   dw2_asm_output_data (2, ver, "DWARF Version");
12335   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12336   ASM_OUTPUT_LABEL (asm_out_file, p1);
12337
12338   /* Define the architecture-dependent minimum instruction length (in
12339    bytes).  In this implementation of DWARF, this field is used for
12340    information purposes only.  Since GCC generates assembly language,
12341    we have no a priori knowledge of how many instruction bytes are
12342    generated for each source line, and therefore can use only the
12343    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12344    commands.  Accordingly, we fix this as `1', which is "correct
12345    enough" for all architectures, and don't let the target override.  */
12346   dw2_asm_output_data (1, 1,
12347                        "Minimum Instruction Length");
12348
12349   if (ver >= 4)
12350     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12351                          "Maximum Operations Per Instruction");
12352   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12353                        "Default is_stmt_start flag");
12354   dw2_asm_output_data (1, DWARF_LINE_BASE,
12355                        "Line Base Value (Special Opcodes)");
12356   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12357                        "Line Range Value (Special Opcodes)");
12358   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12359                        "Special Opcode Base");
12360
12361   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12362     {
12363       switch (opc)
12364         {
12365         case DW_LNS_advance_pc:
12366         case DW_LNS_advance_line:
12367         case DW_LNS_set_file:
12368         case DW_LNS_set_column:
12369         case DW_LNS_fixed_advance_pc:
12370           n_op_args = 1;
12371           break;
12372         default:
12373           n_op_args = 0;
12374           break;
12375         }
12376
12377       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12378                            opc, n_op_args);
12379     }
12380
12381   /* Write out the information about the files we use.  */
12382   output_file_names ();
12383   ASM_OUTPUT_LABEL (asm_out_file, p2);
12384
12385   /* We used to set the address register to the first location in the text
12386      section here, but that didn't accomplish anything since we already
12387      have a line note for the opening brace of the first function.  */
12388
12389   /* Generate the line number to PC correspondence table, encoded as
12390      a series of state machine operations.  */
12391   current_file = 1;
12392   current_line = 1;
12393
12394   if (cfun && in_cold_section_p)
12395     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12396   else
12397     strcpy (prev_line_label, text_section_label);
12398   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12399     {
12400       dw_line_info_ref line_info = &line_info_table[lt_index];
12401
12402 #if 0
12403       /* Disable this optimization for now; GDB wants to see two line notes
12404          at the beginning of a function so it can find the end of the
12405          prologue.  */
12406
12407       /* Don't emit anything for redundant notes.  Just updating the
12408          address doesn't accomplish anything, because we already assume
12409          that anything after the last address is this line.  */
12410       if (line_info->dw_line_num == current_line
12411           && line_info->dw_file_num == current_file)
12412         continue;
12413 #endif
12414
12415       /* Emit debug info for the address of the current line.
12416
12417          Unfortunately, we have little choice here currently, and must always
12418          use the most general form.  GCC does not know the address delta
12419          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12420          attributes which will give an upper bound on the address range.  We
12421          could perhaps use length attributes to determine when it is safe to
12422          use DW_LNS_fixed_advance_pc.  */
12423
12424       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12425       if (0)
12426         {
12427           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12428           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12429                                "DW_LNS_fixed_advance_pc");
12430           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12431         }
12432       else
12433         {
12434           /* This can handle any delta.  This takes
12435              4+DWARF2_ADDR_SIZE bytes.  */
12436           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12437           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12438           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12439           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12440         }
12441
12442       strcpy (prev_line_label, line_label);
12443
12444       /* Emit debug info for the source file of the current line, if
12445          different from the previous line.  */
12446       if (line_info->dw_file_num != current_file)
12447         {
12448           current_file = line_info->dw_file_num;
12449           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12450           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12451         }
12452
12453       /* Emit debug info for the current line number, choosing the encoding
12454          that uses the least amount of space.  */
12455       if (line_info->dw_line_num != current_line)
12456         {
12457           line_offset = line_info->dw_line_num - current_line;
12458           line_delta = line_offset - DWARF_LINE_BASE;
12459           current_line = line_info->dw_line_num;
12460           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12461             /* This can handle deltas from -10 to 234, using the current
12462                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12463                takes 1 byte.  */
12464             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12465                                  "line %lu", current_line);
12466           else
12467             {
12468               /* This can handle any delta.  This takes at least 4 bytes,
12469                  depending on the value being encoded.  */
12470               dw2_asm_output_data (1, DW_LNS_advance_line,
12471                                    "advance to line %lu", current_line);
12472               dw2_asm_output_data_sleb128 (line_offset, NULL);
12473               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12474             }
12475         }
12476       else
12477         /* We still need to start a new row, so output a copy insn.  */
12478         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12479     }
12480
12481   /* Emit debug info for the address of the end of the function.  */
12482   if (0)
12483     {
12484       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12485                            "DW_LNS_fixed_advance_pc");
12486       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12487     }
12488   else
12489     {
12490       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12491       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12492       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12493       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12494     }
12495
12496   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12497   dw2_asm_output_data_uleb128 (1, NULL);
12498   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12499
12500   function = 0;
12501   current_file = 1;
12502   current_line = 1;
12503   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12504     {
12505       dw_separate_line_info_ref line_info
12506         = &separate_line_info_table[lt_index];
12507
12508 #if 0
12509       /* Don't emit anything for redundant notes.  */
12510       if (line_info->dw_line_num == current_line
12511           && line_info->dw_file_num == current_file
12512           && line_info->function == function)
12513         goto cont;
12514 #endif
12515
12516       /* Emit debug info for the address of the current line.  If this is
12517          a new function, or the first line of a function, then we need
12518          to handle it differently.  */
12519       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12520                                    lt_index);
12521       if (function != line_info->function)
12522         {
12523           function = line_info->function;
12524
12525           /* Set the address register to the first line in the function.  */
12526           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12527           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12528           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12529           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12530         }
12531       else
12532         {
12533           /* ??? See the DW_LNS_advance_pc comment above.  */
12534           if (0)
12535             {
12536               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12537                                    "DW_LNS_fixed_advance_pc");
12538               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12539             }
12540           else
12541             {
12542               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12543               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12544               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12545               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12546             }
12547         }
12548
12549       strcpy (prev_line_label, line_label);
12550
12551       /* Emit debug info for the source file of the current line, if
12552          different from the previous line.  */
12553       if (line_info->dw_file_num != current_file)
12554         {
12555           current_file = line_info->dw_file_num;
12556           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12557           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12558         }
12559
12560       /* Emit debug info for the current line number, choosing the encoding
12561          that uses the least amount of space.  */
12562       if (line_info->dw_line_num != current_line)
12563         {
12564           line_offset = line_info->dw_line_num - current_line;
12565           line_delta = line_offset - DWARF_LINE_BASE;
12566           current_line = line_info->dw_line_num;
12567           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12568             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12569                                  "line %lu", current_line);
12570           else
12571             {
12572               dw2_asm_output_data (1, DW_LNS_advance_line,
12573                                    "advance to line %lu", current_line);
12574               dw2_asm_output_data_sleb128 (line_offset, NULL);
12575               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12576             }
12577         }
12578       else
12579         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12580
12581 #if 0
12582     cont:
12583 #endif
12584
12585       lt_index++;
12586
12587       /* If we're done with a function, end its sequence.  */
12588       if (lt_index == separate_line_info_table_in_use
12589           || separate_line_info_table[lt_index].function != function)
12590         {
12591           current_file = 1;
12592           current_line = 1;
12593
12594           /* Emit debug info for the address of the end of the function.  */
12595           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12596           if (0)
12597             {
12598               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12599                                    "DW_LNS_fixed_advance_pc");
12600               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12601             }
12602           else
12603             {
12604               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12605               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12606               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12607               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12608             }
12609
12610           /* Output the marker for the end of this sequence.  */
12611           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12612           dw2_asm_output_data_uleb128 (1, NULL);
12613           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12614         }
12615     }
12616
12617   /* Output the marker for the end of the line number info.  */
12618   ASM_OUTPUT_LABEL (asm_out_file, l2);
12619 }
12620 \f
12621 /* Given a pointer to a tree node for some base type, return a pointer to
12622    a DIE that describes the given type.
12623
12624    This routine must only be called for GCC type nodes that correspond to
12625    Dwarf base (fundamental) types.  */
12626
12627 static dw_die_ref
12628 base_type_die (tree type)
12629 {
12630   dw_die_ref base_type_result;
12631   enum dwarf_type encoding;
12632
12633   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12634     return 0;
12635
12636   /* If this is a subtype that should not be emitted as a subrange type,
12637      use the base type.  See subrange_type_for_debug_p.  */
12638   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12639     type = TREE_TYPE (type);
12640
12641   switch (TREE_CODE (type))
12642     {
12643     case INTEGER_TYPE:
12644       if ((dwarf_version >= 4 || !dwarf_strict)
12645           && TYPE_NAME (type)
12646           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12647           && DECL_IS_BUILTIN (TYPE_NAME (type))
12648           && DECL_NAME (TYPE_NAME (type)))
12649         {
12650           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12651           if (strcmp (name, "char16_t") == 0
12652               || strcmp (name, "char32_t") == 0)
12653             {
12654               encoding = DW_ATE_UTF;
12655               break;
12656             }
12657         }
12658       if (TYPE_STRING_FLAG (type))
12659         {
12660           if (TYPE_UNSIGNED (type))
12661             encoding = DW_ATE_unsigned_char;
12662           else
12663             encoding = DW_ATE_signed_char;
12664         }
12665       else if (TYPE_UNSIGNED (type))
12666         encoding = DW_ATE_unsigned;
12667       else
12668         encoding = DW_ATE_signed;
12669       break;
12670
12671     case REAL_TYPE:
12672       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12673         {
12674           if (dwarf_version >= 3 || !dwarf_strict)
12675             encoding = DW_ATE_decimal_float;
12676           else
12677             encoding = DW_ATE_lo_user;
12678         }
12679       else
12680         encoding = DW_ATE_float;
12681       break;
12682
12683     case FIXED_POINT_TYPE:
12684       if (!(dwarf_version >= 3 || !dwarf_strict))
12685         encoding = DW_ATE_lo_user;
12686       else if (TYPE_UNSIGNED (type))
12687         encoding = DW_ATE_unsigned_fixed;
12688       else
12689         encoding = DW_ATE_signed_fixed;
12690       break;
12691
12692       /* Dwarf2 doesn't know anything about complex ints, so use
12693          a user defined type for it.  */
12694     case COMPLEX_TYPE:
12695       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12696         encoding = DW_ATE_complex_float;
12697       else
12698         encoding = DW_ATE_lo_user;
12699       break;
12700
12701     case BOOLEAN_TYPE:
12702       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12703       encoding = DW_ATE_boolean;
12704       break;
12705
12706     default:
12707       /* No other TREE_CODEs are Dwarf fundamental types.  */
12708       gcc_unreachable ();
12709     }
12710
12711   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12712
12713   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12714                    int_size_in_bytes (type));
12715   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12716
12717   return base_type_result;
12718 }
12719
12720 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12721    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12722
12723 static inline int
12724 is_base_type (tree type)
12725 {
12726   switch (TREE_CODE (type))
12727     {
12728     case ERROR_MARK:
12729     case VOID_TYPE:
12730     case INTEGER_TYPE:
12731     case REAL_TYPE:
12732     case FIXED_POINT_TYPE:
12733     case COMPLEX_TYPE:
12734     case BOOLEAN_TYPE:
12735       return 1;
12736
12737     case ARRAY_TYPE:
12738     case RECORD_TYPE:
12739     case UNION_TYPE:
12740     case QUAL_UNION_TYPE:
12741     case ENUMERAL_TYPE:
12742     case FUNCTION_TYPE:
12743     case METHOD_TYPE:
12744     case POINTER_TYPE:
12745     case REFERENCE_TYPE:
12746     case NULLPTR_TYPE:
12747     case OFFSET_TYPE:
12748     case LANG_TYPE:
12749     case VECTOR_TYPE:
12750       return 0;
12751
12752     default:
12753       gcc_unreachable ();
12754     }
12755
12756   return 0;
12757 }
12758
12759 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12760    node, return the size in bits for the type if it is a constant, or else
12761    return the alignment for the type if the type's size is not constant, or
12762    else return BITS_PER_WORD if the type actually turns out to be an
12763    ERROR_MARK node.  */
12764
12765 static inline unsigned HOST_WIDE_INT
12766 simple_type_size_in_bits (const_tree type)
12767 {
12768   if (TREE_CODE (type) == ERROR_MARK)
12769     return BITS_PER_WORD;
12770   else if (TYPE_SIZE (type) == NULL_TREE)
12771     return 0;
12772   else if (host_integerp (TYPE_SIZE (type), 1))
12773     return tree_low_cst (TYPE_SIZE (type), 1);
12774   else
12775     return TYPE_ALIGN (type);
12776 }
12777
12778 /* Similarly, but return a double_int instead of UHWI.  */
12779
12780 static inline double_int
12781 double_int_type_size_in_bits (const_tree type)
12782 {
12783   if (TREE_CODE (type) == ERROR_MARK)
12784     return uhwi_to_double_int (BITS_PER_WORD);
12785   else if (TYPE_SIZE (type) == NULL_TREE)
12786     return double_int_zero;
12787   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12788     return tree_to_double_int (TYPE_SIZE (type));
12789   else
12790     return uhwi_to_double_int (TYPE_ALIGN (type));
12791 }
12792
12793 /*  Given a pointer to a tree node for a subrange type, return a pointer
12794     to a DIE that describes the given type.  */
12795
12796 static dw_die_ref
12797 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12798 {
12799   dw_die_ref subrange_die;
12800   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12801
12802   if (context_die == NULL)
12803     context_die = comp_unit_die ();
12804
12805   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12806
12807   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12808     {
12809       /* The size of the subrange type and its base type do not match,
12810          so we need to generate a size attribute for the subrange type.  */
12811       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12812     }
12813
12814   if (low)
12815     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12816   if (high)
12817     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12818
12819   return subrange_die;
12820 }
12821
12822 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12823    entry that chains various modifiers in front of the given type.  */
12824
12825 static dw_die_ref
12826 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12827                    dw_die_ref context_die)
12828 {
12829   enum tree_code code = TREE_CODE (type);
12830   dw_die_ref mod_type_die;
12831   dw_die_ref sub_die = NULL;
12832   tree item_type = NULL;
12833   tree qualified_type;
12834   tree name, low, high;
12835
12836   if (code == ERROR_MARK)
12837     return NULL;
12838
12839   /* See if we already have the appropriately qualified variant of
12840      this type.  */
12841   qualified_type
12842     = get_qualified_type (type,
12843                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12844                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12845
12846   if (qualified_type == sizetype
12847       && TYPE_NAME (qualified_type)
12848       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12849     {
12850       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12851
12852       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12853                            && TYPE_PRECISION (t)
12854                            == TYPE_PRECISION (qualified_type)
12855                            && TYPE_UNSIGNED (t)
12856                            == TYPE_UNSIGNED (qualified_type));
12857       qualified_type = t;
12858     }
12859
12860   /* If we do, then we can just use its DIE, if it exists.  */
12861   if (qualified_type)
12862     {
12863       mod_type_die = lookup_type_die (qualified_type);
12864       if (mod_type_die)
12865         return mod_type_die;
12866     }
12867
12868   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12869
12870   /* Handle C typedef types.  */
12871   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12872       && !DECL_ARTIFICIAL (name))
12873     {
12874       tree dtype = TREE_TYPE (name);
12875
12876       if (qualified_type == dtype)
12877         {
12878           /* For a named type, use the typedef.  */
12879           gen_type_die (qualified_type, context_die);
12880           return lookup_type_die (qualified_type);
12881         }
12882       else if (is_const_type < TYPE_READONLY (dtype)
12883                || is_volatile_type < TYPE_VOLATILE (dtype)
12884                || (is_const_type <= TYPE_READONLY (dtype)
12885                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12886                    && DECL_ORIGINAL_TYPE (name) != type))
12887         /* cv-unqualified version of named type.  Just use the unnamed
12888            type to which it refers.  */
12889         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12890                                   is_const_type, is_volatile_type,
12891                                   context_die);
12892       /* Else cv-qualified version of named type; fall through.  */
12893     }
12894
12895   if (is_const_type
12896       /* If both is_const_type and is_volatile_type, prefer the path
12897          which leads to a qualified type.  */
12898       && (!is_volatile_type
12899           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12900           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12901     {
12902       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12903       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12904     }
12905   else if (is_volatile_type)
12906     {
12907       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12908       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12909     }
12910   else if (code == POINTER_TYPE)
12911     {
12912       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12913       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12914                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12915       item_type = TREE_TYPE (type);
12916       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12917         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12918                          TYPE_ADDR_SPACE (item_type));
12919     }
12920   else if (code == REFERENCE_TYPE)
12921     {
12922       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12923         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12924                                 type);
12925       else
12926         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12927       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12928                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12929       item_type = TREE_TYPE (type);
12930       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12931         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12932                          TYPE_ADDR_SPACE (item_type));
12933     }
12934   else if (code == INTEGER_TYPE
12935            && TREE_TYPE (type) != NULL_TREE
12936            && subrange_type_for_debug_p (type, &low, &high))
12937     {
12938       mod_type_die = subrange_type_die (type, low, high, context_die);
12939       item_type = TREE_TYPE (type);
12940     }
12941   else if (is_base_type (type))
12942     mod_type_die = base_type_die (type);
12943   else
12944     {
12945       gen_type_die (type, context_die);
12946
12947       /* We have to get the type_main_variant here (and pass that to the
12948          `lookup_type_die' routine) because the ..._TYPE node we have
12949          might simply be a *copy* of some original type node (where the
12950          copy was created to help us keep track of typedef names) and
12951          that copy might have a different TYPE_UID from the original
12952          ..._TYPE node.  */
12953       if (TREE_CODE (type) != VECTOR_TYPE)
12954         return lookup_type_die (type_main_variant (type));
12955       else
12956         /* Vectors have the debugging information in the type,
12957            not the main variant.  */
12958         return lookup_type_die (type);
12959     }
12960
12961   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12962      don't output a DW_TAG_typedef, since there isn't one in the
12963      user's program; just attach a DW_AT_name to the type.
12964      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12965      if the base type already has the same name.  */
12966   if (name
12967       && ((TREE_CODE (name) != TYPE_DECL
12968            && (qualified_type == TYPE_MAIN_VARIANT (type)
12969                || (!is_const_type && !is_volatile_type)))
12970           || (TREE_CODE (name) == TYPE_DECL
12971               && TREE_TYPE (name) == qualified_type
12972               && DECL_NAME (name))))
12973     {
12974       if (TREE_CODE (name) == TYPE_DECL)
12975         /* Could just call add_name_and_src_coords_attributes here,
12976            but since this is a builtin type it doesn't have any
12977            useful source coordinates anyway.  */
12978         name = DECL_NAME (name);
12979       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12980     }
12981   /* This probably indicates a bug.  */
12982   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12983     add_name_attribute (mod_type_die, "__unknown__");
12984
12985   if (qualified_type)
12986     equate_type_number_to_die (qualified_type, mod_type_die);
12987
12988   if (item_type)
12989     /* We must do this after the equate_type_number_to_die call, in case
12990        this is a recursive type.  This ensures that the modified_type_die
12991        recursion will terminate even if the type is recursive.  Recursive
12992        types are possible in Ada.  */
12993     sub_die = modified_type_die (item_type,
12994                                  TYPE_READONLY (item_type),
12995                                  TYPE_VOLATILE (item_type),
12996                                  context_die);
12997
12998   if (sub_die != NULL)
12999     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13000
13001   return mod_type_die;
13002 }
13003
13004 /* Generate DIEs for the generic parameters of T.
13005    T must be either a generic type or a generic function.
13006    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13007
13008 static void
13009 gen_generic_params_dies (tree t)
13010 {
13011   tree parms, args;
13012   int parms_num, i;
13013   dw_die_ref die = NULL;
13014
13015   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13016     return;
13017
13018   if (TYPE_P (t))
13019     die = lookup_type_die (t);
13020   else if (DECL_P (t))
13021     die = lookup_decl_die (t);
13022
13023   gcc_assert (die);
13024
13025   parms = lang_hooks.get_innermost_generic_parms (t);
13026   if (!parms)
13027     /* T has no generic parameter. It means T is neither a generic type
13028        or function. End of story.  */
13029     return;
13030
13031   parms_num = TREE_VEC_LENGTH (parms);
13032   args = lang_hooks.get_innermost_generic_args (t);
13033   for (i = 0; i < parms_num; i++)
13034     {
13035       tree parm, arg, arg_pack_elems;
13036
13037       parm = TREE_VEC_ELT (parms, i);
13038       arg = TREE_VEC_ELT (args, i);
13039       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13040       gcc_assert (parm && TREE_VALUE (parm) && arg);
13041
13042       if (parm && TREE_VALUE (parm) && arg)
13043         {
13044           /* If PARM represents a template parameter pack,
13045              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13046              by DW_TAG_template_*_parameter DIEs for the argument
13047              pack elements of ARG. Note that ARG would then be
13048              an argument pack.  */
13049           if (arg_pack_elems)
13050             template_parameter_pack_die (TREE_VALUE (parm),
13051                                          arg_pack_elems,
13052                                          die);
13053           else
13054             generic_parameter_die (TREE_VALUE (parm), arg,
13055                                    true /* Emit DW_AT_name */, die);
13056         }
13057     }
13058 }
13059
13060 /* Create and return a DIE for PARM which should be
13061    the representation of a generic type parameter.
13062    For instance, in the C++ front end, PARM would be a template parameter.
13063    ARG is the argument to PARM.
13064    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13065    name of the PARM.
13066    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13067    as a child node.  */
13068
13069 static dw_die_ref
13070 generic_parameter_die (tree parm, tree arg,
13071                        bool emit_name_p,
13072                        dw_die_ref parent_die)
13073 {
13074   dw_die_ref tmpl_die = NULL;
13075   const char *name = NULL;
13076
13077   if (!parm || !DECL_NAME (parm) || !arg)
13078     return NULL;
13079
13080   /* We support non-type generic parameters and arguments,
13081      type generic parameters and arguments, as well as
13082      generic generic parameters (a.k.a. template template parameters in C++)
13083      and arguments.  */
13084   if (TREE_CODE (parm) == PARM_DECL)
13085     /* PARM is a nontype generic parameter  */
13086     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13087   else if (TREE_CODE (parm) == TYPE_DECL)
13088     /* PARM is a type generic parameter.  */
13089     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13090   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13091     /* PARM is a generic generic parameter.
13092        Its DIE is a GNU extension. It shall have a
13093        DW_AT_name attribute to represent the name of the template template
13094        parameter, and a DW_AT_GNU_template_name attribute to represent the
13095        name of the template template argument.  */
13096     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13097                         parent_die, parm);
13098   else
13099     gcc_unreachable ();
13100
13101   if (tmpl_die)
13102     {
13103       tree tmpl_type;
13104
13105       /* If PARM is a generic parameter pack, it means we are
13106          emitting debug info for a template argument pack element.
13107          In other terms, ARG is a template argument pack element.
13108          In that case, we don't emit any DW_AT_name attribute for
13109          the die.  */
13110       if (emit_name_p)
13111         {
13112           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13113           gcc_assert (name);
13114           add_AT_string (tmpl_die, DW_AT_name, name);
13115         }
13116
13117       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13118         {
13119           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13120              TMPL_DIE should have a child DW_AT_type attribute that is set
13121              to the type of the argument to PARM, which is ARG.
13122              If PARM is a type generic parameter, TMPL_DIE should have a
13123              child DW_AT_type that is set to ARG.  */
13124           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13125           add_type_attribute (tmpl_die, tmpl_type, 0,
13126                               TREE_THIS_VOLATILE (tmpl_type),
13127                               parent_die);
13128         }
13129       else
13130         {
13131           /* So TMPL_DIE is a DIE representing a
13132              a generic generic template parameter, a.k.a template template
13133              parameter in C++ and arg is a template.  */
13134
13135           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13136              to the name of the argument.  */
13137           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13138           if (name)
13139             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13140         }
13141
13142       if (TREE_CODE (parm) == PARM_DECL)
13143         /* So PARM is a non-type generic parameter.
13144            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13145            attribute of TMPL_DIE which value represents the value
13146            of ARG.
13147            We must be careful here:
13148            The value of ARG might reference some function decls.
13149            We might currently be emitting debug info for a generic
13150            type and types are emitted before function decls, we don't
13151            know if the function decls referenced by ARG will actually be
13152            emitted after cgraph computations.
13153            So must defer the generation of the DW_AT_const_value to
13154            after cgraph is ready.  */
13155         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13156     }
13157
13158   return tmpl_die;
13159 }
13160
13161 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13162    PARM_PACK must be a template parameter pack. The returned DIE
13163    will be child DIE of PARENT_DIE.  */
13164
13165 static dw_die_ref
13166 template_parameter_pack_die (tree parm_pack,
13167                              tree parm_pack_args,
13168                              dw_die_ref parent_die)
13169 {
13170   dw_die_ref die;
13171   int j;
13172
13173   gcc_assert (parent_die && parm_pack);
13174
13175   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13176   add_name_and_src_coords_attributes (die, parm_pack);
13177   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13178     generic_parameter_die (parm_pack,
13179                            TREE_VEC_ELT (parm_pack_args, j),
13180                            false /* Don't emit DW_AT_name */,
13181                            die);
13182   return die;
13183 }
13184
13185 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13186    an enumerated type.  */
13187
13188 static inline int
13189 type_is_enum (const_tree type)
13190 {
13191   return TREE_CODE (type) == ENUMERAL_TYPE;
13192 }
13193
13194 /* Return the DBX register number described by a given RTL node.  */
13195
13196 static unsigned int
13197 dbx_reg_number (const_rtx rtl)
13198 {
13199   unsigned regno = REGNO (rtl);
13200
13201   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13202
13203 #ifdef LEAF_REG_REMAP
13204   if (current_function_uses_only_leaf_regs)
13205     {
13206       int leaf_reg = LEAF_REG_REMAP (regno);
13207       if (leaf_reg != -1)
13208         regno = (unsigned) leaf_reg;
13209     }
13210 #endif
13211
13212   return DBX_REGISTER_NUMBER (regno);
13213 }
13214
13215 /* Optionally add a DW_OP_piece term to a location description expression.
13216    DW_OP_piece is only added if the location description expression already
13217    doesn't end with DW_OP_piece.  */
13218
13219 static void
13220 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13221 {
13222   dw_loc_descr_ref loc;
13223
13224   if (*list_head != NULL)
13225     {
13226       /* Find the end of the chain.  */
13227       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13228         ;
13229
13230       if (loc->dw_loc_opc != DW_OP_piece)
13231         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13232     }
13233 }
13234
13235 /* Return a location descriptor that designates a machine register or
13236    zero if there is none.  */
13237
13238 static dw_loc_descr_ref
13239 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13240 {
13241   rtx regs;
13242
13243   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13244     return 0;
13245
13246   /* We only use "frame base" when we're sure we're talking about the
13247      post-prologue local stack frame.  We do this by *not* running
13248      register elimination until this point, and recognizing the special
13249      argument pointer and soft frame pointer rtx's.
13250      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13251   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13252       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13253     {
13254       dw_loc_descr_ref result = NULL;
13255
13256       if (dwarf_version >= 4 || !dwarf_strict)
13257         {
13258           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13259           if (result)
13260             add_loc_descr (&result,
13261                            new_loc_descr (DW_OP_stack_value, 0, 0));
13262         }
13263       return result;
13264     }
13265
13266   regs = targetm.dwarf_register_span (rtl);
13267
13268   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13269     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13270   else
13271     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13272 }
13273
13274 /* Return a location descriptor that designates a machine register for
13275    a given hard register number.  */
13276
13277 static dw_loc_descr_ref
13278 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13279 {
13280   dw_loc_descr_ref reg_loc_descr;
13281
13282   if (regno <= 31)
13283     reg_loc_descr
13284       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13285   else
13286     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13287
13288   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13289     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13290
13291   return reg_loc_descr;
13292 }
13293
13294 /* Given an RTL of a register, return a location descriptor that
13295    designates a value that spans more than one register.  */
13296
13297 static dw_loc_descr_ref
13298 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13299                              enum var_init_status initialized)
13300 {
13301   int nregs, size, i;
13302   unsigned reg;
13303   dw_loc_descr_ref loc_result = NULL;
13304
13305   reg = REGNO (rtl);
13306 #ifdef LEAF_REG_REMAP
13307   if (current_function_uses_only_leaf_regs)
13308     {
13309       int leaf_reg = LEAF_REG_REMAP (reg);
13310       if (leaf_reg != -1)
13311         reg = (unsigned) leaf_reg;
13312     }
13313 #endif
13314   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13315   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13316
13317   /* Simple, contiguous registers.  */
13318   if (regs == NULL_RTX)
13319     {
13320       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13321
13322       loc_result = NULL;
13323       while (nregs--)
13324         {
13325           dw_loc_descr_ref t;
13326
13327           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13328                                       VAR_INIT_STATUS_INITIALIZED);
13329           add_loc_descr (&loc_result, t);
13330           add_loc_descr_op_piece (&loc_result, size);
13331           ++reg;
13332         }
13333       return loc_result;
13334     }
13335
13336   /* Now onto stupid register sets in non contiguous locations.  */
13337
13338   gcc_assert (GET_CODE (regs) == PARALLEL);
13339
13340   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13341   loc_result = NULL;
13342
13343   for (i = 0; i < XVECLEN (regs, 0); ++i)
13344     {
13345       dw_loc_descr_ref t;
13346
13347       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13348                                   VAR_INIT_STATUS_INITIALIZED);
13349       add_loc_descr (&loc_result, t);
13350       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13351       add_loc_descr_op_piece (&loc_result, size);
13352     }
13353
13354   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13355     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13356   return loc_result;
13357 }
13358
13359 /* Return a location descriptor that designates a constant.  */
13360
13361 static dw_loc_descr_ref
13362 int_loc_descriptor (HOST_WIDE_INT i)
13363 {
13364   enum dwarf_location_atom op;
13365
13366   /* Pick the smallest representation of a constant, rather than just
13367      defaulting to the LEB encoding.  */
13368   if (i >= 0)
13369     {
13370       if (i <= 31)
13371         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13372       else if (i <= 0xff)
13373         op = DW_OP_const1u;
13374       else if (i <= 0xffff)
13375         op = DW_OP_const2u;
13376       else if (HOST_BITS_PER_WIDE_INT == 32
13377                || i <= 0xffffffff)
13378         op = DW_OP_const4u;
13379       else
13380         op = DW_OP_constu;
13381     }
13382   else
13383     {
13384       if (i >= -0x80)
13385         op = DW_OP_const1s;
13386       else if (i >= -0x8000)
13387         op = DW_OP_const2s;
13388       else if (HOST_BITS_PER_WIDE_INT == 32
13389                || i >= -0x80000000)
13390         op = DW_OP_const4s;
13391       else
13392         op = DW_OP_consts;
13393     }
13394
13395   return new_loc_descr (op, i, 0);
13396 }
13397
13398 /* Return loc description representing "address" of integer value.
13399    This can appear only as toplevel expression.  */
13400
13401 static dw_loc_descr_ref
13402 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13403 {
13404   int litsize;
13405   dw_loc_descr_ref loc_result = NULL;
13406
13407   if (!(dwarf_version >= 4 || !dwarf_strict))
13408     return NULL;
13409
13410   if (i >= 0)
13411     {
13412       if (i <= 31)
13413         litsize = 1;
13414       else if (i <= 0xff)
13415         litsize = 2;
13416       else if (i <= 0xffff)
13417         litsize = 3;
13418       else if (HOST_BITS_PER_WIDE_INT == 32
13419                || i <= 0xffffffff)
13420         litsize = 5;
13421       else
13422         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13423     }
13424   else
13425     {
13426       if (i >= -0x80)
13427         litsize = 2;
13428       else if (i >= -0x8000)
13429         litsize = 3;
13430       else if (HOST_BITS_PER_WIDE_INT == 32
13431                || i >= -0x80000000)
13432         litsize = 5;
13433       else
13434         litsize = 1 + size_of_sleb128 (i);
13435     }
13436   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13437      is more compact.  For DW_OP_stack_value we need:
13438      litsize + 1 (DW_OP_stack_value)
13439      and for DW_OP_implicit_value:
13440      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13441   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13442     {
13443       loc_result = int_loc_descriptor (i);
13444       add_loc_descr (&loc_result,
13445                      new_loc_descr (DW_OP_stack_value, 0, 0));
13446       return loc_result;
13447     }
13448
13449   loc_result = new_loc_descr (DW_OP_implicit_value,
13450                               size, 0);
13451   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13452   loc_result->dw_loc_oprnd2.v.val_int = i;
13453   return loc_result;
13454 }
13455
13456 /* Return a location descriptor that designates a base+offset location.  */
13457
13458 static dw_loc_descr_ref
13459 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13460                  enum var_init_status initialized)
13461 {
13462   unsigned int regno;
13463   dw_loc_descr_ref result;
13464   dw_fde_ref fde = current_fde ();
13465
13466   /* We only use "frame base" when we're sure we're talking about the
13467      post-prologue local stack frame.  We do this by *not* running
13468      register elimination until this point, and recognizing the special
13469      argument pointer and soft frame pointer rtx's.  */
13470   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13471     {
13472       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13473
13474       if (elim != reg)
13475         {
13476           if (GET_CODE (elim) == PLUS)
13477             {
13478               offset += INTVAL (XEXP (elim, 1));
13479               elim = XEXP (elim, 0);
13480             }
13481           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13482                        && (elim == hard_frame_pointer_rtx
13483                            || elim == stack_pointer_rtx))
13484                       || elim == (frame_pointer_needed
13485                                   ? hard_frame_pointer_rtx
13486                                   : stack_pointer_rtx));
13487
13488           /* If drap register is used to align stack, use frame
13489              pointer + offset to access stack variables.  If stack
13490              is aligned without drap, use stack pointer + offset to
13491              access stack variables.  */
13492           if (crtl->stack_realign_tried
13493               && reg == frame_pointer_rtx)
13494             {
13495               int base_reg
13496                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13497                                       ? HARD_FRAME_POINTER_REGNUM
13498                                       : STACK_POINTER_REGNUM);
13499               return new_reg_loc_descr (base_reg, offset);
13500             }
13501
13502           offset += frame_pointer_fb_offset;
13503           return new_loc_descr (DW_OP_fbreg, offset, 0);
13504         }
13505     }
13506   else if (!optimize
13507            && fde
13508            && (fde->drap_reg == REGNO (reg)
13509                || fde->vdrap_reg == REGNO (reg)))
13510     {
13511       /* Use cfa+offset to represent the location of arguments passed
13512          on the stack when drap is used to align stack.
13513          Only do this when not optimizing, for optimized code var-tracking
13514          is supposed to track where the arguments live and the register
13515          used as vdrap or drap in some spot might be used for something
13516          else in other part of the routine.  */
13517       return new_loc_descr (DW_OP_fbreg, offset, 0);
13518     }
13519
13520   regno = dbx_reg_number (reg);
13521   if (regno <= 31)
13522     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13523                             offset, 0);
13524   else
13525     result = new_loc_descr (DW_OP_bregx, regno, offset);
13526
13527   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13528     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13529
13530   return result;
13531 }
13532
13533 /* Return true if this RTL expression describes a base+offset calculation.  */
13534
13535 static inline int
13536 is_based_loc (const_rtx rtl)
13537 {
13538   return (GET_CODE (rtl) == PLUS
13539           && ((REG_P (XEXP (rtl, 0))
13540                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13541                && CONST_INT_P (XEXP (rtl, 1)))));
13542 }
13543
13544 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13545    failed.  */
13546
13547 static dw_loc_descr_ref
13548 tls_mem_loc_descriptor (rtx mem)
13549 {
13550   tree base;
13551   dw_loc_descr_ref loc_result;
13552
13553   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13554     return NULL;
13555
13556   base = get_base_address (MEM_EXPR (mem));
13557   if (base == NULL
13558       || TREE_CODE (base) != VAR_DECL
13559       || !DECL_THREAD_LOCAL_P (base))
13560     return NULL;
13561
13562   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13563   if (loc_result == NULL)
13564     return NULL;
13565
13566   if (INTVAL (MEM_OFFSET (mem)))
13567     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13568
13569   return loc_result;
13570 }
13571
13572 /* Output debug info about reason why we failed to expand expression as dwarf
13573    expression.  */
13574
13575 static void
13576 expansion_failed (tree expr, rtx rtl, char const *reason)
13577 {
13578   if (dump_file && (dump_flags & TDF_DETAILS))
13579     {
13580       fprintf (dump_file, "Failed to expand as dwarf: ");
13581       if (expr)
13582         print_generic_expr (dump_file, expr, dump_flags);
13583       if (rtl)
13584         {
13585           fprintf (dump_file, "\n");
13586           print_rtl (dump_file, rtl);
13587         }
13588       fprintf (dump_file, "\nReason: %s\n", reason);
13589     }
13590 }
13591
13592 /* Helper function for const_ok_for_output, called either directly
13593    or via for_each_rtx.  */
13594
13595 static int
13596 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13597 {
13598   rtx rtl = *rtlp;
13599
13600   if (GET_CODE (rtl) == UNSPEC)
13601     {
13602       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13603          we can't express it in the debug info.  */
13604 #ifdef ENABLE_CHECKING
13605       /* Don't complain about TLS UNSPECs, those are just too hard to
13606          delegitimize.  */
13607       if (XVECLEN (rtl, 0) != 1
13608           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13609           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13610           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13611           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13612         inform (current_function_decl
13613                 ? DECL_SOURCE_LOCATION (current_function_decl)
13614                 : UNKNOWN_LOCATION,
13615                 "non-delegitimized UNSPEC %d found in variable location",
13616                 XINT (rtl, 1));
13617 #endif
13618       expansion_failed (NULL_TREE, rtl,
13619                         "UNSPEC hasn't been delegitimized.\n");
13620       return 1;
13621     }
13622
13623   if (GET_CODE (rtl) != SYMBOL_REF)
13624     return 0;
13625
13626   if (CONSTANT_POOL_ADDRESS_P (rtl))
13627     {
13628       bool marked;
13629       get_pool_constant_mark (rtl, &marked);
13630       /* If all references to this pool constant were optimized away,
13631          it was not output and thus we can't represent it.  */
13632       if (!marked)
13633         {
13634           expansion_failed (NULL_TREE, rtl,
13635                             "Constant was removed from constant pool.\n");
13636           return 1;
13637         }
13638     }
13639
13640   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13641     return 1;
13642
13643   /* Avoid references to external symbols in debug info, on several targets
13644      the linker might even refuse to link when linking a shared library,
13645      and in many other cases the relocations for .debug_info/.debug_loc are
13646      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13647      to be defined within the same shared library or executable are fine.  */
13648   if (SYMBOL_REF_EXTERNAL_P (rtl))
13649     {
13650       tree decl = SYMBOL_REF_DECL (rtl);
13651
13652       if (decl == NULL || !targetm.binds_local_p (decl))
13653         {
13654           expansion_failed (NULL_TREE, rtl,
13655                             "Symbol not defined in current TU.\n");
13656           return 1;
13657         }
13658     }
13659
13660   return 0;
13661 }
13662
13663 /* Return true if constant RTL can be emitted in DW_OP_addr or
13664    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13665    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13666
13667 static bool
13668 const_ok_for_output (rtx rtl)
13669 {
13670   if (GET_CODE (rtl) == SYMBOL_REF)
13671     return const_ok_for_output_1 (&rtl, NULL) == 0;
13672
13673   if (GET_CODE (rtl) == CONST)
13674     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13675
13676   return true;
13677 }
13678
13679 /* The following routine converts the RTL for a variable or parameter
13680    (resident in memory) into an equivalent Dwarf representation of a
13681    mechanism for getting the address of that same variable onto the top of a
13682    hypothetical "address evaluation" stack.
13683
13684    When creating memory location descriptors, we are effectively transforming
13685    the RTL for a memory-resident object into its Dwarf postfix expression
13686    equivalent.  This routine recursively descends an RTL tree, turning
13687    it into Dwarf postfix code as it goes.
13688
13689    MODE is the mode of the memory reference, needed to handle some
13690    autoincrement addressing modes.
13691
13692    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13693    location list for RTL.
13694
13695    Return 0 if we can't represent the location.  */
13696
13697 static dw_loc_descr_ref
13698 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13699                     enum var_init_status initialized)
13700 {
13701   dw_loc_descr_ref mem_loc_result = NULL;
13702   enum dwarf_location_atom op;
13703   dw_loc_descr_ref op0, op1;
13704
13705   /* Note that for a dynamically sized array, the location we will generate a
13706      description of here will be the lowest numbered location which is
13707      actually within the array.  That's *not* necessarily the same as the
13708      zeroth element of the array.  */
13709
13710   rtl = targetm.delegitimize_address (rtl);
13711
13712   switch (GET_CODE (rtl))
13713     {
13714     case POST_INC:
13715     case POST_DEC:
13716     case POST_MODIFY:
13717       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13718
13719     case SUBREG:
13720       /* The case of a subreg may arise when we have a local (register)
13721          variable or a formal (register) parameter which doesn't quite fill
13722          up an entire register.  For now, just assume that it is
13723          legitimate to make the Dwarf info refer to the whole register which
13724          contains the given subreg.  */
13725       if (!subreg_lowpart_p (rtl))
13726         break;
13727       rtl = SUBREG_REG (rtl);
13728       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13729         break;
13730       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13731         break;
13732       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13733       break;
13734
13735     case REG:
13736       /* Whenever a register number forms a part of the description of the
13737          method for calculating the (dynamic) address of a memory resident
13738          object, DWARF rules require the register number be referred to as
13739          a "base register".  This distinction is not based in any way upon
13740          what category of register the hardware believes the given register
13741          belongs to.  This is strictly DWARF terminology we're dealing with
13742          here. Note that in cases where the location of a memory-resident
13743          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13744          OP_CONST (0)) the actual DWARF location descriptor that we generate
13745          may just be OP_BASEREG (basereg).  This may look deceptively like
13746          the object in question was allocated to a register (rather than in
13747          memory) so DWARF consumers need to be aware of the subtle
13748          distinction between OP_REG and OP_BASEREG.  */
13749       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13750         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13751       else if (stack_realign_drap
13752                && crtl->drap_reg
13753                && crtl->args.internal_arg_pointer == rtl
13754                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13755         {
13756           /* If RTL is internal_arg_pointer, which has been optimized
13757              out, use DRAP instead.  */
13758           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13759                                             VAR_INIT_STATUS_INITIALIZED);
13760         }
13761       break;
13762
13763     case SIGN_EXTEND:
13764     case ZERO_EXTEND:
13765       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13766                                 VAR_INIT_STATUS_INITIALIZED);
13767       if (op0 == 0)
13768         break;
13769       else
13770         {
13771           int shift = DWARF2_ADDR_SIZE
13772                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13773           shift *= BITS_PER_UNIT;
13774           if (GET_CODE (rtl) == SIGN_EXTEND)
13775             op = DW_OP_shra;
13776           else
13777             op = DW_OP_shr;
13778           mem_loc_result = op0;
13779           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13780           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13781           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13782           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13783         }
13784       break;
13785
13786     case MEM:
13787       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13788                                            VAR_INIT_STATUS_INITIALIZED);
13789       if (mem_loc_result == NULL)
13790         mem_loc_result = tls_mem_loc_descriptor (rtl);
13791       if (mem_loc_result != 0)
13792         {
13793           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13794             {
13795               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13796               return 0;
13797             }
13798           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13799             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13800           else
13801             add_loc_descr (&mem_loc_result,
13802                            new_loc_descr (DW_OP_deref_size,
13803                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13804         }
13805       else
13806         {
13807           rtx new_rtl = avoid_constant_pool_reference (rtl);
13808           if (new_rtl != rtl)
13809             return mem_loc_descriptor (new_rtl, mode, initialized);
13810         }
13811       break;
13812
13813     case LO_SUM:
13814          rtl = XEXP (rtl, 1);
13815
13816       /* ... fall through ...  */
13817
13818     case LABEL_REF:
13819       /* Some ports can transform a symbol ref into a label ref, because
13820          the symbol ref is too far away and has to be dumped into a constant
13821          pool.  */
13822     case CONST:
13823     case SYMBOL_REF:
13824       if (GET_CODE (rtl) == SYMBOL_REF
13825           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13826         {
13827           dw_loc_descr_ref temp;
13828
13829           /* If this is not defined, we have no way to emit the data.  */
13830           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13831             break;
13832
13833           /* We used to emit DW_OP_addr here, but that's wrong, since
13834              DW_OP_addr should be relocated by the debug info consumer,
13835              while DW_OP_GNU_push_tls_address operand should not.  */
13836           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13837                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13838           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13839           temp->dw_loc_oprnd1.v.val_addr = rtl;
13840           temp->dtprel = true;
13841
13842           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13843           add_loc_descr (&mem_loc_result, temp);
13844
13845           break;
13846         }
13847
13848       if (!const_ok_for_output (rtl))
13849         break;
13850
13851     symref:
13852       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13853       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13854       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13855       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13856       break;
13857
13858     case CONCAT:
13859     case CONCATN:
13860     case VAR_LOCATION:
13861     case DEBUG_IMPLICIT_PTR:
13862       expansion_failed (NULL_TREE, rtl,
13863                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13864       return 0;
13865
13866     case ENTRY_VALUE:
13867       if (dwarf_strict)
13868         return NULL;
13869       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13870       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13871       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13872         mem_loc_result->dw_loc_oprnd1.v.val_loc
13873           = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13874                                     VAR_INIT_STATUS_INITIALIZED);
13875       else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13876         {
13877           dw_loc_descr_ref ref
13878             = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13879                                   VAR_INIT_STATUS_INITIALIZED);
13880           if (ref == NULL)
13881             return NULL;
13882           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13883         }
13884       else
13885         gcc_unreachable ();
13886       return mem_loc_result;
13887
13888     case PRE_MODIFY:
13889       /* Extract the PLUS expression nested inside and fall into
13890          PLUS code below.  */
13891       rtl = XEXP (rtl, 1);
13892       goto plus;
13893
13894     case PRE_INC:
13895     case PRE_DEC:
13896       /* Turn these into a PLUS expression and fall into the PLUS code
13897          below.  */
13898       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13899                           GEN_INT (GET_CODE (rtl) == PRE_INC
13900                                    ? GET_MODE_UNIT_SIZE (mode)
13901                                    : -GET_MODE_UNIT_SIZE (mode)));
13902
13903       /* ... fall through ...  */
13904
13905     case PLUS:
13906     plus:
13907       if (is_based_loc (rtl))
13908         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13909                                           INTVAL (XEXP (rtl, 1)),
13910                                           VAR_INIT_STATUS_INITIALIZED);
13911       else
13912         {
13913           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13914                                                VAR_INIT_STATUS_INITIALIZED);
13915           if (mem_loc_result == 0)
13916             break;
13917
13918           if (CONST_INT_P (XEXP (rtl, 1)))
13919             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13920           else
13921             {
13922               dw_loc_descr_ref mem_loc_result2
13923                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13924                                       VAR_INIT_STATUS_INITIALIZED);
13925               if (mem_loc_result2 == 0)
13926                 break;
13927               add_loc_descr (&mem_loc_result, mem_loc_result2);
13928               add_loc_descr (&mem_loc_result,
13929                              new_loc_descr (DW_OP_plus, 0, 0));
13930             }
13931         }
13932       break;
13933
13934     /* If a pseudo-reg is optimized away, it is possible for it to
13935        be replaced with a MEM containing a multiply or shift.  */
13936     case MINUS:
13937       op = DW_OP_minus;
13938       goto do_binop;
13939
13940     case MULT:
13941       op = DW_OP_mul;
13942       goto do_binop;
13943
13944     case DIV:
13945       op = DW_OP_div;
13946       goto do_binop;
13947
13948     case UMOD:
13949       op = DW_OP_mod;
13950       goto do_binop;
13951
13952     case ASHIFT:
13953       op = DW_OP_shl;
13954       goto do_binop;
13955
13956     case ASHIFTRT:
13957       op = DW_OP_shra;
13958       goto do_binop;
13959
13960     case LSHIFTRT:
13961       op = DW_OP_shr;
13962       goto do_binop;
13963
13964     case AND:
13965       op = DW_OP_and;
13966       goto do_binop;
13967
13968     case IOR:
13969       op = DW_OP_or;
13970       goto do_binop;
13971
13972     case XOR:
13973       op = DW_OP_xor;
13974       goto do_binop;
13975
13976     do_binop:
13977       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13978                                 VAR_INIT_STATUS_INITIALIZED);
13979       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13980                                 VAR_INIT_STATUS_INITIALIZED);
13981
13982       if (op0 == 0 || op1 == 0)
13983         break;
13984
13985       mem_loc_result = op0;
13986       add_loc_descr (&mem_loc_result, op1);
13987       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13988       break;
13989
13990     case MOD:
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 (DW_OP_over, 0, 0));
14002       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14003       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14004       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14005       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14006       break;
14007
14008     case NOT:
14009       op = DW_OP_not;
14010       goto do_unop;
14011
14012     case ABS:
14013       op = DW_OP_abs;
14014       goto do_unop;
14015
14016     case NEG:
14017       op = DW_OP_neg;
14018       goto do_unop;
14019
14020     do_unop:
14021       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14022                                 VAR_INIT_STATUS_INITIALIZED);
14023
14024       if (op0 == 0)
14025         break;
14026
14027       mem_loc_result = op0;
14028       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14029       break;
14030
14031     case CONST_INT:
14032       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14033       break;
14034
14035     case EQ:
14036       op = DW_OP_eq;
14037       goto do_scompare;
14038
14039     case GE:
14040       op = DW_OP_ge;
14041       goto do_scompare;
14042
14043     case GT:
14044       op = DW_OP_gt;
14045       goto do_scompare;
14046
14047     case LE:
14048       op = DW_OP_le;
14049       goto do_scompare;
14050
14051     case LT:
14052       op = DW_OP_lt;
14053       goto do_scompare;
14054
14055     case NE:
14056       op = DW_OP_ne;
14057       goto do_scompare;
14058
14059     do_scompare:
14060       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14061           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14062         break;
14063       else
14064         {
14065           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14066
14067           if (op_mode == VOIDmode)
14068             op_mode = GET_MODE (XEXP (rtl, 1));
14069           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14070             break;
14071
14072           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14073                                     VAR_INIT_STATUS_INITIALIZED);
14074           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14075                                     VAR_INIT_STATUS_INITIALIZED);
14076
14077           if (op0 == 0 || op1 == 0)
14078             break;
14079
14080           if (op_mode != VOIDmode
14081               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14082             {
14083               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14084               shift *= BITS_PER_UNIT;
14085               /* For eq/ne, if the operands are known to be zero-extended,
14086                  there is no need to do the fancy shifting up.  */
14087               if (op == DW_OP_eq || op == DW_OP_ne)
14088                 {
14089                   dw_loc_descr_ref last0, last1;
14090                   for (last0 = op0;
14091                        last0->dw_loc_next != NULL;
14092                        last0 = last0->dw_loc_next)
14093                     ;
14094                   for (last1 = op1;
14095                        last1->dw_loc_next != NULL;
14096                        last1 = last1->dw_loc_next)
14097                     ;
14098                   /* deref_size zero extends, and for constants we can check
14099                      whether they are zero extended or not.  */
14100                   if (((last0->dw_loc_opc == DW_OP_deref_size
14101                         && last0->dw_loc_oprnd1.v.val_int
14102                            <= GET_MODE_SIZE (op_mode))
14103                        || (CONST_INT_P (XEXP (rtl, 0))
14104                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14105                                == (INTVAL (XEXP (rtl, 0))
14106                                    & GET_MODE_MASK (op_mode))))
14107                       && ((last1->dw_loc_opc == DW_OP_deref_size
14108                            && last1->dw_loc_oprnd1.v.val_int
14109                               <= GET_MODE_SIZE (op_mode))
14110                           || (CONST_INT_P (XEXP (rtl, 1))
14111                               && (unsigned HOST_WIDE_INT)
14112                                  INTVAL (XEXP (rtl, 1))
14113                                  == (INTVAL (XEXP (rtl, 1))
14114                                      & GET_MODE_MASK (op_mode)))))
14115                     goto do_compare;
14116                 }
14117               add_loc_descr (&op0, int_loc_descriptor (shift));
14118               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14119               if (CONST_INT_P (XEXP (rtl, 1)))
14120                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14121               else
14122                 {
14123                   add_loc_descr (&op1, int_loc_descriptor (shift));
14124                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14125                 }
14126             }
14127         }
14128
14129     do_compare:
14130       mem_loc_result = op0;
14131       add_loc_descr (&mem_loc_result, op1);
14132       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14133       if (STORE_FLAG_VALUE != 1)
14134         {
14135           add_loc_descr (&mem_loc_result,
14136                          int_loc_descriptor (STORE_FLAG_VALUE));
14137           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14138         }
14139       break;
14140
14141     case GEU:
14142       op = DW_OP_ge;
14143       goto do_ucompare;
14144
14145     case GTU:
14146       op = DW_OP_gt;
14147       goto do_ucompare;
14148
14149     case LEU:
14150       op = DW_OP_le;
14151       goto do_ucompare;
14152
14153     case LTU:
14154       op = DW_OP_lt;
14155       goto do_ucompare;
14156
14157     do_ucompare:
14158       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14159           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14160         break;
14161       else
14162         {
14163           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14164
14165           if (op_mode == VOIDmode)
14166             op_mode = GET_MODE (XEXP (rtl, 1));
14167           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14168             break;
14169
14170           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14171                                     VAR_INIT_STATUS_INITIALIZED);
14172           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14173                                     VAR_INIT_STATUS_INITIALIZED);
14174
14175           if (op0 == 0 || op1 == 0)
14176             break;
14177
14178           if (op_mode != VOIDmode
14179               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14180             {
14181               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14182               dw_loc_descr_ref last0, last1;
14183               for (last0 = op0;
14184                    last0->dw_loc_next != NULL;
14185                    last0 = last0->dw_loc_next)
14186                 ;
14187               for (last1 = op1;
14188                    last1->dw_loc_next != NULL;
14189                    last1 = last1->dw_loc_next)
14190                 ;
14191               if (CONST_INT_P (XEXP (rtl, 0)))
14192                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14193               /* deref_size zero extends, so no need to mask it again.  */
14194               else if (last0->dw_loc_opc != DW_OP_deref_size
14195                        || last0->dw_loc_oprnd1.v.val_int
14196                           > GET_MODE_SIZE (op_mode))
14197                 {
14198                   add_loc_descr (&op0, int_loc_descriptor (mask));
14199                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14200                 }
14201               if (CONST_INT_P (XEXP (rtl, 1)))
14202                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14203               /* deref_size zero extends, so no need to mask it again.  */
14204               else if (last1->dw_loc_opc != DW_OP_deref_size
14205                        || last1->dw_loc_oprnd1.v.val_int
14206                           > GET_MODE_SIZE (op_mode))
14207                 {
14208                   add_loc_descr (&op1, int_loc_descriptor (mask));
14209                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14210                 }
14211             }
14212           else
14213             {
14214               HOST_WIDE_INT bias = 1;
14215               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14216               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14217               if (CONST_INT_P (XEXP (rtl, 1)))
14218                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14219                                           + INTVAL (XEXP (rtl, 1)));
14220               else
14221                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14222                                                     bias, 0));
14223             }
14224         }
14225       goto do_compare;
14226
14227     case SMIN:
14228     case SMAX:
14229     case UMIN:
14230     case UMAX:
14231       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14232           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14233           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14234         break;
14235
14236       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14237                                 VAR_INIT_STATUS_INITIALIZED);
14238       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14239                                 VAR_INIT_STATUS_INITIALIZED);
14240
14241       if (op0 == 0 || op1 == 0)
14242         break;
14243
14244       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14245       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14246       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14247       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14248         {
14249           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14250             {
14251               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14252               add_loc_descr (&op0, int_loc_descriptor (mask));
14253               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14254               add_loc_descr (&op1, int_loc_descriptor (mask));
14255               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14256             }
14257           else
14258             {
14259               HOST_WIDE_INT bias = 1;
14260               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14261               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14262               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14263             }
14264         }
14265       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14266         {
14267           int shift = DWARF2_ADDR_SIZE
14268                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14269           shift *= BITS_PER_UNIT;
14270           add_loc_descr (&op0, int_loc_descriptor (shift));
14271           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14272           add_loc_descr (&op1, int_loc_descriptor (shift));
14273           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14274         }
14275
14276       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14277         op = DW_OP_lt;
14278       else
14279         op = DW_OP_gt;
14280       mem_loc_result = op0;
14281       add_loc_descr (&mem_loc_result, op1);
14282       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14283       {
14284         dw_loc_descr_ref bra_node, drop_node;
14285
14286         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14287         add_loc_descr (&mem_loc_result, bra_node);
14288         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14289         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14290         add_loc_descr (&mem_loc_result, drop_node);
14291         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14292         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14293       }
14294       break;
14295
14296     case ZERO_EXTRACT:
14297     case SIGN_EXTRACT:
14298       if (CONST_INT_P (XEXP (rtl, 1))
14299           && CONST_INT_P (XEXP (rtl, 2))
14300           && ((unsigned) INTVAL (XEXP (rtl, 1))
14301               + (unsigned) INTVAL (XEXP (rtl, 2))
14302               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14303           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14304           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14305         {
14306           int shift, size;
14307           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14308                                     VAR_INIT_STATUS_INITIALIZED);
14309           if (op0 == 0)
14310             break;
14311           if (GET_CODE (rtl) == SIGN_EXTRACT)
14312             op = DW_OP_shra;
14313           else
14314             op = DW_OP_shr;
14315           mem_loc_result = op0;
14316           size = INTVAL (XEXP (rtl, 1));
14317           shift = INTVAL (XEXP (rtl, 2));
14318           if (BITS_BIG_ENDIAN)
14319             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14320                     - shift - size;
14321           if (shift + size != (int) DWARF2_ADDR_SIZE)
14322             {
14323               add_loc_descr (&mem_loc_result,
14324                              int_loc_descriptor (DWARF2_ADDR_SIZE
14325                                                  - shift - size));
14326               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14327             }
14328           if (size != (int) DWARF2_ADDR_SIZE)
14329             {
14330               add_loc_descr (&mem_loc_result,
14331                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14332               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14333             }
14334         }
14335       break;
14336
14337     case IF_THEN_ELSE:
14338       {
14339         dw_loc_descr_ref op2, bra_node, drop_node;
14340         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14341                                   VAR_INIT_STATUS_INITIALIZED);
14342         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14343                                   VAR_INIT_STATUS_INITIALIZED);
14344         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14345                                   VAR_INIT_STATUS_INITIALIZED);
14346         if (op0 == NULL || op1 == NULL || op2 == NULL)
14347           break;
14348
14349         mem_loc_result = op1;
14350         add_loc_descr (&mem_loc_result, op2);
14351         add_loc_descr (&mem_loc_result, op0);
14352         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14353         add_loc_descr (&mem_loc_result, bra_node);
14354         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14355         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14356         add_loc_descr (&mem_loc_result, drop_node);
14357         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14358         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14359       }
14360       break;
14361
14362     case COMPARE:
14363     case ROTATE:
14364     case ROTATERT:
14365     case TRUNCATE:
14366       /* In theory, we could implement the above.  */
14367       /* DWARF cannot represent the unsigned compare operations
14368          natively.  */
14369     case SS_MULT:
14370     case US_MULT:
14371     case SS_DIV:
14372     case US_DIV:
14373     case SS_PLUS:
14374     case US_PLUS:
14375     case SS_MINUS:
14376     case US_MINUS:
14377     case SS_NEG:
14378     case US_NEG:
14379     case SS_ABS:
14380     case SS_ASHIFT:
14381     case US_ASHIFT:
14382     case SS_TRUNCATE:
14383     case US_TRUNCATE:
14384     case UDIV:
14385     case UNORDERED:
14386     case ORDERED:
14387     case UNEQ:
14388     case UNGE:
14389     case UNGT:
14390     case UNLE:
14391     case UNLT:
14392     case LTGT:
14393     case FLOAT_EXTEND:
14394     case FLOAT_TRUNCATE:
14395     case FLOAT:
14396     case UNSIGNED_FLOAT:
14397     case FIX:
14398     case UNSIGNED_FIX:
14399     case FRACT_CONVERT:
14400     case UNSIGNED_FRACT_CONVERT:
14401     case SAT_FRACT:
14402     case UNSIGNED_SAT_FRACT:
14403     case SQRT:
14404     case BSWAP:
14405     case FFS:
14406     case CLZ:
14407     case CTZ:
14408     case POPCOUNT:
14409     case PARITY:
14410     case ASM_OPERANDS:
14411     case VEC_MERGE:
14412     case VEC_SELECT:
14413     case VEC_CONCAT:
14414     case VEC_DUPLICATE:
14415     case UNSPEC:
14416     case HIGH:
14417       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14418          can't express it in the debug info.  This can happen e.g. with some
14419          TLS UNSPECs.  */
14420       break;
14421
14422     case CONST_STRING:
14423       resolve_one_addr (&rtl, NULL);
14424       goto symref;
14425
14426     default:
14427 #ifdef ENABLE_CHECKING
14428       print_rtl (stderr, rtl);
14429       gcc_unreachable ();
14430 #else
14431       break;
14432 #endif
14433     }
14434
14435   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14436     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14437
14438   return mem_loc_result;
14439 }
14440
14441 /* Return a descriptor that describes the concatenation of two locations.
14442    This is typically a complex variable.  */
14443
14444 static dw_loc_descr_ref
14445 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14446 {
14447   dw_loc_descr_ref cc_loc_result = NULL;
14448   dw_loc_descr_ref x0_ref
14449     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14450   dw_loc_descr_ref x1_ref
14451     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14452
14453   if (x0_ref == 0 || x1_ref == 0)
14454     return 0;
14455
14456   cc_loc_result = x0_ref;
14457   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14458
14459   add_loc_descr (&cc_loc_result, x1_ref);
14460   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14461
14462   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14463     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14464
14465   return cc_loc_result;
14466 }
14467
14468 /* Return a descriptor that describes the concatenation of N
14469    locations.  */
14470
14471 static dw_loc_descr_ref
14472 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14473 {
14474   unsigned int i;
14475   dw_loc_descr_ref cc_loc_result = NULL;
14476   unsigned int n = XVECLEN (concatn, 0);
14477
14478   for (i = 0; i < n; ++i)
14479     {
14480       dw_loc_descr_ref ref;
14481       rtx x = XVECEXP (concatn, 0, i);
14482
14483       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14484       if (ref == NULL)
14485         return NULL;
14486
14487       add_loc_descr (&cc_loc_result, ref);
14488       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14489     }
14490
14491   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14492     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14493
14494   return cc_loc_result;
14495 }
14496
14497 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14498    for DEBUG_IMPLICIT_PTR RTL.  */
14499
14500 static dw_loc_descr_ref
14501 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14502 {
14503   dw_loc_descr_ref ret;
14504   dw_die_ref ref;
14505
14506   if (dwarf_strict)
14507     return NULL;
14508   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14509               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14510               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14511   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14512   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14513   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14514   if (ref)
14515     {
14516       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14517       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14518       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14519     }
14520   else
14521     {
14522       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14523       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14524     }
14525   return ret;
14526 }
14527
14528 /* Output a proper Dwarf location descriptor for a variable or parameter
14529    which is either allocated in a register or in a memory location.  For a
14530    register, we just generate an OP_REG and the register number.  For a
14531    memory location we provide a Dwarf postfix expression describing how to
14532    generate the (dynamic) address of the object onto the address stack.
14533
14534    MODE is mode of the decl if this loc_descriptor is going to be used in
14535    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14536    allowed, VOIDmode otherwise.
14537
14538    If we don't know how to describe it, return 0.  */
14539
14540 static dw_loc_descr_ref
14541 loc_descriptor (rtx rtl, enum machine_mode mode,
14542                 enum var_init_status initialized)
14543 {
14544   dw_loc_descr_ref loc_result = NULL;
14545
14546   switch (GET_CODE (rtl))
14547     {
14548     case SUBREG:
14549       /* The case of a subreg may arise when we have a local (register)
14550          variable or a formal (register) parameter which doesn't quite fill
14551          up an entire register.  For now, just assume that it is
14552          legitimate to make the Dwarf info refer to the whole register which
14553          contains the given subreg.  */
14554       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14555       break;
14556
14557     case REG:
14558       loc_result = reg_loc_descriptor (rtl, initialized);
14559       break;
14560
14561     case MEM:
14562       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14563                                        initialized);
14564       if (loc_result == NULL)
14565         loc_result = tls_mem_loc_descriptor (rtl);
14566       if (loc_result == NULL)
14567         {
14568           rtx new_rtl = avoid_constant_pool_reference (rtl);
14569           if (new_rtl != rtl)
14570             loc_result = loc_descriptor (new_rtl, mode, initialized);
14571         }
14572       break;
14573
14574     case CONCAT:
14575       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14576                                           initialized);
14577       break;
14578
14579     case CONCATN:
14580       loc_result = concatn_loc_descriptor (rtl, initialized);
14581       break;
14582
14583     case VAR_LOCATION:
14584       /* Single part.  */
14585       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14586         {
14587           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14588           if (GET_CODE (loc) == EXPR_LIST)
14589             loc = XEXP (loc, 0);
14590           loc_result = loc_descriptor (loc, mode, initialized);
14591           break;
14592         }
14593
14594       rtl = XEXP (rtl, 1);
14595       /* FALLTHRU */
14596
14597     case PARALLEL:
14598       {
14599         rtvec par_elems = XVEC (rtl, 0);
14600         int num_elem = GET_NUM_ELEM (par_elems);
14601         enum machine_mode mode;
14602         int i;
14603
14604         /* Create the first one, so we have something to add to.  */
14605         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14606                                      VOIDmode, initialized);
14607         if (loc_result == NULL)
14608           return NULL;
14609         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14610         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14611         for (i = 1; i < num_elem; i++)
14612           {
14613             dw_loc_descr_ref temp;
14614
14615             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14616                                    VOIDmode, initialized);
14617             if (temp == NULL)
14618               return NULL;
14619             add_loc_descr (&loc_result, temp);
14620             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14621             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14622           }
14623       }
14624       break;
14625
14626     case CONST_INT:
14627       if (mode != VOIDmode && mode != BLKmode)
14628         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14629                                                     INTVAL (rtl));
14630       break;
14631
14632     case CONST_DOUBLE:
14633       if (mode == VOIDmode)
14634         mode = GET_MODE (rtl);
14635
14636       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14637         {
14638           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14639
14640           /* Note that a CONST_DOUBLE rtx could represent either an integer
14641              or a floating-point constant.  A CONST_DOUBLE is used whenever
14642              the constant requires more than one word in order to be
14643              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14644           loc_result = new_loc_descr (DW_OP_implicit_value,
14645                                       GET_MODE_SIZE (mode), 0);
14646           if (SCALAR_FLOAT_MODE_P (mode))
14647             {
14648               unsigned int length = GET_MODE_SIZE (mode);
14649               unsigned char *array
14650                   = (unsigned char*) ggc_alloc_atomic (length);
14651
14652               insert_float (rtl, array);
14653               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14654               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14655               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14656               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14657             }
14658           else
14659             {
14660               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14661               loc_result->dw_loc_oprnd2.v.val_double
14662                 = rtx_to_double_int (rtl);
14663             }
14664         }
14665       break;
14666
14667     case CONST_VECTOR:
14668       if (mode == VOIDmode)
14669         mode = GET_MODE (rtl);
14670
14671       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14672         {
14673           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14674           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14675           unsigned char *array = (unsigned char *)
14676             ggc_alloc_atomic (length * elt_size);
14677           unsigned int i;
14678           unsigned char *p;
14679
14680           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14681           switch (GET_MODE_CLASS (mode))
14682             {
14683             case MODE_VECTOR_INT:
14684               for (i = 0, p = array; i < length; i++, p += elt_size)
14685                 {
14686                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14687                   double_int val = rtx_to_double_int (elt);
14688
14689                   if (elt_size <= sizeof (HOST_WIDE_INT))
14690                     insert_int (double_int_to_shwi (val), elt_size, p);
14691                   else
14692                     {
14693                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14694                       insert_double (val, p);
14695                     }
14696                 }
14697               break;
14698
14699             case MODE_VECTOR_FLOAT:
14700               for (i = 0, p = array; i < length; i++, p += elt_size)
14701                 {
14702                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14703                   insert_float (elt, p);
14704                 }
14705               break;
14706
14707             default:
14708               gcc_unreachable ();
14709             }
14710
14711           loc_result = new_loc_descr (DW_OP_implicit_value,
14712                                       length * elt_size, 0);
14713           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14714           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14715           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14716           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14717         }
14718       break;
14719
14720     case CONST:
14721       if (mode == VOIDmode
14722           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14723           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14724           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14725         {
14726           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14727           break;
14728         }
14729       /* FALLTHROUGH */
14730     case SYMBOL_REF:
14731       if (!const_ok_for_output (rtl))
14732         break;
14733     case LABEL_REF:
14734       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14735           && (dwarf_version >= 4 || !dwarf_strict))
14736         {
14737           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14738           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14739           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14740           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14741           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14742         }
14743       break;
14744
14745     case DEBUG_IMPLICIT_PTR:
14746       loc_result = implicit_ptr_descriptor (rtl, 0);
14747       break;
14748
14749     case PLUS:
14750       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14751           && CONST_INT_P (XEXP (rtl, 1)))
14752         {
14753           loc_result
14754             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14755           break;
14756         }
14757       /* FALLTHRU */
14758     default:
14759       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14760           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14761           && (dwarf_version >= 4 || !dwarf_strict))
14762         {
14763           /* Value expression.  */
14764           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14765           if (loc_result)
14766             add_loc_descr (&loc_result,
14767                            new_loc_descr (DW_OP_stack_value, 0, 0));
14768         }
14769       break;
14770     }
14771
14772   return loc_result;
14773 }
14774
14775 /* We need to figure out what section we should use as the base for the
14776    address ranges where a given location is valid.
14777    1. If this particular DECL has a section associated with it, use that.
14778    2. If this function has a section associated with it, use that.
14779    3. Otherwise, use the text section.
14780    XXX: If you split a variable across multiple sections, we won't notice.  */
14781
14782 static const char *
14783 secname_for_decl (const_tree decl)
14784 {
14785   const char *secname;
14786
14787   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14788     {
14789       tree sectree = DECL_SECTION_NAME (decl);
14790       secname = TREE_STRING_POINTER (sectree);
14791     }
14792   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14793     {
14794       tree sectree = DECL_SECTION_NAME (current_function_decl);
14795       secname = TREE_STRING_POINTER (sectree);
14796     }
14797   else if (cfun && in_cold_section_p)
14798     secname = crtl->subsections.cold_section_label;
14799   else
14800     secname = text_section_label;
14801
14802   return secname;
14803 }
14804
14805 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14806
14807 static bool
14808 decl_by_reference_p (tree decl)
14809 {
14810   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14811            || TREE_CODE (decl) == VAR_DECL)
14812           && DECL_BY_REFERENCE (decl));
14813 }
14814
14815 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14816    for VARLOC.  */
14817
14818 static dw_loc_descr_ref
14819 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14820                enum var_init_status initialized)
14821 {
14822   int have_address = 0;
14823   dw_loc_descr_ref descr;
14824   enum machine_mode mode;
14825
14826   if (want_address != 2)
14827     {
14828       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14829       /* Single part.  */
14830       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14831         {
14832           varloc = PAT_VAR_LOCATION_LOC (varloc);
14833           if (GET_CODE (varloc) == EXPR_LIST)
14834             varloc = XEXP (varloc, 0);
14835           mode = GET_MODE (varloc);
14836           if (MEM_P (varloc))
14837             {
14838               rtx addr = XEXP (varloc, 0);
14839               descr = mem_loc_descriptor (addr, mode, initialized);
14840               if (descr)
14841                 have_address = 1;
14842               else
14843                 {
14844                   rtx x = avoid_constant_pool_reference (varloc);
14845                   if (x != varloc)
14846                     descr = mem_loc_descriptor (x, mode, initialized);
14847                 }
14848             }
14849           else
14850             descr = mem_loc_descriptor (varloc, mode, initialized);
14851         }
14852       else
14853         return 0;
14854     }
14855   else
14856     {
14857       if (GET_CODE (varloc) == VAR_LOCATION)
14858         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14859       else
14860         mode = DECL_MODE (loc);
14861       descr = loc_descriptor (varloc, mode, initialized);
14862       have_address = 1;
14863     }
14864
14865   if (!descr)
14866     return 0;
14867
14868   if (want_address == 2 && !have_address
14869       && (dwarf_version >= 4 || !dwarf_strict))
14870     {
14871       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14872         {
14873           expansion_failed (loc, NULL_RTX,
14874                             "DWARF address size mismatch");
14875           return 0;
14876         }
14877       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14878       have_address = 1;
14879     }
14880   /* Show if we can't fill the request for an address.  */
14881   if (want_address && !have_address)
14882     {
14883       expansion_failed (loc, NULL_RTX,
14884                         "Want address and only have value");
14885       return 0;
14886     }
14887
14888   /* If we've got an address and don't want one, dereference.  */
14889   if (!want_address && have_address)
14890     {
14891       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14892       enum dwarf_location_atom op;
14893
14894       if (size > DWARF2_ADDR_SIZE || size == -1)
14895         {
14896           expansion_failed (loc, NULL_RTX,
14897                             "DWARF address size mismatch");
14898           return 0;
14899         }
14900       else if (size == DWARF2_ADDR_SIZE)
14901         op = DW_OP_deref;
14902       else
14903         op = DW_OP_deref_size;
14904
14905       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14906     }
14907
14908   return descr;
14909 }
14910
14911 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14912    if it is not possible.  */
14913
14914 static dw_loc_descr_ref
14915 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14916 {
14917   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14918     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14919   else if (dwarf_version >= 3 || !dwarf_strict)
14920     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14921   else
14922     return NULL;
14923 }
14924
14925 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14926    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14927
14928 static dw_loc_descr_ref
14929 dw_sra_loc_expr (tree decl, rtx loc)
14930 {
14931   rtx p;
14932   unsigned int padsize = 0;
14933   dw_loc_descr_ref descr, *descr_tail;
14934   unsigned HOST_WIDE_INT decl_size;
14935   rtx varloc;
14936   enum var_init_status initialized;
14937
14938   if (DECL_SIZE (decl) == NULL
14939       || !host_integerp (DECL_SIZE (decl), 1))
14940     return NULL;
14941
14942   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14943   descr = NULL;
14944   descr_tail = &descr;
14945
14946   for (p = loc; p; p = XEXP (p, 1))
14947     {
14948       unsigned int bitsize = decl_piece_bitsize (p);
14949       rtx loc_note = *decl_piece_varloc_ptr (p);
14950       dw_loc_descr_ref cur_descr;
14951       dw_loc_descr_ref *tail, last = NULL;
14952       unsigned int opsize = 0;
14953
14954       if (loc_note == NULL_RTX
14955           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14956         {
14957           padsize += bitsize;
14958           continue;
14959         }
14960       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14961       varloc = NOTE_VAR_LOCATION (loc_note);
14962       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14963       if (cur_descr == NULL)
14964         {
14965           padsize += bitsize;
14966           continue;
14967         }
14968
14969       /* Check that cur_descr either doesn't use
14970          DW_OP_*piece operations, or their sum is equal
14971          to bitsize.  Otherwise we can't embed it.  */
14972       for (tail = &cur_descr; *tail != NULL;
14973            tail = &(*tail)->dw_loc_next)
14974         if ((*tail)->dw_loc_opc == DW_OP_piece)
14975           {
14976             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14977                       * BITS_PER_UNIT;
14978             last = *tail;
14979           }
14980         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14981           {
14982             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14983             last = *tail;
14984           }
14985
14986       if (last != NULL && opsize != bitsize)
14987         {
14988           padsize += bitsize;
14989           continue;
14990         }
14991
14992       /* If there is a hole, add DW_OP_*piece after empty DWARF
14993          expression, which means that those bits are optimized out.  */
14994       if (padsize)
14995         {
14996           if (padsize > decl_size)
14997             return NULL;
14998           decl_size -= padsize;
14999           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15000           if (*descr_tail == NULL)
15001             return NULL;
15002           descr_tail = &(*descr_tail)->dw_loc_next;
15003           padsize = 0;
15004         }
15005       *descr_tail = cur_descr;
15006       descr_tail = tail;
15007       if (bitsize > decl_size)
15008         return NULL;
15009       decl_size -= bitsize;
15010       if (last == NULL)
15011         {
15012           HOST_WIDE_INT offset = 0;
15013           if (GET_CODE (varloc) == VAR_LOCATION
15014               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15015             {
15016               varloc = PAT_VAR_LOCATION_LOC (varloc);
15017               if (GET_CODE (varloc) == EXPR_LIST)
15018                 varloc = XEXP (varloc, 0);
15019             }
15020           do 
15021             {
15022               if (GET_CODE (varloc) == CONST
15023                   || GET_CODE (varloc) == SIGN_EXTEND
15024                   || GET_CODE (varloc) == ZERO_EXTEND)
15025                 varloc = XEXP (varloc, 0);
15026               else if (GET_CODE (varloc) == SUBREG)
15027                 varloc = SUBREG_REG (varloc);
15028               else
15029                 break;
15030             }
15031           while (1);
15032           /* DW_OP_bit_size offset should be zero for register
15033              or implicit location descriptions and empty location
15034              descriptions, but for memory addresses needs big endian
15035              adjustment.  */
15036           if (MEM_P (varloc))
15037             {
15038               unsigned HOST_WIDE_INT memsize
15039                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15040               if (memsize != bitsize)
15041                 {
15042                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15043                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15044                     return NULL;
15045                   if (memsize < bitsize)
15046                     return NULL;
15047                   if (BITS_BIG_ENDIAN)
15048                     offset = memsize - bitsize;
15049                 }
15050             }
15051
15052           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15053           if (*descr_tail == NULL)
15054             return NULL;
15055           descr_tail = &(*descr_tail)->dw_loc_next;
15056         }
15057     }
15058
15059   /* If there were any non-empty expressions, add padding till the end of
15060      the decl.  */
15061   if (descr != NULL && decl_size != 0)
15062     {
15063       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15064       if (*descr_tail == NULL)
15065         return NULL;
15066     }
15067   return descr;
15068 }
15069
15070 /* Return the dwarf representation of the location list LOC_LIST of
15071    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15072    function.  */
15073
15074 static dw_loc_list_ref
15075 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15076 {
15077   const char *endname, *secname;
15078   rtx varloc;
15079   enum var_init_status initialized;
15080   struct var_loc_node *node;
15081   dw_loc_descr_ref descr;
15082   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15083   dw_loc_list_ref list = NULL;
15084   dw_loc_list_ref *listp = &list;
15085
15086   /* Now that we know what section we are using for a base,
15087      actually construct the list of locations.
15088      The first location information is what is passed to the
15089      function that creates the location list, and the remaining
15090      locations just get added on to that list.
15091      Note that we only know the start address for a location
15092      (IE location changes), so to build the range, we use
15093      the range [current location start, next location start].
15094      This means we have to special case the last node, and generate
15095      a range of [last location start, end of function label].  */
15096
15097   secname = secname_for_decl (decl);
15098
15099   for (node = loc_list->first; node; node = node->next)
15100     if (GET_CODE (node->loc) == EXPR_LIST
15101         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15102       {
15103         if (GET_CODE (node->loc) == EXPR_LIST)
15104           {
15105             /* This requires DW_OP_{,bit_}piece, which is not usable
15106                inside DWARF expressions.  */
15107             if (want_address != 2)
15108               continue;
15109             descr = dw_sra_loc_expr (decl, node->loc);
15110             if (descr == NULL)
15111               continue;
15112           }
15113         else
15114           {
15115             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15116             varloc = NOTE_VAR_LOCATION (node->loc);
15117             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15118           }
15119         if (descr)
15120           {
15121             /* The variable has a location between NODE->LABEL and
15122                NODE->NEXT->LABEL.  */
15123             if (node->next)
15124               endname = node->next->label;
15125             /* If the variable has a location at the last label
15126                it keeps its location until the end of function.  */
15127             else if (!current_function_decl)
15128               endname = text_end_label;
15129             else
15130               {
15131                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15132                                              current_function_funcdef_no);
15133                 endname = ggc_strdup (label_id);
15134               }
15135
15136             *listp = new_loc_list (descr, node->label, endname, secname);
15137             listp = &(*listp)->dw_loc_next;
15138           }
15139       }
15140
15141   /* Try to avoid the overhead of a location list emitting a location
15142      expression instead, but only if we didn't have more than one
15143      location entry in the first place.  If some entries were not
15144      representable, we don't want to pretend a single entry that was
15145      applies to the entire scope in which the variable is
15146      available.  */
15147   if (list && loc_list->first->next)
15148     gen_llsym (list);
15149
15150   return list;
15151 }
15152
15153 /* Return if the loc_list has only single element and thus can be represented
15154    as location description.   */
15155
15156 static bool
15157 single_element_loc_list_p (dw_loc_list_ref list)
15158 {
15159   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15160   return !list->ll_symbol;
15161 }
15162
15163 /* To each location in list LIST add loc descr REF.  */
15164
15165 static void
15166 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15167 {
15168   dw_loc_descr_ref copy;
15169   add_loc_descr (&list->expr, ref);
15170   list = list->dw_loc_next;
15171   while (list)
15172     {
15173       copy = ggc_alloc_dw_loc_descr_node ();
15174       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15175       add_loc_descr (&list->expr, copy);
15176       while (copy->dw_loc_next)
15177         {
15178           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15179           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15180           copy->dw_loc_next = new_copy;
15181           copy = new_copy;
15182         }
15183       list = list->dw_loc_next;
15184     }
15185 }
15186
15187 /* Given two lists RET and LIST
15188    produce location list that is result of adding expression in LIST
15189    to expression in RET on each possition in program.
15190    Might be destructive on both RET and LIST.
15191
15192    TODO: We handle only simple cases of RET or LIST having at most one
15193    element. General case would inolve sorting the lists in program order
15194    and merging them that will need some additional work.
15195    Adding that will improve quality of debug info especially for SRA-ed
15196    structures.  */
15197
15198 static void
15199 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15200 {
15201   if (!list)
15202     return;
15203   if (!*ret)
15204     {
15205       *ret = list;
15206       return;
15207     }
15208   if (!list->dw_loc_next)
15209     {
15210       add_loc_descr_to_each (*ret, list->expr);
15211       return;
15212     }
15213   if (!(*ret)->dw_loc_next)
15214     {
15215       add_loc_descr_to_each (list, (*ret)->expr);
15216       *ret = list;
15217       return;
15218     }
15219   expansion_failed (NULL_TREE, NULL_RTX,
15220                     "Don't know how to merge two non-trivial"
15221                     " location lists.\n");
15222   *ret = NULL;
15223   return;
15224 }
15225
15226 /* LOC is constant expression.  Try a luck, look it up in constant
15227    pool and return its loc_descr of its address.  */
15228
15229 static dw_loc_descr_ref
15230 cst_pool_loc_descr (tree loc)
15231 {
15232   /* Get an RTL for this, if something has been emitted.  */
15233   rtx rtl = lookup_constant_def (loc);
15234   enum machine_mode mode;
15235
15236   if (!rtl || !MEM_P (rtl))
15237     {
15238       gcc_assert (!rtl);
15239       return 0;
15240     }
15241   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15242
15243   /* TODO: We might get more coverage if we was actually delaying expansion
15244      of all expressions till end of compilation when constant pools are fully
15245      populated.  */
15246   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15247     {
15248       expansion_failed (loc, NULL_RTX,
15249                         "CST value in contant pool but not marked.");
15250       return 0;
15251     }
15252   mode = GET_MODE (rtl);
15253   rtl = XEXP (rtl, 0);
15254   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15255 }
15256
15257 /* Return dw_loc_list representing address of addr_expr LOC
15258    by looking for innder INDIRECT_REF expression and turing it
15259    into simple arithmetics.  */
15260
15261 static dw_loc_list_ref
15262 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15263 {
15264   tree obj, offset;
15265   HOST_WIDE_INT bitsize, bitpos, bytepos;
15266   enum machine_mode mode;
15267   int volatilep;
15268   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15269   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15270
15271   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15272                              &bitsize, &bitpos, &offset, &mode,
15273                              &unsignedp, &volatilep, false);
15274   STRIP_NOPS (obj);
15275   if (bitpos % BITS_PER_UNIT)
15276     {
15277       expansion_failed (loc, NULL_RTX, "bitfield access");
15278       return 0;
15279     }
15280   if (!INDIRECT_REF_P (obj))
15281     {
15282       expansion_failed (obj,
15283                         NULL_RTX, "no indirect ref in inner refrence");
15284       return 0;
15285     }
15286   if (!offset && !bitpos)
15287     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15288   else if (toplev
15289            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15290            && (dwarf_version >= 4 || !dwarf_strict))
15291     {
15292       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15293       if (!list_ret)
15294         return 0;
15295       if (offset)
15296         {
15297           /* Variable offset.  */
15298           list_ret1 = loc_list_from_tree (offset, 0);
15299           if (list_ret1 == 0)
15300             return 0;
15301           add_loc_list (&list_ret, list_ret1);
15302           if (!list_ret)
15303             return 0;
15304           add_loc_descr_to_each (list_ret,
15305                                  new_loc_descr (DW_OP_plus, 0, 0));
15306         }
15307       bytepos = bitpos / BITS_PER_UNIT;
15308       if (bytepos > 0)
15309         add_loc_descr_to_each (list_ret,
15310                                new_loc_descr (DW_OP_plus_uconst,
15311                                               bytepos, 0));
15312       else if (bytepos < 0)
15313         loc_list_plus_const (list_ret, bytepos);
15314       add_loc_descr_to_each (list_ret,
15315                              new_loc_descr (DW_OP_stack_value, 0, 0));
15316     }
15317   return list_ret;
15318 }
15319
15320
15321 /* Generate Dwarf location list representing LOC.
15322    If WANT_ADDRESS is false, expression computing LOC will be computed
15323    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15324    if WANT_ADDRESS is 2, expression computing address useable in location
15325      will be returned (i.e. DW_OP_reg can be used
15326      to refer to register values).  */
15327
15328 static dw_loc_list_ref
15329 loc_list_from_tree (tree loc, int want_address)
15330 {
15331   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15332   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15333   int have_address = 0;
15334   enum dwarf_location_atom op;
15335
15336   /* ??? Most of the time we do not take proper care for sign/zero
15337      extending the values properly.  Hopefully this won't be a real
15338      problem...  */
15339
15340   switch (TREE_CODE (loc))
15341     {
15342     case ERROR_MARK:
15343       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15344       return 0;
15345
15346     case PLACEHOLDER_EXPR:
15347       /* This case involves extracting fields from an object to determine the
15348          position of other fields.  We don't try to encode this here.  The
15349          only user of this is Ada, which encodes the needed information using
15350          the names of types.  */
15351       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15352       return 0;
15353
15354     case CALL_EXPR:
15355       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15356       /* There are no opcodes for these operations.  */
15357       return 0;
15358
15359     case PREINCREMENT_EXPR:
15360     case PREDECREMENT_EXPR:
15361     case POSTINCREMENT_EXPR:
15362     case POSTDECREMENT_EXPR:
15363       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15364       /* There are no opcodes for these operations.  */
15365       return 0;
15366
15367     case ADDR_EXPR:
15368       /* If we already want an address, see if there is INDIRECT_REF inside
15369          e.g. for &this->field.  */
15370       if (want_address)
15371         {
15372           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15373                        (loc, want_address == 2);
15374           if (list_ret)
15375             have_address = 1;
15376           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15377                    && (ret = cst_pool_loc_descr (loc)))
15378             have_address = 1;
15379         }
15380         /* Otherwise, process the argument and look for the address.  */
15381       if (!list_ret && !ret)
15382         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15383       else
15384         {
15385           if (want_address)
15386             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15387           return NULL;
15388         }
15389       break;
15390
15391     case VAR_DECL:
15392       if (DECL_THREAD_LOCAL_P (loc))
15393         {
15394           rtx rtl;
15395           enum dwarf_location_atom first_op;
15396           enum dwarf_location_atom second_op;
15397           bool dtprel = false;
15398
15399           if (targetm.have_tls)
15400             {
15401               /* If this is not defined, we have no way to emit the
15402                  data.  */
15403               if (!targetm.asm_out.output_dwarf_dtprel)
15404                 return 0;
15405
15406                /* The way DW_OP_GNU_push_tls_address is specified, we
15407                   can only look up addresses of objects in the current
15408                   module.  We used DW_OP_addr as first op, but that's
15409                   wrong, because DW_OP_addr is relocated by the debug
15410                   info consumer, while DW_OP_GNU_push_tls_address
15411                   operand shouldn't be.  */
15412               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15413                 return 0;
15414               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15415               dtprel = true;
15416               second_op = DW_OP_GNU_push_tls_address;
15417             }
15418           else
15419             {
15420               if (!targetm.emutls.debug_form_tls_address
15421                   || !(dwarf_version >= 3 || !dwarf_strict))
15422                 return 0;
15423               /* We stuffed the control variable into the DECL_VALUE_EXPR
15424                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15425                  no longer appear in gimple code.  We used the control
15426                  variable in specific so that we could pick it up here.  */
15427               loc = DECL_VALUE_EXPR (loc);
15428               first_op = DW_OP_addr;
15429               second_op = DW_OP_form_tls_address;
15430             }
15431
15432           rtl = rtl_for_decl_location (loc);
15433           if (rtl == NULL_RTX)
15434             return 0;
15435
15436           if (!MEM_P (rtl))
15437             return 0;
15438           rtl = XEXP (rtl, 0);
15439           if (! CONSTANT_P (rtl))
15440             return 0;
15441
15442           ret = new_loc_descr (first_op, 0, 0);
15443           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15444           ret->dw_loc_oprnd1.v.val_addr = rtl;
15445           ret->dtprel = dtprel;
15446
15447           ret1 = new_loc_descr (second_op, 0, 0);
15448           add_loc_descr (&ret, ret1);
15449
15450           have_address = 1;
15451           break;
15452         }
15453       /* FALLTHRU */
15454
15455     case PARM_DECL:
15456     case RESULT_DECL:
15457       if (DECL_HAS_VALUE_EXPR_P (loc))
15458         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15459                                    want_address);
15460       /* FALLTHRU */
15461
15462     case FUNCTION_DECL:
15463       {
15464         rtx rtl;
15465         var_loc_list *loc_list = lookup_decl_loc (loc);
15466
15467         if (loc_list && loc_list->first)
15468           {
15469             list_ret = dw_loc_list (loc_list, loc, want_address);
15470             have_address = want_address != 0;
15471             break;
15472           }
15473         rtl = rtl_for_decl_location (loc);
15474         if (rtl == NULL_RTX)
15475           {
15476             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15477             return 0;
15478           }
15479         else if (CONST_INT_P (rtl))
15480           {
15481             HOST_WIDE_INT val = INTVAL (rtl);
15482             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15483               val &= GET_MODE_MASK (DECL_MODE (loc));
15484             ret = int_loc_descriptor (val);
15485           }
15486         else if (GET_CODE (rtl) == CONST_STRING)
15487           {
15488             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15489             return 0;
15490           }
15491         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15492           {
15493             ret = new_loc_descr (DW_OP_addr, 0, 0);
15494             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15495             ret->dw_loc_oprnd1.v.val_addr = rtl;
15496           }
15497         else
15498           {
15499             enum machine_mode mode;
15500
15501             /* Certain constructs can only be represented at top-level.  */
15502             if (want_address == 2)
15503               {
15504                 ret = loc_descriptor (rtl, VOIDmode,
15505                                       VAR_INIT_STATUS_INITIALIZED);
15506                 have_address = 1;
15507               }
15508             else
15509               {
15510                 mode = GET_MODE (rtl);
15511                 if (MEM_P (rtl))
15512                   {
15513                     rtl = XEXP (rtl, 0);
15514                     have_address = 1;
15515                   }
15516                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15517               }
15518             if (!ret)
15519               expansion_failed (loc, rtl,
15520                                 "failed to produce loc descriptor for rtl");
15521           }
15522       }
15523       break;
15524
15525     case MEM_REF:
15526       /* ??? FIXME.  */
15527       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15528         return 0;
15529       /* Fallthru.  */
15530     case INDIRECT_REF:
15531       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15532       have_address = 1;
15533       break;
15534
15535     case COMPOUND_EXPR:
15536       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15537
15538     CASE_CONVERT:
15539     case VIEW_CONVERT_EXPR:
15540     case SAVE_EXPR:
15541     case MODIFY_EXPR:
15542       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15543
15544     case COMPONENT_REF:
15545     case BIT_FIELD_REF:
15546     case ARRAY_REF:
15547     case ARRAY_RANGE_REF:
15548     case REALPART_EXPR:
15549     case IMAGPART_EXPR:
15550       {
15551         tree obj, offset;
15552         HOST_WIDE_INT bitsize, bitpos, bytepos;
15553         enum machine_mode mode;
15554         int volatilep;
15555         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15556
15557         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15558                                    &unsignedp, &volatilep, false);
15559
15560         gcc_assert (obj != loc);
15561
15562         list_ret = loc_list_from_tree (obj,
15563                                        want_address == 2
15564                                        && !bitpos && !offset ? 2 : 1);
15565         /* TODO: We can extract value of the small expression via shifting even
15566            for nonzero bitpos.  */
15567         if (list_ret == 0)
15568           return 0;
15569         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15570           {
15571             expansion_failed (loc, NULL_RTX,
15572                               "bitfield access");
15573             return 0;
15574           }
15575
15576         if (offset != NULL_TREE)
15577           {
15578             /* Variable offset.  */
15579             list_ret1 = loc_list_from_tree (offset, 0);
15580             if (list_ret1 == 0)
15581               return 0;
15582             add_loc_list (&list_ret, list_ret1);
15583             if (!list_ret)
15584               return 0;
15585             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15586           }
15587
15588         bytepos = bitpos / BITS_PER_UNIT;
15589         if (bytepos > 0)
15590           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15591         else if (bytepos < 0)
15592           loc_list_plus_const (list_ret, bytepos);
15593
15594         have_address = 1;
15595         break;
15596       }
15597
15598     case INTEGER_CST:
15599       if ((want_address || !host_integerp (loc, 0))
15600           && (ret = cst_pool_loc_descr (loc)))
15601         have_address = 1;
15602       else if (want_address == 2
15603                && host_integerp (loc, 0)
15604                && (ret = address_of_int_loc_descriptor
15605                            (int_size_in_bytes (TREE_TYPE (loc)),
15606                             tree_low_cst (loc, 0))))
15607         have_address = 1;
15608       else if (host_integerp (loc, 0))
15609         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15610       else
15611         {
15612           expansion_failed (loc, NULL_RTX,
15613                             "Integer operand is not host integer");
15614           return 0;
15615         }
15616       break;
15617
15618     case CONSTRUCTOR:
15619     case REAL_CST:
15620     case STRING_CST:
15621     case COMPLEX_CST:
15622       if ((ret = cst_pool_loc_descr (loc)))
15623         have_address = 1;
15624       else
15625       /* We can construct small constants here using int_loc_descriptor.  */
15626         expansion_failed (loc, NULL_RTX,
15627                           "constructor or constant not in constant pool");
15628       break;
15629
15630     case TRUTH_AND_EXPR:
15631     case TRUTH_ANDIF_EXPR:
15632     case BIT_AND_EXPR:
15633       op = DW_OP_and;
15634       goto do_binop;
15635
15636     case TRUTH_XOR_EXPR:
15637     case BIT_XOR_EXPR:
15638       op = DW_OP_xor;
15639       goto do_binop;
15640
15641     case TRUTH_OR_EXPR:
15642     case TRUTH_ORIF_EXPR:
15643     case BIT_IOR_EXPR:
15644       op = DW_OP_or;
15645       goto do_binop;
15646
15647     case FLOOR_DIV_EXPR:
15648     case CEIL_DIV_EXPR:
15649     case ROUND_DIV_EXPR:
15650     case TRUNC_DIV_EXPR:
15651       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15652         return 0;
15653       op = DW_OP_div;
15654       goto do_binop;
15655
15656     case MINUS_EXPR:
15657       op = DW_OP_minus;
15658       goto do_binop;
15659
15660     case FLOOR_MOD_EXPR:
15661     case CEIL_MOD_EXPR:
15662     case ROUND_MOD_EXPR:
15663     case TRUNC_MOD_EXPR:
15664       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15665         {
15666           op = DW_OP_mod;
15667           goto do_binop;
15668         }
15669       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15670       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15671       if (list_ret == 0 || list_ret1 == 0)
15672         return 0;
15673
15674       add_loc_list (&list_ret, list_ret1);
15675       if (list_ret == 0)
15676         return 0;
15677       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15678       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15679       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15680       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15681       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15682       break;
15683
15684     case MULT_EXPR:
15685       op = DW_OP_mul;
15686       goto do_binop;
15687
15688     case LSHIFT_EXPR:
15689       op = DW_OP_shl;
15690       goto do_binop;
15691
15692     case RSHIFT_EXPR:
15693       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15694       goto do_binop;
15695
15696     case POINTER_PLUS_EXPR:
15697     case PLUS_EXPR:
15698       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15699         {
15700           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15701           if (list_ret == 0)
15702             return 0;
15703
15704           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15705           break;
15706         }
15707
15708       op = DW_OP_plus;
15709       goto do_binop;
15710
15711     case LE_EXPR:
15712       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15713         return 0;
15714
15715       op = DW_OP_le;
15716       goto do_binop;
15717
15718     case GE_EXPR:
15719       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15720         return 0;
15721
15722       op = DW_OP_ge;
15723       goto do_binop;
15724
15725     case LT_EXPR:
15726       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15727         return 0;
15728
15729       op = DW_OP_lt;
15730       goto do_binop;
15731
15732     case GT_EXPR:
15733       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15734         return 0;
15735
15736       op = DW_OP_gt;
15737       goto do_binop;
15738
15739     case EQ_EXPR:
15740       op = DW_OP_eq;
15741       goto do_binop;
15742
15743     case NE_EXPR:
15744       op = DW_OP_ne;
15745       goto do_binop;
15746
15747     do_binop:
15748       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15749       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15750       if (list_ret == 0 || list_ret1 == 0)
15751         return 0;
15752
15753       add_loc_list (&list_ret, list_ret1);
15754       if (list_ret == 0)
15755         return 0;
15756       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15757       break;
15758
15759     case TRUTH_NOT_EXPR:
15760     case BIT_NOT_EXPR:
15761       op = DW_OP_not;
15762       goto do_unop;
15763
15764     case ABS_EXPR:
15765       op = DW_OP_abs;
15766       goto do_unop;
15767
15768     case NEGATE_EXPR:
15769       op = DW_OP_neg;
15770       goto do_unop;
15771
15772     do_unop:
15773       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15774       if (list_ret == 0)
15775         return 0;
15776
15777       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15778       break;
15779
15780     case MIN_EXPR:
15781     case MAX_EXPR:
15782       {
15783         const enum tree_code code =
15784           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15785
15786         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15787                       build2 (code, integer_type_node,
15788                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15789                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15790       }
15791
15792       /* ... fall through ...  */
15793
15794     case COND_EXPR:
15795       {
15796         dw_loc_descr_ref lhs
15797           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15798         dw_loc_list_ref rhs
15799           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15800         dw_loc_descr_ref bra_node, jump_node, tmp;
15801
15802         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15803         if (list_ret == 0 || lhs == 0 || rhs == 0)
15804           return 0;
15805
15806         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15807         add_loc_descr_to_each (list_ret, bra_node);
15808
15809         add_loc_list (&list_ret, rhs);
15810         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15811         add_loc_descr_to_each (list_ret, jump_node);
15812
15813         add_loc_descr_to_each (list_ret, lhs);
15814         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15815         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15816
15817         /* ??? Need a node to point the skip at.  Use a nop.  */
15818         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15819         add_loc_descr_to_each (list_ret, tmp);
15820         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15821         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15822       }
15823       break;
15824
15825     case FIX_TRUNC_EXPR:
15826       return 0;
15827
15828     default:
15829       /* Leave front-end specific codes as simply unknown.  This comes
15830          up, for instance, with the C STMT_EXPR.  */
15831       if ((unsigned int) TREE_CODE (loc)
15832           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15833         {
15834           expansion_failed (loc, NULL_RTX,
15835                             "language specific tree node");
15836           return 0;
15837         }
15838
15839 #ifdef ENABLE_CHECKING
15840       /* Otherwise this is a generic code; we should just lists all of
15841          these explicitly.  We forgot one.  */
15842       gcc_unreachable ();
15843 #else
15844       /* In a release build, we want to degrade gracefully: better to
15845          generate incomplete debugging information than to crash.  */
15846       return NULL;
15847 #endif
15848     }
15849
15850   if (!ret && !list_ret)
15851     return 0;
15852
15853   if (want_address == 2 && !have_address
15854       && (dwarf_version >= 4 || !dwarf_strict))
15855     {
15856       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15857         {
15858           expansion_failed (loc, NULL_RTX,
15859                             "DWARF address size mismatch");
15860           return 0;
15861         }
15862       if (ret)
15863         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15864       else
15865         add_loc_descr_to_each (list_ret,
15866                                new_loc_descr (DW_OP_stack_value, 0, 0));
15867       have_address = 1;
15868     }
15869   /* Show if we can't fill the request for an address.  */
15870   if (want_address && !have_address)
15871     {
15872       expansion_failed (loc, NULL_RTX,
15873                         "Want address and only have value");
15874       return 0;
15875     }
15876
15877   gcc_assert (!ret || !list_ret);
15878
15879   /* If we've got an address and don't want one, dereference.  */
15880   if (!want_address && have_address)
15881     {
15882       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15883
15884       if (size > DWARF2_ADDR_SIZE || size == -1)
15885         {
15886           expansion_failed (loc, NULL_RTX,
15887                             "DWARF address size mismatch");
15888           return 0;
15889         }
15890       else if (size == DWARF2_ADDR_SIZE)
15891         op = DW_OP_deref;
15892       else
15893         op = DW_OP_deref_size;
15894
15895       if (ret)
15896         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15897       else
15898         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15899     }
15900   if (ret)
15901     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15902
15903   return list_ret;
15904 }
15905
15906 /* Same as above but return only single location expression.  */
15907 static dw_loc_descr_ref
15908 loc_descriptor_from_tree (tree loc, int want_address)
15909 {
15910   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15911   if (!ret)
15912     return NULL;
15913   if (ret->dw_loc_next)
15914     {
15915       expansion_failed (loc, NULL_RTX,
15916                         "Location list where only loc descriptor needed");
15917       return NULL;
15918     }
15919   return ret->expr;
15920 }
15921
15922 /* Given a value, round it up to the lowest multiple of `boundary'
15923    which is not less than the value itself.  */
15924
15925 static inline HOST_WIDE_INT
15926 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15927 {
15928   return (((value + boundary - 1) / boundary) * boundary);
15929 }
15930
15931 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15932    pointer to the declared type for the relevant field variable, or return
15933    `integer_type_node' if the given node turns out to be an
15934    ERROR_MARK node.  */
15935
15936 static inline tree
15937 field_type (const_tree decl)
15938 {
15939   tree type;
15940
15941   if (TREE_CODE (decl) == ERROR_MARK)
15942     return integer_type_node;
15943
15944   type = DECL_BIT_FIELD_TYPE (decl);
15945   if (type == NULL_TREE)
15946     type = TREE_TYPE (decl);
15947
15948   return type;
15949 }
15950
15951 /* Given a pointer to a tree node, return the alignment in bits for
15952    it, or else return BITS_PER_WORD if the node actually turns out to
15953    be an ERROR_MARK node.  */
15954
15955 static inline unsigned
15956 simple_type_align_in_bits (const_tree type)
15957 {
15958   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15959 }
15960
15961 static inline unsigned
15962 simple_decl_align_in_bits (const_tree decl)
15963 {
15964   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15965 }
15966
15967 /* Return the result of rounding T up to ALIGN.  */
15968
15969 static inline double_int
15970 round_up_to_align (double_int t, unsigned int align)
15971 {
15972   double_int alignd = uhwi_to_double_int (align);
15973   t = double_int_add (t, alignd);
15974   t = double_int_add (t, double_int_minus_one);
15975   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15976   t = double_int_mul (t, alignd);
15977   return t;
15978 }
15979
15980 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15981    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15982    or return 0 if we are unable to determine what that offset is, either
15983    because the argument turns out to be a pointer to an ERROR_MARK node, or
15984    because the offset is actually variable.  (We can't handle the latter case
15985    just yet).  */
15986
15987 static HOST_WIDE_INT
15988 field_byte_offset (const_tree decl)
15989 {
15990   double_int object_offset_in_bits;
15991   double_int object_offset_in_bytes;
15992   double_int bitpos_int;
15993
15994   if (TREE_CODE (decl) == ERROR_MARK)
15995     return 0;
15996
15997   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15998
15999   /* We cannot yet cope with fields whose positions are variable, so
16000      for now, when we see such things, we simply return 0.  Someday, we may
16001      be able to handle such cases, but it will be damn difficult.  */
16002   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16003     return 0;
16004
16005   bitpos_int = tree_to_double_int (bit_position (decl));
16006
16007 #ifdef PCC_BITFIELD_TYPE_MATTERS
16008   if (PCC_BITFIELD_TYPE_MATTERS)
16009     {
16010       tree type;
16011       tree field_size_tree;
16012       double_int deepest_bitpos;
16013       double_int field_size_in_bits;
16014       unsigned int type_align_in_bits;
16015       unsigned int decl_align_in_bits;
16016       double_int type_size_in_bits;
16017
16018       type = field_type (decl);
16019       type_size_in_bits = double_int_type_size_in_bits (type);
16020       type_align_in_bits = simple_type_align_in_bits (type);
16021
16022       field_size_tree = DECL_SIZE (decl);
16023
16024       /* The size could be unspecified if there was an error, or for
16025          a flexible array member.  */
16026       if (!field_size_tree)
16027         field_size_tree = bitsize_zero_node;
16028
16029       /* If the size of the field is not constant, use the type size.  */
16030       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16031         field_size_in_bits = tree_to_double_int (field_size_tree);
16032       else
16033         field_size_in_bits = type_size_in_bits;
16034
16035       decl_align_in_bits = simple_decl_align_in_bits (decl);
16036
16037       /* The GCC front-end doesn't make any attempt to keep track of the
16038          starting bit offset (relative to the start of the containing
16039          structure type) of the hypothetical "containing object" for a
16040          bit-field.  Thus, when computing the byte offset value for the
16041          start of the "containing object" of a bit-field, we must deduce
16042          this information on our own. This can be rather tricky to do in
16043          some cases.  For example, handling the following structure type
16044          definition when compiling for an i386/i486 target (which only
16045          aligns long long's to 32-bit boundaries) can be very tricky:
16046
16047          struct S { int field1; long long field2:31; };
16048
16049          Fortunately, there is a simple rule-of-thumb which can be used
16050          in such cases.  When compiling for an i386/i486, GCC will
16051          allocate 8 bytes for the structure shown above.  It decides to
16052          do this based upon one simple rule for bit-field allocation.
16053          GCC allocates each "containing object" for each bit-field at
16054          the first (i.e. lowest addressed) legitimate alignment boundary
16055          (based upon the required minimum alignment for the declared
16056          type of the field) which it can possibly use, subject to the
16057          condition that there is still enough available space remaining
16058          in the containing object (when allocated at the selected point)
16059          to fully accommodate all of the bits of the bit-field itself.
16060
16061          This simple rule makes it obvious why GCC allocates 8 bytes for
16062          each object of the structure type shown above.  When looking
16063          for a place to allocate the "containing object" for `field2',
16064          the compiler simply tries to allocate a 64-bit "containing
16065          object" at each successive 32-bit boundary (starting at zero)
16066          until it finds a place to allocate that 64- bit field such that
16067          at least 31 contiguous (and previously unallocated) bits remain
16068          within that selected 64 bit field.  (As it turns out, for the
16069          example above, the compiler finds it is OK to allocate the
16070          "containing object" 64-bit field at bit-offset zero within the
16071          structure type.)
16072
16073          Here we attempt to work backwards from the limited set of facts
16074          we're given, and we try to deduce from those facts, where GCC
16075          must have believed that the containing object started (within
16076          the structure type). The value we deduce is then used (by the
16077          callers of this routine) to generate DW_AT_location and
16078          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16079          the case of DW_AT_location, regular fields as well).  */
16080
16081       /* Figure out the bit-distance from the start of the structure to
16082          the "deepest" bit of the bit-field.  */
16083       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16084
16085       /* This is the tricky part.  Use some fancy footwork to deduce
16086          where the lowest addressed bit of the containing object must
16087          be.  */
16088       object_offset_in_bits
16089         = double_int_sub (deepest_bitpos, type_size_in_bits);
16090
16091       /* Round up to type_align by default.  This works best for
16092          bitfields.  */
16093       object_offset_in_bits
16094         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16095
16096       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16097         {
16098           object_offset_in_bits
16099             = double_int_sub (deepest_bitpos, type_size_in_bits);
16100
16101           /* Round up to decl_align instead.  */
16102           object_offset_in_bits
16103             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16104         }
16105     }
16106   else
16107 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16108     object_offset_in_bits = bitpos_int;
16109
16110   object_offset_in_bytes
16111     = double_int_div (object_offset_in_bits,
16112                       uhwi_to_double_int (BITS_PER_UNIT), true,
16113                       TRUNC_DIV_EXPR);
16114   return double_int_to_shwi (object_offset_in_bytes);
16115 }
16116 \f
16117 /* The following routines define various Dwarf attributes and any data
16118    associated with them.  */
16119
16120 /* Add a location description attribute value to a DIE.
16121
16122    This emits location attributes suitable for whole variables and
16123    whole parameters.  Note that the location attributes for struct fields are
16124    generated by the routine `data_member_location_attribute' below.  */
16125
16126 static inline void
16127 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16128                              dw_loc_list_ref descr)
16129 {
16130   if (descr == 0)
16131     return;
16132   if (single_element_loc_list_p (descr))
16133     add_AT_loc (die, attr_kind, descr->expr);
16134   else
16135     add_AT_loc_list (die, attr_kind, descr);
16136 }
16137
16138 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16139
16140 static void
16141 add_accessibility_attribute (dw_die_ref die, tree decl)
16142 {
16143   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16144      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16145      the default has always been DW_ACCESS_public.  */
16146   if (TREE_PROTECTED (decl))
16147     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16148   else if (TREE_PRIVATE (decl))
16149     {
16150       if (dwarf_version == 2
16151           || die->die_parent == NULL
16152           || die->die_parent->die_tag != DW_TAG_class_type)
16153         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16154     }
16155   else if (dwarf_version > 2
16156            && die->die_parent
16157            && die->die_parent->die_tag == DW_TAG_class_type)
16158     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16159 }
16160
16161 /* Attach the specialized form of location attribute used for data members of
16162    struct and union types.  In the special case of a FIELD_DECL node which
16163    represents a bit-field, the "offset" part of this special location
16164    descriptor must indicate the distance in bytes from the lowest-addressed
16165    byte of the containing struct or union type to the lowest-addressed byte of
16166    the "containing object" for the bit-field.  (See the `field_byte_offset'
16167    function above).
16168
16169    For any given bit-field, the "containing object" is a hypothetical object
16170    (of some integral or enum type) within which the given bit-field lives.  The
16171    type of this hypothetical "containing object" is always the same as the
16172    declared type of the individual bit-field itself (for GCC anyway... the
16173    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16174    bytes) of the hypothetical "containing object" which will be given in the
16175    DW_AT_byte_size attribute for this bit-field.  (See the
16176    `byte_size_attribute' function below.)  It is also used when calculating the
16177    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16178    function below.)  */
16179
16180 static void
16181 add_data_member_location_attribute (dw_die_ref die, tree decl)
16182 {
16183   HOST_WIDE_INT offset;
16184   dw_loc_descr_ref loc_descr = 0;
16185
16186   if (TREE_CODE (decl) == TREE_BINFO)
16187     {
16188       /* We're working on the TAG_inheritance for a base class.  */
16189       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16190         {
16191           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16192              aren't at a fixed offset from all (sub)objects of the same
16193              type.  We need to extract the appropriate offset from our
16194              vtable.  The following dwarf expression means
16195
16196                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16197
16198              This is specific to the V3 ABI, of course.  */
16199
16200           dw_loc_descr_ref tmp;
16201
16202           /* Make a copy of the object address.  */
16203           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16204           add_loc_descr (&loc_descr, tmp);
16205
16206           /* Extract the vtable address.  */
16207           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16208           add_loc_descr (&loc_descr, tmp);
16209
16210           /* Calculate the address of the offset.  */
16211           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16212           gcc_assert (offset < 0);
16213
16214           tmp = int_loc_descriptor (-offset);
16215           add_loc_descr (&loc_descr, tmp);
16216           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16217           add_loc_descr (&loc_descr, tmp);
16218
16219           /* Extract the offset.  */
16220           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16221           add_loc_descr (&loc_descr, tmp);
16222
16223           /* Add it to the object address.  */
16224           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16225           add_loc_descr (&loc_descr, tmp);
16226         }
16227       else
16228         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16229     }
16230   else
16231     offset = field_byte_offset (decl);
16232
16233   if (! loc_descr)
16234     {
16235       if (dwarf_version > 2)
16236         {
16237           /* Don't need to output a location expression, just the constant. */
16238           if (offset < 0)
16239             add_AT_int (die, DW_AT_data_member_location, offset);
16240           else
16241             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16242           return;
16243         }
16244       else
16245         {
16246           enum dwarf_location_atom op;
16247
16248           /* The DWARF2 standard says that we should assume that the structure
16249              address is already on the stack, so we can specify a structure
16250              field address by using DW_OP_plus_uconst.  */
16251
16252 #ifdef MIPS_DEBUGGING_INFO
16253           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16254              operator correctly.  It works only if we leave the offset on the
16255              stack.  */
16256           op = DW_OP_constu;
16257 #else
16258           op = DW_OP_plus_uconst;
16259 #endif
16260
16261           loc_descr = new_loc_descr (op, offset, 0);
16262         }
16263     }
16264
16265   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16266 }
16267
16268 /* Writes integer values to dw_vec_const array.  */
16269
16270 static void
16271 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16272 {
16273   while (size != 0)
16274     {
16275       *dest++ = val & 0xff;
16276       val >>= 8;
16277       --size;
16278     }
16279 }
16280
16281 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16282
16283 static HOST_WIDE_INT
16284 extract_int (const unsigned char *src, unsigned int size)
16285 {
16286   HOST_WIDE_INT val = 0;
16287
16288   src += size;
16289   while (size != 0)
16290     {
16291       val <<= 8;
16292       val |= *--src & 0xff;
16293       --size;
16294     }
16295   return val;
16296 }
16297
16298 /* Writes double_int values to dw_vec_const array.  */
16299
16300 static void
16301 insert_double (double_int val, unsigned char *dest)
16302 {
16303   unsigned char *p0 = dest;
16304   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16305
16306   if (WORDS_BIG_ENDIAN)
16307     {
16308       p0 = p1;
16309       p1 = dest;
16310     }
16311
16312   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16313   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16314 }
16315
16316 /* Writes floating point values to dw_vec_const array.  */
16317
16318 static void
16319 insert_float (const_rtx rtl, unsigned char *array)
16320 {
16321   REAL_VALUE_TYPE rv;
16322   long val[4];
16323   int i;
16324
16325   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16326   real_to_target (val, &rv, GET_MODE (rtl));
16327
16328   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16329   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16330     {
16331       insert_int (val[i], 4, array);
16332       array += 4;
16333     }
16334 }
16335
16336 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16337    does not have a "location" either in memory or in a register.  These
16338    things can arise in GNU C when a constant is passed as an actual parameter
16339    to an inlined function.  They can also arise in C++ where declared
16340    constants do not necessarily get memory "homes".  */
16341
16342 static bool
16343 add_const_value_attribute (dw_die_ref die, rtx rtl)
16344 {
16345   switch (GET_CODE (rtl))
16346     {
16347     case CONST_INT:
16348       {
16349         HOST_WIDE_INT val = INTVAL (rtl);
16350
16351         if (val < 0)
16352           add_AT_int (die, DW_AT_const_value, val);
16353         else
16354           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16355       }
16356       return true;
16357
16358     case CONST_DOUBLE:
16359       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16360          floating-point constant.  A CONST_DOUBLE is used whenever the
16361          constant requires more than one word in order to be adequately
16362          represented.  */
16363       {
16364         enum machine_mode mode = GET_MODE (rtl);
16365
16366         if (SCALAR_FLOAT_MODE_P (mode))
16367           {
16368             unsigned int length = GET_MODE_SIZE (mode);
16369             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16370
16371             insert_float (rtl, array);
16372             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16373           }
16374         else
16375           add_AT_double (die, DW_AT_const_value,
16376                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16377       }
16378       return true;
16379
16380     case CONST_VECTOR:
16381       {
16382         enum machine_mode mode = GET_MODE (rtl);
16383         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16384         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16385         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16386           (length * elt_size);
16387         unsigned int i;
16388         unsigned char *p;
16389
16390         switch (GET_MODE_CLASS (mode))
16391           {
16392           case MODE_VECTOR_INT:
16393             for (i = 0, p = array; i < length; i++, p += elt_size)
16394               {
16395                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16396                 double_int val = rtx_to_double_int (elt);
16397
16398                 if (elt_size <= sizeof (HOST_WIDE_INT))
16399                   insert_int (double_int_to_shwi (val), elt_size, p);
16400                 else
16401                   {
16402                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16403                     insert_double (val, p);
16404                   }
16405               }
16406             break;
16407
16408           case MODE_VECTOR_FLOAT:
16409             for (i = 0, p = array; i < length; i++, p += elt_size)
16410               {
16411                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16412                 insert_float (elt, p);
16413               }
16414             break;
16415
16416           default:
16417             gcc_unreachable ();
16418           }
16419
16420         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16421       }
16422       return true;
16423
16424     case CONST_STRING:
16425       if (dwarf_version >= 4 || !dwarf_strict)
16426         {
16427           dw_loc_descr_ref loc_result;
16428           resolve_one_addr (&rtl, NULL);
16429         rtl_addr:
16430           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16431           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16432           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16433           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16434           add_AT_loc (die, DW_AT_location, loc_result);
16435           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16436           return true;
16437         }
16438       return false;
16439
16440     case CONST:
16441       if (CONSTANT_P (XEXP (rtl, 0)))
16442         return add_const_value_attribute (die, XEXP (rtl, 0));
16443       /* FALLTHROUGH */
16444     case SYMBOL_REF:
16445       if (!const_ok_for_output (rtl))
16446         return false;
16447     case LABEL_REF:
16448       if (dwarf_version >= 4 || !dwarf_strict)
16449         goto rtl_addr;
16450       return false;
16451
16452     case PLUS:
16453       /* In cases where an inlined instance of an inline function is passed
16454          the address of an `auto' variable (which is local to the caller) we
16455          can get a situation where the DECL_RTL of the artificial local
16456          variable (for the inlining) which acts as a stand-in for the
16457          corresponding formal parameter (of the inline function) will look
16458          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16459          exactly a compile-time constant expression, but it isn't the address
16460          of the (artificial) local variable either.  Rather, it represents the
16461          *value* which the artificial local variable always has during its
16462          lifetime.  We currently have no way to represent such quasi-constant
16463          values in Dwarf, so for now we just punt and generate nothing.  */
16464       return false;
16465
16466     case HIGH:
16467     case CONST_FIXED:
16468       return false;
16469
16470     case MEM:
16471       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16472           && MEM_READONLY_P (rtl)
16473           && GET_MODE (rtl) == BLKmode)
16474         {
16475           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16476           return true;
16477         }
16478       return false;
16479
16480     default:
16481       /* No other kinds of rtx should be possible here.  */
16482       gcc_unreachable ();
16483     }
16484   return false;
16485 }
16486
16487 /* Determine whether the evaluation of EXPR references any variables
16488    or functions which aren't otherwise used (and therefore may not be
16489    output).  */
16490 static tree
16491 reference_to_unused (tree * tp, int * walk_subtrees,
16492                      void * data ATTRIBUTE_UNUSED)
16493 {
16494   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16495     *walk_subtrees = 0;
16496
16497   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16498       && ! TREE_ASM_WRITTEN (*tp))
16499     return *tp;
16500   /* ???  The C++ FE emits debug information for using decls, so
16501      putting gcc_unreachable here falls over.  See PR31899.  For now
16502      be conservative.  */
16503   else if (!cgraph_global_info_ready
16504            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16505     return *tp;
16506   else if (TREE_CODE (*tp) == VAR_DECL)
16507     {
16508       struct varpool_node *node = varpool_get_node (*tp);
16509       if (!node || !node->needed)
16510         return *tp;
16511     }
16512   else if (TREE_CODE (*tp) == FUNCTION_DECL
16513            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16514     {
16515       /* The call graph machinery must have finished analyzing,
16516          optimizing and gimplifying the CU by now.
16517          So if *TP has no call graph node associated
16518          to it, it means *TP will not be emitted.  */
16519       if (!cgraph_get_node (*tp))
16520         return *tp;
16521     }
16522   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16523     return *tp;
16524
16525   return NULL_TREE;
16526 }
16527
16528 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16529    for use in a later add_const_value_attribute call.  */
16530
16531 static rtx
16532 rtl_for_decl_init (tree init, tree type)
16533 {
16534   rtx rtl = NULL_RTX;
16535
16536   STRIP_NOPS (init);
16537
16538   /* If a variable is initialized with a string constant without embedded
16539      zeros, build CONST_STRING.  */
16540   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16541     {
16542       tree enttype = TREE_TYPE (type);
16543       tree domain = TYPE_DOMAIN (type);
16544       enum machine_mode mode = TYPE_MODE (enttype);
16545
16546       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16547           && domain
16548           && integer_zerop (TYPE_MIN_VALUE (domain))
16549           && compare_tree_int (TYPE_MAX_VALUE (domain),
16550                                TREE_STRING_LENGTH (init) - 1) == 0
16551           && ((size_t) TREE_STRING_LENGTH (init)
16552               == strlen (TREE_STRING_POINTER (init)) + 1))
16553         {
16554           rtl = gen_rtx_CONST_STRING (VOIDmode,
16555                                       ggc_strdup (TREE_STRING_POINTER (init)));
16556           rtl = gen_rtx_MEM (BLKmode, rtl);
16557           MEM_READONLY_P (rtl) = 1;
16558         }
16559     }
16560   /* Other aggregates, and complex values, could be represented using
16561      CONCAT: FIXME!  */
16562   else if (AGGREGATE_TYPE_P (type)
16563            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16564                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16565            || TREE_CODE (type) == COMPLEX_TYPE)
16566     ;
16567   /* Vectors only work if their mode is supported by the target.
16568      FIXME: generic vectors ought to work too.  */
16569   else if (TREE_CODE (type) == VECTOR_TYPE
16570            && !VECTOR_MODE_P (TYPE_MODE (type)))
16571     ;
16572   /* If the initializer is something that we know will expand into an
16573      immediate RTL constant, expand it now.  We must be careful not to
16574      reference variables which won't be output.  */
16575   else if (initializer_constant_valid_p (init, type)
16576            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16577     {
16578       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16579          possible.  */
16580       if (TREE_CODE (type) == VECTOR_TYPE)
16581         switch (TREE_CODE (init))
16582           {
16583           case VECTOR_CST:
16584             break;
16585           case CONSTRUCTOR:
16586             if (TREE_CONSTANT (init))
16587               {
16588                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16589                 bool constant_p = true;
16590                 tree value;
16591                 unsigned HOST_WIDE_INT ix;
16592
16593                 /* Even when ctor is constant, it might contain non-*_CST
16594                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16595                    belong into VECTOR_CST nodes.  */
16596                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16597                   if (!CONSTANT_CLASS_P (value))
16598                     {
16599                       constant_p = false;
16600                       break;
16601                     }
16602
16603                 if (constant_p)
16604                   {
16605                     init = build_vector_from_ctor (type, elts);
16606                     break;
16607                   }
16608               }
16609             /* FALLTHRU */
16610
16611           default:
16612             return NULL;
16613           }
16614
16615       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16616
16617       /* If expand_expr returns a MEM, it wasn't immediate.  */
16618       gcc_assert (!rtl || !MEM_P (rtl));
16619     }
16620
16621   return rtl;
16622 }
16623
16624 /* Generate RTL for the variable DECL to represent its location.  */
16625
16626 static rtx
16627 rtl_for_decl_location (tree decl)
16628 {
16629   rtx rtl;
16630
16631   /* Here we have to decide where we are going to say the parameter "lives"
16632      (as far as the debugger is concerned).  We only have a couple of
16633      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16634
16635      DECL_RTL normally indicates where the parameter lives during most of the
16636      activation of the function.  If optimization is enabled however, this
16637      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16638      that the parameter doesn't really live anywhere (as far as the code
16639      generation parts of GCC are concerned) during most of the function's
16640      activation.  That will happen (for example) if the parameter is never
16641      referenced within the function.
16642
16643      We could just generate a location descriptor here for all non-NULL
16644      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16645      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16646      where DECL_RTL is NULL or is a pseudo-reg.
16647
16648      Note however that we can only get away with using DECL_INCOMING_RTL as
16649      a backup substitute for DECL_RTL in certain limited cases.  In cases
16650      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16651      we can be sure that the parameter was passed using the same type as it is
16652      declared to have within the function, and that its DECL_INCOMING_RTL
16653      points us to a place where a value of that type is passed.
16654
16655      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16656      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16657      because in these cases DECL_INCOMING_RTL points us to a value of some
16658      type which is *different* from the type of the parameter itself.  Thus,
16659      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16660      such cases, the debugger would end up (for example) trying to fetch a
16661      `float' from a place which actually contains the first part of a
16662      `double'.  That would lead to really incorrect and confusing
16663      output at debug-time.
16664
16665      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16666      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16667      are a couple of exceptions however.  On little-endian machines we can
16668      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16669      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16670      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16671      when (on a little-endian machine) a non-prototyped function has a
16672      parameter declared to be of type `short' or `char'.  In such cases,
16673      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16674      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16675      passed `int' value.  If the debugger then uses that address to fetch
16676      a `short' or a `char' (on a little-endian machine) the result will be
16677      the correct data, so we allow for such exceptional cases below.
16678
16679      Note that our goal here is to describe the place where the given formal
16680      parameter lives during most of the function's activation (i.e. between the
16681      end of the prologue and the start of the epilogue).  We'll do that as best
16682      as we can. Note however that if the given formal parameter is modified
16683      sometime during the execution of the function, then a stack backtrace (at
16684      debug-time) will show the function as having been called with the *new*
16685      value rather than the value which was originally passed in.  This happens
16686      rarely enough that it is not a major problem, but it *is* a problem, and
16687      I'd like to fix it.
16688
16689      A future version of dwarf2out.c may generate two additional attributes for
16690      any given DW_TAG_formal_parameter DIE which will describe the "passed
16691      type" and the "passed location" for the given formal parameter in addition
16692      to the attributes we now generate to indicate the "declared type" and the
16693      "active location" for each parameter.  This additional set of attributes
16694      could be used by debuggers for stack backtraces. Separately, note that
16695      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16696      This happens (for example) for inlined-instances of inline function formal
16697      parameters which are never referenced.  This really shouldn't be
16698      happening.  All PARM_DECL nodes should get valid non-NULL
16699      DECL_INCOMING_RTL values.  FIXME.  */
16700
16701   /* Use DECL_RTL as the "location" unless we find something better.  */
16702   rtl = DECL_RTL_IF_SET (decl);
16703
16704   /* When generating abstract instances, ignore everything except
16705      constants, symbols living in memory, and symbols living in
16706      fixed registers.  */
16707   if (! reload_completed)
16708     {
16709       if (rtl
16710           && (CONSTANT_P (rtl)
16711               || (MEM_P (rtl)
16712                   && CONSTANT_P (XEXP (rtl, 0)))
16713               || (REG_P (rtl)
16714                   && TREE_CODE (decl) == VAR_DECL
16715                   && TREE_STATIC (decl))))
16716         {
16717           rtl = targetm.delegitimize_address (rtl);
16718           return rtl;
16719         }
16720       rtl = NULL_RTX;
16721     }
16722   else if (TREE_CODE (decl) == PARM_DECL)
16723     {
16724       if (rtl == NULL_RTX
16725           || is_pseudo_reg (rtl)
16726           || (MEM_P (rtl)
16727               && is_pseudo_reg (XEXP (rtl, 0))
16728               && DECL_INCOMING_RTL (decl)
16729               && MEM_P (DECL_INCOMING_RTL (decl))
16730               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16731         {
16732           tree declared_type = TREE_TYPE (decl);
16733           tree passed_type = DECL_ARG_TYPE (decl);
16734           enum machine_mode dmode = TYPE_MODE (declared_type);
16735           enum machine_mode pmode = TYPE_MODE (passed_type);
16736
16737           /* This decl represents a formal parameter which was optimized out.
16738              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16739              all cases where (rtl == NULL_RTX) just below.  */
16740           if (dmode == pmode)
16741             rtl = DECL_INCOMING_RTL (decl);
16742           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16743                    && SCALAR_INT_MODE_P (dmode)
16744                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16745                    && DECL_INCOMING_RTL (decl))
16746             {
16747               rtx inc = DECL_INCOMING_RTL (decl);
16748               if (REG_P (inc))
16749                 rtl = inc;
16750               else if (MEM_P (inc))
16751                 {
16752                   if (BYTES_BIG_ENDIAN)
16753                     rtl = adjust_address_nv (inc, dmode,
16754                                              GET_MODE_SIZE (pmode)
16755                                              - GET_MODE_SIZE (dmode));
16756                   else
16757                     rtl = inc;
16758                 }
16759             }
16760         }
16761
16762       /* If the parm was passed in registers, but lives on the stack, then
16763          make a big endian correction if the mode of the type of the
16764          parameter is not the same as the mode of the rtl.  */
16765       /* ??? This is the same series of checks that are made in dbxout.c before
16766          we reach the big endian correction code there.  It isn't clear if all
16767          of these checks are necessary here, but keeping them all is the safe
16768          thing to do.  */
16769       else if (MEM_P (rtl)
16770                && XEXP (rtl, 0) != const0_rtx
16771                && ! CONSTANT_P (XEXP (rtl, 0))
16772                /* Not passed in memory.  */
16773                && !MEM_P (DECL_INCOMING_RTL (decl))
16774                /* Not passed by invisible reference.  */
16775                && (!REG_P (XEXP (rtl, 0))
16776                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16777                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16778 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16779                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16780 #endif
16781                      )
16782                /* Big endian correction check.  */
16783                && BYTES_BIG_ENDIAN
16784                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16785                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16786                    < UNITS_PER_WORD))
16787         {
16788           int offset = (UNITS_PER_WORD
16789                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16790
16791           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16792                              plus_constant (XEXP (rtl, 0), offset));
16793         }
16794     }
16795   else if (TREE_CODE (decl) == VAR_DECL
16796            && rtl
16797            && MEM_P (rtl)
16798            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16799            && BYTES_BIG_ENDIAN)
16800     {
16801       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16802       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16803
16804       /* If a variable is declared "register" yet is smaller than
16805          a register, then if we store the variable to memory, it
16806          looks like we're storing a register-sized value, when in
16807          fact we are not.  We need to adjust the offset of the
16808          storage location to reflect the actual value's bytes,
16809          else gdb will not be able to display it.  */
16810       if (rsize > dsize)
16811         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16812                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16813     }
16814
16815   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16816      and will have been substituted directly into all expressions that use it.
16817      C does not have such a concept, but C++ and other languages do.  */
16818   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16819     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16820
16821   if (rtl)
16822     rtl = targetm.delegitimize_address (rtl);
16823
16824   /* If we don't look past the constant pool, we risk emitting a
16825      reference to a constant pool entry that isn't referenced from
16826      code, and thus is not emitted.  */
16827   if (rtl)
16828     rtl = avoid_constant_pool_reference (rtl);
16829
16830   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16831      in the current CU, resolve_addr will remove the expression referencing
16832      it.  */
16833   if (rtl == NULL_RTX
16834       && TREE_CODE (decl) == VAR_DECL
16835       && !DECL_EXTERNAL (decl)
16836       && TREE_STATIC (decl)
16837       && DECL_NAME (decl)
16838       && !DECL_HARD_REGISTER (decl)
16839       && DECL_MODE (decl) != VOIDmode)
16840     {
16841       rtl = make_decl_rtl_for_debug (decl);
16842       if (!MEM_P (rtl)
16843           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16844           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16845         rtl = NULL_RTX;
16846     }
16847
16848   return rtl;
16849 }
16850
16851 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16852    returned.  If so, the decl for the COMMON block is returned, and the
16853    value is the offset into the common block for the symbol.  */
16854
16855 static tree
16856 fortran_common (tree decl, HOST_WIDE_INT *value)
16857 {
16858   tree val_expr, cvar;
16859   enum machine_mode mode;
16860   HOST_WIDE_INT bitsize, bitpos;
16861   tree offset;
16862   int volatilep = 0, unsignedp = 0;
16863
16864   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16865      it does not have a value (the offset into the common area), or if it
16866      is thread local (as opposed to global) then it isn't common, and shouldn't
16867      be handled as such.  */
16868   if (TREE_CODE (decl) != VAR_DECL
16869       || !TREE_STATIC (decl)
16870       || !DECL_HAS_VALUE_EXPR_P (decl)
16871       || !is_fortran ())
16872     return NULL_TREE;
16873
16874   val_expr = DECL_VALUE_EXPR (decl);
16875   if (TREE_CODE (val_expr) != COMPONENT_REF)
16876     return NULL_TREE;
16877
16878   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16879                               &mode, &unsignedp, &volatilep, true);
16880
16881   if (cvar == NULL_TREE
16882       || TREE_CODE (cvar) != VAR_DECL
16883       || DECL_ARTIFICIAL (cvar)
16884       || !TREE_PUBLIC (cvar))
16885     return NULL_TREE;
16886
16887   *value = 0;
16888   if (offset != NULL)
16889     {
16890       if (!host_integerp (offset, 0))
16891         return NULL_TREE;
16892       *value = tree_low_cst (offset, 0);
16893     }
16894   if (bitpos != 0)
16895     *value += bitpos / BITS_PER_UNIT;
16896
16897   return cvar;
16898 }
16899
16900 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16901    data attribute for a variable or a parameter.  We generate the
16902    DW_AT_const_value attribute only in those cases where the given variable
16903    or parameter does not have a true "location" either in memory or in a
16904    register.  This can happen (for example) when a constant is passed as an
16905    actual argument in a call to an inline function.  (It's possible that
16906    these things can crop up in other ways also.)  Note that one type of
16907    constant value which can be passed into an inlined function is a constant
16908    pointer.  This can happen for example if an actual argument in an inlined
16909    function call evaluates to a compile-time constant address.  */
16910
16911 static bool
16912 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16913                                        enum dwarf_attribute attr)
16914 {
16915   rtx rtl;
16916   dw_loc_list_ref list;
16917   var_loc_list *loc_list;
16918
16919   if (TREE_CODE (decl) == ERROR_MARK)
16920     return false;
16921
16922   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16923               || TREE_CODE (decl) == RESULT_DECL);
16924
16925   /* Try to get some constant RTL for this decl, and use that as the value of
16926      the location.  */
16927
16928   rtl = rtl_for_decl_location (decl);
16929   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16930       && add_const_value_attribute (die, rtl))
16931     return true;
16932
16933   /* See if we have single element location list that is equivalent to
16934      a constant value.  That way we are better to use add_const_value_attribute
16935      rather than expanding constant value equivalent.  */
16936   loc_list = lookup_decl_loc (decl);
16937   if (loc_list
16938       && loc_list->first
16939       && loc_list->first->next == NULL
16940       && NOTE_P (loc_list->first->loc)
16941       && NOTE_VAR_LOCATION (loc_list->first->loc)
16942       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16943     {
16944       struct var_loc_node *node;
16945
16946       node = loc_list->first;
16947       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16948       if (GET_CODE (rtl) == EXPR_LIST)
16949         rtl = XEXP (rtl, 0);
16950       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16951           && add_const_value_attribute (die, rtl))
16952          return true;
16953     }
16954   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16955   if (list)
16956     {
16957       add_AT_location_description (die, attr, list);
16958       return true;
16959     }
16960   /* None of that worked, so it must not really have a location;
16961      try adding a constant value attribute from the DECL_INITIAL.  */
16962   return tree_add_const_value_attribute_for_decl (die, decl);
16963 }
16964
16965 /* Add VARIABLE and DIE into deferred locations list.  */
16966
16967 static void
16968 defer_location (tree variable, dw_die_ref die)
16969 {
16970   deferred_locations entry;
16971   entry.variable = variable;
16972   entry.die = die;
16973   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16974 }
16975
16976 /* Helper function for tree_add_const_value_attribute.  Natively encode
16977    initializer INIT into an array.  Return true if successful.  */
16978
16979 static bool
16980 native_encode_initializer (tree init, unsigned char *array, int size)
16981 {
16982   tree type;
16983
16984   if (init == NULL_TREE)
16985     return false;
16986
16987   STRIP_NOPS (init);
16988   switch (TREE_CODE (init))
16989     {
16990     case STRING_CST:
16991       type = TREE_TYPE (init);
16992       if (TREE_CODE (type) == ARRAY_TYPE)
16993         {
16994           tree enttype = TREE_TYPE (type);
16995           enum machine_mode mode = TYPE_MODE (enttype);
16996
16997           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16998             return false;
16999           if (int_size_in_bytes (type) != size)
17000             return false;
17001           if (size > TREE_STRING_LENGTH (init))
17002             {
17003               memcpy (array, TREE_STRING_POINTER (init),
17004                       TREE_STRING_LENGTH (init));
17005               memset (array + TREE_STRING_LENGTH (init),
17006                       '\0', size - TREE_STRING_LENGTH (init));
17007             }
17008           else
17009             memcpy (array, TREE_STRING_POINTER (init), size);
17010           return true;
17011         }
17012       return false;
17013     case CONSTRUCTOR:
17014       type = TREE_TYPE (init);
17015       if (int_size_in_bytes (type) != size)
17016         return false;
17017       if (TREE_CODE (type) == ARRAY_TYPE)
17018         {
17019           HOST_WIDE_INT min_index;
17020           unsigned HOST_WIDE_INT cnt;
17021           int curpos = 0, fieldsize;
17022           constructor_elt *ce;
17023
17024           if (TYPE_DOMAIN (type) == NULL_TREE
17025               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17026             return false;
17027
17028           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17029           if (fieldsize <= 0)
17030             return false;
17031
17032           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17033           memset (array, '\0', size);
17034           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17035             {
17036               tree val = ce->value;
17037               tree index = ce->index;
17038               int pos = curpos;
17039               if (index && TREE_CODE (index) == RANGE_EXPR)
17040                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17041                       * fieldsize;
17042               else if (index)
17043                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17044
17045               if (val)
17046                 {
17047                   STRIP_NOPS (val);
17048                   if (!native_encode_initializer (val, array + pos, fieldsize))
17049                     return false;
17050                 }
17051               curpos = pos + fieldsize;
17052               if (index && TREE_CODE (index) == RANGE_EXPR)
17053                 {
17054                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17055                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17056                   while (count > 0)
17057                     {
17058                       if (val)
17059                         memcpy (array + curpos, array + pos, fieldsize);
17060                       curpos += fieldsize;
17061                     }
17062                 }
17063               gcc_assert (curpos <= size);
17064             }
17065           return true;
17066         }
17067       else if (TREE_CODE (type) == RECORD_TYPE
17068                || TREE_CODE (type) == UNION_TYPE)
17069         {
17070           tree field = NULL_TREE;
17071           unsigned HOST_WIDE_INT cnt;
17072           constructor_elt *ce;
17073
17074           if (int_size_in_bytes (type) != size)
17075             return false;
17076
17077           if (TREE_CODE (type) == RECORD_TYPE)
17078             field = TYPE_FIELDS (type);
17079
17080           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17081             {
17082               tree val = ce->value;
17083               int pos, fieldsize;
17084
17085               if (ce->index != 0)
17086                 field = ce->index;
17087
17088               if (val)
17089                 STRIP_NOPS (val);
17090
17091               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17092                 return false;
17093
17094               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17095                   && TYPE_DOMAIN (TREE_TYPE (field))
17096                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17097                 return false;
17098               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17099                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17100                 return false;
17101               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17102               pos = int_byte_position (field);
17103               gcc_assert (pos + fieldsize <= size);
17104               if (val
17105                   && !native_encode_initializer (val, array + pos, fieldsize))
17106                 return false;
17107             }
17108           return true;
17109         }
17110       return false;
17111     case VIEW_CONVERT_EXPR:
17112     case NON_LVALUE_EXPR:
17113       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17114     default:
17115       return native_encode_expr (init, array, size) == size;
17116     }
17117 }
17118
17119 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17120    attribute is the const value T.  */
17121
17122 static bool
17123 tree_add_const_value_attribute (dw_die_ref die, tree t)
17124 {
17125   tree init;
17126   tree type = TREE_TYPE (t);
17127   rtx rtl;
17128
17129   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17130     return false;
17131
17132   init = t;
17133   gcc_assert (!DECL_P (init));
17134
17135   rtl = rtl_for_decl_init (init, type);
17136   if (rtl)
17137     return add_const_value_attribute (die, rtl);
17138   /* If the host and target are sane, try harder.  */
17139   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17140            && initializer_constant_valid_p (init, type))
17141     {
17142       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17143       if (size > 0 && (int) size == size)
17144         {
17145           unsigned char *array = (unsigned char *)
17146             ggc_alloc_cleared_atomic (size);
17147
17148           if (native_encode_initializer (init, array, size))
17149             {
17150               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17151               return true;
17152             }
17153         }
17154     }
17155   return false;
17156 }
17157
17158 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17159    attribute is the const value of T, where T is an integral constant
17160    variable with static storage duration
17161    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17162
17163 static bool
17164 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17165 {
17166
17167   if (!decl
17168       || (TREE_CODE (decl) != VAR_DECL
17169           && TREE_CODE (decl) != CONST_DECL))
17170     return false;
17171
17172     if (TREE_READONLY (decl)
17173         && ! TREE_THIS_VOLATILE (decl)
17174         && DECL_INITIAL (decl))
17175       /* OK */;
17176     else
17177       return false;
17178
17179   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17180   if (get_AT (var_die, DW_AT_const_value))
17181     return false;
17182
17183   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17184 }
17185
17186 /* Convert the CFI instructions for the current function into a
17187    location list.  This is used for DW_AT_frame_base when we targeting
17188    a dwarf2 consumer that does not support the dwarf3
17189    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17190    expressions.  */
17191
17192 static dw_loc_list_ref
17193 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17194 {
17195   dw_fde_ref fde;
17196   dw_loc_list_ref list, *list_tail;
17197   dw_cfi_ref cfi;
17198   dw_cfa_location last_cfa, next_cfa;
17199   const char *start_label, *last_label, *section;
17200   dw_cfa_location remember;
17201
17202   fde = current_fde ();
17203   gcc_assert (fde != NULL);
17204
17205   section = secname_for_decl (current_function_decl);
17206   list_tail = &list;
17207   list = NULL;
17208
17209   memset (&next_cfa, 0, sizeof (next_cfa));
17210   next_cfa.reg = INVALID_REGNUM;
17211   remember = next_cfa;
17212
17213   start_label = fde->dw_fde_begin;
17214
17215   /* ??? Bald assumption that the CIE opcode list does not contain
17216      advance opcodes.  */
17217   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17218     lookup_cfa_1 (cfi, &next_cfa, &remember);
17219
17220   last_cfa = next_cfa;
17221   last_label = start_label;
17222
17223   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17224     switch (cfi->dw_cfi_opc)
17225       {
17226       case DW_CFA_set_loc:
17227       case DW_CFA_advance_loc1:
17228       case DW_CFA_advance_loc2:
17229       case DW_CFA_advance_loc4:
17230         if (!cfa_equal_p (&last_cfa, &next_cfa))
17231           {
17232             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17233                                        start_label, last_label, section);
17234
17235             list_tail = &(*list_tail)->dw_loc_next;
17236             last_cfa = next_cfa;
17237             start_label = last_label;
17238           }
17239         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17240         break;
17241
17242       case DW_CFA_advance_loc:
17243         /* The encoding is complex enough that we should never emit this.  */
17244         gcc_unreachable ();
17245
17246       default:
17247         lookup_cfa_1 (cfi, &next_cfa, &remember);
17248         break;
17249       }
17250
17251   if (!cfa_equal_p (&last_cfa, &next_cfa))
17252     {
17253       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17254                                  start_label, last_label, section);
17255       list_tail = &(*list_tail)->dw_loc_next;
17256       start_label = last_label;
17257     }
17258
17259   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17260                              start_label, fde->dw_fde_end, section);
17261
17262   if (list && list->dw_loc_next)
17263     gen_llsym (list);
17264
17265   return list;
17266 }
17267
17268 /* Compute a displacement from the "steady-state frame pointer" to the
17269    frame base (often the same as the CFA), and store it in
17270    frame_pointer_fb_offset.  OFFSET is added to the displacement
17271    before the latter is negated.  */
17272
17273 static void
17274 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17275 {
17276   rtx reg, elim;
17277
17278 #ifdef FRAME_POINTER_CFA_OFFSET
17279   reg = frame_pointer_rtx;
17280   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17281 #else
17282   reg = arg_pointer_rtx;
17283   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17284 #endif
17285
17286   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17287   if (GET_CODE (elim) == PLUS)
17288     {
17289       offset += INTVAL (XEXP (elim, 1));
17290       elim = XEXP (elim, 0);
17291     }
17292
17293   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17294                && (elim == hard_frame_pointer_rtx
17295                    || elim == stack_pointer_rtx))
17296               || elim == (frame_pointer_needed
17297                           ? hard_frame_pointer_rtx
17298                           : stack_pointer_rtx));
17299
17300   frame_pointer_fb_offset = -offset;
17301 }
17302
17303 /* Generate a DW_AT_name attribute given some string value to be included as
17304    the value of the attribute.  */
17305
17306 static void
17307 add_name_attribute (dw_die_ref die, const char *name_string)
17308 {
17309   if (name_string != NULL && *name_string != 0)
17310     {
17311       if (demangle_name_func)
17312         name_string = (*demangle_name_func) (name_string);
17313
17314       add_AT_string (die, DW_AT_name, name_string);
17315     }
17316 }
17317
17318 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17319
17320 static void
17321 add_comp_dir_attribute (dw_die_ref die)
17322 {
17323   const char *wd = get_src_pwd ();
17324   char *wd1;
17325
17326   if (wd == NULL)
17327     return;
17328
17329   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17330     {
17331       int wdlen;
17332
17333       wdlen = strlen (wd);
17334       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17335       strcpy (wd1, wd);
17336       wd1 [wdlen] = DIR_SEPARATOR;
17337       wd1 [wdlen + 1] = 0;
17338       wd = wd1;
17339     }
17340
17341     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17342 }
17343
17344 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17345    default.  */
17346
17347 static int
17348 lower_bound_default (void)
17349 {
17350   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17351     {
17352     case DW_LANG_C:
17353     case DW_LANG_C89:
17354     case DW_LANG_C99:
17355     case DW_LANG_C_plus_plus:
17356     case DW_LANG_ObjC:
17357     case DW_LANG_ObjC_plus_plus:
17358     case DW_LANG_Java:
17359       return 0;
17360     case DW_LANG_Fortran77:
17361     case DW_LANG_Fortran90:
17362     case DW_LANG_Fortran95:
17363       return 1;
17364     case DW_LANG_UPC:
17365     case DW_LANG_D:
17366     case DW_LANG_Python:
17367       return dwarf_version >= 4 ? 0 : -1;
17368     case DW_LANG_Ada95:
17369     case DW_LANG_Ada83:
17370     case DW_LANG_Cobol74:
17371     case DW_LANG_Cobol85:
17372     case DW_LANG_Pascal83:
17373     case DW_LANG_Modula2:
17374     case DW_LANG_PLI:
17375       return dwarf_version >= 4 ? 1 : -1;
17376     default:
17377       return -1;
17378     }
17379 }
17380
17381 /* Given a tree node describing an array bound (either lower or upper) output
17382    a representation for that bound.  */
17383
17384 static void
17385 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17386 {
17387   switch (TREE_CODE (bound))
17388     {
17389     case ERROR_MARK:
17390       return;
17391
17392     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17393     case INTEGER_CST:
17394       {
17395         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17396         int dflt;
17397
17398         /* Use the default if possible.  */
17399         if (bound_attr == DW_AT_lower_bound
17400             && host_integerp (bound, 0)
17401             && (dflt = lower_bound_default ()) != -1
17402             && tree_low_cst (bound, 0) == dflt)
17403           ;
17404
17405         /* Otherwise represent the bound as an unsigned value with the
17406            precision of its type.  The precision and signedness of the
17407            type will be necessary to re-interpret it unambiguously.  */
17408         else if (prec < HOST_BITS_PER_WIDE_INT)
17409           {
17410             unsigned HOST_WIDE_INT mask
17411               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17412             add_AT_unsigned (subrange_die, bound_attr,
17413                              TREE_INT_CST_LOW (bound) & mask);
17414           }
17415         else if (prec == HOST_BITS_PER_WIDE_INT
17416                  || TREE_INT_CST_HIGH (bound) == 0)
17417           add_AT_unsigned (subrange_die, bound_attr,
17418                            TREE_INT_CST_LOW (bound));
17419         else
17420           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17421                          TREE_INT_CST_LOW (bound));
17422       }
17423       break;
17424
17425     CASE_CONVERT:
17426     case VIEW_CONVERT_EXPR:
17427       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17428       break;
17429
17430     case SAVE_EXPR:
17431       break;
17432
17433     case VAR_DECL:
17434     case PARM_DECL:
17435     case RESULT_DECL:
17436       {
17437         dw_die_ref decl_die = lookup_decl_die (bound);
17438
17439         /* ??? Can this happen, or should the variable have been bound
17440            first?  Probably it can, since I imagine that we try to create
17441            the types of parameters in the order in which they exist in
17442            the list, and won't have created a forward reference to a
17443            later parameter.  */
17444         if (decl_die != NULL)
17445           {
17446             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17447             break;
17448           }
17449       }
17450       /* FALLTHRU */
17451
17452     default:
17453       {
17454         /* Otherwise try to create a stack operation procedure to
17455            evaluate the value of the array bound.  */
17456
17457         dw_die_ref ctx, decl_die;
17458         dw_loc_list_ref list;
17459
17460         list = loc_list_from_tree (bound, 2);
17461         if (list == NULL || single_element_loc_list_p (list))
17462           {
17463             /* If DW_AT_*bound is not a reference nor constant, it is
17464                a DWARF expression rather than location description.
17465                For that loc_list_from_tree (bound, 0) is needed.
17466                If that fails to give a single element list,
17467                fall back to outputting this as a reference anyway.  */
17468             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17469             if (list2 && single_element_loc_list_p (list2))
17470               {
17471                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17472                 break;
17473               }
17474           }
17475         if (list == NULL)
17476           break;
17477
17478         if (current_function_decl == 0)
17479           ctx = comp_unit_die ();
17480         else
17481           ctx = lookup_decl_die (current_function_decl);
17482
17483         decl_die = new_die (DW_TAG_variable, ctx, bound);
17484         add_AT_flag (decl_die, DW_AT_artificial, 1);
17485         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17486         add_AT_location_description (decl_die, DW_AT_location, list);
17487         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17488         break;
17489       }
17490     }
17491 }
17492
17493 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17494    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17495    Note that the block of subscript information for an array type also
17496    includes information about the element type of the given array type.  */
17497
17498 static void
17499 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17500 {
17501   unsigned dimension_number;
17502   tree lower, upper;
17503   dw_die_ref subrange_die;
17504
17505   for (dimension_number = 0;
17506        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17507        type = TREE_TYPE (type), dimension_number++)
17508     {
17509       tree domain = TYPE_DOMAIN (type);
17510
17511       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17512         break;
17513
17514       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17515          and (in GNU C only) variable bounds.  Handle all three forms
17516          here.  */
17517       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17518       if (domain)
17519         {
17520           /* We have an array type with specified bounds.  */
17521           lower = TYPE_MIN_VALUE (domain);
17522           upper = TYPE_MAX_VALUE (domain);
17523
17524           /* Define the index type.  */
17525           if (TREE_TYPE (domain))
17526             {
17527               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17528                  TREE_TYPE field.  We can't emit debug info for this
17529                  because it is an unnamed integral type.  */
17530               if (TREE_CODE (domain) == INTEGER_TYPE
17531                   && TYPE_NAME (domain) == NULL_TREE
17532                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17533                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17534                 ;
17535               else
17536                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17537                                     type_die);
17538             }
17539
17540           /* ??? If upper is NULL, the array has unspecified length,
17541              but it does have a lower bound.  This happens with Fortran
17542                dimension arr(N:*)
17543              Since the debugger is definitely going to need to know N
17544              to produce useful results, go ahead and output the lower
17545              bound solo, and hope the debugger can cope.  */
17546
17547           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17548           if (upper)
17549             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17550         }
17551
17552       /* Otherwise we have an array type with an unspecified length.  The
17553          DWARF-2 spec does not say how to handle this; let's just leave out the
17554          bounds.  */
17555     }
17556 }
17557
17558 static void
17559 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17560 {
17561   unsigned size;
17562
17563   switch (TREE_CODE (tree_node))
17564     {
17565     case ERROR_MARK:
17566       size = 0;
17567       break;
17568     case ENUMERAL_TYPE:
17569     case RECORD_TYPE:
17570     case UNION_TYPE:
17571     case QUAL_UNION_TYPE:
17572       size = int_size_in_bytes (tree_node);
17573       break;
17574     case FIELD_DECL:
17575       /* For a data member of a struct or union, the DW_AT_byte_size is
17576          generally given as the number of bytes normally allocated for an
17577          object of the *declared* type of the member itself.  This is true
17578          even for bit-fields.  */
17579       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17580       break;
17581     default:
17582       gcc_unreachable ();
17583     }
17584
17585   /* Note that `size' might be -1 when we get to this point.  If it is, that
17586      indicates that the byte size of the entity in question is variable.  We
17587      have no good way of expressing this fact in Dwarf at the present time,
17588      so just let the -1 pass on through.  */
17589   add_AT_unsigned (die, DW_AT_byte_size, size);
17590 }
17591
17592 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17593    which specifies the distance in bits from the highest order bit of the
17594    "containing object" for the bit-field to the highest order bit of the
17595    bit-field itself.
17596
17597    For any given bit-field, the "containing object" is a hypothetical object
17598    (of some integral or enum type) within which the given bit-field lives.  The
17599    type of this hypothetical "containing object" is always the same as the
17600    declared type of the individual bit-field itself.  The determination of the
17601    exact location of the "containing object" for a bit-field is rather
17602    complicated.  It's handled by the `field_byte_offset' function (above).
17603
17604    Note that it is the size (in bytes) of the hypothetical "containing object"
17605    which will be given in the DW_AT_byte_size attribute for this bit-field.
17606    (See `byte_size_attribute' above).  */
17607
17608 static inline void
17609 add_bit_offset_attribute (dw_die_ref die, tree decl)
17610 {
17611   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17612   tree type = DECL_BIT_FIELD_TYPE (decl);
17613   HOST_WIDE_INT bitpos_int;
17614   HOST_WIDE_INT highest_order_object_bit_offset;
17615   HOST_WIDE_INT highest_order_field_bit_offset;
17616   HOST_WIDE_INT bit_offset;
17617
17618   /* Must be a field and a bit field.  */
17619   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17620
17621   /* We can't yet handle bit-fields whose offsets are variable, so if we
17622      encounter such things, just return without generating any attribute
17623      whatsoever.  Likewise for variable or too large size.  */
17624   if (! host_integerp (bit_position (decl), 0)
17625       || ! host_integerp (DECL_SIZE (decl), 1))
17626     return;
17627
17628   bitpos_int = int_bit_position (decl);
17629
17630   /* Note that the bit offset is always the distance (in bits) from the
17631      highest-order bit of the "containing object" to the highest-order bit of
17632      the bit-field itself.  Since the "high-order end" of any object or field
17633      is different on big-endian and little-endian machines, the computation
17634      below must take account of these differences.  */
17635   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17636   highest_order_field_bit_offset = bitpos_int;
17637
17638   if (! BYTES_BIG_ENDIAN)
17639     {
17640       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17641       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17642     }
17643
17644   bit_offset
17645     = (! BYTES_BIG_ENDIAN
17646        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17647        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17648
17649   if (bit_offset < 0)
17650     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17651   else
17652     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17653 }
17654
17655 /* For a FIELD_DECL node which represents a bit field, output an attribute
17656    which specifies the length in bits of the given field.  */
17657
17658 static inline void
17659 add_bit_size_attribute (dw_die_ref die, tree decl)
17660 {
17661   /* Must be a field and a bit field.  */
17662   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17663               && DECL_BIT_FIELD_TYPE (decl));
17664
17665   if (host_integerp (DECL_SIZE (decl), 1))
17666     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17667 }
17668
17669 /* If the compiled language is ANSI C, then add a 'prototyped'
17670    attribute, if arg types are given for the parameters of a function.  */
17671
17672 static inline void
17673 add_prototyped_attribute (dw_die_ref die, tree func_type)
17674 {
17675   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17676       && prototype_p (func_type))
17677     add_AT_flag (die, DW_AT_prototyped, 1);
17678 }
17679
17680 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17681    by looking in either the type declaration or object declaration
17682    equate table.  */
17683
17684 static inline dw_die_ref
17685 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17686 {
17687   dw_die_ref origin_die = NULL;
17688
17689   if (TREE_CODE (origin) != FUNCTION_DECL)
17690     {
17691       /* We may have gotten separated from the block for the inlined
17692          function, if we're in an exception handler or some such; make
17693          sure that the abstract function has been written out.
17694
17695          Doing this for nested functions is wrong, however; functions are
17696          distinct units, and our context might not even be inline.  */
17697       tree fn = origin;
17698
17699       if (TYPE_P (fn))
17700         fn = TYPE_STUB_DECL (fn);
17701
17702       fn = decl_function_context (fn);
17703       if (fn)
17704         dwarf2out_abstract_function (fn);
17705     }
17706
17707   if (DECL_P (origin))
17708     origin_die = lookup_decl_die (origin);
17709   else if (TYPE_P (origin))
17710     origin_die = lookup_type_die (origin);
17711
17712   /* XXX: Functions that are never lowered don't always have correct block
17713      trees (in the case of java, they simply have no block tree, in some other
17714      languages).  For these functions, there is nothing we can really do to
17715      output correct debug info for inlined functions in all cases.  Rather
17716      than die, we'll just produce deficient debug info now, in that we will
17717      have variables without a proper abstract origin.  In the future, when all
17718      functions are lowered, we should re-add a gcc_assert (origin_die)
17719      here.  */
17720
17721   if (origin_die)
17722     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17723   return origin_die;
17724 }
17725
17726 /* We do not currently support the pure_virtual attribute.  */
17727
17728 static inline void
17729 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17730 {
17731   if (DECL_VINDEX (func_decl))
17732     {
17733       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17734
17735       if (host_integerp (DECL_VINDEX (func_decl), 0))
17736         add_AT_loc (die, DW_AT_vtable_elem_location,
17737                     new_loc_descr (DW_OP_constu,
17738                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17739                                    0));
17740
17741       /* GNU extension: Record what type this method came from originally.  */
17742       if (debug_info_level > DINFO_LEVEL_TERSE
17743           && DECL_CONTEXT (func_decl))
17744         add_AT_die_ref (die, DW_AT_containing_type,
17745                         lookup_type_die (DECL_CONTEXT (func_decl)));
17746     }
17747 }
17748 \f
17749 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17750    given decl.  This used to be a vendor extension until after DWARF 4
17751    standardized it.  */
17752
17753 static void
17754 add_linkage_attr (dw_die_ref die, tree decl)
17755 {
17756   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17757
17758   /* Mimic what assemble_name_raw does with a leading '*'.  */
17759   if (name[0] == '*')
17760     name = &name[1];
17761
17762   if (dwarf_version >= 4)
17763     add_AT_string (die, DW_AT_linkage_name, name);
17764   else
17765     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17766 }
17767
17768 /* Add source coordinate attributes for the given decl.  */
17769
17770 static void
17771 add_src_coords_attributes (dw_die_ref die, tree decl)
17772 {
17773   expanded_location s;
17774
17775   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17776     return;
17777   s = expand_location (DECL_SOURCE_LOCATION (decl));
17778   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17779   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17780 }
17781
17782 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17783
17784 static void
17785 add_linkage_name (dw_die_ref die, tree decl)
17786 {
17787   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17788        && TREE_PUBLIC (decl)
17789        && !DECL_ABSTRACT (decl)
17790        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17791        && die->die_tag != DW_TAG_member)
17792     {
17793       /* Defer until we have an assembler name set.  */
17794       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17795         {
17796           limbo_die_node *asm_name;
17797
17798           asm_name = ggc_alloc_cleared_limbo_die_node ();
17799           asm_name->die = die;
17800           asm_name->created_for = decl;
17801           asm_name->next = deferred_asm_name;
17802           deferred_asm_name = asm_name;
17803         }
17804       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17805         add_linkage_attr (die, decl);
17806     }
17807 }
17808
17809 /* Add a DW_AT_name attribute and source coordinate attribute for the
17810    given decl, but only if it actually has a name.  */
17811
17812 static void
17813 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17814 {
17815   tree decl_name;
17816
17817   decl_name = DECL_NAME (decl);
17818   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17819     {
17820       const char *name = dwarf2_name (decl, 0);
17821       if (name)
17822         add_name_attribute (die, name);
17823       if (! DECL_ARTIFICIAL (decl))
17824         add_src_coords_attributes (die, decl);
17825
17826       add_linkage_name (die, decl);
17827     }
17828
17829 #ifdef VMS_DEBUGGING_INFO
17830   /* Get the function's name, as described by its RTL.  This may be different
17831      from the DECL_NAME name used in the source file.  */
17832   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17833     {
17834       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17835                    XEXP (DECL_RTL (decl), 0));
17836       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17837     }
17838 #endif /* VMS_DEBUGGING_INFO */
17839 }
17840
17841 #ifdef VMS_DEBUGGING_INFO
17842 /* Output the debug main pointer die for VMS */
17843
17844 void
17845 dwarf2out_vms_debug_main_pointer (void)
17846 {
17847   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17848   dw_die_ref die;
17849
17850   /* Allocate the VMS debug main subprogram die.  */
17851   die = ggc_alloc_cleared_die_node ();
17852   die->die_tag = DW_TAG_subprogram;
17853   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17854   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17855                                current_function_funcdef_no);
17856   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17857
17858   /* Make it the first child of comp_unit_die ().  */
17859   die->die_parent = comp_unit_die ();
17860   if (comp_unit_die ()->die_child)
17861     {
17862       die->die_sib = comp_unit_die ()->die_child->die_sib;
17863       comp_unit_die ()->die_child->die_sib = die;
17864     }
17865   else
17866     {
17867       die->die_sib = die;
17868       comp_unit_die ()->die_child = die;
17869     }
17870 }
17871 #endif /* VMS_DEBUGGING_INFO */
17872
17873 /* Push a new declaration scope.  */
17874
17875 static void
17876 push_decl_scope (tree scope)
17877 {
17878   VEC_safe_push (tree, gc, decl_scope_table, scope);
17879 }
17880
17881 /* Pop a declaration scope.  */
17882
17883 static inline void
17884 pop_decl_scope (void)
17885 {
17886   VEC_pop (tree, decl_scope_table);
17887 }
17888
17889 /* Return the DIE for the scope that immediately contains this type.
17890    Non-named types get global scope.  Named types nested in other
17891    types get their containing scope if it's open, or global scope
17892    otherwise.  All other types (i.e. function-local named types) get
17893    the current active scope.  */
17894
17895 static dw_die_ref
17896 scope_die_for (tree t, dw_die_ref context_die)
17897 {
17898   dw_die_ref scope_die = NULL;
17899   tree containing_scope;
17900   int i;
17901
17902   /* Non-types always go in the current scope.  */
17903   gcc_assert (TYPE_P (t));
17904
17905   containing_scope = TYPE_CONTEXT (t);
17906
17907   /* Use the containing namespace if it was passed in (for a declaration).  */
17908   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17909     {
17910       if (context_die == lookup_decl_die (containing_scope))
17911         /* OK */;
17912       else
17913         containing_scope = NULL_TREE;
17914     }
17915
17916   /* Ignore function type "scopes" from the C frontend.  They mean that
17917      a tagged type is local to a parmlist of a function declarator, but
17918      that isn't useful to DWARF.  */
17919   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17920     containing_scope = NULL_TREE;
17921
17922   if (SCOPE_FILE_SCOPE_P (containing_scope))
17923     scope_die = comp_unit_die ();
17924   else if (TYPE_P (containing_scope))
17925     {
17926       /* For types, we can just look up the appropriate DIE.  But
17927          first we check to see if we're in the middle of emitting it
17928          so we know where the new DIE should go.  */
17929       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17930         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17931           break;
17932
17933       if (i < 0)
17934         {
17935           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17936                       || TREE_ASM_WRITTEN (containing_scope));
17937           /*We are not in the middle of emitting the type
17938             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17939           scope_die = lookup_type_die (containing_scope);
17940
17941           /* If none of the current dies are suitable, we get file scope.  */
17942           if (scope_die == NULL)
17943             scope_die = comp_unit_die ();
17944         }
17945       else
17946         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17947     }
17948   else
17949     scope_die = context_die;
17950
17951   return scope_die;
17952 }
17953
17954 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17955
17956 static inline int
17957 local_scope_p (dw_die_ref context_die)
17958 {
17959   for (; context_die; context_die = context_die->die_parent)
17960     if (context_die->die_tag == DW_TAG_inlined_subroutine
17961         || context_die->die_tag == DW_TAG_subprogram)
17962       return 1;
17963
17964   return 0;
17965 }
17966
17967 /* Returns nonzero if CONTEXT_DIE is a class.  */
17968
17969 static inline int
17970 class_scope_p (dw_die_ref context_die)
17971 {
17972   return (context_die
17973           && (context_die->die_tag == DW_TAG_structure_type
17974               || context_die->die_tag == DW_TAG_class_type
17975               || context_die->die_tag == DW_TAG_interface_type
17976               || context_die->die_tag == DW_TAG_union_type));
17977 }
17978
17979 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17980    whether or not to treat a DIE in this context as a declaration.  */
17981
17982 static inline int
17983 class_or_namespace_scope_p (dw_die_ref context_die)
17984 {
17985   return (class_scope_p (context_die)
17986           || (context_die && context_die->die_tag == DW_TAG_namespace));
17987 }
17988
17989 /* Many forms of DIEs require a "type description" attribute.  This
17990    routine locates the proper "type descriptor" die for the type given
17991    by 'type', and adds a DW_AT_type attribute below the given die.  */
17992
17993 static void
17994 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17995                     int decl_volatile, dw_die_ref context_die)
17996 {
17997   enum tree_code code  = TREE_CODE (type);
17998   dw_die_ref type_die  = NULL;
17999
18000   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18001      or fixed-point type, use the inner type.  This is because we have no
18002      support for unnamed types in base_type_die.  This can happen if this is
18003      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18004   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18005       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18006     type = TREE_TYPE (type), code = TREE_CODE (type);
18007
18008   if (code == ERROR_MARK
18009       /* Handle a special case.  For functions whose return type is void, we
18010          generate *no* type attribute.  (Note that no object may have type
18011          `void', so this only applies to function return types).  */
18012       || code == VOID_TYPE)
18013     return;
18014
18015   type_die = modified_type_die (type,
18016                                 decl_const || TYPE_READONLY (type),
18017                                 decl_volatile || TYPE_VOLATILE (type),
18018                                 context_die);
18019
18020   if (type_die != NULL)
18021     add_AT_die_ref (object_die, DW_AT_type, type_die);
18022 }
18023
18024 /* Given an object die, add the calling convention attribute for the
18025    function call type.  */
18026 static void
18027 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18028 {
18029   enum dwarf_calling_convention value = DW_CC_normal;
18030
18031   value = ((enum dwarf_calling_convention)
18032            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18033
18034   if (is_fortran ()
18035       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18036     {
18037       /* DWARF 2 doesn't provide a way to identify a program's source-level
18038         entry point.  DW_AT_calling_convention attributes are only meant
18039         to describe functions' calling conventions.  However, lacking a
18040         better way to signal the Fortran main program, we used this for 
18041         a long time, following existing custom.  Now, DWARF 4 has 
18042         DW_AT_main_subprogram, which we add below, but some tools still
18043         rely on the old way, which we thus keep.  */
18044       value = DW_CC_program;
18045
18046       if (dwarf_version >= 4 || !dwarf_strict)
18047         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18048     }
18049
18050   /* Only add the attribute if the backend requests it, and
18051      is not DW_CC_normal.  */
18052   if (value && (value != DW_CC_normal))
18053     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18054 }
18055
18056 /* Given a tree pointer to a struct, class, union, or enum type node, return
18057    a pointer to the (string) tag name for the given type, or zero if the type
18058    was declared without a tag.  */
18059
18060 static const char *
18061 type_tag (const_tree type)
18062 {
18063   const char *name = 0;
18064
18065   if (TYPE_NAME (type) != 0)
18066     {
18067       tree t = 0;
18068
18069       /* Find the IDENTIFIER_NODE for the type name.  */
18070       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18071           && !TYPE_NAMELESS (type))
18072         t = TYPE_NAME (type);
18073
18074       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18075          a TYPE_DECL node, regardless of whether or not a `typedef' was
18076          involved.  */
18077       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18078                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18079         {
18080           /* We want to be extra verbose.  Don't call dwarf_name if
18081              DECL_NAME isn't set.  The default hook for decl_printable_name
18082              doesn't like that, and in this context it's correct to return
18083              0, instead of "<anonymous>" or the like.  */
18084           if (DECL_NAME (TYPE_NAME (type))
18085               && !DECL_NAMELESS (TYPE_NAME (type)))
18086             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18087         }
18088
18089       /* Now get the name as a string, or invent one.  */
18090       if (!name && t != 0)
18091         name = IDENTIFIER_POINTER (t);
18092     }
18093
18094   return (name == 0 || *name == '\0') ? 0 : name;
18095 }
18096
18097 /* Return the type associated with a data member, make a special check
18098    for bit field types.  */
18099
18100 static inline tree
18101 member_declared_type (const_tree member)
18102 {
18103   return (DECL_BIT_FIELD_TYPE (member)
18104           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18105 }
18106
18107 /* Get the decl's label, as described by its RTL. This may be different
18108    from the DECL_NAME name used in the source file.  */
18109
18110 #if 0
18111 static const char *
18112 decl_start_label (tree decl)
18113 {
18114   rtx x;
18115   const char *fnname;
18116
18117   x = DECL_RTL (decl);
18118   gcc_assert (MEM_P (x));
18119
18120   x = XEXP (x, 0);
18121   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18122
18123   fnname = XSTR (x, 0);
18124   return fnname;
18125 }
18126 #endif
18127 \f
18128 /* These routines generate the internal representation of the DIE's for
18129    the compilation unit.  Debugging information is collected by walking
18130    the declaration trees passed in from dwarf2out_decl().  */
18131
18132 static void
18133 gen_array_type_die (tree type, dw_die_ref context_die)
18134 {
18135   dw_die_ref scope_die = scope_die_for (type, context_die);
18136   dw_die_ref array_die;
18137
18138   /* GNU compilers represent multidimensional array types as sequences of one
18139      dimensional array types whose element types are themselves array types.
18140      We sometimes squish that down to a single array_type DIE with multiple
18141      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18142      say that we are allowed to do this kind of compression in C, because
18143      there is no difference between an array of arrays and a multidimensional
18144      array.  We don't do this for Ada to remain as close as possible to the
18145      actual representation, which is especially important against the language
18146      flexibilty wrt arrays of variable size.  */
18147
18148   bool collapse_nested_arrays = !is_ada ();
18149   tree element_type;
18150
18151   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18152      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18153   if (TYPE_STRING_FLAG (type)
18154       && TREE_CODE (type) == ARRAY_TYPE
18155       && is_fortran ()
18156       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18157     {
18158       HOST_WIDE_INT size;
18159
18160       array_die = new_die (DW_TAG_string_type, scope_die, type);
18161       add_name_attribute (array_die, type_tag (type));
18162       equate_type_number_to_die (type, array_die);
18163       size = int_size_in_bytes (type);
18164       if (size >= 0)
18165         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18166       else if (TYPE_DOMAIN (type) != NULL_TREE
18167                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18168                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18169         {
18170           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18171           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18172
18173           size = int_size_in_bytes (TREE_TYPE (szdecl));
18174           if (loc && size > 0)
18175             {
18176               add_AT_location_description (array_die, DW_AT_string_length, loc);
18177               if (size != DWARF2_ADDR_SIZE)
18178                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18179             }
18180         }
18181       return;
18182     }
18183
18184   /* ??? The SGI dwarf reader fails for array of array of enum types
18185      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18186      array type comes before the outer array type.  We thus call gen_type_die
18187      before we new_die and must prevent nested array types collapsing for this
18188      target.  */
18189
18190 #ifdef MIPS_DEBUGGING_INFO
18191   gen_type_die (TREE_TYPE (type), context_die);
18192   collapse_nested_arrays = false;
18193 #endif
18194
18195   array_die = new_die (DW_TAG_array_type, scope_die, type);
18196   add_name_attribute (array_die, type_tag (type));
18197   equate_type_number_to_die (type, array_die);
18198
18199   if (TREE_CODE (type) == VECTOR_TYPE)
18200     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18201
18202   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18203   if (is_fortran ()
18204       && TREE_CODE (type) == ARRAY_TYPE
18205       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18206       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18207     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18208
18209 #if 0
18210   /* We default the array ordering.  SDB will probably do
18211      the right things even if DW_AT_ordering is not present.  It's not even
18212      an issue until we start to get into multidimensional arrays anyway.  If
18213      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18214      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18215      and when we find out that we need to put these in, we will only do so
18216      for multidimensional arrays.  */
18217   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18218 #endif
18219
18220 #ifdef MIPS_DEBUGGING_INFO
18221   /* The SGI compilers handle arrays of unknown bound by setting
18222      AT_declaration and not emitting any subrange DIEs.  */
18223   if (TREE_CODE (type) == ARRAY_TYPE
18224       && ! TYPE_DOMAIN (type))
18225     add_AT_flag (array_die, DW_AT_declaration, 1);
18226   else
18227 #endif
18228   if (TREE_CODE (type) == VECTOR_TYPE)
18229     {
18230       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18231       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18232       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18233       add_bound_info (subrange_die, DW_AT_upper_bound,
18234                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18235     }
18236   else
18237     add_subscript_info (array_die, type, collapse_nested_arrays);
18238
18239   /* Add representation of the type of the elements of this array type and
18240      emit the corresponding DIE if we haven't done it already.  */
18241   element_type = TREE_TYPE (type);
18242   if (collapse_nested_arrays)
18243     while (TREE_CODE (element_type) == ARRAY_TYPE)
18244       {
18245         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18246           break;
18247         element_type = TREE_TYPE (element_type);
18248       }
18249
18250 #ifndef MIPS_DEBUGGING_INFO
18251   gen_type_die (element_type, context_die);
18252 #endif
18253
18254   add_type_attribute (array_die, element_type, 0, 0, context_die);
18255
18256   if (get_AT (array_die, DW_AT_name))
18257     add_pubtype (type, array_die);
18258 }
18259
18260 static dw_loc_descr_ref
18261 descr_info_loc (tree val, tree base_decl)
18262 {
18263   HOST_WIDE_INT size;
18264   dw_loc_descr_ref loc, loc2;
18265   enum dwarf_location_atom op;
18266
18267   if (val == base_decl)
18268     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18269
18270   switch (TREE_CODE (val))
18271     {
18272     CASE_CONVERT:
18273       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18274     case VAR_DECL:
18275       return loc_descriptor_from_tree (val, 0);
18276     case INTEGER_CST:
18277       if (host_integerp (val, 0))
18278         return int_loc_descriptor (tree_low_cst (val, 0));
18279       break;
18280     case INDIRECT_REF:
18281       size = int_size_in_bytes (TREE_TYPE (val));
18282       if (size < 0)
18283         break;
18284       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18285       if (!loc)
18286         break;
18287       if (size == DWARF2_ADDR_SIZE)
18288         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18289       else
18290         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18291       return loc;
18292     case POINTER_PLUS_EXPR:
18293     case PLUS_EXPR:
18294       if (host_integerp (TREE_OPERAND (val, 1), 1)
18295           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18296              < 16384)
18297         {
18298           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18299           if (!loc)
18300             break;
18301           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18302         }
18303       else
18304         {
18305           op = DW_OP_plus;
18306         do_binop:
18307           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18308           if (!loc)
18309             break;
18310           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18311           if (!loc2)
18312             break;
18313           add_loc_descr (&loc, loc2);
18314           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18315         }
18316       return loc;
18317     case MINUS_EXPR:
18318       op = DW_OP_minus;
18319       goto do_binop;
18320     case MULT_EXPR:
18321       op = DW_OP_mul;
18322       goto do_binop;
18323     case EQ_EXPR:
18324       op = DW_OP_eq;
18325       goto do_binop;
18326     case NE_EXPR:
18327       op = DW_OP_ne;
18328       goto do_binop;
18329     default:
18330       break;
18331     }
18332   return NULL;
18333 }
18334
18335 static void
18336 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18337                       tree val, tree base_decl)
18338 {
18339   dw_loc_descr_ref loc;
18340
18341   if (host_integerp (val, 0))
18342     {
18343       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18344       return;
18345     }
18346
18347   loc = descr_info_loc (val, base_decl);
18348   if (!loc)
18349     return;
18350
18351   add_AT_loc (die, attr, loc);
18352 }
18353
18354 /* This routine generates DIE for array with hidden descriptor, details
18355    are filled into *info by a langhook.  */
18356
18357 static void
18358 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18359                           dw_die_ref context_die)
18360 {
18361   dw_die_ref scope_die = scope_die_for (type, context_die);
18362   dw_die_ref array_die;
18363   int dim;
18364
18365   array_die = new_die (DW_TAG_array_type, scope_die, type);
18366   add_name_attribute (array_die, type_tag (type));
18367   equate_type_number_to_die (type, array_die);
18368
18369   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18370   if (is_fortran ()
18371       && info->ndimensions >= 2)
18372     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18373
18374   if (info->data_location)
18375     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18376                           info->base_decl);
18377   if (info->associated)
18378     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18379                           info->base_decl);
18380   if (info->allocated)
18381     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18382                           info->base_decl);
18383
18384   for (dim = 0; dim < info->ndimensions; dim++)
18385     {
18386       dw_die_ref subrange_die
18387         = new_die (DW_TAG_subrange_type, array_die, NULL);
18388
18389       if (info->dimen[dim].lower_bound)
18390         {
18391           /* If it is the default value, omit it.  */
18392           int dflt;
18393
18394           if (host_integerp (info->dimen[dim].lower_bound, 0)
18395               && (dflt = lower_bound_default ()) != -1
18396               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18397             ;
18398           else
18399             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18400                                   info->dimen[dim].lower_bound,
18401                                   info->base_decl);
18402         }
18403       if (info->dimen[dim].upper_bound)
18404         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18405                               info->dimen[dim].upper_bound,
18406                               info->base_decl);
18407       if (info->dimen[dim].stride)
18408         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18409                               info->dimen[dim].stride,
18410                               info->base_decl);
18411     }
18412
18413   gen_type_die (info->element_type, context_die);
18414   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18415
18416   if (get_AT (array_die, DW_AT_name))
18417     add_pubtype (type, array_die);
18418 }
18419
18420 #if 0
18421 static void
18422 gen_entry_point_die (tree decl, dw_die_ref context_die)
18423 {
18424   tree origin = decl_ultimate_origin (decl);
18425   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18426
18427   if (origin != NULL)
18428     add_abstract_origin_attribute (decl_die, origin);
18429   else
18430     {
18431       add_name_and_src_coords_attributes (decl_die, decl);
18432       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18433                           0, 0, context_die);
18434     }
18435
18436   if (DECL_ABSTRACT (decl))
18437     equate_decl_number_to_die (decl, decl_die);
18438   else
18439     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18440 }
18441 #endif
18442
18443 /* Walk through the list of incomplete types again, trying once more to
18444    emit full debugging info for them.  */
18445
18446 static void
18447 retry_incomplete_types (void)
18448 {
18449   int i;
18450
18451   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18452     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18453                                   DINFO_USAGE_DIR_USE))
18454       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18455 }
18456
18457 /* Determine what tag to use for a record type.  */
18458
18459 static enum dwarf_tag
18460 record_type_tag (tree type)
18461 {
18462   if (! lang_hooks.types.classify_record)
18463     return DW_TAG_structure_type;
18464
18465   switch (lang_hooks.types.classify_record (type))
18466     {
18467     case RECORD_IS_STRUCT:
18468       return DW_TAG_structure_type;
18469
18470     case RECORD_IS_CLASS:
18471       return DW_TAG_class_type;
18472
18473     case RECORD_IS_INTERFACE:
18474       if (dwarf_version >= 3 || !dwarf_strict)
18475         return DW_TAG_interface_type;
18476       return DW_TAG_structure_type;
18477
18478     default:
18479       gcc_unreachable ();
18480     }
18481 }
18482
18483 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18484    include all of the information about the enumeration values also. Each
18485    enumerated type name/value is listed as a child of the enumerated type
18486    DIE.  */
18487
18488 static dw_die_ref
18489 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18490 {
18491   dw_die_ref type_die = lookup_type_die (type);
18492
18493   if (type_die == NULL)
18494     {
18495       type_die = new_die (DW_TAG_enumeration_type,
18496                           scope_die_for (type, context_die), type);
18497       equate_type_number_to_die (type, type_die);
18498       add_name_attribute (type_die, type_tag (type));
18499       if (dwarf_version >= 4 || !dwarf_strict)
18500         {
18501           if (ENUM_IS_SCOPED (type))
18502             add_AT_flag (type_die, DW_AT_enum_class, 1);
18503           if (ENUM_IS_OPAQUE (type))
18504             add_AT_flag (type_die, DW_AT_declaration, 1);
18505         }
18506     }
18507   else if (! TYPE_SIZE (type))
18508     return type_die;
18509   else
18510     remove_AT (type_die, DW_AT_declaration);
18511
18512   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18513      given enum type is incomplete, do not generate the DW_AT_byte_size
18514      attribute or the DW_AT_element_list attribute.  */
18515   if (TYPE_SIZE (type))
18516     {
18517       tree link;
18518
18519       TREE_ASM_WRITTEN (type) = 1;
18520       add_byte_size_attribute (type_die, type);
18521       if (TYPE_STUB_DECL (type) != NULL_TREE)
18522         {
18523           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18524           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18525         }
18526
18527       /* If the first reference to this type was as the return type of an
18528          inline function, then it may not have a parent.  Fix this now.  */
18529       if (type_die->die_parent == NULL)
18530         add_child_die (scope_die_for (type, context_die), type_die);
18531
18532       for (link = TYPE_VALUES (type);
18533            link != NULL; link = TREE_CHAIN (link))
18534         {
18535           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18536           tree value = TREE_VALUE (link);
18537
18538           add_name_attribute (enum_die,
18539                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18540
18541           if (TREE_CODE (value) == CONST_DECL)
18542             value = DECL_INITIAL (value);
18543
18544           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18545             /* DWARF2 does not provide a way of indicating whether or
18546                not enumeration constants are signed or unsigned.  GDB
18547                always assumes the values are signed, so we output all
18548                values as if they were signed.  That means that
18549                enumeration constants with very large unsigned values
18550                will appear to have negative values in the debugger.  */
18551             add_AT_int (enum_die, DW_AT_const_value,
18552                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18553         }
18554     }
18555   else
18556     add_AT_flag (type_die, DW_AT_declaration, 1);
18557
18558   if (get_AT (type_die, DW_AT_name))
18559     add_pubtype (type, type_die);
18560
18561   return type_die;
18562 }
18563
18564 /* Generate a DIE to represent either a real live formal parameter decl or to
18565    represent just the type of some formal parameter position in some function
18566    type.
18567
18568    Note that this routine is a bit unusual because its argument may be a
18569    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18570    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18571    node.  If it's the former then this function is being called to output a
18572    DIE to represent a formal parameter object (or some inlining thereof).  If
18573    it's the latter, then this function is only being called to output a
18574    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18575    argument type of some subprogram type.
18576    If EMIT_NAME_P is true, name and source coordinate attributes
18577    are emitted.  */
18578
18579 static dw_die_ref
18580 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18581                           dw_die_ref context_die)
18582 {
18583   tree node_or_origin = node ? node : origin;
18584   tree ultimate_origin;
18585   dw_die_ref parm_die
18586     = new_die (DW_TAG_formal_parameter, context_die, node);
18587
18588   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18589     {
18590     case tcc_declaration:
18591       ultimate_origin = decl_ultimate_origin (node_or_origin);
18592       if (node || ultimate_origin)
18593         origin = ultimate_origin;
18594       if (origin != NULL)
18595         add_abstract_origin_attribute (parm_die, origin);
18596       else if (emit_name_p)
18597         add_name_and_src_coords_attributes (parm_die, node);
18598       if (origin == NULL
18599           || (! DECL_ABSTRACT (node_or_origin)
18600               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18601                                            decl_function_context
18602                                                             (node_or_origin))))
18603         {
18604           tree type = TREE_TYPE (node_or_origin);
18605           if (decl_by_reference_p (node_or_origin))
18606             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18607                                 context_die);
18608           else
18609             add_type_attribute (parm_die, type,
18610                                 TREE_READONLY (node_or_origin),
18611                                 TREE_THIS_VOLATILE (node_or_origin),
18612                                 context_die);
18613         }
18614       if (origin == NULL && DECL_ARTIFICIAL (node))
18615         add_AT_flag (parm_die, DW_AT_artificial, 1);
18616
18617       if (node && node != origin)
18618         equate_decl_number_to_die (node, parm_die);
18619       if (! DECL_ABSTRACT (node_or_origin))
18620         add_location_or_const_value_attribute (parm_die, node_or_origin,
18621                                                DW_AT_location);
18622
18623       break;
18624
18625     case tcc_type:
18626       /* We were called with some kind of a ..._TYPE node.  */
18627       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18628       break;
18629
18630     default:
18631       gcc_unreachable ();
18632     }
18633
18634   return parm_die;
18635 }
18636
18637 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18638    children DW_TAG_formal_parameter DIEs representing the arguments of the
18639    parameter pack.
18640
18641    PARM_PACK must be a function parameter pack.
18642    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18643    must point to the subsequent arguments of the function PACK_ARG belongs to.
18644    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18645    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18646    following the last one for which a DIE was generated.  */
18647
18648 static dw_die_ref
18649 gen_formal_parameter_pack_die  (tree parm_pack,
18650                                 tree pack_arg,
18651                                 dw_die_ref subr_die,
18652                                 tree *next_arg)
18653 {
18654   tree arg;
18655   dw_die_ref parm_pack_die;
18656
18657   gcc_assert (parm_pack
18658               && lang_hooks.function_parameter_pack_p (parm_pack)
18659               && subr_die);
18660
18661   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18662   add_src_coords_attributes (parm_pack_die, parm_pack);
18663
18664   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18665     {
18666       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18667                                                                  parm_pack))
18668         break;
18669       gen_formal_parameter_die (arg, NULL,
18670                                 false /* Don't emit name attribute.  */,
18671                                 parm_pack_die);
18672     }
18673   if (next_arg)
18674     *next_arg = arg;
18675   return parm_pack_die;
18676 }
18677
18678 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18679    at the end of an (ANSI prototyped) formal parameters list.  */
18680
18681 static void
18682 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18683 {
18684   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18685 }
18686
18687 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18688    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18689    parameters as specified in some function type specification (except for
18690    those which appear as part of a function *definition*).  */
18691
18692 static void
18693 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18694 {
18695   tree link;
18696   tree formal_type = NULL;
18697   tree first_parm_type;
18698   tree arg;
18699
18700   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18701     {
18702       arg = DECL_ARGUMENTS (function_or_method_type);
18703       function_or_method_type = TREE_TYPE (function_or_method_type);
18704     }
18705   else
18706     arg = NULL_TREE;
18707
18708   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18709
18710   /* Make our first pass over the list of formal parameter types and output a
18711      DW_TAG_formal_parameter DIE for each one.  */
18712   for (link = first_parm_type; link; )
18713     {
18714       dw_die_ref parm_die;
18715
18716       formal_type = TREE_VALUE (link);
18717       if (formal_type == void_type_node)
18718         break;
18719
18720       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18721       parm_die = gen_formal_parameter_die (formal_type, NULL,
18722                                            true /* Emit name attribute.  */,
18723                                            context_die);
18724       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18725           && link == first_parm_type)
18726         {
18727           add_AT_flag (parm_die, DW_AT_artificial, 1);
18728           if (dwarf_version >= 3 || !dwarf_strict)
18729             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18730         }
18731       else if (arg && DECL_ARTIFICIAL (arg))
18732         add_AT_flag (parm_die, DW_AT_artificial, 1);
18733
18734       link = TREE_CHAIN (link);
18735       if (arg)
18736         arg = DECL_CHAIN (arg);
18737     }
18738
18739   /* If this function type has an ellipsis, add a
18740      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18741   if (formal_type != void_type_node)
18742     gen_unspecified_parameters_die (function_or_method_type, context_die);
18743
18744   /* Make our second (and final) pass over the list of formal parameter types
18745      and output DIEs to represent those types (as necessary).  */
18746   for (link = TYPE_ARG_TYPES (function_or_method_type);
18747        link && TREE_VALUE (link);
18748        link = TREE_CHAIN (link))
18749     gen_type_die (TREE_VALUE (link), context_die);
18750 }
18751
18752 /* We want to generate the DIE for TYPE so that we can generate the
18753    die for MEMBER, which has been defined; we will need to refer back
18754    to the member declaration nested within TYPE.  If we're trying to
18755    generate minimal debug info for TYPE, processing TYPE won't do the
18756    trick; we need to attach the member declaration by hand.  */
18757
18758 static void
18759 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18760 {
18761   gen_type_die (type, context_die);
18762
18763   /* If we're trying to avoid duplicate debug info, we may not have
18764      emitted the member decl for this function.  Emit it now.  */
18765   if (TYPE_STUB_DECL (type)
18766       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18767       && ! lookup_decl_die (member))
18768     {
18769       dw_die_ref type_die;
18770       gcc_assert (!decl_ultimate_origin (member));
18771
18772       push_decl_scope (type);
18773       type_die = lookup_type_die_strip_naming_typedef (type);
18774       if (TREE_CODE (member) == FUNCTION_DECL)
18775         gen_subprogram_die (member, type_die);
18776       else if (TREE_CODE (member) == FIELD_DECL)
18777         {
18778           /* Ignore the nameless fields that are used to skip bits but handle
18779              C++ anonymous unions and structs.  */
18780           if (DECL_NAME (member) != NULL_TREE
18781               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18782               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18783             {
18784               gen_type_die (member_declared_type (member), type_die);
18785               gen_field_die (member, type_die);
18786             }
18787         }
18788       else
18789         gen_variable_die (member, NULL_TREE, type_die);
18790
18791       pop_decl_scope ();
18792     }
18793 }
18794
18795 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18796    may later generate inlined and/or out-of-line instances of.  */
18797
18798 static void
18799 dwarf2out_abstract_function (tree decl)
18800 {
18801   dw_die_ref old_die;
18802   tree save_fn;
18803   tree context;
18804   int was_abstract;
18805   htab_t old_decl_loc_table;
18806   int old_call_site_count, old_tail_call_site_count;
18807   struct call_arg_loc_node *old_call_arg_locations;
18808
18809   /* Make sure we have the actual abstract inline, not a clone.  */
18810   decl = DECL_ORIGIN (decl);
18811
18812   old_die = lookup_decl_die (decl);
18813   if (old_die && get_AT (old_die, DW_AT_inline))
18814     /* We've already generated the abstract instance.  */
18815     return;
18816
18817   /* We can be called while recursively when seeing block defining inlined subroutine
18818      DIE.  Be sure to not clobber the outer location table nor use it or we would
18819      get locations in abstract instantces.  */
18820   old_decl_loc_table = decl_loc_table;
18821   decl_loc_table = NULL;
18822   old_call_arg_locations = call_arg_locations;
18823   call_arg_locations = NULL;
18824   old_call_site_count = call_site_count;
18825   call_site_count = -1;
18826   old_tail_call_site_count = tail_call_site_count;
18827   tail_call_site_count = -1;
18828
18829   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18830      we don't get confused by DECL_ABSTRACT.  */
18831   if (debug_info_level > DINFO_LEVEL_TERSE)
18832     {
18833       context = decl_class_context (decl);
18834       if (context)
18835         gen_type_die_for_member
18836           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18837     }
18838
18839   /* Pretend we've just finished compiling this function.  */
18840   save_fn = current_function_decl;
18841   current_function_decl = decl;
18842   push_cfun (DECL_STRUCT_FUNCTION (decl));
18843
18844   was_abstract = DECL_ABSTRACT (decl);
18845   set_decl_abstract_flags (decl, 1);
18846   dwarf2out_decl (decl);
18847   if (! was_abstract)
18848     set_decl_abstract_flags (decl, 0);
18849
18850   current_function_decl = save_fn;
18851   decl_loc_table = old_decl_loc_table;
18852   call_arg_locations = old_call_arg_locations;
18853   call_site_count = old_call_site_count;
18854   tail_call_site_count = old_tail_call_site_count;
18855   pop_cfun ();
18856 }
18857
18858 /* Helper function of premark_used_types() which gets called through
18859    htab_traverse.
18860
18861    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18862    marked as unused by prune_unused_types.  */
18863
18864 static int
18865 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18866 {
18867   tree type;
18868   dw_die_ref die;
18869
18870   type = (tree) *slot;
18871   die = lookup_type_die (type);
18872   if (die != NULL)
18873     die->die_perennial_p = 1;
18874   return 1;
18875 }
18876
18877 /* Helper function of premark_types_used_by_global_vars which gets called
18878    through htab_traverse.
18879
18880    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18881    marked as unused by prune_unused_types. The DIE of the type is marked
18882    only if the global variable using the type will actually be emitted.  */
18883
18884 static int
18885 premark_types_used_by_global_vars_helper (void **slot,
18886                                           void *data ATTRIBUTE_UNUSED)
18887 {
18888   struct types_used_by_vars_entry *entry;
18889   dw_die_ref die;
18890
18891   entry = (struct types_used_by_vars_entry *) *slot;
18892   gcc_assert (entry->type != NULL
18893               && entry->var_decl != NULL);
18894   die = lookup_type_die (entry->type);
18895   if (die)
18896     {
18897       /* Ask cgraph if the global variable really is to be emitted.
18898          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18899       struct varpool_node *node = varpool_get_node (entry->var_decl);
18900       if (node && node->needed)
18901         {
18902           die->die_perennial_p = 1;
18903           /* Keep the parent DIEs as well.  */
18904           while ((die = die->die_parent) && die->die_perennial_p == 0)
18905             die->die_perennial_p = 1;
18906         }
18907     }
18908   return 1;
18909 }
18910
18911 /* Mark all members of used_types_hash as perennial.  */
18912
18913 static void
18914 premark_used_types (void)
18915 {
18916   if (cfun && cfun->used_types_hash)
18917     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18918 }
18919
18920 /* Mark all members of types_used_by_vars_entry as perennial.  */
18921
18922 static void
18923 premark_types_used_by_global_vars (void)
18924 {
18925   if (types_used_by_vars_hash)
18926     htab_traverse (types_used_by_vars_hash,
18927                    premark_types_used_by_global_vars_helper, NULL);
18928 }
18929
18930 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18931    for CA_LOC call arg loc node.  */
18932
18933 static dw_die_ref
18934 gen_call_site_die (tree decl, dw_die_ref subr_die,
18935                    struct call_arg_loc_node *ca_loc)
18936 {
18937   dw_die_ref stmt_die = NULL, die;
18938   tree block = ca_loc->block;
18939
18940   while (block
18941          && block != DECL_INITIAL (decl)
18942          && TREE_CODE (block) == BLOCK)
18943     {
18944       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
18945         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
18946       if (stmt_die)
18947         break;
18948       block = BLOCK_SUPERCONTEXT (block);
18949     }
18950   if (stmt_die == NULL)
18951     stmt_die = subr_die;
18952   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18953   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18954   if (ca_loc->tail_call_p)
18955     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18956   if (ca_loc->symbol_ref)
18957     {
18958       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18959       if (tdie)
18960         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18961       else
18962         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
18963     }
18964   return die;
18965 }
18966
18967 /* Generate a DIE to represent a declared function (either file-scope or
18968    block-local).  */
18969
18970 static void
18971 gen_subprogram_die (tree decl, dw_die_ref context_die)
18972 {
18973   tree origin = decl_ultimate_origin (decl);
18974   dw_die_ref subr_die;
18975   tree outer_scope;
18976   dw_die_ref old_die = lookup_decl_die (decl);
18977   int declaration = (current_function_decl != decl
18978                      || class_or_namespace_scope_p (context_die));
18979
18980   premark_used_types ();
18981
18982   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18983      started to generate the abstract instance of an inline, decided to output
18984      its containing class, and proceeded to emit the declaration of the inline
18985      from the member list for the class.  If so, DECLARATION takes priority;
18986      we'll get back to the abstract instance when done with the class.  */
18987
18988   /* The class-scope declaration DIE must be the primary DIE.  */
18989   if (origin && declaration && class_or_namespace_scope_p (context_die))
18990     {
18991       origin = NULL;
18992       gcc_assert (!old_die);
18993     }
18994
18995   /* Now that the C++ front end lazily declares artificial member fns, we
18996      might need to retrofit the declaration into its class.  */
18997   if (!declaration && !origin && !old_die
18998       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18999       && !class_or_namespace_scope_p (context_die)
19000       && debug_info_level > DINFO_LEVEL_TERSE)
19001     old_die = force_decl_die (decl);
19002
19003   if (origin != NULL)
19004     {
19005       gcc_assert (!declaration || local_scope_p (context_die));
19006
19007       /* Fixup die_parent for the abstract instance of a nested
19008          inline function.  */
19009       if (old_die && old_die->die_parent == NULL)
19010         add_child_die (context_die, old_die);
19011
19012       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19013       add_abstract_origin_attribute (subr_die, origin);
19014     }
19015   else if (old_die)
19016     {
19017       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19018       struct dwarf_file_data * file_index = lookup_filename (s.file);
19019
19020       if (!get_AT_flag (old_die, DW_AT_declaration)
19021           /* We can have a normal definition following an inline one in the
19022              case of redefinition of GNU C extern inlines.
19023              It seems reasonable to use AT_specification in this case.  */
19024           && !get_AT (old_die, DW_AT_inline))
19025         {
19026           /* Detect and ignore this case, where we are trying to output
19027              something we have already output.  */
19028           return;
19029         }
19030
19031       /* If the definition comes from the same place as the declaration,
19032          maybe use the old DIE.  We always want the DIE for this function
19033          that has the *_pc attributes to be under comp_unit_die so the
19034          debugger can find it.  We also need to do this for abstract
19035          instances of inlines, since the spec requires the out-of-line copy
19036          to have the same parent.  For local class methods, this doesn't
19037          apply; we just use the old DIE.  */
19038       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19039           && (DECL_ARTIFICIAL (decl)
19040               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19041                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19042                       == (unsigned) s.line))))
19043         {
19044           subr_die = old_die;
19045
19046           /* Clear out the declaration attribute and the formal parameters.
19047              Do not remove all children, because it is possible that this
19048              declaration die was forced using force_decl_die(). In such
19049              cases die that forced declaration die (e.g. TAG_imported_module)
19050              is one of the children that we do not want to remove.  */
19051           remove_AT (subr_die, DW_AT_declaration);
19052           remove_AT (subr_die, DW_AT_object_pointer);
19053           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19054         }
19055       else
19056         {
19057           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19058           add_AT_specification (subr_die, old_die);
19059           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19060             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19061           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19062             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19063         }
19064     }
19065   else
19066     {
19067       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19068
19069       if (TREE_PUBLIC (decl))
19070         add_AT_flag (subr_die, DW_AT_external, 1);
19071
19072       add_name_and_src_coords_attributes (subr_die, decl);
19073       if (debug_info_level > DINFO_LEVEL_TERSE)
19074         {
19075           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19076           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19077                               0, 0, context_die);
19078         }
19079
19080       add_pure_or_virtual_attribute (subr_die, decl);
19081       if (DECL_ARTIFICIAL (decl))
19082         add_AT_flag (subr_die, DW_AT_artificial, 1);
19083
19084       add_accessibility_attribute (subr_die, decl);
19085     }
19086
19087   if (declaration)
19088     {
19089       if (!old_die || !get_AT (old_die, DW_AT_inline))
19090         {
19091           add_AT_flag (subr_die, DW_AT_declaration, 1);
19092
19093           /* If this is an explicit function declaration then generate
19094              a DW_AT_explicit attribute.  */
19095           if (lang_hooks.decls.function_decl_explicit_p (decl)
19096               && (dwarf_version >= 3 || !dwarf_strict))
19097             add_AT_flag (subr_die, DW_AT_explicit, 1);
19098
19099           /* The first time we see a member function, it is in the context of
19100              the class to which it belongs.  We make sure of this by emitting
19101              the class first.  The next time is the definition, which is
19102              handled above.  The two may come from the same source text.
19103
19104              Note that force_decl_die() forces function declaration die. It is
19105              later reused to represent definition.  */
19106           equate_decl_number_to_die (decl, subr_die);
19107         }
19108     }
19109   else if (DECL_ABSTRACT (decl))
19110     {
19111       if (DECL_DECLARED_INLINE_P (decl))
19112         {
19113           if (cgraph_function_possibly_inlined_p (decl))
19114             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19115           else
19116             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19117         }
19118       else
19119         {
19120           if (cgraph_function_possibly_inlined_p (decl))
19121             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19122           else
19123             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19124         }
19125
19126       if (DECL_DECLARED_INLINE_P (decl)
19127           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19128         add_AT_flag (subr_die, DW_AT_artificial, 1);
19129
19130       equate_decl_number_to_die (decl, subr_die);
19131     }
19132   else if (!DECL_EXTERNAL (decl))
19133     {
19134       HOST_WIDE_INT cfa_fb_offset;
19135
19136       if (!old_die || !get_AT (old_die, DW_AT_inline))
19137         equate_decl_number_to_die (decl, subr_die);
19138
19139       if (!flag_reorder_blocks_and_partition)
19140         {
19141           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19142           if (fde->dw_fde_begin)
19143             {
19144               /* We have already generated the labels.  */
19145               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19146               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19147             }
19148           else
19149             {
19150               /* Create start/end labels and add the range.  */
19151               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19152               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19153                                            current_function_funcdef_no);
19154               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19155               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19156                                            current_function_funcdef_no);
19157               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19158             }
19159
19160 #if VMS_DEBUGGING_INFO
19161       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19162          Section 2.3 Prologue and Epilogue Attributes:
19163          When a breakpoint is set on entry to a function, it is generally
19164          desirable for execution to be suspended, not on the very first
19165          instruction of the function, but rather at a point after the
19166          function's frame has been set up, after any language defined local
19167          declaration processing has been completed, and before execution of
19168          the first statement of the function begins. Debuggers generally
19169          cannot properly determine where this point is.  Similarly for a
19170          breakpoint set on exit from a function. The prologue and epilogue
19171          attributes allow a compiler to communicate the location(s) to use.  */
19172
19173       {
19174         if (fde->dw_fde_vms_end_prologue)
19175           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19176             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19177
19178         if (fde->dw_fde_vms_begin_epilogue)
19179           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19180             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19181       }
19182 #endif
19183
19184           add_pubname (decl, subr_die);
19185           add_arange (decl, subr_die);
19186         }
19187       else
19188         {  /* Generate pubnames entries for the split function code
19189               ranges.  */
19190           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19191
19192           if (fde->dw_fde_switched_sections)
19193             {
19194               if (dwarf_version >= 3 || !dwarf_strict)
19195                 {
19196                   /* We should use ranges for non-contiguous code section 
19197                      addresses.  Use the actual code range for the initial
19198                      section, since the HOT/COLD labels might precede an 
19199                      alignment offset.  */
19200                   bool range_list_added = false;
19201                   if (fde->in_std_section)
19202                     {
19203                       add_ranges_by_labels (subr_die,
19204                                             fde->dw_fde_begin,
19205                                             fde->dw_fde_end,
19206                                             &range_list_added);
19207                       add_ranges_by_labels (subr_die,
19208                                             fde->dw_fde_unlikely_section_label,
19209                                             fde->dw_fde_unlikely_section_end_label,
19210                                             &range_list_added);
19211                     }
19212                   else
19213                     {
19214                       add_ranges_by_labels (subr_die,
19215                                             fde->dw_fde_begin,
19216                                             fde->dw_fde_end,
19217                                             &range_list_added);
19218                       add_ranges_by_labels (subr_die,
19219                                             fde->dw_fde_hot_section_label,
19220                                             fde->dw_fde_hot_section_end_label,
19221                                             &range_list_added);
19222                     }
19223                   add_pubname (decl, subr_die);
19224                   if (range_list_added)
19225                     add_ranges (NULL);
19226                 }
19227               else
19228                 {
19229                   /* There is no real support in DW2 for this .. so we make
19230                      a work-around.  First, emit the pub name for the segment
19231                      containing the function label.  Then make and emit a
19232                      simplified subprogram DIE for the second segment with the
19233                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19234                      linkage name for the second die so that gdb will find both
19235                      sections when given "b foo".  */
19236                   const char *name = NULL;
19237                   tree decl_name = DECL_NAME (decl);
19238                   dw_die_ref seg_die;
19239
19240                   /* Do the 'primary' section.   */
19241                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19242                                  fde->dw_fde_begin);
19243                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19244                                  fde->dw_fde_end);
19245                   /* Add it.   */
19246                   add_pubname (decl, subr_die);
19247                   add_arange (decl, subr_die);
19248
19249                   /* Build a minimal DIE for the secondary section.  */
19250                   seg_die = new_die (DW_TAG_subprogram,
19251                                      subr_die->die_parent, decl);
19252
19253                   if (TREE_PUBLIC (decl))
19254                     add_AT_flag (seg_die, DW_AT_external, 1);
19255
19256                   if (decl_name != NULL 
19257                       && IDENTIFIER_POINTER (decl_name) != NULL)
19258                     {
19259                       name = dwarf2_name (decl, 1);
19260                       if (! DECL_ARTIFICIAL (decl))
19261                         add_src_coords_attributes (seg_die, decl);
19262
19263                       add_linkage_name (seg_die, decl);
19264                     }
19265                   gcc_assert (name!=NULL);
19266                   add_pure_or_virtual_attribute (seg_die, decl);
19267                   if (DECL_ARTIFICIAL (decl))
19268                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19269
19270                   if (fde->in_std_section)
19271                     {
19272                       name = concat ("__cold_sect_of_", name, NULL); 
19273                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19274                                      fde->dw_fde_unlikely_section_label);
19275                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19276                                      fde->dw_fde_unlikely_section_end_label); 
19277                     }
19278                   else 
19279                     {
19280                       name = concat ("__hot_sect_of_", name, NULL); 
19281                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19282                                      fde->dw_fde_hot_section_label);
19283                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19284                                      fde->dw_fde_hot_section_end_label); 
19285                     }
19286                   add_name_attribute (seg_die, name);
19287                   add_pubname_string (name, seg_die);
19288                   add_arange (decl, seg_die);
19289                 }
19290             }
19291           else
19292             {
19293               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19294               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19295               add_pubname (decl, subr_die);
19296               add_arange (decl, subr_die);
19297             }
19298         }
19299
19300 #ifdef MIPS_DEBUGGING_INFO
19301       /* Add a reference to the FDE for this routine.  */
19302       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19303 #endif
19304
19305       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19306
19307       /* We define the "frame base" as the function's CFA.  This is more
19308          convenient for several reasons: (1) It's stable across the prologue
19309          and epilogue, which makes it better than just a frame pointer,
19310          (2) With dwarf3, there exists a one-byte encoding that allows us
19311          to reference the .debug_frame data by proxy, but failing that,
19312          (3) We can at least reuse the code inspection and interpretation
19313          code that determines the CFA position at various points in the
19314          function.  */
19315       if (dwarf_version >= 3)
19316         {
19317           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19318           add_AT_loc (subr_die, DW_AT_frame_base, op);
19319         }
19320       else
19321         {
19322           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19323           if (list->dw_loc_next)
19324             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19325           else
19326             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19327         }
19328
19329       /* Compute a displacement from the "steady-state frame pointer" to
19330          the CFA.  The former is what all stack slots and argument slots
19331          will reference in the rtl; the later is what we've told the
19332          debugger about.  We'll need to adjust all frame_base references
19333          by this displacement.  */
19334       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19335
19336       if (cfun->static_chain_decl)
19337         add_AT_location_description (subr_die, DW_AT_static_link,
19338                  loc_list_from_tree (cfun->static_chain_decl, 2));
19339     }
19340
19341   /* Generate child dies for template paramaters.  */
19342   if (debug_info_level > DINFO_LEVEL_TERSE)
19343     gen_generic_params_dies (decl);
19344
19345   /* Now output descriptions of the arguments for this function. This gets
19346      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19347      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19348      `...' at the end of the formal parameter list.  In order to find out if
19349      there was a trailing ellipsis or not, we must instead look at the type
19350      associated with the FUNCTION_DECL.  This will be a node of type
19351      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19352      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19353      an ellipsis at the end.  */
19354
19355   /* In the case where we are describing a mere function declaration, all we
19356      need to do here (and all we *can* do here) is to describe the *types* of
19357      its formal parameters.  */
19358   if (debug_info_level <= DINFO_LEVEL_TERSE)
19359     ;
19360   else if (declaration)
19361     gen_formal_types_die (decl, subr_die);
19362   else
19363     {
19364       /* Generate DIEs to represent all known formal parameters.  */
19365       tree parm = DECL_ARGUMENTS (decl);
19366       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19367       tree generic_decl_parm = generic_decl
19368                                 ? DECL_ARGUMENTS (generic_decl)
19369                                 : NULL;
19370
19371       /* Now we want to walk the list of parameters of the function and
19372          emit their relevant DIEs.
19373
19374          We consider the case of DECL being an instance of a generic function
19375          as well as it being a normal function.
19376
19377          If DECL is an instance of a generic function we walk the
19378          parameters of the generic function declaration _and_ the parameters of
19379          DECL itself. This is useful because we want to emit specific DIEs for
19380          function parameter packs and those are declared as part of the
19381          generic function declaration. In that particular case,
19382          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19383          That DIE has children DIEs representing the set of arguments
19384          of the pack. Note that the set of pack arguments can be empty.
19385          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19386          children DIE.
19387
19388          Otherwise, we just consider the parameters of DECL.  */
19389       while (generic_decl_parm || parm)
19390         {
19391           if (generic_decl_parm
19392               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19393             gen_formal_parameter_pack_die (generic_decl_parm,
19394                                            parm, subr_die,
19395                                            &parm);
19396           else if (parm)
19397             {
19398               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19399
19400               if (parm == DECL_ARGUMENTS (decl)
19401                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19402                   && parm_die
19403                   && (dwarf_version >= 3 || !dwarf_strict))
19404                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19405
19406               parm = DECL_CHAIN (parm);
19407             }
19408
19409           if (generic_decl_parm)
19410             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19411         }
19412
19413       /* Decide whether we need an unspecified_parameters DIE at the end.
19414          There are 2 more cases to do this for: 1) the ansi ... declaration -
19415          this is detectable when the end of the arg list is not a
19416          void_type_node 2) an unprototyped function declaration (not a
19417          definition).  This just means that we have no info about the
19418          parameters at all.  */
19419       if (prototype_p (TREE_TYPE (decl)))
19420         {
19421           /* This is the prototyped case, check for....  */
19422           if (stdarg_p (TREE_TYPE (decl)))
19423             gen_unspecified_parameters_die (decl, subr_die);
19424         }
19425       else if (DECL_INITIAL (decl) == NULL_TREE)
19426         gen_unspecified_parameters_die (decl, subr_die);
19427     }
19428
19429   /* Output Dwarf info for all of the stuff within the body of the function
19430      (if it has one - it may be just a declaration).  */
19431   outer_scope = DECL_INITIAL (decl);
19432
19433   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19434      a function.  This BLOCK actually represents the outermost binding contour
19435      for the function, i.e. the contour in which the function's formal
19436      parameters and labels get declared. Curiously, it appears that the front
19437      end doesn't actually put the PARM_DECL nodes for the current function onto
19438      the BLOCK_VARS list for this outer scope, but are strung off of the
19439      DECL_ARGUMENTS list for the function instead.
19440
19441      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19442      the LABEL_DECL nodes for the function however, and we output DWARF info
19443      for those in decls_for_scope.  Just within the `outer_scope' there will be
19444      a BLOCK node representing the function's outermost pair of curly braces,
19445      and any blocks used for the base and member initializers of a C++
19446      constructor function.  */
19447   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19448     {
19449       int call_site_note_count = 0;
19450       int tail_call_site_note_count = 0;
19451
19452       /* Emit a DW_TAG_variable DIE for a named return value.  */
19453       if (DECL_NAME (DECL_RESULT (decl)))
19454         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19455
19456       current_function_has_inlines = 0;
19457       decls_for_scope (outer_scope, subr_die, 0);
19458
19459       if (call_arg_locations && !dwarf_strict)
19460         {
19461           struct call_arg_loc_node *ca_loc;
19462           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19463             {
19464               dw_die_ref die = NULL;
19465               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19466               rtx arg, next_arg;
19467
19468               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19469                    arg; arg = next_arg)
19470                 {
19471                   dw_loc_descr_ref reg, val;
19472                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19473                   dw_die_ref cdie;
19474
19475                   next_arg = XEXP (arg, 1);
19476                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19477                       && next_arg
19478                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19479                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19480                       && REGNO (XEXP (XEXP (arg, 0), 0))
19481                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19482                     next_arg = XEXP (next_arg, 1);
19483                   if (mode == VOIDmode)
19484                     {
19485                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19486                       if (mode == VOIDmode)
19487                         mode = GET_MODE (XEXP (arg, 0));
19488                     }
19489                   if (GET_MODE_CLASS (mode) != MODE_INT
19490                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19491                     continue;
19492                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19493                     {
19494                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19495                       tloc = XEXP (XEXP (arg, 0), 1);
19496                       continue;
19497                     }
19498                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19499                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19500                     {
19501                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19502                       tlocc = XEXP (XEXP (arg, 0), 1);
19503                       continue;
19504                     }
19505                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19506                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19507                                               VAR_INIT_STATUS_INITIALIZED);
19508                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19509                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19510                                                           0), 0), mode,
19511                                               VAR_INIT_STATUS_INITIALIZED);
19512                   else
19513                     continue;
19514                   if (reg == NULL)
19515                     continue;
19516                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19517                                             VAR_INIT_STATUS_INITIALIZED);
19518                   if (val == NULL)
19519                     continue;
19520                   if (die == NULL)
19521                     die = gen_call_site_die (decl, subr_die, ca_loc);
19522                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19523                                   NULL_TREE);           
19524                   add_AT_loc (cdie, DW_AT_location, reg);
19525                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19526                   if (next_arg != XEXP (arg, 1))
19527                     {
19528                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19529                                                             0), 1), VOIDmode,
19530                                                 VAR_INIT_STATUS_INITIALIZED);
19531                       if (val != NULL)
19532                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19533                     }
19534                 }
19535               if (die == NULL
19536                   && (ca_loc->symbol_ref || tloc))
19537                 die = gen_call_site_die (decl, subr_die, ca_loc);
19538               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19539                 {
19540                   dw_loc_descr_ref tval = NULL;
19541
19542                   if (tloc != NULL_RTX)
19543                     tval = mem_loc_descriptor (tloc, VOIDmode,
19544                                                VAR_INIT_STATUS_INITIALIZED);
19545                   if (tval)
19546                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19547                   else if (tlocc != NULL_RTX)
19548                     {
19549                       tval = mem_loc_descriptor (tlocc, VOIDmode,
19550                                                  VAR_INIT_STATUS_INITIALIZED);
19551                       if (tval)
19552                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19553                                     tval);
19554                     }
19555                 }
19556               if (die != NULL)
19557                 {
19558                   call_site_note_count++;
19559                   if (ca_loc->tail_call_p)
19560                     tail_call_site_note_count++;
19561                 }
19562             }
19563         }
19564       call_arg_locations = NULL;
19565       call_arg_loc_last = NULL;
19566       if (tail_call_site_count >= 0
19567           && tail_call_site_count == tail_call_site_note_count
19568           && !dwarf_strict)
19569         {
19570           if (call_site_count >= 0
19571               && call_site_count == call_site_note_count)
19572             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19573           else
19574             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19575         }
19576       call_site_count = -1;
19577       tail_call_site_count = -1;
19578     }
19579   /* Add the calling convention attribute if requested.  */
19580   add_calling_convention_attribute (subr_die, decl);
19581
19582 }
19583
19584 /* Returns a hash value for X (which really is a die_struct).  */
19585
19586 static hashval_t
19587 common_block_die_table_hash (const void *x)
19588 {
19589   const_dw_die_ref d = (const_dw_die_ref) x;
19590   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19591 }
19592
19593 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19594    as decl_id and die_parent of die_struct Y.  */
19595
19596 static int
19597 common_block_die_table_eq (const void *x, const void *y)
19598 {
19599   const_dw_die_ref d = (const_dw_die_ref) x;
19600   const_dw_die_ref e = (const_dw_die_ref) y;
19601   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19602 }
19603
19604 /* Generate a DIE to represent a declared data object.
19605    Either DECL or ORIGIN must be non-null.  */
19606
19607 static void
19608 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19609 {
19610   HOST_WIDE_INT off;
19611   tree com_decl;
19612   tree decl_or_origin = decl ? decl : origin;
19613   tree ultimate_origin;
19614   dw_die_ref var_die;
19615   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19616   dw_die_ref origin_die;
19617   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19618                       || class_or_namespace_scope_p (context_die));
19619   bool specialization_p = false;
19620
19621   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19622   if (decl || ultimate_origin)
19623     origin = ultimate_origin;
19624   com_decl = fortran_common (decl_or_origin, &off);
19625
19626   /* Symbol in common gets emitted as a child of the common block, in the form
19627      of a data member.  */
19628   if (com_decl)
19629     {
19630       dw_die_ref com_die;
19631       dw_loc_list_ref loc;
19632       die_node com_die_arg;
19633
19634       var_die = lookup_decl_die (decl_or_origin);
19635       if (var_die)
19636         {
19637           if (get_AT (var_die, DW_AT_location) == NULL)
19638             {
19639               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19640               if (loc)
19641                 {
19642                   if (off)
19643                     {
19644                       /* Optimize the common case.  */
19645                       if (single_element_loc_list_p (loc)
19646                           && loc->expr->dw_loc_opc == DW_OP_addr
19647                           && loc->expr->dw_loc_next == NULL
19648                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19649                              == SYMBOL_REF)
19650                         loc->expr->dw_loc_oprnd1.v.val_addr
19651                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19652                         else
19653                           loc_list_plus_const (loc, off);
19654                     }
19655                   add_AT_location_description (var_die, DW_AT_location, loc);
19656                   remove_AT (var_die, DW_AT_declaration);
19657                 }
19658             }
19659           return;
19660         }
19661
19662       if (common_block_die_table == NULL)
19663         common_block_die_table
19664           = htab_create_ggc (10, common_block_die_table_hash,
19665                              common_block_die_table_eq, NULL);
19666
19667       com_die_arg.decl_id = DECL_UID (com_decl);
19668       com_die_arg.die_parent = context_die;
19669       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19670       loc = loc_list_from_tree (com_decl, 2);
19671       if (com_die == NULL)
19672         {
19673           const char *cnam
19674             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19675           void **slot;
19676
19677           com_die = new_die (DW_TAG_common_block, context_die, decl);
19678           add_name_and_src_coords_attributes (com_die, com_decl);
19679           if (loc)
19680             {
19681               add_AT_location_description (com_die, DW_AT_location, loc);
19682               /* Avoid sharing the same loc descriptor between
19683                  DW_TAG_common_block and DW_TAG_variable.  */
19684               loc = loc_list_from_tree (com_decl, 2);
19685             }
19686           else if (DECL_EXTERNAL (decl))
19687             add_AT_flag (com_die, DW_AT_declaration, 1);
19688           add_pubname_string (cnam, com_die); /* ??? needed? */
19689           com_die->decl_id = DECL_UID (com_decl);
19690           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19691           *slot = (void *) com_die;
19692         }
19693       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19694         {
19695           add_AT_location_description (com_die, DW_AT_location, loc);
19696           loc = loc_list_from_tree (com_decl, 2);
19697           remove_AT (com_die, DW_AT_declaration);
19698         }
19699       var_die = new_die (DW_TAG_variable, com_die, decl);
19700       add_name_and_src_coords_attributes (var_die, decl);
19701       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19702                           TREE_THIS_VOLATILE (decl), context_die);
19703       add_AT_flag (var_die, DW_AT_external, 1);
19704       if (loc)
19705         {
19706           if (off)
19707             {
19708               /* Optimize the common case.  */
19709               if (single_element_loc_list_p (loc)
19710                   && loc->expr->dw_loc_opc == DW_OP_addr
19711                   && loc->expr->dw_loc_next == NULL
19712                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19713                 loc->expr->dw_loc_oprnd1.v.val_addr
19714                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19715               else
19716                 loc_list_plus_const (loc, off);
19717             }
19718           add_AT_location_description (var_die, DW_AT_location, loc);
19719         }
19720       else if (DECL_EXTERNAL (decl))
19721         add_AT_flag (var_die, DW_AT_declaration, 1);
19722       equate_decl_number_to_die (decl, var_die);
19723       return;
19724     }
19725
19726   /* If the compiler emitted a definition for the DECL declaration
19727      and if we already emitted a DIE for it, don't emit a second
19728      DIE for it again. Allow re-declarations of DECLs that are
19729      inside functions, though.  */
19730   if (old_die && declaration && !local_scope_p (context_die))
19731     return;
19732
19733   /* For static data members, the declaration in the class is supposed
19734      to have DW_TAG_member tag; the specification should still be
19735      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19736   if (declaration && class_scope_p (context_die))
19737     var_die = new_die (DW_TAG_member, context_die, decl);
19738   else
19739     var_die = new_die (DW_TAG_variable, context_die, decl);
19740
19741   origin_die = NULL;
19742   if (origin != NULL)
19743     origin_die = add_abstract_origin_attribute (var_die, origin);
19744
19745   /* Loop unrolling can create multiple blocks that refer to the same
19746      static variable, so we must test for the DW_AT_declaration flag.
19747
19748      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19749      copy decls and set the DECL_ABSTRACT flag on them instead of
19750      sharing them.
19751
19752      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19753
19754      ??? The declare_in_namespace support causes us to get two DIEs for one
19755      variable, both of which are declarations.  We want to avoid considering
19756      one to be a specification, so we must test that this DIE is not a
19757      declaration.  */
19758   else if (old_die && TREE_STATIC (decl) && ! declaration
19759            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19760     {
19761       /* This is a definition of a C++ class level static.  */
19762       add_AT_specification (var_die, old_die);
19763       specialization_p = true;
19764       if (DECL_NAME (decl))
19765         {
19766           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19767           struct dwarf_file_data * file_index = lookup_filename (s.file);
19768
19769           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19770             add_AT_file (var_die, DW_AT_decl_file, file_index);
19771
19772           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19773             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19774
19775           if (old_die->die_tag == DW_TAG_member)
19776             add_linkage_name (var_die, decl);
19777         }
19778     }
19779   else
19780     add_name_and_src_coords_attributes (var_die, decl);
19781
19782   if ((origin == NULL && !specialization_p)
19783       || (origin != NULL
19784           && !DECL_ABSTRACT (decl_or_origin)
19785           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19786                                        decl_function_context
19787                                                         (decl_or_origin))))
19788     {
19789       tree type = TREE_TYPE (decl_or_origin);
19790
19791       if (decl_by_reference_p (decl_or_origin))
19792         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19793       else
19794         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19795                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19796     }
19797
19798   if (origin == NULL && !specialization_p)
19799     {
19800       if (TREE_PUBLIC (decl))
19801         add_AT_flag (var_die, DW_AT_external, 1);
19802
19803       if (DECL_ARTIFICIAL (decl))
19804         add_AT_flag (var_die, DW_AT_artificial, 1);
19805
19806       add_accessibility_attribute (var_die, decl);
19807     }
19808
19809   if (declaration)
19810     add_AT_flag (var_die, DW_AT_declaration, 1);
19811
19812   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19813     equate_decl_number_to_die (decl, var_die);
19814
19815   if (! declaration
19816       && (! DECL_ABSTRACT (decl_or_origin)
19817           /* Local static vars are shared between all clones/inlines,
19818              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19819              already set.  */
19820           || (TREE_CODE (decl_or_origin) == VAR_DECL
19821               && TREE_STATIC (decl_or_origin)
19822               && DECL_RTL_SET_P (decl_or_origin)))
19823       /* When abstract origin already has DW_AT_location attribute, no need
19824          to add it again.  */
19825       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19826     {
19827       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19828           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19829         defer_location (decl_or_origin, var_die);
19830       else
19831         add_location_or_const_value_attribute (var_die,
19832                                                decl_or_origin,
19833                                                DW_AT_location);
19834       add_pubname (decl_or_origin, var_die);
19835     }
19836   else
19837     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19838 }
19839
19840 /* Generate a DIE to represent a named constant.  */
19841
19842 static void
19843 gen_const_die (tree decl, dw_die_ref context_die)
19844 {
19845   dw_die_ref const_die;
19846   tree type = TREE_TYPE (decl);
19847
19848   const_die = new_die (DW_TAG_constant, context_die, decl);
19849   add_name_and_src_coords_attributes (const_die, decl);
19850   add_type_attribute (const_die, type, 1, 0, context_die);
19851   if (TREE_PUBLIC (decl))
19852     add_AT_flag (const_die, DW_AT_external, 1);
19853   if (DECL_ARTIFICIAL (decl))
19854     add_AT_flag (const_die, DW_AT_artificial, 1);
19855   tree_add_const_value_attribute_for_decl (const_die, decl);
19856 }
19857
19858 /* Generate a DIE to represent a label identifier.  */
19859
19860 static void
19861 gen_label_die (tree decl, dw_die_ref context_die)
19862 {
19863   tree origin = decl_ultimate_origin (decl);
19864   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19865   rtx insn;
19866   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19867
19868   if (origin != NULL)
19869     add_abstract_origin_attribute (lbl_die, origin);
19870   else
19871     add_name_and_src_coords_attributes (lbl_die, decl);
19872
19873   if (DECL_ABSTRACT (decl))
19874     equate_decl_number_to_die (decl, lbl_die);
19875   else
19876     {
19877       insn = DECL_RTL_IF_SET (decl);
19878
19879       /* Deleted labels are programmer specified labels which have been
19880          eliminated because of various optimizations.  We still emit them
19881          here so that it is possible to put breakpoints on them.  */
19882       if (insn
19883           && (LABEL_P (insn)
19884               || ((NOTE_P (insn)
19885                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19886         {
19887           /* When optimization is enabled (via -O) some parts of the compiler
19888              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19889              represent source-level labels which were explicitly declared by
19890              the user.  This really shouldn't be happening though, so catch
19891              it if it ever does happen.  */
19892           gcc_assert (!INSN_DELETED_P (insn));
19893
19894           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19895           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19896         }
19897     }
19898 }
19899
19900 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19901    attributes to the DIE for a block STMT, to describe where the inlined
19902    function was called from.  This is similar to add_src_coords_attributes.  */
19903
19904 static inline void
19905 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19906 {
19907   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19908
19909   if (dwarf_version >= 3 || !dwarf_strict)
19910     {
19911       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19912       add_AT_unsigned (die, DW_AT_call_line, s.line);
19913     }
19914 }
19915
19916
19917 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19918    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19919
19920 static inline void
19921 add_high_low_attributes (tree stmt, dw_die_ref die)
19922 {
19923   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19924
19925   if (BLOCK_FRAGMENT_CHAIN (stmt)
19926       && (dwarf_version >= 3 || !dwarf_strict))
19927     {
19928       tree chain;
19929
19930       if (inlined_function_outer_scope_p (stmt))
19931         {
19932           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19933                                        BLOCK_NUMBER (stmt));
19934           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19935         }
19936
19937       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19938
19939       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19940       do
19941         {
19942           add_ranges (chain);
19943           chain = BLOCK_FRAGMENT_CHAIN (chain);
19944         }
19945       while (chain);
19946       add_ranges (NULL);
19947     }
19948   else
19949     {
19950       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19951                                    BLOCK_NUMBER (stmt));
19952       add_AT_lbl_id (die, DW_AT_low_pc, label);
19953       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19954                                    BLOCK_NUMBER (stmt));
19955       add_AT_lbl_id (die, DW_AT_high_pc, label);
19956     }
19957 }
19958
19959 /* Generate a DIE for a lexical block.  */
19960
19961 static void
19962 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19963 {
19964   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19965
19966   if (call_arg_locations)
19967     {
19968       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19969         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19970                                BLOCK_NUMBER (stmt) + 1);
19971       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
19972     }
19973
19974   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19975     add_high_low_attributes (stmt, stmt_die);
19976
19977   decls_for_scope (stmt, stmt_die, depth);
19978 }
19979
19980 /* Generate a DIE for an inlined subprogram.  */
19981
19982 static void
19983 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19984 {
19985   tree decl;
19986
19987   /* The instance of function that is effectively being inlined shall not
19988      be abstract.  */
19989   gcc_assert (! BLOCK_ABSTRACT (stmt));
19990
19991   decl = block_ultimate_origin (stmt);
19992
19993   /* Emit info for the abstract instance first, if we haven't yet.  We
19994      must emit this even if the block is abstract, otherwise when we
19995      emit the block below (or elsewhere), we may end up trying to emit
19996      a die whose origin die hasn't been emitted, and crashing.  */
19997   dwarf2out_abstract_function (decl);
19998
19999   if (! BLOCK_ABSTRACT (stmt))
20000     {
20001       dw_die_ref subr_die
20002         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20003
20004       if (call_arg_locations)
20005         {
20006           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20007             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20008                                    BLOCK_NUMBER (stmt) + 1);
20009           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20010         }
20011       add_abstract_origin_attribute (subr_die, decl);
20012       if (TREE_ASM_WRITTEN (stmt))
20013         add_high_low_attributes (stmt, subr_die);
20014       add_call_src_coords_attributes (stmt, subr_die);
20015
20016       decls_for_scope (stmt, subr_die, depth);
20017       current_function_has_inlines = 1;
20018     }
20019 }
20020
20021 /* Generate a DIE for a field in a record, or structure.  */
20022
20023 static void
20024 gen_field_die (tree decl, dw_die_ref context_die)
20025 {
20026   dw_die_ref decl_die;
20027
20028   if (TREE_TYPE (decl) == error_mark_node)
20029     return;
20030
20031   decl_die = new_die (DW_TAG_member, context_die, decl);
20032   add_name_and_src_coords_attributes (decl_die, decl);
20033   add_type_attribute (decl_die, member_declared_type (decl),
20034                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20035                       context_die);
20036
20037   if (DECL_BIT_FIELD_TYPE (decl))
20038     {
20039       add_byte_size_attribute (decl_die, decl);
20040       add_bit_size_attribute (decl_die, decl);
20041       add_bit_offset_attribute (decl_die, decl);
20042     }
20043
20044   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20045     add_data_member_location_attribute (decl_die, decl);
20046
20047   if (DECL_ARTIFICIAL (decl))
20048     add_AT_flag (decl_die, DW_AT_artificial, 1);
20049
20050   add_accessibility_attribute (decl_die, decl);
20051
20052   /* Equate decl number to die, so that we can look up this decl later on.  */
20053   equate_decl_number_to_die (decl, decl_die);
20054 }
20055
20056 #if 0
20057 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20058    Use modified_type_die instead.
20059    We keep this code here just in case these types of DIEs may be needed to
20060    represent certain things in other languages (e.g. Pascal) someday.  */
20061
20062 static void
20063 gen_pointer_type_die (tree type, dw_die_ref context_die)
20064 {
20065   dw_die_ref ptr_die
20066     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20067
20068   equate_type_number_to_die (type, ptr_die);
20069   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20070   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20071 }
20072
20073 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20074    Use modified_type_die instead.
20075    We keep this code here just in case these types of DIEs may be needed to
20076    represent certain things in other languages (e.g. Pascal) someday.  */
20077
20078 static void
20079 gen_reference_type_die (tree type, dw_die_ref context_die)
20080 {
20081   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20082
20083   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20084     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20085   else
20086     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20087
20088   equate_type_number_to_die (type, ref_die);
20089   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20090   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20091 }
20092 #endif
20093
20094 /* Generate a DIE for a pointer to a member type.  */
20095
20096 static void
20097 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20098 {
20099   dw_die_ref ptr_die
20100     = new_die (DW_TAG_ptr_to_member_type,
20101                scope_die_for (type, context_die), type);
20102
20103   equate_type_number_to_die (type, ptr_die);
20104   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20105                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20106   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20107 }
20108
20109 /* Generate the DIE for the compilation unit.  */
20110
20111 static dw_die_ref
20112 gen_compile_unit_die (const char *filename)
20113 {
20114   dw_die_ref die;
20115   char producer[250];
20116   const char *language_string = lang_hooks.name;
20117   int language;
20118
20119   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20120
20121   if (filename)
20122     {
20123       add_name_attribute (die, filename);
20124       /* Don't add cwd for <built-in>.  */
20125       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20126         add_comp_dir_attribute (die);
20127     }
20128
20129   sprintf (producer, "%s %s", language_string, version_string);
20130
20131 #ifdef MIPS_DEBUGGING_INFO
20132   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20133      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20134      not appear in the producer string, the debugger reaches the conclusion
20135      that the object file is stripped and has no debugging information.
20136      To get the MIPS/SGI debugger to believe that there is debugging
20137      information in the object file, we add a -g to the producer string.  */
20138   if (debug_info_level > DINFO_LEVEL_TERSE)
20139     strcat (producer, " -g");
20140 #endif
20141
20142   add_AT_string (die, DW_AT_producer, producer);
20143
20144   /* If our producer is LTO try to figure out a common language to use
20145      from the global list of translation units.  */
20146   if (strcmp (language_string, "GNU GIMPLE") == 0)
20147     {
20148       unsigned i;
20149       tree t;
20150       const char *common_lang = NULL;
20151
20152       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20153         {
20154           if (!TRANSLATION_UNIT_LANGUAGE (t))
20155             continue;
20156           if (!common_lang)
20157             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20158           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20159             ;
20160           else if (strncmp (common_lang, "GNU C", 5) == 0
20161                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20162             /* Mixing C and C++ is ok, use C++ in that case.  */
20163             common_lang = "GNU C++";
20164           else
20165             {
20166               /* Fall back to C.  */
20167               common_lang = NULL;
20168               break;
20169             }
20170         }
20171
20172       if (common_lang)
20173         language_string = common_lang;
20174     }
20175
20176   language = DW_LANG_C89;
20177   if (strcmp (language_string, "GNU C++") == 0)
20178     language = DW_LANG_C_plus_plus;
20179   else if (strcmp (language_string, "GNU F77") == 0)
20180     language = DW_LANG_Fortran77;
20181   else if (strcmp (language_string, "GNU Pascal") == 0)
20182     language = DW_LANG_Pascal83;
20183   else if (dwarf_version >= 3 || !dwarf_strict)
20184     {
20185       if (strcmp (language_string, "GNU Ada") == 0)
20186         language = DW_LANG_Ada95;
20187       else if (strcmp (language_string, "GNU Fortran") == 0)
20188         language = DW_LANG_Fortran95;
20189       else if (strcmp (language_string, "GNU Java") == 0)
20190         language = DW_LANG_Java;
20191       else if (strcmp (language_string, "GNU Objective-C") == 0)
20192         language = DW_LANG_ObjC;
20193       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20194         language = DW_LANG_ObjC_plus_plus;
20195     }
20196
20197   add_AT_unsigned (die, DW_AT_language, language);
20198
20199   switch (language)
20200     {
20201     case DW_LANG_Fortran77:
20202     case DW_LANG_Fortran90:
20203     case DW_LANG_Fortran95:
20204       /* Fortran has case insensitive identifiers and the front-end
20205          lowercases everything.  */
20206       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20207       break;
20208     default:
20209       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20210       break;
20211     }
20212   return die;
20213 }
20214
20215 /* Generate the DIE for a base class.  */
20216
20217 static void
20218 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20219 {
20220   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20221
20222   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20223   add_data_member_location_attribute (die, binfo);
20224
20225   if (BINFO_VIRTUAL_P (binfo))
20226     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20227
20228   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20229      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20230      the default has always been DW_ACCESS_private.  */
20231   if (access == access_public_node)
20232     {
20233       if (dwarf_version == 2
20234           || context_die->die_tag == DW_TAG_class_type)
20235       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20236     }
20237   else if (access == access_protected_node)
20238     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20239   else if (dwarf_version > 2
20240            && context_die->die_tag != DW_TAG_class_type)
20241     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20242 }
20243
20244 /* Generate a DIE for a class member.  */
20245
20246 static void
20247 gen_member_die (tree type, dw_die_ref context_die)
20248 {
20249   tree member;
20250   tree binfo = TYPE_BINFO (type);
20251   dw_die_ref child;
20252
20253   /* If this is not an incomplete type, output descriptions of each of its
20254      members. Note that as we output the DIEs necessary to represent the
20255      members of this record or union type, we will also be trying to output
20256      DIEs to represent the *types* of those members. However the `type'
20257      function (above) will specifically avoid generating type DIEs for member
20258      types *within* the list of member DIEs for this (containing) type except
20259      for those types (of members) which are explicitly marked as also being
20260      members of this (containing) type themselves.  The g++ front- end can
20261      force any given type to be treated as a member of some other (containing)
20262      type by setting the TYPE_CONTEXT of the given (member) type to point to
20263      the TREE node representing the appropriate (containing) type.  */
20264
20265   /* First output info about the base classes.  */
20266   if (binfo)
20267     {
20268       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20269       int i;
20270       tree base;
20271
20272       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20273         gen_inheritance_die (base,
20274                              (accesses ? VEC_index (tree, accesses, i)
20275                               : access_public_node), context_die);
20276     }
20277
20278   /* Now output info about the data members and type members.  */
20279   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20280     {
20281       /* If we thought we were generating minimal debug info for TYPE
20282          and then changed our minds, some of the member declarations
20283          may have already been defined.  Don't define them again, but
20284          do put them in the right order.  */
20285
20286       child = lookup_decl_die (member);
20287       if (child)
20288         splice_child_die (context_die, child);
20289       else
20290         gen_decl_die (member, NULL, context_die);
20291     }
20292
20293   /* Now output info about the function members (if any).  */
20294   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20295     {
20296       /* Don't include clones in the member list.  */
20297       if (DECL_ABSTRACT_ORIGIN (member))
20298         continue;
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
20308 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20309    is set, we pretend that the type was never defined, so we only get the
20310    member DIEs needed by later specification DIEs.  */
20311
20312 static void
20313 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20314                                 enum debug_info_usage usage)
20315 {
20316   dw_die_ref type_die = lookup_type_die (type);
20317   dw_die_ref scope_die = 0;
20318   int nested = 0;
20319   int complete = (TYPE_SIZE (type)
20320                   && (! TYPE_STUB_DECL (type)
20321                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20322   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20323   complete = complete && should_emit_struct_debug (type, usage);
20324
20325   if (type_die && ! complete)
20326     return;
20327
20328   if (TYPE_CONTEXT (type) != NULL_TREE
20329       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20330           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20331     nested = 1;
20332
20333   scope_die = scope_die_for (type, context_die);
20334
20335   if (! type_die || (nested && is_cu_die (scope_die)))
20336     /* First occurrence of type or toplevel definition of nested class.  */
20337     {
20338       dw_die_ref old_die = type_die;
20339
20340       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20341                           ? record_type_tag (type) : DW_TAG_union_type,
20342                           scope_die, type);
20343       equate_type_number_to_die (type, type_die);
20344       if (old_die)
20345         add_AT_specification (type_die, old_die);
20346       else
20347         add_name_attribute (type_die, type_tag (type));
20348     }
20349   else
20350     remove_AT (type_die, DW_AT_declaration);
20351
20352   /* Generate child dies for template paramaters.  */
20353   if (debug_info_level > DINFO_LEVEL_TERSE
20354       && COMPLETE_TYPE_P (type))
20355     schedule_generic_params_dies_gen (type);
20356
20357   /* If this type has been completed, then give it a byte_size attribute and
20358      then give a list of members.  */
20359   if (complete && !ns_decl)
20360     {
20361       /* Prevent infinite recursion in cases where the type of some member of
20362          this type is expressed in terms of this type itself.  */
20363       TREE_ASM_WRITTEN (type) = 1;
20364       add_byte_size_attribute (type_die, type);
20365       if (TYPE_STUB_DECL (type) != NULL_TREE)
20366         {
20367           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20368           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20369         }
20370
20371       /* If the first reference to this type was as the return type of an
20372          inline function, then it may not have a parent.  Fix this now.  */
20373       if (type_die->die_parent == NULL)
20374         add_child_die (scope_die, type_die);
20375
20376       push_decl_scope (type);
20377       gen_member_die (type, type_die);
20378       pop_decl_scope ();
20379
20380       /* GNU extension: Record what type our vtable lives in.  */
20381       if (TYPE_VFIELD (type))
20382         {
20383           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20384
20385           gen_type_die (vtype, context_die);
20386           add_AT_die_ref (type_die, DW_AT_containing_type,
20387                           lookup_type_die (vtype));
20388         }
20389     }
20390   else
20391     {
20392       add_AT_flag (type_die, DW_AT_declaration, 1);
20393
20394       /* We don't need to do this for function-local types.  */
20395       if (TYPE_STUB_DECL (type)
20396           && ! decl_function_context (TYPE_STUB_DECL (type)))
20397         VEC_safe_push (tree, gc, incomplete_types, type);
20398     }
20399
20400   if (get_AT (type_die, DW_AT_name))
20401     add_pubtype (type, type_die);
20402 }
20403
20404 /* Generate a DIE for a subroutine _type_.  */
20405
20406 static void
20407 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20408 {
20409   tree return_type = TREE_TYPE (type);
20410   dw_die_ref subr_die
20411     = new_die (DW_TAG_subroutine_type,
20412                scope_die_for (type, context_die), type);
20413
20414   equate_type_number_to_die (type, subr_die);
20415   add_prototyped_attribute (subr_die, type);
20416   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20417   gen_formal_types_die (type, subr_die);
20418
20419   if (get_AT (subr_die, DW_AT_name))
20420     add_pubtype (type, subr_die);
20421 }
20422
20423 /* Generate a DIE for a type definition.  */
20424
20425 static void
20426 gen_typedef_die (tree decl, dw_die_ref context_die)
20427 {
20428   dw_die_ref type_die;
20429   tree origin;
20430
20431   if (TREE_ASM_WRITTEN (decl))
20432     return;
20433
20434   TREE_ASM_WRITTEN (decl) = 1;
20435   type_die = new_die (DW_TAG_typedef, context_die, decl);
20436   origin = decl_ultimate_origin (decl);
20437   if (origin != NULL)
20438     add_abstract_origin_attribute (type_die, origin);
20439   else
20440     {
20441       tree type;
20442
20443       add_name_and_src_coords_attributes (type_die, decl);
20444       if (DECL_ORIGINAL_TYPE (decl))
20445         {
20446           type = DECL_ORIGINAL_TYPE (decl);
20447
20448           gcc_assert (type != TREE_TYPE (decl));
20449           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20450         }
20451       else
20452         {
20453           type = TREE_TYPE (decl);
20454
20455           if (is_naming_typedef_decl (TYPE_NAME (type)))
20456             {
20457               /* Here, we are in the case of decl being a typedef naming
20458                  an anonymous type, e.g:
20459                      typedef struct {...} foo;
20460                  In that case TREE_TYPE (decl) is not a typedef variant
20461                  type and TYPE_NAME of the anonymous type is set to the
20462                  TYPE_DECL of the typedef. This construct is emitted by
20463                  the C++ FE.
20464
20465                  TYPE is the anonymous struct named by the typedef
20466                  DECL. As we need the DW_AT_type attribute of the
20467                  DW_TAG_typedef to point to the DIE of TYPE, let's
20468                  generate that DIE right away. add_type_attribute
20469                  called below will then pick (via lookup_type_die) that
20470                  anonymous struct DIE.  */
20471               if (!TREE_ASM_WRITTEN (type))
20472                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20473
20474               /* This is a GNU Extension.  We are adding a
20475                  DW_AT_linkage_name attribute to the DIE of the
20476                  anonymous struct TYPE.  The value of that attribute
20477                  is the name of the typedef decl naming the anonymous
20478                  struct.  This greatly eases the work of consumers of
20479                  this debug info.  */
20480               add_linkage_attr (lookup_type_die (type), decl);
20481             }
20482         }
20483
20484       add_type_attribute (type_die, type, TREE_READONLY (decl),
20485                           TREE_THIS_VOLATILE (decl), context_die);
20486
20487       if (is_naming_typedef_decl (decl))
20488         /* We want that all subsequent calls to lookup_type_die with
20489            TYPE in argument yield the DW_TAG_typedef we have just
20490            created.  */
20491         equate_type_number_to_die (type, type_die);
20492
20493       add_accessibility_attribute (type_die, decl);
20494     }
20495
20496   if (DECL_ABSTRACT (decl))
20497     equate_decl_number_to_die (decl, type_die);
20498
20499   if (get_AT (type_die, DW_AT_name))
20500     add_pubtype (decl, type_die);
20501 }
20502
20503 /* Generate a DIE for a struct, class, enum or union type.  */
20504
20505 static void
20506 gen_tagged_type_die (tree type,
20507                      dw_die_ref context_die,
20508                      enum debug_info_usage usage)
20509 {
20510   int need_pop;
20511
20512   if (type == NULL_TREE
20513       || !is_tagged_type (type))
20514     return;
20515
20516   /* If this is a nested type whose containing class hasn't been written
20517      out yet, writing it out will cover this one, too.  This does not apply
20518      to instantiations of member class templates; they need to be added to
20519      the containing class as they are generated.  FIXME: This hurts the
20520      idea of combining type decls from multiple TUs, since we can't predict
20521      what set of template instantiations we'll get.  */
20522   if (TYPE_CONTEXT (type)
20523       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20524       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20525     {
20526       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20527
20528       if (TREE_ASM_WRITTEN (type))
20529         return;
20530
20531       /* If that failed, attach ourselves to the stub.  */
20532       push_decl_scope (TYPE_CONTEXT (type));
20533       context_die = lookup_type_die (TYPE_CONTEXT (type));
20534       need_pop = 1;
20535     }
20536   else if (TYPE_CONTEXT (type) != NULL_TREE
20537            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20538     {
20539       /* If this type is local to a function that hasn't been written
20540          out yet, use a NULL context for now; it will be fixed up in
20541          decls_for_scope.  */
20542       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20543       /* A declaration DIE doesn't count; nested types need to go in the
20544          specification.  */
20545       if (context_die && is_declaration_die (context_die))
20546         context_die = NULL;
20547       need_pop = 0;
20548     }
20549   else
20550     {
20551       context_die = declare_in_namespace (type, context_die);
20552       need_pop = 0;
20553     }
20554
20555   if (TREE_CODE (type) == ENUMERAL_TYPE)
20556     {
20557       /* This might have been written out by the call to
20558          declare_in_namespace.  */
20559       if (!TREE_ASM_WRITTEN (type))
20560         gen_enumeration_type_die (type, context_die);
20561     }
20562   else
20563     gen_struct_or_union_type_die (type, context_die, usage);
20564
20565   if (need_pop)
20566     pop_decl_scope ();
20567
20568   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20569      it up if it is ever completed.  gen_*_type_die will set it for us
20570      when appropriate.  */
20571 }
20572
20573 /* Generate a type description DIE.  */
20574
20575 static void
20576 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20577                          enum debug_info_usage usage)
20578 {
20579   struct array_descr_info info;
20580
20581   if (type == NULL_TREE || type == error_mark_node)
20582     return;
20583
20584   if (TYPE_NAME (type) != NULL_TREE
20585       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20586       && is_redundant_typedef (TYPE_NAME (type))
20587       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20588     /* The DECL of this type is a typedef we don't want to emit debug
20589        info for but we want debug info for its underlying typedef.
20590        This can happen for e.g, the injected-class-name of a C++
20591        type.  */
20592     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20593
20594   /* If TYPE is a typedef type variant, let's generate debug info
20595      for the parent typedef which TYPE is a type of.  */
20596   if (typedef_variant_p (type))
20597     {
20598       if (TREE_ASM_WRITTEN (type))
20599         return;
20600
20601       /* Prevent broken recursion; we can't hand off to the same type.  */
20602       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20603
20604       /* Use the DIE of the containing namespace as the parent DIE of
20605          the type description DIE we want to generate.  */
20606       if (DECL_CONTEXT (TYPE_NAME (type))
20607           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20608         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20609
20610       TREE_ASM_WRITTEN (type) = 1;
20611
20612       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20613       return;
20614     }
20615
20616   /* If type is an anonymous tagged type named by a typedef, let's
20617      generate debug info for the typedef.  */
20618   if (is_naming_typedef_decl (TYPE_NAME (type)))
20619     {
20620       /* Use the DIE of the containing namespace as the parent DIE of
20621          the type description DIE we want to generate.  */
20622       if (DECL_CONTEXT (TYPE_NAME (type))
20623           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20624         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20625       
20626       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20627       return;
20628     }
20629
20630   /* If this is an array type with hidden descriptor, handle it first.  */
20631   if (!TREE_ASM_WRITTEN (type)
20632       && lang_hooks.types.get_array_descr_info
20633       && lang_hooks.types.get_array_descr_info (type, &info)
20634       && (dwarf_version >= 3 || !dwarf_strict))
20635     {
20636       gen_descr_array_type_die (type, &info, context_die);
20637       TREE_ASM_WRITTEN (type) = 1;
20638       return;
20639     }
20640
20641   /* We are going to output a DIE to represent the unqualified version
20642      of this type (i.e. without any const or volatile qualifiers) so
20643      get the main variant (i.e. the unqualified version) of this type
20644      now.  (Vectors are special because the debugging info is in the
20645      cloned type itself).  */
20646   if (TREE_CODE (type) != VECTOR_TYPE)
20647     type = type_main_variant (type);
20648
20649   if (TREE_ASM_WRITTEN (type))
20650     return;
20651
20652   switch (TREE_CODE (type))
20653     {
20654     case ERROR_MARK:
20655       break;
20656
20657     case POINTER_TYPE:
20658     case REFERENCE_TYPE:
20659       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20660          ensures that the gen_type_die recursion will terminate even if the
20661          type is recursive.  Recursive types are possible in Ada.  */
20662       /* ??? We could perhaps do this for all types before the switch
20663          statement.  */
20664       TREE_ASM_WRITTEN (type) = 1;
20665
20666       /* For these types, all that is required is that we output a DIE (or a
20667          set of DIEs) to represent the "basis" type.  */
20668       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20669                                 DINFO_USAGE_IND_USE);
20670       break;
20671
20672     case OFFSET_TYPE:
20673       /* This code is used for C++ pointer-to-data-member types.
20674          Output a description of the relevant class type.  */
20675       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20676                                         DINFO_USAGE_IND_USE);
20677
20678       /* Output a description of the type of the object pointed to.  */
20679       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20680                                         DINFO_USAGE_IND_USE);
20681
20682       /* Now output a DIE to represent this pointer-to-data-member type
20683          itself.  */
20684       gen_ptr_to_mbr_type_die (type, context_die);
20685       break;
20686
20687     case FUNCTION_TYPE:
20688       /* Force out return type (in case it wasn't forced out already).  */
20689       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20690                                         DINFO_USAGE_DIR_USE);
20691       gen_subroutine_type_die (type, context_die);
20692       break;
20693
20694     case METHOD_TYPE:
20695       /* Force out return type (in case it wasn't forced out already).  */
20696       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20697                                         DINFO_USAGE_DIR_USE);
20698       gen_subroutine_type_die (type, context_die);
20699       break;
20700
20701     case ARRAY_TYPE:
20702       gen_array_type_die (type, context_die);
20703       break;
20704
20705     case VECTOR_TYPE:
20706       gen_array_type_die (type, context_die);
20707       break;
20708
20709     case ENUMERAL_TYPE:
20710     case RECORD_TYPE:
20711     case UNION_TYPE:
20712     case QUAL_UNION_TYPE:
20713       gen_tagged_type_die (type, context_die, usage);
20714       return;
20715
20716     case VOID_TYPE:
20717     case INTEGER_TYPE:
20718     case REAL_TYPE:
20719     case FIXED_POINT_TYPE:
20720     case COMPLEX_TYPE:
20721     case BOOLEAN_TYPE:
20722       /* No DIEs needed for fundamental types.  */
20723       break;
20724
20725     case NULLPTR_TYPE:
20726     case LANG_TYPE:
20727       /* Just use DW_TAG_unspecified_type.  */
20728       {
20729         dw_die_ref type_die = lookup_type_die (type);
20730         if (type_die == NULL)
20731           {
20732             tree name = TYPE_NAME (type);
20733             if (TREE_CODE (name) == TYPE_DECL)
20734               name = DECL_NAME (name);
20735             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20736             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20737             equate_type_number_to_die (type, type_die);
20738           }
20739       }
20740       break;
20741
20742     default:
20743       gcc_unreachable ();
20744     }
20745
20746   TREE_ASM_WRITTEN (type) = 1;
20747 }
20748
20749 static void
20750 gen_type_die (tree type, dw_die_ref context_die)
20751 {
20752   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20753 }
20754
20755 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20756    things which are local to the given block.  */
20757
20758 static void
20759 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20760 {
20761   int must_output_die = 0;
20762   bool inlined_func;
20763
20764   /* Ignore blocks that are NULL.  */
20765   if (stmt == NULL_TREE)
20766     return;
20767
20768   inlined_func = inlined_function_outer_scope_p (stmt);
20769
20770   /* If the block is one fragment of a non-contiguous block, do not
20771      process the variables, since they will have been done by the
20772      origin block.  Do process subblocks.  */
20773   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20774     {
20775       tree sub;
20776
20777       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20778         gen_block_die (sub, context_die, depth + 1);
20779
20780       return;
20781     }
20782
20783   /* Determine if we need to output any Dwarf DIEs at all to represent this
20784      block.  */
20785   if (inlined_func)
20786     /* The outer scopes for inlinings *must* always be represented.  We
20787        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20788     must_output_die = 1;
20789   else
20790     {
20791       /* Determine if this block directly contains any "significant"
20792          local declarations which we will need to output DIEs for.  */
20793       if (debug_info_level > DINFO_LEVEL_TERSE)
20794         /* We are not in terse mode so *any* local declaration counts
20795            as being a "significant" one.  */
20796         must_output_die = ((BLOCK_VARS (stmt) != NULL
20797                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20798                            && (TREE_USED (stmt)
20799                                || TREE_ASM_WRITTEN (stmt)
20800                                || BLOCK_ABSTRACT (stmt)));
20801       else if ((TREE_USED (stmt)
20802                 || TREE_ASM_WRITTEN (stmt)
20803                 || BLOCK_ABSTRACT (stmt))
20804                && !dwarf2out_ignore_block (stmt))
20805         must_output_die = 1;
20806     }
20807
20808   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20809      DIE for any block which contains no significant local declarations at
20810      all.  Rather, in such cases we just call `decls_for_scope' so that any
20811      needed Dwarf info for any sub-blocks will get properly generated. Note
20812      that in terse mode, our definition of what constitutes a "significant"
20813      local declaration gets restricted to include only inlined function
20814      instances and local (nested) function definitions.  */
20815   if (must_output_die)
20816     {
20817       if (inlined_func)
20818         {
20819           /* If STMT block is abstract, that means we have been called
20820              indirectly from dwarf2out_abstract_function.
20821              That function rightfully marks the descendent blocks (of
20822              the abstract function it is dealing with) as being abstract,
20823              precisely to prevent us from emitting any
20824              DW_TAG_inlined_subroutine DIE as a descendent
20825              of an abstract function instance. So in that case, we should
20826              not call gen_inlined_subroutine_die.
20827
20828              Later though, when cgraph asks dwarf2out to emit info
20829              for the concrete instance of the function decl into which
20830              the concrete instance of STMT got inlined, the later will lead
20831              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20832           if (! BLOCK_ABSTRACT (stmt))
20833             gen_inlined_subroutine_die (stmt, context_die, depth);
20834         }
20835       else
20836         gen_lexical_block_die (stmt, context_die, depth);
20837     }
20838   else
20839     decls_for_scope (stmt, context_die, depth);
20840 }
20841
20842 /* Process variable DECL (or variable with origin ORIGIN) within
20843    block STMT and add it to CONTEXT_DIE.  */
20844 static void
20845 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20846 {
20847   dw_die_ref die;
20848   tree decl_or_origin = decl ? decl : origin;
20849
20850   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20851     die = lookup_decl_die (decl_or_origin);
20852   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20853            && TYPE_DECL_IS_STUB (decl_or_origin))
20854     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20855   else
20856     die = NULL;
20857
20858   if (die != NULL && die->die_parent == NULL)
20859     add_child_die (context_die, die);
20860   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20861     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20862                                          stmt, context_die);
20863   else
20864     gen_decl_die (decl, origin, context_die);
20865 }
20866
20867 /* Generate all of the decls declared within a given scope and (recursively)
20868    all of its sub-blocks.  */
20869
20870 static void
20871 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20872 {
20873   tree decl;
20874   unsigned int i;
20875   tree subblocks;
20876
20877   /* Ignore NULL blocks.  */
20878   if (stmt == NULL_TREE)
20879     return;
20880
20881   /* Output the DIEs to represent all of the data objects and typedefs
20882      declared directly within this block but not within any nested
20883      sub-blocks.  Also, nested function and tag DIEs have been
20884      generated with a parent of NULL; fix that up now.  */
20885   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20886     process_scope_var (stmt, decl, NULL_TREE, context_die);
20887   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20888     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20889                        context_die);
20890
20891   /* If we're at -g1, we're not interested in subblocks.  */
20892   if (debug_info_level <= DINFO_LEVEL_TERSE)
20893     return;
20894
20895   /* Output the DIEs to represent all sub-blocks (and the items declared
20896      therein) of this block.  */
20897   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20898        subblocks != NULL;
20899        subblocks = BLOCK_CHAIN (subblocks))
20900     gen_block_die (subblocks, context_die, depth + 1);
20901 }
20902
20903 /* Is this a typedef we can avoid emitting?  */
20904
20905 static inline int
20906 is_redundant_typedef (const_tree decl)
20907 {
20908   if (TYPE_DECL_IS_STUB (decl))
20909     return 1;
20910
20911   if (DECL_ARTIFICIAL (decl)
20912       && DECL_CONTEXT (decl)
20913       && is_tagged_type (DECL_CONTEXT (decl))
20914       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20915       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20916     /* Also ignore the artificial member typedef for the class name.  */
20917     return 1;
20918
20919   return 0;
20920 }
20921
20922 /* Return TRUE if TYPE is a typedef that names a type for linkage
20923    purposes. This kind of typedefs is produced by the C++ FE for
20924    constructs like:
20925
20926    typedef struct {...} foo;
20927
20928    In that case, there is no typedef variant type produced for foo.
20929    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20930    struct type.  */
20931
20932 static bool
20933 is_naming_typedef_decl (const_tree decl)
20934 {
20935   if (decl == NULL_TREE
20936       || TREE_CODE (decl) != TYPE_DECL
20937       || !is_tagged_type (TREE_TYPE (decl))
20938       || DECL_IS_BUILTIN (decl)
20939       || is_redundant_typedef (decl)
20940       /* It looks like Ada produces TYPE_DECLs that are very similar
20941          to C++ naming typedefs but that have different
20942          semantics. Let's be specific to c++ for now.  */
20943       || !is_cxx ())
20944     return FALSE;
20945
20946   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20947           && TYPE_NAME (TREE_TYPE (decl)) == decl
20948           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20949               != TYPE_NAME (TREE_TYPE (decl))));
20950 }
20951
20952 /* Returns the DIE for a context.  */
20953
20954 static inline dw_die_ref
20955 get_context_die (tree context)
20956 {
20957   if (context)
20958     {
20959       /* Find die that represents this context.  */
20960       if (TYPE_P (context))
20961         {
20962           context = TYPE_MAIN_VARIANT (context);
20963           return strip_naming_typedef (context, force_type_die (context));
20964         }
20965       else
20966         return force_decl_die (context);
20967     }
20968   return comp_unit_die ();
20969 }
20970
20971 /* Returns the DIE for decl.  A DIE will always be returned.  */
20972
20973 static dw_die_ref
20974 force_decl_die (tree decl)
20975 {
20976   dw_die_ref decl_die;
20977   unsigned saved_external_flag;
20978   tree save_fn = NULL_TREE;
20979   decl_die = lookup_decl_die (decl);
20980   if (!decl_die)
20981     {
20982       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20983
20984       decl_die = lookup_decl_die (decl);
20985       if (decl_die)
20986         return decl_die;
20987
20988       switch (TREE_CODE (decl))
20989         {
20990         case FUNCTION_DECL:
20991           /* Clear current_function_decl, so that gen_subprogram_die thinks
20992              that this is a declaration. At this point, we just want to force
20993              declaration die.  */
20994           save_fn = current_function_decl;
20995           current_function_decl = NULL_TREE;
20996           gen_subprogram_die (decl, context_die);
20997           current_function_decl = save_fn;
20998           break;
20999
21000         case VAR_DECL:
21001           /* Set external flag to force declaration die. Restore it after
21002            gen_decl_die() call.  */
21003           saved_external_flag = DECL_EXTERNAL (decl);
21004           DECL_EXTERNAL (decl) = 1;
21005           gen_decl_die (decl, NULL, context_die);
21006           DECL_EXTERNAL (decl) = saved_external_flag;
21007           break;
21008
21009         case NAMESPACE_DECL:
21010           if (dwarf_version >= 3 || !dwarf_strict)
21011             dwarf2out_decl (decl);
21012           else
21013             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21014             decl_die = comp_unit_die ();
21015           break;
21016
21017         case TRANSLATION_UNIT_DECL:
21018           decl_die = comp_unit_die ();
21019           break;
21020
21021         default:
21022           gcc_unreachable ();
21023         }
21024
21025       /* We should be able to find the DIE now.  */
21026       if (!decl_die)
21027         decl_die = lookup_decl_die (decl);
21028       gcc_assert (decl_die);
21029     }
21030
21031   return decl_die;
21032 }
21033
21034 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21035    always returned.  */
21036
21037 static dw_die_ref
21038 force_type_die (tree type)
21039 {
21040   dw_die_ref type_die;
21041
21042   type_die = lookup_type_die (type);
21043   if (!type_die)
21044     {
21045       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21046
21047       type_die = modified_type_die (type, TYPE_READONLY (type),
21048                                     TYPE_VOLATILE (type), context_die);
21049       gcc_assert (type_die);
21050     }
21051   return type_die;
21052 }
21053
21054 /* Force out any required namespaces to be able to output DECL,
21055    and return the new context_die for it, if it's changed.  */
21056
21057 static dw_die_ref
21058 setup_namespace_context (tree thing, dw_die_ref context_die)
21059 {
21060   tree context = (DECL_P (thing)
21061                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21062   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21063     /* Force out the namespace.  */
21064     context_die = force_decl_die (context);
21065
21066   return context_die;
21067 }
21068
21069 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21070    type) within its namespace, if appropriate.
21071
21072    For compatibility with older debuggers, namespace DIEs only contain
21073    declarations; all definitions are emitted at CU scope.  */
21074
21075 static dw_die_ref
21076 declare_in_namespace (tree thing, dw_die_ref context_die)
21077 {
21078   dw_die_ref ns_context;
21079
21080   if (debug_info_level <= DINFO_LEVEL_TERSE)
21081     return context_die;
21082
21083   /* If this decl is from an inlined function, then don't try to emit it in its
21084      namespace, as we will get confused.  It would have already been emitted
21085      when the abstract instance of the inline function was emitted anyways.  */
21086   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21087     return context_die;
21088
21089   ns_context = setup_namespace_context (thing, context_die);
21090
21091   if (ns_context != context_die)
21092     {
21093       if (is_fortran ())
21094         return ns_context;
21095       if (DECL_P (thing))
21096         gen_decl_die (thing, NULL, ns_context);
21097       else
21098         gen_type_die (thing, ns_context);
21099     }
21100   return context_die;
21101 }
21102
21103 /* Generate a DIE for a namespace or namespace alias.  */
21104
21105 static void
21106 gen_namespace_die (tree decl, dw_die_ref context_die)
21107 {
21108   dw_die_ref namespace_die;
21109
21110   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21111      they are an alias of.  */
21112   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21113     {
21114       /* Output a real namespace or module.  */
21115       context_die = setup_namespace_context (decl, comp_unit_die ());
21116       namespace_die = new_die (is_fortran ()
21117                                ? DW_TAG_module : DW_TAG_namespace,
21118                                context_die, decl);
21119       /* For Fortran modules defined in different CU don't add src coords.  */
21120       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21121         {
21122           const char *name = dwarf2_name (decl, 0);
21123           if (name)
21124             add_name_attribute (namespace_die, name);
21125         }
21126       else
21127         add_name_and_src_coords_attributes (namespace_die, decl);
21128       if (DECL_EXTERNAL (decl))
21129         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21130       equate_decl_number_to_die (decl, namespace_die);
21131     }
21132   else
21133     {
21134       /* Output a namespace alias.  */
21135
21136       /* Force out the namespace we are an alias of, if necessary.  */
21137       dw_die_ref origin_die
21138         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21139
21140       if (DECL_FILE_SCOPE_P (decl)
21141           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21142         context_die = setup_namespace_context (decl, comp_unit_die ());
21143       /* Now create the namespace alias DIE.  */
21144       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21145       add_name_and_src_coords_attributes (namespace_die, decl);
21146       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21147       equate_decl_number_to_die (decl, namespace_die);
21148     }
21149 }
21150
21151 /* Generate Dwarf debug information for a decl described by DECL.
21152    The return value is currently only meaningful for PARM_DECLs,
21153    for all other decls it returns NULL.  */
21154
21155 static dw_die_ref
21156 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21157 {
21158   tree decl_or_origin = decl ? decl : origin;
21159   tree class_origin = NULL, ultimate_origin;
21160
21161   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21162     return NULL;
21163
21164   switch (TREE_CODE (decl_or_origin))
21165     {
21166     case ERROR_MARK:
21167       break;
21168
21169     case CONST_DECL:
21170       if (!is_fortran () && !is_ada ())
21171         {
21172           /* The individual enumerators of an enum type get output when we output
21173              the Dwarf representation of the relevant enum type itself.  */
21174           break;
21175         }
21176
21177       /* Emit its type.  */
21178       gen_type_die (TREE_TYPE (decl), context_die);
21179
21180       /* And its containing namespace.  */
21181       context_die = declare_in_namespace (decl, context_die);
21182
21183       gen_const_die (decl, context_die);
21184       break;
21185
21186     case FUNCTION_DECL:
21187       /* Don't output any DIEs to represent mere function declarations,
21188          unless they are class members or explicit block externs.  */
21189       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21190           && DECL_FILE_SCOPE_P (decl_or_origin)
21191           && (current_function_decl == NULL_TREE
21192               || DECL_ARTIFICIAL (decl_or_origin)))
21193         break;
21194
21195 #if 0
21196       /* FIXME */
21197       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21198          on local redeclarations of global functions.  That seems broken.  */
21199       if (current_function_decl != decl)
21200         /* This is only a declaration.  */;
21201 #endif
21202
21203       /* If we're emitting a clone, emit info for the abstract instance.  */
21204       if (origin || DECL_ORIGIN (decl) != decl)
21205         dwarf2out_abstract_function (origin
21206                                      ? DECL_ORIGIN (origin)
21207                                      : DECL_ABSTRACT_ORIGIN (decl));
21208
21209       /* If we're emitting an out-of-line copy of an inline function,
21210          emit info for the abstract instance and set up to refer to it.  */
21211       else if (cgraph_function_possibly_inlined_p (decl)
21212                && ! DECL_ABSTRACT (decl)
21213                && ! class_or_namespace_scope_p (context_die)
21214                /* dwarf2out_abstract_function won't emit a die if this is just
21215                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21216                   that case, because that works only if we have a die.  */
21217                && DECL_INITIAL (decl) != NULL_TREE)
21218         {
21219           dwarf2out_abstract_function (decl);
21220           set_decl_origin_self (decl);
21221         }
21222
21223       /* Otherwise we're emitting the primary DIE for this decl.  */
21224       else if (debug_info_level > DINFO_LEVEL_TERSE)
21225         {
21226           /* Before we describe the FUNCTION_DECL itself, make sure that we
21227              have its containing type.  */
21228           if (!origin)
21229             origin = decl_class_context (decl);
21230           if (origin != NULL_TREE)
21231             gen_type_die (origin, context_die);
21232
21233           /* And its return type.  */
21234           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21235
21236           /* And its virtual context.  */
21237           if (DECL_VINDEX (decl) != NULL_TREE)
21238             gen_type_die (DECL_CONTEXT (decl), context_die);
21239
21240           /* Make sure we have a member DIE for decl.  */
21241           if (origin != NULL_TREE)
21242             gen_type_die_for_member (origin, decl, context_die);
21243
21244           /* And its containing namespace.  */
21245           context_die = declare_in_namespace (decl, context_die);
21246         }
21247
21248       /* Now output a DIE to represent the function itself.  */
21249       if (decl)
21250         gen_subprogram_die (decl, context_die);
21251       break;
21252
21253     case TYPE_DECL:
21254       /* If we are in terse mode, don't generate any DIEs to represent any
21255          actual typedefs.  */
21256       if (debug_info_level <= DINFO_LEVEL_TERSE)
21257         break;
21258
21259       /* In the special case of a TYPE_DECL node representing the declaration
21260          of some type tag, if the given TYPE_DECL is marked as having been
21261          instantiated from some other (original) TYPE_DECL node (e.g. one which
21262          was generated within the original definition of an inline function) we
21263          used to generate a special (abbreviated) DW_TAG_structure_type,
21264          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21265          should be actually referencing those DIEs, as variable DIEs with that
21266          type would be emitted already in the abstract origin, so it was always
21267          removed during unused type prunning.  Don't add anything in this
21268          case.  */
21269       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21270         break;
21271
21272       if (is_redundant_typedef (decl))
21273         gen_type_die (TREE_TYPE (decl), context_die);
21274       else
21275         /* Output a DIE to represent the typedef itself.  */
21276         gen_typedef_die (decl, context_die);
21277       break;
21278
21279     case LABEL_DECL:
21280       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21281         gen_label_die (decl, context_die);
21282       break;
21283
21284     case VAR_DECL:
21285     case RESULT_DECL:
21286       /* If we are in terse mode, don't generate any DIEs to represent any
21287          variable declarations or definitions.  */
21288       if (debug_info_level <= DINFO_LEVEL_TERSE)
21289         break;
21290
21291       /* Output any DIEs that are needed to specify the type of this data
21292          object.  */
21293       if (decl_by_reference_p (decl_or_origin))
21294         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21295       else
21296         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21297
21298       /* And its containing type.  */
21299       class_origin = decl_class_context (decl_or_origin);
21300       if (class_origin != NULL_TREE)
21301         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21302
21303       /* And its containing namespace.  */
21304       context_die = declare_in_namespace (decl_or_origin, context_die);
21305
21306       /* Now output the DIE to represent the data object itself.  This gets
21307          complicated because of the possibility that the VAR_DECL really
21308          represents an inlined instance of a formal parameter for an inline
21309          function.  */
21310       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21311       if (ultimate_origin != NULL_TREE
21312           && TREE_CODE (ultimate_origin) == PARM_DECL)
21313         gen_formal_parameter_die (decl, origin,
21314                                   true /* Emit name attribute.  */,
21315                                   context_die);
21316       else
21317         gen_variable_die (decl, origin, context_die);
21318       break;
21319
21320     case FIELD_DECL:
21321       /* Ignore the nameless fields that are used to skip bits but handle C++
21322          anonymous unions and structs.  */
21323       if (DECL_NAME (decl) != NULL_TREE
21324           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21325           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21326         {
21327           gen_type_die (member_declared_type (decl), context_die);
21328           gen_field_die (decl, context_die);
21329         }
21330       break;
21331
21332     case PARM_DECL:
21333       if (DECL_BY_REFERENCE (decl_or_origin))
21334         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21335       else
21336         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21337       return gen_formal_parameter_die (decl, origin,
21338                                        true /* Emit name attribute.  */,
21339                                        context_die);
21340
21341     case NAMESPACE_DECL:
21342     case IMPORTED_DECL:
21343       if (dwarf_version >= 3 || !dwarf_strict)
21344         gen_namespace_die (decl, context_die);
21345       break;
21346
21347     default:
21348       /* Probably some frontend-internal decl.  Assume we don't care.  */
21349       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21350       break;
21351     }
21352
21353   return NULL;
21354 }
21355 \f
21356 /* Output debug information for global decl DECL.  Called from toplev.c after
21357    compilation proper has finished.  */
21358
21359 static void
21360 dwarf2out_global_decl (tree decl)
21361 {
21362   /* Output DWARF2 information for file-scope tentative data object
21363      declarations, file-scope (extern) function declarations (which
21364      had no corresponding body) and file-scope tagged type declarations
21365      and definitions which have not yet been forced out.  */
21366   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21367     dwarf2out_decl (decl);
21368 }
21369
21370 /* Output debug information for type decl DECL.  Called from toplev.c
21371    and from language front ends (to record built-in types).  */
21372 static void
21373 dwarf2out_type_decl (tree decl, int local)
21374 {
21375   if (!local)
21376     dwarf2out_decl (decl);
21377 }
21378
21379 /* Output debug information for imported module or decl DECL.
21380    NAME is non-NULL name in the lexical block if the decl has been renamed.
21381    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21382    that DECL belongs to.
21383    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21384 static void
21385 dwarf2out_imported_module_or_decl_1 (tree decl,
21386                                      tree name,
21387                                      tree lexical_block,
21388                                      dw_die_ref lexical_block_die)
21389 {
21390   expanded_location xloc;
21391   dw_die_ref imported_die = NULL;
21392   dw_die_ref at_import_die;
21393
21394   if (TREE_CODE (decl) == IMPORTED_DECL)
21395     {
21396       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21397       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21398       gcc_assert (decl);
21399     }
21400   else
21401     xloc = expand_location (input_location);
21402
21403   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21404     {
21405       at_import_die = force_type_die (TREE_TYPE (decl));
21406       /* For namespace N { typedef void T; } using N::T; base_type_die
21407          returns NULL, but DW_TAG_imported_declaration requires
21408          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21409       if (!at_import_die)
21410         {
21411           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21412           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21413           at_import_die = lookup_type_die (TREE_TYPE (decl));
21414           gcc_assert (at_import_die);
21415         }
21416     }
21417   else
21418     {
21419       at_import_die = lookup_decl_die (decl);
21420       if (!at_import_die)
21421         {
21422           /* If we're trying to avoid duplicate debug info, we may not have
21423              emitted the member decl for this field.  Emit it now.  */
21424           if (TREE_CODE (decl) == FIELD_DECL)
21425             {
21426               tree type = DECL_CONTEXT (decl);
21427
21428               if (TYPE_CONTEXT (type)
21429                   && TYPE_P (TYPE_CONTEXT (type))
21430                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21431                                                 DINFO_USAGE_DIR_USE))
21432                 return;
21433               gen_type_die_for_member (type, decl,
21434                                        get_context_die (TYPE_CONTEXT (type)));
21435             }
21436           at_import_die = force_decl_die (decl);
21437         }
21438     }
21439
21440   if (TREE_CODE (decl) == NAMESPACE_DECL)
21441     {
21442       if (dwarf_version >= 3 || !dwarf_strict)
21443         imported_die = new_die (DW_TAG_imported_module,
21444                                 lexical_block_die,
21445                                 lexical_block);
21446       else
21447         return;
21448     }
21449   else
21450     imported_die = new_die (DW_TAG_imported_declaration,
21451                             lexical_block_die,
21452                             lexical_block);
21453
21454   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21455   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21456   if (name)
21457     add_AT_string (imported_die, DW_AT_name,
21458                    IDENTIFIER_POINTER (name));
21459   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21460 }
21461
21462 /* Output debug information for imported module or decl DECL.
21463    NAME is non-NULL name in context if the decl has been renamed.
21464    CHILD is true if decl is one of the renamed decls as part of
21465    importing whole module.  */
21466
21467 static void
21468 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21469                                    bool child)
21470 {
21471   /* dw_die_ref at_import_die;  */
21472   dw_die_ref scope_die;
21473
21474   if (debug_info_level <= DINFO_LEVEL_TERSE)
21475     return;
21476
21477   gcc_assert (decl);
21478
21479   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21480      We need decl DIE for reference and scope die. First, get DIE for the decl
21481      itself.  */
21482
21483   /* Get the scope die for decl context. Use comp_unit_die for global module
21484      or decl. If die is not found for non globals, force new die.  */
21485   if (context
21486       && TYPE_P (context)
21487       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21488     return;
21489
21490   if (!(dwarf_version >= 3 || !dwarf_strict))
21491     return;
21492
21493   scope_die = get_context_die (context);
21494
21495   if (child)
21496     {
21497       gcc_assert (scope_die->die_child);
21498       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21499       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21500       scope_die = scope_die->die_child;
21501     }
21502
21503   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21504   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21505
21506 }
21507
21508 /* Write the debugging output for DECL.  */
21509
21510 void
21511 dwarf2out_decl (tree decl)
21512 {
21513   dw_die_ref context_die = comp_unit_die ();
21514
21515   switch (TREE_CODE (decl))
21516     {
21517     case ERROR_MARK:
21518       return;
21519
21520     case FUNCTION_DECL:
21521       /* What we would really like to do here is to filter out all mere
21522          file-scope declarations of file-scope functions which are never
21523          referenced later within this translation unit (and keep all of ones
21524          that *are* referenced later on) but we aren't clairvoyant, so we have
21525          no idea which functions will be referenced in the future (i.e. later
21526          on within the current translation unit). So here we just ignore all
21527          file-scope function declarations which are not also definitions.  If
21528          and when the debugger needs to know something about these functions,
21529          it will have to hunt around and find the DWARF information associated
21530          with the definition of the function.
21531
21532          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21533          nodes represent definitions and which ones represent mere
21534          declarations.  We have to check DECL_INITIAL instead. That's because
21535          the C front-end supports some weird semantics for "extern inline"
21536          function definitions.  These can get inlined within the current
21537          translation unit (and thus, we need to generate Dwarf info for their
21538          abstract instances so that the Dwarf info for the concrete inlined
21539          instances can have something to refer to) but the compiler never
21540          generates any out-of-lines instances of such things (despite the fact
21541          that they *are* definitions).
21542
21543          The important point is that the C front-end marks these "extern
21544          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21545          them anyway. Note that the C++ front-end also plays some similar games
21546          for inline function definitions appearing within include files which
21547          also contain `#pragma interface' pragmas.  */
21548       if (DECL_INITIAL (decl) == NULL_TREE)
21549         return;
21550
21551       /* If we're a nested function, initially use a parent of NULL; if we're
21552          a plain function, this will be fixed up in decls_for_scope.  If
21553          we're a method, it will be ignored, since we already have a DIE.  */
21554       if (decl_function_context (decl)
21555           /* But if we're in terse mode, we don't care about scope.  */
21556           && debug_info_level > DINFO_LEVEL_TERSE)
21557         context_die = NULL;
21558       break;
21559
21560     case VAR_DECL:
21561       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21562          declaration and if the declaration was never even referenced from
21563          within this entire compilation unit.  We suppress these DIEs in
21564          order to save space in the .debug section (by eliminating entries
21565          which are probably useless).  Note that we must not suppress
21566          block-local extern declarations (whether used or not) because that
21567          would screw-up the debugger's name lookup mechanism and cause it to
21568          miss things which really ought to be in scope at a given point.  */
21569       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21570         return;
21571
21572       /* For local statics lookup proper context die.  */
21573       if (TREE_STATIC (decl) && decl_function_context (decl))
21574         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21575
21576       /* If we are in terse mode, don't generate any DIEs to represent any
21577          variable declarations or definitions.  */
21578       if (debug_info_level <= DINFO_LEVEL_TERSE)
21579         return;
21580       break;
21581
21582     case CONST_DECL:
21583       if (debug_info_level <= DINFO_LEVEL_TERSE)
21584         return;
21585       if (!is_fortran () && !is_ada ())
21586         return;
21587       if (TREE_STATIC (decl) && decl_function_context (decl))
21588         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21589       break;
21590
21591     case NAMESPACE_DECL:
21592     case IMPORTED_DECL:
21593       if (debug_info_level <= DINFO_LEVEL_TERSE)
21594         return;
21595       if (lookup_decl_die (decl) != NULL)
21596         return;
21597       break;
21598
21599     case TYPE_DECL:
21600       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21601       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21602         return;
21603
21604       /* Don't bother trying to generate any DIEs to represent any of the
21605          normal built-in types for the language we are compiling.  */
21606       if (DECL_IS_BUILTIN (decl))
21607         return;
21608
21609       /* If we are in terse mode, don't generate any DIEs for types.  */
21610       if (debug_info_level <= DINFO_LEVEL_TERSE)
21611         return;
21612
21613       /* If we're a function-scope tag, initially use a parent of NULL;
21614          this will be fixed up in decls_for_scope.  */
21615       if (decl_function_context (decl))
21616         context_die = NULL;
21617
21618       break;
21619
21620     default:
21621       return;
21622     }
21623
21624   gen_decl_die (decl, NULL, context_die);
21625 }
21626
21627 /* Write the debugging output for DECL.  */
21628
21629 static void
21630 dwarf2out_function_decl (tree decl)
21631 {
21632   dwarf2out_decl (decl);
21633   call_arg_locations = NULL;
21634   call_arg_loc_last = NULL;
21635   call_site_count = -1;
21636   tail_call_site_count = -1;
21637   VEC_free (dw_die_ref, heap, block_map);
21638   htab_empty (decl_loc_table);
21639 }
21640
21641 /* Output a marker (i.e. a label) for the beginning of the generated code for
21642    a lexical block.  */
21643
21644 static void
21645 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21646                        unsigned int blocknum)
21647 {
21648   switch_to_section (current_function_section ());
21649   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21650 }
21651
21652 /* Output a marker (i.e. a label) for the end of the generated code for a
21653    lexical block.  */
21654
21655 static void
21656 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21657 {
21658   switch_to_section (current_function_section ());
21659   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21660 }
21661
21662 /* Returns nonzero if it is appropriate not to emit any debugging
21663    information for BLOCK, because it doesn't contain any instructions.
21664
21665    Don't allow this for blocks with nested functions or local classes
21666    as we would end up with orphans, and in the presence of scheduling
21667    we may end up calling them anyway.  */
21668
21669 static bool
21670 dwarf2out_ignore_block (const_tree block)
21671 {
21672   tree decl;
21673   unsigned int i;
21674
21675   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21676     if (TREE_CODE (decl) == FUNCTION_DECL
21677         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21678       return 0;
21679   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21680     {
21681       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21682       if (TREE_CODE (decl) == FUNCTION_DECL
21683           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21684       return 0;
21685     }
21686
21687   return 1;
21688 }
21689
21690 /* Hash table routines for file_hash.  */
21691
21692 static int
21693 file_table_eq (const void *p1_p, const void *p2_p)
21694 {
21695   const struct dwarf_file_data *const p1 =
21696     (const struct dwarf_file_data *) p1_p;
21697   const char *const p2 = (const char *) p2_p;
21698   return filename_cmp (p1->filename, p2) == 0;
21699 }
21700
21701 static hashval_t
21702 file_table_hash (const void *p_p)
21703 {
21704   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21705   return htab_hash_string (p->filename);
21706 }
21707
21708 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21709    dwarf2out.c) and return its "index".  The index of each (known) filename is
21710    just a unique number which is associated with only that one filename.  We
21711    need such numbers for the sake of generating labels (in the .debug_sfnames
21712    section) and references to those files numbers (in the .debug_srcinfo
21713    and.debug_macinfo sections).  If the filename given as an argument is not
21714    found in our current list, add it to the list and assign it the next
21715    available unique index number.  In order to speed up searches, we remember
21716    the index of the filename was looked up last.  This handles the majority of
21717    all searches.  */
21718
21719 static struct dwarf_file_data *
21720 lookup_filename (const char *file_name)
21721 {
21722   void ** slot;
21723   struct dwarf_file_data * created;
21724
21725   /* Check to see if the file name that was searched on the previous
21726      call matches this file name.  If so, return the index.  */
21727   if (file_table_last_lookup
21728       && (file_name == file_table_last_lookup->filename
21729           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21730     return file_table_last_lookup;
21731
21732   /* Didn't match the previous lookup, search the table.  */
21733   slot = htab_find_slot_with_hash (file_table, file_name,
21734                                    htab_hash_string (file_name), INSERT);
21735   if (*slot)
21736     return (struct dwarf_file_data *) *slot;
21737
21738   created = ggc_alloc_dwarf_file_data ();
21739   created->filename = file_name;
21740   created->emitted_number = 0;
21741   *slot = created;
21742   return created;
21743 }
21744
21745 /* If the assembler will construct the file table, then translate the compiler
21746    internal file table number into the assembler file table number, and emit
21747    a .file directive if we haven't already emitted one yet.  The file table
21748    numbers are different because we prune debug info for unused variables and
21749    types, which may include filenames.  */
21750
21751 static int
21752 maybe_emit_file (struct dwarf_file_data * fd)
21753 {
21754   if (! fd->emitted_number)
21755     {
21756       if (last_emitted_file)
21757         fd->emitted_number = last_emitted_file->emitted_number + 1;
21758       else
21759         fd->emitted_number = 1;
21760       last_emitted_file = fd;
21761
21762       if (DWARF2_ASM_LINE_DEBUG_INFO)
21763         {
21764           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21765           output_quoted_string (asm_out_file,
21766                                 remap_debug_filename (fd->filename));
21767           fputc ('\n', asm_out_file);
21768         }
21769     }
21770
21771   return fd->emitted_number;
21772 }
21773
21774 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21775    That generation should happen after function debug info has been
21776    generated. The value of the attribute is the constant value of ARG.  */
21777
21778 static void
21779 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21780 {
21781   die_arg_entry entry;
21782
21783   if (!die || !arg)
21784     return;
21785
21786   if (!tmpl_value_parm_die_table)
21787     tmpl_value_parm_die_table
21788       = VEC_alloc (die_arg_entry, gc, 32);
21789
21790   entry.die = die;
21791   entry.arg = arg;
21792   VEC_safe_push (die_arg_entry, gc,
21793                  tmpl_value_parm_die_table,
21794                  &entry);
21795 }
21796
21797 /* Return TRUE if T is an instance of generic type, FALSE
21798    otherwise.  */
21799
21800 static bool
21801 generic_type_p (tree t)
21802 {
21803   if (t == NULL_TREE || !TYPE_P (t))
21804     return false;
21805   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21806 }
21807
21808 /* Schedule the generation of the generic parameter dies for the
21809   instance of generic type T. The proper generation itself is later
21810   done by gen_scheduled_generic_parms_dies. */
21811
21812 static void
21813 schedule_generic_params_dies_gen (tree t)
21814 {
21815   if (!generic_type_p (t))
21816     return;
21817
21818   if (generic_type_instances == NULL)
21819     generic_type_instances = VEC_alloc (tree, gc, 256);
21820
21821   VEC_safe_push (tree, gc, generic_type_instances, t);
21822 }
21823
21824 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21825    by append_entry_to_tmpl_value_parm_die_table. This function must
21826    be called after function DIEs have been generated.  */
21827
21828 static void
21829 gen_remaining_tmpl_value_param_die_attribute (void)
21830 {
21831   if (tmpl_value_parm_die_table)
21832     {
21833       unsigned i;
21834       die_arg_entry *e;
21835
21836       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21837         tree_add_const_value_attribute (e->die, e->arg);
21838     }
21839 }
21840
21841 /* Generate generic parameters DIEs for instances of generic types
21842    that have been previously scheduled by
21843    schedule_generic_params_dies_gen. This function must be called
21844    after all the types of the CU have been laid out.  */
21845
21846 static void
21847 gen_scheduled_generic_parms_dies (void)
21848 {
21849   unsigned i;
21850   tree t;
21851
21852   if (generic_type_instances == NULL)
21853     return;
21854   
21855   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21856     gen_generic_params_dies (t);
21857 }
21858
21859
21860 /* Replace DW_AT_name for the decl with name.  */
21861
21862 static void
21863 dwarf2out_set_name (tree decl, tree name)
21864 {
21865   dw_die_ref die;
21866   dw_attr_ref attr;
21867   const char *dname;
21868
21869   die = TYPE_SYMTAB_DIE (decl);
21870   if (!die)
21871     return;
21872
21873   dname = dwarf2_name (name, 0);
21874   if (!dname)
21875     return;
21876
21877   attr = get_AT (die, DW_AT_name);
21878   if (attr)
21879     {
21880       struct indirect_string_node *node;
21881
21882       node = find_AT_string (dname);
21883       /* replace the string.  */
21884       attr->dw_attr_val.v.val_str = node;
21885     }
21886
21887   else
21888     add_name_attribute (die, dname);
21889 }
21890
21891 /* Called by the final INSN scan whenever we see a var location.  We
21892    use it to drop labels in the right places, and throw the location in
21893    our lookup table.  */
21894
21895 static void
21896 dwarf2out_var_location (rtx loc_note)
21897 {
21898   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21899   struct var_loc_node *newloc;
21900   rtx next_real;
21901   static const char *last_label;
21902   static const char *last_postcall_label;
21903   static bool last_in_cold_section_p;
21904   tree decl;
21905   bool var_loc_p;
21906
21907   if (!NOTE_P (loc_note))
21908     {
21909       if (CALL_P (loc_note))
21910         {
21911           call_site_count++;
21912           if (SIBLING_CALL_P (loc_note))
21913             tail_call_site_count++;
21914         }
21915       return;
21916     }
21917
21918   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21919   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21920     return;
21921
21922   next_real = next_real_insn (loc_note);
21923
21924   /* If there are no instructions which would be affected by this note,
21925      don't do anything.  */
21926   if (var_loc_p
21927       && next_real == NULL_RTX
21928       && !NOTE_DURING_CALL_P (loc_note))
21929     return;
21930
21931   if (next_real == NULL_RTX)
21932     next_real = get_last_insn ();
21933
21934   /* If there were any real insns between note we processed last time
21935      and this note (or if it is the first note), clear
21936      last_{,postcall_}label so that they are not reused this time.  */
21937   if (last_var_location_insn == NULL_RTX
21938       || last_var_location_insn != next_real
21939       || last_in_cold_section_p != in_cold_section_p)
21940     {
21941       last_label = NULL;
21942       last_postcall_label = NULL;
21943     }
21944
21945   if (var_loc_p)
21946     {
21947       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21948       newloc = add_var_loc_to_decl (decl, loc_note,
21949                                     NOTE_DURING_CALL_P (loc_note)
21950                                     ? last_postcall_label : last_label);
21951       if (newloc == NULL)
21952         return;
21953     }
21954   else
21955     {
21956       decl = NULL_TREE;
21957       newloc = NULL;
21958     }
21959
21960   /* If there were no real insns between note we processed last time
21961      and this note, use the label we emitted last time.  Otherwise
21962      create a new label and emit it.  */
21963   if (last_label == NULL)
21964     {
21965       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21966       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21967       loclabel_num++;
21968       last_label = ggc_strdup (loclabel);
21969     }
21970
21971   if (!var_loc_p)
21972     {
21973       struct call_arg_loc_node *ca_loc
21974         = ggc_alloc_cleared_call_arg_loc_node ();
21975       rtx prev = prev_real_insn (loc_note), x;
21976       ca_loc->call_arg_loc_note = loc_note;
21977       ca_loc->next = NULL;
21978       ca_loc->label = last_label;
21979       gcc_assert (prev
21980                   && (CALL_P (prev)
21981                       || (NONJUMP_INSN_P (prev)
21982                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21983                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21984       if (!CALL_P (prev))
21985         prev = XVECEXP (PATTERN (prev), 0, 0);
21986       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21987       x = PATTERN (prev);
21988       if (GET_CODE (x) == PARALLEL)
21989         x = XVECEXP (x, 0, 0);
21990       if (GET_CODE (x) == SET)
21991         x = SET_SRC (x);
21992       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
21993         {
21994           x = XEXP (XEXP (x, 0), 0);
21995           if (GET_CODE (x) == SYMBOL_REF
21996               && SYMBOL_REF_DECL (x)
21997               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21998             ca_loc->symbol_ref = x;
21999         }
22000       ca_loc->block = insn_scope (prev);
22001       if (call_arg_locations)
22002         call_arg_loc_last->next = ca_loc;
22003       else
22004         call_arg_locations = ca_loc;
22005       call_arg_loc_last = ca_loc;
22006     }
22007   else if (!NOTE_DURING_CALL_P (loc_note))
22008     newloc->label = last_label;
22009   else
22010     {
22011       if (!last_postcall_label)
22012         {
22013           sprintf (loclabel, "%s-1", last_label);
22014           last_postcall_label = ggc_strdup (loclabel);
22015         }
22016       newloc->label = last_postcall_label;
22017     }
22018
22019   last_var_location_insn = next_real;
22020   last_in_cold_section_p = in_cold_section_p;
22021 }
22022
22023 /* We need to reset the locations at the beginning of each
22024    function. We can't do this in the end_function hook, because the
22025    declarations that use the locations won't have been output when
22026    that hook is called.  Also compute have_multiple_function_sections here.  */
22027
22028 static void
22029 dwarf2out_begin_function (tree fun)
22030 {
22031   if (function_section (fun) != text_section)
22032     have_multiple_function_sections = true;
22033   else if (flag_reorder_blocks_and_partition && !cold_text_section)
22034     {
22035       gcc_assert (current_function_decl == fun);
22036       cold_text_section = unlikely_text_section ();
22037       switch_to_section (cold_text_section);
22038       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22039       switch_to_section (current_function_section ());
22040     }
22041
22042   dwarf2out_note_section_used ();
22043   call_site_count = 0;
22044   tail_call_site_count = 0;
22045 }
22046
22047 /* Output a label to mark the beginning of a source code line entry
22048    and record information relating to this source line, in
22049    'line_info_table' for later output of the .debug_line section.  */
22050
22051 static void
22052 dwarf2out_source_line (unsigned int line, const char *filename,
22053                        int discriminator, bool is_stmt)
22054 {
22055   static bool last_is_stmt = true;
22056
22057   if (debug_info_level >= DINFO_LEVEL_NORMAL
22058       && line != 0)
22059     {
22060       int file_num = maybe_emit_file (lookup_filename (filename));
22061
22062       switch_to_section (current_function_section ());
22063
22064       /* If requested, emit something human-readable.  */
22065       if (flag_debug_asm)
22066         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22067                  filename, line);
22068
22069       if (DWARF2_ASM_LINE_DEBUG_INFO)
22070         {
22071           /* Emit the .loc directive understood by GNU as.  */
22072           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22073           if (is_stmt != last_is_stmt)
22074             {
22075               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22076               last_is_stmt = is_stmt;
22077             }
22078           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22079             fprintf (asm_out_file, " discriminator %d", discriminator);
22080           fputc ('\n', asm_out_file);
22081
22082           /* Indicate that line number info exists.  */
22083           line_info_table_in_use++;
22084         }
22085       else if (function_section (current_function_decl) != text_section)
22086         {
22087           dw_separate_line_info_ref line_info;
22088           targetm.asm_out.internal_label (asm_out_file,
22089                                           SEPARATE_LINE_CODE_LABEL,
22090                                           separate_line_info_table_in_use);
22091
22092           /* Expand the line info table if necessary.  */
22093           if (separate_line_info_table_in_use
22094               == separate_line_info_table_allocated)
22095             {
22096               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22097               separate_line_info_table
22098                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22099                                  separate_line_info_table,
22100                                  separate_line_info_table_allocated);
22101               memset (separate_line_info_table
22102                        + separate_line_info_table_in_use,
22103                       0,
22104                       (LINE_INFO_TABLE_INCREMENT
22105                        * sizeof (dw_separate_line_info_entry)));
22106             }
22107
22108           /* Add the new entry at the end of the line_info_table.  */
22109           line_info
22110             = &separate_line_info_table[separate_line_info_table_in_use++];
22111           line_info->dw_file_num = file_num;
22112           line_info->dw_line_num = line;
22113           line_info->function = current_function_funcdef_no;
22114         }
22115       else
22116         {
22117           dw_line_info_ref line_info;
22118
22119           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22120                                      line_info_table_in_use);
22121
22122           /* Expand the line info table if necessary.  */
22123           if (line_info_table_in_use == line_info_table_allocated)
22124             {
22125               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22126               line_info_table
22127                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22128                                  line_info_table_allocated);
22129               memset (line_info_table + line_info_table_in_use, 0,
22130                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22131             }
22132
22133           /* Add the new entry at the end of the line_info_table.  */
22134           line_info = &line_info_table[line_info_table_in_use++];
22135           line_info->dw_file_num = file_num;
22136           line_info->dw_line_num = line;
22137         }
22138     }
22139 }
22140
22141 /* Record the beginning of a new source file.  */
22142
22143 static void
22144 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22145 {
22146   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22147     {
22148       /* Record the beginning of the file for break_out_includes.  */
22149       dw_die_ref bincl_die;
22150
22151       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22152       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22153     }
22154
22155   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22156     {
22157       macinfo_entry e;
22158       e.code = DW_MACINFO_start_file;
22159       e.lineno = lineno;
22160       e.info = xstrdup (filename);
22161       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22162     }
22163 }
22164
22165 /* Record the end of a source file.  */
22166
22167 static void
22168 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22169 {
22170   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22171     /* Record the end of the file for break_out_includes.  */
22172     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22173
22174   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22175     {
22176       macinfo_entry e;
22177       e.code = DW_MACINFO_end_file;
22178       e.lineno = lineno;
22179       e.info = NULL;
22180       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22181     }
22182 }
22183
22184 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22185    the tail part of the directive line, i.e. the part which is past the
22186    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22187
22188 static void
22189 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22190                   const char *buffer ATTRIBUTE_UNUSED)
22191 {
22192   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22193     {
22194       macinfo_entry e;
22195       e.code = DW_MACINFO_define;
22196       e.lineno = lineno;
22197       e.info = xstrdup (buffer);;
22198       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22199     }
22200 }
22201
22202 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22203    the tail part of the directive line, i.e. the part which is past the
22204    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22205
22206 static void
22207 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22208                  const char *buffer ATTRIBUTE_UNUSED)
22209 {
22210   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22211     {
22212       macinfo_entry e;
22213       e.code = DW_MACINFO_undef;
22214       e.lineno = lineno;
22215       e.info = xstrdup (buffer);;
22216       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22217     }
22218 }
22219
22220 static void
22221 output_macinfo (void)
22222 {
22223   unsigned i;
22224   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22225   macinfo_entry *ref;
22226
22227   if (! length)
22228     return;
22229
22230   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22231     {
22232       switch (ref->code)
22233         {
22234           case DW_MACINFO_start_file:
22235             {
22236               int file_num = maybe_emit_file (lookup_filename (ref->info));
22237               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22238               dw2_asm_output_data_uleb128 
22239                         (ref->lineno, "Included from line number %lu", 
22240                                                 (unsigned long)ref->lineno);
22241               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22242             }
22243             break;
22244           case DW_MACINFO_end_file:
22245             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22246             break;
22247           case DW_MACINFO_define:
22248             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22249             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22250                                                 (unsigned long)ref->lineno);
22251             dw2_asm_output_nstring (ref->info, -1, "The macro");
22252             break;
22253           case DW_MACINFO_undef:
22254             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22255             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22256                                                 (unsigned long)ref->lineno);
22257             dw2_asm_output_nstring (ref->info, -1, "The macro");
22258             break;
22259           default:
22260            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22261              ASM_COMMENT_START, (unsigned long)ref->code);
22262           break;
22263         }
22264     }
22265 }
22266
22267 /* Set up for Dwarf output at the start of compilation.  */
22268
22269 static void
22270 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22271 {
22272   /* Allocate the file_table.  */
22273   file_table = htab_create_ggc (50, file_table_hash,
22274                                 file_table_eq, NULL);
22275
22276   /* Allocate the decl_die_table.  */
22277   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22278                                     decl_die_table_eq, NULL);
22279
22280   /* Allocate the decl_loc_table.  */
22281   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22282                                     decl_loc_table_eq, NULL);
22283
22284   /* Allocate the initial hunk of the decl_scope_table.  */
22285   decl_scope_table = VEC_alloc (tree, gc, 256);
22286
22287   /* Allocate the initial hunk of the abbrev_die_table.  */
22288   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22289     (ABBREV_DIE_TABLE_INCREMENT);
22290   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22291   /* Zero-th entry is allocated, but unused.  */
22292   abbrev_die_table_in_use = 1;
22293
22294   /* Allocate the initial hunk of the line_info_table.  */
22295   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22296     (LINE_INFO_TABLE_INCREMENT);
22297   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22298
22299   /* Zero-th entry is allocated, but unused.  */
22300   line_info_table_in_use = 1;
22301
22302   /* Allocate the pubtypes and pubnames vectors.  */
22303   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22304   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22305
22306   incomplete_types = VEC_alloc (tree, gc, 64);
22307
22308   used_rtx_array = VEC_alloc (rtx, gc, 32);
22309
22310   debug_info_section = get_section (DEBUG_INFO_SECTION,
22311                                     SECTION_DEBUG, NULL);
22312   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22313                                       SECTION_DEBUG, NULL);
22314   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22315                                        SECTION_DEBUG, NULL);
22316   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22317                                        SECTION_DEBUG, NULL);
22318   debug_line_section = get_section (DEBUG_LINE_SECTION,
22319                                     SECTION_DEBUG, NULL);
22320   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22321                                    SECTION_DEBUG, NULL);
22322   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22323                                         SECTION_DEBUG, NULL);
22324   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22325                                         SECTION_DEBUG, NULL);
22326   debug_str_section = get_section (DEBUG_STR_SECTION,
22327                                    DEBUG_STR_SECTION_FLAGS, NULL);
22328   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22329                                       SECTION_DEBUG, NULL);
22330   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22331                                      SECTION_DEBUG, NULL);
22332
22333   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22334   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22335                                DEBUG_ABBREV_SECTION_LABEL, 0);
22336   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22337   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22338                                COLD_TEXT_SECTION_LABEL, 0);
22339   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22340
22341   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22342                                DEBUG_INFO_SECTION_LABEL, 0);
22343   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22344                                DEBUG_LINE_SECTION_LABEL, 0);
22345   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22346                                DEBUG_RANGES_SECTION_LABEL, 0);
22347   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22348                                DEBUG_MACINFO_SECTION_LABEL, 0);
22349
22350   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22351     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22352
22353   switch_to_section (text_section);
22354   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22355 }
22356
22357 /* Called before cgraph_optimize starts outputtting functions, variables
22358    and toplevel asms into assembly.  */
22359
22360 static void
22361 dwarf2out_assembly_start (void)
22362 {
22363   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22364       && dwarf2out_do_cfi_asm ()
22365       && (!(flag_unwind_tables || flag_exceptions)
22366           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22367     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22368 }
22369
22370 /* A helper function for dwarf2out_finish called through
22371    htab_traverse.  Emit one queued .debug_str string.  */
22372
22373 static int
22374 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22375 {
22376   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22377
22378   if (node->label && node->refcount)
22379     {
22380       switch_to_section (debug_str_section);
22381       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22382       assemble_string (node->str, strlen (node->str) + 1);
22383     }
22384
22385   return 1;
22386 }
22387
22388 #if ENABLE_ASSERT_CHECKING
22389 /* Verify that all marks are clear.  */
22390
22391 static void
22392 verify_marks_clear (dw_die_ref die)
22393 {
22394   dw_die_ref c;
22395
22396   gcc_assert (! die->die_mark);
22397   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22398 }
22399 #endif /* ENABLE_ASSERT_CHECKING */
22400
22401 /* Clear the marks for a die and its children.
22402    Be cool if the mark isn't set.  */
22403
22404 static void
22405 prune_unmark_dies (dw_die_ref die)
22406 {
22407   dw_die_ref c;
22408
22409   if (die->die_mark)
22410     die->die_mark = 0;
22411   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22412 }
22413
22414 /* Given DIE that we're marking as used, find any other dies
22415    it references as attributes and mark them as used.  */
22416
22417 static void
22418 prune_unused_types_walk_attribs (dw_die_ref die)
22419 {
22420   dw_attr_ref a;
22421   unsigned ix;
22422
22423   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22424     {
22425       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22426         {
22427           /* A reference to another DIE.
22428              Make sure that it will get emitted.
22429              If it was broken out into a comdat group, don't follow it.  */
22430           if (dwarf_version < 4
22431               || a->dw_attr == DW_AT_specification
22432               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22433             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22434         }
22435       /* Set the string's refcount to 0 so that prune_unused_types_mark
22436          accounts properly for it.  */
22437       if (AT_class (a) == dw_val_class_str)
22438         a->dw_attr_val.v.val_str->refcount = 0;
22439     }
22440 }
22441
22442 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22443
22444 static void
22445 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22446 {
22447   dw_die_ref c;
22448
22449   if (die == NULL || die->die_child == NULL)
22450     return;
22451   c = die->die_child;
22452   do
22453     {
22454       switch (c->die_tag)
22455         {
22456         case DW_TAG_template_type_param:
22457         case DW_TAG_template_value_param:
22458         case DW_TAG_GNU_template_template_param:
22459         case DW_TAG_GNU_template_parameter_pack:
22460           prune_unused_types_mark (c, 1);
22461           break;
22462         default:
22463           break;
22464         }
22465       c = c->die_sib;
22466     } while (c && c != die->die_child);
22467 }
22468
22469 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22470    to DIE's children.  */
22471
22472 static void
22473 prune_unused_types_mark (dw_die_ref die, int dokids)
22474 {
22475   dw_die_ref c;
22476
22477   if (die->die_mark == 0)
22478     {
22479       /* We haven't done this node yet.  Mark it as used.  */
22480       die->die_mark = 1;
22481       /* If this is the DIE of a generic type instantiation,
22482          mark the children DIEs that describe its generic parms and
22483          args.  */
22484       prune_unused_types_mark_generic_parms_dies (die);
22485
22486       /* We also have to mark its parents as used.
22487          (But we don't want to mark our parents' kids due to this.)  */
22488       if (die->die_parent)
22489         prune_unused_types_mark (die->die_parent, 0);
22490
22491       /* Mark any referenced nodes.  */
22492       prune_unused_types_walk_attribs (die);
22493
22494       /* If this node is a specification,
22495          also mark the definition, if it exists.  */
22496       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22497         prune_unused_types_mark (die->die_definition, 1);
22498     }
22499
22500   if (dokids && die->die_mark != 2)
22501     {
22502       /* We need to walk the children, but haven't done so yet.
22503          Remember that we've walked the kids.  */
22504       die->die_mark = 2;
22505
22506       /* If this is an array type, we need to make sure our
22507          kids get marked, even if they're types.  If we're
22508          breaking out types into comdat sections, do this
22509          for all type definitions.  */
22510       if (die->die_tag == DW_TAG_array_type
22511           || (dwarf_version >= 4
22512               && is_type_die (die) && ! is_declaration_die (die)))
22513         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22514       else
22515         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22516     }
22517 }
22518
22519 /* For local classes, look if any static member functions were emitted
22520    and if so, mark them.  */
22521
22522 static void
22523 prune_unused_types_walk_local_classes (dw_die_ref die)
22524 {
22525   dw_die_ref c;
22526
22527   if (die->die_mark == 2)
22528     return;
22529
22530   switch (die->die_tag)
22531     {
22532     case DW_TAG_structure_type:
22533     case DW_TAG_union_type:
22534     case DW_TAG_class_type:
22535       break;
22536
22537     case DW_TAG_subprogram:
22538       if (!get_AT_flag (die, DW_AT_declaration)
22539           || die->die_definition != NULL)
22540         prune_unused_types_mark (die, 1);
22541       return;
22542
22543     default:
22544       return;
22545     }
22546
22547   /* Mark children.  */
22548   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22549 }
22550
22551 /* Walk the tree DIE and mark types that we actually use.  */
22552
22553 static void
22554 prune_unused_types_walk (dw_die_ref die)
22555 {
22556   dw_die_ref c;
22557
22558   /* Don't do anything if this node is already marked and
22559      children have been marked as well.  */
22560   if (die->die_mark == 2)
22561     return;
22562
22563   switch (die->die_tag)
22564     {
22565     case DW_TAG_structure_type:
22566     case DW_TAG_union_type:
22567     case DW_TAG_class_type:
22568       if (die->die_perennial_p)
22569         break;
22570
22571       for (c = die->die_parent; c; c = c->die_parent)
22572         if (c->die_tag == DW_TAG_subprogram)
22573           break;
22574
22575       /* Finding used static member functions inside of classes
22576          is needed just for local classes, because for other classes
22577          static member function DIEs with DW_AT_specification
22578          are emitted outside of the DW_TAG_*_type.  If we ever change
22579          it, we'd need to call this even for non-local classes.  */
22580       if (c)
22581         prune_unused_types_walk_local_classes (die);
22582
22583       /* It's a type node --- don't mark it.  */
22584       return;
22585
22586     case DW_TAG_const_type:
22587     case DW_TAG_packed_type:
22588     case DW_TAG_pointer_type:
22589     case DW_TAG_reference_type:
22590     case DW_TAG_rvalue_reference_type:
22591     case DW_TAG_volatile_type:
22592     case DW_TAG_typedef:
22593     case DW_TAG_array_type:
22594     case DW_TAG_interface_type:
22595     case DW_TAG_friend:
22596     case DW_TAG_variant_part:
22597     case DW_TAG_enumeration_type:
22598     case DW_TAG_subroutine_type:
22599     case DW_TAG_string_type:
22600     case DW_TAG_set_type:
22601     case DW_TAG_subrange_type:
22602     case DW_TAG_ptr_to_member_type:
22603     case DW_TAG_file_type:
22604       if (die->die_perennial_p)
22605         break;
22606
22607       /* It's a type node --- don't mark it.  */
22608       return;
22609
22610     default:
22611       /* Mark everything else.  */
22612       break;
22613   }
22614
22615   if (die->die_mark == 0)
22616     {
22617       die->die_mark = 1;
22618
22619       /* Now, mark any dies referenced from here.  */
22620       prune_unused_types_walk_attribs (die);
22621     }
22622
22623   die->die_mark = 2;
22624
22625   /* Mark children.  */
22626   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22627 }
22628
22629 /* Increment the string counts on strings referred to from DIE's
22630    attributes.  */
22631
22632 static void
22633 prune_unused_types_update_strings (dw_die_ref die)
22634 {
22635   dw_attr_ref a;
22636   unsigned ix;
22637
22638   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22639     if (AT_class (a) == dw_val_class_str)
22640       {
22641         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22642         s->refcount++;
22643         /* Avoid unnecessarily putting strings that are used less than
22644            twice in the hash table.  */
22645         if (s->refcount
22646             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22647           {
22648             void ** slot;
22649             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22650                                              htab_hash_string (s->str),
22651                                              INSERT);
22652             gcc_assert (*slot == NULL);
22653             *slot = s;
22654           }
22655       }
22656 }
22657
22658 /* Remove from the tree DIE any dies that aren't marked.  */
22659
22660 static void
22661 prune_unused_types_prune (dw_die_ref die)
22662 {
22663   dw_die_ref c;
22664
22665   gcc_assert (die->die_mark);
22666   prune_unused_types_update_strings (die);
22667
22668   if (! die->die_child)
22669     return;
22670
22671   c = die->die_child;
22672   do {
22673     dw_die_ref prev = c;
22674     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22675       if (c == die->die_child)
22676         {
22677           /* No marked children between 'prev' and the end of the list.  */
22678           if (prev == c)
22679             /* No marked children at all.  */
22680             die->die_child = NULL;
22681           else
22682             {
22683               prev->die_sib = c->die_sib;
22684               die->die_child = prev;
22685             }
22686           return;
22687         }
22688
22689     if (c != prev->die_sib)
22690       prev->die_sib = c;
22691     prune_unused_types_prune (c);
22692   } while (c != die->die_child);
22693 }
22694
22695 /* A helper function for dwarf2out_finish called through
22696    htab_traverse.  Clear .debug_str strings that we haven't already
22697    decided to emit.  */
22698
22699 static int
22700 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22701 {
22702   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22703
22704   if (!node->label || !node->refcount)
22705     htab_clear_slot (debug_str_hash, h);
22706
22707   return 1;
22708 }
22709
22710 /* Remove dies representing declarations that we never use.  */
22711
22712 static void
22713 prune_unused_types (void)
22714 {
22715   unsigned int i;
22716   limbo_die_node *node;
22717   comdat_type_node *ctnode;
22718   pubname_ref pub;
22719
22720 #if ENABLE_ASSERT_CHECKING
22721   /* All the marks should already be clear.  */
22722   verify_marks_clear (comp_unit_die ());
22723   for (node = limbo_die_list; node; node = node->next)
22724     verify_marks_clear (node->die);
22725   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22726     verify_marks_clear (ctnode->root_die);
22727 #endif /* ENABLE_ASSERT_CHECKING */
22728
22729   /* Mark types that are used in global variables.  */
22730   premark_types_used_by_global_vars ();
22731
22732   /* Set the mark on nodes that are actually used.  */
22733   prune_unused_types_walk (comp_unit_die ());
22734   for (node = limbo_die_list; node; node = node->next)
22735     prune_unused_types_walk (node->die);
22736   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22737     {
22738       prune_unused_types_walk (ctnode->root_die);
22739       prune_unused_types_mark (ctnode->type_die, 1);
22740     }
22741
22742   /* Also set the mark on nodes referenced from the
22743      pubname_table or arange_table.  */
22744   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22745     prune_unused_types_mark (pub->die, 1);
22746   for (i = 0; i < arange_table_in_use; i++)
22747     prune_unused_types_mark (arange_table[i], 1);
22748
22749   /* Get rid of nodes that aren't marked; and update the string counts.  */
22750   if (debug_str_hash && debug_str_hash_forced)
22751     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22752   else if (debug_str_hash)
22753     htab_empty (debug_str_hash);
22754   prune_unused_types_prune (comp_unit_die ());
22755   for (node = limbo_die_list; node; node = node->next)
22756     prune_unused_types_prune (node->die);
22757   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22758     prune_unused_types_prune (ctnode->root_die);
22759
22760   /* Leave the marks clear.  */
22761   prune_unmark_dies (comp_unit_die ());
22762   for (node = limbo_die_list; node; node = node->next)
22763     prune_unmark_dies (node->die);
22764   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22765     prune_unmark_dies (ctnode->root_die);
22766 }
22767
22768 /* Set the parameter to true if there are any relative pathnames in
22769    the file table.  */
22770 static int
22771 file_table_relative_p (void ** slot, void *param)
22772 {
22773   bool *p = (bool *) param;
22774   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22775   if (!IS_ABSOLUTE_PATH (d->filename))
22776     {
22777       *p = true;
22778       return 0;
22779     }
22780   return 1;
22781 }
22782
22783 /* Routines to manipulate hash table of comdat type units.  */
22784
22785 static hashval_t
22786 htab_ct_hash (const void *of)
22787 {
22788   hashval_t h;
22789   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22790
22791   memcpy (&h, type_node->signature, sizeof (h));
22792   return h;
22793 }
22794
22795 static int
22796 htab_ct_eq (const void *of1, const void *of2)
22797 {
22798   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22799   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22800
22801   return (! memcmp (type_node_1->signature, type_node_2->signature,
22802                     DWARF_TYPE_SIGNATURE_SIZE));
22803 }
22804
22805 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22806    to the location it would have been added, should we know its
22807    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22808    probably improve compactness of debug info, removing equivalent
22809    abbrevs, and hide any differences caused by deferring the
22810    computation of the assembler name, triggered by e.g. PCH.  */
22811
22812 static inline void
22813 move_linkage_attr (dw_die_ref die)
22814 {
22815   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22816   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22817
22818   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22819               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22820
22821   while (--ix > 0)
22822     {
22823       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22824
22825       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22826         break;
22827     }
22828
22829   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22830     {
22831       VEC_pop (dw_attr_node, die->die_attr);
22832       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22833     }
22834 }
22835
22836 /* Helper function for resolve_addr, attempt to resolve
22837    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22838    SYMBOL_REFs refer to variables emitted in the current CU.  */
22839
22840 static int
22841 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22842 {
22843   rtx rtl = *addr;
22844
22845   if (GET_CODE (rtl) == CONST_STRING)
22846     {
22847       size_t len = strlen (XSTR (rtl, 0)) + 1;
22848       tree t = build_string (len, XSTR (rtl, 0));
22849       tree tlen = build_int_cst (NULL_TREE, len - 1);
22850       TREE_TYPE (t)
22851         = build_array_type (char_type_node, build_index_type (tlen));
22852       rtl = lookup_constant_def (t);
22853       if (!rtl || !MEM_P (rtl))
22854         return 1;
22855       rtl = XEXP (rtl, 0);
22856       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22857       *addr = rtl;
22858       return 0;
22859     }
22860
22861   if (GET_CODE (rtl) == SYMBOL_REF
22862       && SYMBOL_REF_DECL (rtl))
22863     {
22864       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22865         {
22866           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22867             return 1;
22868         }
22869       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22870         return 1;
22871     }
22872
22873   if (GET_CODE (rtl) == CONST
22874       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22875     return 1;
22876
22877   return 0;
22878 }
22879
22880 /* Helper function for resolve_addr, handle one location
22881    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22882    the location list couldn't be resolved.  */
22883
22884 static bool
22885 resolve_addr_in_expr (dw_loc_descr_ref loc)
22886 {
22887   for (; loc; loc = loc->dw_loc_next)
22888     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22889          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22890         || (loc->dw_loc_opc == DW_OP_implicit_value
22891             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22892             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22893       return false;
22894     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22895              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22896       {
22897         dw_die_ref ref
22898           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22899         if (ref == NULL)
22900           return false;
22901         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22902         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22903         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22904       }
22905   return true;
22906 }
22907
22908 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22909    an address in .rodata section if the string literal is emitted there,
22910    or remove the containing location list or replace DW_AT_const_value
22911    with DW_AT_location and empty location expression, if it isn't found
22912    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22913    to something that has been emitted in the current CU.  */
22914
22915 static void
22916 resolve_addr (dw_die_ref die)
22917 {
22918   dw_die_ref c;
22919   dw_attr_ref a;
22920   dw_loc_list_ref *curr;
22921   unsigned ix;
22922
22923   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22924     switch (AT_class (a))
22925       {
22926       case dw_val_class_loc_list:
22927         curr = AT_loc_list_ptr (a);
22928         while (*curr)
22929           {
22930             if (!resolve_addr_in_expr ((*curr)->expr))
22931               {
22932                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22933                 if (next && (*curr)->ll_symbol)
22934                   {
22935                     gcc_assert (!next->ll_symbol);
22936                     next->ll_symbol = (*curr)->ll_symbol;
22937                   }
22938                 *curr = next;
22939               }
22940             else
22941               curr = &(*curr)->dw_loc_next;
22942           }
22943         if (!AT_loc_list (a))
22944           {
22945             remove_AT (die, a->dw_attr);
22946             ix--;
22947           }
22948         break;
22949       case dw_val_class_loc:
22950         if (!resolve_addr_in_expr (AT_loc (a)))
22951           {
22952             remove_AT (die, a->dw_attr);
22953             ix--;
22954           }
22955         break;
22956       case dw_val_class_addr:
22957         if (a->dw_attr == DW_AT_const_value
22958             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22959           {
22960             remove_AT (die, a->dw_attr);
22961             ix--;
22962           }
22963         if (die->die_tag == DW_TAG_GNU_call_site
22964             && a->dw_attr == DW_AT_abstract_origin)
22965           {
22966             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22967             dw_die_ref tdie = lookup_decl_die (tdecl);
22968             if (tdie == NULL && DECL_EXTERNAL (tdecl))
22969               {
22970                 force_decl_die (tdecl);
22971                 tdie = lookup_decl_die (tdecl);
22972               }
22973             if (tdie)
22974               {
22975                 a->dw_attr_val.val_class = dw_val_class_die_ref;
22976                 a->dw_attr_val.v.val_die_ref.die = tdie;
22977                 a->dw_attr_val.v.val_die_ref.external = 0;
22978               }
22979             else
22980               {
22981                 remove_AT (die, a->dw_attr);
22982                 ix--;
22983               }
22984           }
22985         break;
22986       default:
22987         break;
22988       }
22989
22990   FOR_EACH_CHILD (die, c, resolve_addr (c));
22991 }
22992 \f
22993 /* Helper routines for optimize_location_lists.
22994    This pass tries to share identical local lists in .debug_loc
22995    section.  */
22996
22997 /* Iteratively hash operands of LOC opcode.  */
22998
22999 static inline hashval_t
23000 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23001 {
23002   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23003   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23004
23005   switch (loc->dw_loc_opc)
23006     {
23007     case DW_OP_const4u:
23008     case DW_OP_const8u:
23009       if (loc->dtprel)
23010         goto hash_addr;
23011       /* FALLTHRU */
23012     case DW_OP_const1u:
23013     case DW_OP_const1s:
23014     case DW_OP_const2u:
23015     case DW_OP_const2s:
23016     case DW_OP_const4s:
23017     case DW_OP_const8s:
23018     case DW_OP_constu:
23019     case DW_OP_consts:
23020     case DW_OP_pick:
23021     case DW_OP_plus_uconst:
23022     case DW_OP_breg0:
23023     case DW_OP_breg1:
23024     case DW_OP_breg2:
23025     case DW_OP_breg3:
23026     case DW_OP_breg4:
23027     case DW_OP_breg5:
23028     case DW_OP_breg6:
23029     case DW_OP_breg7:
23030     case DW_OP_breg8:
23031     case DW_OP_breg9:
23032     case DW_OP_breg10:
23033     case DW_OP_breg11:
23034     case DW_OP_breg12:
23035     case DW_OP_breg13:
23036     case DW_OP_breg14:
23037     case DW_OP_breg15:
23038     case DW_OP_breg16:
23039     case DW_OP_breg17:
23040     case DW_OP_breg18:
23041     case DW_OP_breg19:
23042     case DW_OP_breg20:
23043     case DW_OP_breg21:
23044     case DW_OP_breg22:
23045     case DW_OP_breg23:
23046     case DW_OP_breg24:
23047     case DW_OP_breg25:
23048     case DW_OP_breg26:
23049     case DW_OP_breg27:
23050     case DW_OP_breg28:
23051     case DW_OP_breg29:
23052     case DW_OP_breg30:
23053     case DW_OP_breg31:
23054     case DW_OP_regx:
23055     case DW_OP_fbreg:
23056     case DW_OP_piece:
23057     case DW_OP_deref_size:
23058     case DW_OP_xderef_size:
23059       hash = iterative_hash_object (val1->v.val_int, hash);
23060       break;
23061     case DW_OP_skip:
23062     case DW_OP_bra:
23063       {
23064         int offset;
23065
23066         gcc_assert (val1->val_class == dw_val_class_loc);
23067         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23068         hash = iterative_hash_object (offset, hash);
23069       }
23070       break;
23071     case DW_OP_implicit_value:
23072       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23073       switch (val2->val_class)
23074         {
23075         case dw_val_class_const:
23076           hash = iterative_hash_object (val2->v.val_int, hash);
23077           break;
23078         case dw_val_class_vec:
23079           {
23080             unsigned int elt_size = val2->v.val_vec.elt_size;
23081             unsigned int len = val2->v.val_vec.length;
23082
23083             hash = iterative_hash_object (elt_size, hash);
23084             hash = iterative_hash_object (len, hash);
23085             hash = iterative_hash (val2->v.val_vec.array,
23086                                    len * elt_size, hash);
23087           }
23088           break;
23089         case dw_val_class_const_double:
23090           hash = iterative_hash_object (val2->v.val_double.low, hash);
23091           hash = iterative_hash_object (val2->v.val_double.high, hash);
23092           break;
23093         case dw_val_class_addr:
23094           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23095           break;
23096         default:
23097           gcc_unreachable ();
23098         }
23099       break;
23100     case DW_OP_bregx:
23101     case DW_OP_bit_piece:
23102       hash = iterative_hash_object (val1->v.val_int, hash);
23103       hash = iterative_hash_object (val2->v.val_int, hash);
23104       break;
23105     case DW_OP_addr:
23106     hash_addr:
23107       if (loc->dtprel)
23108         {
23109           unsigned char dtprel = 0xd1;
23110           hash = iterative_hash_object (dtprel, hash);
23111         }
23112       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23113       break;
23114     case DW_OP_GNU_implicit_pointer:
23115       hash = iterative_hash_object (val2->v.val_int, hash);
23116       break;
23117
23118     default:
23119       /* Other codes have no operands.  */
23120       break;
23121     }
23122   return hash;
23123 }
23124
23125 /* Iteratively hash the whole DWARF location expression LOC.  */
23126
23127 static inline hashval_t
23128 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23129 {
23130   dw_loc_descr_ref l;
23131   bool sizes_computed = false;
23132   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23133   size_of_locs (loc);
23134
23135   for (l = loc; l != NULL; l = l->dw_loc_next)
23136     {
23137       enum dwarf_location_atom opc = l->dw_loc_opc;
23138       hash = iterative_hash_object (opc, hash);
23139       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23140         {
23141           size_of_locs (loc);
23142           sizes_computed = true;
23143         }
23144       hash = hash_loc_operands (l, hash);
23145     }
23146   return hash;
23147 }
23148
23149 /* Compute hash of the whole location list LIST_HEAD.  */
23150
23151 static inline void
23152 hash_loc_list (dw_loc_list_ref list_head)
23153 {
23154   dw_loc_list_ref curr = list_head;
23155   hashval_t hash = 0;
23156
23157   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23158     {
23159       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23160       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23161       if (curr->section)
23162         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23163                                hash);
23164       hash = hash_locs (curr->expr, hash);
23165     }
23166   list_head->hash = hash;
23167 }
23168
23169 /* Return true if X and Y opcodes have the same operands.  */
23170
23171 static inline bool
23172 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23173 {
23174   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23175   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23176   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23177   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23178
23179   switch (x->dw_loc_opc)
23180     {
23181     case DW_OP_const4u:
23182     case DW_OP_const8u:
23183       if (x->dtprel)
23184         goto hash_addr;
23185       /* FALLTHRU */
23186     case DW_OP_const1u:
23187     case DW_OP_const1s:
23188     case DW_OP_const2u:
23189     case DW_OP_const2s:
23190     case DW_OP_const4s:
23191     case DW_OP_const8s:
23192     case DW_OP_constu:
23193     case DW_OP_consts:
23194     case DW_OP_pick:
23195     case DW_OP_plus_uconst:
23196     case DW_OP_breg0:
23197     case DW_OP_breg1:
23198     case DW_OP_breg2:
23199     case DW_OP_breg3:
23200     case DW_OP_breg4:
23201     case DW_OP_breg5:
23202     case DW_OP_breg6:
23203     case DW_OP_breg7:
23204     case DW_OP_breg8:
23205     case DW_OP_breg9:
23206     case DW_OP_breg10:
23207     case DW_OP_breg11:
23208     case DW_OP_breg12:
23209     case DW_OP_breg13:
23210     case DW_OP_breg14:
23211     case DW_OP_breg15:
23212     case DW_OP_breg16:
23213     case DW_OP_breg17:
23214     case DW_OP_breg18:
23215     case DW_OP_breg19:
23216     case DW_OP_breg20:
23217     case DW_OP_breg21:
23218     case DW_OP_breg22:
23219     case DW_OP_breg23:
23220     case DW_OP_breg24:
23221     case DW_OP_breg25:
23222     case DW_OP_breg26:
23223     case DW_OP_breg27:
23224     case DW_OP_breg28:
23225     case DW_OP_breg29:
23226     case DW_OP_breg30:
23227     case DW_OP_breg31:
23228     case DW_OP_regx:
23229     case DW_OP_fbreg:
23230     case DW_OP_piece:
23231     case DW_OP_deref_size:
23232     case DW_OP_xderef_size:
23233       return valx1->v.val_int == valy1->v.val_int;
23234     case DW_OP_skip:
23235     case DW_OP_bra:
23236       gcc_assert (valx1->val_class == dw_val_class_loc
23237                   && valy1->val_class == dw_val_class_loc
23238                   && x->dw_loc_addr == y->dw_loc_addr);
23239       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23240     case DW_OP_implicit_value:
23241       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23242           || valx2->val_class != valy2->val_class)
23243         return false;
23244       switch (valx2->val_class)
23245         {
23246         case dw_val_class_const:
23247           return valx2->v.val_int == valy2->v.val_int;
23248         case dw_val_class_vec:
23249           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23250                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23251                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23252                             valx2->v.val_vec.elt_size
23253                             * valx2->v.val_vec.length) == 0;
23254         case dw_val_class_const_double:
23255           return valx2->v.val_double.low == valy2->v.val_double.low
23256                  && valx2->v.val_double.high == valy2->v.val_double.high;
23257         case dw_val_class_addr:
23258           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23259         default:
23260           gcc_unreachable ();
23261         }
23262     case DW_OP_bregx:
23263     case DW_OP_bit_piece:
23264       return valx1->v.val_int == valy1->v.val_int
23265              && valx2->v.val_int == valy2->v.val_int;
23266     case DW_OP_addr:
23267     hash_addr:
23268       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23269     case DW_OP_GNU_implicit_pointer:
23270       return valx1->val_class == dw_val_class_die_ref
23271              && valx1->val_class == valy1->val_class
23272              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23273              && valx2->v.val_int == valy2->v.val_int;
23274     default:
23275       /* Other codes have no operands.  */
23276       return true;
23277     }
23278 }
23279
23280 /* Return true if DWARF location expressions X and Y are the same.  */
23281
23282 static inline bool
23283 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23284 {
23285   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23286     if (x->dw_loc_opc != y->dw_loc_opc
23287         || x->dtprel != y->dtprel
23288         || !compare_loc_operands (x, y))
23289       break;
23290   return x == NULL && y == NULL;
23291 }
23292
23293 /* Return precomputed hash of location list X.  */
23294
23295 static hashval_t
23296 loc_list_hash (const void *x)
23297 {
23298   return ((const struct dw_loc_list_struct *) x)->hash;
23299 }
23300
23301 /* Return 1 if location lists X and Y are the same.  */
23302
23303 static int
23304 loc_list_eq (const void *x, const void *y)
23305 {
23306   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23307   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23308   if (a == b)
23309     return 1;
23310   if (a->hash != b->hash)
23311     return 0;
23312   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23313     if (strcmp (a->begin, b->begin) != 0
23314         || strcmp (a->end, b->end) != 0
23315         || (a->section == NULL) != (b->section == NULL)
23316         || (a->section && strcmp (a->section, b->section) != 0)
23317         || !compare_locs (a->expr, b->expr))
23318       break;
23319   return a == NULL && b == NULL;
23320 }
23321
23322 /* Recursively optimize location lists referenced from DIE
23323    children and share them whenever possible.  */
23324
23325 static void
23326 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23327 {
23328   dw_die_ref c;
23329   dw_attr_ref a;
23330   unsigned ix;
23331   void **slot;
23332
23333   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23334     if (AT_class (a) == dw_val_class_loc_list)
23335       {
23336         dw_loc_list_ref list = AT_loc_list (a);
23337         /* TODO: perform some optimizations here, before hashing
23338            it and storing into the hash table.  */
23339         hash_loc_list (list);
23340         slot = htab_find_slot_with_hash (htab, list, list->hash,
23341                                          INSERT);
23342         if (*slot == NULL)
23343           *slot = (void *) list;
23344         else
23345           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23346       }
23347
23348   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23349 }
23350
23351 /* Optimize location lists referenced from DIE
23352    children and share them whenever possible.  */
23353
23354 static void
23355 optimize_location_lists (dw_die_ref die)
23356 {
23357   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23358   optimize_location_lists_1 (die, htab);
23359   htab_delete (htab);
23360 }
23361 \f
23362 /* Output stuff that dwarf requires at the end of every file,
23363    and generate the DWARF-2 debugging info.  */
23364
23365 static void
23366 dwarf2out_finish (const char *filename)
23367 {
23368   limbo_die_node *node, *next_node;
23369   comdat_type_node *ctnode;
23370   htab_t comdat_type_table;
23371   unsigned int i;
23372
23373   gen_scheduled_generic_parms_dies ();
23374   gen_remaining_tmpl_value_param_die_attribute ();
23375
23376   /* Add the name for the main input file now.  We delayed this from
23377      dwarf2out_init to avoid complications with PCH.  */
23378   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23379   if (!IS_ABSOLUTE_PATH (filename))
23380     add_comp_dir_attribute (comp_unit_die ());
23381   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23382     {
23383       bool p = false;
23384       htab_traverse (file_table, file_table_relative_p, &p);
23385       if (p)
23386         add_comp_dir_attribute (comp_unit_die ());
23387     }
23388
23389   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23390     {
23391       add_location_or_const_value_attribute (
23392         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23393         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23394         DW_AT_location);
23395     }
23396
23397   /* Traverse the limbo die list, and add parent/child links.  The only
23398      dies without parents that should be here are concrete instances of
23399      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23400      For concrete instances, we can get the parent die from the abstract
23401      instance.  */
23402   for (node = limbo_die_list; node; node = next_node)
23403     {
23404       dw_die_ref die = node->die;
23405       next_node = node->next;
23406
23407       if (die->die_parent == NULL)
23408         {
23409           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23410
23411           if (origin)
23412             add_child_die (origin->die_parent, die);
23413           else if (is_cu_die (die))
23414             ;
23415           else if (seen_error ())
23416             /* It's OK to be confused by errors in the input.  */
23417             add_child_die (comp_unit_die (), die);
23418           else
23419             {
23420               /* In certain situations, the lexical block containing a
23421                  nested function can be optimized away, which results
23422                  in the nested function die being orphaned.  Likewise
23423                  with the return type of that nested function.  Force
23424                  this to be a child of the containing function.
23425
23426                  It may happen that even the containing function got fully
23427                  inlined and optimized out.  In that case we are lost and
23428                  assign the empty child.  This should not be big issue as
23429                  the function is likely unreachable too.  */
23430               tree context = NULL_TREE;
23431
23432               gcc_assert (node->created_for);
23433
23434               if (DECL_P (node->created_for))
23435                 context = DECL_CONTEXT (node->created_for);
23436               else if (TYPE_P (node->created_for))
23437                 context = TYPE_CONTEXT (node->created_for);
23438
23439               gcc_assert (context
23440                           && (TREE_CODE (context) == FUNCTION_DECL
23441                               || TREE_CODE (context) == NAMESPACE_DECL));
23442
23443               origin = lookup_decl_die (context);
23444               if (origin)
23445                 add_child_die (origin, die);
23446               else
23447                 add_child_die (comp_unit_die (), die);
23448             }
23449         }
23450     }
23451
23452   limbo_die_list = NULL;
23453
23454   resolve_addr (comp_unit_die ());
23455
23456   for (node = deferred_asm_name; node; node = node->next)
23457     {
23458       tree decl = node->created_for;
23459       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23460         {
23461           add_linkage_attr (node->die, decl);
23462           move_linkage_attr (node->die);
23463         }
23464     }
23465
23466   deferred_asm_name = NULL;
23467
23468   /* Walk through the list of incomplete types again, trying once more to
23469      emit full debugging info for them.  */
23470   retry_incomplete_types ();
23471
23472   if (flag_eliminate_unused_debug_types)
23473     prune_unused_types ();
23474
23475   /* Generate separate CUs for each of the include files we've seen.
23476      They will go into limbo_die_list.  */
23477   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23478     break_out_includes (comp_unit_die ());
23479
23480   /* Generate separate COMDAT sections for type DIEs. */
23481   if (dwarf_version >= 4)
23482     {
23483       break_out_comdat_types (comp_unit_die ());
23484
23485       /* Each new type_unit DIE was added to the limbo die list when created.
23486          Since these have all been added to comdat_type_list, clear the
23487          limbo die list.  */
23488       limbo_die_list = NULL;
23489
23490       /* For each new comdat type unit, copy declarations for incomplete
23491          types to make the new unit self-contained (i.e., no direct
23492          references to the main compile unit).  */
23493       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23494         copy_decls_for_unworthy_types (ctnode->root_die);
23495       copy_decls_for_unworthy_types (comp_unit_die ());
23496
23497       /* In the process of copying declarations from one unit to another,
23498          we may have left some declarations behind that are no longer
23499          referenced.  Prune them.  */
23500       prune_unused_types ();
23501     }
23502
23503   /* Traverse the DIE's and add add sibling attributes to those DIE's
23504      that have children.  */
23505   add_sibling_attributes (comp_unit_die ());
23506   for (node = limbo_die_list; node; node = node->next)
23507     add_sibling_attributes (node->die);
23508   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23509     add_sibling_attributes (ctnode->root_die);
23510
23511   /* Output a terminator label for the .text section.  */
23512   switch_to_section (text_section);
23513   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23514   if (cold_text_section)
23515     {
23516       switch_to_section (cold_text_section);
23517       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23518     }
23519
23520   /* We can only use the low/high_pc attributes if all of the code was
23521      in .text.  */
23522   if (!have_multiple_function_sections 
23523       || (dwarf_version < 3 && dwarf_strict))
23524     {
23525       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23526       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23527     }
23528
23529   else
23530     {
23531       unsigned fde_idx = 0;
23532       bool range_list_added = false;
23533
23534       /* We need to give .debug_loc and .debug_ranges an appropriate
23535          "base address".  Use zero so that these addresses become
23536          absolute.  Historically, we've emitted the unexpected
23537          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23538          Emit both to give time for other tools to adapt.  */
23539       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23540       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23541
23542       if (text_section_used)
23543         add_ranges_by_labels (comp_unit_die (), text_section_label,
23544                               text_end_label, &range_list_added);
23545       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23546         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23547                               cold_end_label, &range_list_added);
23548
23549       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23550         {
23551           dw_fde_ref fde = &fde_table[fde_idx];
23552
23553           if (fde->dw_fde_switched_sections)
23554             {
23555               if (!fde->in_std_section)
23556                 add_ranges_by_labels (comp_unit_die (),
23557                                       fde->dw_fde_hot_section_label,
23558                                       fde->dw_fde_hot_section_end_label,
23559                                       &range_list_added);
23560               if (!fde->cold_in_std_section)
23561                 add_ranges_by_labels (comp_unit_die (),
23562                                       fde->dw_fde_unlikely_section_label,
23563                                       fde->dw_fde_unlikely_section_end_label,
23564                                       &range_list_added);
23565             }
23566           else if (!fde->in_std_section)
23567             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23568                                   fde->dw_fde_end, &range_list_added);
23569         }
23570
23571       if (range_list_added)
23572         add_ranges (NULL);
23573     }
23574
23575   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23576     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23577                     debug_line_section_label);
23578
23579   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23580     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23581
23582   if (have_location_lists)
23583     optimize_location_lists (comp_unit_die ());
23584
23585   /* Output all of the compilation units.  We put the main one last so that
23586      the offsets are available to output_pubnames.  */
23587   for (node = limbo_die_list; node; node = node->next)
23588     output_comp_unit (node->die, 0);
23589
23590   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23591   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23592     {
23593       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23594
23595       /* Don't output duplicate types.  */
23596       if (*slot != HTAB_EMPTY_ENTRY)
23597         continue;
23598
23599       /* Add a pointer to the line table for the main compilation unit
23600          so that the debugger can make sense of DW_AT_decl_file
23601          attributes.  */
23602       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23603         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23604                         debug_line_section_label);
23605
23606       output_comdat_type_unit (ctnode);
23607       *slot = ctnode;
23608     }
23609   htab_delete (comdat_type_table);
23610
23611   /* Output the main compilation unit if non-empty or if .debug_macinfo
23612      will be emitted.  */
23613   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23614
23615   /* Output the abbreviation table.  */
23616   switch_to_section (debug_abbrev_section);
23617   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23618   output_abbrev_section ();
23619
23620   /* Output location list section if necessary.  */
23621   if (have_location_lists)
23622     {
23623       /* Output the location lists info.  */
23624       switch_to_section (debug_loc_section);
23625       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23626                                    DEBUG_LOC_SECTION_LABEL, 0);
23627       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23628       output_location_lists (comp_unit_die ());
23629     }
23630
23631   /* Output public names table if necessary.  */
23632   if (!VEC_empty (pubname_entry, pubname_table))
23633     {
23634       gcc_assert (info_section_emitted);
23635       switch_to_section (debug_pubnames_section);
23636       output_pubnames (pubname_table);
23637     }
23638
23639   /* Output public types table if necessary.  */
23640   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23641      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23642      simply won't look for the section.  */
23643   if (!VEC_empty (pubname_entry, pubtype_table))
23644     {
23645       bool empty = false;
23646       
23647       if (flag_eliminate_unused_debug_types)
23648         {
23649           /* The pubtypes table might be emptied by pruning unused items.  */
23650           unsigned i;
23651           pubname_ref p;
23652           empty = true;
23653           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23654             if (p->die->die_offset != 0)
23655               {
23656                 empty = false;
23657                 break;
23658               }
23659         }
23660       if (!empty)
23661         {
23662           gcc_assert (info_section_emitted);
23663           switch_to_section (debug_pubtypes_section);
23664           output_pubnames (pubtype_table);
23665         }
23666     }
23667
23668   /* Output the address range information.  We only put functions in the arange
23669      table, so don't write it out if we don't have any.  */
23670   if ((text_section_used || cold_text_section_used || arange_table_in_use)
23671       && info_section_emitted)
23672     {
23673       switch_to_section (debug_aranges_section);
23674       output_aranges ();
23675     }
23676
23677   /* Output ranges section if necessary.  */
23678   if (ranges_table_in_use)
23679     {
23680       switch_to_section (debug_ranges_section);
23681       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23682       output_ranges ();
23683     }
23684
23685   /* Output the source line correspondence table.  We must do this
23686      even if there is no line information.  Otherwise, on an empty
23687      translation unit, we will generate a present, but empty,
23688      .debug_info section.  IRIX 6.5 `nm' will then complain when
23689      examining the file.  This is done late so that any filenames
23690      used by the debug_info section are marked as 'used'.  */
23691   switch_to_section (debug_line_section);
23692   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23693   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23694     output_line_info ();
23695
23696   /* Have to end the macro section.  */
23697   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23698     {
23699       switch_to_section (debug_macinfo_section);
23700       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23701       if (!VEC_empty (macinfo_entry, macinfo_table))
23702         output_macinfo ();
23703       dw2_asm_output_data (1, 0, "End compilation unit");
23704     }
23705
23706   /* If we emitted any DW_FORM_strp form attribute, output the string
23707      table too.  */
23708   if (debug_str_hash)
23709     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23710 }
23711
23712 #include "gt-dwarf2out.h"