OSDN Git Service

* dwarf2out.c (dwarf2out_frame_debug_expr) [rule 11]: Handle post_dec.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95 #include "cfglayout.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
126 #endif
127
128 /* Map register numbers held in the call frame info that gcc has
129    collected using DWARF_FRAME_REGNUM to those that should be output in
130    .debug_frame and .eh_frame.  */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
134
135 /* Save the result of dwarf2out_do_frame across PCH.  */
136 static GTY(()) bool saved_do_cfi_asm = 0;
137
138 /* Decide whether we want to emit frame unwind information for the current
139    translation unit.  */
140
141 int
142 dwarf2out_do_frame (void)
143 {
144   /* We want to emit correct CFA location expressions or lists, so we
145      have to return true if we're going to output debug info, even if
146      we're not going to output frame or unwind info.  */
147   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148     return true;
149
150   if (saved_do_cfi_asm)
151     return true;
152
153   if (targetm.debug_unwind_info () == UI_DWARF2)
154     return true;
155
156   if ((flag_unwind_tables || flag_exceptions)
157       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158     return true;
159
160   return false;
161 }
162
163 /* Decide whether to emit frame unwind via assembler directives.  */
164
165 int
166 dwarf2out_do_cfi_asm (void)
167 {
168   int enc;
169
170 #ifdef MIPS_DEBUGGING_INFO
171   return false;
172 #endif
173   if (saved_do_cfi_asm)
174     return true;
175   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176     return false;
177   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178     return false;
179
180   /* Make sure the personality encoding is one the assembler can support.
181      In particular, aligned addresses can't be handled.  */
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187     return false;
188
189   /* If we can't get the assembler to emit only .debug_frame, and we don't need
190      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
191   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192       && !flag_unwind_tables && !flag_exceptions
193       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194     return false;
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
234
235 /* Personality decl of current unit.  Used only when assembler does not support
236    personality CFI.  */
237 static GTY(()) rtx current_unit_personality;
238
239 /* How to start an assembler comment.  */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
243
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247
248 /* Call frames are described using a sequence of Call Frame
249    Information instructions.  The register number, offset
250    and address fields are provided as possible operands;
251    their use is selected by the opcode field.  */
252
253 enum dw_cfi_oprnd_type {
254   dw_cfi_oprnd_unused,
255   dw_cfi_oprnd_reg_num,
256   dw_cfi_oprnd_offset,
257   dw_cfi_oprnd_addr,
258   dw_cfi_oprnd_loc
259 };
260
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 }
267 dw_cfi_oprnd;
268
269 typedef struct GTY(()) dw_cfi_struct {
270   dw_cfi_ref dw_cfi_next;
271   enum dwarf_call_frame_info dw_cfi_opc;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd1;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd2;
276 }
277 dw_cfi_node;
278
279 /* This is how we define the location of the CFA. We use to handle it
280    as REG + OFFSET all the time,  but now it can be more complex.
281    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
282    Instead of passing around REG and OFFSET, we pass a copy
283    of this structure.  */
284 typedef struct cfa_loc {
285   HOST_WIDE_INT offset;
286   HOST_WIDE_INT base_offset;
287   unsigned int reg;
288   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
289   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
290 } dw_cfa_location;
291
292 /* All call frame descriptions (FDE's) in the GCC generated DWARF
293    refer to a single Common Information Entry (CIE), defined at
294    the beginning of the .debug_frame section.  This use of a single
295    CIE obviates the need to keep track of multiple CIE's
296    in the DWARF generation routines below.  */
297
298 typedef struct GTY(()) dw_fde_struct {
299   tree decl;
300   const char *dw_fde_begin;
301   const char *dw_fde_current_label;
302   const char *dw_fde_end;
303   const char *dw_fde_vms_end_prologue;
304   const char *dw_fde_vms_begin_epilogue;
305   const char *dw_fde_hot_section_label;
306   const char *dw_fde_hot_section_end_label;
307   const char *dw_fde_unlikely_section_label;
308   const char *dw_fde_unlikely_section_end_label;
309   dw_cfi_ref dw_fde_cfi;
310   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
311   HOST_WIDE_INT stack_realignment;
312   unsigned funcdef_number;
313   /* Dynamic realign argument pointer register.  */
314   unsigned int drap_reg;
315   /* Virtual dynamic realign argument pointer register.  */
316   unsigned int vdrap_reg;
317   /* These 3 flags are copied from rtl_data in function.h.  */
318   unsigned all_throwers_are_sibcalls : 1;
319   unsigned uses_eh_lsda : 1;
320   unsigned nothrow : 1;
321   /* Whether we did stack realign in this call frame.  */
322   unsigned stack_realign : 1;
323   /* Whether dynamic realign argument pointer register has been saved.  */
324   unsigned drap_reg_saved: 1;
325   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
326   unsigned in_std_section : 1;
327   /* True iff dw_fde_unlikely_section_label is in text_section or
328      cold_text_section.  */
329   unsigned cold_in_std_section : 1;
330   /* True iff switched sections.  */
331   unsigned dw_fde_switched_sections : 1;
332   /* True iff switching from cold to hot section.  */
333   unsigned dw_fde_switched_cold_to_hot : 1;
334 }
335 dw_fde_node;
336
337 /* Maximum size (in bytes) of an artificially generated label.  */
338 #define MAX_ARTIFICIAL_LABEL_BYTES      30
339
340 /* The size of addresses as they appear in the Dwarf 2 data.
341    Some architectures use word addresses to refer to code locations,
342    but Dwarf 2 info always uses byte addresses.  On such machines,
343    Dwarf 2 addresses need to be larger than the architecture's
344    pointers.  */
345 #ifndef DWARF2_ADDR_SIZE
346 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
347 #endif
348
349 /* The size in bytes of a DWARF field indicating an offset or length
350    relative to a debug info section, specified to be 4 bytes in the
351    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
352    as PTR_SIZE.  */
353
354 #ifndef DWARF_OFFSET_SIZE
355 #define DWARF_OFFSET_SIZE 4
356 #endif
357
358 /* The size in bytes of a DWARF 4 type signature.  */
359
360 #ifndef DWARF_TYPE_SIGNATURE_SIZE
361 #define DWARF_TYPE_SIGNATURE_SIZE 8
362 #endif
363
364 /* According to the (draft) DWARF 3 specification, the initial length
365    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
366    bytes are 0xffffffff, followed by the length stored in the next 8
367    bytes.
368
369    However, the SGI/MIPS ABI uses an initial length which is equal to
370    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
371
372 #ifndef DWARF_INITIAL_LENGTH_SIZE
373 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
374 #endif
375
376 /* Round SIZE up to the nearest BOUNDARY.  */
377 #define DWARF_ROUND(SIZE,BOUNDARY) \
378   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
379
380 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
381 #ifndef DWARF_CIE_DATA_ALIGNMENT
382 #ifdef STACK_GROWS_DOWNWARD
383 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
384 #else
385 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
386 #endif
387 #endif
388
389 /* CIE identifier.  */
390 #if HOST_BITS_PER_WIDE_INT >= 64
391 #define DWARF_CIE_ID \
392   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
393 #else
394 #define DWARF_CIE_ID DW_CIE_ID
395 #endif
396
397 /* A pointer to the base of a table that contains frame description
398    information for each routine.  */
399 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
400
401 /* Number of elements currently allocated for fde_table.  */
402 static GTY(()) unsigned fde_table_allocated;
403
404 /* Number of elements in fde_table currently in use.  */
405 static GTY(()) unsigned fde_table_in_use;
406
407 /* Size (in elements) of increments by which we may expand the
408    fde_table.  */
409 #define FDE_TABLE_INCREMENT 256
410
411 /* Get the current fde_table entry we should use.  */
412
413 static inline dw_fde_ref
414 current_fde (void)
415 {
416   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
417 }
418
419 /* A list of call frame insns for the CIE.  */
420 static GTY(()) dw_cfi_ref cie_cfi_head;
421
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423    attribute that accelerates the lookup of the FDE associated
424    with the subprogram.  This variable holds the table index of the FDE
425    associated with the current function (body) definition.  */
426 static unsigned current_funcdef_fde;
427
428 struct GTY(()) indirect_string_node {
429   const char *str;
430   unsigned int refcount;
431   enum dwarf_form form;
432   char *label;
433 };
434
435 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
436
437 /* True if the compilation unit has location entries that reference
438    debug strings.  */
439 static GTY(()) bool debug_str_hash_forced = false;
440
441 static GTY(()) int dw2_string_counter;
442 static GTY(()) unsigned long dwarf2out_cfi_label_num;
443
444 /* True if the compilation unit places functions in more than one section.  */
445 static GTY(()) bool have_multiple_function_sections = false;
446
447 /* Whether the default text and cold text sections have been used at all.  */
448
449 static GTY(()) bool text_section_used = false;
450 static GTY(()) bool cold_text_section_used = false;
451
452 /* The default cold text section.  */
453 static GTY(()) section *cold_text_section;
454
455 /* Forward declarations for functions defined in this file.  */
456
457 static char *stripattributes (const char *);
458 static const char *dwarf_cfi_name (unsigned);
459 static dw_cfi_ref new_cfi (void);
460 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
461 static void add_fde_cfi (const char *, dw_cfi_ref);
462 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
463 static void lookup_cfa (dw_cfa_location *);
464 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
465 static void initial_return_save (rtx);
466 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
467                                           HOST_WIDE_INT);
468 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
469 static void output_cfi_directive (dw_cfi_ref);
470 static void output_call_frame_info (int);
471 static void dwarf2out_note_section_used (void);
472 static bool clobbers_queued_reg_save (const_rtx);
473 static void dwarf2out_frame_debug_expr (rtx, const char *);
474
475 /* Support for complex CFA locations.  */
476 static void output_cfa_loc (dw_cfi_ref, int);
477 static void output_cfa_loc_raw (dw_cfi_ref);
478 static void get_cfa_from_loc_descr (dw_cfa_location *,
479                                     struct dw_loc_descr_struct *);
480 static struct dw_loc_descr_struct *build_cfa_loc
481   (dw_cfa_location *, HOST_WIDE_INT);
482 static struct dw_loc_descr_struct *build_cfa_aligned_loc
483   (HOST_WIDE_INT, HOST_WIDE_INT);
484 static void def_cfa_1 (const char *, dw_cfa_location *);
485 static struct dw_loc_descr_struct *mem_loc_descriptor
486   (rtx, enum machine_mode mode, enum var_init_status);
487
488 /* How to start an assembler comment.  */
489 #ifndef ASM_COMMENT_START
490 #define ASM_COMMENT_START ";#"
491 #endif
492
493 /* Data and reference forms for relocatable data.  */
494 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
495 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
496
497 #ifndef DEBUG_FRAME_SECTION
498 #define DEBUG_FRAME_SECTION     ".debug_frame"
499 #endif
500
501 #ifndef FUNC_BEGIN_LABEL
502 #define FUNC_BEGIN_LABEL        "LFB"
503 #endif
504
505 #ifndef FUNC_END_LABEL
506 #define FUNC_END_LABEL          "LFE"
507 #endif
508
509 #ifndef PROLOGUE_END_LABEL
510 #define PROLOGUE_END_LABEL      "LPE"
511 #endif
512
513 #ifndef EPILOGUE_BEGIN_LABEL
514 #define EPILOGUE_BEGIN_LABEL    "LEB"
515 #endif
516
517 #ifndef FRAME_BEGIN_LABEL
518 #define FRAME_BEGIN_LABEL       "Lframe"
519 #endif
520 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
521 #define CIE_END_LABEL           "LECIE"
522 #define FDE_LABEL               "LSFDE"
523 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
524 #define FDE_END_LABEL           "LEFDE"
525 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
526 #define LINE_NUMBER_END_LABEL   "LELT"
527 #define LN_PROLOG_AS_LABEL      "LASLTP"
528 #define LN_PROLOG_END_LABEL     "LELTP"
529 #define DIE_LABEL_PREFIX        "DW"
530
531 /* The DWARF 2 CFA column which tracks the return address.  Normally this
532    is the column for PC, or the first column after all of the hard
533    registers.  */
534 #ifndef DWARF_FRAME_RETURN_COLUMN
535 #ifdef PC_REGNUM
536 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
537 #else
538 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
539 #endif
540 #endif
541
542 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
543    default, we just provide columns for all registers.  */
544 #ifndef DWARF_FRAME_REGNUM
545 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
546 #endif
547 \f
548 /* Match the base name of a file to the base name of a compilation unit. */
549
550 static int
551 matches_main_base (const char *path)
552 {
553   /* Cache the last query. */
554   static const char *last_path = NULL;
555   static int last_match = 0;
556   if (path != last_path)
557     {
558       const char *base;
559       int length = base_of_path (path, &base);
560       last_path = path;
561       last_match = (length == main_input_baselength
562                     && memcmp (base, main_input_basename, length) == 0);
563     }
564   return last_match;
565 }
566
567 #ifdef DEBUG_DEBUG_STRUCT
568
569 static int
570 dump_struct_debug (tree type, enum debug_info_usage usage,
571                    enum debug_struct_file criterion, int generic,
572                    int matches, int result)
573 {
574   /* Find the type name. */
575   tree type_decl = TYPE_STUB_DECL (type);
576   tree t = type_decl;
577   const char *name = 0;
578   if (TREE_CODE (t) == TYPE_DECL)
579     t = DECL_NAME (t);
580   if (t)
581     name = IDENTIFIER_POINTER (t);
582
583   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
584            criterion,
585            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
586            matches ? "bas" : "hdr",
587            generic ? "gen" : "ord",
588            usage == DINFO_USAGE_DFN ? ";" :
589              usage == DINFO_USAGE_DIR_USE ? "." : "*",
590            result,
591            (void*) type_decl, name);
592   return result;
593 }
594 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
595   dump_struct_debug (type, usage, criterion, generic, matches, result)
596
597 #else
598
599 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
600   (result)
601
602 #endif
603
604 static bool
605 should_emit_struct_debug (tree type, enum debug_info_usage usage)
606 {
607   enum debug_struct_file criterion;
608   tree type_decl;
609   bool generic = lang_hooks.types.generic_p (type);
610
611   if (generic)
612     criterion = debug_struct_generic[usage];
613   else
614     criterion = debug_struct_ordinary[usage];
615
616   if (criterion == DINFO_STRUCT_FILE_NONE)
617     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
618   if (criterion == DINFO_STRUCT_FILE_ANY)
619     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
620
621   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
622
623   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
624     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
625
626   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
627     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
628   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
629 }
630 \f
631 /* Hook used by __throw.  */
632
633 rtx
634 expand_builtin_dwarf_sp_column (void)
635 {
636   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
637   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
638 }
639
640 /* Return a pointer to a copy of the section string name S with all
641    attributes stripped off, and an asterisk prepended (for assemble_name).  */
642
643 static inline char *
644 stripattributes (const char *s)
645 {
646   char *stripped = XNEWVEC (char, strlen (s) + 2);
647   char *p = stripped;
648
649   *p++ = '*';
650
651   while (*s && *s != ',')
652     *p++ = *s++;
653
654   *p = '\0';
655   return stripped;
656 }
657
658 /* MEM is a memory reference for the register size table, each element of
659    which has mode MODE.  Initialize column C as a return address column.  */
660
661 static void
662 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
663 {
664   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
665   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
666   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
667 }
668
669 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
670
671 static inline HOST_WIDE_INT
672 div_data_align (HOST_WIDE_INT off)
673 {
674   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
675   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
676   return r;
677 }
678
679 /* Return true if we need a signed version of a given opcode
680    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
681
682 static inline bool
683 need_data_align_sf_opcode (HOST_WIDE_INT off)
684 {
685   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
686 }
687
688 /* Generate code to initialize the register size table.  */
689
690 void
691 expand_builtin_init_dwarf_reg_sizes (tree address)
692 {
693   unsigned int i;
694   enum machine_mode mode = TYPE_MODE (char_type_node);
695   rtx addr = expand_normal (address);
696   rtx mem = gen_rtx_MEM (BLKmode, addr);
697   bool wrote_return_column = false;
698
699   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
700     {
701       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
702
703       if (rnum < DWARF_FRAME_REGISTERS)
704         {
705           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
706           enum machine_mode save_mode = reg_raw_mode[i];
707           HOST_WIDE_INT size;
708
709           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
710             save_mode = choose_hard_reg_mode (i, 1, true);
711           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
712             {
713               if (save_mode == VOIDmode)
714                 continue;
715               wrote_return_column = true;
716             }
717           size = GET_MODE_SIZE (save_mode);
718           if (offset < 0)
719             continue;
720
721           emit_move_insn (adjust_address (mem, mode, offset),
722                           gen_int_mode (size, mode));
723         }
724     }
725
726   if (!wrote_return_column)
727     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
728
729 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
730   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
731 #endif
732
733   targetm.init_dwarf_reg_sizes_extra (address);
734 }
735
736 /* Convert a DWARF call frame info. operation to its string name */
737
738 static const char *
739 dwarf_cfi_name (unsigned int cfi_opc)
740 {
741   switch (cfi_opc)
742     {
743     case DW_CFA_advance_loc:
744       return "DW_CFA_advance_loc";
745     case DW_CFA_offset:
746       return "DW_CFA_offset";
747     case DW_CFA_restore:
748       return "DW_CFA_restore";
749     case DW_CFA_nop:
750       return "DW_CFA_nop";
751     case DW_CFA_set_loc:
752       return "DW_CFA_set_loc";
753     case DW_CFA_advance_loc1:
754       return "DW_CFA_advance_loc1";
755     case DW_CFA_advance_loc2:
756       return "DW_CFA_advance_loc2";
757     case DW_CFA_advance_loc4:
758       return "DW_CFA_advance_loc4";
759     case DW_CFA_offset_extended:
760       return "DW_CFA_offset_extended";
761     case DW_CFA_restore_extended:
762       return "DW_CFA_restore_extended";
763     case DW_CFA_undefined:
764       return "DW_CFA_undefined";
765     case DW_CFA_same_value:
766       return "DW_CFA_same_value";
767     case DW_CFA_register:
768       return "DW_CFA_register";
769     case DW_CFA_remember_state:
770       return "DW_CFA_remember_state";
771     case DW_CFA_restore_state:
772       return "DW_CFA_restore_state";
773     case DW_CFA_def_cfa:
774       return "DW_CFA_def_cfa";
775     case DW_CFA_def_cfa_register:
776       return "DW_CFA_def_cfa_register";
777     case DW_CFA_def_cfa_offset:
778       return "DW_CFA_def_cfa_offset";
779
780     /* DWARF 3 */
781     case DW_CFA_def_cfa_expression:
782       return "DW_CFA_def_cfa_expression";
783     case DW_CFA_expression:
784       return "DW_CFA_expression";
785     case DW_CFA_offset_extended_sf:
786       return "DW_CFA_offset_extended_sf";
787     case DW_CFA_def_cfa_sf:
788       return "DW_CFA_def_cfa_sf";
789     case DW_CFA_def_cfa_offset_sf:
790       return "DW_CFA_def_cfa_offset_sf";
791
792     /* SGI/MIPS specific */
793     case DW_CFA_MIPS_advance_loc8:
794       return "DW_CFA_MIPS_advance_loc8";
795
796     /* GNU extensions */
797     case DW_CFA_GNU_window_save:
798       return "DW_CFA_GNU_window_save";
799     case DW_CFA_GNU_args_size:
800       return "DW_CFA_GNU_args_size";
801     case DW_CFA_GNU_negative_offset_extended:
802       return "DW_CFA_GNU_negative_offset_extended";
803
804     default:
805       return "DW_CFA_<unknown>";
806     }
807 }
808
809 /* Return a pointer to a newly allocated Call Frame Instruction.  */
810
811 static inline dw_cfi_ref
812 new_cfi (void)
813 {
814   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
815
816   cfi->dw_cfi_next = NULL;
817   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
818   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
819
820   return cfi;
821 }
822
823 /* Add a Call Frame Instruction to list of instructions.  */
824
825 static inline void
826 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
827 {
828   dw_cfi_ref *p;
829   dw_fde_ref fde = current_fde ();
830
831   /* When DRAP is used, CFA is defined with an expression.  Redefine
832      CFA may lead to a different CFA value.   */
833   /* ??? Of course, this heuristic fails when we're annotating epilogues,
834      because of course we'll always want to redefine the CFA back to the
835      stack pointer on the way out.  Where should we move this check?  */
836   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
837     switch (cfi->dw_cfi_opc)
838       {
839         case DW_CFA_def_cfa_register:
840         case DW_CFA_def_cfa_offset:
841         case DW_CFA_def_cfa_offset_sf:
842         case DW_CFA_def_cfa:
843         case DW_CFA_def_cfa_sf:
844           gcc_unreachable ();
845
846         default:
847           break;
848       }
849
850   /* Find the end of the chain.  */
851   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
852     ;
853
854   *p = cfi;
855 }
856
857 /* Generate a new label for the CFI info to refer to.  FORCE is true
858    if a label needs to be output even when using .cfi_* directives.  */
859
860 char *
861 dwarf2out_cfi_label (bool force)
862 {
863   static char label[20];
864
865   if (!force && dwarf2out_do_cfi_asm ())
866     {
867       /* In this case, we will be emitting the asm directive instead of
868          the label, so just return a placeholder to keep the rest of the
869          interfaces happy.  */
870       strcpy (label, "<do not output>");
871     }
872   else
873     {
874       int num = dwarf2out_cfi_label_num++;
875       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
876       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
877     }
878
879   return label;
880 }
881
882 /* True if remember_state should be emitted before following CFI directive.  */
883 static bool emit_cfa_remember;
884
885 /* True if any CFI directives were emitted at the current insn.  */
886 static bool any_cfis_emitted;
887
888 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
889    or to the CIE if LABEL is NULL.  */
890
891 static void
892 add_fde_cfi (const char *label, dw_cfi_ref cfi)
893 {
894   dw_cfi_ref *list_head;
895
896   if (emit_cfa_remember)
897     {
898       dw_cfi_ref cfi_remember;
899
900       /* Emit the state save.  */
901       emit_cfa_remember = false;
902       cfi_remember = new_cfi ();
903       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
904       add_fde_cfi (label, cfi_remember);
905     }
906
907   list_head = &cie_cfi_head;
908
909   if (dwarf2out_do_cfi_asm ())
910     {
911       if (label)
912         {
913           dw_fde_ref fde = current_fde ();
914
915           gcc_assert (fde != NULL);
916
917           /* We still have to add the cfi to the list so that lookup_cfa
918              works later on.  When -g2 and above we even need to force
919              emitting of CFI labels and add to list a DW_CFA_set_loc for
920              convert_cfa_to_fb_loc_list purposes.  If we're generating
921              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
922              convert_cfa_to_fb_loc_list.  */
923           if (dwarf_version == 2
924               && debug_info_level > DINFO_LEVEL_TERSE
925               && (write_symbols == DWARF2_DEBUG
926                   || write_symbols == VMS_AND_DWARF2_DEBUG))
927             {
928               switch (cfi->dw_cfi_opc)
929                 {
930                 case DW_CFA_def_cfa_offset:
931                 case DW_CFA_def_cfa_offset_sf:
932                 case DW_CFA_def_cfa_register:
933                 case DW_CFA_def_cfa:
934                 case DW_CFA_def_cfa_sf:
935                 case DW_CFA_def_cfa_expression:
936                 case DW_CFA_restore_state:
937                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
938                     label = dwarf2out_cfi_label (true);
939
940                   if (fde->dw_fde_current_label == NULL
941                       || strcmp (label, fde->dw_fde_current_label) != 0)
942                     {
943                       dw_cfi_ref xcfi;
944
945                       label = xstrdup (label);
946
947                       /* Set the location counter to the new label.  */
948                       xcfi = new_cfi ();
949                       /* It doesn't metter whether DW_CFA_set_loc
950                          or DW_CFA_advance_loc4 is added here, those aren't
951                          emitted into assembly, only looked up by
952                          convert_cfa_to_fb_loc_list.  */
953                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
954                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
955                       add_cfi (&fde->dw_fde_cfi, xcfi);
956                       fde->dw_fde_current_label = label;
957                     }
958                   break;
959                 default:
960                   break;
961                 }
962             }
963
964           output_cfi_directive (cfi);
965
966           list_head = &fde->dw_fde_cfi;
967           any_cfis_emitted = true;
968         }
969       /* ??? If this is a CFI for the CIE, we don't emit.  This
970          assumes that the standard CIE contents that the assembler
971          uses matches the standard CIE contents that the compiler
972          uses.  This is probably a bad assumption.  I'm not quite
973          sure how to address this for now.  */
974     }
975   else if (label)
976     {
977       dw_fde_ref fde = current_fde ();
978
979       gcc_assert (fde != NULL);
980
981       if (*label == 0)
982         label = dwarf2out_cfi_label (false);
983
984       if (fde->dw_fde_current_label == NULL
985           || strcmp (label, fde->dw_fde_current_label) != 0)
986         {
987           dw_cfi_ref xcfi;
988
989           label = xstrdup (label);
990
991           /* Set the location counter to the new label.  */
992           xcfi = new_cfi ();
993           /* If we have a current label, advance from there, otherwise
994              set the location directly using set_loc.  */
995           xcfi->dw_cfi_opc = fde->dw_fde_current_label
996                              ? DW_CFA_advance_loc4
997                              : DW_CFA_set_loc;
998           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
999           add_cfi (&fde->dw_fde_cfi, xcfi);
1000
1001           fde->dw_fde_current_label = label;
1002         }
1003
1004       list_head = &fde->dw_fde_cfi;
1005       any_cfis_emitted = true;
1006     }
1007
1008   add_cfi (list_head, cfi);
1009 }
1010
1011 /* Subroutine of lookup_cfa.  */
1012
1013 static void
1014 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1015 {
1016   switch (cfi->dw_cfi_opc)
1017     {
1018     case DW_CFA_def_cfa_offset:
1019     case DW_CFA_def_cfa_offset_sf:
1020       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1021       break;
1022     case DW_CFA_def_cfa_register:
1023       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1024       break;
1025     case DW_CFA_def_cfa:
1026     case DW_CFA_def_cfa_sf:
1027       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1028       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1029       break;
1030     case DW_CFA_def_cfa_expression:
1031       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1032       break;
1033
1034     case DW_CFA_remember_state:
1035       gcc_assert (!remember->in_use);
1036       *remember = *loc;
1037       remember->in_use = 1;
1038       break;
1039     case DW_CFA_restore_state:
1040       gcc_assert (remember->in_use);
1041       *loc = *remember;
1042       remember->in_use = 0;
1043       break;
1044
1045     default:
1046       break;
1047     }
1048 }
1049
1050 /* Find the previous value for the CFA.  */
1051
1052 static void
1053 lookup_cfa (dw_cfa_location *loc)
1054 {
1055   dw_cfi_ref cfi;
1056   dw_fde_ref fde;
1057   dw_cfa_location remember;
1058
1059   memset (loc, 0, sizeof (*loc));
1060   loc->reg = INVALID_REGNUM;
1061   remember = *loc;
1062
1063   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1064     lookup_cfa_1 (cfi, loc, &remember);
1065
1066   fde = current_fde ();
1067   if (fde)
1068     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1069       lookup_cfa_1 (cfi, loc, &remember);
1070 }
1071
1072 /* The current rule for calculating the DWARF2 canonical frame address.  */
1073 static dw_cfa_location cfa;
1074
1075 /* The register used for saving registers to the stack, and its offset
1076    from the CFA.  */
1077 static dw_cfa_location cfa_store;
1078
1079 /* The current save location around an epilogue.  */
1080 static dw_cfa_location cfa_remember;
1081
1082 /* The running total of the size of arguments pushed onto the stack.  */
1083 static HOST_WIDE_INT args_size;
1084
1085 /* The last args_size we actually output.  */
1086 static HOST_WIDE_INT old_args_size;
1087
1088 /* Entry point to update the canonical frame address (CFA).
1089    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1090    calculated from REG+OFFSET.  */
1091
1092 void
1093 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1094 {
1095   dw_cfa_location loc;
1096   loc.indirect = 0;
1097   loc.base_offset = 0;
1098   loc.reg = reg;
1099   loc.offset = offset;
1100   def_cfa_1 (label, &loc);
1101 }
1102
1103 /* Determine if two dw_cfa_location structures define the same data.  */
1104
1105 static bool
1106 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1107 {
1108   return (loc1->reg == loc2->reg
1109           && loc1->offset == loc2->offset
1110           && loc1->indirect == loc2->indirect
1111           && (loc1->indirect == 0
1112               || loc1->base_offset == loc2->base_offset));
1113 }
1114
1115 /* This routine does the actual work.  The CFA is now calculated from
1116    the dw_cfa_location structure.  */
1117
1118 static void
1119 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1120 {
1121   dw_cfi_ref cfi;
1122   dw_cfa_location old_cfa, loc;
1123
1124   cfa = *loc_p;
1125   loc = *loc_p;
1126
1127   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1128     cfa_store.offset = loc.offset;
1129
1130   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1131   lookup_cfa (&old_cfa);
1132
1133   /* If nothing changed, no need to issue any call frame instructions.  */
1134   if (cfa_equal_p (&loc, &old_cfa))
1135     return;
1136
1137   cfi = new_cfi ();
1138
1139   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1140     {
1141       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1142          the CFA register did not change but the offset did.  The data
1143          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1144          in the assembler via the .cfi_def_cfa_offset directive.  */
1145       if (loc.offset < 0)
1146         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1147       else
1148         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1149       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1150     }
1151
1152 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1153   else if (loc.offset == old_cfa.offset
1154            && old_cfa.reg != INVALID_REGNUM
1155            && !loc.indirect
1156            && !old_cfa.indirect)
1157     {
1158       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1159          indicating the CFA register has changed to <register> but the
1160          offset has not changed.  */
1161       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1162       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1163     }
1164 #endif
1165
1166   else if (loc.indirect == 0)
1167     {
1168       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1169          indicating the CFA register has changed to <register> with
1170          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1171          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1172          directive.  */
1173       if (loc.offset < 0)
1174         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1175       else
1176         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1177       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1178       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1179     }
1180   else
1181     {
1182       /* Construct a DW_CFA_def_cfa_expression instruction to
1183          calculate the CFA using a full location expression since no
1184          register-offset pair is available.  */
1185       struct dw_loc_descr_struct *loc_list;
1186
1187       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1188       loc_list = build_cfa_loc (&loc, 0);
1189       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1190     }
1191
1192   add_fde_cfi (label, cfi);
1193 }
1194
1195 /* Add the CFI for saving a register.  REG is the CFA column number.
1196    LABEL is passed to add_fde_cfi.
1197    If SREG is -1, the register is saved at OFFSET from the CFA;
1198    otherwise it is saved in SREG.  */
1199
1200 static void
1201 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1202 {
1203   dw_cfi_ref cfi = new_cfi ();
1204   dw_fde_ref fde = current_fde ();
1205
1206   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1207
1208   /* When stack is aligned, store REG using DW_CFA_expression with
1209      FP.  */
1210   if (fde
1211       && fde->stack_realign
1212       && sreg == INVALID_REGNUM)
1213     {
1214       cfi->dw_cfi_opc = DW_CFA_expression;
1215       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1216       cfi->dw_cfi_oprnd2.dw_cfi_loc
1217         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1218     }
1219   else if (sreg == INVALID_REGNUM)
1220     {
1221       if (need_data_align_sf_opcode (offset))
1222         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1223       else if (reg & ~0x3f)
1224         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1225       else
1226         cfi->dw_cfi_opc = DW_CFA_offset;
1227       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1228     }
1229   else if (sreg == reg)
1230     cfi->dw_cfi_opc = DW_CFA_same_value;
1231   else
1232     {
1233       cfi->dw_cfi_opc = DW_CFA_register;
1234       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1235     }
1236
1237   add_fde_cfi (label, cfi);
1238 }
1239
1240 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1241    This CFI tells the unwinder that it needs to restore the window registers
1242    from the previous frame's window save area.
1243
1244    ??? Perhaps we should note in the CIE where windows are saved (instead of
1245    assuming 0(cfa)) and what registers are in the window.  */
1246
1247 void
1248 dwarf2out_window_save (const char *label)
1249 {
1250   dw_cfi_ref cfi = new_cfi ();
1251
1252   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1253   add_fde_cfi (label, cfi);
1254 }
1255
1256 /* Entry point for saving a register to the stack.  REG is the GCC register
1257    number.  LABEL and OFFSET are passed to reg_save.  */
1258
1259 void
1260 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1261 {
1262   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1263 }
1264
1265 /* Entry point for saving the return address in the stack.
1266    LABEL and OFFSET are passed to reg_save.  */
1267
1268 void
1269 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1270 {
1271   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1272 }
1273
1274 /* Entry point for saving the return address in a register.
1275    LABEL and SREG are passed to reg_save.  */
1276
1277 void
1278 dwarf2out_return_reg (const char *label, unsigned int sreg)
1279 {
1280   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1281 }
1282
1283 /* Record the initial position of the return address.  RTL is
1284    INCOMING_RETURN_ADDR_RTX.  */
1285
1286 static void
1287 initial_return_save (rtx rtl)
1288 {
1289   unsigned int reg = INVALID_REGNUM;
1290   HOST_WIDE_INT offset = 0;
1291
1292   switch (GET_CODE (rtl))
1293     {
1294     case REG:
1295       /* RA is in a register.  */
1296       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1297       break;
1298
1299     case MEM:
1300       /* RA is on the stack.  */
1301       rtl = XEXP (rtl, 0);
1302       switch (GET_CODE (rtl))
1303         {
1304         case REG:
1305           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1306           offset = 0;
1307           break;
1308
1309         case PLUS:
1310           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1311           offset = INTVAL (XEXP (rtl, 1));
1312           break;
1313
1314         case MINUS:
1315           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1316           offset = -INTVAL (XEXP (rtl, 1));
1317           break;
1318
1319         default:
1320           gcc_unreachable ();
1321         }
1322
1323       break;
1324
1325     case PLUS:
1326       /* The return address is at some offset from any value we can
1327          actually load.  For instance, on the SPARC it is in %i7+8. Just
1328          ignore the offset for now; it doesn't matter for unwinding frames.  */
1329       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1330       initial_return_save (XEXP (rtl, 0));
1331       return;
1332
1333     default:
1334       gcc_unreachable ();
1335     }
1336
1337   if (reg != DWARF_FRAME_RETURN_COLUMN)
1338     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1339 }
1340
1341 /* Given a SET, calculate the amount of stack adjustment it
1342    contains.  */
1343
1344 static HOST_WIDE_INT
1345 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1346                      HOST_WIDE_INT cur_offset)
1347 {
1348   const_rtx src = SET_SRC (pattern);
1349   const_rtx dest = SET_DEST (pattern);
1350   HOST_WIDE_INT offset = 0;
1351   enum rtx_code code;
1352
1353   if (dest == stack_pointer_rtx)
1354     {
1355       code = GET_CODE (src);
1356
1357       /* Assume (set (reg sp) (reg whatever)) sets args_size
1358          level to 0.  */
1359       if (code == REG && src != stack_pointer_rtx)
1360         {
1361           offset = -cur_args_size;
1362 #ifndef STACK_GROWS_DOWNWARD
1363           offset = -offset;
1364 #endif
1365           return offset - cur_offset;
1366         }
1367
1368       if (! (code == PLUS || code == MINUS)
1369           || XEXP (src, 0) != stack_pointer_rtx
1370           || !CONST_INT_P (XEXP (src, 1)))
1371         return 0;
1372
1373       /* (set (reg sp) (plus (reg sp) (const_int))) */
1374       offset = INTVAL (XEXP (src, 1));
1375       if (code == PLUS)
1376         offset = -offset;
1377       return offset;
1378     }
1379
1380   if (MEM_P (src) && !MEM_P (dest))
1381     dest = src;
1382   if (MEM_P (dest))
1383     {
1384       /* (set (mem (pre_dec (reg sp))) (foo)) */
1385       src = XEXP (dest, 0);
1386       code = GET_CODE (src);
1387
1388       switch (code)
1389         {
1390         case PRE_MODIFY:
1391         case POST_MODIFY:
1392           if (XEXP (src, 0) == stack_pointer_rtx)
1393             {
1394               rtx val = XEXP (XEXP (src, 1), 1);
1395               /* We handle only adjustments by constant amount.  */
1396               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1397                           && CONST_INT_P (val));
1398               offset = -INTVAL (val);
1399               break;
1400             }
1401           return 0;
1402
1403         case PRE_DEC:
1404         case POST_DEC:
1405           if (XEXP (src, 0) == stack_pointer_rtx)
1406             {
1407               offset = GET_MODE_SIZE (GET_MODE (dest));
1408               break;
1409             }
1410           return 0;
1411
1412         case PRE_INC:
1413         case POST_INC:
1414           if (XEXP (src, 0) == stack_pointer_rtx)
1415             {
1416               offset = -GET_MODE_SIZE (GET_MODE (dest));
1417               break;
1418             }
1419           return 0;
1420
1421         default:
1422           return 0;
1423         }
1424     }
1425   else
1426     return 0;
1427
1428   return offset;
1429 }
1430
1431 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1432    indexed by INSN_UID.  */
1433
1434 static HOST_WIDE_INT *barrier_args_size;
1435
1436 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1437
1438 static HOST_WIDE_INT
1439 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1440                              VEC (rtx, heap) **next)
1441 {
1442   HOST_WIDE_INT offset = 0;
1443   int i;
1444
1445   if (! RTX_FRAME_RELATED_P (insn))
1446     {
1447       if (prologue_epilogue_contains (insn))
1448         /* Nothing */;
1449       else if (GET_CODE (PATTERN (insn)) == SET)
1450         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1451       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1452                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1453         {
1454           /* There may be stack adjustments inside compound insns.  Search
1455              for them.  */
1456           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1457             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1458               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1459                                              cur_args_size, offset);
1460         }
1461     }
1462   else
1463     {
1464       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1465
1466       if (expr)
1467         {
1468           expr = XEXP (expr, 0);
1469           if (GET_CODE (expr) == PARALLEL
1470               || GET_CODE (expr) == SEQUENCE)
1471             for (i = 1; i < XVECLEN (expr, 0); i++)
1472               {
1473                 rtx elem = XVECEXP (expr, 0, i);
1474
1475                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1476                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1477               }
1478         }
1479     }
1480
1481 #ifndef STACK_GROWS_DOWNWARD
1482   offset = -offset;
1483 #endif
1484
1485   cur_args_size += offset;
1486   if (cur_args_size < 0)
1487     cur_args_size = 0;
1488
1489   if (JUMP_P (insn))
1490     {
1491       rtx dest = JUMP_LABEL (insn);
1492
1493       if (dest)
1494         {
1495           if (barrier_args_size [INSN_UID (dest)] < 0)
1496             {
1497               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1498               VEC_safe_push (rtx, heap, *next, dest);
1499             }
1500         }
1501     }
1502
1503   return cur_args_size;
1504 }
1505
1506 /* Walk the whole function and compute args_size on BARRIERs.  */
1507
1508 static void
1509 compute_barrier_args_size (void)
1510 {
1511   int max_uid = get_max_uid (), i;
1512   rtx insn;
1513   VEC (rtx, heap) *worklist, *next, *tmp;
1514
1515   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1516   for (i = 0; i < max_uid; i++)
1517     barrier_args_size[i] = -1;
1518
1519   worklist = VEC_alloc (rtx, heap, 20);
1520   next = VEC_alloc (rtx, heap, 20);
1521   insn = get_insns ();
1522   barrier_args_size[INSN_UID (insn)] = 0;
1523   VEC_quick_push (rtx, worklist, insn);
1524   for (;;)
1525     {
1526       while (!VEC_empty (rtx, worklist))
1527         {
1528           rtx prev, body, first_insn;
1529           HOST_WIDE_INT cur_args_size;
1530
1531           first_insn = insn = VEC_pop (rtx, worklist);
1532           cur_args_size = barrier_args_size[INSN_UID (insn)];
1533           prev = prev_nonnote_insn (insn);
1534           if (prev && BARRIER_P (prev))
1535             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1536
1537           for (; insn; insn = NEXT_INSN (insn))
1538             {
1539               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1540                 continue;
1541               if (BARRIER_P (insn))
1542                 break;
1543
1544               if (LABEL_P (insn))
1545                 {
1546                   if (insn == first_insn)
1547                     continue;
1548                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1549                     {
1550                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1551                       continue;
1552                     }
1553                   else
1554                     {
1555                       /* The insns starting with this label have been
1556                          already scanned or are in the worklist.  */
1557                       break;
1558                     }
1559                 }
1560
1561               body = PATTERN (insn);
1562               if (GET_CODE (body) == SEQUENCE)
1563                 {
1564                   HOST_WIDE_INT dest_args_size = cur_args_size;
1565                   for (i = 1; i < XVECLEN (body, 0); i++)
1566                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1567                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1568                       dest_args_size
1569                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1570                                                        dest_args_size, &next);
1571                     else
1572                       cur_args_size
1573                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1574                                                        cur_args_size, &next);
1575
1576                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1577                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1578                                                  dest_args_size, &next);
1579                   else
1580                     cur_args_size
1581                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1582                                                      cur_args_size, &next);
1583                 }
1584               else
1585                 cur_args_size
1586                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1587             }
1588         }
1589
1590       if (VEC_empty (rtx, next))
1591         break;
1592
1593       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1594       tmp = next;
1595       next = worklist;
1596       worklist = tmp;
1597       VEC_truncate (rtx, next, 0);
1598     }
1599
1600   VEC_free (rtx, heap, worklist);
1601   VEC_free (rtx, heap, next);
1602 }
1603
1604 /* Add a CFI to update the running total of the size of arguments
1605    pushed onto the stack.  */
1606
1607 static void
1608 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1609 {
1610   dw_cfi_ref cfi;
1611
1612   if (size == old_args_size)
1613     return;
1614
1615   old_args_size = size;
1616
1617   cfi = new_cfi ();
1618   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1619   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1620   add_fde_cfi (label, cfi);
1621 }
1622
1623 /* Record a stack adjustment of OFFSET bytes.  */
1624
1625 static void
1626 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1627 {
1628   if (cfa.reg == STACK_POINTER_REGNUM)
1629     cfa.offset += offset;
1630
1631   if (cfa_store.reg == STACK_POINTER_REGNUM)
1632     cfa_store.offset += offset;
1633
1634   if (ACCUMULATE_OUTGOING_ARGS)
1635     return;
1636
1637 #ifndef STACK_GROWS_DOWNWARD
1638   offset = -offset;
1639 #endif
1640
1641   args_size += offset;
1642   if (args_size < 0)
1643     args_size = 0;
1644
1645   def_cfa_1 (label, &cfa);
1646   if (flag_asynchronous_unwind_tables)
1647     dwarf2out_args_size (label, args_size);
1648 }
1649
1650 /* Check INSN to see if it looks like a push or a stack adjustment, and
1651    make a note of it if it does.  EH uses this information to find out
1652    how much extra space it needs to pop off the stack.  */
1653
1654 static void
1655 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1656 {
1657   HOST_WIDE_INT offset;
1658   const char *label;
1659   int i;
1660
1661   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1662      with this function.  Proper support would require all frame-related
1663      insns to be marked, and to be able to handle saving state around
1664      epilogues textually in the middle of the function.  */
1665   if (prologue_epilogue_contains (insn))
1666     return;
1667
1668   /* If INSN is an instruction from target of an annulled branch, the
1669      effects are for the target only and so current argument size
1670      shouldn't change at all.  */
1671   if (final_sequence
1672       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1673       && INSN_FROM_TARGET_P (insn))
1674     return;
1675
1676   /* If only calls can throw, and we have a frame pointer,
1677      save up adjustments until we see the CALL_INSN.  */
1678   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1679     {
1680       if (CALL_P (insn) && !after_p)
1681         {
1682           /* Extract the size of the args from the CALL rtx itself.  */
1683           insn = PATTERN (insn);
1684           if (GET_CODE (insn) == PARALLEL)
1685             insn = XVECEXP (insn, 0, 0);
1686           if (GET_CODE (insn) == SET)
1687             insn = SET_SRC (insn);
1688           gcc_assert (GET_CODE (insn) == CALL);
1689           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1690         }
1691       return;
1692     }
1693
1694   if (CALL_P (insn) && !after_p)
1695     {
1696       if (!flag_asynchronous_unwind_tables)
1697         dwarf2out_args_size ("", args_size);
1698       return;
1699     }
1700   else if (BARRIER_P (insn))
1701     {
1702       /* Don't call compute_barrier_args_size () if the only
1703          BARRIER is at the end of function.  */
1704       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1705         compute_barrier_args_size ();
1706       if (barrier_args_size == NULL)
1707         offset = 0;
1708       else
1709         {
1710           offset = barrier_args_size[INSN_UID (insn)];
1711           if (offset < 0)
1712             offset = 0;
1713         }
1714
1715       offset -= args_size;
1716 #ifndef STACK_GROWS_DOWNWARD
1717       offset = -offset;
1718 #endif
1719     }
1720   else if (GET_CODE (PATTERN (insn)) == SET)
1721     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1722   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1723            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1724     {
1725       /* There may be stack adjustments inside compound insns.  Search
1726          for them.  */
1727       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1728         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1729           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1730                                          args_size, offset);
1731     }
1732   else
1733     return;
1734
1735   if (offset == 0)
1736     return;
1737
1738   label = dwarf2out_cfi_label (false);
1739   dwarf2out_stack_adjust (offset, label);
1740 }
1741
1742 /* We delay emitting a register save until either (a) we reach the end
1743    of the prologue or (b) the register is clobbered.  This clusters
1744    register saves so that there are fewer pc advances.  */
1745
1746 struct GTY(()) queued_reg_save {
1747   struct queued_reg_save *next;
1748   rtx reg;
1749   HOST_WIDE_INT cfa_offset;
1750   rtx saved_reg;
1751 };
1752
1753 static GTY(()) struct queued_reg_save *queued_reg_saves;
1754
1755 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1756 struct GTY(()) reg_saved_in_data {
1757   rtx orig_reg;
1758   rtx saved_in_reg;
1759 };
1760
1761 /* A list of registers saved in other registers.
1762    The list intentionally has a small maximum capacity of 4; if your
1763    port needs more than that, you might consider implementing a
1764    more efficient data structure.  */
1765 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1766 static GTY(()) size_t num_regs_saved_in_regs;
1767
1768 static const char *last_reg_save_label;
1769
1770 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1771    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1772
1773 static void
1774 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1775 {
1776   struct queued_reg_save *q;
1777
1778   /* Duplicates waste space, but it's also necessary to remove them
1779      for correctness, since the queue gets output in reverse
1780      order.  */
1781   for (q = queued_reg_saves; q != NULL; q = q->next)
1782     if (REGNO (q->reg) == REGNO (reg))
1783       break;
1784
1785   if (q == NULL)
1786     {
1787       q = ggc_alloc_queued_reg_save ();
1788       q->next = queued_reg_saves;
1789       queued_reg_saves = q;
1790     }
1791
1792   q->reg = reg;
1793   q->cfa_offset = offset;
1794   q->saved_reg = sreg;
1795
1796   last_reg_save_label = label;
1797 }
1798
1799 /* Output all the entries in QUEUED_REG_SAVES.  */
1800
1801 void
1802 dwarf2out_flush_queued_reg_saves (void)
1803 {
1804   struct queued_reg_save *q;
1805
1806   for (q = queued_reg_saves; q; q = q->next)
1807     {
1808       size_t i;
1809       unsigned int reg, sreg;
1810
1811       for (i = 0; i < num_regs_saved_in_regs; i++)
1812         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1813           break;
1814       if (q->saved_reg && i == num_regs_saved_in_regs)
1815         {
1816           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1817           num_regs_saved_in_regs++;
1818         }
1819       if (i != num_regs_saved_in_regs)
1820         {
1821           regs_saved_in_regs[i].orig_reg = q->reg;
1822           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1823         }
1824
1825       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1826       if (q->saved_reg)
1827         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1828       else
1829         sreg = INVALID_REGNUM;
1830       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1831     }
1832
1833   queued_reg_saves = NULL;
1834   last_reg_save_label = NULL;
1835 }
1836
1837 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1838    location for?  Or, does it clobber a register which we've previously
1839    said that some other register is saved in, and for which we now
1840    have a new location for?  */
1841
1842 static bool
1843 clobbers_queued_reg_save (const_rtx insn)
1844 {
1845   struct queued_reg_save *q;
1846
1847   for (q = queued_reg_saves; q; q = q->next)
1848     {
1849       size_t i;
1850       if (modified_in_p (q->reg, insn))
1851         return true;
1852       for (i = 0; i < num_regs_saved_in_regs; i++)
1853         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1854             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1855           return true;
1856     }
1857
1858   return false;
1859 }
1860
1861 /* Entry point for saving the first register into the second.  */
1862
1863 void
1864 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1865 {
1866   size_t i;
1867   unsigned int regno, sregno;
1868
1869   for (i = 0; i < num_regs_saved_in_regs; i++)
1870     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1871       break;
1872   if (i == num_regs_saved_in_regs)
1873     {
1874       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1875       num_regs_saved_in_regs++;
1876     }
1877   regs_saved_in_regs[i].orig_reg = reg;
1878   regs_saved_in_regs[i].saved_in_reg = sreg;
1879
1880   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1881   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1882   reg_save (label, regno, sregno, 0);
1883 }
1884
1885 /* What register, if any, is currently saved in REG?  */
1886
1887 static rtx
1888 reg_saved_in (rtx reg)
1889 {
1890   unsigned int regn = REGNO (reg);
1891   size_t i;
1892   struct queued_reg_save *q;
1893
1894   for (q = queued_reg_saves; q; q = q->next)
1895     if (q->saved_reg && regn == REGNO (q->saved_reg))
1896       return q->reg;
1897
1898   for (i = 0; i < num_regs_saved_in_regs; i++)
1899     if (regs_saved_in_regs[i].saved_in_reg
1900         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1901       return regs_saved_in_regs[i].orig_reg;
1902
1903   return NULL_RTX;
1904 }
1905
1906
1907 /* A temporary register holding an integral value used in adjusting SP
1908    or setting up the store_reg.  The "offset" field holds the integer
1909    value, not an offset.  */
1910 static dw_cfa_location cfa_temp;
1911
1912 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1913
1914 static void
1915 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1916 {
1917   memset (&cfa, 0, sizeof (cfa));
1918
1919   switch (GET_CODE (pat))
1920     {
1921     case PLUS:
1922       cfa.reg = REGNO (XEXP (pat, 0));
1923       cfa.offset = INTVAL (XEXP (pat, 1));
1924       break;
1925
1926     case REG:
1927       cfa.reg = REGNO (pat);
1928       break;
1929
1930     case MEM:
1931       cfa.indirect = 1;
1932       pat = XEXP (pat, 0);
1933       if (GET_CODE (pat) == PLUS)
1934         {
1935           cfa.base_offset = INTVAL (XEXP (pat, 1));
1936           pat = XEXP (pat, 0);
1937         }
1938       cfa.reg = REGNO (pat);
1939       break;
1940
1941     default:
1942       /* Recurse and define an expression.  */
1943       gcc_unreachable ();
1944     }
1945
1946   def_cfa_1 (label, &cfa);
1947 }
1948
1949 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1950
1951 static void
1952 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1953 {
1954   rtx src, dest;
1955
1956   gcc_assert (GET_CODE (pat) == SET);
1957   dest = XEXP (pat, 0);
1958   src = XEXP (pat, 1);
1959
1960   switch (GET_CODE (src))
1961     {
1962     case PLUS:
1963       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1964       cfa.offset -= INTVAL (XEXP (src, 1));
1965       break;
1966
1967     case REG:
1968         break;
1969
1970     default:
1971         gcc_unreachable ();
1972     }
1973
1974   cfa.reg = REGNO (dest);
1975   gcc_assert (cfa.indirect == 0);
1976
1977   def_cfa_1 (label, &cfa);
1978 }
1979
1980 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1981
1982 static void
1983 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1984 {
1985   HOST_WIDE_INT offset;
1986   rtx src, addr, span;
1987
1988   src = XEXP (set, 1);
1989   addr = XEXP (set, 0);
1990   gcc_assert (MEM_P (addr));
1991   addr = XEXP (addr, 0);
1992
1993   /* As documented, only consider extremely simple addresses.  */
1994   switch (GET_CODE (addr))
1995     {
1996     case REG:
1997       gcc_assert (REGNO (addr) == cfa.reg);
1998       offset = -cfa.offset;
1999       break;
2000     case PLUS:
2001       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2002       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2003       break;
2004     default:
2005       gcc_unreachable ();
2006     }
2007
2008   span = targetm.dwarf_register_span (src);
2009
2010   /* ??? We'd like to use queue_reg_save, but we need to come up with
2011      a different flushing heuristic for epilogues.  */
2012   if (!span)
2013     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2014   else
2015     {
2016       /* We have a PARALLEL describing where the contents of SRC live.
2017          Queue register saves for each piece of the PARALLEL.  */
2018       int par_index;
2019       int limit;
2020       HOST_WIDE_INT span_offset = offset;
2021
2022       gcc_assert (GET_CODE (span) == PARALLEL);
2023
2024       limit = XVECLEN (span, 0);
2025       for (par_index = 0; par_index < limit; par_index++)
2026         {
2027           rtx elem = XVECEXP (span, 0, par_index);
2028
2029           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2030                     INVALID_REGNUM, span_offset);
2031           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2032         }
2033     }
2034 }
2035
2036 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2037
2038 static void
2039 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2040 {
2041   rtx src, dest;
2042   unsigned sregno, dregno;
2043
2044   src = XEXP (set, 1);
2045   dest = XEXP (set, 0);
2046
2047   if (src == pc_rtx)
2048     sregno = DWARF_FRAME_RETURN_COLUMN;
2049   else
2050     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2051
2052   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2053
2054   /* ??? We'd like to use queue_reg_save, but we need to come up with
2055      a different flushing heuristic for epilogues.  */
2056   reg_save (label, sregno, dregno, 0);
2057 }
2058
2059 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2060
2061 static void
2062 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2063 {
2064   rtx src, dest, span;
2065   dw_cfi_ref cfi = new_cfi ();
2066
2067   dest = SET_DEST (set);
2068   src = SET_SRC (set);
2069
2070   gcc_assert (REG_P (src));
2071   gcc_assert (MEM_P (dest));
2072
2073   span = targetm.dwarf_register_span (src);
2074   gcc_assert (!span);
2075
2076   cfi->dw_cfi_opc = DW_CFA_expression;
2077   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2078   cfi->dw_cfi_oprnd2.dw_cfi_loc
2079     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2080                           VAR_INIT_STATUS_INITIALIZED);
2081
2082   /* ??? We'd like to use queue_reg_save, were the interface different,
2083      and, as above, we could manage flushing for epilogues.  */
2084   add_fde_cfi (label, cfi);
2085 }
2086
2087 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2088
2089 static void
2090 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2091 {
2092   dw_cfi_ref cfi = new_cfi ();
2093   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2094
2095   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2096   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2097
2098   add_fde_cfi (label, cfi);
2099 }
2100
2101 /* Record call frame debugging information for an expression EXPR,
2102    which either sets SP or FP (adjusting how we calculate the frame
2103    address) or saves a register to the stack or another register.
2104    LABEL indicates the address of EXPR.
2105
2106    This function encodes a state machine mapping rtxes to actions on
2107    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2108    users need not read the source code.
2109
2110   The High-Level Picture
2111
2112   Changes in the register we use to calculate the CFA: Currently we
2113   assume that if you copy the CFA register into another register, we
2114   should take the other one as the new CFA register; this seems to
2115   work pretty well.  If it's wrong for some target, it's simple
2116   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2117
2118   Changes in the register we use for saving registers to the stack:
2119   This is usually SP, but not always.  Again, we deduce that if you
2120   copy SP into another register (and SP is not the CFA register),
2121   then the new register is the one we will be using for register
2122   saves.  This also seems to work.
2123
2124   Register saves: There's not much guesswork about this one; if
2125   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2126   register save, and the register used to calculate the destination
2127   had better be the one we think we're using for this purpose.
2128   It's also assumed that a copy from a call-saved register to another
2129   register is saving that register if RTX_FRAME_RELATED_P is set on
2130   that instruction.  If the copy is from a call-saved register to
2131   the *same* register, that means that the register is now the same
2132   value as in the caller.
2133
2134   Except: If the register being saved is the CFA register, and the
2135   offset is nonzero, we are saving the CFA, so we assume we have to
2136   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2137   the intent is to save the value of SP from the previous frame.
2138
2139   In addition, if a register has previously been saved to a different
2140   register,
2141
2142   Invariants / Summaries of Rules
2143
2144   cfa          current rule for calculating the CFA.  It usually
2145                consists of a register and an offset.
2146   cfa_store    register used by prologue code to save things to the stack
2147                cfa_store.offset is the offset from the value of
2148                cfa_store.reg to the actual CFA
2149   cfa_temp     register holding an integral value.  cfa_temp.offset
2150                stores the value, which will be used to adjust the
2151                stack pointer.  cfa_temp is also used like cfa_store,
2152                to track stores to the stack via fp or a temp reg.
2153
2154   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2155                with cfa.reg as the first operand changes the cfa.reg and its
2156                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2157                cfa_temp.offset.
2158
2159   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2160                expression yielding a constant.  This sets cfa_temp.reg
2161                and cfa_temp.offset.
2162
2163   Rule 5:      Create a new register cfa_store used to save items to the
2164                stack.
2165
2166   Rules 10-14: Save a register to the stack.  Define offset as the
2167                difference of the original location and cfa_store's
2168                location (or cfa_temp's location if cfa_temp is used).
2169
2170   Rules 16-20: If AND operation happens on sp in prologue, we assume
2171                stack is realigned.  We will use a group of DW_OP_XXX
2172                expressions to represent the location of the stored
2173                register instead of CFA+offset.
2174
2175   The Rules
2176
2177   "{a,b}" indicates a choice of a xor b.
2178   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2179
2180   Rule 1:
2181   (set <reg1> <reg2>:cfa.reg)
2182   effects: cfa.reg = <reg1>
2183            cfa.offset unchanged
2184            cfa_temp.reg = <reg1>
2185            cfa_temp.offset = cfa.offset
2186
2187   Rule 2:
2188   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2189                               {<const_int>,<reg>:cfa_temp.reg}))
2190   effects: cfa.reg = sp if fp used
2191            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2192            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2193              if cfa_store.reg==sp
2194
2195   Rule 3:
2196   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2197   effects: cfa.reg = fp
2198            cfa_offset += +/- <const_int>
2199
2200   Rule 4:
2201   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2202   constraints: <reg1> != fp
2203                <reg1> != sp
2204   effects: cfa.reg = <reg1>
2205            cfa_temp.reg = <reg1>
2206            cfa_temp.offset = cfa.offset
2207
2208   Rule 5:
2209   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2210   constraints: <reg1> != fp
2211                <reg1> != sp
2212   effects: cfa_store.reg = <reg1>
2213            cfa_store.offset = cfa.offset - cfa_temp.offset
2214
2215   Rule 6:
2216   (set <reg> <const_int>)
2217   effects: cfa_temp.reg = <reg>
2218            cfa_temp.offset = <const_int>
2219
2220   Rule 7:
2221   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2222   effects: cfa_temp.reg = <reg1>
2223            cfa_temp.offset |= <const_int>
2224
2225   Rule 8:
2226   (set <reg> (high <exp>))
2227   effects: none
2228
2229   Rule 9:
2230   (set <reg> (lo_sum <exp> <const_int>))
2231   effects: cfa_temp.reg = <reg>
2232            cfa_temp.offset = <const_int>
2233
2234   Rule 10:
2235   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2236   effects: cfa_store.offset -= <const_int>
2237            cfa.offset = cfa_store.offset if cfa.reg == sp
2238            cfa.reg = sp
2239            cfa.base_offset = -cfa_store.offset
2240
2241   Rule 11:
2242   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2243   effects: cfa_store.offset += -/+ mode_size(mem)
2244            cfa.offset = cfa_store.offset if cfa.reg == sp
2245            cfa.reg = sp
2246            cfa.base_offset = -cfa_store.offset
2247
2248   Rule 12:
2249   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2250
2251        <reg2>)
2252   effects: cfa.reg = <reg1>
2253            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2254
2255   Rule 13:
2256   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2257   effects: cfa.reg = <reg1>
2258            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2259
2260   Rule 14:
2261   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2262   effects: cfa.reg = <reg1>
2263            cfa.base_offset = -cfa_temp.offset
2264            cfa_temp.offset -= mode_size(mem)
2265
2266   Rule 15:
2267   (set <reg> {unspec, unspec_volatile})
2268   effects: target-dependent
2269
2270   Rule 16:
2271   (set sp (and: sp <const_int>))
2272   constraints: cfa_store.reg == sp
2273   effects: current_fde.stack_realign = 1
2274            cfa_store.offset = 0
2275            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2276
2277   Rule 17:
2278   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2279   effects: cfa_store.offset += -/+ mode_size(mem)
2280
2281   Rule 18:
2282   (set (mem ({pre_inc, pre_dec} sp)) fp)
2283   constraints: fde->stack_realign == 1
2284   effects: cfa_store.offset = 0
2285            cfa.reg != HARD_FRAME_POINTER_REGNUM
2286
2287   Rule 19:
2288   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2289   constraints: fde->stack_realign == 1
2290                && cfa.offset == 0
2291                && cfa.indirect == 0
2292                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2293   effects: Use DW_CFA_def_cfa_expression to define cfa
2294            cfa.reg == fde->drap_reg  */
2295
2296 static void
2297 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2298 {
2299   rtx src, dest, span;
2300   HOST_WIDE_INT offset;
2301   dw_fde_ref fde;
2302
2303   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2304      the PARALLEL independently. The first element is always processed if
2305      it is a SET. This is for backward compatibility.   Other elements
2306      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2307      flag is set in them.  */
2308   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2309     {
2310       int par_index;
2311       int limit = XVECLEN (expr, 0);
2312       rtx elem;
2313
2314       /* PARALLELs have strict read-modify-write semantics, so we
2315          ought to evaluate every rvalue before changing any lvalue.
2316          It's cumbersome to do that in general, but there's an
2317          easy approximation that is enough for all current users:
2318          handle register saves before register assignments.  */
2319       if (GET_CODE (expr) == PARALLEL)
2320         for (par_index = 0; par_index < limit; par_index++)
2321           {
2322             elem = XVECEXP (expr, 0, par_index);
2323             if (GET_CODE (elem) == SET
2324                 && MEM_P (SET_DEST (elem))
2325                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2326               dwarf2out_frame_debug_expr (elem, label);
2327           }
2328
2329       for (par_index = 0; par_index < limit; par_index++)
2330         {
2331           elem = XVECEXP (expr, 0, par_index);
2332           if (GET_CODE (elem) == SET
2333               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2334               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2335             dwarf2out_frame_debug_expr (elem, label);
2336           else if (GET_CODE (elem) == SET
2337                    && par_index != 0
2338                    && !RTX_FRAME_RELATED_P (elem))
2339             {
2340               /* Stack adjustment combining might combine some post-prologue
2341                  stack adjustment into a prologue stack adjustment.  */
2342               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2343
2344               if (offset != 0)
2345                 dwarf2out_stack_adjust (offset, label);
2346             }
2347         }
2348       return;
2349     }
2350
2351   gcc_assert (GET_CODE (expr) == SET);
2352
2353   src = SET_SRC (expr);
2354   dest = SET_DEST (expr);
2355
2356   if (REG_P (src))
2357     {
2358       rtx rsi = reg_saved_in (src);
2359       if (rsi)
2360         src = rsi;
2361     }
2362
2363   fde = current_fde ();
2364
2365   switch (GET_CODE (dest))
2366     {
2367     case REG:
2368       switch (GET_CODE (src))
2369         {
2370           /* Setting FP from SP.  */
2371         case REG:
2372           if (cfa.reg == (unsigned) REGNO (src))
2373             {
2374               /* Rule 1 */
2375               /* Update the CFA rule wrt SP or FP.  Make sure src is
2376                  relative to the current CFA register.
2377
2378                  We used to require that dest be either SP or FP, but the
2379                  ARM copies SP to a temporary register, and from there to
2380                  FP.  So we just rely on the backends to only set
2381                  RTX_FRAME_RELATED_P on appropriate insns.  */
2382               cfa.reg = REGNO (dest);
2383               cfa_temp.reg = cfa.reg;
2384               cfa_temp.offset = cfa.offset;
2385             }
2386           else
2387             {
2388               /* Saving a register in a register.  */
2389               gcc_assert (!fixed_regs [REGNO (dest)]
2390                           /* For the SPARC and its register window.  */
2391                           || (DWARF_FRAME_REGNUM (REGNO (src))
2392                               == DWARF_FRAME_RETURN_COLUMN));
2393
2394               /* After stack is aligned, we can only save SP in FP
2395                  if drap register is used.  In this case, we have
2396                  to restore stack pointer with the CFA value and we
2397                  don't generate this DWARF information.  */
2398               if (fde
2399                   && fde->stack_realign
2400                   && REGNO (src) == STACK_POINTER_REGNUM)
2401                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2402                             && fde->drap_reg != INVALID_REGNUM
2403                             && cfa.reg != REGNO (src));
2404               else
2405                 queue_reg_save (label, src, dest, 0);
2406             }
2407           break;
2408
2409         case PLUS:
2410         case MINUS:
2411         case LO_SUM:
2412           if (dest == stack_pointer_rtx)
2413             {
2414               /* Rule 2 */
2415               /* Adjusting SP.  */
2416               switch (GET_CODE (XEXP (src, 1)))
2417                 {
2418                 case CONST_INT:
2419                   offset = INTVAL (XEXP (src, 1));
2420                   break;
2421                 case REG:
2422                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2423                               == cfa_temp.reg);
2424                   offset = cfa_temp.offset;
2425                   break;
2426                 default:
2427                   gcc_unreachable ();
2428                 }
2429
2430               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2431                 {
2432                   /* Restoring SP from FP in the epilogue.  */
2433                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2434                   cfa.reg = STACK_POINTER_REGNUM;
2435                 }
2436               else if (GET_CODE (src) == LO_SUM)
2437                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2438                 ;
2439               else
2440                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2441
2442               if (GET_CODE (src) != MINUS)
2443                 offset = -offset;
2444               if (cfa.reg == STACK_POINTER_REGNUM)
2445                 cfa.offset += offset;
2446               if (cfa_store.reg == STACK_POINTER_REGNUM)
2447                 cfa_store.offset += offset;
2448             }
2449           else if (dest == hard_frame_pointer_rtx)
2450             {
2451               /* Rule 3 */
2452               /* Either setting the FP from an offset of the SP,
2453                  or adjusting the FP */
2454               gcc_assert (frame_pointer_needed);
2455
2456               gcc_assert (REG_P (XEXP (src, 0))
2457                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2458                           && CONST_INT_P (XEXP (src, 1)));
2459               offset = INTVAL (XEXP (src, 1));
2460               if (GET_CODE (src) != MINUS)
2461                 offset = -offset;
2462               cfa.offset += offset;
2463               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2464             }
2465           else
2466             {
2467               gcc_assert (GET_CODE (src) != MINUS);
2468
2469               /* Rule 4 */
2470               if (REG_P (XEXP (src, 0))
2471                   && REGNO (XEXP (src, 0)) == cfa.reg
2472                   && CONST_INT_P (XEXP (src, 1)))
2473                 {
2474                   /* Setting a temporary CFA register that will be copied
2475                      into the FP later on.  */
2476                   offset = - INTVAL (XEXP (src, 1));
2477                   cfa.offset += offset;
2478                   cfa.reg = REGNO (dest);
2479                   /* Or used to save regs to the stack.  */
2480                   cfa_temp.reg = cfa.reg;
2481                   cfa_temp.offset = cfa.offset;
2482                 }
2483
2484               /* Rule 5 */
2485               else if (REG_P (XEXP (src, 0))
2486                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2487                        && XEXP (src, 1) == stack_pointer_rtx)
2488                 {
2489                   /* Setting a scratch register that we will use instead
2490                      of SP for saving registers to the stack.  */
2491                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2492                   cfa_store.reg = REGNO (dest);
2493                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2494                 }
2495
2496               /* Rule 9 */
2497               else if (GET_CODE (src) == LO_SUM
2498                        && CONST_INT_P (XEXP (src, 1)))
2499                 {
2500                   cfa_temp.reg = REGNO (dest);
2501                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2502                 }
2503               else
2504                 gcc_unreachable ();
2505             }
2506           break;
2507
2508           /* Rule 6 */
2509         case CONST_INT:
2510           cfa_temp.reg = REGNO (dest);
2511           cfa_temp.offset = INTVAL (src);
2512           break;
2513
2514           /* Rule 7 */
2515         case IOR:
2516           gcc_assert (REG_P (XEXP (src, 0))
2517                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2518                       && CONST_INT_P (XEXP (src, 1)));
2519
2520           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2521             cfa_temp.reg = REGNO (dest);
2522           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2523           break;
2524
2525           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2526              which will fill in all of the bits.  */
2527           /* Rule 8 */
2528         case HIGH:
2529           break;
2530
2531           /* Rule 15 */
2532         case UNSPEC:
2533         case UNSPEC_VOLATILE:
2534           gcc_assert (targetm.dwarf_handle_frame_unspec);
2535           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2536           return;
2537
2538           /* Rule 16 */
2539         case AND:
2540           /* If this AND operation happens on stack pointer in prologue,
2541              we assume the stack is realigned and we extract the
2542              alignment.  */
2543           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2544             {
2545               /* We interpret reg_save differently with stack_realign set.
2546                  Thus we must flush whatever we have queued first.  */
2547               dwarf2out_flush_queued_reg_saves ();
2548
2549               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2550               fde->stack_realign = 1;
2551               fde->stack_realignment = INTVAL (XEXP (src, 1));
2552               cfa_store.offset = 0;
2553
2554               if (cfa.reg != STACK_POINTER_REGNUM
2555                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2556                 fde->drap_reg = cfa.reg;
2557             }
2558           return;
2559
2560         default:
2561           gcc_unreachable ();
2562         }
2563
2564       def_cfa_1 (label, &cfa);
2565       break;
2566
2567     case MEM:
2568
2569       /* Saving a register to the stack.  Make sure dest is relative to the
2570          CFA register.  */
2571       switch (GET_CODE (XEXP (dest, 0)))
2572         {
2573           /* Rule 10 */
2574           /* With a push.  */
2575         case PRE_MODIFY:
2576           /* We can't handle variable size modifications.  */
2577           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2578                       == CONST_INT);
2579           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2580
2581           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2582                       && cfa_store.reg == STACK_POINTER_REGNUM);
2583
2584           cfa_store.offset += offset;
2585           if (cfa.reg == STACK_POINTER_REGNUM)
2586             cfa.offset = cfa_store.offset;
2587
2588           offset = -cfa_store.offset;
2589           break;
2590
2591           /* Rule 11 */
2592         case PRE_INC:
2593         case PRE_DEC:
2594         case POST_DEC:
2595           offset = GET_MODE_SIZE (GET_MODE (dest));
2596           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2597             offset = -offset;
2598
2599           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2600                        == STACK_POINTER_REGNUM)
2601                       && cfa_store.reg == STACK_POINTER_REGNUM);
2602
2603           cfa_store.offset += offset;
2604
2605           /* Rule 18: If stack is aligned, we will use FP as a
2606              reference to represent the address of the stored
2607              regiser.  */
2608           if (fde
2609               && fde->stack_realign
2610               && src == hard_frame_pointer_rtx)
2611             {
2612               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2613               cfa_store.offset = 0;
2614             }
2615
2616           if (cfa.reg == STACK_POINTER_REGNUM)
2617             cfa.offset = cfa_store.offset;
2618
2619           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2620             offset += -cfa_store.offset;
2621           else
2622             offset = -cfa_store.offset;
2623           break;
2624
2625           /* Rule 12 */
2626           /* With an offset.  */
2627         case PLUS:
2628         case MINUS:
2629         case LO_SUM:
2630           {
2631             int regno;
2632
2633             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2634                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2635             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2636             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2637               offset = -offset;
2638
2639             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2640
2641             if (cfa.reg == (unsigned) regno)
2642               offset -= cfa.offset;
2643             else if (cfa_store.reg == (unsigned) regno)
2644               offset -= cfa_store.offset;
2645             else
2646               {
2647                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2648                 offset -= cfa_temp.offset;
2649               }
2650           }
2651           break;
2652
2653           /* Rule 13 */
2654           /* Without an offset.  */
2655         case REG:
2656           {
2657             int regno = REGNO (XEXP (dest, 0));
2658
2659             if (cfa.reg == (unsigned) regno)
2660               offset = -cfa.offset;
2661             else if (cfa_store.reg == (unsigned) regno)
2662               offset = -cfa_store.offset;
2663             else
2664               {
2665                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2666                 offset = -cfa_temp.offset;
2667               }
2668           }
2669           break;
2670
2671           /* Rule 14 */
2672         case POST_INC:
2673           gcc_assert (cfa_temp.reg
2674                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2675           offset = -cfa_temp.offset;
2676           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2677           break;
2678
2679         default:
2680           gcc_unreachable ();
2681         }
2682
2683         /* Rule 17 */
2684         /* If the source operand of this MEM operation is not a
2685            register, basically the source is return address.  Here
2686            we only care how much stack grew and we don't save it.  */
2687       if (!REG_P (src))
2688         break;
2689
2690       if (REGNO (src) != STACK_POINTER_REGNUM
2691           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2692           && (unsigned) REGNO (src) == cfa.reg)
2693         {
2694           /* We're storing the current CFA reg into the stack.  */
2695
2696           if (cfa.offset == 0)
2697             {
2698               /* Rule 19 */
2699               /* If stack is aligned, putting CFA reg into stack means
2700                  we can no longer use reg + offset to represent CFA.
2701                  Here we use DW_CFA_def_cfa_expression instead.  The
2702                  result of this expression equals to the original CFA
2703                  value.  */
2704               if (fde
2705                   && fde->stack_realign
2706                   && cfa.indirect == 0
2707                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2708                 {
2709                   dw_cfa_location cfa_exp;
2710
2711                   gcc_assert (fde->drap_reg == cfa.reg);
2712
2713                   cfa_exp.indirect = 1;
2714                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2715                   cfa_exp.base_offset = offset;
2716                   cfa_exp.offset = 0;
2717
2718                   fde->drap_reg_saved = 1;
2719
2720                   def_cfa_1 (label, &cfa_exp);
2721                   break;
2722                 }
2723
2724               /* If the source register is exactly the CFA, assume
2725                  we're saving SP like any other register; this happens
2726                  on the ARM.  */
2727               def_cfa_1 (label, &cfa);
2728               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2729               break;
2730             }
2731           else
2732             {
2733               /* Otherwise, we'll need to look in the stack to
2734                  calculate the CFA.  */
2735               rtx x = XEXP (dest, 0);
2736
2737               if (!REG_P (x))
2738                 x = XEXP (x, 0);
2739               gcc_assert (REG_P (x));
2740
2741               cfa.reg = REGNO (x);
2742               cfa.base_offset = offset;
2743               cfa.indirect = 1;
2744               def_cfa_1 (label, &cfa);
2745               break;
2746             }
2747         }
2748
2749       def_cfa_1 (label, &cfa);
2750       {
2751         span = targetm.dwarf_register_span (src);
2752
2753         if (!span)
2754           queue_reg_save (label, src, NULL_RTX, offset);
2755         else
2756           {
2757             /* We have a PARALLEL describing where the contents of SRC
2758                live.  Queue register saves for each piece of the
2759                PARALLEL.  */
2760             int par_index;
2761             int limit;
2762             HOST_WIDE_INT span_offset = offset;
2763
2764             gcc_assert (GET_CODE (span) == PARALLEL);
2765
2766             limit = XVECLEN (span, 0);
2767             for (par_index = 0; par_index < limit; par_index++)
2768               {
2769                 rtx elem = XVECEXP (span, 0, par_index);
2770
2771                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2772                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2773               }
2774           }
2775       }
2776       break;
2777
2778     default:
2779       gcc_unreachable ();
2780     }
2781 }
2782
2783 /* Record call frame debugging information for INSN, which either
2784    sets SP or FP (adjusting how we calculate the frame address) or saves a
2785    register to the stack.  If INSN is NULL_RTX, initialize our state.
2786
2787    If AFTER_P is false, we're being called before the insn is emitted,
2788    otherwise after.  Call instructions get invoked twice.  */
2789
2790 void
2791 dwarf2out_frame_debug (rtx insn, bool after_p)
2792 {
2793   const char *label;
2794   rtx note, n;
2795   bool handled_one = false;
2796
2797   if (insn == NULL_RTX)
2798     {
2799       size_t i;
2800
2801       /* Flush any queued register saves.  */
2802       dwarf2out_flush_queued_reg_saves ();
2803
2804       /* Set up state for generating call frame debug info.  */
2805       lookup_cfa (&cfa);
2806       gcc_assert (cfa.reg
2807                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2808
2809       cfa.reg = STACK_POINTER_REGNUM;
2810       cfa_store = cfa;
2811       cfa_temp.reg = -1;
2812       cfa_temp.offset = 0;
2813
2814       for (i = 0; i < num_regs_saved_in_regs; i++)
2815         {
2816           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2817           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2818         }
2819       num_regs_saved_in_regs = 0;
2820
2821       if (barrier_args_size)
2822         {
2823           XDELETEVEC (barrier_args_size);
2824           barrier_args_size = NULL;
2825         }
2826       return;
2827     }
2828
2829   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2830     dwarf2out_flush_queued_reg_saves ();
2831
2832   if (!RTX_FRAME_RELATED_P (insn))
2833     {
2834       /* ??? This should be done unconditionally since stack adjustments
2835          matter if the stack pointer is not the CFA register anymore but
2836          is still used to save registers.  */
2837       if (!ACCUMULATE_OUTGOING_ARGS)
2838         dwarf2out_notice_stack_adjust (insn, after_p);
2839       return;
2840     }
2841
2842   label = dwarf2out_cfi_label (false);
2843   any_cfis_emitted = false;
2844
2845   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2846     switch (REG_NOTE_KIND (note))
2847       {
2848       case REG_FRAME_RELATED_EXPR:
2849         insn = XEXP (note, 0);
2850         goto found;
2851
2852       case REG_CFA_DEF_CFA:
2853         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2854         handled_one = true;
2855         break;
2856
2857       case REG_CFA_ADJUST_CFA:
2858         n = XEXP (note, 0);
2859         if (n == NULL)
2860           {
2861             n = PATTERN (insn);
2862             if (GET_CODE (n) == PARALLEL)
2863               n = XVECEXP (n, 0, 0);
2864           }
2865         dwarf2out_frame_debug_adjust_cfa (n, label);
2866         handled_one = true;
2867         break;
2868
2869       case REG_CFA_OFFSET:
2870         n = XEXP (note, 0);
2871         if (n == NULL)
2872           n = single_set (insn);
2873         dwarf2out_frame_debug_cfa_offset (n, label);
2874         handled_one = true;
2875         break;
2876
2877       case REG_CFA_REGISTER:
2878         n = XEXP (note, 0);
2879         if (n == NULL)
2880           {
2881             n = PATTERN (insn);
2882             if (GET_CODE (n) == PARALLEL)
2883               n = XVECEXP (n, 0, 0);
2884           }
2885         dwarf2out_frame_debug_cfa_register (n, label);
2886         handled_one = true;
2887         break;
2888
2889       case REG_CFA_EXPRESSION:
2890         n = XEXP (note, 0);
2891         if (n == NULL)
2892           n = single_set (insn);
2893         dwarf2out_frame_debug_cfa_expression (n, label);
2894         handled_one = true;
2895         break;
2896
2897       case REG_CFA_RESTORE:
2898         n = XEXP (note, 0);
2899         if (n == NULL)
2900           {
2901             n = PATTERN (insn);
2902             if (GET_CODE (n) == PARALLEL)
2903               n = XVECEXP (n, 0, 0);
2904             n = XEXP (n, 0);
2905           }
2906         dwarf2out_frame_debug_cfa_restore (n, label);
2907         handled_one = true;
2908         break;
2909
2910       case REG_CFA_SET_VDRAP:
2911         n = XEXP (note, 0);
2912         if (REG_P (n))
2913           {
2914             dw_fde_ref fde = current_fde ();
2915             if (fde)
2916               {
2917                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2918                 if (REG_P (n))
2919                   fde->vdrap_reg = REGNO (n);
2920               }
2921           }
2922         handled_one = true;
2923         break;
2924
2925       default:
2926         break;
2927       }
2928   if (handled_one)
2929     {
2930       if (any_cfis_emitted)
2931         dwarf2out_flush_queued_reg_saves ();
2932       return;
2933     }
2934
2935   insn = PATTERN (insn);
2936  found:
2937   dwarf2out_frame_debug_expr (insn, label);
2938
2939   /* Check again.  A parallel can save and update the same register.
2940      We could probably check just once, here, but this is safer than
2941      removing the check above.  */
2942   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2943     dwarf2out_flush_queued_reg_saves ();
2944 }
2945
2946 /* Determine if we need to save and restore CFI information around this
2947    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2948    we do need to save/restore, then emit the save now, and insert a
2949    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2950
2951 void
2952 dwarf2out_cfi_begin_epilogue (rtx insn)
2953 {
2954   bool saw_frp = false;
2955   rtx i;
2956
2957   /* Scan forward to the return insn, noticing if there are possible
2958      frame related insns.  */
2959   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2960     {
2961       if (!INSN_P (i))
2962         continue;
2963
2964       /* Look for both regular and sibcalls to end the block.  */
2965       if (returnjump_p (i))
2966         break;
2967       if (CALL_P (i) && SIBLING_CALL_P (i))
2968         break;
2969
2970       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2971         {
2972           int idx;
2973           rtx seq = PATTERN (i);
2974
2975           if (returnjump_p (XVECEXP (seq, 0, 0)))
2976             break;
2977           if (CALL_P (XVECEXP (seq, 0, 0))
2978               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2979             break;
2980
2981           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2982             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2983               saw_frp = true;
2984         }
2985
2986       if (RTX_FRAME_RELATED_P (i))
2987         saw_frp = true;
2988     }
2989
2990   /* If the port doesn't emit epilogue unwind info, we don't need a
2991      save/restore pair.  */
2992   if (!saw_frp)
2993     return;
2994
2995   /* Otherwise, search forward to see if the return insn was the last
2996      basic block of the function.  If so, we don't need save/restore.  */
2997   gcc_assert (i != NULL);
2998   i = next_real_insn (i);
2999   if (i == NULL)
3000     return;
3001
3002   /* Insert the restore before that next real insn in the stream, and before
3003      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3004      properly nested.  This should be after any label or alignment.  This
3005      will be pushed into the CFI stream by the function below.  */
3006   while (1)
3007     {
3008       rtx p = PREV_INSN (i);
3009       if (!NOTE_P (p))
3010         break;
3011       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3012         break;
3013       i = p;
3014     }
3015   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3016
3017   emit_cfa_remember = true;
3018
3019   /* And emulate the state save.  */
3020   gcc_assert (!cfa_remember.in_use);
3021   cfa_remember = cfa;
3022   cfa_remember.in_use = 1;
3023 }
3024
3025 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3026    required.  */
3027
3028 void
3029 dwarf2out_frame_debug_restore_state (void)
3030 {
3031   dw_cfi_ref cfi = new_cfi ();
3032   const char *label = dwarf2out_cfi_label (false);
3033
3034   cfi->dw_cfi_opc = DW_CFA_restore_state;
3035   add_fde_cfi (label, cfi);
3036
3037   gcc_assert (cfa_remember.in_use);
3038   cfa = cfa_remember;
3039   cfa_remember.in_use = 0;
3040 }
3041
3042 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3043 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3044  (enum dwarf_call_frame_info cfi);
3045
3046 static enum dw_cfi_oprnd_type
3047 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3048 {
3049   switch (cfi)
3050     {
3051     case DW_CFA_nop:
3052     case DW_CFA_GNU_window_save:
3053     case DW_CFA_remember_state:
3054     case DW_CFA_restore_state:
3055       return dw_cfi_oprnd_unused;
3056
3057     case DW_CFA_set_loc:
3058     case DW_CFA_advance_loc1:
3059     case DW_CFA_advance_loc2:
3060     case DW_CFA_advance_loc4:
3061     case DW_CFA_MIPS_advance_loc8:
3062       return dw_cfi_oprnd_addr;
3063
3064     case DW_CFA_offset:
3065     case DW_CFA_offset_extended:
3066     case DW_CFA_def_cfa:
3067     case DW_CFA_offset_extended_sf:
3068     case DW_CFA_def_cfa_sf:
3069     case DW_CFA_restore:
3070     case DW_CFA_restore_extended:
3071     case DW_CFA_undefined:
3072     case DW_CFA_same_value:
3073     case DW_CFA_def_cfa_register:
3074     case DW_CFA_register:
3075     case DW_CFA_expression:
3076       return dw_cfi_oprnd_reg_num;
3077
3078     case DW_CFA_def_cfa_offset:
3079     case DW_CFA_GNU_args_size:
3080     case DW_CFA_def_cfa_offset_sf:
3081       return dw_cfi_oprnd_offset;
3082
3083     case DW_CFA_def_cfa_expression:
3084       return dw_cfi_oprnd_loc;
3085
3086     default:
3087       gcc_unreachable ();
3088     }
3089 }
3090
3091 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3092 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3093  (enum dwarf_call_frame_info cfi);
3094
3095 static enum dw_cfi_oprnd_type
3096 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3097 {
3098   switch (cfi)
3099     {
3100     case DW_CFA_def_cfa:
3101     case DW_CFA_def_cfa_sf:
3102     case DW_CFA_offset:
3103     case DW_CFA_offset_extended_sf:
3104     case DW_CFA_offset_extended:
3105       return dw_cfi_oprnd_offset;
3106
3107     case DW_CFA_register:
3108       return dw_cfi_oprnd_reg_num;
3109
3110     case DW_CFA_expression:
3111       return dw_cfi_oprnd_loc;
3112
3113     default:
3114       return dw_cfi_oprnd_unused;
3115     }
3116 }
3117
3118 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3119    switch to the data section instead, and write out a synthetic start label
3120    for collect2 the first time around.  */
3121
3122 static void
3123 switch_to_eh_frame_section (bool back)
3124 {
3125   tree label;
3126
3127 #ifdef EH_FRAME_SECTION_NAME
3128   if (eh_frame_section == 0)
3129     {
3130       int flags;
3131
3132       if (EH_TABLES_CAN_BE_READ_ONLY)
3133         {
3134           int fde_encoding;
3135           int per_encoding;
3136           int lsda_encoding;
3137
3138           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3139                                                        /*global=*/0);
3140           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3141                                                        /*global=*/1);
3142           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3143                                                         /*global=*/0);
3144           flags = ((! flag_pic
3145                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3146                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3147                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3148                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3149                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3150                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3151                    ? 0 : SECTION_WRITE);
3152         }
3153       else
3154         flags = SECTION_WRITE;
3155       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3156     }
3157 #endif /* EH_FRAME_SECTION_NAME */
3158
3159   if (eh_frame_section)
3160     switch_to_section (eh_frame_section);
3161   else
3162     {
3163       /* We have no special eh_frame section.  Put the information in
3164          the data section and emit special labels to guide collect2.  */
3165       switch_to_section (data_section);
3166
3167       if (!back)
3168         {
3169           label = get_file_function_name ("F");
3170           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3171           targetm.asm_out.globalize_label (asm_out_file,
3172                                            IDENTIFIER_POINTER (label));
3173           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3174         }
3175     }
3176 }
3177
3178 /* Switch [BACK] to the eh or debug frame table section, depending on
3179    FOR_EH.  */
3180
3181 static void
3182 switch_to_frame_table_section (int for_eh, bool back)
3183 {
3184   if (for_eh)
3185     switch_to_eh_frame_section (back);
3186   else
3187     {
3188       if (!debug_frame_section)
3189         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3190                                            SECTION_DEBUG, NULL);
3191       switch_to_section (debug_frame_section);
3192     }
3193 }
3194
3195 /* Output a Call Frame Information opcode and its operand(s).  */
3196
3197 static void
3198 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3199 {
3200   unsigned long r;
3201   HOST_WIDE_INT off;
3202
3203   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3204     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3205                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3206                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3207                          ((unsigned HOST_WIDE_INT)
3208                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3209   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3210     {
3211       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3212       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3213                            "DW_CFA_offset, column %#lx", r);
3214       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3215       dw2_asm_output_data_uleb128 (off, NULL);
3216     }
3217   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3218     {
3219       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3220       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3221                            "DW_CFA_restore, column %#lx", r);
3222     }
3223   else
3224     {
3225       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3226                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3227
3228       switch (cfi->dw_cfi_opc)
3229         {
3230         case DW_CFA_set_loc:
3231           if (for_eh)
3232             dw2_asm_output_encoded_addr_rtx (
3233                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3234                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3235                 false, NULL);
3236           else
3237             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3238                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3239           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3240           break;
3241
3242         case DW_CFA_advance_loc1:
3243           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3244                                 fde->dw_fde_current_label, NULL);
3245           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3246           break;
3247
3248         case DW_CFA_advance_loc2:
3249           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3250                                 fde->dw_fde_current_label, NULL);
3251           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3252           break;
3253
3254         case DW_CFA_advance_loc4:
3255           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3256                                 fde->dw_fde_current_label, NULL);
3257           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3258           break;
3259
3260         case DW_CFA_MIPS_advance_loc8:
3261           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3262                                 fde->dw_fde_current_label, NULL);
3263           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3264           break;
3265
3266         case DW_CFA_offset_extended:
3267           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3268           dw2_asm_output_data_uleb128 (r, NULL);
3269           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3270           dw2_asm_output_data_uleb128 (off, NULL);
3271           break;
3272
3273         case DW_CFA_def_cfa:
3274           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3275           dw2_asm_output_data_uleb128 (r, NULL);
3276           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3277           break;
3278
3279         case DW_CFA_offset_extended_sf:
3280           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3281           dw2_asm_output_data_uleb128 (r, NULL);
3282           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3283           dw2_asm_output_data_sleb128 (off, NULL);
3284           break;
3285
3286         case DW_CFA_def_cfa_sf:
3287           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3288           dw2_asm_output_data_uleb128 (r, NULL);
3289           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3290           dw2_asm_output_data_sleb128 (off, NULL);
3291           break;
3292
3293         case DW_CFA_restore_extended:
3294         case DW_CFA_undefined:
3295         case DW_CFA_same_value:
3296         case DW_CFA_def_cfa_register:
3297           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3298           dw2_asm_output_data_uleb128 (r, NULL);
3299           break;
3300
3301         case DW_CFA_register:
3302           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3303           dw2_asm_output_data_uleb128 (r, NULL);
3304           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3305           dw2_asm_output_data_uleb128 (r, NULL);
3306           break;
3307
3308         case DW_CFA_def_cfa_offset:
3309         case DW_CFA_GNU_args_size:
3310           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3311           break;
3312
3313         case DW_CFA_def_cfa_offset_sf:
3314           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3315           dw2_asm_output_data_sleb128 (off, NULL);
3316           break;
3317
3318         case DW_CFA_GNU_window_save:
3319           break;
3320
3321         case DW_CFA_def_cfa_expression:
3322         case DW_CFA_expression:
3323           output_cfa_loc (cfi, for_eh);
3324           break;
3325
3326         case DW_CFA_GNU_negative_offset_extended:
3327           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3328           gcc_unreachable ();
3329
3330         default:
3331           break;
3332         }
3333     }
3334 }
3335
3336 /* Similar, but do it via assembler directives instead.  */
3337
3338 static void
3339 output_cfi_directive (dw_cfi_ref cfi)
3340 {
3341   unsigned long r, r2;
3342
3343   switch (cfi->dw_cfi_opc)
3344     {
3345     case DW_CFA_advance_loc:
3346     case DW_CFA_advance_loc1:
3347     case DW_CFA_advance_loc2:
3348     case DW_CFA_advance_loc4:
3349     case DW_CFA_MIPS_advance_loc8:
3350     case DW_CFA_set_loc:
3351       /* Should only be created by add_fde_cfi in a code path not
3352          followed when emitting via directives.  The assembler is
3353          going to take care of this for us.  */
3354       gcc_unreachable ();
3355
3356     case DW_CFA_offset:
3357     case DW_CFA_offset_extended:
3358     case DW_CFA_offset_extended_sf:
3359       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3360       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3361                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3362       break;
3363
3364     case DW_CFA_restore:
3365     case DW_CFA_restore_extended:
3366       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3367       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3368       break;
3369
3370     case DW_CFA_undefined:
3371       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3372       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3373       break;
3374
3375     case DW_CFA_same_value:
3376       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3377       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3378       break;
3379
3380     case DW_CFA_def_cfa:
3381     case DW_CFA_def_cfa_sf:
3382       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3384                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3385       break;
3386
3387     case DW_CFA_def_cfa_register:
3388       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3389       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3390       break;
3391
3392     case DW_CFA_register:
3393       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3394       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3395       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3396       break;
3397
3398     case DW_CFA_def_cfa_offset:
3399     case DW_CFA_def_cfa_offset_sf:
3400       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3401                HOST_WIDE_INT_PRINT_DEC"\n",
3402                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3403       break;
3404
3405     case DW_CFA_remember_state:
3406       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3407       break;
3408     case DW_CFA_restore_state:
3409       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3410       break;
3411
3412     case DW_CFA_GNU_args_size:
3413       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3414       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415       if (flag_debug_asm)
3416         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3417                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3418       fputc ('\n', asm_out_file);
3419       break;
3420
3421     case DW_CFA_GNU_window_save:
3422       fprintf (asm_out_file, "\t.cfi_window_save\n");
3423       break;
3424
3425     case DW_CFA_def_cfa_expression:
3426     case DW_CFA_expression:
3427       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3428       output_cfa_loc_raw (cfi);
3429       fputc ('\n', asm_out_file);
3430       break;
3431
3432     default:
3433       gcc_unreachable ();
3434     }
3435 }
3436
3437 DEF_VEC_P (dw_cfi_ref);
3438 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3439
3440 /* Output CFIs to bring current FDE to the same state as after executing
3441    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3442    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3443    other arguments to pass to output_cfi.  */
3444
3445 static void
3446 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3447 {
3448   struct dw_cfi_struct cfi_buf;
3449   dw_cfi_ref cfi2;
3450   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3451   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3452   unsigned int len, idx;
3453
3454   for (;; cfi = cfi->dw_cfi_next)
3455     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3456       {
3457       case DW_CFA_advance_loc:
3458       case DW_CFA_advance_loc1:
3459       case DW_CFA_advance_loc2:
3460       case DW_CFA_advance_loc4:
3461       case DW_CFA_MIPS_advance_loc8:
3462       case DW_CFA_set_loc:
3463         /* All advances should be ignored.  */
3464         break;
3465       case DW_CFA_remember_state:
3466         {
3467           dw_cfi_ref args_size = cfi_args_size;
3468
3469           /* Skip everything between .cfi_remember_state and
3470              .cfi_restore_state.  */
3471           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3472             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3473               break;
3474             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3475               args_size = cfi2;
3476             else
3477               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3478
3479           if (cfi2 == NULL)
3480             goto flush_all;
3481           else
3482             {
3483               cfi = cfi2;
3484               cfi_args_size = args_size;
3485             }
3486           break;
3487         }
3488       case DW_CFA_GNU_args_size:
3489         cfi_args_size = cfi;
3490         break;
3491       case DW_CFA_GNU_window_save:
3492         goto flush_all;
3493       case DW_CFA_offset:
3494       case DW_CFA_offset_extended:
3495       case DW_CFA_offset_extended_sf:
3496       case DW_CFA_restore:
3497       case DW_CFA_restore_extended:
3498       case DW_CFA_undefined:
3499       case DW_CFA_same_value:
3500       case DW_CFA_register:
3501       case DW_CFA_val_offset:
3502       case DW_CFA_val_offset_sf:
3503       case DW_CFA_expression:
3504       case DW_CFA_val_expression:
3505       case DW_CFA_GNU_negative_offset_extended:
3506         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3507           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3508                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3509         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3510         break;
3511       case DW_CFA_def_cfa:
3512       case DW_CFA_def_cfa_sf:
3513       case DW_CFA_def_cfa_expression:
3514         cfi_cfa = cfi;
3515         cfi_cfa_offset = cfi;
3516         break;
3517       case DW_CFA_def_cfa_register:
3518         cfi_cfa = cfi;
3519         break;
3520       case DW_CFA_def_cfa_offset:
3521       case DW_CFA_def_cfa_offset_sf:
3522         cfi_cfa_offset = cfi;
3523         break;
3524       case DW_CFA_nop:
3525         gcc_assert (cfi == NULL);
3526       flush_all:
3527         len = VEC_length (dw_cfi_ref, regs);
3528         for (idx = 0; idx < len; idx++)
3529           {
3530             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3531             if (cfi2 != NULL
3532                 && cfi2->dw_cfi_opc != DW_CFA_restore
3533                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3534               {
3535                 if (do_cfi_asm)
3536                   output_cfi_directive (cfi2);
3537                 else
3538                   output_cfi (cfi2, fde, for_eh);
3539               }
3540           }
3541         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3542           {
3543             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3544             cfi_buf = *cfi_cfa;
3545             switch (cfi_cfa_offset->dw_cfi_opc)
3546               {
3547               case DW_CFA_def_cfa_offset:
3548                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3549                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3550                 break;
3551               case DW_CFA_def_cfa_offset_sf:
3552                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3553                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3554                 break;
3555               case DW_CFA_def_cfa:
3556               case DW_CFA_def_cfa_sf:
3557                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3558                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3559                 break;
3560               default:
3561                 gcc_unreachable ();
3562               }
3563             cfi_cfa = &cfi_buf;
3564           }
3565         else if (cfi_cfa_offset)
3566           cfi_cfa = cfi_cfa_offset;
3567         if (cfi_cfa)
3568           {
3569             if (do_cfi_asm)
3570               output_cfi_directive (cfi_cfa);
3571             else
3572               output_cfi (cfi_cfa, fde, for_eh);
3573           }
3574         cfi_cfa = NULL;
3575         cfi_cfa_offset = NULL;
3576         if (cfi_args_size
3577             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3578           {
3579             if (do_cfi_asm)
3580               output_cfi_directive (cfi_args_size);
3581             else
3582               output_cfi (cfi_args_size, fde, for_eh);
3583           }
3584         cfi_args_size = NULL;
3585         if (cfi == NULL)
3586           {
3587             VEC_free (dw_cfi_ref, heap, regs);
3588             return;
3589           }
3590         else if (do_cfi_asm)
3591           output_cfi_directive (cfi);
3592         else
3593           output_cfi (cfi, fde, for_eh);
3594         break;
3595       default:
3596         gcc_unreachable ();
3597     }
3598 }
3599
3600 /* Output one FDE.  */
3601
3602 static void
3603 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3604             char *section_start_label, int fde_encoding, char *augmentation,
3605             bool any_lsda_needed, int lsda_encoding)
3606 {
3607   const char *begin, *end;
3608   static unsigned int j;
3609   char l1[20], l2[20];
3610   dw_cfi_ref cfi;
3611
3612   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3613                                      /* empty */ 0);
3614   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3615                                   for_eh + j);
3616   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3617   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3618   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3619     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3620                          " indicating 64-bit DWARF extension");
3621   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3622                         "FDE Length");
3623   ASM_OUTPUT_LABEL (asm_out_file, l1);
3624
3625   if (for_eh)
3626     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3627   else
3628     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3629                            debug_frame_section, "FDE CIE offset");
3630
3631   if (!fde->dw_fde_switched_sections)
3632     {
3633       begin = fde->dw_fde_begin;
3634       end = fde->dw_fde_end;
3635     }
3636   else
3637     {
3638       /* For the first section, prefer dw_fde_begin over
3639          dw_fde_{hot,cold}_section_label, as the latter
3640          might be separated from the real start of the
3641          function by alignment padding.  */
3642       if (!second)
3643         begin = fde->dw_fde_begin;
3644       else if (fde->dw_fde_switched_cold_to_hot)
3645         begin = fde->dw_fde_hot_section_label;
3646       else
3647         begin = fde->dw_fde_unlikely_section_label;
3648       if (second ^ fde->dw_fde_switched_cold_to_hot)
3649         end = fde->dw_fde_unlikely_section_end_label;
3650       else
3651         end = fde->dw_fde_hot_section_end_label;
3652     }
3653
3654   if (for_eh)
3655     {
3656       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3657       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3658       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3659                                        "FDE initial location");
3660       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3661                             end, begin, "FDE address range");
3662     }
3663   else
3664     {
3665       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3666       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3667     }
3668
3669   if (augmentation[0])
3670     {
3671       if (any_lsda_needed)
3672         {
3673           int size = size_of_encoded_value (lsda_encoding);
3674
3675           if (lsda_encoding == DW_EH_PE_aligned)
3676             {
3677               int offset = (  4         /* Length */
3678                             + 4         /* CIE offset */
3679                             + 2 * size_of_encoded_value (fde_encoding)
3680                             + 1         /* Augmentation size */ );
3681               int pad = -offset & (PTR_SIZE - 1);
3682
3683               size += pad;
3684               gcc_assert (size_of_uleb128 (size) == 1);
3685             }
3686
3687           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3688
3689           if (fde->uses_eh_lsda)
3690             {
3691               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3692                                            fde->funcdef_number);
3693               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3694                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3695                                                false,
3696                                                "Language Specific Data Area");
3697             }
3698           else
3699             {
3700               if (lsda_encoding == DW_EH_PE_aligned)
3701                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3702               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3703                                    "Language Specific Data Area (none)");
3704             }
3705         }
3706       else
3707         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3708     }
3709
3710   /* Loop through the Call Frame Instructions associated with
3711      this FDE.  */
3712   fde->dw_fde_current_label = begin;
3713   if (!fde->dw_fde_switched_sections)
3714     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3715       output_cfi (cfi, fde, for_eh);
3716   else if (!second)
3717     {
3718       if (fde->dw_fde_switch_cfi)
3719         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3720           {
3721             output_cfi (cfi, fde, for_eh);
3722             if (cfi == fde->dw_fde_switch_cfi)
3723               break;
3724           }
3725     }
3726   else
3727     {
3728       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3729
3730       if (fde->dw_fde_switch_cfi)
3731         {
3732           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3733           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3734           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3735           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3736         }
3737       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3738         output_cfi (cfi, fde, for_eh);
3739     }
3740
3741   /* If we are to emit a ref/link from function bodies to their frame tables,
3742      do it now.  This is typically performed to make sure that tables
3743      associated with functions are dragged with them and not discarded in
3744      garbage collecting links. We need to do this on a per function basis to
3745      cope with -ffunction-sections.  */
3746
3747 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3748   /* Switch to the function section, emit the ref to the tables, and
3749      switch *back* into the table section.  */
3750   switch_to_section (function_section (fde->decl));
3751   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3752   switch_to_frame_table_section (for_eh, true);
3753 #endif
3754
3755   /* Pad the FDE out to an address sized boundary.  */
3756   ASM_OUTPUT_ALIGN (asm_out_file,
3757                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3758   ASM_OUTPUT_LABEL (asm_out_file, l2);
3759
3760   j += 2;
3761 }
3762
3763 /* Return true if frame description entry FDE is needed for EH.  */
3764
3765 static bool
3766 fde_needed_for_eh_p (dw_fde_ref fde)
3767 {
3768   if (flag_asynchronous_unwind_tables)
3769     return true;
3770
3771   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3772     return true;
3773
3774   if (fde->uses_eh_lsda)
3775     return true;
3776
3777   /* If exceptions are enabled, we have collected nothrow info.  */
3778   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3779     return false;
3780
3781   return true;
3782 }
3783
3784 /* Output the call frame information used to record information
3785    that relates to calculating the frame pointer, and records the
3786    location of saved registers.  */
3787
3788 static void
3789 output_call_frame_info (int for_eh)
3790 {
3791   unsigned int i;
3792   dw_fde_ref fde;
3793   dw_cfi_ref cfi;
3794   char l1[20], l2[20], section_start_label[20];
3795   bool any_lsda_needed = false;
3796   char augmentation[6];
3797   int augmentation_size;
3798   int fde_encoding = DW_EH_PE_absptr;
3799   int per_encoding = DW_EH_PE_absptr;
3800   int lsda_encoding = DW_EH_PE_absptr;
3801   int return_reg;
3802   rtx personality = NULL;
3803   int dw_cie_version;
3804
3805   /* Don't emit a CIE if there won't be any FDEs.  */
3806   if (fde_table_in_use == 0)
3807     return;
3808
3809   /* Nothing to do if the assembler's doing it all.  */
3810   if (dwarf2out_do_cfi_asm ())
3811     return;
3812
3813   /* If we don't have any functions we'll want to unwind out of, don't emit
3814      any EH unwind information.  If we make FDEs linkonce, we may have to
3815      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3816      want to avoid having an FDE kept around when the function it refers to
3817      is discarded.  Example where this matters: a primary function template
3818      in C++ requires EH information, an explicit specialization doesn't.  */
3819   if (for_eh)
3820     {
3821       bool any_eh_needed = false;
3822
3823       for (i = 0; i < fde_table_in_use; i++)
3824         if (fde_table[i].uses_eh_lsda)
3825           any_eh_needed = any_lsda_needed = true;
3826         else if (fde_needed_for_eh_p (&fde_table[i]))
3827           any_eh_needed = true;
3828         else if (TARGET_USES_WEAK_UNWIND_INFO)
3829           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3830                                              1, 1);
3831
3832       if (!any_eh_needed)
3833         return;
3834     }
3835
3836   /* We're going to be generating comments, so turn on app.  */
3837   if (flag_debug_asm)
3838     app_enable ();
3839
3840   /* Switch to the proper frame section, first time.  */
3841   switch_to_frame_table_section (for_eh, false);
3842
3843   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3844   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3845
3846   /* Output the CIE.  */
3847   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3848   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3849   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3850     dw2_asm_output_data (4, 0xffffffff,
3851       "Initial length escape value indicating 64-bit DWARF extension");
3852   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3853                         "Length of Common Information Entry");
3854   ASM_OUTPUT_LABEL (asm_out_file, l1);
3855
3856   /* Now that the CIE pointer is PC-relative for EH,
3857      use 0 to identify the CIE.  */
3858   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3859                        (for_eh ? 0 : DWARF_CIE_ID),
3860                        "CIE Identifier Tag");
3861
3862   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3863      use CIE version 1, unless that would produce incorrect results
3864      due to overflowing the return register column.  */
3865   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3866   dw_cie_version = 1;
3867   if (return_reg >= 256 || dwarf_version > 2)
3868     dw_cie_version = 3;
3869   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3870
3871   augmentation[0] = 0;
3872   augmentation_size = 0;
3873
3874   personality = current_unit_personality;
3875   if (for_eh)
3876     {
3877       char *p;
3878
3879       /* Augmentation:
3880          z      Indicates that a uleb128 is present to size the
3881                 augmentation section.
3882          L      Indicates the encoding (and thus presence) of
3883                 an LSDA pointer in the FDE augmentation.
3884          R      Indicates a non-default pointer encoding for
3885                 FDE code pointers.
3886          P      Indicates the presence of an encoding + language
3887                 personality routine in the CIE augmentation.  */
3888
3889       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3890       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3891       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3892
3893       p = augmentation + 1;
3894       if (personality)
3895         {
3896           *p++ = 'P';
3897           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3898           assemble_external_libcall (personality);
3899         }
3900       if (any_lsda_needed)
3901         {
3902           *p++ = 'L';
3903           augmentation_size += 1;
3904         }
3905       if (fde_encoding != DW_EH_PE_absptr)
3906         {
3907           *p++ = 'R';
3908           augmentation_size += 1;
3909         }
3910       if (p > augmentation + 1)
3911         {
3912           augmentation[0] = 'z';
3913           *p = '\0';
3914         }
3915
3916       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3917       if (personality && per_encoding == DW_EH_PE_aligned)
3918         {
3919           int offset = (  4             /* Length */
3920                         + 4             /* CIE Id */
3921                         + 1             /* CIE version */
3922                         + strlen (augmentation) + 1     /* Augmentation */
3923                         + size_of_uleb128 (1)           /* Code alignment */
3924                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3925                         + 1             /* RA column */
3926                         + 1             /* Augmentation size */
3927                         + 1             /* Personality encoding */ );
3928           int pad = -offset & (PTR_SIZE - 1);
3929
3930           augmentation_size += pad;
3931
3932           /* Augmentations should be small, so there's scarce need to
3933              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3934           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3935         }
3936     }
3937
3938   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3939   if (dw_cie_version >= 4)
3940     {
3941       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3942       dw2_asm_output_data (1, 0, "CIE Segment Size");
3943     }
3944   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3945   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3946                                "CIE Data Alignment Factor");
3947
3948   if (dw_cie_version == 1)
3949     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3950   else
3951     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3952
3953   if (augmentation[0])
3954     {
3955       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3956       if (personality)
3957         {
3958           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3959                                eh_data_format_name (per_encoding));
3960           dw2_asm_output_encoded_addr_rtx (per_encoding,
3961                                            personality,
3962                                            true, NULL);
3963         }
3964
3965       if (any_lsda_needed)
3966         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3967                              eh_data_format_name (lsda_encoding));
3968
3969       if (fde_encoding != DW_EH_PE_absptr)
3970         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3971                              eh_data_format_name (fde_encoding));
3972     }
3973
3974   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3975     output_cfi (cfi, NULL, for_eh);
3976
3977   /* Pad the CIE out to an address sized boundary.  */
3978   ASM_OUTPUT_ALIGN (asm_out_file,
3979                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3980   ASM_OUTPUT_LABEL (asm_out_file, l2);
3981
3982   /* Loop through all of the FDE's.  */
3983   for (i = 0; i < fde_table_in_use; i++)
3984     {
3985       unsigned int k;
3986       fde = &fde_table[i];
3987
3988       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3989       if (for_eh && !fde_needed_for_eh_p (fde))
3990         continue;
3991
3992       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3993         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3994                     augmentation, any_lsda_needed, lsda_encoding);
3995     }
3996
3997   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3998     dw2_asm_output_data (4, 0, "End of Table");
3999 #ifdef MIPS_DEBUGGING_INFO
4000   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4001      get a value of 0.  Putting .align 0 after the label fixes it.  */
4002   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4003 #endif
4004
4005   /* Turn off app to make assembly quicker.  */
4006   if (flag_debug_asm)
4007     app_disable ();
4008 }
4009
4010 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4011
4012 static void
4013 dwarf2out_do_cfi_startproc (bool second)
4014 {
4015   int enc;
4016   rtx ref;
4017   rtx personality = get_personality_function (current_function_decl);
4018
4019   fprintf (asm_out_file, "\t.cfi_startproc\n");
4020
4021   if (personality)
4022     {
4023       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4024       ref = personality;
4025
4026       /* ??? The GAS support isn't entirely consistent.  We have to
4027          handle indirect support ourselves, but PC-relative is done
4028          in the assembler.  Further, the assembler can't handle any
4029          of the weirder relocation types.  */
4030       if (enc & DW_EH_PE_indirect)
4031         ref = dw2_force_const_mem (ref, true);
4032
4033       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4034       output_addr_const (asm_out_file, ref);
4035       fputc ('\n', asm_out_file);
4036     }
4037
4038   if (crtl->uses_eh_lsda)
4039     {
4040       char lab[20];
4041
4042       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4043       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4044                                    current_function_funcdef_no);
4045       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4046       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4047
4048       if (enc & DW_EH_PE_indirect)
4049         ref = dw2_force_const_mem (ref, true);
4050
4051       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4052       output_addr_const (asm_out_file, ref);
4053       fputc ('\n', asm_out_file);
4054     }
4055 }
4056
4057 /* Output a marker (i.e. a label) for the beginning of a function, before
4058    the prologue.  */
4059
4060 void
4061 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4062                           const char *file ATTRIBUTE_UNUSED)
4063 {
4064   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4065   char * dup_label;
4066   dw_fde_ref fde;
4067   section *fnsec;
4068   bool do_frame;
4069
4070   current_function_func_begin_label = NULL;
4071
4072   do_frame = dwarf2out_do_frame ();
4073
4074   /* ??? current_function_func_begin_label is also used by except.c for
4075      call-site information.  We must emit this label if it might be used.  */
4076   if (!do_frame
4077       && (!flag_exceptions
4078           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4079     return;
4080
4081   fnsec = function_section (current_function_decl);
4082   switch_to_section (fnsec);
4083   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4084                                current_function_funcdef_no);
4085   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4086                           current_function_funcdef_no);
4087   dup_label = xstrdup (label);
4088   current_function_func_begin_label = dup_label;
4089
4090   /* We can elide the fde allocation if we're not emitting debug info.  */
4091   if (!do_frame)
4092     return;
4093
4094   /* Expand the fde table if necessary.  */
4095   if (fde_table_in_use == fde_table_allocated)
4096     {
4097       fde_table_allocated += FDE_TABLE_INCREMENT;
4098       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4099       memset (fde_table + fde_table_in_use, 0,
4100               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4101     }
4102
4103   /* Record the FDE associated with this function.  */
4104   current_funcdef_fde = fde_table_in_use;
4105
4106   /* Add the new FDE at the end of the fde_table.  */
4107   fde = &fde_table[fde_table_in_use++];
4108   fde->decl = current_function_decl;
4109   fde->dw_fde_begin = dup_label;
4110   fde->dw_fde_current_label = dup_label;
4111   fde->dw_fde_hot_section_label = NULL;
4112   fde->dw_fde_hot_section_end_label = NULL;
4113   fde->dw_fde_unlikely_section_label = NULL;
4114   fde->dw_fde_unlikely_section_end_label = NULL;
4115   fde->dw_fde_switched_sections = 0;
4116   fde->dw_fde_switched_cold_to_hot = 0;
4117   fde->dw_fde_end = NULL;
4118   fde->dw_fde_vms_end_prologue = NULL;
4119   fde->dw_fde_vms_begin_epilogue = NULL;
4120   fde->dw_fde_cfi = NULL;
4121   fde->dw_fde_switch_cfi = NULL;
4122   fde->funcdef_number = current_function_funcdef_no;
4123   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4124   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4125   fde->nothrow = crtl->nothrow;
4126   fde->drap_reg = INVALID_REGNUM;
4127   fde->vdrap_reg = INVALID_REGNUM;
4128   if (flag_reorder_blocks_and_partition)
4129     {
4130       section *unlikelysec;
4131       if (first_function_block_is_cold)
4132         fde->in_std_section = 1;
4133       else
4134         fde->in_std_section
4135           = (fnsec == text_section
4136              || (cold_text_section && fnsec == cold_text_section));
4137       unlikelysec = unlikely_text_section ();
4138       fde->cold_in_std_section
4139         = (unlikelysec == text_section
4140            || (cold_text_section && unlikelysec == cold_text_section));
4141     }
4142   else
4143     {
4144       fde->in_std_section
4145         = (fnsec == text_section
4146            || (cold_text_section && fnsec == cold_text_section));
4147       fde->cold_in_std_section = 0;
4148     }
4149
4150   args_size = old_args_size = 0;
4151
4152   /* We only want to output line number information for the genuine dwarf2
4153      prologue case, not the eh frame case.  */
4154 #ifdef DWARF2_DEBUGGING_INFO
4155   if (file)
4156     dwarf2out_source_line (line, file, 0, true);
4157 #endif
4158
4159   if (dwarf2out_do_cfi_asm ())
4160     dwarf2out_do_cfi_startproc (false);
4161   else
4162     {
4163       rtx personality = get_personality_function (current_function_decl);
4164       if (!current_unit_personality)
4165         current_unit_personality = personality;
4166
4167       /* We cannot keep a current personality per function as without CFI
4168          asm, at the point where we emit the CFI data, there is no current
4169          function anymore.  */
4170       if (personality && current_unit_personality != personality)
4171         sorry ("multiple EH personalities are supported only with assemblers "
4172                "supporting .cfi_personality directive");
4173     }
4174 }
4175
4176 /* Output a marker (i.e. a label) for the end of the generated code
4177    for a function prologue.  This gets called *after* the prologue code has
4178    been generated.  */
4179
4180 void
4181 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4182                         const char *file ATTRIBUTE_UNUSED)
4183 {
4184   dw_fde_ref fde;
4185   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4186
4187   /* Output a label to mark the endpoint of the code generated for this
4188      function.  */
4189   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4190                                current_function_funcdef_no);
4191   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4192                           current_function_funcdef_no);
4193   fde = &fde_table[fde_table_in_use - 1];
4194   fde->dw_fde_vms_end_prologue = xstrdup (label);
4195 }
4196
4197 /* Output a marker (i.e. a label) for the beginning of the generated code
4198    for a function epilogue.  This gets called *before* the prologue code has
4199    been generated.  */
4200
4201 void
4202 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4203                           const char *file ATTRIBUTE_UNUSED)
4204 {
4205   dw_fde_ref fde;
4206   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4207
4208   fde = &fde_table[fde_table_in_use - 1];
4209   if (fde->dw_fde_vms_begin_epilogue)
4210     return;
4211
4212   /* Output a label to mark the endpoint of the code generated for this
4213      function.  */
4214   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4215                                current_function_funcdef_no);
4216   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4217                           current_function_funcdef_no);
4218   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4219 }
4220
4221 /* Output a marker (i.e. a label) for the absolute end of the generated code
4222    for a function definition.  This gets called *after* the epilogue code has
4223    been generated.  */
4224
4225 void
4226 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4227                         const char *file ATTRIBUTE_UNUSED)
4228 {
4229   dw_fde_ref fde;
4230   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4231
4232   last_var_location_insn = NULL_RTX;
4233
4234   if (dwarf2out_do_cfi_asm ())
4235     fprintf (asm_out_file, "\t.cfi_endproc\n");
4236
4237   /* Output a label to mark the endpoint of the code generated for this
4238      function.  */
4239   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4240                                current_function_funcdef_no);
4241   ASM_OUTPUT_LABEL (asm_out_file, label);
4242   fde = current_fde ();
4243   gcc_assert (fde != NULL);
4244   fde->dw_fde_end = xstrdup (label);
4245 }
4246
4247 void
4248 dwarf2out_frame_init (void)
4249 {
4250   /* Allocate the initial hunk of the fde_table.  */
4251   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4252   fde_table_allocated = FDE_TABLE_INCREMENT;
4253   fde_table_in_use = 0;
4254
4255   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4256      sake of lookup_cfa.  */
4257
4258   /* On entry, the Canonical Frame Address is at SP.  */
4259   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4260
4261   if (targetm.debug_unwind_info () == UI_DWARF2
4262       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4263     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4264 }
4265
4266 void
4267 dwarf2out_frame_finish (void)
4268 {
4269   /* Output call frame information.  */
4270   if (targetm.debug_unwind_info () == UI_DWARF2)
4271     output_call_frame_info (0);
4272
4273   /* Output another copy for the unwinder.  */
4274   if ((flag_unwind_tables || flag_exceptions)
4275       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4276     output_call_frame_info (1);
4277 }
4278
4279 /* Note that the current function section is being used for code.  */
4280
4281 static void
4282 dwarf2out_note_section_used (void)
4283 {
4284   section *sec = current_function_section ();
4285   if (sec == text_section)
4286     text_section_used = true;
4287   else if (sec == cold_text_section)
4288     cold_text_section_used = true;
4289 }
4290
4291 void
4292 dwarf2out_switch_text_section (void)
4293 {
4294   dw_fde_ref fde = current_fde ();
4295
4296   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4297
4298   fde->dw_fde_switched_sections = 1;
4299   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4300
4301   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4302   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4303   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4304   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4305   have_multiple_function_sections = true;
4306
4307   /* Reset the current label on switching text sections, so that we
4308      don't attempt to advance_loc4 between labels in different sections.  */
4309   fde->dw_fde_current_label = NULL;
4310
4311   /* There is no need to mark used sections when not debugging.  */
4312   if (cold_text_section != NULL)
4313     dwarf2out_note_section_used ();
4314
4315   if (dwarf2out_do_cfi_asm ())
4316     fprintf (asm_out_file, "\t.cfi_endproc\n");
4317
4318   /* Now do the real section switch.  */
4319   switch_to_section (current_function_section ());
4320
4321   if (dwarf2out_do_cfi_asm ())
4322     {
4323       dwarf2out_do_cfi_startproc (true);
4324       /* As this is a different FDE, insert all current CFI instructions
4325          again.  */
4326       output_cfis (fde->dw_fde_cfi, true, fde, true);
4327     }
4328   else
4329     {
4330       dw_cfi_ref cfi = fde->dw_fde_cfi;
4331
4332       cfi = fde->dw_fde_cfi;
4333       if (cfi)
4334         while (cfi->dw_cfi_next != NULL)
4335           cfi = cfi->dw_cfi_next;
4336       fde->dw_fde_switch_cfi = cfi;
4337     }
4338 }
4339 \f
4340 /* And now, the subset of the debugging information support code necessary
4341    for emitting location expressions.  */
4342
4343 /* Data about a single source file.  */
4344 struct GTY(()) dwarf_file_data {
4345   const char * filename;
4346   int emitted_number;
4347 };
4348
4349 typedef struct dw_val_struct *dw_val_ref;
4350 typedef struct die_struct *dw_die_ref;
4351 typedef const struct die_struct *const_dw_die_ref;
4352 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4353 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4354
4355 typedef struct GTY(()) deferred_locations_struct
4356 {
4357   tree variable;
4358   dw_die_ref die;
4359 } deferred_locations;
4360
4361 DEF_VEC_O(deferred_locations);
4362 DEF_VEC_ALLOC_O(deferred_locations,gc);
4363
4364 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4365
4366 DEF_VEC_P(dw_die_ref);
4367 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4368
4369 /* Each DIE may have a series of attribute/value pairs.  Values
4370    can take on several forms.  The forms that are used in this
4371    implementation are listed below.  */
4372
4373 enum dw_val_class
4374 {
4375   dw_val_class_addr,
4376   dw_val_class_offset,
4377   dw_val_class_loc,
4378   dw_val_class_loc_list,
4379   dw_val_class_range_list,
4380   dw_val_class_const,
4381   dw_val_class_unsigned_const,
4382   dw_val_class_const_double,
4383   dw_val_class_vec,
4384   dw_val_class_flag,
4385   dw_val_class_die_ref,
4386   dw_val_class_fde_ref,
4387   dw_val_class_lbl_id,
4388   dw_val_class_lineptr,
4389   dw_val_class_str,
4390   dw_val_class_macptr,
4391   dw_val_class_file,
4392   dw_val_class_data8,
4393   dw_val_class_decl_ref,
4394   dw_val_class_vms_delta
4395 };
4396
4397 /* Describe a floating point constant value, or a vector constant value.  */
4398
4399 typedef struct GTY(()) dw_vec_struct {
4400   unsigned char * GTY((length ("%h.length"))) array;
4401   unsigned length;
4402   unsigned elt_size;
4403 }
4404 dw_vec_const;
4405
4406 /* The dw_val_node describes an attribute's value, as it is
4407    represented internally.  */
4408
4409 typedef struct GTY(()) dw_val_struct {
4410   enum dw_val_class val_class;
4411   union dw_val_struct_union
4412     {
4413       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4414       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4415       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4416       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4417       HOST_WIDE_INT GTY ((default)) val_int;
4418       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4419       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4420       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4421       struct dw_val_die_union
4422         {
4423           dw_die_ref die;
4424           int external;
4425         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4426       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4427       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4428       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4429       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4430       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4431       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4432       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4433       struct dw_val_vms_delta_union
4434         {
4435           char * lbl1;
4436           char * lbl2;
4437         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4438     }
4439   GTY ((desc ("%1.val_class"))) v;
4440 }
4441 dw_val_node;
4442
4443 /* Locations in memory are described using a sequence of stack machine
4444    operations.  */
4445
4446 typedef struct GTY(()) dw_loc_descr_struct {
4447   dw_loc_descr_ref dw_loc_next;
4448   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4449   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4450      from DW_OP_addr with a dtp-relative symbol relocation.  */
4451   unsigned int dtprel : 1;
4452   int dw_loc_addr;
4453   dw_val_node dw_loc_oprnd1;
4454   dw_val_node dw_loc_oprnd2;
4455 }
4456 dw_loc_descr_node;
4457
4458 /* Location lists are ranges + location descriptions for that range,
4459    so you can track variables that are in different places over
4460    their entire life.  */
4461 typedef struct GTY(()) dw_loc_list_struct {
4462   dw_loc_list_ref dw_loc_next;
4463   const char *begin; /* Label for begin address of range */
4464   const char *end;  /* Label for end address of range */
4465   char *ll_symbol; /* Label for beginning of location list.
4466                       Only on head of list */
4467   const char *section; /* Section this loclist is relative to */
4468   dw_loc_descr_ref expr;
4469   hashval_t hash;
4470   bool emitted;
4471 } dw_loc_list_node;
4472
4473 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4474
4475 /* Convert a DWARF stack opcode into its string name.  */
4476
4477 static const char *
4478 dwarf_stack_op_name (unsigned int op)
4479 {
4480   switch (op)
4481     {
4482     case DW_OP_addr:
4483       return "DW_OP_addr";
4484     case DW_OP_deref:
4485       return "DW_OP_deref";
4486     case DW_OP_const1u:
4487       return "DW_OP_const1u";
4488     case DW_OP_const1s:
4489       return "DW_OP_const1s";
4490     case DW_OP_const2u:
4491       return "DW_OP_const2u";
4492     case DW_OP_const2s:
4493       return "DW_OP_const2s";
4494     case DW_OP_const4u:
4495       return "DW_OP_const4u";
4496     case DW_OP_const4s:
4497       return "DW_OP_const4s";
4498     case DW_OP_const8u:
4499       return "DW_OP_const8u";
4500     case DW_OP_const8s:
4501       return "DW_OP_const8s";
4502     case DW_OP_constu:
4503       return "DW_OP_constu";
4504     case DW_OP_consts:
4505       return "DW_OP_consts";
4506     case DW_OP_dup:
4507       return "DW_OP_dup";
4508     case DW_OP_drop:
4509       return "DW_OP_drop";
4510     case DW_OP_over:
4511       return "DW_OP_over";
4512     case DW_OP_pick:
4513       return "DW_OP_pick";
4514     case DW_OP_swap:
4515       return "DW_OP_swap";
4516     case DW_OP_rot:
4517       return "DW_OP_rot";
4518     case DW_OP_xderef:
4519       return "DW_OP_xderef";
4520     case DW_OP_abs:
4521       return "DW_OP_abs";
4522     case DW_OP_and:
4523       return "DW_OP_and";
4524     case DW_OP_div:
4525       return "DW_OP_div";
4526     case DW_OP_minus:
4527       return "DW_OP_minus";
4528     case DW_OP_mod:
4529       return "DW_OP_mod";
4530     case DW_OP_mul:
4531       return "DW_OP_mul";
4532     case DW_OP_neg:
4533       return "DW_OP_neg";
4534     case DW_OP_not:
4535       return "DW_OP_not";
4536     case DW_OP_or:
4537       return "DW_OP_or";
4538     case DW_OP_plus:
4539       return "DW_OP_plus";
4540     case DW_OP_plus_uconst:
4541       return "DW_OP_plus_uconst";
4542     case DW_OP_shl:
4543       return "DW_OP_shl";
4544     case DW_OP_shr:
4545       return "DW_OP_shr";
4546     case DW_OP_shra:
4547       return "DW_OP_shra";
4548     case DW_OP_xor:
4549       return "DW_OP_xor";
4550     case DW_OP_bra:
4551       return "DW_OP_bra";
4552     case DW_OP_eq:
4553       return "DW_OP_eq";
4554     case DW_OP_ge:
4555       return "DW_OP_ge";
4556     case DW_OP_gt:
4557       return "DW_OP_gt";
4558     case DW_OP_le:
4559       return "DW_OP_le";
4560     case DW_OP_lt:
4561       return "DW_OP_lt";
4562     case DW_OP_ne:
4563       return "DW_OP_ne";
4564     case DW_OP_skip:
4565       return "DW_OP_skip";
4566     case DW_OP_lit0:
4567       return "DW_OP_lit0";
4568     case DW_OP_lit1:
4569       return "DW_OP_lit1";
4570     case DW_OP_lit2:
4571       return "DW_OP_lit2";
4572     case DW_OP_lit3:
4573       return "DW_OP_lit3";
4574     case DW_OP_lit4:
4575       return "DW_OP_lit4";
4576     case DW_OP_lit5:
4577       return "DW_OP_lit5";
4578     case DW_OP_lit6:
4579       return "DW_OP_lit6";
4580     case DW_OP_lit7:
4581       return "DW_OP_lit7";
4582     case DW_OP_lit8:
4583       return "DW_OP_lit8";
4584     case DW_OP_lit9:
4585       return "DW_OP_lit9";
4586     case DW_OP_lit10:
4587       return "DW_OP_lit10";
4588     case DW_OP_lit11:
4589       return "DW_OP_lit11";
4590     case DW_OP_lit12:
4591       return "DW_OP_lit12";
4592     case DW_OP_lit13:
4593       return "DW_OP_lit13";
4594     case DW_OP_lit14:
4595       return "DW_OP_lit14";
4596     case DW_OP_lit15:
4597       return "DW_OP_lit15";
4598     case DW_OP_lit16:
4599       return "DW_OP_lit16";
4600     case DW_OP_lit17:
4601       return "DW_OP_lit17";
4602     case DW_OP_lit18:
4603       return "DW_OP_lit18";
4604     case DW_OP_lit19:
4605       return "DW_OP_lit19";
4606     case DW_OP_lit20:
4607       return "DW_OP_lit20";
4608     case DW_OP_lit21:
4609       return "DW_OP_lit21";
4610     case DW_OP_lit22:
4611       return "DW_OP_lit22";
4612     case DW_OP_lit23:
4613       return "DW_OP_lit23";
4614     case DW_OP_lit24:
4615       return "DW_OP_lit24";
4616     case DW_OP_lit25:
4617       return "DW_OP_lit25";
4618     case DW_OP_lit26:
4619       return "DW_OP_lit26";
4620     case DW_OP_lit27:
4621       return "DW_OP_lit27";
4622     case DW_OP_lit28:
4623       return "DW_OP_lit28";
4624     case DW_OP_lit29:
4625       return "DW_OP_lit29";
4626     case DW_OP_lit30:
4627       return "DW_OP_lit30";
4628     case DW_OP_lit31:
4629       return "DW_OP_lit31";
4630     case DW_OP_reg0:
4631       return "DW_OP_reg0";
4632     case DW_OP_reg1:
4633       return "DW_OP_reg1";
4634     case DW_OP_reg2:
4635       return "DW_OP_reg2";
4636     case DW_OP_reg3:
4637       return "DW_OP_reg3";
4638     case DW_OP_reg4:
4639       return "DW_OP_reg4";
4640     case DW_OP_reg5:
4641       return "DW_OP_reg5";
4642     case DW_OP_reg6:
4643       return "DW_OP_reg6";
4644     case DW_OP_reg7:
4645       return "DW_OP_reg7";
4646     case DW_OP_reg8:
4647       return "DW_OP_reg8";
4648     case DW_OP_reg9:
4649       return "DW_OP_reg9";
4650     case DW_OP_reg10:
4651       return "DW_OP_reg10";
4652     case DW_OP_reg11:
4653       return "DW_OP_reg11";
4654     case DW_OP_reg12:
4655       return "DW_OP_reg12";
4656     case DW_OP_reg13:
4657       return "DW_OP_reg13";
4658     case DW_OP_reg14:
4659       return "DW_OP_reg14";
4660     case DW_OP_reg15:
4661       return "DW_OP_reg15";
4662     case DW_OP_reg16:
4663       return "DW_OP_reg16";
4664     case DW_OP_reg17:
4665       return "DW_OP_reg17";
4666     case DW_OP_reg18:
4667       return "DW_OP_reg18";
4668     case DW_OP_reg19:
4669       return "DW_OP_reg19";
4670     case DW_OP_reg20:
4671       return "DW_OP_reg20";
4672     case DW_OP_reg21:
4673       return "DW_OP_reg21";
4674     case DW_OP_reg22:
4675       return "DW_OP_reg22";
4676     case DW_OP_reg23:
4677       return "DW_OP_reg23";
4678     case DW_OP_reg24:
4679       return "DW_OP_reg24";
4680     case DW_OP_reg25:
4681       return "DW_OP_reg25";
4682     case DW_OP_reg26:
4683       return "DW_OP_reg26";
4684     case DW_OP_reg27:
4685       return "DW_OP_reg27";
4686     case DW_OP_reg28:
4687       return "DW_OP_reg28";
4688     case DW_OP_reg29:
4689       return "DW_OP_reg29";
4690     case DW_OP_reg30:
4691       return "DW_OP_reg30";
4692     case DW_OP_reg31:
4693       return "DW_OP_reg31";
4694     case DW_OP_breg0:
4695       return "DW_OP_breg0";
4696     case DW_OP_breg1:
4697       return "DW_OP_breg1";
4698     case DW_OP_breg2:
4699       return "DW_OP_breg2";
4700     case DW_OP_breg3:
4701       return "DW_OP_breg3";
4702     case DW_OP_breg4:
4703       return "DW_OP_breg4";
4704     case DW_OP_breg5:
4705       return "DW_OP_breg5";
4706     case DW_OP_breg6:
4707       return "DW_OP_breg6";
4708     case DW_OP_breg7:
4709       return "DW_OP_breg7";
4710     case DW_OP_breg8:
4711       return "DW_OP_breg8";
4712     case DW_OP_breg9:
4713       return "DW_OP_breg9";
4714     case DW_OP_breg10:
4715       return "DW_OP_breg10";
4716     case DW_OP_breg11:
4717       return "DW_OP_breg11";
4718     case DW_OP_breg12:
4719       return "DW_OP_breg12";
4720     case DW_OP_breg13:
4721       return "DW_OP_breg13";
4722     case DW_OP_breg14:
4723       return "DW_OP_breg14";
4724     case DW_OP_breg15:
4725       return "DW_OP_breg15";
4726     case DW_OP_breg16:
4727       return "DW_OP_breg16";
4728     case DW_OP_breg17:
4729       return "DW_OP_breg17";
4730     case DW_OP_breg18:
4731       return "DW_OP_breg18";
4732     case DW_OP_breg19:
4733       return "DW_OP_breg19";
4734     case DW_OP_breg20:
4735       return "DW_OP_breg20";
4736     case DW_OP_breg21:
4737       return "DW_OP_breg21";
4738     case DW_OP_breg22:
4739       return "DW_OP_breg22";
4740     case DW_OP_breg23:
4741       return "DW_OP_breg23";
4742     case DW_OP_breg24:
4743       return "DW_OP_breg24";
4744     case DW_OP_breg25:
4745       return "DW_OP_breg25";
4746     case DW_OP_breg26:
4747       return "DW_OP_breg26";
4748     case DW_OP_breg27:
4749       return "DW_OP_breg27";
4750     case DW_OP_breg28:
4751       return "DW_OP_breg28";
4752     case DW_OP_breg29:
4753       return "DW_OP_breg29";
4754     case DW_OP_breg30:
4755       return "DW_OP_breg30";
4756     case DW_OP_breg31:
4757       return "DW_OP_breg31";
4758     case DW_OP_regx:
4759       return "DW_OP_regx";
4760     case DW_OP_fbreg:
4761       return "DW_OP_fbreg";
4762     case DW_OP_bregx:
4763       return "DW_OP_bregx";
4764     case DW_OP_piece:
4765       return "DW_OP_piece";
4766     case DW_OP_deref_size:
4767       return "DW_OP_deref_size";
4768     case DW_OP_xderef_size:
4769       return "DW_OP_xderef_size";
4770     case DW_OP_nop:
4771       return "DW_OP_nop";
4772
4773     case DW_OP_push_object_address:
4774       return "DW_OP_push_object_address";
4775     case DW_OP_call2:
4776       return "DW_OP_call2";
4777     case DW_OP_call4:
4778       return "DW_OP_call4";
4779     case DW_OP_call_ref:
4780       return "DW_OP_call_ref";
4781     case DW_OP_implicit_value:
4782       return "DW_OP_implicit_value";
4783     case DW_OP_stack_value:
4784       return "DW_OP_stack_value";
4785     case DW_OP_form_tls_address:
4786       return "DW_OP_form_tls_address";
4787     case DW_OP_call_frame_cfa:
4788       return "DW_OP_call_frame_cfa";
4789     case DW_OP_bit_piece:
4790       return "DW_OP_bit_piece";
4791
4792     case DW_OP_GNU_push_tls_address:
4793       return "DW_OP_GNU_push_tls_address";
4794     case DW_OP_GNU_uninit:
4795       return "DW_OP_GNU_uninit";
4796     case DW_OP_GNU_encoded_addr:
4797       return "DW_OP_GNU_encoded_addr";
4798     case DW_OP_GNU_implicit_pointer:
4799       return "DW_OP_GNU_implicit_pointer";
4800     case DW_OP_GNU_entry_value:
4801       return "DW_OP_GNU_entry_value";
4802
4803     default:
4804       return "OP_<unknown>";
4805     }
4806 }
4807
4808 /* Return a pointer to a newly allocated location description.  Location
4809    descriptions are simple expression terms that can be strung
4810    together to form more complicated location (address) descriptions.  */
4811
4812 static inline dw_loc_descr_ref
4813 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4814                unsigned HOST_WIDE_INT oprnd2)
4815 {
4816   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4817
4818   descr->dw_loc_opc = op;
4819   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4820   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4821   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4822   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4823
4824   return descr;
4825 }
4826
4827 /* Return a pointer to a newly allocated location description for
4828    REG and OFFSET.  */
4829
4830 static inline dw_loc_descr_ref
4831 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4832 {
4833   if (reg <= 31)
4834     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4835                           offset, 0);
4836   else
4837     return new_loc_descr (DW_OP_bregx, reg, offset);
4838 }
4839
4840 /* Add a location description term to a location description expression.  */
4841
4842 static inline void
4843 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4844 {
4845   dw_loc_descr_ref *d;
4846
4847   /* Find the end of the chain.  */
4848   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4849     ;
4850
4851   *d = descr;
4852 }
4853
4854 /* Add a constant OFFSET to a location expression.  */
4855
4856 static void
4857 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4858 {
4859   dw_loc_descr_ref loc;
4860   HOST_WIDE_INT *p;
4861
4862   gcc_assert (*list_head != NULL);
4863
4864   if (!offset)
4865     return;
4866
4867   /* Find the end of the chain.  */
4868   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4869     ;
4870
4871   p = NULL;
4872   if (loc->dw_loc_opc == DW_OP_fbreg
4873       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4874     p = &loc->dw_loc_oprnd1.v.val_int;
4875   else if (loc->dw_loc_opc == DW_OP_bregx)
4876     p = &loc->dw_loc_oprnd2.v.val_int;
4877
4878   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4879      offset.  Don't optimize if an signed integer overflow would happen.  */
4880   if (p != NULL
4881       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4882           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4883     *p += offset;
4884
4885   else if (offset > 0)
4886     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4887
4888   else
4889     {
4890       loc->dw_loc_next = int_loc_descriptor (-offset);
4891       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4892     }
4893 }
4894
4895 /* Add a constant OFFSET to a location list.  */
4896
4897 static void
4898 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4899 {
4900   dw_loc_list_ref d;
4901   for (d = list_head; d != NULL; d = d->dw_loc_next)
4902     loc_descr_plus_const (&d->expr, offset);
4903 }
4904
4905 #define DWARF_REF_SIZE  \
4906   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4907
4908 static unsigned long size_of_locs (dw_loc_descr_ref);
4909
4910 /* Return the size of a location descriptor.  */
4911
4912 static unsigned long
4913 size_of_loc_descr (dw_loc_descr_ref loc)
4914 {
4915   unsigned long size = 1;
4916
4917   switch (loc->dw_loc_opc)
4918     {
4919     case DW_OP_addr:
4920       size += DWARF2_ADDR_SIZE;
4921       break;
4922     case DW_OP_const1u:
4923     case DW_OP_const1s:
4924       size += 1;
4925       break;
4926     case DW_OP_const2u:
4927     case DW_OP_const2s:
4928       size += 2;
4929       break;
4930     case DW_OP_const4u:
4931     case DW_OP_const4s:
4932       size += 4;
4933       break;
4934     case DW_OP_const8u:
4935     case DW_OP_const8s:
4936       size += 8;
4937       break;
4938     case DW_OP_constu:
4939       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4940       break;
4941     case DW_OP_consts:
4942       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4943       break;
4944     case DW_OP_pick:
4945       size += 1;
4946       break;
4947     case DW_OP_plus_uconst:
4948       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4949       break;
4950     case DW_OP_skip:
4951     case DW_OP_bra:
4952       size += 2;
4953       break;
4954     case DW_OP_breg0:
4955     case DW_OP_breg1:
4956     case DW_OP_breg2:
4957     case DW_OP_breg3:
4958     case DW_OP_breg4:
4959     case DW_OP_breg5:
4960     case DW_OP_breg6:
4961     case DW_OP_breg7:
4962     case DW_OP_breg8:
4963     case DW_OP_breg9:
4964     case DW_OP_breg10:
4965     case DW_OP_breg11:
4966     case DW_OP_breg12:
4967     case DW_OP_breg13:
4968     case DW_OP_breg14:
4969     case DW_OP_breg15:
4970     case DW_OP_breg16:
4971     case DW_OP_breg17:
4972     case DW_OP_breg18:
4973     case DW_OP_breg19:
4974     case DW_OP_breg20:
4975     case DW_OP_breg21:
4976     case DW_OP_breg22:
4977     case DW_OP_breg23:
4978     case DW_OP_breg24:
4979     case DW_OP_breg25:
4980     case DW_OP_breg26:
4981     case DW_OP_breg27:
4982     case DW_OP_breg28:
4983     case DW_OP_breg29:
4984     case DW_OP_breg30:
4985     case DW_OP_breg31:
4986       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4987       break;
4988     case DW_OP_regx:
4989       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4990       break;
4991     case DW_OP_fbreg:
4992       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4993       break;
4994     case DW_OP_bregx:
4995       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4997       break;
4998     case DW_OP_piece:
4999       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5000       break;
5001     case DW_OP_bit_piece:
5002       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5003       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5004       break;
5005     case DW_OP_deref_size:
5006     case DW_OP_xderef_size:
5007       size += 1;
5008       break;
5009     case DW_OP_call2:
5010       size += 2;
5011       break;
5012     case DW_OP_call4:
5013       size += 4;
5014       break;
5015     case DW_OP_call_ref:
5016       size += DWARF_REF_SIZE;
5017       break;
5018     case DW_OP_implicit_value:
5019       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5020               + loc->dw_loc_oprnd1.v.val_unsigned;
5021       break;
5022     case DW_OP_GNU_implicit_pointer:
5023       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5024       break;
5025     case DW_OP_GNU_entry_value:
5026       {
5027         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5028         size += size_of_uleb128 (op_size) + op_size;
5029         break;
5030       }
5031     default:
5032       break;
5033     }
5034
5035   return size;
5036 }
5037
5038 /* Return the size of a series of location descriptors.  */
5039
5040 static unsigned long
5041 size_of_locs (dw_loc_descr_ref loc)
5042 {
5043   dw_loc_descr_ref l;
5044   unsigned long size;
5045
5046   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5047      field, to avoid writing to a PCH file.  */
5048   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5049     {
5050       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5051         break;
5052       size += size_of_loc_descr (l);
5053     }
5054   if (! l)
5055     return size;
5056
5057   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5058     {
5059       l->dw_loc_addr = size;
5060       size += size_of_loc_descr (l);
5061     }
5062
5063   return size;
5064 }
5065
5066 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5067 static void get_ref_die_offset_label (char *, dw_die_ref);
5068 static void output_loc_sequence (dw_loc_descr_ref, int);
5069
5070 /* Output location description stack opcode's operands (if any).
5071    The for_eh_or_skip parameter controls whether register numbers are
5072    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5073    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5074    info).  This should be suppressed for the cases that have not been converted
5075    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5076
5077 static void
5078 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5079 {
5080   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5081   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5082
5083   switch (loc->dw_loc_opc)
5084     {
5085 #ifdef DWARF2_DEBUGGING_INFO
5086     case DW_OP_const2u:
5087     case DW_OP_const2s:
5088       dw2_asm_output_data (2, val1->v.val_int, NULL);
5089       break;
5090     case DW_OP_const4u:
5091       if (loc->dtprel)
5092         {
5093           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5094           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5095                                                val1->v.val_addr);
5096           fputc ('\n', asm_out_file);
5097           break;
5098         }
5099       /* FALLTHRU */
5100     case DW_OP_const4s:
5101       dw2_asm_output_data (4, val1->v.val_int, NULL);
5102       break;
5103     case DW_OP_const8u:
5104       if (loc->dtprel)
5105         {
5106           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5107           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5108                                                val1->v.val_addr);
5109           fputc ('\n', asm_out_file);
5110           break;
5111         }
5112       /* FALLTHRU */
5113     case DW_OP_const8s:
5114       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5115       dw2_asm_output_data (8, val1->v.val_int, NULL);
5116       break;
5117     case DW_OP_skip:
5118     case DW_OP_bra:
5119       {
5120         int offset;
5121
5122         gcc_assert (val1->val_class == dw_val_class_loc);
5123         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5124
5125         dw2_asm_output_data (2, offset, NULL);
5126       }
5127       break;
5128     case DW_OP_implicit_value:
5129       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5130       switch (val2->val_class)
5131         {
5132         case dw_val_class_const:
5133           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5134           break;
5135         case dw_val_class_vec:
5136           {
5137             unsigned int elt_size = val2->v.val_vec.elt_size;
5138             unsigned int len = val2->v.val_vec.length;
5139             unsigned int i;
5140             unsigned char *p;
5141
5142             if (elt_size > sizeof (HOST_WIDE_INT))
5143               {
5144                 elt_size /= 2;
5145                 len *= 2;
5146               }
5147             for (i = 0, p = val2->v.val_vec.array;
5148                  i < len;
5149                  i++, p += elt_size)
5150               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5151                                    "fp or vector constant word %u", i);
5152           }
5153           break;
5154         case dw_val_class_const_double:
5155           {
5156             unsigned HOST_WIDE_INT first, second;
5157
5158             if (WORDS_BIG_ENDIAN)
5159               {
5160                 first = val2->v.val_double.high;
5161                 second = val2->v.val_double.low;
5162               }
5163             else
5164               {
5165                 first = val2->v.val_double.low;
5166                 second = val2->v.val_double.high;
5167               }
5168             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5169                                  first, NULL);
5170             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5171                                  second, NULL);
5172           }
5173           break;
5174         case dw_val_class_addr:
5175           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5176           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5177           break;
5178         default:
5179           gcc_unreachable ();
5180         }
5181       break;
5182 #else
5183     case DW_OP_const2u:
5184     case DW_OP_const2s:
5185     case DW_OP_const4u:
5186     case DW_OP_const4s:
5187     case DW_OP_const8u:
5188     case DW_OP_const8s:
5189     case DW_OP_skip:
5190     case DW_OP_bra:
5191     case DW_OP_implicit_value:
5192       /* We currently don't make any attempt to make sure these are
5193          aligned properly like we do for the main unwind info, so
5194          don't support emitting things larger than a byte if we're
5195          only doing unwinding.  */
5196       gcc_unreachable ();
5197 #endif
5198     case DW_OP_const1u:
5199     case DW_OP_const1s:
5200       dw2_asm_output_data (1, val1->v.val_int, NULL);
5201       break;
5202     case DW_OP_constu:
5203       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5204       break;
5205     case DW_OP_consts:
5206       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5207       break;
5208     case DW_OP_pick:
5209       dw2_asm_output_data (1, val1->v.val_int, NULL);
5210       break;
5211     case DW_OP_plus_uconst:
5212       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5213       break;
5214     case DW_OP_breg0:
5215     case DW_OP_breg1:
5216     case DW_OP_breg2:
5217     case DW_OP_breg3:
5218     case DW_OP_breg4:
5219     case DW_OP_breg5:
5220     case DW_OP_breg6:
5221     case DW_OP_breg7:
5222     case DW_OP_breg8:
5223     case DW_OP_breg9:
5224     case DW_OP_breg10:
5225     case DW_OP_breg11:
5226     case DW_OP_breg12:
5227     case DW_OP_breg13:
5228     case DW_OP_breg14:
5229     case DW_OP_breg15:
5230     case DW_OP_breg16:
5231     case DW_OP_breg17:
5232     case DW_OP_breg18:
5233     case DW_OP_breg19:
5234     case DW_OP_breg20:
5235     case DW_OP_breg21:
5236     case DW_OP_breg22:
5237     case DW_OP_breg23:
5238     case DW_OP_breg24:
5239     case DW_OP_breg25:
5240     case DW_OP_breg26:
5241     case DW_OP_breg27:
5242     case DW_OP_breg28:
5243     case DW_OP_breg29:
5244     case DW_OP_breg30:
5245     case DW_OP_breg31:
5246       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5247       break;
5248     case DW_OP_regx:
5249       {
5250         unsigned r = val1->v.val_unsigned;
5251         if (for_eh_or_skip >= 0)
5252           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5253         gcc_assert (size_of_uleb128 (r) 
5254                     == size_of_uleb128 (val1->v.val_unsigned));
5255         dw2_asm_output_data_uleb128 (r, NULL);  
5256       }
5257       break;
5258     case DW_OP_fbreg:
5259       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5260       break;
5261     case DW_OP_bregx:
5262       {
5263         unsigned r = val1->v.val_unsigned;
5264         if (for_eh_or_skip >= 0)
5265           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5266         gcc_assert (size_of_uleb128 (r) 
5267                     == size_of_uleb128 (val1->v.val_unsigned));
5268         dw2_asm_output_data_uleb128 (r, NULL);  
5269         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5270       }
5271       break;
5272     case DW_OP_piece:
5273       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5274       break;
5275     case DW_OP_bit_piece:
5276       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5277       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5278       break;
5279     case DW_OP_deref_size:
5280     case DW_OP_xderef_size:
5281       dw2_asm_output_data (1, val1->v.val_int, NULL);
5282       break;
5283
5284     case DW_OP_addr:
5285       if (loc->dtprel)
5286         {
5287           if (targetm.asm_out.output_dwarf_dtprel)
5288             {
5289               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5290                                                    DWARF2_ADDR_SIZE,
5291                                                    val1->v.val_addr);
5292               fputc ('\n', asm_out_file);
5293             }
5294           else
5295             gcc_unreachable ();
5296         }
5297       else
5298         {
5299 #ifdef DWARF2_DEBUGGING_INFO
5300           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5301 #else
5302           gcc_unreachable ();
5303 #endif
5304         }
5305       break;
5306
5307     case DW_OP_GNU_implicit_pointer:
5308       {
5309         char label[MAX_ARTIFICIAL_LABEL_BYTES
5310                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5311         gcc_assert (val1->val_class == dw_val_class_die_ref);
5312         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5313         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5314         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5315       }
5316       break;
5317
5318     case DW_OP_GNU_entry_value:
5319       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5320       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5321       break;
5322
5323     default:
5324       /* Other codes have no operands.  */
5325       break;
5326     }
5327 }
5328
5329 /* Output a sequence of location operations.  
5330    The for_eh_or_skip parameter controls whether register numbers are
5331    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5332    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5333    info).  This should be suppressed for the cases that have not been converted
5334    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5335
5336 static void
5337 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5338 {
5339   for (; loc != NULL; loc = loc->dw_loc_next)
5340     {
5341       enum dwarf_location_atom opc = loc->dw_loc_opc;
5342       /* Output the opcode.  */
5343       if (for_eh_or_skip >= 0 
5344           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5345         {
5346           unsigned r = (opc - DW_OP_breg0);
5347           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5348           gcc_assert (r <= 31);
5349           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5350         }
5351       else if (for_eh_or_skip >= 0 
5352                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5353         {
5354           unsigned r = (opc - DW_OP_reg0);
5355           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5356           gcc_assert (r <= 31);
5357           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5358         }
5359
5360       dw2_asm_output_data (1, opc,
5361                              "%s", dwarf_stack_op_name (opc));
5362
5363       /* Output the operand(s) (if any).  */
5364       output_loc_operands (loc, for_eh_or_skip);
5365     }
5366 }
5367
5368 /* Output location description stack opcode's operands (if any).
5369    The output is single bytes on a line, suitable for .cfi_escape.  */
5370
5371 static void
5372 output_loc_operands_raw (dw_loc_descr_ref loc)
5373 {
5374   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5375   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5376
5377   switch (loc->dw_loc_opc)
5378     {
5379     case DW_OP_addr:
5380     case DW_OP_implicit_value:
5381       /* We cannot output addresses in .cfi_escape, only bytes.  */
5382       gcc_unreachable ();
5383
5384     case DW_OP_const1u:
5385     case DW_OP_const1s:
5386     case DW_OP_pick:
5387     case DW_OP_deref_size:
5388     case DW_OP_xderef_size:
5389       fputc (',', asm_out_file);
5390       dw2_asm_output_data_raw (1, val1->v.val_int);
5391       break;
5392
5393     case DW_OP_const2u:
5394     case DW_OP_const2s:
5395       fputc (',', asm_out_file);
5396       dw2_asm_output_data_raw (2, val1->v.val_int);
5397       break;
5398
5399     case DW_OP_const4u:
5400     case DW_OP_const4s:
5401       fputc (',', asm_out_file);
5402       dw2_asm_output_data_raw (4, val1->v.val_int);
5403       break;
5404
5405     case DW_OP_const8u:
5406     case DW_OP_const8s:
5407       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5408       fputc (',', asm_out_file);
5409       dw2_asm_output_data_raw (8, val1->v.val_int);
5410       break;
5411
5412     case DW_OP_skip:
5413     case DW_OP_bra:
5414       {
5415         int offset;
5416
5417         gcc_assert (val1->val_class == dw_val_class_loc);
5418         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5419
5420         fputc (',', asm_out_file);
5421         dw2_asm_output_data_raw (2, offset);
5422       }
5423       break;
5424
5425     case DW_OP_regx:
5426       {
5427         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5428         gcc_assert (size_of_uleb128 (r) 
5429                     == size_of_uleb128 (val1->v.val_unsigned));
5430         fputc (',', asm_out_file);
5431         dw2_asm_output_data_uleb128_raw (r);
5432       }
5433       break;
5434       
5435     case DW_OP_constu:
5436     case DW_OP_plus_uconst:
5437     case DW_OP_piece:
5438       fputc (',', asm_out_file);
5439       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5440       break;
5441
5442     case DW_OP_bit_piece:
5443       fputc (',', asm_out_file);
5444       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5445       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5446       break;
5447
5448     case DW_OP_consts:
5449     case DW_OP_breg0:
5450     case DW_OP_breg1:
5451     case DW_OP_breg2:
5452     case DW_OP_breg3:
5453     case DW_OP_breg4:
5454     case DW_OP_breg5:
5455     case DW_OP_breg6:
5456     case DW_OP_breg7:
5457     case DW_OP_breg8:
5458     case DW_OP_breg9:
5459     case DW_OP_breg10:
5460     case DW_OP_breg11:
5461     case DW_OP_breg12:
5462     case DW_OP_breg13:
5463     case DW_OP_breg14:
5464     case DW_OP_breg15:
5465     case DW_OP_breg16:
5466     case DW_OP_breg17:
5467     case DW_OP_breg18:
5468     case DW_OP_breg19:
5469     case DW_OP_breg20:
5470     case DW_OP_breg21:
5471     case DW_OP_breg22:
5472     case DW_OP_breg23:
5473     case DW_OP_breg24:
5474     case DW_OP_breg25:
5475     case DW_OP_breg26:
5476     case DW_OP_breg27:
5477     case DW_OP_breg28:
5478     case DW_OP_breg29:
5479     case DW_OP_breg30:
5480     case DW_OP_breg31:
5481     case DW_OP_fbreg:
5482       fputc (',', asm_out_file);
5483       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5484       break;
5485
5486     case DW_OP_bregx:
5487       {
5488         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5489         gcc_assert (size_of_uleb128 (r) 
5490                     == size_of_uleb128 (val1->v.val_unsigned));
5491         fputc (',', asm_out_file);
5492         dw2_asm_output_data_uleb128_raw (r);
5493         fputc (',', asm_out_file);
5494         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5495       }
5496       break;
5497
5498     case DW_OP_GNU_implicit_pointer:
5499     case DW_OP_GNU_entry_value:
5500       gcc_unreachable ();
5501       break;
5502
5503     default:
5504       /* Other codes have no operands.  */
5505       break;
5506     }
5507 }
5508
5509 static void
5510 output_loc_sequence_raw (dw_loc_descr_ref loc)
5511 {
5512   while (1)
5513     {
5514       enum dwarf_location_atom opc = loc->dw_loc_opc;
5515       /* Output the opcode.  */
5516       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5517         {
5518           unsigned r = (opc - DW_OP_breg0);
5519           r = DWARF2_FRAME_REG_OUT (r, 1);
5520           gcc_assert (r <= 31);
5521           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5522         }
5523       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5524         {
5525           unsigned r = (opc - DW_OP_reg0);
5526           r = DWARF2_FRAME_REG_OUT (r, 1);
5527           gcc_assert (r <= 31);
5528           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5529         }
5530       /* Output the opcode.  */
5531       fprintf (asm_out_file, "%#x", opc);
5532       output_loc_operands_raw (loc);
5533
5534       if (!loc->dw_loc_next)
5535         break;
5536       loc = loc->dw_loc_next;
5537
5538       fputc (',', asm_out_file);
5539     }
5540 }
5541
5542 /* This routine will generate the correct assembly data for a location
5543    description based on a cfi entry with a complex address.  */
5544
5545 static void
5546 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5547 {
5548   dw_loc_descr_ref loc;
5549   unsigned long size;
5550
5551   if (cfi->dw_cfi_opc == DW_CFA_expression)
5552     {
5553       unsigned r = 
5554         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5555       dw2_asm_output_data (1, r, NULL);
5556       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5557     }
5558   else
5559     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5560
5561   /* Output the size of the block.  */
5562   size = size_of_locs (loc);
5563   dw2_asm_output_data_uleb128 (size, NULL);
5564
5565   /* Now output the operations themselves.  */
5566   output_loc_sequence (loc, for_eh);
5567 }
5568
5569 /* Similar, but used for .cfi_escape.  */
5570
5571 static void
5572 output_cfa_loc_raw (dw_cfi_ref cfi)
5573 {
5574   dw_loc_descr_ref loc;
5575   unsigned long size;
5576
5577   if (cfi->dw_cfi_opc == DW_CFA_expression)
5578     {
5579       unsigned r = 
5580         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5581       fprintf (asm_out_file, "%#x,", r);
5582       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5583     }
5584   else
5585     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5586
5587   /* Output the size of the block.  */
5588   size = size_of_locs (loc);
5589   dw2_asm_output_data_uleb128_raw (size);
5590   fputc (',', asm_out_file);
5591
5592   /* Now output the operations themselves.  */
5593   output_loc_sequence_raw (loc);
5594 }
5595
5596 /* This function builds a dwarf location descriptor sequence from a
5597    dw_cfa_location, adding the given OFFSET to the result of the
5598    expression.  */
5599
5600 static struct dw_loc_descr_struct *
5601 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5602 {
5603   struct dw_loc_descr_struct *head, *tmp;
5604
5605   offset += cfa->offset;
5606
5607   if (cfa->indirect)
5608     {
5609       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5610       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5611       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5612       add_loc_descr (&head, tmp);
5613       if (offset != 0)
5614         {
5615           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5616           add_loc_descr (&head, tmp);
5617         }
5618     }
5619   else
5620     head = new_reg_loc_descr (cfa->reg, offset);
5621
5622   return head;
5623 }
5624
5625 /* This function builds a dwarf location descriptor sequence for
5626    the address at OFFSET from the CFA when stack is aligned to
5627    ALIGNMENT byte.  */
5628
5629 static struct dw_loc_descr_struct *
5630 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5631 {
5632   struct dw_loc_descr_struct *head;
5633   unsigned int dwarf_fp
5634     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5635
5636  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5637   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5638     {
5639       head = new_reg_loc_descr (dwarf_fp, 0);
5640       add_loc_descr (&head, int_loc_descriptor (alignment));
5641       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5642       loc_descr_plus_const (&head, offset);
5643     }
5644   else
5645     head = new_reg_loc_descr (dwarf_fp, offset);
5646   return head;
5647 }
5648
5649 /* This function fills in aa dw_cfa_location structure from a dwarf location
5650    descriptor sequence.  */
5651
5652 static void
5653 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5654 {
5655   struct dw_loc_descr_struct *ptr;
5656   cfa->offset = 0;
5657   cfa->base_offset = 0;
5658   cfa->indirect = 0;
5659   cfa->reg = -1;
5660
5661   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5662     {
5663       enum dwarf_location_atom op = ptr->dw_loc_opc;
5664
5665       switch (op)
5666         {
5667         case DW_OP_reg0:
5668         case DW_OP_reg1:
5669         case DW_OP_reg2:
5670         case DW_OP_reg3:
5671         case DW_OP_reg4:
5672         case DW_OP_reg5:
5673         case DW_OP_reg6:
5674         case DW_OP_reg7:
5675         case DW_OP_reg8:
5676         case DW_OP_reg9:
5677         case DW_OP_reg10:
5678         case DW_OP_reg11:
5679         case DW_OP_reg12:
5680         case DW_OP_reg13:
5681         case DW_OP_reg14:
5682         case DW_OP_reg15:
5683         case DW_OP_reg16:
5684         case DW_OP_reg17:
5685         case DW_OP_reg18:
5686         case DW_OP_reg19:
5687         case DW_OP_reg20:
5688         case DW_OP_reg21:
5689         case DW_OP_reg22:
5690         case DW_OP_reg23:
5691         case DW_OP_reg24:
5692         case DW_OP_reg25:
5693         case DW_OP_reg26:
5694         case DW_OP_reg27:
5695         case DW_OP_reg28:
5696         case DW_OP_reg29:
5697         case DW_OP_reg30:
5698         case DW_OP_reg31:
5699           cfa->reg = op - DW_OP_reg0;
5700           break;
5701         case DW_OP_regx:
5702           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5703           break;
5704         case DW_OP_breg0:
5705         case DW_OP_breg1:
5706         case DW_OP_breg2:
5707         case DW_OP_breg3:
5708         case DW_OP_breg4:
5709         case DW_OP_breg5:
5710         case DW_OP_breg6:
5711         case DW_OP_breg7:
5712         case DW_OP_breg8:
5713         case DW_OP_breg9:
5714         case DW_OP_breg10:
5715         case DW_OP_breg11:
5716         case DW_OP_breg12:
5717         case DW_OP_breg13:
5718         case DW_OP_breg14:
5719         case DW_OP_breg15:
5720         case DW_OP_breg16:
5721         case DW_OP_breg17:
5722         case DW_OP_breg18:
5723         case DW_OP_breg19:
5724         case DW_OP_breg20:
5725         case DW_OP_breg21:
5726         case DW_OP_breg22:
5727         case DW_OP_breg23:
5728         case DW_OP_breg24:
5729         case DW_OP_breg25:
5730         case DW_OP_breg26:
5731         case DW_OP_breg27:
5732         case DW_OP_breg28:
5733         case DW_OP_breg29:
5734         case DW_OP_breg30:
5735         case DW_OP_breg31:
5736           cfa->reg = op - DW_OP_breg0;
5737           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5738           break;
5739         case DW_OP_bregx:
5740           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5741           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5742           break;
5743         case DW_OP_deref:
5744           cfa->indirect = 1;
5745           break;
5746         case DW_OP_plus_uconst:
5747           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5748           break;
5749         default:
5750           internal_error ("DW_LOC_OP %s not implemented",
5751                           dwarf_stack_op_name (ptr->dw_loc_opc));
5752         }
5753     }
5754 }
5755 \f
5756 /* And now, the support for symbolic debugging information.  */
5757
5758 /* .debug_str support.  */
5759 static int output_indirect_string (void **, void *);
5760
5761 static void dwarf2out_init (const char *);
5762 static void dwarf2out_finish (const char *);
5763 static void dwarf2out_assembly_start (void);
5764 static void dwarf2out_define (unsigned int, const char *);
5765 static void dwarf2out_undef (unsigned int, const char *);
5766 static void dwarf2out_start_source_file (unsigned, const char *);
5767 static void dwarf2out_end_source_file (unsigned);
5768 static void dwarf2out_function_decl (tree);
5769 static void dwarf2out_begin_block (unsigned, unsigned);
5770 static void dwarf2out_end_block (unsigned, unsigned);
5771 static bool dwarf2out_ignore_block (const_tree);
5772 static void dwarf2out_global_decl (tree);
5773 static void dwarf2out_type_decl (tree, int);
5774 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5775 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5776                                                  dw_die_ref);
5777 static void dwarf2out_abstract_function (tree);
5778 static void dwarf2out_var_location (rtx);
5779 static void dwarf2out_begin_function (tree);
5780 static void dwarf2out_set_name (tree, tree);
5781
5782 /* The debug hooks structure.  */
5783
5784 const struct gcc_debug_hooks dwarf2_debug_hooks =
5785 {
5786   dwarf2out_init,
5787   dwarf2out_finish,
5788   dwarf2out_assembly_start,
5789   dwarf2out_define,
5790   dwarf2out_undef,
5791   dwarf2out_start_source_file,
5792   dwarf2out_end_source_file,
5793   dwarf2out_begin_block,
5794   dwarf2out_end_block,
5795   dwarf2out_ignore_block,
5796   dwarf2out_source_line,
5797   dwarf2out_begin_prologue,
5798 #if VMS_DEBUGGING_INFO
5799   dwarf2out_vms_end_prologue,
5800   dwarf2out_vms_begin_epilogue,
5801 #else
5802   debug_nothing_int_charstar,
5803   debug_nothing_int_charstar,
5804 #endif
5805   dwarf2out_end_epilogue,
5806   dwarf2out_begin_function,
5807   debug_nothing_int,            /* end_function */
5808   dwarf2out_function_decl,      /* function_decl */
5809   dwarf2out_global_decl,
5810   dwarf2out_type_decl,          /* type_decl */
5811   dwarf2out_imported_module_or_decl,
5812   debug_nothing_tree,           /* deferred_inline_function */
5813   /* The DWARF 2 backend tries to reduce debugging bloat by not
5814      emitting the abstract description of inline functions until
5815      something tries to reference them.  */
5816   dwarf2out_abstract_function,  /* outlining_inline_function */
5817   debug_nothing_rtx,            /* label */
5818   debug_nothing_int,            /* handle_pch */
5819   dwarf2out_var_location,
5820   dwarf2out_switch_text_section,
5821   dwarf2out_set_name,
5822   1,                            /* start_end_main_source_file */
5823   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5824 };
5825 \f
5826 /* NOTE: In the comments in this file, many references are made to
5827    "Debugging Information Entries".  This term is abbreviated as `DIE'
5828    throughout the remainder of this file.  */
5829
5830 /* An internal representation of the DWARF output is built, and then
5831    walked to generate the DWARF debugging info.  The walk of the internal
5832    representation is done after the entire program has been compiled.
5833    The types below are used to describe the internal representation.  */
5834
5835 /* Various DIE's use offsets relative to the beginning of the
5836    .debug_info section to refer to each other.  */
5837
5838 typedef long int dw_offset;
5839
5840 /* Define typedefs here to avoid circular dependencies.  */
5841
5842 typedef struct dw_attr_struct *dw_attr_ref;
5843 typedef struct dw_line_info_struct *dw_line_info_ref;
5844 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5845 typedef struct pubname_struct *pubname_ref;
5846 typedef struct dw_ranges_struct *dw_ranges_ref;
5847 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5848 typedef struct comdat_type_struct *comdat_type_node_ref;
5849
5850 /* Each entry in the line_info_table maintains the file and
5851    line number associated with the label generated for that
5852    entry.  The label gives the PC value associated with
5853    the line number entry.  */
5854
5855 typedef struct GTY(()) dw_line_info_struct {
5856   unsigned long dw_file_num;
5857   unsigned long dw_line_num;
5858 }
5859 dw_line_info_entry;
5860
5861 /* Line information for functions in separate sections; each one gets its
5862    own sequence.  */
5863 typedef struct GTY(()) dw_separate_line_info_struct {
5864   unsigned long dw_file_num;
5865   unsigned long dw_line_num;
5866   unsigned long function;
5867 }
5868 dw_separate_line_info_entry;
5869
5870 /* Each DIE attribute has a field specifying the attribute kind,
5871    a link to the next attribute in the chain, and an attribute value.
5872    Attributes are typically linked below the DIE they modify.  */
5873
5874 typedef struct GTY(()) dw_attr_struct {
5875   enum dwarf_attribute dw_attr;
5876   dw_val_node dw_attr_val;
5877 }
5878 dw_attr_node;
5879
5880 DEF_VEC_O(dw_attr_node);
5881 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5882
5883 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5884    The children of each node form a circular list linked by
5885    die_sib.  die_child points to the node *before* the "first" child node.  */
5886
5887 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5888   union die_symbol_or_type_node
5889     {
5890       char * GTY ((tag ("0"))) die_symbol;
5891       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5892     }
5893   GTY ((desc ("dwarf_version >= 4"))) die_id;
5894   VEC(dw_attr_node,gc) * die_attr;
5895   dw_die_ref die_parent;
5896   dw_die_ref die_child;
5897   dw_die_ref die_sib;
5898   dw_die_ref die_definition; /* ref from a specification to its definition */
5899   dw_offset die_offset;
5900   unsigned long die_abbrev;
5901   int die_mark;
5902   /* Die is used and must not be pruned as unused.  */
5903   int die_perennial_p;
5904   unsigned int decl_id;
5905   enum dwarf_tag die_tag;
5906 }
5907 die_node;
5908
5909 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5910 #define FOR_EACH_CHILD(die, c, expr) do {       \
5911   c = die->die_child;                           \
5912   if (c) do {                                   \
5913     c = c->die_sib;                             \
5914     expr;                                       \
5915   } while (c != die->die_child);                \
5916 } while (0)
5917
5918 /* The pubname structure */
5919
5920 typedef struct GTY(()) pubname_struct {
5921   dw_die_ref die;
5922   const char *name;
5923 }
5924 pubname_entry;
5925
5926 DEF_VEC_O(pubname_entry);
5927 DEF_VEC_ALLOC_O(pubname_entry, gc);
5928
5929 struct GTY(()) dw_ranges_struct {
5930   /* If this is positive, it's a block number, otherwise it's a
5931      bitwise-negated index into dw_ranges_by_label.  */
5932   int num;
5933 };
5934
5935 /* A structure to hold a macinfo entry.  */
5936
5937 typedef struct GTY(()) macinfo_struct {
5938   unsigned HOST_WIDE_INT code;
5939   unsigned HOST_WIDE_INT lineno;
5940   const char *info;
5941 }
5942 macinfo_entry;
5943
5944 DEF_VEC_O(macinfo_entry);
5945 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5946
5947 struct GTY(()) dw_ranges_by_label_struct {
5948   const char *begin;
5949   const char *end;
5950 };
5951
5952 /* The comdat type node structure.  */
5953 typedef struct GTY(()) comdat_type_struct
5954 {
5955   dw_die_ref root_die;
5956   dw_die_ref type_die;
5957   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5958   struct comdat_type_struct *next;
5959 }
5960 comdat_type_node;
5961
5962 /* The limbo die list structure.  */
5963 typedef struct GTY(()) limbo_die_struct {
5964   dw_die_ref die;
5965   tree created_for;
5966   struct limbo_die_struct *next;
5967 }
5968 limbo_die_node;
5969
5970 typedef struct skeleton_chain_struct
5971 {
5972   dw_die_ref old_die;
5973   dw_die_ref new_die;
5974   struct skeleton_chain_struct *parent;
5975 }
5976 skeleton_chain_node;
5977
5978 /* How to start an assembler comment.  */
5979 #ifndef ASM_COMMENT_START
5980 #define ASM_COMMENT_START ";#"
5981 #endif
5982
5983 /* Define a macro which returns nonzero for a TYPE_DECL which was
5984    implicitly generated for a tagged type.
5985
5986    Note that unlike the gcc front end (which generates a NULL named
5987    TYPE_DECL node for each complete tagged type, each array type, and
5988    each function type node created) the g++ front end generates a
5989    _named_ TYPE_DECL node for each tagged type node created.
5990    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5991    generate a DW_TAG_typedef DIE for them.  */
5992
5993 #define TYPE_DECL_IS_STUB(decl)                         \
5994   (DECL_NAME (decl) == NULL_TREE                        \
5995    || (DECL_ARTIFICIAL (decl)                           \
5996        && is_tagged_type (TREE_TYPE (decl))             \
5997        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5998            /* This is necessary for stub decls that     \
5999               appear in nested inline functions.  */    \
6000            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6001                && (decl_ultimate_origin (decl)          \
6002                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6003
6004 /* Information concerning the compilation unit's programming
6005    language, and compiler version.  */
6006
6007 /* Fixed size portion of the DWARF compilation unit header.  */
6008 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6009   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6010
6011 /* Fixed size portion of the DWARF comdat type unit header.  */
6012 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6013   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6014    + DWARF_OFFSET_SIZE)
6015
6016 /* Fixed size portion of public names info.  */
6017 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6018
6019 /* Fixed size portion of the address range info.  */
6020 #define DWARF_ARANGES_HEADER_SIZE                                       \
6021   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6022                 DWARF2_ADDR_SIZE * 2)                                   \
6023    - DWARF_INITIAL_LENGTH_SIZE)
6024
6025 /* Size of padding portion in the address range info.  It must be
6026    aligned to twice the pointer size.  */
6027 #define DWARF_ARANGES_PAD_SIZE \
6028   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6029                 DWARF2_ADDR_SIZE * 2)                              \
6030    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6031
6032 /* Use assembler line directives if available.  */
6033 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6034 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6035 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6036 #else
6037 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6038 #endif
6039 #endif
6040
6041 /* Minimum line offset in a special line info. opcode.
6042    This value was chosen to give a reasonable range of values.  */
6043 #define DWARF_LINE_BASE  -10
6044
6045 /* First special line opcode - leave room for the standard opcodes.  */
6046 #define DWARF_LINE_OPCODE_BASE  10
6047
6048 /* Range of line offsets in a special line info. opcode.  */
6049 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6050
6051 /* Flag that indicates the initial value of the is_stmt_start flag.
6052    In the present implementation, we do not mark any lines as
6053    the beginning of a source statement, because that information
6054    is not made available by the GCC front-end.  */
6055 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6056
6057 /* Maximum number of operations per instruction bundle.  */
6058 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6059 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6060 #endif
6061
6062 /* This location is used by calc_die_sizes() to keep track
6063    the offset of each DIE within the .debug_info section.  */
6064 static unsigned long next_die_offset;
6065
6066 /* Record the root of the DIE's built for the current compilation unit.  */
6067 static GTY(()) dw_die_ref single_comp_unit_die;
6068
6069 /* A list of type DIEs that have been separated into comdat sections.  */
6070 static GTY(()) comdat_type_node *comdat_type_list;
6071
6072 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6073 static GTY(()) limbo_die_node *limbo_die_list;
6074
6075 /* A list of DIEs for which we may have to generate
6076    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6077 static GTY(()) limbo_die_node *deferred_asm_name;
6078
6079 /* Filenames referenced by this compilation unit.  */
6080 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6081
6082 /* A hash table of references to DIE's that describe declarations.
6083    The key is a DECL_UID() which is a unique number identifying each decl.  */
6084 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6085
6086 /* A hash table of references to DIE's that describe COMMON blocks.
6087    The key is DECL_UID() ^ die_parent.  */
6088 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6089
6090 typedef struct GTY(()) die_arg_entry_struct {
6091     dw_die_ref die;
6092     tree arg;
6093 } die_arg_entry;
6094
6095 DEF_VEC_O(die_arg_entry);
6096 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6097
6098 /* Node of the variable location list.  */
6099 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6100   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6101      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6102      in mode of the EXPR_LIST node and first EXPR_LIST operand
6103      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6104      location or NULL for padding.  For larger bitsizes,
6105      mode is 0 and first operand is a CONCAT with bitsize
6106      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6107      NULL as second operand.  */
6108   rtx GTY (()) loc;
6109   const char * GTY (()) label;
6110   struct var_loc_node * GTY (()) next;
6111 };
6112
6113 /* Variable location list.  */
6114 struct GTY (()) var_loc_list_def {
6115   struct var_loc_node * GTY (()) first;
6116
6117   /* Pointer to the last but one or last element of the
6118      chained list.  If the list is empty, both first and
6119      last are NULL, if the list contains just one node
6120      or the last node certainly is not redundant, it points
6121      to the last node, otherwise points to the last but one.
6122      Do not mark it for GC because it is marked through the chain.  */
6123   struct var_loc_node * GTY ((skip ("%h"))) last;
6124
6125   /* DECL_UID of the variable decl.  */
6126   unsigned int decl_id;
6127 };
6128 typedef struct var_loc_list_def var_loc_list;
6129
6130 /* Call argument location list.  */
6131 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6132   rtx GTY (()) call_arg_loc_note;
6133   const char * GTY (()) label;
6134   tree GTY (()) block;
6135   bool tail_call_p;
6136   rtx GTY (()) symbol_ref;
6137   struct call_arg_loc_node * GTY (()) next;
6138 };
6139
6140
6141 /* Table of decl location linked lists.  */
6142 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6143
6144 /* Head and tail of call_arg_loc chain.  */
6145 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6146 static struct call_arg_loc_node *call_arg_loc_last;
6147
6148 /* Number of call sites in the current function.  */
6149 static int call_site_count = -1;
6150 /* Number of tail call sites in the current function.  */
6151 static int tail_call_site_count = -1;
6152
6153 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6154    DIEs.  */
6155 static VEC (dw_die_ref, heap) *block_map;
6156
6157 /* A pointer to the base of a list of references to DIE's that
6158    are uniquely identified by their tag, presence/absence of
6159    children DIE's, and list of attribute/value pairs.  */
6160 static GTY((length ("abbrev_die_table_allocated")))
6161   dw_die_ref *abbrev_die_table;
6162
6163 /* Number of elements currently allocated for abbrev_die_table.  */
6164 static GTY(()) unsigned abbrev_die_table_allocated;
6165
6166 /* Number of elements in type_die_table currently in use.  */
6167 static GTY(()) unsigned abbrev_die_table_in_use;
6168
6169 /* Size (in elements) of increments by which we may expand the
6170    abbrev_die_table.  */
6171 #define ABBREV_DIE_TABLE_INCREMENT 256
6172
6173 /* A pointer to the base of a table that contains line information
6174    for each source code line in .text in the compilation unit.  */
6175 static GTY((length ("line_info_table_allocated")))
6176      dw_line_info_ref line_info_table;
6177
6178 /* Number of elements currently allocated for line_info_table.  */
6179 static GTY(()) unsigned line_info_table_allocated;
6180
6181 /* Number of elements in line_info_table currently in use.  */
6182 static GTY(()) unsigned line_info_table_in_use;
6183
6184 /* A pointer to the base of a table that contains line information
6185    for each source code line outside of .text in the compilation unit.  */
6186 static GTY ((length ("separate_line_info_table_allocated")))
6187      dw_separate_line_info_ref separate_line_info_table;
6188
6189 /* Number of elements currently allocated for separate_line_info_table.  */
6190 static GTY(()) unsigned separate_line_info_table_allocated;
6191
6192 /* Number of elements in separate_line_info_table currently in use.  */
6193 static GTY(()) unsigned separate_line_info_table_in_use;
6194
6195 /* Size (in elements) of increments by which we may expand the
6196    line_info_table.  */
6197 #define LINE_INFO_TABLE_INCREMENT 1024
6198
6199 /* A flag to tell pubnames/types export if there is an info section to
6200    refer to.  */
6201 static bool info_section_emitted;
6202
6203 /* A pointer to the base of a table that contains a list of publicly
6204    accessible names.  */
6205 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6206
6207 /* A pointer to the base of a table that contains a list of publicly
6208    accessible types.  */
6209 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6210
6211 /* A pointer to the base of a table that contains a list of macro
6212    defines/undefines (and file start/end markers).  */
6213 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6214
6215 /* Array of dies for which we should generate .debug_arange info.  */
6216 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6217
6218 /* Number of elements currently allocated for arange_table.  */
6219 static GTY(()) unsigned arange_table_allocated;
6220
6221 /* Number of elements in arange_table currently in use.  */
6222 static GTY(()) unsigned arange_table_in_use;
6223
6224 /* Size (in elements) of increments by which we may expand the
6225    arange_table.  */
6226 #define ARANGE_TABLE_INCREMENT 64
6227
6228 /* Array of dies for which we should generate .debug_ranges info.  */
6229 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6230
6231 /* Number of elements currently allocated for ranges_table.  */
6232 static GTY(()) unsigned ranges_table_allocated;
6233
6234 /* Number of elements in ranges_table currently in use.  */
6235 static GTY(()) unsigned ranges_table_in_use;
6236
6237 /* Array of pairs of labels referenced in ranges_table.  */
6238 static GTY ((length ("ranges_by_label_allocated")))
6239      dw_ranges_by_label_ref ranges_by_label;
6240
6241 /* Number of elements currently allocated for ranges_by_label.  */
6242 static GTY(()) unsigned ranges_by_label_allocated;
6243
6244 /* Number of elements in ranges_by_label currently in use.  */
6245 static GTY(()) unsigned ranges_by_label_in_use;
6246
6247 /* Size (in elements) of increments by which we may expand the
6248    ranges_table.  */
6249 #define RANGES_TABLE_INCREMENT 64
6250
6251 /* Whether we have location lists that need outputting */
6252 static GTY(()) bool have_location_lists;
6253
6254 /* Unique label counter.  */
6255 static GTY(()) unsigned int loclabel_num;
6256
6257 /* Unique label counter for point-of-call tables.  */
6258 static GTY(()) unsigned int poc_label_num;
6259
6260 /* Record whether the function being analyzed contains inlined functions.  */
6261 static int current_function_has_inlines;
6262
6263 /* The last file entry emitted by maybe_emit_file().  */
6264 static GTY(()) struct dwarf_file_data * last_emitted_file;
6265
6266 /* Number of internal labels generated by gen_internal_sym().  */
6267 static GTY(()) int label_num;
6268
6269 /* Cached result of previous call to lookup_filename.  */
6270 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6271
6272 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6273
6274 /* Instances of generic types for which we need to generate debug
6275    info that describe their generic parameters and arguments. That
6276    generation needs to happen once all types are properly laid out so
6277    we do it at the end of compilation.  */
6278 static GTY(()) VEC(tree,gc) *generic_type_instances;
6279
6280 /* Offset from the "steady-state frame pointer" to the frame base,
6281    within the current function.  */
6282 static HOST_WIDE_INT frame_pointer_fb_offset;
6283
6284 /* Forward declarations for functions defined in this file.  */
6285
6286 static int is_pseudo_reg (const_rtx);
6287 static tree type_main_variant (tree);
6288 static int is_tagged_type (const_tree);
6289 static const char *dwarf_tag_name (unsigned);
6290 static const char *dwarf_attr_name (unsigned);
6291 static const char *dwarf_form_name (unsigned);
6292 static tree decl_ultimate_origin (const_tree);
6293 static tree decl_class_context (tree);
6294 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6295 static inline enum dw_val_class AT_class (dw_attr_ref);
6296 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6297 static inline unsigned AT_flag (dw_attr_ref);
6298 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6299 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6300 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6301 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6302 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6303                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6304 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6305                                unsigned int, unsigned char *);
6306 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6307 static hashval_t debug_str_do_hash (const void *);
6308 static int debug_str_eq (const void *, const void *);
6309 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6310 static inline const char *AT_string (dw_attr_ref);
6311 static enum dwarf_form AT_string_form (dw_attr_ref);
6312 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6313 static void add_AT_specification (dw_die_ref, dw_die_ref);
6314 static inline dw_die_ref AT_ref (dw_attr_ref);
6315 static inline int AT_ref_external (dw_attr_ref);
6316 static inline void set_AT_ref_external (dw_attr_ref, int);
6317 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6318 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6319 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6320 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6321                              dw_loc_list_ref);
6322 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6323 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6324 static inline rtx AT_addr (dw_attr_ref);
6325 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6326 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6327 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6328 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6329                            unsigned HOST_WIDE_INT);
6330 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6331                                unsigned long);
6332 static inline const char *AT_lbl (dw_attr_ref);
6333 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6334 static const char *get_AT_low_pc (dw_die_ref);
6335 static const char *get_AT_hi_pc (dw_die_ref);
6336 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6337 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6338 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6339 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6340 static bool is_cxx (void);
6341 static bool is_fortran (void);
6342 static bool is_ada (void);
6343 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6344 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6345 static void add_child_die (dw_die_ref, dw_die_ref);
6346 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6347 static dw_die_ref lookup_type_die (tree);
6348 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6349 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6350 static void equate_type_number_to_die (tree, dw_die_ref);
6351 static hashval_t decl_die_table_hash (const void *);
6352 static int decl_die_table_eq (const void *, const void *);
6353 static dw_die_ref lookup_decl_die (tree);
6354 static hashval_t common_block_die_table_hash (const void *);
6355 static int common_block_die_table_eq (const void *, const void *);
6356 static hashval_t decl_loc_table_hash (const void *);
6357 static int decl_loc_table_eq (const void *, const void *);
6358 static var_loc_list *lookup_decl_loc (const_tree);
6359 static void equate_decl_number_to_die (tree, dw_die_ref);
6360 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6361 static void print_spaces (FILE *);
6362 static void print_die (dw_die_ref, FILE *);
6363 static void print_dwarf_line_table (FILE *);
6364 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6365 static dw_die_ref pop_compile_unit (dw_die_ref);
6366 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6367 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6368 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6369 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6370 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6371 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6372 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6373                                    struct md5_ctx *, int *);
6374 struct checksum_attributes;
6375 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6376 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6377 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6378 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6379 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6380 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6381 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6382 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6383 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6384 static void compute_section_prefix (dw_die_ref);
6385 static int is_type_die (dw_die_ref);
6386 static int is_comdat_die (dw_die_ref);
6387 static int is_symbol_die (dw_die_ref);
6388 static void assign_symbol_names (dw_die_ref);
6389 static void break_out_includes (dw_die_ref);
6390 static int is_declaration_die (dw_die_ref);
6391 static int should_move_die_to_comdat (dw_die_ref);
6392 static dw_die_ref clone_as_declaration (dw_die_ref);
6393 static dw_die_ref clone_die (dw_die_ref);
6394 static dw_die_ref clone_tree (dw_die_ref);
6395 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6396 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6397 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6398 static dw_die_ref generate_skeleton (dw_die_ref);
6399 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6400                                                          dw_die_ref);
6401 static void break_out_comdat_types (dw_die_ref);
6402 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6403 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6404 static void copy_decls_for_unworthy_types (dw_die_ref);
6405
6406 static hashval_t htab_cu_hash (const void *);
6407 static int htab_cu_eq (const void *, const void *);
6408 static void htab_cu_del (void *);
6409 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6410 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6411 static void add_sibling_attributes (dw_die_ref);
6412 static void build_abbrev_table (dw_die_ref);
6413 static void output_location_lists (dw_die_ref);
6414 static int constant_size (unsigned HOST_WIDE_INT);
6415 static unsigned long size_of_die (dw_die_ref);
6416 static void calc_die_sizes (dw_die_ref);
6417 static void mark_dies (dw_die_ref);
6418 static void unmark_dies (dw_die_ref);
6419 static void unmark_all_dies (dw_die_ref);
6420 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6421 static unsigned long size_of_aranges (void);
6422 static enum dwarf_form value_format (dw_attr_ref);
6423 static void output_value_format (dw_attr_ref);
6424 static void output_abbrev_section (void);
6425 static void output_die_symbol (dw_die_ref);
6426 static void output_die (dw_die_ref);
6427 static void output_compilation_unit_header (void);
6428 static void output_comp_unit (dw_die_ref, int);
6429 static void output_comdat_type_unit (comdat_type_node *);
6430 static const char *dwarf2_name (tree, int);
6431 static void add_pubname (tree, dw_die_ref);
6432 static void add_pubname_string (const char *, dw_die_ref);
6433 static void add_pubtype (tree, dw_die_ref);
6434 static void output_pubnames (VEC (pubname_entry,gc) *);
6435 static void add_arange (tree, dw_die_ref);
6436 static void output_aranges (void);
6437 static unsigned int add_ranges_num (int);
6438 static unsigned int add_ranges (const_tree);
6439 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6440                                   bool *);
6441 static void output_ranges (void);
6442 static void output_line_info (void);
6443 static void output_file_names (void);
6444 static dw_die_ref base_type_die (tree);
6445 static int is_base_type (tree);
6446 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6447 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6448 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6449 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6450 static int type_is_enum (const_tree);
6451 static unsigned int dbx_reg_number (const_rtx);
6452 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6453 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6454 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6455                                                 enum var_init_status);
6456 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6457                                                      enum var_init_status);
6458 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6459                                          enum var_init_status);
6460 static int is_based_loc (const_rtx);
6461 static int resolve_one_addr (rtx *, void *);
6462 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6463                                                enum var_init_status);
6464 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6465                                         enum var_init_status);
6466 static dw_loc_list_ref loc_list_from_tree (tree, int);
6467 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6468 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6469 static tree field_type (const_tree);
6470 static unsigned int simple_type_align_in_bits (const_tree);
6471 static unsigned int simple_decl_align_in_bits (const_tree);
6472 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6473 static HOST_WIDE_INT field_byte_offset (const_tree);
6474 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6475                                          dw_loc_list_ref);
6476 static void add_data_member_location_attribute (dw_die_ref, tree);
6477 static bool add_const_value_attribute (dw_die_ref, rtx);
6478 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6479 static void insert_double (double_int, unsigned char *);
6480 static void insert_float (const_rtx, unsigned char *);
6481 static rtx rtl_for_decl_location (tree);
6482 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6483                                                    enum dwarf_attribute);
6484 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6485 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6486 static void add_name_attribute (dw_die_ref, const char *);
6487 static void add_comp_dir_attribute (dw_die_ref);
6488 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6489 static void add_subscript_info (dw_die_ref, tree, bool);
6490 static void add_byte_size_attribute (dw_die_ref, tree);
6491 static void add_bit_offset_attribute (dw_die_ref, tree);
6492 static void add_bit_size_attribute (dw_die_ref, tree);
6493 static void add_prototyped_attribute (dw_die_ref, tree);
6494 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6495 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6496 static void add_src_coords_attributes (dw_die_ref, tree);
6497 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6498 static void push_decl_scope (tree);
6499 static void pop_decl_scope (void);
6500 static dw_die_ref scope_die_for (tree, dw_die_ref);
6501 static inline int local_scope_p (dw_die_ref);
6502 static inline int class_scope_p (dw_die_ref);
6503 static inline int class_or_namespace_scope_p (dw_die_ref);
6504 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6505 static void add_calling_convention_attribute (dw_die_ref, tree);
6506 static const char *type_tag (const_tree);
6507 static tree member_declared_type (const_tree);
6508 #if 0
6509 static const char *decl_start_label (tree);
6510 #endif
6511 static void gen_array_type_die (tree, dw_die_ref);
6512 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6513 #if 0
6514 static void gen_entry_point_die (tree, dw_die_ref);
6515 #endif
6516 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6517 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6518 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6519 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6520 static void gen_formal_types_die (tree, dw_die_ref);
6521 static void gen_subprogram_die (tree, dw_die_ref);
6522 static void gen_variable_die (tree, tree, dw_die_ref);
6523 static void gen_const_die (tree, dw_die_ref);
6524 static void gen_label_die (tree, dw_die_ref);
6525 static void gen_lexical_block_die (tree, dw_die_ref, int);
6526 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6527 static void gen_field_die (tree, dw_die_ref);
6528 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6529 static dw_die_ref gen_compile_unit_die (const char *);
6530 static void gen_inheritance_die (tree, tree, dw_die_ref);
6531 static void gen_member_die (tree, dw_die_ref);
6532 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6533                                                 enum debug_info_usage);
6534 static void gen_subroutine_type_die (tree, dw_die_ref);
6535 static void gen_typedef_die (tree, dw_die_ref);
6536 static void gen_type_die (tree, dw_die_ref);
6537 static void gen_block_die (tree, dw_die_ref, int);
6538 static void decls_for_scope (tree, dw_die_ref, int);
6539 static int is_redundant_typedef (const_tree);
6540 static bool is_naming_typedef_decl (const_tree);
6541 static inline dw_die_ref get_context_die (tree);
6542 static void gen_namespace_die (tree, dw_die_ref);
6543 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6544 static dw_die_ref force_decl_die (tree);
6545 static dw_die_ref force_type_die (tree);
6546 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6547 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6548 static struct dwarf_file_data * lookup_filename (const char *);
6549 static void retry_incomplete_types (void);
6550 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6551 static void gen_generic_params_dies (tree);
6552 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6553 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6554 static void splice_child_die (dw_die_ref, dw_die_ref);
6555 static int file_info_cmp (const void *, const void *);
6556 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6557                                      const char *, const char *);
6558 static void output_loc_list (dw_loc_list_ref);
6559 static char *gen_internal_sym (const char *);
6560
6561 static void prune_unmark_dies (dw_die_ref);
6562 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6563 static void prune_unused_types_mark (dw_die_ref, int);
6564 static void prune_unused_types_walk (dw_die_ref);
6565 static void prune_unused_types_walk_attribs (dw_die_ref);
6566 static void prune_unused_types_prune (dw_die_ref);
6567 static void prune_unused_types (void);
6568 static int maybe_emit_file (struct dwarf_file_data *fd);
6569 static inline const char *AT_vms_delta1 (dw_attr_ref);
6570 static inline const char *AT_vms_delta2 (dw_attr_ref);
6571 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6572                                      const char *, const char *);
6573 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6574 static void gen_remaining_tmpl_value_param_die_attribute (void);
6575 static bool generic_type_p (tree);
6576 static void schedule_generic_params_dies_gen (tree t);
6577 static void gen_scheduled_generic_parms_dies (void);
6578
6579 /* Section names used to hold DWARF debugging information.  */
6580 #ifndef DEBUG_INFO_SECTION
6581 #define DEBUG_INFO_SECTION      ".debug_info"
6582 #endif
6583 #ifndef DEBUG_ABBREV_SECTION
6584 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6585 #endif
6586 #ifndef DEBUG_ARANGES_SECTION
6587 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6588 #endif
6589 #ifndef DEBUG_MACINFO_SECTION
6590 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6591 #endif
6592 #ifndef DEBUG_LINE_SECTION
6593 #define DEBUG_LINE_SECTION      ".debug_line"
6594 #endif
6595 #ifndef DEBUG_LOC_SECTION
6596 #define DEBUG_LOC_SECTION       ".debug_loc"
6597 #endif
6598 #ifndef DEBUG_PUBNAMES_SECTION
6599 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6600 #endif
6601 #ifndef DEBUG_PUBTYPES_SECTION
6602 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6603 #endif
6604 #ifndef DEBUG_STR_SECTION
6605 #define DEBUG_STR_SECTION       ".debug_str"
6606 #endif
6607 #ifndef DEBUG_RANGES_SECTION
6608 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6609 #endif
6610
6611 /* Standard ELF section names for compiled code and data.  */
6612 #ifndef TEXT_SECTION_NAME
6613 #define TEXT_SECTION_NAME       ".text"
6614 #endif
6615
6616 /* Section flags for .debug_str section.  */
6617 #define DEBUG_STR_SECTION_FLAGS \
6618   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6619    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6620    : SECTION_DEBUG)
6621
6622 /* Labels we insert at beginning sections we can reference instead of
6623    the section names themselves.  */
6624
6625 #ifndef TEXT_SECTION_LABEL
6626 #define TEXT_SECTION_LABEL              "Ltext"
6627 #endif
6628 #ifndef COLD_TEXT_SECTION_LABEL
6629 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6630 #endif
6631 #ifndef DEBUG_LINE_SECTION_LABEL
6632 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6633 #endif
6634 #ifndef DEBUG_INFO_SECTION_LABEL
6635 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6636 #endif
6637 #ifndef DEBUG_ABBREV_SECTION_LABEL
6638 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6639 #endif
6640 #ifndef DEBUG_LOC_SECTION_LABEL
6641 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6642 #endif
6643 #ifndef DEBUG_RANGES_SECTION_LABEL
6644 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6645 #endif
6646 #ifndef DEBUG_MACINFO_SECTION_LABEL
6647 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6648 #endif
6649
6650
6651 /* Definitions of defaults for formats and names of various special
6652    (artificial) labels which may be generated within this file (when the -g
6653    options is used and DWARF2_DEBUGGING_INFO is in effect.
6654    If necessary, these may be overridden from within the tm.h file, but
6655    typically, overriding these defaults is unnecessary.  */
6656
6657 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6658 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6659 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6660 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6661 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6662 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6663 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6664 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6665 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6666 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6667
6668 #ifndef TEXT_END_LABEL
6669 #define TEXT_END_LABEL          "Letext"
6670 #endif
6671 #ifndef COLD_END_LABEL
6672 #define COLD_END_LABEL          "Letext_cold"
6673 #endif
6674 #ifndef BLOCK_BEGIN_LABEL
6675 #define BLOCK_BEGIN_LABEL       "LBB"
6676 #endif
6677 #ifndef BLOCK_END_LABEL
6678 #define BLOCK_END_LABEL         "LBE"
6679 #endif
6680 #ifndef LINE_CODE_LABEL
6681 #define LINE_CODE_LABEL         "LM"
6682 #endif
6683 #ifndef SEPARATE_LINE_CODE_LABEL
6684 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6685 #endif
6686
6687 \f
6688 /* Return the root of the DIE's built for the current compilation unit.  */
6689 static dw_die_ref
6690 comp_unit_die (void)
6691 {
6692   if (!single_comp_unit_die)
6693     single_comp_unit_die = gen_compile_unit_die (NULL);
6694   return single_comp_unit_die;
6695 }
6696
6697 /* We allow a language front-end to designate a function that is to be
6698    called to "demangle" any name before it is put into a DIE.  */
6699
6700 static const char *(*demangle_name_func) (const char *);
6701
6702 void
6703 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6704 {
6705   demangle_name_func = func;
6706 }
6707
6708 /* Test if rtl node points to a pseudo register.  */
6709
6710 static inline int
6711 is_pseudo_reg (const_rtx rtl)
6712 {
6713   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6714           || (GET_CODE (rtl) == SUBREG
6715               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6716 }
6717
6718 /* Return a reference to a type, with its const and volatile qualifiers
6719    removed.  */
6720
6721 static inline tree
6722 type_main_variant (tree type)
6723 {
6724   type = TYPE_MAIN_VARIANT (type);
6725
6726   /* ??? There really should be only one main variant among any group of
6727      variants of a given type (and all of the MAIN_VARIANT values for all
6728      members of the group should point to that one type) but sometimes the C
6729      front-end messes this up for array types, so we work around that bug
6730      here.  */
6731   if (TREE_CODE (type) == ARRAY_TYPE)
6732     while (type != TYPE_MAIN_VARIANT (type))
6733       type = TYPE_MAIN_VARIANT (type);
6734
6735   return type;
6736 }
6737
6738 /* Return nonzero if the given type node represents a tagged type.  */
6739
6740 static inline int
6741 is_tagged_type (const_tree type)
6742 {
6743   enum tree_code code = TREE_CODE (type);
6744
6745   return (code == RECORD_TYPE || code == UNION_TYPE
6746           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6747 }
6748
6749 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6750
6751 static void
6752 get_ref_die_offset_label (char *label, dw_die_ref ref)
6753 {
6754   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6755 }
6756
6757 /* Convert a DIE tag into its string name.  */
6758
6759 static const char *
6760 dwarf_tag_name (unsigned int tag)
6761 {
6762   switch (tag)
6763     {
6764     case DW_TAG_padding:
6765       return "DW_TAG_padding";
6766     case DW_TAG_array_type:
6767       return "DW_TAG_array_type";
6768     case DW_TAG_class_type:
6769       return "DW_TAG_class_type";
6770     case DW_TAG_entry_point:
6771       return "DW_TAG_entry_point";
6772     case DW_TAG_enumeration_type:
6773       return "DW_TAG_enumeration_type";
6774     case DW_TAG_formal_parameter:
6775       return "DW_TAG_formal_parameter";
6776     case DW_TAG_imported_declaration:
6777       return "DW_TAG_imported_declaration";
6778     case DW_TAG_label:
6779       return "DW_TAG_label";
6780     case DW_TAG_lexical_block:
6781       return "DW_TAG_lexical_block";
6782     case DW_TAG_member:
6783       return "DW_TAG_member";
6784     case DW_TAG_pointer_type:
6785       return "DW_TAG_pointer_type";
6786     case DW_TAG_reference_type:
6787       return "DW_TAG_reference_type";
6788     case DW_TAG_compile_unit:
6789       return "DW_TAG_compile_unit";
6790     case DW_TAG_string_type:
6791       return "DW_TAG_string_type";
6792     case DW_TAG_structure_type:
6793       return "DW_TAG_structure_type";
6794     case DW_TAG_subroutine_type:
6795       return "DW_TAG_subroutine_type";
6796     case DW_TAG_typedef:
6797       return "DW_TAG_typedef";
6798     case DW_TAG_union_type:
6799       return "DW_TAG_union_type";
6800     case DW_TAG_unspecified_parameters:
6801       return "DW_TAG_unspecified_parameters";
6802     case DW_TAG_variant:
6803       return "DW_TAG_variant";
6804     case DW_TAG_common_block:
6805       return "DW_TAG_common_block";
6806     case DW_TAG_common_inclusion:
6807       return "DW_TAG_common_inclusion";
6808     case DW_TAG_inheritance:
6809       return "DW_TAG_inheritance";
6810     case DW_TAG_inlined_subroutine:
6811       return "DW_TAG_inlined_subroutine";
6812     case DW_TAG_module:
6813       return "DW_TAG_module";
6814     case DW_TAG_ptr_to_member_type:
6815       return "DW_TAG_ptr_to_member_type";
6816     case DW_TAG_set_type:
6817       return "DW_TAG_set_type";
6818     case DW_TAG_subrange_type:
6819       return "DW_TAG_subrange_type";
6820     case DW_TAG_with_stmt:
6821       return "DW_TAG_with_stmt";
6822     case DW_TAG_access_declaration:
6823       return "DW_TAG_access_declaration";
6824     case DW_TAG_base_type:
6825       return "DW_TAG_base_type";
6826     case DW_TAG_catch_block:
6827       return "DW_TAG_catch_block";
6828     case DW_TAG_const_type:
6829       return "DW_TAG_const_type";
6830     case DW_TAG_constant:
6831       return "DW_TAG_constant";
6832     case DW_TAG_enumerator:
6833       return "DW_TAG_enumerator";
6834     case DW_TAG_file_type:
6835       return "DW_TAG_file_type";
6836     case DW_TAG_friend:
6837       return "DW_TAG_friend";
6838     case DW_TAG_namelist:
6839       return "DW_TAG_namelist";
6840     case DW_TAG_namelist_item:
6841       return "DW_TAG_namelist_item";
6842     case DW_TAG_packed_type:
6843       return "DW_TAG_packed_type";
6844     case DW_TAG_subprogram:
6845       return "DW_TAG_subprogram";
6846     case DW_TAG_template_type_param:
6847       return "DW_TAG_template_type_param";
6848     case DW_TAG_template_value_param:
6849       return "DW_TAG_template_value_param";
6850     case DW_TAG_thrown_type:
6851       return "DW_TAG_thrown_type";
6852     case DW_TAG_try_block:
6853       return "DW_TAG_try_block";
6854     case DW_TAG_variant_part:
6855       return "DW_TAG_variant_part";
6856     case DW_TAG_variable:
6857       return "DW_TAG_variable";
6858     case DW_TAG_volatile_type:
6859       return "DW_TAG_volatile_type";
6860     case DW_TAG_dwarf_procedure:
6861       return "DW_TAG_dwarf_procedure";
6862     case DW_TAG_restrict_type:
6863       return "DW_TAG_restrict_type";
6864     case DW_TAG_interface_type:
6865       return "DW_TAG_interface_type";
6866     case DW_TAG_namespace:
6867       return "DW_TAG_namespace";
6868     case DW_TAG_imported_module:
6869       return "DW_TAG_imported_module";
6870     case DW_TAG_unspecified_type:
6871       return "DW_TAG_unspecified_type";
6872     case DW_TAG_partial_unit:
6873       return "DW_TAG_partial_unit";
6874     case DW_TAG_imported_unit:
6875       return "DW_TAG_imported_unit";
6876     case DW_TAG_condition:
6877       return "DW_TAG_condition";
6878     case DW_TAG_shared_type:
6879       return "DW_TAG_shared_type";
6880     case DW_TAG_type_unit:
6881       return "DW_TAG_type_unit";
6882     case DW_TAG_rvalue_reference_type:
6883       return "DW_TAG_rvalue_reference_type";
6884     case DW_TAG_template_alias:
6885       return "DW_TAG_template_alias";
6886     case DW_TAG_GNU_template_parameter_pack:
6887       return "DW_TAG_GNU_template_parameter_pack";
6888     case DW_TAG_GNU_formal_parameter_pack:
6889       return "DW_TAG_GNU_formal_parameter_pack";
6890     case DW_TAG_MIPS_loop:
6891       return "DW_TAG_MIPS_loop";
6892     case DW_TAG_format_label:
6893       return "DW_TAG_format_label";
6894     case DW_TAG_function_template:
6895       return "DW_TAG_function_template";
6896     case DW_TAG_class_template:
6897       return "DW_TAG_class_template";
6898     case DW_TAG_GNU_BINCL:
6899       return "DW_TAG_GNU_BINCL";
6900     case DW_TAG_GNU_EINCL:
6901       return "DW_TAG_GNU_EINCL";
6902     case DW_TAG_GNU_template_template_param:
6903       return "DW_TAG_GNU_template_template_param";
6904     case DW_TAG_GNU_call_site:
6905       return "DW_TAG_GNU_call_site";
6906     case DW_TAG_GNU_call_site_parameter:
6907       return "DW_TAG_GNU_call_site_parameter";
6908     default:
6909       return "DW_TAG_<unknown>";
6910     }
6911 }
6912
6913 /* Convert a DWARF attribute code into its string name.  */
6914
6915 static const char *
6916 dwarf_attr_name (unsigned int attr)
6917 {
6918   switch (attr)
6919     {
6920     case DW_AT_sibling:
6921       return "DW_AT_sibling";
6922     case DW_AT_location:
6923       return "DW_AT_location";
6924     case DW_AT_name:
6925       return "DW_AT_name";
6926     case DW_AT_ordering:
6927       return "DW_AT_ordering";
6928     case DW_AT_subscr_data:
6929       return "DW_AT_subscr_data";
6930     case DW_AT_byte_size:
6931       return "DW_AT_byte_size";
6932     case DW_AT_bit_offset:
6933       return "DW_AT_bit_offset";
6934     case DW_AT_bit_size:
6935       return "DW_AT_bit_size";
6936     case DW_AT_element_list:
6937       return "DW_AT_element_list";
6938     case DW_AT_stmt_list:
6939       return "DW_AT_stmt_list";
6940     case DW_AT_low_pc:
6941       return "DW_AT_low_pc";
6942     case DW_AT_high_pc:
6943       return "DW_AT_high_pc";
6944     case DW_AT_language:
6945       return "DW_AT_language";
6946     case DW_AT_member:
6947       return "DW_AT_member";
6948     case DW_AT_discr:
6949       return "DW_AT_discr";
6950     case DW_AT_discr_value:
6951       return "DW_AT_discr_value";
6952     case DW_AT_visibility:
6953       return "DW_AT_visibility";
6954     case DW_AT_import:
6955       return "DW_AT_import";
6956     case DW_AT_string_length:
6957       return "DW_AT_string_length";
6958     case DW_AT_common_reference:
6959       return "DW_AT_common_reference";
6960     case DW_AT_comp_dir:
6961       return "DW_AT_comp_dir";
6962     case DW_AT_const_value:
6963       return "DW_AT_const_value";
6964     case DW_AT_containing_type:
6965       return "DW_AT_containing_type";
6966     case DW_AT_default_value:
6967       return "DW_AT_default_value";
6968     case DW_AT_inline:
6969       return "DW_AT_inline";
6970     case DW_AT_is_optional:
6971       return "DW_AT_is_optional";
6972     case DW_AT_lower_bound:
6973       return "DW_AT_lower_bound";
6974     case DW_AT_producer:
6975       return "DW_AT_producer";
6976     case DW_AT_prototyped:
6977       return "DW_AT_prototyped";
6978     case DW_AT_return_addr:
6979       return "DW_AT_return_addr";
6980     case DW_AT_start_scope:
6981       return "DW_AT_start_scope";
6982     case DW_AT_bit_stride:
6983       return "DW_AT_bit_stride";
6984     case DW_AT_upper_bound:
6985       return "DW_AT_upper_bound";
6986     case DW_AT_abstract_origin:
6987       return "DW_AT_abstract_origin";
6988     case DW_AT_accessibility:
6989       return "DW_AT_accessibility";
6990     case DW_AT_address_class:
6991       return "DW_AT_address_class";
6992     case DW_AT_artificial:
6993       return "DW_AT_artificial";
6994     case DW_AT_base_types:
6995       return "DW_AT_base_types";
6996     case DW_AT_calling_convention:
6997       return "DW_AT_calling_convention";
6998     case DW_AT_count:
6999       return "DW_AT_count";
7000     case DW_AT_data_member_location:
7001       return "DW_AT_data_member_location";
7002     case DW_AT_decl_column:
7003       return "DW_AT_decl_column";
7004     case DW_AT_decl_file:
7005       return "DW_AT_decl_file";
7006     case DW_AT_decl_line:
7007       return "DW_AT_decl_line";
7008     case DW_AT_declaration:
7009       return "DW_AT_declaration";
7010     case DW_AT_discr_list:
7011       return "DW_AT_discr_list";
7012     case DW_AT_encoding:
7013       return "DW_AT_encoding";
7014     case DW_AT_external:
7015       return "DW_AT_external";
7016     case DW_AT_explicit:
7017       return "DW_AT_explicit";
7018     case DW_AT_frame_base:
7019       return "DW_AT_frame_base";
7020     case DW_AT_friend:
7021       return "DW_AT_friend";
7022     case DW_AT_identifier_case:
7023       return "DW_AT_identifier_case";
7024     case DW_AT_macro_info:
7025       return "DW_AT_macro_info";
7026     case DW_AT_namelist_items:
7027       return "DW_AT_namelist_items";
7028     case DW_AT_priority:
7029       return "DW_AT_priority";
7030     case DW_AT_segment:
7031       return "DW_AT_segment";
7032     case DW_AT_specification:
7033       return "DW_AT_specification";
7034     case DW_AT_static_link:
7035       return "DW_AT_static_link";
7036     case DW_AT_type:
7037       return "DW_AT_type";
7038     case DW_AT_use_location:
7039       return "DW_AT_use_location";
7040     case DW_AT_variable_parameter:
7041       return "DW_AT_variable_parameter";
7042     case DW_AT_virtuality:
7043       return "DW_AT_virtuality";
7044     case DW_AT_vtable_elem_location:
7045       return "DW_AT_vtable_elem_location";
7046
7047     case DW_AT_allocated:
7048       return "DW_AT_allocated";
7049     case DW_AT_associated:
7050       return "DW_AT_associated";
7051     case DW_AT_data_location:
7052       return "DW_AT_data_location";
7053     case DW_AT_byte_stride:
7054       return "DW_AT_byte_stride";
7055     case DW_AT_entry_pc:
7056       return "DW_AT_entry_pc";
7057     case DW_AT_use_UTF8:
7058       return "DW_AT_use_UTF8";
7059     case DW_AT_extension:
7060       return "DW_AT_extension";
7061     case DW_AT_ranges:
7062       return "DW_AT_ranges";
7063     case DW_AT_trampoline:
7064       return "DW_AT_trampoline";
7065     case DW_AT_call_column:
7066       return "DW_AT_call_column";
7067     case DW_AT_call_file:
7068       return "DW_AT_call_file";
7069     case DW_AT_call_line:
7070       return "DW_AT_call_line";
7071     case DW_AT_object_pointer:
7072       return "DW_AT_object_pointer";
7073
7074     case DW_AT_signature:
7075       return "DW_AT_signature";
7076     case DW_AT_main_subprogram:
7077       return "DW_AT_main_subprogram";
7078     case DW_AT_data_bit_offset:
7079       return "DW_AT_data_bit_offset";
7080     case DW_AT_const_expr:
7081       return "DW_AT_const_expr";
7082     case DW_AT_enum_class:
7083       return "DW_AT_enum_class";
7084     case DW_AT_linkage_name:
7085       return "DW_AT_linkage_name";
7086
7087     case DW_AT_MIPS_fde:
7088       return "DW_AT_MIPS_fde";
7089     case DW_AT_MIPS_loop_begin:
7090       return "DW_AT_MIPS_loop_begin";
7091     case DW_AT_MIPS_tail_loop_begin:
7092       return "DW_AT_MIPS_tail_loop_begin";
7093     case DW_AT_MIPS_epilog_begin:
7094       return "DW_AT_MIPS_epilog_begin";
7095 #if VMS_DEBUGGING_INFO
7096     case DW_AT_HP_prologue:
7097       return "DW_AT_HP_prologue";
7098 #else
7099     case DW_AT_MIPS_loop_unroll_factor:
7100       return "DW_AT_MIPS_loop_unroll_factor";
7101 #endif
7102     case DW_AT_MIPS_software_pipeline_depth:
7103       return "DW_AT_MIPS_software_pipeline_depth";
7104     case DW_AT_MIPS_linkage_name:
7105       return "DW_AT_MIPS_linkage_name";
7106 #if VMS_DEBUGGING_INFO
7107     case DW_AT_HP_epilogue:
7108       return "DW_AT_HP_epilogue";
7109 #else
7110     case DW_AT_MIPS_stride:
7111       return "DW_AT_MIPS_stride";
7112 #endif
7113     case DW_AT_MIPS_abstract_name:
7114       return "DW_AT_MIPS_abstract_name";
7115     case DW_AT_MIPS_clone_origin:
7116       return "DW_AT_MIPS_clone_origin";
7117     case DW_AT_MIPS_has_inlines:
7118       return "DW_AT_MIPS_has_inlines";
7119
7120     case DW_AT_sf_names:
7121       return "DW_AT_sf_names";
7122     case DW_AT_src_info:
7123       return "DW_AT_src_info";
7124     case DW_AT_mac_info:
7125       return "DW_AT_mac_info";
7126     case DW_AT_src_coords:
7127       return "DW_AT_src_coords";
7128     case DW_AT_body_begin:
7129       return "DW_AT_body_begin";
7130     case DW_AT_body_end:
7131       return "DW_AT_body_end";
7132     case DW_AT_GNU_vector:
7133       return "DW_AT_GNU_vector";
7134     case DW_AT_GNU_guarded_by:
7135       return "DW_AT_GNU_guarded_by";
7136     case DW_AT_GNU_pt_guarded_by:
7137       return "DW_AT_GNU_pt_guarded_by";
7138     case DW_AT_GNU_guarded:
7139       return "DW_AT_GNU_guarded";
7140     case DW_AT_GNU_pt_guarded:
7141       return "DW_AT_GNU_pt_guarded";
7142     case DW_AT_GNU_locks_excluded:
7143       return "DW_AT_GNU_locks_excluded";
7144     case DW_AT_GNU_exclusive_locks_required:
7145       return "DW_AT_GNU_exclusive_locks_required";
7146     case DW_AT_GNU_shared_locks_required:
7147       return "DW_AT_GNU_shared_locks_required";
7148     case DW_AT_GNU_odr_signature:
7149       return "DW_AT_GNU_odr_signature";
7150     case DW_AT_GNU_template_name:
7151       return "DW_AT_GNU_template_name";
7152     case DW_AT_GNU_call_site_value:
7153       return "DW_AT_GNU_call_site_value";
7154     case DW_AT_GNU_call_site_data_value:
7155       return "DW_AT_GNU_call_site_data_value";
7156     case DW_AT_GNU_call_site_target:
7157       return "DW_AT_GNU_call_site_target";
7158     case DW_AT_GNU_call_site_target_clobbered:
7159       return "DW_AT_GNU_call_site_target_clobbered";
7160     case DW_AT_GNU_tail_call:
7161       return "DW_AT_GNU_tail_call";
7162     case DW_AT_GNU_all_tail_call_sites:
7163       return "DW_AT_GNU_all_tail_call_sites";
7164     case DW_AT_GNU_all_call_sites:
7165       return "DW_AT_GNU_all_call_sites";
7166     case DW_AT_GNU_all_source_call_sites:
7167       return "DW_AT_GNU_all_source_call_sites";
7168
7169     case DW_AT_VMS_rtnbeg_pd_address:
7170       return "DW_AT_VMS_rtnbeg_pd_address";
7171
7172     default:
7173       return "DW_AT_<unknown>";
7174     }
7175 }
7176
7177 /* Convert a DWARF value form code into its string name.  */
7178
7179 static const char *
7180 dwarf_form_name (unsigned int form)
7181 {
7182   switch (form)
7183     {
7184     case DW_FORM_addr:
7185       return "DW_FORM_addr";
7186     case DW_FORM_block2:
7187       return "DW_FORM_block2";
7188     case DW_FORM_block4:
7189       return "DW_FORM_block4";
7190     case DW_FORM_data2:
7191       return "DW_FORM_data2";
7192     case DW_FORM_data4:
7193       return "DW_FORM_data4";
7194     case DW_FORM_data8:
7195       return "DW_FORM_data8";
7196     case DW_FORM_string:
7197       return "DW_FORM_string";
7198     case DW_FORM_block:
7199       return "DW_FORM_block";
7200     case DW_FORM_block1:
7201       return "DW_FORM_block1";
7202     case DW_FORM_data1:
7203       return "DW_FORM_data1";
7204     case DW_FORM_flag:
7205       return "DW_FORM_flag";
7206     case DW_FORM_sdata:
7207       return "DW_FORM_sdata";
7208     case DW_FORM_strp:
7209       return "DW_FORM_strp";
7210     case DW_FORM_udata:
7211       return "DW_FORM_udata";
7212     case DW_FORM_ref_addr:
7213       return "DW_FORM_ref_addr";
7214     case DW_FORM_ref1:
7215       return "DW_FORM_ref1";
7216     case DW_FORM_ref2:
7217       return "DW_FORM_ref2";
7218     case DW_FORM_ref4:
7219       return "DW_FORM_ref4";
7220     case DW_FORM_ref8:
7221       return "DW_FORM_ref8";
7222     case DW_FORM_ref_udata:
7223       return "DW_FORM_ref_udata";
7224     case DW_FORM_indirect:
7225       return "DW_FORM_indirect";
7226     case DW_FORM_sec_offset:
7227       return "DW_FORM_sec_offset";
7228     case DW_FORM_exprloc:
7229       return "DW_FORM_exprloc";
7230     case DW_FORM_flag_present:
7231       return "DW_FORM_flag_present";
7232     case DW_FORM_ref_sig8:
7233       return "DW_FORM_ref_sig8";
7234     default:
7235       return "DW_FORM_<unknown>";
7236     }
7237 }
7238 \f
7239 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7240    instance of an inlined instance of a decl which is local to an inline
7241    function, so we have to trace all of the way back through the origin chain
7242    to find out what sort of node actually served as the original seed for the
7243    given block.  */
7244
7245 static tree
7246 decl_ultimate_origin (const_tree decl)
7247 {
7248   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7249     return NULL_TREE;
7250
7251   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7252      nodes in the function to point to themselves; ignore that if
7253      we're trying to output the abstract instance of this function.  */
7254   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7255     return NULL_TREE;
7256
7257   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7258      most distant ancestor, this should never happen.  */
7259   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7260
7261   return DECL_ABSTRACT_ORIGIN (decl);
7262 }
7263
7264 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7265    of a virtual function may refer to a base class, so we check the 'this'
7266    parameter.  */
7267
7268 static tree
7269 decl_class_context (tree decl)
7270 {
7271   tree context = NULL_TREE;
7272
7273   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7274     context = DECL_CONTEXT (decl);
7275   else
7276     context = TYPE_MAIN_VARIANT
7277       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7278
7279   if (context && !TYPE_P (context))
7280     context = NULL_TREE;
7281
7282   return context;
7283 }
7284 \f
7285 /* Add an attribute/value pair to a DIE.  */
7286
7287 static inline void
7288 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7289 {
7290   /* Maybe this should be an assert?  */
7291   if (die == NULL)
7292     return;
7293
7294   if (die->die_attr == NULL)
7295     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7296   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7297 }
7298
7299 static inline enum dw_val_class
7300 AT_class (dw_attr_ref a)
7301 {
7302   return a->dw_attr_val.val_class;
7303 }
7304
7305 /* Add a flag value attribute to a DIE.  */
7306
7307 static inline void
7308 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7309 {
7310   dw_attr_node attr;
7311
7312   attr.dw_attr = attr_kind;
7313   attr.dw_attr_val.val_class = dw_val_class_flag;
7314   attr.dw_attr_val.v.val_flag = flag;
7315   add_dwarf_attr (die, &attr);
7316 }
7317
7318 static inline unsigned
7319 AT_flag (dw_attr_ref a)
7320 {
7321   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7322   return a->dw_attr_val.v.val_flag;
7323 }
7324
7325 /* Add a signed integer attribute value to a DIE.  */
7326
7327 static inline void
7328 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7329 {
7330   dw_attr_node attr;
7331
7332   attr.dw_attr = attr_kind;
7333   attr.dw_attr_val.val_class = dw_val_class_const;
7334   attr.dw_attr_val.v.val_int = int_val;
7335   add_dwarf_attr (die, &attr);
7336 }
7337
7338 static inline HOST_WIDE_INT
7339 AT_int (dw_attr_ref a)
7340 {
7341   gcc_assert (a && AT_class (a) == dw_val_class_const);
7342   return a->dw_attr_val.v.val_int;
7343 }
7344
7345 /* Add an unsigned integer attribute value to a DIE.  */
7346
7347 static inline void
7348 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7349                  unsigned HOST_WIDE_INT unsigned_val)
7350 {
7351   dw_attr_node attr;
7352
7353   attr.dw_attr = attr_kind;
7354   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7355   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7356   add_dwarf_attr (die, &attr);
7357 }
7358
7359 static inline unsigned HOST_WIDE_INT
7360 AT_unsigned (dw_attr_ref a)
7361 {
7362   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7363   return a->dw_attr_val.v.val_unsigned;
7364 }
7365
7366 /* Add an unsigned double integer attribute value to a DIE.  */
7367
7368 static inline void
7369 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7370                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7371 {
7372   dw_attr_node attr;
7373
7374   attr.dw_attr = attr_kind;
7375   attr.dw_attr_val.val_class = dw_val_class_const_double;
7376   attr.dw_attr_val.v.val_double.high = high;
7377   attr.dw_attr_val.v.val_double.low = low;
7378   add_dwarf_attr (die, &attr);
7379 }
7380
7381 /* Add a floating point attribute value to a DIE and return it.  */
7382
7383 static inline void
7384 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7385             unsigned int length, unsigned int elt_size, unsigned char *array)
7386 {
7387   dw_attr_node attr;
7388
7389   attr.dw_attr = attr_kind;
7390   attr.dw_attr_val.val_class = dw_val_class_vec;
7391   attr.dw_attr_val.v.val_vec.length = length;
7392   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7393   attr.dw_attr_val.v.val_vec.array = array;
7394   add_dwarf_attr (die, &attr);
7395 }
7396
7397 /* Add an 8-byte data attribute value to a DIE.  */
7398
7399 static inline void
7400 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7401               unsigned char data8[8])
7402 {
7403   dw_attr_node attr;
7404
7405   attr.dw_attr = attr_kind;
7406   attr.dw_attr_val.val_class = dw_val_class_data8;
7407   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7408   add_dwarf_attr (die, &attr);
7409 }
7410
7411 /* Hash and equality functions for debug_str_hash.  */
7412
7413 static hashval_t
7414 debug_str_do_hash (const void *x)
7415 {
7416   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7417 }
7418
7419 static int
7420 debug_str_eq (const void *x1, const void *x2)
7421 {
7422   return strcmp ((((const struct indirect_string_node *)x1)->str),
7423                  (const char *)x2) == 0;
7424 }
7425
7426 /* Add STR to the indirect string hash table.  */
7427
7428 static struct indirect_string_node *
7429 find_AT_string (const char *str)
7430 {
7431   struct indirect_string_node *node;
7432   void **slot;
7433
7434   if (! debug_str_hash)
7435     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7436                                       debug_str_eq, NULL);
7437
7438   slot = htab_find_slot_with_hash (debug_str_hash, str,
7439                                    htab_hash_string (str), INSERT);
7440   if (*slot == NULL)
7441     {
7442       node = ggc_alloc_cleared_indirect_string_node ();
7443       node->str = ggc_strdup (str);
7444       *slot = node;
7445     }
7446   else
7447     node = (struct indirect_string_node *) *slot;
7448
7449   node->refcount++;
7450   return node;
7451 }
7452
7453 /* Add a string attribute value to a DIE.  */
7454
7455 static inline void
7456 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7457 {
7458   dw_attr_node attr;
7459   struct indirect_string_node *node;
7460
7461   node = find_AT_string (str);
7462
7463   attr.dw_attr = attr_kind;
7464   attr.dw_attr_val.val_class = dw_val_class_str;
7465   attr.dw_attr_val.v.val_str = node;
7466   add_dwarf_attr (die, &attr);
7467 }
7468
7469 /* Create a label for an indirect string node, ensuring it is going to
7470    be output, unless its reference count goes down to zero.  */
7471
7472 static inline void
7473 gen_label_for_indirect_string (struct indirect_string_node *node)
7474 {
7475   char label[32];
7476
7477   if (node->label)
7478     return;
7479
7480   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7481   ++dw2_string_counter;
7482   node->label = xstrdup (label);
7483 }
7484
7485 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7486    debug string STR.  */
7487
7488 static inline rtx
7489 get_debug_string_label (const char *str)
7490 {
7491   struct indirect_string_node *node = find_AT_string (str);
7492
7493   debug_str_hash_forced = true;
7494
7495   gen_label_for_indirect_string (node);
7496
7497   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7498 }
7499
7500 static inline const char *
7501 AT_string (dw_attr_ref a)
7502 {
7503   gcc_assert (a && AT_class (a) == dw_val_class_str);
7504   return a->dw_attr_val.v.val_str->str;
7505 }
7506
7507 /* Find out whether a string should be output inline in DIE
7508    or out-of-line in .debug_str section.  */
7509
7510 static enum dwarf_form
7511 AT_string_form (dw_attr_ref a)
7512 {
7513   struct indirect_string_node *node;
7514   unsigned int len;
7515
7516   gcc_assert (a && AT_class (a) == dw_val_class_str);
7517
7518   node = a->dw_attr_val.v.val_str;
7519   if (node->form)
7520     return node->form;
7521
7522   len = strlen (node->str) + 1;
7523
7524   /* If the string is shorter or equal to the size of the reference, it is
7525      always better to put it inline.  */
7526   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7527     return node->form = DW_FORM_string;
7528
7529   /* If we cannot expect the linker to merge strings in .debug_str
7530      section, only put it into .debug_str if it is worth even in this
7531      single module.  */
7532   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7533       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7534       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7535     return node->form = DW_FORM_string;
7536
7537   gen_label_for_indirect_string (node);
7538
7539   return node->form = DW_FORM_strp;
7540 }
7541
7542 /* Add a DIE reference attribute value to a DIE.  */
7543
7544 static inline void
7545 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7546 {
7547   dw_attr_node attr;
7548
7549 #ifdef ENABLE_CHECKING
7550   gcc_assert (targ_die != NULL);
7551 #else
7552   /* With LTO we can end up trying to reference something we didn't create
7553      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7554   if (targ_die == NULL)
7555     return;
7556 #endif
7557
7558   attr.dw_attr = attr_kind;
7559   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7560   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7561   attr.dw_attr_val.v.val_die_ref.external = 0;
7562   add_dwarf_attr (die, &attr);
7563 }
7564
7565 /* Add an AT_specification attribute to a DIE, and also make the back
7566    pointer from the specification to the definition.  */
7567
7568 static inline void
7569 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7570 {
7571   add_AT_die_ref (die, DW_AT_specification, targ_die);
7572   gcc_assert (!targ_die->die_definition);
7573   targ_die->die_definition = die;
7574 }
7575
7576 static inline dw_die_ref
7577 AT_ref (dw_attr_ref a)
7578 {
7579   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7580   return a->dw_attr_val.v.val_die_ref.die;
7581 }
7582
7583 static inline int
7584 AT_ref_external (dw_attr_ref a)
7585 {
7586   if (a && AT_class (a) == dw_val_class_die_ref)
7587     return a->dw_attr_val.v.val_die_ref.external;
7588
7589   return 0;
7590 }
7591
7592 static inline void
7593 set_AT_ref_external (dw_attr_ref a, int i)
7594 {
7595   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7596   a->dw_attr_val.v.val_die_ref.external = i;
7597 }
7598
7599 /* Add an FDE reference attribute value to a DIE.  */
7600
7601 static inline void
7602 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7603 {
7604   dw_attr_node attr;
7605
7606   attr.dw_attr = attr_kind;
7607   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7608   attr.dw_attr_val.v.val_fde_index = targ_fde;
7609   add_dwarf_attr (die, &attr);
7610 }
7611
7612 /* Add a location description attribute value to a DIE.  */
7613
7614 static inline void
7615 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7616 {
7617   dw_attr_node attr;
7618
7619   attr.dw_attr = attr_kind;
7620   attr.dw_attr_val.val_class = dw_val_class_loc;
7621   attr.dw_attr_val.v.val_loc = loc;
7622   add_dwarf_attr (die, &attr);
7623 }
7624
7625 static inline dw_loc_descr_ref
7626 AT_loc (dw_attr_ref a)
7627 {
7628   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7629   return a->dw_attr_val.v.val_loc;
7630 }
7631
7632 static inline void
7633 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7634 {
7635   dw_attr_node attr;
7636
7637   attr.dw_attr = attr_kind;
7638   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7639   attr.dw_attr_val.v.val_loc_list = loc_list;
7640   add_dwarf_attr (die, &attr);
7641   have_location_lists = true;
7642 }
7643
7644 static inline dw_loc_list_ref
7645 AT_loc_list (dw_attr_ref a)
7646 {
7647   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7648   return a->dw_attr_val.v.val_loc_list;
7649 }
7650
7651 static inline dw_loc_list_ref *
7652 AT_loc_list_ptr (dw_attr_ref a)
7653 {
7654   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7655   return &a->dw_attr_val.v.val_loc_list;
7656 }
7657
7658 /* Add an address constant attribute value to a DIE.  */
7659
7660 static inline void
7661 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7662 {
7663   dw_attr_node attr;
7664
7665   attr.dw_attr = attr_kind;
7666   attr.dw_attr_val.val_class = dw_val_class_addr;
7667   attr.dw_attr_val.v.val_addr = addr;
7668   add_dwarf_attr (die, &attr);
7669 }
7670
7671 /* Get the RTX from to an address DIE attribute.  */
7672
7673 static inline rtx
7674 AT_addr (dw_attr_ref a)
7675 {
7676   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7677   return a->dw_attr_val.v.val_addr;
7678 }
7679
7680 /* Add a file attribute value to a DIE.  */
7681
7682 static inline void
7683 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7684              struct dwarf_file_data *fd)
7685 {
7686   dw_attr_node attr;
7687
7688   attr.dw_attr = attr_kind;
7689   attr.dw_attr_val.val_class = dw_val_class_file;
7690   attr.dw_attr_val.v.val_file = fd;
7691   add_dwarf_attr (die, &attr);
7692 }
7693
7694 /* Get the dwarf_file_data from a file DIE attribute.  */
7695
7696 static inline struct dwarf_file_data *
7697 AT_file (dw_attr_ref a)
7698 {
7699   gcc_assert (a && AT_class (a) == dw_val_class_file);
7700   return a->dw_attr_val.v.val_file;
7701 }
7702
7703 /* Add a vms delta attribute value to a DIE.  */
7704
7705 static inline void
7706 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7707                   const char *lbl1, const char *lbl2)
7708 {
7709   dw_attr_node attr;
7710
7711   attr.dw_attr = attr_kind;
7712   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7713   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7714   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7715   add_dwarf_attr (die, &attr);
7716 }
7717
7718 /* Add a label identifier attribute value to a DIE.  */
7719
7720 static inline void
7721 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7722 {
7723   dw_attr_node attr;
7724
7725   attr.dw_attr = attr_kind;
7726   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7727   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7728   add_dwarf_attr (die, &attr);
7729 }
7730
7731 /* Add a section offset attribute value to a DIE, an offset into the
7732    debug_line section.  */
7733
7734 static inline void
7735 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7736                 const char *label)
7737 {
7738   dw_attr_node attr;
7739
7740   attr.dw_attr = attr_kind;
7741   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7742   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7743   add_dwarf_attr (die, &attr);
7744 }
7745
7746 /* Add a section offset attribute value to a DIE, an offset into the
7747    debug_macinfo section.  */
7748
7749 static inline void
7750 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7751                const char *label)
7752 {
7753   dw_attr_node attr;
7754
7755   attr.dw_attr = attr_kind;
7756   attr.dw_attr_val.val_class = dw_val_class_macptr;
7757   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7758   add_dwarf_attr (die, &attr);
7759 }
7760
7761 /* Add an offset attribute value to a DIE.  */
7762
7763 static inline void
7764 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7765                unsigned HOST_WIDE_INT offset)
7766 {
7767   dw_attr_node attr;
7768
7769   attr.dw_attr = attr_kind;
7770   attr.dw_attr_val.val_class = dw_val_class_offset;
7771   attr.dw_attr_val.v.val_offset = offset;
7772   add_dwarf_attr (die, &attr);
7773 }
7774
7775 /* Add an range_list attribute value to a DIE.  */
7776
7777 static void
7778 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7779                    long unsigned int offset)
7780 {
7781   dw_attr_node attr;
7782
7783   attr.dw_attr = attr_kind;
7784   attr.dw_attr_val.val_class = dw_val_class_range_list;
7785   attr.dw_attr_val.v.val_offset = offset;
7786   add_dwarf_attr (die, &attr);
7787 }
7788
7789 /* Return the start label of a delta attribute.  */
7790
7791 static inline const char *
7792 AT_vms_delta1 (dw_attr_ref a)
7793 {
7794   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7795   return a->dw_attr_val.v.val_vms_delta.lbl1;
7796 }
7797
7798 /* Return the end label of a delta attribute.  */
7799
7800 static inline const char *
7801 AT_vms_delta2 (dw_attr_ref a)
7802 {
7803   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7804   return a->dw_attr_val.v.val_vms_delta.lbl2;
7805 }
7806
7807 static inline const char *
7808 AT_lbl (dw_attr_ref a)
7809 {
7810   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7811                     || AT_class (a) == dw_val_class_lineptr
7812                     || AT_class (a) == dw_val_class_macptr));
7813   return a->dw_attr_val.v.val_lbl_id;
7814 }
7815
7816 /* Get the attribute of type attr_kind.  */
7817
7818 static dw_attr_ref
7819 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7820 {
7821   dw_attr_ref a;
7822   unsigned ix;
7823   dw_die_ref spec = NULL;
7824
7825   if (! die)
7826     return NULL;
7827
7828   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7829     if (a->dw_attr == attr_kind)
7830       return a;
7831     else if (a->dw_attr == DW_AT_specification
7832              || a->dw_attr == DW_AT_abstract_origin)
7833       spec = AT_ref (a);
7834
7835   if (spec)
7836     return get_AT (spec, attr_kind);
7837
7838   return NULL;
7839 }
7840
7841 /* Return the "low pc" attribute value, typically associated with a subprogram
7842    DIE.  Return null if the "low pc" attribute is either not present, or if it
7843    cannot be represented as an assembler label identifier.  */
7844
7845 static inline const char *
7846 get_AT_low_pc (dw_die_ref die)
7847 {
7848   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7849
7850   return a ? AT_lbl (a) : NULL;
7851 }
7852
7853 /* Return the "high pc" attribute value, typically associated with a subprogram
7854    DIE.  Return null if the "high pc" attribute is either not present, or if it
7855    cannot be represented as an assembler label identifier.  */
7856
7857 static inline const char *
7858 get_AT_hi_pc (dw_die_ref die)
7859 {
7860   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7861
7862   return a ? AT_lbl (a) : NULL;
7863 }
7864
7865 /* Return the value of the string attribute designated by ATTR_KIND, or
7866    NULL if it is not present.  */
7867
7868 static inline const char *
7869 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7870 {
7871   dw_attr_ref a = get_AT (die, attr_kind);
7872
7873   return a ? AT_string (a) : NULL;
7874 }
7875
7876 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7877    if it is not present.  */
7878
7879 static inline int
7880 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7881 {
7882   dw_attr_ref a = get_AT (die, attr_kind);
7883
7884   return a ? AT_flag (a) : 0;
7885 }
7886
7887 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7888    if it is not present.  */
7889
7890 static inline unsigned
7891 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7892 {
7893   dw_attr_ref a = get_AT (die, attr_kind);
7894
7895   return a ? AT_unsigned (a) : 0;
7896 }
7897
7898 static inline dw_die_ref
7899 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7900 {
7901   dw_attr_ref a = get_AT (die, attr_kind);
7902
7903   return a ? AT_ref (a) : NULL;
7904 }
7905
7906 static inline struct dwarf_file_data *
7907 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7908 {
7909   dw_attr_ref a = get_AT (die, attr_kind);
7910
7911   return a ? AT_file (a) : NULL;
7912 }
7913
7914 /* Return TRUE if the language is C++.  */
7915
7916 static inline bool
7917 is_cxx (void)
7918 {
7919   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7920
7921   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7922 }
7923
7924 /* Return TRUE if the language is Fortran.  */
7925
7926 static inline bool
7927 is_fortran (void)
7928 {
7929   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7930
7931   return (lang == DW_LANG_Fortran77
7932           || lang == DW_LANG_Fortran90
7933           || lang == DW_LANG_Fortran95);
7934 }
7935
7936 /* Return TRUE if the language is Ada.  */
7937
7938 static inline bool
7939 is_ada (void)
7940 {
7941   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7942
7943   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7944 }
7945
7946 /* Remove the specified attribute if present.  */
7947
7948 static void
7949 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7950 {
7951   dw_attr_ref a;
7952   unsigned ix;
7953
7954   if (! die)
7955     return;
7956
7957   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7958     if (a->dw_attr == attr_kind)
7959       {
7960         if (AT_class (a) == dw_val_class_str)
7961           if (a->dw_attr_val.v.val_str->refcount)
7962             a->dw_attr_val.v.val_str->refcount--;
7963
7964         /* VEC_ordered_remove should help reduce the number of abbrevs
7965            that are needed.  */
7966         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7967         return;
7968       }
7969 }
7970
7971 /* Remove CHILD from its parent.  PREV must have the property that
7972    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7973
7974 static void
7975 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7976 {
7977   gcc_assert (child->die_parent == prev->die_parent);
7978   gcc_assert (prev->die_sib == child);
7979   if (prev == child)
7980     {
7981       gcc_assert (child->die_parent->die_child == child);
7982       prev = NULL;
7983     }
7984   else
7985     prev->die_sib = child->die_sib;
7986   if (child->die_parent->die_child == child)
7987     child->die_parent->die_child = prev;
7988 }
7989
7990 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7991    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7992
7993 static void
7994 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7995 {
7996   dw_die_ref parent = old_child->die_parent;
7997
7998   gcc_assert (parent == prev->die_parent);
7999   gcc_assert (prev->die_sib == old_child);
8000
8001   new_child->die_parent = parent;
8002   if (prev == old_child)
8003     {
8004       gcc_assert (parent->die_child == old_child);
8005       new_child->die_sib = new_child;
8006     }
8007   else
8008     {
8009       prev->die_sib = new_child;
8010       new_child->die_sib = old_child->die_sib;
8011     }
8012   if (old_child->die_parent->die_child == old_child)
8013     old_child->die_parent->die_child = new_child;
8014 }
8015
8016 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8017
8018 static void
8019 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8020 {
8021   dw_die_ref c;
8022   new_parent->die_child = old_parent->die_child;
8023   old_parent->die_child = NULL;
8024   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8025 }
8026
8027 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8028    matches TAG.  */
8029
8030 static void
8031 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8032 {
8033   dw_die_ref c;
8034
8035   c = die->die_child;
8036   if (c) do {
8037     dw_die_ref prev = c;
8038     c = c->die_sib;
8039     while (c->die_tag == tag)
8040       {
8041         remove_child_with_prev (c, prev);
8042         /* Might have removed every child.  */
8043         if (c == c->die_sib)
8044           return;
8045         c = c->die_sib;
8046       }
8047   } while (c != die->die_child);
8048 }
8049
8050 /* Add a CHILD_DIE as the last child of DIE.  */
8051
8052 static void
8053 add_child_die (dw_die_ref die, dw_die_ref child_die)
8054 {
8055   /* FIXME this should probably be an assert.  */
8056   if (! die || ! child_die)
8057     return;
8058   gcc_assert (die != child_die);
8059
8060   child_die->die_parent = die;
8061   if (die->die_child)
8062     {
8063       child_die->die_sib = die->die_child->die_sib;
8064       die->die_child->die_sib = child_die;
8065     }
8066   else
8067     child_die->die_sib = child_die;
8068   die->die_child = child_die;
8069 }
8070
8071 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8072    is the specification, to the end of PARENT's list of children.
8073    This is done by removing and re-adding it.  */
8074
8075 static void
8076 splice_child_die (dw_die_ref parent, dw_die_ref child)
8077 {
8078   dw_die_ref p;
8079
8080   /* We want the declaration DIE from inside the class, not the
8081      specification DIE at toplevel.  */
8082   if (child->die_parent != parent)
8083     {
8084       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8085
8086       if (tmp)
8087         child = tmp;
8088     }
8089
8090   gcc_assert (child->die_parent == parent
8091               || (child->die_parent
8092                   == get_AT_ref (parent, DW_AT_specification)));
8093
8094   for (p = child->die_parent->die_child; ; p = p->die_sib)
8095     if (p->die_sib == child)
8096       {
8097         remove_child_with_prev (child, p);
8098         break;
8099       }
8100
8101   add_child_die (parent, child);
8102 }
8103
8104 /* Return a pointer to a newly created DIE node.  */
8105
8106 static inline dw_die_ref
8107 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8108 {
8109   dw_die_ref die = ggc_alloc_cleared_die_node ();
8110
8111   die->die_tag = tag_value;
8112
8113   if (parent_die != NULL)
8114     add_child_die (parent_die, die);
8115   else
8116     {
8117       limbo_die_node *limbo_node;
8118
8119       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8120       limbo_node->die = die;
8121       limbo_node->created_for = t;
8122       limbo_node->next = limbo_die_list;
8123       limbo_die_list = limbo_node;
8124     }
8125
8126   return die;
8127 }
8128
8129 /* Return the DIE associated with the given type specifier.  */
8130
8131 static inline dw_die_ref
8132 lookup_type_die (tree type)
8133 {
8134   return TYPE_SYMTAB_DIE (type);
8135 }
8136
8137 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8138    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8139    anonymous type instead the one of the naming typedef.  */
8140
8141 static inline dw_die_ref
8142 strip_naming_typedef (tree type, dw_die_ref type_die)
8143 {
8144   if (type
8145       && TREE_CODE (type) == RECORD_TYPE
8146       && type_die
8147       && type_die->die_tag == DW_TAG_typedef
8148       && is_naming_typedef_decl (TYPE_NAME (type)))
8149     type_die = get_AT_ref (type_die, DW_AT_type);
8150   return type_die;
8151 }
8152
8153 /* Like lookup_type_die, but if type is an anonymous type named by a
8154    typedef[1], return the DIE of the anonymous type instead the one of
8155    the naming typedef.  This is because in gen_typedef_die, we did
8156    equate the anonymous struct named by the typedef with the DIE of
8157    the naming typedef. So by default, lookup_type_die on an anonymous
8158    struct yields the DIE of the naming typedef.
8159
8160    [1]: Read the comment of is_naming_typedef_decl to learn about what
8161    a naming typedef is.  */
8162
8163 static inline dw_die_ref
8164 lookup_type_die_strip_naming_typedef (tree type)
8165 {
8166   dw_die_ref die = lookup_type_die (type);
8167   return strip_naming_typedef (type, die);
8168 }
8169
8170 /* Equate a DIE to a given type specifier.  */
8171
8172 static inline void
8173 equate_type_number_to_die (tree type, dw_die_ref type_die)
8174 {
8175   TYPE_SYMTAB_DIE (type) = type_die;
8176 }
8177
8178 /* Returns a hash value for X (which really is a die_struct).  */
8179
8180 static hashval_t
8181 decl_die_table_hash (const void *x)
8182 {
8183   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8184 }
8185
8186 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8187
8188 static int
8189 decl_die_table_eq (const void *x, const void *y)
8190 {
8191   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8192 }
8193
8194 /* Return the DIE associated with a given declaration.  */
8195
8196 static inline dw_die_ref
8197 lookup_decl_die (tree decl)
8198 {
8199   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8200 }
8201
8202 /* Returns a hash value for X (which really is a var_loc_list).  */
8203
8204 static hashval_t
8205 decl_loc_table_hash (const void *x)
8206 {
8207   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8208 }
8209
8210 /* Return nonzero if decl_id of var_loc_list X is the same as
8211    UID of decl *Y.  */
8212
8213 static int
8214 decl_loc_table_eq (const void *x, const void *y)
8215 {
8216   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8217 }
8218
8219 /* Return the var_loc list associated with a given declaration.  */
8220
8221 static inline var_loc_list *
8222 lookup_decl_loc (const_tree decl)
8223 {
8224   if (!decl_loc_table)
8225     return NULL;
8226   return (var_loc_list *)
8227     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8228 }
8229
8230 /* Equate a DIE to a particular declaration.  */
8231
8232 static void
8233 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8234 {
8235   unsigned int decl_id = DECL_UID (decl);
8236   void **slot;
8237
8238   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8239   *slot = decl_die;
8240   decl_die->decl_id = decl_id;
8241 }
8242
8243 /* Return how many bits covers PIECE EXPR_LIST.  */
8244
8245 static int
8246 decl_piece_bitsize (rtx piece)
8247 {
8248   int ret = (int) GET_MODE (piece);
8249   if (ret)
8250     return ret;
8251   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8252               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8253   return INTVAL (XEXP (XEXP (piece, 0), 0));
8254 }
8255
8256 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8257
8258 static rtx *
8259 decl_piece_varloc_ptr (rtx piece)
8260 {
8261   if ((int) GET_MODE (piece))
8262     return &XEXP (piece, 0);
8263   else
8264     return &XEXP (XEXP (piece, 0), 1);
8265 }
8266
8267 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8268    Next is the chain of following piece nodes.  */
8269
8270 static rtx
8271 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8272 {
8273   if (bitsize <= (int) MAX_MACHINE_MODE)
8274     return alloc_EXPR_LIST (bitsize, loc_note, next);
8275   else
8276     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8277                                                GEN_INT (bitsize),
8278                                                loc_note), next);
8279 }
8280
8281 /* Return rtx that should be stored into loc field for
8282    LOC_NOTE and BITPOS/BITSIZE.  */
8283
8284 static rtx
8285 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8286                       HOST_WIDE_INT bitsize)
8287 {
8288   if (bitsize != -1)
8289     {
8290       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8291       if (bitpos != 0)
8292         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8293     }
8294   return loc_note;
8295 }
8296
8297 /* This function either modifies location piece list *DEST in
8298    place (if SRC and INNER is NULL), or copies location piece list
8299    *SRC to *DEST while modifying it.  Location BITPOS is modified
8300    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8301    not copied and if needed some padding around it is added.
8302    When modifying in place, DEST should point to EXPR_LIST where
8303    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8304    to the start of the whole list and INNER points to the EXPR_LIST
8305    where earlier pieces cover PIECE_BITPOS bits.  */
8306
8307 static void
8308 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8309                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8310                    HOST_WIDE_INT bitsize, rtx loc_note)
8311 {
8312   int diff;
8313   bool copy = inner != NULL;
8314
8315   if (copy)
8316     {
8317       /* First copy all nodes preceeding the current bitpos.  */
8318       while (src != inner)
8319         {
8320           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8321                                    decl_piece_bitsize (*src), NULL_RTX);
8322           dest = &XEXP (*dest, 1);
8323           src = &XEXP (*src, 1);
8324         }
8325     }
8326   /* Add padding if needed.  */
8327   if (bitpos != piece_bitpos)
8328     {
8329       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8330                                copy ? NULL_RTX : *dest);
8331       dest = &XEXP (*dest, 1);
8332     }
8333   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8334     {
8335       gcc_assert (!copy);
8336       /* A piece with correct bitpos and bitsize already exist,
8337          just update the location for it and return.  */
8338       *decl_piece_varloc_ptr (*dest) = loc_note;
8339       return;
8340     }
8341   /* Add the piece that changed.  */
8342   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8343   dest = &XEXP (*dest, 1);
8344   /* Skip over pieces that overlap it.  */
8345   diff = bitpos - piece_bitpos + bitsize;
8346   if (!copy)
8347     src = dest;
8348   while (diff > 0 && *src)
8349     {
8350       rtx piece = *src;
8351       diff -= decl_piece_bitsize (piece);
8352       if (copy)
8353         src = &XEXP (piece, 1);
8354       else
8355         {
8356           *src = XEXP (piece, 1);
8357           free_EXPR_LIST_node (piece);
8358         }
8359     }
8360   /* Add padding if needed.  */
8361   if (diff < 0 && *src)
8362     {
8363       if (!copy)
8364         dest = src;
8365       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8366       dest = &XEXP (*dest, 1);
8367     }
8368   if (!copy)
8369     return;
8370   /* Finally copy all nodes following it.  */
8371   while (*src)
8372     {
8373       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8374                                decl_piece_bitsize (*src), NULL_RTX);
8375       dest = &XEXP (*dest, 1);
8376       src = &XEXP (*src, 1);
8377     }
8378 }
8379
8380 /* Add a variable location node to the linked list for DECL.  */
8381
8382 static struct var_loc_node *
8383 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8384 {
8385   unsigned int decl_id;
8386   var_loc_list *temp;
8387   void **slot;
8388   struct var_loc_node *loc = NULL;
8389   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8390
8391   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8392     {
8393       tree realdecl = DECL_DEBUG_EXPR (decl);
8394       if (realdecl && handled_component_p (realdecl))
8395         {
8396           HOST_WIDE_INT maxsize;
8397           tree innerdecl;
8398           innerdecl
8399             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8400           if (!DECL_P (innerdecl)
8401               || DECL_IGNORED_P (innerdecl)
8402               || TREE_STATIC (innerdecl)
8403               || bitsize <= 0
8404               || bitpos + bitsize > 256
8405               || bitsize != maxsize)
8406             return NULL;
8407           decl = innerdecl;
8408         }
8409     }
8410
8411   decl_id = DECL_UID (decl);
8412   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8413   if (*slot == NULL)
8414     {
8415       temp = ggc_alloc_cleared_var_loc_list ();
8416       temp->decl_id = decl_id;
8417       *slot = temp;
8418     }
8419   else
8420     temp = (var_loc_list *) *slot;
8421
8422   if (temp->last)
8423     {
8424       struct var_loc_node *last = temp->last, *unused = NULL;
8425       rtx *piece_loc = NULL, last_loc_note;
8426       int piece_bitpos = 0;
8427       if (last->next)
8428         {
8429           last = last->next;
8430           gcc_assert (last->next == NULL);
8431         }
8432       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8433         {
8434           piece_loc = &last->loc;
8435           do
8436             {
8437               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8438               if (piece_bitpos + cur_bitsize > bitpos)
8439                 break;
8440               piece_bitpos += cur_bitsize;
8441               piece_loc = &XEXP (*piece_loc, 1);
8442             }
8443           while (*piece_loc);
8444         }
8445       /* TEMP->LAST here is either pointer to the last but one or
8446          last element in the chained list, LAST is pointer to the
8447          last element.  */
8448       if (label && strcmp (last->label, label) == 0)
8449         {
8450           /* For SRA optimized variables if there weren't any real
8451              insns since last note, just modify the last node.  */
8452           if (piece_loc != NULL)
8453             {
8454               adjust_piece_list (piece_loc, NULL, NULL,
8455                                  bitpos, piece_bitpos, bitsize, loc_note);
8456               return NULL;
8457             }
8458           /* If the last note doesn't cover any instructions, remove it.  */
8459           if (temp->last != last)
8460             {
8461               temp->last->next = NULL;
8462               unused = last;
8463               last = temp->last;
8464               gcc_assert (strcmp (last->label, label) != 0);
8465             }
8466           else
8467             {
8468               gcc_assert (temp->first == temp->last);
8469               memset (temp->last, '\0', sizeof (*temp->last));
8470               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8471               return temp->last;
8472             }
8473         }
8474       if (bitsize == -1 && NOTE_P (last->loc))
8475         last_loc_note = last->loc;
8476       else if (piece_loc != NULL
8477                && *piece_loc != NULL_RTX
8478                && piece_bitpos == bitpos
8479                && decl_piece_bitsize (*piece_loc) == bitsize)
8480         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8481       else
8482         last_loc_note = NULL_RTX;
8483       /* If the current location is the same as the end of the list,
8484          and either both or neither of the locations is uninitialized,
8485          we have nothing to do.  */
8486       if (last_loc_note == NULL_RTX
8487           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8488                             NOTE_VAR_LOCATION_LOC (loc_note)))
8489           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8490                != NOTE_VAR_LOCATION_STATUS (loc_note))
8491               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8492                    == VAR_INIT_STATUS_UNINITIALIZED)
8493                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8494                       == VAR_INIT_STATUS_UNINITIALIZED))))
8495         {
8496           /* Add LOC to the end of list and update LAST.  If the last
8497              element of the list has been removed above, reuse its
8498              memory for the new node, otherwise allocate a new one.  */
8499           if (unused)
8500             {
8501               loc = unused;
8502               memset (loc, '\0', sizeof (*loc));
8503             }
8504           else
8505             loc = ggc_alloc_cleared_var_loc_node ();
8506           if (bitsize == -1 || piece_loc == NULL)
8507             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8508           else
8509             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8510                                bitpos, piece_bitpos, bitsize, loc_note);
8511           last->next = loc;
8512           /* Ensure TEMP->LAST will point either to the new last but one
8513              element of the chain, or to the last element in it.  */
8514           if (last != temp->last)
8515             temp->last = last;
8516         }
8517       else if (unused)
8518         ggc_free (unused);
8519     }
8520   else
8521     {
8522       loc = ggc_alloc_cleared_var_loc_node ();
8523       temp->first = loc;
8524       temp->last = loc;
8525       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8526     }
8527   return loc;
8528 }
8529 \f
8530 /* Keep track of the number of spaces used to indent the
8531    output of the debugging routines that print the structure of
8532    the DIE internal representation.  */
8533 static int print_indent;
8534
8535 /* Indent the line the number of spaces given by print_indent.  */
8536
8537 static inline void
8538 print_spaces (FILE *outfile)
8539 {
8540   fprintf (outfile, "%*s", print_indent, "");
8541 }
8542
8543 /* Print a type signature in hex.  */
8544
8545 static inline void
8546 print_signature (FILE *outfile, char *sig)
8547 {
8548   int i;
8549
8550   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8551     fprintf (outfile, "%02x", sig[i] & 0xff);
8552 }
8553
8554 /* Print the information associated with a given DIE, and its children.
8555    This routine is a debugging aid only.  */
8556
8557 static void
8558 print_die (dw_die_ref die, FILE *outfile)
8559 {
8560   dw_attr_ref a;
8561   dw_die_ref c;
8562   unsigned ix;
8563
8564   print_spaces (outfile);
8565   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8566            die->die_offset, dwarf_tag_name (die->die_tag),
8567            (void*) die);
8568   print_spaces (outfile);
8569   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8570   fprintf (outfile, " offset: %ld", die->die_offset);
8571   fprintf (outfile, " mark: %d\n", die->die_mark);
8572
8573   if (dwarf_version >= 4 && die->die_id.die_type_node)
8574     {
8575       print_spaces (outfile);
8576       fprintf (outfile, "  signature: ");
8577       print_signature (outfile, die->die_id.die_type_node->signature);
8578       fprintf (outfile, "\n");
8579     }
8580
8581   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8582     {
8583       print_spaces (outfile);
8584       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8585
8586       switch (AT_class (a))
8587         {
8588         case dw_val_class_addr:
8589           fprintf (outfile, "address");
8590           break;
8591         case dw_val_class_offset:
8592           fprintf (outfile, "offset");
8593           break;
8594         case dw_val_class_loc:
8595           fprintf (outfile, "location descriptor");
8596           break;
8597         case dw_val_class_loc_list:
8598           fprintf (outfile, "location list -> label:%s",
8599                    AT_loc_list (a)->ll_symbol);
8600           break;
8601         case dw_val_class_range_list:
8602           fprintf (outfile, "range list");
8603           break;
8604         case dw_val_class_const:
8605           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8606           break;
8607         case dw_val_class_unsigned_const:
8608           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8609           break;
8610         case dw_val_class_const_double:
8611           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8612                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8613                    a->dw_attr_val.v.val_double.high,
8614                    a->dw_attr_val.v.val_double.low);
8615           break;
8616         case dw_val_class_vec:
8617           fprintf (outfile, "floating-point or vector constant");
8618           break;
8619         case dw_val_class_flag:
8620           fprintf (outfile, "%u", AT_flag (a));
8621           break;
8622         case dw_val_class_die_ref:
8623           if (AT_ref (a) != NULL)
8624             {
8625               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8626                 {
8627                   fprintf (outfile, "die -> signature: ");
8628                   print_signature (outfile,
8629                                    AT_ref (a)->die_id.die_type_node->signature);
8630                 }
8631               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8632                 fprintf (outfile, "die -> label: %s",
8633                          AT_ref (a)->die_id.die_symbol);
8634               else
8635                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8636               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8637             }
8638           else
8639             fprintf (outfile, "die -> <null>");
8640           break;
8641         case dw_val_class_vms_delta:
8642           fprintf (outfile, "delta: @slotcount(%s-%s)",
8643                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8644           break;
8645         case dw_val_class_lbl_id:
8646         case dw_val_class_lineptr:
8647         case dw_val_class_macptr:
8648           fprintf (outfile, "label: %s", AT_lbl (a));
8649           break;
8650         case dw_val_class_str:
8651           if (AT_string (a) != NULL)
8652             fprintf (outfile, "\"%s\"", AT_string (a));
8653           else
8654             fprintf (outfile, "<null>");
8655           break;
8656         case dw_val_class_file:
8657           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8658                    AT_file (a)->emitted_number);
8659           break;
8660         case dw_val_class_data8:
8661           {
8662             int i;
8663
8664             for (i = 0; i < 8; i++)
8665               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8666             break;
8667           }
8668         default:
8669           break;
8670         }
8671
8672       fprintf (outfile, "\n");
8673     }
8674
8675   if (die->die_child != NULL)
8676     {
8677       print_indent += 4;
8678       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8679       print_indent -= 4;
8680     }
8681   if (print_indent == 0)
8682     fprintf (outfile, "\n");
8683 }
8684
8685 /* Print the contents of the source code line number correspondence table.
8686    This routine is a debugging aid only.  */
8687
8688 static void
8689 print_dwarf_line_table (FILE *outfile)
8690 {
8691   unsigned i;
8692   dw_line_info_ref line_info;
8693
8694   fprintf (outfile, "\n\nDWARF source line information\n");
8695   for (i = 1; i < line_info_table_in_use; i++)
8696     {
8697       line_info = &line_info_table[i];
8698       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8699                line_info->dw_file_num,
8700                line_info->dw_line_num);
8701     }
8702
8703   fprintf (outfile, "\n\n");
8704 }
8705
8706 /* Print the information collected for a given DIE.  */
8707
8708 DEBUG_FUNCTION void
8709 debug_dwarf_die (dw_die_ref die)
8710 {
8711   print_die (die, stderr);
8712 }
8713
8714 /* Print all DWARF information collected for the compilation unit.
8715    This routine is a debugging aid only.  */
8716
8717 DEBUG_FUNCTION void
8718 debug_dwarf (void)
8719 {
8720   print_indent = 0;
8721   print_die (comp_unit_die (), stderr);
8722   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8723     print_dwarf_line_table (stderr);
8724 }
8725 \f
8726 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8727    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8728    DIE that marks the start of the DIEs for this include file.  */
8729
8730 static dw_die_ref
8731 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8732 {
8733   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8734   dw_die_ref new_unit = gen_compile_unit_die (filename);
8735
8736   new_unit->die_sib = old_unit;
8737   return new_unit;
8738 }
8739
8740 /* Close an include-file CU and reopen the enclosing one.  */
8741
8742 static dw_die_ref
8743 pop_compile_unit (dw_die_ref old_unit)
8744 {
8745   dw_die_ref new_unit = old_unit->die_sib;
8746
8747   old_unit->die_sib = NULL;
8748   return new_unit;
8749 }
8750
8751 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8752 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8753
8754 /* Calculate the checksum of a location expression.  */
8755
8756 static inline void
8757 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8758 {
8759   int tem;
8760
8761   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8762   CHECKSUM (tem);
8763   CHECKSUM (loc->dw_loc_oprnd1);
8764   CHECKSUM (loc->dw_loc_oprnd2);
8765 }
8766
8767 /* Calculate the checksum of an attribute.  */
8768
8769 static void
8770 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8771 {
8772   dw_loc_descr_ref loc;
8773   rtx r;
8774
8775   CHECKSUM (at->dw_attr);
8776
8777   /* We don't care that this was compiled with a different compiler
8778      snapshot; if the output is the same, that's what matters.  */
8779   if (at->dw_attr == DW_AT_producer)
8780     return;
8781
8782   switch (AT_class (at))
8783     {
8784     case dw_val_class_const:
8785       CHECKSUM (at->dw_attr_val.v.val_int);
8786       break;
8787     case dw_val_class_unsigned_const:
8788       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8789       break;
8790     case dw_val_class_const_double:
8791       CHECKSUM (at->dw_attr_val.v.val_double);
8792       break;
8793     case dw_val_class_vec:
8794       CHECKSUM (at->dw_attr_val.v.val_vec);
8795       break;
8796     case dw_val_class_flag:
8797       CHECKSUM (at->dw_attr_val.v.val_flag);
8798       break;
8799     case dw_val_class_str:
8800       CHECKSUM_STRING (AT_string (at));
8801       break;
8802
8803     case dw_val_class_addr:
8804       r = AT_addr (at);
8805       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8806       CHECKSUM_STRING (XSTR (r, 0));
8807       break;
8808
8809     case dw_val_class_offset:
8810       CHECKSUM (at->dw_attr_val.v.val_offset);
8811       break;
8812
8813     case dw_val_class_loc:
8814       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8815         loc_checksum (loc, ctx);
8816       break;
8817
8818     case dw_val_class_die_ref:
8819       die_checksum (AT_ref (at), ctx, mark);
8820       break;
8821
8822     case dw_val_class_fde_ref:
8823     case dw_val_class_vms_delta:
8824     case dw_val_class_lbl_id:
8825     case dw_val_class_lineptr:
8826     case dw_val_class_macptr:
8827       break;
8828
8829     case dw_val_class_file:
8830       CHECKSUM_STRING (AT_file (at)->filename);
8831       break;
8832
8833     case dw_val_class_data8:
8834       CHECKSUM (at->dw_attr_val.v.val_data8);
8835       break;
8836
8837     default:
8838       break;
8839     }
8840 }
8841
8842 /* Calculate the checksum of a DIE.  */
8843
8844 static void
8845 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8846 {
8847   dw_die_ref c;
8848   dw_attr_ref a;
8849   unsigned ix;
8850
8851   /* To avoid infinite recursion.  */
8852   if (die->die_mark)
8853     {
8854       CHECKSUM (die->die_mark);
8855       return;
8856     }
8857   die->die_mark = ++(*mark);
8858
8859   CHECKSUM (die->die_tag);
8860
8861   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8862     attr_checksum (a, ctx, mark);
8863
8864   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8865 }
8866
8867 #undef CHECKSUM
8868 #undef CHECKSUM_STRING
8869
8870 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8871 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8872 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8873 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8874 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8875 #define CHECKSUM_ATTR(FOO) \
8876   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8877
8878 /* Calculate the checksum of a number in signed LEB128 format.  */
8879
8880 static void
8881 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8882 {
8883   unsigned char byte;
8884   bool more;
8885
8886   while (1)
8887     {
8888       byte = (value & 0x7f);
8889       value >>= 7;
8890       more = !((value == 0 && (byte & 0x40) == 0)
8891                 || (value == -1 && (byte & 0x40) != 0));
8892       if (more)
8893         byte |= 0x80;
8894       CHECKSUM (byte);
8895       if (!more)
8896         break;
8897     }
8898 }
8899
8900 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8901
8902 static void
8903 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8904 {
8905   while (1)
8906     {
8907       unsigned char byte = (value & 0x7f);
8908       value >>= 7;
8909       if (value != 0)
8910         /* More bytes to follow.  */
8911         byte |= 0x80;
8912       CHECKSUM (byte);
8913       if (value == 0)
8914         break;
8915     }
8916 }
8917
8918 /* Checksum the context of the DIE.  This adds the names of any
8919    surrounding namespaces or structures to the checksum.  */
8920
8921 static void
8922 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8923 {
8924   const char *name;
8925   dw_die_ref spec;
8926   int tag = die->die_tag;
8927
8928   if (tag != DW_TAG_namespace
8929       && tag != DW_TAG_structure_type
8930       && tag != DW_TAG_class_type)
8931     return;
8932
8933   name = get_AT_string (die, DW_AT_name);
8934
8935   spec = get_AT_ref (die, DW_AT_specification);
8936   if (spec != NULL)
8937     die = spec;
8938
8939   if (die->die_parent != NULL)
8940     checksum_die_context (die->die_parent, ctx);
8941
8942   CHECKSUM_ULEB128 ('C');
8943   CHECKSUM_ULEB128 (tag);
8944   if (name != NULL)
8945     CHECKSUM_STRING (name);
8946 }
8947
8948 /* Calculate the checksum of a location expression.  */
8949
8950 static inline void
8951 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8952 {
8953   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8954      were emitted as a DW_FORM_sdata instead of a location expression.  */
8955   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8956     {
8957       CHECKSUM_ULEB128 (DW_FORM_sdata);
8958       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8959       return;
8960     }
8961
8962   /* Otherwise, just checksum the raw location expression.  */
8963   while (loc != NULL)
8964     {
8965       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8966       CHECKSUM (loc->dw_loc_oprnd1);
8967       CHECKSUM (loc->dw_loc_oprnd2);
8968       loc = loc->dw_loc_next;
8969     }
8970 }
8971
8972 /* Calculate the checksum of an attribute.  */
8973
8974 static void
8975 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8976                        struct md5_ctx *ctx, int *mark)
8977 {
8978   dw_loc_descr_ref loc;
8979   rtx r;
8980
8981   if (AT_class (at) == dw_val_class_die_ref)
8982     {
8983       dw_die_ref target_die = AT_ref (at);
8984
8985       /* For pointer and reference types, we checksum only the (qualified)
8986          name of the target type (if there is a name).  For friend entries,
8987          we checksum only the (qualified) name of the target type or function.
8988          This allows the checksum to remain the same whether the target type
8989          is complete or not.  */
8990       if ((at->dw_attr == DW_AT_type
8991            && (tag == DW_TAG_pointer_type
8992                || tag == DW_TAG_reference_type
8993                || tag == DW_TAG_rvalue_reference_type
8994                || tag == DW_TAG_ptr_to_member_type))
8995           || (at->dw_attr == DW_AT_friend
8996               && tag == DW_TAG_friend))
8997         {
8998           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8999
9000           if (name_attr != NULL)
9001             {
9002               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9003
9004               if (decl == NULL)
9005                 decl = target_die;
9006               CHECKSUM_ULEB128 ('N');
9007               CHECKSUM_ULEB128 (at->dw_attr);
9008               if (decl->die_parent != NULL)
9009                 checksum_die_context (decl->die_parent, ctx);
9010               CHECKSUM_ULEB128 ('E');
9011               CHECKSUM_STRING (AT_string (name_attr));
9012               return;
9013             }
9014         }
9015
9016       /* For all other references to another DIE, we check to see if the
9017          target DIE has already been visited.  If it has, we emit a
9018          backward reference; if not, we descend recursively.  */
9019       if (target_die->die_mark > 0)
9020         {
9021           CHECKSUM_ULEB128 ('R');
9022           CHECKSUM_ULEB128 (at->dw_attr);
9023           CHECKSUM_ULEB128 (target_die->die_mark);
9024         }
9025       else
9026         {
9027           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9028
9029           if (decl == NULL)
9030             decl = target_die;
9031           target_die->die_mark = ++(*mark);
9032           CHECKSUM_ULEB128 ('T');
9033           CHECKSUM_ULEB128 (at->dw_attr);
9034           if (decl->die_parent != NULL)
9035             checksum_die_context (decl->die_parent, ctx);
9036           die_checksum_ordered (target_die, ctx, mark);
9037         }
9038       return;
9039     }
9040
9041   CHECKSUM_ULEB128 ('A');
9042   CHECKSUM_ULEB128 (at->dw_attr);
9043
9044   switch (AT_class (at))
9045     {
9046     case dw_val_class_const:
9047       CHECKSUM_ULEB128 (DW_FORM_sdata);
9048       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9049       break;
9050
9051     case dw_val_class_unsigned_const:
9052       CHECKSUM_ULEB128 (DW_FORM_sdata);
9053       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9054       break;
9055
9056     case dw_val_class_const_double:
9057       CHECKSUM_ULEB128 (DW_FORM_block);
9058       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9059       CHECKSUM (at->dw_attr_val.v.val_double);
9060       break;
9061
9062     case dw_val_class_vec:
9063       CHECKSUM_ULEB128 (DW_FORM_block);
9064       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9065       CHECKSUM (at->dw_attr_val.v.val_vec);
9066       break;
9067
9068     case dw_val_class_flag:
9069       CHECKSUM_ULEB128 (DW_FORM_flag);
9070       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9071       break;
9072
9073     case dw_val_class_str:
9074       CHECKSUM_ULEB128 (DW_FORM_string);
9075       CHECKSUM_STRING (AT_string (at));
9076       break;
9077
9078     case dw_val_class_addr:
9079       r = AT_addr (at);
9080       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9081       CHECKSUM_ULEB128 (DW_FORM_string);
9082       CHECKSUM_STRING (XSTR (r, 0));
9083       break;
9084
9085     case dw_val_class_offset:
9086       CHECKSUM_ULEB128 (DW_FORM_sdata);
9087       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9088       break;
9089
9090     case dw_val_class_loc:
9091       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9092         loc_checksum_ordered (loc, ctx);
9093       break;
9094
9095     case dw_val_class_fde_ref:
9096     case dw_val_class_lbl_id:
9097     case dw_val_class_lineptr:
9098     case dw_val_class_macptr:
9099       break;
9100
9101     case dw_val_class_file:
9102       CHECKSUM_ULEB128 (DW_FORM_string);
9103       CHECKSUM_STRING (AT_file (at)->filename);
9104       break;
9105
9106     case dw_val_class_data8:
9107       CHECKSUM (at->dw_attr_val.v.val_data8);
9108       break;
9109
9110     default:
9111       break;
9112     }
9113 }
9114
9115 struct checksum_attributes
9116 {
9117   dw_attr_ref at_name;
9118   dw_attr_ref at_type;
9119   dw_attr_ref at_friend;
9120   dw_attr_ref at_accessibility;
9121   dw_attr_ref at_address_class;
9122   dw_attr_ref at_allocated;
9123   dw_attr_ref at_artificial;
9124   dw_attr_ref at_associated;
9125   dw_attr_ref at_binary_scale;
9126   dw_attr_ref at_bit_offset;
9127   dw_attr_ref at_bit_size;
9128   dw_attr_ref at_bit_stride;
9129   dw_attr_ref at_byte_size;
9130   dw_attr_ref at_byte_stride;
9131   dw_attr_ref at_const_value;
9132   dw_attr_ref at_containing_type;
9133   dw_attr_ref at_count;
9134   dw_attr_ref at_data_location;
9135   dw_attr_ref at_data_member_location;
9136   dw_attr_ref at_decimal_scale;
9137   dw_attr_ref at_decimal_sign;
9138   dw_attr_ref at_default_value;
9139   dw_attr_ref at_digit_count;
9140   dw_attr_ref at_discr;
9141   dw_attr_ref at_discr_list;
9142   dw_attr_ref at_discr_value;
9143   dw_attr_ref at_encoding;
9144   dw_attr_ref at_endianity;
9145   dw_attr_ref at_explicit;
9146   dw_attr_ref at_is_optional;
9147   dw_attr_ref at_location;
9148   dw_attr_ref at_lower_bound;
9149   dw_attr_ref at_mutable;
9150   dw_attr_ref at_ordering;
9151   dw_attr_ref at_picture_string;
9152   dw_attr_ref at_prototyped;
9153   dw_attr_ref at_small;
9154   dw_attr_ref at_segment;
9155   dw_attr_ref at_string_length;
9156   dw_attr_ref at_threads_scaled;
9157   dw_attr_ref at_upper_bound;
9158   dw_attr_ref at_use_location;
9159   dw_attr_ref at_use_UTF8;
9160   dw_attr_ref at_variable_parameter;
9161   dw_attr_ref at_virtuality;
9162   dw_attr_ref at_visibility;
9163   dw_attr_ref at_vtable_elem_location;
9164 };
9165
9166 /* Collect the attributes that we will want to use for the checksum.  */
9167
9168 static void
9169 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9170 {
9171   dw_attr_ref a;
9172   unsigned ix;
9173
9174   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9175     {
9176       switch (a->dw_attr)
9177         {
9178         case DW_AT_name:
9179           attrs->at_name = a;
9180           break;
9181         case DW_AT_type:
9182           attrs->at_type = a;
9183           break;
9184         case DW_AT_friend:
9185           attrs->at_friend = a;
9186           break;
9187         case DW_AT_accessibility:
9188           attrs->at_accessibility = a;
9189           break;
9190         case DW_AT_address_class:
9191           attrs->at_address_class = a;
9192           break;
9193         case DW_AT_allocated:
9194           attrs->at_allocated = a;
9195           break;
9196         case DW_AT_artificial:
9197           attrs->at_artificial = a;
9198           break;
9199         case DW_AT_associated:
9200           attrs->at_associated = a;
9201           break;
9202         case DW_AT_binary_scale:
9203           attrs->at_binary_scale = a;
9204           break;
9205         case DW_AT_bit_offset:
9206           attrs->at_bit_offset = a;
9207           break;
9208         case DW_AT_bit_size:
9209           attrs->at_bit_size = a;
9210           break;
9211         case DW_AT_bit_stride:
9212           attrs->at_bit_stride = a;
9213           break;
9214         case DW_AT_byte_size:
9215           attrs->at_byte_size = a;
9216           break;
9217         case DW_AT_byte_stride:
9218           attrs->at_byte_stride = a;
9219           break;
9220         case DW_AT_const_value:
9221           attrs->at_const_value = a;
9222           break;
9223         case DW_AT_containing_type:
9224           attrs->at_containing_type = a;
9225           break;
9226         case DW_AT_count:
9227           attrs->at_count = a;
9228           break;
9229         case DW_AT_data_location:
9230           attrs->at_data_location = a;
9231           break;
9232         case DW_AT_data_member_location:
9233           attrs->at_data_member_location = a;
9234           break;
9235         case DW_AT_decimal_scale:
9236           attrs->at_decimal_scale = a;
9237           break;
9238         case DW_AT_decimal_sign:
9239           attrs->at_decimal_sign = a;
9240           break;
9241         case DW_AT_default_value:
9242           attrs->at_default_value = a;
9243           break;
9244         case DW_AT_digit_count:
9245           attrs->at_digit_count = a;
9246           break;
9247         case DW_AT_discr:
9248           attrs->at_discr = a;
9249           break;
9250         case DW_AT_discr_list:
9251           attrs->at_discr_list = a;
9252           break;
9253         case DW_AT_discr_value:
9254           attrs->at_discr_value = a;
9255           break;
9256         case DW_AT_encoding:
9257           attrs->at_encoding = a;
9258           break;
9259         case DW_AT_endianity:
9260           attrs->at_endianity = a;
9261           break;
9262         case DW_AT_explicit:
9263           attrs->at_explicit = a;
9264           break;
9265         case DW_AT_is_optional:
9266           attrs->at_is_optional = a;
9267           break;
9268         case DW_AT_location:
9269           attrs->at_location = a;
9270           break;
9271         case DW_AT_lower_bound:
9272           attrs->at_lower_bound = a;
9273           break;
9274         case DW_AT_mutable:
9275           attrs->at_mutable = a;
9276           break;
9277         case DW_AT_ordering:
9278           attrs->at_ordering = a;
9279           break;
9280         case DW_AT_picture_string:
9281           attrs->at_picture_string = a;
9282           break;
9283         case DW_AT_prototyped:
9284           attrs->at_prototyped = a;
9285           break;
9286         case DW_AT_small:
9287           attrs->at_small = a;
9288           break;
9289         case DW_AT_segment:
9290           attrs->at_segment = a;
9291           break;
9292         case DW_AT_string_length:
9293           attrs->at_string_length = a;
9294           break;
9295         case DW_AT_threads_scaled:
9296           attrs->at_threads_scaled = a;
9297           break;
9298         case DW_AT_upper_bound:
9299           attrs->at_upper_bound = a;
9300           break;
9301         case DW_AT_use_location:
9302           attrs->at_use_location = a;
9303           break;
9304         case DW_AT_use_UTF8:
9305           attrs->at_use_UTF8 = a;
9306           break;
9307         case DW_AT_variable_parameter:
9308           attrs->at_variable_parameter = a;
9309           break;
9310         case DW_AT_virtuality:
9311           attrs->at_virtuality = a;
9312           break;
9313         case DW_AT_visibility:
9314           attrs->at_visibility = a;
9315           break;
9316         case DW_AT_vtable_elem_location:
9317           attrs->at_vtable_elem_location = a;
9318           break;
9319         default:
9320           break;
9321         }
9322     }
9323 }
9324
9325 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9326
9327 static void
9328 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9329 {
9330   dw_die_ref c;
9331   dw_die_ref decl;
9332   struct checksum_attributes attrs;
9333
9334   CHECKSUM_ULEB128 ('D');
9335   CHECKSUM_ULEB128 (die->die_tag);
9336
9337   memset (&attrs, 0, sizeof (attrs));
9338
9339   decl = get_AT_ref (die, DW_AT_specification);
9340   if (decl != NULL)
9341     collect_checksum_attributes (&attrs, decl);
9342   collect_checksum_attributes (&attrs, die);
9343
9344   CHECKSUM_ATTR (attrs.at_name);
9345   CHECKSUM_ATTR (attrs.at_accessibility);
9346   CHECKSUM_ATTR (attrs.at_address_class);
9347   CHECKSUM_ATTR (attrs.at_allocated);
9348   CHECKSUM_ATTR (attrs.at_artificial);
9349   CHECKSUM_ATTR (attrs.at_associated);
9350   CHECKSUM_ATTR (attrs.at_binary_scale);
9351   CHECKSUM_ATTR (attrs.at_bit_offset);
9352   CHECKSUM_ATTR (attrs.at_bit_size);
9353   CHECKSUM_ATTR (attrs.at_bit_stride);
9354   CHECKSUM_ATTR (attrs.at_byte_size);
9355   CHECKSUM_ATTR (attrs.at_byte_stride);
9356   CHECKSUM_ATTR (attrs.at_const_value);
9357   CHECKSUM_ATTR (attrs.at_containing_type);
9358   CHECKSUM_ATTR (attrs.at_count);
9359   CHECKSUM_ATTR (attrs.at_data_location);
9360   CHECKSUM_ATTR (attrs.at_data_member_location);
9361   CHECKSUM_ATTR (attrs.at_decimal_scale);
9362   CHECKSUM_ATTR (attrs.at_decimal_sign);
9363   CHECKSUM_ATTR (attrs.at_default_value);
9364   CHECKSUM_ATTR (attrs.at_digit_count);
9365   CHECKSUM_ATTR (attrs.at_discr);
9366   CHECKSUM_ATTR (attrs.at_discr_list);
9367   CHECKSUM_ATTR (attrs.at_discr_value);
9368   CHECKSUM_ATTR (attrs.at_encoding);
9369   CHECKSUM_ATTR (attrs.at_endianity);
9370   CHECKSUM_ATTR (attrs.at_explicit);
9371   CHECKSUM_ATTR (attrs.at_is_optional);
9372   CHECKSUM_ATTR (attrs.at_location);
9373   CHECKSUM_ATTR (attrs.at_lower_bound);
9374   CHECKSUM_ATTR (attrs.at_mutable);
9375   CHECKSUM_ATTR (attrs.at_ordering);
9376   CHECKSUM_ATTR (attrs.at_picture_string);
9377   CHECKSUM_ATTR (attrs.at_prototyped);
9378   CHECKSUM_ATTR (attrs.at_small);
9379   CHECKSUM_ATTR (attrs.at_segment);
9380   CHECKSUM_ATTR (attrs.at_string_length);
9381   CHECKSUM_ATTR (attrs.at_threads_scaled);
9382   CHECKSUM_ATTR (attrs.at_upper_bound);
9383   CHECKSUM_ATTR (attrs.at_use_location);
9384   CHECKSUM_ATTR (attrs.at_use_UTF8);
9385   CHECKSUM_ATTR (attrs.at_variable_parameter);
9386   CHECKSUM_ATTR (attrs.at_virtuality);
9387   CHECKSUM_ATTR (attrs.at_visibility);
9388   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9389   CHECKSUM_ATTR (attrs.at_type);
9390   CHECKSUM_ATTR (attrs.at_friend);
9391
9392   /* Checksum the child DIEs, except for nested types and member functions.  */
9393   c = die->die_child;
9394   if (c) do {
9395     dw_attr_ref name_attr;
9396
9397     c = c->die_sib;
9398     name_attr = get_AT (c, DW_AT_name);
9399     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9400         && name_attr != NULL)
9401       {
9402         CHECKSUM_ULEB128 ('S');
9403         CHECKSUM_ULEB128 (c->die_tag);
9404         CHECKSUM_STRING (AT_string (name_attr));
9405       }
9406     else
9407       {
9408         /* Mark this DIE so it gets processed when unmarking.  */
9409         if (c->die_mark == 0)
9410           c->die_mark = -1;
9411         die_checksum_ordered (c, ctx, mark);
9412       }
9413   } while (c != die->die_child);
9414
9415   CHECKSUM_ULEB128 (0);
9416 }
9417
9418 #undef CHECKSUM
9419 #undef CHECKSUM_STRING
9420 #undef CHECKSUM_ATTR
9421 #undef CHECKSUM_LEB128
9422 #undef CHECKSUM_ULEB128
9423
9424 /* Generate the type signature for DIE.  This is computed by generating an
9425    MD5 checksum over the DIE's tag, its relevant attributes, and its
9426    children.  Attributes that are references to other DIEs are processed
9427    by recursion, using the MARK field to prevent infinite recursion.
9428    If the DIE is nested inside a namespace or another type, we also
9429    need to include that context in the signature.  The lower 64 bits
9430    of the resulting MD5 checksum comprise the signature.  */
9431
9432 static void
9433 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9434 {
9435   int mark;
9436   const char *name;
9437   unsigned char checksum[16];
9438   struct md5_ctx ctx;
9439   dw_die_ref decl;
9440
9441   name = get_AT_string (die, DW_AT_name);
9442   decl = get_AT_ref (die, DW_AT_specification);
9443
9444   /* First, compute a signature for just the type name (and its surrounding
9445      context, if any.  This is stored in the type unit DIE for link-time
9446      ODR (one-definition rule) checking.  */
9447
9448   if (is_cxx() && name != NULL)
9449     {
9450       md5_init_ctx (&ctx);
9451
9452       /* Checksum the names of surrounding namespaces and structures.  */
9453       if (decl != NULL && decl->die_parent != NULL)
9454         checksum_die_context (decl->die_parent, &ctx);
9455
9456       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9457       md5_process_bytes (name, strlen (name) + 1, &ctx);
9458       md5_finish_ctx (&ctx, checksum);
9459
9460       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9461     }
9462
9463   /* Next, compute the complete type signature.  */
9464
9465   md5_init_ctx (&ctx);
9466   mark = 1;
9467   die->die_mark = mark;
9468
9469   /* Checksum the names of surrounding namespaces and structures.  */
9470   if (decl != NULL && decl->die_parent != NULL)
9471     checksum_die_context (decl->die_parent, &ctx);
9472
9473   /* Checksum the DIE and its children.  */
9474   die_checksum_ordered (die, &ctx, &mark);
9475   unmark_all_dies (die);
9476   md5_finish_ctx (&ctx, checksum);
9477
9478   /* Store the signature in the type node and link the type DIE and the
9479      type node together.  */
9480   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9481           DWARF_TYPE_SIGNATURE_SIZE);
9482   die->die_id.die_type_node = type_node;
9483   type_node->type_die = die;
9484
9485   /* If the DIE is a specification, link its declaration to the type node
9486      as well.  */
9487   if (decl != NULL)
9488     decl->die_id.die_type_node = type_node;
9489 }
9490
9491 /* Do the location expressions look same?  */
9492 static inline int
9493 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9494 {
9495   return loc1->dw_loc_opc == loc2->dw_loc_opc
9496          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9497          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9498 }
9499
9500 /* Do the values look the same?  */
9501 static int
9502 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9503 {
9504   dw_loc_descr_ref loc1, loc2;
9505   rtx r1, r2;
9506
9507   if (v1->val_class != v2->val_class)
9508     return 0;
9509
9510   switch (v1->val_class)
9511     {
9512     case dw_val_class_const:
9513       return v1->v.val_int == v2->v.val_int;
9514     case dw_val_class_unsigned_const:
9515       return v1->v.val_unsigned == v2->v.val_unsigned;
9516     case dw_val_class_const_double:
9517       return v1->v.val_double.high == v2->v.val_double.high
9518              && v1->v.val_double.low == v2->v.val_double.low;
9519     case dw_val_class_vec:
9520       if (v1->v.val_vec.length != v2->v.val_vec.length
9521           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9522         return 0;
9523       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9524                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9525         return 0;
9526       return 1;
9527     case dw_val_class_flag:
9528       return v1->v.val_flag == v2->v.val_flag;
9529     case dw_val_class_str:
9530       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9531
9532     case dw_val_class_addr:
9533       r1 = v1->v.val_addr;
9534       r2 = v2->v.val_addr;
9535       if (GET_CODE (r1) != GET_CODE (r2))
9536         return 0;
9537       return !rtx_equal_p (r1, r2);
9538
9539     case dw_val_class_offset:
9540       return v1->v.val_offset == v2->v.val_offset;
9541
9542     case dw_val_class_loc:
9543       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9544            loc1 && loc2;
9545            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9546         if (!same_loc_p (loc1, loc2, mark))
9547           return 0;
9548       return !loc1 && !loc2;
9549
9550     case dw_val_class_die_ref:
9551       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9552
9553     case dw_val_class_fde_ref:
9554     case dw_val_class_vms_delta:
9555     case dw_val_class_lbl_id:
9556     case dw_val_class_lineptr:
9557     case dw_val_class_macptr:
9558       return 1;
9559
9560     case dw_val_class_file:
9561       return v1->v.val_file == v2->v.val_file;
9562
9563     case dw_val_class_data8:
9564       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9565
9566     default:
9567       return 1;
9568     }
9569 }
9570
9571 /* Do the attributes look the same?  */
9572
9573 static int
9574 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9575 {
9576   if (at1->dw_attr != at2->dw_attr)
9577     return 0;
9578
9579   /* We don't care that this was compiled with a different compiler
9580      snapshot; if the output is the same, that's what matters. */
9581   if (at1->dw_attr == DW_AT_producer)
9582     return 1;
9583
9584   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9585 }
9586
9587 /* Do the dies look the same?  */
9588
9589 static int
9590 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9591 {
9592   dw_die_ref c1, c2;
9593   dw_attr_ref a1;
9594   unsigned ix;
9595
9596   /* To avoid infinite recursion.  */
9597   if (die1->die_mark)
9598     return die1->die_mark == die2->die_mark;
9599   die1->die_mark = die2->die_mark = ++(*mark);
9600
9601   if (die1->die_tag != die2->die_tag)
9602     return 0;
9603
9604   if (VEC_length (dw_attr_node, die1->die_attr)
9605       != VEC_length (dw_attr_node, die2->die_attr))
9606     return 0;
9607
9608   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9609     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9610       return 0;
9611
9612   c1 = die1->die_child;
9613   c2 = die2->die_child;
9614   if (! c1)
9615     {
9616       if (c2)
9617         return 0;
9618     }
9619   else
9620     for (;;)
9621       {
9622         if (!same_die_p (c1, c2, mark))
9623           return 0;
9624         c1 = c1->die_sib;
9625         c2 = c2->die_sib;
9626         if (c1 == die1->die_child)
9627           {
9628             if (c2 == die2->die_child)
9629               break;
9630             else
9631               return 0;
9632           }
9633     }
9634
9635   return 1;
9636 }
9637
9638 /* Do the dies look the same?  Wrapper around same_die_p.  */
9639
9640 static int
9641 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9642 {
9643   int mark = 0;
9644   int ret = same_die_p (die1, die2, &mark);
9645
9646   unmark_all_dies (die1);
9647   unmark_all_dies (die2);
9648
9649   return ret;
9650 }
9651
9652 /* The prefix to attach to symbols on DIEs in the current comdat debug
9653    info section.  */
9654 static char *comdat_symbol_id;
9655
9656 /* The index of the current symbol within the current comdat CU.  */
9657 static unsigned int comdat_symbol_number;
9658
9659 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9660    children, and set comdat_symbol_id accordingly.  */
9661
9662 static void
9663 compute_section_prefix (dw_die_ref unit_die)
9664 {
9665   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9666   const char *base = die_name ? lbasename (die_name) : "anonymous";
9667   char *name = XALLOCAVEC (char, strlen (base) + 64);
9668   char *p;
9669   int i, mark;
9670   unsigned char checksum[16];
9671   struct md5_ctx ctx;
9672
9673   /* Compute the checksum of the DIE, then append part of it as hex digits to
9674      the name filename of the unit.  */
9675
9676   md5_init_ctx (&ctx);
9677   mark = 0;
9678   die_checksum (unit_die, &ctx, &mark);
9679   unmark_all_dies (unit_die);
9680   md5_finish_ctx (&ctx, checksum);
9681
9682   sprintf (name, "%s.", base);
9683   clean_symbol_name (name);
9684
9685   p = name + strlen (name);
9686   for (i = 0; i < 4; i++)
9687     {
9688       sprintf (p, "%.2x", checksum[i]);
9689       p += 2;
9690     }
9691
9692   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9693   comdat_symbol_number = 0;
9694 }
9695
9696 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9697
9698 static int
9699 is_type_die (dw_die_ref die)
9700 {
9701   switch (die->die_tag)
9702     {
9703     case DW_TAG_array_type:
9704     case DW_TAG_class_type:
9705     case DW_TAG_interface_type:
9706     case DW_TAG_enumeration_type:
9707     case DW_TAG_pointer_type:
9708     case DW_TAG_reference_type:
9709     case DW_TAG_rvalue_reference_type:
9710     case DW_TAG_string_type:
9711     case DW_TAG_structure_type:
9712     case DW_TAG_subroutine_type:
9713     case DW_TAG_union_type:
9714     case DW_TAG_ptr_to_member_type:
9715     case DW_TAG_set_type:
9716     case DW_TAG_subrange_type:
9717     case DW_TAG_base_type:
9718     case DW_TAG_const_type:
9719     case DW_TAG_file_type:
9720     case DW_TAG_packed_type:
9721     case DW_TAG_volatile_type:
9722     case DW_TAG_typedef:
9723       return 1;
9724     default:
9725       return 0;
9726     }
9727 }
9728
9729 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9730    Basically, we want to choose the bits that are likely to be shared between
9731    compilations (types) and leave out the bits that are specific to individual
9732    compilations (functions).  */
9733
9734 static int
9735 is_comdat_die (dw_die_ref c)
9736 {
9737   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9738      we do for stabs.  The advantage is a greater likelihood of sharing between
9739      objects that don't include headers in the same order (and therefore would
9740      put the base types in a different comdat).  jason 8/28/00 */
9741
9742   if (c->die_tag == DW_TAG_base_type)
9743     return 0;
9744
9745   if (c->die_tag == DW_TAG_pointer_type
9746       || c->die_tag == DW_TAG_reference_type
9747       || c->die_tag == DW_TAG_rvalue_reference_type
9748       || c->die_tag == DW_TAG_const_type
9749       || c->die_tag == DW_TAG_volatile_type)
9750     {
9751       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9752
9753       return t ? is_comdat_die (t) : 0;
9754     }
9755
9756   return is_type_die (c);
9757 }
9758
9759 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9760    compilation unit.  */
9761
9762 static int
9763 is_symbol_die (dw_die_ref c)
9764 {
9765   return (is_type_die (c)
9766           || is_declaration_die (c)
9767           || c->die_tag == DW_TAG_namespace
9768           || c->die_tag == DW_TAG_module);
9769 }
9770
9771 /* Returns true iff C is a compile-unit DIE.  */
9772
9773 static inline bool
9774 is_cu_die (dw_die_ref c)
9775 {
9776   return c && c->die_tag == DW_TAG_compile_unit;
9777 }
9778
9779 static char *
9780 gen_internal_sym (const char *prefix)
9781 {
9782   char buf[256];
9783
9784   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9785   return xstrdup (buf);
9786 }
9787
9788 /* Assign symbols to all worthy DIEs under DIE.  */
9789
9790 static void
9791 assign_symbol_names (dw_die_ref die)
9792 {
9793   dw_die_ref c;
9794
9795   if (is_symbol_die (die))
9796     {
9797       if (comdat_symbol_id)
9798         {
9799           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9800
9801           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9802                    comdat_symbol_id, comdat_symbol_number++);
9803           die->die_id.die_symbol = xstrdup (p);
9804         }
9805       else
9806         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9807     }
9808
9809   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9810 }
9811
9812 struct cu_hash_table_entry
9813 {
9814   dw_die_ref cu;
9815   unsigned min_comdat_num, max_comdat_num;
9816   struct cu_hash_table_entry *next;
9817 };
9818
9819 /* Routines to manipulate hash table of CUs.  */
9820 static hashval_t
9821 htab_cu_hash (const void *of)
9822 {
9823   const struct cu_hash_table_entry *const entry =
9824     (const struct cu_hash_table_entry *) of;
9825
9826   return htab_hash_string (entry->cu->die_id.die_symbol);
9827 }
9828
9829 static int
9830 htab_cu_eq (const void *of1, const void *of2)
9831 {
9832   const struct cu_hash_table_entry *const entry1 =
9833     (const struct cu_hash_table_entry *) of1;
9834   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9835
9836   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9837 }
9838
9839 static void
9840 htab_cu_del (void *what)
9841 {
9842   struct cu_hash_table_entry *next,
9843     *entry = (struct cu_hash_table_entry *) what;
9844
9845   while (entry)
9846     {
9847       next = entry->next;
9848       free (entry);
9849       entry = next;
9850     }
9851 }
9852
9853 /* Check whether we have already seen this CU and set up SYM_NUM
9854    accordingly.  */
9855 static int
9856 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9857 {
9858   struct cu_hash_table_entry dummy;
9859   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9860
9861   dummy.max_comdat_num = 0;
9862
9863   slot = (struct cu_hash_table_entry **)
9864     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9865         INSERT);
9866   entry = *slot;
9867
9868   for (; entry; last = entry, entry = entry->next)
9869     {
9870       if (same_die_p_wrap (cu, entry->cu))
9871         break;
9872     }
9873
9874   if (entry)
9875     {
9876       *sym_num = entry->min_comdat_num;
9877       return 1;
9878     }
9879
9880   entry = XCNEW (struct cu_hash_table_entry);
9881   entry->cu = cu;
9882   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9883   entry->next = *slot;
9884   *slot = entry;
9885
9886   return 0;
9887 }
9888
9889 /* Record SYM_NUM to record of CU in HTABLE.  */
9890 static void
9891 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9892 {
9893   struct cu_hash_table_entry **slot, *entry;
9894
9895   slot = (struct cu_hash_table_entry **)
9896     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9897         NO_INSERT);
9898   entry = *slot;
9899
9900   entry->max_comdat_num = sym_num;
9901 }
9902
9903 /* Traverse the DIE (which is always comp_unit_die), and set up
9904    additional compilation units for each of the include files we see
9905    bracketed by BINCL/EINCL.  */
9906
9907 static void
9908 break_out_includes (dw_die_ref die)
9909 {
9910   dw_die_ref c;
9911   dw_die_ref unit = NULL;
9912   limbo_die_node *node, **pnode;
9913   htab_t cu_hash_table;
9914
9915   c = die->die_child;
9916   if (c) do {
9917     dw_die_ref prev = c;
9918     c = c->die_sib;
9919     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9920            || (unit && is_comdat_die (c)))
9921       {
9922         dw_die_ref next = c->die_sib;
9923
9924         /* This DIE is for a secondary CU; remove it from the main one.  */
9925         remove_child_with_prev (c, prev);
9926
9927         if (c->die_tag == DW_TAG_GNU_BINCL)
9928           unit = push_new_compile_unit (unit, c);
9929         else if (c->die_tag == DW_TAG_GNU_EINCL)
9930           unit = pop_compile_unit (unit);
9931         else
9932           add_child_die (unit, c);
9933         c = next;
9934         if (c == die->die_child)
9935           break;
9936       }
9937   } while (c != die->die_child);
9938
9939 #if 0
9940   /* We can only use this in debugging, since the frontend doesn't check
9941      to make sure that we leave every include file we enter.  */
9942   gcc_assert (!unit);
9943 #endif
9944
9945   assign_symbol_names (die);
9946   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9947   for (node = limbo_die_list, pnode = &limbo_die_list;
9948        node;
9949        node = node->next)
9950     {
9951       int is_dupl;
9952
9953       compute_section_prefix (node->die);
9954       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9955                         &comdat_symbol_number);
9956       assign_symbol_names (node->die);
9957       if (is_dupl)
9958         *pnode = node->next;
9959       else
9960         {
9961           pnode = &node->next;
9962           record_comdat_symbol_number (node->die, cu_hash_table,
9963                 comdat_symbol_number);
9964         }
9965     }
9966   htab_delete (cu_hash_table);
9967 }
9968
9969 /* Return non-zero if this DIE is a declaration.  */
9970
9971 static int
9972 is_declaration_die (dw_die_ref die)
9973 {
9974   dw_attr_ref a;
9975   unsigned ix;
9976
9977   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9978     if (a->dw_attr == DW_AT_declaration)
9979       return 1;
9980
9981   return 0;
9982 }
9983
9984 /* Return non-zero if this DIE is nested inside a subprogram.  */
9985
9986 static int
9987 is_nested_in_subprogram (dw_die_ref die)
9988 {
9989   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9990
9991   if (decl == NULL)
9992     decl = die;
9993   return local_scope_p (decl);
9994 }
9995
9996 /* Return non-zero if this is a type DIE that should be moved to a
9997    COMDAT .debug_types section.  */
9998
9999 static int
10000 should_move_die_to_comdat (dw_die_ref die)
10001 {
10002   switch (die->die_tag)
10003     {
10004     case DW_TAG_class_type:
10005     case DW_TAG_structure_type:
10006     case DW_TAG_enumeration_type:
10007     case DW_TAG_union_type:
10008       /* Don't move declarations, inlined instances, or types nested in a
10009          subprogram.  */
10010       if (is_declaration_die (die)
10011           || get_AT (die, DW_AT_abstract_origin)
10012           || is_nested_in_subprogram (die))
10013         return 0;
10014       return 1;
10015     case DW_TAG_array_type:
10016     case DW_TAG_interface_type:
10017     case DW_TAG_pointer_type:
10018     case DW_TAG_reference_type:
10019     case DW_TAG_rvalue_reference_type:
10020     case DW_TAG_string_type:
10021     case DW_TAG_subroutine_type:
10022     case DW_TAG_ptr_to_member_type:
10023     case DW_TAG_set_type:
10024     case DW_TAG_subrange_type:
10025     case DW_TAG_base_type:
10026     case DW_TAG_const_type:
10027     case DW_TAG_file_type:
10028     case DW_TAG_packed_type:
10029     case DW_TAG_volatile_type:
10030     case DW_TAG_typedef:
10031     default:
10032       return 0;
10033     }
10034 }
10035
10036 /* Make a clone of DIE.  */
10037
10038 static dw_die_ref
10039 clone_die (dw_die_ref die)
10040 {
10041   dw_die_ref clone;
10042   dw_attr_ref a;
10043   unsigned ix;
10044
10045   clone = ggc_alloc_cleared_die_node ();
10046   clone->die_tag = die->die_tag;
10047
10048   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10049     add_dwarf_attr (clone, a);
10050
10051   return clone;
10052 }
10053
10054 /* Make a clone of the tree rooted at DIE.  */
10055
10056 static dw_die_ref
10057 clone_tree (dw_die_ref die)
10058 {
10059   dw_die_ref c;
10060   dw_die_ref clone = clone_die (die);
10061
10062   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10063
10064   return clone;
10065 }
10066
10067 /* Make a clone of DIE as a declaration.  */
10068
10069 static dw_die_ref
10070 clone_as_declaration (dw_die_ref die)
10071 {
10072   dw_die_ref clone;
10073   dw_die_ref decl;
10074   dw_attr_ref a;
10075   unsigned ix;
10076
10077   /* If the DIE is already a declaration, just clone it.  */
10078   if (is_declaration_die (die))
10079     return clone_die (die);
10080
10081   /* If the DIE is a specification, just clone its declaration DIE.  */
10082   decl = get_AT_ref (die, DW_AT_specification);
10083   if (decl != NULL)
10084     return clone_die (decl);
10085
10086   clone = ggc_alloc_cleared_die_node ();
10087   clone->die_tag = die->die_tag;
10088
10089   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10090     {
10091       /* We don't want to copy over all attributes.
10092          For example we don't want DW_AT_byte_size because otherwise we will no
10093          longer have a declaration and GDB will treat it as a definition.  */
10094
10095       switch (a->dw_attr)
10096         {
10097         case DW_AT_artificial:
10098         case DW_AT_containing_type:
10099         case DW_AT_external:
10100         case DW_AT_name:
10101         case DW_AT_type:
10102         case DW_AT_virtuality:
10103         case DW_AT_linkage_name:
10104         case DW_AT_MIPS_linkage_name:
10105           add_dwarf_attr (clone, a);
10106           break;
10107         case DW_AT_byte_size:
10108         default:
10109           break;
10110         }
10111     }
10112
10113   if (die->die_id.die_type_node)
10114     add_AT_die_ref (clone, DW_AT_signature, die);
10115
10116   add_AT_flag (clone, DW_AT_declaration, 1);
10117   return clone;
10118 }
10119
10120 /* Copy the declaration context to the new compile unit DIE.  This includes
10121    any surrounding namespace or type declarations.  If the DIE has an
10122    AT_specification attribute, it also includes attributes and children
10123    attached to the specification.  */
10124
10125 static void
10126 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10127 {
10128   dw_die_ref decl;
10129   dw_die_ref new_decl;
10130
10131   decl = get_AT_ref (die, DW_AT_specification);
10132   if (decl == NULL)
10133     decl = die;
10134   else
10135     {
10136       unsigned ix;
10137       dw_die_ref c;
10138       dw_attr_ref a;
10139
10140       /* Copy the type node pointer from the new DIE to the original
10141          declaration DIE so we can forward references later.  */
10142       decl->die_id.die_type_node = die->die_id.die_type_node;
10143
10144       remove_AT (die, DW_AT_specification);
10145
10146       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10147         {
10148           if (a->dw_attr != DW_AT_name
10149               && a->dw_attr != DW_AT_declaration
10150               && a->dw_attr != DW_AT_external)
10151             add_dwarf_attr (die, a);
10152         }
10153
10154       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10155     }
10156
10157   if (decl->die_parent != NULL
10158       && decl->die_parent->die_tag != DW_TAG_compile_unit
10159       && decl->die_parent->die_tag != DW_TAG_type_unit)
10160     {
10161       new_decl = copy_ancestor_tree (unit, decl, NULL);
10162       if (new_decl != NULL)
10163         {
10164           remove_AT (new_decl, DW_AT_signature);
10165           add_AT_specification (die, new_decl);
10166         }
10167     }
10168 }
10169
10170 /* Generate the skeleton ancestor tree for the given NODE, then clone
10171    the DIE and add the clone into the tree.  */
10172
10173 static void
10174 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10175 {
10176   if (node->new_die != NULL)
10177     return;
10178
10179   node->new_die = clone_as_declaration (node->old_die);
10180
10181   if (node->parent != NULL)
10182     {
10183       generate_skeleton_ancestor_tree (node->parent);
10184       add_child_die (node->parent->new_die, node->new_die);
10185     }
10186 }
10187
10188 /* Generate a skeleton tree of DIEs containing any declarations that are
10189    found in the original tree.  We traverse the tree looking for declaration
10190    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10191
10192 static void
10193 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10194 {
10195   skeleton_chain_node node;
10196   dw_die_ref c;
10197   dw_die_ref first;
10198   dw_die_ref prev = NULL;
10199   dw_die_ref next = NULL;
10200
10201   node.parent = parent;
10202
10203   first = c = parent->old_die->die_child;
10204   if (c)
10205     next = c->die_sib;
10206   if (c) do {
10207     if (prev == NULL || prev->die_sib == c)
10208       prev = c;
10209     c = next;
10210     next = (c == first ? NULL : c->die_sib);
10211     node.old_die = c;
10212     node.new_die = NULL;
10213     if (is_declaration_die (c))
10214       {
10215         /* Clone the existing DIE, move the original to the skeleton
10216            tree (which is in the main CU), and put the clone, with
10217            all the original's children, where the original came from.  */
10218         dw_die_ref clone = clone_die (c);
10219         move_all_children (c, clone);
10220
10221         replace_child (c, clone, prev);
10222         generate_skeleton_ancestor_tree (parent);
10223         add_child_die (parent->new_die, c);
10224         node.new_die = c;
10225         c = clone;
10226       }
10227     generate_skeleton_bottom_up (&node);
10228   } while (next != NULL);
10229 }
10230
10231 /* Wrapper function for generate_skeleton_bottom_up.  */
10232
10233 static dw_die_ref
10234 generate_skeleton (dw_die_ref die)
10235 {
10236   skeleton_chain_node node;
10237
10238   node.old_die = die;
10239   node.new_die = NULL;
10240   node.parent = NULL;
10241
10242   /* If this type definition is nested inside another type,
10243      always leave at least a declaration in its place.  */
10244   if (die->die_parent != NULL && is_type_die (die->die_parent))
10245     node.new_die = clone_as_declaration (die);
10246
10247   generate_skeleton_bottom_up (&node);
10248   return node.new_die;
10249 }
10250
10251 /* Remove the DIE from its parent, possibly replacing it with a cloned
10252    declaration.  The original DIE will be moved to a new compile unit
10253    so that existing references to it follow it to the new location.  If
10254    any of the original DIE's descendants is a declaration, we need to
10255    replace the original DIE with a skeleton tree and move the
10256    declarations back into the skeleton tree.  */
10257
10258 static dw_die_ref
10259 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10260 {
10261   dw_die_ref skeleton;
10262
10263   skeleton = generate_skeleton (child);
10264   if (skeleton == NULL)
10265     remove_child_with_prev (child, prev);
10266   else
10267     {
10268       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10269       replace_child (child, skeleton, prev);
10270     }
10271
10272   return skeleton;
10273 }
10274
10275 /* Traverse the DIE and set up additional .debug_types sections for each
10276    type worthy of being placed in a COMDAT section.  */
10277
10278 static void
10279 break_out_comdat_types (dw_die_ref die)
10280 {
10281   dw_die_ref c;
10282   dw_die_ref first;
10283   dw_die_ref prev = NULL;
10284   dw_die_ref next = NULL;
10285   dw_die_ref unit = NULL;
10286
10287   first = c = die->die_child;
10288   if (c)
10289     next = c->die_sib;
10290   if (c) do {
10291     if (prev == NULL || prev->die_sib == c)
10292       prev = c;
10293     c = next;
10294     next = (c == first ? NULL : c->die_sib);
10295     if (should_move_die_to_comdat (c))
10296       {
10297         dw_die_ref replacement;
10298         comdat_type_node_ref type_node;
10299
10300         /* Create a new type unit DIE as the root for the new tree, and
10301            add it to the list of comdat types.  */
10302         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10303         add_AT_unsigned (unit, DW_AT_language,
10304                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10305         type_node = ggc_alloc_cleared_comdat_type_node ();
10306         type_node->root_die = unit;
10307         type_node->next = comdat_type_list;
10308         comdat_type_list = type_node;
10309
10310         /* Generate the type signature.  */
10311         generate_type_signature (c, type_node);
10312
10313         /* Copy the declaration context, attributes, and children of the
10314            declaration into the new compile unit DIE.  */
10315         copy_declaration_context (unit, c);
10316
10317         /* Remove this DIE from the main CU.  */
10318         replacement = remove_child_or_replace_with_skeleton (c, prev);
10319
10320         /* Break out nested types into their own type units.  */
10321         break_out_comdat_types (c);
10322
10323         /* Add the DIE to the new compunit.  */
10324         add_child_die (unit, c);
10325
10326         if (replacement != NULL)
10327           c = replacement;
10328       }
10329     else if (c->die_tag == DW_TAG_namespace
10330              || c->die_tag == DW_TAG_class_type
10331              || c->die_tag == DW_TAG_structure_type
10332              || c->die_tag == DW_TAG_union_type)
10333       {
10334         /* Look for nested types that can be broken out.  */
10335         break_out_comdat_types (c);
10336       }
10337   } while (next != NULL);
10338 }
10339
10340 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10341
10342 struct decl_table_entry
10343 {
10344   dw_die_ref orig;
10345   dw_die_ref copy;
10346 };
10347
10348 /* Routines to manipulate hash table of copied declarations.  */
10349
10350 static hashval_t
10351 htab_decl_hash (const void *of)
10352 {
10353   const struct decl_table_entry *const entry =
10354     (const struct decl_table_entry *) of;
10355
10356   return htab_hash_pointer (entry->orig);
10357 }
10358
10359 static int
10360 htab_decl_eq (const void *of1, const void *of2)
10361 {
10362   const struct decl_table_entry *const entry1 =
10363     (const struct decl_table_entry *) of1;
10364   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10365
10366   return entry1->orig == entry2;
10367 }
10368
10369 static void
10370 htab_decl_del (void *what)
10371 {
10372   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10373
10374   free (entry);
10375 }
10376
10377 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10378    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10379    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10380    to check if the ancestor has already been copied into UNIT.  */
10381
10382 static dw_die_ref
10383 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10384 {
10385   dw_die_ref parent = die->die_parent;
10386   dw_die_ref new_parent = unit;
10387   dw_die_ref copy;
10388   void **slot = NULL;
10389   struct decl_table_entry *entry = NULL;
10390
10391   if (decl_table)
10392     {
10393       /* Check if the entry has already been copied to UNIT.  */
10394       slot = htab_find_slot_with_hash (decl_table, die,
10395                                        htab_hash_pointer (die), INSERT);
10396       if (*slot != HTAB_EMPTY_ENTRY)
10397         {
10398           entry = (struct decl_table_entry *) *slot;
10399           return entry->copy;
10400         }
10401
10402       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10403       entry = XCNEW (struct decl_table_entry);
10404       entry->orig = die;
10405       entry->copy = NULL;
10406       *slot = entry;
10407     }
10408
10409   if (parent != NULL)
10410     {
10411       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10412       if (spec != NULL)
10413         parent = spec;
10414       if (parent->die_tag != DW_TAG_compile_unit
10415           && parent->die_tag != DW_TAG_type_unit)
10416         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10417     }
10418
10419   copy = clone_as_declaration (die);
10420   add_child_die (new_parent, copy);
10421
10422   if (decl_table != NULL)
10423     {
10424       /* Record the pointer to the copy.  */
10425       entry->copy = copy;
10426     }
10427
10428   return copy;
10429 }
10430
10431 /* Walk the DIE and its children, looking for references to incomplete
10432    or trivial types that are unmarked (i.e., that are not in the current
10433    type_unit).  */
10434
10435 static void
10436 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10437 {
10438   dw_die_ref c;
10439   dw_attr_ref a;
10440   unsigned ix;
10441
10442   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10443     {
10444       if (AT_class (a) == dw_val_class_die_ref)
10445         {
10446           dw_die_ref targ = AT_ref (a);
10447           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10448           void **slot;
10449           struct decl_table_entry *entry;
10450
10451           if (targ->die_mark != 0 || type_node != NULL)
10452             continue;
10453
10454           slot = htab_find_slot_with_hash (decl_table, targ,
10455                                            htab_hash_pointer (targ), INSERT);
10456
10457           if (*slot != HTAB_EMPTY_ENTRY)
10458             {
10459               /* TARG has already been copied, so we just need to
10460                  modify the reference to point to the copy.  */
10461               entry = (struct decl_table_entry *) *slot;
10462               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10463             }
10464           else
10465             {
10466               dw_die_ref parent = unit;
10467               dw_die_ref copy = clone_tree (targ);
10468
10469               /* Make sure the cloned tree is marked as part of the
10470                  type unit.  */
10471               mark_dies (copy);
10472
10473               /* Record in DECL_TABLE that TARG has been copied.
10474                  Need to do this now, before the recursive call,
10475                  because DECL_TABLE may be expanded and SLOT
10476                  would no longer be a valid pointer.  */
10477               entry = XCNEW (struct decl_table_entry);
10478               entry->orig = targ;
10479               entry->copy = copy;
10480               *slot = entry;
10481
10482               /* If TARG has surrounding context, copy its ancestor tree
10483                  into the new type unit.  */
10484               if (targ->die_parent != NULL
10485                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10486                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10487                 parent = copy_ancestor_tree (unit, targ->die_parent,
10488                                              decl_table);
10489
10490               add_child_die (parent, copy);
10491               a->dw_attr_val.v.val_die_ref.die = copy;
10492
10493               /* Make sure the newly-copied DIE is walked.  If it was
10494                  installed in a previously-added context, it won't
10495                  get visited otherwise.  */
10496               if (parent != unit)
10497                 {
10498                   /* Find the highest point of the newly-added tree,
10499                      mark each node along the way, and walk from there.  */
10500                   parent->die_mark = 1;
10501                   while (parent->die_parent
10502                          && parent->die_parent->die_mark == 0)
10503                     {
10504                       parent = parent->die_parent;
10505                       parent->die_mark = 1;
10506                     }
10507                   copy_decls_walk (unit, parent, decl_table);
10508                 }
10509             }
10510         }
10511     }
10512
10513   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10514 }
10515
10516 /* Copy declarations for "unworthy" types into the new comdat section.
10517    Incomplete types, modified types, and certain other types aren't broken
10518    out into comdat sections of their own, so they don't have a signature,
10519    and we need to copy the declaration into the same section so that we
10520    don't have an external reference.  */
10521
10522 static void
10523 copy_decls_for_unworthy_types (dw_die_ref unit)
10524 {
10525   htab_t decl_table;
10526
10527   mark_dies (unit);
10528   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10529   copy_decls_walk (unit, unit, decl_table);
10530   htab_delete (decl_table);
10531   unmark_dies (unit);
10532 }
10533
10534 /* Traverse the DIE and add a sibling attribute if it may have the
10535    effect of speeding up access to siblings.  To save some space,
10536    avoid generating sibling attributes for DIE's without children.  */
10537
10538 static void
10539 add_sibling_attributes (dw_die_ref die)
10540 {
10541   dw_die_ref c;
10542
10543   if (! die->die_child)
10544     return;
10545
10546   if (die->die_parent && die != die->die_parent->die_child)
10547     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10548
10549   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10550 }
10551
10552 /* Output all location lists for the DIE and its children.  */
10553
10554 static void
10555 output_location_lists (dw_die_ref die)
10556 {
10557   dw_die_ref c;
10558   dw_attr_ref a;
10559   unsigned ix;
10560
10561   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10562     if (AT_class (a) == dw_val_class_loc_list)
10563       output_loc_list (AT_loc_list (a));
10564
10565   FOR_EACH_CHILD (die, c, output_location_lists (c));
10566 }
10567
10568 /* The format of each DIE (and its attribute value pairs) is encoded in an
10569    abbreviation table.  This routine builds the abbreviation table and assigns
10570    a unique abbreviation id for each abbreviation entry.  The children of each
10571    die are visited recursively.  */
10572
10573 static void
10574 build_abbrev_table (dw_die_ref die)
10575 {
10576   unsigned long abbrev_id;
10577   unsigned int n_alloc;
10578   dw_die_ref c;
10579   dw_attr_ref a;
10580   unsigned ix;
10581
10582   /* Scan the DIE references, and mark as external any that refer to
10583      DIEs from other CUs (i.e. those which are not marked).  */
10584   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10585     if (AT_class (a) == dw_val_class_die_ref
10586         && AT_ref (a)->die_mark == 0)
10587       {
10588         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10589         set_AT_ref_external (a, 1);
10590       }
10591
10592   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10593     {
10594       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10595       dw_attr_ref die_a, abbrev_a;
10596       unsigned ix;
10597       bool ok = true;
10598
10599       if (abbrev->die_tag != die->die_tag)
10600         continue;
10601       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10602         continue;
10603
10604       if (VEC_length (dw_attr_node, abbrev->die_attr)
10605           != VEC_length (dw_attr_node, die->die_attr))
10606         continue;
10607
10608       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10609         {
10610           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10611           if ((abbrev_a->dw_attr != die_a->dw_attr)
10612               || (value_format (abbrev_a) != value_format (die_a)))
10613             {
10614               ok = false;
10615               break;
10616             }
10617         }
10618       if (ok)
10619         break;
10620     }
10621
10622   if (abbrev_id >= abbrev_die_table_in_use)
10623     {
10624       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10625         {
10626           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10627           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10628                                             n_alloc);
10629
10630           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10631                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10632           abbrev_die_table_allocated = n_alloc;
10633         }
10634
10635       ++abbrev_die_table_in_use;
10636       abbrev_die_table[abbrev_id] = die;
10637     }
10638
10639   die->die_abbrev = abbrev_id;
10640   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10641 }
10642 \f
10643 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10644
10645 static int
10646 constant_size (unsigned HOST_WIDE_INT value)
10647 {
10648   int log;
10649
10650   if (value == 0)
10651     log = 0;
10652   else
10653     log = floor_log2 (value);
10654
10655   log = log / 8;
10656   log = 1 << (floor_log2 (log) + 1);
10657
10658   return log;
10659 }
10660
10661 /* Return the size of a DIE as it is represented in the
10662    .debug_info section.  */
10663
10664 static unsigned long
10665 size_of_die (dw_die_ref die)
10666 {
10667   unsigned long size = 0;
10668   dw_attr_ref a;
10669   unsigned ix;
10670
10671   size += size_of_uleb128 (die->die_abbrev);
10672   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10673     {
10674       switch (AT_class (a))
10675         {
10676         case dw_val_class_addr:
10677           size += DWARF2_ADDR_SIZE;
10678           break;
10679         case dw_val_class_offset:
10680           size += DWARF_OFFSET_SIZE;
10681           break;
10682         case dw_val_class_loc:
10683           {
10684             unsigned long lsize = size_of_locs (AT_loc (a));
10685
10686             /* Block length.  */
10687             if (dwarf_version >= 4)
10688               size += size_of_uleb128 (lsize);
10689             else
10690               size += constant_size (lsize);
10691             size += lsize;
10692           }
10693           break;
10694         case dw_val_class_loc_list:
10695           size += DWARF_OFFSET_SIZE;
10696           break;
10697         case dw_val_class_range_list:
10698           size += DWARF_OFFSET_SIZE;
10699           break;
10700         case dw_val_class_const:
10701           size += size_of_sleb128 (AT_int (a));
10702           break;
10703         case dw_val_class_unsigned_const:
10704           size += constant_size (AT_unsigned (a));
10705           break;
10706         case dw_val_class_const_double:
10707           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10708           if (HOST_BITS_PER_WIDE_INT >= 64)
10709             size++; /* block */
10710           break;
10711         case dw_val_class_vec:
10712           size += constant_size (a->dw_attr_val.v.val_vec.length
10713                                  * a->dw_attr_val.v.val_vec.elt_size)
10714                   + a->dw_attr_val.v.val_vec.length
10715                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10716           break;
10717         case dw_val_class_flag:
10718           if (dwarf_version >= 4)
10719             /* Currently all add_AT_flag calls pass in 1 as last argument,
10720                so DW_FORM_flag_present can be used.  If that ever changes,
10721                we'll need to use DW_FORM_flag and have some optimization
10722                in build_abbrev_table that will change those to
10723                DW_FORM_flag_present if it is set to 1 in all DIEs using
10724                the same abbrev entry.  */
10725             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10726           else
10727             size += 1;
10728           break;
10729         case dw_val_class_die_ref:
10730           if (AT_ref_external (a))
10731             {
10732               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10733                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10734                  is sized by target address length, whereas in DWARF3
10735                  it's always sized as an offset.  */
10736               if (dwarf_version >= 4)
10737                 size += DWARF_TYPE_SIGNATURE_SIZE;
10738               else if (dwarf_version == 2)
10739                 size += DWARF2_ADDR_SIZE;
10740               else
10741                 size += DWARF_OFFSET_SIZE;
10742             }
10743           else
10744             size += DWARF_OFFSET_SIZE;
10745           break;
10746         case dw_val_class_fde_ref:
10747           size += DWARF_OFFSET_SIZE;
10748           break;
10749         case dw_val_class_lbl_id:
10750           size += DWARF2_ADDR_SIZE;
10751           break;
10752         case dw_val_class_lineptr:
10753         case dw_val_class_macptr:
10754           size += DWARF_OFFSET_SIZE;
10755           break;
10756         case dw_val_class_str:
10757           if (AT_string_form (a) == DW_FORM_strp)
10758             size += DWARF_OFFSET_SIZE;
10759           else
10760             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10761           break;
10762         case dw_val_class_file:
10763           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10764           break;
10765         case dw_val_class_data8:
10766           size += 8;
10767           break;
10768         case dw_val_class_vms_delta:
10769           size += DWARF_OFFSET_SIZE;
10770           break;
10771         default:
10772           gcc_unreachable ();
10773         }
10774     }
10775
10776   return size;
10777 }
10778
10779 /* Size the debugging information associated with a given DIE.  Visits the
10780    DIE's children recursively.  Updates the global variable next_die_offset, on
10781    each time through.  Uses the current value of next_die_offset to update the
10782    die_offset field in each DIE.  */
10783
10784 static void
10785 calc_die_sizes (dw_die_ref die)
10786 {
10787   dw_die_ref c;
10788
10789   die->die_offset = next_die_offset;
10790   next_die_offset += size_of_die (die);
10791
10792   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10793
10794   if (die->die_child != NULL)
10795     /* Count the null byte used to terminate sibling lists.  */
10796     next_die_offset += 1;
10797 }
10798
10799 /* Set the marks for a die and its children.  We do this so
10800    that we know whether or not a reference needs to use FORM_ref_addr; only
10801    DIEs in the same CU will be marked.  We used to clear out the offset
10802    and use that as the flag, but ran into ordering problems.  */
10803
10804 static void
10805 mark_dies (dw_die_ref die)
10806 {
10807   dw_die_ref c;
10808
10809   gcc_assert (!die->die_mark);
10810
10811   die->die_mark = 1;
10812   FOR_EACH_CHILD (die, c, mark_dies (c));
10813 }
10814
10815 /* Clear the marks for a die and its children.  */
10816
10817 static void
10818 unmark_dies (dw_die_ref die)
10819 {
10820   dw_die_ref c;
10821
10822   if (dwarf_version < 4)
10823     gcc_assert (die->die_mark);
10824
10825   die->die_mark = 0;
10826   FOR_EACH_CHILD (die, c, unmark_dies (c));
10827 }
10828
10829 /* Clear the marks for a die, its children and referred dies.  */
10830
10831 static void
10832 unmark_all_dies (dw_die_ref die)
10833 {
10834   dw_die_ref c;
10835   dw_attr_ref a;
10836   unsigned ix;
10837
10838   if (!die->die_mark)
10839     return;
10840   die->die_mark = 0;
10841
10842   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10843
10844   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10845     if (AT_class (a) == dw_val_class_die_ref)
10846       unmark_all_dies (AT_ref (a));
10847 }
10848
10849 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10850    generated for the compilation unit.  */
10851
10852 static unsigned long
10853 size_of_pubnames (VEC (pubname_entry, gc) * names)
10854 {
10855   unsigned long size;
10856   unsigned i;
10857   pubname_ref p;
10858
10859   size = DWARF_PUBNAMES_HEADER_SIZE;
10860   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10861     if (names != pubtype_table
10862         || p->die->die_offset != 0
10863         || !flag_eliminate_unused_debug_types)
10864       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10865
10866   size += DWARF_OFFSET_SIZE;
10867   return size;
10868 }
10869
10870 /* Return the size of the information in the .debug_aranges section.  */
10871
10872 static unsigned long
10873 size_of_aranges (void)
10874 {
10875   unsigned long size;
10876
10877   size = DWARF_ARANGES_HEADER_SIZE;
10878
10879   /* Count the address/length pair for this compilation unit.  */
10880   if (text_section_used)
10881     size += 2 * DWARF2_ADDR_SIZE;
10882   if (cold_text_section_used)
10883     size += 2 * DWARF2_ADDR_SIZE;
10884   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10885
10886   /* Count the two zero words used to terminated the address range table.  */
10887   size += 2 * DWARF2_ADDR_SIZE;
10888   return size;
10889 }
10890 \f
10891 /* Select the encoding of an attribute value.  */
10892
10893 static enum dwarf_form
10894 value_format (dw_attr_ref a)
10895 {
10896   switch (a->dw_attr_val.val_class)
10897     {
10898     case dw_val_class_addr:
10899       /* Only very few attributes allow DW_FORM_addr.  */
10900       switch (a->dw_attr)
10901         {
10902         case DW_AT_low_pc:
10903         case DW_AT_high_pc:
10904         case DW_AT_entry_pc:
10905         case DW_AT_trampoline:
10906           return DW_FORM_addr;
10907         default:
10908           break;
10909         }
10910       switch (DWARF2_ADDR_SIZE)
10911         {
10912         case 1:
10913           return DW_FORM_data1;
10914         case 2:
10915           return DW_FORM_data2;
10916         case 4:
10917           return DW_FORM_data4;
10918         case 8:
10919           return DW_FORM_data8;
10920         default:
10921           gcc_unreachable ();
10922         }
10923     case dw_val_class_range_list:
10924     case dw_val_class_loc_list:
10925       if (dwarf_version >= 4)
10926         return DW_FORM_sec_offset;
10927       /* FALLTHRU */
10928     case dw_val_class_vms_delta:
10929     case dw_val_class_offset:
10930       switch (DWARF_OFFSET_SIZE)
10931         {
10932         case 4:
10933           return DW_FORM_data4;
10934         case 8:
10935           return DW_FORM_data8;
10936         default:
10937           gcc_unreachable ();
10938         }
10939     case dw_val_class_loc:
10940       if (dwarf_version >= 4)
10941         return DW_FORM_exprloc;
10942       switch (constant_size (size_of_locs (AT_loc (a))))
10943         {
10944         case 1:
10945           return DW_FORM_block1;
10946         case 2:
10947           return DW_FORM_block2;
10948         default:
10949           gcc_unreachable ();
10950         }
10951     case dw_val_class_const:
10952       return DW_FORM_sdata;
10953     case dw_val_class_unsigned_const:
10954       switch (constant_size (AT_unsigned (a)))
10955         {
10956         case 1:
10957           return DW_FORM_data1;
10958         case 2:
10959           return DW_FORM_data2;
10960         case 4:
10961           return DW_FORM_data4;
10962         case 8:
10963           return DW_FORM_data8;
10964         default:
10965           gcc_unreachable ();
10966         }
10967     case dw_val_class_const_double:
10968       switch (HOST_BITS_PER_WIDE_INT)
10969         {
10970         case 8:
10971           return DW_FORM_data2;
10972         case 16:
10973           return DW_FORM_data4;
10974         case 32:
10975           return DW_FORM_data8;
10976         case 64:
10977         default:
10978           return DW_FORM_block1;
10979         }
10980     case dw_val_class_vec:
10981       switch (constant_size (a->dw_attr_val.v.val_vec.length
10982                              * a->dw_attr_val.v.val_vec.elt_size))
10983         {
10984         case 1:
10985           return DW_FORM_block1;
10986         case 2:
10987           return DW_FORM_block2;
10988         case 4:
10989           return DW_FORM_block4;
10990         default:
10991           gcc_unreachable ();
10992         }
10993     case dw_val_class_flag:
10994       if (dwarf_version >= 4)
10995         {
10996           /* Currently all add_AT_flag calls pass in 1 as last argument,
10997              so DW_FORM_flag_present can be used.  If that ever changes,
10998              we'll need to use DW_FORM_flag and have some optimization
10999              in build_abbrev_table that will change those to
11000              DW_FORM_flag_present if it is set to 1 in all DIEs using
11001              the same abbrev entry.  */
11002           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11003           return DW_FORM_flag_present;
11004         }
11005       return DW_FORM_flag;
11006     case dw_val_class_die_ref:
11007       if (AT_ref_external (a))
11008         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
11009       else
11010         return DW_FORM_ref;
11011     case dw_val_class_fde_ref:
11012       return DW_FORM_data;
11013     case dw_val_class_lbl_id:
11014       return DW_FORM_addr;
11015     case dw_val_class_lineptr:
11016     case dw_val_class_macptr:
11017       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11018     case dw_val_class_str:
11019       return AT_string_form (a);
11020     case dw_val_class_file:
11021       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11022         {
11023         case 1:
11024           return DW_FORM_data1;
11025         case 2:
11026           return DW_FORM_data2;
11027         case 4:
11028           return DW_FORM_data4;
11029         default:
11030           gcc_unreachable ();
11031         }
11032
11033     case dw_val_class_data8:
11034       return DW_FORM_data8;
11035
11036     default:
11037       gcc_unreachable ();
11038     }
11039 }
11040
11041 /* Output the encoding of an attribute value.  */
11042
11043 static void
11044 output_value_format (dw_attr_ref a)
11045 {
11046   enum dwarf_form form = value_format (a);
11047
11048   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11049 }
11050
11051 /* Output the .debug_abbrev section which defines the DIE abbreviation
11052    table.  */
11053
11054 static void
11055 output_abbrev_section (void)
11056 {
11057   unsigned long abbrev_id;
11058
11059   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11060     {
11061       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11062       unsigned ix;
11063       dw_attr_ref a_attr;
11064
11065       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11066       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11067                                    dwarf_tag_name (abbrev->die_tag));
11068
11069       if (abbrev->die_child != NULL)
11070         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11071       else
11072         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11073
11074       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11075            ix++)
11076         {
11077           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11078                                        dwarf_attr_name (a_attr->dw_attr));
11079           output_value_format (a_attr);
11080         }
11081
11082       dw2_asm_output_data (1, 0, NULL);
11083       dw2_asm_output_data (1, 0, NULL);
11084     }
11085
11086   /* Terminate the table.  */
11087   dw2_asm_output_data (1, 0, NULL);
11088 }
11089
11090 /* Output a symbol we can use to refer to this DIE from another CU.  */
11091
11092 static inline void
11093 output_die_symbol (dw_die_ref die)
11094 {
11095   char *sym = die->die_id.die_symbol;
11096
11097   if (sym == 0)
11098     return;
11099
11100   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11101     /* We make these global, not weak; if the target doesn't support
11102        .linkonce, it doesn't support combining the sections, so debugging
11103        will break.  */
11104     targetm.asm_out.globalize_label (asm_out_file, sym);
11105
11106   ASM_OUTPUT_LABEL (asm_out_file, sym);
11107 }
11108
11109 /* Return a new location list, given the begin and end range, and the
11110    expression.  */
11111
11112 static inline dw_loc_list_ref
11113 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11114               const char *section)
11115 {
11116   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11117
11118   retlist->begin = begin;
11119   retlist->end = end;
11120   retlist->expr = expr;
11121   retlist->section = section;
11122
11123   return retlist;
11124 }
11125
11126 /* Generate a new internal symbol for this location list node, if it
11127    hasn't got one yet.  */
11128
11129 static inline void
11130 gen_llsym (dw_loc_list_ref list)
11131 {
11132   gcc_assert (!list->ll_symbol);
11133   list->ll_symbol = gen_internal_sym ("LLST");
11134 }
11135
11136 /* Output the location list given to us.  */
11137
11138 static void
11139 output_loc_list (dw_loc_list_ref list_head)
11140 {
11141   dw_loc_list_ref curr = list_head;
11142
11143   if (list_head->emitted)
11144     return;
11145   list_head->emitted = true;
11146
11147   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11148
11149   /* Walk the location list, and output each range + expression.  */
11150   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11151     {
11152       unsigned long size;
11153       /* Don't output an entry that starts and ends at the same address.  */
11154       if (strcmp (curr->begin, curr->end) == 0)
11155         continue;
11156       if (!have_multiple_function_sections)
11157         {
11158           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11159                                 "Location list begin address (%s)",
11160                                 list_head->ll_symbol);
11161           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11162                                 "Location list end address (%s)",
11163                                 list_head->ll_symbol);
11164         }
11165       else
11166         {
11167           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11168                                "Location list begin address (%s)",
11169                                list_head->ll_symbol);
11170           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11171                                "Location list end address (%s)",
11172                                list_head->ll_symbol);
11173         }
11174       size = size_of_locs (curr->expr);
11175
11176       /* Output the block length for this list of location operations.  */
11177       gcc_assert (size <= 0xffff);
11178       dw2_asm_output_data (2, size, "%s", "Location expression size");
11179
11180       output_loc_sequence (curr->expr, -1);
11181     }
11182
11183   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11184                        "Location list terminator begin (%s)",
11185                        list_head->ll_symbol);
11186   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11187                        "Location list terminator end (%s)",
11188                        list_head->ll_symbol);
11189 }
11190
11191 /* Output a type signature.  */
11192
11193 static inline void
11194 output_signature (const char *sig, const char *name)
11195 {
11196   int i;
11197
11198   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11199     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11200 }
11201
11202 /* Output the DIE and its attributes.  Called recursively to generate
11203    the definitions of each child DIE.  */
11204
11205 static void
11206 output_die (dw_die_ref die)
11207 {
11208   dw_attr_ref a;
11209   dw_die_ref c;
11210   unsigned long size;
11211   unsigned ix;
11212
11213   /* If someone in another CU might refer to us, set up a symbol for
11214      them to point to.  */
11215   if (dwarf_version < 4 && die->die_id.die_symbol)
11216     output_die_symbol (die);
11217
11218   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11219                                (unsigned long)die->die_offset,
11220                                dwarf_tag_name (die->die_tag));
11221
11222   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11223     {
11224       const char *name = dwarf_attr_name (a->dw_attr);
11225
11226       switch (AT_class (a))
11227         {
11228         case dw_val_class_addr:
11229           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11230           break;
11231
11232         case dw_val_class_offset:
11233           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11234                                "%s", name);
11235           break;
11236
11237         case dw_val_class_range_list:
11238           {
11239             char *p = strchr (ranges_section_label, '\0');
11240
11241             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11242                      a->dw_attr_val.v.val_offset);
11243             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11244                                    debug_ranges_section, "%s", name);
11245             *p = '\0';
11246           }
11247           break;
11248
11249         case dw_val_class_loc:
11250           size = size_of_locs (AT_loc (a));
11251
11252           /* Output the block length for this list of location operations.  */
11253           if (dwarf_version >= 4)
11254             dw2_asm_output_data_uleb128 (size, "%s", name);
11255           else
11256             dw2_asm_output_data (constant_size (size), size, "%s", name);
11257
11258           output_loc_sequence (AT_loc (a), -1);
11259           break;
11260
11261         case dw_val_class_const:
11262           /* ??? It would be slightly more efficient to use a scheme like is
11263              used for unsigned constants below, but gdb 4.x does not sign
11264              extend.  Gdb 5.x does sign extend.  */
11265           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11266           break;
11267
11268         case dw_val_class_unsigned_const:
11269           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11270                                AT_unsigned (a), "%s", name);
11271           break;
11272
11273         case dw_val_class_const_double:
11274           {
11275             unsigned HOST_WIDE_INT first, second;
11276
11277             if (HOST_BITS_PER_WIDE_INT >= 64)
11278               dw2_asm_output_data (1,
11279                                    2 * HOST_BITS_PER_WIDE_INT
11280                                    / HOST_BITS_PER_CHAR,
11281                                    NULL);
11282
11283             if (WORDS_BIG_ENDIAN)
11284               {
11285                 first = a->dw_attr_val.v.val_double.high;
11286                 second = a->dw_attr_val.v.val_double.low;
11287               }
11288             else
11289               {
11290                 first = a->dw_attr_val.v.val_double.low;
11291                 second = a->dw_attr_val.v.val_double.high;
11292               }
11293
11294             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11295                                  first, name);
11296             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11297                                  second, NULL);
11298           }
11299           break;
11300
11301         case dw_val_class_vec:
11302           {
11303             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11304             unsigned int len = a->dw_attr_val.v.val_vec.length;
11305             unsigned int i;
11306             unsigned char *p;
11307
11308             dw2_asm_output_data (constant_size (len * elt_size),
11309                                  len * elt_size, "%s", name);
11310             if (elt_size > sizeof (HOST_WIDE_INT))
11311               {
11312                 elt_size /= 2;
11313                 len *= 2;
11314               }
11315             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11316                  i < len;
11317                  i++, p += elt_size)
11318               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11319                                    "fp or vector constant word %u", i);
11320             break;
11321           }
11322
11323         case dw_val_class_flag:
11324           if (dwarf_version >= 4)
11325             {
11326               /* Currently all add_AT_flag calls pass in 1 as last argument,
11327                  so DW_FORM_flag_present can be used.  If that ever changes,
11328                  we'll need to use DW_FORM_flag and have some optimization
11329                  in build_abbrev_table that will change those to
11330                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11331                  the same abbrev entry.  */
11332               gcc_assert (AT_flag (a) == 1);
11333               if (flag_debug_asm)
11334                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11335                          ASM_COMMENT_START, name);
11336               break;
11337             }
11338           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11339           break;
11340
11341         case dw_val_class_loc_list:
11342           {
11343             char *sym = AT_loc_list (a)->ll_symbol;
11344
11345             gcc_assert (sym);
11346             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11347                                    "%s", name);
11348           }
11349           break;
11350
11351         case dw_val_class_die_ref:
11352           if (AT_ref_external (a))
11353             {
11354               if (dwarf_version >= 4)
11355                 {
11356                   comdat_type_node_ref type_node =
11357                     AT_ref (a)->die_id.die_type_node;
11358
11359                   gcc_assert (type_node);
11360                   output_signature (type_node->signature, name);
11361                 }
11362               else
11363                 {
11364                   char *sym = AT_ref (a)->die_id.die_symbol;
11365                   int size;
11366
11367                   gcc_assert (sym);
11368                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11369                      length, whereas in DWARF3 it's always sized as an
11370                      offset.  */
11371                   if (dwarf_version == 2)
11372                     size = DWARF2_ADDR_SIZE;
11373                   else
11374                     size = DWARF_OFFSET_SIZE;
11375                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11376                                          name);
11377                 }
11378             }
11379           else
11380             {
11381               gcc_assert (AT_ref (a)->die_offset);
11382               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11383                                    "%s", name);
11384             }
11385           break;
11386
11387         case dw_val_class_fde_ref:
11388           {
11389             char l1[20];
11390
11391             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11392                                          a->dw_attr_val.v.val_fde_index * 2);
11393             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11394                                    "%s", name);
11395           }
11396           break;
11397
11398         case dw_val_class_vms_delta:
11399           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11400                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11401                                     "%s", name);
11402           break;
11403
11404         case dw_val_class_lbl_id:
11405           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11406           break;
11407
11408         case dw_val_class_lineptr:
11409           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11410                                  debug_line_section, "%s", name);
11411           break;
11412
11413         case dw_val_class_macptr:
11414           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11415                                  debug_macinfo_section, "%s", name);
11416           break;
11417
11418         case dw_val_class_str:
11419           if (AT_string_form (a) == DW_FORM_strp)
11420             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11421                                    a->dw_attr_val.v.val_str->label,
11422                                    debug_str_section,
11423                                    "%s: \"%s\"", name, AT_string (a));
11424           else
11425             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11426           break;
11427
11428         case dw_val_class_file:
11429           {
11430             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11431
11432             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11433                                  a->dw_attr_val.v.val_file->filename);
11434             break;
11435           }
11436
11437         case dw_val_class_data8:
11438           {
11439             int i;
11440
11441             for (i = 0; i < 8; i++)
11442               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11443                                    i == 0 ? "%s" : NULL, name);
11444             break;
11445           }
11446
11447         default:
11448           gcc_unreachable ();
11449         }
11450     }
11451
11452   FOR_EACH_CHILD (die, c, output_die (c));
11453
11454   /* Add null byte to terminate sibling list.  */
11455   if (die->die_child != NULL)
11456     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11457                          (unsigned long) die->die_offset);
11458 }
11459
11460 /* Output the compilation unit that appears at the beginning of the
11461    .debug_info section, and precedes the DIE descriptions.  */
11462
11463 static void
11464 output_compilation_unit_header (void)
11465 {
11466   int ver = dwarf_version;
11467
11468   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11469     dw2_asm_output_data (4, 0xffffffff,
11470       "Initial length escape value indicating 64-bit DWARF extension");
11471   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11472                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11473                        "Length of Compilation Unit Info");
11474   dw2_asm_output_data (2, ver, "DWARF version number");
11475   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11476                          debug_abbrev_section,
11477                          "Offset Into Abbrev. Section");
11478   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11479 }
11480
11481 /* Output the compilation unit DIE and its children.  */
11482
11483 static void
11484 output_comp_unit (dw_die_ref die, int output_if_empty)
11485 {
11486   const char *secname;
11487   char *oldsym, *tmp;
11488
11489   /* Unless we are outputting main CU, we may throw away empty ones.  */
11490   if (!output_if_empty && die->die_child == NULL)
11491     return;
11492
11493   /* Even if there are no children of this DIE, we must output the information
11494      about the compilation unit.  Otherwise, on an empty translation unit, we
11495      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11496      will then complain when examining the file.  First mark all the DIEs in
11497      this CU so we know which get local refs.  */
11498   mark_dies (die);
11499
11500   build_abbrev_table (die);
11501
11502   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11503   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11504   calc_die_sizes (die);
11505
11506   oldsym = die->die_id.die_symbol;
11507   if (oldsym)
11508     {
11509       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11510
11511       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11512       secname = tmp;
11513       die->die_id.die_symbol = NULL;
11514       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11515     }
11516   else
11517     {
11518       switch_to_section (debug_info_section);
11519       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11520       info_section_emitted = true;
11521     }
11522
11523   /* Output debugging information.  */
11524   output_compilation_unit_header ();
11525   output_die (die);
11526
11527   /* Leave the marks on the main CU, so we can check them in
11528      output_pubnames.  */
11529   if (oldsym)
11530     {
11531       unmark_dies (die);
11532       die->die_id.die_symbol = oldsym;
11533     }
11534 }
11535
11536 /* Output a comdat type unit DIE and its children.  */
11537
11538 static void
11539 output_comdat_type_unit (comdat_type_node *node)
11540 {
11541   const char *secname;
11542   char *tmp;
11543   int i;
11544 #if defined (OBJECT_FORMAT_ELF)
11545   tree comdat_key;
11546 #endif
11547
11548   /* First mark all the DIEs in this CU so we know which get local refs.  */
11549   mark_dies (node->root_die);
11550
11551   build_abbrev_table (node->root_die);
11552
11553   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11554   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11555   calc_die_sizes (node->root_die);
11556
11557 #if defined (OBJECT_FORMAT_ELF)
11558   secname = ".debug_types";
11559   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11560   sprintf (tmp, "wt.");
11561   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11562     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11563   comdat_key = get_identifier (tmp);
11564   targetm.asm_out.named_section (secname,
11565                                  SECTION_DEBUG | SECTION_LINKONCE,
11566                                  comdat_key);
11567 #else
11568   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11569   sprintf (tmp, ".gnu.linkonce.wt.");
11570   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11571     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11572   secname = tmp;
11573   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11574 #endif
11575
11576   /* Output debugging information.  */
11577   output_compilation_unit_header ();
11578   output_signature (node->signature, "Type Signature");
11579   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11580                        "Offset to Type DIE");
11581   output_die (node->root_die);
11582
11583   unmark_dies (node->root_die);
11584 }
11585
11586 /* Return the DWARF2/3 pubname associated with a decl.  */
11587
11588 static const char *
11589 dwarf2_name (tree decl, int scope)
11590 {
11591   if (DECL_NAMELESS (decl))
11592     return NULL;
11593   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11594 }
11595
11596 /* Add a new entry to .debug_pubnames if appropriate.  */
11597
11598 static void
11599 add_pubname_string (const char *str, dw_die_ref die)
11600 {
11601   if (targetm.want_debug_pub_sections)
11602     {
11603       pubname_entry e;
11604
11605       e.die = die;
11606       e.name = xstrdup (str);
11607       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11608     }
11609 }
11610
11611 static void
11612 add_pubname (tree decl, dw_die_ref die)
11613 {
11614   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11615     {
11616       const char *name = dwarf2_name (decl, 1);
11617       if (name)
11618         add_pubname_string (name, die);
11619     }
11620 }
11621
11622 /* Add a new entry to .debug_pubtypes if appropriate.  */
11623
11624 static void
11625 add_pubtype (tree decl, dw_die_ref die)
11626 {
11627   pubname_entry e;
11628
11629   if (!targetm.want_debug_pub_sections)
11630     return;
11631
11632   e.name = NULL;
11633   if ((TREE_PUBLIC (decl)
11634        || is_cu_die (die->die_parent))
11635       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11636     {
11637       e.die = die;
11638       if (TYPE_P (decl))
11639         {
11640           if (TYPE_NAME (decl))
11641             {
11642               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11643                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11644               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11645                        && DECL_NAME (TYPE_NAME (decl)))
11646                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11647               else
11648                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11649             }
11650         }
11651       else
11652         {
11653           e.name = dwarf2_name (decl, 1);
11654           if (e.name)
11655             e.name = xstrdup (e.name);
11656         }
11657
11658       /* If we don't have a name for the type, there's no point in adding
11659          it to the table.  */
11660       if (e.name && e.name[0] != '\0')
11661         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11662     }
11663 }
11664
11665 /* Output the public names table used to speed up access to externally
11666    visible names; or the public types table used to find type definitions.  */
11667
11668 static void
11669 output_pubnames (VEC (pubname_entry, gc) * names)
11670 {
11671   unsigned i;
11672   unsigned long pubnames_length = size_of_pubnames (names);
11673   pubname_ref pub;
11674
11675   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11676     dw2_asm_output_data (4, 0xffffffff,
11677       "Initial length escape value indicating 64-bit DWARF extension");
11678   if (names == pubname_table)
11679     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11680                          "Length of Public Names Info");
11681   else
11682     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11683                          "Length of Public Type Names Info");
11684   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11685   dw2_asm_output_data (2, 2, "DWARF Version");
11686   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11687                          debug_info_section,
11688                          "Offset of Compilation Unit Info");
11689   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11690                        "Compilation Unit Length");
11691
11692   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11693     {
11694       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11695       if (names == pubname_table)
11696         gcc_assert (pub->die->die_mark);
11697
11698       if (names != pubtype_table
11699           || pub->die->die_offset != 0
11700           || !flag_eliminate_unused_debug_types)
11701         {
11702           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11703                                "DIE offset");
11704
11705           dw2_asm_output_nstring (pub->name, -1, "external name");
11706         }
11707     }
11708
11709   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11710 }
11711
11712 /* Add a new entry to .debug_aranges if appropriate.  */
11713
11714 static void
11715 add_arange (tree decl, dw_die_ref die)
11716 {
11717   if (! DECL_SECTION_NAME (decl))
11718     return;
11719
11720   if (arange_table_in_use == arange_table_allocated)
11721     {
11722       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11723       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11724                                     arange_table_allocated);
11725       memset (arange_table + arange_table_in_use, 0,
11726               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11727     }
11728
11729   arange_table[arange_table_in_use++] = die;
11730 }
11731
11732 /* Output the information that goes into the .debug_aranges table.
11733    Namely, define the beginning and ending address range of the
11734    text section generated for this compilation unit.  */
11735
11736 static void
11737 output_aranges (void)
11738 {
11739   unsigned i;
11740   unsigned long aranges_length = size_of_aranges ();
11741
11742   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11743     dw2_asm_output_data (4, 0xffffffff,
11744       "Initial length escape value indicating 64-bit DWARF extension");
11745   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11746                        "Length of Address Ranges Info");
11747   /* Version number for aranges is still 2, even in DWARF3.  */
11748   dw2_asm_output_data (2, 2, "DWARF Version");
11749   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11750                          debug_info_section,
11751                          "Offset of Compilation Unit Info");
11752   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11753   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11754
11755   /* We need to align to twice the pointer size here.  */
11756   if (DWARF_ARANGES_PAD_SIZE)
11757     {
11758       /* Pad using a 2 byte words so that padding is correct for any
11759          pointer size.  */
11760       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11761                            2 * DWARF2_ADDR_SIZE);
11762       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11763         dw2_asm_output_data (2, 0, NULL);
11764     }
11765
11766   /* It is necessary not to output these entries if the sections were
11767      not used; if the sections were not used, the length will be 0 and
11768      the address may end up as 0 if the section is discarded by ld
11769      --gc-sections, leaving an invalid (0, 0) entry that can be
11770      confused with the terminator.  */
11771   if (text_section_used)
11772     {
11773       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11774       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11775                             text_section_label, "Length");
11776     }
11777   if (cold_text_section_used)
11778     {
11779       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11780                            "Address");
11781       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11782                             cold_text_section_label, "Length");
11783     }
11784
11785   for (i = 0; i < arange_table_in_use; i++)
11786     {
11787       dw_die_ref die = arange_table[i];
11788
11789       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11790       gcc_assert (die->die_mark);
11791
11792       if (die->die_tag == DW_TAG_subprogram)
11793         {
11794           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11795                                "Address");
11796           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11797                                 get_AT_low_pc (die), "Length");
11798         }
11799       else
11800         {
11801           /* A static variable; extract the symbol from DW_AT_location.
11802              Note that this code isn't currently hit, as we only emit
11803              aranges for functions (jason 9/23/99).  */
11804           dw_attr_ref a = get_AT (die, DW_AT_location);
11805           dw_loc_descr_ref loc;
11806
11807           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11808
11809           loc = AT_loc (a);
11810           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11811
11812           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11813                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11814           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11815                                get_AT_unsigned (die, DW_AT_byte_size),
11816                                "Length");
11817         }
11818     }
11819
11820   /* Output the terminator words.  */
11821   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11822   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11823 }
11824
11825 /* Add a new entry to .debug_ranges.  Return the offset at which it
11826    was placed.  */
11827
11828 static unsigned int
11829 add_ranges_num (int num)
11830 {
11831   unsigned int in_use = ranges_table_in_use;
11832
11833   if (in_use == ranges_table_allocated)
11834     {
11835       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11836       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11837                                     ranges_table_allocated);
11838       memset (ranges_table + ranges_table_in_use, 0,
11839               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11840     }
11841
11842   ranges_table[in_use].num = num;
11843   ranges_table_in_use = in_use + 1;
11844
11845   return in_use * 2 * DWARF2_ADDR_SIZE;
11846 }
11847
11848 /* Add a new entry to .debug_ranges corresponding to a block, or a
11849    range terminator if BLOCK is NULL.  */
11850
11851 static unsigned int
11852 add_ranges (const_tree block)
11853 {
11854   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11855 }
11856
11857 /* Add a new entry to .debug_ranges corresponding to a pair of
11858    labels.  */
11859
11860 static void
11861 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11862                       bool *added)
11863 {
11864   unsigned int in_use = ranges_by_label_in_use;
11865   unsigned int offset;
11866
11867   if (in_use == ranges_by_label_allocated)
11868     {
11869       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11870       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11871                                        ranges_by_label,
11872                                        ranges_by_label_allocated);
11873       memset (ranges_by_label + ranges_by_label_in_use, 0,
11874               RANGES_TABLE_INCREMENT
11875               * sizeof (struct dw_ranges_by_label_struct));
11876     }
11877
11878   ranges_by_label[in_use].begin = begin;
11879   ranges_by_label[in_use].end = end;
11880   ranges_by_label_in_use = in_use + 1;
11881
11882   offset = add_ranges_num (-(int)in_use - 1);
11883   if (!*added)
11884     {
11885       add_AT_range_list (die, DW_AT_ranges, offset);
11886       *added = true;
11887     }
11888 }
11889
11890 static void
11891 output_ranges (void)
11892 {
11893   unsigned i;
11894   static const char *const start_fmt = "Offset %#x";
11895   const char *fmt = start_fmt;
11896
11897   for (i = 0; i < ranges_table_in_use; i++)
11898     {
11899       int block_num = ranges_table[i].num;
11900
11901       if (block_num > 0)
11902         {
11903           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11904           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11905
11906           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11907           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11908
11909           /* If all code is in the text section, then the compilation
11910              unit base address defaults to DW_AT_low_pc, which is the
11911              base of the text section.  */
11912           if (!have_multiple_function_sections)
11913             {
11914               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11915                                     text_section_label,
11916                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11917               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11918                                     text_section_label, NULL);
11919             }
11920
11921           /* Otherwise, the compilation unit base address is zero,
11922              which allows us to use absolute addresses, and not worry
11923              about whether the target supports cross-section
11924              arithmetic.  */
11925           else
11926             {
11927               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11928                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11929               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11930             }
11931
11932           fmt = NULL;
11933         }
11934
11935       /* Negative block_num stands for an index into ranges_by_label.  */
11936       else if (block_num < 0)
11937         {
11938           int lab_idx = - block_num - 1;
11939
11940           if (!have_multiple_function_sections)
11941             {
11942               gcc_unreachable ();
11943 #if 0
11944               /* If we ever use add_ranges_by_labels () for a single
11945                  function section, all we have to do is to take out
11946                  the #if 0 above.  */
11947               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11948                                     ranges_by_label[lab_idx].begin,
11949                                     text_section_label,
11950                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11951               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11952                                     ranges_by_label[lab_idx].end,
11953                                     text_section_label, NULL);
11954 #endif
11955             }
11956           else
11957             {
11958               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11959                                    ranges_by_label[lab_idx].begin,
11960                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11961               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11962                                    ranges_by_label[lab_idx].end,
11963                                    NULL);
11964             }
11965         }
11966       else
11967         {
11968           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11969           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11970           fmt = start_fmt;
11971         }
11972     }
11973 }
11974
11975 /* Data structure containing information about input files.  */
11976 struct file_info
11977 {
11978   const char *path;     /* Complete file name.  */
11979   const char *fname;    /* File name part.  */
11980   int length;           /* Length of entire string.  */
11981   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11982   int dir_idx;          /* Index in directory table.  */
11983 };
11984
11985 /* Data structure containing information about directories with source
11986    files.  */
11987 struct dir_info
11988 {
11989   const char *path;     /* Path including directory name.  */
11990   int length;           /* Path length.  */
11991   int prefix;           /* Index of directory entry which is a prefix.  */
11992   int count;            /* Number of files in this directory.  */
11993   int dir_idx;          /* Index of directory used as base.  */
11994 };
11995
11996 /* Callback function for file_info comparison.  We sort by looking at
11997    the directories in the path.  */
11998
11999 static int
12000 file_info_cmp (const void *p1, const void *p2)
12001 {
12002   const struct file_info *const s1 = (const struct file_info *) p1;
12003   const struct file_info *const s2 = (const struct file_info *) p2;
12004   const unsigned char *cp1;
12005   const unsigned char *cp2;
12006
12007   /* Take care of file names without directories.  We need to make sure that
12008      we return consistent values to qsort since some will get confused if
12009      we return the same value when identical operands are passed in opposite
12010      orders.  So if neither has a directory, return 0 and otherwise return
12011      1 or -1 depending on which one has the directory.  */
12012   if ((s1->path == s1->fname || s2->path == s2->fname))
12013     return (s2->path == s2->fname) - (s1->path == s1->fname);
12014
12015   cp1 = (const unsigned char *) s1->path;
12016   cp2 = (const unsigned char *) s2->path;
12017
12018   while (1)
12019     {
12020       ++cp1;
12021       ++cp2;
12022       /* Reached the end of the first path?  If so, handle like above.  */
12023       if ((cp1 == (const unsigned char *) s1->fname)
12024           || (cp2 == (const unsigned char *) s2->fname))
12025         return ((cp2 == (const unsigned char *) s2->fname)
12026                 - (cp1 == (const unsigned char *) s1->fname));
12027
12028       /* Character of current path component the same?  */
12029       else if (*cp1 != *cp2)
12030         return *cp1 - *cp2;
12031     }
12032 }
12033
12034 struct file_name_acquire_data
12035 {
12036   struct file_info *files;
12037   int used_files;
12038   int max_files;
12039 };
12040
12041 /* Traversal function for the hash table.  */
12042
12043 static int
12044 file_name_acquire (void ** slot, void *data)
12045 {
12046   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12047   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12048   struct file_info *fi;
12049   const char *f;
12050
12051   gcc_assert (fnad->max_files >= d->emitted_number);
12052
12053   if (! d->emitted_number)
12054     return 1;
12055
12056   gcc_assert (fnad->max_files != fnad->used_files);
12057
12058   fi = fnad->files + fnad->used_files++;
12059
12060   /* Skip all leading "./".  */
12061   f = d->filename;
12062   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12063     f += 2;
12064
12065   /* Create a new array entry.  */
12066   fi->path = f;
12067   fi->length = strlen (f);
12068   fi->file_idx = d;
12069
12070   /* Search for the file name part.  */
12071   f = strrchr (f, DIR_SEPARATOR);
12072 #if defined (DIR_SEPARATOR_2)
12073   {
12074     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12075
12076     if (g != NULL)
12077       {
12078         if (f == NULL || f < g)
12079           f = g;
12080       }
12081   }
12082 #endif
12083
12084   fi->fname = f == NULL ? fi->path : f + 1;
12085   return 1;
12086 }
12087
12088 /* Output the directory table and the file name table.  We try to minimize
12089    the total amount of memory needed.  A heuristic is used to avoid large
12090    slowdowns with many input files.  */
12091
12092 static void
12093 output_file_names (void)
12094 {
12095   struct file_name_acquire_data fnad;
12096   int numfiles;
12097   struct file_info *files;
12098   struct dir_info *dirs;
12099   int *saved;
12100   int *savehere;
12101   int *backmap;
12102   int ndirs;
12103   int idx_offset;
12104   int i;
12105
12106   if (!last_emitted_file)
12107     {
12108       dw2_asm_output_data (1, 0, "End directory table");
12109       dw2_asm_output_data (1, 0, "End file name table");
12110       return;
12111     }
12112
12113   numfiles = last_emitted_file->emitted_number;
12114
12115   /* Allocate the various arrays we need.  */
12116   files = XALLOCAVEC (struct file_info, numfiles);
12117   dirs = XALLOCAVEC (struct dir_info, numfiles);
12118
12119   fnad.files = files;
12120   fnad.used_files = 0;
12121   fnad.max_files = numfiles;
12122   htab_traverse (file_table, file_name_acquire, &fnad);
12123   gcc_assert (fnad.used_files == fnad.max_files);
12124
12125   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12126
12127   /* Find all the different directories used.  */
12128   dirs[0].path = files[0].path;
12129   dirs[0].length = files[0].fname - files[0].path;
12130   dirs[0].prefix = -1;
12131   dirs[0].count = 1;
12132   dirs[0].dir_idx = 0;
12133   files[0].dir_idx = 0;
12134   ndirs = 1;
12135
12136   for (i = 1; i < numfiles; i++)
12137     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12138         && memcmp (dirs[ndirs - 1].path, files[i].path,
12139                    dirs[ndirs - 1].length) == 0)
12140       {
12141         /* Same directory as last entry.  */
12142         files[i].dir_idx = ndirs - 1;
12143         ++dirs[ndirs - 1].count;
12144       }
12145     else
12146       {
12147         int j;
12148
12149         /* This is a new directory.  */
12150         dirs[ndirs].path = files[i].path;
12151         dirs[ndirs].length = files[i].fname - files[i].path;
12152         dirs[ndirs].count = 1;
12153         dirs[ndirs].dir_idx = ndirs;
12154         files[i].dir_idx = ndirs;
12155
12156         /* Search for a prefix.  */
12157         dirs[ndirs].prefix = -1;
12158         for (j = 0; j < ndirs; j++)
12159           if (dirs[j].length < dirs[ndirs].length
12160               && dirs[j].length > 1
12161               && (dirs[ndirs].prefix == -1
12162                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12163               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12164             dirs[ndirs].prefix = j;
12165
12166         ++ndirs;
12167       }
12168
12169   /* Now to the actual work.  We have to find a subset of the directories which
12170      allow expressing the file name using references to the directory table
12171      with the least amount of characters.  We do not do an exhaustive search
12172      where we would have to check out every combination of every single
12173      possible prefix.  Instead we use a heuristic which provides nearly optimal
12174      results in most cases and never is much off.  */
12175   saved = XALLOCAVEC (int, ndirs);
12176   savehere = XALLOCAVEC (int, ndirs);
12177
12178   memset (saved, '\0', ndirs * sizeof (saved[0]));
12179   for (i = 0; i < ndirs; i++)
12180     {
12181       int j;
12182       int total;
12183
12184       /* We can always save some space for the current directory.  But this
12185          does not mean it will be enough to justify adding the directory.  */
12186       savehere[i] = dirs[i].length;
12187       total = (savehere[i] - saved[i]) * dirs[i].count;
12188
12189       for (j = i + 1; j < ndirs; j++)
12190         {
12191           savehere[j] = 0;
12192           if (saved[j] < dirs[i].length)
12193             {
12194               /* Determine whether the dirs[i] path is a prefix of the
12195                  dirs[j] path.  */
12196               int k;
12197
12198               k = dirs[j].prefix;
12199               while (k != -1 && k != (int) i)
12200                 k = dirs[k].prefix;
12201
12202               if (k == (int) i)
12203                 {
12204                   /* Yes it is.  We can possibly save some memory by
12205                      writing the filenames in dirs[j] relative to
12206                      dirs[i].  */
12207                   savehere[j] = dirs[i].length;
12208                   total += (savehere[j] - saved[j]) * dirs[j].count;
12209                 }
12210             }
12211         }
12212
12213       /* Check whether we can save enough to justify adding the dirs[i]
12214          directory.  */
12215       if (total > dirs[i].length + 1)
12216         {
12217           /* It's worthwhile adding.  */
12218           for (j = i; j < ndirs; j++)
12219             if (savehere[j] > 0)
12220               {
12221                 /* Remember how much we saved for this directory so far.  */
12222                 saved[j] = savehere[j];
12223
12224                 /* Remember the prefix directory.  */
12225                 dirs[j].dir_idx = i;
12226               }
12227         }
12228     }
12229
12230   /* Emit the directory name table.  */
12231   idx_offset = dirs[0].length > 0 ? 1 : 0;
12232   for (i = 1 - idx_offset; i < ndirs; i++)
12233     dw2_asm_output_nstring (dirs[i].path,
12234                             dirs[i].length
12235                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12236                             "Directory Entry: %#x", i + idx_offset);
12237
12238   dw2_asm_output_data (1, 0, "End directory table");
12239
12240   /* We have to emit them in the order of emitted_number since that's
12241      used in the debug info generation.  To do this efficiently we
12242      generate a back-mapping of the indices first.  */
12243   backmap = XALLOCAVEC (int, numfiles);
12244   for (i = 0; i < numfiles; i++)
12245     backmap[files[i].file_idx->emitted_number - 1] = i;
12246
12247   /* Now write all the file names.  */
12248   for (i = 0; i < numfiles; i++)
12249     {
12250       int file_idx = backmap[i];
12251       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12252
12253 #ifdef VMS_DEBUGGING_INFO
12254 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12255
12256       /* Setting these fields can lead to debugger miscomparisons,
12257          but VMS Debug requires them to be set correctly.  */
12258
12259       int ver;
12260       long long cdt;
12261       long siz;
12262       int maxfilelen = strlen (files[file_idx].path)
12263                                + dirs[dir_idx].length
12264                                + MAX_VMS_VERSION_LEN + 1;
12265       char *filebuf = XALLOCAVEC (char, maxfilelen);
12266
12267       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12268       snprintf (filebuf, maxfilelen, "%s;%d",
12269                 files[file_idx].path + dirs[dir_idx].length, ver);
12270
12271       dw2_asm_output_nstring
12272         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12273
12274       /* Include directory index.  */
12275       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12276
12277       /* Modification time.  */
12278       dw2_asm_output_data_uleb128
12279         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12280           ? cdt : 0,
12281          NULL);
12282
12283       /* File length in bytes.  */
12284       dw2_asm_output_data_uleb128
12285         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12286           ? siz : 0,
12287          NULL);
12288 #else
12289       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12290                               "File Entry: %#x", (unsigned) i + 1);
12291
12292       /* Include directory index.  */
12293       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12294
12295       /* Modification time.  */
12296       dw2_asm_output_data_uleb128 (0, NULL);
12297
12298       /* File length in bytes.  */
12299       dw2_asm_output_data_uleb128 (0, NULL);
12300 #endif /* VMS_DEBUGGING_INFO */
12301     }
12302
12303   dw2_asm_output_data (1, 0, "End file name table");
12304 }
12305
12306
12307 /* Output the source line number correspondence information.  This
12308    information goes into the .debug_line section.  */
12309
12310 static void
12311 output_line_info (void)
12312 {
12313   char l1[20], l2[20], p1[20], p2[20];
12314   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12315   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12316   unsigned opc;
12317   unsigned n_op_args;
12318   unsigned long lt_index;
12319   unsigned long current_line;
12320   long line_offset;
12321   long line_delta;
12322   unsigned long current_file;
12323   unsigned long function;
12324   int ver = dwarf_version;
12325
12326   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12327   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12328   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12329   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12330
12331   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12332     dw2_asm_output_data (4, 0xffffffff,
12333       "Initial length escape value indicating 64-bit DWARF extension");
12334   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12335                         "Length of Source Line Info");
12336   ASM_OUTPUT_LABEL (asm_out_file, l1);
12337
12338   dw2_asm_output_data (2, ver, "DWARF Version");
12339   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12340   ASM_OUTPUT_LABEL (asm_out_file, p1);
12341
12342   /* Define the architecture-dependent minimum instruction length (in
12343    bytes).  In this implementation of DWARF, this field is used for
12344    information purposes only.  Since GCC generates assembly language,
12345    we have no a priori knowledge of how many instruction bytes are
12346    generated for each source line, and therefore can use only the
12347    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12348    commands.  Accordingly, we fix this as `1', which is "correct
12349    enough" for all architectures, and don't let the target override.  */
12350   dw2_asm_output_data (1, 1,
12351                        "Minimum Instruction Length");
12352
12353   if (ver >= 4)
12354     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12355                          "Maximum Operations Per Instruction");
12356   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12357                        "Default is_stmt_start flag");
12358   dw2_asm_output_data (1, DWARF_LINE_BASE,
12359                        "Line Base Value (Special Opcodes)");
12360   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12361                        "Line Range Value (Special Opcodes)");
12362   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12363                        "Special Opcode Base");
12364
12365   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12366     {
12367       switch (opc)
12368         {
12369         case DW_LNS_advance_pc:
12370         case DW_LNS_advance_line:
12371         case DW_LNS_set_file:
12372         case DW_LNS_set_column:
12373         case DW_LNS_fixed_advance_pc:
12374           n_op_args = 1;
12375           break;
12376         default:
12377           n_op_args = 0;
12378           break;
12379         }
12380
12381       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12382                            opc, n_op_args);
12383     }
12384
12385   /* Write out the information about the files we use.  */
12386   output_file_names ();
12387   ASM_OUTPUT_LABEL (asm_out_file, p2);
12388
12389   /* We used to set the address register to the first location in the text
12390      section here, but that didn't accomplish anything since we already
12391      have a line note for the opening brace of the first function.  */
12392
12393   /* Generate the line number to PC correspondence table, encoded as
12394      a series of state machine operations.  */
12395   current_file = 1;
12396   current_line = 1;
12397
12398   if (cfun && in_cold_section_p)
12399     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12400   else
12401     strcpy (prev_line_label, text_section_label);
12402   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12403     {
12404       dw_line_info_ref line_info = &line_info_table[lt_index];
12405
12406 #if 0
12407       /* Disable this optimization for now; GDB wants to see two line notes
12408          at the beginning of a function so it can find the end of the
12409          prologue.  */
12410
12411       /* Don't emit anything for redundant notes.  Just updating the
12412          address doesn't accomplish anything, because we already assume
12413          that anything after the last address is this line.  */
12414       if (line_info->dw_line_num == current_line
12415           && line_info->dw_file_num == current_file)
12416         continue;
12417 #endif
12418
12419       /* Emit debug info for the address of the current line.
12420
12421          Unfortunately, we have little choice here currently, and must always
12422          use the most general form.  GCC does not know the address delta
12423          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12424          attributes which will give an upper bound on the address range.  We
12425          could perhaps use length attributes to determine when it is safe to
12426          use DW_LNS_fixed_advance_pc.  */
12427
12428       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12429       if (0)
12430         {
12431           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12432           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12433                                "DW_LNS_fixed_advance_pc");
12434           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12435         }
12436       else
12437         {
12438           /* This can handle any delta.  This takes
12439              4+DWARF2_ADDR_SIZE bytes.  */
12440           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12441           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12442           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12443           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12444         }
12445
12446       strcpy (prev_line_label, line_label);
12447
12448       /* Emit debug info for the source file of the current line, if
12449          different from the previous line.  */
12450       if (line_info->dw_file_num != current_file)
12451         {
12452           current_file = line_info->dw_file_num;
12453           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12454           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12455         }
12456
12457       /* Emit debug info for the current line number, choosing the encoding
12458          that uses the least amount of space.  */
12459       if (line_info->dw_line_num != current_line)
12460         {
12461           line_offset = line_info->dw_line_num - current_line;
12462           line_delta = line_offset - DWARF_LINE_BASE;
12463           current_line = line_info->dw_line_num;
12464           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12465             /* This can handle deltas from -10 to 234, using the current
12466                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12467                takes 1 byte.  */
12468             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12469                                  "line %lu", current_line);
12470           else
12471             {
12472               /* This can handle any delta.  This takes at least 4 bytes,
12473                  depending on the value being encoded.  */
12474               dw2_asm_output_data (1, DW_LNS_advance_line,
12475                                    "advance to line %lu", current_line);
12476               dw2_asm_output_data_sleb128 (line_offset, NULL);
12477               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12478             }
12479         }
12480       else
12481         /* We still need to start a new row, so output a copy insn.  */
12482         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12483     }
12484
12485   /* Emit debug info for the address of the end of the function.  */
12486   if (0)
12487     {
12488       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12489                            "DW_LNS_fixed_advance_pc");
12490       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12491     }
12492   else
12493     {
12494       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12495       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12496       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12497       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12498     }
12499
12500   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12501   dw2_asm_output_data_uleb128 (1, NULL);
12502   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12503
12504   function = 0;
12505   current_file = 1;
12506   current_line = 1;
12507   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12508     {
12509       dw_separate_line_info_ref line_info
12510         = &separate_line_info_table[lt_index];
12511
12512 #if 0
12513       /* Don't emit anything for redundant notes.  */
12514       if (line_info->dw_line_num == current_line
12515           && line_info->dw_file_num == current_file
12516           && line_info->function == function)
12517         goto cont;
12518 #endif
12519
12520       /* Emit debug info for the address of the current line.  If this is
12521          a new function, or the first line of a function, then we need
12522          to handle it differently.  */
12523       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12524                                    lt_index);
12525       if (function != line_info->function)
12526         {
12527           function = line_info->function;
12528
12529           /* Set the address register to the first line in the function.  */
12530           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12531           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12532           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12533           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12534         }
12535       else
12536         {
12537           /* ??? See the DW_LNS_advance_pc comment above.  */
12538           if (0)
12539             {
12540               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12541                                    "DW_LNS_fixed_advance_pc");
12542               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12543             }
12544           else
12545             {
12546               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12547               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12548               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12549               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12550             }
12551         }
12552
12553       strcpy (prev_line_label, line_label);
12554
12555       /* Emit debug info for the source file of the current line, if
12556          different from the previous line.  */
12557       if (line_info->dw_file_num != current_file)
12558         {
12559           current_file = line_info->dw_file_num;
12560           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12561           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12562         }
12563
12564       /* Emit debug info for the current line number, choosing the encoding
12565          that uses the least amount of space.  */
12566       if (line_info->dw_line_num != current_line)
12567         {
12568           line_offset = line_info->dw_line_num - current_line;
12569           line_delta = line_offset - DWARF_LINE_BASE;
12570           current_line = line_info->dw_line_num;
12571           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12572             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12573                                  "line %lu", current_line);
12574           else
12575             {
12576               dw2_asm_output_data (1, DW_LNS_advance_line,
12577                                    "advance to line %lu", current_line);
12578               dw2_asm_output_data_sleb128 (line_offset, NULL);
12579               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12580             }
12581         }
12582       else
12583         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12584
12585 #if 0
12586     cont:
12587 #endif
12588
12589       lt_index++;
12590
12591       /* If we're done with a function, end its sequence.  */
12592       if (lt_index == separate_line_info_table_in_use
12593           || separate_line_info_table[lt_index].function != function)
12594         {
12595           current_file = 1;
12596           current_line = 1;
12597
12598           /* Emit debug info for the address of the end of the function.  */
12599           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12600           if (0)
12601             {
12602               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12603                                    "DW_LNS_fixed_advance_pc");
12604               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12605             }
12606           else
12607             {
12608               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12609               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12610               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12611               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12612             }
12613
12614           /* Output the marker for the end of this sequence.  */
12615           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12616           dw2_asm_output_data_uleb128 (1, NULL);
12617           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12618         }
12619     }
12620
12621   /* Output the marker for the end of the line number info.  */
12622   ASM_OUTPUT_LABEL (asm_out_file, l2);
12623 }
12624 \f
12625 /* Given a pointer to a tree node for some base type, return a pointer to
12626    a DIE that describes the given type.
12627
12628    This routine must only be called for GCC type nodes that correspond to
12629    Dwarf base (fundamental) types.  */
12630
12631 static dw_die_ref
12632 base_type_die (tree type)
12633 {
12634   dw_die_ref base_type_result;
12635   enum dwarf_type encoding;
12636
12637   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12638     return 0;
12639
12640   /* If this is a subtype that should not be emitted as a subrange type,
12641      use the base type.  See subrange_type_for_debug_p.  */
12642   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12643     type = TREE_TYPE (type);
12644
12645   switch (TREE_CODE (type))
12646     {
12647     case INTEGER_TYPE:
12648       if ((dwarf_version >= 4 || !dwarf_strict)
12649           && TYPE_NAME (type)
12650           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12651           && DECL_IS_BUILTIN (TYPE_NAME (type))
12652           && DECL_NAME (TYPE_NAME (type)))
12653         {
12654           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12655           if (strcmp (name, "char16_t") == 0
12656               || strcmp (name, "char32_t") == 0)
12657             {
12658               encoding = DW_ATE_UTF;
12659               break;
12660             }
12661         }
12662       if (TYPE_STRING_FLAG (type))
12663         {
12664           if (TYPE_UNSIGNED (type))
12665             encoding = DW_ATE_unsigned_char;
12666           else
12667             encoding = DW_ATE_signed_char;
12668         }
12669       else if (TYPE_UNSIGNED (type))
12670         encoding = DW_ATE_unsigned;
12671       else
12672         encoding = DW_ATE_signed;
12673       break;
12674
12675     case REAL_TYPE:
12676       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12677         {
12678           if (dwarf_version >= 3 || !dwarf_strict)
12679             encoding = DW_ATE_decimal_float;
12680           else
12681             encoding = DW_ATE_lo_user;
12682         }
12683       else
12684         encoding = DW_ATE_float;
12685       break;
12686
12687     case FIXED_POINT_TYPE:
12688       if (!(dwarf_version >= 3 || !dwarf_strict))
12689         encoding = DW_ATE_lo_user;
12690       else if (TYPE_UNSIGNED (type))
12691         encoding = DW_ATE_unsigned_fixed;
12692       else
12693         encoding = DW_ATE_signed_fixed;
12694       break;
12695
12696       /* Dwarf2 doesn't know anything about complex ints, so use
12697          a user defined type for it.  */
12698     case COMPLEX_TYPE:
12699       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12700         encoding = DW_ATE_complex_float;
12701       else
12702         encoding = DW_ATE_lo_user;
12703       break;
12704
12705     case BOOLEAN_TYPE:
12706       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12707       encoding = DW_ATE_boolean;
12708       break;
12709
12710     default:
12711       /* No other TREE_CODEs are Dwarf fundamental types.  */
12712       gcc_unreachable ();
12713     }
12714
12715   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12716
12717   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12718                    int_size_in_bytes (type));
12719   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12720
12721   return base_type_result;
12722 }
12723
12724 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12725    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12726
12727 static inline int
12728 is_base_type (tree type)
12729 {
12730   switch (TREE_CODE (type))
12731     {
12732     case ERROR_MARK:
12733     case VOID_TYPE:
12734     case INTEGER_TYPE:
12735     case REAL_TYPE:
12736     case FIXED_POINT_TYPE:
12737     case COMPLEX_TYPE:
12738     case BOOLEAN_TYPE:
12739       return 1;
12740
12741     case ARRAY_TYPE:
12742     case RECORD_TYPE:
12743     case UNION_TYPE:
12744     case QUAL_UNION_TYPE:
12745     case ENUMERAL_TYPE:
12746     case FUNCTION_TYPE:
12747     case METHOD_TYPE:
12748     case POINTER_TYPE:
12749     case REFERENCE_TYPE:
12750     case NULLPTR_TYPE:
12751     case OFFSET_TYPE:
12752     case LANG_TYPE:
12753     case VECTOR_TYPE:
12754       return 0;
12755
12756     default:
12757       gcc_unreachable ();
12758     }
12759
12760   return 0;
12761 }
12762
12763 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12764    node, return the size in bits for the type if it is a constant, or else
12765    return the alignment for the type if the type's size is not constant, or
12766    else return BITS_PER_WORD if the type actually turns out to be an
12767    ERROR_MARK node.  */
12768
12769 static inline unsigned HOST_WIDE_INT
12770 simple_type_size_in_bits (const_tree type)
12771 {
12772   if (TREE_CODE (type) == ERROR_MARK)
12773     return BITS_PER_WORD;
12774   else if (TYPE_SIZE (type) == NULL_TREE)
12775     return 0;
12776   else if (host_integerp (TYPE_SIZE (type), 1))
12777     return tree_low_cst (TYPE_SIZE (type), 1);
12778   else
12779     return TYPE_ALIGN (type);
12780 }
12781
12782 /* Similarly, but return a double_int instead of UHWI.  */
12783
12784 static inline double_int
12785 double_int_type_size_in_bits (const_tree type)
12786 {
12787   if (TREE_CODE (type) == ERROR_MARK)
12788     return uhwi_to_double_int (BITS_PER_WORD);
12789   else if (TYPE_SIZE (type) == NULL_TREE)
12790     return double_int_zero;
12791   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12792     return tree_to_double_int (TYPE_SIZE (type));
12793   else
12794     return uhwi_to_double_int (TYPE_ALIGN (type));
12795 }
12796
12797 /*  Given a pointer to a tree node for a subrange type, return a pointer
12798     to a DIE that describes the given type.  */
12799
12800 static dw_die_ref
12801 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12802 {
12803   dw_die_ref subrange_die;
12804   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12805
12806   if (context_die == NULL)
12807     context_die = comp_unit_die ();
12808
12809   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12810
12811   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12812     {
12813       /* The size of the subrange type and its base type do not match,
12814          so we need to generate a size attribute for the subrange type.  */
12815       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12816     }
12817
12818   if (low)
12819     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12820   if (high)
12821     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12822
12823   return subrange_die;
12824 }
12825
12826 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12827    entry that chains various modifiers in front of the given type.  */
12828
12829 static dw_die_ref
12830 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12831                    dw_die_ref context_die)
12832 {
12833   enum tree_code code = TREE_CODE (type);
12834   dw_die_ref mod_type_die;
12835   dw_die_ref sub_die = NULL;
12836   tree item_type = NULL;
12837   tree qualified_type;
12838   tree name, low, high;
12839
12840   if (code == ERROR_MARK)
12841     return NULL;
12842
12843   /* See if we already have the appropriately qualified variant of
12844      this type.  */
12845   qualified_type
12846     = get_qualified_type (type,
12847                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12848                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12849
12850   if (qualified_type == sizetype
12851       && TYPE_NAME (qualified_type)
12852       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12853     {
12854       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12855
12856       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12857                            && TYPE_PRECISION (t)
12858                            == TYPE_PRECISION (qualified_type)
12859                            && TYPE_UNSIGNED (t)
12860                            == TYPE_UNSIGNED (qualified_type));
12861       qualified_type = t;
12862     }
12863
12864   /* If we do, then we can just use its DIE, if it exists.  */
12865   if (qualified_type)
12866     {
12867       mod_type_die = lookup_type_die (qualified_type);
12868       if (mod_type_die)
12869         return mod_type_die;
12870     }
12871
12872   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12873
12874   /* Handle C typedef types.  */
12875   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12876       && !DECL_ARTIFICIAL (name))
12877     {
12878       tree dtype = TREE_TYPE (name);
12879
12880       if (qualified_type == dtype)
12881         {
12882           /* For a named type, use the typedef.  */
12883           gen_type_die (qualified_type, context_die);
12884           return lookup_type_die (qualified_type);
12885         }
12886       else if (is_const_type < TYPE_READONLY (dtype)
12887                || is_volatile_type < TYPE_VOLATILE (dtype)
12888                || (is_const_type <= TYPE_READONLY (dtype)
12889                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12890                    && DECL_ORIGINAL_TYPE (name) != type))
12891         /* cv-unqualified version of named type.  Just use the unnamed
12892            type to which it refers.  */
12893         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12894                                   is_const_type, is_volatile_type,
12895                                   context_die);
12896       /* Else cv-qualified version of named type; fall through.  */
12897     }
12898
12899   if (is_const_type
12900       /* If both is_const_type and is_volatile_type, prefer the path
12901          which leads to a qualified type.  */
12902       && (!is_volatile_type
12903           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12904           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12905     {
12906       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12907       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12908     }
12909   else if (is_volatile_type)
12910     {
12911       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12912       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12913     }
12914   else if (code == POINTER_TYPE)
12915     {
12916       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12917       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12918                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12919       item_type = TREE_TYPE (type);
12920       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12921         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12922                          TYPE_ADDR_SPACE (item_type));
12923     }
12924   else if (code == REFERENCE_TYPE)
12925     {
12926       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12927         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12928                                 type);
12929       else
12930         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12931       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12932                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12933       item_type = TREE_TYPE (type);
12934       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12935         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12936                          TYPE_ADDR_SPACE (item_type));
12937     }
12938   else if (code == INTEGER_TYPE
12939            && TREE_TYPE (type) != NULL_TREE
12940            && subrange_type_for_debug_p (type, &low, &high))
12941     {
12942       mod_type_die = subrange_type_die (type, low, high, context_die);
12943       item_type = TREE_TYPE (type);
12944     }
12945   else if (is_base_type (type))
12946     mod_type_die = base_type_die (type);
12947   else
12948     {
12949       gen_type_die (type, context_die);
12950
12951       /* We have to get the type_main_variant here (and pass that to the
12952          `lookup_type_die' routine) because the ..._TYPE node we have
12953          might simply be a *copy* of some original type node (where the
12954          copy was created to help us keep track of typedef names) and
12955          that copy might have a different TYPE_UID from the original
12956          ..._TYPE node.  */
12957       if (TREE_CODE (type) != VECTOR_TYPE)
12958         return lookup_type_die (type_main_variant (type));
12959       else
12960         /* Vectors have the debugging information in the type,
12961            not the main variant.  */
12962         return lookup_type_die (type);
12963     }
12964
12965   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12966      don't output a DW_TAG_typedef, since there isn't one in the
12967      user's program; just attach a DW_AT_name to the type.
12968      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12969      if the base type already has the same name.  */
12970   if (name
12971       && ((TREE_CODE (name) != TYPE_DECL
12972            && (qualified_type == TYPE_MAIN_VARIANT (type)
12973                || (!is_const_type && !is_volatile_type)))
12974           || (TREE_CODE (name) == TYPE_DECL
12975               && TREE_TYPE (name) == qualified_type
12976               && DECL_NAME (name))))
12977     {
12978       if (TREE_CODE (name) == TYPE_DECL)
12979         /* Could just call add_name_and_src_coords_attributes here,
12980            but since this is a builtin type it doesn't have any
12981            useful source coordinates anyway.  */
12982         name = DECL_NAME (name);
12983       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12984     }
12985   /* This probably indicates a bug.  */
12986   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12987     add_name_attribute (mod_type_die, "__unknown__");
12988
12989   if (qualified_type)
12990     equate_type_number_to_die (qualified_type, mod_type_die);
12991
12992   if (item_type)
12993     /* We must do this after the equate_type_number_to_die call, in case
12994        this is a recursive type.  This ensures that the modified_type_die
12995        recursion will terminate even if the type is recursive.  Recursive
12996        types are possible in Ada.  */
12997     sub_die = modified_type_die (item_type,
12998                                  TYPE_READONLY (item_type),
12999                                  TYPE_VOLATILE (item_type),
13000                                  context_die);
13001
13002   if (sub_die != NULL)
13003     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13004
13005   return mod_type_die;
13006 }
13007
13008 /* Generate DIEs for the generic parameters of T.
13009    T must be either a generic type or a generic function.
13010    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13011
13012 static void
13013 gen_generic_params_dies (tree t)
13014 {
13015   tree parms, args;
13016   int parms_num, i;
13017   dw_die_ref die = NULL;
13018
13019   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13020     return;
13021
13022   if (TYPE_P (t))
13023     die = lookup_type_die (t);
13024   else if (DECL_P (t))
13025     die = lookup_decl_die (t);
13026
13027   gcc_assert (die);
13028
13029   parms = lang_hooks.get_innermost_generic_parms (t);
13030   if (!parms)
13031     /* T has no generic parameter. It means T is neither a generic type
13032        or function. End of story.  */
13033     return;
13034
13035   parms_num = TREE_VEC_LENGTH (parms);
13036   args = lang_hooks.get_innermost_generic_args (t);
13037   for (i = 0; i < parms_num; i++)
13038     {
13039       tree parm, arg, arg_pack_elems;
13040
13041       parm = TREE_VEC_ELT (parms, i);
13042       arg = TREE_VEC_ELT (args, i);
13043       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13044       gcc_assert (parm && TREE_VALUE (parm) && arg);
13045
13046       if (parm && TREE_VALUE (parm) && arg)
13047         {
13048           /* If PARM represents a template parameter pack,
13049              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13050              by DW_TAG_template_*_parameter DIEs for the argument
13051              pack elements of ARG. Note that ARG would then be
13052              an argument pack.  */
13053           if (arg_pack_elems)
13054             template_parameter_pack_die (TREE_VALUE (parm),
13055                                          arg_pack_elems,
13056                                          die);
13057           else
13058             generic_parameter_die (TREE_VALUE (parm), arg,
13059                                    true /* Emit DW_AT_name */, die);
13060         }
13061     }
13062 }
13063
13064 /* Create and return a DIE for PARM which should be
13065    the representation of a generic type parameter.
13066    For instance, in the C++ front end, PARM would be a template parameter.
13067    ARG is the argument to PARM.
13068    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13069    name of the PARM.
13070    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13071    as a child node.  */
13072
13073 static dw_die_ref
13074 generic_parameter_die (tree parm, tree arg,
13075                        bool emit_name_p,
13076                        dw_die_ref parent_die)
13077 {
13078   dw_die_ref tmpl_die = NULL;
13079   const char *name = NULL;
13080
13081   if (!parm || !DECL_NAME (parm) || !arg)
13082     return NULL;
13083
13084   /* We support non-type generic parameters and arguments,
13085      type generic parameters and arguments, as well as
13086      generic generic parameters (a.k.a. template template parameters in C++)
13087      and arguments.  */
13088   if (TREE_CODE (parm) == PARM_DECL)
13089     /* PARM is a nontype generic parameter  */
13090     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13091   else if (TREE_CODE (parm) == TYPE_DECL)
13092     /* PARM is a type generic parameter.  */
13093     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13094   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13095     /* PARM is a generic generic parameter.
13096        Its DIE is a GNU extension. It shall have a
13097        DW_AT_name attribute to represent the name of the template template
13098        parameter, and a DW_AT_GNU_template_name attribute to represent the
13099        name of the template template argument.  */
13100     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13101                         parent_die, parm);
13102   else
13103     gcc_unreachable ();
13104
13105   if (tmpl_die)
13106     {
13107       tree tmpl_type;
13108
13109       /* If PARM is a generic parameter pack, it means we are
13110          emitting debug info for a template argument pack element.
13111          In other terms, ARG is a template argument pack element.
13112          In that case, we don't emit any DW_AT_name attribute for
13113          the die.  */
13114       if (emit_name_p)
13115         {
13116           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13117           gcc_assert (name);
13118           add_AT_string (tmpl_die, DW_AT_name, name);
13119         }
13120
13121       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13122         {
13123           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13124              TMPL_DIE should have a child DW_AT_type attribute that is set
13125              to the type of the argument to PARM, which is ARG.
13126              If PARM is a type generic parameter, TMPL_DIE should have a
13127              child DW_AT_type that is set to ARG.  */
13128           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13129           add_type_attribute (tmpl_die, tmpl_type, 0,
13130                               TREE_THIS_VOLATILE (tmpl_type),
13131                               parent_die);
13132         }
13133       else
13134         {
13135           /* So TMPL_DIE is a DIE representing a
13136              a generic generic template parameter, a.k.a template template
13137              parameter in C++ and arg is a template.  */
13138
13139           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13140              to the name of the argument.  */
13141           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13142           if (name)
13143             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13144         }
13145
13146       if (TREE_CODE (parm) == PARM_DECL)
13147         /* So PARM is a non-type generic parameter.
13148            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13149            attribute of TMPL_DIE which value represents the value
13150            of ARG.
13151            We must be careful here:
13152            The value of ARG might reference some function decls.
13153            We might currently be emitting debug info for a generic
13154            type and types are emitted before function decls, we don't
13155            know if the function decls referenced by ARG will actually be
13156            emitted after cgraph computations.
13157            So must defer the generation of the DW_AT_const_value to
13158            after cgraph is ready.  */
13159         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13160     }
13161
13162   return tmpl_die;
13163 }
13164
13165 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13166    PARM_PACK must be a template parameter pack. The returned DIE
13167    will be child DIE of PARENT_DIE.  */
13168
13169 static dw_die_ref
13170 template_parameter_pack_die (tree parm_pack,
13171                              tree parm_pack_args,
13172                              dw_die_ref parent_die)
13173 {
13174   dw_die_ref die;
13175   int j;
13176
13177   gcc_assert (parent_die && parm_pack);
13178
13179   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13180   add_name_and_src_coords_attributes (die, parm_pack);
13181   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13182     generic_parameter_die (parm_pack,
13183                            TREE_VEC_ELT (parm_pack_args, j),
13184                            false /* Don't emit DW_AT_name */,
13185                            die);
13186   return die;
13187 }
13188
13189 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13190    an enumerated type.  */
13191
13192 static inline int
13193 type_is_enum (const_tree type)
13194 {
13195   return TREE_CODE (type) == ENUMERAL_TYPE;
13196 }
13197
13198 /* Return the DBX register number described by a given RTL node.  */
13199
13200 static unsigned int
13201 dbx_reg_number (const_rtx rtl)
13202 {
13203   unsigned regno = REGNO (rtl);
13204
13205   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13206
13207 #ifdef LEAF_REG_REMAP
13208   if (current_function_uses_only_leaf_regs)
13209     {
13210       int leaf_reg = LEAF_REG_REMAP (regno);
13211       if (leaf_reg != -1)
13212         regno = (unsigned) leaf_reg;
13213     }
13214 #endif
13215
13216   return DBX_REGISTER_NUMBER (regno);
13217 }
13218
13219 /* Optionally add a DW_OP_piece term to a location description expression.
13220    DW_OP_piece is only added if the location description expression already
13221    doesn't end with DW_OP_piece.  */
13222
13223 static void
13224 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13225 {
13226   dw_loc_descr_ref loc;
13227
13228   if (*list_head != NULL)
13229     {
13230       /* Find the end of the chain.  */
13231       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13232         ;
13233
13234       if (loc->dw_loc_opc != DW_OP_piece)
13235         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13236     }
13237 }
13238
13239 /* Return a location descriptor that designates a machine register or
13240    zero if there is none.  */
13241
13242 static dw_loc_descr_ref
13243 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13244 {
13245   rtx regs;
13246
13247   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13248     return 0;
13249
13250   /* We only use "frame base" when we're sure we're talking about the
13251      post-prologue local stack frame.  We do this by *not* running
13252      register elimination until this point, and recognizing the special
13253      argument pointer and soft frame pointer rtx's.
13254      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13255   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13256       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13257     {
13258       dw_loc_descr_ref result = NULL;
13259
13260       if (dwarf_version >= 4 || !dwarf_strict)
13261         {
13262           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13263           if (result)
13264             add_loc_descr (&result,
13265                            new_loc_descr (DW_OP_stack_value, 0, 0));
13266         }
13267       return result;
13268     }
13269
13270   regs = targetm.dwarf_register_span (rtl);
13271
13272   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13273     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13274   else
13275     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13276 }
13277
13278 /* Return a location descriptor that designates a machine register for
13279    a given hard register number.  */
13280
13281 static dw_loc_descr_ref
13282 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13283 {
13284   dw_loc_descr_ref reg_loc_descr;
13285
13286   if (regno <= 31)
13287     reg_loc_descr
13288       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13289   else
13290     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13291
13292   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13293     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13294
13295   return reg_loc_descr;
13296 }
13297
13298 /* Given an RTL of a register, return a location descriptor that
13299    designates a value that spans more than one register.  */
13300
13301 static dw_loc_descr_ref
13302 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13303                              enum var_init_status initialized)
13304 {
13305   int nregs, size, i;
13306   unsigned reg;
13307   dw_loc_descr_ref loc_result = NULL;
13308
13309   reg = REGNO (rtl);
13310 #ifdef LEAF_REG_REMAP
13311   if (current_function_uses_only_leaf_regs)
13312     {
13313       int leaf_reg = LEAF_REG_REMAP (reg);
13314       if (leaf_reg != -1)
13315         reg = (unsigned) leaf_reg;
13316     }
13317 #endif
13318   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13319   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13320
13321   /* Simple, contiguous registers.  */
13322   if (regs == NULL_RTX)
13323     {
13324       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13325
13326       loc_result = NULL;
13327       while (nregs--)
13328         {
13329           dw_loc_descr_ref t;
13330
13331           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13332                                       VAR_INIT_STATUS_INITIALIZED);
13333           add_loc_descr (&loc_result, t);
13334           add_loc_descr_op_piece (&loc_result, size);
13335           ++reg;
13336         }
13337       return loc_result;
13338     }
13339
13340   /* Now onto stupid register sets in non contiguous locations.  */
13341
13342   gcc_assert (GET_CODE (regs) == PARALLEL);
13343
13344   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13345   loc_result = NULL;
13346
13347   for (i = 0; i < XVECLEN (regs, 0); ++i)
13348     {
13349       dw_loc_descr_ref t;
13350
13351       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13352                                   VAR_INIT_STATUS_INITIALIZED);
13353       add_loc_descr (&loc_result, t);
13354       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13355       add_loc_descr_op_piece (&loc_result, size);
13356     }
13357
13358   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13359     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13360   return loc_result;
13361 }
13362
13363 /* Return a location descriptor that designates a constant.  */
13364
13365 static dw_loc_descr_ref
13366 int_loc_descriptor (HOST_WIDE_INT i)
13367 {
13368   enum dwarf_location_atom op;
13369
13370   /* Pick the smallest representation of a constant, rather than just
13371      defaulting to the LEB encoding.  */
13372   if (i >= 0)
13373     {
13374       if (i <= 31)
13375         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13376       else if (i <= 0xff)
13377         op = DW_OP_const1u;
13378       else if (i <= 0xffff)
13379         op = DW_OP_const2u;
13380       else if (HOST_BITS_PER_WIDE_INT == 32
13381                || i <= 0xffffffff)
13382         op = DW_OP_const4u;
13383       else
13384         op = DW_OP_constu;
13385     }
13386   else
13387     {
13388       if (i >= -0x80)
13389         op = DW_OP_const1s;
13390       else if (i >= -0x8000)
13391         op = DW_OP_const2s;
13392       else if (HOST_BITS_PER_WIDE_INT == 32
13393                || i >= -0x80000000)
13394         op = DW_OP_const4s;
13395       else
13396         op = DW_OP_consts;
13397     }
13398
13399   return new_loc_descr (op, i, 0);
13400 }
13401
13402 /* Return loc description representing "address" of integer value.
13403    This can appear only as toplevel expression.  */
13404
13405 static dw_loc_descr_ref
13406 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13407 {
13408   int litsize;
13409   dw_loc_descr_ref loc_result = NULL;
13410
13411   if (!(dwarf_version >= 4 || !dwarf_strict))
13412     return NULL;
13413
13414   if (i >= 0)
13415     {
13416       if (i <= 31)
13417         litsize = 1;
13418       else if (i <= 0xff)
13419         litsize = 2;
13420       else if (i <= 0xffff)
13421         litsize = 3;
13422       else if (HOST_BITS_PER_WIDE_INT == 32
13423                || i <= 0xffffffff)
13424         litsize = 5;
13425       else
13426         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13427     }
13428   else
13429     {
13430       if (i >= -0x80)
13431         litsize = 2;
13432       else if (i >= -0x8000)
13433         litsize = 3;
13434       else if (HOST_BITS_PER_WIDE_INT == 32
13435                || i >= -0x80000000)
13436         litsize = 5;
13437       else
13438         litsize = 1 + size_of_sleb128 (i);
13439     }
13440   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13441      is more compact.  For DW_OP_stack_value we need:
13442      litsize + 1 (DW_OP_stack_value)
13443      and for DW_OP_implicit_value:
13444      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13445   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13446     {
13447       loc_result = int_loc_descriptor (i);
13448       add_loc_descr (&loc_result,
13449                      new_loc_descr (DW_OP_stack_value, 0, 0));
13450       return loc_result;
13451     }
13452
13453   loc_result = new_loc_descr (DW_OP_implicit_value,
13454                               size, 0);
13455   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13456   loc_result->dw_loc_oprnd2.v.val_int = i;
13457   return loc_result;
13458 }
13459
13460 /* Return a location descriptor that designates a base+offset location.  */
13461
13462 static dw_loc_descr_ref
13463 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13464                  enum var_init_status initialized)
13465 {
13466   unsigned int regno;
13467   dw_loc_descr_ref result;
13468   dw_fde_ref fde = current_fde ();
13469
13470   /* We only use "frame base" when we're sure we're talking about the
13471      post-prologue local stack frame.  We do this by *not* running
13472      register elimination until this point, and recognizing the special
13473      argument pointer and soft frame pointer rtx's.  */
13474   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13475     {
13476       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13477
13478       if (elim != reg)
13479         {
13480           if (GET_CODE (elim) == PLUS)
13481             {
13482               offset += INTVAL (XEXP (elim, 1));
13483               elim = XEXP (elim, 0);
13484             }
13485           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13486                        && (elim == hard_frame_pointer_rtx
13487                            || elim == stack_pointer_rtx))
13488                       || elim == (frame_pointer_needed
13489                                   ? hard_frame_pointer_rtx
13490                                   : stack_pointer_rtx));
13491
13492           /* If drap register is used to align stack, use frame
13493              pointer + offset to access stack variables.  If stack
13494              is aligned without drap, use stack pointer + offset to
13495              access stack variables.  */
13496           if (crtl->stack_realign_tried
13497               && reg == frame_pointer_rtx)
13498             {
13499               int base_reg
13500                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13501                                       ? HARD_FRAME_POINTER_REGNUM
13502                                       : STACK_POINTER_REGNUM);
13503               return new_reg_loc_descr (base_reg, offset);
13504             }
13505
13506           offset += frame_pointer_fb_offset;
13507           return new_loc_descr (DW_OP_fbreg, offset, 0);
13508         }
13509     }
13510   else if (!optimize
13511            && fde
13512            && (fde->drap_reg == REGNO (reg)
13513                || fde->vdrap_reg == REGNO (reg)))
13514     {
13515       /* Use cfa+offset to represent the location of arguments passed
13516          on the stack when drap is used to align stack.
13517          Only do this when not optimizing, for optimized code var-tracking
13518          is supposed to track where the arguments live and the register
13519          used as vdrap or drap in some spot might be used for something
13520          else in other part of the routine.  */
13521       return new_loc_descr (DW_OP_fbreg, offset, 0);
13522     }
13523
13524   regno = dbx_reg_number (reg);
13525   if (regno <= 31)
13526     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13527                             offset, 0);
13528   else
13529     result = new_loc_descr (DW_OP_bregx, regno, offset);
13530
13531   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13532     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13533
13534   return result;
13535 }
13536
13537 /* Return true if this RTL expression describes a base+offset calculation.  */
13538
13539 static inline int
13540 is_based_loc (const_rtx rtl)
13541 {
13542   return (GET_CODE (rtl) == PLUS
13543           && ((REG_P (XEXP (rtl, 0))
13544                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13545                && CONST_INT_P (XEXP (rtl, 1)))));
13546 }
13547
13548 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13549    failed.  */
13550
13551 static dw_loc_descr_ref
13552 tls_mem_loc_descriptor (rtx mem)
13553 {
13554   tree base;
13555   dw_loc_descr_ref loc_result;
13556
13557   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13558     return NULL;
13559
13560   base = get_base_address (MEM_EXPR (mem));
13561   if (base == NULL
13562       || TREE_CODE (base) != VAR_DECL
13563       || !DECL_THREAD_LOCAL_P (base))
13564     return NULL;
13565
13566   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13567   if (loc_result == NULL)
13568     return NULL;
13569
13570   if (INTVAL (MEM_OFFSET (mem)))
13571     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13572
13573   return loc_result;
13574 }
13575
13576 /* Output debug info about reason why we failed to expand expression as dwarf
13577    expression.  */
13578
13579 static void
13580 expansion_failed (tree expr, rtx rtl, char const *reason)
13581 {
13582   if (dump_file && (dump_flags & TDF_DETAILS))
13583     {
13584       fprintf (dump_file, "Failed to expand as dwarf: ");
13585       if (expr)
13586         print_generic_expr (dump_file, expr, dump_flags);
13587       if (rtl)
13588         {
13589           fprintf (dump_file, "\n");
13590           print_rtl (dump_file, rtl);
13591         }
13592       fprintf (dump_file, "\nReason: %s\n", reason);
13593     }
13594 }
13595
13596 /* Helper function for const_ok_for_output, called either directly
13597    or via for_each_rtx.  */
13598
13599 static int
13600 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13601 {
13602   rtx rtl = *rtlp;
13603
13604   if (GET_CODE (rtl) == UNSPEC)
13605     {
13606       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13607          we can't express it in the debug info.  */
13608 #ifdef ENABLE_CHECKING
13609       /* Don't complain about TLS UNSPECs, those are just too hard to
13610          delegitimize.  */
13611       if (XVECLEN (rtl, 0) != 1
13612           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13613           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13614           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13615           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13616         inform (current_function_decl
13617                 ? DECL_SOURCE_LOCATION (current_function_decl)
13618                 : UNKNOWN_LOCATION,
13619                 "non-delegitimized UNSPEC %d found in variable location",
13620                 XINT (rtl, 1));
13621 #endif
13622       expansion_failed (NULL_TREE, rtl,
13623                         "UNSPEC hasn't been delegitimized.\n");
13624       return 1;
13625     }
13626
13627   if (GET_CODE (rtl) != SYMBOL_REF)
13628     return 0;
13629
13630   if (CONSTANT_POOL_ADDRESS_P (rtl))
13631     {
13632       bool marked;
13633       get_pool_constant_mark (rtl, &marked);
13634       /* If all references to this pool constant were optimized away,
13635          it was not output and thus we can't represent it.  */
13636       if (!marked)
13637         {
13638           expansion_failed (NULL_TREE, rtl,
13639                             "Constant was removed from constant pool.\n");
13640           return 1;
13641         }
13642     }
13643
13644   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13645     return 1;
13646
13647   /* Avoid references to external symbols in debug info, on several targets
13648      the linker might even refuse to link when linking a shared library,
13649      and in many other cases the relocations for .debug_info/.debug_loc are
13650      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13651      to be defined within the same shared library or executable are fine.  */
13652   if (SYMBOL_REF_EXTERNAL_P (rtl))
13653     {
13654       tree decl = SYMBOL_REF_DECL (rtl);
13655
13656       if (decl == NULL || !targetm.binds_local_p (decl))
13657         {
13658           expansion_failed (NULL_TREE, rtl,
13659                             "Symbol not defined in current TU.\n");
13660           return 1;
13661         }
13662     }
13663
13664   return 0;
13665 }
13666
13667 /* Return true if constant RTL can be emitted in DW_OP_addr or
13668    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13669    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13670
13671 static bool
13672 const_ok_for_output (rtx rtl)
13673 {
13674   if (GET_CODE (rtl) == SYMBOL_REF)
13675     return const_ok_for_output_1 (&rtl, NULL) == 0;
13676
13677   if (GET_CODE (rtl) == CONST)
13678     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13679
13680   return true;
13681 }
13682
13683 /* The following routine converts the RTL for a variable or parameter
13684    (resident in memory) into an equivalent Dwarf representation of a
13685    mechanism for getting the address of that same variable onto the top of a
13686    hypothetical "address evaluation" stack.
13687
13688    When creating memory location descriptors, we are effectively transforming
13689    the RTL for a memory-resident object into its Dwarf postfix expression
13690    equivalent.  This routine recursively descends an RTL tree, turning
13691    it into Dwarf postfix code as it goes.
13692
13693    MODE is the mode of the memory reference, needed to handle some
13694    autoincrement addressing modes.
13695
13696    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13697    location list for RTL.
13698
13699    Return 0 if we can't represent the location.  */
13700
13701 static dw_loc_descr_ref
13702 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13703                     enum var_init_status initialized)
13704 {
13705   dw_loc_descr_ref mem_loc_result = NULL;
13706   enum dwarf_location_atom op;
13707   dw_loc_descr_ref op0, op1;
13708
13709   /* Note that for a dynamically sized array, the location we will generate a
13710      description of here will be the lowest numbered location which is
13711      actually within the array.  That's *not* necessarily the same as the
13712      zeroth element of the array.  */
13713
13714   rtl = targetm.delegitimize_address (rtl);
13715
13716   switch (GET_CODE (rtl))
13717     {
13718     case POST_INC:
13719     case POST_DEC:
13720     case POST_MODIFY:
13721       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13722
13723     case SUBREG:
13724       /* The case of a subreg may arise when we have a local (register)
13725          variable or a formal (register) parameter which doesn't quite fill
13726          up an entire register.  For now, just assume that it is
13727          legitimate to make the Dwarf info refer to the whole register which
13728          contains the given subreg.  */
13729       if (!subreg_lowpart_p (rtl))
13730         break;
13731       rtl = SUBREG_REG (rtl);
13732       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13733         break;
13734       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13735         break;
13736       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13737       break;
13738
13739     case REG:
13740       /* Whenever a register number forms a part of the description of the
13741          method for calculating the (dynamic) address of a memory resident
13742          object, DWARF rules require the register number be referred to as
13743          a "base register".  This distinction is not based in any way upon
13744          what category of register the hardware believes the given register
13745          belongs to.  This is strictly DWARF terminology we're dealing with
13746          here. Note that in cases where the location of a memory-resident
13747          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13748          OP_CONST (0)) the actual DWARF location descriptor that we generate
13749          may just be OP_BASEREG (basereg).  This may look deceptively like
13750          the object in question was allocated to a register (rather than in
13751          memory) so DWARF consumers need to be aware of the subtle
13752          distinction between OP_REG and OP_BASEREG.  */
13753       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13754         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13755       else if (stack_realign_drap
13756                && crtl->drap_reg
13757                && crtl->args.internal_arg_pointer == rtl
13758                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13759         {
13760           /* If RTL is internal_arg_pointer, which has been optimized
13761              out, use DRAP instead.  */
13762           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13763                                             VAR_INIT_STATUS_INITIALIZED);
13764         }
13765       break;
13766
13767     case SIGN_EXTEND:
13768     case ZERO_EXTEND:
13769       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13770                                 VAR_INIT_STATUS_INITIALIZED);
13771       if (op0 == 0)
13772         break;
13773       else
13774         {
13775           int shift = DWARF2_ADDR_SIZE
13776                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13777           shift *= BITS_PER_UNIT;
13778           if (GET_CODE (rtl) == SIGN_EXTEND)
13779             op = DW_OP_shra;
13780           else
13781             op = DW_OP_shr;
13782           mem_loc_result = op0;
13783           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13784           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13785           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13786           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13787         }
13788       break;
13789
13790     case MEM:
13791       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13792                                            VAR_INIT_STATUS_INITIALIZED);
13793       if (mem_loc_result == NULL)
13794         mem_loc_result = tls_mem_loc_descriptor (rtl);
13795       if (mem_loc_result != 0)
13796         {
13797           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13798             {
13799               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13800               return 0;
13801             }
13802           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13803             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13804           else
13805             add_loc_descr (&mem_loc_result,
13806                            new_loc_descr (DW_OP_deref_size,
13807                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13808         }
13809       else
13810         {
13811           rtx new_rtl = avoid_constant_pool_reference (rtl);
13812           if (new_rtl != rtl)
13813             return mem_loc_descriptor (new_rtl, mode, initialized);
13814         }
13815       break;
13816
13817     case LO_SUM:
13818          rtl = XEXP (rtl, 1);
13819
13820       /* ... fall through ...  */
13821
13822     case LABEL_REF:
13823       /* Some ports can transform a symbol ref into a label ref, because
13824          the symbol ref is too far away and has to be dumped into a constant
13825          pool.  */
13826     case CONST:
13827     case SYMBOL_REF:
13828       if (GET_CODE (rtl) == SYMBOL_REF
13829           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13830         {
13831           dw_loc_descr_ref temp;
13832
13833           /* If this is not defined, we have no way to emit the data.  */
13834           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13835             break;
13836
13837           /* We used to emit DW_OP_addr here, but that's wrong, since
13838              DW_OP_addr should be relocated by the debug info consumer,
13839              while DW_OP_GNU_push_tls_address operand should not.  */
13840           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13841                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13842           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13843           temp->dw_loc_oprnd1.v.val_addr = rtl;
13844           temp->dtprel = true;
13845
13846           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13847           add_loc_descr (&mem_loc_result, temp);
13848
13849           break;
13850         }
13851
13852       if (!const_ok_for_output (rtl))
13853         break;
13854
13855     symref:
13856       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13857       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13858       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13859       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13860       break;
13861
13862     case CONCAT:
13863     case CONCATN:
13864     case VAR_LOCATION:
13865     case DEBUG_IMPLICIT_PTR:
13866       expansion_failed (NULL_TREE, rtl,
13867                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13868       return 0;
13869
13870     case ENTRY_VALUE:
13871       if (dwarf_strict)
13872         return NULL;
13873       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13874       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13875       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13876         mem_loc_result->dw_loc_oprnd1.v.val_loc
13877           = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13878                                     VAR_INIT_STATUS_INITIALIZED);
13879       else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13880         {
13881           dw_loc_descr_ref ref
13882             = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13883                                   VAR_INIT_STATUS_INITIALIZED);
13884           if (ref == NULL)
13885             return NULL;
13886           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13887         }
13888       else
13889         gcc_unreachable ();
13890       return mem_loc_result;
13891
13892     case PRE_MODIFY:
13893       /* Extract the PLUS expression nested inside and fall into
13894          PLUS code below.  */
13895       rtl = XEXP (rtl, 1);
13896       goto plus;
13897
13898     case PRE_INC:
13899     case PRE_DEC:
13900       /* Turn these into a PLUS expression and fall into the PLUS code
13901          below.  */
13902       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13903                           GEN_INT (GET_CODE (rtl) == PRE_INC
13904                                    ? GET_MODE_UNIT_SIZE (mode)
13905                                    : -GET_MODE_UNIT_SIZE (mode)));
13906
13907       /* ... fall through ...  */
13908
13909     case PLUS:
13910     plus:
13911       if (is_based_loc (rtl))
13912         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13913                                           INTVAL (XEXP (rtl, 1)),
13914                                           VAR_INIT_STATUS_INITIALIZED);
13915       else
13916         {
13917           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13918                                                VAR_INIT_STATUS_INITIALIZED);
13919           if (mem_loc_result == 0)
13920             break;
13921
13922           if (CONST_INT_P (XEXP (rtl, 1)))
13923             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13924           else
13925             {
13926               dw_loc_descr_ref mem_loc_result2
13927                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13928                                       VAR_INIT_STATUS_INITIALIZED);
13929               if (mem_loc_result2 == 0)
13930                 break;
13931               add_loc_descr (&mem_loc_result, mem_loc_result2);
13932               add_loc_descr (&mem_loc_result,
13933                              new_loc_descr (DW_OP_plus, 0, 0));
13934             }
13935         }
13936       break;
13937
13938     /* If a pseudo-reg is optimized away, it is possible for it to
13939        be replaced with a MEM containing a multiply or shift.  */
13940     case MINUS:
13941       op = DW_OP_minus;
13942       goto do_binop;
13943
13944     case MULT:
13945       op = DW_OP_mul;
13946       goto do_binop;
13947
13948     case DIV:
13949       op = DW_OP_div;
13950       goto do_binop;
13951
13952     case UMOD:
13953       op = DW_OP_mod;
13954       goto do_binop;
13955
13956     case ASHIFT:
13957       op = DW_OP_shl;
13958       goto do_binop;
13959
13960     case ASHIFTRT:
13961       op = DW_OP_shra;
13962       goto do_binop;
13963
13964     case LSHIFTRT:
13965       op = DW_OP_shr;
13966       goto do_binop;
13967
13968     case AND:
13969       op = DW_OP_and;
13970       goto do_binop;
13971
13972     case IOR:
13973       op = DW_OP_or;
13974       goto do_binop;
13975
13976     case XOR:
13977       op = DW_OP_xor;
13978       goto do_binop;
13979
13980     do_binop:
13981       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13982                                 VAR_INIT_STATUS_INITIALIZED);
13983       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13984                                 VAR_INIT_STATUS_INITIALIZED);
13985
13986       if (op0 == 0 || op1 == 0)
13987         break;
13988
13989       mem_loc_result = op0;
13990       add_loc_descr (&mem_loc_result, op1);
13991       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13992       break;
13993
13994     case MOD:
13995       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13996                                 VAR_INIT_STATUS_INITIALIZED);
13997       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13998                                 VAR_INIT_STATUS_INITIALIZED);
13999
14000       if (op0 == 0 || op1 == 0)
14001         break;
14002
14003       mem_loc_result = op0;
14004       add_loc_descr (&mem_loc_result, op1);
14005       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14006       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14007       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14008       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14009       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14010       break;
14011
14012     case NOT:
14013       op = DW_OP_not;
14014       goto do_unop;
14015
14016     case ABS:
14017       op = DW_OP_abs;
14018       goto do_unop;
14019
14020     case NEG:
14021       op = DW_OP_neg;
14022       goto do_unop;
14023
14024     do_unop:
14025       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14026                                 VAR_INIT_STATUS_INITIALIZED);
14027
14028       if (op0 == 0)
14029         break;
14030
14031       mem_loc_result = op0;
14032       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14033       break;
14034
14035     case CONST_INT:
14036       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14037       break;
14038
14039     case EQ:
14040       op = DW_OP_eq;
14041       goto do_scompare;
14042
14043     case GE:
14044       op = DW_OP_ge;
14045       goto do_scompare;
14046
14047     case GT:
14048       op = DW_OP_gt;
14049       goto do_scompare;
14050
14051     case LE:
14052       op = DW_OP_le;
14053       goto do_scompare;
14054
14055     case LT:
14056       op = DW_OP_lt;
14057       goto do_scompare;
14058
14059     case NE:
14060       op = DW_OP_ne;
14061       goto do_scompare;
14062
14063     do_scompare:
14064       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14065           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14066         break;
14067       else
14068         {
14069           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14070
14071           if (op_mode == VOIDmode)
14072             op_mode = GET_MODE (XEXP (rtl, 1));
14073           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14074             break;
14075
14076           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14077                                     VAR_INIT_STATUS_INITIALIZED);
14078           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14079                                     VAR_INIT_STATUS_INITIALIZED);
14080
14081           if (op0 == 0 || op1 == 0)
14082             break;
14083
14084           if (op_mode != VOIDmode
14085               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14086             {
14087               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14088               shift *= BITS_PER_UNIT;
14089               /* For eq/ne, if the operands are known to be zero-extended,
14090                  there is no need to do the fancy shifting up.  */
14091               if (op == DW_OP_eq || op == DW_OP_ne)
14092                 {
14093                   dw_loc_descr_ref last0, last1;
14094                   for (last0 = op0;
14095                        last0->dw_loc_next != NULL;
14096                        last0 = last0->dw_loc_next)
14097                     ;
14098                   for (last1 = op1;
14099                        last1->dw_loc_next != NULL;
14100                        last1 = last1->dw_loc_next)
14101                     ;
14102                   /* deref_size zero extends, and for constants we can check
14103                      whether they are zero extended or not.  */
14104                   if (((last0->dw_loc_opc == DW_OP_deref_size
14105                         && last0->dw_loc_oprnd1.v.val_int
14106                            <= GET_MODE_SIZE (op_mode))
14107                        || (CONST_INT_P (XEXP (rtl, 0))
14108                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14109                                == (INTVAL (XEXP (rtl, 0))
14110                                    & GET_MODE_MASK (op_mode))))
14111                       && ((last1->dw_loc_opc == DW_OP_deref_size
14112                            && last1->dw_loc_oprnd1.v.val_int
14113                               <= GET_MODE_SIZE (op_mode))
14114                           || (CONST_INT_P (XEXP (rtl, 1))
14115                               && (unsigned HOST_WIDE_INT)
14116                                  INTVAL (XEXP (rtl, 1))
14117                                  == (INTVAL (XEXP (rtl, 1))
14118                                      & GET_MODE_MASK (op_mode)))))
14119                     goto do_compare;
14120                 }
14121               add_loc_descr (&op0, int_loc_descriptor (shift));
14122               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14123               if (CONST_INT_P (XEXP (rtl, 1)))
14124                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14125               else
14126                 {
14127                   add_loc_descr (&op1, int_loc_descriptor (shift));
14128                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14129                 }
14130             }
14131         }
14132
14133     do_compare:
14134       mem_loc_result = op0;
14135       add_loc_descr (&mem_loc_result, op1);
14136       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14137       if (STORE_FLAG_VALUE != 1)
14138         {
14139           add_loc_descr (&mem_loc_result,
14140                          int_loc_descriptor (STORE_FLAG_VALUE));
14141           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14142         }
14143       break;
14144
14145     case GEU:
14146       op = DW_OP_ge;
14147       goto do_ucompare;
14148
14149     case GTU:
14150       op = DW_OP_gt;
14151       goto do_ucompare;
14152
14153     case LEU:
14154       op = DW_OP_le;
14155       goto do_ucompare;
14156
14157     case LTU:
14158       op = DW_OP_lt;
14159       goto do_ucompare;
14160
14161     do_ucompare:
14162       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14163           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14164         break;
14165       else
14166         {
14167           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14168
14169           if (op_mode == VOIDmode)
14170             op_mode = GET_MODE (XEXP (rtl, 1));
14171           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14172             break;
14173
14174           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14175                                     VAR_INIT_STATUS_INITIALIZED);
14176           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14177                                     VAR_INIT_STATUS_INITIALIZED);
14178
14179           if (op0 == 0 || op1 == 0)
14180             break;
14181
14182           if (op_mode != VOIDmode
14183               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14184             {
14185               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14186               dw_loc_descr_ref last0, last1;
14187               for (last0 = op0;
14188                    last0->dw_loc_next != NULL;
14189                    last0 = last0->dw_loc_next)
14190                 ;
14191               for (last1 = op1;
14192                    last1->dw_loc_next != NULL;
14193                    last1 = last1->dw_loc_next)
14194                 ;
14195               if (CONST_INT_P (XEXP (rtl, 0)))
14196                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14197               /* deref_size zero extends, so no need to mask it again.  */
14198               else if (last0->dw_loc_opc != DW_OP_deref_size
14199                        || last0->dw_loc_oprnd1.v.val_int
14200                           > GET_MODE_SIZE (op_mode))
14201                 {
14202                   add_loc_descr (&op0, int_loc_descriptor (mask));
14203                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14204                 }
14205               if (CONST_INT_P (XEXP (rtl, 1)))
14206                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14207               /* deref_size zero extends, so no need to mask it again.  */
14208               else if (last1->dw_loc_opc != DW_OP_deref_size
14209                        || last1->dw_loc_oprnd1.v.val_int
14210                           > GET_MODE_SIZE (op_mode))
14211                 {
14212                   add_loc_descr (&op1, int_loc_descriptor (mask));
14213                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14214                 }
14215             }
14216           else
14217             {
14218               HOST_WIDE_INT bias = 1;
14219               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14220               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14221               if (CONST_INT_P (XEXP (rtl, 1)))
14222                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14223                                           + INTVAL (XEXP (rtl, 1)));
14224               else
14225                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14226                                                     bias, 0));
14227             }
14228         }
14229       goto do_compare;
14230
14231     case SMIN:
14232     case SMAX:
14233     case UMIN:
14234     case UMAX:
14235       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14236           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14237           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14238         break;
14239
14240       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14241                                 VAR_INIT_STATUS_INITIALIZED);
14242       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14243                                 VAR_INIT_STATUS_INITIALIZED);
14244
14245       if (op0 == 0 || op1 == 0)
14246         break;
14247
14248       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14249       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14250       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14251       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14252         {
14253           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14254             {
14255               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14256               add_loc_descr (&op0, int_loc_descriptor (mask));
14257               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14258               add_loc_descr (&op1, int_loc_descriptor (mask));
14259               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14260             }
14261           else
14262             {
14263               HOST_WIDE_INT bias = 1;
14264               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14265               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14266               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14267             }
14268         }
14269       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14270         {
14271           int shift = DWARF2_ADDR_SIZE
14272                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14273           shift *= BITS_PER_UNIT;
14274           add_loc_descr (&op0, int_loc_descriptor (shift));
14275           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14276           add_loc_descr (&op1, int_loc_descriptor (shift));
14277           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14278         }
14279
14280       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14281         op = DW_OP_lt;
14282       else
14283         op = DW_OP_gt;
14284       mem_loc_result = op0;
14285       add_loc_descr (&mem_loc_result, op1);
14286       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14287       {
14288         dw_loc_descr_ref bra_node, drop_node;
14289
14290         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14291         add_loc_descr (&mem_loc_result, bra_node);
14292         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14293         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14294         add_loc_descr (&mem_loc_result, drop_node);
14295         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14296         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14297       }
14298       break;
14299
14300     case ZERO_EXTRACT:
14301     case SIGN_EXTRACT:
14302       if (CONST_INT_P (XEXP (rtl, 1))
14303           && CONST_INT_P (XEXP (rtl, 2))
14304           && ((unsigned) INTVAL (XEXP (rtl, 1))
14305               + (unsigned) INTVAL (XEXP (rtl, 2))
14306               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14307           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14308           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14309         {
14310           int shift, size;
14311           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14312                                     VAR_INIT_STATUS_INITIALIZED);
14313           if (op0 == 0)
14314             break;
14315           if (GET_CODE (rtl) == SIGN_EXTRACT)
14316             op = DW_OP_shra;
14317           else
14318             op = DW_OP_shr;
14319           mem_loc_result = op0;
14320           size = INTVAL (XEXP (rtl, 1));
14321           shift = INTVAL (XEXP (rtl, 2));
14322           if (BITS_BIG_ENDIAN)
14323             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14324                     - shift - size;
14325           if (shift + size != (int) DWARF2_ADDR_SIZE)
14326             {
14327               add_loc_descr (&mem_loc_result,
14328                              int_loc_descriptor (DWARF2_ADDR_SIZE
14329                                                  - shift - size));
14330               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14331             }
14332           if (size != (int) DWARF2_ADDR_SIZE)
14333             {
14334               add_loc_descr (&mem_loc_result,
14335                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14336               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14337             }
14338         }
14339       break;
14340
14341     case IF_THEN_ELSE:
14342       {
14343         dw_loc_descr_ref op2, bra_node, drop_node;
14344         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14345                                   VAR_INIT_STATUS_INITIALIZED);
14346         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14347                                   VAR_INIT_STATUS_INITIALIZED);
14348         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14349                                   VAR_INIT_STATUS_INITIALIZED);
14350         if (op0 == NULL || op1 == NULL || op2 == NULL)
14351           break;
14352
14353         mem_loc_result = op1;
14354         add_loc_descr (&mem_loc_result, op2);
14355         add_loc_descr (&mem_loc_result, op0);
14356         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14357         add_loc_descr (&mem_loc_result, bra_node);
14358         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14359         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14360         add_loc_descr (&mem_loc_result, drop_node);
14361         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14362         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14363       }
14364       break;
14365
14366     case COMPARE:
14367     case ROTATE:
14368     case ROTATERT:
14369     case TRUNCATE:
14370       /* In theory, we could implement the above.  */
14371       /* DWARF cannot represent the unsigned compare operations
14372          natively.  */
14373     case SS_MULT:
14374     case US_MULT:
14375     case SS_DIV:
14376     case US_DIV:
14377     case SS_PLUS:
14378     case US_PLUS:
14379     case SS_MINUS:
14380     case US_MINUS:
14381     case SS_NEG:
14382     case US_NEG:
14383     case SS_ABS:
14384     case SS_ASHIFT:
14385     case US_ASHIFT:
14386     case SS_TRUNCATE:
14387     case US_TRUNCATE:
14388     case UDIV:
14389     case UNORDERED:
14390     case ORDERED:
14391     case UNEQ:
14392     case UNGE:
14393     case UNGT:
14394     case UNLE:
14395     case UNLT:
14396     case LTGT:
14397     case FLOAT_EXTEND:
14398     case FLOAT_TRUNCATE:
14399     case FLOAT:
14400     case UNSIGNED_FLOAT:
14401     case FIX:
14402     case UNSIGNED_FIX:
14403     case FRACT_CONVERT:
14404     case UNSIGNED_FRACT_CONVERT:
14405     case SAT_FRACT:
14406     case UNSIGNED_SAT_FRACT:
14407     case SQRT:
14408     case BSWAP:
14409     case FFS:
14410     case CLZ:
14411     case CTZ:
14412     case POPCOUNT:
14413     case PARITY:
14414     case ASM_OPERANDS:
14415     case VEC_MERGE:
14416     case VEC_SELECT:
14417     case VEC_CONCAT:
14418     case VEC_DUPLICATE:
14419     case UNSPEC:
14420     case HIGH:
14421       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14422          can't express it in the debug info.  This can happen e.g. with some
14423          TLS UNSPECs.  */
14424       break;
14425
14426     case CONST_STRING:
14427       resolve_one_addr (&rtl, NULL);
14428       goto symref;
14429
14430     default:
14431 #ifdef ENABLE_CHECKING
14432       print_rtl (stderr, rtl);
14433       gcc_unreachable ();
14434 #else
14435       break;
14436 #endif
14437     }
14438
14439   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14440     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14441
14442   return mem_loc_result;
14443 }
14444
14445 /* Return a descriptor that describes the concatenation of two locations.
14446    This is typically a complex variable.  */
14447
14448 static dw_loc_descr_ref
14449 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14450 {
14451   dw_loc_descr_ref cc_loc_result = NULL;
14452   dw_loc_descr_ref x0_ref
14453     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14454   dw_loc_descr_ref x1_ref
14455     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14456
14457   if (x0_ref == 0 || x1_ref == 0)
14458     return 0;
14459
14460   cc_loc_result = x0_ref;
14461   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14462
14463   add_loc_descr (&cc_loc_result, x1_ref);
14464   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14465
14466   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14467     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14468
14469   return cc_loc_result;
14470 }
14471
14472 /* Return a descriptor that describes the concatenation of N
14473    locations.  */
14474
14475 static dw_loc_descr_ref
14476 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14477 {
14478   unsigned int i;
14479   dw_loc_descr_ref cc_loc_result = NULL;
14480   unsigned int n = XVECLEN (concatn, 0);
14481
14482   for (i = 0; i < n; ++i)
14483     {
14484       dw_loc_descr_ref ref;
14485       rtx x = XVECEXP (concatn, 0, i);
14486
14487       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14488       if (ref == NULL)
14489         return NULL;
14490
14491       add_loc_descr (&cc_loc_result, ref);
14492       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14493     }
14494
14495   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14496     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14497
14498   return cc_loc_result;
14499 }
14500
14501 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14502    for DEBUG_IMPLICIT_PTR RTL.  */
14503
14504 static dw_loc_descr_ref
14505 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14506 {
14507   dw_loc_descr_ref ret;
14508   dw_die_ref ref;
14509
14510   if (dwarf_strict)
14511     return NULL;
14512   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14513               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14514               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14515   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14516   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14517   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14518   if (ref)
14519     {
14520       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14521       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14522       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14523     }
14524   else
14525     {
14526       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14527       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14528     }
14529   return ret;
14530 }
14531
14532 /* Output a proper Dwarf location descriptor for a variable or parameter
14533    which is either allocated in a register or in a memory location.  For a
14534    register, we just generate an OP_REG and the register number.  For a
14535    memory location we provide a Dwarf postfix expression describing how to
14536    generate the (dynamic) address of the object onto the address stack.
14537
14538    MODE is mode of the decl if this loc_descriptor is going to be used in
14539    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14540    allowed, VOIDmode otherwise.
14541
14542    If we don't know how to describe it, return 0.  */
14543
14544 static dw_loc_descr_ref
14545 loc_descriptor (rtx rtl, enum machine_mode mode,
14546                 enum var_init_status initialized)
14547 {
14548   dw_loc_descr_ref loc_result = NULL;
14549
14550   switch (GET_CODE (rtl))
14551     {
14552     case SUBREG:
14553       /* The case of a subreg may arise when we have a local (register)
14554          variable or a formal (register) parameter which doesn't quite fill
14555          up an entire register.  For now, just assume that it is
14556          legitimate to make the Dwarf info refer to the whole register which
14557          contains the given subreg.  */
14558       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14559       break;
14560
14561     case REG:
14562       loc_result = reg_loc_descriptor (rtl, initialized);
14563       break;
14564
14565     case MEM:
14566       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14567                                        initialized);
14568       if (loc_result == NULL)
14569         loc_result = tls_mem_loc_descriptor (rtl);
14570       if (loc_result == NULL)
14571         {
14572           rtx new_rtl = avoid_constant_pool_reference (rtl);
14573           if (new_rtl != rtl)
14574             loc_result = loc_descriptor (new_rtl, mode, initialized);
14575         }
14576       break;
14577
14578     case CONCAT:
14579       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14580                                           initialized);
14581       break;
14582
14583     case CONCATN:
14584       loc_result = concatn_loc_descriptor (rtl, initialized);
14585       break;
14586
14587     case VAR_LOCATION:
14588       /* Single part.  */
14589       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14590         {
14591           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14592           if (GET_CODE (loc) == EXPR_LIST)
14593             loc = XEXP (loc, 0);
14594           loc_result = loc_descriptor (loc, mode, initialized);
14595           break;
14596         }
14597
14598       rtl = XEXP (rtl, 1);
14599       /* FALLTHRU */
14600
14601     case PARALLEL:
14602       {
14603         rtvec par_elems = XVEC (rtl, 0);
14604         int num_elem = GET_NUM_ELEM (par_elems);
14605         enum machine_mode mode;
14606         int i;
14607
14608         /* Create the first one, so we have something to add to.  */
14609         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14610                                      VOIDmode, initialized);
14611         if (loc_result == NULL)
14612           return NULL;
14613         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14614         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14615         for (i = 1; i < num_elem; i++)
14616           {
14617             dw_loc_descr_ref temp;
14618
14619             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14620                                    VOIDmode, initialized);
14621             if (temp == NULL)
14622               return NULL;
14623             add_loc_descr (&loc_result, temp);
14624             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14625             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14626           }
14627       }
14628       break;
14629
14630     case CONST_INT:
14631       if (mode != VOIDmode && mode != BLKmode)
14632         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14633                                                     INTVAL (rtl));
14634       break;
14635
14636     case CONST_DOUBLE:
14637       if (mode == VOIDmode)
14638         mode = GET_MODE (rtl);
14639
14640       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14641         {
14642           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14643
14644           /* Note that a CONST_DOUBLE rtx could represent either an integer
14645              or a floating-point constant.  A CONST_DOUBLE is used whenever
14646              the constant requires more than one word in order to be
14647              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14648           loc_result = new_loc_descr (DW_OP_implicit_value,
14649                                       GET_MODE_SIZE (mode), 0);
14650           if (SCALAR_FLOAT_MODE_P (mode))
14651             {
14652               unsigned int length = GET_MODE_SIZE (mode);
14653               unsigned char *array
14654                   = (unsigned char*) ggc_alloc_atomic (length);
14655
14656               insert_float (rtl, array);
14657               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14658               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14659               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14660               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14661             }
14662           else
14663             {
14664               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14665               loc_result->dw_loc_oprnd2.v.val_double
14666                 = rtx_to_double_int (rtl);
14667             }
14668         }
14669       break;
14670
14671     case CONST_VECTOR:
14672       if (mode == VOIDmode)
14673         mode = GET_MODE (rtl);
14674
14675       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14676         {
14677           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14678           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14679           unsigned char *array = (unsigned char *)
14680             ggc_alloc_atomic (length * elt_size);
14681           unsigned int i;
14682           unsigned char *p;
14683
14684           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14685           switch (GET_MODE_CLASS (mode))
14686             {
14687             case MODE_VECTOR_INT:
14688               for (i = 0, p = array; i < length; i++, p += elt_size)
14689                 {
14690                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14691                   double_int val = rtx_to_double_int (elt);
14692
14693                   if (elt_size <= sizeof (HOST_WIDE_INT))
14694                     insert_int (double_int_to_shwi (val), elt_size, p);
14695                   else
14696                     {
14697                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14698                       insert_double (val, p);
14699                     }
14700                 }
14701               break;
14702
14703             case MODE_VECTOR_FLOAT:
14704               for (i = 0, p = array; i < length; i++, p += elt_size)
14705                 {
14706                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14707                   insert_float (elt, p);
14708                 }
14709               break;
14710
14711             default:
14712               gcc_unreachable ();
14713             }
14714
14715           loc_result = new_loc_descr (DW_OP_implicit_value,
14716                                       length * elt_size, 0);
14717           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14718           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14719           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14720           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14721         }
14722       break;
14723
14724     case CONST:
14725       if (mode == VOIDmode
14726           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14727           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14728           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14729         {
14730           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14731           break;
14732         }
14733       /* FALLTHROUGH */
14734     case SYMBOL_REF:
14735       if (!const_ok_for_output (rtl))
14736         break;
14737     case LABEL_REF:
14738       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14739           && (dwarf_version >= 4 || !dwarf_strict))
14740         {
14741           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14742           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14743           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14744           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14745           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14746         }
14747       break;
14748
14749     case DEBUG_IMPLICIT_PTR:
14750       loc_result = implicit_ptr_descriptor (rtl, 0);
14751       break;
14752
14753     case PLUS:
14754       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14755           && CONST_INT_P (XEXP (rtl, 1)))
14756         {
14757           loc_result
14758             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14759           break;
14760         }
14761       /* FALLTHRU */
14762     default:
14763       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14764           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14765           && (dwarf_version >= 4 || !dwarf_strict))
14766         {
14767           /* Value expression.  */
14768           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14769           if (loc_result)
14770             add_loc_descr (&loc_result,
14771                            new_loc_descr (DW_OP_stack_value, 0, 0));
14772         }
14773       break;
14774     }
14775
14776   return loc_result;
14777 }
14778
14779 /* We need to figure out what section we should use as the base for the
14780    address ranges where a given location is valid.
14781    1. If this particular DECL has a section associated with it, use that.
14782    2. If this function has a section associated with it, use that.
14783    3. Otherwise, use the text section.
14784    XXX: If you split a variable across multiple sections, we won't notice.  */
14785
14786 static const char *
14787 secname_for_decl (const_tree decl)
14788 {
14789   const char *secname;
14790
14791   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14792     {
14793       tree sectree = DECL_SECTION_NAME (decl);
14794       secname = TREE_STRING_POINTER (sectree);
14795     }
14796   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14797     {
14798       tree sectree = DECL_SECTION_NAME (current_function_decl);
14799       secname = TREE_STRING_POINTER (sectree);
14800     }
14801   else if (cfun && in_cold_section_p)
14802     secname = crtl->subsections.cold_section_label;
14803   else
14804     secname = text_section_label;
14805
14806   return secname;
14807 }
14808
14809 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14810
14811 static bool
14812 decl_by_reference_p (tree decl)
14813 {
14814   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14815            || TREE_CODE (decl) == VAR_DECL)
14816           && DECL_BY_REFERENCE (decl));
14817 }
14818
14819 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14820    for VARLOC.  */
14821
14822 static dw_loc_descr_ref
14823 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14824                enum var_init_status initialized)
14825 {
14826   int have_address = 0;
14827   dw_loc_descr_ref descr;
14828   enum machine_mode mode;
14829
14830   if (want_address != 2)
14831     {
14832       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14833       /* Single part.  */
14834       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14835         {
14836           varloc = PAT_VAR_LOCATION_LOC (varloc);
14837           if (GET_CODE (varloc) == EXPR_LIST)
14838             varloc = XEXP (varloc, 0);
14839           mode = GET_MODE (varloc);
14840           if (MEM_P (varloc))
14841             {
14842               rtx addr = XEXP (varloc, 0);
14843               descr = mem_loc_descriptor (addr, mode, initialized);
14844               if (descr)
14845                 have_address = 1;
14846               else
14847                 {
14848                   rtx x = avoid_constant_pool_reference (varloc);
14849                   if (x != varloc)
14850                     descr = mem_loc_descriptor (x, mode, initialized);
14851                 }
14852             }
14853           else
14854             descr = mem_loc_descriptor (varloc, mode, initialized);
14855         }
14856       else
14857         return 0;
14858     }
14859   else
14860     {
14861       if (GET_CODE (varloc) == VAR_LOCATION)
14862         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14863       else
14864         mode = DECL_MODE (loc);
14865       descr = loc_descriptor (varloc, mode, initialized);
14866       have_address = 1;
14867     }
14868
14869   if (!descr)
14870     return 0;
14871
14872   if (want_address == 2 && !have_address
14873       && (dwarf_version >= 4 || !dwarf_strict))
14874     {
14875       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14876         {
14877           expansion_failed (loc, NULL_RTX,
14878                             "DWARF address size mismatch");
14879           return 0;
14880         }
14881       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14882       have_address = 1;
14883     }
14884   /* Show if we can't fill the request for an address.  */
14885   if (want_address && !have_address)
14886     {
14887       expansion_failed (loc, NULL_RTX,
14888                         "Want address and only have value");
14889       return 0;
14890     }
14891
14892   /* If we've got an address and don't want one, dereference.  */
14893   if (!want_address && have_address)
14894     {
14895       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14896       enum dwarf_location_atom op;
14897
14898       if (size > DWARF2_ADDR_SIZE || size == -1)
14899         {
14900           expansion_failed (loc, NULL_RTX,
14901                             "DWARF address size mismatch");
14902           return 0;
14903         }
14904       else if (size == DWARF2_ADDR_SIZE)
14905         op = DW_OP_deref;
14906       else
14907         op = DW_OP_deref_size;
14908
14909       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14910     }
14911
14912   return descr;
14913 }
14914
14915 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14916    if it is not possible.  */
14917
14918 static dw_loc_descr_ref
14919 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14920 {
14921   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14922     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14923   else if (dwarf_version >= 3 || !dwarf_strict)
14924     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14925   else
14926     return NULL;
14927 }
14928
14929 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14930    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14931
14932 static dw_loc_descr_ref
14933 dw_sra_loc_expr (tree decl, rtx loc)
14934 {
14935   rtx p;
14936   unsigned int padsize = 0;
14937   dw_loc_descr_ref descr, *descr_tail;
14938   unsigned HOST_WIDE_INT decl_size;
14939   rtx varloc;
14940   enum var_init_status initialized;
14941
14942   if (DECL_SIZE (decl) == NULL
14943       || !host_integerp (DECL_SIZE (decl), 1))
14944     return NULL;
14945
14946   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14947   descr = NULL;
14948   descr_tail = &descr;
14949
14950   for (p = loc; p; p = XEXP (p, 1))
14951     {
14952       unsigned int bitsize = decl_piece_bitsize (p);
14953       rtx loc_note = *decl_piece_varloc_ptr (p);
14954       dw_loc_descr_ref cur_descr;
14955       dw_loc_descr_ref *tail, last = NULL;
14956       unsigned int opsize = 0;
14957
14958       if (loc_note == NULL_RTX
14959           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14960         {
14961           padsize += bitsize;
14962           continue;
14963         }
14964       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14965       varloc = NOTE_VAR_LOCATION (loc_note);
14966       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14967       if (cur_descr == NULL)
14968         {
14969           padsize += bitsize;
14970           continue;
14971         }
14972
14973       /* Check that cur_descr either doesn't use
14974          DW_OP_*piece operations, or their sum is equal
14975          to bitsize.  Otherwise we can't embed it.  */
14976       for (tail = &cur_descr; *tail != NULL;
14977            tail = &(*tail)->dw_loc_next)
14978         if ((*tail)->dw_loc_opc == DW_OP_piece)
14979           {
14980             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14981                       * BITS_PER_UNIT;
14982             last = *tail;
14983           }
14984         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14985           {
14986             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14987             last = *tail;
14988           }
14989
14990       if (last != NULL && opsize != bitsize)
14991         {
14992           padsize += bitsize;
14993           continue;
14994         }
14995
14996       /* If there is a hole, add DW_OP_*piece after empty DWARF
14997          expression, which means that those bits are optimized out.  */
14998       if (padsize)
14999         {
15000           if (padsize > decl_size)
15001             return NULL;
15002           decl_size -= padsize;
15003           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15004           if (*descr_tail == NULL)
15005             return NULL;
15006           descr_tail = &(*descr_tail)->dw_loc_next;
15007           padsize = 0;
15008         }
15009       *descr_tail = cur_descr;
15010       descr_tail = tail;
15011       if (bitsize > decl_size)
15012         return NULL;
15013       decl_size -= bitsize;
15014       if (last == NULL)
15015         {
15016           HOST_WIDE_INT offset = 0;
15017           if (GET_CODE (varloc) == VAR_LOCATION
15018               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15019             {
15020               varloc = PAT_VAR_LOCATION_LOC (varloc);
15021               if (GET_CODE (varloc) == EXPR_LIST)
15022                 varloc = XEXP (varloc, 0);
15023             }
15024           do 
15025             {
15026               if (GET_CODE (varloc) == CONST
15027                   || GET_CODE (varloc) == SIGN_EXTEND
15028                   || GET_CODE (varloc) == ZERO_EXTEND)
15029                 varloc = XEXP (varloc, 0);
15030               else if (GET_CODE (varloc) == SUBREG)
15031                 varloc = SUBREG_REG (varloc);
15032               else
15033                 break;
15034             }
15035           while (1);
15036           /* DW_OP_bit_size offset should be zero for register
15037              or implicit location descriptions and empty location
15038              descriptions, but for memory addresses needs big endian
15039              adjustment.  */
15040           if (MEM_P (varloc))
15041             {
15042               unsigned HOST_WIDE_INT memsize
15043                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15044               if (memsize != bitsize)
15045                 {
15046                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15047                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15048                     return NULL;
15049                   if (memsize < bitsize)
15050                     return NULL;
15051                   if (BITS_BIG_ENDIAN)
15052                     offset = memsize - bitsize;
15053                 }
15054             }
15055
15056           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15057           if (*descr_tail == NULL)
15058             return NULL;
15059           descr_tail = &(*descr_tail)->dw_loc_next;
15060         }
15061     }
15062
15063   /* If there were any non-empty expressions, add padding till the end of
15064      the decl.  */
15065   if (descr != NULL && decl_size != 0)
15066     {
15067       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15068       if (*descr_tail == NULL)
15069         return NULL;
15070     }
15071   return descr;
15072 }
15073
15074 /* Return the dwarf representation of the location list LOC_LIST of
15075    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15076    function.  */
15077
15078 static dw_loc_list_ref
15079 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15080 {
15081   const char *endname, *secname;
15082   rtx varloc;
15083   enum var_init_status initialized;
15084   struct var_loc_node *node;
15085   dw_loc_descr_ref descr;
15086   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15087   dw_loc_list_ref list = NULL;
15088   dw_loc_list_ref *listp = &list;
15089
15090   /* Now that we know what section we are using for a base,
15091      actually construct the list of locations.
15092      The first location information is what is passed to the
15093      function that creates the location list, and the remaining
15094      locations just get added on to that list.
15095      Note that we only know the start address for a location
15096      (IE location changes), so to build the range, we use
15097      the range [current location start, next location start].
15098      This means we have to special case the last node, and generate
15099      a range of [last location start, end of function label].  */
15100
15101   secname = secname_for_decl (decl);
15102
15103   for (node = loc_list->first; node; node = node->next)
15104     if (GET_CODE (node->loc) == EXPR_LIST
15105         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15106       {
15107         if (GET_CODE (node->loc) == EXPR_LIST)
15108           {
15109             /* This requires DW_OP_{,bit_}piece, which is not usable
15110                inside DWARF expressions.  */
15111             if (want_address != 2)
15112               continue;
15113             descr = dw_sra_loc_expr (decl, node->loc);
15114             if (descr == NULL)
15115               continue;
15116           }
15117         else
15118           {
15119             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15120             varloc = NOTE_VAR_LOCATION (node->loc);
15121             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15122           }
15123         if (descr)
15124           {
15125             /* The variable has a location between NODE->LABEL and
15126                NODE->NEXT->LABEL.  */
15127             if (node->next)
15128               endname = node->next->label;
15129             /* If the variable has a location at the last label
15130                it keeps its location until the end of function.  */
15131             else if (!current_function_decl)
15132               endname = text_end_label;
15133             else
15134               {
15135                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15136                                              current_function_funcdef_no);
15137                 endname = ggc_strdup (label_id);
15138               }
15139
15140             *listp = new_loc_list (descr, node->label, endname, secname);
15141             listp = &(*listp)->dw_loc_next;
15142           }
15143       }
15144
15145   /* Try to avoid the overhead of a location list emitting a location
15146      expression instead, but only if we didn't have more than one
15147      location entry in the first place.  If some entries were not
15148      representable, we don't want to pretend a single entry that was
15149      applies to the entire scope in which the variable is
15150      available.  */
15151   if (list && loc_list->first->next)
15152     gen_llsym (list);
15153
15154   return list;
15155 }
15156
15157 /* Return if the loc_list has only single element and thus can be represented
15158    as location description.   */
15159
15160 static bool
15161 single_element_loc_list_p (dw_loc_list_ref list)
15162 {
15163   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15164   return !list->ll_symbol;
15165 }
15166
15167 /* To each location in list LIST add loc descr REF.  */
15168
15169 static void
15170 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15171 {
15172   dw_loc_descr_ref copy;
15173   add_loc_descr (&list->expr, ref);
15174   list = list->dw_loc_next;
15175   while (list)
15176     {
15177       copy = ggc_alloc_dw_loc_descr_node ();
15178       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15179       add_loc_descr (&list->expr, copy);
15180       while (copy->dw_loc_next)
15181         {
15182           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15183           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15184           copy->dw_loc_next = new_copy;
15185           copy = new_copy;
15186         }
15187       list = list->dw_loc_next;
15188     }
15189 }
15190
15191 /* Given two lists RET and LIST
15192    produce location list that is result of adding expression in LIST
15193    to expression in RET on each possition in program.
15194    Might be destructive on both RET and LIST.
15195
15196    TODO: We handle only simple cases of RET or LIST having at most one
15197    element. General case would inolve sorting the lists in program order
15198    and merging them that will need some additional work.
15199    Adding that will improve quality of debug info especially for SRA-ed
15200    structures.  */
15201
15202 static void
15203 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15204 {
15205   if (!list)
15206     return;
15207   if (!*ret)
15208     {
15209       *ret = list;
15210       return;
15211     }
15212   if (!list->dw_loc_next)
15213     {
15214       add_loc_descr_to_each (*ret, list->expr);
15215       return;
15216     }
15217   if (!(*ret)->dw_loc_next)
15218     {
15219       add_loc_descr_to_each (list, (*ret)->expr);
15220       *ret = list;
15221       return;
15222     }
15223   expansion_failed (NULL_TREE, NULL_RTX,
15224                     "Don't know how to merge two non-trivial"
15225                     " location lists.\n");
15226   *ret = NULL;
15227   return;
15228 }
15229
15230 /* LOC is constant expression.  Try a luck, look it up in constant
15231    pool and return its loc_descr of its address.  */
15232
15233 static dw_loc_descr_ref
15234 cst_pool_loc_descr (tree loc)
15235 {
15236   /* Get an RTL for this, if something has been emitted.  */
15237   rtx rtl = lookup_constant_def (loc);
15238   enum machine_mode mode;
15239
15240   if (!rtl || !MEM_P (rtl))
15241     {
15242       gcc_assert (!rtl);
15243       return 0;
15244     }
15245   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15246
15247   /* TODO: We might get more coverage if we was actually delaying expansion
15248      of all expressions till end of compilation when constant pools are fully
15249      populated.  */
15250   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15251     {
15252       expansion_failed (loc, NULL_RTX,
15253                         "CST value in contant pool but not marked.");
15254       return 0;
15255     }
15256   mode = GET_MODE (rtl);
15257   rtl = XEXP (rtl, 0);
15258   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15259 }
15260
15261 /* Return dw_loc_list representing address of addr_expr LOC
15262    by looking for innder INDIRECT_REF expression and turing it
15263    into simple arithmetics.  */
15264
15265 static dw_loc_list_ref
15266 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15267 {
15268   tree obj, offset;
15269   HOST_WIDE_INT bitsize, bitpos, bytepos;
15270   enum machine_mode mode;
15271   int volatilep;
15272   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15273   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15274
15275   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15276                              &bitsize, &bitpos, &offset, &mode,
15277                              &unsignedp, &volatilep, false);
15278   STRIP_NOPS (obj);
15279   if (bitpos % BITS_PER_UNIT)
15280     {
15281       expansion_failed (loc, NULL_RTX, "bitfield access");
15282       return 0;
15283     }
15284   if (!INDIRECT_REF_P (obj))
15285     {
15286       expansion_failed (obj,
15287                         NULL_RTX, "no indirect ref in inner refrence");
15288       return 0;
15289     }
15290   if (!offset && !bitpos)
15291     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15292   else if (toplev
15293            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15294            && (dwarf_version >= 4 || !dwarf_strict))
15295     {
15296       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15297       if (!list_ret)
15298         return 0;
15299       if (offset)
15300         {
15301           /* Variable offset.  */
15302           list_ret1 = loc_list_from_tree (offset, 0);
15303           if (list_ret1 == 0)
15304             return 0;
15305           add_loc_list (&list_ret, list_ret1);
15306           if (!list_ret)
15307             return 0;
15308           add_loc_descr_to_each (list_ret,
15309                                  new_loc_descr (DW_OP_plus, 0, 0));
15310         }
15311       bytepos = bitpos / BITS_PER_UNIT;
15312       if (bytepos > 0)
15313         add_loc_descr_to_each (list_ret,
15314                                new_loc_descr (DW_OP_plus_uconst,
15315                                               bytepos, 0));
15316       else if (bytepos < 0)
15317         loc_list_plus_const (list_ret, bytepos);
15318       add_loc_descr_to_each (list_ret,
15319                              new_loc_descr (DW_OP_stack_value, 0, 0));
15320     }
15321   return list_ret;
15322 }
15323
15324
15325 /* Generate Dwarf location list representing LOC.
15326    If WANT_ADDRESS is false, expression computing LOC will be computed
15327    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15328    if WANT_ADDRESS is 2, expression computing address useable in location
15329      will be returned (i.e. DW_OP_reg can be used
15330      to refer to register values).  */
15331
15332 static dw_loc_list_ref
15333 loc_list_from_tree (tree loc, int want_address)
15334 {
15335   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15336   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15337   int have_address = 0;
15338   enum dwarf_location_atom op;
15339
15340   /* ??? Most of the time we do not take proper care for sign/zero
15341      extending the values properly.  Hopefully this won't be a real
15342      problem...  */
15343
15344   switch (TREE_CODE (loc))
15345     {
15346     case ERROR_MARK:
15347       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15348       return 0;
15349
15350     case PLACEHOLDER_EXPR:
15351       /* This case involves extracting fields from an object to determine the
15352          position of other fields.  We don't try to encode this here.  The
15353          only user of this is Ada, which encodes the needed information using
15354          the names of types.  */
15355       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15356       return 0;
15357
15358     case CALL_EXPR:
15359       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15360       /* There are no opcodes for these operations.  */
15361       return 0;
15362
15363     case PREINCREMENT_EXPR:
15364     case PREDECREMENT_EXPR:
15365     case POSTINCREMENT_EXPR:
15366     case POSTDECREMENT_EXPR:
15367       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15368       /* There are no opcodes for these operations.  */
15369       return 0;
15370
15371     case ADDR_EXPR:
15372       /* If we already want an address, see if there is INDIRECT_REF inside
15373          e.g. for &this->field.  */
15374       if (want_address)
15375         {
15376           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15377                        (loc, want_address == 2);
15378           if (list_ret)
15379             have_address = 1;
15380           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15381                    && (ret = cst_pool_loc_descr (loc)))
15382             have_address = 1;
15383         }
15384         /* Otherwise, process the argument and look for the address.  */
15385       if (!list_ret && !ret)
15386         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15387       else
15388         {
15389           if (want_address)
15390             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15391           return NULL;
15392         }
15393       break;
15394
15395     case VAR_DECL:
15396       if (DECL_THREAD_LOCAL_P (loc))
15397         {
15398           rtx rtl;
15399           enum dwarf_location_atom first_op;
15400           enum dwarf_location_atom second_op;
15401           bool dtprel = false;
15402
15403           if (targetm.have_tls)
15404             {
15405               /* If this is not defined, we have no way to emit the
15406                  data.  */
15407               if (!targetm.asm_out.output_dwarf_dtprel)
15408                 return 0;
15409
15410                /* The way DW_OP_GNU_push_tls_address is specified, we
15411                   can only look up addresses of objects in the current
15412                   module.  We used DW_OP_addr as first op, but that's
15413                   wrong, because DW_OP_addr is relocated by the debug
15414                   info consumer, while DW_OP_GNU_push_tls_address
15415                   operand shouldn't be.  */
15416               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15417                 return 0;
15418               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15419               dtprel = true;
15420               second_op = DW_OP_GNU_push_tls_address;
15421             }
15422           else
15423             {
15424               if (!targetm.emutls.debug_form_tls_address
15425                   || !(dwarf_version >= 3 || !dwarf_strict))
15426                 return 0;
15427               /* We stuffed the control variable into the DECL_VALUE_EXPR
15428                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15429                  no longer appear in gimple code.  We used the control
15430                  variable in specific so that we could pick it up here.  */
15431               loc = DECL_VALUE_EXPR (loc);
15432               first_op = DW_OP_addr;
15433               second_op = DW_OP_form_tls_address;
15434             }
15435
15436           rtl = rtl_for_decl_location (loc);
15437           if (rtl == NULL_RTX)
15438             return 0;
15439
15440           if (!MEM_P (rtl))
15441             return 0;
15442           rtl = XEXP (rtl, 0);
15443           if (! CONSTANT_P (rtl))
15444             return 0;
15445
15446           ret = new_loc_descr (first_op, 0, 0);
15447           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15448           ret->dw_loc_oprnd1.v.val_addr = rtl;
15449           ret->dtprel = dtprel;
15450
15451           ret1 = new_loc_descr (second_op, 0, 0);
15452           add_loc_descr (&ret, ret1);
15453
15454           have_address = 1;
15455           break;
15456         }
15457       /* FALLTHRU */
15458
15459     case PARM_DECL:
15460     case RESULT_DECL:
15461       if (DECL_HAS_VALUE_EXPR_P (loc))
15462         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15463                                    want_address);
15464       /* FALLTHRU */
15465
15466     case FUNCTION_DECL:
15467       {
15468         rtx rtl;
15469         var_loc_list *loc_list = lookup_decl_loc (loc);
15470
15471         if (loc_list && loc_list->first)
15472           {
15473             list_ret = dw_loc_list (loc_list, loc, want_address);
15474             have_address = want_address != 0;
15475             break;
15476           }
15477         rtl = rtl_for_decl_location (loc);
15478         if (rtl == NULL_RTX)
15479           {
15480             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15481             return 0;
15482           }
15483         else if (CONST_INT_P (rtl))
15484           {
15485             HOST_WIDE_INT val = INTVAL (rtl);
15486             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15487               val &= GET_MODE_MASK (DECL_MODE (loc));
15488             ret = int_loc_descriptor (val);
15489           }
15490         else if (GET_CODE (rtl) == CONST_STRING)
15491           {
15492             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15493             return 0;
15494           }
15495         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15496           {
15497             ret = new_loc_descr (DW_OP_addr, 0, 0);
15498             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15499             ret->dw_loc_oprnd1.v.val_addr = rtl;
15500           }
15501         else
15502           {
15503             enum machine_mode mode;
15504
15505             /* Certain constructs can only be represented at top-level.  */
15506             if (want_address == 2)
15507               {
15508                 ret = loc_descriptor (rtl, VOIDmode,
15509                                       VAR_INIT_STATUS_INITIALIZED);
15510                 have_address = 1;
15511               }
15512             else
15513               {
15514                 mode = GET_MODE (rtl);
15515                 if (MEM_P (rtl))
15516                   {
15517                     rtl = XEXP (rtl, 0);
15518                     have_address = 1;
15519                   }
15520                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15521               }
15522             if (!ret)
15523               expansion_failed (loc, rtl,
15524                                 "failed to produce loc descriptor for rtl");
15525           }
15526       }
15527       break;
15528
15529     case MEM_REF:
15530       /* ??? FIXME.  */
15531       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15532         return 0;
15533       /* Fallthru.  */
15534     case INDIRECT_REF:
15535       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15536       have_address = 1;
15537       break;
15538
15539     case COMPOUND_EXPR:
15540       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15541
15542     CASE_CONVERT:
15543     case VIEW_CONVERT_EXPR:
15544     case SAVE_EXPR:
15545     case MODIFY_EXPR:
15546       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15547
15548     case COMPONENT_REF:
15549     case BIT_FIELD_REF:
15550     case ARRAY_REF:
15551     case ARRAY_RANGE_REF:
15552     case REALPART_EXPR:
15553     case IMAGPART_EXPR:
15554       {
15555         tree obj, offset;
15556         HOST_WIDE_INT bitsize, bitpos, bytepos;
15557         enum machine_mode mode;
15558         int volatilep;
15559         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15560
15561         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15562                                    &unsignedp, &volatilep, false);
15563
15564         gcc_assert (obj != loc);
15565
15566         list_ret = loc_list_from_tree (obj,
15567                                        want_address == 2
15568                                        && !bitpos && !offset ? 2 : 1);
15569         /* TODO: We can extract value of the small expression via shifting even
15570            for nonzero bitpos.  */
15571         if (list_ret == 0)
15572           return 0;
15573         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15574           {
15575             expansion_failed (loc, NULL_RTX,
15576                               "bitfield access");
15577             return 0;
15578           }
15579
15580         if (offset != NULL_TREE)
15581           {
15582             /* Variable offset.  */
15583             list_ret1 = loc_list_from_tree (offset, 0);
15584             if (list_ret1 == 0)
15585               return 0;
15586             add_loc_list (&list_ret, list_ret1);
15587             if (!list_ret)
15588               return 0;
15589             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15590           }
15591
15592         bytepos = bitpos / BITS_PER_UNIT;
15593         if (bytepos > 0)
15594           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15595         else if (bytepos < 0)
15596           loc_list_plus_const (list_ret, bytepos);
15597
15598         have_address = 1;
15599         break;
15600       }
15601
15602     case INTEGER_CST:
15603       if ((want_address || !host_integerp (loc, 0))
15604           && (ret = cst_pool_loc_descr (loc)))
15605         have_address = 1;
15606       else if (want_address == 2
15607                && host_integerp (loc, 0)
15608                && (ret = address_of_int_loc_descriptor
15609                            (int_size_in_bytes (TREE_TYPE (loc)),
15610                             tree_low_cst (loc, 0))))
15611         have_address = 1;
15612       else if (host_integerp (loc, 0))
15613         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15614       else
15615         {
15616           expansion_failed (loc, NULL_RTX,
15617                             "Integer operand is not host integer");
15618           return 0;
15619         }
15620       break;
15621
15622     case CONSTRUCTOR:
15623     case REAL_CST:
15624     case STRING_CST:
15625     case COMPLEX_CST:
15626       if ((ret = cst_pool_loc_descr (loc)))
15627         have_address = 1;
15628       else
15629       /* We can construct small constants here using int_loc_descriptor.  */
15630         expansion_failed (loc, NULL_RTX,
15631                           "constructor or constant not in constant pool");
15632       break;
15633
15634     case TRUTH_AND_EXPR:
15635     case TRUTH_ANDIF_EXPR:
15636     case BIT_AND_EXPR:
15637       op = DW_OP_and;
15638       goto do_binop;
15639
15640     case TRUTH_XOR_EXPR:
15641     case BIT_XOR_EXPR:
15642       op = DW_OP_xor;
15643       goto do_binop;
15644
15645     case TRUTH_OR_EXPR:
15646     case TRUTH_ORIF_EXPR:
15647     case BIT_IOR_EXPR:
15648       op = DW_OP_or;
15649       goto do_binop;
15650
15651     case FLOOR_DIV_EXPR:
15652     case CEIL_DIV_EXPR:
15653     case ROUND_DIV_EXPR:
15654     case TRUNC_DIV_EXPR:
15655       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15656         return 0;
15657       op = DW_OP_div;
15658       goto do_binop;
15659
15660     case MINUS_EXPR:
15661       op = DW_OP_minus;
15662       goto do_binop;
15663
15664     case FLOOR_MOD_EXPR:
15665     case CEIL_MOD_EXPR:
15666     case ROUND_MOD_EXPR:
15667     case TRUNC_MOD_EXPR:
15668       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15669         {
15670           op = DW_OP_mod;
15671           goto do_binop;
15672         }
15673       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15674       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15675       if (list_ret == 0 || list_ret1 == 0)
15676         return 0;
15677
15678       add_loc_list (&list_ret, list_ret1);
15679       if (list_ret == 0)
15680         return 0;
15681       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15682       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15683       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15684       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15685       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15686       break;
15687
15688     case MULT_EXPR:
15689       op = DW_OP_mul;
15690       goto do_binop;
15691
15692     case LSHIFT_EXPR:
15693       op = DW_OP_shl;
15694       goto do_binop;
15695
15696     case RSHIFT_EXPR:
15697       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15698       goto do_binop;
15699
15700     case POINTER_PLUS_EXPR:
15701     case PLUS_EXPR:
15702       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15703         {
15704           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15705           if (list_ret == 0)
15706             return 0;
15707
15708           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15709           break;
15710         }
15711
15712       op = DW_OP_plus;
15713       goto do_binop;
15714
15715     case LE_EXPR:
15716       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15717         return 0;
15718
15719       op = DW_OP_le;
15720       goto do_binop;
15721
15722     case GE_EXPR:
15723       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15724         return 0;
15725
15726       op = DW_OP_ge;
15727       goto do_binop;
15728
15729     case LT_EXPR:
15730       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15731         return 0;
15732
15733       op = DW_OP_lt;
15734       goto do_binop;
15735
15736     case GT_EXPR:
15737       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15738         return 0;
15739
15740       op = DW_OP_gt;
15741       goto do_binop;
15742
15743     case EQ_EXPR:
15744       op = DW_OP_eq;
15745       goto do_binop;
15746
15747     case NE_EXPR:
15748       op = DW_OP_ne;
15749       goto do_binop;
15750
15751     do_binop:
15752       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15753       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15754       if (list_ret == 0 || list_ret1 == 0)
15755         return 0;
15756
15757       add_loc_list (&list_ret, list_ret1);
15758       if (list_ret == 0)
15759         return 0;
15760       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15761       break;
15762
15763     case TRUTH_NOT_EXPR:
15764     case BIT_NOT_EXPR:
15765       op = DW_OP_not;
15766       goto do_unop;
15767
15768     case ABS_EXPR:
15769       op = DW_OP_abs;
15770       goto do_unop;
15771
15772     case NEGATE_EXPR:
15773       op = DW_OP_neg;
15774       goto do_unop;
15775
15776     do_unop:
15777       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15778       if (list_ret == 0)
15779         return 0;
15780
15781       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15782       break;
15783
15784     case MIN_EXPR:
15785     case MAX_EXPR:
15786       {
15787         const enum tree_code code =
15788           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15789
15790         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15791                       build2 (code, integer_type_node,
15792                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15793                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15794       }
15795
15796       /* ... fall through ...  */
15797
15798     case COND_EXPR:
15799       {
15800         dw_loc_descr_ref lhs
15801           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15802         dw_loc_list_ref rhs
15803           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15804         dw_loc_descr_ref bra_node, jump_node, tmp;
15805
15806         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15807         if (list_ret == 0 || lhs == 0 || rhs == 0)
15808           return 0;
15809
15810         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15811         add_loc_descr_to_each (list_ret, bra_node);
15812
15813         add_loc_list (&list_ret, rhs);
15814         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15815         add_loc_descr_to_each (list_ret, jump_node);
15816
15817         add_loc_descr_to_each (list_ret, lhs);
15818         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15819         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15820
15821         /* ??? Need a node to point the skip at.  Use a nop.  */
15822         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15823         add_loc_descr_to_each (list_ret, tmp);
15824         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15825         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15826       }
15827       break;
15828
15829     case FIX_TRUNC_EXPR:
15830       return 0;
15831
15832     default:
15833       /* Leave front-end specific codes as simply unknown.  This comes
15834          up, for instance, with the C STMT_EXPR.  */
15835       if ((unsigned int) TREE_CODE (loc)
15836           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15837         {
15838           expansion_failed (loc, NULL_RTX,
15839                             "language specific tree node");
15840           return 0;
15841         }
15842
15843 #ifdef ENABLE_CHECKING
15844       /* Otherwise this is a generic code; we should just lists all of
15845          these explicitly.  We forgot one.  */
15846       gcc_unreachable ();
15847 #else
15848       /* In a release build, we want to degrade gracefully: better to
15849          generate incomplete debugging information than to crash.  */
15850       return NULL;
15851 #endif
15852     }
15853
15854   if (!ret && !list_ret)
15855     return 0;
15856
15857   if (want_address == 2 && !have_address
15858       && (dwarf_version >= 4 || !dwarf_strict))
15859     {
15860       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15861         {
15862           expansion_failed (loc, NULL_RTX,
15863                             "DWARF address size mismatch");
15864           return 0;
15865         }
15866       if (ret)
15867         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15868       else
15869         add_loc_descr_to_each (list_ret,
15870                                new_loc_descr (DW_OP_stack_value, 0, 0));
15871       have_address = 1;
15872     }
15873   /* Show if we can't fill the request for an address.  */
15874   if (want_address && !have_address)
15875     {
15876       expansion_failed (loc, NULL_RTX,
15877                         "Want address and only have value");
15878       return 0;
15879     }
15880
15881   gcc_assert (!ret || !list_ret);
15882
15883   /* If we've got an address and don't want one, dereference.  */
15884   if (!want_address && have_address)
15885     {
15886       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15887
15888       if (size > DWARF2_ADDR_SIZE || size == -1)
15889         {
15890           expansion_failed (loc, NULL_RTX,
15891                             "DWARF address size mismatch");
15892           return 0;
15893         }
15894       else if (size == DWARF2_ADDR_SIZE)
15895         op = DW_OP_deref;
15896       else
15897         op = DW_OP_deref_size;
15898
15899       if (ret)
15900         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15901       else
15902         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15903     }
15904   if (ret)
15905     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15906
15907   return list_ret;
15908 }
15909
15910 /* Same as above but return only single location expression.  */
15911 static dw_loc_descr_ref
15912 loc_descriptor_from_tree (tree loc, int want_address)
15913 {
15914   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15915   if (!ret)
15916     return NULL;
15917   if (ret->dw_loc_next)
15918     {
15919       expansion_failed (loc, NULL_RTX,
15920                         "Location list where only loc descriptor needed");
15921       return NULL;
15922     }
15923   return ret->expr;
15924 }
15925
15926 /* Given a value, round it up to the lowest multiple of `boundary'
15927    which is not less than the value itself.  */
15928
15929 static inline HOST_WIDE_INT
15930 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15931 {
15932   return (((value + boundary - 1) / boundary) * boundary);
15933 }
15934
15935 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15936    pointer to the declared type for the relevant field variable, or return
15937    `integer_type_node' if the given node turns out to be an
15938    ERROR_MARK node.  */
15939
15940 static inline tree
15941 field_type (const_tree decl)
15942 {
15943   tree type;
15944
15945   if (TREE_CODE (decl) == ERROR_MARK)
15946     return integer_type_node;
15947
15948   type = DECL_BIT_FIELD_TYPE (decl);
15949   if (type == NULL_TREE)
15950     type = TREE_TYPE (decl);
15951
15952   return type;
15953 }
15954
15955 /* Given a pointer to a tree node, return the alignment in bits for
15956    it, or else return BITS_PER_WORD if the node actually turns out to
15957    be an ERROR_MARK node.  */
15958
15959 static inline unsigned
15960 simple_type_align_in_bits (const_tree type)
15961 {
15962   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15963 }
15964
15965 static inline unsigned
15966 simple_decl_align_in_bits (const_tree decl)
15967 {
15968   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15969 }
15970
15971 /* Return the result of rounding T up to ALIGN.  */
15972
15973 static inline double_int
15974 round_up_to_align (double_int t, unsigned int align)
15975 {
15976   double_int alignd = uhwi_to_double_int (align);
15977   t = double_int_add (t, alignd);
15978   t = double_int_add (t, double_int_minus_one);
15979   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15980   t = double_int_mul (t, alignd);
15981   return t;
15982 }
15983
15984 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15985    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15986    or return 0 if we are unable to determine what that offset is, either
15987    because the argument turns out to be a pointer to an ERROR_MARK node, or
15988    because the offset is actually variable.  (We can't handle the latter case
15989    just yet).  */
15990
15991 static HOST_WIDE_INT
15992 field_byte_offset (const_tree decl)
15993 {
15994   double_int object_offset_in_bits;
15995   double_int object_offset_in_bytes;
15996   double_int bitpos_int;
15997
15998   if (TREE_CODE (decl) == ERROR_MARK)
15999     return 0;
16000
16001   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16002
16003   /* We cannot yet cope with fields whose positions are variable, so
16004      for now, when we see such things, we simply return 0.  Someday, we may
16005      be able to handle such cases, but it will be damn difficult.  */
16006   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16007     return 0;
16008
16009   bitpos_int = tree_to_double_int (bit_position (decl));
16010
16011 #ifdef PCC_BITFIELD_TYPE_MATTERS
16012   if (PCC_BITFIELD_TYPE_MATTERS)
16013     {
16014       tree type;
16015       tree field_size_tree;
16016       double_int deepest_bitpos;
16017       double_int field_size_in_bits;
16018       unsigned int type_align_in_bits;
16019       unsigned int decl_align_in_bits;
16020       double_int type_size_in_bits;
16021
16022       type = field_type (decl);
16023       type_size_in_bits = double_int_type_size_in_bits (type);
16024       type_align_in_bits = simple_type_align_in_bits (type);
16025
16026       field_size_tree = DECL_SIZE (decl);
16027
16028       /* The size could be unspecified if there was an error, or for
16029          a flexible array member.  */
16030       if (!field_size_tree)
16031         field_size_tree = bitsize_zero_node;
16032
16033       /* If the size of the field is not constant, use the type size.  */
16034       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16035         field_size_in_bits = tree_to_double_int (field_size_tree);
16036       else
16037         field_size_in_bits = type_size_in_bits;
16038
16039       decl_align_in_bits = simple_decl_align_in_bits (decl);
16040
16041       /* The GCC front-end doesn't make any attempt to keep track of the
16042          starting bit offset (relative to the start of the containing
16043          structure type) of the hypothetical "containing object" for a
16044          bit-field.  Thus, when computing the byte offset value for the
16045          start of the "containing object" of a bit-field, we must deduce
16046          this information on our own. This can be rather tricky to do in
16047          some cases.  For example, handling the following structure type
16048          definition when compiling for an i386/i486 target (which only
16049          aligns long long's to 32-bit boundaries) can be very tricky:
16050
16051          struct S { int field1; long long field2:31; };
16052
16053          Fortunately, there is a simple rule-of-thumb which can be used
16054          in such cases.  When compiling for an i386/i486, GCC will
16055          allocate 8 bytes for the structure shown above.  It decides to
16056          do this based upon one simple rule for bit-field allocation.
16057          GCC allocates each "containing object" for each bit-field at
16058          the first (i.e. lowest addressed) legitimate alignment boundary
16059          (based upon the required minimum alignment for the declared
16060          type of the field) which it can possibly use, subject to the
16061          condition that there is still enough available space remaining
16062          in the containing object (when allocated at the selected point)
16063          to fully accommodate all of the bits of the bit-field itself.
16064
16065          This simple rule makes it obvious why GCC allocates 8 bytes for
16066          each object of the structure type shown above.  When looking
16067          for a place to allocate the "containing object" for `field2',
16068          the compiler simply tries to allocate a 64-bit "containing
16069          object" at each successive 32-bit boundary (starting at zero)
16070          until it finds a place to allocate that 64- bit field such that
16071          at least 31 contiguous (and previously unallocated) bits remain
16072          within that selected 64 bit field.  (As it turns out, for the
16073          example above, the compiler finds it is OK to allocate the
16074          "containing object" 64-bit field at bit-offset zero within the
16075          structure type.)
16076
16077          Here we attempt to work backwards from the limited set of facts
16078          we're given, and we try to deduce from those facts, where GCC
16079          must have believed that the containing object started (within
16080          the structure type). The value we deduce is then used (by the
16081          callers of this routine) to generate DW_AT_location and
16082          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16083          the case of DW_AT_location, regular fields as well).  */
16084
16085       /* Figure out the bit-distance from the start of the structure to
16086          the "deepest" bit of the bit-field.  */
16087       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16088
16089       /* This is the tricky part.  Use some fancy footwork to deduce
16090          where the lowest addressed bit of the containing object must
16091          be.  */
16092       object_offset_in_bits
16093         = double_int_sub (deepest_bitpos, type_size_in_bits);
16094
16095       /* Round up to type_align by default.  This works best for
16096          bitfields.  */
16097       object_offset_in_bits
16098         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16099
16100       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16101         {
16102           object_offset_in_bits
16103             = double_int_sub (deepest_bitpos, type_size_in_bits);
16104
16105           /* Round up to decl_align instead.  */
16106           object_offset_in_bits
16107             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16108         }
16109     }
16110   else
16111 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16112     object_offset_in_bits = bitpos_int;
16113
16114   object_offset_in_bytes
16115     = double_int_div (object_offset_in_bits,
16116                       uhwi_to_double_int (BITS_PER_UNIT), true,
16117                       TRUNC_DIV_EXPR);
16118   return double_int_to_shwi (object_offset_in_bytes);
16119 }
16120 \f
16121 /* The following routines define various Dwarf attributes and any data
16122    associated with them.  */
16123
16124 /* Add a location description attribute value to a DIE.
16125
16126    This emits location attributes suitable for whole variables and
16127    whole parameters.  Note that the location attributes for struct fields are
16128    generated by the routine `data_member_location_attribute' below.  */
16129
16130 static inline void
16131 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16132                              dw_loc_list_ref descr)
16133 {
16134   if (descr == 0)
16135     return;
16136   if (single_element_loc_list_p (descr))
16137     add_AT_loc (die, attr_kind, descr->expr);
16138   else
16139     add_AT_loc_list (die, attr_kind, descr);
16140 }
16141
16142 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16143
16144 static void
16145 add_accessibility_attribute (dw_die_ref die, tree decl)
16146 {
16147   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16148      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16149      the default has always been DW_ACCESS_public.  */
16150   if (TREE_PROTECTED (decl))
16151     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16152   else if (TREE_PRIVATE (decl))
16153     {
16154       if (dwarf_version == 2
16155           || die->die_parent == NULL
16156           || die->die_parent->die_tag != DW_TAG_class_type)
16157         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16158     }
16159   else if (dwarf_version > 2
16160            && die->die_parent
16161            && die->die_parent->die_tag == DW_TAG_class_type)
16162     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16163 }
16164
16165 /* Attach the specialized form of location attribute used for data members of
16166    struct and union types.  In the special case of a FIELD_DECL node which
16167    represents a bit-field, the "offset" part of this special location
16168    descriptor must indicate the distance in bytes from the lowest-addressed
16169    byte of the containing struct or union type to the lowest-addressed byte of
16170    the "containing object" for the bit-field.  (See the `field_byte_offset'
16171    function above).
16172
16173    For any given bit-field, the "containing object" is a hypothetical object
16174    (of some integral or enum type) within which the given bit-field lives.  The
16175    type of this hypothetical "containing object" is always the same as the
16176    declared type of the individual bit-field itself (for GCC anyway... the
16177    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16178    bytes) of the hypothetical "containing object" which will be given in the
16179    DW_AT_byte_size attribute for this bit-field.  (See the
16180    `byte_size_attribute' function below.)  It is also used when calculating the
16181    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16182    function below.)  */
16183
16184 static void
16185 add_data_member_location_attribute (dw_die_ref die, tree decl)
16186 {
16187   HOST_WIDE_INT offset;
16188   dw_loc_descr_ref loc_descr = 0;
16189
16190   if (TREE_CODE (decl) == TREE_BINFO)
16191     {
16192       /* We're working on the TAG_inheritance for a base class.  */
16193       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16194         {
16195           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16196              aren't at a fixed offset from all (sub)objects of the same
16197              type.  We need to extract the appropriate offset from our
16198              vtable.  The following dwarf expression means
16199
16200                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16201
16202              This is specific to the V3 ABI, of course.  */
16203
16204           dw_loc_descr_ref tmp;
16205
16206           /* Make a copy of the object address.  */
16207           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16208           add_loc_descr (&loc_descr, tmp);
16209
16210           /* Extract the vtable address.  */
16211           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16212           add_loc_descr (&loc_descr, tmp);
16213
16214           /* Calculate the address of the offset.  */
16215           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16216           gcc_assert (offset < 0);
16217
16218           tmp = int_loc_descriptor (-offset);
16219           add_loc_descr (&loc_descr, tmp);
16220           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16221           add_loc_descr (&loc_descr, tmp);
16222
16223           /* Extract the offset.  */
16224           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16225           add_loc_descr (&loc_descr, tmp);
16226
16227           /* Add it to the object address.  */
16228           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16229           add_loc_descr (&loc_descr, tmp);
16230         }
16231       else
16232         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16233     }
16234   else
16235     offset = field_byte_offset (decl);
16236
16237   if (! loc_descr)
16238     {
16239       if (dwarf_version > 2)
16240         {
16241           /* Don't need to output a location expression, just the constant. */
16242           if (offset < 0)
16243             add_AT_int (die, DW_AT_data_member_location, offset);
16244           else
16245             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16246           return;
16247         }
16248       else
16249         {
16250           enum dwarf_location_atom op;
16251
16252           /* The DWARF2 standard says that we should assume that the structure
16253              address is already on the stack, so we can specify a structure
16254              field address by using DW_OP_plus_uconst.  */
16255
16256 #ifdef MIPS_DEBUGGING_INFO
16257           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16258              operator correctly.  It works only if we leave the offset on the
16259              stack.  */
16260           op = DW_OP_constu;
16261 #else
16262           op = DW_OP_plus_uconst;
16263 #endif
16264
16265           loc_descr = new_loc_descr (op, offset, 0);
16266         }
16267     }
16268
16269   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16270 }
16271
16272 /* Writes integer values to dw_vec_const array.  */
16273
16274 static void
16275 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16276 {
16277   while (size != 0)
16278     {
16279       *dest++ = val & 0xff;
16280       val >>= 8;
16281       --size;
16282     }
16283 }
16284
16285 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16286
16287 static HOST_WIDE_INT
16288 extract_int (const unsigned char *src, unsigned int size)
16289 {
16290   HOST_WIDE_INT val = 0;
16291
16292   src += size;
16293   while (size != 0)
16294     {
16295       val <<= 8;
16296       val |= *--src & 0xff;
16297       --size;
16298     }
16299   return val;
16300 }
16301
16302 /* Writes double_int values to dw_vec_const array.  */
16303
16304 static void
16305 insert_double (double_int val, unsigned char *dest)
16306 {
16307   unsigned char *p0 = dest;
16308   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16309
16310   if (WORDS_BIG_ENDIAN)
16311     {
16312       p0 = p1;
16313       p1 = dest;
16314     }
16315
16316   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16317   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16318 }
16319
16320 /* Writes floating point values to dw_vec_const array.  */
16321
16322 static void
16323 insert_float (const_rtx rtl, unsigned char *array)
16324 {
16325   REAL_VALUE_TYPE rv;
16326   long val[4];
16327   int i;
16328
16329   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16330   real_to_target (val, &rv, GET_MODE (rtl));
16331
16332   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16333   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16334     {
16335       insert_int (val[i], 4, array);
16336       array += 4;
16337     }
16338 }
16339
16340 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16341    does not have a "location" either in memory or in a register.  These
16342    things can arise in GNU C when a constant is passed as an actual parameter
16343    to an inlined function.  They can also arise in C++ where declared
16344    constants do not necessarily get memory "homes".  */
16345
16346 static bool
16347 add_const_value_attribute (dw_die_ref die, rtx rtl)
16348 {
16349   switch (GET_CODE (rtl))
16350     {
16351     case CONST_INT:
16352       {
16353         HOST_WIDE_INT val = INTVAL (rtl);
16354
16355         if (val < 0)
16356           add_AT_int (die, DW_AT_const_value, val);
16357         else
16358           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16359       }
16360       return true;
16361
16362     case CONST_DOUBLE:
16363       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16364          floating-point constant.  A CONST_DOUBLE is used whenever the
16365          constant requires more than one word in order to be adequately
16366          represented.  */
16367       {
16368         enum machine_mode mode = GET_MODE (rtl);
16369
16370         if (SCALAR_FLOAT_MODE_P (mode))
16371           {
16372             unsigned int length = GET_MODE_SIZE (mode);
16373             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16374
16375             insert_float (rtl, array);
16376             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16377           }
16378         else
16379           add_AT_double (die, DW_AT_const_value,
16380                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16381       }
16382       return true;
16383
16384     case CONST_VECTOR:
16385       {
16386         enum machine_mode mode = GET_MODE (rtl);
16387         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16388         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16389         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16390           (length * elt_size);
16391         unsigned int i;
16392         unsigned char *p;
16393
16394         switch (GET_MODE_CLASS (mode))
16395           {
16396           case MODE_VECTOR_INT:
16397             for (i = 0, p = array; i < length; i++, p += elt_size)
16398               {
16399                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16400                 double_int val = rtx_to_double_int (elt);
16401
16402                 if (elt_size <= sizeof (HOST_WIDE_INT))
16403                   insert_int (double_int_to_shwi (val), elt_size, p);
16404                 else
16405                   {
16406                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16407                     insert_double (val, p);
16408                   }
16409               }
16410             break;
16411
16412           case MODE_VECTOR_FLOAT:
16413             for (i = 0, p = array; i < length; i++, p += elt_size)
16414               {
16415                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16416                 insert_float (elt, p);
16417               }
16418             break;
16419
16420           default:
16421             gcc_unreachable ();
16422           }
16423
16424         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16425       }
16426       return true;
16427
16428     case CONST_STRING:
16429       if (dwarf_version >= 4 || !dwarf_strict)
16430         {
16431           dw_loc_descr_ref loc_result;
16432           resolve_one_addr (&rtl, NULL);
16433         rtl_addr:
16434           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16435           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16436           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16437           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16438           add_AT_loc (die, DW_AT_location, loc_result);
16439           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16440           return true;
16441         }
16442       return false;
16443
16444     case CONST:
16445       if (CONSTANT_P (XEXP (rtl, 0)))
16446         return add_const_value_attribute (die, XEXP (rtl, 0));
16447       /* FALLTHROUGH */
16448     case SYMBOL_REF:
16449       if (!const_ok_for_output (rtl))
16450         return false;
16451     case LABEL_REF:
16452       if (dwarf_version >= 4 || !dwarf_strict)
16453         goto rtl_addr;
16454       return false;
16455
16456     case PLUS:
16457       /* In cases where an inlined instance of an inline function is passed
16458          the address of an `auto' variable (which is local to the caller) we
16459          can get a situation where the DECL_RTL of the artificial local
16460          variable (for the inlining) which acts as a stand-in for the
16461          corresponding formal parameter (of the inline function) will look
16462          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16463          exactly a compile-time constant expression, but it isn't the address
16464          of the (artificial) local variable either.  Rather, it represents the
16465          *value* which the artificial local variable always has during its
16466          lifetime.  We currently have no way to represent such quasi-constant
16467          values in Dwarf, so for now we just punt and generate nothing.  */
16468       return false;
16469
16470     case HIGH:
16471     case CONST_FIXED:
16472       return false;
16473
16474     case MEM:
16475       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16476           && MEM_READONLY_P (rtl)
16477           && GET_MODE (rtl) == BLKmode)
16478         {
16479           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16480           return true;
16481         }
16482       return false;
16483
16484     default:
16485       /* No other kinds of rtx should be possible here.  */
16486       gcc_unreachable ();
16487     }
16488   return false;
16489 }
16490
16491 /* Determine whether the evaluation of EXPR references any variables
16492    or functions which aren't otherwise used (and therefore may not be
16493    output).  */
16494 static tree
16495 reference_to_unused (tree * tp, int * walk_subtrees,
16496                      void * data ATTRIBUTE_UNUSED)
16497 {
16498   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16499     *walk_subtrees = 0;
16500
16501   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16502       && ! TREE_ASM_WRITTEN (*tp))
16503     return *tp;
16504   /* ???  The C++ FE emits debug information for using decls, so
16505      putting gcc_unreachable here falls over.  See PR31899.  For now
16506      be conservative.  */
16507   else if (!cgraph_global_info_ready
16508            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16509     return *tp;
16510   else if (TREE_CODE (*tp) == VAR_DECL)
16511     {
16512       struct varpool_node *node = varpool_get_node (*tp);
16513       if (!node || !node->needed)
16514         return *tp;
16515     }
16516   else if (TREE_CODE (*tp) == FUNCTION_DECL
16517            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16518     {
16519       /* The call graph machinery must have finished analyzing,
16520          optimizing and gimplifying the CU by now.
16521          So if *TP has no call graph node associated
16522          to it, it means *TP will not be emitted.  */
16523       if (!cgraph_get_node (*tp))
16524         return *tp;
16525     }
16526   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16527     return *tp;
16528
16529   return NULL_TREE;
16530 }
16531
16532 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16533    for use in a later add_const_value_attribute call.  */
16534
16535 static rtx
16536 rtl_for_decl_init (tree init, tree type)
16537 {
16538   rtx rtl = NULL_RTX;
16539
16540   STRIP_NOPS (init);
16541
16542   /* If a variable is initialized with a string constant without embedded
16543      zeros, build CONST_STRING.  */
16544   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16545     {
16546       tree enttype = TREE_TYPE (type);
16547       tree domain = TYPE_DOMAIN (type);
16548       enum machine_mode mode = TYPE_MODE (enttype);
16549
16550       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16551           && domain
16552           && integer_zerop (TYPE_MIN_VALUE (domain))
16553           && compare_tree_int (TYPE_MAX_VALUE (domain),
16554                                TREE_STRING_LENGTH (init) - 1) == 0
16555           && ((size_t) TREE_STRING_LENGTH (init)
16556               == strlen (TREE_STRING_POINTER (init)) + 1))
16557         {
16558           rtl = gen_rtx_CONST_STRING (VOIDmode,
16559                                       ggc_strdup (TREE_STRING_POINTER (init)));
16560           rtl = gen_rtx_MEM (BLKmode, rtl);
16561           MEM_READONLY_P (rtl) = 1;
16562         }
16563     }
16564   /* Other aggregates, and complex values, could be represented using
16565      CONCAT: FIXME!  */
16566   else if (AGGREGATE_TYPE_P (type)
16567            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16568                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16569            || TREE_CODE (type) == COMPLEX_TYPE)
16570     ;
16571   /* Vectors only work if their mode is supported by the target.
16572      FIXME: generic vectors ought to work too.  */
16573   else if (TREE_CODE (type) == VECTOR_TYPE
16574            && !VECTOR_MODE_P (TYPE_MODE (type)))
16575     ;
16576   /* If the initializer is something that we know will expand into an
16577      immediate RTL constant, expand it now.  We must be careful not to
16578      reference variables which won't be output.  */
16579   else if (initializer_constant_valid_p (init, type)
16580            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16581     {
16582       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16583          possible.  */
16584       if (TREE_CODE (type) == VECTOR_TYPE)
16585         switch (TREE_CODE (init))
16586           {
16587           case VECTOR_CST:
16588             break;
16589           case CONSTRUCTOR:
16590             if (TREE_CONSTANT (init))
16591               {
16592                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16593                 bool constant_p = true;
16594                 tree value;
16595                 unsigned HOST_WIDE_INT ix;
16596
16597                 /* Even when ctor is constant, it might contain non-*_CST
16598                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16599                    belong into VECTOR_CST nodes.  */
16600                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16601                   if (!CONSTANT_CLASS_P (value))
16602                     {
16603                       constant_p = false;
16604                       break;
16605                     }
16606
16607                 if (constant_p)
16608                   {
16609                     init = build_vector_from_ctor (type, elts);
16610                     break;
16611                   }
16612               }
16613             /* FALLTHRU */
16614
16615           default:
16616             return NULL;
16617           }
16618
16619       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16620
16621       /* If expand_expr returns a MEM, it wasn't immediate.  */
16622       gcc_assert (!rtl || !MEM_P (rtl));
16623     }
16624
16625   return rtl;
16626 }
16627
16628 /* Generate RTL for the variable DECL to represent its location.  */
16629
16630 static rtx
16631 rtl_for_decl_location (tree decl)
16632 {
16633   rtx rtl;
16634
16635   /* Here we have to decide where we are going to say the parameter "lives"
16636      (as far as the debugger is concerned).  We only have a couple of
16637      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16638
16639      DECL_RTL normally indicates where the parameter lives during most of the
16640      activation of the function.  If optimization is enabled however, this
16641      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16642      that the parameter doesn't really live anywhere (as far as the code
16643      generation parts of GCC are concerned) during most of the function's
16644      activation.  That will happen (for example) if the parameter is never
16645      referenced within the function.
16646
16647      We could just generate a location descriptor here for all non-NULL
16648      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16649      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16650      where DECL_RTL is NULL or is a pseudo-reg.
16651
16652      Note however that we can only get away with using DECL_INCOMING_RTL as
16653      a backup substitute for DECL_RTL in certain limited cases.  In cases
16654      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16655      we can be sure that the parameter was passed using the same type as it is
16656      declared to have within the function, and that its DECL_INCOMING_RTL
16657      points us to a place where a value of that type is passed.
16658
16659      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16660      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16661      because in these cases DECL_INCOMING_RTL points us to a value of some
16662      type which is *different* from the type of the parameter itself.  Thus,
16663      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16664      such cases, the debugger would end up (for example) trying to fetch a
16665      `float' from a place which actually contains the first part of a
16666      `double'.  That would lead to really incorrect and confusing
16667      output at debug-time.
16668
16669      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16670      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16671      are a couple of exceptions however.  On little-endian machines we can
16672      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16673      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16674      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16675      when (on a little-endian machine) a non-prototyped function has a
16676      parameter declared to be of type `short' or `char'.  In such cases,
16677      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16678      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16679      passed `int' value.  If the debugger then uses that address to fetch
16680      a `short' or a `char' (on a little-endian machine) the result will be
16681      the correct data, so we allow for such exceptional cases below.
16682
16683      Note that our goal here is to describe the place where the given formal
16684      parameter lives during most of the function's activation (i.e. between the
16685      end of the prologue and the start of the epilogue).  We'll do that as best
16686      as we can. Note however that if the given formal parameter is modified
16687      sometime during the execution of the function, then a stack backtrace (at
16688      debug-time) will show the function as having been called with the *new*
16689      value rather than the value which was originally passed in.  This happens
16690      rarely enough that it is not a major problem, but it *is* a problem, and
16691      I'd like to fix it.
16692
16693      A future version of dwarf2out.c may generate two additional attributes for
16694      any given DW_TAG_formal_parameter DIE which will describe the "passed
16695      type" and the "passed location" for the given formal parameter in addition
16696      to the attributes we now generate to indicate the "declared type" and the
16697      "active location" for each parameter.  This additional set of attributes
16698      could be used by debuggers for stack backtraces. Separately, note that
16699      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16700      This happens (for example) for inlined-instances of inline function formal
16701      parameters which are never referenced.  This really shouldn't be
16702      happening.  All PARM_DECL nodes should get valid non-NULL
16703      DECL_INCOMING_RTL values.  FIXME.  */
16704
16705   /* Use DECL_RTL as the "location" unless we find something better.  */
16706   rtl = DECL_RTL_IF_SET (decl);
16707
16708   /* When generating abstract instances, ignore everything except
16709      constants, symbols living in memory, and symbols living in
16710      fixed registers.  */
16711   if (! reload_completed)
16712     {
16713       if (rtl
16714           && (CONSTANT_P (rtl)
16715               || (MEM_P (rtl)
16716                   && CONSTANT_P (XEXP (rtl, 0)))
16717               || (REG_P (rtl)
16718                   && TREE_CODE (decl) == VAR_DECL
16719                   && TREE_STATIC (decl))))
16720         {
16721           rtl = targetm.delegitimize_address (rtl);
16722           return rtl;
16723         }
16724       rtl = NULL_RTX;
16725     }
16726   else if (TREE_CODE (decl) == PARM_DECL)
16727     {
16728       if (rtl == NULL_RTX
16729           || is_pseudo_reg (rtl)
16730           || (MEM_P (rtl)
16731               && is_pseudo_reg (XEXP (rtl, 0))
16732               && DECL_INCOMING_RTL (decl)
16733               && MEM_P (DECL_INCOMING_RTL (decl))
16734               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16735         {
16736           tree declared_type = TREE_TYPE (decl);
16737           tree passed_type = DECL_ARG_TYPE (decl);
16738           enum machine_mode dmode = TYPE_MODE (declared_type);
16739           enum machine_mode pmode = TYPE_MODE (passed_type);
16740
16741           /* This decl represents a formal parameter which was optimized out.
16742              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16743              all cases where (rtl == NULL_RTX) just below.  */
16744           if (dmode == pmode)
16745             rtl = DECL_INCOMING_RTL (decl);
16746           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16747                    && SCALAR_INT_MODE_P (dmode)
16748                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16749                    && DECL_INCOMING_RTL (decl))
16750             {
16751               rtx inc = DECL_INCOMING_RTL (decl);
16752               if (REG_P (inc))
16753                 rtl = inc;
16754               else if (MEM_P (inc))
16755                 {
16756                   if (BYTES_BIG_ENDIAN)
16757                     rtl = adjust_address_nv (inc, dmode,
16758                                              GET_MODE_SIZE (pmode)
16759                                              - GET_MODE_SIZE (dmode));
16760                   else
16761                     rtl = inc;
16762                 }
16763             }
16764         }
16765
16766       /* If the parm was passed in registers, but lives on the stack, then
16767          make a big endian correction if the mode of the type of the
16768          parameter is not the same as the mode of the rtl.  */
16769       /* ??? This is the same series of checks that are made in dbxout.c before
16770          we reach the big endian correction code there.  It isn't clear if all
16771          of these checks are necessary here, but keeping them all is the safe
16772          thing to do.  */
16773       else if (MEM_P (rtl)
16774                && XEXP (rtl, 0) != const0_rtx
16775                && ! CONSTANT_P (XEXP (rtl, 0))
16776                /* Not passed in memory.  */
16777                && !MEM_P (DECL_INCOMING_RTL (decl))
16778                /* Not passed by invisible reference.  */
16779                && (!REG_P (XEXP (rtl, 0))
16780                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16781                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16782 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16783                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16784 #endif
16785                      )
16786                /* Big endian correction check.  */
16787                && BYTES_BIG_ENDIAN
16788                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16789                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16790                    < UNITS_PER_WORD))
16791         {
16792           int offset = (UNITS_PER_WORD
16793                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16794
16795           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16796                              plus_constant (XEXP (rtl, 0), offset));
16797         }
16798     }
16799   else if (TREE_CODE (decl) == VAR_DECL
16800            && rtl
16801            && MEM_P (rtl)
16802            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16803            && BYTES_BIG_ENDIAN)
16804     {
16805       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16806       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16807
16808       /* If a variable is declared "register" yet is smaller than
16809          a register, then if we store the variable to memory, it
16810          looks like we're storing a register-sized value, when in
16811          fact we are not.  We need to adjust the offset of the
16812          storage location to reflect the actual value's bytes,
16813          else gdb will not be able to display it.  */
16814       if (rsize > dsize)
16815         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16816                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16817     }
16818
16819   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16820      and will have been substituted directly into all expressions that use it.
16821      C does not have such a concept, but C++ and other languages do.  */
16822   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16823     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16824
16825   if (rtl)
16826     rtl = targetm.delegitimize_address (rtl);
16827
16828   /* If we don't look past the constant pool, we risk emitting a
16829      reference to a constant pool entry that isn't referenced from
16830      code, and thus is not emitted.  */
16831   if (rtl)
16832     rtl = avoid_constant_pool_reference (rtl);
16833
16834   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16835      in the current CU, resolve_addr will remove the expression referencing
16836      it.  */
16837   if (rtl == NULL_RTX
16838       && TREE_CODE (decl) == VAR_DECL
16839       && !DECL_EXTERNAL (decl)
16840       && TREE_STATIC (decl)
16841       && DECL_NAME (decl)
16842       && !DECL_HARD_REGISTER (decl)
16843       && DECL_MODE (decl) != VOIDmode)
16844     {
16845       rtl = make_decl_rtl_for_debug (decl);
16846       if (!MEM_P (rtl)
16847           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16848           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16849         rtl = NULL_RTX;
16850     }
16851
16852   return rtl;
16853 }
16854
16855 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16856    returned.  If so, the decl for the COMMON block is returned, and the
16857    value is the offset into the common block for the symbol.  */
16858
16859 static tree
16860 fortran_common (tree decl, HOST_WIDE_INT *value)
16861 {
16862   tree val_expr, cvar;
16863   enum machine_mode mode;
16864   HOST_WIDE_INT bitsize, bitpos;
16865   tree offset;
16866   int volatilep = 0, unsignedp = 0;
16867
16868   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16869      it does not have a value (the offset into the common area), or if it
16870      is thread local (as opposed to global) then it isn't common, and shouldn't
16871      be handled as such.  */
16872   if (TREE_CODE (decl) != VAR_DECL
16873       || !TREE_STATIC (decl)
16874       || !DECL_HAS_VALUE_EXPR_P (decl)
16875       || !is_fortran ())
16876     return NULL_TREE;
16877
16878   val_expr = DECL_VALUE_EXPR (decl);
16879   if (TREE_CODE (val_expr) != COMPONENT_REF)
16880     return NULL_TREE;
16881
16882   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16883                               &mode, &unsignedp, &volatilep, true);
16884
16885   if (cvar == NULL_TREE
16886       || TREE_CODE (cvar) != VAR_DECL
16887       || DECL_ARTIFICIAL (cvar)
16888       || !TREE_PUBLIC (cvar))
16889     return NULL_TREE;
16890
16891   *value = 0;
16892   if (offset != NULL)
16893     {
16894       if (!host_integerp (offset, 0))
16895         return NULL_TREE;
16896       *value = tree_low_cst (offset, 0);
16897     }
16898   if (bitpos != 0)
16899     *value += bitpos / BITS_PER_UNIT;
16900
16901   return cvar;
16902 }
16903
16904 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16905    data attribute for a variable or a parameter.  We generate the
16906    DW_AT_const_value attribute only in those cases where the given variable
16907    or parameter does not have a true "location" either in memory or in a
16908    register.  This can happen (for example) when a constant is passed as an
16909    actual argument in a call to an inline function.  (It's possible that
16910    these things can crop up in other ways also.)  Note that one type of
16911    constant value which can be passed into an inlined function is a constant
16912    pointer.  This can happen for example if an actual argument in an inlined
16913    function call evaluates to a compile-time constant address.  */
16914
16915 static bool
16916 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16917                                        enum dwarf_attribute attr)
16918 {
16919   rtx rtl;
16920   dw_loc_list_ref list;
16921   var_loc_list *loc_list;
16922
16923   if (TREE_CODE (decl) == ERROR_MARK)
16924     return false;
16925
16926   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16927               || TREE_CODE (decl) == RESULT_DECL);
16928
16929   /* Try to get some constant RTL for this decl, and use that as the value of
16930      the location.  */
16931
16932   rtl = rtl_for_decl_location (decl);
16933   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16934       && add_const_value_attribute (die, rtl))
16935     return true;
16936
16937   /* See if we have single element location list that is equivalent to
16938      a constant value.  That way we are better to use add_const_value_attribute
16939      rather than expanding constant value equivalent.  */
16940   loc_list = lookup_decl_loc (decl);
16941   if (loc_list
16942       && loc_list->first
16943       && loc_list->first->next == NULL
16944       && NOTE_P (loc_list->first->loc)
16945       && NOTE_VAR_LOCATION (loc_list->first->loc)
16946       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16947     {
16948       struct var_loc_node *node;
16949
16950       node = loc_list->first;
16951       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16952       if (GET_CODE (rtl) == EXPR_LIST)
16953         rtl = XEXP (rtl, 0);
16954       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16955           && add_const_value_attribute (die, rtl))
16956          return true;
16957     }
16958   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16959   if (list)
16960     {
16961       add_AT_location_description (die, attr, list);
16962       return true;
16963     }
16964   /* None of that worked, so it must not really have a location;
16965      try adding a constant value attribute from the DECL_INITIAL.  */
16966   return tree_add_const_value_attribute_for_decl (die, decl);
16967 }
16968
16969 /* Add VARIABLE and DIE into deferred locations list.  */
16970
16971 static void
16972 defer_location (tree variable, dw_die_ref die)
16973 {
16974   deferred_locations entry;
16975   entry.variable = variable;
16976   entry.die = die;
16977   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16978 }
16979
16980 /* Helper function for tree_add_const_value_attribute.  Natively encode
16981    initializer INIT into an array.  Return true if successful.  */
16982
16983 static bool
16984 native_encode_initializer (tree init, unsigned char *array, int size)
16985 {
16986   tree type;
16987
16988   if (init == NULL_TREE)
16989     return false;
16990
16991   STRIP_NOPS (init);
16992   switch (TREE_CODE (init))
16993     {
16994     case STRING_CST:
16995       type = TREE_TYPE (init);
16996       if (TREE_CODE (type) == ARRAY_TYPE)
16997         {
16998           tree enttype = TREE_TYPE (type);
16999           enum machine_mode mode = TYPE_MODE (enttype);
17000
17001           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17002             return false;
17003           if (int_size_in_bytes (type) != size)
17004             return false;
17005           if (size > TREE_STRING_LENGTH (init))
17006             {
17007               memcpy (array, TREE_STRING_POINTER (init),
17008                       TREE_STRING_LENGTH (init));
17009               memset (array + TREE_STRING_LENGTH (init),
17010                       '\0', size - TREE_STRING_LENGTH (init));
17011             }
17012           else
17013             memcpy (array, TREE_STRING_POINTER (init), size);
17014           return true;
17015         }
17016       return false;
17017     case CONSTRUCTOR:
17018       type = TREE_TYPE (init);
17019       if (int_size_in_bytes (type) != size)
17020         return false;
17021       if (TREE_CODE (type) == ARRAY_TYPE)
17022         {
17023           HOST_WIDE_INT min_index;
17024           unsigned HOST_WIDE_INT cnt;
17025           int curpos = 0, fieldsize;
17026           constructor_elt *ce;
17027
17028           if (TYPE_DOMAIN (type) == NULL_TREE
17029               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17030             return false;
17031
17032           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17033           if (fieldsize <= 0)
17034             return false;
17035
17036           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17037           memset (array, '\0', size);
17038           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17039             {
17040               tree val = ce->value;
17041               tree index = ce->index;
17042               int pos = curpos;
17043               if (index && TREE_CODE (index) == RANGE_EXPR)
17044                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17045                       * fieldsize;
17046               else if (index)
17047                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17048
17049               if (val)
17050                 {
17051                   STRIP_NOPS (val);
17052                   if (!native_encode_initializer (val, array + pos, fieldsize))
17053                     return false;
17054                 }
17055               curpos = pos + fieldsize;
17056               if (index && TREE_CODE (index) == RANGE_EXPR)
17057                 {
17058                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17059                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17060                   while (count > 0)
17061                     {
17062                       if (val)
17063                         memcpy (array + curpos, array + pos, fieldsize);
17064                       curpos += fieldsize;
17065                     }
17066                 }
17067               gcc_assert (curpos <= size);
17068             }
17069           return true;
17070         }
17071       else if (TREE_CODE (type) == RECORD_TYPE
17072                || TREE_CODE (type) == UNION_TYPE)
17073         {
17074           tree field = NULL_TREE;
17075           unsigned HOST_WIDE_INT cnt;
17076           constructor_elt *ce;
17077
17078           if (int_size_in_bytes (type) != size)
17079             return false;
17080
17081           if (TREE_CODE (type) == RECORD_TYPE)
17082             field = TYPE_FIELDS (type);
17083
17084           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17085             {
17086               tree val = ce->value;
17087               int pos, fieldsize;
17088
17089               if (ce->index != 0)
17090                 field = ce->index;
17091
17092               if (val)
17093                 STRIP_NOPS (val);
17094
17095               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17096                 return false;
17097
17098               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17099                   && TYPE_DOMAIN (TREE_TYPE (field))
17100                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17101                 return false;
17102               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17103                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17104                 return false;
17105               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17106               pos = int_byte_position (field);
17107               gcc_assert (pos + fieldsize <= size);
17108               if (val
17109                   && !native_encode_initializer (val, array + pos, fieldsize))
17110                 return false;
17111             }
17112           return true;
17113         }
17114       return false;
17115     case VIEW_CONVERT_EXPR:
17116     case NON_LVALUE_EXPR:
17117       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17118     default:
17119       return native_encode_expr (init, array, size) == size;
17120     }
17121 }
17122
17123 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17124    attribute is the const value T.  */
17125
17126 static bool
17127 tree_add_const_value_attribute (dw_die_ref die, tree t)
17128 {
17129   tree init;
17130   tree type = TREE_TYPE (t);
17131   rtx rtl;
17132
17133   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17134     return false;
17135
17136   init = t;
17137   gcc_assert (!DECL_P (init));
17138
17139   rtl = rtl_for_decl_init (init, type);
17140   if (rtl)
17141     return add_const_value_attribute (die, rtl);
17142   /* If the host and target are sane, try harder.  */
17143   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17144            && initializer_constant_valid_p (init, type))
17145     {
17146       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17147       if (size > 0 && (int) size == size)
17148         {
17149           unsigned char *array = (unsigned char *)
17150             ggc_alloc_cleared_atomic (size);
17151
17152           if (native_encode_initializer (init, array, size))
17153             {
17154               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17155               return true;
17156             }
17157         }
17158     }
17159   return false;
17160 }
17161
17162 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17163    attribute is the const value of T, where T is an integral constant
17164    variable with static storage duration
17165    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17166
17167 static bool
17168 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17169 {
17170
17171   if (!decl
17172       || (TREE_CODE (decl) != VAR_DECL
17173           && TREE_CODE (decl) != CONST_DECL))
17174     return false;
17175
17176     if (TREE_READONLY (decl)
17177         && ! TREE_THIS_VOLATILE (decl)
17178         && DECL_INITIAL (decl))
17179       /* OK */;
17180     else
17181       return false;
17182
17183   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17184   if (get_AT (var_die, DW_AT_const_value))
17185     return false;
17186
17187   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17188 }
17189
17190 /* Convert the CFI instructions for the current function into a
17191    location list.  This is used for DW_AT_frame_base when we targeting
17192    a dwarf2 consumer that does not support the dwarf3
17193    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17194    expressions.  */
17195
17196 static dw_loc_list_ref
17197 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17198 {
17199   dw_fde_ref fde;
17200   dw_loc_list_ref list, *list_tail;
17201   dw_cfi_ref cfi;
17202   dw_cfa_location last_cfa, next_cfa;
17203   const char *start_label, *last_label, *section;
17204   dw_cfa_location remember;
17205
17206   fde = current_fde ();
17207   gcc_assert (fde != NULL);
17208
17209   section = secname_for_decl (current_function_decl);
17210   list_tail = &list;
17211   list = NULL;
17212
17213   memset (&next_cfa, 0, sizeof (next_cfa));
17214   next_cfa.reg = INVALID_REGNUM;
17215   remember = next_cfa;
17216
17217   start_label = fde->dw_fde_begin;
17218
17219   /* ??? Bald assumption that the CIE opcode list does not contain
17220      advance opcodes.  */
17221   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17222     lookup_cfa_1 (cfi, &next_cfa, &remember);
17223
17224   last_cfa = next_cfa;
17225   last_label = start_label;
17226
17227   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17228     switch (cfi->dw_cfi_opc)
17229       {
17230       case DW_CFA_set_loc:
17231       case DW_CFA_advance_loc1:
17232       case DW_CFA_advance_loc2:
17233       case DW_CFA_advance_loc4:
17234         if (!cfa_equal_p (&last_cfa, &next_cfa))
17235           {
17236             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17237                                        start_label, last_label, section);
17238
17239             list_tail = &(*list_tail)->dw_loc_next;
17240             last_cfa = next_cfa;
17241             start_label = last_label;
17242           }
17243         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17244         break;
17245
17246       case DW_CFA_advance_loc:
17247         /* The encoding is complex enough that we should never emit this.  */
17248         gcc_unreachable ();
17249
17250       default:
17251         lookup_cfa_1 (cfi, &next_cfa, &remember);
17252         break;
17253       }
17254
17255   if (!cfa_equal_p (&last_cfa, &next_cfa))
17256     {
17257       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17258                                  start_label, last_label, section);
17259       list_tail = &(*list_tail)->dw_loc_next;
17260       start_label = last_label;
17261     }
17262
17263   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17264                              start_label, fde->dw_fde_end, section);
17265
17266   if (list && list->dw_loc_next)
17267     gen_llsym (list);
17268
17269   return list;
17270 }
17271
17272 /* Compute a displacement from the "steady-state frame pointer" to the
17273    frame base (often the same as the CFA), and store it in
17274    frame_pointer_fb_offset.  OFFSET is added to the displacement
17275    before the latter is negated.  */
17276
17277 static void
17278 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17279 {
17280   rtx reg, elim;
17281
17282 #ifdef FRAME_POINTER_CFA_OFFSET
17283   reg = frame_pointer_rtx;
17284   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17285 #else
17286   reg = arg_pointer_rtx;
17287   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17288 #endif
17289
17290   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17291   if (GET_CODE (elim) == PLUS)
17292     {
17293       offset += INTVAL (XEXP (elim, 1));
17294       elim = XEXP (elim, 0);
17295     }
17296
17297   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17298                && (elim == hard_frame_pointer_rtx
17299                    || elim == stack_pointer_rtx))
17300               || elim == (frame_pointer_needed
17301                           ? hard_frame_pointer_rtx
17302                           : stack_pointer_rtx));
17303
17304   frame_pointer_fb_offset = -offset;
17305 }
17306
17307 /* Generate a DW_AT_name attribute given some string value to be included as
17308    the value of the attribute.  */
17309
17310 static void
17311 add_name_attribute (dw_die_ref die, const char *name_string)
17312 {
17313   if (name_string != NULL && *name_string != 0)
17314     {
17315       if (demangle_name_func)
17316         name_string = (*demangle_name_func) (name_string);
17317
17318       add_AT_string (die, DW_AT_name, name_string);
17319     }
17320 }
17321
17322 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17323
17324 static void
17325 add_comp_dir_attribute (dw_die_ref die)
17326 {
17327   const char *wd = get_src_pwd ();
17328   char *wd1;
17329
17330   if (wd == NULL)
17331     return;
17332
17333   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17334     {
17335       int wdlen;
17336
17337       wdlen = strlen (wd);
17338       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17339       strcpy (wd1, wd);
17340       wd1 [wdlen] = DIR_SEPARATOR;
17341       wd1 [wdlen + 1] = 0;
17342       wd = wd1;
17343     }
17344
17345     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17346 }
17347
17348 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17349    default.  */
17350
17351 static int
17352 lower_bound_default (void)
17353 {
17354   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17355     {
17356     case DW_LANG_C:
17357     case DW_LANG_C89:
17358     case DW_LANG_C99:
17359     case DW_LANG_C_plus_plus:
17360     case DW_LANG_ObjC:
17361     case DW_LANG_ObjC_plus_plus:
17362     case DW_LANG_Java:
17363       return 0;
17364     case DW_LANG_Fortran77:
17365     case DW_LANG_Fortran90:
17366     case DW_LANG_Fortran95:
17367       return 1;
17368     case DW_LANG_UPC:
17369     case DW_LANG_D:
17370     case DW_LANG_Python:
17371       return dwarf_version >= 4 ? 0 : -1;
17372     case DW_LANG_Ada95:
17373     case DW_LANG_Ada83:
17374     case DW_LANG_Cobol74:
17375     case DW_LANG_Cobol85:
17376     case DW_LANG_Pascal83:
17377     case DW_LANG_Modula2:
17378     case DW_LANG_PLI:
17379       return dwarf_version >= 4 ? 1 : -1;
17380     default:
17381       return -1;
17382     }
17383 }
17384
17385 /* Given a tree node describing an array bound (either lower or upper) output
17386    a representation for that bound.  */
17387
17388 static void
17389 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17390 {
17391   switch (TREE_CODE (bound))
17392     {
17393     case ERROR_MARK:
17394       return;
17395
17396     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17397     case INTEGER_CST:
17398       {
17399         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17400         int dflt;
17401
17402         /* Use the default if possible.  */
17403         if (bound_attr == DW_AT_lower_bound
17404             && host_integerp (bound, 0)
17405             && (dflt = lower_bound_default ()) != -1
17406             && tree_low_cst (bound, 0) == dflt)
17407           ;
17408
17409         /* Otherwise represent the bound as an unsigned value with the
17410            precision of its type.  The precision and signedness of the
17411            type will be necessary to re-interpret it unambiguously.  */
17412         else if (prec < HOST_BITS_PER_WIDE_INT)
17413           {
17414             unsigned HOST_WIDE_INT mask
17415               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17416             add_AT_unsigned (subrange_die, bound_attr,
17417                              TREE_INT_CST_LOW (bound) & mask);
17418           }
17419         else if (prec == HOST_BITS_PER_WIDE_INT
17420                  || TREE_INT_CST_HIGH (bound) == 0)
17421           add_AT_unsigned (subrange_die, bound_attr,
17422                            TREE_INT_CST_LOW (bound));
17423         else
17424           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17425                          TREE_INT_CST_LOW (bound));
17426       }
17427       break;
17428
17429     CASE_CONVERT:
17430     case VIEW_CONVERT_EXPR:
17431       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17432       break;
17433
17434     case SAVE_EXPR:
17435       break;
17436
17437     case VAR_DECL:
17438     case PARM_DECL:
17439     case RESULT_DECL:
17440       {
17441         dw_die_ref decl_die = lookup_decl_die (bound);
17442
17443         /* ??? Can this happen, or should the variable have been bound
17444            first?  Probably it can, since I imagine that we try to create
17445            the types of parameters in the order in which they exist in
17446            the list, and won't have created a forward reference to a
17447            later parameter.  */
17448         if (decl_die != NULL)
17449           {
17450             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17451             break;
17452           }
17453       }
17454       /* FALLTHRU */
17455
17456     default:
17457       {
17458         /* Otherwise try to create a stack operation procedure to
17459            evaluate the value of the array bound.  */
17460
17461         dw_die_ref ctx, decl_die;
17462         dw_loc_list_ref list;
17463
17464         list = loc_list_from_tree (bound, 2);
17465         if (list == NULL || single_element_loc_list_p (list))
17466           {
17467             /* If DW_AT_*bound is not a reference nor constant, it is
17468                a DWARF expression rather than location description.
17469                For that loc_list_from_tree (bound, 0) is needed.
17470                If that fails to give a single element list,
17471                fall back to outputting this as a reference anyway.  */
17472             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17473             if (list2 && single_element_loc_list_p (list2))
17474               {
17475                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17476                 break;
17477               }
17478           }
17479         if (list == NULL)
17480           break;
17481
17482         if (current_function_decl == 0)
17483           ctx = comp_unit_die ();
17484         else
17485           ctx = lookup_decl_die (current_function_decl);
17486
17487         decl_die = new_die (DW_TAG_variable, ctx, bound);
17488         add_AT_flag (decl_die, DW_AT_artificial, 1);
17489         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17490         add_AT_location_description (decl_die, DW_AT_location, list);
17491         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17492         break;
17493       }
17494     }
17495 }
17496
17497 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17498    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17499    Note that the block of subscript information for an array type also
17500    includes information about the element type of the given array type.  */
17501
17502 static void
17503 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17504 {
17505   unsigned dimension_number;
17506   tree lower, upper;
17507   dw_die_ref subrange_die;
17508
17509   for (dimension_number = 0;
17510        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17511        type = TREE_TYPE (type), dimension_number++)
17512     {
17513       tree domain = TYPE_DOMAIN (type);
17514
17515       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17516         break;
17517
17518       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17519          and (in GNU C only) variable bounds.  Handle all three forms
17520          here.  */
17521       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17522       if (domain)
17523         {
17524           /* We have an array type with specified bounds.  */
17525           lower = TYPE_MIN_VALUE (domain);
17526           upper = TYPE_MAX_VALUE (domain);
17527
17528           /* Define the index type.  */
17529           if (TREE_TYPE (domain))
17530             {
17531               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17532                  TREE_TYPE field.  We can't emit debug info for this
17533                  because it is an unnamed integral type.  */
17534               if (TREE_CODE (domain) == INTEGER_TYPE
17535                   && TYPE_NAME (domain) == NULL_TREE
17536                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17537                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17538                 ;
17539               else
17540                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17541                                     type_die);
17542             }
17543
17544           /* ??? If upper is NULL, the array has unspecified length,
17545              but it does have a lower bound.  This happens with Fortran
17546                dimension arr(N:*)
17547              Since the debugger is definitely going to need to know N
17548              to produce useful results, go ahead and output the lower
17549              bound solo, and hope the debugger can cope.  */
17550
17551           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17552           if (upper)
17553             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17554         }
17555
17556       /* Otherwise we have an array type with an unspecified length.  The
17557          DWARF-2 spec does not say how to handle this; let's just leave out the
17558          bounds.  */
17559     }
17560 }
17561
17562 static void
17563 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17564 {
17565   unsigned size;
17566
17567   switch (TREE_CODE (tree_node))
17568     {
17569     case ERROR_MARK:
17570       size = 0;
17571       break;
17572     case ENUMERAL_TYPE:
17573     case RECORD_TYPE:
17574     case UNION_TYPE:
17575     case QUAL_UNION_TYPE:
17576       size = int_size_in_bytes (tree_node);
17577       break;
17578     case FIELD_DECL:
17579       /* For a data member of a struct or union, the DW_AT_byte_size is
17580          generally given as the number of bytes normally allocated for an
17581          object of the *declared* type of the member itself.  This is true
17582          even for bit-fields.  */
17583       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17584       break;
17585     default:
17586       gcc_unreachable ();
17587     }
17588
17589   /* Note that `size' might be -1 when we get to this point.  If it is, that
17590      indicates that the byte size of the entity in question is variable.  We
17591      have no good way of expressing this fact in Dwarf at the present time,
17592      so just let the -1 pass on through.  */
17593   add_AT_unsigned (die, DW_AT_byte_size, size);
17594 }
17595
17596 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17597    which specifies the distance in bits from the highest order bit of the
17598    "containing object" for the bit-field to the highest order bit of the
17599    bit-field itself.
17600
17601    For any given bit-field, the "containing object" is a hypothetical object
17602    (of some integral or enum type) within which the given bit-field lives.  The
17603    type of this hypothetical "containing object" is always the same as the
17604    declared type of the individual bit-field itself.  The determination of the
17605    exact location of the "containing object" for a bit-field is rather
17606    complicated.  It's handled by the `field_byte_offset' function (above).
17607
17608    Note that it is the size (in bytes) of the hypothetical "containing object"
17609    which will be given in the DW_AT_byte_size attribute for this bit-field.
17610    (See `byte_size_attribute' above).  */
17611
17612 static inline void
17613 add_bit_offset_attribute (dw_die_ref die, tree decl)
17614 {
17615   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17616   tree type = DECL_BIT_FIELD_TYPE (decl);
17617   HOST_WIDE_INT bitpos_int;
17618   HOST_WIDE_INT highest_order_object_bit_offset;
17619   HOST_WIDE_INT highest_order_field_bit_offset;
17620   HOST_WIDE_INT bit_offset;
17621
17622   /* Must be a field and a bit field.  */
17623   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17624
17625   /* We can't yet handle bit-fields whose offsets are variable, so if we
17626      encounter such things, just return without generating any attribute
17627      whatsoever.  Likewise for variable or too large size.  */
17628   if (! host_integerp (bit_position (decl), 0)
17629       || ! host_integerp (DECL_SIZE (decl), 1))
17630     return;
17631
17632   bitpos_int = int_bit_position (decl);
17633
17634   /* Note that the bit offset is always the distance (in bits) from the
17635      highest-order bit of the "containing object" to the highest-order bit of
17636      the bit-field itself.  Since the "high-order end" of any object or field
17637      is different on big-endian and little-endian machines, the computation
17638      below must take account of these differences.  */
17639   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17640   highest_order_field_bit_offset = bitpos_int;
17641
17642   if (! BYTES_BIG_ENDIAN)
17643     {
17644       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17645       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17646     }
17647
17648   bit_offset
17649     = (! BYTES_BIG_ENDIAN
17650        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17651        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17652
17653   if (bit_offset < 0)
17654     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17655   else
17656     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17657 }
17658
17659 /* For a FIELD_DECL node which represents a bit field, output an attribute
17660    which specifies the length in bits of the given field.  */
17661
17662 static inline void
17663 add_bit_size_attribute (dw_die_ref die, tree decl)
17664 {
17665   /* Must be a field and a bit field.  */
17666   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17667               && DECL_BIT_FIELD_TYPE (decl));
17668
17669   if (host_integerp (DECL_SIZE (decl), 1))
17670     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17671 }
17672
17673 /* If the compiled language is ANSI C, then add a 'prototyped'
17674    attribute, if arg types are given for the parameters of a function.  */
17675
17676 static inline void
17677 add_prototyped_attribute (dw_die_ref die, tree func_type)
17678 {
17679   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17680       && prototype_p (func_type))
17681     add_AT_flag (die, DW_AT_prototyped, 1);
17682 }
17683
17684 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17685    by looking in either the type declaration or object declaration
17686    equate table.  */
17687
17688 static inline dw_die_ref
17689 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17690 {
17691   dw_die_ref origin_die = NULL;
17692
17693   if (TREE_CODE (origin) != FUNCTION_DECL)
17694     {
17695       /* We may have gotten separated from the block for the inlined
17696          function, if we're in an exception handler or some such; make
17697          sure that the abstract function has been written out.
17698
17699          Doing this for nested functions is wrong, however; functions are
17700          distinct units, and our context might not even be inline.  */
17701       tree fn = origin;
17702
17703       if (TYPE_P (fn))
17704         fn = TYPE_STUB_DECL (fn);
17705
17706       fn = decl_function_context (fn);
17707       if (fn)
17708         dwarf2out_abstract_function (fn);
17709     }
17710
17711   if (DECL_P (origin))
17712     origin_die = lookup_decl_die (origin);
17713   else if (TYPE_P (origin))
17714     origin_die = lookup_type_die (origin);
17715
17716   /* XXX: Functions that are never lowered don't always have correct block
17717      trees (in the case of java, they simply have no block tree, in some other
17718      languages).  For these functions, there is nothing we can really do to
17719      output correct debug info for inlined functions in all cases.  Rather
17720      than die, we'll just produce deficient debug info now, in that we will
17721      have variables without a proper abstract origin.  In the future, when all
17722      functions are lowered, we should re-add a gcc_assert (origin_die)
17723      here.  */
17724
17725   if (origin_die)
17726     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17727   return origin_die;
17728 }
17729
17730 /* We do not currently support the pure_virtual attribute.  */
17731
17732 static inline void
17733 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17734 {
17735   if (DECL_VINDEX (func_decl))
17736     {
17737       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17738
17739       if (host_integerp (DECL_VINDEX (func_decl), 0))
17740         add_AT_loc (die, DW_AT_vtable_elem_location,
17741                     new_loc_descr (DW_OP_constu,
17742                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17743                                    0));
17744
17745       /* GNU extension: Record what type this method came from originally.  */
17746       if (debug_info_level > DINFO_LEVEL_TERSE
17747           && DECL_CONTEXT (func_decl))
17748         add_AT_die_ref (die, DW_AT_containing_type,
17749                         lookup_type_die (DECL_CONTEXT (func_decl)));
17750     }
17751 }
17752 \f
17753 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17754    given decl.  This used to be a vendor extension until after DWARF 4
17755    standardized it.  */
17756
17757 static void
17758 add_linkage_attr (dw_die_ref die, tree decl)
17759 {
17760   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17761
17762   /* Mimic what assemble_name_raw does with a leading '*'.  */
17763   if (name[0] == '*')
17764     name = &name[1];
17765
17766   if (dwarf_version >= 4)
17767     add_AT_string (die, DW_AT_linkage_name, name);
17768   else
17769     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17770 }
17771
17772 /* Add source coordinate attributes for the given decl.  */
17773
17774 static void
17775 add_src_coords_attributes (dw_die_ref die, tree decl)
17776 {
17777   expanded_location s;
17778
17779   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17780     return;
17781   s = expand_location (DECL_SOURCE_LOCATION (decl));
17782   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17783   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17784 }
17785
17786 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17787
17788 static void
17789 add_linkage_name (dw_die_ref die, tree decl)
17790 {
17791   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17792        && TREE_PUBLIC (decl)
17793        && !DECL_ABSTRACT (decl)
17794        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17795        && die->die_tag != DW_TAG_member)
17796     {
17797       /* Defer until we have an assembler name set.  */
17798       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17799         {
17800           limbo_die_node *asm_name;
17801
17802           asm_name = ggc_alloc_cleared_limbo_die_node ();
17803           asm_name->die = die;
17804           asm_name->created_for = decl;
17805           asm_name->next = deferred_asm_name;
17806           deferred_asm_name = asm_name;
17807         }
17808       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17809         add_linkage_attr (die, decl);
17810     }
17811 }
17812
17813 /* Add a DW_AT_name attribute and source coordinate attribute for the
17814    given decl, but only if it actually has a name.  */
17815
17816 static void
17817 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17818 {
17819   tree decl_name;
17820
17821   decl_name = DECL_NAME (decl);
17822   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17823     {
17824       const char *name = dwarf2_name (decl, 0);
17825       if (name)
17826         add_name_attribute (die, name);
17827       if (! DECL_ARTIFICIAL (decl))
17828         add_src_coords_attributes (die, decl);
17829
17830       add_linkage_name (die, decl);
17831     }
17832
17833 #ifdef VMS_DEBUGGING_INFO
17834   /* Get the function's name, as described by its RTL.  This may be different
17835      from the DECL_NAME name used in the source file.  */
17836   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17837     {
17838       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17839                    XEXP (DECL_RTL (decl), 0));
17840       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17841     }
17842 #endif /* VMS_DEBUGGING_INFO */
17843 }
17844
17845 #ifdef VMS_DEBUGGING_INFO
17846 /* Output the debug main pointer die for VMS */
17847
17848 void
17849 dwarf2out_vms_debug_main_pointer (void)
17850 {
17851   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17852   dw_die_ref die;
17853
17854   /* Allocate the VMS debug main subprogram die.  */
17855   die = ggc_alloc_cleared_die_node ();
17856   die->die_tag = DW_TAG_subprogram;
17857   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17858   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17859                                current_function_funcdef_no);
17860   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17861
17862   /* Make it the first child of comp_unit_die ().  */
17863   die->die_parent = comp_unit_die ();
17864   if (comp_unit_die ()->die_child)
17865     {
17866       die->die_sib = comp_unit_die ()->die_child->die_sib;
17867       comp_unit_die ()->die_child->die_sib = die;
17868     }
17869   else
17870     {
17871       die->die_sib = die;
17872       comp_unit_die ()->die_child = die;
17873     }
17874 }
17875 #endif /* VMS_DEBUGGING_INFO */
17876
17877 /* Push a new declaration scope.  */
17878
17879 static void
17880 push_decl_scope (tree scope)
17881 {
17882   VEC_safe_push (tree, gc, decl_scope_table, scope);
17883 }
17884
17885 /* Pop a declaration scope.  */
17886
17887 static inline void
17888 pop_decl_scope (void)
17889 {
17890   VEC_pop (tree, decl_scope_table);
17891 }
17892
17893 /* Return the DIE for the scope that immediately contains this type.
17894    Non-named types get global scope.  Named types nested in other
17895    types get their containing scope if it's open, or global scope
17896    otherwise.  All other types (i.e. function-local named types) get
17897    the current active scope.  */
17898
17899 static dw_die_ref
17900 scope_die_for (tree t, dw_die_ref context_die)
17901 {
17902   dw_die_ref scope_die = NULL;
17903   tree containing_scope;
17904   int i;
17905
17906   /* Non-types always go in the current scope.  */
17907   gcc_assert (TYPE_P (t));
17908
17909   containing_scope = TYPE_CONTEXT (t);
17910
17911   /* Use the containing namespace if it was passed in (for a declaration).  */
17912   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17913     {
17914       if (context_die == lookup_decl_die (containing_scope))
17915         /* OK */;
17916       else
17917         containing_scope = NULL_TREE;
17918     }
17919
17920   /* Ignore function type "scopes" from the C frontend.  They mean that
17921      a tagged type is local to a parmlist of a function declarator, but
17922      that isn't useful to DWARF.  */
17923   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17924     containing_scope = NULL_TREE;
17925
17926   if (SCOPE_FILE_SCOPE_P (containing_scope))
17927     scope_die = comp_unit_die ();
17928   else if (TYPE_P (containing_scope))
17929     {
17930       /* For types, we can just look up the appropriate DIE.  But
17931          first we check to see if we're in the middle of emitting it
17932          so we know where the new DIE should go.  */
17933       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17934         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17935           break;
17936
17937       if (i < 0)
17938         {
17939           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17940                       || TREE_ASM_WRITTEN (containing_scope));
17941           /*We are not in the middle of emitting the type
17942             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17943           scope_die = lookup_type_die (containing_scope);
17944
17945           /* If none of the current dies are suitable, we get file scope.  */
17946           if (scope_die == NULL)
17947             scope_die = comp_unit_die ();
17948         }
17949       else
17950         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17951     }
17952   else
17953     scope_die = context_die;
17954
17955   return scope_die;
17956 }
17957
17958 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17959
17960 static inline int
17961 local_scope_p (dw_die_ref context_die)
17962 {
17963   for (; context_die; context_die = context_die->die_parent)
17964     if (context_die->die_tag == DW_TAG_inlined_subroutine
17965         || context_die->die_tag == DW_TAG_subprogram)
17966       return 1;
17967
17968   return 0;
17969 }
17970
17971 /* Returns nonzero if CONTEXT_DIE is a class.  */
17972
17973 static inline int
17974 class_scope_p (dw_die_ref context_die)
17975 {
17976   return (context_die
17977           && (context_die->die_tag == DW_TAG_structure_type
17978               || context_die->die_tag == DW_TAG_class_type
17979               || context_die->die_tag == DW_TAG_interface_type
17980               || context_die->die_tag == DW_TAG_union_type));
17981 }
17982
17983 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17984    whether or not to treat a DIE in this context as a declaration.  */
17985
17986 static inline int
17987 class_or_namespace_scope_p (dw_die_ref context_die)
17988 {
17989   return (class_scope_p (context_die)
17990           || (context_die && context_die->die_tag == DW_TAG_namespace));
17991 }
17992
17993 /* Many forms of DIEs require a "type description" attribute.  This
17994    routine locates the proper "type descriptor" die for the type given
17995    by 'type', and adds a DW_AT_type attribute below the given die.  */
17996
17997 static void
17998 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17999                     int decl_volatile, dw_die_ref context_die)
18000 {
18001   enum tree_code code  = TREE_CODE (type);
18002   dw_die_ref type_die  = NULL;
18003
18004   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18005      or fixed-point type, use the inner type.  This is because we have no
18006      support for unnamed types in base_type_die.  This can happen if this is
18007      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18008   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18009       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18010     type = TREE_TYPE (type), code = TREE_CODE (type);
18011
18012   if (code == ERROR_MARK
18013       /* Handle a special case.  For functions whose return type is void, we
18014          generate *no* type attribute.  (Note that no object may have type
18015          `void', so this only applies to function return types).  */
18016       || code == VOID_TYPE)
18017     return;
18018
18019   type_die = modified_type_die (type,
18020                                 decl_const || TYPE_READONLY (type),
18021                                 decl_volatile || TYPE_VOLATILE (type),
18022                                 context_die);
18023
18024   if (type_die != NULL)
18025     add_AT_die_ref (object_die, DW_AT_type, type_die);
18026 }
18027
18028 /* Given an object die, add the calling convention attribute for the
18029    function call type.  */
18030 static void
18031 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18032 {
18033   enum dwarf_calling_convention value = DW_CC_normal;
18034
18035   value = ((enum dwarf_calling_convention)
18036            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18037
18038   if (is_fortran ()
18039       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18040     {
18041       /* DWARF 2 doesn't provide a way to identify a program's source-level
18042         entry point.  DW_AT_calling_convention attributes are only meant
18043         to describe functions' calling conventions.  However, lacking a
18044         better way to signal the Fortran main program, we used this for 
18045         a long time, following existing custom.  Now, DWARF 4 has 
18046         DW_AT_main_subprogram, which we add below, but some tools still
18047         rely on the old way, which we thus keep.  */
18048       value = DW_CC_program;
18049
18050       if (dwarf_version >= 4 || !dwarf_strict)
18051         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18052     }
18053
18054   /* Only add the attribute if the backend requests it, and
18055      is not DW_CC_normal.  */
18056   if (value && (value != DW_CC_normal))
18057     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18058 }
18059
18060 /* Given a tree pointer to a struct, class, union, or enum type node, return
18061    a pointer to the (string) tag name for the given type, or zero if the type
18062    was declared without a tag.  */
18063
18064 static const char *
18065 type_tag (const_tree type)
18066 {
18067   const char *name = 0;
18068
18069   if (TYPE_NAME (type) != 0)
18070     {
18071       tree t = 0;
18072
18073       /* Find the IDENTIFIER_NODE for the type name.  */
18074       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18075           && !TYPE_NAMELESS (type))
18076         t = TYPE_NAME (type);
18077
18078       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18079          a TYPE_DECL node, regardless of whether or not a `typedef' was
18080          involved.  */
18081       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18082                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18083         {
18084           /* We want to be extra verbose.  Don't call dwarf_name if
18085              DECL_NAME isn't set.  The default hook for decl_printable_name
18086              doesn't like that, and in this context it's correct to return
18087              0, instead of "<anonymous>" or the like.  */
18088           if (DECL_NAME (TYPE_NAME (type))
18089               && !DECL_NAMELESS (TYPE_NAME (type)))
18090             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18091         }
18092
18093       /* Now get the name as a string, or invent one.  */
18094       if (!name && t != 0)
18095         name = IDENTIFIER_POINTER (t);
18096     }
18097
18098   return (name == 0 || *name == '\0') ? 0 : name;
18099 }
18100
18101 /* Return the type associated with a data member, make a special check
18102    for bit field types.  */
18103
18104 static inline tree
18105 member_declared_type (const_tree member)
18106 {
18107   return (DECL_BIT_FIELD_TYPE (member)
18108           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18109 }
18110
18111 /* Get the decl's label, as described by its RTL. This may be different
18112    from the DECL_NAME name used in the source file.  */
18113
18114 #if 0
18115 static const char *
18116 decl_start_label (tree decl)
18117 {
18118   rtx x;
18119   const char *fnname;
18120
18121   x = DECL_RTL (decl);
18122   gcc_assert (MEM_P (x));
18123
18124   x = XEXP (x, 0);
18125   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18126
18127   fnname = XSTR (x, 0);
18128   return fnname;
18129 }
18130 #endif
18131 \f
18132 /* These routines generate the internal representation of the DIE's for
18133    the compilation unit.  Debugging information is collected by walking
18134    the declaration trees passed in from dwarf2out_decl().  */
18135
18136 static void
18137 gen_array_type_die (tree type, dw_die_ref context_die)
18138 {
18139   dw_die_ref scope_die = scope_die_for (type, context_die);
18140   dw_die_ref array_die;
18141
18142   /* GNU compilers represent multidimensional array types as sequences of one
18143      dimensional array types whose element types are themselves array types.
18144      We sometimes squish that down to a single array_type DIE with multiple
18145      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18146      say that we are allowed to do this kind of compression in C, because
18147      there is no difference between an array of arrays and a multidimensional
18148      array.  We don't do this for Ada to remain as close as possible to the
18149      actual representation, which is especially important against the language
18150      flexibilty wrt arrays of variable size.  */
18151
18152   bool collapse_nested_arrays = !is_ada ();
18153   tree element_type;
18154
18155   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18156      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18157   if (TYPE_STRING_FLAG (type)
18158       && TREE_CODE (type) == ARRAY_TYPE
18159       && is_fortran ()
18160       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18161     {
18162       HOST_WIDE_INT size;
18163
18164       array_die = new_die (DW_TAG_string_type, scope_die, type);
18165       add_name_attribute (array_die, type_tag (type));
18166       equate_type_number_to_die (type, array_die);
18167       size = int_size_in_bytes (type);
18168       if (size >= 0)
18169         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18170       else if (TYPE_DOMAIN (type) != NULL_TREE
18171                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18172                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18173         {
18174           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18175           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18176
18177           size = int_size_in_bytes (TREE_TYPE (szdecl));
18178           if (loc && size > 0)
18179             {
18180               add_AT_location_description (array_die, DW_AT_string_length, loc);
18181               if (size != DWARF2_ADDR_SIZE)
18182                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18183             }
18184         }
18185       return;
18186     }
18187
18188   /* ??? The SGI dwarf reader fails for array of array of enum types
18189      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18190      array type comes before the outer array type.  We thus call gen_type_die
18191      before we new_die and must prevent nested array types collapsing for this
18192      target.  */
18193
18194 #ifdef MIPS_DEBUGGING_INFO
18195   gen_type_die (TREE_TYPE (type), context_die);
18196   collapse_nested_arrays = false;
18197 #endif
18198
18199   array_die = new_die (DW_TAG_array_type, scope_die, type);
18200   add_name_attribute (array_die, type_tag (type));
18201   equate_type_number_to_die (type, array_die);
18202
18203   if (TREE_CODE (type) == VECTOR_TYPE)
18204     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18205
18206   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18207   if (is_fortran ()
18208       && TREE_CODE (type) == ARRAY_TYPE
18209       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18210       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18211     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18212
18213 #if 0
18214   /* We default the array ordering.  SDB will probably do
18215      the right things even if DW_AT_ordering is not present.  It's not even
18216      an issue until we start to get into multidimensional arrays anyway.  If
18217      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18218      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18219      and when we find out that we need to put these in, we will only do so
18220      for multidimensional arrays.  */
18221   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18222 #endif
18223
18224 #ifdef MIPS_DEBUGGING_INFO
18225   /* The SGI compilers handle arrays of unknown bound by setting
18226      AT_declaration and not emitting any subrange DIEs.  */
18227   if (TREE_CODE (type) == ARRAY_TYPE
18228       && ! TYPE_DOMAIN (type))
18229     add_AT_flag (array_die, DW_AT_declaration, 1);
18230   else
18231 #endif
18232   if (TREE_CODE (type) == VECTOR_TYPE)
18233     {
18234       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18235       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18236       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18237       add_bound_info (subrange_die, DW_AT_upper_bound,
18238                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18239     }
18240   else
18241     add_subscript_info (array_die, type, collapse_nested_arrays);
18242
18243   /* Add representation of the type of the elements of this array type and
18244      emit the corresponding DIE if we haven't done it already.  */
18245   element_type = TREE_TYPE (type);
18246   if (collapse_nested_arrays)
18247     while (TREE_CODE (element_type) == ARRAY_TYPE)
18248       {
18249         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18250           break;
18251         element_type = TREE_TYPE (element_type);
18252       }
18253
18254 #ifndef MIPS_DEBUGGING_INFO
18255   gen_type_die (element_type, context_die);
18256 #endif
18257
18258   add_type_attribute (array_die, element_type, 0, 0, context_die);
18259
18260   if (get_AT (array_die, DW_AT_name))
18261     add_pubtype (type, array_die);
18262 }
18263
18264 static dw_loc_descr_ref
18265 descr_info_loc (tree val, tree base_decl)
18266 {
18267   HOST_WIDE_INT size;
18268   dw_loc_descr_ref loc, loc2;
18269   enum dwarf_location_atom op;
18270
18271   if (val == base_decl)
18272     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18273
18274   switch (TREE_CODE (val))
18275     {
18276     CASE_CONVERT:
18277       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18278     case VAR_DECL:
18279       return loc_descriptor_from_tree (val, 0);
18280     case INTEGER_CST:
18281       if (host_integerp (val, 0))
18282         return int_loc_descriptor (tree_low_cst (val, 0));
18283       break;
18284     case INDIRECT_REF:
18285       size = int_size_in_bytes (TREE_TYPE (val));
18286       if (size < 0)
18287         break;
18288       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18289       if (!loc)
18290         break;
18291       if (size == DWARF2_ADDR_SIZE)
18292         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18293       else
18294         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18295       return loc;
18296     case POINTER_PLUS_EXPR:
18297     case PLUS_EXPR:
18298       if (host_integerp (TREE_OPERAND (val, 1), 1)
18299           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18300              < 16384)
18301         {
18302           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18303           if (!loc)
18304             break;
18305           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18306         }
18307       else
18308         {
18309           op = DW_OP_plus;
18310         do_binop:
18311           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18312           if (!loc)
18313             break;
18314           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18315           if (!loc2)
18316             break;
18317           add_loc_descr (&loc, loc2);
18318           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18319         }
18320       return loc;
18321     case MINUS_EXPR:
18322       op = DW_OP_minus;
18323       goto do_binop;
18324     case MULT_EXPR:
18325       op = DW_OP_mul;
18326       goto do_binop;
18327     case EQ_EXPR:
18328       op = DW_OP_eq;
18329       goto do_binop;
18330     case NE_EXPR:
18331       op = DW_OP_ne;
18332       goto do_binop;
18333     default:
18334       break;
18335     }
18336   return NULL;
18337 }
18338
18339 static void
18340 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18341                       tree val, tree base_decl)
18342 {
18343   dw_loc_descr_ref loc;
18344
18345   if (host_integerp (val, 0))
18346     {
18347       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18348       return;
18349     }
18350
18351   loc = descr_info_loc (val, base_decl);
18352   if (!loc)
18353     return;
18354
18355   add_AT_loc (die, attr, loc);
18356 }
18357
18358 /* This routine generates DIE for array with hidden descriptor, details
18359    are filled into *info by a langhook.  */
18360
18361 static void
18362 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18363                           dw_die_ref context_die)
18364 {
18365   dw_die_ref scope_die = scope_die_for (type, context_die);
18366   dw_die_ref array_die;
18367   int dim;
18368
18369   array_die = new_die (DW_TAG_array_type, scope_die, type);
18370   add_name_attribute (array_die, type_tag (type));
18371   equate_type_number_to_die (type, array_die);
18372
18373   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18374   if (is_fortran ()
18375       && info->ndimensions >= 2)
18376     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18377
18378   if (info->data_location)
18379     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18380                           info->base_decl);
18381   if (info->associated)
18382     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18383                           info->base_decl);
18384   if (info->allocated)
18385     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18386                           info->base_decl);
18387
18388   for (dim = 0; dim < info->ndimensions; dim++)
18389     {
18390       dw_die_ref subrange_die
18391         = new_die (DW_TAG_subrange_type, array_die, NULL);
18392
18393       if (info->dimen[dim].lower_bound)
18394         {
18395           /* If it is the default value, omit it.  */
18396           int dflt;
18397
18398           if (host_integerp (info->dimen[dim].lower_bound, 0)
18399               && (dflt = lower_bound_default ()) != -1
18400               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18401             ;
18402           else
18403             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18404                                   info->dimen[dim].lower_bound,
18405                                   info->base_decl);
18406         }
18407       if (info->dimen[dim].upper_bound)
18408         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18409                               info->dimen[dim].upper_bound,
18410                               info->base_decl);
18411       if (info->dimen[dim].stride)
18412         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18413                               info->dimen[dim].stride,
18414                               info->base_decl);
18415     }
18416
18417   gen_type_die (info->element_type, context_die);
18418   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18419
18420   if (get_AT (array_die, DW_AT_name))
18421     add_pubtype (type, array_die);
18422 }
18423
18424 #if 0
18425 static void
18426 gen_entry_point_die (tree decl, dw_die_ref context_die)
18427 {
18428   tree origin = decl_ultimate_origin (decl);
18429   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18430
18431   if (origin != NULL)
18432     add_abstract_origin_attribute (decl_die, origin);
18433   else
18434     {
18435       add_name_and_src_coords_attributes (decl_die, decl);
18436       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18437                           0, 0, context_die);
18438     }
18439
18440   if (DECL_ABSTRACT (decl))
18441     equate_decl_number_to_die (decl, decl_die);
18442   else
18443     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18444 }
18445 #endif
18446
18447 /* Walk through the list of incomplete types again, trying once more to
18448    emit full debugging info for them.  */
18449
18450 static void
18451 retry_incomplete_types (void)
18452 {
18453   int i;
18454
18455   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18456     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18457                                   DINFO_USAGE_DIR_USE))
18458       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18459 }
18460
18461 /* Determine what tag to use for a record type.  */
18462
18463 static enum dwarf_tag
18464 record_type_tag (tree type)
18465 {
18466   if (! lang_hooks.types.classify_record)
18467     return DW_TAG_structure_type;
18468
18469   switch (lang_hooks.types.classify_record (type))
18470     {
18471     case RECORD_IS_STRUCT:
18472       return DW_TAG_structure_type;
18473
18474     case RECORD_IS_CLASS:
18475       return DW_TAG_class_type;
18476
18477     case RECORD_IS_INTERFACE:
18478       if (dwarf_version >= 3 || !dwarf_strict)
18479         return DW_TAG_interface_type;
18480       return DW_TAG_structure_type;
18481
18482     default:
18483       gcc_unreachable ();
18484     }
18485 }
18486
18487 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18488    include all of the information about the enumeration values also. Each
18489    enumerated type name/value is listed as a child of the enumerated type
18490    DIE.  */
18491
18492 static dw_die_ref
18493 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18494 {
18495   dw_die_ref type_die = lookup_type_die (type);
18496
18497   if (type_die == NULL)
18498     {
18499       type_die = new_die (DW_TAG_enumeration_type,
18500                           scope_die_for (type, context_die), type);
18501       equate_type_number_to_die (type, type_die);
18502       add_name_attribute (type_die, type_tag (type));
18503       if (dwarf_version >= 4 || !dwarf_strict)
18504         {
18505           if (ENUM_IS_SCOPED (type))
18506             add_AT_flag (type_die, DW_AT_enum_class, 1);
18507           if (ENUM_IS_OPAQUE (type))
18508             add_AT_flag (type_die, DW_AT_declaration, 1);
18509         }
18510     }
18511   else if (! TYPE_SIZE (type))
18512     return type_die;
18513   else
18514     remove_AT (type_die, DW_AT_declaration);
18515
18516   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18517      given enum type is incomplete, do not generate the DW_AT_byte_size
18518      attribute or the DW_AT_element_list attribute.  */
18519   if (TYPE_SIZE (type))
18520     {
18521       tree link;
18522
18523       TREE_ASM_WRITTEN (type) = 1;
18524       add_byte_size_attribute (type_die, type);
18525       if (TYPE_STUB_DECL (type) != NULL_TREE)
18526         {
18527           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18528           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18529         }
18530
18531       /* If the first reference to this type was as the return type of an
18532          inline function, then it may not have a parent.  Fix this now.  */
18533       if (type_die->die_parent == NULL)
18534         add_child_die (scope_die_for (type, context_die), type_die);
18535
18536       for (link = TYPE_VALUES (type);
18537            link != NULL; link = TREE_CHAIN (link))
18538         {
18539           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18540           tree value = TREE_VALUE (link);
18541
18542           add_name_attribute (enum_die,
18543                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18544
18545           if (TREE_CODE (value) == CONST_DECL)
18546             value = DECL_INITIAL (value);
18547
18548           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18549             /* DWARF2 does not provide a way of indicating whether or
18550                not enumeration constants are signed or unsigned.  GDB
18551                always assumes the values are signed, so we output all
18552                values as if they were signed.  That means that
18553                enumeration constants with very large unsigned values
18554                will appear to have negative values in the debugger.  */
18555             add_AT_int (enum_die, DW_AT_const_value,
18556                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18557         }
18558     }
18559   else
18560     add_AT_flag (type_die, DW_AT_declaration, 1);
18561
18562   if (get_AT (type_die, DW_AT_name))
18563     add_pubtype (type, type_die);
18564
18565   return type_die;
18566 }
18567
18568 /* Generate a DIE to represent either a real live formal parameter decl or to
18569    represent just the type of some formal parameter position in some function
18570    type.
18571
18572    Note that this routine is a bit unusual because its argument may be a
18573    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18574    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18575    node.  If it's the former then this function is being called to output a
18576    DIE to represent a formal parameter object (or some inlining thereof).  If
18577    it's the latter, then this function is only being called to output a
18578    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18579    argument type of some subprogram type.
18580    If EMIT_NAME_P is true, name and source coordinate attributes
18581    are emitted.  */
18582
18583 static dw_die_ref
18584 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18585                           dw_die_ref context_die)
18586 {
18587   tree node_or_origin = node ? node : origin;
18588   tree ultimate_origin;
18589   dw_die_ref parm_die
18590     = new_die (DW_TAG_formal_parameter, context_die, node);
18591
18592   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18593     {
18594     case tcc_declaration:
18595       ultimate_origin = decl_ultimate_origin (node_or_origin);
18596       if (node || ultimate_origin)
18597         origin = ultimate_origin;
18598       if (origin != NULL)
18599         add_abstract_origin_attribute (parm_die, origin);
18600       else if (emit_name_p)
18601         add_name_and_src_coords_attributes (parm_die, node);
18602       if (origin == NULL
18603           || (! DECL_ABSTRACT (node_or_origin)
18604               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18605                                            decl_function_context
18606                                                             (node_or_origin))))
18607         {
18608           tree type = TREE_TYPE (node_or_origin);
18609           if (decl_by_reference_p (node_or_origin))
18610             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18611                                 context_die);
18612           else
18613             add_type_attribute (parm_die, type,
18614                                 TREE_READONLY (node_or_origin),
18615                                 TREE_THIS_VOLATILE (node_or_origin),
18616                                 context_die);
18617         }
18618       if (origin == NULL && DECL_ARTIFICIAL (node))
18619         add_AT_flag (parm_die, DW_AT_artificial, 1);
18620
18621       if (node && node != origin)
18622         equate_decl_number_to_die (node, parm_die);
18623       if (! DECL_ABSTRACT (node_or_origin))
18624         add_location_or_const_value_attribute (parm_die, node_or_origin,
18625                                                DW_AT_location);
18626
18627       break;
18628
18629     case tcc_type:
18630       /* We were called with some kind of a ..._TYPE node.  */
18631       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18632       break;
18633
18634     default:
18635       gcc_unreachable ();
18636     }
18637
18638   return parm_die;
18639 }
18640
18641 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18642    children DW_TAG_formal_parameter DIEs representing the arguments of the
18643    parameter pack.
18644
18645    PARM_PACK must be a function parameter pack.
18646    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18647    must point to the subsequent arguments of the function PACK_ARG belongs to.
18648    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18649    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18650    following the last one for which a DIE was generated.  */
18651
18652 static dw_die_ref
18653 gen_formal_parameter_pack_die  (tree parm_pack,
18654                                 tree pack_arg,
18655                                 dw_die_ref subr_die,
18656                                 tree *next_arg)
18657 {
18658   tree arg;
18659   dw_die_ref parm_pack_die;
18660
18661   gcc_assert (parm_pack
18662               && lang_hooks.function_parameter_pack_p (parm_pack)
18663               && subr_die);
18664
18665   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18666   add_src_coords_attributes (parm_pack_die, parm_pack);
18667
18668   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18669     {
18670       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18671                                                                  parm_pack))
18672         break;
18673       gen_formal_parameter_die (arg, NULL,
18674                                 false /* Don't emit name attribute.  */,
18675                                 parm_pack_die);
18676     }
18677   if (next_arg)
18678     *next_arg = arg;
18679   return parm_pack_die;
18680 }
18681
18682 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18683    at the end of an (ANSI prototyped) formal parameters list.  */
18684
18685 static void
18686 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18687 {
18688   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18689 }
18690
18691 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18692    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18693    parameters as specified in some function type specification (except for
18694    those which appear as part of a function *definition*).  */
18695
18696 static void
18697 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18698 {
18699   tree link;
18700   tree formal_type = NULL;
18701   tree first_parm_type;
18702   tree arg;
18703
18704   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18705     {
18706       arg = DECL_ARGUMENTS (function_or_method_type);
18707       function_or_method_type = TREE_TYPE (function_or_method_type);
18708     }
18709   else
18710     arg = NULL_TREE;
18711
18712   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18713
18714   /* Make our first pass over the list of formal parameter types and output a
18715      DW_TAG_formal_parameter DIE for each one.  */
18716   for (link = first_parm_type; link; )
18717     {
18718       dw_die_ref parm_die;
18719
18720       formal_type = TREE_VALUE (link);
18721       if (formal_type == void_type_node)
18722         break;
18723
18724       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18725       parm_die = gen_formal_parameter_die (formal_type, NULL,
18726                                            true /* Emit name attribute.  */,
18727                                            context_die);
18728       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18729           && link == first_parm_type)
18730         {
18731           add_AT_flag (parm_die, DW_AT_artificial, 1);
18732           if (dwarf_version >= 3 || !dwarf_strict)
18733             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18734         }
18735       else if (arg && DECL_ARTIFICIAL (arg))
18736         add_AT_flag (parm_die, DW_AT_artificial, 1);
18737
18738       link = TREE_CHAIN (link);
18739       if (arg)
18740         arg = DECL_CHAIN (arg);
18741     }
18742
18743   /* If this function type has an ellipsis, add a
18744      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18745   if (formal_type != void_type_node)
18746     gen_unspecified_parameters_die (function_or_method_type, context_die);
18747
18748   /* Make our second (and final) pass over the list of formal parameter types
18749      and output DIEs to represent those types (as necessary).  */
18750   for (link = TYPE_ARG_TYPES (function_or_method_type);
18751        link && TREE_VALUE (link);
18752        link = TREE_CHAIN (link))
18753     gen_type_die (TREE_VALUE (link), context_die);
18754 }
18755
18756 /* We want to generate the DIE for TYPE so that we can generate the
18757    die for MEMBER, which has been defined; we will need to refer back
18758    to the member declaration nested within TYPE.  If we're trying to
18759    generate minimal debug info for TYPE, processing TYPE won't do the
18760    trick; we need to attach the member declaration by hand.  */
18761
18762 static void
18763 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18764 {
18765   gen_type_die (type, context_die);
18766
18767   /* If we're trying to avoid duplicate debug info, we may not have
18768      emitted the member decl for this function.  Emit it now.  */
18769   if (TYPE_STUB_DECL (type)
18770       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18771       && ! lookup_decl_die (member))
18772     {
18773       dw_die_ref type_die;
18774       gcc_assert (!decl_ultimate_origin (member));
18775
18776       push_decl_scope (type);
18777       type_die = lookup_type_die_strip_naming_typedef (type);
18778       if (TREE_CODE (member) == FUNCTION_DECL)
18779         gen_subprogram_die (member, type_die);
18780       else if (TREE_CODE (member) == FIELD_DECL)
18781         {
18782           /* Ignore the nameless fields that are used to skip bits but handle
18783              C++ anonymous unions and structs.  */
18784           if (DECL_NAME (member) != NULL_TREE
18785               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18786               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18787             {
18788               gen_type_die (member_declared_type (member), type_die);
18789               gen_field_die (member, type_die);
18790             }
18791         }
18792       else
18793         gen_variable_die (member, NULL_TREE, type_die);
18794
18795       pop_decl_scope ();
18796     }
18797 }
18798
18799 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18800    may later generate inlined and/or out-of-line instances of.  */
18801
18802 static void
18803 dwarf2out_abstract_function (tree decl)
18804 {
18805   dw_die_ref old_die;
18806   tree save_fn;
18807   tree context;
18808   int was_abstract;
18809   htab_t old_decl_loc_table;
18810   int old_call_site_count, old_tail_call_site_count;
18811   struct call_arg_loc_node *old_call_arg_locations;
18812
18813   /* Make sure we have the actual abstract inline, not a clone.  */
18814   decl = DECL_ORIGIN (decl);
18815
18816   old_die = lookup_decl_die (decl);
18817   if (old_die && get_AT (old_die, DW_AT_inline))
18818     /* We've already generated the abstract instance.  */
18819     return;
18820
18821   /* We can be called while recursively when seeing block defining inlined subroutine
18822      DIE.  Be sure to not clobber the outer location table nor use it or we would
18823      get locations in abstract instantces.  */
18824   old_decl_loc_table = decl_loc_table;
18825   decl_loc_table = NULL;
18826   old_call_arg_locations = call_arg_locations;
18827   call_arg_locations = NULL;
18828   old_call_site_count = call_site_count;
18829   call_site_count = -1;
18830   old_tail_call_site_count = tail_call_site_count;
18831   tail_call_site_count = -1;
18832
18833   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18834      we don't get confused by DECL_ABSTRACT.  */
18835   if (debug_info_level > DINFO_LEVEL_TERSE)
18836     {
18837       context = decl_class_context (decl);
18838       if (context)
18839         gen_type_die_for_member
18840           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18841     }
18842
18843   /* Pretend we've just finished compiling this function.  */
18844   save_fn = current_function_decl;
18845   current_function_decl = decl;
18846   push_cfun (DECL_STRUCT_FUNCTION (decl));
18847
18848   was_abstract = DECL_ABSTRACT (decl);
18849   set_decl_abstract_flags (decl, 1);
18850   dwarf2out_decl (decl);
18851   if (! was_abstract)
18852     set_decl_abstract_flags (decl, 0);
18853
18854   current_function_decl = save_fn;
18855   decl_loc_table = old_decl_loc_table;
18856   call_arg_locations = old_call_arg_locations;
18857   call_site_count = old_call_site_count;
18858   tail_call_site_count = old_tail_call_site_count;
18859   pop_cfun ();
18860 }
18861
18862 /* Helper function of premark_used_types() which gets called through
18863    htab_traverse.
18864
18865    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18866    marked as unused by prune_unused_types.  */
18867
18868 static int
18869 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18870 {
18871   tree type;
18872   dw_die_ref die;
18873
18874   type = (tree) *slot;
18875   die = lookup_type_die (type);
18876   if (die != NULL)
18877     die->die_perennial_p = 1;
18878   return 1;
18879 }
18880
18881 /* Helper function of premark_types_used_by_global_vars which gets called
18882    through htab_traverse.
18883
18884    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18885    marked as unused by prune_unused_types. The DIE of the type is marked
18886    only if the global variable using the type will actually be emitted.  */
18887
18888 static int
18889 premark_types_used_by_global_vars_helper (void **slot,
18890                                           void *data ATTRIBUTE_UNUSED)
18891 {
18892   struct types_used_by_vars_entry *entry;
18893   dw_die_ref die;
18894
18895   entry = (struct types_used_by_vars_entry *) *slot;
18896   gcc_assert (entry->type != NULL
18897               && entry->var_decl != NULL);
18898   die = lookup_type_die (entry->type);
18899   if (die)
18900     {
18901       /* Ask cgraph if the global variable really is to be emitted.
18902          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18903       struct varpool_node *node = varpool_get_node (entry->var_decl);
18904       if (node && node->needed)
18905         {
18906           die->die_perennial_p = 1;
18907           /* Keep the parent DIEs as well.  */
18908           while ((die = die->die_parent) && die->die_perennial_p == 0)
18909             die->die_perennial_p = 1;
18910         }
18911     }
18912   return 1;
18913 }
18914
18915 /* Mark all members of used_types_hash as perennial.  */
18916
18917 static void
18918 premark_used_types (void)
18919 {
18920   if (cfun && cfun->used_types_hash)
18921     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18922 }
18923
18924 /* Mark all members of types_used_by_vars_entry as perennial.  */
18925
18926 static void
18927 premark_types_used_by_global_vars (void)
18928 {
18929   if (types_used_by_vars_hash)
18930     htab_traverse (types_used_by_vars_hash,
18931                    premark_types_used_by_global_vars_helper, NULL);
18932 }
18933
18934 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18935    for CA_LOC call arg loc node.  */
18936
18937 static dw_die_ref
18938 gen_call_site_die (tree decl, dw_die_ref subr_die,
18939                    struct call_arg_loc_node *ca_loc)
18940 {
18941   dw_die_ref stmt_die = NULL, die;
18942   tree block = ca_loc->block;
18943
18944   while (block
18945          && block != DECL_INITIAL (decl)
18946          && TREE_CODE (block) == BLOCK)
18947     {
18948       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
18949         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
18950       if (stmt_die)
18951         break;
18952       block = BLOCK_SUPERCONTEXT (block);
18953     }
18954   if (stmt_die == NULL)
18955     stmt_die = subr_die;
18956   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18957   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18958   if (ca_loc->tail_call_p)
18959     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18960   if (ca_loc->symbol_ref)
18961     {
18962       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18963       if (tdie)
18964         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18965       else
18966         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
18967     }
18968   return die;
18969 }
18970
18971 /* Generate a DIE to represent a declared function (either file-scope or
18972    block-local).  */
18973
18974 static void
18975 gen_subprogram_die (tree decl, dw_die_ref context_die)
18976 {
18977   tree origin = decl_ultimate_origin (decl);
18978   dw_die_ref subr_die;
18979   tree outer_scope;
18980   dw_die_ref old_die = lookup_decl_die (decl);
18981   int declaration = (current_function_decl != decl
18982                      || class_or_namespace_scope_p (context_die));
18983
18984   premark_used_types ();
18985
18986   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18987      started to generate the abstract instance of an inline, decided to output
18988      its containing class, and proceeded to emit the declaration of the inline
18989      from the member list for the class.  If so, DECLARATION takes priority;
18990      we'll get back to the abstract instance when done with the class.  */
18991
18992   /* The class-scope declaration DIE must be the primary DIE.  */
18993   if (origin && declaration && class_or_namespace_scope_p (context_die))
18994     {
18995       origin = NULL;
18996       gcc_assert (!old_die);
18997     }
18998
18999   /* Now that the C++ front end lazily declares artificial member fns, we
19000      might need to retrofit the declaration into its class.  */
19001   if (!declaration && !origin && !old_die
19002       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19003       && !class_or_namespace_scope_p (context_die)
19004       && debug_info_level > DINFO_LEVEL_TERSE)
19005     old_die = force_decl_die (decl);
19006
19007   if (origin != NULL)
19008     {
19009       gcc_assert (!declaration || local_scope_p (context_die));
19010
19011       /* Fixup die_parent for the abstract instance of a nested
19012          inline function.  */
19013       if (old_die && old_die->die_parent == NULL)
19014         add_child_die (context_die, old_die);
19015
19016       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19017       add_abstract_origin_attribute (subr_die, origin);
19018     }
19019   else if (old_die)
19020     {
19021       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19022       struct dwarf_file_data * file_index = lookup_filename (s.file);
19023
19024       if (!get_AT_flag (old_die, DW_AT_declaration)
19025           /* We can have a normal definition following an inline one in the
19026              case of redefinition of GNU C extern inlines.
19027              It seems reasonable to use AT_specification in this case.  */
19028           && !get_AT (old_die, DW_AT_inline))
19029         {
19030           /* Detect and ignore this case, where we are trying to output
19031              something we have already output.  */
19032           return;
19033         }
19034
19035       /* If the definition comes from the same place as the declaration,
19036          maybe use the old DIE.  We always want the DIE for this function
19037          that has the *_pc attributes to be under comp_unit_die so the
19038          debugger can find it.  We also need to do this for abstract
19039          instances of inlines, since the spec requires the out-of-line copy
19040          to have the same parent.  For local class methods, this doesn't
19041          apply; we just use the old DIE.  */
19042       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19043           && (DECL_ARTIFICIAL (decl)
19044               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19045                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19046                       == (unsigned) s.line))))
19047         {
19048           subr_die = old_die;
19049
19050           /* Clear out the declaration attribute and the formal parameters.
19051              Do not remove all children, because it is possible that this
19052              declaration die was forced using force_decl_die(). In such
19053              cases die that forced declaration die (e.g. TAG_imported_module)
19054              is one of the children that we do not want to remove.  */
19055           remove_AT (subr_die, DW_AT_declaration);
19056           remove_AT (subr_die, DW_AT_object_pointer);
19057           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19058         }
19059       else
19060         {
19061           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19062           add_AT_specification (subr_die, old_die);
19063           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19064             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19065           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19066             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19067         }
19068     }
19069   else
19070     {
19071       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19072
19073       if (TREE_PUBLIC (decl))
19074         add_AT_flag (subr_die, DW_AT_external, 1);
19075
19076       add_name_and_src_coords_attributes (subr_die, decl);
19077       if (debug_info_level > DINFO_LEVEL_TERSE)
19078         {
19079           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19080           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19081                               0, 0, context_die);
19082         }
19083
19084       add_pure_or_virtual_attribute (subr_die, decl);
19085       if (DECL_ARTIFICIAL (decl))
19086         add_AT_flag (subr_die, DW_AT_artificial, 1);
19087
19088       add_accessibility_attribute (subr_die, decl);
19089     }
19090
19091   if (declaration)
19092     {
19093       if (!old_die || !get_AT (old_die, DW_AT_inline))
19094         {
19095           add_AT_flag (subr_die, DW_AT_declaration, 1);
19096
19097           /* If this is an explicit function declaration then generate
19098              a DW_AT_explicit attribute.  */
19099           if (lang_hooks.decls.function_decl_explicit_p (decl)
19100               && (dwarf_version >= 3 || !dwarf_strict))
19101             add_AT_flag (subr_die, DW_AT_explicit, 1);
19102
19103           /* The first time we see a member function, it is in the context of
19104              the class to which it belongs.  We make sure of this by emitting
19105              the class first.  The next time is the definition, which is
19106              handled above.  The two may come from the same source text.
19107
19108              Note that force_decl_die() forces function declaration die. It is
19109              later reused to represent definition.  */
19110           equate_decl_number_to_die (decl, subr_die);
19111         }
19112     }
19113   else if (DECL_ABSTRACT (decl))
19114     {
19115       if (DECL_DECLARED_INLINE_P (decl))
19116         {
19117           if (cgraph_function_possibly_inlined_p (decl))
19118             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19119           else
19120             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19121         }
19122       else
19123         {
19124           if (cgraph_function_possibly_inlined_p (decl))
19125             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19126           else
19127             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19128         }
19129
19130       if (DECL_DECLARED_INLINE_P (decl)
19131           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19132         add_AT_flag (subr_die, DW_AT_artificial, 1);
19133
19134       equate_decl_number_to_die (decl, subr_die);
19135     }
19136   else if (!DECL_EXTERNAL (decl))
19137     {
19138       HOST_WIDE_INT cfa_fb_offset;
19139
19140       if (!old_die || !get_AT (old_die, DW_AT_inline))
19141         equate_decl_number_to_die (decl, subr_die);
19142
19143       if (!flag_reorder_blocks_and_partition)
19144         {
19145           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19146           if (fde->dw_fde_begin)
19147             {
19148               /* We have already generated the labels.  */
19149               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19150               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19151             }
19152           else
19153             {
19154               /* Create start/end labels and add the range.  */
19155               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19156               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19157                                            current_function_funcdef_no);
19158               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19159               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19160                                            current_function_funcdef_no);
19161               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19162             }
19163
19164 #if VMS_DEBUGGING_INFO
19165       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19166          Section 2.3 Prologue and Epilogue Attributes:
19167          When a breakpoint is set on entry to a function, it is generally
19168          desirable for execution to be suspended, not on the very first
19169          instruction of the function, but rather at a point after the
19170          function's frame has been set up, after any language defined local
19171          declaration processing has been completed, and before execution of
19172          the first statement of the function begins. Debuggers generally
19173          cannot properly determine where this point is.  Similarly for a
19174          breakpoint set on exit from a function. The prologue and epilogue
19175          attributes allow a compiler to communicate the location(s) to use.  */
19176
19177       {
19178         if (fde->dw_fde_vms_end_prologue)
19179           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19180             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19181
19182         if (fde->dw_fde_vms_begin_epilogue)
19183           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19184             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19185       }
19186 #endif
19187
19188           add_pubname (decl, subr_die);
19189           add_arange (decl, subr_die);
19190         }
19191       else
19192         {  /* Generate pubnames entries for the split function code
19193               ranges.  */
19194           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19195
19196           if (fde->dw_fde_switched_sections)
19197             {
19198               if (dwarf_version >= 3 || !dwarf_strict)
19199                 {
19200                   /* We should use ranges for non-contiguous code section 
19201                      addresses.  Use the actual code range for the initial
19202                      section, since the HOT/COLD labels might precede an 
19203                      alignment offset.  */
19204                   bool range_list_added = false;
19205                   if (fde->in_std_section)
19206                     {
19207                       add_ranges_by_labels (subr_die,
19208                                             fde->dw_fde_begin,
19209                                             fde->dw_fde_end,
19210                                             &range_list_added);
19211                       add_ranges_by_labels (subr_die,
19212                                             fde->dw_fde_unlikely_section_label,
19213                                             fde->dw_fde_unlikely_section_end_label,
19214                                             &range_list_added);
19215                     }
19216                   else
19217                     {
19218                       add_ranges_by_labels (subr_die,
19219                                             fde->dw_fde_begin,
19220                                             fde->dw_fde_end,
19221                                             &range_list_added);
19222                       add_ranges_by_labels (subr_die,
19223                                             fde->dw_fde_hot_section_label,
19224                                             fde->dw_fde_hot_section_end_label,
19225                                             &range_list_added);
19226                     }
19227                   add_pubname (decl, subr_die);
19228                   if (range_list_added)
19229                     add_ranges (NULL);
19230                 }
19231               else
19232                 {
19233                   /* There is no real support in DW2 for this .. so we make
19234                      a work-around.  First, emit the pub name for the segment
19235                      containing the function label.  Then make and emit a
19236                      simplified subprogram DIE for the second segment with the
19237                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19238                      linkage name for the second die so that gdb will find both
19239                      sections when given "b foo".  */
19240                   const char *name = NULL;
19241                   tree decl_name = DECL_NAME (decl);
19242                   dw_die_ref seg_die;
19243
19244                   /* Do the 'primary' section.   */
19245                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19246                                  fde->dw_fde_begin);
19247                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19248                                  fde->dw_fde_end);
19249                   /* Add it.   */
19250                   add_pubname (decl, subr_die);
19251                   add_arange (decl, subr_die);
19252
19253                   /* Build a minimal DIE for the secondary section.  */
19254                   seg_die = new_die (DW_TAG_subprogram,
19255                                      subr_die->die_parent, decl);
19256
19257                   if (TREE_PUBLIC (decl))
19258                     add_AT_flag (seg_die, DW_AT_external, 1);
19259
19260                   if (decl_name != NULL 
19261                       && IDENTIFIER_POINTER (decl_name) != NULL)
19262                     {
19263                       name = dwarf2_name (decl, 1);
19264                       if (! DECL_ARTIFICIAL (decl))
19265                         add_src_coords_attributes (seg_die, decl);
19266
19267                       add_linkage_name (seg_die, decl);
19268                     }
19269                   gcc_assert (name!=NULL);
19270                   add_pure_or_virtual_attribute (seg_die, decl);
19271                   if (DECL_ARTIFICIAL (decl))
19272                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19273
19274                   if (fde->in_std_section)
19275                     {
19276                       name = concat ("__cold_sect_of_", name, NULL); 
19277                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19278                                      fde->dw_fde_unlikely_section_label);
19279                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19280                                      fde->dw_fde_unlikely_section_end_label); 
19281                     }
19282                   else 
19283                     {
19284                       name = concat ("__hot_sect_of_", name, NULL); 
19285                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19286                                      fde->dw_fde_hot_section_label);
19287                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19288                                      fde->dw_fde_hot_section_end_label); 
19289                     }
19290                   add_name_attribute (seg_die, name);
19291                   add_pubname_string (name, seg_die);
19292                   add_arange (decl, seg_die);
19293                 }
19294             }
19295           else
19296             {
19297               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19298               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19299               add_pubname (decl, subr_die);
19300               add_arange (decl, subr_die);
19301             }
19302         }
19303
19304 #ifdef MIPS_DEBUGGING_INFO
19305       /* Add a reference to the FDE for this routine.  */
19306       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19307 #endif
19308
19309       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19310
19311       /* We define the "frame base" as the function's CFA.  This is more
19312          convenient for several reasons: (1) It's stable across the prologue
19313          and epilogue, which makes it better than just a frame pointer,
19314          (2) With dwarf3, there exists a one-byte encoding that allows us
19315          to reference the .debug_frame data by proxy, but failing that,
19316          (3) We can at least reuse the code inspection and interpretation
19317          code that determines the CFA position at various points in the
19318          function.  */
19319       if (dwarf_version >= 3)
19320         {
19321           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19322           add_AT_loc (subr_die, DW_AT_frame_base, op);
19323         }
19324       else
19325         {
19326           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19327           if (list->dw_loc_next)
19328             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19329           else
19330             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19331         }
19332
19333       /* Compute a displacement from the "steady-state frame pointer" to
19334          the CFA.  The former is what all stack slots and argument slots
19335          will reference in the rtl; the later is what we've told the
19336          debugger about.  We'll need to adjust all frame_base references
19337          by this displacement.  */
19338       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19339
19340       if (cfun->static_chain_decl)
19341         add_AT_location_description (subr_die, DW_AT_static_link,
19342                  loc_list_from_tree (cfun->static_chain_decl, 2));
19343     }
19344
19345   /* Generate child dies for template paramaters.  */
19346   if (debug_info_level > DINFO_LEVEL_TERSE)
19347     gen_generic_params_dies (decl);
19348
19349   /* Now output descriptions of the arguments for this function. This gets
19350      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19351      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19352      `...' at the end of the formal parameter list.  In order to find out if
19353      there was a trailing ellipsis or not, we must instead look at the type
19354      associated with the FUNCTION_DECL.  This will be a node of type
19355      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19356      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19357      an ellipsis at the end.  */
19358
19359   /* In the case where we are describing a mere function declaration, all we
19360      need to do here (and all we *can* do here) is to describe the *types* of
19361      its formal parameters.  */
19362   if (debug_info_level <= DINFO_LEVEL_TERSE)
19363     ;
19364   else if (declaration)
19365     gen_formal_types_die (decl, subr_die);
19366   else
19367     {
19368       /* Generate DIEs to represent all known formal parameters.  */
19369       tree parm = DECL_ARGUMENTS (decl);
19370       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19371       tree generic_decl_parm = generic_decl
19372                                 ? DECL_ARGUMENTS (generic_decl)
19373                                 : NULL;
19374
19375       /* Now we want to walk the list of parameters of the function and
19376          emit their relevant DIEs.
19377
19378          We consider the case of DECL being an instance of a generic function
19379          as well as it being a normal function.
19380
19381          If DECL is an instance of a generic function we walk the
19382          parameters of the generic function declaration _and_ the parameters of
19383          DECL itself. This is useful because we want to emit specific DIEs for
19384          function parameter packs and those are declared as part of the
19385          generic function declaration. In that particular case,
19386          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19387          That DIE has children DIEs representing the set of arguments
19388          of the pack. Note that the set of pack arguments can be empty.
19389          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19390          children DIE.
19391
19392          Otherwise, we just consider the parameters of DECL.  */
19393       while (generic_decl_parm || parm)
19394         {
19395           if (generic_decl_parm
19396               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19397             gen_formal_parameter_pack_die (generic_decl_parm,
19398                                            parm, subr_die,
19399                                            &parm);
19400           else if (parm)
19401             {
19402               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19403
19404               if (parm == DECL_ARGUMENTS (decl)
19405                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19406                   && parm_die
19407                   && (dwarf_version >= 3 || !dwarf_strict))
19408                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19409
19410               parm = DECL_CHAIN (parm);
19411             }
19412
19413           if (generic_decl_parm)
19414             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19415         }
19416
19417       /* Decide whether we need an unspecified_parameters DIE at the end.
19418          There are 2 more cases to do this for: 1) the ansi ... declaration -
19419          this is detectable when the end of the arg list is not a
19420          void_type_node 2) an unprototyped function declaration (not a
19421          definition).  This just means that we have no info about the
19422          parameters at all.  */
19423       if (prototype_p (TREE_TYPE (decl)))
19424         {
19425           /* This is the prototyped case, check for....  */
19426           if (stdarg_p (TREE_TYPE (decl)))
19427             gen_unspecified_parameters_die (decl, subr_die);
19428         }
19429       else if (DECL_INITIAL (decl) == NULL_TREE)
19430         gen_unspecified_parameters_die (decl, subr_die);
19431     }
19432
19433   /* Output Dwarf info for all of the stuff within the body of the function
19434      (if it has one - it may be just a declaration).  */
19435   outer_scope = DECL_INITIAL (decl);
19436
19437   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19438      a function.  This BLOCK actually represents the outermost binding contour
19439      for the function, i.e. the contour in which the function's formal
19440      parameters and labels get declared. Curiously, it appears that the front
19441      end doesn't actually put the PARM_DECL nodes for the current function onto
19442      the BLOCK_VARS list for this outer scope, but are strung off of the
19443      DECL_ARGUMENTS list for the function instead.
19444
19445      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19446      the LABEL_DECL nodes for the function however, and we output DWARF info
19447      for those in decls_for_scope.  Just within the `outer_scope' there will be
19448      a BLOCK node representing the function's outermost pair of curly braces,
19449      and any blocks used for the base and member initializers of a C++
19450      constructor function.  */
19451   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19452     {
19453       int call_site_note_count = 0;
19454       int tail_call_site_note_count = 0;
19455
19456       /* Emit a DW_TAG_variable DIE for a named return value.  */
19457       if (DECL_NAME (DECL_RESULT (decl)))
19458         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19459
19460       current_function_has_inlines = 0;
19461       decls_for_scope (outer_scope, subr_die, 0);
19462
19463       if (call_arg_locations && !dwarf_strict)
19464         {
19465           struct call_arg_loc_node *ca_loc;
19466           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19467             {
19468               dw_die_ref die = NULL;
19469               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19470               rtx arg, next_arg;
19471
19472               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19473                    arg; arg = next_arg)
19474                 {
19475                   dw_loc_descr_ref reg, val;
19476                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19477                   dw_die_ref cdie;
19478
19479                   next_arg = XEXP (arg, 1);
19480                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19481                       && next_arg
19482                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19483                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19484                       && REGNO (XEXP (XEXP (arg, 0), 0))
19485                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19486                     next_arg = XEXP (next_arg, 1);
19487                   if (mode == VOIDmode)
19488                     {
19489                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19490                       if (mode == VOIDmode)
19491                         mode = GET_MODE (XEXP (arg, 0));
19492                     }
19493                   if (GET_MODE_CLASS (mode) != MODE_INT
19494                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19495                     continue;
19496                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19497                     {
19498                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19499                       tloc = XEXP (XEXP (arg, 0), 1);
19500                       continue;
19501                     }
19502                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19503                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19504                     {
19505                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19506                       tlocc = XEXP (XEXP (arg, 0), 1);
19507                       continue;
19508                     }
19509                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19510                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19511                                               VAR_INIT_STATUS_INITIALIZED);
19512                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19513                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19514                                                           0), 0), mode,
19515                                               VAR_INIT_STATUS_INITIALIZED);
19516                   else
19517                     continue;
19518                   if (reg == NULL)
19519                     continue;
19520                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19521                                             VAR_INIT_STATUS_INITIALIZED);
19522                   if (val == NULL)
19523                     continue;
19524                   if (die == NULL)
19525                     die = gen_call_site_die (decl, subr_die, ca_loc);
19526                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19527                                   NULL_TREE);           
19528                   add_AT_loc (cdie, DW_AT_location, reg);
19529                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19530                   if (next_arg != XEXP (arg, 1))
19531                     {
19532                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19533                                                             0), 1), VOIDmode,
19534                                                 VAR_INIT_STATUS_INITIALIZED);
19535                       if (val != NULL)
19536                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19537                     }
19538                 }
19539               if (die == NULL
19540                   && (ca_loc->symbol_ref || tloc))
19541                 die = gen_call_site_die (decl, subr_die, ca_loc);
19542               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19543                 {
19544                   dw_loc_descr_ref tval = NULL;
19545
19546                   if (tloc != NULL_RTX)
19547                     tval = mem_loc_descriptor (tloc, VOIDmode,
19548                                                VAR_INIT_STATUS_INITIALIZED);
19549                   if (tval)
19550                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19551                   else if (tlocc != NULL_RTX)
19552                     {
19553                       tval = mem_loc_descriptor (tlocc, VOIDmode,
19554                                                  VAR_INIT_STATUS_INITIALIZED);
19555                       if (tval)
19556                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19557                                     tval);
19558                     }
19559                 }
19560               if (die != NULL)
19561                 {
19562                   call_site_note_count++;
19563                   if (ca_loc->tail_call_p)
19564                     tail_call_site_note_count++;
19565                 }
19566             }
19567         }
19568       call_arg_locations = NULL;
19569       call_arg_loc_last = NULL;
19570       if (tail_call_site_count >= 0
19571           && tail_call_site_count == tail_call_site_note_count
19572           && !dwarf_strict)
19573         {
19574           if (call_site_count >= 0
19575               && call_site_count == call_site_note_count)
19576             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19577           else
19578             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19579         }
19580       call_site_count = -1;
19581       tail_call_site_count = -1;
19582     }
19583   /* Add the calling convention attribute if requested.  */
19584   add_calling_convention_attribute (subr_die, decl);
19585
19586 }
19587
19588 /* Returns a hash value for X (which really is a die_struct).  */
19589
19590 static hashval_t
19591 common_block_die_table_hash (const void *x)
19592 {
19593   const_dw_die_ref d = (const_dw_die_ref) x;
19594   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19595 }
19596
19597 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19598    as decl_id and die_parent of die_struct Y.  */
19599
19600 static int
19601 common_block_die_table_eq (const void *x, const void *y)
19602 {
19603   const_dw_die_ref d = (const_dw_die_ref) x;
19604   const_dw_die_ref e = (const_dw_die_ref) y;
19605   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19606 }
19607
19608 /* Generate a DIE to represent a declared data object.
19609    Either DECL or ORIGIN must be non-null.  */
19610
19611 static void
19612 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19613 {
19614   HOST_WIDE_INT off;
19615   tree com_decl;
19616   tree decl_or_origin = decl ? decl : origin;
19617   tree ultimate_origin;
19618   dw_die_ref var_die;
19619   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19620   dw_die_ref origin_die;
19621   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19622                       || class_or_namespace_scope_p (context_die));
19623   bool specialization_p = false;
19624
19625   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19626   if (decl || ultimate_origin)
19627     origin = ultimate_origin;
19628   com_decl = fortran_common (decl_or_origin, &off);
19629
19630   /* Symbol in common gets emitted as a child of the common block, in the form
19631      of a data member.  */
19632   if (com_decl)
19633     {
19634       dw_die_ref com_die;
19635       dw_loc_list_ref loc;
19636       die_node com_die_arg;
19637
19638       var_die = lookup_decl_die (decl_or_origin);
19639       if (var_die)
19640         {
19641           if (get_AT (var_die, DW_AT_location) == NULL)
19642             {
19643               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19644               if (loc)
19645                 {
19646                   if (off)
19647                     {
19648                       /* Optimize the common case.  */
19649                       if (single_element_loc_list_p (loc)
19650                           && loc->expr->dw_loc_opc == DW_OP_addr
19651                           && loc->expr->dw_loc_next == NULL
19652                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19653                              == SYMBOL_REF)
19654                         loc->expr->dw_loc_oprnd1.v.val_addr
19655                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19656                         else
19657                           loc_list_plus_const (loc, off);
19658                     }
19659                   add_AT_location_description (var_die, DW_AT_location, loc);
19660                   remove_AT (var_die, DW_AT_declaration);
19661                 }
19662             }
19663           return;
19664         }
19665
19666       if (common_block_die_table == NULL)
19667         common_block_die_table
19668           = htab_create_ggc (10, common_block_die_table_hash,
19669                              common_block_die_table_eq, NULL);
19670
19671       com_die_arg.decl_id = DECL_UID (com_decl);
19672       com_die_arg.die_parent = context_die;
19673       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19674       loc = loc_list_from_tree (com_decl, 2);
19675       if (com_die == NULL)
19676         {
19677           const char *cnam
19678             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19679           void **slot;
19680
19681           com_die = new_die (DW_TAG_common_block, context_die, decl);
19682           add_name_and_src_coords_attributes (com_die, com_decl);
19683           if (loc)
19684             {
19685               add_AT_location_description (com_die, DW_AT_location, loc);
19686               /* Avoid sharing the same loc descriptor between
19687                  DW_TAG_common_block and DW_TAG_variable.  */
19688               loc = loc_list_from_tree (com_decl, 2);
19689             }
19690           else if (DECL_EXTERNAL (decl))
19691             add_AT_flag (com_die, DW_AT_declaration, 1);
19692           add_pubname_string (cnam, com_die); /* ??? needed? */
19693           com_die->decl_id = DECL_UID (com_decl);
19694           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19695           *slot = (void *) com_die;
19696         }
19697       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19698         {
19699           add_AT_location_description (com_die, DW_AT_location, loc);
19700           loc = loc_list_from_tree (com_decl, 2);
19701           remove_AT (com_die, DW_AT_declaration);
19702         }
19703       var_die = new_die (DW_TAG_variable, com_die, decl);
19704       add_name_and_src_coords_attributes (var_die, decl);
19705       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19706                           TREE_THIS_VOLATILE (decl), context_die);
19707       add_AT_flag (var_die, DW_AT_external, 1);
19708       if (loc)
19709         {
19710           if (off)
19711             {
19712               /* Optimize the common case.  */
19713               if (single_element_loc_list_p (loc)
19714                   && loc->expr->dw_loc_opc == DW_OP_addr
19715                   && loc->expr->dw_loc_next == NULL
19716                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19717                 loc->expr->dw_loc_oprnd1.v.val_addr
19718                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19719               else
19720                 loc_list_plus_const (loc, off);
19721             }
19722           add_AT_location_description (var_die, DW_AT_location, loc);
19723         }
19724       else if (DECL_EXTERNAL (decl))
19725         add_AT_flag (var_die, DW_AT_declaration, 1);
19726       equate_decl_number_to_die (decl, var_die);
19727       return;
19728     }
19729
19730   /* If the compiler emitted a definition for the DECL declaration
19731      and if we already emitted a DIE for it, don't emit a second
19732      DIE for it again. Allow re-declarations of DECLs that are
19733      inside functions, though.  */
19734   if (old_die && declaration && !local_scope_p (context_die))
19735     return;
19736
19737   /* For static data members, the declaration in the class is supposed
19738      to have DW_TAG_member tag; the specification should still be
19739      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19740   if (declaration && class_scope_p (context_die))
19741     var_die = new_die (DW_TAG_member, context_die, decl);
19742   else
19743     var_die = new_die (DW_TAG_variable, context_die, decl);
19744
19745   origin_die = NULL;
19746   if (origin != NULL)
19747     origin_die = add_abstract_origin_attribute (var_die, origin);
19748
19749   /* Loop unrolling can create multiple blocks that refer to the same
19750      static variable, so we must test for the DW_AT_declaration flag.
19751
19752      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19753      copy decls and set the DECL_ABSTRACT flag on them instead of
19754      sharing them.
19755
19756      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19757
19758      ??? The declare_in_namespace support causes us to get two DIEs for one
19759      variable, both of which are declarations.  We want to avoid considering
19760      one to be a specification, so we must test that this DIE is not a
19761      declaration.  */
19762   else if (old_die && TREE_STATIC (decl) && ! declaration
19763            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19764     {
19765       /* This is a definition of a C++ class level static.  */
19766       add_AT_specification (var_die, old_die);
19767       specialization_p = true;
19768       if (DECL_NAME (decl))
19769         {
19770           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19771           struct dwarf_file_data * file_index = lookup_filename (s.file);
19772
19773           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19774             add_AT_file (var_die, DW_AT_decl_file, file_index);
19775
19776           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19777             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19778
19779           if (old_die->die_tag == DW_TAG_member)
19780             add_linkage_name (var_die, decl);
19781         }
19782     }
19783   else
19784     add_name_and_src_coords_attributes (var_die, decl);
19785
19786   if ((origin == NULL && !specialization_p)
19787       || (origin != NULL
19788           && !DECL_ABSTRACT (decl_or_origin)
19789           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19790                                        decl_function_context
19791                                                         (decl_or_origin))))
19792     {
19793       tree type = TREE_TYPE (decl_or_origin);
19794
19795       if (decl_by_reference_p (decl_or_origin))
19796         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19797       else
19798         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19799                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19800     }
19801
19802   if (origin == NULL && !specialization_p)
19803     {
19804       if (TREE_PUBLIC (decl))
19805         add_AT_flag (var_die, DW_AT_external, 1);
19806
19807       if (DECL_ARTIFICIAL (decl))
19808         add_AT_flag (var_die, DW_AT_artificial, 1);
19809
19810       add_accessibility_attribute (var_die, decl);
19811     }
19812
19813   if (declaration)
19814     add_AT_flag (var_die, DW_AT_declaration, 1);
19815
19816   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19817     equate_decl_number_to_die (decl, var_die);
19818
19819   if (! declaration
19820       && (! DECL_ABSTRACT (decl_or_origin)
19821           /* Local static vars are shared between all clones/inlines,
19822              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19823              already set.  */
19824           || (TREE_CODE (decl_or_origin) == VAR_DECL
19825               && TREE_STATIC (decl_or_origin)
19826               && DECL_RTL_SET_P (decl_or_origin)))
19827       /* When abstract origin already has DW_AT_location attribute, no need
19828          to add it again.  */
19829       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19830     {
19831       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19832           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19833         defer_location (decl_or_origin, var_die);
19834       else
19835         add_location_or_const_value_attribute (var_die,
19836                                                decl_or_origin,
19837                                                DW_AT_location);
19838       add_pubname (decl_or_origin, var_die);
19839     }
19840   else
19841     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19842 }
19843
19844 /* Generate a DIE to represent a named constant.  */
19845
19846 static void
19847 gen_const_die (tree decl, dw_die_ref context_die)
19848 {
19849   dw_die_ref const_die;
19850   tree type = TREE_TYPE (decl);
19851
19852   const_die = new_die (DW_TAG_constant, context_die, decl);
19853   add_name_and_src_coords_attributes (const_die, decl);
19854   add_type_attribute (const_die, type, 1, 0, context_die);
19855   if (TREE_PUBLIC (decl))
19856     add_AT_flag (const_die, DW_AT_external, 1);
19857   if (DECL_ARTIFICIAL (decl))
19858     add_AT_flag (const_die, DW_AT_artificial, 1);
19859   tree_add_const_value_attribute_for_decl (const_die, decl);
19860 }
19861
19862 /* Generate a DIE to represent a label identifier.  */
19863
19864 static void
19865 gen_label_die (tree decl, dw_die_ref context_die)
19866 {
19867   tree origin = decl_ultimate_origin (decl);
19868   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19869   rtx insn;
19870   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19871
19872   if (origin != NULL)
19873     add_abstract_origin_attribute (lbl_die, origin);
19874   else
19875     add_name_and_src_coords_attributes (lbl_die, decl);
19876
19877   if (DECL_ABSTRACT (decl))
19878     equate_decl_number_to_die (decl, lbl_die);
19879   else
19880     {
19881       insn = DECL_RTL_IF_SET (decl);
19882
19883       /* Deleted labels are programmer specified labels which have been
19884          eliminated because of various optimizations.  We still emit them
19885          here so that it is possible to put breakpoints on them.  */
19886       if (insn
19887           && (LABEL_P (insn)
19888               || ((NOTE_P (insn)
19889                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19890         {
19891           /* When optimization is enabled (via -O) some parts of the compiler
19892              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19893              represent source-level labels which were explicitly declared by
19894              the user.  This really shouldn't be happening though, so catch
19895              it if it ever does happen.  */
19896           gcc_assert (!INSN_DELETED_P (insn));
19897
19898           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19899           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19900         }
19901     }
19902 }
19903
19904 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19905    attributes to the DIE for a block STMT, to describe where the inlined
19906    function was called from.  This is similar to add_src_coords_attributes.  */
19907
19908 static inline void
19909 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19910 {
19911   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19912
19913   if (dwarf_version >= 3 || !dwarf_strict)
19914     {
19915       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19916       add_AT_unsigned (die, DW_AT_call_line, s.line);
19917     }
19918 }
19919
19920
19921 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19922    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19923
19924 static inline void
19925 add_high_low_attributes (tree stmt, dw_die_ref die)
19926 {
19927   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19928
19929   if (BLOCK_FRAGMENT_CHAIN (stmt)
19930       && (dwarf_version >= 3 || !dwarf_strict))
19931     {
19932       tree chain;
19933
19934       if (inlined_function_outer_scope_p (stmt))
19935         {
19936           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19937                                        BLOCK_NUMBER (stmt));
19938           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19939         }
19940
19941       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19942
19943       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19944       do
19945         {
19946           add_ranges (chain);
19947           chain = BLOCK_FRAGMENT_CHAIN (chain);
19948         }
19949       while (chain);
19950       add_ranges (NULL);
19951     }
19952   else
19953     {
19954       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19955                                    BLOCK_NUMBER (stmt));
19956       add_AT_lbl_id (die, DW_AT_low_pc, label);
19957       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19958                                    BLOCK_NUMBER (stmt));
19959       add_AT_lbl_id (die, DW_AT_high_pc, label);
19960     }
19961 }
19962
19963 /* Generate a DIE for a lexical block.  */
19964
19965 static void
19966 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19967 {
19968   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19969
19970   if (call_arg_locations)
19971     {
19972       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19973         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19974                                BLOCK_NUMBER (stmt) + 1);
19975       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
19976     }
19977
19978   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19979     add_high_low_attributes (stmt, stmt_die);
19980
19981   decls_for_scope (stmt, stmt_die, depth);
19982 }
19983
19984 /* Generate a DIE for an inlined subprogram.  */
19985
19986 static void
19987 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19988 {
19989   tree decl;
19990
19991   /* The instance of function that is effectively being inlined shall not
19992      be abstract.  */
19993   gcc_assert (! BLOCK_ABSTRACT (stmt));
19994
19995   decl = block_ultimate_origin (stmt);
19996
19997   /* Emit info for the abstract instance first, if we haven't yet.  We
19998      must emit this even if the block is abstract, otherwise when we
19999      emit the block below (or elsewhere), we may end up trying to emit
20000      a die whose origin die hasn't been emitted, and crashing.  */
20001   dwarf2out_abstract_function (decl);
20002
20003   if (! BLOCK_ABSTRACT (stmt))
20004     {
20005       dw_die_ref subr_die
20006         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20007
20008       if (call_arg_locations)
20009         {
20010           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20011             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20012                                    BLOCK_NUMBER (stmt) + 1);
20013           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20014         }
20015       add_abstract_origin_attribute (subr_die, decl);
20016       if (TREE_ASM_WRITTEN (stmt))
20017         add_high_low_attributes (stmt, subr_die);
20018       add_call_src_coords_attributes (stmt, subr_die);
20019
20020       decls_for_scope (stmt, subr_die, depth);
20021       current_function_has_inlines = 1;
20022     }
20023 }
20024
20025 /* Generate a DIE for a field in a record, or structure.  */
20026
20027 static void
20028 gen_field_die (tree decl, dw_die_ref context_die)
20029 {
20030   dw_die_ref decl_die;
20031
20032   if (TREE_TYPE (decl) == error_mark_node)
20033     return;
20034
20035   decl_die = new_die (DW_TAG_member, context_die, decl);
20036   add_name_and_src_coords_attributes (decl_die, decl);
20037   add_type_attribute (decl_die, member_declared_type (decl),
20038                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20039                       context_die);
20040
20041   if (DECL_BIT_FIELD_TYPE (decl))
20042     {
20043       add_byte_size_attribute (decl_die, decl);
20044       add_bit_size_attribute (decl_die, decl);
20045       add_bit_offset_attribute (decl_die, decl);
20046     }
20047
20048   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20049     add_data_member_location_attribute (decl_die, decl);
20050
20051   if (DECL_ARTIFICIAL (decl))
20052     add_AT_flag (decl_die, DW_AT_artificial, 1);
20053
20054   add_accessibility_attribute (decl_die, decl);
20055
20056   /* Equate decl number to die, so that we can look up this decl later on.  */
20057   equate_decl_number_to_die (decl, decl_die);
20058 }
20059
20060 #if 0
20061 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20062    Use modified_type_die instead.
20063    We keep this code here just in case these types of DIEs may be needed to
20064    represent certain things in other languages (e.g. Pascal) someday.  */
20065
20066 static void
20067 gen_pointer_type_die (tree type, dw_die_ref context_die)
20068 {
20069   dw_die_ref ptr_die
20070     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20071
20072   equate_type_number_to_die (type, ptr_die);
20073   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20074   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20075 }
20076
20077 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20078    Use modified_type_die instead.
20079    We keep this code here just in case these types of DIEs may be needed to
20080    represent certain things in other languages (e.g. Pascal) someday.  */
20081
20082 static void
20083 gen_reference_type_die (tree type, dw_die_ref context_die)
20084 {
20085   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20086
20087   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20088     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20089   else
20090     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20091
20092   equate_type_number_to_die (type, ref_die);
20093   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20094   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20095 }
20096 #endif
20097
20098 /* Generate a DIE for a pointer to a member type.  */
20099
20100 static void
20101 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20102 {
20103   dw_die_ref ptr_die
20104     = new_die (DW_TAG_ptr_to_member_type,
20105                scope_die_for (type, context_die), type);
20106
20107   equate_type_number_to_die (type, ptr_die);
20108   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20109                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20110   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20111 }
20112
20113 /* Generate the DIE for the compilation unit.  */
20114
20115 static dw_die_ref
20116 gen_compile_unit_die (const char *filename)
20117 {
20118   dw_die_ref die;
20119   char producer[250];
20120   const char *language_string = lang_hooks.name;
20121   int language;
20122
20123   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20124
20125   if (filename)
20126     {
20127       add_name_attribute (die, filename);
20128       /* Don't add cwd for <built-in>.  */
20129       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20130         add_comp_dir_attribute (die);
20131     }
20132
20133   sprintf (producer, "%s %s", language_string, version_string);
20134
20135 #ifdef MIPS_DEBUGGING_INFO
20136   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20137      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20138      not appear in the producer string, the debugger reaches the conclusion
20139      that the object file is stripped and has no debugging information.
20140      To get the MIPS/SGI debugger to believe that there is debugging
20141      information in the object file, we add a -g to the producer string.  */
20142   if (debug_info_level > DINFO_LEVEL_TERSE)
20143     strcat (producer, " -g");
20144 #endif
20145
20146   add_AT_string (die, DW_AT_producer, producer);
20147
20148   /* If our producer is LTO try to figure out a common language to use
20149      from the global list of translation units.  */
20150   if (strcmp (language_string, "GNU GIMPLE") == 0)
20151     {
20152       unsigned i;
20153       tree t;
20154       const char *common_lang = NULL;
20155
20156       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20157         {
20158           if (!TRANSLATION_UNIT_LANGUAGE (t))
20159             continue;
20160           if (!common_lang)
20161             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20162           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20163             ;
20164           else if (strncmp (common_lang, "GNU C", 5) == 0
20165                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20166             /* Mixing C and C++ is ok, use C++ in that case.  */
20167             common_lang = "GNU C++";
20168           else
20169             {
20170               /* Fall back to C.  */
20171               common_lang = NULL;
20172               break;
20173             }
20174         }
20175
20176       if (common_lang)
20177         language_string = common_lang;
20178     }
20179
20180   language = DW_LANG_C89;
20181   if (strcmp (language_string, "GNU C++") == 0)
20182     language = DW_LANG_C_plus_plus;
20183   else if (strcmp (language_string, "GNU F77") == 0)
20184     language = DW_LANG_Fortran77;
20185   else if (strcmp (language_string, "GNU Pascal") == 0)
20186     language = DW_LANG_Pascal83;
20187   else if (dwarf_version >= 3 || !dwarf_strict)
20188     {
20189       if (strcmp (language_string, "GNU Ada") == 0)
20190         language = DW_LANG_Ada95;
20191       else if (strcmp (language_string, "GNU Fortran") == 0)
20192         language = DW_LANG_Fortran95;
20193       else if (strcmp (language_string, "GNU Java") == 0)
20194         language = DW_LANG_Java;
20195       else if (strcmp (language_string, "GNU Objective-C") == 0)
20196         language = DW_LANG_ObjC;
20197       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20198         language = DW_LANG_ObjC_plus_plus;
20199     }
20200
20201   add_AT_unsigned (die, DW_AT_language, language);
20202
20203   switch (language)
20204     {
20205     case DW_LANG_Fortran77:
20206     case DW_LANG_Fortran90:
20207     case DW_LANG_Fortran95:
20208       /* Fortran has case insensitive identifiers and the front-end
20209          lowercases everything.  */
20210       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20211       break;
20212     default:
20213       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20214       break;
20215     }
20216   return die;
20217 }
20218
20219 /* Generate the DIE for a base class.  */
20220
20221 static void
20222 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20223 {
20224   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20225
20226   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20227   add_data_member_location_attribute (die, binfo);
20228
20229   if (BINFO_VIRTUAL_P (binfo))
20230     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20231
20232   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20233      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20234      the default has always been DW_ACCESS_private.  */
20235   if (access == access_public_node)
20236     {
20237       if (dwarf_version == 2
20238           || context_die->die_tag == DW_TAG_class_type)
20239       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20240     }
20241   else if (access == access_protected_node)
20242     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20243   else if (dwarf_version > 2
20244            && context_die->die_tag != DW_TAG_class_type)
20245     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20246 }
20247
20248 /* Generate a DIE for a class member.  */
20249
20250 static void
20251 gen_member_die (tree type, dw_die_ref context_die)
20252 {
20253   tree member;
20254   tree binfo = TYPE_BINFO (type);
20255   dw_die_ref child;
20256
20257   /* If this is not an incomplete type, output descriptions of each of its
20258      members. Note that as we output the DIEs necessary to represent the
20259      members of this record or union type, we will also be trying to output
20260      DIEs to represent the *types* of those members. However the `type'
20261      function (above) will specifically avoid generating type DIEs for member
20262      types *within* the list of member DIEs for this (containing) type except
20263      for those types (of members) which are explicitly marked as also being
20264      members of this (containing) type themselves.  The g++ front- end can
20265      force any given type to be treated as a member of some other (containing)
20266      type by setting the TYPE_CONTEXT of the given (member) type to point to
20267      the TREE node representing the appropriate (containing) type.  */
20268
20269   /* First output info about the base classes.  */
20270   if (binfo)
20271     {
20272       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20273       int i;
20274       tree base;
20275
20276       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20277         gen_inheritance_die (base,
20278                              (accesses ? VEC_index (tree, accesses, i)
20279                               : access_public_node), context_die);
20280     }
20281
20282   /* Now output info about the data members and type members.  */
20283   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20284     {
20285       /* If we thought we were generating minimal debug info for TYPE
20286          and then changed our minds, some of the member declarations
20287          may have already been defined.  Don't define them again, but
20288          do put them in the right order.  */
20289
20290       child = lookup_decl_die (member);
20291       if (child)
20292         splice_child_die (context_die, child);
20293       else
20294         gen_decl_die (member, NULL, context_die);
20295     }
20296
20297   /* Now output info about the function members (if any).  */
20298   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20299     {
20300       /* Don't include clones in the member list.  */
20301       if (DECL_ABSTRACT_ORIGIN (member))
20302         continue;
20303
20304       child = lookup_decl_die (member);
20305       if (child)
20306         splice_child_die (context_die, child);
20307       else
20308         gen_decl_die (member, NULL, context_die);
20309     }
20310 }
20311
20312 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20313    is set, we pretend that the type was never defined, so we only get the
20314    member DIEs needed by later specification DIEs.  */
20315
20316 static void
20317 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20318                                 enum debug_info_usage usage)
20319 {
20320   dw_die_ref type_die = lookup_type_die (type);
20321   dw_die_ref scope_die = 0;
20322   int nested = 0;
20323   int complete = (TYPE_SIZE (type)
20324                   && (! TYPE_STUB_DECL (type)
20325                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20326   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20327   complete = complete && should_emit_struct_debug (type, usage);
20328
20329   if (type_die && ! complete)
20330     return;
20331
20332   if (TYPE_CONTEXT (type) != NULL_TREE
20333       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20334           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20335     nested = 1;
20336
20337   scope_die = scope_die_for (type, context_die);
20338
20339   if (! type_die || (nested && is_cu_die (scope_die)))
20340     /* First occurrence of type or toplevel definition of nested class.  */
20341     {
20342       dw_die_ref old_die = type_die;
20343
20344       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20345                           ? record_type_tag (type) : DW_TAG_union_type,
20346                           scope_die, type);
20347       equate_type_number_to_die (type, type_die);
20348       if (old_die)
20349         add_AT_specification (type_die, old_die);
20350       else
20351         add_name_attribute (type_die, type_tag (type));
20352     }
20353   else
20354     remove_AT (type_die, DW_AT_declaration);
20355
20356   /* Generate child dies for template paramaters.  */
20357   if (debug_info_level > DINFO_LEVEL_TERSE
20358       && COMPLETE_TYPE_P (type))
20359     schedule_generic_params_dies_gen (type);
20360
20361   /* If this type has been completed, then give it a byte_size attribute and
20362      then give a list of members.  */
20363   if (complete && !ns_decl)
20364     {
20365       /* Prevent infinite recursion in cases where the type of some member of
20366          this type is expressed in terms of this type itself.  */
20367       TREE_ASM_WRITTEN (type) = 1;
20368       add_byte_size_attribute (type_die, type);
20369       if (TYPE_STUB_DECL (type) != NULL_TREE)
20370         {
20371           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20372           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20373         }
20374
20375       /* If the first reference to this type was as the return type of an
20376          inline function, then it may not have a parent.  Fix this now.  */
20377       if (type_die->die_parent == NULL)
20378         add_child_die (scope_die, type_die);
20379
20380       push_decl_scope (type);
20381       gen_member_die (type, type_die);
20382       pop_decl_scope ();
20383
20384       /* GNU extension: Record what type our vtable lives in.  */
20385       if (TYPE_VFIELD (type))
20386         {
20387           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20388
20389           gen_type_die (vtype, context_die);
20390           add_AT_die_ref (type_die, DW_AT_containing_type,
20391                           lookup_type_die (vtype));
20392         }
20393     }
20394   else
20395     {
20396       add_AT_flag (type_die, DW_AT_declaration, 1);
20397
20398       /* We don't need to do this for function-local types.  */
20399       if (TYPE_STUB_DECL (type)
20400           && ! decl_function_context (TYPE_STUB_DECL (type)))
20401         VEC_safe_push (tree, gc, incomplete_types, type);
20402     }
20403
20404   if (get_AT (type_die, DW_AT_name))
20405     add_pubtype (type, type_die);
20406 }
20407
20408 /* Generate a DIE for a subroutine _type_.  */
20409
20410 static void
20411 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20412 {
20413   tree return_type = TREE_TYPE (type);
20414   dw_die_ref subr_die
20415     = new_die (DW_TAG_subroutine_type,
20416                scope_die_for (type, context_die), type);
20417
20418   equate_type_number_to_die (type, subr_die);
20419   add_prototyped_attribute (subr_die, type);
20420   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20421   gen_formal_types_die (type, subr_die);
20422
20423   if (get_AT (subr_die, DW_AT_name))
20424     add_pubtype (type, subr_die);
20425 }
20426
20427 /* Generate a DIE for a type definition.  */
20428
20429 static void
20430 gen_typedef_die (tree decl, dw_die_ref context_die)
20431 {
20432   dw_die_ref type_die;
20433   tree origin;
20434
20435   if (TREE_ASM_WRITTEN (decl))
20436     return;
20437
20438   TREE_ASM_WRITTEN (decl) = 1;
20439   type_die = new_die (DW_TAG_typedef, context_die, decl);
20440   origin = decl_ultimate_origin (decl);
20441   if (origin != NULL)
20442     add_abstract_origin_attribute (type_die, origin);
20443   else
20444     {
20445       tree type;
20446
20447       add_name_and_src_coords_attributes (type_die, decl);
20448       if (DECL_ORIGINAL_TYPE (decl))
20449         {
20450           type = DECL_ORIGINAL_TYPE (decl);
20451
20452           gcc_assert (type != TREE_TYPE (decl));
20453           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20454         }
20455       else
20456         {
20457           type = TREE_TYPE (decl);
20458
20459           if (is_naming_typedef_decl (TYPE_NAME (type)))
20460             {
20461               /* Here, we are in the case of decl being a typedef naming
20462                  an anonymous type, e.g:
20463                      typedef struct {...} foo;
20464                  In that case TREE_TYPE (decl) is not a typedef variant
20465                  type and TYPE_NAME of the anonymous type is set to the
20466                  TYPE_DECL of the typedef. This construct is emitted by
20467                  the C++ FE.
20468
20469                  TYPE is the anonymous struct named by the typedef
20470                  DECL. As we need the DW_AT_type attribute of the
20471                  DW_TAG_typedef to point to the DIE of TYPE, let's
20472                  generate that DIE right away. add_type_attribute
20473                  called below will then pick (via lookup_type_die) that
20474                  anonymous struct DIE.  */
20475               if (!TREE_ASM_WRITTEN (type))
20476                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20477
20478               /* This is a GNU Extension.  We are adding a
20479                  DW_AT_linkage_name attribute to the DIE of the
20480                  anonymous struct TYPE.  The value of that attribute
20481                  is the name of the typedef decl naming the anonymous
20482                  struct.  This greatly eases the work of consumers of
20483                  this debug info.  */
20484               add_linkage_attr (lookup_type_die (type), decl);
20485             }
20486         }
20487
20488       add_type_attribute (type_die, type, TREE_READONLY (decl),
20489                           TREE_THIS_VOLATILE (decl), context_die);
20490
20491       if (is_naming_typedef_decl (decl))
20492         /* We want that all subsequent calls to lookup_type_die with
20493            TYPE in argument yield the DW_TAG_typedef we have just
20494            created.  */
20495         equate_type_number_to_die (type, type_die);
20496
20497       add_accessibility_attribute (type_die, decl);
20498     }
20499
20500   if (DECL_ABSTRACT (decl))
20501     equate_decl_number_to_die (decl, type_die);
20502
20503   if (get_AT (type_die, DW_AT_name))
20504     add_pubtype (decl, type_die);
20505 }
20506
20507 /* Generate a DIE for a struct, class, enum or union type.  */
20508
20509 static void
20510 gen_tagged_type_die (tree type,
20511                      dw_die_ref context_die,
20512                      enum debug_info_usage usage)
20513 {
20514   int need_pop;
20515
20516   if (type == NULL_TREE
20517       || !is_tagged_type (type))
20518     return;
20519
20520   /* If this is a nested type whose containing class hasn't been written
20521      out yet, writing it out will cover this one, too.  This does not apply
20522      to instantiations of member class templates; they need to be added to
20523      the containing class as they are generated.  FIXME: This hurts the
20524      idea of combining type decls from multiple TUs, since we can't predict
20525      what set of template instantiations we'll get.  */
20526   if (TYPE_CONTEXT (type)
20527       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20528       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20529     {
20530       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20531
20532       if (TREE_ASM_WRITTEN (type))
20533         return;
20534
20535       /* If that failed, attach ourselves to the stub.  */
20536       push_decl_scope (TYPE_CONTEXT (type));
20537       context_die = lookup_type_die (TYPE_CONTEXT (type));
20538       need_pop = 1;
20539     }
20540   else if (TYPE_CONTEXT (type) != NULL_TREE
20541            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20542     {
20543       /* If this type is local to a function that hasn't been written
20544          out yet, use a NULL context for now; it will be fixed up in
20545          decls_for_scope.  */
20546       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20547       /* A declaration DIE doesn't count; nested types need to go in the
20548          specification.  */
20549       if (context_die && is_declaration_die (context_die))
20550         context_die = NULL;
20551       need_pop = 0;
20552     }
20553   else
20554     {
20555       context_die = declare_in_namespace (type, context_die);
20556       need_pop = 0;
20557     }
20558
20559   if (TREE_CODE (type) == ENUMERAL_TYPE)
20560     {
20561       /* This might have been written out by the call to
20562          declare_in_namespace.  */
20563       if (!TREE_ASM_WRITTEN (type))
20564         gen_enumeration_type_die (type, context_die);
20565     }
20566   else
20567     gen_struct_or_union_type_die (type, context_die, usage);
20568
20569   if (need_pop)
20570     pop_decl_scope ();
20571
20572   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20573      it up if it is ever completed.  gen_*_type_die will set it for us
20574      when appropriate.  */
20575 }
20576
20577 /* Generate a type description DIE.  */
20578
20579 static void
20580 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20581                          enum debug_info_usage usage)
20582 {
20583   struct array_descr_info info;
20584
20585   if (type == NULL_TREE || type == error_mark_node)
20586     return;
20587
20588   if (TYPE_NAME (type) != NULL_TREE
20589       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20590       && is_redundant_typedef (TYPE_NAME (type))
20591       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20592     /* The DECL of this type is a typedef we don't want to emit debug
20593        info for but we want debug info for its underlying typedef.
20594        This can happen for e.g, the injected-class-name of a C++
20595        type.  */
20596     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20597
20598   /* If TYPE is a typedef type variant, let's generate debug info
20599      for the parent typedef which TYPE is a type of.  */
20600   if (typedef_variant_p (type))
20601     {
20602       if (TREE_ASM_WRITTEN (type))
20603         return;
20604
20605       /* Prevent broken recursion; we can't hand off to the same type.  */
20606       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20607
20608       /* Use the DIE of the containing namespace as the parent DIE of
20609          the type description DIE we want to generate.  */
20610       if (DECL_CONTEXT (TYPE_NAME (type))
20611           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20612         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20613
20614       TREE_ASM_WRITTEN (type) = 1;
20615
20616       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20617       return;
20618     }
20619
20620   /* If type is an anonymous tagged type named by a typedef, let's
20621      generate debug info for the typedef.  */
20622   if (is_naming_typedef_decl (TYPE_NAME (type)))
20623     {
20624       /* Use the DIE of the containing namespace as the parent DIE of
20625          the type description DIE we want to generate.  */
20626       if (DECL_CONTEXT (TYPE_NAME (type))
20627           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20628         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20629       
20630       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20631       return;
20632     }
20633
20634   /* If this is an array type with hidden descriptor, handle it first.  */
20635   if (!TREE_ASM_WRITTEN (type)
20636       && lang_hooks.types.get_array_descr_info
20637       && lang_hooks.types.get_array_descr_info (type, &info)
20638       && (dwarf_version >= 3 || !dwarf_strict))
20639     {
20640       gen_descr_array_type_die (type, &info, context_die);
20641       TREE_ASM_WRITTEN (type) = 1;
20642       return;
20643     }
20644
20645   /* We are going to output a DIE to represent the unqualified version
20646      of this type (i.e. without any const or volatile qualifiers) so
20647      get the main variant (i.e. the unqualified version) of this type
20648      now.  (Vectors are special because the debugging info is in the
20649      cloned type itself).  */
20650   if (TREE_CODE (type) != VECTOR_TYPE)
20651     type = type_main_variant (type);
20652
20653   if (TREE_ASM_WRITTEN (type))
20654     return;
20655
20656   switch (TREE_CODE (type))
20657     {
20658     case ERROR_MARK:
20659       break;
20660
20661     case POINTER_TYPE:
20662     case REFERENCE_TYPE:
20663       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20664          ensures that the gen_type_die recursion will terminate even if the
20665          type is recursive.  Recursive types are possible in Ada.  */
20666       /* ??? We could perhaps do this for all types before the switch
20667          statement.  */
20668       TREE_ASM_WRITTEN (type) = 1;
20669
20670       /* For these types, all that is required is that we output a DIE (or a
20671          set of DIEs) to represent the "basis" type.  */
20672       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20673                                 DINFO_USAGE_IND_USE);
20674       break;
20675
20676     case OFFSET_TYPE:
20677       /* This code is used for C++ pointer-to-data-member types.
20678          Output a description of the relevant class type.  */
20679       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20680                                         DINFO_USAGE_IND_USE);
20681
20682       /* Output a description of the type of the object pointed to.  */
20683       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20684                                         DINFO_USAGE_IND_USE);
20685
20686       /* Now output a DIE to represent this pointer-to-data-member type
20687          itself.  */
20688       gen_ptr_to_mbr_type_die (type, context_die);
20689       break;
20690
20691     case FUNCTION_TYPE:
20692       /* Force out return type (in case it wasn't forced out already).  */
20693       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20694                                         DINFO_USAGE_DIR_USE);
20695       gen_subroutine_type_die (type, context_die);
20696       break;
20697
20698     case METHOD_TYPE:
20699       /* Force out return type (in case it wasn't forced out already).  */
20700       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20701                                         DINFO_USAGE_DIR_USE);
20702       gen_subroutine_type_die (type, context_die);
20703       break;
20704
20705     case ARRAY_TYPE:
20706       gen_array_type_die (type, context_die);
20707       break;
20708
20709     case VECTOR_TYPE:
20710       gen_array_type_die (type, context_die);
20711       break;
20712
20713     case ENUMERAL_TYPE:
20714     case RECORD_TYPE:
20715     case UNION_TYPE:
20716     case QUAL_UNION_TYPE:
20717       gen_tagged_type_die (type, context_die, usage);
20718       return;
20719
20720     case VOID_TYPE:
20721     case INTEGER_TYPE:
20722     case REAL_TYPE:
20723     case FIXED_POINT_TYPE:
20724     case COMPLEX_TYPE:
20725     case BOOLEAN_TYPE:
20726       /* No DIEs needed for fundamental types.  */
20727       break;
20728
20729     case NULLPTR_TYPE:
20730     case LANG_TYPE:
20731       /* Just use DW_TAG_unspecified_type.  */
20732       {
20733         dw_die_ref type_die = lookup_type_die (type);
20734         if (type_die == NULL)
20735           {
20736             tree name = TYPE_NAME (type);
20737             if (TREE_CODE (name) == TYPE_DECL)
20738               name = DECL_NAME (name);
20739             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20740             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20741             equate_type_number_to_die (type, type_die);
20742           }
20743       }
20744       break;
20745
20746     default:
20747       gcc_unreachable ();
20748     }
20749
20750   TREE_ASM_WRITTEN (type) = 1;
20751 }
20752
20753 static void
20754 gen_type_die (tree type, dw_die_ref context_die)
20755 {
20756   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20757 }
20758
20759 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20760    things which are local to the given block.  */
20761
20762 static void
20763 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20764 {
20765   int must_output_die = 0;
20766   bool inlined_func;
20767
20768   /* Ignore blocks that are NULL.  */
20769   if (stmt == NULL_TREE)
20770     return;
20771
20772   inlined_func = inlined_function_outer_scope_p (stmt);
20773
20774   /* If the block is one fragment of a non-contiguous block, do not
20775      process the variables, since they will have been done by the
20776      origin block.  Do process subblocks.  */
20777   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20778     {
20779       tree sub;
20780
20781       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20782         gen_block_die (sub, context_die, depth + 1);
20783
20784       return;
20785     }
20786
20787   /* Determine if we need to output any Dwarf DIEs at all to represent this
20788      block.  */
20789   if (inlined_func)
20790     /* The outer scopes for inlinings *must* always be represented.  We
20791        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20792     must_output_die = 1;
20793   else
20794     {
20795       /* Determine if this block directly contains any "significant"
20796          local declarations which we will need to output DIEs for.  */
20797       if (debug_info_level > DINFO_LEVEL_TERSE)
20798         /* We are not in terse mode so *any* local declaration counts
20799            as being a "significant" one.  */
20800         must_output_die = ((BLOCK_VARS (stmt) != NULL
20801                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20802                            && (TREE_USED (stmt)
20803                                || TREE_ASM_WRITTEN (stmt)
20804                                || BLOCK_ABSTRACT (stmt)));
20805       else if ((TREE_USED (stmt)
20806                 || TREE_ASM_WRITTEN (stmt)
20807                 || BLOCK_ABSTRACT (stmt))
20808                && !dwarf2out_ignore_block (stmt))
20809         must_output_die = 1;
20810     }
20811
20812   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20813      DIE for any block which contains no significant local declarations at
20814      all.  Rather, in such cases we just call `decls_for_scope' so that any
20815      needed Dwarf info for any sub-blocks will get properly generated. Note
20816      that in terse mode, our definition of what constitutes a "significant"
20817      local declaration gets restricted to include only inlined function
20818      instances and local (nested) function definitions.  */
20819   if (must_output_die)
20820     {
20821       if (inlined_func)
20822         {
20823           /* If STMT block is abstract, that means we have been called
20824              indirectly from dwarf2out_abstract_function.
20825              That function rightfully marks the descendent blocks (of
20826              the abstract function it is dealing with) as being abstract,
20827              precisely to prevent us from emitting any
20828              DW_TAG_inlined_subroutine DIE as a descendent
20829              of an abstract function instance. So in that case, we should
20830              not call gen_inlined_subroutine_die.
20831
20832              Later though, when cgraph asks dwarf2out to emit info
20833              for the concrete instance of the function decl into which
20834              the concrete instance of STMT got inlined, the later will lead
20835              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20836           if (! BLOCK_ABSTRACT (stmt))
20837             gen_inlined_subroutine_die (stmt, context_die, depth);
20838         }
20839       else
20840         gen_lexical_block_die (stmt, context_die, depth);
20841     }
20842   else
20843     decls_for_scope (stmt, context_die, depth);
20844 }
20845
20846 /* Process variable DECL (or variable with origin ORIGIN) within
20847    block STMT and add it to CONTEXT_DIE.  */
20848 static void
20849 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20850 {
20851   dw_die_ref die;
20852   tree decl_or_origin = decl ? decl : origin;
20853
20854   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20855     die = lookup_decl_die (decl_or_origin);
20856   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20857            && TYPE_DECL_IS_STUB (decl_or_origin))
20858     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20859   else
20860     die = NULL;
20861
20862   if (die != NULL && die->die_parent == NULL)
20863     add_child_die (context_die, die);
20864   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20865     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20866                                          stmt, context_die);
20867   else
20868     gen_decl_die (decl, origin, context_die);
20869 }
20870
20871 /* Generate all of the decls declared within a given scope and (recursively)
20872    all of its sub-blocks.  */
20873
20874 static void
20875 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20876 {
20877   tree decl;
20878   unsigned int i;
20879   tree subblocks;
20880
20881   /* Ignore NULL blocks.  */
20882   if (stmt == NULL_TREE)
20883     return;
20884
20885   /* Output the DIEs to represent all of the data objects and typedefs
20886      declared directly within this block but not within any nested
20887      sub-blocks.  Also, nested function and tag DIEs have been
20888      generated with a parent of NULL; fix that up now.  */
20889   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20890     process_scope_var (stmt, decl, NULL_TREE, context_die);
20891   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20892     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20893                        context_die);
20894
20895   /* If we're at -g1, we're not interested in subblocks.  */
20896   if (debug_info_level <= DINFO_LEVEL_TERSE)
20897     return;
20898
20899   /* Output the DIEs to represent all sub-blocks (and the items declared
20900      therein) of this block.  */
20901   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20902        subblocks != NULL;
20903        subblocks = BLOCK_CHAIN (subblocks))
20904     gen_block_die (subblocks, context_die, depth + 1);
20905 }
20906
20907 /* Is this a typedef we can avoid emitting?  */
20908
20909 static inline int
20910 is_redundant_typedef (const_tree decl)
20911 {
20912   if (TYPE_DECL_IS_STUB (decl))
20913     return 1;
20914
20915   if (DECL_ARTIFICIAL (decl)
20916       && DECL_CONTEXT (decl)
20917       && is_tagged_type (DECL_CONTEXT (decl))
20918       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20919       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20920     /* Also ignore the artificial member typedef for the class name.  */
20921     return 1;
20922
20923   return 0;
20924 }
20925
20926 /* Return TRUE if TYPE is a typedef that names a type for linkage
20927    purposes. This kind of typedefs is produced by the C++ FE for
20928    constructs like:
20929
20930    typedef struct {...} foo;
20931
20932    In that case, there is no typedef variant type produced for foo.
20933    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20934    struct type.  */
20935
20936 static bool
20937 is_naming_typedef_decl (const_tree decl)
20938 {
20939   if (decl == NULL_TREE
20940       || TREE_CODE (decl) != TYPE_DECL
20941       || !is_tagged_type (TREE_TYPE (decl))
20942       || DECL_IS_BUILTIN (decl)
20943       || is_redundant_typedef (decl)
20944       /* It looks like Ada produces TYPE_DECLs that are very similar
20945          to C++ naming typedefs but that have different
20946          semantics. Let's be specific to c++ for now.  */
20947       || !is_cxx ())
20948     return FALSE;
20949
20950   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20951           && TYPE_NAME (TREE_TYPE (decl)) == decl
20952           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20953               != TYPE_NAME (TREE_TYPE (decl))));
20954 }
20955
20956 /* Returns the DIE for a context.  */
20957
20958 static inline dw_die_ref
20959 get_context_die (tree context)
20960 {
20961   if (context)
20962     {
20963       /* Find die that represents this context.  */
20964       if (TYPE_P (context))
20965         {
20966           context = TYPE_MAIN_VARIANT (context);
20967           return strip_naming_typedef (context, force_type_die (context));
20968         }
20969       else
20970         return force_decl_die (context);
20971     }
20972   return comp_unit_die ();
20973 }
20974
20975 /* Returns the DIE for decl.  A DIE will always be returned.  */
20976
20977 static dw_die_ref
20978 force_decl_die (tree decl)
20979 {
20980   dw_die_ref decl_die;
20981   unsigned saved_external_flag;
20982   tree save_fn = NULL_TREE;
20983   decl_die = lookup_decl_die (decl);
20984   if (!decl_die)
20985     {
20986       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20987
20988       decl_die = lookup_decl_die (decl);
20989       if (decl_die)
20990         return decl_die;
20991
20992       switch (TREE_CODE (decl))
20993         {
20994         case FUNCTION_DECL:
20995           /* Clear current_function_decl, so that gen_subprogram_die thinks
20996              that this is a declaration. At this point, we just want to force
20997              declaration die.  */
20998           save_fn = current_function_decl;
20999           current_function_decl = NULL_TREE;
21000           gen_subprogram_die (decl, context_die);
21001           current_function_decl = save_fn;
21002           break;
21003
21004         case VAR_DECL:
21005           /* Set external flag to force declaration die. Restore it after
21006            gen_decl_die() call.  */
21007           saved_external_flag = DECL_EXTERNAL (decl);
21008           DECL_EXTERNAL (decl) = 1;
21009           gen_decl_die (decl, NULL, context_die);
21010           DECL_EXTERNAL (decl) = saved_external_flag;
21011           break;
21012
21013         case NAMESPACE_DECL:
21014           if (dwarf_version >= 3 || !dwarf_strict)
21015             dwarf2out_decl (decl);
21016           else
21017             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21018             decl_die = comp_unit_die ();
21019           break;
21020
21021         case TRANSLATION_UNIT_DECL:
21022           decl_die = comp_unit_die ();
21023           break;
21024
21025         default:
21026           gcc_unreachable ();
21027         }
21028
21029       /* We should be able to find the DIE now.  */
21030       if (!decl_die)
21031         decl_die = lookup_decl_die (decl);
21032       gcc_assert (decl_die);
21033     }
21034
21035   return decl_die;
21036 }
21037
21038 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21039    always returned.  */
21040
21041 static dw_die_ref
21042 force_type_die (tree type)
21043 {
21044   dw_die_ref type_die;
21045
21046   type_die = lookup_type_die (type);
21047   if (!type_die)
21048     {
21049       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21050
21051       type_die = modified_type_die (type, TYPE_READONLY (type),
21052                                     TYPE_VOLATILE (type), context_die);
21053       gcc_assert (type_die);
21054     }
21055   return type_die;
21056 }
21057
21058 /* Force out any required namespaces to be able to output DECL,
21059    and return the new context_die for it, if it's changed.  */
21060
21061 static dw_die_ref
21062 setup_namespace_context (tree thing, dw_die_ref context_die)
21063 {
21064   tree context = (DECL_P (thing)
21065                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21066   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21067     /* Force out the namespace.  */
21068     context_die = force_decl_die (context);
21069
21070   return context_die;
21071 }
21072
21073 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21074    type) within its namespace, if appropriate.
21075
21076    For compatibility with older debuggers, namespace DIEs only contain
21077    declarations; all definitions are emitted at CU scope.  */
21078
21079 static dw_die_ref
21080 declare_in_namespace (tree thing, dw_die_ref context_die)
21081 {
21082   dw_die_ref ns_context;
21083
21084   if (debug_info_level <= DINFO_LEVEL_TERSE)
21085     return context_die;
21086
21087   /* If this decl is from an inlined function, then don't try to emit it in its
21088      namespace, as we will get confused.  It would have already been emitted
21089      when the abstract instance of the inline function was emitted anyways.  */
21090   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21091     return context_die;
21092
21093   ns_context = setup_namespace_context (thing, context_die);
21094
21095   if (ns_context != context_die)
21096     {
21097       if (is_fortran ())
21098         return ns_context;
21099       if (DECL_P (thing))
21100         gen_decl_die (thing, NULL, ns_context);
21101       else
21102         gen_type_die (thing, ns_context);
21103     }
21104   return context_die;
21105 }
21106
21107 /* Generate a DIE for a namespace or namespace alias.  */
21108
21109 static void
21110 gen_namespace_die (tree decl, dw_die_ref context_die)
21111 {
21112   dw_die_ref namespace_die;
21113
21114   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21115      they are an alias of.  */
21116   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21117     {
21118       /* Output a real namespace or module.  */
21119       context_die = setup_namespace_context (decl, comp_unit_die ());
21120       namespace_die = new_die (is_fortran ()
21121                                ? DW_TAG_module : DW_TAG_namespace,
21122                                context_die, decl);
21123       /* For Fortran modules defined in different CU don't add src coords.  */
21124       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21125         {
21126           const char *name = dwarf2_name (decl, 0);
21127           if (name)
21128             add_name_attribute (namespace_die, name);
21129         }
21130       else
21131         add_name_and_src_coords_attributes (namespace_die, decl);
21132       if (DECL_EXTERNAL (decl))
21133         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21134       equate_decl_number_to_die (decl, namespace_die);
21135     }
21136   else
21137     {
21138       /* Output a namespace alias.  */
21139
21140       /* Force out the namespace we are an alias of, if necessary.  */
21141       dw_die_ref origin_die
21142         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21143
21144       if (DECL_FILE_SCOPE_P (decl)
21145           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21146         context_die = setup_namespace_context (decl, comp_unit_die ());
21147       /* Now create the namespace alias DIE.  */
21148       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21149       add_name_and_src_coords_attributes (namespace_die, decl);
21150       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21151       equate_decl_number_to_die (decl, namespace_die);
21152     }
21153 }
21154
21155 /* Generate Dwarf debug information for a decl described by DECL.
21156    The return value is currently only meaningful for PARM_DECLs,
21157    for all other decls it returns NULL.  */
21158
21159 static dw_die_ref
21160 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21161 {
21162   tree decl_or_origin = decl ? decl : origin;
21163   tree class_origin = NULL, ultimate_origin;
21164
21165   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21166     return NULL;
21167
21168   switch (TREE_CODE (decl_or_origin))
21169     {
21170     case ERROR_MARK:
21171       break;
21172
21173     case CONST_DECL:
21174       if (!is_fortran () && !is_ada ())
21175         {
21176           /* The individual enumerators of an enum type get output when we output
21177              the Dwarf representation of the relevant enum type itself.  */
21178           break;
21179         }
21180
21181       /* Emit its type.  */
21182       gen_type_die (TREE_TYPE (decl), context_die);
21183
21184       /* And its containing namespace.  */
21185       context_die = declare_in_namespace (decl, context_die);
21186
21187       gen_const_die (decl, context_die);
21188       break;
21189
21190     case FUNCTION_DECL:
21191       /* Don't output any DIEs to represent mere function declarations,
21192          unless they are class members or explicit block externs.  */
21193       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21194           && DECL_FILE_SCOPE_P (decl_or_origin)
21195           && (current_function_decl == NULL_TREE
21196               || DECL_ARTIFICIAL (decl_or_origin)))
21197         break;
21198
21199 #if 0
21200       /* FIXME */
21201       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21202          on local redeclarations of global functions.  That seems broken.  */
21203       if (current_function_decl != decl)
21204         /* This is only a declaration.  */;
21205 #endif
21206
21207       /* If we're emitting a clone, emit info for the abstract instance.  */
21208       if (origin || DECL_ORIGIN (decl) != decl)
21209         dwarf2out_abstract_function (origin
21210                                      ? DECL_ORIGIN (origin)
21211                                      : DECL_ABSTRACT_ORIGIN (decl));
21212
21213       /* If we're emitting an out-of-line copy of an inline function,
21214          emit info for the abstract instance and set up to refer to it.  */
21215       else if (cgraph_function_possibly_inlined_p (decl)
21216                && ! DECL_ABSTRACT (decl)
21217                && ! class_or_namespace_scope_p (context_die)
21218                /* dwarf2out_abstract_function won't emit a die if this is just
21219                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21220                   that case, because that works only if we have a die.  */
21221                && DECL_INITIAL (decl) != NULL_TREE)
21222         {
21223           dwarf2out_abstract_function (decl);
21224           set_decl_origin_self (decl);
21225         }
21226
21227       /* Otherwise we're emitting the primary DIE for this decl.  */
21228       else if (debug_info_level > DINFO_LEVEL_TERSE)
21229         {
21230           /* Before we describe the FUNCTION_DECL itself, make sure that we
21231              have its containing type.  */
21232           if (!origin)
21233             origin = decl_class_context (decl);
21234           if (origin != NULL_TREE)
21235             gen_type_die (origin, context_die);
21236
21237           /* And its return type.  */
21238           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21239
21240           /* And its virtual context.  */
21241           if (DECL_VINDEX (decl) != NULL_TREE)
21242             gen_type_die (DECL_CONTEXT (decl), context_die);
21243
21244           /* Make sure we have a member DIE for decl.  */
21245           if (origin != NULL_TREE)
21246             gen_type_die_for_member (origin, decl, context_die);
21247
21248           /* And its containing namespace.  */
21249           context_die = declare_in_namespace (decl, context_die);
21250         }
21251
21252       /* Now output a DIE to represent the function itself.  */
21253       if (decl)
21254         gen_subprogram_die (decl, context_die);
21255       break;
21256
21257     case TYPE_DECL:
21258       /* If we are in terse mode, don't generate any DIEs to represent any
21259          actual typedefs.  */
21260       if (debug_info_level <= DINFO_LEVEL_TERSE)
21261         break;
21262
21263       /* In the special case of a TYPE_DECL node representing the declaration
21264          of some type tag, if the given TYPE_DECL is marked as having been
21265          instantiated from some other (original) TYPE_DECL node (e.g. one which
21266          was generated within the original definition of an inline function) we
21267          used to generate a special (abbreviated) DW_TAG_structure_type,
21268          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21269          should be actually referencing those DIEs, as variable DIEs with that
21270          type would be emitted already in the abstract origin, so it was always
21271          removed during unused type prunning.  Don't add anything in this
21272          case.  */
21273       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21274         break;
21275
21276       if (is_redundant_typedef (decl))
21277         gen_type_die (TREE_TYPE (decl), context_die);
21278       else
21279         /* Output a DIE to represent the typedef itself.  */
21280         gen_typedef_die (decl, context_die);
21281       break;
21282
21283     case LABEL_DECL:
21284       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21285         gen_label_die (decl, context_die);
21286       break;
21287
21288     case VAR_DECL:
21289     case RESULT_DECL:
21290       /* If we are in terse mode, don't generate any DIEs to represent any
21291          variable declarations or definitions.  */
21292       if (debug_info_level <= DINFO_LEVEL_TERSE)
21293         break;
21294
21295       /* Output any DIEs that are needed to specify the type of this data
21296          object.  */
21297       if (decl_by_reference_p (decl_or_origin))
21298         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21299       else
21300         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21301
21302       /* And its containing type.  */
21303       class_origin = decl_class_context (decl_or_origin);
21304       if (class_origin != NULL_TREE)
21305         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21306
21307       /* And its containing namespace.  */
21308       context_die = declare_in_namespace (decl_or_origin, context_die);
21309
21310       /* Now output the DIE to represent the data object itself.  This gets
21311          complicated because of the possibility that the VAR_DECL really
21312          represents an inlined instance of a formal parameter for an inline
21313          function.  */
21314       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21315       if (ultimate_origin != NULL_TREE
21316           && TREE_CODE (ultimate_origin) == PARM_DECL)
21317         gen_formal_parameter_die (decl, origin,
21318                                   true /* Emit name attribute.  */,
21319                                   context_die);
21320       else
21321         gen_variable_die (decl, origin, context_die);
21322       break;
21323
21324     case FIELD_DECL:
21325       /* Ignore the nameless fields that are used to skip bits but handle C++
21326          anonymous unions and structs.  */
21327       if (DECL_NAME (decl) != NULL_TREE
21328           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21329           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21330         {
21331           gen_type_die (member_declared_type (decl), context_die);
21332           gen_field_die (decl, context_die);
21333         }
21334       break;
21335
21336     case PARM_DECL:
21337       if (DECL_BY_REFERENCE (decl_or_origin))
21338         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21339       else
21340         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21341       return gen_formal_parameter_die (decl, origin,
21342                                        true /* Emit name attribute.  */,
21343                                        context_die);
21344
21345     case NAMESPACE_DECL:
21346     case IMPORTED_DECL:
21347       if (dwarf_version >= 3 || !dwarf_strict)
21348         gen_namespace_die (decl, context_die);
21349       break;
21350
21351     default:
21352       /* Probably some frontend-internal decl.  Assume we don't care.  */
21353       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21354       break;
21355     }
21356
21357   return NULL;
21358 }
21359 \f
21360 /* Output debug information for global decl DECL.  Called from toplev.c after
21361    compilation proper has finished.  */
21362
21363 static void
21364 dwarf2out_global_decl (tree decl)
21365 {
21366   /* Output DWARF2 information for file-scope tentative data object
21367      declarations, file-scope (extern) function declarations (which
21368      had no corresponding body) and file-scope tagged type declarations
21369      and definitions which have not yet been forced out.  */
21370   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21371     dwarf2out_decl (decl);
21372 }
21373
21374 /* Output debug information for type decl DECL.  Called from toplev.c
21375    and from language front ends (to record built-in types).  */
21376 static void
21377 dwarf2out_type_decl (tree decl, int local)
21378 {
21379   if (!local)
21380     dwarf2out_decl (decl);
21381 }
21382
21383 /* Output debug information for imported module or decl DECL.
21384    NAME is non-NULL name in the lexical block if the decl has been renamed.
21385    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21386    that DECL belongs to.
21387    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21388 static void
21389 dwarf2out_imported_module_or_decl_1 (tree decl,
21390                                      tree name,
21391                                      tree lexical_block,
21392                                      dw_die_ref lexical_block_die)
21393 {
21394   expanded_location xloc;
21395   dw_die_ref imported_die = NULL;
21396   dw_die_ref at_import_die;
21397
21398   if (TREE_CODE (decl) == IMPORTED_DECL)
21399     {
21400       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21401       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21402       gcc_assert (decl);
21403     }
21404   else
21405     xloc = expand_location (input_location);
21406
21407   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21408     {
21409       at_import_die = force_type_die (TREE_TYPE (decl));
21410       /* For namespace N { typedef void T; } using N::T; base_type_die
21411          returns NULL, but DW_TAG_imported_declaration requires
21412          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21413       if (!at_import_die)
21414         {
21415           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21416           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21417           at_import_die = lookup_type_die (TREE_TYPE (decl));
21418           gcc_assert (at_import_die);
21419         }
21420     }
21421   else
21422     {
21423       at_import_die = lookup_decl_die (decl);
21424       if (!at_import_die)
21425         {
21426           /* If we're trying to avoid duplicate debug info, we may not have
21427              emitted the member decl for this field.  Emit it now.  */
21428           if (TREE_CODE (decl) == FIELD_DECL)
21429             {
21430               tree type = DECL_CONTEXT (decl);
21431
21432               if (TYPE_CONTEXT (type)
21433                   && TYPE_P (TYPE_CONTEXT (type))
21434                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21435                                                 DINFO_USAGE_DIR_USE))
21436                 return;
21437               gen_type_die_for_member (type, decl,
21438                                        get_context_die (TYPE_CONTEXT (type)));
21439             }
21440           at_import_die = force_decl_die (decl);
21441         }
21442     }
21443
21444   if (TREE_CODE (decl) == NAMESPACE_DECL)
21445     {
21446       if (dwarf_version >= 3 || !dwarf_strict)
21447         imported_die = new_die (DW_TAG_imported_module,
21448                                 lexical_block_die,
21449                                 lexical_block);
21450       else
21451         return;
21452     }
21453   else
21454     imported_die = new_die (DW_TAG_imported_declaration,
21455                             lexical_block_die,
21456                             lexical_block);
21457
21458   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21459   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21460   if (name)
21461     add_AT_string (imported_die, DW_AT_name,
21462                    IDENTIFIER_POINTER (name));
21463   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21464 }
21465
21466 /* Output debug information for imported module or decl DECL.
21467    NAME is non-NULL name in context if the decl has been renamed.
21468    CHILD is true if decl is one of the renamed decls as part of
21469    importing whole module.  */
21470
21471 static void
21472 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21473                                    bool child)
21474 {
21475   /* dw_die_ref at_import_die;  */
21476   dw_die_ref scope_die;
21477
21478   if (debug_info_level <= DINFO_LEVEL_TERSE)
21479     return;
21480
21481   gcc_assert (decl);
21482
21483   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21484      We need decl DIE for reference and scope die. First, get DIE for the decl
21485      itself.  */
21486
21487   /* Get the scope die for decl context. Use comp_unit_die for global module
21488      or decl. If die is not found for non globals, force new die.  */
21489   if (context
21490       && TYPE_P (context)
21491       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21492     return;
21493
21494   if (!(dwarf_version >= 3 || !dwarf_strict))
21495     return;
21496
21497   scope_die = get_context_die (context);
21498
21499   if (child)
21500     {
21501       gcc_assert (scope_die->die_child);
21502       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21503       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21504       scope_die = scope_die->die_child;
21505     }
21506
21507   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21508   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21509
21510 }
21511
21512 /* Write the debugging output for DECL.  */
21513
21514 void
21515 dwarf2out_decl (tree decl)
21516 {
21517   dw_die_ref context_die = comp_unit_die ();
21518
21519   switch (TREE_CODE (decl))
21520     {
21521     case ERROR_MARK:
21522       return;
21523
21524     case FUNCTION_DECL:
21525       /* What we would really like to do here is to filter out all mere
21526          file-scope declarations of file-scope functions which are never
21527          referenced later within this translation unit (and keep all of ones
21528          that *are* referenced later on) but we aren't clairvoyant, so we have
21529          no idea which functions will be referenced in the future (i.e. later
21530          on within the current translation unit). So here we just ignore all
21531          file-scope function declarations which are not also definitions.  If
21532          and when the debugger needs to know something about these functions,
21533          it will have to hunt around and find the DWARF information associated
21534          with the definition of the function.
21535
21536          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21537          nodes represent definitions and which ones represent mere
21538          declarations.  We have to check DECL_INITIAL instead. That's because
21539          the C front-end supports some weird semantics for "extern inline"
21540          function definitions.  These can get inlined within the current
21541          translation unit (and thus, we need to generate Dwarf info for their
21542          abstract instances so that the Dwarf info for the concrete inlined
21543          instances can have something to refer to) but the compiler never
21544          generates any out-of-lines instances of such things (despite the fact
21545          that they *are* definitions).
21546
21547          The important point is that the C front-end marks these "extern
21548          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21549          them anyway. Note that the C++ front-end also plays some similar games
21550          for inline function definitions appearing within include files which
21551          also contain `#pragma interface' pragmas.  */
21552       if (DECL_INITIAL (decl) == NULL_TREE)
21553         return;
21554
21555       /* If we're a nested function, initially use a parent of NULL; if we're
21556          a plain function, this will be fixed up in decls_for_scope.  If
21557          we're a method, it will be ignored, since we already have a DIE.  */
21558       if (decl_function_context (decl)
21559           /* But if we're in terse mode, we don't care about scope.  */
21560           && debug_info_level > DINFO_LEVEL_TERSE)
21561         context_die = NULL;
21562       break;
21563
21564     case VAR_DECL:
21565       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21566          declaration and if the declaration was never even referenced from
21567          within this entire compilation unit.  We suppress these DIEs in
21568          order to save space in the .debug section (by eliminating entries
21569          which are probably useless).  Note that we must not suppress
21570          block-local extern declarations (whether used or not) because that
21571          would screw-up the debugger's name lookup mechanism and cause it to
21572          miss things which really ought to be in scope at a given point.  */
21573       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21574         return;
21575
21576       /* For local statics lookup proper context die.  */
21577       if (TREE_STATIC (decl) && decl_function_context (decl))
21578         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21579
21580       /* If we are in terse mode, don't generate any DIEs to represent any
21581          variable declarations or definitions.  */
21582       if (debug_info_level <= DINFO_LEVEL_TERSE)
21583         return;
21584       break;
21585
21586     case CONST_DECL:
21587       if (debug_info_level <= DINFO_LEVEL_TERSE)
21588         return;
21589       if (!is_fortran () && !is_ada ())
21590         return;
21591       if (TREE_STATIC (decl) && decl_function_context (decl))
21592         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21593       break;
21594
21595     case NAMESPACE_DECL:
21596     case IMPORTED_DECL:
21597       if (debug_info_level <= DINFO_LEVEL_TERSE)
21598         return;
21599       if (lookup_decl_die (decl) != NULL)
21600         return;
21601       break;
21602
21603     case TYPE_DECL:
21604       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21605       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21606         return;
21607
21608       /* Don't bother trying to generate any DIEs to represent any of the
21609          normal built-in types for the language we are compiling.  */
21610       if (DECL_IS_BUILTIN (decl))
21611         return;
21612
21613       /* If we are in terse mode, don't generate any DIEs for types.  */
21614       if (debug_info_level <= DINFO_LEVEL_TERSE)
21615         return;
21616
21617       /* If we're a function-scope tag, initially use a parent of NULL;
21618          this will be fixed up in decls_for_scope.  */
21619       if (decl_function_context (decl))
21620         context_die = NULL;
21621
21622       break;
21623
21624     default:
21625       return;
21626     }
21627
21628   gen_decl_die (decl, NULL, context_die);
21629 }
21630
21631 /* Write the debugging output for DECL.  */
21632
21633 static void
21634 dwarf2out_function_decl (tree decl)
21635 {
21636   dwarf2out_decl (decl);
21637   call_arg_locations = NULL;
21638   call_arg_loc_last = NULL;
21639   call_site_count = -1;
21640   tail_call_site_count = -1;
21641   VEC_free (dw_die_ref, heap, block_map);
21642   htab_empty (decl_loc_table);
21643 }
21644
21645 /* Output a marker (i.e. a label) for the beginning of the generated code for
21646    a lexical block.  */
21647
21648 static void
21649 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21650                        unsigned int blocknum)
21651 {
21652   switch_to_section (current_function_section ());
21653   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21654 }
21655
21656 /* Output a marker (i.e. a label) for the end of the generated code for a
21657    lexical block.  */
21658
21659 static void
21660 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21661 {
21662   switch_to_section (current_function_section ());
21663   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21664 }
21665
21666 /* Returns nonzero if it is appropriate not to emit any debugging
21667    information for BLOCK, because it doesn't contain any instructions.
21668
21669    Don't allow this for blocks with nested functions or local classes
21670    as we would end up with orphans, and in the presence of scheduling
21671    we may end up calling them anyway.  */
21672
21673 static bool
21674 dwarf2out_ignore_block (const_tree block)
21675 {
21676   tree decl;
21677   unsigned int i;
21678
21679   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21680     if (TREE_CODE (decl) == FUNCTION_DECL
21681         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21682       return 0;
21683   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21684     {
21685       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21686       if (TREE_CODE (decl) == FUNCTION_DECL
21687           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21688       return 0;
21689     }
21690
21691   return 1;
21692 }
21693
21694 /* Hash table routines for file_hash.  */
21695
21696 static int
21697 file_table_eq (const void *p1_p, const void *p2_p)
21698 {
21699   const struct dwarf_file_data *const p1 =
21700     (const struct dwarf_file_data *) p1_p;
21701   const char *const p2 = (const char *) p2_p;
21702   return filename_cmp (p1->filename, p2) == 0;
21703 }
21704
21705 static hashval_t
21706 file_table_hash (const void *p_p)
21707 {
21708   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21709   return htab_hash_string (p->filename);
21710 }
21711
21712 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21713    dwarf2out.c) and return its "index".  The index of each (known) filename is
21714    just a unique number which is associated with only that one filename.  We
21715    need such numbers for the sake of generating labels (in the .debug_sfnames
21716    section) and references to those files numbers (in the .debug_srcinfo
21717    and.debug_macinfo sections).  If the filename given as an argument is not
21718    found in our current list, add it to the list and assign it the next
21719    available unique index number.  In order to speed up searches, we remember
21720    the index of the filename was looked up last.  This handles the majority of
21721    all searches.  */
21722
21723 static struct dwarf_file_data *
21724 lookup_filename (const char *file_name)
21725 {
21726   void ** slot;
21727   struct dwarf_file_data * created;
21728
21729   /* Check to see if the file name that was searched on the previous
21730      call matches this file name.  If so, return the index.  */
21731   if (file_table_last_lookup
21732       && (file_name == file_table_last_lookup->filename
21733           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21734     return file_table_last_lookup;
21735
21736   /* Didn't match the previous lookup, search the table.  */
21737   slot = htab_find_slot_with_hash (file_table, file_name,
21738                                    htab_hash_string (file_name), INSERT);
21739   if (*slot)
21740     return (struct dwarf_file_data *) *slot;
21741
21742   created = ggc_alloc_dwarf_file_data ();
21743   created->filename = file_name;
21744   created->emitted_number = 0;
21745   *slot = created;
21746   return created;
21747 }
21748
21749 /* If the assembler will construct the file table, then translate the compiler
21750    internal file table number into the assembler file table number, and emit
21751    a .file directive if we haven't already emitted one yet.  The file table
21752    numbers are different because we prune debug info for unused variables and
21753    types, which may include filenames.  */
21754
21755 static int
21756 maybe_emit_file (struct dwarf_file_data * fd)
21757 {
21758   if (! fd->emitted_number)
21759     {
21760       if (last_emitted_file)
21761         fd->emitted_number = last_emitted_file->emitted_number + 1;
21762       else
21763         fd->emitted_number = 1;
21764       last_emitted_file = fd;
21765
21766       if (DWARF2_ASM_LINE_DEBUG_INFO)
21767         {
21768           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21769           output_quoted_string (asm_out_file,
21770                                 remap_debug_filename (fd->filename));
21771           fputc ('\n', asm_out_file);
21772         }
21773     }
21774
21775   return fd->emitted_number;
21776 }
21777
21778 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21779    That generation should happen after function debug info has been
21780    generated. The value of the attribute is the constant value of ARG.  */
21781
21782 static void
21783 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21784 {
21785   die_arg_entry entry;
21786
21787   if (!die || !arg)
21788     return;
21789
21790   if (!tmpl_value_parm_die_table)
21791     tmpl_value_parm_die_table
21792       = VEC_alloc (die_arg_entry, gc, 32);
21793
21794   entry.die = die;
21795   entry.arg = arg;
21796   VEC_safe_push (die_arg_entry, gc,
21797                  tmpl_value_parm_die_table,
21798                  &entry);
21799 }
21800
21801 /* Return TRUE if T is an instance of generic type, FALSE
21802    otherwise.  */
21803
21804 static bool
21805 generic_type_p (tree t)
21806 {
21807   if (t == NULL_TREE || !TYPE_P (t))
21808     return false;
21809   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21810 }
21811
21812 /* Schedule the generation of the generic parameter dies for the
21813   instance of generic type T. The proper generation itself is later
21814   done by gen_scheduled_generic_parms_dies. */
21815
21816 static void
21817 schedule_generic_params_dies_gen (tree t)
21818 {
21819   if (!generic_type_p (t))
21820     return;
21821
21822   if (generic_type_instances == NULL)
21823     generic_type_instances = VEC_alloc (tree, gc, 256);
21824
21825   VEC_safe_push (tree, gc, generic_type_instances, t);
21826 }
21827
21828 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21829    by append_entry_to_tmpl_value_parm_die_table. This function must
21830    be called after function DIEs have been generated.  */
21831
21832 static void
21833 gen_remaining_tmpl_value_param_die_attribute (void)
21834 {
21835   if (tmpl_value_parm_die_table)
21836     {
21837       unsigned i;
21838       die_arg_entry *e;
21839
21840       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21841         tree_add_const_value_attribute (e->die, e->arg);
21842     }
21843 }
21844
21845 /* Generate generic parameters DIEs for instances of generic types
21846    that have been previously scheduled by
21847    schedule_generic_params_dies_gen. This function must be called
21848    after all the types of the CU have been laid out.  */
21849
21850 static void
21851 gen_scheduled_generic_parms_dies (void)
21852 {
21853   unsigned i;
21854   tree t;
21855
21856   if (generic_type_instances == NULL)
21857     return;
21858   
21859   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21860     gen_generic_params_dies (t);
21861 }
21862
21863
21864 /* Replace DW_AT_name for the decl with name.  */
21865
21866 static void
21867 dwarf2out_set_name (tree decl, tree name)
21868 {
21869   dw_die_ref die;
21870   dw_attr_ref attr;
21871   const char *dname;
21872
21873   die = TYPE_SYMTAB_DIE (decl);
21874   if (!die)
21875     return;
21876
21877   dname = dwarf2_name (name, 0);
21878   if (!dname)
21879     return;
21880
21881   attr = get_AT (die, DW_AT_name);
21882   if (attr)
21883     {
21884       struct indirect_string_node *node;
21885
21886       node = find_AT_string (dname);
21887       /* replace the string.  */
21888       attr->dw_attr_val.v.val_str = node;
21889     }
21890
21891   else
21892     add_name_attribute (die, dname);
21893 }
21894
21895 /* Called by the final INSN scan whenever we see a var location.  We
21896    use it to drop labels in the right places, and throw the location in
21897    our lookup table.  */
21898
21899 static void
21900 dwarf2out_var_location (rtx loc_note)
21901 {
21902   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21903   struct var_loc_node *newloc;
21904   rtx next_real;
21905   static const char *last_label;
21906   static const char *last_postcall_label;
21907   static bool last_in_cold_section_p;
21908   tree decl;
21909   bool var_loc_p;
21910
21911   if (!NOTE_P (loc_note))
21912     {
21913       if (CALL_P (loc_note))
21914         {
21915           call_site_count++;
21916           if (SIBLING_CALL_P (loc_note))
21917             tail_call_site_count++;
21918         }
21919       return;
21920     }
21921
21922   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21923   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21924     return;
21925
21926   next_real = next_real_insn (loc_note);
21927
21928   /* If there are no instructions which would be affected by this note,
21929      don't do anything.  */
21930   if (var_loc_p
21931       && next_real == NULL_RTX
21932       && !NOTE_DURING_CALL_P (loc_note))
21933     return;
21934
21935   if (next_real == NULL_RTX)
21936     next_real = get_last_insn ();
21937
21938   /* If there were any real insns between note we processed last time
21939      and this note (or if it is the first note), clear
21940      last_{,postcall_}label so that they are not reused this time.  */
21941   if (last_var_location_insn == NULL_RTX
21942       || last_var_location_insn != next_real
21943       || last_in_cold_section_p != in_cold_section_p)
21944     {
21945       last_label = NULL;
21946       last_postcall_label = NULL;
21947     }
21948
21949   if (var_loc_p)
21950     {
21951       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21952       newloc = add_var_loc_to_decl (decl, loc_note,
21953                                     NOTE_DURING_CALL_P (loc_note)
21954                                     ? last_postcall_label : last_label);
21955       if (newloc == NULL)
21956         return;
21957     }
21958   else
21959     {
21960       decl = NULL_TREE;
21961       newloc = NULL;
21962     }
21963
21964   /* If there were no real insns between note we processed last time
21965      and this note, use the label we emitted last time.  Otherwise
21966      create a new label and emit it.  */
21967   if (last_label == NULL)
21968     {
21969       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21970       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21971       loclabel_num++;
21972       last_label = ggc_strdup (loclabel);
21973     }
21974
21975   if (!var_loc_p)
21976     {
21977       struct call_arg_loc_node *ca_loc
21978         = ggc_alloc_cleared_call_arg_loc_node ();
21979       rtx prev = prev_real_insn (loc_note), x;
21980       ca_loc->call_arg_loc_note = loc_note;
21981       ca_loc->next = NULL;
21982       ca_loc->label = last_label;
21983       gcc_assert (prev
21984                   && (CALL_P (prev)
21985                       || (NONJUMP_INSN_P (prev)
21986                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21987                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21988       if (!CALL_P (prev))
21989         prev = XVECEXP (PATTERN (prev), 0, 0);
21990       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21991       x = PATTERN (prev);
21992       if (GET_CODE (x) == PARALLEL)
21993         x = XVECEXP (x, 0, 0);
21994       if (GET_CODE (x) == SET)
21995         x = SET_SRC (x);
21996       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
21997         {
21998           x = XEXP (XEXP (x, 0), 0);
21999           if (GET_CODE (x) == SYMBOL_REF
22000               && SYMBOL_REF_DECL (x)
22001               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22002             ca_loc->symbol_ref = x;
22003         }
22004       ca_loc->block = insn_scope (prev);
22005       if (call_arg_locations)
22006         call_arg_loc_last->next = ca_loc;
22007       else
22008         call_arg_locations = ca_loc;
22009       call_arg_loc_last = ca_loc;
22010     }
22011   else if (!NOTE_DURING_CALL_P (loc_note))
22012     newloc->label = last_label;
22013   else
22014     {
22015       if (!last_postcall_label)
22016         {
22017           sprintf (loclabel, "%s-1", last_label);
22018           last_postcall_label = ggc_strdup (loclabel);
22019         }
22020       newloc->label = last_postcall_label;
22021     }
22022
22023   last_var_location_insn = next_real;
22024   last_in_cold_section_p = in_cold_section_p;
22025 }
22026
22027 /* We need to reset the locations at the beginning of each
22028    function. We can't do this in the end_function hook, because the
22029    declarations that use the locations won't have been output when
22030    that hook is called.  Also compute have_multiple_function_sections here.  */
22031
22032 static void
22033 dwarf2out_begin_function (tree fun)
22034 {
22035   if (function_section (fun) != text_section)
22036     have_multiple_function_sections = true;
22037   else if (flag_reorder_blocks_and_partition && !cold_text_section)
22038     {
22039       gcc_assert (current_function_decl == fun);
22040       cold_text_section = unlikely_text_section ();
22041       switch_to_section (cold_text_section);
22042       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22043       switch_to_section (current_function_section ());
22044     }
22045
22046   dwarf2out_note_section_used ();
22047   call_site_count = 0;
22048   tail_call_site_count = 0;
22049 }
22050
22051 /* Output a label to mark the beginning of a source code line entry
22052    and record information relating to this source line, in
22053    'line_info_table' for later output of the .debug_line section.  */
22054
22055 static void
22056 dwarf2out_source_line (unsigned int line, const char *filename,
22057                        int discriminator, bool is_stmt)
22058 {
22059   static bool last_is_stmt = true;
22060
22061   if (debug_info_level >= DINFO_LEVEL_NORMAL
22062       && line != 0)
22063     {
22064       int file_num = maybe_emit_file (lookup_filename (filename));
22065
22066       switch_to_section (current_function_section ());
22067
22068       /* If requested, emit something human-readable.  */
22069       if (flag_debug_asm)
22070         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22071                  filename, line);
22072
22073       if (DWARF2_ASM_LINE_DEBUG_INFO)
22074         {
22075           /* Emit the .loc directive understood by GNU as.  */
22076           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22077           if (is_stmt != last_is_stmt)
22078             {
22079               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22080               last_is_stmt = is_stmt;
22081             }
22082           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22083             fprintf (asm_out_file, " discriminator %d", discriminator);
22084           fputc ('\n', asm_out_file);
22085
22086           /* Indicate that line number info exists.  */
22087           line_info_table_in_use++;
22088         }
22089       else if (function_section (current_function_decl) != text_section)
22090         {
22091           dw_separate_line_info_ref line_info;
22092           targetm.asm_out.internal_label (asm_out_file,
22093                                           SEPARATE_LINE_CODE_LABEL,
22094                                           separate_line_info_table_in_use);
22095
22096           /* Expand the line info table if necessary.  */
22097           if (separate_line_info_table_in_use
22098               == separate_line_info_table_allocated)
22099             {
22100               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22101               separate_line_info_table
22102                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22103                                  separate_line_info_table,
22104                                  separate_line_info_table_allocated);
22105               memset (separate_line_info_table
22106                        + separate_line_info_table_in_use,
22107                       0,
22108                       (LINE_INFO_TABLE_INCREMENT
22109                        * sizeof (dw_separate_line_info_entry)));
22110             }
22111
22112           /* Add the new entry at the end of the line_info_table.  */
22113           line_info
22114             = &separate_line_info_table[separate_line_info_table_in_use++];
22115           line_info->dw_file_num = file_num;
22116           line_info->dw_line_num = line;
22117           line_info->function = current_function_funcdef_no;
22118         }
22119       else
22120         {
22121           dw_line_info_ref line_info;
22122
22123           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22124                                      line_info_table_in_use);
22125
22126           /* Expand the line info table if necessary.  */
22127           if (line_info_table_in_use == line_info_table_allocated)
22128             {
22129               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22130               line_info_table
22131                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22132                                  line_info_table_allocated);
22133               memset (line_info_table + line_info_table_in_use, 0,
22134                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22135             }
22136
22137           /* Add the new entry at the end of the line_info_table.  */
22138           line_info = &line_info_table[line_info_table_in_use++];
22139           line_info->dw_file_num = file_num;
22140           line_info->dw_line_num = line;
22141         }
22142     }
22143 }
22144
22145 /* Record the beginning of a new source file.  */
22146
22147 static void
22148 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22149 {
22150   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22151     {
22152       /* Record the beginning of the file for break_out_includes.  */
22153       dw_die_ref bincl_die;
22154
22155       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22156       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22157     }
22158
22159   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22160     {
22161       macinfo_entry e;
22162       e.code = DW_MACINFO_start_file;
22163       e.lineno = lineno;
22164       e.info = xstrdup (filename);
22165       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22166     }
22167 }
22168
22169 /* Record the end of a source file.  */
22170
22171 static void
22172 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22173 {
22174   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22175     /* Record the end of the file for break_out_includes.  */
22176     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22177
22178   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22179     {
22180       macinfo_entry e;
22181       e.code = DW_MACINFO_end_file;
22182       e.lineno = lineno;
22183       e.info = NULL;
22184       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22185     }
22186 }
22187
22188 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22189    the tail part of the directive line, i.e. the part which is past the
22190    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22191
22192 static void
22193 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22194                   const char *buffer ATTRIBUTE_UNUSED)
22195 {
22196   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22197     {
22198       macinfo_entry e;
22199       e.code = DW_MACINFO_define;
22200       e.lineno = lineno;
22201       e.info = xstrdup (buffer);;
22202       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22203     }
22204 }
22205
22206 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22207    the tail part of the directive line, i.e. the part which is past the
22208    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22209
22210 static void
22211 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22212                  const char *buffer ATTRIBUTE_UNUSED)
22213 {
22214   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22215     {
22216       macinfo_entry e;
22217       e.code = DW_MACINFO_undef;
22218       e.lineno = lineno;
22219       e.info = xstrdup (buffer);;
22220       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22221     }
22222 }
22223
22224 static void
22225 output_macinfo (void)
22226 {
22227   unsigned i;
22228   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22229   macinfo_entry *ref;
22230
22231   if (! length)
22232     return;
22233
22234   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22235     {
22236       switch (ref->code)
22237         {
22238           case DW_MACINFO_start_file:
22239             {
22240               int file_num = maybe_emit_file (lookup_filename (ref->info));
22241               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22242               dw2_asm_output_data_uleb128 
22243                         (ref->lineno, "Included from line number %lu", 
22244                                                 (unsigned long)ref->lineno);
22245               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22246             }
22247             break;
22248           case DW_MACINFO_end_file:
22249             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22250             break;
22251           case DW_MACINFO_define:
22252             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22253             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22254                                                 (unsigned long)ref->lineno);
22255             dw2_asm_output_nstring (ref->info, -1, "The macro");
22256             break;
22257           case DW_MACINFO_undef:
22258             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22259             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22260                                                 (unsigned long)ref->lineno);
22261             dw2_asm_output_nstring (ref->info, -1, "The macro");
22262             break;
22263           default:
22264            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22265              ASM_COMMENT_START, (unsigned long)ref->code);
22266           break;
22267         }
22268     }
22269 }
22270
22271 /* Set up for Dwarf output at the start of compilation.  */
22272
22273 static void
22274 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22275 {
22276   /* Allocate the file_table.  */
22277   file_table = htab_create_ggc (50, file_table_hash,
22278                                 file_table_eq, NULL);
22279
22280   /* Allocate the decl_die_table.  */
22281   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22282                                     decl_die_table_eq, NULL);
22283
22284   /* Allocate the decl_loc_table.  */
22285   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22286                                     decl_loc_table_eq, NULL);
22287
22288   /* Allocate the initial hunk of the decl_scope_table.  */
22289   decl_scope_table = VEC_alloc (tree, gc, 256);
22290
22291   /* Allocate the initial hunk of the abbrev_die_table.  */
22292   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22293     (ABBREV_DIE_TABLE_INCREMENT);
22294   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22295   /* Zero-th entry is allocated, but unused.  */
22296   abbrev_die_table_in_use = 1;
22297
22298   /* Allocate the initial hunk of the line_info_table.  */
22299   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22300     (LINE_INFO_TABLE_INCREMENT);
22301   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22302
22303   /* Zero-th entry is allocated, but unused.  */
22304   line_info_table_in_use = 1;
22305
22306   /* Allocate the pubtypes and pubnames vectors.  */
22307   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22308   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22309
22310   incomplete_types = VEC_alloc (tree, gc, 64);
22311
22312   used_rtx_array = VEC_alloc (rtx, gc, 32);
22313
22314   debug_info_section = get_section (DEBUG_INFO_SECTION,
22315                                     SECTION_DEBUG, NULL);
22316   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22317                                       SECTION_DEBUG, NULL);
22318   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22319                                        SECTION_DEBUG, NULL);
22320   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22321                                        SECTION_DEBUG, NULL);
22322   debug_line_section = get_section (DEBUG_LINE_SECTION,
22323                                     SECTION_DEBUG, NULL);
22324   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22325                                    SECTION_DEBUG, NULL);
22326   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22327                                         SECTION_DEBUG, NULL);
22328   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22329                                         SECTION_DEBUG, NULL);
22330   debug_str_section = get_section (DEBUG_STR_SECTION,
22331                                    DEBUG_STR_SECTION_FLAGS, NULL);
22332   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22333                                       SECTION_DEBUG, NULL);
22334   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22335                                      SECTION_DEBUG, NULL);
22336
22337   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22338   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22339                                DEBUG_ABBREV_SECTION_LABEL, 0);
22340   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22341   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22342                                COLD_TEXT_SECTION_LABEL, 0);
22343   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22344
22345   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22346                                DEBUG_INFO_SECTION_LABEL, 0);
22347   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22348                                DEBUG_LINE_SECTION_LABEL, 0);
22349   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22350                                DEBUG_RANGES_SECTION_LABEL, 0);
22351   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22352                                DEBUG_MACINFO_SECTION_LABEL, 0);
22353
22354   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22355     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22356
22357   switch_to_section (text_section);
22358   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22359 }
22360
22361 /* Called before cgraph_optimize starts outputtting functions, variables
22362    and toplevel asms into assembly.  */
22363
22364 static void
22365 dwarf2out_assembly_start (void)
22366 {
22367   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22368       && dwarf2out_do_cfi_asm ()
22369       && (!(flag_unwind_tables || flag_exceptions)
22370           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22371     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22372 }
22373
22374 /* A helper function for dwarf2out_finish called through
22375    htab_traverse.  Emit one queued .debug_str string.  */
22376
22377 static int
22378 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22379 {
22380   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22381
22382   if (node->label && node->refcount)
22383     {
22384       switch_to_section (debug_str_section);
22385       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22386       assemble_string (node->str, strlen (node->str) + 1);
22387     }
22388
22389   return 1;
22390 }
22391
22392 #if ENABLE_ASSERT_CHECKING
22393 /* Verify that all marks are clear.  */
22394
22395 static void
22396 verify_marks_clear (dw_die_ref die)
22397 {
22398   dw_die_ref c;
22399
22400   gcc_assert (! die->die_mark);
22401   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22402 }
22403 #endif /* ENABLE_ASSERT_CHECKING */
22404
22405 /* Clear the marks for a die and its children.
22406    Be cool if the mark isn't set.  */
22407
22408 static void
22409 prune_unmark_dies (dw_die_ref die)
22410 {
22411   dw_die_ref c;
22412
22413   if (die->die_mark)
22414     die->die_mark = 0;
22415   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22416 }
22417
22418 /* Given DIE that we're marking as used, find any other dies
22419    it references as attributes and mark them as used.  */
22420
22421 static void
22422 prune_unused_types_walk_attribs (dw_die_ref die)
22423 {
22424   dw_attr_ref a;
22425   unsigned ix;
22426
22427   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22428     {
22429       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22430         {
22431           /* A reference to another DIE.
22432              Make sure that it will get emitted.
22433              If it was broken out into a comdat group, don't follow it.  */
22434           if (dwarf_version < 4
22435               || a->dw_attr == DW_AT_specification
22436               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22437             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22438         }
22439       /* Set the string's refcount to 0 so that prune_unused_types_mark
22440          accounts properly for it.  */
22441       if (AT_class (a) == dw_val_class_str)
22442         a->dw_attr_val.v.val_str->refcount = 0;
22443     }
22444 }
22445
22446 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22447
22448 static void
22449 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22450 {
22451   dw_die_ref c;
22452
22453   if (die == NULL || die->die_child == NULL)
22454     return;
22455   c = die->die_child;
22456   do
22457     {
22458       switch (c->die_tag)
22459         {
22460         case DW_TAG_template_type_param:
22461         case DW_TAG_template_value_param:
22462         case DW_TAG_GNU_template_template_param:
22463         case DW_TAG_GNU_template_parameter_pack:
22464           prune_unused_types_mark (c, 1);
22465           break;
22466         default:
22467           break;
22468         }
22469       c = c->die_sib;
22470     } while (c && c != die->die_child);
22471 }
22472
22473 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22474    to DIE's children.  */
22475
22476 static void
22477 prune_unused_types_mark (dw_die_ref die, int dokids)
22478 {
22479   dw_die_ref c;
22480
22481   if (die->die_mark == 0)
22482     {
22483       /* We haven't done this node yet.  Mark it as used.  */
22484       die->die_mark = 1;
22485       /* If this is the DIE of a generic type instantiation,
22486          mark the children DIEs that describe its generic parms and
22487          args.  */
22488       prune_unused_types_mark_generic_parms_dies (die);
22489
22490       /* We also have to mark its parents as used.
22491          (But we don't want to mark our parents' kids due to this.)  */
22492       if (die->die_parent)
22493         prune_unused_types_mark (die->die_parent, 0);
22494
22495       /* Mark any referenced nodes.  */
22496       prune_unused_types_walk_attribs (die);
22497
22498       /* If this node is a specification,
22499          also mark the definition, if it exists.  */
22500       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22501         prune_unused_types_mark (die->die_definition, 1);
22502     }
22503
22504   if (dokids && die->die_mark != 2)
22505     {
22506       /* We need to walk the children, but haven't done so yet.
22507          Remember that we've walked the kids.  */
22508       die->die_mark = 2;
22509
22510       /* If this is an array type, we need to make sure our
22511          kids get marked, even if they're types.  If we're
22512          breaking out types into comdat sections, do this
22513          for all type definitions.  */
22514       if (die->die_tag == DW_TAG_array_type
22515           || (dwarf_version >= 4
22516               && is_type_die (die) && ! is_declaration_die (die)))
22517         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22518       else
22519         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22520     }
22521 }
22522
22523 /* For local classes, look if any static member functions were emitted
22524    and if so, mark them.  */
22525
22526 static void
22527 prune_unused_types_walk_local_classes (dw_die_ref die)
22528 {
22529   dw_die_ref c;
22530
22531   if (die->die_mark == 2)
22532     return;
22533
22534   switch (die->die_tag)
22535     {
22536     case DW_TAG_structure_type:
22537     case DW_TAG_union_type:
22538     case DW_TAG_class_type:
22539       break;
22540
22541     case DW_TAG_subprogram:
22542       if (!get_AT_flag (die, DW_AT_declaration)
22543           || die->die_definition != NULL)
22544         prune_unused_types_mark (die, 1);
22545       return;
22546
22547     default:
22548       return;
22549     }
22550
22551   /* Mark children.  */
22552   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22553 }
22554
22555 /* Walk the tree DIE and mark types that we actually use.  */
22556
22557 static void
22558 prune_unused_types_walk (dw_die_ref die)
22559 {
22560   dw_die_ref c;
22561
22562   /* Don't do anything if this node is already marked and
22563      children have been marked as well.  */
22564   if (die->die_mark == 2)
22565     return;
22566
22567   switch (die->die_tag)
22568     {
22569     case DW_TAG_structure_type:
22570     case DW_TAG_union_type:
22571     case DW_TAG_class_type:
22572       if (die->die_perennial_p)
22573         break;
22574
22575       for (c = die->die_parent; c; c = c->die_parent)
22576         if (c->die_tag == DW_TAG_subprogram)
22577           break;
22578
22579       /* Finding used static member functions inside of classes
22580          is needed just for local classes, because for other classes
22581          static member function DIEs with DW_AT_specification
22582          are emitted outside of the DW_TAG_*_type.  If we ever change
22583          it, we'd need to call this even for non-local classes.  */
22584       if (c)
22585         prune_unused_types_walk_local_classes (die);
22586
22587       /* It's a type node --- don't mark it.  */
22588       return;
22589
22590     case DW_TAG_const_type:
22591     case DW_TAG_packed_type:
22592     case DW_TAG_pointer_type:
22593     case DW_TAG_reference_type:
22594     case DW_TAG_rvalue_reference_type:
22595     case DW_TAG_volatile_type:
22596     case DW_TAG_typedef:
22597     case DW_TAG_array_type:
22598     case DW_TAG_interface_type:
22599     case DW_TAG_friend:
22600     case DW_TAG_variant_part:
22601     case DW_TAG_enumeration_type:
22602     case DW_TAG_subroutine_type:
22603     case DW_TAG_string_type:
22604     case DW_TAG_set_type:
22605     case DW_TAG_subrange_type:
22606     case DW_TAG_ptr_to_member_type:
22607     case DW_TAG_file_type:
22608       if (die->die_perennial_p)
22609         break;
22610
22611       /* It's a type node --- don't mark it.  */
22612       return;
22613
22614     default:
22615       /* Mark everything else.  */
22616       break;
22617   }
22618
22619   if (die->die_mark == 0)
22620     {
22621       die->die_mark = 1;
22622
22623       /* Now, mark any dies referenced from here.  */
22624       prune_unused_types_walk_attribs (die);
22625     }
22626
22627   die->die_mark = 2;
22628
22629   /* Mark children.  */
22630   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22631 }
22632
22633 /* Increment the string counts on strings referred to from DIE's
22634    attributes.  */
22635
22636 static void
22637 prune_unused_types_update_strings (dw_die_ref die)
22638 {
22639   dw_attr_ref a;
22640   unsigned ix;
22641
22642   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22643     if (AT_class (a) == dw_val_class_str)
22644       {
22645         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22646         s->refcount++;
22647         /* Avoid unnecessarily putting strings that are used less than
22648            twice in the hash table.  */
22649         if (s->refcount
22650             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22651           {
22652             void ** slot;
22653             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22654                                              htab_hash_string (s->str),
22655                                              INSERT);
22656             gcc_assert (*slot == NULL);
22657             *slot = s;
22658           }
22659       }
22660 }
22661
22662 /* Remove from the tree DIE any dies that aren't marked.  */
22663
22664 static void
22665 prune_unused_types_prune (dw_die_ref die)
22666 {
22667   dw_die_ref c;
22668
22669   gcc_assert (die->die_mark);
22670   prune_unused_types_update_strings (die);
22671
22672   if (! die->die_child)
22673     return;
22674
22675   c = die->die_child;
22676   do {
22677     dw_die_ref prev = c;
22678     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22679       if (c == die->die_child)
22680         {
22681           /* No marked children between 'prev' and the end of the list.  */
22682           if (prev == c)
22683             /* No marked children at all.  */
22684             die->die_child = NULL;
22685           else
22686             {
22687               prev->die_sib = c->die_sib;
22688               die->die_child = prev;
22689             }
22690           return;
22691         }
22692
22693     if (c != prev->die_sib)
22694       prev->die_sib = c;
22695     prune_unused_types_prune (c);
22696   } while (c != die->die_child);
22697 }
22698
22699 /* A helper function for dwarf2out_finish called through
22700    htab_traverse.  Clear .debug_str strings that we haven't already
22701    decided to emit.  */
22702
22703 static int
22704 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22705 {
22706   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22707
22708   if (!node->label || !node->refcount)
22709     htab_clear_slot (debug_str_hash, h);
22710
22711   return 1;
22712 }
22713
22714 /* Remove dies representing declarations that we never use.  */
22715
22716 static void
22717 prune_unused_types (void)
22718 {
22719   unsigned int i;
22720   limbo_die_node *node;
22721   comdat_type_node *ctnode;
22722   pubname_ref pub;
22723
22724 #if ENABLE_ASSERT_CHECKING
22725   /* All the marks should already be clear.  */
22726   verify_marks_clear (comp_unit_die ());
22727   for (node = limbo_die_list; node; node = node->next)
22728     verify_marks_clear (node->die);
22729   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22730     verify_marks_clear (ctnode->root_die);
22731 #endif /* ENABLE_ASSERT_CHECKING */
22732
22733   /* Mark types that are used in global variables.  */
22734   premark_types_used_by_global_vars ();
22735
22736   /* Set the mark on nodes that are actually used.  */
22737   prune_unused_types_walk (comp_unit_die ());
22738   for (node = limbo_die_list; node; node = node->next)
22739     prune_unused_types_walk (node->die);
22740   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22741     {
22742       prune_unused_types_walk (ctnode->root_die);
22743       prune_unused_types_mark (ctnode->type_die, 1);
22744     }
22745
22746   /* Also set the mark on nodes referenced from the
22747      pubname_table or arange_table.  */
22748   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22749     prune_unused_types_mark (pub->die, 1);
22750   for (i = 0; i < arange_table_in_use; i++)
22751     prune_unused_types_mark (arange_table[i], 1);
22752
22753   /* Get rid of nodes that aren't marked; and update the string counts.  */
22754   if (debug_str_hash && debug_str_hash_forced)
22755     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22756   else if (debug_str_hash)
22757     htab_empty (debug_str_hash);
22758   prune_unused_types_prune (comp_unit_die ());
22759   for (node = limbo_die_list; node; node = node->next)
22760     prune_unused_types_prune (node->die);
22761   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22762     prune_unused_types_prune (ctnode->root_die);
22763
22764   /* Leave the marks clear.  */
22765   prune_unmark_dies (comp_unit_die ());
22766   for (node = limbo_die_list; node; node = node->next)
22767     prune_unmark_dies (node->die);
22768   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22769     prune_unmark_dies (ctnode->root_die);
22770 }
22771
22772 /* Set the parameter to true if there are any relative pathnames in
22773    the file table.  */
22774 static int
22775 file_table_relative_p (void ** slot, void *param)
22776 {
22777   bool *p = (bool *) param;
22778   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22779   if (!IS_ABSOLUTE_PATH (d->filename))
22780     {
22781       *p = true;
22782       return 0;
22783     }
22784   return 1;
22785 }
22786
22787 /* Routines to manipulate hash table of comdat type units.  */
22788
22789 static hashval_t
22790 htab_ct_hash (const void *of)
22791 {
22792   hashval_t h;
22793   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22794
22795   memcpy (&h, type_node->signature, sizeof (h));
22796   return h;
22797 }
22798
22799 static int
22800 htab_ct_eq (const void *of1, const void *of2)
22801 {
22802   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22803   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22804
22805   return (! memcmp (type_node_1->signature, type_node_2->signature,
22806                     DWARF_TYPE_SIGNATURE_SIZE));
22807 }
22808
22809 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22810    to the location it would have been added, should we know its
22811    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22812    probably improve compactness of debug info, removing equivalent
22813    abbrevs, and hide any differences caused by deferring the
22814    computation of the assembler name, triggered by e.g. PCH.  */
22815
22816 static inline void
22817 move_linkage_attr (dw_die_ref die)
22818 {
22819   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22820   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22821
22822   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22823               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22824
22825   while (--ix > 0)
22826     {
22827       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22828
22829       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22830         break;
22831     }
22832
22833   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22834     {
22835       VEC_pop (dw_attr_node, die->die_attr);
22836       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22837     }
22838 }
22839
22840 /* Helper function for resolve_addr, attempt to resolve
22841    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22842    SYMBOL_REFs refer to variables emitted in the current CU.  */
22843
22844 static int
22845 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22846 {
22847   rtx rtl = *addr;
22848
22849   if (GET_CODE (rtl) == CONST_STRING)
22850     {
22851       size_t len = strlen (XSTR (rtl, 0)) + 1;
22852       tree t = build_string (len, XSTR (rtl, 0));
22853       tree tlen = build_int_cst (NULL_TREE, len - 1);
22854       TREE_TYPE (t)
22855         = build_array_type (char_type_node, build_index_type (tlen));
22856       rtl = lookup_constant_def (t);
22857       if (!rtl || !MEM_P (rtl))
22858         return 1;
22859       rtl = XEXP (rtl, 0);
22860       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22861       *addr = rtl;
22862       return 0;
22863     }
22864
22865   if (GET_CODE (rtl) == SYMBOL_REF
22866       && SYMBOL_REF_DECL (rtl))
22867     {
22868       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22869         {
22870           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22871             return 1;
22872         }
22873       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22874         return 1;
22875     }
22876
22877   if (GET_CODE (rtl) == CONST
22878       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22879     return 1;
22880
22881   return 0;
22882 }
22883
22884 /* Helper function for resolve_addr, handle one location
22885    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22886    the location list couldn't be resolved.  */
22887
22888 static bool
22889 resolve_addr_in_expr (dw_loc_descr_ref loc)
22890 {
22891   for (; loc; loc = loc->dw_loc_next)
22892     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22893          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22894         || (loc->dw_loc_opc == DW_OP_implicit_value
22895             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22896             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22897       return false;
22898     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22899              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22900       {
22901         dw_die_ref ref
22902           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22903         if (ref == NULL)
22904           return false;
22905         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22906         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22907         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22908       }
22909   return true;
22910 }
22911
22912 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22913    an address in .rodata section if the string literal is emitted there,
22914    or remove the containing location list or replace DW_AT_const_value
22915    with DW_AT_location and empty location expression, if it isn't found
22916    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22917    to something that has been emitted in the current CU.  */
22918
22919 static void
22920 resolve_addr (dw_die_ref die)
22921 {
22922   dw_die_ref c;
22923   dw_attr_ref a;
22924   dw_loc_list_ref *curr;
22925   unsigned ix;
22926
22927   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22928     switch (AT_class (a))
22929       {
22930       case dw_val_class_loc_list:
22931         curr = AT_loc_list_ptr (a);
22932         while (*curr)
22933           {
22934             if (!resolve_addr_in_expr ((*curr)->expr))
22935               {
22936                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22937                 if (next && (*curr)->ll_symbol)
22938                   {
22939                     gcc_assert (!next->ll_symbol);
22940                     next->ll_symbol = (*curr)->ll_symbol;
22941                   }
22942                 *curr = next;
22943               }
22944             else
22945               curr = &(*curr)->dw_loc_next;
22946           }
22947         if (!AT_loc_list (a))
22948           {
22949             remove_AT (die, a->dw_attr);
22950             ix--;
22951           }
22952         break;
22953       case dw_val_class_loc:
22954         if (!resolve_addr_in_expr (AT_loc (a)))
22955           {
22956             remove_AT (die, a->dw_attr);
22957             ix--;
22958           }
22959         break;
22960       case dw_val_class_addr:
22961         if (a->dw_attr == DW_AT_const_value
22962             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22963           {
22964             remove_AT (die, a->dw_attr);
22965             ix--;
22966           }
22967         if (die->die_tag == DW_TAG_GNU_call_site
22968             && a->dw_attr == DW_AT_abstract_origin)
22969           {
22970             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22971             dw_die_ref tdie = lookup_decl_die (tdecl);
22972             if (tdie == NULL && DECL_EXTERNAL (tdecl))
22973               {
22974                 force_decl_die (tdecl);
22975                 tdie = lookup_decl_die (tdecl);
22976               }
22977             if (tdie)
22978               {
22979                 a->dw_attr_val.val_class = dw_val_class_die_ref;
22980                 a->dw_attr_val.v.val_die_ref.die = tdie;
22981                 a->dw_attr_val.v.val_die_ref.external = 0;
22982               }
22983             else
22984               {
22985                 remove_AT (die, a->dw_attr);
22986                 ix--;
22987               }
22988           }
22989         break;
22990       default:
22991         break;
22992       }
22993
22994   FOR_EACH_CHILD (die, c, resolve_addr (c));
22995 }
22996 \f
22997 /* Helper routines for optimize_location_lists.
22998    This pass tries to share identical local lists in .debug_loc
22999    section.  */
23000
23001 /* Iteratively hash operands of LOC opcode.  */
23002
23003 static inline hashval_t
23004 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23005 {
23006   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23007   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23008
23009   switch (loc->dw_loc_opc)
23010     {
23011     case DW_OP_const4u:
23012     case DW_OP_const8u:
23013       if (loc->dtprel)
23014         goto hash_addr;
23015       /* FALLTHRU */
23016     case DW_OP_const1u:
23017     case DW_OP_const1s:
23018     case DW_OP_const2u:
23019     case DW_OP_const2s:
23020     case DW_OP_const4s:
23021     case DW_OP_const8s:
23022     case DW_OP_constu:
23023     case DW_OP_consts:
23024     case DW_OP_pick:
23025     case DW_OP_plus_uconst:
23026     case DW_OP_breg0:
23027     case DW_OP_breg1:
23028     case DW_OP_breg2:
23029     case DW_OP_breg3:
23030     case DW_OP_breg4:
23031     case DW_OP_breg5:
23032     case DW_OP_breg6:
23033     case DW_OP_breg7:
23034     case DW_OP_breg8:
23035     case DW_OP_breg9:
23036     case DW_OP_breg10:
23037     case DW_OP_breg11:
23038     case DW_OP_breg12:
23039     case DW_OP_breg13:
23040     case DW_OP_breg14:
23041     case DW_OP_breg15:
23042     case DW_OP_breg16:
23043     case DW_OP_breg17:
23044     case DW_OP_breg18:
23045     case DW_OP_breg19:
23046     case DW_OP_breg20:
23047     case DW_OP_breg21:
23048     case DW_OP_breg22:
23049     case DW_OP_breg23:
23050     case DW_OP_breg24:
23051     case DW_OP_breg25:
23052     case DW_OP_breg26:
23053     case DW_OP_breg27:
23054     case DW_OP_breg28:
23055     case DW_OP_breg29:
23056     case DW_OP_breg30:
23057     case DW_OP_breg31:
23058     case DW_OP_regx:
23059     case DW_OP_fbreg:
23060     case DW_OP_piece:
23061     case DW_OP_deref_size:
23062     case DW_OP_xderef_size:
23063       hash = iterative_hash_object (val1->v.val_int, hash);
23064       break;
23065     case DW_OP_skip:
23066     case DW_OP_bra:
23067       {
23068         int offset;
23069
23070         gcc_assert (val1->val_class == dw_val_class_loc);
23071         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23072         hash = iterative_hash_object (offset, hash);
23073       }
23074       break;
23075     case DW_OP_implicit_value:
23076       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23077       switch (val2->val_class)
23078         {
23079         case dw_val_class_const:
23080           hash = iterative_hash_object (val2->v.val_int, hash);
23081           break;
23082         case dw_val_class_vec:
23083           {
23084             unsigned int elt_size = val2->v.val_vec.elt_size;
23085             unsigned int len = val2->v.val_vec.length;
23086
23087             hash = iterative_hash_object (elt_size, hash);
23088             hash = iterative_hash_object (len, hash);
23089             hash = iterative_hash (val2->v.val_vec.array,
23090                                    len * elt_size, hash);
23091           }
23092           break;
23093         case dw_val_class_const_double:
23094           hash = iterative_hash_object (val2->v.val_double.low, hash);
23095           hash = iterative_hash_object (val2->v.val_double.high, hash);
23096           break;
23097         case dw_val_class_addr:
23098           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23099           break;
23100         default:
23101           gcc_unreachable ();
23102         }
23103       break;
23104     case DW_OP_bregx:
23105     case DW_OP_bit_piece:
23106       hash = iterative_hash_object (val1->v.val_int, hash);
23107       hash = iterative_hash_object (val2->v.val_int, hash);
23108       break;
23109     case DW_OP_addr:
23110     hash_addr:
23111       if (loc->dtprel)
23112         {
23113           unsigned char dtprel = 0xd1;
23114           hash = iterative_hash_object (dtprel, hash);
23115         }
23116       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23117       break;
23118     case DW_OP_GNU_implicit_pointer:
23119       hash = iterative_hash_object (val2->v.val_int, hash);
23120       break;
23121
23122     default:
23123       /* Other codes have no operands.  */
23124       break;
23125     }
23126   return hash;
23127 }
23128
23129 /* Iteratively hash the whole DWARF location expression LOC.  */
23130
23131 static inline hashval_t
23132 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23133 {
23134   dw_loc_descr_ref l;
23135   bool sizes_computed = false;
23136   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23137   size_of_locs (loc);
23138
23139   for (l = loc; l != NULL; l = l->dw_loc_next)
23140     {
23141       enum dwarf_location_atom opc = l->dw_loc_opc;
23142       hash = iterative_hash_object (opc, hash);
23143       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23144         {
23145           size_of_locs (loc);
23146           sizes_computed = true;
23147         }
23148       hash = hash_loc_operands (l, hash);
23149     }
23150   return hash;
23151 }
23152
23153 /* Compute hash of the whole location list LIST_HEAD.  */
23154
23155 static inline void
23156 hash_loc_list (dw_loc_list_ref list_head)
23157 {
23158   dw_loc_list_ref curr = list_head;
23159   hashval_t hash = 0;
23160
23161   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23162     {
23163       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23164       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23165       if (curr->section)
23166         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23167                                hash);
23168       hash = hash_locs (curr->expr, hash);
23169     }
23170   list_head->hash = hash;
23171 }
23172
23173 /* Return true if X and Y opcodes have the same operands.  */
23174
23175 static inline bool
23176 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23177 {
23178   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23179   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23180   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23181   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23182
23183   switch (x->dw_loc_opc)
23184     {
23185     case DW_OP_const4u:
23186     case DW_OP_const8u:
23187       if (x->dtprel)
23188         goto hash_addr;
23189       /* FALLTHRU */
23190     case DW_OP_const1u:
23191     case DW_OP_const1s:
23192     case DW_OP_const2u:
23193     case DW_OP_const2s:
23194     case DW_OP_const4s:
23195     case DW_OP_const8s:
23196     case DW_OP_constu:
23197     case DW_OP_consts:
23198     case DW_OP_pick:
23199     case DW_OP_plus_uconst:
23200     case DW_OP_breg0:
23201     case DW_OP_breg1:
23202     case DW_OP_breg2:
23203     case DW_OP_breg3:
23204     case DW_OP_breg4:
23205     case DW_OP_breg5:
23206     case DW_OP_breg6:
23207     case DW_OP_breg7:
23208     case DW_OP_breg8:
23209     case DW_OP_breg9:
23210     case DW_OP_breg10:
23211     case DW_OP_breg11:
23212     case DW_OP_breg12:
23213     case DW_OP_breg13:
23214     case DW_OP_breg14:
23215     case DW_OP_breg15:
23216     case DW_OP_breg16:
23217     case DW_OP_breg17:
23218     case DW_OP_breg18:
23219     case DW_OP_breg19:
23220     case DW_OP_breg20:
23221     case DW_OP_breg21:
23222     case DW_OP_breg22:
23223     case DW_OP_breg23:
23224     case DW_OP_breg24:
23225     case DW_OP_breg25:
23226     case DW_OP_breg26:
23227     case DW_OP_breg27:
23228     case DW_OP_breg28:
23229     case DW_OP_breg29:
23230     case DW_OP_breg30:
23231     case DW_OP_breg31:
23232     case DW_OP_regx:
23233     case DW_OP_fbreg:
23234     case DW_OP_piece:
23235     case DW_OP_deref_size:
23236     case DW_OP_xderef_size:
23237       return valx1->v.val_int == valy1->v.val_int;
23238     case DW_OP_skip:
23239     case DW_OP_bra:
23240       gcc_assert (valx1->val_class == dw_val_class_loc
23241                   && valy1->val_class == dw_val_class_loc
23242                   && x->dw_loc_addr == y->dw_loc_addr);
23243       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23244     case DW_OP_implicit_value:
23245       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23246           || valx2->val_class != valy2->val_class)
23247         return false;
23248       switch (valx2->val_class)
23249         {
23250         case dw_val_class_const:
23251           return valx2->v.val_int == valy2->v.val_int;
23252         case dw_val_class_vec:
23253           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23254                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23255                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23256                             valx2->v.val_vec.elt_size
23257                             * valx2->v.val_vec.length) == 0;
23258         case dw_val_class_const_double:
23259           return valx2->v.val_double.low == valy2->v.val_double.low
23260                  && valx2->v.val_double.high == valy2->v.val_double.high;
23261         case dw_val_class_addr:
23262           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23263         default:
23264           gcc_unreachable ();
23265         }
23266     case DW_OP_bregx:
23267     case DW_OP_bit_piece:
23268       return valx1->v.val_int == valy1->v.val_int
23269              && valx2->v.val_int == valy2->v.val_int;
23270     case DW_OP_addr:
23271     hash_addr:
23272       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23273     case DW_OP_GNU_implicit_pointer:
23274       return valx1->val_class == dw_val_class_die_ref
23275              && valx1->val_class == valy1->val_class
23276              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23277              && valx2->v.val_int == valy2->v.val_int;
23278     default:
23279       /* Other codes have no operands.  */
23280       return true;
23281     }
23282 }
23283
23284 /* Return true if DWARF location expressions X and Y are the same.  */
23285
23286 static inline bool
23287 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23288 {
23289   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23290     if (x->dw_loc_opc != y->dw_loc_opc
23291         || x->dtprel != y->dtprel
23292         || !compare_loc_operands (x, y))
23293       break;
23294   return x == NULL && y == NULL;
23295 }
23296
23297 /* Return precomputed hash of location list X.  */
23298
23299 static hashval_t
23300 loc_list_hash (const void *x)
23301 {
23302   return ((const struct dw_loc_list_struct *) x)->hash;
23303 }
23304
23305 /* Return 1 if location lists X and Y are the same.  */
23306
23307 static int
23308 loc_list_eq (const void *x, const void *y)
23309 {
23310   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23311   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23312   if (a == b)
23313     return 1;
23314   if (a->hash != b->hash)
23315     return 0;
23316   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23317     if (strcmp (a->begin, b->begin) != 0
23318         || strcmp (a->end, b->end) != 0
23319         || (a->section == NULL) != (b->section == NULL)
23320         || (a->section && strcmp (a->section, b->section) != 0)
23321         || !compare_locs (a->expr, b->expr))
23322       break;
23323   return a == NULL && b == NULL;
23324 }
23325
23326 /* Recursively optimize location lists referenced from DIE
23327    children and share them whenever possible.  */
23328
23329 static void
23330 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23331 {
23332   dw_die_ref c;
23333   dw_attr_ref a;
23334   unsigned ix;
23335   void **slot;
23336
23337   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23338     if (AT_class (a) == dw_val_class_loc_list)
23339       {
23340         dw_loc_list_ref list = AT_loc_list (a);
23341         /* TODO: perform some optimizations here, before hashing
23342            it and storing into the hash table.  */
23343         hash_loc_list (list);
23344         slot = htab_find_slot_with_hash (htab, list, list->hash,
23345                                          INSERT);
23346         if (*slot == NULL)
23347           *slot = (void *) list;
23348         else
23349           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23350       }
23351
23352   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23353 }
23354
23355 /* Optimize location lists referenced from DIE
23356    children and share them whenever possible.  */
23357
23358 static void
23359 optimize_location_lists (dw_die_ref die)
23360 {
23361   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23362   optimize_location_lists_1 (die, htab);
23363   htab_delete (htab);
23364 }
23365 \f
23366 /* Output stuff that dwarf requires at the end of every file,
23367    and generate the DWARF-2 debugging info.  */
23368
23369 static void
23370 dwarf2out_finish (const char *filename)
23371 {
23372   limbo_die_node *node, *next_node;
23373   comdat_type_node *ctnode;
23374   htab_t comdat_type_table;
23375   unsigned int i;
23376
23377   gen_scheduled_generic_parms_dies ();
23378   gen_remaining_tmpl_value_param_die_attribute ();
23379
23380   /* Add the name for the main input file now.  We delayed this from
23381      dwarf2out_init to avoid complications with PCH.  */
23382   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23383   if (!IS_ABSOLUTE_PATH (filename))
23384     add_comp_dir_attribute (comp_unit_die ());
23385   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23386     {
23387       bool p = false;
23388       htab_traverse (file_table, file_table_relative_p, &p);
23389       if (p)
23390         add_comp_dir_attribute (comp_unit_die ());
23391     }
23392
23393   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23394     {
23395       add_location_or_const_value_attribute (
23396         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23397         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23398         DW_AT_location);
23399     }
23400
23401   /* Traverse the limbo die list, and add parent/child links.  The only
23402      dies without parents that should be here are concrete instances of
23403      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23404      For concrete instances, we can get the parent die from the abstract
23405      instance.  */
23406   for (node = limbo_die_list; node; node = next_node)
23407     {
23408       dw_die_ref die = node->die;
23409       next_node = node->next;
23410
23411       if (die->die_parent == NULL)
23412         {
23413           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23414
23415           if (origin)
23416             add_child_die (origin->die_parent, die);
23417           else if (is_cu_die (die))
23418             ;
23419           else if (seen_error ())
23420             /* It's OK to be confused by errors in the input.  */
23421             add_child_die (comp_unit_die (), die);
23422           else
23423             {
23424               /* In certain situations, the lexical block containing a
23425                  nested function can be optimized away, which results
23426                  in the nested function die being orphaned.  Likewise
23427                  with the return type of that nested function.  Force
23428                  this to be a child of the containing function.
23429
23430                  It may happen that even the containing function got fully
23431                  inlined and optimized out.  In that case we are lost and
23432                  assign the empty child.  This should not be big issue as
23433                  the function is likely unreachable too.  */
23434               tree context = NULL_TREE;
23435
23436               gcc_assert (node->created_for);
23437
23438               if (DECL_P (node->created_for))
23439                 context = DECL_CONTEXT (node->created_for);
23440               else if (TYPE_P (node->created_for))
23441                 context = TYPE_CONTEXT (node->created_for);
23442
23443               gcc_assert (context
23444                           && (TREE_CODE (context) == FUNCTION_DECL
23445                               || TREE_CODE (context) == NAMESPACE_DECL));
23446
23447               origin = lookup_decl_die (context);
23448               if (origin)
23449                 add_child_die (origin, die);
23450               else
23451                 add_child_die (comp_unit_die (), die);
23452             }
23453         }
23454     }
23455
23456   limbo_die_list = NULL;
23457
23458   resolve_addr (comp_unit_die ());
23459
23460   for (node = deferred_asm_name; node; node = node->next)
23461     {
23462       tree decl = node->created_for;
23463       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23464         {
23465           add_linkage_attr (node->die, decl);
23466           move_linkage_attr (node->die);
23467         }
23468     }
23469
23470   deferred_asm_name = NULL;
23471
23472   /* Walk through the list of incomplete types again, trying once more to
23473      emit full debugging info for them.  */
23474   retry_incomplete_types ();
23475
23476   if (flag_eliminate_unused_debug_types)
23477     prune_unused_types ();
23478
23479   /* Generate separate CUs for each of the include files we've seen.
23480      They will go into limbo_die_list.  */
23481   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23482     break_out_includes (comp_unit_die ());
23483
23484   /* Generate separate COMDAT sections for type DIEs. */
23485   if (dwarf_version >= 4)
23486     {
23487       break_out_comdat_types (comp_unit_die ());
23488
23489       /* Each new type_unit DIE was added to the limbo die list when created.
23490          Since these have all been added to comdat_type_list, clear the
23491          limbo die list.  */
23492       limbo_die_list = NULL;
23493
23494       /* For each new comdat type unit, copy declarations for incomplete
23495          types to make the new unit self-contained (i.e., no direct
23496          references to the main compile unit).  */
23497       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23498         copy_decls_for_unworthy_types (ctnode->root_die);
23499       copy_decls_for_unworthy_types (comp_unit_die ());
23500
23501       /* In the process of copying declarations from one unit to another,
23502          we may have left some declarations behind that are no longer
23503          referenced.  Prune them.  */
23504       prune_unused_types ();
23505     }
23506
23507   /* Traverse the DIE's and add add sibling attributes to those DIE's
23508      that have children.  */
23509   add_sibling_attributes (comp_unit_die ());
23510   for (node = limbo_die_list; node; node = node->next)
23511     add_sibling_attributes (node->die);
23512   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23513     add_sibling_attributes (ctnode->root_die);
23514
23515   /* Output a terminator label for the .text section.  */
23516   switch_to_section (text_section);
23517   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23518   if (cold_text_section)
23519     {
23520       switch_to_section (cold_text_section);
23521       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23522     }
23523
23524   /* We can only use the low/high_pc attributes if all of the code was
23525      in .text.  */
23526   if (!have_multiple_function_sections 
23527       || (dwarf_version < 3 && dwarf_strict))
23528     {
23529       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23530       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23531     }
23532
23533   else
23534     {
23535       unsigned fde_idx = 0;
23536       bool range_list_added = false;
23537
23538       /* We need to give .debug_loc and .debug_ranges an appropriate
23539          "base address".  Use zero so that these addresses become
23540          absolute.  Historically, we've emitted the unexpected
23541          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23542          Emit both to give time for other tools to adapt.  */
23543       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23544       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23545
23546       if (text_section_used)
23547         add_ranges_by_labels (comp_unit_die (), text_section_label,
23548                               text_end_label, &range_list_added);
23549       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23550         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23551                               cold_end_label, &range_list_added);
23552
23553       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23554         {
23555           dw_fde_ref fde = &fde_table[fde_idx];
23556
23557           if (fde->dw_fde_switched_sections)
23558             {
23559               if (!fde->in_std_section)
23560                 add_ranges_by_labels (comp_unit_die (),
23561                                       fde->dw_fde_hot_section_label,
23562                                       fde->dw_fde_hot_section_end_label,
23563                                       &range_list_added);
23564               if (!fde->cold_in_std_section)
23565                 add_ranges_by_labels (comp_unit_die (),
23566                                       fde->dw_fde_unlikely_section_label,
23567                                       fde->dw_fde_unlikely_section_end_label,
23568                                       &range_list_added);
23569             }
23570           else if (!fde->in_std_section)
23571             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23572                                   fde->dw_fde_end, &range_list_added);
23573         }
23574
23575       if (range_list_added)
23576         add_ranges (NULL);
23577     }
23578
23579   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23580     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23581                     debug_line_section_label);
23582
23583   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23584     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23585
23586   if (have_location_lists)
23587     optimize_location_lists (comp_unit_die ());
23588
23589   /* Output all of the compilation units.  We put the main one last so that
23590      the offsets are available to output_pubnames.  */
23591   for (node = limbo_die_list; node; node = node->next)
23592     output_comp_unit (node->die, 0);
23593
23594   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23595   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23596     {
23597       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23598
23599       /* Don't output duplicate types.  */
23600       if (*slot != HTAB_EMPTY_ENTRY)
23601         continue;
23602
23603       /* Add a pointer to the line table for the main compilation unit
23604          so that the debugger can make sense of DW_AT_decl_file
23605          attributes.  */
23606       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23607         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23608                         debug_line_section_label);
23609
23610       output_comdat_type_unit (ctnode);
23611       *slot = ctnode;
23612     }
23613   htab_delete (comdat_type_table);
23614
23615   /* Output the main compilation unit if non-empty or if .debug_macinfo
23616      will be emitted.  */
23617   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23618
23619   /* Output the abbreviation table.  */
23620   switch_to_section (debug_abbrev_section);
23621   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23622   output_abbrev_section ();
23623
23624   /* Output location list section if necessary.  */
23625   if (have_location_lists)
23626     {
23627       /* Output the location lists info.  */
23628       switch_to_section (debug_loc_section);
23629       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23630                                    DEBUG_LOC_SECTION_LABEL, 0);
23631       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23632       output_location_lists (comp_unit_die ());
23633     }
23634
23635   /* Output public names table if necessary.  */
23636   if (!VEC_empty (pubname_entry, pubname_table))
23637     {
23638       gcc_assert (info_section_emitted);
23639       switch_to_section (debug_pubnames_section);
23640       output_pubnames (pubname_table);
23641     }
23642
23643   /* Output public types table if necessary.  */
23644   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23645      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23646      simply won't look for the section.  */
23647   if (!VEC_empty (pubname_entry, pubtype_table))
23648     {
23649       bool empty = false;
23650       
23651       if (flag_eliminate_unused_debug_types)
23652         {
23653           /* The pubtypes table might be emptied by pruning unused items.  */
23654           unsigned i;
23655           pubname_ref p;
23656           empty = true;
23657           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23658             if (p->die->die_offset != 0)
23659               {
23660                 empty = false;
23661                 break;
23662               }
23663         }
23664       if (!empty)
23665         {
23666           gcc_assert (info_section_emitted);
23667           switch_to_section (debug_pubtypes_section);
23668           output_pubnames (pubtype_table);
23669         }
23670     }
23671
23672   /* Output the address range information.  We only put functions in the arange
23673      table, so don't write it out if we don't have any.  */
23674   if ((text_section_used || cold_text_section_used || arange_table_in_use)
23675       && info_section_emitted)
23676     {
23677       switch_to_section (debug_aranges_section);
23678       output_aranges ();
23679     }
23680
23681   /* Output ranges section if necessary.  */
23682   if (ranges_table_in_use)
23683     {
23684       switch_to_section (debug_ranges_section);
23685       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23686       output_ranges ();
23687     }
23688
23689   /* Output the source line correspondence table.  We must do this
23690      even if there is no line information.  Otherwise, on an empty
23691      translation unit, we will generate a present, but empty,
23692      .debug_info section.  IRIX 6.5 `nm' will then complain when
23693      examining the file.  This is done late so that any filenames
23694      used by the debug_info section are marked as 'used'.  */
23695   switch_to_section (debug_line_section);
23696   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23697   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23698     output_line_info ();
23699
23700   /* Have to end the macro section.  */
23701   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23702     {
23703       switch_to_section (debug_macinfo_section);
23704       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23705       if (!VEC_empty (macinfo_entry, macinfo_table))
23706         output_macinfo ();
23707       dw2_asm_output_data (1, 0, "End compilation unit");
23708     }
23709
23710   /* If we emitted any DW_FORM_strp form attribute, output the string
23711      table too.  */
23712   if (debug_str_hash)
23713     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23714 }
23715
23716 #include "gt-dwarf2out.h"