OSDN Git Service

Daily bump.
[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
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
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 /* ??? Poison these here until it can be done generically.  They've been
116    totally replaced in this file; make sure it stays that way.  */
117 #undef DWARF2_UNWIND_INFO
118 #undef DWARF2_FRAME_INFO
119 #if (GCC_VERSION >= 3000)
120  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
121 #endif
122
123 #ifndef INCOMING_RETURN_ADDR_RTX
124 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
125 #endif
126
127 /* Map register numbers held in the call frame info that gcc has
128    collected using DWARF_FRAME_REGNUM to those that should be output in
129    .debug_frame and .eh_frame.  */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
133
134 /* Save the result of dwarf2out_do_frame across PCH.  */
135 static GTY(()) bool saved_do_cfi_asm = 0;
136
137 /* Decide whether we want to emit frame unwind information for the current
138    translation unit.  */
139
140 int
141 dwarf2out_do_frame (void)
142 {
143   /* We want to emit correct CFA location expressions or lists, so we
144      have to return true if we're going to output debug info, even if
145      we're not going to output frame or unwind info.  */
146   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
147     return true;
148
149   if (saved_do_cfi_asm)
150     return true;
151
152   if (targetm.debug_unwind_info () == UI_DWARF2)
153     return true;
154
155   if ((flag_unwind_tables || flag_exceptions)
156       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
157     return true;
158
159   return false;
160 }
161
162 /* Decide whether to emit frame unwind via assembler directives.  */
163
164 int
165 dwarf2out_do_cfi_asm (void)
166 {
167   int enc;
168
169 #ifdef MIPS_DEBUGGING_INFO
170   return false;
171 #endif
172   if (saved_do_cfi_asm)
173     return true;
174   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
175     return false;
176   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
177     return false;
178
179   /* Make sure the personality encoding is one the assembler can support.
180      In particular, aligned addresses can't be handled.  */
181   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
182   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
183     return false;
184   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
185   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
186     return false;
187
188   /* If we can't get the assembler to emit only .debug_frame, and we don't need
189      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
190   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
191       && !flag_unwind_tables && !flag_exceptions
192       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
193     return false;
194
195   saved_do_cfi_asm = true;
196   return true;
197 }
198
199 /* The size of the target's pointer type.  */
200 #ifndef PTR_SIZE
201 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
202 #endif
203
204 /* Array of RTXes referenced by the debugging information, which therefore
205    must be kept around forever.  */
206 static GTY(()) VEC(rtx,gc) *used_rtx_array;
207
208 /* A pointer to the base of a list of incomplete types which might be
209    completed at some later time.  incomplete_types_list needs to be a
210    VEC(tree,gc) because we want to tell the garbage collector about
211    it.  */
212 static GTY(()) VEC(tree,gc) *incomplete_types;
213
214 /* A pointer to the base of a table of references to declaration
215    scopes.  This table is a display which tracks the nesting
216    of declaration scopes at the current scope and containing
217    scopes.  This table is used to find the proper place to
218    define type declaration DIE's.  */
219 static GTY(()) VEC(tree,gc) *decl_scope_table;
220
221 /* Pointers to various DWARF2 sections.  */
222 static GTY(()) section *debug_info_section;
223 static GTY(()) section *debug_abbrev_section;
224 static GTY(()) section *debug_aranges_section;
225 static GTY(()) section *debug_macinfo_section;
226 static GTY(()) section *debug_line_section;
227 static GTY(()) section *debug_loc_section;
228 static GTY(()) section *debug_pubnames_section;
229 static GTY(()) section *debug_pubtypes_section;
230 static GTY(()) section *debug_dcall_section;
231 static GTY(()) section *debug_vcall_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
235
236 /* Personality decl of current unit.  Used only when assembler does not support
237    personality CFI.  */
238 static GTY(()) rtx current_unit_personality;
239
240 /* How to start an assembler comment.  */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
244
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248
249 /* Call frames are described using a sequence of Call Frame
250    Information instructions.  The register number, offset
251    and address fields are provided as possible operands;
252    their use is selected by the opcode field.  */
253
254 enum dw_cfi_oprnd_type {
255   dw_cfi_oprnd_unused,
256   dw_cfi_oprnd_reg_num,
257   dw_cfi_oprnd_offset,
258   dw_cfi_oprnd_addr,
259   dw_cfi_oprnd_loc
260 };
261
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 }
268 dw_cfi_oprnd;
269
270 typedef struct GTY(()) dw_cfi_struct {
271   dw_cfi_ref dw_cfi_next;
272   enum dwarf_call_frame_info dw_cfi_opc;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd1;
275   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
276     dw_cfi_oprnd2;
277 }
278 dw_cfi_node;
279
280 /* This is how we define the location of the CFA. We use to handle it
281    as REG + OFFSET all the time,  but now it can be more complex.
282    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
283    Instead of passing around REG and OFFSET, we pass a copy
284    of this structure.  */
285 typedef struct cfa_loc {
286   HOST_WIDE_INT offset;
287   HOST_WIDE_INT base_offset;
288   unsigned int reg;
289   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
290   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
291 } dw_cfa_location;
292
293 /* All call frame descriptions (FDE's) in the GCC generated DWARF
294    refer to a single Common Information Entry (CIE), defined at
295    the beginning of the .debug_frame section.  This use of a single
296    CIE obviates the need to keep track of multiple CIE's
297    in the DWARF generation routines below.  */
298
299 typedef struct GTY(()) dw_fde_struct {
300   tree decl;
301   const char *dw_fde_begin;
302   const char *dw_fde_current_label;
303   const char *dw_fde_end;
304   const char *dw_fde_vms_end_prologue;
305   const char *dw_fde_vms_begin_epilogue;
306   const char *dw_fde_second_begin;
307   const char *dw_fde_second_end;
308   dw_cfi_ref dw_fde_cfi;
309   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
310   HOST_WIDE_INT stack_realignment;
311   unsigned funcdef_number;
312   /* Dynamic realign argument pointer register.  */
313   unsigned int drap_reg;
314   /* Virtual dynamic realign argument pointer register.  */
315   unsigned int vdrap_reg;
316   /* These 3 flags are copied from rtl_data in function.h.  */
317   unsigned all_throwers_are_sibcalls : 1;
318   unsigned uses_eh_lsda : 1;
319   unsigned nothrow : 1;
320   /* Whether we did stack realign in this call frame.  */
321   unsigned stack_realign : 1;
322   /* Whether dynamic realign argument pointer register has been saved.  */
323   unsigned drap_reg_saved: 1;
324   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
325   unsigned in_std_section : 1;
326   /* True iff dw_fde_second_begin label is in text_section or
327      cold_text_section.  */
328   unsigned second_in_std_section : 1;
329 }
330 dw_fde_node;
331
332 /* Maximum size (in bytes) of an artificially generated label.  */
333 #define MAX_ARTIFICIAL_LABEL_BYTES      30
334
335 /* The size of addresses as they appear in the Dwarf 2 data.
336    Some architectures use word addresses to refer to code locations,
337    but Dwarf 2 info always uses byte addresses.  On such machines,
338    Dwarf 2 addresses need to be larger than the architecture's
339    pointers.  */
340 #ifndef DWARF2_ADDR_SIZE
341 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
342 #endif
343
344 /* The size in bytes of a DWARF field indicating an offset or length
345    relative to a debug info section, specified to be 4 bytes in the
346    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
347    as PTR_SIZE.  */
348
349 #ifndef DWARF_OFFSET_SIZE
350 #define DWARF_OFFSET_SIZE 4
351 #endif
352
353 /* The size in bytes of a DWARF 4 type signature.  */
354
355 #ifndef DWARF_TYPE_SIGNATURE_SIZE
356 #define DWARF_TYPE_SIGNATURE_SIZE 8
357 #endif
358
359 /* According to the (draft) DWARF 3 specification, the initial length
360    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
361    bytes are 0xffffffff, followed by the length stored in the next 8
362    bytes.
363
364    However, the SGI/MIPS ABI uses an initial length which is equal to
365    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
366
367 #ifndef DWARF_INITIAL_LENGTH_SIZE
368 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
369 #endif
370
371 /* Round SIZE up to the nearest BOUNDARY.  */
372 #define DWARF_ROUND(SIZE,BOUNDARY) \
373   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
374
375 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
376 #ifndef DWARF_CIE_DATA_ALIGNMENT
377 #ifdef STACK_GROWS_DOWNWARD
378 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
379 #else
380 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
381 #endif
382 #endif
383
384 /* CIE identifier.  */
385 #if HOST_BITS_PER_WIDE_INT >= 64
386 #define DWARF_CIE_ID \
387   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
388 #else
389 #define DWARF_CIE_ID DW_CIE_ID
390 #endif
391
392 /* A pointer to the base of a table that contains frame description
393    information for each routine.  */
394 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
395
396 /* Number of elements currently allocated for fde_table.  */
397 static GTY(()) unsigned fde_table_allocated;
398
399 /* Number of elements in fde_table currently in use.  */
400 static GTY(()) unsigned fde_table_in_use;
401
402 /* Size (in elements) of increments by which we may expand the
403    fde_table.  */
404 #define FDE_TABLE_INCREMENT 256
405
406 /* Get the current fde_table entry we should use.  */
407
408 static inline dw_fde_ref
409 current_fde (void)
410 {
411   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
412 }
413
414 /* A list of call frame insns for the CIE.  */
415 static GTY(()) dw_cfi_ref cie_cfi_head;
416
417 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
418    attribute that accelerates the lookup of the FDE associated
419    with the subprogram.  This variable holds the table index of the FDE
420    associated with the current function (body) definition.  */
421 static unsigned current_funcdef_fde;
422
423 struct GTY(()) indirect_string_node {
424   const char *str;
425   unsigned int refcount;
426   enum dwarf_form form;
427   char *label;
428 };
429
430 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
431
432 /* True if the compilation unit has location entries that reference
433    debug strings.  */
434 static GTY(()) bool debug_str_hash_forced = false;
435
436 static GTY(()) int dw2_string_counter;
437 static GTY(()) unsigned long dwarf2out_cfi_label_num;
438
439 /* True if the compilation unit places functions in more than one section.  */
440 static GTY(()) bool have_multiple_function_sections = false;
441
442 /* Whether the default text and cold text sections have been used at all.  */
443
444 static GTY(()) bool text_section_used = false;
445 static GTY(()) bool cold_text_section_used = false;
446
447 /* The default cold text section.  */
448 static GTY(()) section *cold_text_section;
449
450 /* Forward declarations for functions defined in this file.  */
451
452 static char *stripattributes (const char *);
453 static const char *dwarf_cfi_name (unsigned);
454 static dw_cfi_ref new_cfi (void);
455 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
456 static void add_fde_cfi (const char *, dw_cfi_ref);
457 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
458 static void lookup_cfa (dw_cfa_location *);
459 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
460 static void initial_return_save (rtx);
461 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
462                                           HOST_WIDE_INT);
463 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
464 static void output_cfi_directive (dw_cfi_ref);
465 static void output_call_frame_info (int);
466 static void dwarf2out_note_section_used (void);
467 static bool clobbers_queued_reg_save (const_rtx);
468 static void dwarf2out_frame_debug_expr (rtx, const char *);
469
470 /* Support for complex CFA locations.  */
471 static void output_cfa_loc (dw_cfi_ref, int);
472 static void output_cfa_loc_raw (dw_cfi_ref);
473 static void get_cfa_from_loc_descr (dw_cfa_location *,
474                                     struct dw_loc_descr_struct *);
475 static struct dw_loc_descr_struct *build_cfa_loc
476   (dw_cfa_location *, HOST_WIDE_INT);
477 static struct dw_loc_descr_struct *build_cfa_aligned_loc
478   (HOST_WIDE_INT, HOST_WIDE_INT);
479 static void def_cfa_1 (const char *, dw_cfa_location *);
480 static struct dw_loc_descr_struct *mem_loc_descriptor
481   (rtx, enum machine_mode mode, enum var_init_status);
482
483 /* How to start an assembler comment.  */
484 #ifndef ASM_COMMENT_START
485 #define ASM_COMMENT_START ";#"
486 #endif
487
488 /* Data and reference forms for relocatable data.  */
489 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
490 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
491
492 #ifndef DEBUG_FRAME_SECTION
493 #define DEBUG_FRAME_SECTION     ".debug_frame"
494 #endif
495
496 #ifndef FUNC_BEGIN_LABEL
497 #define FUNC_BEGIN_LABEL        "LFB"
498 #endif
499
500 #ifndef FUNC_END_LABEL
501 #define FUNC_END_LABEL          "LFE"
502 #endif
503
504 #ifndef PROLOGUE_END_LABEL
505 #define PROLOGUE_END_LABEL      "LPE"
506 #endif
507
508 #ifndef EPILOGUE_BEGIN_LABEL
509 #define EPILOGUE_BEGIN_LABEL    "LEB"
510 #endif
511
512 #ifndef FRAME_BEGIN_LABEL
513 #define FRAME_BEGIN_LABEL       "Lframe"
514 #endif
515 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
516 #define CIE_END_LABEL           "LECIE"
517 #define FDE_LABEL               "LSFDE"
518 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
519 #define FDE_END_LABEL           "LEFDE"
520 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
521 #define LINE_NUMBER_END_LABEL   "LELT"
522 #define LN_PROLOG_AS_LABEL      "LASLTP"
523 #define LN_PROLOG_END_LABEL     "LELTP"
524 #define DIE_LABEL_PREFIX        "DW"
525
526 /* The DWARF 2 CFA column which tracks the return address.  Normally this
527    is the column for PC, or the first column after all of the hard
528    registers.  */
529 #ifndef DWARF_FRAME_RETURN_COLUMN
530 #ifdef PC_REGNUM
531 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
532 #else
533 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
534 #endif
535 #endif
536
537 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
538    default, we just provide columns for all registers.  */
539 #ifndef DWARF_FRAME_REGNUM
540 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
541 #endif
542 \f
543 /* Match the base name of a file to the base name of a compilation unit. */
544
545 static int
546 matches_main_base (const char *path)
547 {
548   /* Cache the last query. */
549   static const char *last_path = NULL;
550   static int last_match = 0;
551   if (path != last_path)
552     {
553       const char *base;
554       int length = base_of_path (path, &base);
555       last_path = path;
556       last_match = (length == main_input_baselength
557                     && memcmp (base, main_input_basename, length) == 0);
558     }
559   return last_match;
560 }
561
562 #ifdef DEBUG_DEBUG_STRUCT
563
564 static int
565 dump_struct_debug (tree type, enum debug_info_usage usage,
566                    enum debug_struct_file criterion, int generic,
567                    int matches, int result)
568 {
569   /* Find the type name. */
570   tree type_decl = TYPE_STUB_DECL (type);
571   tree t = type_decl;
572   const char *name = 0;
573   if (TREE_CODE (t) == TYPE_DECL)
574     t = DECL_NAME (t);
575   if (t)
576     name = IDENTIFIER_POINTER (t);
577
578   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
579            criterion,
580            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
581            matches ? "bas" : "hdr",
582            generic ? "gen" : "ord",
583            usage == DINFO_USAGE_DFN ? ";" :
584              usage == DINFO_USAGE_DIR_USE ? "." : "*",
585            result,
586            (void*) type_decl, name);
587   return result;
588 }
589 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
590   dump_struct_debug (type, usage, criterion, generic, matches, result)
591
592 #else
593
594 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
595   (result)
596
597 #endif
598
599 static bool
600 should_emit_struct_debug (tree type, enum debug_info_usage usage)
601 {
602   enum debug_struct_file criterion;
603   tree type_decl;
604   bool generic = lang_hooks.types.generic_p (type);
605
606   if (generic)
607     criterion = debug_struct_generic[usage];
608   else
609     criterion = debug_struct_ordinary[usage];
610
611   if (criterion == DINFO_STRUCT_FILE_NONE)
612     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
613   if (criterion == DINFO_STRUCT_FILE_ANY)
614     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
615
616   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
617
618   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
619     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
620
621   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
622     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
623   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
624 }
625 \f
626 /* Hook used by __throw.  */
627
628 rtx
629 expand_builtin_dwarf_sp_column (void)
630 {
631   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
632   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
633 }
634
635 /* Return a pointer to a copy of the section string name S with all
636    attributes stripped off, and an asterisk prepended (for assemble_name).  */
637
638 static inline char *
639 stripattributes (const char *s)
640 {
641   char *stripped = XNEWVEC (char, strlen (s) + 2);
642   char *p = stripped;
643
644   *p++ = '*';
645
646   while (*s && *s != ',')
647     *p++ = *s++;
648
649   *p = '\0';
650   return stripped;
651 }
652
653 /* MEM is a memory reference for the register size table, each element of
654    which has mode MODE.  Initialize column C as a return address column.  */
655
656 static void
657 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
658 {
659   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
660   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
661   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
662 }
663
664 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
665
666 static inline HOST_WIDE_INT
667 div_data_align (HOST_WIDE_INT off)
668 {
669   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
670   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
671   return r;
672 }
673
674 /* Return true if we need a signed version of a given opcode
675    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
676
677 static inline bool
678 need_data_align_sf_opcode (HOST_WIDE_INT off)
679 {
680   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
681 }
682
683 /* Generate code to initialize the register size table.  */
684
685 void
686 expand_builtin_init_dwarf_reg_sizes (tree address)
687 {
688   unsigned int i;
689   enum machine_mode mode = TYPE_MODE (char_type_node);
690   rtx addr = expand_normal (address);
691   rtx mem = gen_rtx_MEM (BLKmode, addr);
692   bool wrote_return_column = false;
693
694   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
695     {
696       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
697
698       if (rnum < DWARF_FRAME_REGISTERS)
699         {
700           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
701           enum machine_mode save_mode = reg_raw_mode[i];
702           HOST_WIDE_INT size;
703
704           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
705             save_mode = choose_hard_reg_mode (i, 1, true);
706           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
707             {
708               if (save_mode == VOIDmode)
709                 continue;
710               wrote_return_column = true;
711             }
712           size = GET_MODE_SIZE (save_mode);
713           if (offset < 0)
714             continue;
715
716           emit_move_insn (adjust_address (mem, mode, offset),
717                           gen_int_mode (size, mode));
718         }
719     }
720
721   if (!wrote_return_column)
722     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
723
724 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
725   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
726 #endif
727
728   targetm.init_dwarf_reg_sizes_extra (address);
729 }
730
731 /* Convert a DWARF call frame info. operation to its string name */
732
733 static const char *
734 dwarf_cfi_name (unsigned int cfi_opc)
735 {
736   switch (cfi_opc)
737     {
738     case DW_CFA_advance_loc:
739       return "DW_CFA_advance_loc";
740     case DW_CFA_offset:
741       return "DW_CFA_offset";
742     case DW_CFA_restore:
743       return "DW_CFA_restore";
744     case DW_CFA_nop:
745       return "DW_CFA_nop";
746     case DW_CFA_set_loc:
747       return "DW_CFA_set_loc";
748     case DW_CFA_advance_loc1:
749       return "DW_CFA_advance_loc1";
750     case DW_CFA_advance_loc2:
751       return "DW_CFA_advance_loc2";
752     case DW_CFA_advance_loc4:
753       return "DW_CFA_advance_loc4";
754     case DW_CFA_offset_extended:
755       return "DW_CFA_offset_extended";
756     case DW_CFA_restore_extended:
757       return "DW_CFA_restore_extended";
758     case DW_CFA_undefined:
759       return "DW_CFA_undefined";
760     case DW_CFA_same_value:
761       return "DW_CFA_same_value";
762     case DW_CFA_register:
763       return "DW_CFA_register";
764     case DW_CFA_remember_state:
765       return "DW_CFA_remember_state";
766     case DW_CFA_restore_state:
767       return "DW_CFA_restore_state";
768     case DW_CFA_def_cfa:
769       return "DW_CFA_def_cfa";
770     case DW_CFA_def_cfa_register:
771       return "DW_CFA_def_cfa_register";
772     case DW_CFA_def_cfa_offset:
773       return "DW_CFA_def_cfa_offset";
774
775     /* DWARF 3 */
776     case DW_CFA_def_cfa_expression:
777       return "DW_CFA_def_cfa_expression";
778     case DW_CFA_expression:
779       return "DW_CFA_expression";
780     case DW_CFA_offset_extended_sf:
781       return "DW_CFA_offset_extended_sf";
782     case DW_CFA_def_cfa_sf:
783       return "DW_CFA_def_cfa_sf";
784     case DW_CFA_def_cfa_offset_sf:
785       return "DW_CFA_def_cfa_offset_sf";
786
787     /* SGI/MIPS specific */
788     case DW_CFA_MIPS_advance_loc8:
789       return "DW_CFA_MIPS_advance_loc8";
790
791     /* GNU extensions */
792     case DW_CFA_GNU_window_save:
793       return "DW_CFA_GNU_window_save";
794     case DW_CFA_GNU_args_size:
795       return "DW_CFA_GNU_args_size";
796     case DW_CFA_GNU_negative_offset_extended:
797       return "DW_CFA_GNU_negative_offset_extended";
798
799     default:
800       return "DW_CFA_<unknown>";
801     }
802 }
803
804 /* Return a pointer to a newly allocated Call Frame Instruction.  */
805
806 static inline dw_cfi_ref
807 new_cfi (void)
808 {
809   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
810
811   cfi->dw_cfi_next = NULL;
812   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
813   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
814
815   return cfi;
816 }
817
818 /* Add a Call Frame Instruction to list of instructions.  */
819
820 static inline void
821 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
822 {
823   dw_cfi_ref *p;
824   dw_fde_ref fde = current_fde ();
825
826   /* When DRAP is used, CFA is defined with an expression.  Redefine
827      CFA may lead to a different CFA value.   */
828   /* ??? Of course, this heuristic fails when we're annotating epilogues,
829      because of course we'll always want to redefine the CFA back to the
830      stack pointer on the way out.  Where should we move this check?  */
831   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
832     switch (cfi->dw_cfi_opc)
833       {
834         case DW_CFA_def_cfa_register:
835         case DW_CFA_def_cfa_offset:
836         case DW_CFA_def_cfa_offset_sf:
837         case DW_CFA_def_cfa:
838         case DW_CFA_def_cfa_sf:
839           gcc_unreachable ();
840
841         default:
842           break;
843       }
844
845   /* Find the end of the chain.  */
846   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
847     ;
848
849   *p = cfi;
850 }
851
852 /* Generate a new label for the CFI info to refer to.  FORCE is true
853    if a label needs to be output even when using .cfi_* directives.  */
854
855 char *
856 dwarf2out_cfi_label (bool force)
857 {
858   static char label[20];
859
860   if (!force && dwarf2out_do_cfi_asm ())
861     {
862       /* In this case, we will be emitting the asm directive instead of
863          the label, so just return a placeholder to keep the rest of the
864          interfaces happy.  */
865       strcpy (label, "<do not output>");
866     }
867   else
868     {
869       int num = dwarf2out_cfi_label_num++;
870       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
871       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
872     }
873
874   return label;
875 }
876
877 /* True if remember_state should be emitted before following CFI directive.  */
878 static bool emit_cfa_remember;
879
880 /* True if any CFI directives were emitted at the current insn.  */
881 static bool any_cfis_emitted;
882
883 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
884    or to the CIE if LABEL is NULL.  */
885
886 static void
887 add_fde_cfi (const char *label, dw_cfi_ref cfi)
888 {
889   dw_cfi_ref *list_head;
890
891   if (emit_cfa_remember)
892     {
893       dw_cfi_ref cfi_remember;
894
895       /* Emit the state save.  */
896       emit_cfa_remember = false;
897       cfi_remember = new_cfi ();
898       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
899       add_fde_cfi (label, cfi_remember);
900     }
901
902   list_head = &cie_cfi_head;
903
904   if (dwarf2out_do_cfi_asm ())
905     {
906       if (label)
907         {
908           dw_fde_ref fde = current_fde ();
909
910           gcc_assert (fde != NULL);
911
912           /* We still have to add the cfi to the list so that lookup_cfa
913              works later on.  When -g2 and above we even need to force
914              emitting of CFI labels and add to list a DW_CFA_set_loc for
915              convert_cfa_to_fb_loc_list purposes.  If we're generating
916              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
917              convert_cfa_to_fb_loc_list.  */
918           if (dwarf_version == 2
919               && debug_info_level > DINFO_LEVEL_TERSE
920               && (write_symbols == DWARF2_DEBUG
921                   || write_symbols == VMS_AND_DWARF2_DEBUG))
922             {
923               switch (cfi->dw_cfi_opc)
924                 {
925                 case DW_CFA_def_cfa_offset:
926                 case DW_CFA_def_cfa_offset_sf:
927                 case DW_CFA_def_cfa_register:
928                 case DW_CFA_def_cfa:
929                 case DW_CFA_def_cfa_sf:
930                 case DW_CFA_def_cfa_expression:
931                 case DW_CFA_restore_state:
932                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
933                     label = dwarf2out_cfi_label (true);
934
935                   if (fde->dw_fde_current_label == NULL
936                       || strcmp (label, fde->dw_fde_current_label) != 0)
937                     {
938                       dw_cfi_ref xcfi;
939
940                       label = xstrdup (label);
941
942                       /* Set the location counter to the new label.  */
943                       xcfi = new_cfi ();
944                       /* It doesn't metter whether DW_CFA_set_loc
945                          or DW_CFA_advance_loc4 is added here, those aren't
946                          emitted into assembly, only looked up by
947                          convert_cfa_to_fb_loc_list.  */
948                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
949                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
950                       add_cfi (&fde->dw_fde_cfi, xcfi);
951                       fde->dw_fde_current_label = label;
952                     }
953                   break;
954                 default:
955                   break;
956                 }
957             }
958
959           output_cfi_directive (cfi);
960
961           list_head = &fde->dw_fde_cfi;
962           any_cfis_emitted = true;
963         }
964       /* ??? If this is a CFI for the CIE, we don't emit.  This
965          assumes that the standard CIE contents that the assembler
966          uses matches the standard CIE contents that the compiler
967          uses.  This is probably a bad assumption.  I'm not quite
968          sure how to address this for now.  */
969     }
970   else if (label)
971     {
972       dw_fde_ref fde = current_fde ();
973
974       gcc_assert (fde != NULL);
975
976       if (*label == 0)
977         label = dwarf2out_cfi_label (false);
978
979       if (fde->dw_fde_current_label == NULL
980           || strcmp (label, fde->dw_fde_current_label) != 0)
981         {
982           dw_cfi_ref xcfi;
983
984           label = xstrdup (label);
985
986           /* Set the location counter to the new label.  */
987           xcfi = new_cfi ();
988           /* If we have a current label, advance from there, otherwise
989              set the location directly using set_loc.  */
990           xcfi->dw_cfi_opc = fde->dw_fde_current_label
991                              ? DW_CFA_advance_loc4
992                              : DW_CFA_set_loc;
993           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
994           add_cfi (&fde->dw_fde_cfi, xcfi);
995
996           fde->dw_fde_current_label = label;
997         }
998
999       list_head = &fde->dw_fde_cfi;
1000       any_cfis_emitted = true;
1001     }
1002
1003   add_cfi (list_head, cfi);
1004 }
1005
1006 /* Subroutine of lookup_cfa.  */
1007
1008 static void
1009 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1010 {
1011   switch (cfi->dw_cfi_opc)
1012     {
1013     case DW_CFA_def_cfa_offset:
1014     case DW_CFA_def_cfa_offset_sf:
1015       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1016       break;
1017     case DW_CFA_def_cfa_register:
1018       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1019       break;
1020     case DW_CFA_def_cfa:
1021     case DW_CFA_def_cfa_sf:
1022       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1023       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1024       break;
1025     case DW_CFA_def_cfa_expression:
1026       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1027       break;
1028
1029     case DW_CFA_remember_state:
1030       gcc_assert (!remember->in_use);
1031       *remember = *loc;
1032       remember->in_use = 1;
1033       break;
1034     case DW_CFA_restore_state:
1035       gcc_assert (remember->in_use);
1036       *loc = *remember;
1037       remember->in_use = 0;
1038       break;
1039
1040     default:
1041       break;
1042     }
1043 }
1044
1045 /* Find the previous value for the CFA.  */
1046
1047 static void
1048 lookup_cfa (dw_cfa_location *loc)
1049 {
1050   dw_cfi_ref cfi;
1051   dw_fde_ref fde;
1052   dw_cfa_location remember;
1053
1054   memset (loc, 0, sizeof (*loc));
1055   loc->reg = INVALID_REGNUM;
1056   remember = *loc;
1057
1058   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1059     lookup_cfa_1 (cfi, loc, &remember);
1060
1061   fde = current_fde ();
1062   if (fde)
1063     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1064       lookup_cfa_1 (cfi, loc, &remember);
1065 }
1066
1067 /* The current rule for calculating the DWARF2 canonical frame address.  */
1068 static dw_cfa_location cfa;
1069
1070 /* The register used for saving registers to the stack, and its offset
1071    from the CFA.  */
1072 static dw_cfa_location cfa_store;
1073
1074 /* The current save location around an epilogue.  */
1075 static dw_cfa_location cfa_remember;
1076
1077 /* The running total of the size of arguments pushed onto the stack.  */
1078 static HOST_WIDE_INT args_size;
1079
1080 /* The last args_size we actually output.  */
1081 static HOST_WIDE_INT old_args_size;
1082
1083 /* Entry point to update the canonical frame address (CFA).
1084    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1085    calculated from REG+OFFSET.  */
1086
1087 void
1088 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1089 {
1090   dw_cfa_location loc;
1091   loc.indirect = 0;
1092   loc.base_offset = 0;
1093   loc.reg = reg;
1094   loc.offset = offset;
1095   def_cfa_1 (label, &loc);
1096 }
1097
1098 /* Determine if two dw_cfa_location structures define the same data.  */
1099
1100 static bool
1101 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1102 {
1103   return (loc1->reg == loc2->reg
1104           && loc1->offset == loc2->offset
1105           && loc1->indirect == loc2->indirect
1106           && (loc1->indirect == 0
1107               || loc1->base_offset == loc2->base_offset));
1108 }
1109
1110 /* This routine does the actual work.  The CFA is now calculated from
1111    the dw_cfa_location structure.  */
1112
1113 static void
1114 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1115 {
1116   dw_cfi_ref cfi;
1117   dw_cfa_location old_cfa, loc;
1118
1119   cfa = *loc_p;
1120   loc = *loc_p;
1121
1122   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1123     cfa_store.offset = loc.offset;
1124
1125   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1126   lookup_cfa (&old_cfa);
1127
1128   /* If nothing changed, no need to issue any call frame instructions.  */
1129   if (cfa_equal_p (&loc, &old_cfa))
1130     return;
1131
1132   cfi = new_cfi ();
1133
1134   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1135     {
1136       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1137          the CFA register did not change but the offset did.  The data
1138          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1139          in the assembler via the .cfi_def_cfa_offset directive.  */
1140       if (loc.offset < 0)
1141         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1142       else
1143         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1144       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1145     }
1146
1147 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1148   else if (loc.offset == old_cfa.offset
1149            && old_cfa.reg != INVALID_REGNUM
1150            && !loc.indirect
1151            && !old_cfa.indirect)
1152     {
1153       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1154          indicating the CFA register has changed to <register> but the
1155          offset has not changed.  */
1156       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1157       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1158     }
1159 #endif
1160
1161   else if (loc.indirect == 0)
1162     {
1163       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1164          indicating the CFA register has changed to <register> with
1165          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1166          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1167          directive.  */
1168       if (loc.offset < 0)
1169         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1170       else
1171         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1172       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1173       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1174     }
1175   else
1176     {
1177       /* Construct a DW_CFA_def_cfa_expression instruction to
1178          calculate the CFA using a full location expression since no
1179          register-offset pair is available.  */
1180       struct dw_loc_descr_struct *loc_list;
1181
1182       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1183       loc_list = build_cfa_loc (&loc, 0);
1184       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1185     }
1186
1187   add_fde_cfi (label, cfi);
1188 }
1189
1190 /* Add the CFI for saving a register.  REG is the CFA column number.
1191    LABEL is passed to add_fde_cfi.
1192    If SREG is -1, the register is saved at OFFSET from the CFA;
1193    otherwise it is saved in SREG.  */
1194
1195 static void
1196 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1197 {
1198   dw_cfi_ref cfi = new_cfi ();
1199   dw_fde_ref fde = current_fde ();
1200
1201   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1202
1203   /* When stack is aligned, store REG using DW_CFA_expression with
1204      FP.  */
1205   if (fde
1206       && fde->stack_realign
1207       && sreg == INVALID_REGNUM)
1208     {
1209       cfi->dw_cfi_opc = DW_CFA_expression;
1210       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1211       cfi->dw_cfi_oprnd2.dw_cfi_loc
1212         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1213     }
1214   else if (sreg == INVALID_REGNUM)
1215     {
1216       if (need_data_align_sf_opcode (offset))
1217         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1218       else if (reg & ~0x3f)
1219         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1220       else
1221         cfi->dw_cfi_opc = DW_CFA_offset;
1222       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1223     }
1224   else if (sreg == reg)
1225     cfi->dw_cfi_opc = DW_CFA_same_value;
1226   else
1227     {
1228       cfi->dw_cfi_opc = DW_CFA_register;
1229       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1230     }
1231
1232   add_fde_cfi (label, cfi);
1233 }
1234
1235 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1236    This CFI tells the unwinder that it needs to restore the window registers
1237    from the previous frame's window save area.
1238
1239    ??? Perhaps we should note in the CIE where windows are saved (instead of
1240    assuming 0(cfa)) and what registers are in the window.  */
1241
1242 void
1243 dwarf2out_window_save (const char *label)
1244 {
1245   dw_cfi_ref cfi = new_cfi ();
1246
1247   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1248   add_fde_cfi (label, cfi);
1249 }
1250
1251 /* Entry point for saving a register to the stack.  REG is the GCC register
1252    number.  LABEL and OFFSET are passed to reg_save.  */
1253
1254 void
1255 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1256 {
1257   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1258 }
1259
1260 /* Entry point for saving the return address in the stack.
1261    LABEL and OFFSET are passed to reg_save.  */
1262
1263 void
1264 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1265 {
1266   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1267 }
1268
1269 /* Entry point for saving the return address in a register.
1270    LABEL and SREG are passed to reg_save.  */
1271
1272 void
1273 dwarf2out_return_reg (const char *label, unsigned int sreg)
1274 {
1275   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1276 }
1277
1278 /* Record the initial position of the return address.  RTL is
1279    INCOMING_RETURN_ADDR_RTX.  */
1280
1281 static void
1282 initial_return_save (rtx rtl)
1283 {
1284   unsigned int reg = INVALID_REGNUM;
1285   HOST_WIDE_INT offset = 0;
1286
1287   switch (GET_CODE (rtl))
1288     {
1289     case REG:
1290       /* RA is in a register.  */
1291       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1292       break;
1293
1294     case MEM:
1295       /* RA is on the stack.  */
1296       rtl = XEXP (rtl, 0);
1297       switch (GET_CODE (rtl))
1298         {
1299         case REG:
1300           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1301           offset = 0;
1302           break;
1303
1304         case PLUS:
1305           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1306           offset = INTVAL (XEXP (rtl, 1));
1307           break;
1308
1309         case MINUS:
1310           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1311           offset = -INTVAL (XEXP (rtl, 1));
1312           break;
1313
1314         default:
1315           gcc_unreachable ();
1316         }
1317
1318       break;
1319
1320     case PLUS:
1321       /* The return address is at some offset from any value we can
1322          actually load.  For instance, on the SPARC it is in %i7+8. Just
1323          ignore the offset for now; it doesn't matter for unwinding frames.  */
1324       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1325       initial_return_save (XEXP (rtl, 0));
1326       return;
1327
1328     default:
1329       gcc_unreachable ();
1330     }
1331
1332   if (reg != DWARF_FRAME_RETURN_COLUMN)
1333     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1334 }
1335
1336 /* Given a SET, calculate the amount of stack adjustment it
1337    contains.  */
1338
1339 static HOST_WIDE_INT
1340 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1341                      HOST_WIDE_INT cur_offset)
1342 {
1343   const_rtx src = SET_SRC (pattern);
1344   const_rtx dest = SET_DEST (pattern);
1345   HOST_WIDE_INT offset = 0;
1346   enum rtx_code code;
1347
1348   if (dest == stack_pointer_rtx)
1349     {
1350       code = GET_CODE (src);
1351
1352       /* Assume (set (reg sp) (reg whatever)) sets args_size
1353          level to 0.  */
1354       if (code == REG && src != stack_pointer_rtx)
1355         {
1356           offset = -cur_args_size;
1357 #ifndef STACK_GROWS_DOWNWARD
1358           offset = -offset;
1359 #endif
1360           return offset - cur_offset;
1361         }
1362
1363       if (! (code == PLUS || code == MINUS)
1364           || XEXP (src, 0) != stack_pointer_rtx
1365           || !CONST_INT_P (XEXP (src, 1)))
1366         return 0;
1367
1368       /* (set (reg sp) (plus (reg sp) (const_int))) */
1369       offset = INTVAL (XEXP (src, 1));
1370       if (code == PLUS)
1371         offset = -offset;
1372       return offset;
1373     }
1374
1375   if (MEM_P (src) && !MEM_P (dest))
1376     dest = src;
1377   if (MEM_P (dest))
1378     {
1379       /* (set (mem (pre_dec (reg sp))) (foo)) */
1380       src = XEXP (dest, 0);
1381       code = GET_CODE (src);
1382
1383       switch (code)
1384         {
1385         case PRE_MODIFY:
1386         case POST_MODIFY:
1387           if (XEXP (src, 0) == stack_pointer_rtx)
1388             {
1389               rtx val = XEXP (XEXP (src, 1), 1);
1390               /* We handle only adjustments by constant amount.  */
1391               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1392                           && CONST_INT_P (val));
1393               offset = -INTVAL (val);
1394               break;
1395             }
1396           return 0;
1397
1398         case PRE_DEC:
1399         case POST_DEC:
1400           if (XEXP (src, 0) == stack_pointer_rtx)
1401             {
1402               offset = GET_MODE_SIZE (GET_MODE (dest));
1403               break;
1404             }
1405           return 0;
1406
1407         case PRE_INC:
1408         case POST_INC:
1409           if (XEXP (src, 0) == stack_pointer_rtx)
1410             {
1411               offset = -GET_MODE_SIZE (GET_MODE (dest));
1412               break;
1413             }
1414           return 0;
1415
1416         default:
1417           return 0;
1418         }
1419     }
1420   else
1421     return 0;
1422
1423   return offset;
1424 }
1425
1426 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1427    indexed by INSN_UID.  */
1428
1429 static HOST_WIDE_INT *barrier_args_size;
1430
1431 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1432
1433 static HOST_WIDE_INT
1434 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1435                              VEC (rtx, heap) **next)
1436 {
1437   HOST_WIDE_INT offset = 0;
1438   int i;
1439
1440   if (! RTX_FRAME_RELATED_P (insn))
1441     {
1442       if (prologue_epilogue_contains (insn))
1443         /* Nothing */;
1444       else if (GET_CODE (PATTERN (insn)) == SET)
1445         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1446       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1447                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1448         {
1449           /* There may be stack adjustments inside compound insns.  Search
1450              for them.  */
1451           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1452             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1453               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1454                                              cur_args_size, offset);
1455         }
1456     }
1457   else
1458     {
1459       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1460
1461       if (expr)
1462         {
1463           expr = XEXP (expr, 0);
1464           if (GET_CODE (expr) == PARALLEL
1465               || GET_CODE (expr) == SEQUENCE)
1466             for (i = 1; i < XVECLEN (expr, 0); i++)
1467               {
1468                 rtx elem = XVECEXP (expr, 0, i);
1469
1470                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1471                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1472               }
1473         }
1474     }
1475
1476 #ifndef STACK_GROWS_DOWNWARD
1477   offset = -offset;
1478 #endif
1479
1480   cur_args_size += offset;
1481   if (cur_args_size < 0)
1482     cur_args_size = 0;
1483
1484   if (JUMP_P (insn))
1485     {
1486       rtx dest = JUMP_LABEL (insn);
1487
1488       if (dest)
1489         {
1490           if (barrier_args_size [INSN_UID (dest)] < 0)
1491             {
1492               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1493               VEC_safe_push (rtx, heap, *next, dest);
1494             }
1495         }
1496     }
1497
1498   return cur_args_size;
1499 }
1500
1501 /* Walk the whole function and compute args_size on BARRIERs.  */
1502
1503 static void
1504 compute_barrier_args_size (void)
1505 {
1506   int max_uid = get_max_uid (), i;
1507   rtx insn;
1508   VEC (rtx, heap) *worklist, *next, *tmp;
1509
1510   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1511   for (i = 0; i < max_uid; i++)
1512     barrier_args_size[i] = -1;
1513
1514   worklist = VEC_alloc (rtx, heap, 20);
1515   next = VEC_alloc (rtx, heap, 20);
1516   insn = get_insns ();
1517   barrier_args_size[INSN_UID (insn)] = 0;
1518   VEC_quick_push (rtx, worklist, insn);
1519   for (;;)
1520     {
1521       while (!VEC_empty (rtx, worklist))
1522         {
1523           rtx prev, body, first_insn;
1524           HOST_WIDE_INT cur_args_size;
1525
1526           first_insn = insn = VEC_pop (rtx, worklist);
1527           cur_args_size = barrier_args_size[INSN_UID (insn)];
1528           prev = prev_nonnote_insn (insn);
1529           if (prev && BARRIER_P (prev))
1530             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1531
1532           for (; insn; insn = NEXT_INSN (insn))
1533             {
1534               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1535                 continue;
1536               if (BARRIER_P (insn))
1537                 break;
1538
1539               if (LABEL_P (insn))
1540                 {
1541                   if (insn == first_insn)
1542                     continue;
1543                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1544                     {
1545                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1546                       continue;
1547                     }
1548                   else
1549                     {
1550                       /* The insns starting with this label have been
1551                          already scanned or are in the worklist.  */
1552                       break;
1553                     }
1554                 }
1555
1556               body = PATTERN (insn);
1557               if (GET_CODE (body) == SEQUENCE)
1558                 {
1559                   HOST_WIDE_INT dest_args_size = cur_args_size;
1560                   for (i = 1; i < XVECLEN (body, 0); i++)
1561                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1562                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1563                       dest_args_size
1564                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1565                                                        dest_args_size, &next);
1566                     else
1567                       cur_args_size
1568                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1569                                                        cur_args_size, &next);
1570
1571                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1572                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1573                                                  dest_args_size, &next);
1574                   else
1575                     cur_args_size
1576                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1577                                                      cur_args_size, &next);
1578                 }
1579               else
1580                 cur_args_size
1581                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1582             }
1583         }
1584
1585       if (VEC_empty (rtx, next))
1586         break;
1587
1588       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1589       tmp = next;
1590       next = worklist;
1591       worklist = tmp;
1592       VEC_truncate (rtx, next, 0);
1593     }
1594
1595   VEC_free (rtx, heap, worklist);
1596   VEC_free (rtx, heap, next);
1597 }
1598
1599 /* Add a CFI to update the running total of the size of arguments
1600    pushed onto the stack.  */
1601
1602 static void
1603 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1604 {
1605   dw_cfi_ref cfi;
1606
1607   if (size == old_args_size)
1608     return;
1609
1610   old_args_size = size;
1611
1612   cfi = new_cfi ();
1613   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1614   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1615   add_fde_cfi (label, cfi);
1616 }
1617
1618 /* Record a stack adjustment of OFFSET bytes.  */
1619
1620 static void
1621 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1622 {
1623   if (cfa.reg == STACK_POINTER_REGNUM)
1624     cfa.offset += offset;
1625
1626   if (cfa_store.reg == STACK_POINTER_REGNUM)
1627     cfa_store.offset += offset;
1628
1629   if (ACCUMULATE_OUTGOING_ARGS)
1630     return;
1631
1632 #ifndef STACK_GROWS_DOWNWARD
1633   offset = -offset;
1634 #endif
1635
1636   args_size += offset;
1637   if (args_size < 0)
1638     args_size = 0;
1639
1640   def_cfa_1 (label, &cfa);
1641   if (flag_asynchronous_unwind_tables)
1642     dwarf2out_args_size (label, args_size);
1643 }
1644
1645 /* Check INSN to see if it looks like a push or a stack adjustment, and
1646    make a note of it if it does.  EH uses this information to find out
1647    how much extra space it needs to pop off the stack.  */
1648
1649 static void
1650 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1651 {
1652   HOST_WIDE_INT offset;
1653   const char *label;
1654   int i;
1655
1656   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1657      with this function.  Proper support would require all frame-related
1658      insns to be marked, and to be able to handle saving state around
1659      epilogues textually in the middle of the function.  */
1660   if (prologue_epilogue_contains (insn))
1661     return;
1662
1663   /* If INSN is an instruction from target of an annulled branch, the
1664      effects are for the target only and so current argument size
1665      shouldn't change at all.  */
1666   if (final_sequence
1667       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1668       && INSN_FROM_TARGET_P (insn))
1669     return;
1670
1671   /* If only calls can throw, and we have a frame pointer,
1672      save up adjustments until we see the CALL_INSN.  */
1673   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1674     {
1675       if (CALL_P (insn) && !after_p)
1676         {
1677           /* Extract the size of the args from the CALL rtx itself.  */
1678           insn = PATTERN (insn);
1679           if (GET_CODE (insn) == PARALLEL)
1680             insn = XVECEXP (insn, 0, 0);
1681           if (GET_CODE (insn) == SET)
1682             insn = SET_SRC (insn);
1683           gcc_assert (GET_CODE (insn) == CALL);
1684           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1685         }
1686       return;
1687     }
1688
1689   if (CALL_P (insn) && !after_p)
1690     {
1691       if (!flag_asynchronous_unwind_tables)
1692         dwarf2out_args_size ("", args_size);
1693       return;
1694     }
1695   else if (BARRIER_P (insn))
1696     {
1697       /* Don't call compute_barrier_args_size () if the only
1698          BARRIER is at the end of function.  */
1699       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1700         compute_barrier_args_size ();
1701       if (barrier_args_size == NULL)
1702         offset = 0;
1703       else
1704         {
1705           offset = barrier_args_size[INSN_UID (insn)];
1706           if (offset < 0)
1707             offset = 0;
1708         }
1709
1710       offset -= args_size;
1711 #ifndef STACK_GROWS_DOWNWARD
1712       offset = -offset;
1713 #endif
1714     }
1715   else if (GET_CODE (PATTERN (insn)) == SET)
1716     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1717   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1718            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1719     {
1720       /* There may be stack adjustments inside compound insns.  Search
1721          for them.  */
1722       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1723         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1724           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1725                                          args_size, offset);
1726     }
1727   else
1728     return;
1729
1730   if (offset == 0)
1731     return;
1732
1733   label = dwarf2out_cfi_label (false);
1734   dwarf2out_stack_adjust (offset, label);
1735 }
1736
1737 /* We delay emitting a register save until either (a) we reach the end
1738    of the prologue or (b) the register is clobbered.  This clusters
1739    register saves so that there are fewer pc advances.  */
1740
1741 struct GTY(()) queued_reg_save {
1742   struct queued_reg_save *next;
1743   rtx reg;
1744   HOST_WIDE_INT cfa_offset;
1745   rtx saved_reg;
1746 };
1747
1748 static GTY(()) struct queued_reg_save *queued_reg_saves;
1749
1750 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1751 struct GTY(()) reg_saved_in_data {
1752   rtx orig_reg;
1753   rtx saved_in_reg;
1754 };
1755
1756 /* A list of registers saved in other registers.
1757    The list intentionally has a small maximum capacity of 4; if your
1758    port needs more than that, you might consider implementing a
1759    more efficient data structure.  */
1760 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1761 static GTY(()) size_t num_regs_saved_in_regs;
1762
1763 static const char *last_reg_save_label;
1764
1765 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1766    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1767
1768 static void
1769 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1770 {
1771   struct queued_reg_save *q;
1772
1773   /* Duplicates waste space, but it's also necessary to remove them
1774      for correctness, since the queue gets output in reverse
1775      order.  */
1776   for (q = queued_reg_saves; q != NULL; q = q->next)
1777     if (REGNO (q->reg) == REGNO (reg))
1778       break;
1779
1780   if (q == NULL)
1781     {
1782       q = ggc_alloc_queued_reg_save ();
1783       q->next = queued_reg_saves;
1784       queued_reg_saves = q;
1785     }
1786
1787   q->reg = reg;
1788   q->cfa_offset = offset;
1789   q->saved_reg = sreg;
1790
1791   last_reg_save_label = label;
1792 }
1793
1794 /* Output all the entries in QUEUED_REG_SAVES.  */
1795
1796 void
1797 dwarf2out_flush_queued_reg_saves (void)
1798 {
1799   struct queued_reg_save *q;
1800
1801   for (q = queued_reg_saves; q; q = q->next)
1802     {
1803       size_t i;
1804       unsigned int reg, sreg;
1805
1806       for (i = 0; i < num_regs_saved_in_regs; i++)
1807         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1808           break;
1809       if (q->saved_reg && i == num_regs_saved_in_regs)
1810         {
1811           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1812           num_regs_saved_in_regs++;
1813         }
1814       if (i != num_regs_saved_in_regs)
1815         {
1816           regs_saved_in_regs[i].orig_reg = q->reg;
1817           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1818         }
1819
1820       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1821       if (q->saved_reg)
1822         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1823       else
1824         sreg = INVALID_REGNUM;
1825       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1826     }
1827
1828   queued_reg_saves = NULL;
1829   last_reg_save_label = NULL;
1830 }
1831
1832 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1833    location for?  Or, does it clobber a register which we've previously
1834    said that some other register is saved in, and for which we now
1835    have a new location for?  */
1836
1837 static bool
1838 clobbers_queued_reg_save (const_rtx insn)
1839 {
1840   struct queued_reg_save *q;
1841
1842   for (q = queued_reg_saves; q; q = q->next)
1843     {
1844       size_t i;
1845       if (modified_in_p (q->reg, insn))
1846         return true;
1847       for (i = 0; i < num_regs_saved_in_regs; i++)
1848         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1849             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1850           return true;
1851     }
1852
1853   return false;
1854 }
1855
1856 /* Entry point for saving the first register into the second.  */
1857
1858 void
1859 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1860 {
1861   size_t i;
1862   unsigned int regno, sregno;
1863
1864   for (i = 0; i < num_regs_saved_in_regs; i++)
1865     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1866       break;
1867   if (i == num_regs_saved_in_regs)
1868     {
1869       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1870       num_regs_saved_in_regs++;
1871     }
1872   regs_saved_in_regs[i].orig_reg = reg;
1873   regs_saved_in_regs[i].saved_in_reg = sreg;
1874
1875   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1876   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1877   reg_save (label, regno, sregno, 0);
1878 }
1879
1880 /* What register, if any, is currently saved in REG?  */
1881
1882 static rtx
1883 reg_saved_in (rtx reg)
1884 {
1885   unsigned int regn = REGNO (reg);
1886   size_t i;
1887   struct queued_reg_save *q;
1888
1889   for (q = queued_reg_saves; q; q = q->next)
1890     if (q->saved_reg && regn == REGNO (q->saved_reg))
1891       return q->reg;
1892
1893   for (i = 0; i < num_regs_saved_in_regs; i++)
1894     if (regs_saved_in_regs[i].saved_in_reg
1895         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1896       return regs_saved_in_regs[i].orig_reg;
1897
1898   return NULL_RTX;
1899 }
1900
1901
1902 /* A temporary register holding an integral value used in adjusting SP
1903    or setting up the store_reg.  The "offset" field holds the integer
1904    value, not an offset.  */
1905 static dw_cfa_location cfa_temp;
1906
1907 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1908
1909 static void
1910 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1911 {
1912   memset (&cfa, 0, sizeof (cfa));
1913
1914   switch (GET_CODE (pat))
1915     {
1916     case PLUS:
1917       cfa.reg = REGNO (XEXP (pat, 0));
1918       cfa.offset = INTVAL (XEXP (pat, 1));
1919       break;
1920
1921     case REG:
1922       cfa.reg = REGNO (pat);
1923       break;
1924
1925     case MEM:
1926       cfa.indirect = 1;
1927       pat = XEXP (pat, 0);
1928       if (GET_CODE (pat) == PLUS)
1929         {
1930           cfa.base_offset = INTVAL (XEXP (pat, 1));
1931           pat = XEXP (pat, 0);
1932         }
1933       cfa.reg = REGNO (pat);
1934       break;
1935
1936     default:
1937       /* Recurse and define an expression.  */
1938       gcc_unreachable ();
1939     }
1940
1941   def_cfa_1 (label, &cfa);
1942 }
1943
1944 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1945
1946 static void
1947 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1948 {
1949   rtx src, dest;
1950
1951   gcc_assert (GET_CODE (pat) == SET);
1952   dest = XEXP (pat, 0);
1953   src = XEXP (pat, 1);
1954
1955   switch (GET_CODE (src))
1956     {
1957     case PLUS:
1958       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1959       cfa.offset -= INTVAL (XEXP (src, 1));
1960       break;
1961
1962     case REG:
1963         break;
1964
1965     default:
1966         gcc_unreachable ();
1967     }
1968
1969   cfa.reg = REGNO (dest);
1970   gcc_assert (cfa.indirect == 0);
1971
1972   def_cfa_1 (label, &cfa);
1973 }
1974
1975 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1976
1977 static void
1978 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1979 {
1980   HOST_WIDE_INT offset;
1981   rtx src, addr, span;
1982
1983   src = XEXP (set, 1);
1984   addr = XEXP (set, 0);
1985   gcc_assert (MEM_P (addr));
1986   addr = XEXP (addr, 0);
1987
1988   /* As documented, only consider extremely simple addresses.  */
1989   switch (GET_CODE (addr))
1990     {
1991     case REG:
1992       gcc_assert (REGNO (addr) == cfa.reg);
1993       offset = -cfa.offset;
1994       break;
1995     case PLUS:
1996       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1997       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1998       break;
1999     default:
2000       gcc_unreachable ();
2001     }
2002
2003   span = targetm.dwarf_register_span (src);
2004
2005   /* ??? We'd like to use queue_reg_save, but we need to come up with
2006      a different flushing heuristic for epilogues.  */
2007   if (!span)
2008     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2009   else
2010     {
2011       /* We have a PARALLEL describing where the contents of SRC live.
2012          Queue register saves for each piece of the PARALLEL.  */
2013       int par_index;
2014       int limit;
2015       HOST_WIDE_INT span_offset = offset;
2016
2017       gcc_assert (GET_CODE (span) == PARALLEL);
2018
2019       limit = XVECLEN (span, 0);
2020       for (par_index = 0; par_index < limit; par_index++)
2021         {
2022           rtx elem = XVECEXP (span, 0, par_index);
2023
2024           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2025                     INVALID_REGNUM, span_offset);
2026           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2027         }
2028     }
2029 }
2030
2031 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2032
2033 static void
2034 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2035 {
2036   rtx src, dest;
2037   unsigned sregno, dregno;
2038
2039   src = XEXP (set, 1);
2040   dest = XEXP (set, 0);
2041
2042   if (src == pc_rtx)
2043     sregno = DWARF_FRAME_RETURN_COLUMN;
2044   else
2045     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2046
2047   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2048
2049   /* ??? We'd like to use queue_reg_save, but we need to come up with
2050      a different flushing heuristic for epilogues.  */
2051   reg_save (label, sregno, dregno, 0);
2052 }
2053
2054 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2055
2056 static void
2057 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2058 {
2059   rtx src, dest, span;
2060   dw_cfi_ref cfi = new_cfi ();
2061
2062   dest = SET_DEST (set);
2063   src = SET_SRC (set);
2064
2065   gcc_assert (REG_P (src));
2066   gcc_assert (MEM_P (dest));
2067
2068   span = targetm.dwarf_register_span (src);
2069   gcc_assert (!span);
2070
2071   cfi->dw_cfi_opc = DW_CFA_expression;
2072   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2073   cfi->dw_cfi_oprnd2.dw_cfi_loc
2074     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2075                           VAR_INIT_STATUS_INITIALIZED);
2076
2077   /* ??? We'd like to use queue_reg_save, were the interface different,
2078      and, as above, we could manage flushing for epilogues.  */
2079   add_fde_cfi (label, cfi);
2080 }
2081
2082 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2083
2084 static void
2085 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2086 {
2087   dw_cfi_ref cfi = new_cfi ();
2088   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2089
2090   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2091   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2092
2093   add_fde_cfi (label, cfi);
2094 }
2095
2096 /* Record call frame debugging information for an expression EXPR,
2097    which either sets SP or FP (adjusting how we calculate the frame
2098    address) or saves a register to the stack or another register.
2099    LABEL indicates the address of EXPR.
2100
2101    This function encodes a state machine mapping rtxes to actions on
2102    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2103    users need not read the source code.
2104
2105   The High-Level Picture
2106
2107   Changes in the register we use to calculate the CFA: Currently we
2108   assume that if you copy the CFA register into another register, we
2109   should take the other one as the new CFA register; this seems to
2110   work pretty well.  If it's wrong for some target, it's simple
2111   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2112
2113   Changes in the register we use for saving registers to the stack:
2114   This is usually SP, but not always.  Again, we deduce that if you
2115   copy SP into another register (and SP is not the CFA register),
2116   then the new register is the one we will be using for register
2117   saves.  This also seems to work.
2118
2119   Register saves: There's not much guesswork about this one; if
2120   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2121   register save, and the register used to calculate the destination
2122   had better be the one we think we're using for this purpose.
2123   It's also assumed that a copy from a call-saved register to another
2124   register is saving that register if RTX_FRAME_RELATED_P is set on
2125   that instruction.  If the copy is from a call-saved register to
2126   the *same* register, that means that the register is now the same
2127   value as in the caller.
2128
2129   Except: If the register being saved is the CFA register, and the
2130   offset is nonzero, we are saving the CFA, so we assume we have to
2131   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2132   the intent is to save the value of SP from the previous frame.
2133
2134   In addition, if a register has previously been saved to a different
2135   register,
2136
2137   Invariants / Summaries of Rules
2138
2139   cfa          current rule for calculating the CFA.  It usually
2140                consists of a register and an offset.
2141   cfa_store    register used by prologue code to save things to the stack
2142                cfa_store.offset is the offset from the value of
2143                cfa_store.reg to the actual CFA
2144   cfa_temp     register holding an integral value.  cfa_temp.offset
2145                stores the value, which will be used to adjust the
2146                stack pointer.  cfa_temp is also used like cfa_store,
2147                to track stores to the stack via fp or a temp reg.
2148
2149   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2150                with cfa.reg as the first operand changes the cfa.reg and its
2151                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2152                cfa_temp.offset.
2153
2154   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2155                expression yielding a constant.  This sets cfa_temp.reg
2156                and cfa_temp.offset.
2157
2158   Rule 5:      Create a new register cfa_store used to save items to the
2159                stack.
2160
2161   Rules 10-14: Save a register to the stack.  Define offset as the
2162                difference of the original location and cfa_store's
2163                location (or cfa_temp's location if cfa_temp is used).
2164
2165   Rules 16-20: If AND operation happens on sp in prologue, we assume
2166                stack is realigned.  We will use a group of DW_OP_XXX
2167                expressions to represent the location of the stored
2168                register instead of CFA+offset.
2169
2170   The Rules
2171
2172   "{a,b}" indicates a choice of a xor b.
2173   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2174
2175   Rule 1:
2176   (set <reg1> <reg2>:cfa.reg)
2177   effects: cfa.reg = <reg1>
2178            cfa.offset unchanged
2179            cfa_temp.reg = <reg1>
2180            cfa_temp.offset = cfa.offset
2181
2182   Rule 2:
2183   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2184                               {<const_int>,<reg>:cfa_temp.reg}))
2185   effects: cfa.reg = sp if fp used
2186            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2187            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2188              if cfa_store.reg==sp
2189
2190   Rule 3:
2191   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2192   effects: cfa.reg = fp
2193            cfa_offset += +/- <const_int>
2194
2195   Rule 4:
2196   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2197   constraints: <reg1> != fp
2198                <reg1> != sp
2199   effects: cfa.reg = <reg1>
2200            cfa_temp.reg = <reg1>
2201            cfa_temp.offset = cfa.offset
2202
2203   Rule 5:
2204   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2205   constraints: <reg1> != fp
2206                <reg1> != sp
2207   effects: cfa_store.reg = <reg1>
2208            cfa_store.offset = cfa.offset - cfa_temp.offset
2209
2210   Rule 6:
2211   (set <reg> <const_int>)
2212   effects: cfa_temp.reg = <reg>
2213            cfa_temp.offset = <const_int>
2214
2215   Rule 7:
2216   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2217   effects: cfa_temp.reg = <reg1>
2218            cfa_temp.offset |= <const_int>
2219
2220   Rule 8:
2221   (set <reg> (high <exp>))
2222   effects: none
2223
2224   Rule 9:
2225   (set <reg> (lo_sum <exp> <const_int>))
2226   effects: cfa_temp.reg = <reg>
2227            cfa_temp.offset = <const_int>
2228
2229   Rule 10:
2230   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2231   effects: cfa_store.offset -= <const_int>
2232            cfa.offset = cfa_store.offset if cfa.reg == sp
2233            cfa.reg = sp
2234            cfa.base_offset = -cfa_store.offset
2235
2236   Rule 11:
2237   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2238   effects: cfa_store.offset += -/+ mode_size(mem)
2239            cfa.offset = cfa_store.offset if cfa.reg == sp
2240            cfa.reg = sp
2241            cfa.base_offset = -cfa_store.offset
2242
2243   Rule 12:
2244   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2245
2246        <reg2>)
2247   effects: cfa.reg = <reg1>
2248            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2249
2250   Rule 13:
2251   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2252   effects: cfa.reg = <reg1>
2253            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2254
2255   Rule 14:
2256   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2257   effects: cfa.reg = <reg1>
2258            cfa.base_offset = -cfa_temp.offset
2259            cfa_temp.offset -= mode_size(mem)
2260
2261   Rule 15:
2262   (set <reg> {unspec, unspec_volatile})
2263   effects: target-dependent
2264
2265   Rule 16:
2266   (set sp (and: sp <const_int>))
2267   constraints: cfa_store.reg == sp
2268   effects: current_fde.stack_realign = 1
2269            cfa_store.offset = 0
2270            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2271
2272   Rule 17:
2273   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2274   effects: cfa_store.offset += -/+ mode_size(mem)
2275
2276   Rule 18:
2277   (set (mem ({pre_inc, pre_dec} sp)) fp)
2278   constraints: fde->stack_realign == 1
2279   effects: cfa_store.offset = 0
2280            cfa.reg != HARD_FRAME_POINTER_REGNUM
2281
2282   Rule 19:
2283   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2284   constraints: fde->stack_realign == 1
2285                && cfa.offset == 0
2286                && cfa.indirect == 0
2287                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2288   effects: Use DW_CFA_def_cfa_expression to define cfa
2289            cfa.reg == fde->drap_reg  */
2290
2291 static void
2292 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2293 {
2294   rtx src, dest, span;
2295   HOST_WIDE_INT offset;
2296   dw_fde_ref fde;
2297
2298   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2299      the PARALLEL independently. The first element is always processed if
2300      it is a SET. This is for backward compatibility.   Other elements
2301      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2302      flag is set in them.  */
2303   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2304     {
2305       int par_index;
2306       int limit = XVECLEN (expr, 0);
2307       rtx elem;
2308
2309       /* PARALLELs have strict read-modify-write semantics, so we
2310          ought to evaluate every rvalue before changing any lvalue.
2311          It's cumbersome to do that in general, but there's an
2312          easy approximation that is enough for all current users:
2313          handle register saves before register assignments.  */
2314       if (GET_CODE (expr) == PARALLEL)
2315         for (par_index = 0; par_index < limit; par_index++)
2316           {
2317             elem = XVECEXP (expr, 0, par_index);
2318             if (GET_CODE (elem) == SET
2319                 && MEM_P (SET_DEST (elem))
2320                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2321               dwarf2out_frame_debug_expr (elem, label);
2322           }
2323
2324       for (par_index = 0; par_index < limit; par_index++)
2325         {
2326           elem = XVECEXP (expr, 0, par_index);
2327           if (GET_CODE (elem) == SET
2328               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2329               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2330             dwarf2out_frame_debug_expr (elem, label);
2331           else if (GET_CODE (elem) == SET
2332                    && par_index != 0
2333                    && !RTX_FRAME_RELATED_P (elem))
2334             {
2335               /* Stack adjustment combining might combine some post-prologue
2336                  stack adjustment into a prologue stack adjustment.  */
2337               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2338
2339               if (offset != 0)
2340                 dwarf2out_stack_adjust (offset, label);
2341             }
2342         }
2343       return;
2344     }
2345
2346   gcc_assert (GET_CODE (expr) == SET);
2347
2348   src = SET_SRC (expr);
2349   dest = SET_DEST (expr);
2350
2351   if (REG_P (src))
2352     {
2353       rtx rsi = reg_saved_in (src);
2354       if (rsi)
2355         src = rsi;
2356     }
2357
2358   fde = current_fde ();
2359
2360   switch (GET_CODE (dest))
2361     {
2362     case REG:
2363       switch (GET_CODE (src))
2364         {
2365           /* Setting FP from SP.  */
2366         case REG:
2367           if (cfa.reg == (unsigned) REGNO (src))
2368             {
2369               /* Rule 1 */
2370               /* Update the CFA rule wrt SP or FP.  Make sure src is
2371                  relative to the current CFA register.
2372
2373                  We used to require that dest be either SP or FP, but the
2374                  ARM copies SP to a temporary register, and from there to
2375                  FP.  So we just rely on the backends to only set
2376                  RTX_FRAME_RELATED_P on appropriate insns.  */
2377               cfa.reg = REGNO (dest);
2378               cfa_temp.reg = cfa.reg;
2379               cfa_temp.offset = cfa.offset;
2380             }
2381           else
2382             {
2383               /* Saving a register in a register.  */
2384               gcc_assert (!fixed_regs [REGNO (dest)]
2385                           /* For the SPARC and its register window.  */
2386                           || (DWARF_FRAME_REGNUM (REGNO (src))
2387                               == DWARF_FRAME_RETURN_COLUMN));
2388
2389               /* After stack is aligned, we can only save SP in FP
2390                  if drap register is used.  In this case, we have
2391                  to restore stack pointer with the CFA value and we
2392                  don't generate this DWARF information.  */
2393               if (fde
2394                   && fde->stack_realign
2395                   && REGNO (src) == STACK_POINTER_REGNUM)
2396                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2397                             && fde->drap_reg != INVALID_REGNUM
2398                             && cfa.reg != REGNO (src));
2399               else
2400                 queue_reg_save (label, src, dest, 0);
2401             }
2402           break;
2403
2404         case PLUS:
2405         case MINUS:
2406         case LO_SUM:
2407           if (dest == stack_pointer_rtx)
2408             {
2409               /* Rule 2 */
2410               /* Adjusting SP.  */
2411               switch (GET_CODE (XEXP (src, 1)))
2412                 {
2413                 case CONST_INT:
2414                   offset = INTVAL (XEXP (src, 1));
2415                   break;
2416                 case REG:
2417                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2418                               == cfa_temp.reg);
2419                   offset = cfa_temp.offset;
2420                   break;
2421                 default:
2422                   gcc_unreachable ();
2423                 }
2424
2425               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2426                 {
2427                   /* Restoring SP from FP in the epilogue.  */
2428                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2429                   cfa.reg = STACK_POINTER_REGNUM;
2430                 }
2431               else if (GET_CODE (src) == LO_SUM)
2432                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2433                 ;
2434               else
2435                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2436
2437               if (GET_CODE (src) != MINUS)
2438                 offset = -offset;
2439               if (cfa.reg == STACK_POINTER_REGNUM)
2440                 cfa.offset += offset;
2441               if (cfa_store.reg == STACK_POINTER_REGNUM)
2442                 cfa_store.offset += offset;
2443             }
2444           else if (dest == hard_frame_pointer_rtx)
2445             {
2446               /* Rule 3 */
2447               /* Either setting the FP from an offset of the SP,
2448                  or adjusting the FP */
2449               gcc_assert (frame_pointer_needed);
2450
2451               gcc_assert (REG_P (XEXP (src, 0))
2452                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2453                           && CONST_INT_P (XEXP (src, 1)));
2454               offset = INTVAL (XEXP (src, 1));
2455               if (GET_CODE (src) != MINUS)
2456                 offset = -offset;
2457               cfa.offset += offset;
2458               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2459             }
2460           else
2461             {
2462               gcc_assert (GET_CODE (src) != MINUS);
2463
2464               /* Rule 4 */
2465               if (REG_P (XEXP (src, 0))
2466                   && REGNO (XEXP (src, 0)) == cfa.reg
2467                   && CONST_INT_P (XEXP (src, 1)))
2468                 {
2469                   /* Setting a temporary CFA register that will be copied
2470                      into the FP later on.  */
2471                   offset = - INTVAL (XEXP (src, 1));
2472                   cfa.offset += offset;
2473                   cfa.reg = REGNO (dest);
2474                   /* Or used to save regs to the stack.  */
2475                   cfa_temp.reg = cfa.reg;
2476                   cfa_temp.offset = cfa.offset;
2477                 }
2478
2479               /* Rule 5 */
2480               else if (REG_P (XEXP (src, 0))
2481                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2482                        && XEXP (src, 1) == stack_pointer_rtx)
2483                 {
2484                   /* Setting a scratch register that we will use instead
2485                      of SP for saving registers to the stack.  */
2486                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2487                   cfa_store.reg = REGNO (dest);
2488                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2489                 }
2490
2491               /* Rule 9 */
2492               else if (GET_CODE (src) == LO_SUM
2493                        && CONST_INT_P (XEXP (src, 1)))
2494                 {
2495                   cfa_temp.reg = REGNO (dest);
2496                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2497                 }
2498               else
2499                 gcc_unreachable ();
2500             }
2501           break;
2502
2503           /* Rule 6 */
2504         case CONST_INT:
2505           cfa_temp.reg = REGNO (dest);
2506           cfa_temp.offset = INTVAL (src);
2507           break;
2508
2509           /* Rule 7 */
2510         case IOR:
2511           gcc_assert (REG_P (XEXP (src, 0))
2512                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2513                       && CONST_INT_P (XEXP (src, 1)));
2514
2515           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2516             cfa_temp.reg = REGNO (dest);
2517           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2518           break;
2519
2520           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2521              which will fill in all of the bits.  */
2522           /* Rule 8 */
2523         case HIGH:
2524           break;
2525
2526           /* Rule 15 */
2527         case UNSPEC:
2528         case UNSPEC_VOLATILE:
2529           gcc_assert (targetm.dwarf_handle_frame_unspec);
2530           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2531           return;
2532
2533           /* Rule 16 */
2534         case AND:
2535           /* If this AND operation happens on stack pointer in prologue,
2536              we assume the stack is realigned and we extract the
2537              alignment.  */
2538           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2539             {
2540               /* We interpret reg_save differently with stack_realign set.
2541                  Thus we must flush whatever we have queued first.  */
2542               dwarf2out_flush_queued_reg_saves ();
2543
2544               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2545               fde->stack_realign = 1;
2546               fde->stack_realignment = INTVAL (XEXP (src, 1));
2547               cfa_store.offset = 0;
2548
2549               if (cfa.reg != STACK_POINTER_REGNUM
2550                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2551                 fde->drap_reg = cfa.reg;
2552             }
2553           return;
2554
2555         default:
2556           gcc_unreachable ();
2557         }
2558
2559       def_cfa_1 (label, &cfa);
2560       break;
2561
2562     case MEM:
2563
2564       /* Saving a register to the stack.  Make sure dest is relative to the
2565          CFA register.  */
2566       switch (GET_CODE (XEXP (dest, 0)))
2567         {
2568           /* Rule 10 */
2569           /* With a push.  */
2570         case PRE_MODIFY:
2571           /* We can't handle variable size modifications.  */
2572           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2573                       == CONST_INT);
2574           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2575
2576           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2577                       && cfa_store.reg == STACK_POINTER_REGNUM);
2578
2579           cfa_store.offset += offset;
2580           if (cfa.reg == STACK_POINTER_REGNUM)
2581             cfa.offset = cfa_store.offset;
2582
2583           offset = -cfa_store.offset;
2584           break;
2585
2586           /* Rule 11 */
2587         case PRE_INC:
2588         case PRE_DEC:
2589         case POST_DEC:
2590           offset = GET_MODE_SIZE (GET_MODE (dest));
2591           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2592             offset = -offset;
2593
2594           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2595                        == STACK_POINTER_REGNUM)
2596                       && cfa_store.reg == STACK_POINTER_REGNUM);
2597
2598           cfa_store.offset += offset;
2599
2600           /* Rule 18: If stack is aligned, we will use FP as a
2601              reference to represent the address of the stored
2602              regiser.  */
2603           if (fde
2604               && fde->stack_realign
2605               && src == hard_frame_pointer_rtx)
2606             {
2607               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2608               cfa_store.offset = 0;
2609             }
2610
2611           if (cfa.reg == STACK_POINTER_REGNUM)
2612             cfa.offset = cfa_store.offset;
2613
2614           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2615             offset += -cfa_store.offset;
2616           else
2617             offset = -cfa_store.offset;
2618           break;
2619
2620           /* Rule 12 */
2621           /* With an offset.  */
2622         case PLUS:
2623         case MINUS:
2624         case LO_SUM:
2625           {
2626             int regno;
2627
2628             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2629                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2630             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2631             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2632               offset = -offset;
2633
2634             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2635
2636             if (cfa.reg == (unsigned) regno)
2637               offset -= cfa.offset;
2638             else if (cfa_store.reg == (unsigned) regno)
2639               offset -= cfa_store.offset;
2640             else
2641               {
2642                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2643                 offset -= cfa_temp.offset;
2644               }
2645           }
2646           break;
2647
2648           /* Rule 13 */
2649           /* Without an offset.  */
2650         case REG:
2651           {
2652             int regno = REGNO (XEXP (dest, 0));
2653
2654             if (cfa.reg == (unsigned) regno)
2655               offset = -cfa.offset;
2656             else if (cfa_store.reg == (unsigned) regno)
2657               offset = -cfa_store.offset;
2658             else
2659               {
2660                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2661                 offset = -cfa_temp.offset;
2662               }
2663           }
2664           break;
2665
2666           /* Rule 14 */
2667         case POST_INC:
2668           gcc_assert (cfa_temp.reg
2669                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2670           offset = -cfa_temp.offset;
2671           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2672           break;
2673
2674         default:
2675           gcc_unreachable ();
2676         }
2677
2678         /* Rule 17 */
2679         /* If the source operand of this MEM operation is not a
2680            register, basically the source is return address.  Here
2681            we only care how much stack grew and we don't save it.  */
2682       if (!REG_P (src))
2683         break;
2684
2685       if (REGNO (src) != STACK_POINTER_REGNUM
2686           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2687           && (unsigned) REGNO (src) == cfa.reg)
2688         {
2689           /* We're storing the current CFA reg into the stack.  */
2690
2691           if (cfa.offset == 0)
2692             {
2693               /* Rule 19 */
2694               /* If stack is aligned, putting CFA reg into stack means
2695                  we can no longer use reg + offset to represent CFA.
2696                  Here we use DW_CFA_def_cfa_expression instead.  The
2697                  result of this expression equals to the original CFA
2698                  value.  */
2699               if (fde
2700                   && fde->stack_realign
2701                   && cfa.indirect == 0
2702                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2703                 {
2704                   dw_cfa_location cfa_exp;
2705
2706                   gcc_assert (fde->drap_reg == cfa.reg);
2707
2708                   cfa_exp.indirect = 1;
2709                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2710                   cfa_exp.base_offset = offset;
2711                   cfa_exp.offset = 0;
2712
2713                   fde->drap_reg_saved = 1;
2714
2715                   def_cfa_1 (label, &cfa_exp);
2716                   break;
2717                 }
2718
2719               /* If the source register is exactly the CFA, assume
2720                  we're saving SP like any other register; this happens
2721                  on the ARM.  */
2722               def_cfa_1 (label, &cfa);
2723               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2724               break;
2725             }
2726           else
2727             {
2728               /* Otherwise, we'll need to look in the stack to
2729                  calculate the CFA.  */
2730               rtx x = XEXP (dest, 0);
2731
2732               if (!REG_P (x))
2733                 x = XEXP (x, 0);
2734               gcc_assert (REG_P (x));
2735
2736               cfa.reg = REGNO (x);
2737               cfa.base_offset = offset;
2738               cfa.indirect = 1;
2739               def_cfa_1 (label, &cfa);
2740               break;
2741             }
2742         }
2743
2744       def_cfa_1 (label, &cfa);
2745       {
2746         span = targetm.dwarf_register_span (src);
2747
2748         if (!span)
2749           queue_reg_save (label, src, NULL_RTX, offset);
2750         else
2751           {
2752             /* We have a PARALLEL describing where the contents of SRC
2753                live.  Queue register saves for each piece of the
2754                PARALLEL.  */
2755             int par_index;
2756             int limit;
2757             HOST_WIDE_INT span_offset = offset;
2758
2759             gcc_assert (GET_CODE (span) == PARALLEL);
2760
2761             limit = XVECLEN (span, 0);
2762             for (par_index = 0; par_index < limit; par_index++)
2763               {
2764                 rtx elem = XVECEXP (span, 0, par_index);
2765
2766                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2767                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2768               }
2769           }
2770       }
2771       break;
2772
2773     default:
2774       gcc_unreachable ();
2775     }
2776 }
2777
2778 /* Record call frame debugging information for INSN, which either
2779    sets SP or FP (adjusting how we calculate the frame address) or saves a
2780    register to the stack.  If INSN is NULL_RTX, initialize our state.
2781
2782    If AFTER_P is false, we're being called before the insn is emitted,
2783    otherwise after.  Call instructions get invoked twice.  */
2784
2785 void
2786 dwarf2out_frame_debug (rtx insn, bool after_p)
2787 {
2788   const char *label;
2789   rtx note, n;
2790   bool handled_one = false;
2791
2792   if (insn == NULL_RTX)
2793     {
2794       size_t i;
2795
2796       /* Flush any queued register saves.  */
2797       dwarf2out_flush_queued_reg_saves ();
2798
2799       /* Set up state for generating call frame debug info.  */
2800       lookup_cfa (&cfa);
2801       gcc_assert (cfa.reg
2802                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2803
2804       cfa.reg = STACK_POINTER_REGNUM;
2805       cfa_store = cfa;
2806       cfa_temp.reg = -1;
2807       cfa_temp.offset = 0;
2808
2809       for (i = 0; i < num_regs_saved_in_regs; i++)
2810         {
2811           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2812           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2813         }
2814       num_regs_saved_in_regs = 0;
2815
2816       if (barrier_args_size)
2817         {
2818           XDELETEVEC (barrier_args_size);
2819           barrier_args_size = NULL;
2820         }
2821       return;
2822     }
2823
2824   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2825     dwarf2out_flush_queued_reg_saves ();
2826
2827   if (!RTX_FRAME_RELATED_P (insn))
2828     {
2829       /* ??? This should be done unconditionally since stack adjustments
2830          matter if the stack pointer is not the CFA register anymore but
2831          is still used to save registers.  */
2832       if (!ACCUMULATE_OUTGOING_ARGS)
2833         dwarf2out_notice_stack_adjust (insn, after_p);
2834       return;
2835     }
2836
2837   label = dwarf2out_cfi_label (false);
2838   any_cfis_emitted = false;
2839
2840   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2841     switch (REG_NOTE_KIND (note))
2842       {
2843       case REG_FRAME_RELATED_EXPR:
2844         insn = XEXP (note, 0);
2845         goto found;
2846
2847       case REG_CFA_DEF_CFA:
2848         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2849         handled_one = true;
2850         break;
2851
2852       case REG_CFA_ADJUST_CFA:
2853         n = XEXP (note, 0);
2854         if (n == NULL)
2855           {
2856             n = PATTERN (insn);
2857             if (GET_CODE (n) == PARALLEL)
2858               n = XVECEXP (n, 0, 0);
2859           }
2860         dwarf2out_frame_debug_adjust_cfa (n, label);
2861         handled_one = true;
2862         break;
2863
2864       case REG_CFA_OFFSET:
2865         n = XEXP (note, 0);
2866         if (n == NULL)
2867           n = single_set (insn);
2868         dwarf2out_frame_debug_cfa_offset (n, label);
2869         handled_one = true;
2870         break;
2871
2872       case REG_CFA_REGISTER:
2873         n = XEXP (note, 0);
2874         if (n == NULL)
2875           {
2876             n = PATTERN (insn);
2877             if (GET_CODE (n) == PARALLEL)
2878               n = XVECEXP (n, 0, 0);
2879           }
2880         dwarf2out_frame_debug_cfa_register (n, label);
2881         handled_one = true;
2882         break;
2883
2884       case REG_CFA_EXPRESSION:
2885         n = XEXP (note, 0);
2886         if (n == NULL)
2887           n = single_set (insn);
2888         dwarf2out_frame_debug_cfa_expression (n, label);
2889         handled_one = true;
2890         break;
2891
2892       case REG_CFA_RESTORE:
2893         n = XEXP (note, 0);
2894         if (n == NULL)
2895           {
2896             n = PATTERN (insn);
2897             if (GET_CODE (n) == PARALLEL)
2898               n = XVECEXP (n, 0, 0);
2899             n = XEXP (n, 0);
2900           }
2901         dwarf2out_frame_debug_cfa_restore (n, label);
2902         handled_one = true;
2903         break;
2904
2905       case REG_CFA_SET_VDRAP:
2906         n = XEXP (note, 0);
2907         if (REG_P (n))
2908           {
2909             dw_fde_ref fde = current_fde ();
2910             if (fde)
2911               {
2912                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2913                 if (REG_P (n))
2914                   fde->vdrap_reg = REGNO (n);
2915               }
2916           }
2917         handled_one = true;
2918         break;
2919
2920       default:
2921         break;
2922       }
2923   if (handled_one)
2924     {
2925       if (any_cfis_emitted)
2926         dwarf2out_flush_queued_reg_saves ();
2927       return;
2928     }
2929
2930   insn = PATTERN (insn);
2931  found:
2932   dwarf2out_frame_debug_expr (insn, label);
2933
2934   /* Check again.  A parallel can save and update the same register.
2935      We could probably check just once, here, but this is safer than
2936      removing the check above.  */
2937   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2938     dwarf2out_flush_queued_reg_saves ();
2939 }
2940
2941 /* Determine if we need to save and restore CFI information around this
2942    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2943    we do need to save/restore, then emit the save now, and insert a
2944    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2945
2946 void
2947 dwarf2out_cfi_begin_epilogue (rtx insn)
2948 {
2949   bool saw_frp = false;
2950   rtx i;
2951
2952   /* Scan forward to the return insn, noticing if there are possible
2953      frame related insns.  */
2954   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2955     {
2956       if (!INSN_P (i))
2957         continue;
2958
2959       /* Look for both regular and sibcalls to end the block.  */
2960       if (returnjump_p (i))
2961         break;
2962       if (CALL_P (i) && SIBLING_CALL_P (i))
2963         break;
2964
2965       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2966         {
2967           int idx;
2968           rtx seq = PATTERN (i);
2969
2970           if (returnjump_p (XVECEXP (seq, 0, 0)))
2971             break;
2972           if (CALL_P (XVECEXP (seq, 0, 0))
2973               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2974             break;
2975
2976           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2977             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2978               saw_frp = true;
2979         }
2980
2981       if (RTX_FRAME_RELATED_P (i))
2982         saw_frp = true;
2983     }
2984
2985   /* If the port doesn't emit epilogue unwind info, we don't need a
2986      save/restore pair.  */
2987   if (!saw_frp)
2988     return;
2989
2990   /* Otherwise, search forward to see if the return insn was the last
2991      basic block of the function.  If so, we don't need save/restore.  */
2992   gcc_assert (i != NULL);
2993   i = next_real_insn (i);
2994   if (i == NULL)
2995     return;
2996
2997   /* Insert the restore before that next real insn in the stream, and before
2998      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2999      properly nested.  This should be after any label or alignment.  This
3000      will be pushed into the CFI stream by the function below.  */
3001   while (1)
3002     {
3003       rtx p = PREV_INSN (i);
3004       if (!NOTE_P (p))
3005         break;
3006       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3007         break;
3008       i = p;
3009     }
3010   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3011
3012   emit_cfa_remember = true;
3013
3014   /* And emulate the state save.  */
3015   gcc_assert (!cfa_remember.in_use);
3016   cfa_remember = cfa;
3017   cfa_remember.in_use = 1;
3018 }
3019
3020 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3021    required.  */
3022
3023 void
3024 dwarf2out_frame_debug_restore_state (void)
3025 {
3026   dw_cfi_ref cfi = new_cfi ();
3027   const char *label = dwarf2out_cfi_label (false);
3028
3029   cfi->dw_cfi_opc = DW_CFA_restore_state;
3030   add_fde_cfi (label, cfi);
3031
3032   gcc_assert (cfa_remember.in_use);
3033   cfa = cfa_remember;
3034   cfa_remember.in_use = 0;
3035 }
3036
3037 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3038 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3039  (enum dwarf_call_frame_info cfi);
3040
3041 static enum dw_cfi_oprnd_type
3042 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3043 {
3044   switch (cfi)
3045     {
3046     case DW_CFA_nop:
3047     case DW_CFA_GNU_window_save:
3048     case DW_CFA_remember_state:
3049     case DW_CFA_restore_state:
3050       return dw_cfi_oprnd_unused;
3051
3052     case DW_CFA_set_loc:
3053     case DW_CFA_advance_loc1:
3054     case DW_CFA_advance_loc2:
3055     case DW_CFA_advance_loc4:
3056     case DW_CFA_MIPS_advance_loc8:
3057       return dw_cfi_oprnd_addr;
3058
3059     case DW_CFA_offset:
3060     case DW_CFA_offset_extended:
3061     case DW_CFA_def_cfa:
3062     case DW_CFA_offset_extended_sf:
3063     case DW_CFA_def_cfa_sf:
3064     case DW_CFA_restore:
3065     case DW_CFA_restore_extended:
3066     case DW_CFA_undefined:
3067     case DW_CFA_same_value:
3068     case DW_CFA_def_cfa_register:
3069     case DW_CFA_register:
3070     case DW_CFA_expression:
3071       return dw_cfi_oprnd_reg_num;
3072
3073     case DW_CFA_def_cfa_offset:
3074     case DW_CFA_GNU_args_size:
3075     case DW_CFA_def_cfa_offset_sf:
3076       return dw_cfi_oprnd_offset;
3077
3078     case DW_CFA_def_cfa_expression:
3079       return dw_cfi_oprnd_loc;
3080
3081     default:
3082       gcc_unreachable ();
3083     }
3084 }
3085
3086 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3087 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3088  (enum dwarf_call_frame_info cfi);
3089
3090 static enum dw_cfi_oprnd_type
3091 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3092 {
3093   switch (cfi)
3094     {
3095     case DW_CFA_def_cfa:
3096     case DW_CFA_def_cfa_sf:
3097     case DW_CFA_offset:
3098     case DW_CFA_offset_extended_sf:
3099     case DW_CFA_offset_extended:
3100       return dw_cfi_oprnd_offset;
3101
3102     case DW_CFA_register:
3103       return dw_cfi_oprnd_reg_num;
3104
3105     case DW_CFA_expression:
3106       return dw_cfi_oprnd_loc;
3107
3108     default:
3109       return dw_cfi_oprnd_unused;
3110     }
3111 }
3112
3113 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3114    switch to the data section instead, and write out a synthetic start label
3115    for collect2 the first time around.  */
3116
3117 static void
3118 switch_to_eh_frame_section (bool back)
3119 {
3120   tree label;
3121
3122 #ifdef EH_FRAME_SECTION_NAME
3123   if (eh_frame_section == 0)
3124     {
3125       int flags;
3126
3127       if (EH_TABLES_CAN_BE_READ_ONLY)
3128         {
3129           int fde_encoding;
3130           int per_encoding;
3131           int lsda_encoding;
3132
3133           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3134                                                        /*global=*/0);
3135           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3136                                                        /*global=*/1);
3137           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3138                                                         /*global=*/0);
3139           flags = ((! flag_pic
3140                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3141                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3142                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3143                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3144                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3145                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3146                    ? 0 : SECTION_WRITE);
3147         }
3148       else
3149         flags = SECTION_WRITE;
3150       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3151     }
3152 #endif /* EH_FRAME_SECTION_NAME */
3153
3154   if (eh_frame_section)
3155     switch_to_section (eh_frame_section);
3156   else
3157     {
3158       /* We have no special eh_frame section.  Put the information in
3159          the data section and emit special labels to guide collect2.  */
3160       switch_to_section (data_section);
3161
3162       if (!back)
3163         {
3164           label = get_file_function_name ("F");
3165           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3166           targetm.asm_out.globalize_label (asm_out_file,
3167                                            IDENTIFIER_POINTER (label));
3168           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3169         }
3170     }
3171 }
3172
3173 /* Switch [BACK] to the eh or debug frame table section, depending on
3174    FOR_EH.  */
3175
3176 static void
3177 switch_to_frame_table_section (int for_eh, bool back)
3178 {
3179   if (for_eh)
3180     switch_to_eh_frame_section (back);
3181   else
3182     {
3183       if (!debug_frame_section)
3184         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3185                                            SECTION_DEBUG, NULL);
3186       switch_to_section (debug_frame_section);
3187     }
3188 }
3189
3190 /* Output a Call Frame Information opcode and its operand(s).  */
3191
3192 static void
3193 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3194 {
3195   unsigned long r;
3196   HOST_WIDE_INT off;
3197
3198   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3199     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3200                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3201                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3202                          ((unsigned HOST_WIDE_INT)
3203                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3204   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3205     {
3206       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3207       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3208                            "DW_CFA_offset, column %#lx", r);
3209       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3210       dw2_asm_output_data_uleb128 (off, NULL);
3211     }
3212   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3213     {
3214       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3215       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3216                            "DW_CFA_restore, column %#lx", r);
3217     }
3218   else
3219     {
3220       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3221                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3222
3223       switch (cfi->dw_cfi_opc)
3224         {
3225         case DW_CFA_set_loc:
3226           if (for_eh)
3227             dw2_asm_output_encoded_addr_rtx (
3228                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3229                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3230                 false, NULL);
3231           else
3232             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3233                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3234           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3235           break;
3236
3237         case DW_CFA_advance_loc1:
3238           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3239                                 fde->dw_fde_current_label, NULL);
3240           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3241           break;
3242
3243         case DW_CFA_advance_loc2:
3244           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3245                                 fde->dw_fde_current_label, NULL);
3246           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3247           break;
3248
3249         case DW_CFA_advance_loc4:
3250           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3251                                 fde->dw_fde_current_label, NULL);
3252           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3253           break;
3254
3255         case DW_CFA_MIPS_advance_loc8:
3256           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3257                                 fde->dw_fde_current_label, NULL);
3258           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3259           break;
3260
3261         case DW_CFA_offset_extended:
3262           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3263           dw2_asm_output_data_uleb128 (r, NULL);
3264           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3265           dw2_asm_output_data_uleb128 (off, NULL);
3266           break;
3267
3268         case DW_CFA_def_cfa:
3269           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3270           dw2_asm_output_data_uleb128 (r, NULL);
3271           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3272           break;
3273
3274         case DW_CFA_offset_extended_sf:
3275           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3276           dw2_asm_output_data_uleb128 (r, NULL);
3277           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3278           dw2_asm_output_data_sleb128 (off, NULL);
3279           break;
3280
3281         case DW_CFA_def_cfa_sf:
3282           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3283           dw2_asm_output_data_uleb128 (r, NULL);
3284           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3285           dw2_asm_output_data_sleb128 (off, NULL);
3286           break;
3287
3288         case DW_CFA_restore_extended:
3289         case DW_CFA_undefined:
3290         case DW_CFA_same_value:
3291         case DW_CFA_def_cfa_register:
3292           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3293           dw2_asm_output_data_uleb128 (r, NULL);
3294           break;
3295
3296         case DW_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           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3300           dw2_asm_output_data_uleb128 (r, NULL);
3301           break;
3302
3303         case DW_CFA_def_cfa_offset:
3304         case DW_CFA_GNU_args_size:
3305           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3306           break;
3307
3308         case DW_CFA_def_cfa_offset_sf:
3309           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3310           dw2_asm_output_data_sleb128 (off, NULL);
3311           break;
3312
3313         case DW_CFA_GNU_window_save:
3314           break;
3315
3316         case DW_CFA_def_cfa_expression:
3317         case DW_CFA_expression:
3318           output_cfa_loc (cfi, for_eh);
3319           break;
3320
3321         case DW_CFA_GNU_negative_offset_extended:
3322           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3323           gcc_unreachable ();
3324
3325         default:
3326           break;
3327         }
3328     }
3329 }
3330
3331 /* Similar, but do it via assembler directives instead.  */
3332
3333 static void
3334 output_cfi_directive (dw_cfi_ref cfi)
3335 {
3336   unsigned long r, r2;
3337
3338   switch (cfi->dw_cfi_opc)
3339     {
3340     case DW_CFA_advance_loc:
3341     case DW_CFA_advance_loc1:
3342     case DW_CFA_advance_loc2:
3343     case DW_CFA_advance_loc4:
3344     case DW_CFA_MIPS_advance_loc8:
3345     case DW_CFA_set_loc:
3346       /* Should only be created by add_fde_cfi in a code path not
3347          followed when emitting via directives.  The assembler is
3348          going to take care of this for us.  */
3349       gcc_unreachable ();
3350
3351     case DW_CFA_offset:
3352     case DW_CFA_offset_extended:
3353     case DW_CFA_offset_extended_sf:
3354       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3355       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3356                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3357       break;
3358
3359     case DW_CFA_restore:
3360     case DW_CFA_restore_extended:
3361       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3362       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3363       break;
3364
3365     case DW_CFA_undefined:
3366       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3367       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3368       break;
3369
3370     case DW_CFA_same_value:
3371       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3372       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3373       break;
3374
3375     case DW_CFA_def_cfa:
3376     case DW_CFA_def_cfa_sf:
3377       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3378       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3379                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3380       break;
3381
3382     case DW_CFA_def_cfa_register:
3383       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3384       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3385       break;
3386
3387     case DW_CFA_register:
3388       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3389       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3390       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3391       break;
3392
3393     case DW_CFA_def_cfa_offset:
3394     case DW_CFA_def_cfa_offset_sf:
3395       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3396                HOST_WIDE_INT_PRINT_DEC"\n",
3397                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3398       break;
3399
3400     case DW_CFA_remember_state:
3401       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3402       break;
3403     case DW_CFA_restore_state:
3404       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3405       break;
3406
3407     case DW_CFA_GNU_args_size:
3408       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3409       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3410       if (flag_debug_asm)
3411         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3412                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3413       fputc ('\n', asm_out_file);
3414       break;
3415
3416     case DW_CFA_GNU_window_save:
3417       fprintf (asm_out_file, "\t.cfi_window_save\n");
3418       break;
3419
3420     case DW_CFA_def_cfa_expression:
3421     case DW_CFA_expression:
3422       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3423       output_cfa_loc_raw (cfi);
3424       fputc ('\n', asm_out_file);
3425       break;
3426
3427     default:
3428       gcc_unreachable ();
3429     }
3430 }
3431
3432 DEF_VEC_P (dw_cfi_ref);
3433 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3434
3435 /* Output CFIs to bring current FDE to the same state as after executing
3436    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3437    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3438    other arguments to pass to output_cfi.  */
3439
3440 static void
3441 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3442 {
3443   struct dw_cfi_struct cfi_buf;
3444   dw_cfi_ref cfi2;
3445   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3446   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3447   unsigned int len, idx;
3448
3449   for (;; cfi = cfi->dw_cfi_next)
3450     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3451       {
3452       case DW_CFA_advance_loc:
3453       case DW_CFA_advance_loc1:
3454       case DW_CFA_advance_loc2:
3455       case DW_CFA_advance_loc4:
3456       case DW_CFA_MIPS_advance_loc8:
3457       case DW_CFA_set_loc:
3458         /* All advances should be ignored.  */
3459         break;
3460       case DW_CFA_remember_state:
3461         {
3462           dw_cfi_ref args_size = cfi_args_size;
3463
3464           /* Skip everything between .cfi_remember_state and
3465              .cfi_restore_state.  */
3466           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3467             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3468               break;
3469             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3470               args_size = cfi2;
3471             else
3472               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3473
3474           if (cfi2 == NULL)
3475             goto flush_all;
3476           else
3477             {
3478               cfi = cfi2;
3479               cfi_args_size = args_size;
3480             }
3481           break;
3482         }
3483       case DW_CFA_GNU_args_size:
3484         cfi_args_size = cfi;
3485         break;
3486       case DW_CFA_GNU_window_save:
3487         goto flush_all;
3488       case DW_CFA_offset:
3489       case DW_CFA_offset_extended:
3490       case DW_CFA_offset_extended_sf:
3491       case DW_CFA_restore:
3492       case DW_CFA_restore_extended:
3493       case DW_CFA_undefined:
3494       case DW_CFA_same_value:
3495       case DW_CFA_register:
3496       case DW_CFA_val_offset:
3497       case DW_CFA_val_offset_sf:
3498       case DW_CFA_expression:
3499       case DW_CFA_val_expression:
3500       case DW_CFA_GNU_negative_offset_extended:
3501         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3502           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3503                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3504         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3505         break;
3506       case DW_CFA_def_cfa:
3507       case DW_CFA_def_cfa_sf:
3508       case DW_CFA_def_cfa_expression:
3509         cfi_cfa = cfi;
3510         cfi_cfa_offset = cfi;
3511         break;
3512       case DW_CFA_def_cfa_register:
3513         cfi_cfa = cfi;
3514         break;
3515       case DW_CFA_def_cfa_offset:
3516       case DW_CFA_def_cfa_offset_sf:
3517         cfi_cfa_offset = cfi;
3518         break;
3519       case DW_CFA_nop:
3520         gcc_assert (cfi == NULL);
3521       flush_all:
3522         len = VEC_length (dw_cfi_ref, regs);
3523         for (idx = 0; idx < len; idx++)
3524           {
3525             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3526             if (cfi2 != NULL
3527                 && cfi2->dw_cfi_opc != DW_CFA_restore
3528                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3529               {
3530                 if (do_cfi_asm)
3531                   output_cfi_directive (cfi2);
3532                 else
3533                   output_cfi (cfi2, fde, for_eh);
3534               }
3535           }
3536         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3537           {
3538             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3539             cfi_buf = *cfi_cfa;
3540             switch (cfi_cfa_offset->dw_cfi_opc)
3541               {
3542               case DW_CFA_def_cfa_offset:
3543                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3544                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3545                 break;
3546               case DW_CFA_def_cfa_offset_sf:
3547                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3548                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3549                 break;
3550               case DW_CFA_def_cfa:
3551               case DW_CFA_def_cfa_sf:
3552                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3553                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3554                 break;
3555               default:
3556                 gcc_unreachable ();
3557               }
3558             cfi_cfa = &cfi_buf;
3559           }
3560         else if (cfi_cfa_offset)
3561           cfi_cfa = cfi_cfa_offset;
3562         if (cfi_cfa)
3563           {
3564             if (do_cfi_asm)
3565               output_cfi_directive (cfi_cfa);
3566             else
3567               output_cfi (cfi_cfa, fde, for_eh);
3568           }
3569         cfi_cfa = NULL;
3570         cfi_cfa_offset = NULL;
3571         if (cfi_args_size
3572             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3573           {
3574             if (do_cfi_asm)
3575               output_cfi_directive (cfi_args_size);
3576             else
3577               output_cfi (cfi_args_size, fde, for_eh);
3578           }
3579         cfi_args_size = NULL;
3580         if (cfi == NULL)
3581           {
3582             VEC_free (dw_cfi_ref, heap, regs);
3583             return;
3584           }
3585         else if (do_cfi_asm)
3586           output_cfi_directive (cfi);
3587         else
3588           output_cfi (cfi, fde, for_eh);
3589         break;
3590       default:
3591         gcc_unreachable ();
3592     }
3593 }
3594
3595 /* Output one FDE.  */
3596
3597 static void
3598 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3599             char *section_start_label, int fde_encoding, char *augmentation,
3600             bool any_lsda_needed, int lsda_encoding)
3601 {
3602   const char *begin, *end;
3603   static unsigned int j;
3604   char l1[20], l2[20];
3605   dw_cfi_ref cfi;
3606
3607   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3608                                      /* empty */ 0);
3609   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3610                                   for_eh + j);
3611   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3612   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3613   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3614     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3615                          " indicating 64-bit DWARF extension");
3616   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3617                         "FDE Length");
3618   ASM_OUTPUT_LABEL (asm_out_file, l1);
3619
3620   if (for_eh)
3621     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3622   else
3623     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3624                            debug_frame_section, "FDE CIE offset");
3625
3626   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3627   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3628
3629   if (for_eh)
3630     {
3631       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3632       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3633       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3634                                        "FDE initial location");
3635       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3636                             end, begin, "FDE address range");
3637     }
3638   else
3639     {
3640       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3641       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3642     }
3643
3644   if (augmentation[0])
3645     {
3646       if (any_lsda_needed)
3647         {
3648           int size = size_of_encoded_value (lsda_encoding);
3649
3650           if (lsda_encoding == DW_EH_PE_aligned)
3651             {
3652               int offset = (  4         /* Length */
3653                             + 4         /* CIE offset */
3654                             + 2 * size_of_encoded_value (fde_encoding)
3655                             + 1         /* Augmentation size */ );
3656               int pad = -offset & (PTR_SIZE - 1);
3657
3658               size += pad;
3659               gcc_assert (size_of_uleb128 (size) == 1);
3660             }
3661
3662           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3663
3664           if (fde->uses_eh_lsda)
3665             {
3666               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3667                                            fde->funcdef_number);
3668               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3669                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3670                                                false,
3671                                                "Language Specific Data Area");
3672             }
3673           else
3674             {
3675               if (lsda_encoding == DW_EH_PE_aligned)
3676                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3677               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3678                                    "Language Specific Data Area (none)");
3679             }
3680         }
3681       else
3682         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3683     }
3684
3685   /* Loop through the Call Frame Instructions associated with
3686      this FDE.  */
3687   fde->dw_fde_current_label = begin;
3688   if (fde->dw_fde_second_begin == NULL)
3689     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3690       output_cfi (cfi, fde, for_eh);
3691   else if (!second)
3692     {
3693       if (fde->dw_fde_switch_cfi)
3694         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3695           {
3696             output_cfi (cfi, fde, for_eh);
3697             if (cfi == fde->dw_fde_switch_cfi)
3698               break;
3699           }
3700     }
3701   else
3702     {
3703       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3704
3705       if (fde->dw_fde_switch_cfi)
3706         {
3707           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3708           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3709           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3710           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3711         }
3712       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3713         output_cfi (cfi, fde, for_eh);
3714     }
3715
3716   /* If we are to emit a ref/link from function bodies to their frame tables,
3717      do it now.  This is typically performed to make sure that tables
3718      associated with functions are dragged with them and not discarded in
3719      garbage collecting links. We need to do this on a per function basis to
3720      cope with -ffunction-sections.  */
3721
3722 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3723   /* Switch to the function section, emit the ref to the tables, and
3724      switch *back* into the table section.  */
3725   switch_to_section (function_section (fde->decl));
3726   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3727   switch_to_frame_table_section (for_eh, true);
3728 #endif
3729
3730   /* Pad the FDE out to an address sized boundary.  */
3731   ASM_OUTPUT_ALIGN (asm_out_file,
3732                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3733   ASM_OUTPUT_LABEL (asm_out_file, l2);
3734
3735   j += 2;
3736 }
3737
3738 /* Return true if frame description entry FDE is needed for EH.  */
3739
3740 static bool
3741 fde_needed_for_eh_p (dw_fde_ref fde)
3742 {
3743   if (flag_asynchronous_unwind_tables)
3744     return true;
3745
3746   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3747     return true;
3748
3749   if (fde->uses_eh_lsda)
3750     return true;
3751
3752   /* If exceptions are enabled, we have collected nothrow info.  */
3753   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3754     return false;
3755
3756   return true;
3757 }
3758
3759 /* Output the call frame information used to record information
3760    that relates to calculating the frame pointer, and records the
3761    location of saved registers.  */
3762
3763 static void
3764 output_call_frame_info (int for_eh)
3765 {
3766   unsigned int i;
3767   dw_fde_ref fde;
3768   dw_cfi_ref cfi;
3769   char l1[20], l2[20], section_start_label[20];
3770   bool any_lsda_needed = false;
3771   char augmentation[6];
3772   int augmentation_size;
3773   int fde_encoding = DW_EH_PE_absptr;
3774   int per_encoding = DW_EH_PE_absptr;
3775   int lsda_encoding = DW_EH_PE_absptr;
3776   int return_reg;
3777   rtx personality = NULL;
3778   int dw_cie_version;
3779
3780   /* Don't emit a CIE if there won't be any FDEs.  */
3781   if (fde_table_in_use == 0)
3782     return;
3783
3784   /* Nothing to do if the assembler's doing it all.  */
3785   if (dwarf2out_do_cfi_asm ())
3786     return;
3787
3788   /* If we don't have any functions we'll want to unwind out of, don't emit
3789      any EH unwind information.  If we make FDEs linkonce, we may have to
3790      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3791      want to avoid having an FDE kept around when the function it refers to
3792      is discarded.  Example where this matters: a primary function template
3793      in C++ requires EH information, an explicit specialization doesn't.  */
3794   if (for_eh)
3795     {
3796       bool any_eh_needed = false;
3797
3798       for (i = 0; i < fde_table_in_use; i++)
3799         if (fde_table[i].uses_eh_lsda)
3800           any_eh_needed = any_lsda_needed = true;
3801         else if (fde_needed_for_eh_p (&fde_table[i]))
3802           any_eh_needed = true;
3803         else if (TARGET_USES_WEAK_UNWIND_INFO)
3804           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3805                                              1, 1);
3806
3807       if (!any_eh_needed)
3808         return;
3809     }
3810
3811   /* We're going to be generating comments, so turn on app.  */
3812   if (flag_debug_asm)
3813     app_enable ();
3814
3815   /* Switch to the proper frame section, first time.  */
3816   switch_to_frame_table_section (for_eh, false);
3817
3818   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3819   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3820
3821   /* Output the CIE.  */
3822   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3823   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3824   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3825     dw2_asm_output_data (4, 0xffffffff,
3826       "Initial length escape value indicating 64-bit DWARF extension");
3827   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3828                         "Length of Common Information Entry");
3829   ASM_OUTPUT_LABEL (asm_out_file, l1);
3830
3831   /* Now that the CIE pointer is PC-relative for EH,
3832      use 0 to identify the CIE.  */
3833   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3834                        (for_eh ? 0 : DWARF_CIE_ID),
3835                        "CIE Identifier Tag");
3836
3837   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3838      use CIE version 1, unless that would produce incorrect results
3839      due to overflowing the return register column.  */
3840   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3841   dw_cie_version = 1;
3842   if (return_reg >= 256 || dwarf_version > 2)
3843     dw_cie_version = 3;
3844   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3845
3846   augmentation[0] = 0;
3847   augmentation_size = 0;
3848
3849   personality = current_unit_personality;
3850   if (for_eh)
3851     {
3852       char *p;
3853
3854       /* Augmentation:
3855          z      Indicates that a uleb128 is present to size the
3856                 augmentation section.
3857          L      Indicates the encoding (and thus presence) of
3858                 an LSDA pointer in the FDE augmentation.
3859          R      Indicates a non-default pointer encoding for
3860                 FDE code pointers.
3861          P      Indicates the presence of an encoding + language
3862                 personality routine in the CIE augmentation.  */
3863
3864       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3865       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3866       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3867
3868       p = augmentation + 1;
3869       if (personality)
3870         {
3871           *p++ = 'P';
3872           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3873           assemble_external_libcall (personality);
3874         }
3875       if (any_lsda_needed)
3876         {
3877           *p++ = 'L';
3878           augmentation_size += 1;
3879         }
3880       if (fde_encoding != DW_EH_PE_absptr)
3881         {
3882           *p++ = 'R';
3883           augmentation_size += 1;
3884         }
3885       if (p > augmentation + 1)
3886         {
3887           augmentation[0] = 'z';
3888           *p = '\0';
3889         }
3890
3891       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3892       if (personality && per_encoding == DW_EH_PE_aligned)
3893         {
3894           int offset = (  4             /* Length */
3895                         + 4             /* CIE Id */
3896                         + 1             /* CIE version */
3897                         + strlen (augmentation) + 1     /* Augmentation */
3898                         + size_of_uleb128 (1)           /* Code alignment */
3899                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3900                         + 1             /* RA column */
3901                         + 1             /* Augmentation size */
3902                         + 1             /* Personality encoding */ );
3903           int pad = -offset & (PTR_SIZE - 1);
3904
3905           augmentation_size += pad;
3906
3907           /* Augmentations should be small, so there's scarce need to
3908              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3909           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3910         }
3911     }
3912
3913   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3914   if (dw_cie_version >= 4)
3915     {
3916       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3917       dw2_asm_output_data (1, 0, "CIE Segment Size");
3918     }
3919   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3920   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3921                                "CIE Data Alignment Factor");
3922
3923   if (dw_cie_version == 1)
3924     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3925   else
3926     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3927
3928   if (augmentation[0])
3929     {
3930       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3931       if (personality)
3932         {
3933           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3934                                eh_data_format_name (per_encoding));
3935           dw2_asm_output_encoded_addr_rtx (per_encoding,
3936                                            personality,
3937                                            true, NULL);
3938         }
3939
3940       if (any_lsda_needed)
3941         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3942                              eh_data_format_name (lsda_encoding));
3943
3944       if (fde_encoding != DW_EH_PE_absptr)
3945         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3946                              eh_data_format_name (fde_encoding));
3947     }
3948
3949   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3950     output_cfi (cfi, NULL, for_eh);
3951
3952   /* Pad the CIE out to an address sized boundary.  */
3953   ASM_OUTPUT_ALIGN (asm_out_file,
3954                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3955   ASM_OUTPUT_LABEL (asm_out_file, l2);
3956
3957   /* Loop through all of the FDE's.  */
3958   for (i = 0; i < fde_table_in_use; i++)
3959     {
3960       unsigned int k;
3961       fde = &fde_table[i];
3962
3963       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3964       if (for_eh && !fde_needed_for_eh_p (fde))
3965         continue;
3966
3967       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3968         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3969                     augmentation, any_lsda_needed, lsda_encoding);
3970     }
3971
3972   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3973     dw2_asm_output_data (4, 0, "End of Table");
3974 #ifdef MIPS_DEBUGGING_INFO
3975   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3976      get a value of 0.  Putting .align 0 after the label fixes it.  */
3977   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3978 #endif
3979
3980   /* Turn off app to make assembly quicker.  */
3981   if (flag_debug_asm)
3982     app_disable ();
3983 }
3984
3985 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3986
3987 static void
3988 dwarf2out_do_cfi_startproc (bool second)
3989 {
3990   int enc;
3991   rtx ref;
3992   rtx personality = get_personality_function (current_function_decl);
3993
3994   fprintf (asm_out_file, "\t.cfi_startproc\n");
3995
3996   if (personality)
3997     {
3998       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3999       ref = personality;
4000
4001       /* ??? The GAS support isn't entirely consistent.  We have to
4002          handle indirect support ourselves, but PC-relative is done
4003          in the assembler.  Further, the assembler can't handle any
4004          of the weirder relocation types.  */
4005       if (enc & DW_EH_PE_indirect)
4006         ref = dw2_force_const_mem (ref, true);
4007
4008       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4009       output_addr_const (asm_out_file, ref);
4010       fputc ('\n', asm_out_file);
4011     }
4012
4013   if (crtl->uses_eh_lsda)
4014     {
4015       char lab[20];
4016
4017       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4018       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4019                                    current_function_funcdef_no);
4020       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4021       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4022
4023       if (enc & DW_EH_PE_indirect)
4024         ref = dw2_force_const_mem (ref, true);
4025
4026       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4027       output_addr_const (asm_out_file, ref);
4028       fputc ('\n', asm_out_file);
4029     }
4030 }
4031
4032 /* Output a marker (i.e. a label) for the beginning of a function, before
4033    the prologue.  */
4034
4035 void
4036 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4037                           const char *file ATTRIBUTE_UNUSED)
4038 {
4039   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4040   char * dup_label;
4041   dw_fde_ref fde;
4042   section *fnsec;
4043   bool do_frame;
4044
4045   current_function_func_begin_label = NULL;
4046
4047   do_frame = dwarf2out_do_frame ();
4048
4049   /* ??? current_function_func_begin_label is also used by except.c for
4050      call-site information.  We must emit this label if it might be used.  */
4051   if (!do_frame
4052       && (!flag_exceptions
4053           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4054     return;
4055
4056   fnsec = function_section (current_function_decl);
4057   switch_to_section (fnsec);
4058   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4059                                current_function_funcdef_no);
4060   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4061                           current_function_funcdef_no);
4062   dup_label = xstrdup (label);
4063   current_function_func_begin_label = dup_label;
4064
4065   /* We can elide the fde allocation if we're not emitting debug info.  */
4066   if (!do_frame)
4067     return;
4068
4069   /* Expand the fde table if necessary.  */
4070   if (fde_table_in_use == fde_table_allocated)
4071     {
4072       fde_table_allocated += FDE_TABLE_INCREMENT;
4073       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4074       memset (fde_table + fde_table_in_use, 0,
4075               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4076     }
4077
4078   /* Record the FDE associated with this function.  */
4079   current_funcdef_fde = fde_table_in_use;
4080
4081   /* Add the new FDE at the end of the fde_table.  */
4082   fde = &fde_table[fde_table_in_use++];
4083   fde->decl = current_function_decl;
4084   fde->dw_fde_begin = dup_label;
4085   fde->dw_fde_end = NULL;
4086   fde->dw_fde_current_label = dup_label;
4087   fde->dw_fde_second_begin = NULL;
4088   fde->dw_fde_second_end = NULL;
4089   fde->dw_fde_vms_end_prologue = NULL;
4090   fde->dw_fde_vms_begin_epilogue = NULL;
4091   fde->dw_fde_cfi = NULL;
4092   fde->dw_fde_switch_cfi = NULL;
4093   fde->funcdef_number = current_function_funcdef_no;
4094   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4095   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4096   fde->nothrow = crtl->nothrow;
4097   fde->drap_reg = INVALID_REGNUM;
4098   fde->vdrap_reg = INVALID_REGNUM;
4099   fde->in_std_section = (fnsec == text_section
4100                          || (cold_text_section && fnsec == cold_text_section));
4101   fde->second_in_std_section = 0;
4102
4103   args_size = old_args_size = 0;
4104
4105   /* We only want to output line number information for the genuine dwarf2
4106      prologue case, not the eh frame case.  */
4107 #ifdef DWARF2_DEBUGGING_INFO
4108   if (file)
4109     dwarf2out_source_line (line, file, 0, true);
4110 #endif
4111
4112   if (dwarf2out_do_cfi_asm ())
4113     dwarf2out_do_cfi_startproc (false);
4114   else
4115     {
4116       rtx personality = get_personality_function (current_function_decl);
4117       if (!current_unit_personality)
4118         current_unit_personality = personality;
4119
4120       /* We cannot keep a current personality per function as without CFI
4121          asm, at the point where we emit the CFI data, there is no current
4122          function anymore.  */
4123       if (personality && current_unit_personality != personality)
4124         sorry ("multiple EH personalities are supported only with assemblers "
4125                "supporting .cfi_personality directive");
4126     }
4127 }
4128
4129 /* Output a marker (i.e. a label) for the end of the generated code
4130    for a function prologue.  This gets called *after* the prologue code has
4131    been generated.  */
4132
4133 void
4134 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4135                         const char *file ATTRIBUTE_UNUSED)
4136 {
4137   dw_fde_ref fde;
4138   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4139
4140   /* Output a label to mark the endpoint of the code generated for this
4141      function.  */
4142   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4143                                current_function_funcdef_no);
4144   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4145                           current_function_funcdef_no);
4146   fde = &fde_table[fde_table_in_use - 1];
4147   fde->dw_fde_vms_end_prologue = xstrdup (label);
4148 }
4149
4150 /* Output a marker (i.e. a label) for the beginning of the generated code
4151    for a function epilogue.  This gets called *before* the prologue code has
4152    been generated.  */
4153
4154 void
4155 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4156                           const char *file ATTRIBUTE_UNUSED)
4157 {
4158   dw_fde_ref fde;
4159   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4160
4161   fde = &fde_table[fde_table_in_use - 1];
4162   if (fde->dw_fde_vms_begin_epilogue)
4163     return;
4164
4165   /* Output a label to mark the endpoint of the code generated for this
4166      function.  */
4167   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4168                                current_function_funcdef_no);
4169   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4170                           current_function_funcdef_no);
4171   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4172 }
4173
4174 /* Output a marker (i.e. a label) for the absolute end of the generated code
4175    for a function definition.  This gets called *after* the epilogue code has
4176    been generated.  */
4177
4178 void
4179 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4180                         const char *file ATTRIBUTE_UNUSED)
4181 {
4182   dw_fde_ref fde;
4183   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4184
4185   last_var_location_insn = NULL_RTX;
4186
4187   if (dwarf2out_do_cfi_asm ())
4188     fprintf (asm_out_file, "\t.cfi_endproc\n");
4189
4190   /* Output a label to mark the endpoint of the code generated for this
4191      function.  */
4192   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4193                                current_function_funcdef_no);
4194   ASM_OUTPUT_LABEL (asm_out_file, label);
4195   fde = current_fde ();
4196   gcc_assert (fde != NULL);
4197   if (fde->dw_fde_second_begin == NULL)
4198     fde->dw_fde_end = xstrdup (label);
4199 }
4200
4201 void
4202 dwarf2out_frame_init (void)
4203 {
4204   /* Allocate the initial hunk of the fde_table.  */
4205   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4206   fde_table_allocated = FDE_TABLE_INCREMENT;
4207   fde_table_in_use = 0;
4208
4209   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4210      sake of lookup_cfa.  */
4211
4212   /* On entry, the Canonical Frame Address is at SP.  */
4213   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4214
4215   if (targetm.debug_unwind_info () == UI_DWARF2
4216       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4217     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4218 }
4219
4220 void
4221 dwarf2out_frame_finish (void)
4222 {
4223   /* Output call frame information.  */
4224   if (targetm.debug_unwind_info () == UI_DWARF2)
4225     output_call_frame_info (0);
4226
4227   /* Output another copy for the unwinder.  */
4228   if ((flag_unwind_tables || flag_exceptions)
4229       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4230     output_call_frame_info (1);
4231 }
4232
4233 /* Note that the current function section is being used for code.  */
4234
4235 static void
4236 dwarf2out_note_section_used (void)
4237 {
4238   section *sec = current_function_section ();
4239   if (sec == text_section)
4240     text_section_used = true;
4241   else if (sec == cold_text_section)
4242     cold_text_section_used = true;
4243 }
4244
4245 static void var_location_switch_text_section (void);
4246
4247 void
4248 dwarf2out_switch_text_section (void)
4249 {
4250   section *sect;
4251   dw_fde_ref fde = current_fde ();
4252   dw_cfi_ref cfi;
4253
4254   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4255
4256   if (!in_cold_section_p)
4257     {
4258       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4259       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4260       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4261     }
4262   else
4263     {
4264       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4265       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4266       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4267     }
4268   have_multiple_function_sections = true;
4269
4270   /* Reset the current label on switching text sections, so that we
4271      don't attempt to advance_loc4 between labels in different sections.  */
4272   fde->dw_fde_current_label = NULL;
4273
4274   /* There is no need to mark used sections when not debugging.  */
4275   if (cold_text_section != NULL)
4276     dwarf2out_note_section_used ();
4277
4278   if (dwarf2out_do_cfi_asm ())
4279     fprintf (asm_out_file, "\t.cfi_endproc\n");
4280
4281   /* Now do the real section switch.  */
4282   sect = current_function_section ();
4283   switch_to_section (sect);
4284
4285   fde->second_in_std_section
4286     = (sect == text_section
4287        || (cold_text_section && sect == cold_text_section));
4288
4289   if (dwarf2out_do_cfi_asm ())
4290     {
4291       dwarf2out_do_cfi_startproc (true);
4292       /* As this is a different FDE, insert all current CFI instructions
4293          again.  */
4294       output_cfis (fde->dw_fde_cfi, true, fde, true);
4295     }
4296   cfi = fde->dw_fde_cfi;
4297   if (cfi)
4298     while (cfi->dw_cfi_next != NULL)
4299       cfi = cfi->dw_cfi_next;
4300   fde->dw_fde_switch_cfi = cfi;
4301   var_location_switch_text_section ();
4302 }
4303 \f
4304 /* And now, the subset of the debugging information support code necessary
4305    for emitting location expressions.  */
4306
4307 /* Data about a single source file.  */
4308 struct GTY(()) dwarf_file_data {
4309   const char * filename;
4310   int emitted_number;
4311 };
4312
4313 typedef struct dw_val_struct *dw_val_ref;
4314 typedef struct die_struct *dw_die_ref;
4315 typedef const struct die_struct *const_dw_die_ref;
4316 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4317 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4318
4319 typedef struct GTY(()) deferred_locations_struct
4320 {
4321   tree variable;
4322   dw_die_ref die;
4323 } deferred_locations;
4324
4325 DEF_VEC_O(deferred_locations);
4326 DEF_VEC_ALLOC_O(deferred_locations,gc);
4327
4328 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4329
4330 DEF_VEC_P(dw_die_ref);
4331 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4332
4333 /* Each DIE may have a series of attribute/value pairs.  Values
4334    can take on several forms.  The forms that are used in this
4335    implementation are listed below.  */
4336
4337 enum dw_val_class
4338 {
4339   dw_val_class_addr,
4340   dw_val_class_offset,
4341   dw_val_class_loc,
4342   dw_val_class_loc_list,
4343   dw_val_class_range_list,
4344   dw_val_class_const,
4345   dw_val_class_unsigned_const,
4346   dw_val_class_const_double,
4347   dw_val_class_vec,
4348   dw_val_class_flag,
4349   dw_val_class_die_ref,
4350   dw_val_class_fde_ref,
4351   dw_val_class_lbl_id,
4352   dw_val_class_lineptr,
4353   dw_val_class_str,
4354   dw_val_class_macptr,
4355   dw_val_class_file,
4356   dw_val_class_data8,
4357   dw_val_class_decl_ref,
4358   dw_val_class_vms_delta
4359 };
4360
4361 /* Describe a floating point constant value, or a vector constant value.  */
4362
4363 typedef struct GTY(()) dw_vec_struct {
4364   unsigned char * GTY((length ("%h.length"))) array;
4365   unsigned length;
4366   unsigned elt_size;
4367 }
4368 dw_vec_const;
4369
4370 /* The dw_val_node describes an attribute's value, as it is
4371    represented internally.  */
4372
4373 typedef struct GTY(()) dw_val_struct {
4374   enum dw_val_class val_class;
4375   union dw_val_struct_union
4376     {
4377       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4378       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4379       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4380       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4381       HOST_WIDE_INT GTY ((default)) val_int;
4382       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4383       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4384       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4385       struct dw_val_die_union
4386         {
4387           dw_die_ref die;
4388           int external;
4389         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4390       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4391       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4392       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4393       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4394       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4395       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4396       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4397       struct dw_val_vms_delta_union
4398         {
4399           char * lbl1;
4400           char * lbl2;
4401         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4402     }
4403   GTY ((desc ("%1.val_class"))) v;
4404 }
4405 dw_val_node;
4406
4407 /* Locations in memory are described using a sequence of stack machine
4408    operations.  */
4409
4410 typedef struct GTY(()) dw_loc_descr_struct {
4411   dw_loc_descr_ref dw_loc_next;
4412   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4413   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4414      from DW_OP_addr with a dtp-relative symbol relocation.  */
4415   unsigned int dtprel : 1;
4416   int dw_loc_addr;
4417   dw_val_node dw_loc_oprnd1;
4418   dw_val_node dw_loc_oprnd2;
4419 }
4420 dw_loc_descr_node;
4421
4422 /* Location lists are ranges + location descriptions for that range,
4423    so you can track variables that are in different places over
4424    their entire life.  */
4425 typedef struct GTY(()) dw_loc_list_struct {
4426   dw_loc_list_ref dw_loc_next;
4427   const char *begin; /* Label for begin address of range */
4428   const char *end;  /* Label for end address of range */
4429   char *ll_symbol; /* Label for beginning of location list.
4430                       Only on head of list */
4431   const char *section; /* Section this loclist is relative to */
4432   dw_loc_descr_ref expr;
4433   hashval_t hash;
4434   /* True if all addresses in this and subsequent lists are known to be
4435      resolved.  */
4436   bool resolved_addr;
4437   /* True if this list has been replaced by dw_loc_next.  */
4438   bool replaced;
4439   bool emitted;
4440 } dw_loc_list_node;
4441
4442 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4443
4444 /* Convert a DWARF stack opcode into its string name.  */
4445
4446 static const char *
4447 dwarf_stack_op_name (unsigned int op)
4448 {
4449   switch (op)
4450     {
4451     case DW_OP_addr:
4452       return "DW_OP_addr";
4453     case DW_OP_deref:
4454       return "DW_OP_deref";
4455     case DW_OP_const1u:
4456       return "DW_OP_const1u";
4457     case DW_OP_const1s:
4458       return "DW_OP_const1s";
4459     case DW_OP_const2u:
4460       return "DW_OP_const2u";
4461     case DW_OP_const2s:
4462       return "DW_OP_const2s";
4463     case DW_OP_const4u:
4464       return "DW_OP_const4u";
4465     case DW_OP_const4s:
4466       return "DW_OP_const4s";
4467     case DW_OP_const8u:
4468       return "DW_OP_const8u";
4469     case DW_OP_const8s:
4470       return "DW_OP_const8s";
4471     case DW_OP_constu:
4472       return "DW_OP_constu";
4473     case DW_OP_consts:
4474       return "DW_OP_consts";
4475     case DW_OP_dup:
4476       return "DW_OP_dup";
4477     case DW_OP_drop:
4478       return "DW_OP_drop";
4479     case DW_OP_over:
4480       return "DW_OP_over";
4481     case DW_OP_pick:
4482       return "DW_OP_pick";
4483     case DW_OP_swap:
4484       return "DW_OP_swap";
4485     case DW_OP_rot:
4486       return "DW_OP_rot";
4487     case DW_OP_xderef:
4488       return "DW_OP_xderef";
4489     case DW_OP_abs:
4490       return "DW_OP_abs";
4491     case DW_OP_and:
4492       return "DW_OP_and";
4493     case DW_OP_div:
4494       return "DW_OP_div";
4495     case DW_OP_minus:
4496       return "DW_OP_minus";
4497     case DW_OP_mod:
4498       return "DW_OP_mod";
4499     case DW_OP_mul:
4500       return "DW_OP_mul";
4501     case DW_OP_neg:
4502       return "DW_OP_neg";
4503     case DW_OP_not:
4504       return "DW_OP_not";
4505     case DW_OP_or:
4506       return "DW_OP_or";
4507     case DW_OP_plus:
4508       return "DW_OP_plus";
4509     case DW_OP_plus_uconst:
4510       return "DW_OP_plus_uconst";
4511     case DW_OP_shl:
4512       return "DW_OP_shl";
4513     case DW_OP_shr:
4514       return "DW_OP_shr";
4515     case DW_OP_shra:
4516       return "DW_OP_shra";
4517     case DW_OP_xor:
4518       return "DW_OP_xor";
4519     case DW_OP_bra:
4520       return "DW_OP_bra";
4521     case DW_OP_eq:
4522       return "DW_OP_eq";
4523     case DW_OP_ge:
4524       return "DW_OP_ge";
4525     case DW_OP_gt:
4526       return "DW_OP_gt";
4527     case DW_OP_le:
4528       return "DW_OP_le";
4529     case DW_OP_lt:
4530       return "DW_OP_lt";
4531     case DW_OP_ne:
4532       return "DW_OP_ne";
4533     case DW_OP_skip:
4534       return "DW_OP_skip";
4535     case DW_OP_lit0:
4536       return "DW_OP_lit0";
4537     case DW_OP_lit1:
4538       return "DW_OP_lit1";
4539     case DW_OP_lit2:
4540       return "DW_OP_lit2";
4541     case DW_OP_lit3:
4542       return "DW_OP_lit3";
4543     case DW_OP_lit4:
4544       return "DW_OP_lit4";
4545     case DW_OP_lit5:
4546       return "DW_OP_lit5";
4547     case DW_OP_lit6:
4548       return "DW_OP_lit6";
4549     case DW_OP_lit7:
4550       return "DW_OP_lit7";
4551     case DW_OP_lit8:
4552       return "DW_OP_lit8";
4553     case DW_OP_lit9:
4554       return "DW_OP_lit9";
4555     case DW_OP_lit10:
4556       return "DW_OP_lit10";
4557     case DW_OP_lit11:
4558       return "DW_OP_lit11";
4559     case DW_OP_lit12:
4560       return "DW_OP_lit12";
4561     case DW_OP_lit13:
4562       return "DW_OP_lit13";
4563     case DW_OP_lit14:
4564       return "DW_OP_lit14";
4565     case DW_OP_lit15:
4566       return "DW_OP_lit15";
4567     case DW_OP_lit16:
4568       return "DW_OP_lit16";
4569     case DW_OP_lit17:
4570       return "DW_OP_lit17";
4571     case DW_OP_lit18:
4572       return "DW_OP_lit18";
4573     case DW_OP_lit19:
4574       return "DW_OP_lit19";
4575     case DW_OP_lit20:
4576       return "DW_OP_lit20";
4577     case DW_OP_lit21:
4578       return "DW_OP_lit21";
4579     case DW_OP_lit22:
4580       return "DW_OP_lit22";
4581     case DW_OP_lit23:
4582       return "DW_OP_lit23";
4583     case DW_OP_lit24:
4584       return "DW_OP_lit24";
4585     case DW_OP_lit25:
4586       return "DW_OP_lit25";
4587     case DW_OP_lit26:
4588       return "DW_OP_lit26";
4589     case DW_OP_lit27:
4590       return "DW_OP_lit27";
4591     case DW_OP_lit28:
4592       return "DW_OP_lit28";
4593     case DW_OP_lit29:
4594       return "DW_OP_lit29";
4595     case DW_OP_lit30:
4596       return "DW_OP_lit30";
4597     case DW_OP_lit31:
4598       return "DW_OP_lit31";
4599     case DW_OP_reg0:
4600       return "DW_OP_reg0";
4601     case DW_OP_reg1:
4602       return "DW_OP_reg1";
4603     case DW_OP_reg2:
4604       return "DW_OP_reg2";
4605     case DW_OP_reg3:
4606       return "DW_OP_reg3";
4607     case DW_OP_reg4:
4608       return "DW_OP_reg4";
4609     case DW_OP_reg5:
4610       return "DW_OP_reg5";
4611     case DW_OP_reg6:
4612       return "DW_OP_reg6";
4613     case DW_OP_reg7:
4614       return "DW_OP_reg7";
4615     case DW_OP_reg8:
4616       return "DW_OP_reg8";
4617     case DW_OP_reg9:
4618       return "DW_OP_reg9";
4619     case DW_OP_reg10:
4620       return "DW_OP_reg10";
4621     case DW_OP_reg11:
4622       return "DW_OP_reg11";
4623     case DW_OP_reg12:
4624       return "DW_OP_reg12";
4625     case DW_OP_reg13:
4626       return "DW_OP_reg13";
4627     case DW_OP_reg14:
4628       return "DW_OP_reg14";
4629     case DW_OP_reg15:
4630       return "DW_OP_reg15";
4631     case DW_OP_reg16:
4632       return "DW_OP_reg16";
4633     case DW_OP_reg17:
4634       return "DW_OP_reg17";
4635     case DW_OP_reg18:
4636       return "DW_OP_reg18";
4637     case DW_OP_reg19:
4638       return "DW_OP_reg19";
4639     case DW_OP_reg20:
4640       return "DW_OP_reg20";
4641     case DW_OP_reg21:
4642       return "DW_OP_reg21";
4643     case DW_OP_reg22:
4644       return "DW_OP_reg22";
4645     case DW_OP_reg23:
4646       return "DW_OP_reg23";
4647     case DW_OP_reg24:
4648       return "DW_OP_reg24";
4649     case DW_OP_reg25:
4650       return "DW_OP_reg25";
4651     case DW_OP_reg26:
4652       return "DW_OP_reg26";
4653     case DW_OP_reg27:
4654       return "DW_OP_reg27";
4655     case DW_OP_reg28:
4656       return "DW_OP_reg28";
4657     case DW_OP_reg29:
4658       return "DW_OP_reg29";
4659     case DW_OP_reg30:
4660       return "DW_OP_reg30";
4661     case DW_OP_reg31:
4662       return "DW_OP_reg31";
4663     case DW_OP_breg0:
4664       return "DW_OP_breg0";
4665     case DW_OP_breg1:
4666       return "DW_OP_breg1";
4667     case DW_OP_breg2:
4668       return "DW_OP_breg2";
4669     case DW_OP_breg3:
4670       return "DW_OP_breg3";
4671     case DW_OP_breg4:
4672       return "DW_OP_breg4";
4673     case DW_OP_breg5:
4674       return "DW_OP_breg5";
4675     case DW_OP_breg6:
4676       return "DW_OP_breg6";
4677     case DW_OP_breg7:
4678       return "DW_OP_breg7";
4679     case DW_OP_breg8:
4680       return "DW_OP_breg8";
4681     case DW_OP_breg9:
4682       return "DW_OP_breg9";
4683     case DW_OP_breg10:
4684       return "DW_OP_breg10";
4685     case DW_OP_breg11:
4686       return "DW_OP_breg11";
4687     case DW_OP_breg12:
4688       return "DW_OP_breg12";
4689     case DW_OP_breg13:
4690       return "DW_OP_breg13";
4691     case DW_OP_breg14:
4692       return "DW_OP_breg14";
4693     case DW_OP_breg15:
4694       return "DW_OP_breg15";
4695     case DW_OP_breg16:
4696       return "DW_OP_breg16";
4697     case DW_OP_breg17:
4698       return "DW_OP_breg17";
4699     case DW_OP_breg18:
4700       return "DW_OP_breg18";
4701     case DW_OP_breg19:
4702       return "DW_OP_breg19";
4703     case DW_OP_breg20:
4704       return "DW_OP_breg20";
4705     case DW_OP_breg21:
4706       return "DW_OP_breg21";
4707     case DW_OP_breg22:
4708       return "DW_OP_breg22";
4709     case DW_OP_breg23:
4710       return "DW_OP_breg23";
4711     case DW_OP_breg24:
4712       return "DW_OP_breg24";
4713     case DW_OP_breg25:
4714       return "DW_OP_breg25";
4715     case DW_OP_breg26:
4716       return "DW_OP_breg26";
4717     case DW_OP_breg27:
4718       return "DW_OP_breg27";
4719     case DW_OP_breg28:
4720       return "DW_OP_breg28";
4721     case DW_OP_breg29:
4722       return "DW_OP_breg29";
4723     case DW_OP_breg30:
4724       return "DW_OP_breg30";
4725     case DW_OP_breg31:
4726       return "DW_OP_breg31";
4727     case DW_OP_regx:
4728       return "DW_OP_regx";
4729     case DW_OP_fbreg:
4730       return "DW_OP_fbreg";
4731     case DW_OP_bregx:
4732       return "DW_OP_bregx";
4733     case DW_OP_piece:
4734       return "DW_OP_piece";
4735     case DW_OP_deref_size:
4736       return "DW_OP_deref_size";
4737     case DW_OP_xderef_size:
4738       return "DW_OP_xderef_size";
4739     case DW_OP_nop:
4740       return "DW_OP_nop";
4741
4742     case DW_OP_push_object_address:
4743       return "DW_OP_push_object_address";
4744     case DW_OP_call2:
4745       return "DW_OP_call2";
4746     case DW_OP_call4:
4747       return "DW_OP_call4";
4748     case DW_OP_call_ref:
4749       return "DW_OP_call_ref";
4750     case DW_OP_implicit_value:
4751       return "DW_OP_implicit_value";
4752     case DW_OP_stack_value:
4753       return "DW_OP_stack_value";
4754     case DW_OP_form_tls_address:
4755       return "DW_OP_form_tls_address";
4756     case DW_OP_call_frame_cfa:
4757       return "DW_OP_call_frame_cfa";
4758     case DW_OP_bit_piece:
4759       return "DW_OP_bit_piece";
4760
4761     case DW_OP_GNU_push_tls_address:
4762       return "DW_OP_GNU_push_tls_address";
4763     case DW_OP_GNU_uninit:
4764       return "DW_OP_GNU_uninit";
4765     case DW_OP_GNU_encoded_addr:
4766       return "DW_OP_GNU_encoded_addr";
4767     case DW_OP_GNU_implicit_pointer:
4768       return "DW_OP_GNU_implicit_pointer";
4769
4770     default:
4771       return "OP_<unknown>";
4772     }
4773 }
4774
4775 /* Return a pointer to a newly allocated location description.  Location
4776    descriptions are simple expression terms that can be strung
4777    together to form more complicated location (address) descriptions.  */
4778
4779 static inline dw_loc_descr_ref
4780 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4781                unsigned HOST_WIDE_INT oprnd2)
4782 {
4783   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4784
4785   descr->dw_loc_opc = op;
4786   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4787   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4788   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4789   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4790
4791   return descr;
4792 }
4793
4794 /* Return a pointer to a newly allocated location description for
4795    REG and OFFSET.  */
4796
4797 static inline dw_loc_descr_ref
4798 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4799 {
4800   if (reg <= 31)
4801     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4802                           offset, 0);
4803   else
4804     return new_loc_descr (DW_OP_bregx, reg, offset);
4805 }
4806
4807 /* Add a location description term to a location description expression.  */
4808
4809 static inline void
4810 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4811 {
4812   dw_loc_descr_ref *d;
4813
4814   /* Find the end of the chain.  */
4815   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4816     ;
4817
4818   *d = descr;
4819 }
4820
4821 /* Add a constant OFFSET to a location expression.  */
4822
4823 static void
4824 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4825 {
4826   dw_loc_descr_ref loc;
4827   HOST_WIDE_INT *p;
4828
4829   gcc_assert (*list_head != NULL);
4830
4831   if (!offset)
4832     return;
4833
4834   /* Find the end of the chain.  */
4835   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4836     ;
4837
4838   p = NULL;
4839   if (loc->dw_loc_opc == DW_OP_fbreg
4840       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4841     p = &loc->dw_loc_oprnd1.v.val_int;
4842   else if (loc->dw_loc_opc == DW_OP_bregx)
4843     p = &loc->dw_loc_oprnd2.v.val_int;
4844
4845   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4846      offset.  Don't optimize if an signed integer overflow would happen.  */
4847   if (p != NULL
4848       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4849           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4850     *p += offset;
4851
4852   else if (offset > 0)
4853     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4854
4855   else
4856     {
4857       loc->dw_loc_next = int_loc_descriptor (-offset);
4858       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4859     }
4860 }
4861
4862 /* Add a constant OFFSET to a location list.  */
4863
4864 static void
4865 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4866 {
4867   dw_loc_list_ref d;
4868   for (d = list_head; d != NULL; d = d->dw_loc_next)
4869     loc_descr_plus_const (&d->expr, offset);
4870 }
4871
4872 #define DWARF_REF_SIZE  \
4873   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4874
4875 /* Return the size of a location descriptor.  */
4876
4877 static unsigned long
4878 size_of_loc_descr (dw_loc_descr_ref loc)
4879 {
4880   unsigned long size = 1;
4881
4882   switch (loc->dw_loc_opc)
4883     {
4884     case DW_OP_addr:
4885       size += DWARF2_ADDR_SIZE;
4886       break;
4887     case DW_OP_const1u:
4888     case DW_OP_const1s:
4889       size += 1;
4890       break;
4891     case DW_OP_const2u:
4892     case DW_OP_const2s:
4893       size += 2;
4894       break;
4895     case DW_OP_const4u:
4896     case DW_OP_const4s:
4897       size += 4;
4898       break;
4899     case DW_OP_const8u:
4900     case DW_OP_const8s:
4901       size += 8;
4902       break;
4903     case DW_OP_constu:
4904       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4905       break;
4906     case DW_OP_consts:
4907       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4908       break;
4909     case DW_OP_pick:
4910       size += 1;
4911       break;
4912     case DW_OP_plus_uconst:
4913       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4914       break;
4915     case DW_OP_skip:
4916     case DW_OP_bra:
4917       size += 2;
4918       break;
4919     case DW_OP_breg0:
4920     case DW_OP_breg1:
4921     case DW_OP_breg2:
4922     case DW_OP_breg3:
4923     case DW_OP_breg4:
4924     case DW_OP_breg5:
4925     case DW_OP_breg6:
4926     case DW_OP_breg7:
4927     case DW_OP_breg8:
4928     case DW_OP_breg9:
4929     case DW_OP_breg10:
4930     case DW_OP_breg11:
4931     case DW_OP_breg12:
4932     case DW_OP_breg13:
4933     case DW_OP_breg14:
4934     case DW_OP_breg15:
4935     case DW_OP_breg16:
4936     case DW_OP_breg17:
4937     case DW_OP_breg18:
4938     case DW_OP_breg19:
4939     case DW_OP_breg20:
4940     case DW_OP_breg21:
4941     case DW_OP_breg22:
4942     case DW_OP_breg23:
4943     case DW_OP_breg24:
4944     case DW_OP_breg25:
4945     case DW_OP_breg26:
4946     case DW_OP_breg27:
4947     case DW_OP_breg28:
4948     case DW_OP_breg29:
4949     case DW_OP_breg30:
4950     case DW_OP_breg31:
4951       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4952       break;
4953     case DW_OP_regx:
4954       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4955       break;
4956     case DW_OP_fbreg:
4957       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4958       break;
4959     case DW_OP_bregx:
4960       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4961       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4962       break;
4963     case DW_OP_piece:
4964       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4965       break;
4966     case DW_OP_bit_piece:
4967       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4968       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4969       break;
4970     case DW_OP_deref_size:
4971     case DW_OP_xderef_size:
4972       size += 1;
4973       break;
4974     case DW_OP_call2:
4975       size += 2;
4976       break;
4977     case DW_OP_call4:
4978       size += 4;
4979       break;
4980     case DW_OP_call_ref:
4981       size += DWARF_REF_SIZE;
4982       break;
4983     case DW_OP_implicit_value:
4984       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4985               + loc->dw_loc_oprnd1.v.val_unsigned;
4986       break;
4987     case DW_OP_GNU_implicit_pointer:
4988       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4989       break;
4990     default:
4991       break;
4992     }
4993
4994   return size;
4995 }
4996
4997 /* Return the size of a series of location descriptors.  */
4998
4999 static unsigned long
5000 size_of_locs (dw_loc_descr_ref loc)
5001 {
5002   dw_loc_descr_ref l;
5003   unsigned long size;
5004
5005   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5006      field, to avoid writing to a PCH file.  */
5007   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5008     {
5009       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5010         break;
5011       size += size_of_loc_descr (l);
5012     }
5013   if (! l)
5014     return size;
5015
5016   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5017     {
5018       l->dw_loc_addr = size;
5019       size += size_of_loc_descr (l);
5020     }
5021
5022   return size;
5023 }
5024
5025 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5026 static void get_ref_die_offset_label (char *, dw_die_ref);
5027
5028 /* Output location description stack opcode's operands (if any).
5029    The for_eh_or_skip parameter controls whether register numbers are
5030    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5031    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5032    info).  This should be suppressed for the cases that have not been converted
5033    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5034
5035 static void
5036 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5037 {
5038   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5039   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5040
5041   switch (loc->dw_loc_opc)
5042     {
5043 #ifdef DWARF2_DEBUGGING_INFO
5044     case DW_OP_const2u:
5045     case DW_OP_const2s:
5046       dw2_asm_output_data (2, val1->v.val_int, NULL);
5047       break;
5048     case DW_OP_const4u:
5049       if (loc->dtprel)
5050         {
5051           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5052           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5053                                                val1->v.val_addr);
5054           fputc ('\n', asm_out_file);
5055           break;
5056         }
5057       /* FALLTHRU */
5058     case DW_OP_const4s:
5059       dw2_asm_output_data (4, val1->v.val_int, NULL);
5060       break;
5061     case DW_OP_const8u:
5062       if (loc->dtprel)
5063         {
5064           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5065           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5066                                                val1->v.val_addr);
5067           fputc ('\n', asm_out_file);
5068           break;
5069         }
5070       /* FALLTHRU */
5071     case DW_OP_const8s:
5072       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5073       dw2_asm_output_data (8, val1->v.val_int, NULL);
5074       break;
5075     case DW_OP_skip:
5076     case DW_OP_bra:
5077       {
5078         int offset;
5079
5080         gcc_assert (val1->val_class == dw_val_class_loc);
5081         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5082
5083         dw2_asm_output_data (2, offset, NULL);
5084       }
5085       break;
5086     case DW_OP_implicit_value:
5087       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5088       switch (val2->val_class)
5089         {
5090         case dw_val_class_const:
5091           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5092           break;
5093         case dw_val_class_vec:
5094           {
5095             unsigned int elt_size = val2->v.val_vec.elt_size;
5096             unsigned int len = val2->v.val_vec.length;
5097             unsigned int i;
5098             unsigned char *p;
5099
5100             if (elt_size > sizeof (HOST_WIDE_INT))
5101               {
5102                 elt_size /= 2;
5103                 len *= 2;
5104               }
5105             for (i = 0, p = val2->v.val_vec.array;
5106                  i < len;
5107                  i++, p += elt_size)
5108               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5109                                    "fp or vector constant word %u", i);
5110           }
5111           break;
5112         case dw_val_class_const_double:
5113           {
5114             unsigned HOST_WIDE_INT first, second;
5115
5116             if (WORDS_BIG_ENDIAN)
5117               {
5118                 first = val2->v.val_double.high;
5119                 second = val2->v.val_double.low;
5120               }
5121             else
5122               {
5123                 first = val2->v.val_double.low;
5124                 second = val2->v.val_double.high;
5125               }
5126             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5127                                  first, NULL);
5128             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5129                                  second, NULL);
5130           }
5131           break;
5132         case dw_val_class_addr:
5133           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5134           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5135           break;
5136         default:
5137           gcc_unreachable ();
5138         }
5139       break;
5140 #else
5141     case DW_OP_const2u:
5142     case DW_OP_const2s:
5143     case DW_OP_const4u:
5144     case DW_OP_const4s:
5145     case DW_OP_const8u:
5146     case DW_OP_const8s:
5147     case DW_OP_skip:
5148     case DW_OP_bra:
5149     case DW_OP_implicit_value:
5150       /* We currently don't make any attempt to make sure these are
5151          aligned properly like we do for the main unwind info, so
5152          don't support emitting things larger than a byte if we're
5153          only doing unwinding.  */
5154       gcc_unreachable ();
5155 #endif
5156     case DW_OP_const1u:
5157     case DW_OP_const1s:
5158       dw2_asm_output_data (1, val1->v.val_int, NULL);
5159       break;
5160     case DW_OP_constu:
5161       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5162       break;
5163     case DW_OP_consts:
5164       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5165       break;
5166     case DW_OP_pick:
5167       dw2_asm_output_data (1, val1->v.val_int, NULL);
5168       break;
5169     case DW_OP_plus_uconst:
5170       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5171       break;
5172     case DW_OP_breg0:
5173     case DW_OP_breg1:
5174     case DW_OP_breg2:
5175     case DW_OP_breg3:
5176     case DW_OP_breg4:
5177     case DW_OP_breg5:
5178     case DW_OP_breg6:
5179     case DW_OP_breg7:
5180     case DW_OP_breg8:
5181     case DW_OP_breg9:
5182     case DW_OP_breg10:
5183     case DW_OP_breg11:
5184     case DW_OP_breg12:
5185     case DW_OP_breg13:
5186     case DW_OP_breg14:
5187     case DW_OP_breg15:
5188     case DW_OP_breg16:
5189     case DW_OP_breg17:
5190     case DW_OP_breg18:
5191     case DW_OP_breg19:
5192     case DW_OP_breg20:
5193     case DW_OP_breg21:
5194     case DW_OP_breg22:
5195     case DW_OP_breg23:
5196     case DW_OP_breg24:
5197     case DW_OP_breg25:
5198     case DW_OP_breg26:
5199     case DW_OP_breg27:
5200     case DW_OP_breg28:
5201     case DW_OP_breg29:
5202     case DW_OP_breg30:
5203     case DW_OP_breg31:
5204       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5205       break;
5206     case DW_OP_regx:
5207       {
5208         unsigned r = val1->v.val_unsigned;
5209         if (for_eh_or_skip >= 0)
5210           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5211         gcc_assert (size_of_uleb128 (r) 
5212                     == size_of_uleb128 (val1->v.val_unsigned));
5213         dw2_asm_output_data_uleb128 (r, NULL);  
5214       }
5215       break;
5216     case DW_OP_fbreg:
5217       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5218       break;
5219     case DW_OP_bregx:
5220       {
5221         unsigned r = val1->v.val_unsigned;
5222         if (for_eh_or_skip >= 0)
5223           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5224         gcc_assert (size_of_uleb128 (r) 
5225                     == size_of_uleb128 (val1->v.val_unsigned));
5226         dw2_asm_output_data_uleb128 (r, NULL);  
5227         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5228       }
5229       break;
5230     case DW_OP_piece:
5231       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5232       break;
5233     case DW_OP_bit_piece:
5234       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5235       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5236       break;
5237     case DW_OP_deref_size:
5238     case DW_OP_xderef_size:
5239       dw2_asm_output_data (1, val1->v.val_int, NULL);
5240       break;
5241
5242     case DW_OP_addr:
5243       if (loc->dtprel)
5244         {
5245           if (targetm.asm_out.output_dwarf_dtprel)
5246             {
5247               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5248                                                    DWARF2_ADDR_SIZE,
5249                                                    val1->v.val_addr);
5250               fputc ('\n', asm_out_file);
5251             }
5252           else
5253             gcc_unreachable ();
5254         }
5255       else
5256         {
5257 #ifdef DWARF2_DEBUGGING_INFO
5258           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5259 #else
5260           gcc_unreachable ();
5261 #endif
5262         }
5263       break;
5264
5265     case DW_OP_GNU_implicit_pointer:
5266       {
5267         char label[MAX_ARTIFICIAL_LABEL_BYTES
5268                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5269         gcc_assert (val1->val_class == dw_val_class_die_ref);
5270         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5271         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5272         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5273       }
5274       break;
5275
5276     default:
5277       /* Other codes have no operands.  */
5278       break;
5279     }
5280 }
5281
5282 /* Output a sequence of location operations.  
5283    The for_eh_or_skip parameter controls whether register numbers are
5284    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5285    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5286    info).  This should be suppressed for the cases that have not been converted
5287    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5288
5289 static void
5290 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5291 {
5292   for (; loc != NULL; loc = loc->dw_loc_next)
5293     {
5294       enum dwarf_location_atom opc = loc->dw_loc_opc;
5295       /* Output the opcode.  */
5296       if (for_eh_or_skip >= 0 
5297           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5298         {
5299           unsigned r = (opc - DW_OP_breg0);
5300           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5301           gcc_assert (r <= 31);
5302           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5303         }
5304       else if (for_eh_or_skip >= 0 
5305                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5306         {
5307           unsigned r = (opc - DW_OP_reg0);
5308           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5309           gcc_assert (r <= 31);
5310           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5311         }
5312
5313       dw2_asm_output_data (1, opc,
5314                              "%s", dwarf_stack_op_name (opc));
5315
5316       /* Output the operand(s) (if any).  */
5317       output_loc_operands (loc, for_eh_or_skip);
5318     }
5319 }
5320
5321 /* Output location description stack opcode's operands (if any).
5322    The output is single bytes on a line, suitable for .cfi_escape.  */
5323
5324 static void
5325 output_loc_operands_raw (dw_loc_descr_ref loc)
5326 {
5327   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5328   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5329
5330   switch (loc->dw_loc_opc)
5331     {
5332     case DW_OP_addr:
5333     case DW_OP_implicit_value:
5334       /* We cannot output addresses in .cfi_escape, only bytes.  */
5335       gcc_unreachable ();
5336
5337     case DW_OP_const1u:
5338     case DW_OP_const1s:
5339     case DW_OP_pick:
5340     case DW_OP_deref_size:
5341     case DW_OP_xderef_size:
5342       fputc (',', asm_out_file);
5343       dw2_asm_output_data_raw (1, val1->v.val_int);
5344       break;
5345
5346     case DW_OP_const2u:
5347     case DW_OP_const2s:
5348       fputc (',', asm_out_file);
5349       dw2_asm_output_data_raw (2, val1->v.val_int);
5350       break;
5351
5352     case DW_OP_const4u:
5353     case DW_OP_const4s:
5354       fputc (',', asm_out_file);
5355       dw2_asm_output_data_raw (4, val1->v.val_int);
5356       break;
5357
5358     case DW_OP_const8u:
5359     case DW_OP_const8s:
5360       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5361       fputc (',', asm_out_file);
5362       dw2_asm_output_data_raw (8, val1->v.val_int);
5363       break;
5364
5365     case DW_OP_skip:
5366     case DW_OP_bra:
5367       {
5368         int offset;
5369
5370         gcc_assert (val1->val_class == dw_val_class_loc);
5371         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5372
5373         fputc (',', asm_out_file);
5374         dw2_asm_output_data_raw (2, offset);
5375       }
5376       break;
5377
5378     case DW_OP_regx:
5379       {
5380         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5381         gcc_assert (size_of_uleb128 (r) 
5382                     == size_of_uleb128 (val1->v.val_unsigned));
5383         fputc (',', asm_out_file);
5384         dw2_asm_output_data_uleb128_raw (r);
5385       }
5386       break;
5387       
5388     case DW_OP_constu:
5389     case DW_OP_plus_uconst:
5390     case DW_OP_piece:
5391       fputc (',', asm_out_file);
5392       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5393       break;
5394
5395     case DW_OP_bit_piece:
5396       fputc (',', asm_out_file);
5397       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5398       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5399       break;
5400
5401     case DW_OP_consts:
5402     case DW_OP_breg0:
5403     case DW_OP_breg1:
5404     case DW_OP_breg2:
5405     case DW_OP_breg3:
5406     case DW_OP_breg4:
5407     case DW_OP_breg5:
5408     case DW_OP_breg6:
5409     case DW_OP_breg7:
5410     case DW_OP_breg8:
5411     case DW_OP_breg9:
5412     case DW_OP_breg10:
5413     case DW_OP_breg11:
5414     case DW_OP_breg12:
5415     case DW_OP_breg13:
5416     case DW_OP_breg14:
5417     case DW_OP_breg15:
5418     case DW_OP_breg16:
5419     case DW_OP_breg17:
5420     case DW_OP_breg18:
5421     case DW_OP_breg19:
5422     case DW_OP_breg20:
5423     case DW_OP_breg21:
5424     case DW_OP_breg22:
5425     case DW_OP_breg23:
5426     case DW_OP_breg24:
5427     case DW_OP_breg25:
5428     case DW_OP_breg26:
5429     case DW_OP_breg27:
5430     case DW_OP_breg28:
5431     case DW_OP_breg29:
5432     case DW_OP_breg30:
5433     case DW_OP_breg31:
5434     case DW_OP_fbreg:
5435       fputc (',', asm_out_file);
5436       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5437       break;
5438
5439     case DW_OP_bregx:
5440       {
5441         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5442         gcc_assert (size_of_uleb128 (r) 
5443                     == size_of_uleb128 (val1->v.val_unsigned));
5444         fputc (',', asm_out_file);
5445         dw2_asm_output_data_uleb128_raw (r);
5446         fputc (',', asm_out_file);
5447         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5448       }
5449       break;
5450
5451     case DW_OP_GNU_implicit_pointer:
5452       gcc_unreachable ();
5453       break;
5454
5455     default:
5456       /* Other codes have no operands.  */
5457       break;
5458     }
5459 }
5460
5461 static void
5462 output_loc_sequence_raw (dw_loc_descr_ref loc)
5463 {
5464   while (1)
5465     {
5466       enum dwarf_location_atom opc = loc->dw_loc_opc;
5467       /* Output the opcode.  */
5468       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5469         {
5470           unsigned r = (opc - DW_OP_breg0);
5471           r = DWARF2_FRAME_REG_OUT (r, 1);
5472           gcc_assert (r <= 31);
5473           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5474         }
5475       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5476         {
5477           unsigned r = (opc - DW_OP_reg0);
5478           r = DWARF2_FRAME_REG_OUT (r, 1);
5479           gcc_assert (r <= 31);
5480           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5481         }
5482       /* Output the opcode.  */
5483       fprintf (asm_out_file, "%#x", opc);
5484       output_loc_operands_raw (loc);
5485
5486       if (!loc->dw_loc_next)
5487         break;
5488       loc = loc->dw_loc_next;
5489
5490       fputc (',', asm_out_file);
5491     }
5492 }
5493
5494 /* This routine will generate the correct assembly data for a location
5495    description based on a cfi entry with a complex address.  */
5496
5497 static void
5498 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5499 {
5500   dw_loc_descr_ref loc;
5501   unsigned long size;
5502
5503   if (cfi->dw_cfi_opc == DW_CFA_expression)
5504     {
5505       unsigned r = 
5506         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5507       dw2_asm_output_data (1, r, NULL);
5508       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5509     }
5510   else
5511     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5512
5513   /* Output the size of the block.  */
5514   size = size_of_locs (loc);
5515   dw2_asm_output_data_uleb128 (size, NULL);
5516
5517   /* Now output the operations themselves.  */
5518   output_loc_sequence (loc, for_eh);
5519 }
5520
5521 /* Similar, but used for .cfi_escape.  */
5522
5523 static void
5524 output_cfa_loc_raw (dw_cfi_ref cfi)
5525 {
5526   dw_loc_descr_ref loc;
5527   unsigned long size;
5528
5529   if (cfi->dw_cfi_opc == DW_CFA_expression)
5530     {
5531       unsigned r = 
5532         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5533       fprintf (asm_out_file, "%#x,", r);
5534       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5535     }
5536   else
5537     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5538
5539   /* Output the size of the block.  */
5540   size = size_of_locs (loc);
5541   dw2_asm_output_data_uleb128_raw (size);
5542   fputc (',', asm_out_file);
5543
5544   /* Now output the operations themselves.  */
5545   output_loc_sequence_raw (loc);
5546 }
5547
5548 /* This function builds a dwarf location descriptor sequence from a
5549    dw_cfa_location, adding the given OFFSET to the result of the
5550    expression.  */
5551
5552 static struct dw_loc_descr_struct *
5553 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5554 {
5555   struct dw_loc_descr_struct *head, *tmp;
5556
5557   offset += cfa->offset;
5558
5559   if (cfa->indirect)
5560     {
5561       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5562       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5563       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5564       add_loc_descr (&head, tmp);
5565       if (offset != 0)
5566         {
5567           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5568           add_loc_descr (&head, tmp);
5569         }
5570     }
5571   else
5572     head = new_reg_loc_descr (cfa->reg, offset);
5573
5574   return head;
5575 }
5576
5577 /* This function builds a dwarf location descriptor sequence for
5578    the address at OFFSET from the CFA when stack is aligned to
5579    ALIGNMENT byte.  */
5580
5581 static struct dw_loc_descr_struct *
5582 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5583 {
5584   struct dw_loc_descr_struct *head;
5585   unsigned int dwarf_fp
5586     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5587
5588  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5589   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5590     {
5591       head = new_reg_loc_descr (dwarf_fp, 0);
5592       add_loc_descr (&head, int_loc_descriptor (alignment));
5593       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5594       loc_descr_plus_const (&head, offset);
5595     }
5596   else
5597     head = new_reg_loc_descr (dwarf_fp, offset);
5598   return head;
5599 }
5600
5601 /* This function fills in aa dw_cfa_location structure from a dwarf location
5602    descriptor sequence.  */
5603
5604 static void
5605 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5606 {
5607   struct dw_loc_descr_struct *ptr;
5608   cfa->offset = 0;
5609   cfa->base_offset = 0;
5610   cfa->indirect = 0;
5611   cfa->reg = -1;
5612
5613   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5614     {
5615       enum dwarf_location_atom op = ptr->dw_loc_opc;
5616
5617       switch (op)
5618         {
5619         case DW_OP_reg0:
5620         case DW_OP_reg1:
5621         case DW_OP_reg2:
5622         case DW_OP_reg3:
5623         case DW_OP_reg4:
5624         case DW_OP_reg5:
5625         case DW_OP_reg6:
5626         case DW_OP_reg7:
5627         case DW_OP_reg8:
5628         case DW_OP_reg9:
5629         case DW_OP_reg10:
5630         case DW_OP_reg11:
5631         case DW_OP_reg12:
5632         case DW_OP_reg13:
5633         case DW_OP_reg14:
5634         case DW_OP_reg15:
5635         case DW_OP_reg16:
5636         case DW_OP_reg17:
5637         case DW_OP_reg18:
5638         case DW_OP_reg19:
5639         case DW_OP_reg20:
5640         case DW_OP_reg21:
5641         case DW_OP_reg22:
5642         case DW_OP_reg23:
5643         case DW_OP_reg24:
5644         case DW_OP_reg25:
5645         case DW_OP_reg26:
5646         case DW_OP_reg27:
5647         case DW_OP_reg28:
5648         case DW_OP_reg29:
5649         case DW_OP_reg30:
5650         case DW_OP_reg31:
5651           cfa->reg = op - DW_OP_reg0;
5652           break;
5653         case DW_OP_regx:
5654           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5655           break;
5656         case DW_OP_breg0:
5657         case DW_OP_breg1:
5658         case DW_OP_breg2:
5659         case DW_OP_breg3:
5660         case DW_OP_breg4:
5661         case DW_OP_breg5:
5662         case DW_OP_breg6:
5663         case DW_OP_breg7:
5664         case DW_OP_breg8:
5665         case DW_OP_breg9:
5666         case DW_OP_breg10:
5667         case DW_OP_breg11:
5668         case DW_OP_breg12:
5669         case DW_OP_breg13:
5670         case DW_OP_breg14:
5671         case DW_OP_breg15:
5672         case DW_OP_breg16:
5673         case DW_OP_breg17:
5674         case DW_OP_breg18:
5675         case DW_OP_breg19:
5676         case DW_OP_breg20:
5677         case DW_OP_breg21:
5678         case DW_OP_breg22:
5679         case DW_OP_breg23:
5680         case DW_OP_breg24:
5681         case DW_OP_breg25:
5682         case DW_OP_breg26:
5683         case DW_OP_breg27:
5684         case DW_OP_breg28:
5685         case DW_OP_breg29:
5686         case DW_OP_breg30:
5687         case DW_OP_breg31:
5688           cfa->reg = op - DW_OP_breg0;
5689           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5690           break;
5691         case DW_OP_bregx:
5692           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5693           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5694           break;
5695         case DW_OP_deref:
5696           cfa->indirect = 1;
5697           break;
5698         case DW_OP_plus_uconst:
5699           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5700           break;
5701         default:
5702           internal_error ("DW_LOC_OP %s not implemented",
5703                           dwarf_stack_op_name (ptr->dw_loc_opc));
5704         }
5705     }
5706 }
5707 \f
5708 /* And now, the support for symbolic debugging information.  */
5709
5710 /* .debug_str support.  */
5711 static int output_indirect_string (void **, void *);
5712
5713 static void dwarf2out_init (const char *);
5714 static void dwarf2out_finish (const char *);
5715 static void dwarf2out_assembly_start (void);
5716 static void dwarf2out_define (unsigned int, const char *);
5717 static void dwarf2out_undef (unsigned int, const char *);
5718 static void dwarf2out_start_source_file (unsigned, const char *);
5719 static void dwarf2out_end_source_file (unsigned);
5720 static void dwarf2out_function_decl (tree);
5721 static void dwarf2out_begin_block (unsigned, unsigned);
5722 static void dwarf2out_end_block (unsigned, unsigned);
5723 static bool dwarf2out_ignore_block (const_tree);
5724 static void dwarf2out_global_decl (tree);
5725 static void dwarf2out_type_decl (tree, int);
5726 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5727 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5728                                                  dw_die_ref);
5729 static void dwarf2out_abstract_function (tree);
5730 static void dwarf2out_var_location (rtx);
5731 static void dwarf2out_direct_call (tree);
5732 static void dwarf2out_virtual_call_token (tree, int);
5733 static void dwarf2out_copy_call_info (rtx, rtx);
5734 static void dwarf2out_virtual_call (int);
5735 static void dwarf2out_begin_function (tree);
5736 static void dwarf2out_set_name (tree, tree);
5737
5738 /* The debug hooks structure.  */
5739
5740 const struct gcc_debug_hooks dwarf2_debug_hooks =
5741 {
5742   dwarf2out_init,
5743   dwarf2out_finish,
5744   dwarf2out_assembly_start,
5745   dwarf2out_define,
5746   dwarf2out_undef,
5747   dwarf2out_start_source_file,
5748   dwarf2out_end_source_file,
5749   dwarf2out_begin_block,
5750   dwarf2out_end_block,
5751   dwarf2out_ignore_block,
5752   dwarf2out_source_line,
5753   dwarf2out_begin_prologue,
5754 #if VMS_DEBUGGING_INFO
5755   dwarf2out_vms_end_prologue,
5756   dwarf2out_vms_begin_epilogue,
5757 #else
5758   debug_nothing_int_charstar,
5759   debug_nothing_int_charstar,
5760 #endif
5761   dwarf2out_end_epilogue,
5762   dwarf2out_begin_function,
5763   debug_nothing_int,            /* end_function */
5764   dwarf2out_function_decl,      /* function_decl */
5765   dwarf2out_global_decl,
5766   dwarf2out_type_decl,          /* type_decl */
5767   dwarf2out_imported_module_or_decl,
5768   debug_nothing_tree,           /* deferred_inline_function */
5769   /* The DWARF 2 backend tries to reduce debugging bloat by not
5770      emitting the abstract description of inline functions until
5771      something tries to reference them.  */
5772   dwarf2out_abstract_function,  /* outlining_inline_function */
5773   debug_nothing_rtx,            /* label */
5774   debug_nothing_int,            /* handle_pch */
5775   dwarf2out_var_location,
5776   dwarf2out_switch_text_section,
5777   dwarf2out_direct_call,
5778   dwarf2out_virtual_call_token,
5779   dwarf2out_copy_call_info,
5780   dwarf2out_virtual_call,
5781   dwarf2out_set_name,
5782   1,                            /* start_end_main_source_file */
5783   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5784 };
5785 \f
5786 /* NOTE: In the comments in this file, many references are made to
5787    "Debugging Information Entries".  This term is abbreviated as `DIE'
5788    throughout the remainder of this file.  */
5789
5790 /* An internal representation of the DWARF output is built, and then
5791    walked to generate the DWARF debugging info.  The walk of the internal
5792    representation is done after the entire program has been compiled.
5793    The types below are used to describe the internal representation.  */
5794
5795 /* Various DIE's use offsets relative to the beginning of the
5796    .debug_info section to refer to each other.  */
5797
5798 typedef long int dw_offset;
5799
5800 /* Define typedefs here to avoid circular dependencies.  */
5801
5802 typedef struct dw_attr_struct *dw_attr_ref;
5803 typedef struct dw_line_info_struct *dw_line_info_ref;
5804 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5805 typedef struct pubname_struct *pubname_ref;
5806 typedef struct dw_ranges_struct *dw_ranges_ref;
5807 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5808 typedef struct comdat_type_struct *comdat_type_node_ref;
5809
5810 /* Each entry in the line_info_table maintains the file and
5811    line number associated with the label generated for that
5812    entry.  The label gives the PC value associated with
5813    the line number entry.  */
5814
5815 typedef struct GTY(()) dw_line_info_struct {
5816   unsigned long dw_file_num;
5817   unsigned long dw_line_num;
5818 }
5819 dw_line_info_entry;
5820
5821 /* Line information for functions in separate sections; each one gets its
5822    own sequence.  */
5823 typedef struct GTY(()) dw_separate_line_info_struct {
5824   unsigned long dw_file_num;
5825   unsigned long dw_line_num;
5826   unsigned long function;
5827 }
5828 dw_separate_line_info_entry;
5829
5830 /* Each DIE attribute has a field specifying the attribute kind,
5831    a link to the next attribute in the chain, and an attribute value.
5832    Attributes are typically linked below the DIE they modify.  */
5833
5834 typedef struct GTY(()) dw_attr_struct {
5835   enum dwarf_attribute dw_attr;
5836   dw_val_node dw_attr_val;
5837 }
5838 dw_attr_node;
5839
5840 DEF_VEC_O(dw_attr_node);
5841 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5842
5843 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5844    The children of each node form a circular list linked by
5845    die_sib.  die_child points to the node *before* the "first" child node.  */
5846
5847 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5848   union die_symbol_or_type_node
5849     {
5850       char * GTY ((tag ("0"))) die_symbol;
5851       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5852     }
5853   GTY ((desc ("dwarf_version >= 4"))) die_id;
5854   VEC(dw_attr_node,gc) * die_attr;
5855   dw_die_ref die_parent;
5856   dw_die_ref die_child;
5857   dw_die_ref die_sib;
5858   dw_die_ref die_definition; /* ref from a specification to its definition */
5859   dw_offset die_offset;
5860   unsigned long die_abbrev;
5861   int die_mark;
5862   /* Die is used and must not be pruned as unused.  */
5863   int die_perennial_p;
5864   unsigned int decl_id;
5865   enum dwarf_tag die_tag;
5866 }
5867 die_node;
5868
5869 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5870 #define FOR_EACH_CHILD(die, c, expr) do {       \
5871   c = die->die_child;                           \
5872   if (c) do {                                   \
5873     c = c->die_sib;                             \
5874     expr;                                       \
5875   } while (c != die->die_child);                \
5876 } while (0)
5877
5878 /* The pubname structure */
5879
5880 typedef struct GTY(()) pubname_struct {
5881   dw_die_ref die;
5882   const char *name;
5883 }
5884 pubname_entry;
5885
5886 DEF_VEC_O(pubname_entry);
5887 DEF_VEC_ALLOC_O(pubname_entry, gc);
5888
5889 struct GTY(()) dw_ranges_struct {
5890   /* If this is positive, it's a block number, otherwise it's a
5891      bitwise-negated index into dw_ranges_by_label.  */
5892   int num;
5893 };
5894
5895 /* A structure to hold a macinfo entry.  */
5896
5897 typedef struct GTY(()) macinfo_struct {
5898   unsigned HOST_WIDE_INT code;
5899   unsigned HOST_WIDE_INT lineno;
5900   const char *info;
5901 }
5902 macinfo_entry;
5903
5904 DEF_VEC_O(macinfo_entry);
5905 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5906
5907 struct GTY(()) dw_ranges_by_label_struct {
5908   const char *begin;
5909   const char *end;
5910 };
5911
5912 /* The comdat type node structure.  */
5913 typedef struct GTY(()) comdat_type_struct
5914 {
5915   dw_die_ref root_die;
5916   dw_die_ref type_die;
5917   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5918   struct comdat_type_struct *next;
5919 }
5920 comdat_type_node;
5921
5922 /* The limbo die list structure.  */
5923 typedef struct GTY(()) limbo_die_struct {
5924   dw_die_ref die;
5925   tree created_for;
5926   struct limbo_die_struct *next;
5927 }
5928 limbo_die_node;
5929
5930 typedef struct skeleton_chain_struct
5931 {
5932   dw_die_ref old_die;
5933   dw_die_ref new_die;
5934   struct skeleton_chain_struct *parent;
5935 }
5936 skeleton_chain_node;
5937
5938 /* How to start an assembler comment.  */
5939 #ifndef ASM_COMMENT_START
5940 #define ASM_COMMENT_START ";#"
5941 #endif
5942
5943 /* Define a macro which returns nonzero for a TYPE_DECL which was
5944    implicitly generated for a tagged type.
5945
5946    Note that unlike the gcc front end (which generates a NULL named
5947    TYPE_DECL node for each complete tagged type, each array type, and
5948    each function type node created) the g++ front end generates a
5949    _named_ TYPE_DECL node for each tagged type node created.
5950    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5951    generate a DW_TAG_typedef DIE for them.  */
5952
5953 #define TYPE_DECL_IS_STUB(decl)                         \
5954   (DECL_NAME (decl) == NULL_TREE                        \
5955    || (DECL_ARTIFICIAL (decl)                           \
5956        && is_tagged_type (TREE_TYPE (decl))             \
5957        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5958            /* This is necessary for stub decls that     \
5959               appear in nested inline functions.  */    \
5960            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5961                && (decl_ultimate_origin (decl)          \
5962                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5963
5964 /* Information concerning the compilation unit's programming
5965    language, and compiler version.  */
5966
5967 /* Fixed size portion of the DWARF compilation unit header.  */
5968 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5969   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5970
5971 /* Fixed size portion of the DWARF comdat type unit header.  */
5972 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5973   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5974    + DWARF_OFFSET_SIZE)
5975
5976 /* Fixed size portion of public names info.  */
5977 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5978
5979 /* Fixed size portion of the address range info.  */
5980 #define DWARF_ARANGES_HEADER_SIZE                                       \
5981   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5982                 DWARF2_ADDR_SIZE * 2)                                   \
5983    - DWARF_INITIAL_LENGTH_SIZE)
5984
5985 /* Size of padding portion in the address range info.  It must be
5986    aligned to twice the pointer size.  */
5987 #define DWARF_ARANGES_PAD_SIZE \
5988   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5989                 DWARF2_ADDR_SIZE * 2)                              \
5990    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5991
5992 /* Use assembler line directives if available.  */
5993 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5994 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5995 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5996 #else
5997 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5998 #endif
5999 #endif
6000
6001 /* Minimum line offset in a special line info. opcode.
6002    This value was chosen to give a reasonable range of values.  */
6003 #define DWARF_LINE_BASE  -10
6004
6005 /* First special line opcode - leave room for the standard opcodes.  */
6006 #define DWARF_LINE_OPCODE_BASE  10
6007
6008 /* Range of line offsets in a special line info. opcode.  */
6009 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6010
6011 /* Flag that indicates the initial value of the is_stmt_start flag.
6012    In the present implementation, we do not mark any lines as
6013    the beginning of a source statement, because that information
6014    is not made available by the GCC front-end.  */
6015 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6016
6017 /* Maximum number of operations per instruction bundle.  */
6018 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6019 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6020 #endif
6021
6022 /* This location is used by calc_die_sizes() to keep track
6023    the offset of each DIE within the .debug_info section.  */
6024 static unsigned long next_die_offset;
6025
6026 /* Record the root of the DIE's built for the current compilation unit.  */
6027 static GTY(()) dw_die_ref single_comp_unit_die;
6028
6029 /* A list of type DIEs that have been separated into comdat sections.  */
6030 static GTY(()) comdat_type_node *comdat_type_list;
6031
6032 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6033 static GTY(()) limbo_die_node *limbo_die_list;
6034
6035 /* A list of DIEs for which we may have to generate
6036    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6037 static GTY(()) limbo_die_node *deferred_asm_name;
6038
6039 /* Filenames referenced by this compilation unit.  */
6040 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6041
6042 /* A hash table of references to DIE's that describe declarations.
6043    The key is a DECL_UID() which is a unique number identifying each decl.  */
6044 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6045
6046 /* A hash table of references to DIE's that describe COMMON blocks.
6047    The key is DECL_UID() ^ die_parent.  */
6048 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6049
6050 typedef struct GTY(()) die_arg_entry_struct {
6051     dw_die_ref die;
6052     tree arg;
6053 } die_arg_entry;
6054
6055 DEF_VEC_O(die_arg_entry);
6056 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6057
6058 /* Node of the variable location list.  */
6059 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6060   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6061      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6062      in mode of the EXPR_LIST node and first EXPR_LIST operand
6063      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6064      location or NULL for padding.  For larger bitsizes,
6065      mode is 0 and first operand is a CONCAT with bitsize
6066      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6067      NULL as second operand.  */
6068   rtx GTY (()) loc;
6069   const char * GTY (()) label;
6070   struct var_loc_node * GTY (()) next;
6071 };
6072
6073 /* Variable location list.  */
6074 struct GTY (()) var_loc_list_def {
6075   struct var_loc_node * GTY (()) first;
6076
6077   /* Pointer to the last but one or last element of the
6078      chained list.  If the list is empty, both first and
6079      last are NULL, if the list contains just one node
6080      or the last node certainly is not redundant, it points
6081      to the last node, otherwise points to the last but one.
6082      Do not mark it for GC because it is marked through the chain.  */
6083   struct var_loc_node * GTY ((skip ("%h"))) last;
6084
6085   /* Pointer to the last element before section switch,
6086      if NULL, either sections weren't switched or first
6087      is after section switch.  */
6088   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6089
6090   /* DECL_UID of the variable decl.  */
6091   unsigned int decl_id;
6092 };
6093 typedef struct var_loc_list_def var_loc_list;
6094
6095
6096 /* Table of decl location linked lists.  */
6097 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6098
6099 /* A cached location list.  */
6100 struct GTY (()) cached_dw_loc_list_def {
6101   /* The DECL_UID of the decl that this entry describes.  */
6102   unsigned int decl_id;
6103
6104   /* The cached location list.  */
6105   dw_loc_list_ref loc_list;
6106 };
6107 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6108
6109 /* Table of cached location lists.  */
6110 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6111
6112 /* A pointer to the base of a list of references to DIE's that
6113    are uniquely identified by their tag, presence/absence of
6114    children DIE's, and list of attribute/value pairs.  */
6115 static GTY((length ("abbrev_die_table_allocated")))
6116   dw_die_ref *abbrev_die_table;
6117
6118 /* Number of elements currently allocated for abbrev_die_table.  */
6119 static GTY(()) unsigned abbrev_die_table_allocated;
6120
6121 /* Number of elements in type_die_table currently in use.  */
6122 static GTY(()) unsigned abbrev_die_table_in_use;
6123
6124 /* Size (in elements) of increments by which we may expand the
6125    abbrev_die_table.  */
6126 #define ABBREV_DIE_TABLE_INCREMENT 256
6127
6128 /* A pointer to the base of a table that contains line information
6129    for each source code line in .text in the compilation unit.  */
6130 static GTY((length ("line_info_table_allocated")))
6131      dw_line_info_ref line_info_table;
6132
6133 /* Number of elements currently allocated for line_info_table.  */
6134 static GTY(()) unsigned line_info_table_allocated;
6135
6136 /* Number of elements in line_info_table currently in use.  */
6137 static GTY(()) unsigned line_info_table_in_use;
6138
6139 /* A pointer to the base of a table that contains line information
6140    for each source code line outside of .text in the compilation unit.  */
6141 static GTY ((length ("separate_line_info_table_allocated")))
6142      dw_separate_line_info_ref separate_line_info_table;
6143
6144 /* Number of elements currently allocated for separate_line_info_table.  */
6145 static GTY(()) unsigned separate_line_info_table_allocated;
6146
6147 /* Number of elements in separate_line_info_table currently in use.  */
6148 static GTY(()) unsigned separate_line_info_table_in_use;
6149
6150 /* Size (in elements) of increments by which we may expand the
6151    line_info_table.  */
6152 #define LINE_INFO_TABLE_INCREMENT 1024
6153
6154 /* A flag to tell pubnames/types export if there is an info section to
6155    refer to.  */
6156 static bool info_section_emitted;
6157
6158 /* A pointer to the base of a table that contains a list of publicly
6159    accessible names.  */
6160 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6161
6162 /* A pointer to the base of a table that contains a list of publicly
6163    accessible types.  */
6164 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6165
6166 /* A pointer to the base of a table that contains a list of macro
6167    defines/undefines (and file start/end markers).  */
6168 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6169
6170 /* Array of dies for which we should generate .debug_ranges info.  */
6171 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6172
6173 /* Number of elements currently allocated for ranges_table.  */
6174 static GTY(()) unsigned ranges_table_allocated;
6175
6176 /* Number of elements in ranges_table currently in use.  */
6177 static GTY(()) unsigned ranges_table_in_use;
6178
6179 /* Array of pairs of labels referenced in ranges_table.  */
6180 static GTY ((length ("ranges_by_label_allocated")))
6181      dw_ranges_by_label_ref ranges_by_label;
6182
6183 /* Number of elements currently allocated for ranges_by_label.  */
6184 static GTY(()) unsigned ranges_by_label_allocated;
6185
6186 /* Number of elements in ranges_by_label currently in use.  */
6187 static GTY(()) unsigned ranges_by_label_in_use;
6188
6189 /* Size (in elements) of increments by which we may expand the
6190    ranges_table.  */
6191 #define RANGES_TABLE_INCREMENT 64
6192
6193 /* Whether we have location lists that need outputting */
6194 static GTY(()) bool have_location_lists;
6195
6196 /* Unique label counter.  */
6197 static GTY(()) unsigned int loclabel_num;
6198
6199 /* Unique label counter for point-of-call tables.  */
6200 static GTY(()) unsigned int poc_label_num;
6201
6202 /* The direct call table structure.  */
6203
6204 typedef struct GTY(()) dcall_struct {
6205   unsigned int poc_label_num;
6206   tree poc_decl;
6207   dw_die_ref targ_die;
6208 }
6209 dcall_entry;
6210
6211 DEF_VEC_O(dcall_entry);
6212 DEF_VEC_ALLOC_O(dcall_entry, gc);
6213
6214 /* The virtual call table structure.  */
6215
6216 typedef struct GTY(()) vcall_struct {
6217   unsigned int poc_label_num;
6218   unsigned int vtable_slot;
6219 }
6220 vcall_entry;
6221
6222 DEF_VEC_O(vcall_entry);
6223 DEF_VEC_ALLOC_O(vcall_entry, gc);
6224
6225 /* Pointers to the direct and virtual call tables.  */
6226 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6227 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6228
6229 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6230
6231 struct GTY (()) vcall_insn {
6232   int insn_uid;
6233   unsigned int vtable_slot;
6234 };
6235
6236 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6237
6238 /* Record whether the function being analyzed contains inlined functions.  */
6239 static int current_function_has_inlines;
6240
6241 /* The last file entry emitted by maybe_emit_file().  */
6242 static GTY(()) struct dwarf_file_data * last_emitted_file;
6243
6244 /* Number of internal labels generated by gen_internal_sym().  */
6245 static GTY(()) int label_num;
6246
6247 /* Cached result of previous call to lookup_filename.  */
6248 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6249
6250 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6251
6252 /* Instances of generic types for which we need to generate debug
6253    info that describe their generic parameters and arguments. That
6254    generation needs to happen once all types are properly laid out so
6255    we do it at the end of compilation.  */
6256 static GTY(()) VEC(tree,gc) *generic_type_instances;
6257
6258 /* Offset from the "steady-state frame pointer" to the frame base,
6259    within the current function.  */
6260 static HOST_WIDE_INT frame_pointer_fb_offset;
6261 static bool frame_pointer_fb_offset_valid;
6262
6263 /* Forward declarations for functions defined in this file.  */
6264
6265 static int is_pseudo_reg (const_rtx);
6266 static tree type_main_variant (tree);
6267 static int is_tagged_type (const_tree);
6268 static const char *dwarf_tag_name (unsigned);
6269 static const char *dwarf_attr_name (unsigned);
6270 static const char *dwarf_form_name (unsigned);
6271 static tree decl_ultimate_origin (const_tree);
6272 static tree decl_class_context (tree);
6273 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6274 static inline enum dw_val_class AT_class (dw_attr_ref);
6275 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6276 static inline unsigned AT_flag (dw_attr_ref);
6277 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6278 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6279 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6280 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6281 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6282                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6283 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6284                                unsigned int, unsigned char *);
6285 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6286 static hashval_t debug_str_do_hash (const void *);
6287 static int debug_str_eq (const void *, const void *);
6288 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6289 static inline const char *AT_string (dw_attr_ref);
6290 static enum dwarf_form AT_string_form (dw_attr_ref);
6291 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6292 static void add_AT_specification (dw_die_ref, dw_die_ref);
6293 static inline dw_die_ref AT_ref (dw_attr_ref);
6294 static inline int AT_ref_external (dw_attr_ref);
6295 static inline void set_AT_ref_external (dw_attr_ref, int);
6296 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6297 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6298 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6299 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6300                              dw_loc_list_ref);
6301 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6302 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6303 static inline rtx AT_addr (dw_attr_ref);
6304 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6305 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6306 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6307 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6308                            unsigned HOST_WIDE_INT);
6309 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6310                                unsigned long);
6311 static inline const char *AT_lbl (dw_attr_ref);
6312 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6313 static const char *get_AT_low_pc (dw_die_ref);
6314 static const char *get_AT_hi_pc (dw_die_ref);
6315 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6316 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6317 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6318 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6319 static bool is_cxx (void);
6320 static bool is_fortran (void);
6321 static bool is_ada (void);
6322 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6323 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6324 static void add_child_die (dw_die_ref, dw_die_ref);
6325 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6326 static dw_die_ref lookup_type_die (tree);
6327 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6328 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6329 static void equate_type_number_to_die (tree, dw_die_ref);
6330 static hashval_t decl_die_table_hash (const void *);
6331 static int decl_die_table_eq (const void *, const void *);
6332 static dw_die_ref lookup_decl_die (tree);
6333 static hashval_t common_block_die_table_hash (const void *);
6334 static int common_block_die_table_eq (const void *, const void *);
6335 static hashval_t decl_loc_table_hash (const void *);
6336 static int decl_loc_table_eq (const void *, const void *);
6337 static var_loc_list *lookup_decl_loc (const_tree);
6338 static void equate_decl_number_to_die (tree, dw_die_ref);
6339 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6340 static void print_spaces (FILE *);
6341 static void print_die (dw_die_ref, FILE *);
6342 static void print_dwarf_line_table (FILE *);
6343 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6344 static dw_die_ref pop_compile_unit (dw_die_ref);
6345 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6346 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6347 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6348 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6349 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6350 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6351 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6352                                    struct md5_ctx *, int *);
6353 struct checksum_attributes;
6354 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6355 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6356 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6357 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6358 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6359 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6360 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6361 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6362 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6363 static void compute_section_prefix (dw_die_ref);
6364 static int is_type_die (dw_die_ref);
6365 static int is_comdat_die (dw_die_ref);
6366 static int is_symbol_die (dw_die_ref);
6367 static void assign_symbol_names (dw_die_ref);
6368 static void break_out_includes (dw_die_ref);
6369 static int is_declaration_die (dw_die_ref);
6370 static int should_move_die_to_comdat (dw_die_ref);
6371 static dw_die_ref clone_as_declaration (dw_die_ref);
6372 static dw_die_ref clone_die (dw_die_ref);
6373 static dw_die_ref clone_tree (dw_die_ref);
6374 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6375 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6376 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6377 static dw_die_ref generate_skeleton (dw_die_ref);
6378 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6379                                                          dw_die_ref);
6380 static void break_out_comdat_types (dw_die_ref);
6381 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6382 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6383 static void copy_decls_for_unworthy_types (dw_die_ref);
6384
6385 static hashval_t htab_cu_hash (const void *);
6386 static int htab_cu_eq (const void *, const void *);
6387 static void htab_cu_del (void *);
6388 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6389 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6390 static void add_sibling_attributes (dw_die_ref);
6391 static void build_abbrev_table (dw_die_ref);
6392 static void output_location_lists (dw_die_ref);
6393 static int constant_size (unsigned HOST_WIDE_INT);
6394 static unsigned long size_of_die (dw_die_ref);
6395 static void calc_die_sizes (dw_die_ref);
6396 static void mark_dies (dw_die_ref);
6397 static void unmark_dies (dw_die_ref);
6398 static void unmark_all_dies (dw_die_ref);
6399 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6400 static unsigned long size_of_aranges (void);
6401 static enum dwarf_form value_format (dw_attr_ref);
6402 static void output_value_format (dw_attr_ref);
6403 static void output_abbrev_section (void);
6404 static void output_die_symbol (dw_die_ref);
6405 static void output_die (dw_die_ref);
6406 static void output_compilation_unit_header (void);
6407 static void output_comp_unit (dw_die_ref, int);
6408 static void output_comdat_type_unit (comdat_type_node *);
6409 static const char *dwarf2_name (tree, int);
6410 static void add_pubname (tree, dw_die_ref);
6411 static void add_pubname_string (const char *, dw_die_ref);
6412 static void add_pubtype (tree, dw_die_ref);
6413 static void output_pubnames (VEC (pubname_entry,gc) *);
6414 static void output_aranges (unsigned long);
6415 static unsigned int add_ranges_num (int);
6416 static unsigned int add_ranges (const_tree);
6417 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6418                                   bool *);
6419 static void output_ranges (void);
6420 static void output_line_info (void);
6421 static void output_file_names (void);
6422 static dw_die_ref base_type_die (tree);
6423 static int is_base_type (tree);
6424 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6425 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6426 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6427 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6428 static int type_is_enum (const_tree);
6429 static unsigned int dbx_reg_number (const_rtx);
6430 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6431 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6432 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6433                                                 enum var_init_status);
6434 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6435                                                      enum var_init_status);
6436 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6437                                          enum var_init_status);
6438 static int is_based_loc (const_rtx);
6439 static int resolve_one_addr (rtx *, void *);
6440 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6441                                                enum var_init_status);
6442 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6443                                         enum var_init_status);
6444 static dw_loc_list_ref loc_list_from_tree (tree, int);
6445 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6446 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6447 static tree field_type (const_tree);
6448 static unsigned int simple_type_align_in_bits (const_tree);
6449 static unsigned int simple_decl_align_in_bits (const_tree);
6450 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6451 static HOST_WIDE_INT field_byte_offset (const_tree);
6452 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6453                                          dw_loc_list_ref);
6454 static void add_data_member_location_attribute (dw_die_ref, tree);
6455 static bool add_const_value_attribute (dw_die_ref, rtx);
6456 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6457 static void insert_double (double_int, unsigned char *);
6458 static void insert_float (const_rtx, unsigned char *);
6459 static rtx rtl_for_decl_location (tree);
6460 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6461                                                    enum dwarf_attribute);
6462 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6463 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6464 static void add_name_attribute (dw_die_ref, const char *);
6465 static void add_comp_dir_attribute (dw_die_ref);
6466 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6467 static void add_subscript_info (dw_die_ref, tree, bool);
6468 static void add_byte_size_attribute (dw_die_ref, tree);
6469 static void add_bit_offset_attribute (dw_die_ref, tree);
6470 static void add_bit_size_attribute (dw_die_ref, tree);
6471 static void add_prototyped_attribute (dw_die_ref, tree);
6472 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6473 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6474 static void add_src_coords_attributes (dw_die_ref, tree);
6475 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6476 static void push_decl_scope (tree);
6477 static void pop_decl_scope (void);
6478 static dw_die_ref scope_die_for (tree, dw_die_ref);
6479 static inline int local_scope_p (dw_die_ref);
6480 static inline int class_scope_p (dw_die_ref);
6481 static inline int class_or_namespace_scope_p (dw_die_ref);
6482 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6483 static void add_calling_convention_attribute (dw_die_ref, tree);
6484 static const char *type_tag (const_tree);
6485 static tree member_declared_type (const_tree);
6486 #if 0
6487 static const char *decl_start_label (tree);
6488 #endif
6489 static void gen_array_type_die (tree, dw_die_ref);
6490 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6491 #if 0
6492 static void gen_entry_point_die (tree, dw_die_ref);
6493 #endif
6494 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6495 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6496 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6497 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6498 static void gen_formal_types_die (tree, dw_die_ref);
6499 static void gen_subprogram_die (tree, dw_die_ref);
6500 static void gen_variable_die (tree, tree, dw_die_ref);
6501 static void gen_const_die (tree, dw_die_ref);
6502 static void gen_label_die (tree, dw_die_ref);
6503 static void gen_lexical_block_die (tree, dw_die_ref, int);
6504 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6505 static void gen_field_die (tree, dw_die_ref);
6506 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6507 static dw_die_ref gen_compile_unit_die (const char *);
6508 static void gen_inheritance_die (tree, tree, dw_die_ref);
6509 static void gen_member_die (tree, dw_die_ref);
6510 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6511                                                 enum debug_info_usage);
6512 static void gen_subroutine_type_die (tree, dw_die_ref);
6513 static void gen_typedef_die (tree, dw_die_ref);
6514 static void gen_type_die (tree, dw_die_ref);
6515 static void gen_block_die (tree, dw_die_ref, int);
6516 static void decls_for_scope (tree, dw_die_ref, int);
6517 static inline int is_redundant_typedef (const_tree);
6518 static bool is_naming_typedef_decl (const_tree);
6519 static inline dw_die_ref get_context_die (tree);
6520 static void gen_namespace_die (tree, dw_die_ref);
6521 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6522 static dw_die_ref force_decl_die (tree);
6523 static dw_die_ref force_type_die (tree);
6524 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6525 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6526 static struct dwarf_file_data * lookup_filename (const char *);
6527 static void retry_incomplete_types (void);
6528 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6529 static void gen_generic_params_dies (tree);
6530 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6531 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6532 static void splice_child_die (dw_die_ref, dw_die_ref);
6533 static int file_info_cmp (const void *, const void *);
6534 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6535                                      const char *, const char *);
6536 static void output_loc_list (dw_loc_list_ref);
6537 static char *gen_internal_sym (const char *);
6538
6539 static void prune_unmark_dies (dw_die_ref);
6540 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6541 static void prune_unused_types_mark (dw_die_ref, int);
6542 static void prune_unused_types_walk (dw_die_ref);
6543 static void prune_unused_types_walk_attribs (dw_die_ref);
6544 static void prune_unused_types_prune (dw_die_ref);
6545 static void prune_unused_types (void);
6546 static int maybe_emit_file (struct dwarf_file_data *fd);
6547 static inline const char *AT_vms_delta1 (dw_attr_ref);
6548 static inline const char *AT_vms_delta2 (dw_attr_ref);
6549 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6550                                      const char *, const char *);
6551 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6552 static void gen_remaining_tmpl_value_param_die_attribute (void);
6553 static bool generic_type_p (tree);
6554 static void schedule_generic_params_dies_gen (tree t);
6555 static void gen_scheduled_generic_parms_dies (void);
6556
6557 /* Section names used to hold DWARF debugging information.  */
6558 #ifndef DEBUG_INFO_SECTION
6559 #define DEBUG_INFO_SECTION      ".debug_info"
6560 #endif
6561 #ifndef DEBUG_ABBREV_SECTION
6562 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6563 #endif
6564 #ifndef DEBUG_ARANGES_SECTION
6565 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6566 #endif
6567 #ifndef DEBUG_MACINFO_SECTION
6568 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6569 #endif
6570 #ifndef DEBUG_LINE_SECTION
6571 #define DEBUG_LINE_SECTION      ".debug_line"
6572 #endif
6573 #ifndef DEBUG_LOC_SECTION
6574 #define DEBUG_LOC_SECTION       ".debug_loc"
6575 #endif
6576 #ifndef DEBUG_PUBNAMES_SECTION
6577 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6578 #endif
6579 #ifndef DEBUG_PUBTYPES_SECTION
6580 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6581 #endif
6582 #ifndef DEBUG_DCALL_SECTION
6583 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6584 #endif
6585 #ifndef DEBUG_VCALL_SECTION
6586 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6587 #endif
6588 #ifndef DEBUG_STR_SECTION
6589 #define DEBUG_STR_SECTION       ".debug_str"
6590 #endif
6591 #ifndef DEBUG_RANGES_SECTION
6592 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6593 #endif
6594
6595 /* Standard ELF section names for compiled code and data.  */
6596 #ifndef TEXT_SECTION_NAME
6597 #define TEXT_SECTION_NAME       ".text"
6598 #endif
6599
6600 /* Section flags for .debug_str section.  */
6601 #define DEBUG_STR_SECTION_FLAGS \
6602   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6603    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6604    : SECTION_DEBUG)
6605
6606 /* Labels we insert at beginning sections we can reference instead of
6607    the section names themselves.  */
6608
6609 #ifndef TEXT_SECTION_LABEL
6610 #define TEXT_SECTION_LABEL              "Ltext"
6611 #endif
6612 #ifndef COLD_TEXT_SECTION_LABEL
6613 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6614 #endif
6615 #ifndef DEBUG_LINE_SECTION_LABEL
6616 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6617 #endif
6618 #ifndef DEBUG_INFO_SECTION_LABEL
6619 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6620 #endif
6621 #ifndef DEBUG_ABBREV_SECTION_LABEL
6622 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6623 #endif
6624 #ifndef DEBUG_LOC_SECTION_LABEL
6625 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6626 #endif
6627 #ifndef DEBUG_RANGES_SECTION_LABEL
6628 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6629 #endif
6630 #ifndef DEBUG_MACINFO_SECTION_LABEL
6631 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6632 #endif
6633
6634
6635 /* Definitions of defaults for formats and names of various special
6636    (artificial) labels which may be generated within this file (when the -g
6637    options is used and DWARF2_DEBUGGING_INFO is in effect.
6638    If necessary, these may be overridden from within the tm.h file, but
6639    typically, overriding these defaults is unnecessary.  */
6640
6641 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6642 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6643 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6644 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6645 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6646 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6647 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6648 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6649 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6650 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6651
6652 #ifndef TEXT_END_LABEL
6653 #define TEXT_END_LABEL          "Letext"
6654 #endif
6655 #ifndef COLD_END_LABEL
6656 #define COLD_END_LABEL          "Letext_cold"
6657 #endif
6658 #ifndef BLOCK_BEGIN_LABEL
6659 #define BLOCK_BEGIN_LABEL       "LBB"
6660 #endif
6661 #ifndef BLOCK_END_LABEL
6662 #define BLOCK_END_LABEL         "LBE"
6663 #endif
6664 #ifndef LINE_CODE_LABEL
6665 #define LINE_CODE_LABEL         "LM"
6666 #endif
6667 #ifndef SEPARATE_LINE_CODE_LABEL
6668 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6669 #endif
6670
6671 \f
6672 /* Return the root of the DIE's built for the current compilation unit.  */
6673 static dw_die_ref
6674 comp_unit_die (void)
6675 {
6676   if (!single_comp_unit_die)
6677     single_comp_unit_die = gen_compile_unit_die (NULL);
6678   return single_comp_unit_die;
6679 }
6680
6681 /* We allow a language front-end to designate a function that is to be
6682    called to "demangle" any name before it is put into a DIE.  */
6683
6684 static const char *(*demangle_name_func) (const char *);
6685
6686 void
6687 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6688 {
6689   demangle_name_func = func;
6690 }
6691
6692 /* Test if rtl node points to a pseudo register.  */
6693
6694 static inline int
6695 is_pseudo_reg (const_rtx rtl)
6696 {
6697   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6698           || (GET_CODE (rtl) == SUBREG
6699               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6700 }
6701
6702 /* Return a reference to a type, with its const and volatile qualifiers
6703    removed.  */
6704
6705 static inline tree
6706 type_main_variant (tree type)
6707 {
6708   type = TYPE_MAIN_VARIANT (type);
6709
6710   /* ??? There really should be only one main variant among any group of
6711      variants of a given type (and all of the MAIN_VARIANT values for all
6712      members of the group should point to that one type) but sometimes the C
6713      front-end messes this up for array types, so we work around that bug
6714      here.  */
6715   if (TREE_CODE (type) == ARRAY_TYPE)
6716     while (type != TYPE_MAIN_VARIANT (type))
6717       type = TYPE_MAIN_VARIANT (type);
6718
6719   return type;
6720 }
6721
6722 /* Return nonzero if the given type node represents a tagged type.  */
6723
6724 static inline int
6725 is_tagged_type (const_tree type)
6726 {
6727   enum tree_code code = TREE_CODE (type);
6728
6729   return (code == RECORD_TYPE || code == UNION_TYPE
6730           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6731 }
6732
6733 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6734
6735 static void
6736 get_ref_die_offset_label (char *label, dw_die_ref ref)
6737 {
6738   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6739 }
6740
6741 /* Convert a DIE tag into its string name.  */
6742
6743 static const char *
6744 dwarf_tag_name (unsigned int tag)
6745 {
6746   switch (tag)
6747     {
6748     case DW_TAG_padding:
6749       return "DW_TAG_padding";
6750     case DW_TAG_array_type:
6751       return "DW_TAG_array_type";
6752     case DW_TAG_class_type:
6753       return "DW_TAG_class_type";
6754     case DW_TAG_entry_point:
6755       return "DW_TAG_entry_point";
6756     case DW_TAG_enumeration_type:
6757       return "DW_TAG_enumeration_type";
6758     case DW_TAG_formal_parameter:
6759       return "DW_TAG_formal_parameter";
6760     case DW_TAG_imported_declaration:
6761       return "DW_TAG_imported_declaration";
6762     case DW_TAG_label:
6763       return "DW_TAG_label";
6764     case DW_TAG_lexical_block:
6765       return "DW_TAG_lexical_block";
6766     case DW_TAG_member:
6767       return "DW_TAG_member";
6768     case DW_TAG_pointer_type:
6769       return "DW_TAG_pointer_type";
6770     case DW_TAG_reference_type:
6771       return "DW_TAG_reference_type";
6772     case DW_TAG_compile_unit:
6773       return "DW_TAG_compile_unit";
6774     case DW_TAG_string_type:
6775       return "DW_TAG_string_type";
6776     case DW_TAG_structure_type:
6777       return "DW_TAG_structure_type";
6778     case DW_TAG_subroutine_type:
6779       return "DW_TAG_subroutine_type";
6780     case DW_TAG_typedef:
6781       return "DW_TAG_typedef";
6782     case DW_TAG_union_type:
6783       return "DW_TAG_union_type";
6784     case DW_TAG_unspecified_parameters:
6785       return "DW_TAG_unspecified_parameters";
6786     case DW_TAG_variant:
6787       return "DW_TAG_variant";
6788     case DW_TAG_common_block:
6789       return "DW_TAG_common_block";
6790     case DW_TAG_common_inclusion:
6791       return "DW_TAG_common_inclusion";
6792     case DW_TAG_inheritance:
6793       return "DW_TAG_inheritance";
6794     case DW_TAG_inlined_subroutine:
6795       return "DW_TAG_inlined_subroutine";
6796     case DW_TAG_module:
6797       return "DW_TAG_module";
6798     case DW_TAG_ptr_to_member_type:
6799       return "DW_TAG_ptr_to_member_type";
6800     case DW_TAG_set_type:
6801       return "DW_TAG_set_type";
6802     case DW_TAG_subrange_type:
6803       return "DW_TAG_subrange_type";
6804     case DW_TAG_with_stmt:
6805       return "DW_TAG_with_stmt";
6806     case DW_TAG_access_declaration:
6807       return "DW_TAG_access_declaration";
6808     case DW_TAG_base_type:
6809       return "DW_TAG_base_type";
6810     case DW_TAG_catch_block:
6811       return "DW_TAG_catch_block";
6812     case DW_TAG_const_type:
6813       return "DW_TAG_const_type";
6814     case DW_TAG_constant:
6815       return "DW_TAG_constant";
6816     case DW_TAG_enumerator:
6817       return "DW_TAG_enumerator";
6818     case DW_TAG_file_type:
6819       return "DW_TAG_file_type";
6820     case DW_TAG_friend:
6821       return "DW_TAG_friend";
6822     case DW_TAG_namelist:
6823       return "DW_TAG_namelist";
6824     case DW_TAG_namelist_item:
6825       return "DW_TAG_namelist_item";
6826     case DW_TAG_packed_type:
6827       return "DW_TAG_packed_type";
6828     case DW_TAG_subprogram:
6829       return "DW_TAG_subprogram";
6830     case DW_TAG_template_type_param:
6831       return "DW_TAG_template_type_param";
6832     case DW_TAG_template_value_param:
6833       return "DW_TAG_template_value_param";
6834     case DW_TAG_thrown_type:
6835       return "DW_TAG_thrown_type";
6836     case DW_TAG_try_block:
6837       return "DW_TAG_try_block";
6838     case DW_TAG_variant_part:
6839       return "DW_TAG_variant_part";
6840     case DW_TAG_variable:
6841       return "DW_TAG_variable";
6842     case DW_TAG_volatile_type:
6843       return "DW_TAG_volatile_type";
6844     case DW_TAG_dwarf_procedure:
6845       return "DW_TAG_dwarf_procedure";
6846     case DW_TAG_restrict_type:
6847       return "DW_TAG_restrict_type";
6848     case DW_TAG_interface_type:
6849       return "DW_TAG_interface_type";
6850     case DW_TAG_namespace:
6851       return "DW_TAG_namespace";
6852     case DW_TAG_imported_module:
6853       return "DW_TAG_imported_module";
6854     case DW_TAG_unspecified_type:
6855       return "DW_TAG_unspecified_type";
6856     case DW_TAG_partial_unit:
6857       return "DW_TAG_partial_unit";
6858     case DW_TAG_imported_unit:
6859       return "DW_TAG_imported_unit";
6860     case DW_TAG_condition:
6861       return "DW_TAG_condition";
6862     case DW_TAG_shared_type:
6863       return "DW_TAG_shared_type";
6864     case DW_TAG_type_unit:
6865       return "DW_TAG_type_unit";
6866     case DW_TAG_rvalue_reference_type:
6867       return "DW_TAG_rvalue_reference_type";
6868     case DW_TAG_template_alias:
6869       return "DW_TAG_template_alias";
6870     case DW_TAG_GNU_template_parameter_pack:
6871       return "DW_TAG_GNU_template_parameter_pack";
6872     case DW_TAG_GNU_formal_parameter_pack:
6873       return "DW_TAG_GNU_formal_parameter_pack";
6874     case DW_TAG_MIPS_loop:
6875       return "DW_TAG_MIPS_loop";
6876     case DW_TAG_format_label:
6877       return "DW_TAG_format_label";
6878     case DW_TAG_function_template:
6879       return "DW_TAG_function_template";
6880     case DW_TAG_class_template:
6881       return "DW_TAG_class_template";
6882     case DW_TAG_GNU_BINCL:
6883       return "DW_TAG_GNU_BINCL";
6884     case DW_TAG_GNU_EINCL:
6885       return "DW_TAG_GNU_EINCL";
6886     case DW_TAG_GNU_template_template_param:
6887       return "DW_TAG_GNU_template_template_param";
6888     default:
6889       return "DW_TAG_<unknown>";
6890     }
6891 }
6892
6893 /* Convert a DWARF attribute code into its string name.  */
6894
6895 static const char *
6896 dwarf_attr_name (unsigned int attr)
6897 {
6898   switch (attr)
6899     {
6900     case DW_AT_sibling:
6901       return "DW_AT_sibling";
6902     case DW_AT_location:
6903       return "DW_AT_location";
6904     case DW_AT_name:
6905       return "DW_AT_name";
6906     case DW_AT_ordering:
6907       return "DW_AT_ordering";
6908     case DW_AT_subscr_data:
6909       return "DW_AT_subscr_data";
6910     case DW_AT_byte_size:
6911       return "DW_AT_byte_size";
6912     case DW_AT_bit_offset:
6913       return "DW_AT_bit_offset";
6914     case DW_AT_bit_size:
6915       return "DW_AT_bit_size";
6916     case DW_AT_element_list:
6917       return "DW_AT_element_list";
6918     case DW_AT_stmt_list:
6919       return "DW_AT_stmt_list";
6920     case DW_AT_low_pc:
6921       return "DW_AT_low_pc";
6922     case DW_AT_high_pc:
6923       return "DW_AT_high_pc";
6924     case DW_AT_language:
6925       return "DW_AT_language";
6926     case DW_AT_member:
6927       return "DW_AT_member";
6928     case DW_AT_discr:
6929       return "DW_AT_discr";
6930     case DW_AT_discr_value:
6931       return "DW_AT_discr_value";
6932     case DW_AT_visibility:
6933       return "DW_AT_visibility";
6934     case DW_AT_import:
6935       return "DW_AT_import";
6936     case DW_AT_string_length:
6937       return "DW_AT_string_length";
6938     case DW_AT_common_reference:
6939       return "DW_AT_common_reference";
6940     case DW_AT_comp_dir:
6941       return "DW_AT_comp_dir";
6942     case DW_AT_const_value:
6943       return "DW_AT_const_value";
6944     case DW_AT_containing_type:
6945       return "DW_AT_containing_type";
6946     case DW_AT_default_value:
6947       return "DW_AT_default_value";
6948     case DW_AT_inline:
6949       return "DW_AT_inline";
6950     case DW_AT_is_optional:
6951       return "DW_AT_is_optional";
6952     case DW_AT_lower_bound:
6953       return "DW_AT_lower_bound";
6954     case DW_AT_producer:
6955       return "DW_AT_producer";
6956     case DW_AT_prototyped:
6957       return "DW_AT_prototyped";
6958     case DW_AT_return_addr:
6959       return "DW_AT_return_addr";
6960     case DW_AT_start_scope:
6961       return "DW_AT_start_scope";
6962     case DW_AT_bit_stride:
6963       return "DW_AT_bit_stride";
6964     case DW_AT_upper_bound:
6965       return "DW_AT_upper_bound";
6966     case DW_AT_abstract_origin:
6967       return "DW_AT_abstract_origin";
6968     case DW_AT_accessibility:
6969       return "DW_AT_accessibility";
6970     case DW_AT_address_class:
6971       return "DW_AT_address_class";
6972     case DW_AT_artificial:
6973       return "DW_AT_artificial";
6974     case DW_AT_base_types:
6975       return "DW_AT_base_types";
6976     case DW_AT_calling_convention:
6977       return "DW_AT_calling_convention";
6978     case DW_AT_count:
6979       return "DW_AT_count";
6980     case DW_AT_data_member_location:
6981       return "DW_AT_data_member_location";
6982     case DW_AT_decl_column:
6983       return "DW_AT_decl_column";
6984     case DW_AT_decl_file:
6985       return "DW_AT_decl_file";
6986     case DW_AT_decl_line:
6987       return "DW_AT_decl_line";
6988     case DW_AT_declaration:
6989       return "DW_AT_declaration";
6990     case DW_AT_discr_list:
6991       return "DW_AT_discr_list";
6992     case DW_AT_encoding:
6993       return "DW_AT_encoding";
6994     case DW_AT_external:
6995       return "DW_AT_external";
6996     case DW_AT_explicit:
6997       return "DW_AT_explicit";
6998     case DW_AT_frame_base:
6999       return "DW_AT_frame_base";
7000     case DW_AT_friend:
7001       return "DW_AT_friend";
7002     case DW_AT_identifier_case:
7003       return "DW_AT_identifier_case";
7004     case DW_AT_macro_info:
7005       return "DW_AT_macro_info";
7006     case DW_AT_namelist_items:
7007       return "DW_AT_namelist_items";
7008     case DW_AT_priority:
7009       return "DW_AT_priority";
7010     case DW_AT_segment:
7011       return "DW_AT_segment";
7012     case DW_AT_specification:
7013       return "DW_AT_specification";
7014     case DW_AT_static_link:
7015       return "DW_AT_static_link";
7016     case DW_AT_type:
7017       return "DW_AT_type";
7018     case DW_AT_use_location:
7019       return "DW_AT_use_location";
7020     case DW_AT_variable_parameter:
7021       return "DW_AT_variable_parameter";
7022     case DW_AT_virtuality:
7023       return "DW_AT_virtuality";
7024     case DW_AT_vtable_elem_location:
7025       return "DW_AT_vtable_elem_location";
7026
7027     case DW_AT_allocated:
7028       return "DW_AT_allocated";
7029     case DW_AT_associated:
7030       return "DW_AT_associated";
7031     case DW_AT_data_location:
7032       return "DW_AT_data_location";
7033     case DW_AT_byte_stride:
7034       return "DW_AT_byte_stride";
7035     case DW_AT_entry_pc:
7036       return "DW_AT_entry_pc";
7037     case DW_AT_use_UTF8:
7038       return "DW_AT_use_UTF8";
7039     case DW_AT_extension:
7040       return "DW_AT_extension";
7041     case DW_AT_ranges:
7042       return "DW_AT_ranges";
7043     case DW_AT_trampoline:
7044       return "DW_AT_trampoline";
7045     case DW_AT_call_column:
7046       return "DW_AT_call_column";
7047     case DW_AT_call_file:
7048       return "DW_AT_call_file";
7049     case DW_AT_call_line:
7050       return "DW_AT_call_line";
7051     case DW_AT_object_pointer:
7052       return "DW_AT_object_pointer";
7053
7054     case DW_AT_signature:
7055       return "DW_AT_signature";
7056     case DW_AT_main_subprogram:
7057       return "DW_AT_main_subprogram";
7058     case DW_AT_data_bit_offset:
7059       return "DW_AT_data_bit_offset";
7060     case DW_AT_const_expr:
7061       return "DW_AT_const_expr";
7062     case DW_AT_enum_class:
7063       return "DW_AT_enum_class";
7064     case DW_AT_linkage_name:
7065       return "DW_AT_linkage_name";
7066
7067     case DW_AT_MIPS_fde:
7068       return "DW_AT_MIPS_fde";
7069     case DW_AT_MIPS_loop_begin:
7070       return "DW_AT_MIPS_loop_begin";
7071     case DW_AT_MIPS_tail_loop_begin:
7072       return "DW_AT_MIPS_tail_loop_begin";
7073     case DW_AT_MIPS_epilog_begin:
7074       return "DW_AT_MIPS_epilog_begin";
7075 #if VMS_DEBUGGING_INFO
7076     case DW_AT_HP_prologue:
7077       return "DW_AT_HP_prologue";
7078 #else
7079     case DW_AT_MIPS_loop_unroll_factor:
7080       return "DW_AT_MIPS_loop_unroll_factor";
7081 #endif
7082     case DW_AT_MIPS_software_pipeline_depth:
7083       return "DW_AT_MIPS_software_pipeline_depth";
7084     case DW_AT_MIPS_linkage_name:
7085       return "DW_AT_MIPS_linkage_name";
7086 #if VMS_DEBUGGING_INFO
7087     case DW_AT_HP_epilogue:
7088       return "DW_AT_HP_epilogue";
7089 #else
7090     case DW_AT_MIPS_stride:
7091       return "DW_AT_MIPS_stride";
7092 #endif
7093     case DW_AT_MIPS_abstract_name:
7094       return "DW_AT_MIPS_abstract_name";
7095     case DW_AT_MIPS_clone_origin:
7096       return "DW_AT_MIPS_clone_origin";
7097     case DW_AT_MIPS_has_inlines:
7098       return "DW_AT_MIPS_has_inlines";
7099
7100     case DW_AT_sf_names:
7101       return "DW_AT_sf_names";
7102     case DW_AT_src_info:
7103       return "DW_AT_src_info";
7104     case DW_AT_mac_info:
7105       return "DW_AT_mac_info";
7106     case DW_AT_src_coords:
7107       return "DW_AT_src_coords";
7108     case DW_AT_body_begin:
7109       return "DW_AT_body_begin";
7110     case DW_AT_body_end:
7111       return "DW_AT_body_end";
7112     case DW_AT_GNU_vector:
7113       return "DW_AT_GNU_vector";
7114     case DW_AT_GNU_guarded_by:
7115       return "DW_AT_GNU_guarded_by";
7116     case DW_AT_GNU_pt_guarded_by:
7117       return "DW_AT_GNU_pt_guarded_by";
7118     case DW_AT_GNU_guarded:
7119       return "DW_AT_GNU_guarded";
7120     case DW_AT_GNU_pt_guarded:
7121       return "DW_AT_GNU_pt_guarded";
7122     case DW_AT_GNU_locks_excluded:
7123       return "DW_AT_GNU_locks_excluded";
7124     case DW_AT_GNU_exclusive_locks_required:
7125       return "DW_AT_GNU_exclusive_locks_required";
7126     case DW_AT_GNU_shared_locks_required:
7127       return "DW_AT_GNU_shared_locks_required";
7128     case DW_AT_GNU_odr_signature:
7129       return "DW_AT_GNU_odr_signature";
7130     case DW_AT_GNU_template_name:
7131       return "DW_AT_GNU_template_name";
7132
7133     case DW_AT_VMS_rtnbeg_pd_address:
7134       return "DW_AT_VMS_rtnbeg_pd_address";
7135
7136     default:
7137       return "DW_AT_<unknown>";
7138     }
7139 }
7140
7141 /* Convert a DWARF value form code into its string name.  */
7142
7143 static const char *
7144 dwarf_form_name (unsigned int form)
7145 {
7146   switch (form)
7147     {
7148     case DW_FORM_addr:
7149       return "DW_FORM_addr";
7150     case DW_FORM_block2:
7151       return "DW_FORM_block2";
7152     case DW_FORM_block4:
7153       return "DW_FORM_block4";
7154     case DW_FORM_data2:
7155       return "DW_FORM_data2";
7156     case DW_FORM_data4:
7157       return "DW_FORM_data4";
7158     case DW_FORM_data8:
7159       return "DW_FORM_data8";
7160     case DW_FORM_string:
7161       return "DW_FORM_string";
7162     case DW_FORM_block:
7163       return "DW_FORM_block";
7164     case DW_FORM_block1:
7165       return "DW_FORM_block1";
7166     case DW_FORM_data1:
7167       return "DW_FORM_data1";
7168     case DW_FORM_flag:
7169       return "DW_FORM_flag";
7170     case DW_FORM_sdata:
7171       return "DW_FORM_sdata";
7172     case DW_FORM_strp:
7173       return "DW_FORM_strp";
7174     case DW_FORM_udata:
7175       return "DW_FORM_udata";
7176     case DW_FORM_ref_addr:
7177       return "DW_FORM_ref_addr";
7178     case DW_FORM_ref1:
7179       return "DW_FORM_ref1";
7180     case DW_FORM_ref2:
7181       return "DW_FORM_ref2";
7182     case DW_FORM_ref4:
7183       return "DW_FORM_ref4";
7184     case DW_FORM_ref8:
7185       return "DW_FORM_ref8";
7186     case DW_FORM_ref_udata:
7187       return "DW_FORM_ref_udata";
7188     case DW_FORM_indirect:
7189       return "DW_FORM_indirect";
7190     case DW_FORM_sec_offset:
7191       return "DW_FORM_sec_offset";
7192     case DW_FORM_exprloc:
7193       return "DW_FORM_exprloc";
7194     case DW_FORM_flag_present:
7195       return "DW_FORM_flag_present";
7196     case DW_FORM_ref_sig8:
7197       return "DW_FORM_ref_sig8";
7198     default:
7199       return "DW_FORM_<unknown>";
7200     }
7201 }
7202 \f
7203 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7204    instance of an inlined instance of a decl which is local to an inline
7205    function, so we have to trace all of the way back through the origin chain
7206    to find out what sort of node actually served as the original seed for the
7207    given block.  */
7208
7209 static tree
7210 decl_ultimate_origin (const_tree decl)
7211 {
7212   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7213     return NULL_TREE;
7214
7215   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7216      nodes in the function to point to themselves; ignore that if
7217      we're trying to output the abstract instance of this function.  */
7218   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7219     return NULL_TREE;
7220
7221   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7222      most distant ancestor, this should never happen.  */
7223   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7224
7225   return DECL_ABSTRACT_ORIGIN (decl);
7226 }
7227
7228 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7229    of a virtual function may refer to a base class, so we check the 'this'
7230    parameter.  */
7231
7232 static tree
7233 decl_class_context (tree decl)
7234 {
7235   tree context = NULL_TREE;
7236
7237   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7238     context = DECL_CONTEXT (decl);
7239   else
7240     context = TYPE_MAIN_VARIANT
7241       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7242
7243   if (context && !TYPE_P (context))
7244     context = NULL_TREE;
7245
7246   return context;
7247 }
7248 \f
7249 /* Add an attribute/value pair to a DIE.  */
7250
7251 static inline void
7252 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7253 {
7254   /* Maybe this should be an assert?  */
7255   if (die == NULL)
7256     return;
7257
7258   if (die->die_attr == NULL)
7259     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7260   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7261 }
7262
7263 static inline enum dw_val_class
7264 AT_class (dw_attr_ref a)
7265 {
7266   return a->dw_attr_val.val_class;
7267 }
7268
7269 /* Add a flag value attribute to a DIE.  */
7270
7271 static inline void
7272 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7273 {
7274   dw_attr_node attr;
7275
7276   attr.dw_attr = attr_kind;
7277   attr.dw_attr_val.val_class = dw_val_class_flag;
7278   attr.dw_attr_val.v.val_flag = flag;
7279   add_dwarf_attr (die, &attr);
7280 }
7281
7282 static inline unsigned
7283 AT_flag (dw_attr_ref a)
7284 {
7285   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7286   return a->dw_attr_val.v.val_flag;
7287 }
7288
7289 /* Add a signed integer attribute value to a DIE.  */
7290
7291 static inline void
7292 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7293 {
7294   dw_attr_node attr;
7295
7296   attr.dw_attr = attr_kind;
7297   attr.dw_attr_val.val_class = dw_val_class_const;
7298   attr.dw_attr_val.v.val_int = int_val;
7299   add_dwarf_attr (die, &attr);
7300 }
7301
7302 static inline HOST_WIDE_INT
7303 AT_int (dw_attr_ref a)
7304 {
7305   gcc_assert (a && AT_class (a) == dw_val_class_const);
7306   return a->dw_attr_val.v.val_int;
7307 }
7308
7309 /* Add an unsigned integer attribute value to a DIE.  */
7310
7311 static inline void
7312 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7313                  unsigned HOST_WIDE_INT unsigned_val)
7314 {
7315   dw_attr_node attr;
7316
7317   attr.dw_attr = attr_kind;
7318   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7319   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7320   add_dwarf_attr (die, &attr);
7321 }
7322
7323 static inline unsigned HOST_WIDE_INT
7324 AT_unsigned (dw_attr_ref a)
7325 {
7326   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7327   return a->dw_attr_val.v.val_unsigned;
7328 }
7329
7330 /* Add an unsigned double integer attribute value to a DIE.  */
7331
7332 static inline void
7333 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7334                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7335 {
7336   dw_attr_node attr;
7337
7338   attr.dw_attr = attr_kind;
7339   attr.dw_attr_val.val_class = dw_val_class_const_double;
7340   attr.dw_attr_val.v.val_double.high = high;
7341   attr.dw_attr_val.v.val_double.low = low;
7342   add_dwarf_attr (die, &attr);
7343 }
7344
7345 /* Add a floating point attribute value to a DIE and return it.  */
7346
7347 static inline void
7348 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7349             unsigned int length, unsigned int elt_size, unsigned char *array)
7350 {
7351   dw_attr_node attr;
7352
7353   attr.dw_attr = attr_kind;
7354   attr.dw_attr_val.val_class = dw_val_class_vec;
7355   attr.dw_attr_val.v.val_vec.length = length;
7356   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7357   attr.dw_attr_val.v.val_vec.array = array;
7358   add_dwarf_attr (die, &attr);
7359 }
7360
7361 /* Add an 8-byte data attribute value to a DIE.  */
7362
7363 static inline void
7364 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7365               unsigned char data8[8])
7366 {
7367   dw_attr_node attr;
7368
7369   attr.dw_attr = attr_kind;
7370   attr.dw_attr_val.val_class = dw_val_class_data8;
7371   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7372   add_dwarf_attr (die, &attr);
7373 }
7374
7375 /* Hash and equality functions for debug_str_hash.  */
7376
7377 static hashval_t
7378 debug_str_do_hash (const void *x)
7379 {
7380   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7381 }
7382
7383 static int
7384 debug_str_eq (const void *x1, const void *x2)
7385 {
7386   return strcmp ((((const struct indirect_string_node *)x1)->str),
7387                  (const char *)x2) == 0;
7388 }
7389
7390 /* Add STR to the indirect string hash table.  */
7391
7392 static struct indirect_string_node *
7393 find_AT_string (const char *str)
7394 {
7395   struct indirect_string_node *node;
7396   void **slot;
7397
7398   if (! debug_str_hash)
7399     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7400                                       debug_str_eq, NULL);
7401
7402   slot = htab_find_slot_with_hash (debug_str_hash, str,
7403                                    htab_hash_string (str), INSERT);
7404   if (*slot == NULL)
7405     {
7406       node = ggc_alloc_cleared_indirect_string_node ();
7407       node->str = ggc_strdup (str);
7408       *slot = node;
7409     }
7410   else
7411     node = (struct indirect_string_node *) *slot;
7412
7413   node->refcount++;
7414   return node;
7415 }
7416
7417 /* Add a string attribute value to a DIE.  */
7418
7419 static inline void
7420 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7421 {
7422   dw_attr_node attr;
7423   struct indirect_string_node *node;
7424
7425   node = find_AT_string (str);
7426
7427   attr.dw_attr = attr_kind;
7428   attr.dw_attr_val.val_class = dw_val_class_str;
7429   attr.dw_attr_val.v.val_str = node;
7430   add_dwarf_attr (die, &attr);
7431 }
7432
7433 /* Create a label for an indirect string node, ensuring it is going to
7434    be output, unless its reference count goes down to zero.  */
7435
7436 static inline void
7437 gen_label_for_indirect_string (struct indirect_string_node *node)
7438 {
7439   char label[32];
7440
7441   if (node->label)
7442     return;
7443
7444   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7445   ++dw2_string_counter;
7446   node->label = xstrdup (label);
7447 }
7448
7449 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7450    debug string STR.  */
7451
7452 static inline rtx
7453 get_debug_string_label (const char *str)
7454 {
7455   struct indirect_string_node *node = find_AT_string (str);
7456
7457   debug_str_hash_forced = true;
7458
7459   gen_label_for_indirect_string (node);
7460
7461   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7462 }
7463
7464 static inline const char *
7465 AT_string (dw_attr_ref a)
7466 {
7467   gcc_assert (a && AT_class (a) == dw_val_class_str);
7468   return a->dw_attr_val.v.val_str->str;
7469 }
7470
7471 /* Find out whether a string should be output inline in DIE
7472    or out-of-line in .debug_str section.  */
7473
7474 static enum dwarf_form
7475 AT_string_form (dw_attr_ref a)
7476 {
7477   struct indirect_string_node *node;
7478   unsigned int len;
7479
7480   gcc_assert (a && AT_class (a) == dw_val_class_str);
7481
7482   node = a->dw_attr_val.v.val_str;
7483   if (node->form)
7484     return node->form;
7485
7486   len = strlen (node->str) + 1;
7487
7488   /* If the string is shorter or equal to the size of the reference, it is
7489      always better to put it inline.  */
7490   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7491     return node->form = DW_FORM_string;
7492
7493   /* If we cannot expect the linker to merge strings in .debug_str
7494      section, only put it into .debug_str if it is worth even in this
7495      single module.  */
7496   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7497       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7498       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7499     return node->form = DW_FORM_string;
7500
7501   gen_label_for_indirect_string (node);
7502
7503   return node->form = DW_FORM_strp;
7504 }
7505
7506 /* Add a DIE reference attribute value to a DIE.  */
7507
7508 static inline void
7509 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7510 {
7511   dw_attr_node attr;
7512
7513 #ifdef ENABLE_CHECKING
7514   gcc_assert (targ_die != NULL);
7515 #else
7516   /* With LTO we can end up trying to reference something we didn't create
7517      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7518   if (targ_die == NULL)
7519     return;
7520 #endif
7521
7522   attr.dw_attr = attr_kind;
7523   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7524   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7525   attr.dw_attr_val.v.val_die_ref.external = 0;
7526   add_dwarf_attr (die, &attr);
7527 }
7528
7529 /* Add an AT_specification attribute to a DIE, and also make the back
7530    pointer from the specification to the definition.  */
7531
7532 static inline void
7533 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7534 {
7535   add_AT_die_ref (die, DW_AT_specification, targ_die);
7536   gcc_assert (!targ_die->die_definition);
7537   targ_die->die_definition = die;
7538 }
7539
7540 static inline dw_die_ref
7541 AT_ref (dw_attr_ref a)
7542 {
7543   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7544   return a->dw_attr_val.v.val_die_ref.die;
7545 }
7546
7547 static inline int
7548 AT_ref_external (dw_attr_ref a)
7549 {
7550   if (a && AT_class (a) == dw_val_class_die_ref)
7551     return a->dw_attr_val.v.val_die_ref.external;
7552
7553   return 0;
7554 }
7555
7556 static inline void
7557 set_AT_ref_external (dw_attr_ref a, int i)
7558 {
7559   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7560   a->dw_attr_val.v.val_die_ref.external = i;
7561 }
7562
7563 /* Add an FDE reference attribute value to a DIE.  */
7564
7565 static inline void
7566 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7567 {
7568   dw_attr_node attr;
7569
7570   attr.dw_attr = attr_kind;
7571   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7572   attr.dw_attr_val.v.val_fde_index = targ_fde;
7573   add_dwarf_attr (die, &attr);
7574 }
7575
7576 /* Add a location description attribute value to a DIE.  */
7577
7578 static inline void
7579 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7580 {
7581   dw_attr_node attr;
7582
7583   attr.dw_attr = attr_kind;
7584   attr.dw_attr_val.val_class = dw_val_class_loc;
7585   attr.dw_attr_val.v.val_loc = loc;
7586   add_dwarf_attr (die, &attr);
7587 }
7588
7589 static inline dw_loc_descr_ref
7590 AT_loc (dw_attr_ref a)
7591 {
7592   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7593   return a->dw_attr_val.v.val_loc;
7594 }
7595
7596 static inline void
7597 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7598 {
7599   dw_attr_node attr;
7600
7601   attr.dw_attr = attr_kind;
7602   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7603   attr.dw_attr_val.v.val_loc_list = loc_list;
7604   add_dwarf_attr (die, &attr);
7605   have_location_lists = true;
7606 }
7607
7608 static inline dw_loc_list_ref
7609 AT_loc_list (dw_attr_ref a)
7610 {
7611   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7612   return a->dw_attr_val.v.val_loc_list;
7613 }
7614
7615 static inline dw_loc_list_ref *
7616 AT_loc_list_ptr (dw_attr_ref a)
7617 {
7618   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7619   return &a->dw_attr_val.v.val_loc_list;
7620 }
7621
7622 /* Add an address constant attribute value to a DIE.  */
7623
7624 static inline void
7625 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7626 {
7627   dw_attr_node attr;
7628
7629   attr.dw_attr = attr_kind;
7630   attr.dw_attr_val.val_class = dw_val_class_addr;
7631   attr.dw_attr_val.v.val_addr = addr;
7632   add_dwarf_attr (die, &attr);
7633 }
7634
7635 /* Get the RTX from to an address DIE attribute.  */
7636
7637 static inline rtx
7638 AT_addr (dw_attr_ref a)
7639 {
7640   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7641   return a->dw_attr_val.v.val_addr;
7642 }
7643
7644 /* Add a file attribute value to a DIE.  */
7645
7646 static inline void
7647 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7648              struct dwarf_file_data *fd)
7649 {
7650   dw_attr_node attr;
7651
7652   attr.dw_attr = attr_kind;
7653   attr.dw_attr_val.val_class = dw_val_class_file;
7654   attr.dw_attr_val.v.val_file = fd;
7655   add_dwarf_attr (die, &attr);
7656 }
7657
7658 /* Get the dwarf_file_data from a file DIE attribute.  */
7659
7660 static inline struct dwarf_file_data *
7661 AT_file (dw_attr_ref a)
7662 {
7663   gcc_assert (a && AT_class (a) == dw_val_class_file);
7664   return a->dw_attr_val.v.val_file;
7665 }
7666
7667 /* Add a vms delta attribute value to a DIE.  */
7668
7669 static inline void
7670 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7671                   const char *lbl1, const char *lbl2)
7672 {
7673   dw_attr_node attr;
7674
7675   attr.dw_attr = attr_kind;
7676   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7677   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7678   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7679   add_dwarf_attr (die, &attr);
7680 }
7681
7682 /* Add a label identifier attribute value to a DIE.  */
7683
7684 static inline void
7685 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7686 {
7687   dw_attr_node attr;
7688
7689   attr.dw_attr = attr_kind;
7690   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7691   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7692   add_dwarf_attr (die, &attr);
7693 }
7694
7695 /* Add a section offset attribute value to a DIE, an offset into the
7696    debug_line section.  */
7697
7698 static inline void
7699 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7700                 const char *label)
7701 {
7702   dw_attr_node attr;
7703
7704   attr.dw_attr = attr_kind;
7705   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7706   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7707   add_dwarf_attr (die, &attr);
7708 }
7709
7710 /* Add a section offset attribute value to a DIE, an offset into the
7711    debug_macinfo section.  */
7712
7713 static inline void
7714 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7715                const char *label)
7716 {
7717   dw_attr_node attr;
7718
7719   attr.dw_attr = attr_kind;
7720   attr.dw_attr_val.val_class = dw_val_class_macptr;
7721   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7722   add_dwarf_attr (die, &attr);
7723 }
7724
7725 /* Add an offset attribute value to a DIE.  */
7726
7727 static inline void
7728 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7729                unsigned HOST_WIDE_INT offset)
7730 {
7731   dw_attr_node attr;
7732
7733   attr.dw_attr = attr_kind;
7734   attr.dw_attr_val.val_class = dw_val_class_offset;
7735   attr.dw_attr_val.v.val_offset = offset;
7736   add_dwarf_attr (die, &attr);
7737 }
7738
7739 /* Add an range_list attribute value to a DIE.  */
7740
7741 static void
7742 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7743                    long unsigned int offset)
7744 {
7745   dw_attr_node attr;
7746
7747   attr.dw_attr = attr_kind;
7748   attr.dw_attr_val.val_class = dw_val_class_range_list;
7749   attr.dw_attr_val.v.val_offset = offset;
7750   add_dwarf_attr (die, &attr);
7751 }
7752
7753 /* Return the start label of a delta attribute.  */
7754
7755 static inline const char *
7756 AT_vms_delta1 (dw_attr_ref a)
7757 {
7758   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7759   return a->dw_attr_val.v.val_vms_delta.lbl1;
7760 }
7761
7762 /* Return the end label of a delta attribute.  */
7763
7764 static inline const char *
7765 AT_vms_delta2 (dw_attr_ref a)
7766 {
7767   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7768   return a->dw_attr_val.v.val_vms_delta.lbl2;
7769 }
7770
7771 static inline const char *
7772 AT_lbl (dw_attr_ref a)
7773 {
7774   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7775                     || AT_class (a) == dw_val_class_lineptr
7776                     || AT_class (a) == dw_val_class_macptr));
7777   return a->dw_attr_val.v.val_lbl_id;
7778 }
7779
7780 /* Get the attribute of type attr_kind.  */
7781
7782 static dw_attr_ref
7783 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7784 {
7785   dw_attr_ref a;
7786   unsigned ix;
7787   dw_die_ref spec = NULL;
7788
7789   if (! die)
7790     return NULL;
7791
7792   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7793     if (a->dw_attr == attr_kind)
7794       return a;
7795     else if (a->dw_attr == DW_AT_specification
7796              || a->dw_attr == DW_AT_abstract_origin)
7797       spec = AT_ref (a);
7798
7799   if (spec)
7800     return get_AT (spec, attr_kind);
7801
7802   return NULL;
7803 }
7804
7805 /* Return the "low pc" attribute value, typically associated with a subprogram
7806    DIE.  Return null if the "low pc" attribute is either not present, or if it
7807    cannot be represented as an assembler label identifier.  */
7808
7809 static inline const char *
7810 get_AT_low_pc (dw_die_ref die)
7811 {
7812   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7813
7814   return a ? AT_lbl (a) : NULL;
7815 }
7816
7817 /* Return the "high pc" attribute value, typically associated with a subprogram
7818    DIE.  Return null if the "high pc" attribute is either not present, or if it
7819    cannot be represented as an assembler label identifier.  */
7820
7821 static inline const char *
7822 get_AT_hi_pc (dw_die_ref die)
7823 {
7824   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7825
7826   return a ? AT_lbl (a) : NULL;
7827 }
7828
7829 /* Return the value of the string attribute designated by ATTR_KIND, or
7830    NULL if it is not present.  */
7831
7832 static inline const char *
7833 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7834 {
7835   dw_attr_ref a = get_AT (die, attr_kind);
7836
7837   return a ? AT_string (a) : NULL;
7838 }
7839
7840 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7841    if it is not present.  */
7842
7843 static inline int
7844 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7845 {
7846   dw_attr_ref a = get_AT (die, attr_kind);
7847
7848   return a ? AT_flag (a) : 0;
7849 }
7850
7851 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7852    if it is not present.  */
7853
7854 static inline unsigned
7855 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7856 {
7857   dw_attr_ref a = get_AT (die, attr_kind);
7858
7859   return a ? AT_unsigned (a) : 0;
7860 }
7861
7862 static inline dw_die_ref
7863 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7864 {
7865   dw_attr_ref a = get_AT (die, attr_kind);
7866
7867   return a ? AT_ref (a) : NULL;
7868 }
7869
7870 static inline struct dwarf_file_data *
7871 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7872 {
7873   dw_attr_ref a = get_AT (die, attr_kind);
7874
7875   return a ? AT_file (a) : NULL;
7876 }
7877
7878 /* Return TRUE if the language is C++.  */
7879
7880 static inline bool
7881 is_cxx (void)
7882 {
7883   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7884
7885   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7886 }
7887
7888 /* Return TRUE if the language is Fortran.  */
7889
7890 static inline bool
7891 is_fortran (void)
7892 {
7893   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7894
7895   return (lang == DW_LANG_Fortran77
7896           || lang == DW_LANG_Fortran90
7897           || lang == DW_LANG_Fortran95);
7898 }
7899
7900 /* Return TRUE if the language is Ada.  */
7901
7902 static inline bool
7903 is_ada (void)
7904 {
7905   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7906
7907   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7908 }
7909
7910 /* Remove the specified attribute if present.  */
7911
7912 static void
7913 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7914 {
7915   dw_attr_ref a;
7916   unsigned ix;
7917
7918   if (! die)
7919     return;
7920
7921   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7922     if (a->dw_attr == attr_kind)
7923       {
7924         if (AT_class (a) == dw_val_class_str)
7925           if (a->dw_attr_val.v.val_str->refcount)
7926             a->dw_attr_val.v.val_str->refcount--;
7927
7928         /* VEC_ordered_remove should help reduce the number of abbrevs
7929            that are needed.  */
7930         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7931         return;
7932       }
7933 }
7934
7935 /* Remove CHILD from its parent.  PREV must have the property that
7936    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7937
7938 static void
7939 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7940 {
7941   gcc_assert (child->die_parent == prev->die_parent);
7942   gcc_assert (prev->die_sib == child);
7943   if (prev == child)
7944     {
7945       gcc_assert (child->die_parent->die_child == child);
7946       prev = NULL;
7947     }
7948   else
7949     prev->die_sib = child->die_sib;
7950   if (child->die_parent->die_child == child)
7951     child->die_parent->die_child = prev;
7952 }
7953
7954 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7955    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7956
7957 static void
7958 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7959 {
7960   dw_die_ref parent = old_child->die_parent;
7961
7962   gcc_assert (parent == prev->die_parent);
7963   gcc_assert (prev->die_sib == old_child);
7964
7965   new_child->die_parent = parent;
7966   if (prev == old_child)
7967     {
7968       gcc_assert (parent->die_child == old_child);
7969       new_child->die_sib = new_child;
7970     }
7971   else
7972     {
7973       prev->die_sib = new_child;
7974       new_child->die_sib = old_child->die_sib;
7975     }
7976   if (old_child->die_parent->die_child == old_child)
7977     old_child->die_parent->die_child = new_child;
7978 }
7979
7980 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7981
7982 static void
7983 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7984 {
7985   dw_die_ref c;
7986   new_parent->die_child = old_parent->die_child;
7987   old_parent->die_child = NULL;
7988   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7989 }
7990
7991 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7992    matches TAG.  */
7993
7994 static void
7995 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7996 {
7997   dw_die_ref c;
7998
7999   c = die->die_child;
8000   if (c) do {
8001     dw_die_ref prev = c;
8002     c = c->die_sib;
8003     while (c->die_tag == tag)
8004       {
8005         remove_child_with_prev (c, prev);
8006         /* Might have removed every child.  */
8007         if (c == c->die_sib)
8008           return;
8009         c = c->die_sib;
8010       }
8011   } while (c != die->die_child);
8012 }
8013
8014 /* Add a CHILD_DIE as the last child of DIE.  */
8015
8016 static void
8017 add_child_die (dw_die_ref die, dw_die_ref child_die)
8018 {
8019   /* FIXME this should probably be an assert.  */
8020   if (! die || ! child_die)
8021     return;
8022   gcc_assert (die != child_die);
8023
8024   child_die->die_parent = die;
8025   if (die->die_child)
8026     {
8027       child_die->die_sib = die->die_child->die_sib;
8028       die->die_child->die_sib = child_die;
8029     }
8030   else
8031     child_die->die_sib = child_die;
8032   die->die_child = child_die;
8033 }
8034
8035 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8036    is the specification, to the end of PARENT's list of children.
8037    This is done by removing and re-adding it.  */
8038
8039 static void
8040 splice_child_die (dw_die_ref parent, dw_die_ref child)
8041 {
8042   dw_die_ref p;
8043
8044   /* We want the declaration DIE from inside the class, not the
8045      specification DIE at toplevel.  */
8046   if (child->die_parent != parent)
8047     {
8048       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8049
8050       if (tmp)
8051         child = tmp;
8052     }
8053
8054   gcc_assert (child->die_parent == parent
8055               || (child->die_parent
8056                   == get_AT_ref (parent, DW_AT_specification)));
8057
8058   for (p = child->die_parent->die_child; ; p = p->die_sib)
8059     if (p->die_sib == child)
8060       {
8061         remove_child_with_prev (child, p);
8062         break;
8063       }
8064
8065   add_child_die (parent, child);
8066 }
8067
8068 /* Return a pointer to a newly created DIE node.  */
8069
8070 static inline dw_die_ref
8071 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8072 {
8073   dw_die_ref die = ggc_alloc_cleared_die_node ();
8074
8075   die->die_tag = tag_value;
8076
8077   if (parent_die != NULL)
8078     add_child_die (parent_die, die);
8079   else
8080     {
8081       limbo_die_node *limbo_node;
8082
8083       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8084       limbo_node->die = die;
8085       limbo_node->created_for = t;
8086       limbo_node->next = limbo_die_list;
8087       limbo_die_list = limbo_node;
8088     }
8089
8090   return die;
8091 }
8092
8093 /* Return the DIE associated with the given type specifier.  */
8094
8095 static inline dw_die_ref
8096 lookup_type_die (tree type)
8097 {
8098   return TYPE_SYMTAB_DIE (type);
8099 }
8100
8101 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8102    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8103    anonymous type instead the one of the naming typedef.  */
8104
8105 static inline dw_die_ref
8106 strip_naming_typedef (tree type, dw_die_ref type_die)
8107 {
8108   if (type
8109       && TREE_CODE (type) == RECORD_TYPE
8110       && type_die
8111       && type_die->die_tag == DW_TAG_typedef
8112       && is_naming_typedef_decl (TYPE_NAME (type)))
8113     type_die = get_AT_ref (type_die, DW_AT_type);
8114   return type_die;
8115 }
8116
8117 /* Like lookup_type_die, but if type is an anonymous type named by a
8118    typedef[1], return the DIE of the anonymous type instead the one of
8119    the naming typedef.  This is because in gen_typedef_die, we did
8120    equate the anonymous struct named by the typedef with the DIE of
8121    the naming typedef. So by default, lookup_type_die on an anonymous
8122    struct yields the DIE of the naming typedef.
8123
8124    [1]: Read the comment of is_naming_typedef_decl to learn about what
8125    a naming typedef is.  */
8126
8127 static inline dw_die_ref
8128 lookup_type_die_strip_naming_typedef (tree type)
8129 {
8130   dw_die_ref die = lookup_type_die (type);
8131   return strip_naming_typedef (type, die);
8132 }
8133
8134 /* Equate a DIE to a given type specifier.  */
8135
8136 static inline void
8137 equate_type_number_to_die (tree type, dw_die_ref type_die)
8138 {
8139   TYPE_SYMTAB_DIE (type) = type_die;
8140 }
8141
8142 /* Returns a hash value for X (which really is a die_struct).  */
8143
8144 static hashval_t
8145 decl_die_table_hash (const void *x)
8146 {
8147   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8148 }
8149
8150 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8151
8152 static int
8153 decl_die_table_eq (const void *x, const void *y)
8154 {
8155   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8156 }
8157
8158 /* Return the DIE associated with a given declaration.  */
8159
8160 static inline dw_die_ref
8161 lookup_decl_die (tree decl)
8162 {
8163   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8164 }
8165
8166 /* Returns a hash value for X (which really is a var_loc_list).  */
8167
8168 static hashval_t
8169 decl_loc_table_hash (const void *x)
8170 {
8171   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8172 }
8173
8174 /* Return nonzero if decl_id of var_loc_list X is the same as
8175    UID of decl *Y.  */
8176
8177 static int
8178 decl_loc_table_eq (const void *x, const void *y)
8179 {
8180   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8181 }
8182
8183 /* Return the var_loc list associated with a given declaration.  */
8184
8185 static inline var_loc_list *
8186 lookup_decl_loc (const_tree decl)
8187 {
8188   if (!decl_loc_table)
8189     return NULL;
8190   return (var_loc_list *)
8191     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8192 }
8193
8194 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8195
8196 static hashval_t
8197 cached_dw_loc_list_table_hash (const void *x)
8198 {
8199   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8200 }
8201
8202 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8203    UID of decl *Y.  */
8204
8205 static int
8206 cached_dw_loc_list_table_eq (const void *x, const void *y)
8207 {
8208   return (((const cached_dw_loc_list *) x)->decl_id
8209           == DECL_UID ((const_tree) y));
8210 }
8211
8212 /* Equate a DIE to a particular declaration.  */
8213
8214 static void
8215 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8216 {
8217   unsigned int decl_id = DECL_UID (decl);
8218   void **slot;
8219
8220   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8221   *slot = decl_die;
8222   decl_die->decl_id = decl_id;
8223 }
8224
8225 /* Return how many bits covers PIECE EXPR_LIST.  */
8226
8227 static int
8228 decl_piece_bitsize (rtx piece)
8229 {
8230   int ret = (int) GET_MODE (piece);
8231   if (ret)
8232     return ret;
8233   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8234               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8235   return INTVAL (XEXP (XEXP (piece, 0), 0));
8236 }
8237
8238 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8239
8240 static rtx *
8241 decl_piece_varloc_ptr (rtx piece)
8242 {
8243   if ((int) GET_MODE (piece))
8244     return &XEXP (piece, 0);
8245   else
8246     return &XEXP (XEXP (piece, 0), 1);
8247 }
8248
8249 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8250    Next is the chain of following piece nodes.  */
8251
8252 static rtx
8253 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8254 {
8255   if (bitsize <= (int) MAX_MACHINE_MODE)
8256     return alloc_EXPR_LIST (bitsize, loc_note, next);
8257   else
8258     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8259                                                GEN_INT (bitsize),
8260                                                loc_note), next);
8261 }
8262
8263 /* Return rtx that should be stored into loc field for
8264    LOC_NOTE and BITPOS/BITSIZE.  */
8265
8266 static rtx
8267 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8268                       HOST_WIDE_INT bitsize)
8269 {
8270   if (bitsize != -1)
8271     {
8272       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8273       if (bitpos != 0)
8274         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8275     }
8276   return loc_note;
8277 }
8278
8279 /* This function either modifies location piece list *DEST in
8280    place (if SRC and INNER is NULL), or copies location piece list
8281    *SRC to *DEST while modifying it.  Location BITPOS is modified
8282    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8283    not copied and if needed some padding around it is added.
8284    When modifying in place, DEST should point to EXPR_LIST where
8285    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8286    to the start of the whole list and INNER points to the EXPR_LIST
8287    where earlier pieces cover PIECE_BITPOS bits.  */
8288
8289 static void
8290 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8291                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8292                    HOST_WIDE_INT bitsize, rtx loc_note)
8293 {
8294   int diff;
8295   bool copy = inner != NULL;
8296
8297   if (copy)
8298     {
8299       /* First copy all nodes preceeding the current bitpos.  */
8300       while (src != inner)
8301         {
8302           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8303                                    decl_piece_bitsize (*src), NULL_RTX);
8304           dest = &XEXP (*dest, 1);
8305           src = &XEXP (*src, 1);
8306         }
8307     }
8308   /* Add padding if needed.  */
8309   if (bitpos != piece_bitpos)
8310     {
8311       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8312                                copy ? NULL_RTX : *dest);
8313       dest = &XEXP (*dest, 1);
8314     }
8315   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8316     {
8317       gcc_assert (!copy);
8318       /* A piece with correct bitpos and bitsize already exist,
8319          just update the location for it and return.  */
8320       *decl_piece_varloc_ptr (*dest) = loc_note;
8321       return;
8322     }
8323   /* Add the piece that changed.  */
8324   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8325   dest = &XEXP (*dest, 1);
8326   /* Skip over pieces that overlap it.  */
8327   diff = bitpos - piece_bitpos + bitsize;
8328   if (!copy)
8329     src = dest;
8330   while (diff > 0 && *src)
8331     {
8332       rtx piece = *src;
8333       diff -= decl_piece_bitsize (piece);
8334       if (copy)
8335         src = &XEXP (piece, 1);
8336       else
8337         {
8338           *src = XEXP (piece, 1);
8339           free_EXPR_LIST_node (piece);
8340         }
8341     }
8342   /* Add padding if needed.  */
8343   if (diff < 0 && *src)
8344     {
8345       if (!copy)
8346         dest = src;
8347       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8348       dest = &XEXP (*dest, 1);
8349     }
8350   if (!copy)
8351     return;
8352   /* Finally copy all nodes following it.  */
8353   while (*src)
8354     {
8355       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8356                                decl_piece_bitsize (*src), NULL_RTX);
8357       dest = &XEXP (*dest, 1);
8358       src = &XEXP (*src, 1);
8359     }
8360 }
8361
8362 /* Add a variable location node to the linked list for DECL.  */
8363
8364 static struct var_loc_node *
8365 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8366 {
8367   unsigned int decl_id;
8368   var_loc_list *temp;
8369   void **slot;
8370   struct var_loc_node *loc = NULL;
8371   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8372
8373   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8374     {
8375       tree realdecl = DECL_DEBUG_EXPR (decl);
8376       if (realdecl && handled_component_p (realdecl))
8377         {
8378           HOST_WIDE_INT maxsize;
8379           tree innerdecl;
8380           innerdecl
8381             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8382           if (!DECL_P (innerdecl)
8383               || DECL_IGNORED_P (innerdecl)
8384               || TREE_STATIC (innerdecl)
8385               || bitsize <= 0
8386               || bitpos + bitsize > 256
8387               || bitsize != maxsize)
8388             return NULL;
8389           decl = innerdecl;
8390         }
8391     }
8392
8393   decl_id = DECL_UID (decl);
8394   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8395   if (*slot == NULL)
8396     {
8397       temp = ggc_alloc_cleared_var_loc_list ();
8398       temp->decl_id = decl_id;
8399       *slot = temp;
8400     }
8401   else
8402     temp = (var_loc_list *) *slot;
8403
8404   if (temp->last)
8405     {
8406       struct var_loc_node *last = temp->last, *unused = NULL;
8407       rtx *piece_loc = NULL, last_loc_note;
8408       int piece_bitpos = 0;
8409       if (last->next)
8410         {
8411           last = last->next;
8412           gcc_assert (last->next == NULL);
8413         }
8414       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8415         {
8416           piece_loc = &last->loc;
8417           do
8418             {
8419               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8420               if (piece_bitpos + cur_bitsize > bitpos)
8421                 break;
8422               piece_bitpos += cur_bitsize;
8423               piece_loc = &XEXP (*piece_loc, 1);
8424             }
8425           while (*piece_loc);
8426         }
8427       /* TEMP->LAST here is either pointer to the last but one or
8428          last element in the chained list, LAST is pointer to the
8429          last element.  */
8430       if (label && strcmp (last->label, label) == 0)
8431         {
8432           /* For SRA optimized variables if there weren't any real
8433              insns since last note, just modify the last node.  */
8434           if (piece_loc != NULL)
8435             {
8436               adjust_piece_list (piece_loc, NULL, NULL,
8437                                  bitpos, piece_bitpos, bitsize, loc_note);
8438               return NULL;
8439             }
8440           /* If the last note doesn't cover any instructions, remove it.  */
8441           if (temp->last != last)
8442             {
8443               temp->last->next = NULL;
8444               unused = last;
8445               last = temp->last;
8446               gcc_assert (strcmp (last->label, label) != 0);
8447             }
8448           else
8449             {
8450               gcc_assert (temp->first == temp->last);
8451               memset (temp->last, '\0', sizeof (*temp->last));
8452               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8453               return temp->last;
8454             }
8455         }
8456       if (bitsize == -1 && NOTE_P (last->loc))
8457         last_loc_note = last->loc;
8458       else if (piece_loc != NULL
8459                && *piece_loc != NULL_RTX
8460                && piece_bitpos == bitpos
8461                && decl_piece_bitsize (*piece_loc) == bitsize)
8462         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8463       else
8464         last_loc_note = NULL_RTX;
8465       /* If the current location is the same as the end of the list,
8466          and either both or neither of the locations is uninitialized,
8467          we have nothing to do.  */
8468       if (last_loc_note == NULL_RTX
8469           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8470                             NOTE_VAR_LOCATION_LOC (loc_note)))
8471           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8472                != NOTE_VAR_LOCATION_STATUS (loc_note))
8473               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8474                    == VAR_INIT_STATUS_UNINITIALIZED)
8475                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8476                       == VAR_INIT_STATUS_UNINITIALIZED))))
8477         {
8478           /* Add LOC to the end of list and update LAST.  If the last
8479              element of the list has been removed above, reuse its
8480              memory for the new node, otherwise allocate a new one.  */
8481           if (unused)
8482             {
8483               loc = unused;
8484               memset (loc, '\0', sizeof (*loc));
8485             }
8486           else
8487             loc = ggc_alloc_cleared_var_loc_node ();
8488           if (bitsize == -1 || piece_loc == NULL)
8489             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8490           else
8491             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8492                                bitpos, piece_bitpos, bitsize, loc_note);
8493           last->next = loc;
8494           /* Ensure TEMP->LAST will point either to the new last but one
8495              element of the chain, or to the last element in it.  */
8496           if (last != temp->last)
8497             temp->last = last;
8498         }
8499       else if (unused)
8500         ggc_free (unused);
8501     }
8502   else
8503     {
8504       loc = ggc_alloc_cleared_var_loc_node ();
8505       temp->first = loc;
8506       temp->last = loc;
8507       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8508     }
8509   return loc;
8510 }
8511 \f
8512 /* Keep track of the number of spaces used to indent the
8513    output of the debugging routines that print the structure of
8514    the DIE internal representation.  */
8515 static int print_indent;
8516
8517 /* Indent the line the number of spaces given by print_indent.  */
8518
8519 static inline void
8520 print_spaces (FILE *outfile)
8521 {
8522   fprintf (outfile, "%*s", print_indent, "");
8523 }
8524
8525 /* Print a type signature in hex.  */
8526
8527 static inline void
8528 print_signature (FILE *outfile, char *sig)
8529 {
8530   int i;
8531
8532   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8533     fprintf (outfile, "%02x", sig[i] & 0xff);
8534 }
8535
8536 /* Print the information associated with a given DIE, and its children.
8537    This routine is a debugging aid only.  */
8538
8539 static void
8540 print_die (dw_die_ref die, FILE *outfile)
8541 {
8542   dw_attr_ref a;
8543   dw_die_ref c;
8544   unsigned ix;
8545
8546   print_spaces (outfile);
8547   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8548            die->die_offset, dwarf_tag_name (die->die_tag),
8549            (void*) die);
8550   print_spaces (outfile);
8551   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8552   fprintf (outfile, " offset: %ld", die->die_offset);
8553   fprintf (outfile, " mark: %d\n", die->die_mark);
8554
8555   if (dwarf_version >= 4 && die->die_id.die_type_node)
8556     {
8557       print_spaces (outfile);
8558       fprintf (outfile, "  signature: ");
8559       print_signature (outfile, die->die_id.die_type_node->signature);
8560       fprintf (outfile, "\n");
8561     }
8562
8563   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8564     {
8565       print_spaces (outfile);
8566       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8567
8568       switch (AT_class (a))
8569         {
8570         case dw_val_class_addr:
8571           fprintf (outfile, "address");
8572           break;
8573         case dw_val_class_offset:
8574           fprintf (outfile, "offset");
8575           break;
8576         case dw_val_class_loc:
8577           fprintf (outfile, "location descriptor");
8578           break;
8579         case dw_val_class_loc_list:
8580           fprintf (outfile, "location list -> label:%s",
8581                    AT_loc_list (a)->ll_symbol);
8582           break;
8583         case dw_val_class_range_list:
8584           fprintf (outfile, "range list");
8585           break;
8586         case dw_val_class_const:
8587           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8588           break;
8589         case dw_val_class_unsigned_const:
8590           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8591           break;
8592         case dw_val_class_const_double:
8593           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8594                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8595                    a->dw_attr_val.v.val_double.high,
8596                    a->dw_attr_val.v.val_double.low);
8597           break;
8598         case dw_val_class_vec:
8599           fprintf (outfile, "floating-point or vector constant");
8600           break;
8601         case dw_val_class_flag:
8602           fprintf (outfile, "%u", AT_flag (a));
8603           break;
8604         case dw_val_class_die_ref:
8605           if (AT_ref (a) != NULL)
8606             {
8607               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8608                 {
8609                   fprintf (outfile, "die -> signature: ");
8610                   print_signature (outfile,
8611                                    AT_ref (a)->die_id.die_type_node->signature);
8612                 }
8613               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8614                 fprintf (outfile, "die -> label: %s",
8615                          AT_ref (a)->die_id.die_symbol);
8616               else
8617                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8618               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8619             }
8620           else
8621             fprintf (outfile, "die -> <null>");
8622           break;
8623         case dw_val_class_vms_delta:
8624           fprintf (outfile, "delta: @slotcount(%s-%s)",
8625                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8626           break;
8627         case dw_val_class_lbl_id:
8628         case dw_val_class_lineptr:
8629         case dw_val_class_macptr:
8630           fprintf (outfile, "label: %s", AT_lbl (a));
8631           break;
8632         case dw_val_class_str:
8633           if (AT_string (a) != NULL)
8634             fprintf (outfile, "\"%s\"", AT_string (a));
8635           else
8636             fprintf (outfile, "<null>");
8637           break;
8638         case dw_val_class_file:
8639           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8640                    AT_file (a)->emitted_number);
8641           break;
8642         case dw_val_class_data8:
8643           {
8644             int i;
8645
8646             for (i = 0; i < 8; i++)
8647               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8648             break;
8649           }
8650         default:
8651           break;
8652         }
8653
8654       fprintf (outfile, "\n");
8655     }
8656
8657   if (die->die_child != NULL)
8658     {
8659       print_indent += 4;
8660       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8661       print_indent -= 4;
8662     }
8663   if (print_indent == 0)
8664     fprintf (outfile, "\n");
8665 }
8666
8667 /* Print the contents of the source code line number correspondence table.
8668    This routine is a debugging aid only.  */
8669
8670 static void
8671 print_dwarf_line_table (FILE *outfile)
8672 {
8673   unsigned i;
8674   dw_line_info_ref line_info;
8675
8676   fprintf (outfile, "\n\nDWARF source line information\n");
8677   for (i = 1; i < line_info_table_in_use; i++)
8678     {
8679       line_info = &line_info_table[i];
8680       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8681                line_info->dw_file_num,
8682                line_info->dw_line_num);
8683     }
8684
8685   fprintf (outfile, "\n\n");
8686 }
8687
8688 /* Print the information collected for a given DIE.  */
8689
8690 DEBUG_FUNCTION void
8691 debug_dwarf_die (dw_die_ref die)
8692 {
8693   print_die (die, stderr);
8694 }
8695
8696 /* Print all DWARF information collected for the compilation unit.
8697    This routine is a debugging aid only.  */
8698
8699 DEBUG_FUNCTION void
8700 debug_dwarf (void)
8701 {
8702   print_indent = 0;
8703   print_die (comp_unit_die (), stderr);
8704   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8705     print_dwarf_line_table (stderr);
8706 }
8707 \f
8708 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8709    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8710    DIE that marks the start of the DIEs for this include file.  */
8711
8712 static dw_die_ref
8713 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8714 {
8715   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8716   dw_die_ref new_unit = gen_compile_unit_die (filename);
8717
8718   new_unit->die_sib = old_unit;
8719   return new_unit;
8720 }
8721
8722 /* Close an include-file CU and reopen the enclosing one.  */
8723
8724 static dw_die_ref
8725 pop_compile_unit (dw_die_ref old_unit)
8726 {
8727   dw_die_ref new_unit = old_unit->die_sib;
8728
8729   old_unit->die_sib = NULL;
8730   return new_unit;
8731 }
8732
8733 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8734 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8735
8736 /* Calculate the checksum of a location expression.  */
8737
8738 static inline void
8739 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8740 {
8741   int tem;
8742
8743   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8744   CHECKSUM (tem);
8745   CHECKSUM (loc->dw_loc_oprnd1);
8746   CHECKSUM (loc->dw_loc_oprnd2);
8747 }
8748
8749 /* Calculate the checksum of an attribute.  */
8750
8751 static void
8752 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8753 {
8754   dw_loc_descr_ref loc;
8755   rtx r;
8756
8757   CHECKSUM (at->dw_attr);
8758
8759   /* We don't care that this was compiled with a different compiler
8760      snapshot; if the output is the same, that's what matters.  */
8761   if (at->dw_attr == DW_AT_producer)
8762     return;
8763
8764   switch (AT_class (at))
8765     {
8766     case dw_val_class_const:
8767       CHECKSUM (at->dw_attr_val.v.val_int);
8768       break;
8769     case dw_val_class_unsigned_const:
8770       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8771       break;
8772     case dw_val_class_const_double:
8773       CHECKSUM (at->dw_attr_val.v.val_double);
8774       break;
8775     case dw_val_class_vec:
8776       CHECKSUM (at->dw_attr_val.v.val_vec);
8777       break;
8778     case dw_val_class_flag:
8779       CHECKSUM (at->dw_attr_val.v.val_flag);
8780       break;
8781     case dw_val_class_str:
8782       CHECKSUM_STRING (AT_string (at));
8783       break;
8784
8785     case dw_val_class_addr:
8786       r = AT_addr (at);
8787       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8788       CHECKSUM_STRING (XSTR (r, 0));
8789       break;
8790
8791     case dw_val_class_offset:
8792       CHECKSUM (at->dw_attr_val.v.val_offset);
8793       break;
8794
8795     case dw_val_class_loc:
8796       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8797         loc_checksum (loc, ctx);
8798       break;
8799
8800     case dw_val_class_die_ref:
8801       die_checksum (AT_ref (at), ctx, mark);
8802       break;
8803
8804     case dw_val_class_fde_ref:
8805     case dw_val_class_vms_delta:
8806     case dw_val_class_lbl_id:
8807     case dw_val_class_lineptr:
8808     case dw_val_class_macptr:
8809       break;
8810
8811     case dw_val_class_file:
8812       CHECKSUM_STRING (AT_file (at)->filename);
8813       break;
8814
8815     case dw_val_class_data8:
8816       CHECKSUM (at->dw_attr_val.v.val_data8);
8817       break;
8818
8819     default:
8820       break;
8821     }
8822 }
8823
8824 /* Calculate the checksum of a DIE.  */
8825
8826 static void
8827 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8828 {
8829   dw_die_ref c;
8830   dw_attr_ref a;
8831   unsigned ix;
8832
8833   /* To avoid infinite recursion.  */
8834   if (die->die_mark)
8835     {
8836       CHECKSUM (die->die_mark);
8837       return;
8838     }
8839   die->die_mark = ++(*mark);
8840
8841   CHECKSUM (die->die_tag);
8842
8843   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8844     attr_checksum (a, ctx, mark);
8845
8846   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8847 }
8848
8849 #undef CHECKSUM
8850 #undef CHECKSUM_STRING
8851
8852 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8853 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8854 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8855 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8856 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8857 #define CHECKSUM_ATTR(FOO) \
8858   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8859
8860 /* Calculate the checksum of a number in signed LEB128 format.  */
8861
8862 static void
8863 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8864 {
8865   unsigned char byte;
8866   bool more;
8867
8868   while (1)
8869     {
8870       byte = (value & 0x7f);
8871       value >>= 7;
8872       more = !((value == 0 && (byte & 0x40) == 0)
8873                 || (value == -1 && (byte & 0x40) != 0));
8874       if (more)
8875         byte |= 0x80;
8876       CHECKSUM (byte);
8877       if (!more)
8878         break;
8879     }
8880 }
8881
8882 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8883
8884 static void
8885 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8886 {
8887   while (1)
8888     {
8889       unsigned char byte = (value & 0x7f);
8890       value >>= 7;
8891       if (value != 0)
8892         /* More bytes to follow.  */
8893         byte |= 0x80;
8894       CHECKSUM (byte);
8895       if (value == 0)
8896         break;
8897     }
8898 }
8899
8900 /* Checksum the context of the DIE.  This adds the names of any
8901    surrounding namespaces or structures to the checksum.  */
8902
8903 static void
8904 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8905 {
8906   const char *name;
8907   dw_die_ref spec;
8908   int tag = die->die_tag;
8909
8910   if (tag != DW_TAG_namespace
8911       && tag != DW_TAG_structure_type
8912       && tag != DW_TAG_class_type)
8913     return;
8914
8915   name = get_AT_string (die, DW_AT_name);
8916
8917   spec = get_AT_ref (die, DW_AT_specification);
8918   if (spec != NULL)
8919     die = spec;
8920
8921   if (die->die_parent != NULL)
8922     checksum_die_context (die->die_parent, ctx);
8923
8924   CHECKSUM_ULEB128 ('C');
8925   CHECKSUM_ULEB128 (tag);
8926   if (name != NULL)
8927     CHECKSUM_STRING (name);
8928 }
8929
8930 /* Calculate the checksum of a location expression.  */
8931
8932 static inline void
8933 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8934 {
8935   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8936      were emitted as a DW_FORM_sdata instead of a location expression.  */
8937   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8938     {
8939       CHECKSUM_ULEB128 (DW_FORM_sdata);
8940       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8941       return;
8942     }
8943
8944   /* Otherwise, just checksum the raw location expression.  */
8945   while (loc != NULL)
8946     {
8947       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8948       CHECKSUM (loc->dw_loc_oprnd1);
8949       CHECKSUM (loc->dw_loc_oprnd2);
8950       loc = loc->dw_loc_next;
8951     }
8952 }
8953
8954 /* Calculate the checksum of an attribute.  */
8955
8956 static void
8957 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8958                        struct md5_ctx *ctx, int *mark)
8959 {
8960   dw_loc_descr_ref loc;
8961   rtx r;
8962
8963   if (AT_class (at) == dw_val_class_die_ref)
8964     {
8965       dw_die_ref target_die = AT_ref (at);
8966
8967       /* For pointer and reference types, we checksum only the (qualified)
8968          name of the target type (if there is a name).  For friend entries,
8969          we checksum only the (qualified) name of the target type or function.
8970          This allows the checksum to remain the same whether the target type
8971          is complete or not.  */
8972       if ((at->dw_attr == DW_AT_type
8973            && (tag == DW_TAG_pointer_type
8974                || tag == DW_TAG_reference_type
8975                || tag == DW_TAG_rvalue_reference_type
8976                || tag == DW_TAG_ptr_to_member_type))
8977           || (at->dw_attr == DW_AT_friend
8978               && tag == DW_TAG_friend))
8979         {
8980           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8981
8982           if (name_attr != NULL)
8983             {
8984               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8985
8986               if (decl == NULL)
8987                 decl = target_die;
8988               CHECKSUM_ULEB128 ('N');
8989               CHECKSUM_ULEB128 (at->dw_attr);
8990               if (decl->die_parent != NULL)
8991                 checksum_die_context (decl->die_parent, ctx);
8992               CHECKSUM_ULEB128 ('E');
8993               CHECKSUM_STRING (AT_string (name_attr));
8994               return;
8995             }
8996         }
8997
8998       /* For all other references to another DIE, we check to see if the
8999          target DIE has already been visited.  If it has, we emit a
9000          backward reference; if not, we descend recursively.  */
9001       if (target_die->die_mark > 0)
9002         {
9003           CHECKSUM_ULEB128 ('R');
9004           CHECKSUM_ULEB128 (at->dw_attr);
9005           CHECKSUM_ULEB128 (target_die->die_mark);
9006         }
9007       else
9008         {
9009           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9010
9011           if (decl == NULL)
9012             decl = target_die;
9013           target_die->die_mark = ++(*mark);
9014           CHECKSUM_ULEB128 ('T');
9015           CHECKSUM_ULEB128 (at->dw_attr);
9016           if (decl->die_parent != NULL)
9017             checksum_die_context (decl->die_parent, ctx);
9018           die_checksum_ordered (target_die, ctx, mark);
9019         }
9020       return;
9021     }
9022
9023   CHECKSUM_ULEB128 ('A');
9024   CHECKSUM_ULEB128 (at->dw_attr);
9025
9026   switch (AT_class (at))
9027     {
9028     case dw_val_class_const:
9029       CHECKSUM_ULEB128 (DW_FORM_sdata);
9030       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9031       break;
9032
9033     case dw_val_class_unsigned_const:
9034       CHECKSUM_ULEB128 (DW_FORM_sdata);
9035       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9036       break;
9037
9038     case dw_val_class_const_double:
9039       CHECKSUM_ULEB128 (DW_FORM_block);
9040       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9041       CHECKSUM (at->dw_attr_val.v.val_double);
9042       break;
9043
9044     case dw_val_class_vec:
9045       CHECKSUM_ULEB128 (DW_FORM_block);
9046       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9047       CHECKSUM (at->dw_attr_val.v.val_vec);
9048       break;
9049
9050     case dw_val_class_flag:
9051       CHECKSUM_ULEB128 (DW_FORM_flag);
9052       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9053       break;
9054
9055     case dw_val_class_str:
9056       CHECKSUM_ULEB128 (DW_FORM_string);
9057       CHECKSUM_STRING (AT_string (at));
9058       break;
9059
9060     case dw_val_class_addr:
9061       r = AT_addr (at);
9062       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9063       CHECKSUM_ULEB128 (DW_FORM_string);
9064       CHECKSUM_STRING (XSTR (r, 0));
9065       break;
9066
9067     case dw_val_class_offset:
9068       CHECKSUM_ULEB128 (DW_FORM_sdata);
9069       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9070       break;
9071
9072     case dw_val_class_loc:
9073       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9074         loc_checksum_ordered (loc, ctx);
9075       break;
9076
9077     case dw_val_class_fde_ref:
9078     case dw_val_class_lbl_id:
9079     case dw_val_class_lineptr:
9080     case dw_val_class_macptr:
9081       break;
9082
9083     case dw_val_class_file:
9084       CHECKSUM_ULEB128 (DW_FORM_string);
9085       CHECKSUM_STRING (AT_file (at)->filename);
9086       break;
9087
9088     case dw_val_class_data8:
9089       CHECKSUM (at->dw_attr_val.v.val_data8);
9090       break;
9091
9092     default:
9093       break;
9094     }
9095 }
9096
9097 struct checksum_attributes
9098 {
9099   dw_attr_ref at_name;
9100   dw_attr_ref at_type;
9101   dw_attr_ref at_friend;
9102   dw_attr_ref at_accessibility;
9103   dw_attr_ref at_address_class;
9104   dw_attr_ref at_allocated;
9105   dw_attr_ref at_artificial;
9106   dw_attr_ref at_associated;
9107   dw_attr_ref at_binary_scale;
9108   dw_attr_ref at_bit_offset;
9109   dw_attr_ref at_bit_size;
9110   dw_attr_ref at_bit_stride;
9111   dw_attr_ref at_byte_size;
9112   dw_attr_ref at_byte_stride;
9113   dw_attr_ref at_const_value;
9114   dw_attr_ref at_containing_type;
9115   dw_attr_ref at_count;
9116   dw_attr_ref at_data_location;
9117   dw_attr_ref at_data_member_location;
9118   dw_attr_ref at_decimal_scale;
9119   dw_attr_ref at_decimal_sign;
9120   dw_attr_ref at_default_value;
9121   dw_attr_ref at_digit_count;
9122   dw_attr_ref at_discr;
9123   dw_attr_ref at_discr_list;
9124   dw_attr_ref at_discr_value;
9125   dw_attr_ref at_encoding;
9126   dw_attr_ref at_endianity;
9127   dw_attr_ref at_explicit;
9128   dw_attr_ref at_is_optional;
9129   dw_attr_ref at_location;
9130   dw_attr_ref at_lower_bound;
9131   dw_attr_ref at_mutable;
9132   dw_attr_ref at_ordering;
9133   dw_attr_ref at_picture_string;
9134   dw_attr_ref at_prototyped;
9135   dw_attr_ref at_small;
9136   dw_attr_ref at_segment;
9137   dw_attr_ref at_string_length;
9138   dw_attr_ref at_threads_scaled;
9139   dw_attr_ref at_upper_bound;
9140   dw_attr_ref at_use_location;
9141   dw_attr_ref at_use_UTF8;
9142   dw_attr_ref at_variable_parameter;
9143   dw_attr_ref at_virtuality;
9144   dw_attr_ref at_visibility;
9145   dw_attr_ref at_vtable_elem_location;
9146 };
9147
9148 /* Collect the attributes that we will want to use for the checksum.  */
9149
9150 static void
9151 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9152 {
9153   dw_attr_ref a;
9154   unsigned ix;
9155
9156   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9157     {
9158       switch (a->dw_attr)
9159         {
9160         case DW_AT_name:
9161           attrs->at_name = a;
9162           break;
9163         case DW_AT_type:
9164           attrs->at_type = a;
9165           break;
9166         case DW_AT_friend:
9167           attrs->at_friend = a;
9168           break;
9169         case DW_AT_accessibility:
9170           attrs->at_accessibility = a;
9171           break;
9172         case DW_AT_address_class:
9173           attrs->at_address_class = a;
9174           break;
9175         case DW_AT_allocated:
9176           attrs->at_allocated = a;
9177           break;
9178         case DW_AT_artificial:
9179           attrs->at_artificial = a;
9180           break;
9181         case DW_AT_associated:
9182           attrs->at_associated = a;
9183           break;
9184         case DW_AT_binary_scale:
9185           attrs->at_binary_scale = a;
9186           break;
9187         case DW_AT_bit_offset:
9188           attrs->at_bit_offset = a;
9189           break;
9190         case DW_AT_bit_size:
9191           attrs->at_bit_size = a;
9192           break;
9193         case DW_AT_bit_stride:
9194           attrs->at_bit_stride = a;
9195           break;
9196         case DW_AT_byte_size:
9197           attrs->at_byte_size = a;
9198           break;
9199         case DW_AT_byte_stride:
9200           attrs->at_byte_stride = a;
9201           break;
9202         case DW_AT_const_value:
9203           attrs->at_const_value = a;
9204           break;
9205         case DW_AT_containing_type:
9206           attrs->at_containing_type = a;
9207           break;
9208         case DW_AT_count:
9209           attrs->at_count = a;
9210           break;
9211         case DW_AT_data_location:
9212           attrs->at_data_location = a;
9213           break;
9214         case DW_AT_data_member_location:
9215           attrs->at_data_member_location = a;
9216           break;
9217         case DW_AT_decimal_scale:
9218           attrs->at_decimal_scale = a;
9219           break;
9220         case DW_AT_decimal_sign:
9221           attrs->at_decimal_sign = a;
9222           break;
9223         case DW_AT_default_value:
9224           attrs->at_default_value = a;
9225           break;
9226         case DW_AT_digit_count:
9227           attrs->at_digit_count = a;
9228           break;
9229         case DW_AT_discr:
9230           attrs->at_discr = a;
9231           break;
9232         case DW_AT_discr_list:
9233           attrs->at_discr_list = a;
9234           break;
9235         case DW_AT_discr_value:
9236           attrs->at_discr_value = a;
9237           break;
9238         case DW_AT_encoding:
9239           attrs->at_encoding = a;
9240           break;
9241         case DW_AT_endianity:
9242           attrs->at_endianity = a;
9243           break;
9244         case DW_AT_explicit:
9245           attrs->at_explicit = a;
9246           break;
9247         case DW_AT_is_optional:
9248           attrs->at_is_optional = a;
9249           break;
9250         case DW_AT_location:
9251           attrs->at_location = a;
9252           break;
9253         case DW_AT_lower_bound:
9254           attrs->at_lower_bound = a;
9255           break;
9256         case DW_AT_mutable:
9257           attrs->at_mutable = a;
9258           break;
9259         case DW_AT_ordering:
9260           attrs->at_ordering = a;
9261           break;
9262         case DW_AT_picture_string:
9263           attrs->at_picture_string = a;
9264           break;
9265         case DW_AT_prototyped:
9266           attrs->at_prototyped = a;
9267           break;
9268         case DW_AT_small:
9269           attrs->at_small = a;
9270           break;
9271         case DW_AT_segment:
9272           attrs->at_segment = a;
9273           break;
9274         case DW_AT_string_length:
9275           attrs->at_string_length = a;
9276           break;
9277         case DW_AT_threads_scaled:
9278           attrs->at_threads_scaled = a;
9279           break;
9280         case DW_AT_upper_bound:
9281           attrs->at_upper_bound = a;
9282           break;
9283         case DW_AT_use_location:
9284           attrs->at_use_location = a;
9285           break;
9286         case DW_AT_use_UTF8:
9287           attrs->at_use_UTF8 = a;
9288           break;
9289         case DW_AT_variable_parameter:
9290           attrs->at_variable_parameter = a;
9291           break;
9292         case DW_AT_virtuality:
9293           attrs->at_virtuality = a;
9294           break;
9295         case DW_AT_visibility:
9296           attrs->at_visibility = a;
9297           break;
9298         case DW_AT_vtable_elem_location:
9299           attrs->at_vtable_elem_location = a;
9300           break;
9301         default:
9302           break;
9303         }
9304     }
9305 }
9306
9307 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9308
9309 static void
9310 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9311 {
9312   dw_die_ref c;
9313   dw_die_ref decl;
9314   struct checksum_attributes attrs;
9315
9316   CHECKSUM_ULEB128 ('D');
9317   CHECKSUM_ULEB128 (die->die_tag);
9318
9319   memset (&attrs, 0, sizeof (attrs));
9320
9321   decl = get_AT_ref (die, DW_AT_specification);
9322   if (decl != NULL)
9323     collect_checksum_attributes (&attrs, decl);
9324   collect_checksum_attributes (&attrs, die);
9325
9326   CHECKSUM_ATTR (attrs.at_name);
9327   CHECKSUM_ATTR (attrs.at_accessibility);
9328   CHECKSUM_ATTR (attrs.at_address_class);
9329   CHECKSUM_ATTR (attrs.at_allocated);
9330   CHECKSUM_ATTR (attrs.at_artificial);
9331   CHECKSUM_ATTR (attrs.at_associated);
9332   CHECKSUM_ATTR (attrs.at_binary_scale);
9333   CHECKSUM_ATTR (attrs.at_bit_offset);
9334   CHECKSUM_ATTR (attrs.at_bit_size);
9335   CHECKSUM_ATTR (attrs.at_bit_stride);
9336   CHECKSUM_ATTR (attrs.at_byte_size);
9337   CHECKSUM_ATTR (attrs.at_byte_stride);
9338   CHECKSUM_ATTR (attrs.at_const_value);
9339   CHECKSUM_ATTR (attrs.at_containing_type);
9340   CHECKSUM_ATTR (attrs.at_count);
9341   CHECKSUM_ATTR (attrs.at_data_location);
9342   CHECKSUM_ATTR (attrs.at_data_member_location);
9343   CHECKSUM_ATTR (attrs.at_decimal_scale);
9344   CHECKSUM_ATTR (attrs.at_decimal_sign);
9345   CHECKSUM_ATTR (attrs.at_default_value);
9346   CHECKSUM_ATTR (attrs.at_digit_count);
9347   CHECKSUM_ATTR (attrs.at_discr);
9348   CHECKSUM_ATTR (attrs.at_discr_list);
9349   CHECKSUM_ATTR (attrs.at_discr_value);
9350   CHECKSUM_ATTR (attrs.at_encoding);
9351   CHECKSUM_ATTR (attrs.at_endianity);
9352   CHECKSUM_ATTR (attrs.at_explicit);
9353   CHECKSUM_ATTR (attrs.at_is_optional);
9354   CHECKSUM_ATTR (attrs.at_location);
9355   CHECKSUM_ATTR (attrs.at_lower_bound);
9356   CHECKSUM_ATTR (attrs.at_mutable);
9357   CHECKSUM_ATTR (attrs.at_ordering);
9358   CHECKSUM_ATTR (attrs.at_picture_string);
9359   CHECKSUM_ATTR (attrs.at_prototyped);
9360   CHECKSUM_ATTR (attrs.at_small);
9361   CHECKSUM_ATTR (attrs.at_segment);
9362   CHECKSUM_ATTR (attrs.at_string_length);
9363   CHECKSUM_ATTR (attrs.at_threads_scaled);
9364   CHECKSUM_ATTR (attrs.at_upper_bound);
9365   CHECKSUM_ATTR (attrs.at_use_location);
9366   CHECKSUM_ATTR (attrs.at_use_UTF8);
9367   CHECKSUM_ATTR (attrs.at_variable_parameter);
9368   CHECKSUM_ATTR (attrs.at_virtuality);
9369   CHECKSUM_ATTR (attrs.at_visibility);
9370   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9371   CHECKSUM_ATTR (attrs.at_type);
9372   CHECKSUM_ATTR (attrs.at_friend);
9373
9374   /* Checksum the child DIEs, except for nested types and member functions.  */
9375   c = die->die_child;
9376   if (c) do {
9377     dw_attr_ref name_attr;
9378
9379     c = c->die_sib;
9380     name_attr = get_AT (c, DW_AT_name);
9381     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9382         && name_attr != NULL)
9383       {
9384         CHECKSUM_ULEB128 ('S');
9385         CHECKSUM_ULEB128 (c->die_tag);
9386         CHECKSUM_STRING (AT_string (name_attr));
9387       }
9388     else
9389       {
9390         /* Mark this DIE so it gets processed when unmarking.  */
9391         if (c->die_mark == 0)
9392           c->die_mark = -1;
9393         die_checksum_ordered (c, ctx, mark);
9394       }
9395   } while (c != die->die_child);
9396
9397   CHECKSUM_ULEB128 (0);
9398 }
9399
9400 #undef CHECKSUM
9401 #undef CHECKSUM_STRING
9402 #undef CHECKSUM_ATTR
9403 #undef CHECKSUM_LEB128
9404 #undef CHECKSUM_ULEB128
9405
9406 /* Generate the type signature for DIE.  This is computed by generating an
9407    MD5 checksum over the DIE's tag, its relevant attributes, and its
9408    children.  Attributes that are references to other DIEs are processed
9409    by recursion, using the MARK field to prevent infinite recursion.
9410    If the DIE is nested inside a namespace or another type, we also
9411    need to include that context in the signature.  The lower 64 bits
9412    of the resulting MD5 checksum comprise the signature.  */
9413
9414 static void
9415 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9416 {
9417   int mark;
9418   const char *name;
9419   unsigned char checksum[16];
9420   struct md5_ctx ctx;
9421   dw_die_ref decl;
9422
9423   name = get_AT_string (die, DW_AT_name);
9424   decl = get_AT_ref (die, DW_AT_specification);
9425
9426   /* First, compute a signature for just the type name (and its surrounding
9427      context, if any.  This is stored in the type unit DIE for link-time
9428      ODR (one-definition rule) checking.  */
9429
9430   if (is_cxx() && name != NULL)
9431     {
9432       md5_init_ctx (&ctx);
9433
9434       /* Checksum the names of surrounding namespaces and structures.  */
9435       if (decl != NULL && decl->die_parent != NULL)
9436         checksum_die_context (decl->die_parent, &ctx);
9437
9438       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9439       md5_process_bytes (name, strlen (name) + 1, &ctx);
9440       md5_finish_ctx (&ctx, checksum);
9441
9442       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9443     }
9444
9445   /* Next, compute the complete type signature.  */
9446
9447   md5_init_ctx (&ctx);
9448   mark = 1;
9449   die->die_mark = mark;
9450
9451   /* Checksum the names of surrounding namespaces and structures.  */
9452   if (decl != NULL && decl->die_parent != NULL)
9453     checksum_die_context (decl->die_parent, &ctx);
9454
9455   /* Checksum the DIE and its children.  */
9456   die_checksum_ordered (die, &ctx, &mark);
9457   unmark_all_dies (die);
9458   md5_finish_ctx (&ctx, checksum);
9459
9460   /* Store the signature in the type node and link the type DIE and the
9461      type node together.  */
9462   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9463           DWARF_TYPE_SIGNATURE_SIZE);
9464   die->die_id.die_type_node = type_node;
9465   type_node->type_die = die;
9466
9467   /* If the DIE is a specification, link its declaration to the type node
9468      as well.  */
9469   if (decl != NULL)
9470     decl->die_id.die_type_node = type_node;
9471 }
9472
9473 /* Do the location expressions look same?  */
9474 static inline int
9475 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9476 {
9477   return loc1->dw_loc_opc == loc2->dw_loc_opc
9478          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9479          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9480 }
9481
9482 /* Do the values look the same?  */
9483 static int
9484 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9485 {
9486   dw_loc_descr_ref loc1, loc2;
9487   rtx r1, r2;
9488
9489   if (v1->val_class != v2->val_class)
9490     return 0;
9491
9492   switch (v1->val_class)
9493     {
9494     case dw_val_class_const:
9495       return v1->v.val_int == v2->v.val_int;
9496     case dw_val_class_unsigned_const:
9497       return v1->v.val_unsigned == v2->v.val_unsigned;
9498     case dw_val_class_const_double:
9499       return v1->v.val_double.high == v2->v.val_double.high
9500              && v1->v.val_double.low == v2->v.val_double.low;
9501     case dw_val_class_vec:
9502       if (v1->v.val_vec.length != v2->v.val_vec.length
9503           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9504         return 0;
9505       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9506                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9507         return 0;
9508       return 1;
9509     case dw_val_class_flag:
9510       return v1->v.val_flag == v2->v.val_flag;
9511     case dw_val_class_str:
9512       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9513
9514     case dw_val_class_addr:
9515       r1 = v1->v.val_addr;
9516       r2 = v2->v.val_addr;
9517       if (GET_CODE (r1) != GET_CODE (r2))
9518         return 0;
9519       return !rtx_equal_p (r1, r2);
9520
9521     case dw_val_class_offset:
9522       return v1->v.val_offset == v2->v.val_offset;
9523
9524     case dw_val_class_loc:
9525       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9526            loc1 && loc2;
9527            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9528         if (!same_loc_p (loc1, loc2, mark))
9529           return 0;
9530       return !loc1 && !loc2;
9531
9532     case dw_val_class_die_ref:
9533       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9534
9535     case dw_val_class_fde_ref:
9536     case dw_val_class_vms_delta:
9537     case dw_val_class_lbl_id:
9538     case dw_val_class_lineptr:
9539     case dw_val_class_macptr:
9540       return 1;
9541
9542     case dw_val_class_file:
9543       return v1->v.val_file == v2->v.val_file;
9544
9545     case dw_val_class_data8:
9546       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9547
9548     default:
9549       return 1;
9550     }
9551 }
9552
9553 /* Do the attributes look the same?  */
9554
9555 static int
9556 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9557 {
9558   if (at1->dw_attr != at2->dw_attr)
9559     return 0;
9560
9561   /* We don't care that this was compiled with a different compiler
9562      snapshot; if the output is the same, that's what matters. */
9563   if (at1->dw_attr == DW_AT_producer)
9564     return 1;
9565
9566   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9567 }
9568
9569 /* Do the dies look the same?  */
9570
9571 static int
9572 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9573 {
9574   dw_die_ref c1, c2;
9575   dw_attr_ref a1;
9576   unsigned ix;
9577
9578   /* To avoid infinite recursion.  */
9579   if (die1->die_mark)
9580     return die1->die_mark == die2->die_mark;
9581   die1->die_mark = die2->die_mark = ++(*mark);
9582
9583   if (die1->die_tag != die2->die_tag)
9584     return 0;
9585
9586   if (VEC_length (dw_attr_node, die1->die_attr)
9587       != VEC_length (dw_attr_node, die2->die_attr))
9588     return 0;
9589
9590   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9591     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9592       return 0;
9593
9594   c1 = die1->die_child;
9595   c2 = die2->die_child;
9596   if (! c1)
9597     {
9598       if (c2)
9599         return 0;
9600     }
9601   else
9602     for (;;)
9603       {
9604         if (!same_die_p (c1, c2, mark))
9605           return 0;
9606         c1 = c1->die_sib;
9607         c2 = c2->die_sib;
9608         if (c1 == die1->die_child)
9609           {
9610             if (c2 == die2->die_child)
9611               break;
9612             else
9613               return 0;
9614           }
9615     }
9616
9617   return 1;
9618 }
9619
9620 /* Do the dies look the same?  Wrapper around same_die_p.  */
9621
9622 static int
9623 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9624 {
9625   int mark = 0;
9626   int ret = same_die_p (die1, die2, &mark);
9627
9628   unmark_all_dies (die1);
9629   unmark_all_dies (die2);
9630
9631   return ret;
9632 }
9633
9634 /* The prefix to attach to symbols on DIEs in the current comdat debug
9635    info section.  */
9636 static char *comdat_symbol_id;
9637
9638 /* The index of the current symbol within the current comdat CU.  */
9639 static unsigned int comdat_symbol_number;
9640
9641 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9642    children, and set comdat_symbol_id accordingly.  */
9643
9644 static void
9645 compute_section_prefix (dw_die_ref unit_die)
9646 {
9647   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9648   const char *base = die_name ? lbasename (die_name) : "anonymous";
9649   char *name = XALLOCAVEC (char, strlen (base) + 64);
9650   char *p;
9651   int i, mark;
9652   unsigned char checksum[16];
9653   struct md5_ctx ctx;
9654
9655   /* Compute the checksum of the DIE, then append part of it as hex digits to
9656      the name filename of the unit.  */
9657
9658   md5_init_ctx (&ctx);
9659   mark = 0;
9660   die_checksum (unit_die, &ctx, &mark);
9661   unmark_all_dies (unit_die);
9662   md5_finish_ctx (&ctx, checksum);
9663
9664   sprintf (name, "%s.", base);
9665   clean_symbol_name (name);
9666
9667   p = name + strlen (name);
9668   for (i = 0; i < 4; i++)
9669     {
9670       sprintf (p, "%.2x", checksum[i]);
9671       p += 2;
9672     }
9673
9674   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9675   comdat_symbol_number = 0;
9676 }
9677
9678 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9679
9680 static int
9681 is_type_die (dw_die_ref die)
9682 {
9683   switch (die->die_tag)
9684     {
9685     case DW_TAG_array_type:
9686     case DW_TAG_class_type:
9687     case DW_TAG_interface_type:
9688     case DW_TAG_enumeration_type:
9689     case DW_TAG_pointer_type:
9690     case DW_TAG_reference_type:
9691     case DW_TAG_rvalue_reference_type:
9692     case DW_TAG_string_type:
9693     case DW_TAG_structure_type:
9694     case DW_TAG_subroutine_type:
9695     case DW_TAG_union_type:
9696     case DW_TAG_ptr_to_member_type:
9697     case DW_TAG_set_type:
9698     case DW_TAG_subrange_type:
9699     case DW_TAG_base_type:
9700     case DW_TAG_const_type:
9701     case DW_TAG_file_type:
9702     case DW_TAG_packed_type:
9703     case DW_TAG_volatile_type:
9704     case DW_TAG_typedef:
9705       return 1;
9706     default:
9707       return 0;
9708     }
9709 }
9710
9711 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9712    Basically, we want to choose the bits that are likely to be shared between
9713    compilations (types) and leave out the bits that are specific to individual
9714    compilations (functions).  */
9715
9716 static int
9717 is_comdat_die (dw_die_ref c)
9718 {
9719   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9720      we do for stabs.  The advantage is a greater likelihood of sharing between
9721      objects that don't include headers in the same order (and therefore would
9722      put the base types in a different comdat).  jason 8/28/00 */
9723
9724   if (c->die_tag == DW_TAG_base_type)
9725     return 0;
9726
9727   if (c->die_tag == DW_TAG_pointer_type
9728       || c->die_tag == DW_TAG_reference_type
9729       || c->die_tag == DW_TAG_rvalue_reference_type
9730       || c->die_tag == DW_TAG_const_type
9731       || c->die_tag == DW_TAG_volatile_type)
9732     {
9733       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9734
9735       return t ? is_comdat_die (t) : 0;
9736     }
9737
9738   return is_type_die (c);
9739 }
9740
9741 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9742    compilation unit.  */
9743
9744 static int
9745 is_symbol_die (dw_die_ref c)
9746 {
9747   return (is_type_die (c)
9748           || is_declaration_die (c)
9749           || c->die_tag == DW_TAG_namespace
9750           || c->die_tag == DW_TAG_module);
9751 }
9752
9753 /* Returns true iff C is a compile-unit DIE.  */
9754
9755 static inline bool
9756 is_cu_die (dw_die_ref c)
9757 {
9758   return c && c->die_tag == DW_TAG_compile_unit;
9759 }
9760
9761 static char *
9762 gen_internal_sym (const char *prefix)
9763 {
9764   char buf[256];
9765
9766   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9767   return xstrdup (buf);
9768 }
9769
9770 /* Assign symbols to all worthy DIEs under DIE.  */
9771
9772 static void
9773 assign_symbol_names (dw_die_ref die)
9774 {
9775   dw_die_ref c;
9776
9777   if (is_symbol_die (die))
9778     {
9779       if (comdat_symbol_id)
9780         {
9781           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9782
9783           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9784                    comdat_symbol_id, comdat_symbol_number++);
9785           die->die_id.die_symbol = xstrdup (p);
9786         }
9787       else
9788         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9789     }
9790
9791   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9792 }
9793
9794 struct cu_hash_table_entry
9795 {
9796   dw_die_ref cu;
9797   unsigned min_comdat_num, max_comdat_num;
9798   struct cu_hash_table_entry *next;
9799 };
9800
9801 /* Routines to manipulate hash table of CUs.  */
9802 static hashval_t
9803 htab_cu_hash (const void *of)
9804 {
9805   const struct cu_hash_table_entry *const entry =
9806     (const struct cu_hash_table_entry *) of;
9807
9808   return htab_hash_string (entry->cu->die_id.die_symbol);
9809 }
9810
9811 static int
9812 htab_cu_eq (const void *of1, const void *of2)
9813 {
9814   const struct cu_hash_table_entry *const entry1 =
9815     (const struct cu_hash_table_entry *) of1;
9816   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9817
9818   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9819 }
9820
9821 static void
9822 htab_cu_del (void *what)
9823 {
9824   struct cu_hash_table_entry *next,
9825     *entry = (struct cu_hash_table_entry *) what;
9826
9827   while (entry)
9828     {
9829       next = entry->next;
9830       free (entry);
9831       entry = next;
9832     }
9833 }
9834
9835 /* Check whether we have already seen this CU and set up SYM_NUM
9836    accordingly.  */
9837 static int
9838 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9839 {
9840   struct cu_hash_table_entry dummy;
9841   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9842
9843   dummy.max_comdat_num = 0;
9844
9845   slot = (struct cu_hash_table_entry **)
9846     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9847         INSERT);
9848   entry = *slot;
9849
9850   for (; entry; last = entry, entry = entry->next)
9851     {
9852       if (same_die_p_wrap (cu, entry->cu))
9853         break;
9854     }
9855
9856   if (entry)
9857     {
9858       *sym_num = entry->min_comdat_num;
9859       return 1;
9860     }
9861
9862   entry = XCNEW (struct cu_hash_table_entry);
9863   entry->cu = cu;
9864   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9865   entry->next = *slot;
9866   *slot = entry;
9867
9868   return 0;
9869 }
9870
9871 /* Record SYM_NUM to record of CU in HTABLE.  */
9872 static void
9873 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9874 {
9875   struct cu_hash_table_entry **slot, *entry;
9876
9877   slot = (struct cu_hash_table_entry **)
9878     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9879         NO_INSERT);
9880   entry = *slot;
9881
9882   entry->max_comdat_num = sym_num;
9883 }
9884
9885 /* Traverse the DIE (which is always comp_unit_die), and set up
9886    additional compilation units for each of the include files we see
9887    bracketed by BINCL/EINCL.  */
9888
9889 static void
9890 break_out_includes (dw_die_ref die)
9891 {
9892   dw_die_ref c;
9893   dw_die_ref unit = NULL;
9894   limbo_die_node *node, **pnode;
9895   htab_t cu_hash_table;
9896
9897   c = die->die_child;
9898   if (c) do {
9899     dw_die_ref prev = c;
9900     c = c->die_sib;
9901     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9902            || (unit && is_comdat_die (c)))
9903       {
9904         dw_die_ref next = c->die_sib;
9905
9906         /* This DIE is for a secondary CU; remove it from the main one.  */
9907         remove_child_with_prev (c, prev);
9908
9909         if (c->die_tag == DW_TAG_GNU_BINCL)
9910           unit = push_new_compile_unit (unit, c);
9911         else if (c->die_tag == DW_TAG_GNU_EINCL)
9912           unit = pop_compile_unit (unit);
9913         else
9914           add_child_die (unit, c);
9915         c = next;
9916         if (c == die->die_child)
9917           break;
9918       }
9919   } while (c != die->die_child);
9920
9921 #if 0
9922   /* We can only use this in debugging, since the frontend doesn't check
9923      to make sure that we leave every include file we enter.  */
9924   gcc_assert (!unit);
9925 #endif
9926
9927   assign_symbol_names (die);
9928   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9929   for (node = limbo_die_list, pnode = &limbo_die_list;
9930        node;
9931        node = node->next)
9932     {
9933       int is_dupl;
9934
9935       compute_section_prefix (node->die);
9936       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9937                         &comdat_symbol_number);
9938       assign_symbol_names (node->die);
9939       if (is_dupl)
9940         *pnode = node->next;
9941       else
9942         {
9943           pnode = &node->next;
9944           record_comdat_symbol_number (node->die, cu_hash_table,
9945                 comdat_symbol_number);
9946         }
9947     }
9948   htab_delete (cu_hash_table);
9949 }
9950
9951 /* Return non-zero if this DIE is a declaration.  */
9952
9953 static int
9954 is_declaration_die (dw_die_ref die)
9955 {
9956   dw_attr_ref a;
9957   unsigned ix;
9958
9959   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9960     if (a->dw_attr == DW_AT_declaration)
9961       return 1;
9962
9963   return 0;
9964 }
9965
9966 /* Return non-zero if this DIE is nested inside a subprogram.  */
9967
9968 static int
9969 is_nested_in_subprogram (dw_die_ref die)
9970 {
9971   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9972
9973   if (decl == NULL)
9974     decl = die;
9975   return local_scope_p (decl);
9976 }
9977
9978 /* Return non-zero if this is a type DIE that should be moved to a
9979    COMDAT .debug_types section.  */
9980
9981 static int
9982 should_move_die_to_comdat (dw_die_ref die)
9983 {
9984   switch (die->die_tag)
9985     {
9986     case DW_TAG_class_type:
9987     case DW_TAG_structure_type:
9988     case DW_TAG_enumeration_type:
9989     case DW_TAG_union_type:
9990       /* Don't move declarations, inlined instances, or types nested in a
9991          subprogram.  */
9992       if (is_declaration_die (die)
9993           || get_AT (die, DW_AT_abstract_origin)
9994           || is_nested_in_subprogram (die))
9995         return 0;
9996       return 1;
9997     case DW_TAG_array_type:
9998     case DW_TAG_interface_type:
9999     case DW_TAG_pointer_type:
10000     case DW_TAG_reference_type:
10001     case DW_TAG_rvalue_reference_type:
10002     case DW_TAG_string_type:
10003     case DW_TAG_subroutine_type:
10004     case DW_TAG_ptr_to_member_type:
10005     case DW_TAG_set_type:
10006     case DW_TAG_subrange_type:
10007     case DW_TAG_base_type:
10008     case DW_TAG_const_type:
10009     case DW_TAG_file_type:
10010     case DW_TAG_packed_type:
10011     case DW_TAG_volatile_type:
10012     case DW_TAG_typedef:
10013     default:
10014       return 0;
10015     }
10016 }
10017
10018 /* Make a clone of DIE.  */
10019
10020 static dw_die_ref
10021 clone_die (dw_die_ref die)
10022 {
10023   dw_die_ref clone;
10024   dw_attr_ref a;
10025   unsigned ix;
10026
10027   clone = ggc_alloc_cleared_die_node ();
10028   clone->die_tag = die->die_tag;
10029
10030   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10031     add_dwarf_attr (clone, a);
10032
10033   return clone;
10034 }
10035
10036 /* Make a clone of the tree rooted at DIE.  */
10037
10038 static dw_die_ref
10039 clone_tree (dw_die_ref die)
10040 {
10041   dw_die_ref c;
10042   dw_die_ref clone = clone_die (die);
10043
10044   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10045
10046   return clone;
10047 }
10048
10049 /* Make a clone of DIE as a declaration.  */
10050
10051 static dw_die_ref
10052 clone_as_declaration (dw_die_ref die)
10053 {
10054   dw_die_ref clone;
10055   dw_die_ref decl;
10056   dw_attr_ref a;
10057   unsigned ix;
10058
10059   /* If the DIE is already a declaration, just clone it.  */
10060   if (is_declaration_die (die))
10061     return clone_die (die);
10062
10063   /* If the DIE is a specification, just clone its declaration DIE.  */
10064   decl = get_AT_ref (die, DW_AT_specification);
10065   if (decl != NULL)
10066     return clone_die (decl);
10067
10068   clone = ggc_alloc_cleared_die_node ();
10069   clone->die_tag = die->die_tag;
10070
10071   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10072     {
10073       /* We don't want to copy over all attributes.
10074          For example we don't want DW_AT_byte_size because otherwise we will no
10075          longer have a declaration and GDB will treat it as a definition.  */
10076
10077       switch (a->dw_attr)
10078         {
10079         case DW_AT_artificial:
10080         case DW_AT_containing_type:
10081         case DW_AT_external:
10082         case DW_AT_name:
10083         case DW_AT_type:
10084         case DW_AT_virtuality:
10085         case DW_AT_linkage_name:
10086         case DW_AT_MIPS_linkage_name:
10087           add_dwarf_attr (clone, a);
10088           break;
10089         case DW_AT_byte_size:
10090         default:
10091           break;
10092         }
10093     }
10094
10095   if (die->die_id.die_type_node)
10096     add_AT_die_ref (clone, DW_AT_signature, die);
10097
10098   add_AT_flag (clone, DW_AT_declaration, 1);
10099   return clone;
10100 }
10101
10102 /* Copy the declaration context to the new compile unit DIE.  This includes
10103    any surrounding namespace or type declarations.  If the DIE has an
10104    AT_specification attribute, it also includes attributes and children
10105    attached to the specification.  */
10106
10107 static void
10108 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10109 {
10110   dw_die_ref decl;
10111   dw_die_ref new_decl;
10112
10113   decl = get_AT_ref (die, DW_AT_specification);
10114   if (decl == NULL)
10115     decl = die;
10116   else
10117     {
10118       unsigned ix;
10119       dw_die_ref c;
10120       dw_attr_ref a;
10121
10122       /* Copy the type node pointer from the new DIE to the original
10123          declaration DIE so we can forward references later.  */
10124       decl->die_id.die_type_node = die->die_id.die_type_node;
10125
10126       remove_AT (die, DW_AT_specification);
10127
10128       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10129         {
10130           if (a->dw_attr != DW_AT_name
10131               && a->dw_attr != DW_AT_declaration
10132               && a->dw_attr != DW_AT_external)
10133             add_dwarf_attr (die, a);
10134         }
10135
10136       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10137     }
10138
10139   if (decl->die_parent != NULL
10140       && decl->die_parent->die_tag != DW_TAG_compile_unit
10141       && decl->die_parent->die_tag != DW_TAG_type_unit)
10142     {
10143       new_decl = copy_ancestor_tree (unit, decl, NULL);
10144       if (new_decl != NULL)
10145         {
10146           remove_AT (new_decl, DW_AT_signature);
10147           add_AT_specification (die, new_decl);
10148         }
10149     }
10150 }
10151
10152 /* Generate the skeleton ancestor tree for the given NODE, then clone
10153    the DIE and add the clone into the tree.  */
10154
10155 static void
10156 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10157 {
10158   if (node->new_die != NULL)
10159     return;
10160
10161   node->new_die = clone_as_declaration (node->old_die);
10162
10163   if (node->parent != NULL)
10164     {
10165       generate_skeleton_ancestor_tree (node->parent);
10166       add_child_die (node->parent->new_die, node->new_die);
10167     }
10168 }
10169
10170 /* Generate a skeleton tree of DIEs containing any declarations that are
10171    found in the original tree.  We traverse the tree looking for declaration
10172    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10173
10174 static void
10175 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10176 {
10177   skeleton_chain_node node;
10178   dw_die_ref c;
10179   dw_die_ref first;
10180   dw_die_ref prev = NULL;
10181   dw_die_ref next = NULL;
10182
10183   node.parent = parent;
10184
10185   first = c = parent->old_die->die_child;
10186   if (c)
10187     next = c->die_sib;
10188   if (c) do {
10189     if (prev == NULL || prev->die_sib == c)
10190       prev = c;
10191     c = next;
10192     next = (c == first ? NULL : c->die_sib);
10193     node.old_die = c;
10194     node.new_die = NULL;
10195     if (is_declaration_die (c))
10196       {
10197         /* Clone the existing DIE, move the original to the skeleton
10198            tree (which is in the main CU), and put the clone, with
10199            all the original's children, where the original came from.  */
10200         dw_die_ref clone = clone_die (c);
10201         move_all_children (c, clone);
10202
10203         replace_child (c, clone, prev);
10204         generate_skeleton_ancestor_tree (parent);
10205         add_child_die (parent->new_die, c);
10206         node.new_die = c;
10207         c = clone;
10208       }
10209     generate_skeleton_bottom_up (&node);
10210   } while (next != NULL);
10211 }
10212
10213 /* Wrapper function for generate_skeleton_bottom_up.  */
10214
10215 static dw_die_ref
10216 generate_skeleton (dw_die_ref die)
10217 {
10218   skeleton_chain_node node;
10219
10220   node.old_die = die;
10221   node.new_die = NULL;
10222   node.parent = NULL;
10223
10224   /* If this type definition is nested inside another type,
10225      always leave at least a declaration in its place.  */
10226   if (die->die_parent != NULL && is_type_die (die->die_parent))
10227     node.new_die = clone_as_declaration (die);
10228
10229   generate_skeleton_bottom_up (&node);
10230   return node.new_die;
10231 }
10232
10233 /* Remove the DIE from its parent, possibly replacing it with a cloned
10234    declaration.  The original DIE will be moved to a new compile unit
10235    so that existing references to it follow it to the new location.  If
10236    any of the original DIE's descendants is a declaration, we need to
10237    replace the original DIE with a skeleton tree and move the
10238    declarations back into the skeleton tree.  */
10239
10240 static dw_die_ref
10241 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10242 {
10243   dw_die_ref skeleton;
10244
10245   skeleton = generate_skeleton (child);
10246   if (skeleton == NULL)
10247     remove_child_with_prev (child, prev);
10248   else
10249     {
10250       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10251       replace_child (child, skeleton, prev);
10252     }
10253
10254   return skeleton;
10255 }
10256
10257 /* Traverse the DIE and set up additional .debug_types sections for each
10258    type worthy of being placed in a COMDAT section.  */
10259
10260 static void
10261 break_out_comdat_types (dw_die_ref die)
10262 {
10263   dw_die_ref c;
10264   dw_die_ref first;
10265   dw_die_ref prev = NULL;
10266   dw_die_ref next = NULL;
10267   dw_die_ref unit = NULL;
10268
10269   first = c = die->die_child;
10270   if (c)
10271     next = c->die_sib;
10272   if (c) do {
10273     if (prev == NULL || prev->die_sib == c)
10274       prev = c;
10275     c = next;
10276     next = (c == first ? NULL : c->die_sib);
10277     if (should_move_die_to_comdat (c))
10278       {
10279         dw_die_ref replacement;
10280         comdat_type_node_ref type_node;
10281
10282         /* Create a new type unit DIE as the root for the new tree, and
10283            add it to the list of comdat types.  */
10284         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10285         add_AT_unsigned (unit, DW_AT_language,
10286                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10287         type_node = ggc_alloc_cleared_comdat_type_node ();
10288         type_node->root_die = unit;
10289         type_node->next = comdat_type_list;
10290         comdat_type_list = type_node;
10291
10292         /* Generate the type signature.  */
10293         generate_type_signature (c, type_node);
10294
10295         /* Copy the declaration context, attributes, and children of the
10296            declaration into the new compile unit DIE.  */
10297         copy_declaration_context (unit, c);
10298
10299         /* Remove this DIE from the main CU.  */
10300         replacement = remove_child_or_replace_with_skeleton (c, prev);
10301
10302         /* Break out nested types into their own type units.  */
10303         break_out_comdat_types (c);
10304
10305         /* Add the DIE to the new compunit.  */
10306         add_child_die (unit, c);
10307
10308         if (replacement != NULL)
10309           c = replacement;
10310       }
10311     else if (c->die_tag == DW_TAG_namespace
10312              || c->die_tag == DW_TAG_class_type
10313              || c->die_tag == DW_TAG_structure_type
10314              || c->die_tag == DW_TAG_union_type)
10315       {
10316         /* Look for nested types that can be broken out.  */
10317         break_out_comdat_types (c);
10318       }
10319   } while (next != NULL);
10320 }
10321
10322 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10323
10324 struct decl_table_entry
10325 {
10326   dw_die_ref orig;
10327   dw_die_ref copy;
10328 };
10329
10330 /* Routines to manipulate hash table of copied declarations.  */
10331
10332 static hashval_t
10333 htab_decl_hash (const void *of)
10334 {
10335   const struct decl_table_entry *const entry =
10336     (const struct decl_table_entry *) of;
10337
10338   return htab_hash_pointer (entry->orig);
10339 }
10340
10341 static int
10342 htab_decl_eq (const void *of1, const void *of2)
10343 {
10344   const struct decl_table_entry *const entry1 =
10345     (const struct decl_table_entry *) of1;
10346   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10347
10348   return entry1->orig == entry2;
10349 }
10350
10351 static void
10352 htab_decl_del (void *what)
10353 {
10354   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10355
10356   free (entry);
10357 }
10358
10359 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10360    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10361    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10362    to check if the ancestor has already been copied into UNIT.  */
10363
10364 static dw_die_ref
10365 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10366 {
10367   dw_die_ref parent = die->die_parent;
10368   dw_die_ref new_parent = unit;
10369   dw_die_ref copy;
10370   void **slot = NULL;
10371   struct decl_table_entry *entry = NULL;
10372
10373   if (decl_table)
10374     {
10375       /* Check if the entry has already been copied to UNIT.  */
10376       slot = htab_find_slot_with_hash (decl_table, die,
10377                                        htab_hash_pointer (die), INSERT);
10378       if (*slot != HTAB_EMPTY_ENTRY)
10379         {
10380           entry = (struct decl_table_entry *) *slot;
10381           return entry->copy;
10382         }
10383
10384       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10385       entry = XCNEW (struct decl_table_entry);
10386       entry->orig = die;
10387       entry->copy = NULL;
10388       *slot = entry;
10389     }
10390
10391   if (parent != NULL)
10392     {
10393       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10394       if (spec != NULL)
10395         parent = spec;
10396       if (parent->die_tag != DW_TAG_compile_unit
10397           && parent->die_tag != DW_TAG_type_unit)
10398         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10399     }
10400
10401   copy = clone_as_declaration (die);
10402   add_child_die (new_parent, copy);
10403
10404   if (decl_table != NULL)
10405     {
10406       /* Record the pointer to the copy.  */
10407       entry->copy = copy;
10408     }
10409
10410   return copy;
10411 }
10412
10413 /* Like clone_tree, but additionally enter all the children into
10414    the hash table decl_table.  */
10415
10416 static dw_die_ref
10417 clone_tree_hash (dw_die_ref die, htab_t decl_table)
10418 {
10419   dw_die_ref c;
10420   dw_die_ref clone = clone_die (die);
10421   struct decl_table_entry *entry;
10422   void **slot = htab_find_slot_with_hash (decl_table, die,
10423                                           htab_hash_pointer (die), INSERT);
10424   /* Assert that DIE isn't in the hash table yet.  If it would be there
10425      before, the ancestors would be necessarily there as well, therefore
10426      clone_tree_hash wouldn't be called.  */
10427   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
10428   entry = XCNEW (struct decl_table_entry);
10429   entry->orig = die;
10430   entry->copy = clone;
10431   *slot = entry;
10432
10433   FOR_EACH_CHILD (die, c,
10434                   add_child_die (clone, clone_tree_hash (c, decl_table)));
10435
10436   return clone;
10437 }
10438
10439 /* Walk the DIE and its children, looking for references to incomplete
10440    or trivial types that are unmarked (i.e., that are not in the current
10441    type_unit).  */
10442
10443 static void
10444 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10445 {
10446   dw_die_ref c;
10447   dw_attr_ref a;
10448   unsigned ix;
10449
10450   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10451     {
10452       if (AT_class (a) == dw_val_class_die_ref)
10453         {
10454           dw_die_ref targ = AT_ref (a);
10455           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10456           void **slot;
10457           struct decl_table_entry *entry;
10458
10459           if (targ->die_mark != 0 || type_node != NULL)
10460             continue;
10461
10462           slot = htab_find_slot_with_hash (decl_table, targ,
10463                                            htab_hash_pointer (targ), INSERT);
10464
10465           if (*slot != HTAB_EMPTY_ENTRY)
10466             {
10467               /* TARG has already been copied, so we just need to
10468                  modify the reference to point to the copy.  */
10469               entry = (struct decl_table_entry *) *slot;
10470               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10471             }
10472           else
10473             {
10474               dw_die_ref parent = unit;
10475               dw_die_ref copy = clone_die (targ);
10476
10477               /* Record in DECL_TABLE that TARG has been copied.
10478                  Need to do this now, before the recursive call,
10479                  because DECL_TABLE may be expanded and SLOT
10480                  would no longer be a valid pointer.  */
10481               entry = XCNEW (struct decl_table_entry);
10482               entry->orig = targ;
10483               entry->copy = copy;
10484               *slot = entry;
10485
10486               FOR_EACH_CHILD (targ, c,
10487                               add_child_die (copy,
10488                                              clone_tree_hash (c, decl_table)));
10489
10490               /* Make sure the cloned tree is marked as part of the
10491                  type unit.  */
10492               mark_dies (copy);
10493
10494               /* If TARG has surrounding context, copy its ancestor tree
10495                  into the new type unit.  */
10496               if (targ->die_parent != NULL
10497                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10498                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10499                 parent = copy_ancestor_tree (unit, targ->die_parent,
10500                                              decl_table);
10501
10502               add_child_die (parent, copy);
10503               a->dw_attr_val.v.val_die_ref.die = copy;
10504
10505               /* Make sure the newly-copied DIE is walked.  If it was
10506                  installed in a previously-added context, it won't
10507                  get visited otherwise.  */
10508               if (parent != unit)
10509                 {
10510                   /* Find the highest point of the newly-added tree,
10511                      mark each node along the way, and walk from there.  */
10512                   parent->die_mark = 1;
10513                   while (parent->die_parent
10514                          && parent->die_parent->die_mark == 0)
10515                     {
10516                       parent = parent->die_parent;
10517                       parent->die_mark = 1;
10518                     }
10519                   copy_decls_walk (unit, parent, decl_table);
10520                 }
10521             }
10522         }
10523     }
10524
10525   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10526 }
10527
10528 /* Copy declarations for "unworthy" types into the new comdat section.
10529    Incomplete types, modified types, and certain other types aren't broken
10530    out into comdat sections of their own, so they don't have a signature,
10531    and we need to copy the declaration into the same section so that we
10532    don't have an external reference.  */
10533
10534 static void
10535 copy_decls_for_unworthy_types (dw_die_ref unit)
10536 {
10537   htab_t decl_table;
10538
10539   mark_dies (unit);
10540   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10541   copy_decls_walk (unit, unit, decl_table);
10542   htab_delete (decl_table);
10543   unmark_dies (unit);
10544 }
10545
10546 /* Traverse the DIE and add a sibling attribute if it may have the
10547    effect of speeding up access to siblings.  To save some space,
10548    avoid generating sibling attributes for DIE's without children.  */
10549
10550 static void
10551 add_sibling_attributes (dw_die_ref die)
10552 {
10553   dw_die_ref c;
10554
10555   if (! die->die_child)
10556     return;
10557
10558   if (die->die_parent && die != die->die_parent->die_child)
10559     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10560
10561   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10562 }
10563
10564 /* Output all location lists for the DIE and its children.  */
10565
10566 static void
10567 output_location_lists (dw_die_ref die)
10568 {
10569   dw_die_ref c;
10570   dw_attr_ref a;
10571   unsigned ix;
10572
10573   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10574     if (AT_class (a) == dw_val_class_loc_list)
10575       output_loc_list (AT_loc_list (a));
10576
10577   FOR_EACH_CHILD (die, c, output_location_lists (c));
10578 }
10579
10580 /* The format of each DIE (and its attribute value pairs) is encoded in an
10581    abbreviation table.  This routine builds the abbreviation table and assigns
10582    a unique abbreviation id for each abbreviation entry.  The children of each
10583    die are visited recursively.  */
10584
10585 static void
10586 build_abbrev_table (dw_die_ref die)
10587 {
10588   unsigned long abbrev_id;
10589   unsigned int n_alloc;
10590   dw_die_ref c;
10591   dw_attr_ref a;
10592   unsigned ix;
10593
10594   /* Scan the DIE references, and mark as external any that refer to
10595      DIEs from other CUs (i.e. those which are not marked).  */
10596   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10597     if (AT_class (a) == dw_val_class_die_ref
10598         && AT_ref (a)->die_mark == 0)
10599       {
10600         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10601         set_AT_ref_external (a, 1);
10602       }
10603
10604   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10605     {
10606       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10607       dw_attr_ref die_a, abbrev_a;
10608       unsigned ix;
10609       bool ok = true;
10610
10611       if (abbrev->die_tag != die->die_tag)
10612         continue;
10613       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10614         continue;
10615
10616       if (VEC_length (dw_attr_node, abbrev->die_attr)
10617           != VEC_length (dw_attr_node, die->die_attr))
10618         continue;
10619
10620       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10621         {
10622           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10623           if ((abbrev_a->dw_attr != die_a->dw_attr)
10624               || (value_format (abbrev_a) != value_format (die_a)))
10625             {
10626               ok = false;
10627               break;
10628             }
10629         }
10630       if (ok)
10631         break;
10632     }
10633
10634   if (abbrev_id >= abbrev_die_table_in_use)
10635     {
10636       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10637         {
10638           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10639           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10640                                             n_alloc);
10641
10642           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10643                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10644           abbrev_die_table_allocated = n_alloc;
10645         }
10646
10647       ++abbrev_die_table_in_use;
10648       abbrev_die_table[abbrev_id] = die;
10649     }
10650
10651   die->die_abbrev = abbrev_id;
10652   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10653 }
10654 \f
10655 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10656
10657 static int
10658 constant_size (unsigned HOST_WIDE_INT value)
10659 {
10660   int log;
10661
10662   if (value == 0)
10663     log = 0;
10664   else
10665     log = floor_log2 (value);
10666
10667   log = log / 8;
10668   log = 1 << (floor_log2 (log) + 1);
10669
10670   return log;
10671 }
10672
10673 /* Return the size of a DIE as it is represented in the
10674    .debug_info section.  */
10675
10676 static unsigned long
10677 size_of_die (dw_die_ref die)
10678 {
10679   unsigned long size = 0;
10680   dw_attr_ref a;
10681   unsigned ix;
10682
10683   size += size_of_uleb128 (die->die_abbrev);
10684   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10685     {
10686       switch (AT_class (a))
10687         {
10688         case dw_val_class_addr:
10689           size += DWARF2_ADDR_SIZE;
10690           break;
10691         case dw_val_class_offset:
10692           size += DWARF_OFFSET_SIZE;
10693           break;
10694         case dw_val_class_loc:
10695           {
10696             unsigned long lsize = size_of_locs (AT_loc (a));
10697
10698             /* Block length.  */
10699             if (dwarf_version >= 4)
10700               size += size_of_uleb128 (lsize);
10701             else
10702               size += constant_size (lsize);
10703             size += lsize;
10704           }
10705           break;
10706         case dw_val_class_loc_list:
10707           size += DWARF_OFFSET_SIZE;
10708           break;
10709         case dw_val_class_range_list:
10710           size += DWARF_OFFSET_SIZE;
10711           break;
10712         case dw_val_class_const:
10713           size += size_of_sleb128 (AT_int (a));
10714           break;
10715         case dw_val_class_unsigned_const:
10716           {
10717             int csize = constant_size (AT_unsigned (a));
10718             if (dwarf_version == 3
10719                 && a->dw_attr == DW_AT_data_member_location
10720                 && csize >= 4)
10721               size += size_of_uleb128 (AT_unsigned (a));
10722             else
10723               size += csize;
10724           }
10725           break;
10726         case dw_val_class_const_double:
10727           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10728           if (HOST_BITS_PER_WIDE_INT >= 64)
10729             size++; /* block */
10730           break;
10731         case dw_val_class_vec:
10732           size += constant_size (a->dw_attr_val.v.val_vec.length
10733                                  * a->dw_attr_val.v.val_vec.elt_size)
10734                   + a->dw_attr_val.v.val_vec.length
10735                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10736           break;
10737         case dw_val_class_flag:
10738           if (dwarf_version >= 4)
10739             /* Currently all add_AT_flag calls pass in 1 as last argument,
10740                so DW_FORM_flag_present can be used.  If that ever changes,
10741                we'll need to use DW_FORM_flag and have some optimization
10742                in build_abbrev_table that will change those to
10743                DW_FORM_flag_present if it is set to 1 in all DIEs using
10744                the same abbrev entry.  */
10745             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10746           else
10747             size += 1;
10748           break;
10749         case dw_val_class_die_ref:
10750           if (AT_ref_external (a))
10751             {
10752               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10753                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10754                  is sized by target address length, whereas in DWARF3
10755                  it's always sized as an offset.  */
10756               if (dwarf_version >= 4)
10757                 size += DWARF_TYPE_SIGNATURE_SIZE;
10758               else if (dwarf_version == 2)
10759                 size += DWARF2_ADDR_SIZE;
10760               else
10761                 size += DWARF_OFFSET_SIZE;
10762             }
10763           else
10764             size += DWARF_OFFSET_SIZE;
10765           break;
10766         case dw_val_class_fde_ref:
10767           size += DWARF_OFFSET_SIZE;
10768           break;
10769         case dw_val_class_lbl_id:
10770           size += DWARF2_ADDR_SIZE;
10771           break;
10772         case dw_val_class_lineptr:
10773         case dw_val_class_macptr:
10774           size += DWARF_OFFSET_SIZE;
10775           break;
10776         case dw_val_class_str:
10777           if (AT_string_form (a) == DW_FORM_strp)
10778             size += DWARF_OFFSET_SIZE;
10779           else
10780             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10781           break;
10782         case dw_val_class_file:
10783           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10784           break;
10785         case dw_val_class_data8:
10786           size += 8;
10787           break;
10788         case dw_val_class_vms_delta:
10789           size += DWARF_OFFSET_SIZE;
10790           break;
10791         default:
10792           gcc_unreachable ();
10793         }
10794     }
10795
10796   return size;
10797 }
10798
10799 /* Size the debugging information associated with a given DIE.  Visits the
10800    DIE's children recursively.  Updates the global variable next_die_offset, on
10801    each time through.  Uses the current value of next_die_offset to update the
10802    die_offset field in each DIE.  */
10803
10804 static void
10805 calc_die_sizes (dw_die_ref die)
10806 {
10807   dw_die_ref c;
10808
10809   die->die_offset = next_die_offset;
10810   next_die_offset += size_of_die (die);
10811
10812   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10813
10814   if (die->die_child != NULL)
10815     /* Count the null byte used to terminate sibling lists.  */
10816     next_die_offset += 1;
10817 }
10818
10819 /* Set the marks for a die and its children.  We do this so
10820    that we know whether or not a reference needs to use FORM_ref_addr; only
10821    DIEs in the same CU will be marked.  We used to clear out the offset
10822    and use that as the flag, but ran into ordering problems.  */
10823
10824 static void
10825 mark_dies (dw_die_ref die)
10826 {
10827   dw_die_ref c;
10828
10829   gcc_assert (!die->die_mark);
10830
10831   die->die_mark = 1;
10832   FOR_EACH_CHILD (die, c, mark_dies (c));
10833 }
10834
10835 /* Clear the marks for a die and its children.  */
10836
10837 static void
10838 unmark_dies (dw_die_ref die)
10839 {
10840   dw_die_ref c;
10841
10842   if (dwarf_version < 4)
10843     gcc_assert (die->die_mark);
10844
10845   die->die_mark = 0;
10846   FOR_EACH_CHILD (die, c, unmark_dies (c));
10847 }
10848
10849 /* Clear the marks for a die, its children and referred dies.  */
10850
10851 static void
10852 unmark_all_dies (dw_die_ref die)
10853 {
10854   dw_die_ref c;
10855   dw_attr_ref a;
10856   unsigned ix;
10857
10858   if (!die->die_mark)
10859     return;
10860   die->die_mark = 0;
10861
10862   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10863
10864   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10865     if (AT_class (a) == dw_val_class_die_ref)
10866       unmark_all_dies (AT_ref (a));
10867 }
10868
10869 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10870    generated for the compilation unit.  */
10871
10872 static unsigned long
10873 size_of_pubnames (VEC (pubname_entry, gc) * names)
10874 {
10875   unsigned long size;
10876   unsigned i;
10877   pubname_ref p;
10878
10879   size = DWARF_PUBNAMES_HEADER_SIZE;
10880   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10881     if (names != pubtype_table
10882         || p->die->die_offset != 0
10883         || !flag_eliminate_unused_debug_types)
10884       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10885
10886   size += DWARF_OFFSET_SIZE;
10887   return size;
10888 }
10889
10890 /* Return the size of the information in the .debug_aranges section.  */
10891
10892 static unsigned long
10893 size_of_aranges (void)
10894 {
10895   unsigned long size;
10896
10897   size = DWARF_ARANGES_HEADER_SIZE;
10898
10899   /* Count the address/length pair for this compilation unit.  */
10900   if (text_section_used)
10901     size += 2 * DWARF2_ADDR_SIZE;
10902   if (cold_text_section_used)
10903     size += 2 * DWARF2_ADDR_SIZE;
10904   if (have_multiple_function_sections)
10905     {
10906       unsigned fde_idx = 0;
10907
10908       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
10909         {
10910           dw_fde_ref fde = &fde_table[fde_idx];
10911
10912           if (!fde->in_std_section)
10913             size += 2 * DWARF2_ADDR_SIZE;
10914           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10915             size += 2 * DWARF2_ADDR_SIZE;
10916         }
10917     }
10918
10919   /* Count the two zero words used to terminated the address range table.  */
10920   size += 2 * DWARF2_ADDR_SIZE;
10921   return size;
10922 }
10923 \f
10924 /* Select the encoding of an attribute value.  */
10925
10926 static enum dwarf_form
10927 value_format (dw_attr_ref a)
10928 {
10929   switch (a->dw_attr_val.val_class)
10930     {
10931     case dw_val_class_addr:
10932       /* Only very few attributes allow DW_FORM_addr.  */
10933       switch (a->dw_attr)
10934         {
10935         case DW_AT_low_pc:
10936         case DW_AT_high_pc:
10937         case DW_AT_entry_pc:
10938         case DW_AT_trampoline:
10939           return DW_FORM_addr;
10940         default:
10941           break;
10942         }
10943       switch (DWARF2_ADDR_SIZE)
10944         {
10945         case 1:
10946           return DW_FORM_data1;
10947         case 2:
10948           return DW_FORM_data2;
10949         case 4:
10950           return DW_FORM_data4;
10951         case 8:
10952           return DW_FORM_data8;
10953         default:
10954           gcc_unreachable ();
10955         }
10956     case dw_val_class_range_list:
10957     case dw_val_class_loc_list:
10958       if (dwarf_version >= 4)
10959         return DW_FORM_sec_offset;
10960       /* FALLTHRU */
10961     case dw_val_class_vms_delta:
10962     case dw_val_class_offset:
10963       switch (DWARF_OFFSET_SIZE)
10964         {
10965         case 4:
10966           return DW_FORM_data4;
10967         case 8:
10968           return DW_FORM_data8;
10969         default:
10970           gcc_unreachable ();
10971         }
10972     case dw_val_class_loc:
10973       if (dwarf_version >= 4)
10974         return DW_FORM_exprloc;
10975       switch (constant_size (size_of_locs (AT_loc (a))))
10976         {
10977         case 1:
10978           return DW_FORM_block1;
10979         case 2:
10980           return DW_FORM_block2;
10981         default:
10982           gcc_unreachable ();
10983         }
10984     case dw_val_class_const:
10985       return DW_FORM_sdata;
10986     case dw_val_class_unsigned_const:
10987       switch (constant_size (AT_unsigned (a)))
10988         {
10989         case 1:
10990           return DW_FORM_data1;
10991         case 2:
10992           return DW_FORM_data2;
10993         case 4:
10994           /* In DWARF3 DW_AT_data_member_location with
10995              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
10996              constant, so we need to use DW_FORM_udata if we need
10997              a large constant.  */
10998           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10999             return DW_FORM_udata;
11000           return DW_FORM_data4;
11001         case 8:
11002           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
11003             return DW_FORM_udata;
11004           return DW_FORM_data8;
11005         default:
11006           gcc_unreachable ();
11007         }
11008     case dw_val_class_const_double:
11009       switch (HOST_BITS_PER_WIDE_INT)
11010         {
11011         case 8:
11012           return DW_FORM_data2;
11013         case 16:
11014           return DW_FORM_data4;
11015         case 32:
11016           return DW_FORM_data8;
11017         case 64:
11018         default:
11019           return DW_FORM_block1;
11020         }
11021     case dw_val_class_vec:
11022       switch (constant_size (a->dw_attr_val.v.val_vec.length
11023                              * a->dw_attr_val.v.val_vec.elt_size))
11024         {
11025         case 1:
11026           return DW_FORM_block1;
11027         case 2:
11028           return DW_FORM_block2;
11029         case 4:
11030           return DW_FORM_block4;
11031         default:
11032           gcc_unreachable ();
11033         }
11034     case dw_val_class_flag:
11035       if (dwarf_version >= 4)
11036         {
11037           /* Currently all add_AT_flag calls pass in 1 as last argument,
11038              so DW_FORM_flag_present can be used.  If that ever changes,
11039              we'll need to use DW_FORM_flag and have some optimization
11040              in build_abbrev_table that will change those to
11041              DW_FORM_flag_present if it is set to 1 in all DIEs using
11042              the same abbrev entry.  */
11043           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11044           return DW_FORM_flag_present;
11045         }
11046       return DW_FORM_flag;
11047     case dw_val_class_die_ref:
11048       if (AT_ref_external (a))
11049         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
11050       else
11051         return DW_FORM_ref;
11052     case dw_val_class_fde_ref:
11053       return DW_FORM_data;
11054     case dw_val_class_lbl_id:
11055       return DW_FORM_addr;
11056     case dw_val_class_lineptr:
11057     case dw_val_class_macptr:
11058       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11059     case dw_val_class_str:
11060       return AT_string_form (a);
11061     case dw_val_class_file:
11062       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11063         {
11064         case 1:
11065           return DW_FORM_data1;
11066         case 2:
11067           return DW_FORM_data2;
11068         case 4:
11069           return DW_FORM_data4;
11070         default:
11071           gcc_unreachable ();
11072         }
11073
11074     case dw_val_class_data8:
11075       return DW_FORM_data8;
11076
11077     default:
11078       gcc_unreachable ();
11079     }
11080 }
11081
11082 /* Output the encoding of an attribute value.  */
11083
11084 static void
11085 output_value_format (dw_attr_ref a)
11086 {
11087   enum dwarf_form form = value_format (a);
11088
11089   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11090 }
11091
11092 /* Output the .debug_abbrev section which defines the DIE abbreviation
11093    table.  */
11094
11095 static void
11096 output_abbrev_section (void)
11097 {
11098   unsigned long abbrev_id;
11099
11100   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11101     {
11102       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11103       unsigned ix;
11104       dw_attr_ref a_attr;
11105
11106       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11107       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11108                                    dwarf_tag_name (abbrev->die_tag));
11109
11110       if (abbrev->die_child != NULL)
11111         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11112       else
11113         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11114
11115       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11116            ix++)
11117         {
11118           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11119                                        dwarf_attr_name (a_attr->dw_attr));
11120           output_value_format (a_attr);
11121         }
11122
11123       dw2_asm_output_data (1, 0, NULL);
11124       dw2_asm_output_data (1, 0, NULL);
11125     }
11126
11127   /* Terminate the table.  */
11128   dw2_asm_output_data (1, 0, NULL);
11129 }
11130
11131 /* Output a symbol we can use to refer to this DIE from another CU.  */
11132
11133 static inline void
11134 output_die_symbol (dw_die_ref die)
11135 {
11136   char *sym = die->die_id.die_symbol;
11137
11138   if (sym == 0)
11139     return;
11140
11141   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11142     /* We make these global, not weak; if the target doesn't support
11143        .linkonce, it doesn't support combining the sections, so debugging
11144        will break.  */
11145     targetm.asm_out.globalize_label (asm_out_file, sym);
11146
11147   ASM_OUTPUT_LABEL (asm_out_file, sym);
11148 }
11149
11150 /* Return a new location list, given the begin and end range, and the
11151    expression.  */
11152
11153 static inline dw_loc_list_ref
11154 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11155               const char *section)
11156 {
11157   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11158
11159   retlist->begin = begin;
11160   retlist->end = end;
11161   retlist->expr = expr;
11162   retlist->section = section;
11163
11164   return retlist;
11165 }
11166
11167 /* Generate a new internal symbol for this location list node, if it
11168    hasn't got one yet.  */
11169
11170 static inline void
11171 gen_llsym (dw_loc_list_ref list)
11172 {
11173   gcc_assert (!list->ll_symbol);
11174   list->ll_symbol = gen_internal_sym ("LLST");
11175 }
11176
11177 /* Output the location list given to us.  */
11178
11179 static void
11180 output_loc_list (dw_loc_list_ref list_head)
11181 {
11182   dw_loc_list_ref curr = list_head;
11183
11184   if (list_head->emitted)
11185     return;
11186   list_head->emitted = true;
11187
11188   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11189
11190   /* Walk the location list, and output each range + expression.  */
11191   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11192     {
11193       unsigned long size;
11194       /* Don't output an entry that starts and ends at the same address.  */
11195       if (strcmp (curr->begin, curr->end) == 0)
11196         continue;
11197       size = size_of_locs (curr->expr);
11198       /* If the expression is too large, drop it on the floor.  We could
11199          perhaps put it into DW_TAG_dwarf_procedure and refer to that
11200          in the expression, but >= 64KB expressions for a single value
11201          in a single range are unlikely very useful.  */
11202       if (size > 0xffff)
11203         continue;
11204       if (!have_multiple_function_sections)
11205         {
11206           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11207                                 "Location list begin address (%s)",
11208                                 list_head->ll_symbol);
11209           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11210                                 "Location list end address (%s)",
11211                                 list_head->ll_symbol);
11212         }
11213       else
11214         {
11215           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11216                                "Location list begin address (%s)",
11217                                list_head->ll_symbol);
11218           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11219                                "Location list end address (%s)",
11220                                list_head->ll_symbol);
11221         }
11222
11223       /* Output the block length for this list of location operations.  */
11224       gcc_assert (size <= 0xffff);
11225       dw2_asm_output_data (2, size, "%s", "Location expression size");
11226
11227       output_loc_sequence (curr->expr, -1);
11228     }
11229
11230   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11231                        "Location list terminator begin (%s)",
11232                        list_head->ll_symbol);
11233   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11234                        "Location list terminator end (%s)",
11235                        list_head->ll_symbol);
11236 }
11237
11238 /* Output a type signature.  */
11239
11240 static inline void
11241 output_signature (const char *sig, const char *name)
11242 {
11243   int i;
11244
11245   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11246     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11247 }
11248
11249 /* Output the DIE and its attributes.  Called recursively to generate
11250    the definitions of each child DIE.  */
11251
11252 static void
11253 output_die (dw_die_ref die)
11254 {
11255   dw_attr_ref a;
11256   dw_die_ref c;
11257   unsigned long size;
11258   unsigned ix;
11259
11260   /* If someone in another CU might refer to us, set up a symbol for
11261      them to point to.  */
11262   if (dwarf_version < 4 && die->die_id.die_symbol)
11263     output_die_symbol (die);
11264
11265   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11266                                (unsigned long)die->die_offset,
11267                                dwarf_tag_name (die->die_tag));
11268
11269   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11270     {
11271       const char *name = dwarf_attr_name (a->dw_attr);
11272
11273       switch (AT_class (a))
11274         {
11275         case dw_val_class_addr:
11276           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11277           break;
11278
11279         case dw_val_class_offset:
11280           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11281                                "%s", name);
11282           break;
11283
11284         case dw_val_class_range_list:
11285           {
11286             char *p = strchr (ranges_section_label, '\0');
11287
11288             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11289                      a->dw_attr_val.v.val_offset);
11290             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11291                                    debug_ranges_section, "%s", name);
11292             *p = '\0';
11293           }
11294           break;
11295
11296         case dw_val_class_loc:
11297           size = size_of_locs (AT_loc (a));
11298
11299           /* Output the block length for this list of location operations.  */
11300           if (dwarf_version >= 4)
11301             dw2_asm_output_data_uleb128 (size, "%s", name);
11302           else
11303             dw2_asm_output_data (constant_size (size), size, "%s", name);
11304
11305           output_loc_sequence (AT_loc (a), -1);
11306           break;
11307
11308         case dw_val_class_const:
11309           /* ??? It would be slightly more efficient to use a scheme like is
11310              used for unsigned constants below, but gdb 4.x does not sign
11311              extend.  Gdb 5.x does sign extend.  */
11312           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11313           break;
11314
11315         case dw_val_class_unsigned_const:
11316           {
11317             int csize = constant_size (AT_unsigned (a));
11318             if (dwarf_version == 3
11319                 && a->dw_attr == DW_AT_data_member_location
11320                 && csize >= 4)
11321               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
11322             else
11323               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
11324           }
11325           break;
11326
11327         case dw_val_class_const_double:
11328           {
11329             unsigned HOST_WIDE_INT first, second;
11330
11331             if (HOST_BITS_PER_WIDE_INT >= 64)
11332               dw2_asm_output_data (1,
11333                                    2 * HOST_BITS_PER_WIDE_INT
11334                                    / HOST_BITS_PER_CHAR,
11335                                    NULL);
11336
11337             if (WORDS_BIG_ENDIAN)
11338               {
11339                 first = a->dw_attr_val.v.val_double.high;
11340                 second = a->dw_attr_val.v.val_double.low;
11341               }
11342             else
11343               {
11344                 first = a->dw_attr_val.v.val_double.low;
11345                 second = a->dw_attr_val.v.val_double.high;
11346               }
11347
11348             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11349                                  first, name);
11350             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11351                                  second, NULL);
11352           }
11353           break;
11354
11355         case dw_val_class_vec:
11356           {
11357             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11358             unsigned int len = a->dw_attr_val.v.val_vec.length;
11359             unsigned int i;
11360             unsigned char *p;
11361
11362             dw2_asm_output_data (constant_size (len * elt_size),
11363                                  len * elt_size, "%s", name);
11364             if (elt_size > sizeof (HOST_WIDE_INT))
11365               {
11366                 elt_size /= 2;
11367                 len *= 2;
11368               }
11369             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11370                  i < len;
11371                  i++, p += elt_size)
11372               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11373                                    "fp or vector constant word %u", i);
11374             break;
11375           }
11376
11377         case dw_val_class_flag:
11378           if (dwarf_version >= 4)
11379             {
11380               /* Currently all add_AT_flag calls pass in 1 as last argument,
11381                  so DW_FORM_flag_present can be used.  If that ever changes,
11382                  we'll need to use DW_FORM_flag and have some optimization
11383                  in build_abbrev_table that will change those to
11384                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11385                  the same abbrev entry.  */
11386               gcc_assert (AT_flag (a) == 1);
11387               if (flag_debug_asm)
11388                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11389                          ASM_COMMENT_START, name);
11390               break;
11391             }
11392           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11393           break;
11394
11395         case dw_val_class_loc_list:
11396           {
11397             char *sym = AT_loc_list (a)->ll_symbol;
11398
11399             gcc_assert (sym);
11400             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11401                                    "%s", name);
11402           }
11403           break;
11404
11405         case dw_val_class_die_ref:
11406           if (AT_ref_external (a))
11407             {
11408               if (dwarf_version >= 4)
11409                 {
11410                   comdat_type_node_ref type_node =
11411                     AT_ref (a)->die_id.die_type_node;
11412
11413                   gcc_assert (type_node);
11414                   output_signature (type_node->signature, name);
11415                 }
11416               else
11417                 {
11418                   char *sym = AT_ref (a)->die_id.die_symbol;
11419                   int size;
11420
11421                   gcc_assert (sym);
11422                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11423                      length, whereas in DWARF3 it's always sized as an
11424                      offset.  */
11425                   if (dwarf_version == 2)
11426                     size = DWARF2_ADDR_SIZE;
11427                   else
11428                     size = DWARF_OFFSET_SIZE;
11429                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11430                                          name);
11431                 }
11432             }
11433           else
11434             {
11435               gcc_assert (AT_ref (a)->die_offset);
11436               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11437                                    "%s", name);
11438             }
11439           break;
11440
11441         case dw_val_class_fde_ref:
11442           {
11443             char l1[20];
11444
11445             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11446                                          a->dw_attr_val.v.val_fde_index * 2);
11447             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11448                                    "%s", name);
11449           }
11450           break;
11451
11452         case dw_val_class_vms_delta:
11453           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11454                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11455                                     "%s", name);
11456           break;
11457
11458         case dw_val_class_lbl_id:
11459           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11460           break;
11461
11462         case dw_val_class_lineptr:
11463           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11464                                  debug_line_section, "%s", name);
11465           break;
11466
11467         case dw_val_class_macptr:
11468           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11469                                  debug_macinfo_section, "%s", name);
11470           break;
11471
11472         case dw_val_class_str:
11473           if (AT_string_form (a) == DW_FORM_strp)
11474             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11475                                    a->dw_attr_val.v.val_str->label,
11476                                    debug_str_section,
11477                                    "%s: \"%s\"", name, AT_string (a));
11478           else
11479             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11480           break;
11481
11482         case dw_val_class_file:
11483           {
11484             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11485
11486             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11487                                  a->dw_attr_val.v.val_file->filename);
11488             break;
11489           }
11490
11491         case dw_val_class_data8:
11492           {
11493             int i;
11494
11495             for (i = 0; i < 8; i++)
11496               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11497                                    i == 0 ? "%s" : NULL, name);
11498             break;
11499           }
11500
11501         default:
11502           gcc_unreachable ();
11503         }
11504     }
11505
11506   FOR_EACH_CHILD (die, c, output_die (c));
11507
11508   /* Add null byte to terminate sibling list.  */
11509   if (die->die_child != NULL)
11510     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11511                          (unsigned long) die->die_offset);
11512 }
11513
11514 /* Output the compilation unit that appears at the beginning of the
11515    .debug_info section, and precedes the DIE descriptions.  */
11516
11517 static void
11518 output_compilation_unit_header (void)
11519 {
11520   int ver = dwarf_version;
11521
11522   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11523     dw2_asm_output_data (4, 0xffffffff,
11524       "Initial length escape value indicating 64-bit DWARF extension");
11525   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11526                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11527                        "Length of Compilation Unit Info");
11528   dw2_asm_output_data (2, ver, "DWARF version number");
11529   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11530                          debug_abbrev_section,
11531                          "Offset Into Abbrev. Section");
11532   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11533 }
11534
11535 /* Output the compilation unit DIE and its children.  */
11536
11537 static void
11538 output_comp_unit (dw_die_ref die, int output_if_empty)
11539 {
11540   const char *secname;
11541   char *oldsym, *tmp;
11542
11543   /* Unless we are outputting main CU, we may throw away empty ones.  */
11544   if (!output_if_empty && die->die_child == NULL)
11545     return;
11546
11547   /* Even if there are no children of this DIE, we must output the information
11548      about the compilation unit.  Otherwise, on an empty translation unit, we
11549      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11550      will then complain when examining the file.  First mark all the DIEs in
11551      this CU so we know which get local refs.  */
11552   mark_dies (die);
11553
11554   build_abbrev_table (die);
11555
11556   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11557   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11558   calc_die_sizes (die);
11559
11560   oldsym = die->die_id.die_symbol;
11561   if (oldsym)
11562     {
11563       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11564
11565       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11566       secname = tmp;
11567       die->die_id.die_symbol = NULL;
11568       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11569     }
11570   else
11571     {
11572       switch_to_section (debug_info_section);
11573       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11574       info_section_emitted = true;
11575     }
11576
11577   /* Output debugging information.  */
11578   output_compilation_unit_header ();
11579   output_die (die);
11580
11581   /* Leave the marks on the main CU, so we can check them in
11582      output_pubnames.  */
11583   if (oldsym)
11584     {
11585       unmark_dies (die);
11586       die->die_id.die_symbol = oldsym;
11587     }
11588 }
11589
11590 /* Output a comdat type unit DIE and its children.  */
11591
11592 static void
11593 output_comdat_type_unit (comdat_type_node *node)
11594 {
11595   const char *secname;
11596   char *tmp;
11597   int i;
11598 #if defined (OBJECT_FORMAT_ELF)
11599   tree comdat_key;
11600 #endif
11601
11602   /* First mark all the DIEs in this CU so we know which get local refs.  */
11603   mark_dies (node->root_die);
11604
11605   build_abbrev_table (node->root_die);
11606
11607   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11608   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11609   calc_die_sizes (node->root_die);
11610
11611 #if defined (OBJECT_FORMAT_ELF)
11612   secname = ".debug_types";
11613   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11614   sprintf (tmp, "wt.");
11615   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11616     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11617   comdat_key = get_identifier (tmp);
11618   targetm.asm_out.named_section (secname,
11619                                  SECTION_DEBUG | SECTION_LINKONCE,
11620                                  comdat_key);
11621 #else
11622   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11623   sprintf (tmp, ".gnu.linkonce.wt.");
11624   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11625     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11626   secname = tmp;
11627   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11628 #endif
11629
11630   /* Output debugging information.  */
11631   output_compilation_unit_header ();
11632   output_signature (node->signature, "Type Signature");
11633   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11634                        "Offset to Type DIE");
11635   output_die (node->root_die);
11636
11637   unmark_dies (node->root_die);
11638 }
11639
11640 /* Return the DWARF2/3 pubname associated with a decl.  */
11641
11642 static const char *
11643 dwarf2_name (tree decl, int scope)
11644 {
11645   if (DECL_NAMELESS (decl))
11646     return NULL;
11647   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11648 }
11649
11650 /* Add a new entry to .debug_pubnames if appropriate.  */
11651
11652 static void
11653 add_pubname_string (const char *str, dw_die_ref die)
11654 {
11655   if (targetm.want_debug_pub_sections)
11656     {
11657       pubname_entry e;
11658
11659       e.die = die;
11660       e.name = xstrdup (str);
11661       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11662     }
11663 }
11664
11665 static void
11666 add_pubname (tree decl, dw_die_ref die)
11667 {
11668   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11669     {
11670       const char *name = dwarf2_name (decl, 1);
11671       if (name)
11672         add_pubname_string (name, die);
11673     }
11674 }
11675
11676 /* Add a new entry to .debug_pubtypes if appropriate.  */
11677
11678 static void
11679 add_pubtype (tree decl, dw_die_ref die)
11680 {
11681   pubname_entry e;
11682
11683   if (!targetm.want_debug_pub_sections)
11684     return;
11685
11686   e.name = NULL;
11687   if ((TREE_PUBLIC (decl)
11688        || is_cu_die (die->die_parent))
11689       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11690     {
11691       e.die = die;
11692       if (TYPE_P (decl))
11693         {
11694           if (TYPE_NAME (decl))
11695             {
11696               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11697                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11698               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11699                        && DECL_NAME (TYPE_NAME (decl)))
11700                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11701               else
11702                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11703             }
11704         }
11705       else
11706         {
11707           e.name = dwarf2_name (decl, 1);
11708           if (e.name)
11709             e.name = xstrdup (e.name);
11710         }
11711
11712       /* If we don't have a name for the type, there's no point in adding
11713          it to the table.  */
11714       if (e.name && e.name[0] != '\0')
11715         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11716     }
11717 }
11718
11719 /* Output the public names table used to speed up access to externally
11720    visible names; or the public types table used to find type definitions.  */
11721
11722 static void
11723 output_pubnames (VEC (pubname_entry, gc) * names)
11724 {
11725   unsigned i;
11726   unsigned long pubnames_length = size_of_pubnames (names);
11727   pubname_ref pub;
11728
11729   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11730     dw2_asm_output_data (4, 0xffffffff,
11731       "Initial length escape value indicating 64-bit DWARF extension");
11732   if (names == pubname_table)
11733     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11734                          "Length of Public Names Info");
11735   else
11736     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11737                          "Length of Public Type Names Info");
11738   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11739   dw2_asm_output_data (2, 2, "DWARF Version");
11740   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11741                          debug_info_section,
11742                          "Offset of Compilation Unit Info");
11743   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11744                        "Compilation Unit Length");
11745
11746   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11747     {
11748       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11749       if (names == pubname_table)
11750         gcc_assert (pub->die->die_mark);
11751
11752       if (names != pubtype_table
11753           || pub->die->die_offset != 0
11754           || !flag_eliminate_unused_debug_types)
11755         {
11756           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11757                                "DIE offset");
11758
11759           dw2_asm_output_nstring (pub->name, -1, "external name");
11760         }
11761     }
11762
11763   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11764 }
11765
11766 /* Output the information that goes into the .debug_aranges table.
11767    Namely, define the beginning and ending address range of the
11768    text section generated for this compilation unit.  */
11769
11770 static void
11771 output_aranges (unsigned long aranges_length)
11772 {
11773   unsigned i;
11774
11775   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11776     dw2_asm_output_data (4, 0xffffffff,
11777       "Initial length escape value indicating 64-bit DWARF extension");
11778   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11779                        "Length of Address Ranges Info");
11780   /* Version number for aranges is still 2, even in DWARF3.  */
11781   dw2_asm_output_data (2, 2, "DWARF Version");
11782   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11783                          debug_info_section,
11784                          "Offset of Compilation Unit Info");
11785   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11786   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11787
11788   /* We need to align to twice the pointer size here.  */
11789   if (DWARF_ARANGES_PAD_SIZE)
11790     {
11791       /* Pad using a 2 byte words so that padding is correct for any
11792          pointer size.  */
11793       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11794                            2 * DWARF2_ADDR_SIZE);
11795       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11796         dw2_asm_output_data (2, 0, NULL);
11797     }
11798
11799   /* It is necessary not to output these entries if the sections were
11800      not used; if the sections were not used, the length will be 0 and
11801      the address may end up as 0 if the section is discarded by ld
11802      --gc-sections, leaving an invalid (0, 0) entry that can be
11803      confused with the terminator.  */
11804   if (text_section_used)
11805     {
11806       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11807       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11808                             text_section_label, "Length");
11809     }
11810   if (cold_text_section_used)
11811     {
11812       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11813                            "Address");
11814       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11815                             cold_text_section_label, "Length");
11816     }
11817
11818   if (have_multiple_function_sections)
11819     {
11820       unsigned fde_idx = 0;
11821
11822       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11823         {
11824           dw_fde_ref fde = &fde_table[fde_idx];
11825
11826           if (!fde->in_std_section)
11827             {
11828               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11829                                    "Address");
11830               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11831                                     fde->dw_fde_begin, "Length");
11832             }
11833           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11834             {
11835               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11836                                    "Address");
11837               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11838                                     fde->dw_fde_second_begin, "Length");
11839             }
11840         }
11841     }
11842
11843   /* Output the terminator words.  */
11844   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11845   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11846 }
11847
11848 /* Add a new entry to .debug_ranges.  Return the offset at which it
11849    was placed.  */
11850
11851 static unsigned int
11852 add_ranges_num (int num)
11853 {
11854   unsigned int in_use = ranges_table_in_use;
11855
11856   if (in_use == ranges_table_allocated)
11857     {
11858       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11859       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11860                                     ranges_table_allocated);
11861       memset (ranges_table + ranges_table_in_use, 0,
11862               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11863     }
11864
11865   ranges_table[in_use].num = num;
11866   ranges_table_in_use = in_use + 1;
11867
11868   return in_use * 2 * DWARF2_ADDR_SIZE;
11869 }
11870
11871 /* Add a new entry to .debug_ranges corresponding to a block, or a
11872    range terminator if BLOCK is NULL.  */
11873
11874 static unsigned int
11875 add_ranges (const_tree block)
11876 {
11877   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11878 }
11879
11880 /* Add a new entry to .debug_ranges corresponding to a pair of
11881    labels.  */
11882
11883 static void
11884 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11885                       bool *added)
11886 {
11887   unsigned int in_use = ranges_by_label_in_use;
11888   unsigned int offset;
11889
11890   if (in_use == ranges_by_label_allocated)
11891     {
11892       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11893       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11894                                        ranges_by_label,
11895                                        ranges_by_label_allocated);
11896       memset (ranges_by_label + ranges_by_label_in_use, 0,
11897               RANGES_TABLE_INCREMENT
11898               * sizeof (struct dw_ranges_by_label_struct));
11899     }
11900
11901   ranges_by_label[in_use].begin = begin;
11902   ranges_by_label[in_use].end = end;
11903   ranges_by_label_in_use = in_use + 1;
11904
11905   offset = add_ranges_num (-(int)in_use - 1);
11906   if (!*added)
11907     {
11908       add_AT_range_list (die, DW_AT_ranges, offset);
11909       *added = true;
11910     }
11911 }
11912
11913 static void
11914 output_ranges (void)
11915 {
11916   unsigned i;
11917   static const char *const start_fmt = "Offset %#x";
11918   const char *fmt = start_fmt;
11919
11920   for (i = 0; i < ranges_table_in_use; i++)
11921     {
11922       int block_num = ranges_table[i].num;
11923
11924       if (block_num > 0)
11925         {
11926           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11927           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11928
11929           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11930           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11931
11932           /* If all code is in the text section, then the compilation
11933              unit base address defaults to DW_AT_low_pc, which is the
11934              base of the text section.  */
11935           if (!have_multiple_function_sections)
11936             {
11937               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11938                                     text_section_label,
11939                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11940               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11941                                     text_section_label, NULL);
11942             }
11943
11944           /* Otherwise, the compilation unit base address is zero,
11945              which allows us to use absolute addresses, and not worry
11946              about whether the target supports cross-section
11947              arithmetic.  */
11948           else
11949             {
11950               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11951                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11952               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11953             }
11954
11955           fmt = NULL;
11956         }
11957
11958       /* Negative block_num stands for an index into ranges_by_label.  */
11959       else if (block_num < 0)
11960         {
11961           int lab_idx = - block_num - 1;
11962
11963           if (!have_multiple_function_sections)
11964             {
11965               gcc_unreachable ();
11966 #if 0
11967               /* If we ever use add_ranges_by_labels () for a single
11968                  function section, all we have to do is to take out
11969                  the #if 0 above.  */
11970               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11971                                     ranges_by_label[lab_idx].begin,
11972                                     text_section_label,
11973                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11974               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11975                                     ranges_by_label[lab_idx].end,
11976                                     text_section_label, NULL);
11977 #endif
11978             }
11979           else
11980             {
11981               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11982                                    ranges_by_label[lab_idx].begin,
11983                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11984               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11985                                    ranges_by_label[lab_idx].end,
11986                                    NULL);
11987             }
11988         }
11989       else
11990         {
11991           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11992           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11993           fmt = start_fmt;
11994         }
11995     }
11996 }
11997
11998 /* Data structure containing information about input files.  */
11999 struct file_info
12000 {
12001   const char *path;     /* Complete file name.  */
12002   const char *fname;    /* File name part.  */
12003   int length;           /* Length of entire string.  */
12004   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12005   int dir_idx;          /* Index in directory table.  */
12006 };
12007
12008 /* Data structure containing information about directories with source
12009    files.  */
12010 struct dir_info
12011 {
12012   const char *path;     /* Path including directory name.  */
12013   int length;           /* Path length.  */
12014   int prefix;           /* Index of directory entry which is a prefix.  */
12015   int count;            /* Number of files in this directory.  */
12016   int dir_idx;          /* Index of directory used as base.  */
12017 };
12018
12019 /* Callback function for file_info comparison.  We sort by looking at
12020    the directories in the path.  */
12021
12022 static int
12023 file_info_cmp (const void *p1, const void *p2)
12024 {
12025   const struct file_info *const s1 = (const struct file_info *) p1;
12026   const struct file_info *const s2 = (const struct file_info *) p2;
12027   const unsigned char *cp1;
12028   const unsigned char *cp2;
12029
12030   /* Take care of file names without directories.  We need to make sure that
12031      we return consistent values to qsort since some will get confused if
12032      we return the same value when identical operands are passed in opposite
12033      orders.  So if neither has a directory, return 0 and otherwise return
12034      1 or -1 depending on which one has the directory.  */
12035   if ((s1->path == s1->fname || s2->path == s2->fname))
12036     return (s2->path == s2->fname) - (s1->path == s1->fname);
12037
12038   cp1 = (const unsigned char *) s1->path;
12039   cp2 = (const unsigned char *) s2->path;
12040
12041   while (1)
12042     {
12043       ++cp1;
12044       ++cp2;
12045       /* Reached the end of the first path?  If so, handle like above.  */
12046       if ((cp1 == (const unsigned char *) s1->fname)
12047           || (cp2 == (const unsigned char *) s2->fname))
12048         return ((cp2 == (const unsigned char *) s2->fname)
12049                 - (cp1 == (const unsigned char *) s1->fname));
12050
12051       /* Character of current path component the same?  */
12052       else if (*cp1 != *cp2)
12053         return *cp1 - *cp2;
12054     }
12055 }
12056
12057 struct file_name_acquire_data
12058 {
12059   struct file_info *files;
12060   int used_files;
12061   int max_files;
12062 };
12063
12064 /* Traversal function for the hash table.  */
12065
12066 static int
12067 file_name_acquire (void ** slot, void *data)
12068 {
12069   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12070   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12071   struct file_info *fi;
12072   const char *f;
12073
12074   gcc_assert (fnad->max_files >= d->emitted_number);
12075
12076   if (! d->emitted_number)
12077     return 1;
12078
12079   gcc_assert (fnad->max_files != fnad->used_files);
12080
12081   fi = fnad->files + fnad->used_files++;
12082
12083   /* Skip all leading "./".  */
12084   f = d->filename;
12085   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12086     f += 2;
12087
12088   /* Create a new array entry.  */
12089   fi->path = f;
12090   fi->length = strlen (f);
12091   fi->file_idx = d;
12092
12093   /* Search for the file name part.  */
12094   f = strrchr (f, DIR_SEPARATOR);
12095 #if defined (DIR_SEPARATOR_2)
12096   {
12097     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12098
12099     if (g != NULL)
12100       {
12101         if (f == NULL || f < g)
12102           f = g;
12103       }
12104   }
12105 #endif
12106
12107   fi->fname = f == NULL ? fi->path : f + 1;
12108   return 1;
12109 }
12110
12111 /* Output the directory table and the file name table.  We try to minimize
12112    the total amount of memory needed.  A heuristic is used to avoid large
12113    slowdowns with many input files.  */
12114
12115 static void
12116 output_file_names (void)
12117 {
12118   struct file_name_acquire_data fnad;
12119   int numfiles;
12120   struct file_info *files;
12121   struct dir_info *dirs;
12122   int *saved;
12123   int *savehere;
12124   int *backmap;
12125   int ndirs;
12126   int idx_offset;
12127   int i;
12128
12129   if (!last_emitted_file)
12130     {
12131       dw2_asm_output_data (1, 0, "End directory table");
12132       dw2_asm_output_data (1, 0, "End file name table");
12133       return;
12134     }
12135
12136   numfiles = last_emitted_file->emitted_number;
12137
12138   /* Allocate the various arrays we need.  */
12139   files = XALLOCAVEC (struct file_info, numfiles);
12140   dirs = XALLOCAVEC (struct dir_info, numfiles);
12141
12142   fnad.files = files;
12143   fnad.used_files = 0;
12144   fnad.max_files = numfiles;
12145   htab_traverse (file_table, file_name_acquire, &fnad);
12146   gcc_assert (fnad.used_files == fnad.max_files);
12147
12148   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12149
12150   /* Find all the different directories used.  */
12151   dirs[0].path = files[0].path;
12152   dirs[0].length = files[0].fname - files[0].path;
12153   dirs[0].prefix = -1;
12154   dirs[0].count = 1;
12155   dirs[0].dir_idx = 0;
12156   files[0].dir_idx = 0;
12157   ndirs = 1;
12158
12159   for (i = 1; i < numfiles; i++)
12160     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12161         && memcmp (dirs[ndirs - 1].path, files[i].path,
12162                    dirs[ndirs - 1].length) == 0)
12163       {
12164         /* Same directory as last entry.  */
12165         files[i].dir_idx = ndirs - 1;
12166         ++dirs[ndirs - 1].count;
12167       }
12168     else
12169       {
12170         int j;
12171
12172         /* This is a new directory.  */
12173         dirs[ndirs].path = files[i].path;
12174         dirs[ndirs].length = files[i].fname - files[i].path;
12175         dirs[ndirs].count = 1;
12176         dirs[ndirs].dir_idx = ndirs;
12177         files[i].dir_idx = ndirs;
12178
12179         /* Search for a prefix.  */
12180         dirs[ndirs].prefix = -1;
12181         for (j = 0; j < ndirs; j++)
12182           if (dirs[j].length < dirs[ndirs].length
12183               && dirs[j].length > 1
12184               && (dirs[ndirs].prefix == -1
12185                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12186               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12187             dirs[ndirs].prefix = j;
12188
12189         ++ndirs;
12190       }
12191
12192   /* Now to the actual work.  We have to find a subset of the directories which
12193      allow expressing the file name using references to the directory table
12194      with the least amount of characters.  We do not do an exhaustive search
12195      where we would have to check out every combination of every single
12196      possible prefix.  Instead we use a heuristic which provides nearly optimal
12197      results in most cases and never is much off.  */
12198   saved = XALLOCAVEC (int, ndirs);
12199   savehere = XALLOCAVEC (int, ndirs);
12200
12201   memset (saved, '\0', ndirs * sizeof (saved[0]));
12202   for (i = 0; i < ndirs; i++)
12203     {
12204       int j;
12205       int total;
12206
12207       /* We can always save some space for the current directory.  But this
12208          does not mean it will be enough to justify adding the directory.  */
12209       savehere[i] = dirs[i].length;
12210       total = (savehere[i] - saved[i]) * dirs[i].count;
12211
12212       for (j = i + 1; j < ndirs; j++)
12213         {
12214           savehere[j] = 0;
12215           if (saved[j] < dirs[i].length)
12216             {
12217               /* Determine whether the dirs[i] path is a prefix of the
12218                  dirs[j] path.  */
12219               int k;
12220
12221               k = dirs[j].prefix;
12222               while (k != -1 && k != (int) i)
12223                 k = dirs[k].prefix;
12224
12225               if (k == (int) i)
12226                 {
12227                   /* Yes it is.  We can possibly save some memory by
12228                      writing the filenames in dirs[j] relative to
12229                      dirs[i].  */
12230                   savehere[j] = dirs[i].length;
12231                   total += (savehere[j] - saved[j]) * dirs[j].count;
12232                 }
12233             }
12234         }
12235
12236       /* Check whether we can save enough to justify adding the dirs[i]
12237          directory.  */
12238       if (total > dirs[i].length + 1)
12239         {
12240           /* It's worthwhile adding.  */
12241           for (j = i; j < ndirs; j++)
12242             if (savehere[j] > 0)
12243               {
12244                 /* Remember how much we saved for this directory so far.  */
12245                 saved[j] = savehere[j];
12246
12247                 /* Remember the prefix directory.  */
12248                 dirs[j].dir_idx = i;
12249               }
12250         }
12251     }
12252
12253   /* Emit the directory name table.  */
12254   idx_offset = dirs[0].length > 0 ? 1 : 0;
12255   for (i = 1 - idx_offset; i < ndirs; i++)
12256     dw2_asm_output_nstring (dirs[i].path,
12257                             dirs[i].length
12258                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12259                             "Directory Entry: %#x", i + idx_offset);
12260
12261   dw2_asm_output_data (1, 0, "End directory table");
12262
12263   /* We have to emit them in the order of emitted_number since that's
12264      used in the debug info generation.  To do this efficiently we
12265      generate a back-mapping of the indices first.  */
12266   backmap = XALLOCAVEC (int, numfiles);
12267   for (i = 0; i < numfiles; i++)
12268     backmap[files[i].file_idx->emitted_number - 1] = i;
12269
12270   /* Now write all the file names.  */
12271   for (i = 0; i < numfiles; i++)
12272     {
12273       int file_idx = backmap[i];
12274       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12275
12276 #ifdef VMS_DEBUGGING_INFO
12277 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12278
12279       /* Setting these fields can lead to debugger miscomparisons,
12280          but VMS Debug requires them to be set correctly.  */
12281
12282       int ver;
12283       long long cdt;
12284       long siz;
12285       int maxfilelen = strlen (files[file_idx].path)
12286                                + dirs[dir_idx].length
12287                                + MAX_VMS_VERSION_LEN + 1;
12288       char *filebuf = XALLOCAVEC (char, maxfilelen);
12289
12290       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12291       snprintf (filebuf, maxfilelen, "%s;%d",
12292                 files[file_idx].path + dirs[dir_idx].length, ver);
12293
12294       dw2_asm_output_nstring
12295         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12296
12297       /* Include directory index.  */
12298       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12299
12300       /* Modification time.  */
12301       dw2_asm_output_data_uleb128
12302         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12303           ? cdt : 0,
12304          NULL);
12305
12306       /* File length in bytes.  */
12307       dw2_asm_output_data_uleb128
12308         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12309           ? siz : 0,
12310          NULL);
12311 #else
12312       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12313                               "File Entry: %#x", (unsigned) i + 1);
12314
12315       /* Include directory index.  */
12316       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12317
12318       /* Modification time.  */
12319       dw2_asm_output_data_uleb128 (0, NULL);
12320
12321       /* File length in bytes.  */
12322       dw2_asm_output_data_uleb128 (0, NULL);
12323 #endif /* VMS_DEBUGGING_INFO */
12324     }
12325
12326   dw2_asm_output_data (1, 0, "End file name table");
12327 }
12328
12329
12330 /* Output the source line number correspondence information.  This
12331    information goes into the .debug_line section.  */
12332
12333 static void
12334 output_line_info (void)
12335 {
12336   char l1[20], l2[20], p1[20], p2[20];
12337   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12338   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12339   unsigned opc;
12340   unsigned n_op_args;
12341   unsigned long lt_index;
12342   unsigned long current_line;
12343   long line_offset;
12344   long line_delta;
12345   unsigned long current_file;
12346   unsigned long function;
12347   int ver = dwarf_version;
12348
12349   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12350   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12351   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12352   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12353
12354   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12355     dw2_asm_output_data (4, 0xffffffff,
12356       "Initial length escape value indicating 64-bit DWARF extension");
12357   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12358                         "Length of Source Line Info");
12359   ASM_OUTPUT_LABEL (asm_out_file, l1);
12360
12361   dw2_asm_output_data (2, ver, "DWARF Version");
12362   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12363   ASM_OUTPUT_LABEL (asm_out_file, p1);
12364
12365   /* Define the architecture-dependent minimum instruction length (in
12366    bytes).  In this implementation of DWARF, this field is used for
12367    information purposes only.  Since GCC generates assembly language,
12368    we have no a priori knowledge of how many instruction bytes are
12369    generated for each source line, and therefore can use only the
12370    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12371    commands.  Accordingly, we fix this as `1', which is "correct
12372    enough" for all architectures, and don't let the target override.  */
12373   dw2_asm_output_data (1, 1,
12374                        "Minimum Instruction Length");
12375
12376   if (ver >= 4)
12377     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12378                          "Maximum Operations Per Instruction");
12379   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12380                        "Default is_stmt_start flag");
12381   dw2_asm_output_data (1, DWARF_LINE_BASE,
12382                        "Line Base Value (Special Opcodes)");
12383   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12384                        "Line Range Value (Special Opcodes)");
12385   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12386                        "Special Opcode Base");
12387
12388   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12389     {
12390       switch (opc)
12391         {
12392         case DW_LNS_advance_pc:
12393         case DW_LNS_advance_line:
12394         case DW_LNS_set_file:
12395         case DW_LNS_set_column:
12396         case DW_LNS_fixed_advance_pc:
12397           n_op_args = 1;
12398           break;
12399         default:
12400           n_op_args = 0;
12401           break;
12402         }
12403
12404       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12405                            opc, n_op_args);
12406     }
12407
12408   /* Write out the information about the files we use.  */
12409   output_file_names ();
12410   ASM_OUTPUT_LABEL (asm_out_file, p2);
12411
12412   /* We used to set the address register to the first location in the text
12413      section here, but that didn't accomplish anything since we already
12414      have a line note for the opening brace of the first function.  */
12415
12416   /* Generate the line number to PC correspondence table, encoded as
12417      a series of state machine operations.  */
12418   current_file = 1;
12419   current_line = 1;
12420
12421   if (cfun && in_cold_section_p)
12422     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12423   else
12424     strcpy (prev_line_label, text_section_label);
12425   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12426     {
12427       dw_line_info_ref line_info = &line_info_table[lt_index];
12428
12429 #if 0
12430       /* Disable this optimization for now; GDB wants to see two line notes
12431          at the beginning of a function so it can find the end of the
12432          prologue.  */
12433
12434       /* Don't emit anything for redundant notes.  Just updating the
12435          address doesn't accomplish anything, because we already assume
12436          that anything after the last address is this line.  */
12437       if (line_info->dw_line_num == current_line
12438           && line_info->dw_file_num == current_file)
12439         continue;
12440 #endif
12441
12442       /* Emit debug info for the address of the current line.
12443
12444          Unfortunately, we have little choice here currently, and must always
12445          use the most general form.  GCC does not know the address delta
12446          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12447          attributes which will give an upper bound on the address range.  We
12448          could perhaps use length attributes to determine when it is safe to
12449          use DW_LNS_fixed_advance_pc.  */
12450
12451       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12452       if (0)
12453         {
12454           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12455           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12456                                "DW_LNS_fixed_advance_pc");
12457           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12458         }
12459       else
12460         {
12461           /* This can handle any delta.  This takes
12462              4+DWARF2_ADDR_SIZE bytes.  */
12463           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12464           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12465           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12466           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12467         }
12468
12469       strcpy (prev_line_label, line_label);
12470
12471       /* Emit debug info for the source file of the current line, if
12472          different from the previous line.  */
12473       if (line_info->dw_file_num != current_file)
12474         {
12475           current_file = line_info->dw_file_num;
12476           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12477           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12478         }
12479
12480       /* Emit debug info for the current line number, choosing the encoding
12481          that uses the least amount of space.  */
12482       if (line_info->dw_line_num != current_line)
12483         {
12484           line_offset = line_info->dw_line_num - current_line;
12485           line_delta = line_offset - DWARF_LINE_BASE;
12486           current_line = line_info->dw_line_num;
12487           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12488             /* This can handle deltas from -10 to 234, using the current
12489                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12490                takes 1 byte.  */
12491             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12492                                  "line %lu", current_line);
12493           else
12494             {
12495               /* This can handle any delta.  This takes at least 4 bytes,
12496                  depending on the value being encoded.  */
12497               dw2_asm_output_data (1, DW_LNS_advance_line,
12498                                    "advance to line %lu", current_line);
12499               dw2_asm_output_data_sleb128 (line_offset, NULL);
12500               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12501             }
12502         }
12503       else
12504         /* We still need to start a new row, so output a copy insn.  */
12505         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12506     }
12507
12508   /* Emit debug info for the address of the end of the function.  */
12509   if (0)
12510     {
12511       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12512                            "DW_LNS_fixed_advance_pc");
12513       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12514     }
12515   else
12516     {
12517       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12518       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12519       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12520       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12521     }
12522
12523   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12524   dw2_asm_output_data_uleb128 (1, NULL);
12525   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12526
12527   function = 0;
12528   current_file = 1;
12529   current_line = 1;
12530   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12531     {
12532       dw_separate_line_info_ref line_info
12533         = &separate_line_info_table[lt_index];
12534
12535 #if 0
12536       /* Don't emit anything for redundant notes.  */
12537       if (line_info->dw_line_num == current_line
12538           && line_info->dw_file_num == current_file
12539           && line_info->function == function)
12540         goto cont;
12541 #endif
12542
12543       /* Emit debug info for the address of the current line.  If this is
12544          a new function, or the first line of a function, then we need
12545          to handle it differently.  */
12546       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12547                                    lt_index);
12548       if (function != line_info->function)
12549         {
12550           function = line_info->function;
12551
12552           /* Set the address register to the first line in the function.  */
12553           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12554           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12555           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12556           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12557         }
12558       else
12559         {
12560           /* ??? See the DW_LNS_advance_pc comment above.  */
12561           if (0)
12562             {
12563               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12564                                    "DW_LNS_fixed_advance_pc");
12565               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12566             }
12567           else
12568             {
12569               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12570               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12571               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12572               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12573             }
12574         }
12575
12576       strcpy (prev_line_label, line_label);
12577
12578       /* Emit debug info for the source file of the current line, if
12579          different from the previous line.  */
12580       if (line_info->dw_file_num != current_file)
12581         {
12582           current_file = line_info->dw_file_num;
12583           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12584           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12585         }
12586
12587       /* Emit debug info for the current line number, choosing the encoding
12588          that uses the least amount of space.  */
12589       if (line_info->dw_line_num != current_line)
12590         {
12591           line_offset = line_info->dw_line_num - current_line;
12592           line_delta = line_offset - DWARF_LINE_BASE;
12593           current_line = line_info->dw_line_num;
12594           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12595             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12596                                  "line %lu", current_line);
12597           else
12598             {
12599               dw2_asm_output_data (1, DW_LNS_advance_line,
12600                                    "advance to line %lu", current_line);
12601               dw2_asm_output_data_sleb128 (line_offset, NULL);
12602               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12603             }
12604         }
12605       else
12606         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12607
12608 #if 0
12609     cont:
12610 #endif
12611
12612       lt_index++;
12613
12614       /* If we're done with a function, end its sequence.  */
12615       if (lt_index == separate_line_info_table_in_use
12616           || separate_line_info_table[lt_index].function != function)
12617         {
12618           current_file = 1;
12619           current_line = 1;
12620
12621           /* Emit debug info for the address of the end of the function.  */
12622           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12623           if (0)
12624             {
12625               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12626                                    "DW_LNS_fixed_advance_pc");
12627               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12628             }
12629           else
12630             {
12631               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12632               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12633               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12634               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12635             }
12636
12637           /* Output the marker for the end of this sequence.  */
12638           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12639           dw2_asm_output_data_uleb128 (1, NULL);
12640           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12641         }
12642     }
12643
12644   /* Output the marker for the end of the line number info.  */
12645   ASM_OUTPUT_LABEL (asm_out_file, l2);
12646 }
12647
12648 /* Return the size of the .debug_dcall table for the compilation unit.  */
12649
12650 static unsigned long
12651 size_of_dcall_table (void)
12652 {
12653   unsigned long size;
12654   unsigned int i;
12655   dcall_entry *p;
12656   tree last_poc_decl = NULL;
12657
12658   /* Header:  version + debug info section pointer + pointer size.  */
12659   size = 2 + DWARF_OFFSET_SIZE + 1;
12660
12661   /* Each entry:  code label + DIE offset.  */
12662   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12663     {
12664       gcc_assert (p->targ_die != NULL);
12665       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12666       if (p->poc_decl != last_poc_decl)
12667         {
12668           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12669           gcc_assert (poc_die);
12670           last_poc_decl = p->poc_decl;
12671           if (poc_die)
12672             size += (DWARF_OFFSET_SIZE
12673                      + size_of_uleb128 (poc_die->die_offset));
12674         }
12675       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12676     }
12677
12678   return size;
12679 }
12680
12681 /* Output the direct call table used to disambiguate PC values when
12682    identical function have been merged.  */
12683
12684 static void
12685 output_dcall_table (void)
12686 {
12687   unsigned i;
12688   unsigned long dcall_length = size_of_dcall_table ();
12689   dcall_entry *p;
12690   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12691   tree last_poc_decl = NULL;
12692
12693   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12694     dw2_asm_output_data (4, 0xffffffff,
12695       "Initial length escape value indicating 64-bit DWARF extension");
12696   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12697                        "Length of Direct Call Table");
12698   dw2_asm_output_data (2, 4, "Version number");
12699   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12700                          debug_info_section,
12701                          "Offset of Compilation Unit Info");
12702   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12703
12704   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12705     {
12706       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12707       if (p->poc_decl != last_poc_decl)
12708         {
12709           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12710           last_poc_decl = p->poc_decl;
12711           if (poc_die)
12712             {
12713               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12714               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12715                                            "Caller DIE offset");
12716             }
12717         }
12718       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12719       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12720       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12721                                    "Callee DIE offset");
12722     }
12723 }
12724 \f
12725 /* Return the size of the .debug_vcall table for the compilation unit.  */
12726
12727 static unsigned long
12728 size_of_vcall_table (void)
12729 {
12730   unsigned long size;
12731   unsigned int i;
12732   vcall_entry *p;
12733
12734   /* Header:  version + pointer size.  */
12735   size = 2 + 1;
12736
12737   /* Each entry:  code label + vtable slot index.  */
12738   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12739     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12740
12741   return size;
12742 }
12743
12744 /* Output the virtual call table used to disambiguate PC values when
12745    identical function have been merged.  */
12746
12747 static void
12748 output_vcall_table (void)
12749 {
12750   unsigned i;
12751   unsigned long vcall_length = size_of_vcall_table ();
12752   vcall_entry *p;
12753   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12754
12755   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12756     dw2_asm_output_data (4, 0xffffffff,
12757       "Initial length escape value indicating 64-bit DWARF extension");
12758   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12759                        "Length of Virtual Call Table");
12760   dw2_asm_output_data (2, 4, "Version number");
12761   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12762
12763   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12764     {
12765       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12766       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12767       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12768     }
12769 }
12770 \f
12771 /* Given a pointer to a tree node for some base type, return a pointer to
12772    a DIE that describes the given type.
12773
12774    This routine must only be called for GCC type nodes that correspond to
12775    Dwarf base (fundamental) types.  */
12776
12777 static dw_die_ref
12778 base_type_die (tree type)
12779 {
12780   dw_die_ref base_type_result;
12781   enum dwarf_type encoding;
12782
12783   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12784     return 0;
12785
12786   /* If this is a subtype that should not be emitted as a subrange type,
12787      use the base type.  See subrange_type_for_debug_p.  */
12788   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12789     type = TREE_TYPE (type);
12790
12791   switch (TREE_CODE (type))
12792     {
12793     case INTEGER_TYPE:
12794       if ((dwarf_version >= 4 || !dwarf_strict)
12795           && TYPE_NAME (type)
12796           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12797           && DECL_IS_BUILTIN (TYPE_NAME (type))
12798           && DECL_NAME (TYPE_NAME (type)))
12799         {
12800           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12801           if (strcmp (name, "char16_t") == 0
12802               || strcmp (name, "char32_t") == 0)
12803             {
12804               encoding = DW_ATE_UTF;
12805               break;
12806             }
12807         }
12808       if (TYPE_STRING_FLAG (type))
12809         {
12810           if (TYPE_UNSIGNED (type))
12811             encoding = DW_ATE_unsigned_char;
12812           else
12813             encoding = DW_ATE_signed_char;
12814         }
12815       else if (TYPE_UNSIGNED (type))
12816         encoding = DW_ATE_unsigned;
12817       else
12818         encoding = DW_ATE_signed;
12819       break;
12820
12821     case REAL_TYPE:
12822       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12823         {
12824           if (dwarf_version >= 3 || !dwarf_strict)
12825             encoding = DW_ATE_decimal_float;
12826           else
12827             encoding = DW_ATE_lo_user;
12828         }
12829       else
12830         encoding = DW_ATE_float;
12831       break;
12832
12833     case FIXED_POINT_TYPE:
12834       if (!(dwarf_version >= 3 || !dwarf_strict))
12835         encoding = DW_ATE_lo_user;
12836       else if (TYPE_UNSIGNED (type))
12837         encoding = DW_ATE_unsigned_fixed;
12838       else
12839         encoding = DW_ATE_signed_fixed;
12840       break;
12841
12842       /* Dwarf2 doesn't know anything about complex ints, so use
12843          a user defined type for it.  */
12844     case COMPLEX_TYPE:
12845       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12846         encoding = DW_ATE_complex_float;
12847       else
12848         encoding = DW_ATE_lo_user;
12849       break;
12850
12851     case BOOLEAN_TYPE:
12852       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12853       encoding = DW_ATE_boolean;
12854       break;
12855
12856     default:
12857       /* No other TREE_CODEs are Dwarf fundamental types.  */
12858       gcc_unreachable ();
12859     }
12860
12861   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12862
12863   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12864                    int_size_in_bytes (type));
12865   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12866
12867   return base_type_result;
12868 }
12869
12870 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12871    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12872
12873 static inline int
12874 is_base_type (tree type)
12875 {
12876   switch (TREE_CODE (type))
12877     {
12878     case ERROR_MARK:
12879     case VOID_TYPE:
12880     case INTEGER_TYPE:
12881     case REAL_TYPE:
12882     case FIXED_POINT_TYPE:
12883     case COMPLEX_TYPE:
12884     case BOOLEAN_TYPE:
12885       return 1;
12886
12887     case ARRAY_TYPE:
12888     case RECORD_TYPE:
12889     case UNION_TYPE:
12890     case QUAL_UNION_TYPE:
12891     case ENUMERAL_TYPE:
12892     case FUNCTION_TYPE:
12893     case METHOD_TYPE:
12894     case POINTER_TYPE:
12895     case REFERENCE_TYPE:
12896     case NULLPTR_TYPE:
12897     case OFFSET_TYPE:
12898     case LANG_TYPE:
12899     case VECTOR_TYPE:
12900       return 0;
12901
12902     default:
12903       gcc_unreachable ();
12904     }
12905
12906   return 0;
12907 }
12908
12909 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12910    node, return the size in bits for the type if it is a constant, or else
12911    return the alignment for the type if the type's size is not constant, or
12912    else return BITS_PER_WORD if the type actually turns out to be an
12913    ERROR_MARK node.  */
12914
12915 static inline unsigned HOST_WIDE_INT
12916 simple_type_size_in_bits (const_tree type)
12917 {
12918   if (TREE_CODE (type) == ERROR_MARK)
12919     return BITS_PER_WORD;
12920   else if (TYPE_SIZE (type) == NULL_TREE)
12921     return 0;
12922   else if (host_integerp (TYPE_SIZE (type), 1))
12923     return tree_low_cst (TYPE_SIZE (type), 1);
12924   else
12925     return TYPE_ALIGN (type);
12926 }
12927
12928 /* Similarly, but return a double_int instead of UHWI.  */
12929
12930 static inline double_int
12931 double_int_type_size_in_bits (const_tree type)
12932 {
12933   if (TREE_CODE (type) == ERROR_MARK)
12934     return uhwi_to_double_int (BITS_PER_WORD);
12935   else if (TYPE_SIZE (type) == NULL_TREE)
12936     return double_int_zero;
12937   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12938     return tree_to_double_int (TYPE_SIZE (type));
12939   else
12940     return uhwi_to_double_int (TYPE_ALIGN (type));
12941 }
12942
12943 /*  Given a pointer to a tree node for a subrange type, return a pointer
12944     to a DIE that describes the given type.  */
12945
12946 static dw_die_ref
12947 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12948 {
12949   dw_die_ref subrange_die;
12950   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12951
12952   if (context_die == NULL)
12953     context_die = comp_unit_die ();
12954
12955   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12956
12957   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12958     {
12959       /* The size of the subrange type and its base type do not match,
12960          so we need to generate a size attribute for the subrange type.  */
12961       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12962     }
12963
12964   if (low)
12965     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12966   if (high)
12967     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12968
12969   return subrange_die;
12970 }
12971
12972 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12973    entry that chains various modifiers in front of the given type.  */
12974
12975 static dw_die_ref
12976 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12977                    dw_die_ref context_die)
12978 {
12979   enum tree_code code = TREE_CODE (type);
12980   dw_die_ref mod_type_die;
12981   dw_die_ref sub_die = NULL;
12982   tree item_type = NULL;
12983   tree qualified_type;
12984   tree name, low, high;
12985
12986   if (code == ERROR_MARK)
12987     return NULL;
12988
12989   /* See if we already have the appropriately qualified variant of
12990      this type.  */
12991   qualified_type
12992     = get_qualified_type (type,
12993                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12994                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12995
12996   if (qualified_type == sizetype
12997       && TYPE_NAME (qualified_type)
12998       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12999     {
13000       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13001
13002       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13003                            && TYPE_PRECISION (t)
13004                            == TYPE_PRECISION (qualified_type)
13005                            && TYPE_UNSIGNED (t)
13006                            == TYPE_UNSIGNED (qualified_type));
13007       qualified_type = t;
13008     }
13009
13010   /* If we do, then we can just use its DIE, if it exists.  */
13011   if (qualified_type)
13012     {
13013       mod_type_die = lookup_type_die (qualified_type);
13014       if (mod_type_die)
13015         return mod_type_die;
13016     }
13017
13018   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13019
13020   /* Handle C typedef types.  */
13021   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
13022       && !DECL_ARTIFICIAL (name))
13023     {
13024       tree dtype = TREE_TYPE (name);
13025
13026       if (qualified_type == dtype)
13027         {
13028           /* For a named type, use the typedef.  */
13029           gen_type_die (qualified_type, context_die);
13030           return lookup_type_die (qualified_type);
13031         }
13032       else if (is_const_type < TYPE_READONLY (dtype)
13033                || is_volatile_type < TYPE_VOLATILE (dtype)
13034                || (is_const_type <= TYPE_READONLY (dtype)
13035                    && is_volatile_type <= TYPE_VOLATILE (dtype)
13036                    && DECL_ORIGINAL_TYPE (name) != type))
13037         /* cv-unqualified version of named type.  Just use the unnamed
13038            type to which it refers.  */
13039         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13040                                   is_const_type, is_volatile_type,
13041                                   context_die);
13042       /* Else cv-qualified version of named type; fall through.  */
13043     }
13044
13045   if (is_const_type
13046       /* If both is_const_type and is_volatile_type, prefer the path
13047          which leads to a qualified type.  */
13048       && (!is_volatile_type
13049           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13050           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13051     {
13052       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13053       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13054     }
13055   else if (is_volatile_type)
13056     {
13057       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13058       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13059     }
13060   else if (code == POINTER_TYPE)
13061     {
13062       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13063       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13064                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13065       item_type = TREE_TYPE (type);
13066       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13067         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13068                          TYPE_ADDR_SPACE (item_type));
13069     }
13070   else if (code == REFERENCE_TYPE)
13071     {
13072       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13073         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13074                                 type);
13075       else
13076         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13077       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13078                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13079       item_type = TREE_TYPE (type);
13080       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13081         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13082                          TYPE_ADDR_SPACE (item_type));
13083     }
13084   else if (code == INTEGER_TYPE
13085            && TREE_TYPE (type) != NULL_TREE
13086            && subrange_type_for_debug_p (type, &low, &high))
13087     {
13088       mod_type_die = subrange_type_die (type, low, high, context_die);
13089       item_type = TREE_TYPE (type);
13090     }
13091   else if (is_base_type (type))
13092     mod_type_die = base_type_die (type);
13093   else
13094     {
13095       gen_type_die (type, context_die);
13096
13097       /* We have to get the type_main_variant here (and pass that to the
13098          `lookup_type_die' routine) because the ..._TYPE node we have
13099          might simply be a *copy* of some original type node (where the
13100          copy was created to help us keep track of typedef names) and
13101          that copy might have a different TYPE_UID from the original
13102          ..._TYPE node.  */
13103       if (TREE_CODE (type) != VECTOR_TYPE)
13104         return lookup_type_die (type_main_variant (type));
13105       else
13106         /* Vectors have the debugging information in the type,
13107            not the main variant.  */
13108         return lookup_type_die (type);
13109     }
13110
13111   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13112      don't output a DW_TAG_typedef, since there isn't one in the
13113      user's program; just attach a DW_AT_name to the type.
13114      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13115      if the base type already has the same name.  */
13116   if (name
13117       && ((TREE_CODE (name) != TYPE_DECL
13118            && (qualified_type == TYPE_MAIN_VARIANT (type)
13119                || (!is_const_type && !is_volatile_type)))
13120           || (TREE_CODE (name) == TYPE_DECL
13121               && TREE_TYPE (name) == qualified_type
13122               && DECL_NAME (name))))
13123     {
13124       if (TREE_CODE (name) == TYPE_DECL)
13125         /* Could just call add_name_and_src_coords_attributes here,
13126            but since this is a builtin type it doesn't have any
13127            useful source coordinates anyway.  */
13128         name = DECL_NAME (name);
13129       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13130     }
13131   /* This probably indicates a bug.  */
13132   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13133     add_name_attribute (mod_type_die, "__unknown__");
13134
13135   if (qualified_type)
13136     equate_type_number_to_die (qualified_type, mod_type_die);
13137
13138   if (item_type)
13139     /* We must do this after the equate_type_number_to_die call, in case
13140        this is a recursive type.  This ensures that the modified_type_die
13141        recursion will terminate even if the type is recursive.  Recursive
13142        types are possible in Ada.  */
13143     sub_die = modified_type_die (item_type,
13144                                  TYPE_READONLY (item_type),
13145                                  TYPE_VOLATILE (item_type),
13146                                  context_die);
13147
13148   if (sub_die != NULL)
13149     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13150
13151   return mod_type_die;
13152 }
13153
13154 /* Generate DIEs for the generic parameters of T.
13155    T must be either a generic type or a generic function.
13156    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13157
13158 static void
13159 gen_generic_params_dies (tree t)
13160 {
13161   tree parms, args;
13162   int parms_num, i;
13163   dw_die_ref die = NULL;
13164
13165   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13166     return;
13167
13168   if (TYPE_P (t))
13169     die = lookup_type_die (t);
13170   else if (DECL_P (t))
13171     die = lookup_decl_die (t);
13172
13173   gcc_assert (die);
13174
13175   parms = lang_hooks.get_innermost_generic_parms (t);
13176   if (!parms)
13177     /* T has no generic parameter. It means T is neither a generic type
13178        or function. End of story.  */
13179     return;
13180
13181   parms_num = TREE_VEC_LENGTH (parms);
13182   args = lang_hooks.get_innermost_generic_args (t);
13183   for (i = 0; i < parms_num; i++)
13184     {
13185       tree parm, arg, arg_pack_elems;
13186
13187       parm = TREE_VEC_ELT (parms, i);
13188       arg = TREE_VEC_ELT (args, i);
13189       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13190       gcc_assert (parm && TREE_VALUE (parm) && arg);
13191
13192       if (parm && TREE_VALUE (parm) && arg)
13193         {
13194           /* If PARM represents a template parameter pack,
13195              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13196              by DW_TAG_template_*_parameter DIEs for the argument
13197              pack elements of ARG. Note that ARG would then be
13198              an argument pack.  */
13199           if (arg_pack_elems)
13200             template_parameter_pack_die (TREE_VALUE (parm),
13201                                          arg_pack_elems,
13202                                          die);
13203           else
13204             generic_parameter_die (TREE_VALUE (parm), arg,
13205                                    true /* Emit DW_AT_name */, die);
13206         }
13207     }
13208 }
13209
13210 /* Create and return a DIE for PARM which should be
13211    the representation of a generic type parameter.
13212    For instance, in the C++ front end, PARM would be a template parameter.
13213    ARG is the argument to PARM.
13214    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13215    name of the PARM.
13216    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13217    as a child node.  */
13218
13219 static dw_die_ref
13220 generic_parameter_die (tree parm, tree arg,
13221                        bool emit_name_p,
13222                        dw_die_ref parent_die)
13223 {
13224   dw_die_ref tmpl_die = NULL;
13225   const char *name = NULL;
13226
13227   if (!parm || !DECL_NAME (parm) || !arg)
13228     return NULL;
13229
13230   /* We support non-type generic parameters and arguments,
13231      type generic parameters and arguments, as well as
13232      generic generic parameters (a.k.a. template template parameters in C++)
13233      and arguments.  */
13234   if (TREE_CODE (parm) == PARM_DECL)
13235     /* PARM is a nontype generic parameter  */
13236     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13237   else if (TREE_CODE (parm) == TYPE_DECL)
13238     /* PARM is a type generic parameter.  */
13239     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13240   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13241     /* PARM is a generic generic parameter.
13242        Its DIE is a GNU extension. It shall have a
13243        DW_AT_name attribute to represent the name of the template template
13244        parameter, and a DW_AT_GNU_template_name attribute to represent the
13245        name of the template template argument.  */
13246     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13247                         parent_die, parm);
13248   else
13249     gcc_unreachable ();
13250
13251   if (tmpl_die)
13252     {
13253       tree tmpl_type;
13254
13255       /* If PARM is a generic parameter pack, it means we are
13256          emitting debug info for a template argument pack element.
13257          In other terms, ARG is a template argument pack element.
13258          In that case, we don't emit any DW_AT_name attribute for
13259          the die.  */
13260       if (emit_name_p)
13261         {
13262           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13263           gcc_assert (name);
13264           add_AT_string (tmpl_die, DW_AT_name, name);
13265         }
13266
13267       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13268         {
13269           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13270              TMPL_DIE should have a child DW_AT_type attribute that is set
13271              to the type of the argument to PARM, which is ARG.
13272              If PARM is a type generic parameter, TMPL_DIE should have a
13273              child DW_AT_type that is set to ARG.  */
13274           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13275           add_type_attribute (tmpl_die, tmpl_type, 0,
13276                               TREE_THIS_VOLATILE (tmpl_type),
13277                               parent_die);
13278         }
13279       else
13280         {
13281           /* So TMPL_DIE is a DIE representing a
13282              a generic generic template parameter, a.k.a template template
13283              parameter in C++ and arg is a template.  */
13284
13285           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13286              to the name of the argument.  */
13287           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13288           if (name)
13289             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13290         }
13291
13292       if (TREE_CODE (parm) == PARM_DECL)
13293         /* So PARM is a non-type generic parameter.
13294            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13295            attribute of TMPL_DIE which value represents the value
13296            of ARG.
13297            We must be careful here:
13298            The value of ARG might reference some function decls.
13299            We might currently be emitting debug info for a generic
13300            type and types are emitted before function decls, we don't
13301            know if the function decls referenced by ARG will actually be
13302            emitted after cgraph computations.
13303            So must defer the generation of the DW_AT_const_value to
13304            after cgraph is ready.  */
13305         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13306     }
13307
13308   return tmpl_die;
13309 }
13310
13311 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13312    PARM_PACK must be a template parameter pack. The returned DIE
13313    will be child DIE of PARENT_DIE.  */
13314
13315 static dw_die_ref
13316 template_parameter_pack_die (tree parm_pack,
13317                              tree parm_pack_args,
13318                              dw_die_ref parent_die)
13319 {
13320   dw_die_ref die;
13321   int j;
13322
13323   gcc_assert (parent_die && parm_pack);
13324
13325   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13326   add_name_and_src_coords_attributes (die, parm_pack);
13327   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13328     generic_parameter_die (parm_pack,
13329                            TREE_VEC_ELT (parm_pack_args, j),
13330                            false /* Don't emit DW_AT_name */,
13331                            die);
13332   return die;
13333 }
13334
13335 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13336    an enumerated type.  */
13337
13338 static inline int
13339 type_is_enum (const_tree type)
13340 {
13341   return TREE_CODE (type) == ENUMERAL_TYPE;
13342 }
13343
13344 /* Return the DBX register number described by a given RTL node.  */
13345
13346 static unsigned int
13347 dbx_reg_number (const_rtx rtl)
13348 {
13349   unsigned regno = REGNO (rtl);
13350
13351   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13352
13353 #ifdef LEAF_REG_REMAP
13354   if (current_function_uses_only_leaf_regs)
13355     {
13356       int leaf_reg = LEAF_REG_REMAP (regno);
13357       if (leaf_reg != -1)
13358         regno = (unsigned) leaf_reg;
13359     }
13360 #endif
13361
13362   return DBX_REGISTER_NUMBER (regno);
13363 }
13364
13365 /* Optionally add a DW_OP_piece term to a location description expression.
13366    DW_OP_piece is only added if the location description expression already
13367    doesn't end with DW_OP_piece.  */
13368
13369 static void
13370 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13371 {
13372   dw_loc_descr_ref loc;
13373
13374   if (*list_head != NULL)
13375     {
13376       /* Find the end of the chain.  */
13377       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13378         ;
13379
13380       if (loc->dw_loc_opc != DW_OP_piece)
13381         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13382     }
13383 }
13384
13385 /* Return a location descriptor that designates a machine register or
13386    zero if there is none.  */
13387
13388 static dw_loc_descr_ref
13389 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13390 {
13391   rtx regs;
13392
13393   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13394     return 0;
13395
13396   /* We only use "frame base" when we're sure we're talking about the
13397      post-prologue local stack frame.  We do this by *not* running
13398      register elimination until this point, and recognizing the special
13399      argument pointer and soft frame pointer rtx's.
13400      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13401   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13402       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13403     {
13404       dw_loc_descr_ref result = NULL;
13405
13406       if (dwarf_version >= 4 || !dwarf_strict)
13407         {
13408           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13409           if (result)
13410             add_loc_descr (&result,
13411                            new_loc_descr (DW_OP_stack_value, 0, 0));
13412         }
13413       return result;
13414     }
13415
13416   regs = targetm.dwarf_register_span (rtl);
13417
13418   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13419     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13420   else
13421     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13422 }
13423
13424 /* Return a location descriptor that designates a machine register for
13425    a given hard register number.  */
13426
13427 static dw_loc_descr_ref
13428 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13429 {
13430   dw_loc_descr_ref reg_loc_descr;
13431
13432   if (regno <= 31)
13433     reg_loc_descr
13434       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13435   else
13436     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13437
13438   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13439     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13440
13441   return reg_loc_descr;
13442 }
13443
13444 /* Given an RTL of a register, return a location descriptor that
13445    designates a value that spans more than one register.  */
13446
13447 static dw_loc_descr_ref
13448 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13449                              enum var_init_status initialized)
13450 {
13451   int nregs, size, i;
13452   unsigned reg;
13453   dw_loc_descr_ref loc_result = NULL;
13454
13455   reg = REGNO (rtl);
13456 #ifdef LEAF_REG_REMAP
13457   if (current_function_uses_only_leaf_regs)
13458     {
13459       int leaf_reg = LEAF_REG_REMAP (reg);
13460       if (leaf_reg != -1)
13461         reg = (unsigned) leaf_reg;
13462     }
13463 #endif
13464   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13465   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13466
13467   /* Simple, contiguous registers.  */
13468   if (regs == NULL_RTX)
13469     {
13470       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13471
13472       loc_result = NULL;
13473       while (nregs--)
13474         {
13475           dw_loc_descr_ref t;
13476
13477           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13478                                       VAR_INIT_STATUS_INITIALIZED);
13479           add_loc_descr (&loc_result, t);
13480           add_loc_descr_op_piece (&loc_result, size);
13481           ++reg;
13482         }
13483       return loc_result;
13484     }
13485
13486   /* Now onto stupid register sets in non contiguous locations.  */
13487
13488   gcc_assert (GET_CODE (regs) == PARALLEL);
13489
13490   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13491   loc_result = NULL;
13492
13493   for (i = 0; i < XVECLEN (regs, 0); ++i)
13494     {
13495       dw_loc_descr_ref t;
13496
13497       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13498                                   VAR_INIT_STATUS_INITIALIZED);
13499       add_loc_descr (&loc_result, t);
13500       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13501       add_loc_descr_op_piece (&loc_result, size);
13502     }
13503
13504   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13505     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13506   return loc_result;
13507 }
13508
13509 /* Return a location descriptor that designates a constant.  */
13510
13511 static dw_loc_descr_ref
13512 int_loc_descriptor (HOST_WIDE_INT i)
13513 {
13514   enum dwarf_location_atom op;
13515
13516   /* Pick the smallest representation of a constant, rather than just
13517      defaulting to the LEB encoding.  */
13518   if (i >= 0)
13519     {
13520       if (i <= 31)
13521         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13522       else if (i <= 0xff)
13523         op = DW_OP_const1u;
13524       else if (i <= 0xffff)
13525         op = DW_OP_const2u;
13526       else if (HOST_BITS_PER_WIDE_INT == 32
13527                || i <= 0xffffffff)
13528         op = DW_OP_const4u;
13529       else
13530         op = DW_OP_constu;
13531     }
13532   else
13533     {
13534       if (i >= -0x80)
13535         op = DW_OP_const1s;
13536       else if (i >= -0x8000)
13537         op = DW_OP_const2s;
13538       else if (HOST_BITS_PER_WIDE_INT == 32
13539                || i >= -0x80000000)
13540         op = DW_OP_const4s;
13541       else
13542         op = DW_OP_consts;
13543     }
13544
13545   return new_loc_descr (op, i, 0);
13546 }
13547
13548 /* Return loc description representing "address" of integer value.
13549    This can appear only as toplevel expression.  */
13550
13551 static dw_loc_descr_ref
13552 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13553 {
13554   int litsize;
13555   dw_loc_descr_ref loc_result = NULL;
13556
13557   if (!(dwarf_version >= 4 || !dwarf_strict))
13558     return NULL;
13559
13560   if (i >= 0)
13561     {
13562       if (i <= 31)
13563         litsize = 1;
13564       else if (i <= 0xff)
13565         litsize = 2;
13566       else if (i <= 0xffff)
13567         litsize = 3;
13568       else if (HOST_BITS_PER_WIDE_INT == 32
13569                || i <= 0xffffffff)
13570         litsize = 5;
13571       else
13572         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13573     }
13574   else
13575     {
13576       if (i >= -0x80)
13577         litsize = 2;
13578       else if (i >= -0x8000)
13579         litsize = 3;
13580       else if (HOST_BITS_PER_WIDE_INT == 32
13581                || i >= -0x80000000)
13582         litsize = 5;
13583       else
13584         litsize = 1 + size_of_sleb128 (i);
13585     }
13586   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13587      is more compact.  For DW_OP_stack_value we need:
13588      litsize + 1 (DW_OP_stack_value)
13589      and for DW_OP_implicit_value:
13590      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13591   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13592     {
13593       loc_result = int_loc_descriptor (i);
13594       add_loc_descr (&loc_result,
13595                      new_loc_descr (DW_OP_stack_value, 0, 0));
13596       return loc_result;
13597     }
13598
13599   loc_result = new_loc_descr (DW_OP_implicit_value,
13600                               size, 0);
13601   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13602   loc_result->dw_loc_oprnd2.v.val_int = i;
13603   return loc_result;
13604 }
13605
13606 /* Return a location descriptor that designates a base+offset location.  */
13607
13608 static dw_loc_descr_ref
13609 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13610                  enum var_init_status initialized)
13611 {
13612   unsigned int regno;
13613   dw_loc_descr_ref result;
13614   dw_fde_ref fde = current_fde ();
13615
13616   /* We only use "frame base" when we're sure we're talking about the
13617      post-prologue local stack frame.  We do this by *not* running
13618      register elimination until this point, and recognizing the special
13619      argument pointer and soft frame pointer rtx's.  */
13620   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13621     {
13622       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13623
13624       if (elim != reg)
13625         {
13626           if (GET_CODE (elim) == PLUS)
13627             {
13628               offset += INTVAL (XEXP (elim, 1));
13629               elim = XEXP (elim, 0);
13630             }
13631           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13632                        && (elim == hard_frame_pointer_rtx
13633                            || elim == stack_pointer_rtx))
13634                       || elim == (frame_pointer_needed
13635                                   ? hard_frame_pointer_rtx
13636                                   : stack_pointer_rtx));
13637
13638           /* If drap register is used to align stack, use frame
13639              pointer + offset to access stack variables.  If stack
13640              is aligned without drap, use stack pointer + offset to
13641              access stack variables.  */
13642           if (crtl->stack_realign_tried
13643               && reg == frame_pointer_rtx)
13644             {
13645               int base_reg
13646                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13647                                       ? HARD_FRAME_POINTER_REGNUM
13648                                       : REGNO (elim));
13649               return new_reg_loc_descr (base_reg, offset);
13650             }
13651
13652           gcc_assert (frame_pointer_fb_offset_valid);
13653           offset += frame_pointer_fb_offset;
13654           return new_loc_descr (DW_OP_fbreg, offset, 0);
13655         }
13656     }
13657   else if (!optimize
13658            && fde
13659            && (fde->drap_reg == REGNO (reg)
13660                || fde->vdrap_reg == REGNO (reg)))
13661     {
13662       /* Use cfa+offset to represent the location of arguments passed
13663          on the stack when drap is used to align stack.
13664          Only do this when not optimizing, for optimized code var-tracking
13665          is supposed to track where the arguments live and the register
13666          used as vdrap or drap in some spot might be used for something
13667          else in other part of the routine.  */
13668       return new_loc_descr (DW_OP_fbreg, offset, 0);
13669     }
13670
13671   regno = dbx_reg_number (reg);
13672   if (regno <= 31)
13673     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13674                             offset, 0);
13675   else
13676     result = new_loc_descr (DW_OP_bregx, regno, offset);
13677
13678   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13679     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13680
13681   return result;
13682 }
13683
13684 /* Return true if this RTL expression describes a base+offset calculation.  */
13685
13686 static inline int
13687 is_based_loc (const_rtx rtl)
13688 {
13689   return (GET_CODE (rtl) == PLUS
13690           && ((REG_P (XEXP (rtl, 0))
13691                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13692                && CONST_INT_P (XEXP (rtl, 1)))));
13693 }
13694
13695 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13696    failed.  */
13697
13698 static dw_loc_descr_ref
13699 tls_mem_loc_descriptor (rtx mem)
13700 {
13701   tree base;
13702   dw_loc_descr_ref loc_result;
13703
13704   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13705     return NULL;
13706
13707   base = get_base_address (MEM_EXPR (mem));
13708   if (base == NULL
13709       || TREE_CODE (base) != VAR_DECL
13710       || !DECL_THREAD_LOCAL_P (base))
13711     return NULL;
13712
13713   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13714   if (loc_result == NULL)
13715     return NULL;
13716
13717   if (INTVAL (MEM_OFFSET (mem)))
13718     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13719
13720   return loc_result;
13721 }
13722
13723 /* Output debug info about reason why we failed to expand expression as dwarf
13724    expression.  */
13725
13726 static void
13727 expansion_failed (tree expr, rtx rtl, char const *reason)
13728 {
13729   if (dump_file && (dump_flags & TDF_DETAILS))
13730     {
13731       fprintf (dump_file, "Failed to expand as dwarf: ");
13732       if (expr)
13733         print_generic_expr (dump_file, expr, dump_flags);
13734       if (rtl)
13735         {
13736           fprintf (dump_file, "\n");
13737           print_rtl (dump_file, rtl);
13738         }
13739       fprintf (dump_file, "\nReason: %s\n", reason);
13740     }
13741 }
13742
13743 /* Helper function for const_ok_for_output, called either directly
13744    or via for_each_rtx.  */
13745
13746 static int
13747 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13748 {
13749   rtx rtl = *rtlp;
13750
13751   if (GET_CODE (rtl) == UNSPEC)
13752     {
13753       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13754          we can't express it in the debug info.  */
13755 #ifdef ENABLE_CHECKING
13756       /* Don't complain about TLS UNSPECs, those are just too hard to
13757          delegitimize.  */
13758       if (XVECLEN (rtl, 0) != 1
13759           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13760           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13761           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13762           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13763         inform (current_function_decl
13764                 ? DECL_SOURCE_LOCATION (current_function_decl)
13765                 : UNKNOWN_LOCATION,
13766                 "non-delegitimized UNSPEC %d found in variable location",
13767                 XINT (rtl, 1));
13768 #endif
13769       expansion_failed (NULL_TREE, rtl,
13770                         "UNSPEC hasn't been delegitimized.\n");
13771       return 1;
13772     }
13773
13774   if (GET_CODE (rtl) != SYMBOL_REF)
13775     return 0;
13776
13777   if (CONSTANT_POOL_ADDRESS_P (rtl))
13778     {
13779       bool marked;
13780       get_pool_constant_mark (rtl, &marked);
13781       /* If all references to this pool constant were optimized away,
13782          it was not output and thus we can't represent it.  */
13783       if (!marked)
13784         {
13785           expansion_failed (NULL_TREE, rtl,
13786                             "Constant was removed from constant pool.\n");
13787           return 1;
13788         }
13789     }
13790
13791   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13792     return 1;
13793
13794   /* Avoid references to external symbols in debug info, on several targets
13795      the linker might even refuse to link when linking a shared library,
13796      and in many other cases the relocations for .debug_info/.debug_loc are
13797      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13798      to be defined within the same shared library or executable are fine.  */
13799   if (SYMBOL_REF_EXTERNAL_P (rtl))
13800     {
13801       tree decl = SYMBOL_REF_DECL (rtl);
13802
13803       if (decl == NULL || !targetm.binds_local_p (decl))
13804         {
13805           expansion_failed (NULL_TREE, rtl,
13806                             "Symbol not defined in current TU.\n");
13807           return 1;
13808         }
13809     }
13810
13811   return 0;
13812 }
13813
13814 /* Return true if constant RTL can be emitted in DW_OP_addr or
13815    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13816    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13817
13818 static bool
13819 const_ok_for_output (rtx rtl)
13820 {
13821   if (GET_CODE (rtl) == SYMBOL_REF)
13822     return const_ok_for_output_1 (&rtl, NULL) == 0;
13823
13824   if (GET_CODE (rtl) == CONST)
13825     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13826
13827   return true;
13828 }
13829
13830 /* The following routine converts the RTL for a variable or parameter
13831    (resident in memory) into an equivalent Dwarf representation of a
13832    mechanism for getting the address of that same variable onto the top of a
13833    hypothetical "address evaluation" stack.
13834
13835    When creating memory location descriptors, we are effectively transforming
13836    the RTL for a memory-resident object into its Dwarf postfix expression
13837    equivalent.  This routine recursively descends an RTL tree, turning
13838    it into Dwarf postfix code as it goes.
13839
13840    MODE is the mode of the memory reference, needed to handle some
13841    autoincrement addressing modes.
13842
13843    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13844    location list for RTL.
13845
13846    Return 0 if we can't represent the location.  */
13847
13848 static dw_loc_descr_ref
13849 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13850                     enum var_init_status initialized)
13851 {
13852   dw_loc_descr_ref mem_loc_result = NULL;
13853   enum dwarf_location_atom op;
13854   dw_loc_descr_ref op0, op1;
13855
13856   /* Note that for a dynamically sized array, the location we will generate a
13857      description of here will be the lowest numbered location which is
13858      actually within the array.  That's *not* necessarily the same as the
13859      zeroth element of the array.  */
13860
13861   rtl = targetm.delegitimize_address (rtl);
13862
13863   switch (GET_CODE (rtl))
13864     {
13865     case POST_INC:
13866     case POST_DEC:
13867     case POST_MODIFY:
13868       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13869
13870     case SUBREG:
13871       /* The case of a subreg may arise when we have a local (register)
13872          variable or a formal (register) parameter which doesn't quite fill
13873          up an entire register.  For now, just assume that it is
13874          legitimate to make the Dwarf info refer to the whole register which
13875          contains the given subreg.  */
13876       if (!subreg_lowpart_p (rtl))
13877         break;
13878       rtl = SUBREG_REG (rtl);
13879       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13880         break;
13881       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13882         break;
13883       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13884       break;
13885
13886     case REG:
13887       /* Whenever a register number forms a part of the description of the
13888          method for calculating the (dynamic) address of a memory resident
13889          object, DWARF rules require the register number be referred to as
13890          a "base register".  This distinction is not based in any way upon
13891          what category of register the hardware believes the given register
13892          belongs to.  This is strictly DWARF terminology we're dealing with
13893          here. Note that in cases where the location of a memory-resident
13894          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13895          OP_CONST (0)) the actual DWARF location descriptor that we generate
13896          may just be OP_BASEREG (basereg).  This may look deceptively like
13897          the object in question was allocated to a register (rather than in
13898          memory) so DWARF consumers need to be aware of the subtle
13899          distinction between OP_REG and OP_BASEREG.  */
13900       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13901         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13902       else if (stack_realign_drap
13903                && crtl->drap_reg
13904                && crtl->args.internal_arg_pointer == rtl
13905                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13906         {
13907           /* If RTL is internal_arg_pointer, which has been optimized
13908              out, use DRAP instead.  */
13909           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13910                                             VAR_INIT_STATUS_INITIALIZED);
13911         }
13912       break;
13913
13914     case SIGN_EXTEND:
13915     case ZERO_EXTEND:
13916       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13917                                 VAR_INIT_STATUS_INITIALIZED);
13918       if (op0 == 0)
13919         break;
13920       else
13921         {
13922           int shift = DWARF2_ADDR_SIZE
13923                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13924           shift *= BITS_PER_UNIT;
13925           if (GET_CODE (rtl) == SIGN_EXTEND)
13926             op = DW_OP_shra;
13927           else
13928             op = DW_OP_shr;
13929           mem_loc_result = op0;
13930           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13931           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13932           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13933           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13934         }
13935       break;
13936
13937     case MEM:
13938       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13939                                            VAR_INIT_STATUS_INITIALIZED);
13940       if (mem_loc_result == NULL)
13941         mem_loc_result = tls_mem_loc_descriptor (rtl);
13942       if (mem_loc_result != 0)
13943         {
13944           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13945             {
13946               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13947               return 0;
13948             }
13949           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13950             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13951           else
13952             add_loc_descr (&mem_loc_result,
13953                            new_loc_descr (DW_OP_deref_size,
13954                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13955         }
13956       else
13957         {
13958           rtx new_rtl = avoid_constant_pool_reference (rtl);
13959           if (new_rtl != rtl)
13960             return mem_loc_descriptor (new_rtl, mode, initialized);
13961         }
13962       break;
13963
13964     case LO_SUM:
13965          rtl = XEXP (rtl, 1);
13966
13967       /* ... fall through ...  */
13968
13969     case LABEL_REF:
13970       /* Some ports can transform a symbol ref into a label ref, because
13971          the symbol ref is too far away and has to be dumped into a constant
13972          pool.  */
13973     case CONST:
13974     case SYMBOL_REF:
13975       if (GET_CODE (rtl) == SYMBOL_REF
13976           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13977         {
13978           dw_loc_descr_ref temp;
13979
13980           /* If this is not defined, we have no way to emit the data.  */
13981           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13982             break;
13983
13984           /* We used to emit DW_OP_addr here, but that's wrong, since
13985              DW_OP_addr should be relocated by the debug info consumer,
13986              while DW_OP_GNU_push_tls_address operand should not.  */
13987           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13988                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13989           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13990           temp->dw_loc_oprnd1.v.val_addr = rtl;
13991           temp->dtprel = true;
13992
13993           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13994           add_loc_descr (&mem_loc_result, temp);
13995
13996           break;
13997         }
13998
13999       if (!const_ok_for_output (rtl))
14000         break;
14001
14002     symref:
14003       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14004       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14005       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14006       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14007       break;
14008
14009     case CONCAT:
14010     case CONCATN:
14011     case VAR_LOCATION:
14012     case DEBUG_IMPLICIT_PTR:
14013       expansion_failed (NULL_TREE, rtl,
14014                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14015       return 0;
14016
14017     case PRE_MODIFY:
14018       /* Extract the PLUS expression nested inside and fall into
14019          PLUS code below.  */
14020       rtl = XEXP (rtl, 1);
14021       goto plus;
14022
14023     case PRE_INC:
14024     case PRE_DEC:
14025       /* Turn these into a PLUS expression and fall into the PLUS code
14026          below.  */
14027       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
14028                           GEN_INT (GET_CODE (rtl) == PRE_INC
14029                                    ? GET_MODE_UNIT_SIZE (mode)
14030                                    : -GET_MODE_UNIT_SIZE (mode)));
14031
14032       /* ... fall through ...  */
14033
14034     case PLUS:
14035     plus:
14036       if (is_based_loc (rtl))
14037         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14038                                           INTVAL (XEXP (rtl, 1)),
14039                                           VAR_INIT_STATUS_INITIALIZED);
14040       else
14041         {
14042           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
14043                                                VAR_INIT_STATUS_INITIALIZED);
14044           if (mem_loc_result == 0)
14045             break;
14046
14047           if (CONST_INT_P (XEXP (rtl, 1)))
14048             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14049           else
14050             {
14051               dw_loc_descr_ref mem_loc_result2
14052                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14053                                       VAR_INIT_STATUS_INITIALIZED);
14054               if (mem_loc_result2 == 0)
14055                 break;
14056               add_loc_descr (&mem_loc_result, mem_loc_result2);
14057               add_loc_descr (&mem_loc_result,
14058                              new_loc_descr (DW_OP_plus, 0, 0));
14059             }
14060         }
14061       break;
14062
14063     /* If a pseudo-reg is optimized away, it is possible for it to
14064        be replaced with a MEM containing a multiply or shift.  */
14065     case MINUS:
14066       op = DW_OP_minus;
14067       goto do_binop;
14068
14069     case MULT:
14070       op = DW_OP_mul;
14071       goto do_binop;
14072
14073     case DIV:
14074       op = DW_OP_div;
14075       goto do_binop;
14076
14077     case UMOD:
14078       op = DW_OP_mod;
14079       goto do_binop;
14080
14081     case ASHIFT:
14082       op = DW_OP_shl;
14083       goto do_binop;
14084
14085     case ASHIFTRT:
14086       op = DW_OP_shra;
14087       goto do_binop;
14088
14089     case LSHIFTRT:
14090       op = DW_OP_shr;
14091       goto do_binop;
14092
14093     case AND:
14094       op = DW_OP_and;
14095       goto do_binop;
14096
14097     case IOR:
14098       op = DW_OP_or;
14099       goto do_binop;
14100
14101     case XOR:
14102       op = DW_OP_xor;
14103       goto do_binop;
14104
14105     do_binop:
14106       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14107                                 VAR_INIT_STATUS_INITIALIZED);
14108       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14109                                 VAR_INIT_STATUS_INITIALIZED);
14110
14111       if (op0 == 0 || op1 == 0)
14112         break;
14113
14114       mem_loc_result = op0;
14115       add_loc_descr (&mem_loc_result, op1);
14116       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14117       break;
14118
14119     case MOD:
14120       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14121                                 VAR_INIT_STATUS_INITIALIZED);
14122       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14123                                 VAR_INIT_STATUS_INITIALIZED);
14124
14125       if (op0 == 0 || op1 == 0)
14126         break;
14127
14128       mem_loc_result = op0;
14129       add_loc_descr (&mem_loc_result, op1);
14130       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14131       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14132       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14133       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14134       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14135       break;
14136
14137     case NOT:
14138       op = DW_OP_not;
14139       goto do_unop;
14140
14141     case ABS:
14142       op = DW_OP_abs;
14143       goto do_unop;
14144
14145     case NEG:
14146       op = DW_OP_neg;
14147       goto do_unop;
14148
14149     do_unop:
14150       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14151                                 VAR_INIT_STATUS_INITIALIZED);
14152
14153       if (op0 == 0)
14154         break;
14155
14156       mem_loc_result = op0;
14157       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14158       break;
14159
14160     case CONST_INT:
14161       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14162       break;
14163
14164     case EQ:
14165       op = DW_OP_eq;
14166       goto do_scompare;
14167
14168     case GE:
14169       op = DW_OP_ge;
14170       goto do_scompare;
14171
14172     case GT:
14173       op = DW_OP_gt;
14174       goto do_scompare;
14175
14176     case LE:
14177       op = DW_OP_le;
14178       goto do_scompare;
14179
14180     case LT:
14181       op = DW_OP_lt;
14182       goto do_scompare;
14183
14184     case NE:
14185       op = DW_OP_ne;
14186       goto do_scompare;
14187
14188     do_scompare:
14189       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14190           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14191         break;
14192       else
14193         {
14194           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14195
14196           if (op_mode == VOIDmode)
14197             op_mode = GET_MODE (XEXP (rtl, 1));
14198           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14199             break;
14200
14201           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14202                                     VAR_INIT_STATUS_INITIALIZED);
14203           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14204                                     VAR_INIT_STATUS_INITIALIZED);
14205
14206           if (op0 == 0 || op1 == 0)
14207             break;
14208
14209           if (op_mode != VOIDmode
14210               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14211             {
14212               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14213               shift *= BITS_PER_UNIT;
14214               /* For eq/ne, if the operands are known to be zero-extended,
14215                  there is no need to do the fancy shifting up.  */
14216               if (op == DW_OP_eq || op == DW_OP_ne)
14217                 {
14218                   dw_loc_descr_ref last0, last1;
14219                   for (last0 = op0;
14220                        last0->dw_loc_next != NULL;
14221                        last0 = last0->dw_loc_next)
14222                     ;
14223                   for (last1 = op1;
14224                        last1->dw_loc_next != NULL;
14225                        last1 = last1->dw_loc_next)
14226                     ;
14227                   /* deref_size zero extends, and for constants we can check
14228                      whether they are zero extended or not.  */
14229                   if (((last0->dw_loc_opc == DW_OP_deref_size
14230                         && last0->dw_loc_oprnd1.v.val_int
14231                            <= GET_MODE_SIZE (op_mode))
14232                        || (CONST_INT_P (XEXP (rtl, 0))
14233                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14234                                == (INTVAL (XEXP (rtl, 0))
14235                                    & GET_MODE_MASK (op_mode))))
14236                       && ((last1->dw_loc_opc == DW_OP_deref_size
14237                            && last1->dw_loc_oprnd1.v.val_int
14238                               <= GET_MODE_SIZE (op_mode))
14239                           || (CONST_INT_P (XEXP (rtl, 1))
14240                               && (unsigned HOST_WIDE_INT)
14241                                  INTVAL (XEXP (rtl, 1))
14242                                  == (INTVAL (XEXP (rtl, 1))
14243                                      & GET_MODE_MASK (op_mode)))))
14244                     goto do_compare;
14245                 }
14246               add_loc_descr (&op0, int_loc_descriptor (shift));
14247               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14248               if (CONST_INT_P (XEXP (rtl, 1)))
14249                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14250               else
14251                 {
14252                   add_loc_descr (&op1, int_loc_descriptor (shift));
14253                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14254                 }
14255             }
14256         }
14257
14258     do_compare:
14259       mem_loc_result = op0;
14260       add_loc_descr (&mem_loc_result, op1);
14261       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14262       if (STORE_FLAG_VALUE != 1)
14263         {
14264           add_loc_descr (&mem_loc_result,
14265                          int_loc_descriptor (STORE_FLAG_VALUE));
14266           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14267         }
14268       break;
14269
14270     case GEU:
14271       op = DW_OP_ge;
14272       goto do_ucompare;
14273
14274     case GTU:
14275       op = DW_OP_gt;
14276       goto do_ucompare;
14277
14278     case LEU:
14279       op = DW_OP_le;
14280       goto do_ucompare;
14281
14282     case LTU:
14283       op = DW_OP_lt;
14284       goto do_ucompare;
14285
14286     do_ucompare:
14287       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14288           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14289         break;
14290       else
14291         {
14292           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14293
14294           if (op_mode == VOIDmode)
14295             op_mode = GET_MODE (XEXP (rtl, 1));
14296           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14297             break;
14298
14299           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14300                                     VAR_INIT_STATUS_INITIALIZED);
14301           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14302                                     VAR_INIT_STATUS_INITIALIZED);
14303
14304           if (op0 == 0 || op1 == 0)
14305             break;
14306
14307           if (op_mode != VOIDmode
14308               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14309             {
14310               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14311               dw_loc_descr_ref last0, last1;
14312               for (last0 = op0;
14313                    last0->dw_loc_next != NULL;
14314                    last0 = last0->dw_loc_next)
14315                 ;
14316               for (last1 = op1;
14317                    last1->dw_loc_next != NULL;
14318                    last1 = last1->dw_loc_next)
14319                 ;
14320               if (CONST_INT_P (XEXP (rtl, 0)))
14321                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14322               /* deref_size zero extends, so no need to mask it again.  */
14323               else if (last0->dw_loc_opc != DW_OP_deref_size
14324                        || last0->dw_loc_oprnd1.v.val_int
14325                           > GET_MODE_SIZE (op_mode))
14326                 {
14327                   add_loc_descr (&op0, int_loc_descriptor (mask));
14328                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14329                 }
14330               if (CONST_INT_P (XEXP (rtl, 1)))
14331                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14332               /* deref_size zero extends, so no need to mask it again.  */
14333               else if (last1->dw_loc_opc != DW_OP_deref_size
14334                        || last1->dw_loc_oprnd1.v.val_int
14335                           > GET_MODE_SIZE (op_mode))
14336                 {
14337                   add_loc_descr (&op1, int_loc_descriptor (mask));
14338                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14339                 }
14340             }
14341           else
14342             {
14343               HOST_WIDE_INT bias = 1;
14344               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14345               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14346               if (CONST_INT_P (XEXP (rtl, 1)))
14347                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14348                                           + INTVAL (XEXP (rtl, 1)));
14349               else
14350                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14351                                                     bias, 0));
14352             }
14353         }
14354       goto do_compare;
14355
14356     case SMIN:
14357     case SMAX:
14358     case UMIN:
14359     case UMAX:
14360       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14361           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14362           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14363         break;
14364
14365       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14366                                 VAR_INIT_STATUS_INITIALIZED);
14367       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14368                                 VAR_INIT_STATUS_INITIALIZED);
14369
14370       if (op0 == 0 || op1 == 0)
14371         break;
14372
14373       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14374       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14375       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14376       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14377         {
14378           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14379             {
14380               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14381               add_loc_descr (&op0, int_loc_descriptor (mask));
14382               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14383               add_loc_descr (&op1, int_loc_descriptor (mask));
14384               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14385             }
14386           else
14387             {
14388               HOST_WIDE_INT bias = 1;
14389               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14390               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14391               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14392             }
14393         }
14394       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14395         {
14396           int shift = DWARF2_ADDR_SIZE
14397                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14398           shift *= BITS_PER_UNIT;
14399           add_loc_descr (&op0, int_loc_descriptor (shift));
14400           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14401           add_loc_descr (&op1, int_loc_descriptor (shift));
14402           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14403         }
14404
14405       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14406         op = DW_OP_lt;
14407       else
14408         op = DW_OP_gt;
14409       mem_loc_result = op0;
14410       add_loc_descr (&mem_loc_result, op1);
14411       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14412       {
14413         dw_loc_descr_ref bra_node, drop_node;
14414
14415         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14416         add_loc_descr (&mem_loc_result, bra_node);
14417         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14418         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14419         add_loc_descr (&mem_loc_result, drop_node);
14420         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14421         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14422       }
14423       break;
14424
14425     case ZERO_EXTRACT:
14426     case SIGN_EXTRACT:
14427       if (CONST_INT_P (XEXP (rtl, 1))
14428           && CONST_INT_P (XEXP (rtl, 2))
14429           && ((unsigned) INTVAL (XEXP (rtl, 1))
14430               + (unsigned) INTVAL (XEXP (rtl, 2))
14431               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14432           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14433           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14434         {
14435           int shift, size;
14436           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14437                                     VAR_INIT_STATUS_INITIALIZED);
14438           if (op0 == 0)
14439             break;
14440           if (GET_CODE (rtl) == SIGN_EXTRACT)
14441             op = DW_OP_shra;
14442           else
14443             op = DW_OP_shr;
14444           mem_loc_result = op0;
14445           size = INTVAL (XEXP (rtl, 1));
14446           shift = INTVAL (XEXP (rtl, 2));
14447           if (BITS_BIG_ENDIAN)
14448             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14449                     - shift - size;
14450           if (shift + size != (int) DWARF2_ADDR_SIZE)
14451             {
14452               add_loc_descr (&mem_loc_result,
14453                              int_loc_descriptor (DWARF2_ADDR_SIZE
14454                                                  - shift - size));
14455               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14456             }
14457           if (size != (int) DWARF2_ADDR_SIZE)
14458             {
14459               add_loc_descr (&mem_loc_result,
14460                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14461               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14462             }
14463         }
14464       break;
14465
14466     case IF_THEN_ELSE:
14467       {
14468         dw_loc_descr_ref op2, bra_node, drop_node;
14469         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14470                                   VAR_INIT_STATUS_INITIALIZED);
14471         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14472                                   VAR_INIT_STATUS_INITIALIZED);
14473         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14474                                   VAR_INIT_STATUS_INITIALIZED);
14475         if (op0 == NULL || op1 == NULL || op2 == NULL)
14476           break;
14477
14478         mem_loc_result = op1;
14479         add_loc_descr (&mem_loc_result, op2);
14480         add_loc_descr (&mem_loc_result, op0);
14481         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14482         add_loc_descr (&mem_loc_result, bra_node);
14483         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14484         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14485         add_loc_descr (&mem_loc_result, drop_node);
14486         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14487         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14488       }
14489       break;
14490
14491     case COMPARE:
14492     case ROTATE:
14493     case ROTATERT:
14494     case TRUNCATE:
14495       /* In theory, we could implement the above.  */
14496       /* DWARF cannot represent the unsigned compare operations
14497          natively.  */
14498     case SS_MULT:
14499     case US_MULT:
14500     case SS_DIV:
14501     case US_DIV:
14502     case SS_PLUS:
14503     case US_PLUS:
14504     case SS_MINUS:
14505     case US_MINUS:
14506     case SS_NEG:
14507     case US_NEG:
14508     case SS_ABS:
14509     case SS_ASHIFT:
14510     case US_ASHIFT:
14511     case SS_TRUNCATE:
14512     case US_TRUNCATE:
14513     case UDIV:
14514     case UNORDERED:
14515     case ORDERED:
14516     case UNEQ:
14517     case UNGE:
14518     case UNGT:
14519     case UNLE:
14520     case UNLT:
14521     case LTGT:
14522     case FLOAT_EXTEND:
14523     case FLOAT_TRUNCATE:
14524     case FLOAT:
14525     case UNSIGNED_FLOAT:
14526     case FIX:
14527     case UNSIGNED_FIX:
14528     case FRACT_CONVERT:
14529     case UNSIGNED_FRACT_CONVERT:
14530     case SAT_FRACT:
14531     case UNSIGNED_SAT_FRACT:
14532     case SQRT:
14533     case BSWAP:
14534     case FFS:
14535     case CLZ:
14536     case CTZ:
14537     case POPCOUNT:
14538     case PARITY:
14539     case ASM_OPERANDS:
14540     case VEC_MERGE:
14541     case VEC_SELECT:
14542     case VEC_CONCAT:
14543     case VEC_DUPLICATE:
14544     case UNSPEC:
14545     case HIGH:
14546       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14547          can't express it in the debug info.  This can happen e.g. with some
14548          TLS UNSPECs.  */
14549       break;
14550
14551     case CONST_STRING:
14552       resolve_one_addr (&rtl, NULL);
14553       goto symref;
14554
14555     default:
14556 #ifdef ENABLE_CHECKING
14557       print_rtl (stderr, rtl);
14558       gcc_unreachable ();
14559 #else
14560       break;
14561 #endif
14562     }
14563
14564   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14565     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14566
14567   return mem_loc_result;
14568 }
14569
14570 /* Return a descriptor that describes the concatenation of two locations.
14571    This is typically a complex variable.  */
14572
14573 static dw_loc_descr_ref
14574 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14575 {
14576   dw_loc_descr_ref cc_loc_result = NULL;
14577   dw_loc_descr_ref x0_ref
14578     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14579   dw_loc_descr_ref x1_ref
14580     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14581
14582   if (x0_ref == 0 || x1_ref == 0)
14583     return 0;
14584
14585   cc_loc_result = x0_ref;
14586   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14587
14588   add_loc_descr (&cc_loc_result, x1_ref);
14589   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14590
14591   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14592     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14593
14594   return cc_loc_result;
14595 }
14596
14597 /* Return a descriptor that describes the concatenation of N
14598    locations.  */
14599
14600 static dw_loc_descr_ref
14601 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14602 {
14603   unsigned int i;
14604   dw_loc_descr_ref cc_loc_result = NULL;
14605   unsigned int n = XVECLEN (concatn, 0);
14606
14607   for (i = 0; i < n; ++i)
14608     {
14609       dw_loc_descr_ref ref;
14610       rtx x = XVECEXP (concatn, 0, i);
14611
14612       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14613       if (ref == NULL)
14614         return NULL;
14615
14616       add_loc_descr (&cc_loc_result, ref);
14617       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14618     }
14619
14620   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14621     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14622
14623   return cc_loc_result;
14624 }
14625
14626 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14627    for DEBUG_IMPLICIT_PTR RTL.  */
14628
14629 static dw_loc_descr_ref
14630 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14631 {
14632   dw_loc_descr_ref ret;
14633   dw_die_ref ref;
14634
14635   if (dwarf_strict)
14636     return NULL;
14637   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14638               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14639               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14640   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14641   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14642   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14643   if (ref)
14644     {
14645       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14646       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14647       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14648     }
14649   else
14650     {
14651       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14652       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14653     }
14654   return ret;
14655 }
14656
14657 /* Output a proper Dwarf location descriptor for a variable or parameter
14658    which is either allocated in a register or in a memory location.  For a
14659    register, we just generate an OP_REG and the register number.  For a
14660    memory location we provide a Dwarf postfix expression describing how to
14661    generate the (dynamic) address of the object onto the address stack.
14662
14663    MODE is mode of the decl if this loc_descriptor is going to be used in
14664    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14665    allowed, VOIDmode otherwise.
14666
14667    If we don't know how to describe it, return 0.  */
14668
14669 static dw_loc_descr_ref
14670 loc_descriptor (rtx rtl, enum machine_mode mode,
14671                 enum var_init_status initialized)
14672 {
14673   dw_loc_descr_ref loc_result = NULL;
14674
14675   switch (GET_CODE (rtl))
14676     {
14677     case SUBREG:
14678       /* The case of a subreg may arise when we have a local (register)
14679          variable or a formal (register) parameter which doesn't quite fill
14680          up an entire register.  For now, just assume that it is
14681          legitimate to make the Dwarf info refer to the whole register which
14682          contains the given subreg.  */
14683       loc_result = loc_descriptor (SUBREG_REG (rtl),
14684                                    GET_MODE (SUBREG_REG (rtl)), initialized);
14685       break;
14686
14687     case REG:
14688       loc_result = reg_loc_descriptor (rtl, initialized);
14689       break;
14690
14691     case MEM:
14692       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14693                                        initialized);
14694       if (loc_result == NULL)
14695         loc_result = tls_mem_loc_descriptor (rtl);
14696       if (loc_result == NULL)
14697         {
14698           rtx new_rtl = avoid_constant_pool_reference (rtl);
14699           if (new_rtl != rtl)
14700             loc_result = loc_descriptor (new_rtl, mode, initialized);
14701         }
14702       break;
14703
14704     case CONCAT:
14705       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14706                                           initialized);
14707       break;
14708
14709     case CONCATN:
14710       loc_result = concatn_loc_descriptor (rtl, initialized);
14711       break;
14712
14713     case VAR_LOCATION:
14714       /* Single part.  */
14715       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14716         {
14717           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14718           if (GET_CODE (loc) == EXPR_LIST)
14719             loc = XEXP (loc, 0);
14720           loc_result = loc_descriptor (loc, mode, initialized);
14721           break;
14722         }
14723
14724       rtl = XEXP (rtl, 1);
14725       /* FALLTHRU */
14726
14727     case PARALLEL:
14728       {
14729         rtvec par_elems = XVEC (rtl, 0);
14730         int num_elem = GET_NUM_ELEM (par_elems);
14731         enum machine_mode mode;
14732         int i;
14733
14734         /* Create the first one, so we have something to add to.  */
14735         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14736                                      VOIDmode, initialized);
14737         if (loc_result == NULL)
14738           return NULL;
14739         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14740         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14741         for (i = 1; i < num_elem; i++)
14742           {
14743             dw_loc_descr_ref temp;
14744
14745             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14746                                    VOIDmode, initialized);
14747             if (temp == NULL)
14748               return NULL;
14749             add_loc_descr (&loc_result, temp);
14750             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14751             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14752           }
14753       }
14754       break;
14755
14756     case CONST_INT:
14757       if (mode != VOIDmode && mode != BLKmode)
14758         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14759                                                     INTVAL (rtl));
14760       break;
14761
14762     case CONST_DOUBLE:
14763       if (mode == VOIDmode)
14764         mode = GET_MODE (rtl);
14765
14766       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14767         {
14768           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14769
14770           /* Note that a CONST_DOUBLE rtx could represent either an integer
14771              or a floating-point constant.  A CONST_DOUBLE is used whenever
14772              the constant requires more than one word in order to be
14773              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14774           loc_result = new_loc_descr (DW_OP_implicit_value,
14775                                       GET_MODE_SIZE (mode), 0);
14776           if (SCALAR_FLOAT_MODE_P (mode))
14777             {
14778               unsigned int length = GET_MODE_SIZE (mode);
14779               unsigned char *array
14780                   = (unsigned char*) ggc_alloc_atomic (length);
14781
14782               insert_float (rtl, array);
14783               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14784               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14785               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14786               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14787             }
14788           else
14789             {
14790               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14791               loc_result->dw_loc_oprnd2.v.val_double
14792                 = rtx_to_double_int (rtl);
14793             }
14794         }
14795       break;
14796
14797     case CONST_VECTOR:
14798       if (mode == VOIDmode)
14799         mode = GET_MODE (rtl);
14800
14801       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14802         {
14803           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14804           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14805           unsigned char *array = (unsigned char *)
14806             ggc_alloc_atomic (length * elt_size);
14807           unsigned int i;
14808           unsigned char *p;
14809
14810           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14811           switch (GET_MODE_CLASS (mode))
14812             {
14813             case MODE_VECTOR_INT:
14814               for (i = 0, p = array; i < length; i++, p += elt_size)
14815                 {
14816                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14817                   double_int val = rtx_to_double_int (elt);
14818
14819                   if (elt_size <= sizeof (HOST_WIDE_INT))
14820                     insert_int (double_int_to_shwi (val), elt_size, p);
14821                   else
14822                     {
14823                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14824                       insert_double (val, p);
14825                     }
14826                 }
14827               break;
14828
14829             case MODE_VECTOR_FLOAT:
14830               for (i = 0, p = array; i < length; i++, p += elt_size)
14831                 {
14832                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14833                   insert_float (elt, p);
14834                 }
14835               break;
14836
14837             default:
14838               gcc_unreachable ();
14839             }
14840
14841           loc_result = new_loc_descr (DW_OP_implicit_value,
14842                                       length * elt_size, 0);
14843           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14844           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14845           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14846           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14847         }
14848       break;
14849
14850     case CONST:
14851       if (mode == VOIDmode
14852           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14853           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14854           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14855         {
14856           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14857           break;
14858         }
14859       /* FALLTHROUGH */
14860     case SYMBOL_REF:
14861       if (!const_ok_for_output (rtl))
14862         break;
14863     case LABEL_REF:
14864       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14865           && (dwarf_version >= 4 || !dwarf_strict))
14866         {
14867           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14868           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14869           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14870           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14871           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14872         }
14873       break;
14874
14875     case DEBUG_IMPLICIT_PTR:
14876       loc_result = implicit_ptr_descriptor (rtl, 0);
14877       break;
14878
14879     case PLUS:
14880       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14881           && CONST_INT_P (XEXP (rtl, 1)))
14882         {
14883           loc_result
14884             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14885           break;
14886         }
14887       /* FALLTHRU */
14888     default:
14889       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14890           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14891           && (dwarf_version >= 4 || !dwarf_strict))
14892         {
14893           /* Value expression.  */
14894           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14895           if (loc_result)
14896             add_loc_descr (&loc_result,
14897                            new_loc_descr (DW_OP_stack_value, 0, 0));
14898         }
14899       break;
14900     }
14901
14902   return loc_result;
14903 }
14904
14905 /* We need to figure out what section we should use as the base for the
14906    address ranges where a given location is valid.
14907    1. If this particular DECL has a section associated with it, use that.
14908    2. If this function has a section associated with it, use that.
14909    3. Otherwise, use the text section.
14910    XXX: If you split a variable across multiple sections, we won't notice.  */
14911
14912 static const char *
14913 secname_for_decl (const_tree decl)
14914 {
14915   const char *secname;
14916
14917   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14918     {
14919       tree sectree = DECL_SECTION_NAME (decl);
14920       secname = TREE_STRING_POINTER (sectree);
14921     }
14922   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14923     {
14924       tree sectree = DECL_SECTION_NAME (current_function_decl);
14925       secname = TREE_STRING_POINTER (sectree);
14926     }
14927   else if (cfun && in_cold_section_p)
14928     secname = crtl->subsections.cold_section_label;
14929   else
14930     secname = text_section_label;
14931
14932   return secname;
14933 }
14934
14935 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14936
14937 static bool
14938 decl_by_reference_p (tree decl)
14939 {
14940   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14941            || TREE_CODE (decl) == VAR_DECL)
14942           && DECL_BY_REFERENCE (decl));
14943 }
14944
14945 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14946    for VARLOC.  */
14947
14948 static dw_loc_descr_ref
14949 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14950                enum var_init_status initialized)
14951 {
14952   int have_address = 0;
14953   dw_loc_descr_ref descr;
14954   enum machine_mode mode;
14955
14956   if (want_address != 2)
14957     {
14958       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14959       /* Single part.  */
14960       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14961         {
14962           varloc = PAT_VAR_LOCATION_LOC (varloc);
14963           if (GET_CODE (varloc) == EXPR_LIST)
14964             varloc = XEXP (varloc, 0);
14965           mode = GET_MODE (varloc);
14966           if (MEM_P (varloc))
14967             {
14968               rtx addr = XEXP (varloc, 0);
14969               descr = mem_loc_descriptor (addr, mode, initialized);
14970               if (descr)
14971                 have_address = 1;
14972               else
14973                 {
14974                   rtx x = avoid_constant_pool_reference (varloc);
14975                   if (x != varloc)
14976                     descr = mem_loc_descriptor (x, mode, initialized);
14977                 }
14978             }
14979           else
14980             descr = mem_loc_descriptor (varloc, mode, initialized);
14981         }
14982       else
14983         return 0;
14984     }
14985   else
14986     {
14987       if (GET_CODE (varloc) == VAR_LOCATION)
14988         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14989       else
14990         mode = DECL_MODE (loc);
14991       descr = loc_descriptor (varloc, mode, initialized);
14992       have_address = 1;
14993     }
14994
14995   if (!descr)
14996     return 0;
14997
14998   if (want_address == 2 && !have_address
14999       && (dwarf_version >= 4 || !dwarf_strict))
15000     {
15001       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15002         {
15003           expansion_failed (loc, NULL_RTX,
15004                             "DWARF address size mismatch");
15005           return 0;
15006         }
15007       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15008       have_address = 1;
15009     }
15010   /* Show if we can't fill the request for an address.  */
15011   if (want_address && !have_address)
15012     {
15013       expansion_failed (loc, NULL_RTX,
15014                         "Want address and only have value");
15015       return 0;
15016     }
15017
15018   /* If we've got an address and don't want one, dereference.  */
15019   if (!want_address && have_address)
15020     {
15021       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15022       enum dwarf_location_atom op;
15023
15024       if (size > DWARF2_ADDR_SIZE || size == -1)
15025         {
15026           expansion_failed (loc, NULL_RTX,
15027                             "DWARF address size mismatch");
15028           return 0;
15029         }
15030       else if (size == DWARF2_ADDR_SIZE)
15031         op = DW_OP_deref;
15032       else
15033         op = DW_OP_deref_size;
15034
15035       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15036     }
15037
15038   return descr;
15039 }
15040
15041 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15042    if it is not possible.  */
15043
15044 static dw_loc_descr_ref
15045 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15046 {
15047   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15048     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15049   else if (dwarf_version >= 3 || !dwarf_strict)
15050     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15051   else
15052     return NULL;
15053 }
15054
15055 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15056    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15057
15058 static dw_loc_descr_ref
15059 dw_sra_loc_expr (tree decl, rtx loc)
15060 {
15061   rtx p;
15062   unsigned int padsize = 0;
15063   dw_loc_descr_ref descr, *descr_tail;
15064   unsigned HOST_WIDE_INT decl_size;
15065   rtx varloc;
15066   enum var_init_status initialized;
15067
15068   if (DECL_SIZE (decl) == NULL
15069       || !host_integerp (DECL_SIZE (decl), 1))
15070     return NULL;
15071
15072   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15073   descr = NULL;
15074   descr_tail = &descr;
15075
15076   for (p = loc; p; p = XEXP (p, 1))
15077     {
15078       unsigned int bitsize = decl_piece_bitsize (p);
15079       rtx loc_note = *decl_piece_varloc_ptr (p);
15080       dw_loc_descr_ref cur_descr;
15081       dw_loc_descr_ref *tail, last = NULL;
15082       unsigned int opsize = 0;
15083
15084       if (loc_note == NULL_RTX
15085           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15086         {
15087           padsize += bitsize;
15088           continue;
15089         }
15090       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15091       varloc = NOTE_VAR_LOCATION (loc_note);
15092       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15093       if (cur_descr == NULL)
15094         {
15095           padsize += bitsize;
15096           continue;
15097         }
15098
15099       /* Check that cur_descr either doesn't use
15100          DW_OP_*piece operations, or their sum is equal
15101          to bitsize.  Otherwise we can't embed it.  */
15102       for (tail = &cur_descr; *tail != NULL;
15103            tail = &(*tail)->dw_loc_next)
15104         if ((*tail)->dw_loc_opc == DW_OP_piece)
15105           {
15106             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15107                       * BITS_PER_UNIT;
15108             last = *tail;
15109           }
15110         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15111           {
15112             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15113             last = *tail;
15114           }
15115
15116       if (last != NULL && opsize != bitsize)
15117         {
15118           padsize += bitsize;
15119           continue;
15120         }
15121
15122       /* If there is a hole, add DW_OP_*piece after empty DWARF
15123          expression, which means that those bits are optimized out.  */
15124       if (padsize)
15125         {
15126           if (padsize > decl_size)
15127             return NULL;
15128           decl_size -= padsize;
15129           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15130           if (*descr_tail == NULL)
15131             return NULL;
15132           descr_tail = &(*descr_tail)->dw_loc_next;
15133           padsize = 0;
15134         }
15135       *descr_tail = cur_descr;
15136       descr_tail = tail;
15137       if (bitsize > decl_size)
15138         return NULL;
15139       decl_size -= bitsize;
15140       if (last == NULL)
15141         {
15142           HOST_WIDE_INT offset = 0;
15143           if (GET_CODE (varloc) == VAR_LOCATION
15144               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15145             {
15146               varloc = PAT_VAR_LOCATION_LOC (varloc);
15147               if (GET_CODE (varloc) == EXPR_LIST)
15148                 varloc = XEXP (varloc, 0);
15149             }
15150           do 
15151             {
15152               if (GET_CODE (varloc) == CONST
15153                   || GET_CODE (varloc) == SIGN_EXTEND
15154                   || GET_CODE (varloc) == ZERO_EXTEND)
15155                 varloc = XEXP (varloc, 0);
15156               else if (GET_CODE (varloc) == SUBREG)
15157                 varloc = SUBREG_REG (varloc);
15158               else
15159                 break;
15160             }
15161           while (1);
15162           /* DW_OP_bit_size offset should be zero for register
15163              or implicit location descriptions and empty location
15164              descriptions, but for memory addresses needs big endian
15165              adjustment.  */
15166           if (MEM_P (varloc))
15167             {
15168               unsigned HOST_WIDE_INT memsize
15169                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15170               if (memsize != bitsize)
15171                 {
15172                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15173                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15174                     return NULL;
15175                   if (memsize < bitsize)
15176                     return NULL;
15177                   if (BITS_BIG_ENDIAN)
15178                     offset = memsize - bitsize;
15179                 }
15180             }
15181
15182           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15183           if (*descr_tail == NULL)
15184             return NULL;
15185           descr_tail = &(*descr_tail)->dw_loc_next;
15186         }
15187     }
15188
15189   /* If there were any non-empty expressions, add padding till the end of
15190      the decl.  */
15191   if (descr != NULL && decl_size != 0)
15192     {
15193       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15194       if (*descr_tail == NULL)
15195         return NULL;
15196     }
15197   return descr;
15198 }
15199
15200 /* Return the dwarf representation of the location list LOC_LIST of
15201    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15202    function.  */
15203
15204 static dw_loc_list_ref
15205 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15206 {
15207   const char *endname, *secname;
15208   rtx varloc;
15209   enum var_init_status initialized;
15210   struct var_loc_node *node;
15211   dw_loc_descr_ref descr;
15212   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15213   dw_loc_list_ref list = NULL;
15214   dw_loc_list_ref *listp = &list;
15215
15216   /* Now that we know what section we are using for a base,
15217      actually construct the list of locations.
15218      The first location information is what is passed to the
15219      function that creates the location list, and the remaining
15220      locations just get added on to that list.
15221      Note that we only know the start address for a location
15222      (IE location changes), so to build the range, we use
15223      the range [current location start, next location start].
15224      This means we have to special case the last node, and generate
15225      a range of [last location start, end of function label].  */
15226
15227   secname = secname_for_decl (decl);
15228
15229   for (node = loc_list->first; node; node = node->next)
15230     if (GET_CODE (node->loc) == EXPR_LIST
15231         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15232       {
15233         if (GET_CODE (node->loc) == EXPR_LIST)
15234           {
15235             /* This requires DW_OP_{,bit_}piece, which is not usable
15236                inside DWARF expressions.  */
15237             if (want_address != 2)
15238               continue;
15239             descr = dw_sra_loc_expr (decl, node->loc);
15240             if (descr == NULL)
15241               continue;
15242           }
15243         else
15244           {
15245             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15246             varloc = NOTE_VAR_LOCATION (node->loc);
15247             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15248           }
15249         if (descr)
15250           {
15251             bool range_across_switch = false;
15252             /* If section switch happens in between node->label
15253                and node->next->label (or end of function) and
15254                we can't emit it as a single entry list,
15255                emit two ranges, first one ending at the end
15256                of first partition and second one starting at the
15257                beginning of second partition.  */
15258             if (node == loc_list->last_before_switch
15259                 && (node != loc_list->first || loc_list->first->next)
15260                 && current_function_decl)
15261               {
15262                 endname = current_fde ()->dw_fde_end;
15263                 range_across_switch = true;
15264               }
15265             /* The variable has a location between NODE->LABEL and
15266                NODE->NEXT->LABEL.  */
15267             else if (node->next)
15268               endname = node->next->label;
15269             /* If the variable has a location at the last label
15270                it keeps its location until the end of function.  */
15271             else if (!current_function_decl)
15272               endname = text_end_label;
15273             else
15274               {
15275                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15276                                              current_function_funcdef_no);
15277                 endname = ggc_strdup (label_id);
15278               }
15279
15280             *listp = new_loc_list (descr, node->label, endname, secname);
15281             listp = &(*listp)->dw_loc_next;
15282
15283             if (range_across_switch)
15284               {
15285                 if (GET_CODE (node->loc) == EXPR_LIST)
15286                   descr = dw_sra_loc_expr (decl, node->loc);
15287                 else
15288                   {
15289                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15290                     varloc = NOTE_VAR_LOCATION (node->loc);
15291                     descr = dw_loc_list_1 (decl, varloc, want_address,
15292                                            initialized);
15293                   }
15294                 gcc_assert (descr);
15295                 /* The variable has a location between NODE->LABEL and
15296                    NODE->NEXT->LABEL.  */
15297                 if (node->next)
15298                   endname = node->next->label;
15299                 else
15300                   endname = current_fde ()->dw_fde_second_end;
15301                 *listp = new_loc_list (descr,
15302                                        current_fde ()->dw_fde_second_begin,
15303                                        endname, secname);
15304                 listp = &(*listp)->dw_loc_next;
15305               }
15306           }
15307       }
15308
15309   /* Try to avoid the overhead of a location list emitting a location
15310      expression instead, but only if we didn't have more than one
15311      location entry in the first place.  If some entries were not
15312      representable, we don't want to pretend a single entry that was
15313      applies to the entire scope in which the variable is
15314      available.  */
15315   if (list && loc_list->first->next)
15316     gen_llsym (list);
15317
15318   return list;
15319 }
15320
15321 /* Return if the loc_list has only single element and thus can be represented
15322    as location description.   */
15323
15324 static bool
15325 single_element_loc_list_p (dw_loc_list_ref list)
15326 {
15327   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15328   return !list->ll_symbol;
15329 }
15330
15331 /* To each location in list LIST add loc descr REF.  */
15332
15333 static void
15334 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15335 {
15336   dw_loc_descr_ref copy;
15337   add_loc_descr (&list->expr, ref);
15338   list = list->dw_loc_next;
15339   while (list)
15340     {
15341       copy = ggc_alloc_dw_loc_descr_node ();
15342       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15343       add_loc_descr (&list->expr, copy);
15344       while (copy->dw_loc_next)
15345         {
15346           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15347           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15348           copy->dw_loc_next = new_copy;
15349           copy = new_copy;
15350         }
15351       list = list->dw_loc_next;
15352     }
15353 }
15354
15355 /* Given two lists RET and LIST
15356    produce location list that is result of adding expression in LIST
15357    to expression in RET on each possition in program.
15358    Might be destructive on both RET and LIST.
15359
15360    TODO: We handle only simple cases of RET or LIST having at most one
15361    element. General case would inolve sorting the lists in program order
15362    and merging them that will need some additional work.
15363    Adding that will improve quality of debug info especially for SRA-ed
15364    structures.  */
15365
15366 static void
15367 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15368 {
15369   if (!list)
15370     return;
15371   if (!*ret)
15372     {
15373       *ret = list;
15374       return;
15375     }
15376   if (!list->dw_loc_next)
15377     {
15378       add_loc_descr_to_each (*ret, list->expr);
15379       return;
15380     }
15381   if (!(*ret)->dw_loc_next)
15382     {
15383       add_loc_descr_to_each (list, (*ret)->expr);
15384       *ret = list;
15385       return;
15386     }
15387   expansion_failed (NULL_TREE, NULL_RTX,
15388                     "Don't know how to merge two non-trivial"
15389                     " location lists.\n");
15390   *ret = NULL;
15391   return;
15392 }
15393
15394 /* LOC is constant expression.  Try a luck, look it up in constant
15395    pool and return its loc_descr of its address.  */
15396
15397 static dw_loc_descr_ref
15398 cst_pool_loc_descr (tree loc)
15399 {
15400   /* Get an RTL for this, if something has been emitted.  */
15401   rtx rtl = lookup_constant_def (loc);
15402   enum machine_mode mode;
15403
15404   if (!rtl || !MEM_P (rtl))
15405     {
15406       gcc_assert (!rtl);
15407       return 0;
15408     }
15409   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15410
15411   /* TODO: We might get more coverage if we was actually delaying expansion
15412      of all expressions till end of compilation when constant pools are fully
15413      populated.  */
15414   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15415     {
15416       expansion_failed (loc, NULL_RTX,
15417                         "CST value in contant pool but not marked.");
15418       return 0;
15419     }
15420   mode = GET_MODE (rtl);
15421   rtl = XEXP (rtl, 0);
15422   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15423 }
15424
15425 /* Return dw_loc_list representing address of addr_expr LOC
15426    by looking for innder INDIRECT_REF expression and turing it
15427    into simple arithmetics.  */
15428
15429 static dw_loc_list_ref
15430 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15431 {
15432   tree obj, offset;
15433   HOST_WIDE_INT bitsize, bitpos, bytepos;
15434   enum machine_mode mode;
15435   int volatilep;
15436   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15437   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15438
15439   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15440                              &bitsize, &bitpos, &offset, &mode,
15441                              &unsignedp, &volatilep, false);
15442   STRIP_NOPS (obj);
15443   if (bitpos % BITS_PER_UNIT)
15444     {
15445       expansion_failed (loc, NULL_RTX, "bitfield access");
15446       return 0;
15447     }
15448   if (!INDIRECT_REF_P (obj))
15449     {
15450       expansion_failed (obj,
15451                         NULL_RTX, "no indirect ref in inner refrence");
15452       return 0;
15453     }
15454   if (!offset && !bitpos)
15455     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15456   else if (toplev
15457            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15458            && (dwarf_version >= 4 || !dwarf_strict))
15459     {
15460       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15461       if (!list_ret)
15462         return 0;
15463       if (offset)
15464         {
15465           /* Variable offset.  */
15466           list_ret1 = loc_list_from_tree (offset, 0);
15467           if (list_ret1 == 0)
15468             return 0;
15469           add_loc_list (&list_ret, list_ret1);
15470           if (!list_ret)
15471             return 0;
15472           add_loc_descr_to_each (list_ret,
15473                                  new_loc_descr (DW_OP_plus, 0, 0));
15474         }
15475       bytepos = bitpos / BITS_PER_UNIT;
15476       if (bytepos > 0)
15477         add_loc_descr_to_each (list_ret,
15478                                new_loc_descr (DW_OP_plus_uconst,
15479                                               bytepos, 0));
15480       else if (bytepos < 0)
15481         loc_list_plus_const (list_ret, bytepos);
15482       add_loc_descr_to_each (list_ret,
15483                              new_loc_descr (DW_OP_stack_value, 0, 0));
15484     }
15485   return list_ret;
15486 }
15487
15488
15489 /* Generate Dwarf location list representing LOC.
15490    If WANT_ADDRESS is false, expression computing LOC will be computed
15491    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15492    if WANT_ADDRESS is 2, expression computing address useable in location
15493      will be returned (i.e. DW_OP_reg can be used
15494      to refer to register values).  */
15495
15496 static dw_loc_list_ref
15497 loc_list_from_tree (tree loc, int want_address)
15498 {
15499   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15500   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15501   int have_address = 0;
15502   enum dwarf_location_atom op;
15503
15504   /* ??? Most of the time we do not take proper care for sign/zero
15505      extending the values properly.  Hopefully this won't be a real
15506      problem...  */
15507
15508   switch (TREE_CODE (loc))
15509     {
15510     case ERROR_MARK:
15511       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15512       return 0;
15513
15514     case PLACEHOLDER_EXPR:
15515       /* This case involves extracting fields from an object to determine the
15516          position of other fields.  We don't try to encode this here.  The
15517          only user of this is Ada, which encodes the needed information using
15518          the names of types.  */
15519       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15520       return 0;
15521
15522     case CALL_EXPR:
15523       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15524       /* There are no opcodes for these operations.  */
15525       return 0;
15526
15527     case PREINCREMENT_EXPR:
15528     case PREDECREMENT_EXPR:
15529     case POSTINCREMENT_EXPR:
15530     case POSTDECREMENT_EXPR:
15531       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15532       /* There are no opcodes for these operations.  */
15533       return 0;
15534
15535     case ADDR_EXPR:
15536       /* If we already want an address, see if there is INDIRECT_REF inside
15537          e.g. for &this->field.  */
15538       if (want_address)
15539         {
15540           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15541                        (loc, want_address == 2);
15542           if (list_ret)
15543             have_address = 1;
15544           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15545                    && (ret = cst_pool_loc_descr (loc)))
15546             have_address = 1;
15547         }
15548         /* Otherwise, process the argument and look for the address.  */
15549       if (!list_ret && !ret)
15550         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15551       else
15552         {
15553           if (want_address)
15554             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15555           return NULL;
15556         }
15557       break;
15558
15559     case VAR_DECL:
15560       if (DECL_THREAD_LOCAL_P (loc))
15561         {
15562           rtx rtl;
15563           enum dwarf_location_atom first_op;
15564           enum dwarf_location_atom second_op;
15565           bool dtprel = false;
15566
15567           if (targetm.have_tls)
15568             {
15569               /* If this is not defined, we have no way to emit the
15570                  data.  */
15571               if (!targetm.asm_out.output_dwarf_dtprel)
15572                 return 0;
15573
15574                /* The way DW_OP_GNU_push_tls_address is specified, we
15575                   can only look up addresses of objects in the current
15576                   module.  We used DW_OP_addr as first op, but that's
15577                   wrong, because DW_OP_addr is relocated by the debug
15578                   info consumer, while DW_OP_GNU_push_tls_address
15579                   operand shouldn't be.  */
15580               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15581                 return 0;
15582               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15583               dtprel = true;
15584               second_op = DW_OP_GNU_push_tls_address;
15585             }
15586           else
15587             {
15588               if (!targetm.emutls.debug_form_tls_address
15589                   || !(dwarf_version >= 3 || !dwarf_strict))
15590                 return 0;
15591               /* We stuffed the control variable into the DECL_VALUE_EXPR
15592                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15593                  no longer appear in gimple code.  We used the control
15594                  variable in specific so that we could pick it up here.  */
15595               loc = DECL_VALUE_EXPR (loc);
15596               first_op = DW_OP_addr;
15597               second_op = DW_OP_form_tls_address;
15598             }
15599
15600           rtl = rtl_for_decl_location (loc);
15601           if (rtl == NULL_RTX)
15602             return 0;
15603
15604           if (!MEM_P (rtl))
15605             return 0;
15606           rtl = XEXP (rtl, 0);
15607           if (! CONSTANT_P (rtl))
15608             return 0;
15609
15610           ret = new_loc_descr (first_op, 0, 0);
15611           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15612           ret->dw_loc_oprnd1.v.val_addr = rtl;
15613           ret->dtprel = dtprel;
15614
15615           ret1 = new_loc_descr (second_op, 0, 0);
15616           add_loc_descr (&ret, ret1);
15617
15618           have_address = 1;
15619           break;
15620         }
15621       /* FALLTHRU */
15622
15623     case PARM_DECL:
15624     case RESULT_DECL:
15625       if (DECL_HAS_VALUE_EXPR_P (loc))
15626         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15627                                    want_address);
15628       /* FALLTHRU */
15629
15630     case FUNCTION_DECL:
15631       {
15632         rtx rtl;
15633         var_loc_list *loc_list = lookup_decl_loc (loc);
15634
15635         if (loc_list && loc_list->first)
15636           {
15637             list_ret = dw_loc_list (loc_list, loc, want_address);
15638             have_address = want_address != 0;
15639             break;
15640           }
15641         rtl = rtl_for_decl_location (loc);
15642         if (rtl == NULL_RTX)
15643           {
15644             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15645             return 0;
15646           }
15647         else if (CONST_INT_P (rtl))
15648           {
15649             HOST_WIDE_INT val = INTVAL (rtl);
15650             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15651               val &= GET_MODE_MASK (DECL_MODE (loc));
15652             ret = int_loc_descriptor (val);
15653           }
15654         else if (GET_CODE (rtl) == CONST_STRING)
15655           {
15656             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15657             return 0;
15658           }
15659         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15660           {
15661             ret = new_loc_descr (DW_OP_addr, 0, 0);
15662             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15663             ret->dw_loc_oprnd1.v.val_addr = rtl;
15664           }
15665         else
15666           {
15667             enum machine_mode mode;
15668
15669             /* Certain constructs can only be represented at top-level.  */
15670             if (want_address == 2)
15671               {
15672                 ret = loc_descriptor (rtl, VOIDmode,
15673                                       VAR_INIT_STATUS_INITIALIZED);
15674                 have_address = 1;
15675               }
15676             else
15677               {
15678                 mode = GET_MODE (rtl);
15679                 if (MEM_P (rtl))
15680                   {
15681                     rtl = XEXP (rtl, 0);
15682                     have_address = 1;
15683                   }
15684                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15685               }
15686             if (!ret)
15687               expansion_failed (loc, rtl,
15688                                 "failed to produce loc descriptor for rtl");
15689           }
15690       }
15691       break;
15692
15693     case MEM_REF:
15694       /* ??? FIXME.  */
15695       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15696         return 0;
15697       /* Fallthru.  */
15698     case INDIRECT_REF:
15699       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15700       have_address = 1;
15701       break;
15702
15703     case COMPOUND_EXPR:
15704       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15705
15706     CASE_CONVERT:
15707     case VIEW_CONVERT_EXPR:
15708     case SAVE_EXPR:
15709     case MODIFY_EXPR:
15710       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15711
15712     case COMPONENT_REF:
15713     case BIT_FIELD_REF:
15714     case ARRAY_REF:
15715     case ARRAY_RANGE_REF:
15716     case REALPART_EXPR:
15717     case IMAGPART_EXPR:
15718       {
15719         tree obj, offset;
15720         HOST_WIDE_INT bitsize, bitpos, bytepos;
15721         enum machine_mode mode;
15722         int volatilep;
15723         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15724
15725         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15726                                    &unsignedp, &volatilep, false);
15727
15728         gcc_assert (obj != loc);
15729
15730         list_ret = loc_list_from_tree (obj,
15731                                        want_address == 2
15732                                        && !bitpos && !offset ? 2 : 1);
15733         /* TODO: We can extract value of the small expression via shifting even
15734            for nonzero bitpos.  */
15735         if (list_ret == 0)
15736           return 0;
15737         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15738           {
15739             expansion_failed (loc, NULL_RTX,
15740                               "bitfield access");
15741             return 0;
15742           }
15743
15744         if (offset != NULL_TREE)
15745           {
15746             /* Variable offset.  */
15747             list_ret1 = loc_list_from_tree (offset, 0);
15748             if (list_ret1 == 0)
15749               return 0;
15750             add_loc_list (&list_ret, list_ret1);
15751             if (!list_ret)
15752               return 0;
15753             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15754           }
15755
15756         bytepos = bitpos / BITS_PER_UNIT;
15757         if (bytepos > 0)
15758           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15759         else if (bytepos < 0)
15760           loc_list_plus_const (list_ret, bytepos);
15761
15762         have_address = 1;
15763         break;
15764       }
15765
15766     case INTEGER_CST:
15767       if ((want_address || !host_integerp (loc, 0))
15768           && (ret = cst_pool_loc_descr (loc)))
15769         have_address = 1;
15770       else if (want_address == 2
15771                && host_integerp (loc, 0)
15772                && (ret = address_of_int_loc_descriptor
15773                            (int_size_in_bytes (TREE_TYPE (loc)),
15774                             tree_low_cst (loc, 0))))
15775         have_address = 1;
15776       else if (host_integerp (loc, 0))
15777         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15778       else
15779         {
15780           expansion_failed (loc, NULL_RTX,
15781                             "Integer operand is not host integer");
15782           return 0;
15783         }
15784       break;
15785
15786     case CONSTRUCTOR:
15787     case REAL_CST:
15788     case STRING_CST:
15789     case COMPLEX_CST:
15790       if ((ret = cst_pool_loc_descr (loc)))
15791         have_address = 1;
15792       else
15793       /* We can construct small constants here using int_loc_descriptor.  */
15794         expansion_failed (loc, NULL_RTX,
15795                           "constructor or constant not in constant pool");
15796       break;
15797
15798     case TRUTH_AND_EXPR:
15799     case TRUTH_ANDIF_EXPR:
15800     case BIT_AND_EXPR:
15801       op = DW_OP_and;
15802       goto do_binop;
15803
15804     case TRUTH_XOR_EXPR:
15805     case BIT_XOR_EXPR:
15806       op = DW_OP_xor;
15807       goto do_binop;
15808
15809     case TRUTH_OR_EXPR:
15810     case TRUTH_ORIF_EXPR:
15811     case BIT_IOR_EXPR:
15812       op = DW_OP_or;
15813       goto do_binop;
15814
15815     case FLOOR_DIV_EXPR:
15816     case CEIL_DIV_EXPR:
15817     case ROUND_DIV_EXPR:
15818     case TRUNC_DIV_EXPR:
15819       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15820         return 0;
15821       op = DW_OP_div;
15822       goto do_binop;
15823
15824     case MINUS_EXPR:
15825       op = DW_OP_minus;
15826       goto do_binop;
15827
15828     case FLOOR_MOD_EXPR:
15829     case CEIL_MOD_EXPR:
15830     case ROUND_MOD_EXPR:
15831     case TRUNC_MOD_EXPR:
15832       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15833         {
15834           op = DW_OP_mod;
15835           goto do_binop;
15836         }
15837       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15838       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15839       if (list_ret == 0 || list_ret1 == 0)
15840         return 0;
15841
15842       add_loc_list (&list_ret, list_ret1);
15843       if (list_ret == 0)
15844         return 0;
15845       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15846       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15847       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15848       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15849       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15850       break;
15851
15852     case MULT_EXPR:
15853       op = DW_OP_mul;
15854       goto do_binop;
15855
15856     case LSHIFT_EXPR:
15857       op = DW_OP_shl;
15858       goto do_binop;
15859
15860     case RSHIFT_EXPR:
15861       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15862       goto do_binop;
15863
15864     case POINTER_PLUS_EXPR:
15865     case PLUS_EXPR:
15866       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15867         {
15868           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15869           if (list_ret == 0)
15870             return 0;
15871
15872           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15873           break;
15874         }
15875
15876       op = DW_OP_plus;
15877       goto do_binop;
15878
15879     case LE_EXPR:
15880       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15881         return 0;
15882
15883       op = DW_OP_le;
15884       goto do_binop;
15885
15886     case GE_EXPR:
15887       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15888         return 0;
15889
15890       op = DW_OP_ge;
15891       goto do_binop;
15892
15893     case LT_EXPR:
15894       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15895         return 0;
15896
15897       op = DW_OP_lt;
15898       goto do_binop;
15899
15900     case GT_EXPR:
15901       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15902         return 0;
15903
15904       op = DW_OP_gt;
15905       goto do_binop;
15906
15907     case EQ_EXPR:
15908       op = DW_OP_eq;
15909       goto do_binop;
15910
15911     case NE_EXPR:
15912       op = DW_OP_ne;
15913       goto do_binop;
15914
15915     do_binop:
15916       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15917       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15918       if (list_ret == 0 || list_ret1 == 0)
15919         return 0;
15920
15921       add_loc_list (&list_ret, list_ret1);
15922       if (list_ret == 0)
15923         return 0;
15924       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15925       break;
15926
15927     case TRUTH_NOT_EXPR:
15928     case BIT_NOT_EXPR:
15929       op = DW_OP_not;
15930       goto do_unop;
15931
15932     case ABS_EXPR:
15933       op = DW_OP_abs;
15934       goto do_unop;
15935
15936     case NEGATE_EXPR:
15937       op = DW_OP_neg;
15938       goto do_unop;
15939
15940     do_unop:
15941       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15942       if (list_ret == 0)
15943         return 0;
15944
15945       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15946       break;
15947
15948     case MIN_EXPR:
15949     case MAX_EXPR:
15950       {
15951         const enum tree_code code =
15952           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15953
15954         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15955                       build2 (code, integer_type_node,
15956                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15957                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15958       }
15959
15960       /* ... fall through ...  */
15961
15962     case COND_EXPR:
15963       {
15964         dw_loc_descr_ref lhs
15965           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15966         dw_loc_list_ref rhs
15967           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15968         dw_loc_descr_ref bra_node, jump_node, tmp;
15969
15970         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15971         if (list_ret == 0 || lhs == 0 || rhs == 0)
15972           return 0;
15973
15974         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15975         add_loc_descr_to_each (list_ret, bra_node);
15976
15977         add_loc_list (&list_ret, rhs);
15978         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15979         add_loc_descr_to_each (list_ret, jump_node);
15980
15981         add_loc_descr_to_each (list_ret, lhs);
15982         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15983         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15984
15985         /* ??? Need a node to point the skip at.  Use a nop.  */
15986         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15987         add_loc_descr_to_each (list_ret, tmp);
15988         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15989         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15990       }
15991       break;
15992
15993     case FIX_TRUNC_EXPR:
15994       return 0;
15995
15996     default:
15997       /* Leave front-end specific codes as simply unknown.  This comes
15998          up, for instance, with the C STMT_EXPR.  */
15999       if ((unsigned int) TREE_CODE (loc)
16000           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16001         {
16002           expansion_failed (loc, NULL_RTX,
16003                             "language specific tree node");
16004           return 0;
16005         }
16006
16007 #ifdef ENABLE_CHECKING
16008       /* Otherwise this is a generic code; we should just lists all of
16009          these explicitly.  We forgot one.  */
16010       gcc_unreachable ();
16011 #else
16012       /* In a release build, we want to degrade gracefully: better to
16013          generate incomplete debugging information than to crash.  */
16014       return NULL;
16015 #endif
16016     }
16017
16018   if (!ret && !list_ret)
16019     return 0;
16020
16021   if (want_address == 2 && !have_address
16022       && (dwarf_version >= 4 || !dwarf_strict))
16023     {
16024       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16025         {
16026           expansion_failed (loc, NULL_RTX,
16027                             "DWARF address size mismatch");
16028           return 0;
16029         }
16030       if (ret)
16031         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16032       else
16033         add_loc_descr_to_each (list_ret,
16034                                new_loc_descr (DW_OP_stack_value, 0, 0));
16035       have_address = 1;
16036     }
16037   /* Show if we can't fill the request for an address.  */
16038   if (want_address && !have_address)
16039     {
16040       expansion_failed (loc, NULL_RTX,
16041                         "Want address and only have value");
16042       return 0;
16043     }
16044
16045   gcc_assert (!ret || !list_ret);
16046
16047   /* If we've got an address and don't want one, dereference.  */
16048   if (!want_address && have_address)
16049     {
16050       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16051
16052       if (size > DWARF2_ADDR_SIZE || size == -1)
16053         {
16054           expansion_failed (loc, NULL_RTX,
16055                             "DWARF address size mismatch");
16056           return 0;
16057         }
16058       else if (size == DWARF2_ADDR_SIZE)
16059         op = DW_OP_deref;
16060       else
16061         op = DW_OP_deref_size;
16062
16063       if (ret)
16064         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16065       else
16066         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16067     }
16068   if (ret)
16069     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16070
16071   return list_ret;
16072 }
16073
16074 /* Same as above but return only single location expression.  */
16075 static dw_loc_descr_ref
16076 loc_descriptor_from_tree (tree loc, int want_address)
16077 {
16078   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
16079   if (!ret)
16080     return NULL;
16081   if (ret->dw_loc_next)
16082     {
16083       expansion_failed (loc, NULL_RTX,
16084                         "Location list where only loc descriptor needed");
16085       return NULL;
16086     }
16087   return ret->expr;
16088 }
16089
16090 /* Given a value, round it up to the lowest multiple of `boundary'
16091    which is not less than the value itself.  */
16092
16093 static inline HOST_WIDE_INT
16094 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16095 {
16096   return (((value + boundary - 1) / boundary) * boundary);
16097 }
16098
16099 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16100    pointer to the declared type for the relevant field variable, or return
16101    `integer_type_node' if the given node turns out to be an
16102    ERROR_MARK node.  */
16103
16104 static inline tree
16105 field_type (const_tree decl)
16106 {
16107   tree type;
16108
16109   if (TREE_CODE (decl) == ERROR_MARK)
16110     return integer_type_node;
16111
16112   type = DECL_BIT_FIELD_TYPE (decl);
16113   if (type == NULL_TREE)
16114     type = TREE_TYPE (decl);
16115
16116   return type;
16117 }
16118
16119 /* Given a pointer to a tree node, return the alignment in bits for
16120    it, or else return BITS_PER_WORD if the node actually turns out to
16121    be an ERROR_MARK node.  */
16122
16123 static inline unsigned
16124 simple_type_align_in_bits (const_tree type)
16125 {
16126   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16127 }
16128
16129 static inline unsigned
16130 simple_decl_align_in_bits (const_tree decl)
16131 {
16132   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16133 }
16134
16135 /* Return the result of rounding T up to ALIGN.  */
16136
16137 static inline double_int
16138 round_up_to_align (double_int t, unsigned int align)
16139 {
16140   double_int alignd = uhwi_to_double_int (align);
16141   t = double_int_add (t, alignd);
16142   t = double_int_add (t, double_int_minus_one);
16143   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16144   t = double_int_mul (t, alignd);
16145   return t;
16146 }
16147
16148 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16149    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16150    or return 0 if we are unable to determine what that offset is, either
16151    because the argument turns out to be a pointer to an ERROR_MARK node, or
16152    because the offset is actually variable.  (We can't handle the latter case
16153    just yet).  */
16154
16155 static HOST_WIDE_INT
16156 field_byte_offset (const_tree decl)
16157 {
16158   double_int object_offset_in_bits;
16159   double_int object_offset_in_bytes;
16160   double_int bitpos_int;
16161
16162   if (TREE_CODE (decl) == ERROR_MARK)
16163     return 0;
16164
16165   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16166
16167   /* We cannot yet cope with fields whose positions are variable, so
16168      for now, when we see such things, we simply return 0.  Someday, we may
16169      be able to handle such cases, but it will be damn difficult.  */
16170   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16171     return 0;
16172
16173   bitpos_int = tree_to_double_int (bit_position (decl));
16174
16175 #ifdef PCC_BITFIELD_TYPE_MATTERS
16176   if (PCC_BITFIELD_TYPE_MATTERS)
16177     {
16178       tree type;
16179       tree field_size_tree;
16180       double_int deepest_bitpos;
16181       double_int field_size_in_bits;
16182       unsigned int type_align_in_bits;
16183       unsigned int decl_align_in_bits;
16184       double_int type_size_in_bits;
16185
16186       type = field_type (decl);
16187       type_size_in_bits = double_int_type_size_in_bits (type);
16188       type_align_in_bits = simple_type_align_in_bits (type);
16189
16190       field_size_tree = DECL_SIZE (decl);
16191
16192       /* The size could be unspecified if there was an error, or for
16193          a flexible array member.  */
16194       if (!field_size_tree)
16195         field_size_tree = bitsize_zero_node;
16196
16197       /* If the size of the field is not constant, use the type size.  */
16198       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16199         field_size_in_bits = tree_to_double_int (field_size_tree);
16200       else
16201         field_size_in_bits = type_size_in_bits;
16202
16203       decl_align_in_bits = simple_decl_align_in_bits (decl);
16204
16205       /* The GCC front-end doesn't make any attempt to keep track of the
16206          starting bit offset (relative to the start of the containing
16207          structure type) of the hypothetical "containing object" for a
16208          bit-field.  Thus, when computing the byte offset value for the
16209          start of the "containing object" of a bit-field, we must deduce
16210          this information on our own. This can be rather tricky to do in
16211          some cases.  For example, handling the following structure type
16212          definition when compiling for an i386/i486 target (which only
16213          aligns long long's to 32-bit boundaries) can be very tricky:
16214
16215          struct S { int field1; long long field2:31; };
16216
16217          Fortunately, there is a simple rule-of-thumb which can be used
16218          in such cases.  When compiling for an i386/i486, GCC will
16219          allocate 8 bytes for the structure shown above.  It decides to
16220          do this based upon one simple rule for bit-field allocation.
16221          GCC allocates each "containing object" for each bit-field at
16222          the first (i.e. lowest addressed) legitimate alignment boundary
16223          (based upon the required minimum alignment for the declared
16224          type of the field) which it can possibly use, subject to the
16225          condition that there is still enough available space remaining
16226          in the containing object (when allocated at the selected point)
16227          to fully accommodate all of the bits of the bit-field itself.
16228
16229          This simple rule makes it obvious why GCC allocates 8 bytes for
16230          each object of the structure type shown above.  When looking
16231          for a place to allocate the "containing object" for `field2',
16232          the compiler simply tries to allocate a 64-bit "containing
16233          object" at each successive 32-bit boundary (starting at zero)
16234          until it finds a place to allocate that 64- bit field such that
16235          at least 31 contiguous (and previously unallocated) bits remain
16236          within that selected 64 bit field.  (As it turns out, for the
16237          example above, the compiler finds it is OK to allocate the
16238          "containing object" 64-bit field at bit-offset zero within the
16239          structure type.)
16240
16241          Here we attempt to work backwards from the limited set of facts
16242          we're given, and we try to deduce from those facts, where GCC
16243          must have believed that the containing object started (within
16244          the structure type). The value we deduce is then used (by the
16245          callers of this routine) to generate DW_AT_location and
16246          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16247          the case of DW_AT_location, regular fields as well).  */
16248
16249       /* Figure out the bit-distance from the start of the structure to
16250          the "deepest" bit of the bit-field.  */
16251       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16252
16253       /* This is the tricky part.  Use some fancy footwork to deduce
16254          where the lowest addressed bit of the containing object must
16255          be.  */
16256       object_offset_in_bits
16257         = double_int_sub (deepest_bitpos, type_size_in_bits);
16258
16259       /* Round up to type_align by default.  This works best for
16260          bitfields.  */
16261       object_offset_in_bits
16262         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16263
16264       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16265         {
16266           object_offset_in_bits
16267             = double_int_sub (deepest_bitpos, type_size_in_bits);
16268
16269           /* Round up to decl_align instead.  */
16270           object_offset_in_bits
16271             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16272         }
16273     }
16274   else
16275 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16276     object_offset_in_bits = bitpos_int;
16277
16278   object_offset_in_bytes
16279     = double_int_div (object_offset_in_bits,
16280                       uhwi_to_double_int (BITS_PER_UNIT), true,
16281                       TRUNC_DIV_EXPR);
16282   return double_int_to_shwi (object_offset_in_bytes);
16283 }
16284 \f
16285 /* The following routines define various Dwarf attributes and any data
16286    associated with them.  */
16287
16288 /* Add a location description attribute value to a DIE.
16289
16290    This emits location attributes suitable for whole variables and
16291    whole parameters.  Note that the location attributes for struct fields are
16292    generated by the routine `data_member_location_attribute' below.  */
16293
16294 static inline void
16295 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16296                              dw_loc_list_ref descr)
16297 {
16298   if (descr == 0)
16299     return;
16300   if (single_element_loc_list_p (descr))
16301     add_AT_loc (die, attr_kind, descr->expr);
16302   else
16303     add_AT_loc_list (die, attr_kind, descr);
16304 }
16305
16306 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16307
16308 static void
16309 add_accessibility_attribute (dw_die_ref die, tree decl)
16310 {
16311   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16312      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16313      the default has always been DW_ACCESS_public.  */
16314   if (TREE_PROTECTED (decl))
16315     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16316   else if (TREE_PRIVATE (decl))
16317     {
16318       if (dwarf_version == 2
16319           || die->die_parent == NULL
16320           || die->die_parent->die_tag != DW_TAG_class_type)
16321         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16322     }
16323   else if (dwarf_version > 2
16324            && die->die_parent
16325            && die->die_parent->die_tag == DW_TAG_class_type)
16326     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16327 }
16328
16329 /* Attach the specialized form of location attribute used for data members of
16330    struct and union types.  In the special case of a FIELD_DECL node which
16331    represents a bit-field, the "offset" part of this special location
16332    descriptor must indicate the distance in bytes from the lowest-addressed
16333    byte of the containing struct or union type to the lowest-addressed byte of
16334    the "containing object" for the bit-field.  (See the `field_byte_offset'
16335    function above).
16336
16337    For any given bit-field, the "containing object" is a hypothetical object
16338    (of some integral or enum type) within which the given bit-field lives.  The
16339    type of this hypothetical "containing object" is always the same as the
16340    declared type of the individual bit-field itself (for GCC anyway... the
16341    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16342    bytes) of the hypothetical "containing object" which will be given in the
16343    DW_AT_byte_size attribute for this bit-field.  (See the
16344    `byte_size_attribute' function below.)  It is also used when calculating the
16345    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16346    function below.)  */
16347
16348 static void
16349 add_data_member_location_attribute (dw_die_ref die, tree decl)
16350 {
16351   HOST_WIDE_INT offset;
16352   dw_loc_descr_ref loc_descr = 0;
16353
16354   if (TREE_CODE (decl) == TREE_BINFO)
16355     {
16356       /* We're working on the TAG_inheritance for a base class.  */
16357       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16358         {
16359           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16360              aren't at a fixed offset from all (sub)objects of the same
16361              type.  We need to extract the appropriate offset from our
16362              vtable.  The following dwarf expression means
16363
16364                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16365
16366              This is specific to the V3 ABI, of course.  */
16367
16368           dw_loc_descr_ref tmp;
16369
16370           /* Make a copy of the object address.  */
16371           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16372           add_loc_descr (&loc_descr, tmp);
16373
16374           /* Extract the vtable address.  */
16375           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16376           add_loc_descr (&loc_descr, tmp);
16377
16378           /* Calculate the address of the offset.  */
16379           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16380           gcc_assert (offset < 0);
16381
16382           tmp = int_loc_descriptor (-offset);
16383           add_loc_descr (&loc_descr, tmp);
16384           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16385           add_loc_descr (&loc_descr, tmp);
16386
16387           /* Extract the offset.  */
16388           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16389           add_loc_descr (&loc_descr, tmp);
16390
16391           /* Add it to the object address.  */
16392           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16393           add_loc_descr (&loc_descr, tmp);
16394         }
16395       else
16396         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16397     }
16398   else
16399     offset = field_byte_offset (decl);
16400
16401   if (! loc_descr)
16402     {
16403       if (dwarf_version > 2)
16404         {
16405           /* Don't need to output a location expression, just the constant. */
16406           if (offset < 0)
16407             add_AT_int (die, DW_AT_data_member_location, offset);
16408           else
16409             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16410           return;
16411         }
16412       else
16413         {
16414           enum dwarf_location_atom op;
16415
16416           /* The DWARF2 standard says that we should assume that the structure
16417              address is already on the stack, so we can specify a structure
16418              field address by using DW_OP_plus_uconst.  */
16419
16420 #ifdef MIPS_DEBUGGING_INFO
16421           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16422              operator correctly.  It works only if we leave the offset on the
16423              stack.  */
16424           op = DW_OP_constu;
16425 #else
16426           op = DW_OP_plus_uconst;
16427 #endif
16428
16429           loc_descr = new_loc_descr (op, offset, 0);
16430         }
16431     }
16432
16433   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16434 }
16435
16436 /* Writes integer values to dw_vec_const array.  */
16437
16438 static void
16439 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16440 {
16441   while (size != 0)
16442     {
16443       *dest++ = val & 0xff;
16444       val >>= 8;
16445       --size;
16446     }
16447 }
16448
16449 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16450
16451 static HOST_WIDE_INT
16452 extract_int (const unsigned char *src, unsigned int size)
16453 {
16454   HOST_WIDE_INT val = 0;
16455
16456   src += size;
16457   while (size != 0)
16458     {
16459       val <<= 8;
16460       val |= *--src & 0xff;
16461       --size;
16462     }
16463   return val;
16464 }
16465
16466 /* Writes double_int values to dw_vec_const array.  */
16467
16468 static void
16469 insert_double (double_int val, unsigned char *dest)
16470 {
16471   unsigned char *p0 = dest;
16472   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16473
16474   if (WORDS_BIG_ENDIAN)
16475     {
16476       p0 = p1;
16477       p1 = dest;
16478     }
16479
16480   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16481   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16482 }
16483
16484 /* Writes floating point values to dw_vec_const array.  */
16485
16486 static void
16487 insert_float (const_rtx rtl, unsigned char *array)
16488 {
16489   REAL_VALUE_TYPE rv;
16490   long val[4];
16491   int i;
16492
16493   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16494   real_to_target (val, &rv, GET_MODE (rtl));
16495
16496   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16497   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16498     {
16499       insert_int (val[i], 4, array);
16500       array += 4;
16501     }
16502 }
16503
16504 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16505    does not have a "location" either in memory or in a register.  These
16506    things can arise in GNU C when a constant is passed as an actual parameter
16507    to an inlined function.  They can also arise in C++ where declared
16508    constants do not necessarily get memory "homes".  */
16509
16510 static bool
16511 add_const_value_attribute (dw_die_ref die, rtx rtl)
16512 {
16513   switch (GET_CODE (rtl))
16514     {
16515     case CONST_INT:
16516       {
16517         HOST_WIDE_INT val = INTVAL (rtl);
16518
16519         if (val < 0)
16520           add_AT_int (die, DW_AT_const_value, val);
16521         else
16522           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16523       }
16524       return true;
16525
16526     case CONST_DOUBLE:
16527       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16528          floating-point constant.  A CONST_DOUBLE is used whenever the
16529          constant requires more than one word in order to be adequately
16530          represented.  */
16531       {
16532         enum machine_mode mode = GET_MODE (rtl);
16533
16534         if (SCALAR_FLOAT_MODE_P (mode))
16535           {
16536             unsigned int length = GET_MODE_SIZE (mode);
16537             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16538
16539             insert_float (rtl, array);
16540             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16541           }
16542         else
16543           add_AT_double (die, DW_AT_const_value,
16544                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16545       }
16546       return true;
16547
16548     case CONST_VECTOR:
16549       {
16550         enum machine_mode mode = GET_MODE (rtl);
16551         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16552         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16553         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16554           (length * elt_size);
16555         unsigned int i;
16556         unsigned char *p;
16557
16558         switch (GET_MODE_CLASS (mode))
16559           {
16560           case MODE_VECTOR_INT:
16561             for (i = 0, p = array; i < length; i++, p += elt_size)
16562               {
16563                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16564                 double_int val = rtx_to_double_int (elt);
16565
16566                 if (elt_size <= sizeof (HOST_WIDE_INT))
16567                   insert_int (double_int_to_shwi (val), elt_size, p);
16568                 else
16569                   {
16570                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16571                     insert_double (val, p);
16572                   }
16573               }
16574             break;
16575
16576           case MODE_VECTOR_FLOAT:
16577             for (i = 0, p = array; i < length; i++, p += elt_size)
16578               {
16579                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16580                 insert_float (elt, p);
16581               }
16582             break;
16583
16584           default:
16585             gcc_unreachable ();
16586           }
16587
16588         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16589       }
16590       return true;
16591
16592     case CONST_STRING:
16593       if (dwarf_version >= 4 || !dwarf_strict)
16594         {
16595           dw_loc_descr_ref loc_result;
16596           resolve_one_addr (&rtl, NULL);
16597         rtl_addr:
16598           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16599           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16600           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16601           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16602           add_AT_loc (die, DW_AT_location, loc_result);
16603           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16604           return true;
16605         }
16606       return false;
16607
16608     case CONST:
16609       if (CONSTANT_P (XEXP (rtl, 0)))
16610         return add_const_value_attribute (die, XEXP (rtl, 0));
16611       /* FALLTHROUGH */
16612     case SYMBOL_REF:
16613       if (!const_ok_for_output (rtl))
16614         return false;
16615     case LABEL_REF:
16616       if (dwarf_version >= 4 || !dwarf_strict)
16617         goto rtl_addr;
16618       return false;
16619
16620     case PLUS:
16621       /* In cases where an inlined instance of an inline function is passed
16622          the address of an `auto' variable (which is local to the caller) we
16623          can get a situation where the DECL_RTL of the artificial local
16624          variable (for the inlining) which acts as a stand-in for the
16625          corresponding formal parameter (of the inline function) will look
16626          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16627          exactly a compile-time constant expression, but it isn't the address
16628          of the (artificial) local variable either.  Rather, it represents the
16629          *value* which the artificial local variable always has during its
16630          lifetime.  We currently have no way to represent such quasi-constant
16631          values in Dwarf, so for now we just punt and generate nothing.  */
16632       return false;
16633
16634     case HIGH:
16635     case CONST_FIXED:
16636       return false;
16637
16638     case MEM:
16639       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16640           && MEM_READONLY_P (rtl)
16641           && GET_MODE (rtl) == BLKmode)
16642         {
16643           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16644           return true;
16645         }
16646       return false;
16647
16648     default:
16649       /* No other kinds of rtx should be possible here.  */
16650       gcc_unreachable ();
16651     }
16652   return false;
16653 }
16654
16655 /* Determine whether the evaluation of EXPR references any variables
16656    or functions which aren't otherwise used (and therefore may not be
16657    output).  */
16658 static tree
16659 reference_to_unused (tree * tp, int * walk_subtrees,
16660                      void * data ATTRIBUTE_UNUSED)
16661 {
16662   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16663     *walk_subtrees = 0;
16664
16665   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16666       && ! TREE_ASM_WRITTEN (*tp))
16667     return *tp;
16668   /* ???  The C++ FE emits debug information for using decls, so
16669      putting gcc_unreachable here falls over.  See PR31899.  For now
16670      be conservative.  */
16671   else if (!cgraph_global_info_ready
16672            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16673     return *tp;
16674   else if (TREE_CODE (*tp) == VAR_DECL)
16675     {
16676       struct varpool_node *node = varpool_get_node (*tp);
16677       if (!node || !node->needed)
16678         return *tp;
16679     }
16680   else if (TREE_CODE (*tp) == FUNCTION_DECL
16681            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16682     {
16683       /* The call graph machinery must have finished analyzing,
16684          optimizing and gimplifying the CU by now.
16685          So if *TP has no call graph node associated
16686          to it, it means *TP will not be emitted.  */
16687       if (!cgraph_get_node (*tp))
16688         return *tp;
16689     }
16690   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16691     return *tp;
16692
16693   return NULL_TREE;
16694 }
16695
16696 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16697    for use in a later add_const_value_attribute call.  */
16698
16699 static rtx
16700 rtl_for_decl_init (tree init, tree type)
16701 {
16702   rtx rtl = NULL_RTX;
16703
16704   STRIP_NOPS (init);
16705
16706   /* If a variable is initialized with a string constant without embedded
16707      zeros, build CONST_STRING.  */
16708   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16709     {
16710       tree enttype = TREE_TYPE (type);
16711       tree domain = TYPE_DOMAIN (type);
16712       enum machine_mode mode = TYPE_MODE (enttype);
16713
16714       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16715           && domain
16716           && integer_zerop (TYPE_MIN_VALUE (domain))
16717           && compare_tree_int (TYPE_MAX_VALUE (domain),
16718                                TREE_STRING_LENGTH (init) - 1) == 0
16719           && ((size_t) TREE_STRING_LENGTH (init)
16720               == strlen (TREE_STRING_POINTER (init)) + 1))
16721         {
16722           rtl = gen_rtx_CONST_STRING (VOIDmode,
16723                                       ggc_strdup (TREE_STRING_POINTER (init)));
16724           rtl = gen_rtx_MEM (BLKmode, rtl);
16725           MEM_READONLY_P (rtl) = 1;
16726         }
16727     }
16728   /* Other aggregates, and complex values, could be represented using
16729      CONCAT: FIXME!  */
16730   else if (AGGREGATE_TYPE_P (type)
16731            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16732                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16733            || TREE_CODE (type) == COMPLEX_TYPE)
16734     ;
16735   /* Vectors only work if their mode is supported by the target.
16736      FIXME: generic vectors ought to work too.  */
16737   else if (TREE_CODE (type) == VECTOR_TYPE
16738            && !VECTOR_MODE_P (TYPE_MODE (type)))
16739     ;
16740   /* If the initializer is something that we know will expand into an
16741      immediate RTL constant, expand it now.  We must be careful not to
16742      reference variables which won't be output.  */
16743   else if (initializer_constant_valid_p (init, type)
16744            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16745     {
16746       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16747          possible.  */
16748       if (TREE_CODE (type) == VECTOR_TYPE)
16749         switch (TREE_CODE (init))
16750           {
16751           case VECTOR_CST:
16752             break;
16753           case CONSTRUCTOR:
16754             if (TREE_CONSTANT (init))
16755               {
16756                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16757                 bool constant_p = true;
16758                 tree value;
16759                 unsigned HOST_WIDE_INT ix;
16760
16761                 /* Even when ctor is constant, it might contain non-*_CST
16762                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16763                    belong into VECTOR_CST nodes.  */
16764                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16765                   if (!CONSTANT_CLASS_P (value))
16766                     {
16767                       constant_p = false;
16768                       break;
16769                     }
16770
16771                 if (constant_p)
16772                   {
16773                     init = build_vector_from_ctor (type, elts);
16774                     break;
16775                   }
16776               }
16777             /* FALLTHRU */
16778
16779           default:
16780             return NULL;
16781           }
16782
16783       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16784
16785       /* If expand_expr returns a MEM, it wasn't immediate.  */
16786       gcc_assert (!rtl || !MEM_P (rtl));
16787     }
16788
16789   return rtl;
16790 }
16791
16792 /* Generate RTL for the variable DECL to represent its location.  */
16793
16794 static rtx
16795 rtl_for_decl_location (tree decl)
16796 {
16797   rtx rtl;
16798
16799   /* Here we have to decide where we are going to say the parameter "lives"
16800      (as far as the debugger is concerned).  We only have a couple of
16801      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16802
16803      DECL_RTL normally indicates where the parameter lives during most of the
16804      activation of the function.  If optimization is enabled however, this
16805      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16806      that the parameter doesn't really live anywhere (as far as the code
16807      generation parts of GCC are concerned) during most of the function's
16808      activation.  That will happen (for example) if the parameter is never
16809      referenced within the function.
16810
16811      We could just generate a location descriptor here for all non-NULL
16812      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16813      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16814      where DECL_RTL is NULL or is a pseudo-reg.
16815
16816      Note however that we can only get away with using DECL_INCOMING_RTL as
16817      a backup substitute for DECL_RTL in certain limited cases.  In cases
16818      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16819      we can be sure that the parameter was passed using the same type as it is
16820      declared to have within the function, and that its DECL_INCOMING_RTL
16821      points us to a place where a value of that type is passed.
16822
16823      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16824      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16825      because in these cases DECL_INCOMING_RTL points us to a value of some
16826      type which is *different* from the type of the parameter itself.  Thus,
16827      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16828      such cases, the debugger would end up (for example) trying to fetch a
16829      `float' from a place which actually contains the first part of a
16830      `double'.  That would lead to really incorrect and confusing
16831      output at debug-time.
16832
16833      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16834      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16835      are a couple of exceptions however.  On little-endian machines we can
16836      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16837      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16838      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16839      when (on a little-endian machine) a non-prototyped function has a
16840      parameter declared to be of type `short' or `char'.  In such cases,
16841      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16842      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16843      passed `int' value.  If the debugger then uses that address to fetch
16844      a `short' or a `char' (on a little-endian machine) the result will be
16845      the correct data, so we allow for such exceptional cases below.
16846
16847      Note that our goal here is to describe the place where the given formal
16848      parameter lives during most of the function's activation (i.e. between the
16849      end of the prologue and the start of the epilogue).  We'll do that as best
16850      as we can. Note however that if the given formal parameter is modified
16851      sometime during the execution of the function, then a stack backtrace (at
16852      debug-time) will show the function as having been called with the *new*
16853      value rather than the value which was originally passed in.  This happens
16854      rarely enough that it is not a major problem, but it *is* a problem, and
16855      I'd like to fix it.
16856
16857      A future version of dwarf2out.c may generate two additional attributes for
16858      any given DW_TAG_formal_parameter DIE which will describe the "passed
16859      type" and the "passed location" for the given formal parameter in addition
16860      to the attributes we now generate to indicate the "declared type" and the
16861      "active location" for each parameter.  This additional set of attributes
16862      could be used by debuggers for stack backtraces. Separately, note that
16863      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16864      This happens (for example) for inlined-instances of inline function formal
16865      parameters which are never referenced.  This really shouldn't be
16866      happening.  All PARM_DECL nodes should get valid non-NULL
16867      DECL_INCOMING_RTL values.  FIXME.  */
16868
16869   /* Use DECL_RTL as the "location" unless we find something better.  */
16870   rtl = DECL_RTL_IF_SET (decl);
16871
16872   /* When generating abstract instances, ignore everything except
16873      constants, symbols living in memory, and symbols living in
16874      fixed registers.  */
16875   if (! reload_completed)
16876     {
16877       if (rtl
16878           && (CONSTANT_P (rtl)
16879               || (MEM_P (rtl)
16880                   && CONSTANT_P (XEXP (rtl, 0)))
16881               || (REG_P (rtl)
16882                   && TREE_CODE (decl) == VAR_DECL
16883                   && TREE_STATIC (decl))))
16884         {
16885           rtl = targetm.delegitimize_address (rtl);
16886           return rtl;
16887         }
16888       rtl = NULL_RTX;
16889     }
16890   else if (TREE_CODE (decl) == PARM_DECL)
16891     {
16892       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16893         {
16894           tree declared_type = TREE_TYPE (decl);
16895           tree passed_type = DECL_ARG_TYPE (decl);
16896           enum machine_mode dmode = TYPE_MODE (declared_type);
16897           enum machine_mode pmode = TYPE_MODE (passed_type);
16898
16899           /* This decl represents a formal parameter which was optimized out.
16900              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16901              all cases where (rtl == NULL_RTX) just below.  */
16902           if (dmode == pmode)
16903             rtl = DECL_INCOMING_RTL (decl);
16904           else if (SCALAR_INT_MODE_P (dmode)
16905                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16906                    && DECL_INCOMING_RTL (decl))
16907             {
16908               rtx inc = DECL_INCOMING_RTL (decl);
16909               if (REG_P (inc))
16910                 rtl = inc;
16911               else if (MEM_P (inc))
16912                 {
16913                   if (BYTES_BIG_ENDIAN)
16914                     rtl = adjust_address_nv (inc, dmode,
16915                                              GET_MODE_SIZE (pmode)
16916                                              - GET_MODE_SIZE (dmode));
16917                   else
16918                     rtl = inc;
16919                 }
16920             }
16921         }
16922
16923       /* If the parm was passed in registers, but lives on the stack, then
16924          make a big endian correction if the mode of the type of the
16925          parameter is not the same as the mode of the rtl.  */
16926       /* ??? This is the same series of checks that are made in dbxout.c before
16927          we reach the big endian correction code there.  It isn't clear if all
16928          of these checks are necessary here, but keeping them all is the safe
16929          thing to do.  */
16930       else if (MEM_P (rtl)
16931                && XEXP (rtl, 0) != const0_rtx
16932                && ! CONSTANT_P (XEXP (rtl, 0))
16933                /* Not passed in memory.  */
16934                && !MEM_P (DECL_INCOMING_RTL (decl))
16935                /* Not passed by invisible reference.  */
16936                && (!REG_P (XEXP (rtl, 0))
16937                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16938                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16939 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16940                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16941 #endif
16942                      )
16943                /* Big endian correction check.  */
16944                && BYTES_BIG_ENDIAN
16945                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16946                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16947                    < UNITS_PER_WORD))
16948         {
16949           int offset = (UNITS_PER_WORD
16950                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16951
16952           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16953                              plus_constant (XEXP (rtl, 0), offset));
16954         }
16955     }
16956   else if (TREE_CODE (decl) == VAR_DECL
16957            && rtl
16958            && MEM_P (rtl)
16959            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16960            && BYTES_BIG_ENDIAN)
16961     {
16962       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16963       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16964
16965       /* If a variable is declared "register" yet is smaller than
16966          a register, then if we store the variable to memory, it
16967          looks like we're storing a register-sized value, when in
16968          fact we are not.  We need to adjust the offset of the
16969          storage location to reflect the actual value's bytes,
16970          else gdb will not be able to display it.  */
16971       if (rsize > dsize)
16972         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16973                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16974     }
16975
16976   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16977      and will have been substituted directly into all expressions that use it.
16978      C does not have such a concept, but C++ and other languages do.  */
16979   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16980     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16981
16982   if (rtl)
16983     rtl = targetm.delegitimize_address (rtl);
16984
16985   /* If we don't look past the constant pool, we risk emitting a
16986      reference to a constant pool entry that isn't referenced from
16987      code, and thus is not emitted.  */
16988   if (rtl)
16989     rtl = avoid_constant_pool_reference (rtl);
16990
16991   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16992      in the current CU, resolve_addr will remove the expression referencing
16993      it.  */
16994   if (rtl == NULL_RTX
16995       && TREE_CODE (decl) == VAR_DECL
16996       && !DECL_EXTERNAL (decl)
16997       && TREE_STATIC (decl)
16998       && DECL_NAME (decl)
16999       && !DECL_HARD_REGISTER (decl)
17000       && DECL_MODE (decl) != VOIDmode)
17001     {
17002       rtl = make_decl_rtl_for_debug (decl);
17003       if (!MEM_P (rtl)
17004           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17005           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17006         rtl = NULL_RTX;
17007     }
17008
17009   return rtl;
17010 }
17011
17012 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17013    returned.  If so, the decl for the COMMON block is returned, and the
17014    value is the offset into the common block for the symbol.  */
17015
17016 static tree
17017 fortran_common (tree decl, HOST_WIDE_INT *value)
17018 {
17019   tree val_expr, cvar;
17020   enum machine_mode mode;
17021   HOST_WIDE_INT bitsize, bitpos;
17022   tree offset;
17023   int volatilep = 0, unsignedp = 0;
17024
17025   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17026      it does not have a value (the offset into the common area), or if it
17027      is thread local (as opposed to global) then it isn't common, and shouldn't
17028      be handled as such.  */
17029   if (TREE_CODE (decl) != VAR_DECL
17030       || !TREE_STATIC (decl)
17031       || !DECL_HAS_VALUE_EXPR_P (decl)
17032       || !is_fortran ())
17033     return NULL_TREE;
17034
17035   val_expr = DECL_VALUE_EXPR (decl);
17036   if (TREE_CODE (val_expr) != COMPONENT_REF)
17037     return NULL_TREE;
17038
17039   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17040                               &mode, &unsignedp, &volatilep, true);
17041
17042   if (cvar == NULL_TREE
17043       || TREE_CODE (cvar) != VAR_DECL
17044       || DECL_ARTIFICIAL (cvar)
17045       || !TREE_PUBLIC (cvar))
17046     return NULL_TREE;
17047
17048   *value = 0;
17049   if (offset != NULL)
17050     {
17051       if (!host_integerp (offset, 0))
17052         return NULL_TREE;
17053       *value = tree_low_cst (offset, 0);
17054     }
17055   if (bitpos != 0)
17056     *value += bitpos / BITS_PER_UNIT;
17057
17058   return cvar;
17059 }
17060
17061 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17062    data attribute for a variable or a parameter.  We generate the
17063    DW_AT_const_value attribute only in those cases where the given variable
17064    or parameter does not have a true "location" either in memory or in a
17065    register.  This can happen (for example) when a constant is passed as an
17066    actual argument in a call to an inline function.  (It's possible that
17067    these things can crop up in other ways also.)  Note that one type of
17068    constant value which can be passed into an inlined function is a constant
17069    pointer.  This can happen for example if an actual argument in an inlined
17070    function call evaluates to a compile-time constant address.
17071
17072    CACHE_P is true if it is worth caching the location list for DECL,
17073    so that future calls can reuse it rather than regenerate it from scratch.
17074    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17075    since we will need to refer to them each time the function is inlined.  */
17076
17077 static bool
17078 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
17079                                        enum dwarf_attribute attr)
17080 {
17081   rtx rtl;
17082   dw_loc_list_ref list;
17083   var_loc_list *loc_list;
17084   cached_dw_loc_list *cache;
17085   void **slot;
17086
17087   if (TREE_CODE (decl) == ERROR_MARK)
17088     return false;
17089
17090   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17091               || TREE_CODE (decl) == RESULT_DECL);
17092
17093   /* Try to get some constant RTL for this decl, and use that as the value of
17094      the location.  */
17095
17096   rtl = rtl_for_decl_location (decl);
17097   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17098       && add_const_value_attribute (die, rtl))
17099     return true;
17100
17101   /* See if we have single element location list that is equivalent to
17102      a constant value.  That way we are better to use add_const_value_attribute
17103      rather than expanding constant value equivalent.  */
17104   loc_list = lookup_decl_loc (decl);
17105   if (loc_list
17106       && loc_list->first
17107       && loc_list->first->next == NULL
17108       && NOTE_P (loc_list->first->loc)
17109       && NOTE_VAR_LOCATION (loc_list->first->loc)
17110       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17111     {
17112       struct var_loc_node *node;
17113
17114       node = loc_list->first;
17115       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17116       if (GET_CODE (rtl) == EXPR_LIST)
17117         rtl = XEXP (rtl, 0);
17118       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17119           && add_const_value_attribute (die, rtl))
17120          return true;
17121     }
17122   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17123      list several times.  See if we've already cached the contents.  */
17124   list = NULL;
17125   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17126     cache_p = false;
17127   if (cache_p)
17128     {
17129       cache = (cached_dw_loc_list *)
17130         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
17131       if (cache)
17132         list = cache->loc_list;
17133     }
17134   if (list == NULL)
17135     {
17136       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17137       /* It is usually worth caching this result if the decl is from
17138          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17139       if (cache_p && list && list->dw_loc_next)
17140         {
17141           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17142                                            DECL_UID (decl), INSERT);
17143           cache = ggc_alloc_cleared_cached_dw_loc_list ();
17144           cache->decl_id = DECL_UID (decl);
17145           cache->loc_list = list;
17146           *slot = cache;
17147         }
17148     }
17149   if (list)
17150     {
17151       add_AT_location_description (die, attr, list);
17152       return true;
17153     }
17154   /* None of that worked, so it must not really have a location;
17155      try adding a constant value attribute from the DECL_INITIAL.  */
17156   return tree_add_const_value_attribute_for_decl (die, decl);
17157 }
17158
17159 /* Add VARIABLE and DIE into deferred locations list.  */
17160
17161 static void
17162 defer_location (tree variable, dw_die_ref die)
17163 {
17164   deferred_locations entry;
17165   entry.variable = variable;
17166   entry.die = die;
17167   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17168 }
17169
17170 /* Helper function for tree_add_const_value_attribute.  Natively encode
17171    initializer INIT into an array.  Return true if successful.  */
17172
17173 static bool
17174 native_encode_initializer (tree init, unsigned char *array, int size)
17175 {
17176   tree type;
17177
17178   if (init == NULL_TREE)
17179     return false;
17180
17181   STRIP_NOPS (init);
17182   switch (TREE_CODE (init))
17183     {
17184     case STRING_CST:
17185       type = TREE_TYPE (init);
17186       if (TREE_CODE (type) == ARRAY_TYPE)
17187         {
17188           tree enttype = TREE_TYPE (type);
17189           enum machine_mode mode = TYPE_MODE (enttype);
17190
17191           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17192             return false;
17193           if (int_size_in_bytes (type) != size)
17194             return false;
17195           if (size > TREE_STRING_LENGTH (init))
17196             {
17197               memcpy (array, TREE_STRING_POINTER (init),
17198                       TREE_STRING_LENGTH (init));
17199               memset (array + TREE_STRING_LENGTH (init),
17200                       '\0', size - TREE_STRING_LENGTH (init));
17201             }
17202           else
17203             memcpy (array, TREE_STRING_POINTER (init), size);
17204           return true;
17205         }
17206       return false;
17207     case CONSTRUCTOR:
17208       type = TREE_TYPE (init);
17209       if (int_size_in_bytes (type) != size)
17210         return false;
17211       if (TREE_CODE (type) == ARRAY_TYPE)
17212         {
17213           HOST_WIDE_INT min_index;
17214           unsigned HOST_WIDE_INT cnt;
17215           int curpos = 0, fieldsize;
17216           constructor_elt *ce;
17217
17218           if (TYPE_DOMAIN (type) == NULL_TREE
17219               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17220             return false;
17221
17222           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17223           if (fieldsize <= 0)
17224             return false;
17225
17226           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17227           memset (array, '\0', size);
17228           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17229             {
17230               tree val = ce->value;
17231               tree index = ce->index;
17232               int pos = curpos;
17233               if (index && TREE_CODE (index) == RANGE_EXPR)
17234                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17235                       * fieldsize;
17236               else if (index)
17237                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17238
17239               if (val)
17240                 {
17241                   STRIP_NOPS (val);
17242                   if (!native_encode_initializer (val, array + pos, fieldsize))
17243                     return false;
17244                 }
17245               curpos = pos + fieldsize;
17246               if (index && TREE_CODE (index) == RANGE_EXPR)
17247                 {
17248                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17249                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17250                   while (count-- > 0)
17251                     {
17252                       if (val)
17253                         memcpy (array + curpos, array + pos, fieldsize);
17254                       curpos += fieldsize;
17255                     }
17256                 }
17257               gcc_assert (curpos <= size);
17258             }
17259           return true;
17260         }
17261       else if (TREE_CODE (type) == RECORD_TYPE
17262                || TREE_CODE (type) == UNION_TYPE)
17263         {
17264           tree field = NULL_TREE;
17265           unsigned HOST_WIDE_INT cnt;
17266           constructor_elt *ce;
17267
17268           if (int_size_in_bytes (type) != size)
17269             return false;
17270
17271           if (TREE_CODE (type) == RECORD_TYPE)
17272             field = TYPE_FIELDS (type);
17273
17274           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17275             {
17276               tree val = ce->value;
17277               int pos, fieldsize;
17278
17279               if (ce->index != 0)
17280                 field = ce->index;
17281
17282               if (val)
17283                 STRIP_NOPS (val);
17284
17285               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17286                 return false;
17287
17288               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17289                   && TYPE_DOMAIN (TREE_TYPE (field))
17290                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17291                 return false;
17292               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17293                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17294                 return false;
17295               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17296               pos = int_byte_position (field);
17297               gcc_assert (pos + fieldsize <= size);
17298               if (val
17299                   && !native_encode_initializer (val, array + pos, fieldsize))
17300                 return false;
17301             }
17302           return true;
17303         }
17304       return false;
17305     case VIEW_CONVERT_EXPR:
17306     case NON_LVALUE_EXPR:
17307       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17308     default:
17309       return native_encode_expr (init, array, size) == size;
17310     }
17311 }
17312
17313 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17314    attribute is the const value T.  */
17315
17316 static bool
17317 tree_add_const_value_attribute (dw_die_ref die, tree t)
17318 {
17319   tree init;
17320   tree type = TREE_TYPE (t);
17321   rtx rtl;
17322
17323   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17324     return false;
17325
17326   init = t;
17327   gcc_assert (!DECL_P (init));
17328
17329   rtl = rtl_for_decl_init (init, type);
17330   if (rtl)
17331     return add_const_value_attribute (die, rtl);
17332   /* If the host and target are sane, try harder.  */
17333   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17334            && initializer_constant_valid_p (init, type))
17335     {
17336       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17337       if (size > 0 && (int) size == size)
17338         {
17339           unsigned char *array = (unsigned char *)
17340             ggc_alloc_cleared_atomic (size);
17341
17342           if (native_encode_initializer (init, array, size))
17343             {
17344               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17345               return true;
17346             }
17347         }
17348     }
17349   return false;
17350 }
17351
17352 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17353    attribute is the const value of T, where T is an integral constant
17354    variable with static storage duration
17355    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17356
17357 static bool
17358 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17359 {
17360
17361   if (!decl
17362       || (TREE_CODE (decl) != VAR_DECL
17363           && TREE_CODE (decl) != CONST_DECL))
17364     return false;
17365
17366     if (TREE_READONLY (decl)
17367         && ! TREE_THIS_VOLATILE (decl)
17368         && DECL_INITIAL (decl))
17369       /* OK */;
17370     else
17371       return false;
17372
17373   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17374   if (get_AT (var_die, DW_AT_const_value))
17375     return false;
17376
17377   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17378 }
17379
17380 /* Convert the CFI instructions for the current function into a
17381    location list.  This is used for DW_AT_frame_base when we targeting
17382    a dwarf2 consumer that does not support the dwarf3
17383    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17384    expressions.  */
17385
17386 static dw_loc_list_ref
17387 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17388 {
17389   dw_fde_ref fde;
17390   dw_loc_list_ref list, *list_tail;
17391   dw_cfi_ref cfi;
17392   dw_cfa_location last_cfa, next_cfa;
17393   const char *start_label, *last_label, *section;
17394   dw_cfa_location remember;
17395
17396   fde = current_fde ();
17397   gcc_assert (fde != NULL);
17398
17399   section = secname_for_decl (current_function_decl);
17400   list_tail = &list;
17401   list = NULL;
17402
17403   memset (&next_cfa, 0, sizeof (next_cfa));
17404   next_cfa.reg = INVALID_REGNUM;
17405   remember = next_cfa;
17406
17407   start_label = fde->dw_fde_begin;
17408
17409   /* ??? Bald assumption that the CIE opcode list does not contain
17410      advance opcodes.  */
17411   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17412     lookup_cfa_1 (cfi, &next_cfa, &remember);
17413
17414   last_cfa = next_cfa;
17415   last_label = start_label;
17416
17417   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi == NULL)
17418     {
17419       /* If the first partition contained no CFI adjustments, the
17420          CIE opcodes apply to the whole first partition.  */
17421       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17422                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17423       list_tail =&(*list_tail)->dw_loc_next;
17424       start_label = last_label = fde->dw_fde_second_begin;
17425     }
17426
17427   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17428     {
17429       switch (cfi->dw_cfi_opc)
17430         {
17431         case DW_CFA_set_loc:
17432         case DW_CFA_advance_loc1:
17433         case DW_CFA_advance_loc2:
17434         case DW_CFA_advance_loc4:
17435           if (!cfa_equal_p (&last_cfa, &next_cfa))
17436             {
17437               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17438                                          start_label, last_label, section);
17439
17440               list_tail = &(*list_tail)->dw_loc_next;
17441               last_cfa = next_cfa;
17442               start_label = last_label;
17443             }
17444           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17445           break;
17446
17447         case DW_CFA_advance_loc:
17448           /* The encoding is complex enough that we should never emit this.  */
17449           gcc_unreachable ();
17450
17451         default:
17452           lookup_cfa_1 (cfi, &next_cfa, &remember);
17453           break;
17454         }
17455       if (cfi == fde->dw_fde_switch_cfi)
17456         {
17457           if (!cfa_equal_p (&last_cfa, &next_cfa))
17458             {
17459               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17460                                          start_label, last_label, section);
17461
17462               list_tail = &(*list_tail)->dw_loc_next;
17463               last_cfa = next_cfa;
17464               start_label = last_label;
17465             }
17466           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17467                                      start_label, fde->dw_fde_end, section);
17468           list_tail = &(*list_tail)->dw_loc_next;
17469           start_label = last_label = fde->dw_fde_second_begin;
17470         }
17471     }
17472
17473   if (!cfa_equal_p (&last_cfa, &next_cfa))
17474     {
17475       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17476                                  start_label, last_label, section);
17477       list_tail = &(*list_tail)->dw_loc_next;
17478       start_label = last_label;
17479     }
17480
17481   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17482                              start_label,
17483                              fde->dw_fde_second_begin
17484                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17485                              section);
17486
17487   if (list && list->dw_loc_next)
17488     gen_llsym (list);
17489
17490   return list;
17491 }
17492
17493 /* Compute a displacement from the "steady-state frame pointer" to the
17494    frame base (often the same as the CFA), and store it in
17495    frame_pointer_fb_offset.  OFFSET is added to the displacement
17496    before the latter is negated.  */
17497
17498 static void
17499 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17500 {
17501   rtx reg, elim;
17502
17503 #ifdef FRAME_POINTER_CFA_OFFSET
17504   reg = frame_pointer_rtx;
17505   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17506 #else
17507   reg = arg_pointer_rtx;
17508   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17509 #endif
17510
17511   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17512   if (GET_CODE (elim) == PLUS)
17513     {
17514       offset += INTVAL (XEXP (elim, 1));
17515       elim = XEXP (elim, 0);
17516     }
17517
17518   frame_pointer_fb_offset = -offset;
17519
17520   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
17521      in which to eliminate.  This is because it's stack pointer isn't 
17522      directly accessible as a register within the ISA.  To work around
17523      this, assume that while we cannot provide a proper value for
17524      frame_pointer_fb_offset, we won't need one either.  */
17525   frame_pointer_fb_offset_valid
17526     = ((SUPPORTS_STACK_ALIGNMENT
17527         && (elim == hard_frame_pointer_rtx
17528             || elim == stack_pointer_rtx))
17529        || elim == (frame_pointer_needed
17530                    ? hard_frame_pointer_rtx
17531                    : stack_pointer_rtx));
17532 }
17533
17534 /* Generate a DW_AT_name attribute given some string value to be included as
17535    the value of the attribute.  */
17536
17537 static void
17538 add_name_attribute (dw_die_ref die, const char *name_string)
17539 {
17540   if (name_string != NULL && *name_string != 0)
17541     {
17542       if (demangle_name_func)
17543         name_string = (*demangle_name_func) (name_string);
17544
17545       add_AT_string (die, DW_AT_name, name_string);
17546     }
17547 }
17548
17549 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17550
17551 static void
17552 add_comp_dir_attribute (dw_die_ref die)
17553 {
17554   const char *wd = get_src_pwd ();
17555   char *wd1;
17556
17557   if (wd == NULL)
17558     return;
17559
17560   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17561     {
17562       int wdlen;
17563
17564       wdlen = strlen (wd);
17565       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17566       strcpy (wd1, wd);
17567       wd1 [wdlen] = DIR_SEPARATOR;
17568       wd1 [wdlen + 1] = 0;
17569       wd = wd1;
17570     }
17571
17572     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17573 }
17574
17575 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17576    default.  */
17577
17578 static int
17579 lower_bound_default (void)
17580 {
17581   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17582     {
17583     case DW_LANG_C:
17584     case DW_LANG_C89:
17585     case DW_LANG_C99:
17586     case DW_LANG_C_plus_plus:
17587     case DW_LANG_ObjC:
17588     case DW_LANG_ObjC_plus_plus:
17589     case DW_LANG_Java:
17590       return 0;
17591     case DW_LANG_Fortran77:
17592     case DW_LANG_Fortran90:
17593     case DW_LANG_Fortran95:
17594       return 1;
17595     case DW_LANG_UPC:
17596     case DW_LANG_D:
17597     case DW_LANG_Python:
17598       return dwarf_version >= 4 ? 0 : -1;
17599     case DW_LANG_Ada95:
17600     case DW_LANG_Ada83:
17601     case DW_LANG_Cobol74:
17602     case DW_LANG_Cobol85:
17603     case DW_LANG_Pascal83:
17604     case DW_LANG_Modula2:
17605     case DW_LANG_PLI:
17606       return dwarf_version >= 4 ? 1 : -1;
17607     default:
17608       return -1;
17609     }
17610 }
17611
17612 /* Given a tree node describing an array bound (either lower or upper) output
17613    a representation for that bound.  */
17614
17615 static void
17616 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17617 {
17618   switch (TREE_CODE (bound))
17619     {
17620     case ERROR_MARK:
17621       return;
17622
17623     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17624     case INTEGER_CST:
17625       {
17626         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17627         int dflt;
17628
17629         /* Use the default if possible.  */
17630         if (bound_attr == DW_AT_lower_bound
17631             && host_integerp (bound, 0)
17632             && (dflt = lower_bound_default ()) != -1
17633             && tree_low_cst (bound, 0) == dflt)
17634           ;
17635
17636         /* Otherwise represent the bound as an unsigned value with the
17637            precision of its type.  The precision and signedness of the
17638            type will be necessary to re-interpret it unambiguously.  */
17639         else if (prec < HOST_BITS_PER_WIDE_INT)
17640           {
17641             unsigned HOST_WIDE_INT mask
17642               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17643             add_AT_unsigned (subrange_die, bound_attr,
17644                              TREE_INT_CST_LOW (bound) & mask);
17645           }
17646         else if (prec == HOST_BITS_PER_WIDE_INT
17647                  || TREE_INT_CST_HIGH (bound) == 0)
17648           add_AT_unsigned (subrange_die, bound_attr,
17649                            TREE_INT_CST_LOW (bound));
17650         else
17651           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17652                          TREE_INT_CST_LOW (bound));
17653       }
17654       break;
17655
17656     CASE_CONVERT:
17657     case VIEW_CONVERT_EXPR:
17658       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17659       break;
17660
17661     case SAVE_EXPR:
17662       break;
17663
17664     case VAR_DECL:
17665     case PARM_DECL:
17666     case RESULT_DECL:
17667       {
17668         dw_die_ref decl_die = lookup_decl_die (bound);
17669
17670         /* ??? Can this happen, or should the variable have been bound
17671            first?  Probably it can, since I imagine that we try to create
17672            the types of parameters in the order in which they exist in
17673            the list, and won't have created a forward reference to a
17674            later parameter.  */
17675         if (decl_die != NULL)
17676           {
17677             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17678             break;
17679           }
17680       }
17681       /* FALLTHRU */
17682
17683     default:
17684       {
17685         /* Otherwise try to create a stack operation procedure to
17686            evaluate the value of the array bound.  */
17687
17688         dw_die_ref ctx, decl_die;
17689         dw_loc_list_ref list;
17690
17691         list = loc_list_from_tree (bound, 2);
17692         if (list == NULL || single_element_loc_list_p (list))
17693           {
17694             /* If DW_AT_*bound is not a reference nor constant, it is
17695                a DWARF expression rather than location description.
17696                For that loc_list_from_tree (bound, 0) is needed.
17697                If that fails to give a single element list,
17698                fall back to outputting this as a reference anyway.  */
17699             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17700             if (list2 && single_element_loc_list_p (list2))
17701               {
17702                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17703                 break;
17704               }
17705           }
17706         if (list == NULL)
17707           break;
17708
17709         if (current_function_decl == 0)
17710           ctx = comp_unit_die ();
17711         else
17712           ctx = lookup_decl_die (current_function_decl);
17713
17714         decl_die = new_die (DW_TAG_variable, ctx, bound);
17715         add_AT_flag (decl_die, DW_AT_artificial, 1);
17716         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17717         add_AT_location_description (decl_die, DW_AT_location, list);
17718         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17719         break;
17720       }
17721     }
17722 }
17723
17724 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17725    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17726    Note that the block of subscript information for an array type also
17727    includes information about the element type of the given array type.  */
17728
17729 static void
17730 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17731 {
17732   unsigned dimension_number;
17733   tree lower, upper;
17734   dw_die_ref subrange_die;
17735
17736   for (dimension_number = 0;
17737        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17738        type = TREE_TYPE (type), dimension_number++)
17739     {
17740       tree domain = TYPE_DOMAIN (type);
17741
17742       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17743         break;
17744
17745       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17746          and (in GNU C only) variable bounds.  Handle all three forms
17747          here.  */
17748       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17749       if (domain)
17750         {
17751           /* We have an array type with specified bounds.  */
17752           lower = TYPE_MIN_VALUE (domain);
17753           upper = TYPE_MAX_VALUE (domain);
17754
17755           /* Define the index type.  */
17756           if (TREE_TYPE (domain))
17757             {
17758               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17759                  TREE_TYPE field.  We can't emit debug info for this
17760                  because it is an unnamed integral type.  */
17761               if (TREE_CODE (domain) == INTEGER_TYPE
17762                   && TYPE_NAME (domain) == NULL_TREE
17763                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17764                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17765                 ;
17766               else
17767                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17768                                     type_die);
17769             }
17770
17771           /* ??? If upper is NULL, the array has unspecified length,
17772              but it does have a lower bound.  This happens with Fortran
17773                dimension arr(N:*)
17774              Since the debugger is definitely going to need to know N
17775              to produce useful results, go ahead and output the lower
17776              bound solo, and hope the debugger can cope.  */
17777
17778           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17779           if (upper)
17780             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17781         }
17782
17783       /* Otherwise we have an array type with an unspecified length.  The
17784          DWARF-2 spec does not say how to handle this; let's just leave out the
17785          bounds.  */
17786     }
17787 }
17788
17789 static void
17790 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17791 {
17792   unsigned size;
17793
17794   switch (TREE_CODE (tree_node))
17795     {
17796     case ERROR_MARK:
17797       size = 0;
17798       break;
17799     case ENUMERAL_TYPE:
17800     case RECORD_TYPE:
17801     case UNION_TYPE:
17802     case QUAL_UNION_TYPE:
17803       size = int_size_in_bytes (tree_node);
17804       break;
17805     case FIELD_DECL:
17806       /* For a data member of a struct or union, the DW_AT_byte_size is
17807          generally given as the number of bytes normally allocated for an
17808          object of the *declared* type of the member itself.  This is true
17809          even for bit-fields.  */
17810       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17811       break;
17812     default:
17813       gcc_unreachable ();
17814     }
17815
17816   /* Note that `size' might be -1 when we get to this point.  If it is, that
17817      indicates that the byte size of the entity in question is variable.  We
17818      have no good way of expressing this fact in Dwarf at the present time,
17819      so just let the -1 pass on through.  */
17820   add_AT_unsigned (die, DW_AT_byte_size, size);
17821 }
17822
17823 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17824    which specifies the distance in bits from the highest order bit of the
17825    "containing object" for the bit-field to the highest order bit of the
17826    bit-field itself.
17827
17828    For any given bit-field, the "containing object" is a hypothetical object
17829    (of some integral or enum type) within which the given bit-field lives.  The
17830    type of this hypothetical "containing object" is always the same as the
17831    declared type of the individual bit-field itself.  The determination of the
17832    exact location of the "containing object" for a bit-field is rather
17833    complicated.  It's handled by the `field_byte_offset' function (above).
17834
17835    Note that it is the size (in bytes) of the hypothetical "containing object"
17836    which will be given in the DW_AT_byte_size attribute for this bit-field.
17837    (See `byte_size_attribute' above).  */
17838
17839 static inline void
17840 add_bit_offset_attribute (dw_die_ref die, tree decl)
17841 {
17842   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17843   tree type = DECL_BIT_FIELD_TYPE (decl);
17844   HOST_WIDE_INT bitpos_int;
17845   HOST_WIDE_INT highest_order_object_bit_offset;
17846   HOST_WIDE_INT highest_order_field_bit_offset;
17847   HOST_WIDE_INT unsigned bit_offset;
17848
17849   /* Must be a field and a bit field.  */
17850   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17851
17852   /* We can't yet handle bit-fields whose offsets are variable, so if we
17853      encounter such things, just return without generating any attribute
17854      whatsoever.  Likewise for variable or too large size.  */
17855   if (! host_integerp (bit_position (decl), 0)
17856       || ! host_integerp (DECL_SIZE (decl), 1))
17857     return;
17858
17859   bitpos_int = int_bit_position (decl);
17860
17861   /* Note that the bit offset is always the distance (in bits) from the
17862      highest-order bit of the "containing object" to the highest-order bit of
17863      the bit-field itself.  Since the "high-order end" of any object or field
17864      is different on big-endian and little-endian machines, the computation
17865      below must take account of these differences.  */
17866   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17867   highest_order_field_bit_offset = bitpos_int;
17868
17869   if (! BYTES_BIG_ENDIAN)
17870     {
17871       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17872       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17873     }
17874
17875   bit_offset
17876     = (! BYTES_BIG_ENDIAN
17877        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17878        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17879
17880   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17881 }
17882
17883 /* For a FIELD_DECL node which represents a bit field, output an attribute
17884    which specifies the length in bits of the given field.  */
17885
17886 static inline void
17887 add_bit_size_attribute (dw_die_ref die, tree decl)
17888 {
17889   /* Must be a field and a bit field.  */
17890   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17891               && DECL_BIT_FIELD_TYPE (decl));
17892
17893   if (host_integerp (DECL_SIZE (decl), 1))
17894     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17895 }
17896
17897 /* If the compiled language is ANSI C, then add a 'prototyped'
17898    attribute, if arg types are given for the parameters of a function.  */
17899
17900 static inline void
17901 add_prototyped_attribute (dw_die_ref die, tree func_type)
17902 {
17903   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17904       && prototype_p (func_type))
17905     add_AT_flag (die, DW_AT_prototyped, 1);
17906 }
17907
17908 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17909    by looking in either the type declaration or object declaration
17910    equate table.  */
17911
17912 static inline dw_die_ref
17913 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17914 {
17915   dw_die_ref origin_die = NULL;
17916
17917   if (TREE_CODE (origin) != FUNCTION_DECL)
17918     {
17919       /* We may have gotten separated from the block for the inlined
17920          function, if we're in an exception handler or some such; make
17921          sure that the abstract function has been written out.
17922
17923          Doing this for nested functions is wrong, however; functions are
17924          distinct units, and our context might not even be inline.  */
17925       tree fn = origin;
17926
17927       if (TYPE_P (fn))
17928         fn = TYPE_STUB_DECL (fn);
17929
17930       fn = decl_function_context (fn);
17931       if (fn)
17932         dwarf2out_abstract_function (fn);
17933     }
17934
17935   if (DECL_P (origin))
17936     origin_die = lookup_decl_die (origin);
17937   else if (TYPE_P (origin))
17938     origin_die = lookup_type_die (origin);
17939
17940   /* XXX: Functions that are never lowered don't always have correct block
17941      trees (in the case of java, they simply have no block tree, in some other
17942      languages).  For these functions, there is nothing we can really do to
17943      output correct debug info for inlined functions in all cases.  Rather
17944      than die, we'll just produce deficient debug info now, in that we will
17945      have variables without a proper abstract origin.  In the future, when all
17946      functions are lowered, we should re-add a gcc_assert (origin_die)
17947      here.  */
17948
17949   if (origin_die)
17950     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17951   return origin_die;
17952 }
17953
17954 /* We do not currently support the pure_virtual attribute.  */
17955
17956 static inline void
17957 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17958 {
17959   if (DECL_VINDEX (func_decl))
17960     {
17961       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17962
17963       if (host_integerp (DECL_VINDEX (func_decl), 0))
17964         add_AT_loc (die, DW_AT_vtable_elem_location,
17965                     new_loc_descr (DW_OP_constu,
17966                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17967                                    0));
17968
17969       /* GNU extension: Record what type this method came from originally.  */
17970       if (debug_info_level > DINFO_LEVEL_TERSE
17971           && DECL_CONTEXT (func_decl))
17972         add_AT_die_ref (die, DW_AT_containing_type,
17973                         lookup_type_die (DECL_CONTEXT (func_decl)));
17974     }
17975 }
17976 \f
17977 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17978    given decl.  This used to be a vendor extension until after DWARF 4
17979    standardized it.  */
17980
17981 static void
17982 add_linkage_attr (dw_die_ref die, tree decl)
17983 {
17984   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17985
17986   /* Mimic what assemble_name_raw does with a leading '*'.  */
17987   if (name[0] == '*')
17988     name = &name[1];
17989
17990   if (dwarf_version >= 4)
17991     add_AT_string (die, DW_AT_linkage_name, name);
17992   else
17993     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17994 }
17995
17996 /* Add source coordinate attributes for the given decl.  */
17997
17998 static void
17999 add_src_coords_attributes (dw_die_ref die, tree decl)
18000 {
18001   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18002
18003   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18004   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18005 }
18006
18007 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18008
18009 static void
18010 add_linkage_name (dw_die_ref die, tree decl)
18011 {
18012   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18013        && TREE_PUBLIC (decl)
18014        && !DECL_ABSTRACT (decl)
18015        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18016        && die->die_tag != DW_TAG_member)
18017     {
18018       /* Defer until we have an assembler name set.  */
18019       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18020         {
18021           limbo_die_node *asm_name;
18022
18023           asm_name = ggc_alloc_cleared_limbo_die_node ();
18024           asm_name->die = die;
18025           asm_name->created_for = decl;
18026           asm_name->next = deferred_asm_name;
18027           deferred_asm_name = asm_name;
18028         }
18029       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18030         add_linkage_attr (die, decl);
18031     }
18032 }
18033
18034 /* Add a DW_AT_name attribute and source coordinate attribute for the
18035    given decl, but only if it actually has a name.  */
18036
18037 static void
18038 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18039 {
18040   tree decl_name;
18041
18042   decl_name = DECL_NAME (decl);
18043   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18044     {
18045       const char *name = dwarf2_name (decl, 0);
18046       if (name)
18047         add_name_attribute (die, name);
18048       if (! DECL_ARTIFICIAL (decl))
18049         add_src_coords_attributes (die, decl);
18050
18051       add_linkage_name (die, decl);
18052     }
18053
18054 #ifdef VMS_DEBUGGING_INFO
18055   /* Get the function's name, as described by its RTL.  This may be different
18056      from the DECL_NAME name used in the source file.  */
18057   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18058     {
18059       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18060                    XEXP (DECL_RTL (decl), 0));
18061       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
18062     }
18063 #endif /* VMS_DEBUGGING_INFO */
18064 }
18065
18066 #ifdef VMS_DEBUGGING_INFO
18067 /* Output the debug main pointer die for VMS */
18068
18069 void
18070 dwarf2out_vms_debug_main_pointer (void)
18071 {
18072   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18073   dw_die_ref die;
18074
18075   /* Allocate the VMS debug main subprogram die.  */
18076   die = ggc_alloc_cleared_die_node ();
18077   die->die_tag = DW_TAG_subprogram;
18078   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18079   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18080                                current_function_funcdef_no);
18081   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18082
18083   /* Make it the first child of comp_unit_die ().  */
18084   die->die_parent = comp_unit_die ();
18085   if (comp_unit_die ()->die_child)
18086     {
18087       die->die_sib = comp_unit_die ()->die_child->die_sib;
18088       comp_unit_die ()->die_child->die_sib = die;
18089     }
18090   else
18091     {
18092       die->die_sib = die;
18093       comp_unit_die ()->die_child = die;
18094     }
18095 }
18096 #endif /* VMS_DEBUGGING_INFO */
18097
18098 /* Push a new declaration scope.  */
18099
18100 static void
18101 push_decl_scope (tree scope)
18102 {
18103   VEC_safe_push (tree, gc, decl_scope_table, scope);
18104 }
18105
18106 /* Pop a declaration scope.  */
18107
18108 static inline void
18109 pop_decl_scope (void)
18110 {
18111   VEC_pop (tree, decl_scope_table);
18112 }
18113
18114 /* Return the DIE for the scope that immediately contains this type.
18115    Non-named types get global scope.  Named types nested in other
18116    types get their containing scope if it's open, or global scope
18117    otherwise.  All other types (i.e. function-local named types) get
18118    the current active scope.  */
18119
18120 static dw_die_ref
18121 scope_die_for (tree t, dw_die_ref context_die)
18122 {
18123   dw_die_ref scope_die = NULL;
18124   tree containing_scope;
18125   int i;
18126
18127   /* Non-types always go in the current scope.  */
18128   gcc_assert (TYPE_P (t));
18129
18130   containing_scope = TYPE_CONTEXT (t);
18131
18132   /* Use the containing namespace if it was passed in (for a declaration).  */
18133   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18134     {
18135       if (context_die == lookup_decl_die (containing_scope))
18136         /* OK */;
18137       else
18138         containing_scope = NULL_TREE;
18139     }
18140
18141   /* Ignore function type "scopes" from the C frontend.  They mean that
18142      a tagged type is local to a parmlist of a function declarator, but
18143      that isn't useful to DWARF.  */
18144   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18145     containing_scope = NULL_TREE;
18146
18147   if (SCOPE_FILE_SCOPE_P (containing_scope))
18148     scope_die = comp_unit_die ();
18149   else if (TYPE_P (containing_scope))
18150     {
18151       /* For types, we can just look up the appropriate DIE.  But
18152          first we check to see if we're in the middle of emitting it
18153          so we know where the new DIE should go.  */
18154       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18155         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18156           break;
18157
18158       if (i < 0)
18159         {
18160           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18161                       || TREE_ASM_WRITTEN (containing_scope));
18162           /*We are not in the middle of emitting the type
18163             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18164           scope_die = lookup_type_die (containing_scope);
18165
18166           /* If none of the current dies are suitable, we get file scope.  */
18167           if (scope_die == NULL)
18168             scope_die = comp_unit_die ();
18169         }
18170       else
18171         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18172     }
18173   else
18174     scope_die = context_die;
18175
18176   return scope_die;
18177 }
18178
18179 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18180
18181 static inline int
18182 local_scope_p (dw_die_ref context_die)
18183 {
18184   for (; context_die; context_die = context_die->die_parent)
18185     if (context_die->die_tag == DW_TAG_inlined_subroutine
18186         || context_die->die_tag == DW_TAG_subprogram)
18187       return 1;
18188
18189   return 0;
18190 }
18191
18192 /* Returns nonzero if CONTEXT_DIE is a class.  */
18193
18194 static inline int
18195 class_scope_p (dw_die_ref context_die)
18196 {
18197   return (context_die
18198           && (context_die->die_tag == DW_TAG_structure_type
18199               || context_die->die_tag == DW_TAG_class_type
18200               || context_die->die_tag == DW_TAG_interface_type
18201               || context_die->die_tag == DW_TAG_union_type));
18202 }
18203
18204 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18205    whether or not to treat a DIE in this context as a declaration.  */
18206
18207 static inline int
18208 class_or_namespace_scope_p (dw_die_ref context_die)
18209 {
18210   return (class_scope_p (context_die)
18211           || (context_die && context_die->die_tag == DW_TAG_namespace));
18212 }
18213
18214 /* Many forms of DIEs require a "type description" attribute.  This
18215    routine locates the proper "type descriptor" die for the type given
18216    by 'type', and adds a DW_AT_type attribute below the given die.  */
18217
18218 static void
18219 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18220                     int decl_volatile, dw_die_ref context_die)
18221 {
18222   enum tree_code code  = TREE_CODE (type);
18223   dw_die_ref type_die  = NULL;
18224
18225   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18226      or fixed-point type, use the inner type.  This is because we have no
18227      support for unnamed types in base_type_die.  This can happen if this is
18228      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18229   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18230       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18231     type = TREE_TYPE (type), code = TREE_CODE (type);
18232
18233   if (code == ERROR_MARK
18234       /* Handle a special case.  For functions whose return type is void, we
18235          generate *no* type attribute.  (Note that no object may have type
18236          `void', so this only applies to function return types).  */
18237       || code == VOID_TYPE)
18238     return;
18239
18240   type_die = modified_type_die (type,
18241                                 decl_const || TYPE_READONLY (type),
18242                                 decl_volatile || TYPE_VOLATILE (type),
18243                                 context_die);
18244
18245   if (type_die != NULL)
18246     add_AT_die_ref (object_die, DW_AT_type, type_die);
18247 }
18248
18249 /* Given an object die, add the calling convention attribute for the
18250    function call type.  */
18251 static void
18252 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18253 {
18254   enum dwarf_calling_convention value = DW_CC_normal;
18255
18256   value = ((enum dwarf_calling_convention)
18257            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18258
18259   if (is_fortran ()
18260       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18261     {
18262       /* DWARF 2 doesn't provide a way to identify a program's source-level
18263         entry point.  DW_AT_calling_convention attributes are only meant
18264         to describe functions' calling conventions.  However, lacking a
18265         better way to signal the Fortran main program, we used this for 
18266         a long time, following existing custom.  Now, DWARF 4 has 
18267         DW_AT_main_subprogram, which we add below, but some tools still
18268         rely on the old way, which we thus keep.  */
18269       value = DW_CC_program;
18270
18271       if (dwarf_version >= 4 || !dwarf_strict)
18272         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18273     }
18274
18275   /* Only add the attribute if the backend requests it, and
18276      is not DW_CC_normal.  */
18277   if (value && (value != DW_CC_normal))
18278     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18279 }
18280
18281 /* Given a tree pointer to a struct, class, union, or enum type node, return
18282    a pointer to the (string) tag name for the given type, or zero if the type
18283    was declared without a tag.  */
18284
18285 static const char *
18286 type_tag (const_tree type)
18287 {
18288   const char *name = 0;
18289
18290   if (TYPE_NAME (type) != 0)
18291     {
18292       tree t = 0;
18293
18294       /* Find the IDENTIFIER_NODE for the type name.  */
18295       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18296           && !TYPE_NAMELESS (type))
18297         t = TYPE_NAME (type);
18298
18299       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18300          a TYPE_DECL node, regardless of whether or not a `typedef' was
18301          involved.  */
18302       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18303                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18304         {
18305           /* We want to be extra verbose.  Don't call dwarf_name if
18306              DECL_NAME isn't set.  The default hook for decl_printable_name
18307              doesn't like that, and in this context it's correct to return
18308              0, instead of "<anonymous>" or the like.  */
18309           if (DECL_NAME (TYPE_NAME (type))
18310               && !DECL_NAMELESS (TYPE_NAME (type)))
18311             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18312         }
18313
18314       /* Now get the name as a string, or invent one.  */
18315       if (!name && t != 0)
18316         name = IDENTIFIER_POINTER (t);
18317     }
18318
18319   return (name == 0 || *name == '\0') ? 0 : name;
18320 }
18321
18322 /* Return the type associated with a data member, make a special check
18323    for bit field types.  */
18324
18325 static inline tree
18326 member_declared_type (const_tree member)
18327 {
18328   return (DECL_BIT_FIELD_TYPE (member)
18329           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18330 }
18331
18332 /* Get the decl's label, as described by its RTL. This may be different
18333    from the DECL_NAME name used in the source file.  */
18334
18335 #if 0
18336 static const char *
18337 decl_start_label (tree decl)
18338 {
18339   rtx x;
18340   const char *fnname;
18341
18342   x = DECL_RTL (decl);
18343   gcc_assert (MEM_P (x));
18344
18345   x = XEXP (x, 0);
18346   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18347
18348   fnname = XSTR (x, 0);
18349   return fnname;
18350 }
18351 #endif
18352 \f
18353 /* These routines generate the internal representation of the DIE's for
18354    the compilation unit.  Debugging information is collected by walking
18355    the declaration trees passed in from dwarf2out_decl().  */
18356
18357 static void
18358 gen_array_type_die (tree type, dw_die_ref context_die)
18359 {
18360   dw_die_ref scope_die = scope_die_for (type, context_die);
18361   dw_die_ref array_die;
18362
18363   /* GNU compilers represent multidimensional array types as sequences of one
18364      dimensional array types whose element types are themselves array types.
18365      We sometimes squish that down to a single array_type DIE with multiple
18366      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18367      say that we are allowed to do this kind of compression in C, because
18368      there is no difference between an array of arrays and a multidimensional
18369      array.  We don't do this for Ada to remain as close as possible to the
18370      actual representation, which is especially important against the language
18371      flexibilty wrt arrays of variable size.  */
18372
18373   bool collapse_nested_arrays = !is_ada ();
18374   tree element_type;
18375
18376   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18377      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18378   if (TYPE_STRING_FLAG (type)
18379       && TREE_CODE (type) == ARRAY_TYPE
18380       && is_fortran ()
18381       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18382     {
18383       HOST_WIDE_INT size;
18384
18385       array_die = new_die (DW_TAG_string_type, scope_die, type);
18386       add_name_attribute (array_die, type_tag (type));
18387       equate_type_number_to_die (type, array_die);
18388       size = int_size_in_bytes (type);
18389       if (size >= 0)
18390         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18391       else if (TYPE_DOMAIN (type) != NULL_TREE
18392                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18393                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18394         {
18395           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18396           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18397
18398           size = int_size_in_bytes (TREE_TYPE (szdecl));
18399           if (loc && size > 0)
18400             {
18401               add_AT_location_description (array_die, DW_AT_string_length, loc);
18402               if (size != DWARF2_ADDR_SIZE)
18403                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18404             }
18405         }
18406       return;
18407     }
18408
18409   /* ??? The SGI dwarf reader fails for array of array of enum types
18410      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18411      array type comes before the outer array type.  We thus call gen_type_die
18412      before we new_die and must prevent nested array types collapsing for this
18413      target.  */
18414
18415 #ifdef MIPS_DEBUGGING_INFO
18416   gen_type_die (TREE_TYPE (type), context_die);
18417   collapse_nested_arrays = false;
18418 #endif
18419
18420   array_die = new_die (DW_TAG_array_type, scope_die, type);
18421   add_name_attribute (array_die, type_tag (type));
18422   equate_type_number_to_die (type, array_die);
18423
18424   if (TREE_CODE (type) == VECTOR_TYPE)
18425     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18426
18427   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18428   if (is_fortran ()
18429       && TREE_CODE (type) == ARRAY_TYPE
18430       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18431       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18432     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18433
18434 #if 0
18435   /* We default the array ordering.  SDB will probably do
18436      the right things even if DW_AT_ordering is not present.  It's not even
18437      an issue until we start to get into multidimensional arrays anyway.  If
18438      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18439      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18440      and when we find out that we need to put these in, we will only do so
18441      for multidimensional arrays.  */
18442   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18443 #endif
18444
18445 #ifdef MIPS_DEBUGGING_INFO
18446   /* The SGI compilers handle arrays of unknown bound by setting
18447      AT_declaration and not emitting any subrange DIEs.  */
18448   if (TREE_CODE (type) == ARRAY_TYPE
18449       && ! TYPE_DOMAIN (type))
18450     add_AT_flag (array_die, DW_AT_declaration, 1);
18451   else
18452 #endif
18453   if (TREE_CODE (type) == VECTOR_TYPE)
18454     {
18455       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18456       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18457       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18458       add_bound_info (subrange_die, DW_AT_upper_bound,
18459                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18460     }
18461   else
18462     add_subscript_info (array_die, type, collapse_nested_arrays);
18463
18464   /* Add representation of the type of the elements of this array type and
18465      emit the corresponding DIE if we haven't done it already.  */
18466   element_type = TREE_TYPE (type);
18467   if (collapse_nested_arrays)
18468     while (TREE_CODE (element_type) == ARRAY_TYPE)
18469       {
18470         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18471           break;
18472         element_type = TREE_TYPE (element_type);
18473       }
18474
18475 #ifndef MIPS_DEBUGGING_INFO
18476   gen_type_die (element_type, context_die);
18477 #endif
18478
18479   add_type_attribute (array_die, element_type, 0, 0, context_die);
18480
18481   if (get_AT (array_die, DW_AT_name))
18482     add_pubtype (type, array_die);
18483 }
18484
18485 static dw_loc_descr_ref
18486 descr_info_loc (tree val, tree base_decl)
18487 {
18488   HOST_WIDE_INT size;
18489   dw_loc_descr_ref loc, loc2;
18490   enum dwarf_location_atom op;
18491
18492   if (val == base_decl)
18493     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18494
18495   switch (TREE_CODE (val))
18496     {
18497     CASE_CONVERT:
18498       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18499     case VAR_DECL:
18500       return loc_descriptor_from_tree (val, 0);
18501     case INTEGER_CST:
18502       if (host_integerp (val, 0))
18503         return int_loc_descriptor (tree_low_cst (val, 0));
18504       break;
18505     case INDIRECT_REF:
18506       size = int_size_in_bytes (TREE_TYPE (val));
18507       if (size < 0)
18508         break;
18509       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18510       if (!loc)
18511         break;
18512       if (size == DWARF2_ADDR_SIZE)
18513         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18514       else
18515         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18516       return loc;
18517     case POINTER_PLUS_EXPR:
18518     case PLUS_EXPR:
18519       if (host_integerp (TREE_OPERAND (val, 1), 1)
18520           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18521              < 16384)
18522         {
18523           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18524           if (!loc)
18525             break;
18526           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18527         }
18528       else
18529         {
18530           op = DW_OP_plus;
18531         do_binop:
18532           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18533           if (!loc)
18534             break;
18535           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18536           if (!loc2)
18537             break;
18538           add_loc_descr (&loc, loc2);
18539           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18540         }
18541       return loc;
18542     case MINUS_EXPR:
18543       op = DW_OP_minus;
18544       goto do_binop;
18545     case MULT_EXPR:
18546       op = DW_OP_mul;
18547       goto do_binop;
18548     case EQ_EXPR:
18549       op = DW_OP_eq;
18550       goto do_binop;
18551     case NE_EXPR:
18552       op = DW_OP_ne;
18553       goto do_binop;
18554     default:
18555       break;
18556     }
18557   return NULL;
18558 }
18559
18560 static void
18561 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18562                       tree val, tree base_decl)
18563 {
18564   dw_loc_descr_ref loc;
18565
18566   if (host_integerp (val, 0))
18567     {
18568       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18569       return;
18570     }
18571
18572   loc = descr_info_loc (val, base_decl);
18573   if (!loc)
18574     return;
18575
18576   add_AT_loc (die, attr, loc);
18577 }
18578
18579 /* This routine generates DIE for array with hidden descriptor, details
18580    are filled into *info by a langhook.  */
18581
18582 static void
18583 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18584                           dw_die_ref context_die)
18585 {
18586   dw_die_ref scope_die = scope_die_for (type, context_die);
18587   dw_die_ref array_die;
18588   int dim;
18589
18590   array_die = new_die (DW_TAG_array_type, scope_die, type);
18591   add_name_attribute (array_die, type_tag (type));
18592   equate_type_number_to_die (type, array_die);
18593
18594   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18595   if (is_fortran ()
18596       && info->ndimensions >= 2)
18597     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18598
18599   if (info->data_location)
18600     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18601                           info->base_decl);
18602   if (info->associated)
18603     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18604                           info->base_decl);
18605   if (info->allocated)
18606     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18607                           info->base_decl);
18608
18609   for (dim = 0; dim < info->ndimensions; dim++)
18610     {
18611       dw_die_ref subrange_die
18612         = new_die (DW_TAG_subrange_type, array_die, NULL);
18613
18614       if (info->dimen[dim].lower_bound)
18615         {
18616           /* If it is the default value, omit it.  */
18617           int dflt;
18618
18619           if (host_integerp (info->dimen[dim].lower_bound, 0)
18620               && (dflt = lower_bound_default ()) != -1
18621               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18622             ;
18623           else
18624             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18625                                   info->dimen[dim].lower_bound,
18626                                   info->base_decl);
18627         }
18628       if (info->dimen[dim].upper_bound)
18629         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18630                               info->dimen[dim].upper_bound,
18631                               info->base_decl);
18632       if (info->dimen[dim].stride)
18633         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18634                               info->dimen[dim].stride,
18635                               info->base_decl);
18636     }
18637
18638   gen_type_die (info->element_type, context_die);
18639   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18640
18641   if (get_AT (array_die, DW_AT_name))
18642     add_pubtype (type, array_die);
18643 }
18644
18645 #if 0
18646 static void
18647 gen_entry_point_die (tree decl, dw_die_ref context_die)
18648 {
18649   tree origin = decl_ultimate_origin (decl);
18650   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18651
18652   if (origin != NULL)
18653     add_abstract_origin_attribute (decl_die, origin);
18654   else
18655     {
18656       add_name_and_src_coords_attributes (decl_die, decl);
18657       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18658                           0, 0, context_die);
18659     }
18660
18661   if (DECL_ABSTRACT (decl))
18662     equate_decl_number_to_die (decl, decl_die);
18663   else
18664     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18665 }
18666 #endif
18667
18668 /* Walk through the list of incomplete types again, trying once more to
18669    emit full debugging info for them.  */
18670
18671 static void
18672 retry_incomplete_types (void)
18673 {
18674   int i;
18675
18676   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18677     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18678                                   DINFO_USAGE_DIR_USE))
18679       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18680 }
18681
18682 /* Determine what tag to use for a record type.  */
18683
18684 static enum dwarf_tag
18685 record_type_tag (tree type)
18686 {
18687   if (! lang_hooks.types.classify_record)
18688     return DW_TAG_structure_type;
18689
18690   switch (lang_hooks.types.classify_record (type))
18691     {
18692     case RECORD_IS_STRUCT:
18693       return DW_TAG_structure_type;
18694
18695     case RECORD_IS_CLASS:
18696       return DW_TAG_class_type;
18697
18698     case RECORD_IS_INTERFACE:
18699       if (dwarf_version >= 3 || !dwarf_strict)
18700         return DW_TAG_interface_type;
18701       return DW_TAG_structure_type;
18702
18703     default:
18704       gcc_unreachable ();
18705     }
18706 }
18707
18708 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18709    include all of the information about the enumeration values also. Each
18710    enumerated type name/value is listed as a child of the enumerated type
18711    DIE.  */
18712
18713 static dw_die_ref
18714 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18715 {
18716   dw_die_ref type_die = lookup_type_die (type);
18717
18718   if (type_die == NULL)
18719     {
18720       type_die = new_die (DW_TAG_enumeration_type,
18721                           scope_die_for (type, context_die), type);
18722       equate_type_number_to_die (type, type_die);
18723       add_name_attribute (type_die, type_tag (type));
18724       if (dwarf_version >= 4 || !dwarf_strict)
18725         {
18726           if (ENUM_IS_SCOPED (type))
18727             add_AT_flag (type_die, DW_AT_enum_class, 1);
18728           if (ENUM_IS_OPAQUE (type))
18729             add_AT_flag (type_die, DW_AT_declaration, 1);
18730         }
18731     }
18732   else if (! TYPE_SIZE (type))
18733     return type_die;
18734   else
18735     remove_AT (type_die, DW_AT_declaration);
18736
18737   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18738      given enum type is incomplete, do not generate the DW_AT_byte_size
18739      attribute or the DW_AT_element_list attribute.  */
18740   if (TYPE_SIZE (type))
18741     {
18742       tree link;
18743
18744       TREE_ASM_WRITTEN (type) = 1;
18745       add_byte_size_attribute (type_die, type);
18746       if (TYPE_STUB_DECL (type) != NULL_TREE)
18747         {
18748           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18749           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18750         }
18751
18752       /* If the first reference to this type was as the return type of an
18753          inline function, then it may not have a parent.  Fix this now.  */
18754       if (type_die->die_parent == NULL)
18755         add_child_die (scope_die_for (type, context_die), type_die);
18756
18757       for (link = TYPE_VALUES (type);
18758            link != NULL; link = TREE_CHAIN (link))
18759         {
18760           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18761           tree value = TREE_VALUE (link);
18762
18763           add_name_attribute (enum_die,
18764                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18765
18766           if (TREE_CODE (value) == CONST_DECL)
18767             value = DECL_INITIAL (value);
18768
18769           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18770             /* DWARF2 does not provide a way of indicating whether or
18771                not enumeration constants are signed or unsigned.  GDB
18772                always assumes the values are signed, so we output all
18773                values as if they were signed.  That means that
18774                enumeration constants with very large unsigned values
18775                will appear to have negative values in the debugger.  */
18776             add_AT_int (enum_die, DW_AT_const_value,
18777                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18778         }
18779     }
18780   else
18781     add_AT_flag (type_die, DW_AT_declaration, 1);
18782
18783   if (get_AT (type_die, DW_AT_name))
18784     add_pubtype (type, type_die);
18785
18786   return type_die;
18787 }
18788
18789 /* Generate a DIE to represent either a real live formal parameter decl or to
18790    represent just the type of some formal parameter position in some function
18791    type.
18792
18793    Note that this routine is a bit unusual because its argument may be a
18794    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18795    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18796    node.  If it's the former then this function is being called to output a
18797    DIE to represent a formal parameter object (or some inlining thereof).  If
18798    it's the latter, then this function is only being called to output a
18799    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18800    argument type of some subprogram type.
18801    If EMIT_NAME_P is true, name and source coordinate attributes
18802    are emitted.  */
18803
18804 static dw_die_ref
18805 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18806                           dw_die_ref context_die)
18807 {
18808   tree node_or_origin = node ? node : origin;
18809   tree ultimate_origin;
18810   dw_die_ref parm_die
18811     = new_die (DW_TAG_formal_parameter, context_die, node);
18812
18813   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18814     {
18815     case tcc_declaration:
18816       ultimate_origin = decl_ultimate_origin (node_or_origin);
18817       if (node || ultimate_origin)
18818         origin = ultimate_origin;
18819       if (origin != NULL)
18820         add_abstract_origin_attribute (parm_die, origin);
18821       else if (emit_name_p)
18822         add_name_and_src_coords_attributes (parm_die, node);
18823       if (origin == NULL
18824           || (! DECL_ABSTRACT (node_or_origin)
18825               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18826                                            decl_function_context
18827                                                             (node_or_origin))))
18828         {
18829           tree type = TREE_TYPE (node_or_origin);
18830           if (decl_by_reference_p (node_or_origin))
18831             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18832                                 context_die);
18833           else
18834             add_type_attribute (parm_die, type,
18835                                 TREE_READONLY (node_or_origin),
18836                                 TREE_THIS_VOLATILE (node_or_origin),
18837                                 context_die);
18838         }
18839       if (origin == NULL && DECL_ARTIFICIAL (node))
18840         add_AT_flag (parm_die, DW_AT_artificial, 1);
18841
18842       if (node && node != origin)
18843         equate_decl_number_to_die (node, parm_die);
18844       if (! DECL_ABSTRACT (node_or_origin))
18845         add_location_or_const_value_attribute (parm_die, node_or_origin,
18846                                                node == NULL, DW_AT_location);
18847
18848       break;
18849
18850     case tcc_type:
18851       /* We were called with some kind of a ..._TYPE node.  */
18852       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18853       break;
18854
18855     default:
18856       gcc_unreachable ();
18857     }
18858
18859   return parm_die;
18860 }
18861
18862 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18863    children DW_TAG_formal_parameter DIEs representing the arguments of the
18864    parameter pack.
18865
18866    PARM_PACK must be a function parameter pack.
18867    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18868    must point to the subsequent arguments of the function PACK_ARG belongs to.
18869    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18870    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18871    following the last one for which a DIE was generated.  */
18872
18873 static dw_die_ref
18874 gen_formal_parameter_pack_die  (tree parm_pack,
18875                                 tree pack_arg,
18876                                 dw_die_ref subr_die,
18877                                 tree *next_arg)
18878 {
18879   tree arg;
18880   dw_die_ref parm_pack_die;
18881
18882   gcc_assert (parm_pack
18883               && lang_hooks.function_parameter_pack_p (parm_pack)
18884               && subr_die);
18885
18886   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18887   add_src_coords_attributes (parm_pack_die, parm_pack);
18888
18889   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18890     {
18891       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18892                                                                  parm_pack))
18893         break;
18894       gen_formal_parameter_die (arg, NULL,
18895                                 false /* Don't emit name attribute.  */,
18896                                 parm_pack_die);
18897     }
18898   if (next_arg)
18899     *next_arg = arg;
18900   return parm_pack_die;
18901 }
18902
18903 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18904    at the end of an (ANSI prototyped) formal parameters list.  */
18905
18906 static void
18907 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18908 {
18909   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18910 }
18911
18912 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18913    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18914    parameters as specified in some function type specification (except for
18915    those which appear as part of a function *definition*).  */
18916
18917 static void
18918 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18919 {
18920   tree link;
18921   tree formal_type = NULL;
18922   tree first_parm_type;
18923   tree arg;
18924
18925   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18926     {
18927       arg = DECL_ARGUMENTS (function_or_method_type);
18928       function_or_method_type = TREE_TYPE (function_or_method_type);
18929     }
18930   else
18931     arg = NULL_TREE;
18932
18933   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18934
18935   /* Make our first pass over the list of formal parameter types and output a
18936      DW_TAG_formal_parameter DIE for each one.  */
18937   for (link = first_parm_type; link; )
18938     {
18939       dw_die_ref parm_die;
18940
18941       formal_type = TREE_VALUE (link);
18942       if (formal_type == void_type_node)
18943         break;
18944
18945       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18946       parm_die = gen_formal_parameter_die (formal_type, NULL,
18947                                            true /* Emit name attribute.  */,
18948                                            context_die);
18949       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18950           && link == first_parm_type)
18951         {
18952           add_AT_flag (parm_die, DW_AT_artificial, 1);
18953           if (dwarf_version >= 3 || !dwarf_strict)
18954             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18955         }
18956       else if (arg && DECL_ARTIFICIAL (arg))
18957         add_AT_flag (parm_die, DW_AT_artificial, 1);
18958
18959       link = TREE_CHAIN (link);
18960       if (arg)
18961         arg = DECL_CHAIN (arg);
18962     }
18963
18964   /* If this function type has an ellipsis, add a
18965      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18966   if (formal_type != void_type_node)
18967     gen_unspecified_parameters_die (function_or_method_type, context_die);
18968
18969   /* Make our second (and final) pass over the list of formal parameter types
18970      and output DIEs to represent those types (as necessary).  */
18971   for (link = TYPE_ARG_TYPES (function_or_method_type);
18972        link && TREE_VALUE (link);
18973        link = TREE_CHAIN (link))
18974     gen_type_die (TREE_VALUE (link), context_die);
18975 }
18976
18977 /* We want to generate the DIE for TYPE so that we can generate the
18978    die for MEMBER, which has been defined; we will need to refer back
18979    to the member declaration nested within TYPE.  If we're trying to
18980    generate minimal debug info for TYPE, processing TYPE won't do the
18981    trick; we need to attach the member declaration by hand.  */
18982
18983 static void
18984 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18985 {
18986   gen_type_die (type, context_die);
18987
18988   /* If we're trying to avoid duplicate debug info, we may not have
18989      emitted the member decl for this function.  Emit it now.  */
18990   if (TYPE_STUB_DECL (type)
18991       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18992       && ! lookup_decl_die (member))
18993     {
18994       dw_die_ref type_die;
18995       gcc_assert (!decl_ultimate_origin (member));
18996
18997       push_decl_scope (type);
18998       type_die = lookup_type_die_strip_naming_typedef (type);
18999       if (TREE_CODE (member) == FUNCTION_DECL)
19000         gen_subprogram_die (member, type_die);
19001       else if (TREE_CODE (member) == FIELD_DECL)
19002         {
19003           /* Ignore the nameless fields that are used to skip bits but handle
19004              C++ anonymous unions and structs.  */
19005           if (DECL_NAME (member) != NULL_TREE
19006               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19007               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19008             {
19009               gen_type_die (member_declared_type (member), type_die);
19010               gen_field_die (member, type_die);
19011             }
19012         }
19013       else
19014         gen_variable_die (member, NULL_TREE, type_die);
19015
19016       pop_decl_scope ();
19017     }
19018 }
19019
19020 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19021    may later generate inlined and/or out-of-line instances of.  */
19022
19023 static void
19024 dwarf2out_abstract_function (tree decl)
19025 {
19026   dw_die_ref old_die;
19027   tree save_fn;
19028   tree context;
19029   int was_abstract;
19030   htab_t old_decl_loc_table;
19031   htab_t old_cached_dw_loc_list_table;
19032
19033   /* Make sure we have the actual abstract inline, not a clone.  */
19034   decl = DECL_ORIGIN (decl);
19035
19036   old_die = lookup_decl_die (decl);
19037   if (old_die && get_AT (old_die, DW_AT_inline))
19038     /* We've already generated the abstract instance.  */
19039     return;
19040
19041   /* We can be called while recursively when seeing block defining inlined subroutine
19042      DIE.  Be sure to not clobber the outer location table nor use it or we would
19043      get locations in abstract instantces.  */
19044   old_decl_loc_table = decl_loc_table;
19045   decl_loc_table = NULL;
19046   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
19047   cached_dw_loc_list_table = NULL;
19048
19049   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
19050      we don't get confused by DECL_ABSTRACT.  */
19051   if (debug_info_level > DINFO_LEVEL_TERSE)
19052     {
19053       context = decl_class_context (decl);
19054       if (context)
19055         gen_type_die_for_member
19056           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
19057     }
19058
19059   /* Pretend we've just finished compiling this function.  */
19060   save_fn = current_function_decl;
19061   current_function_decl = decl;
19062   push_cfun (DECL_STRUCT_FUNCTION (decl));
19063
19064   was_abstract = DECL_ABSTRACT (decl);
19065   set_decl_abstract_flags (decl, 1);
19066   dwarf2out_decl (decl);
19067   if (! was_abstract)
19068     set_decl_abstract_flags (decl, 0);
19069
19070   current_function_decl = save_fn;
19071   decl_loc_table = old_decl_loc_table;
19072   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
19073   pop_cfun ();
19074 }
19075
19076 /* Helper function of premark_used_types() which gets called through
19077    htab_traverse.
19078
19079    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19080    marked as unused by prune_unused_types.  */
19081
19082 static int
19083 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
19084 {
19085   tree type;
19086   dw_die_ref die;
19087
19088   type = (tree) *slot;
19089   die = lookup_type_die (type);
19090   if (die != NULL)
19091     die->die_perennial_p = 1;
19092   return 1;
19093 }
19094
19095 /* Helper function of premark_types_used_by_global_vars which gets called
19096    through htab_traverse.
19097
19098    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19099    marked as unused by prune_unused_types. The DIE of the type is marked
19100    only if the global variable using the type will actually be emitted.  */
19101
19102 static int
19103 premark_types_used_by_global_vars_helper (void **slot,
19104                                           void *data ATTRIBUTE_UNUSED)
19105 {
19106   struct types_used_by_vars_entry *entry;
19107   dw_die_ref die;
19108
19109   entry = (struct types_used_by_vars_entry *) *slot;
19110   gcc_assert (entry->type != NULL
19111               && entry->var_decl != NULL);
19112   die = lookup_type_die (entry->type);
19113   if (die)
19114     {
19115       /* Ask cgraph if the global variable really is to be emitted.
19116          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19117       struct varpool_node *node = varpool_get_node (entry->var_decl);
19118       if (node && node->needed)
19119         {
19120           die->die_perennial_p = 1;
19121           /* Keep the parent DIEs as well.  */
19122           while ((die = die->die_parent) && die->die_perennial_p == 0)
19123             die->die_perennial_p = 1;
19124         }
19125     }
19126   return 1;
19127 }
19128
19129 /* Mark all members of used_types_hash as perennial.  */
19130
19131 static void
19132 premark_used_types (void)
19133 {
19134   if (cfun && cfun->used_types_hash)
19135     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19136 }
19137
19138 /* Mark all members of types_used_by_vars_entry as perennial.  */
19139
19140 static void
19141 premark_types_used_by_global_vars (void)
19142 {
19143   if (types_used_by_vars_hash)
19144     htab_traverse (types_used_by_vars_hash,
19145                    premark_types_used_by_global_vars_helper, NULL);
19146 }
19147
19148 /* Generate a DIE to represent a declared function (either file-scope or
19149    block-local).  */
19150
19151 static void
19152 gen_subprogram_die (tree decl, dw_die_ref context_die)
19153 {
19154   tree origin = decl_ultimate_origin (decl);
19155   dw_die_ref subr_die;
19156   tree outer_scope;
19157   dw_die_ref old_die = lookup_decl_die (decl);
19158   int declaration = (current_function_decl != decl
19159                      || class_or_namespace_scope_p (context_die));
19160
19161   premark_used_types ();
19162
19163   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19164      started to generate the abstract instance of an inline, decided to output
19165      its containing class, and proceeded to emit the declaration of the inline
19166      from the member list for the class.  If so, DECLARATION takes priority;
19167      we'll get back to the abstract instance when done with the class.  */
19168
19169   /* The class-scope declaration DIE must be the primary DIE.  */
19170   if (origin && declaration && class_or_namespace_scope_p (context_die))
19171     {
19172       origin = NULL;
19173       gcc_assert (!old_die);
19174     }
19175
19176   /* Now that the C++ front end lazily declares artificial member fns, we
19177      might need to retrofit the declaration into its class.  */
19178   if (!declaration && !origin && !old_die
19179       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19180       && !class_or_namespace_scope_p (context_die)
19181       && debug_info_level > DINFO_LEVEL_TERSE)
19182     old_die = force_decl_die (decl);
19183
19184   if (origin != NULL)
19185     {
19186       gcc_assert (!declaration || local_scope_p (context_die));
19187
19188       /* Fixup die_parent for the abstract instance of a nested
19189          inline function.  */
19190       if (old_die && old_die->die_parent == NULL)
19191         add_child_die (context_die, old_die);
19192
19193       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19194       add_abstract_origin_attribute (subr_die, origin);
19195     }
19196   else if (old_die)
19197     {
19198       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19199       struct dwarf_file_data * file_index = lookup_filename (s.file);
19200
19201       if (!get_AT_flag (old_die, DW_AT_declaration)
19202           /* We can have a normal definition following an inline one in the
19203              case of redefinition of GNU C extern inlines.
19204              It seems reasonable to use AT_specification in this case.  */
19205           && !get_AT (old_die, DW_AT_inline))
19206         {
19207           /* Detect and ignore this case, where we are trying to output
19208              something we have already output.  */
19209           return;
19210         }
19211
19212       /* If the definition comes from the same place as the declaration,
19213          maybe use the old DIE.  We always want the DIE for this function
19214          that has the *_pc attributes to be under comp_unit_die so the
19215          debugger can find it.  We also need to do this for abstract
19216          instances of inlines, since the spec requires the out-of-line copy
19217          to have the same parent.  For local class methods, this doesn't
19218          apply; we just use the old DIE.  */
19219       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19220           && (DECL_ARTIFICIAL (decl)
19221               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19222                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19223                       == (unsigned) s.line))))
19224         {
19225           subr_die = old_die;
19226
19227           /* Clear out the declaration attribute and the formal parameters.
19228              Do not remove all children, because it is possible that this
19229              declaration die was forced using force_decl_die(). In such
19230              cases die that forced declaration die (e.g. TAG_imported_module)
19231              is one of the children that we do not want to remove.  */
19232           remove_AT (subr_die, DW_AT_declaration);
19233           remove_AT (subr_die, DW_AT_object_pointer);
19234           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19235         }
19236       else
19237         {
19238           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19239           add_AT_specification (subr_die, old_die);
19240           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19241             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19242           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19243             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19244         }
19245     }
19246   else
19247     {
19248       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19249
19250       if (TREE_PUBLIC (decl))
19251         add_AT_flag (subr_die, DW_AT_external, 1);
19252
19253       add_name_and_src_coords_attributes (subr_die, decl);
19254       if (debug_info_level > DINFO_LEVEL_TERSE)
19255         {
19256           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19257           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19258                               0, 0, context_die);
19259         }
19260
19261       add_pure_or_virtual_attribute (subr_die, decl);
19262       if (DECL_ARTIFICIAL (decl))
19263         add_AT_flag (subr_die, DW_AT_artificial, 1);
19264
19265       add_accessibility_attribute (subr_die, decl);
19266     }
19267
19268   if (declaration)
19269     {
19270       if (!old_die || !get_AT (old_die, DW_AT_inline))
19271         {
19272           add_AT_flag (subr_die, DW_AT_declaration, 1);
19273
19274           /* If this is an explicit function declaration then generate
19275              a DW_AT_explicit attribute.  */
19276           if (lang_hooks.decls.function_decl_explicit_p (decl)
19277               && (dwarf_version >= 3 || !dwarf_strict))
19278             add_AT_flag (subr_die, DW_AT_explicit, 1);
19279
19280           /* The first time we see a member function, it is in the context of
19281              the class to which it belongs.  We make sure of this by emitting
19282              the class first.  The next time is the definition, which is
19283              handled above.  The two may come from the same source text.
19284
19285              Note that force_decl_die() forces function declaration die. It is
19286              later reused to represent definition.  */
19287           equate_decl_number_to_die (decl, subr_die);
19288         }
19289     }
19290   else if (DECL_ABSTRACT (decl))
19291     {
19292       if (DECL_DECLARED_INLINE_P (decl))
19293         {
19294           if (cgraph_function_possibly_inlined_p (decl))
19295             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19296           else
19297             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19298         }
19299       else
19300         {
19301           if (cgraph_function_possibly_inlined_p (decl))
19302             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19303           else
19304             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19305         }
19306
19307       if (DECL_DECLARED_INLINE_P (decl)
19308           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19309         add_AT_flag (subr_die, DW_AT_artificial, 1);
19310
19311       equate_decl_number_to_die (decl, subr_die);
19312     }
19313   else if (!DECL_EXTERNAL (decl))
19314     {
19315       HOST_WIDE_INT cfa_fb_offset;
19316
19317       if (!old_die || !get_AT (old_die, DW_AT_inline))
19318         equate_decl_number_to_die (decl, subr_die);
19319
19320       if (!flag_reorder_blocks_and_partition)
19321         {
19322           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19323           if (fde->dw_fde_begin)
19324             {
19325               /* We have already generated the labels.  */
19326               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19327               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19328             }
19329           else
19330             {
19331               /* Create start/end labels and add the range.  */
19332               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19333               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19334                                            current_function_funcdef_no);
19335               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19336               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19337                                            current_function_funcdef_no);
19338               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19339             }
19340
19341 #if VMS_DEBUGGING_INFO
19342       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19343          Section 2.3 Prologue and Epilogue Attributes:
19344          When a breakpoint is set on entry to a function, it is generally
19345          desirable for execution to be suspended, not on the very first
19346          instruction of the function, but rather at a point after the
19347          function's frame has been set up, after any language defined local
19348          declaration processing has been completed, and before execution of
19349          the first statement of the function begins. Debuggers generally
19350          cannot properly determine where this point is.  Similarly for a
19351          breakpoint set on exit from a function. The prologue and epilogue
19352          attributes allow a compiler to communicate the location(s) to use.  */
19353
19354       {
19355         if (fde->dw_fde_vms_end_prologue)
19356           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19357             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19358
19359         if (fde->dw_fde_vms_begin_epilogue)
19360           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19361             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19362       }
19363 #endif
19364
19365           add_pubname (decl, subr_die);
19366         }
19367       else
19368         {  /* Generate pubnames entries for the split function code
19369               ranges.  */
19370           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19371
19372           if (fde->dw_fde_second_begin)
19373             {
19374               if (dwarf_version >= 3 || !dwarf_strict)
19375                 {
19376                   /* We should use ranges for non-contiguous code section 
19377                      addresses.  Use the actual code range for the initial
19378                      section, since the HOT/COLD labels might precede an 
19379                      alignment offset.  */
19380                   bool range_list_added = false;
19381                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19382                                         fde->dw_fde_end, &range_list_added);
19383                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19384                                         fde->dw_fde_second_end,
19385                                         &range_list_added);
19386                   add_pubname (decl, subr_die);
19387                   if (range_list_added)
19388                     add_ranges (NULL);
19389                 }
19390               else
19391                 {
19392                   /* There is no real support in DW2 for this .. so we make
19393                      a work-around.  First, emit the pub name for the segment
19394                      containing the function label.  Then make and emit a
19395                      simplified subprogram DIE for the second segment with the
19396                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19397                      linkage name for the second die so that gdb will find both
19398                      sections when given "b foo".  */
19399                   const char *name = NULL;
19400                   tree decl_name = DECL_NAME (decl);
19401                   dw_die_ref seg_die;
19402
19403                   /* Do the 'primary' section.   */
19404                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19405                                  fde->dw_fde_begin);
19406                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19407                                  fde->dw_fde_end);
19408                   /* Add it.   */
19409                   add_pubname (decl, subr_die);
19410
19411                   /* Build a minimal DIE for the secondary section.  */
19412                   seg_die = new_die (DW_TAG_subprogram,
19413                                      subr_die->die_parent, decl);
19414
19415                   if (TREE_PUBLIC (decl))
19416                     add_AT_flag (seg_die, DW_AT_external, 1);
19417
19418                   if (decl_name != NULL 
19419                       && IDENTIFIER_POINTER (decl_name) != NULL)
19420                     {
19421                       name = dwarf2_name (decl, 1);
19422                       if (! DECL_ARTIFICIAL (decl))
19423                         add_src_coords_attributes (seg_die, decl);
19424
19425                       add_linkage_name (seg_die, decl);
19426                     }
19427                   gcc_assert (name != NULL);
19428                   add_pure_or_virtual_attribute (seg_die, decl);
19429                   if (DECL_ARTIFICIAL (decl))
19430                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19431
19432                   name = concat ("__second_sect_of_", name, NULL); 
19433                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19434                                  fde->dw_fde_second_begin);
19435                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19436                                  fde->dw_fde_second_end);
19437                   add_name_attribute (seg_die, name);
19438                   add_pubname_string (name, seg_die);
19439                 }
19440             }
19441           else
19442             {
19443               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19444               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19445               add_pubname (decl, subr_die);
19446             }
19447         }
19448
19449 #ifdef MIPS_DEBUGGING_INFO
19450       /* Add a reference to the FDE for this routine.  */
19451       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19452 #endif
19453
19454       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19455
19456       /* We define the "frame base" as the function's CFA.  This is more
19457          convenient for several reasons: (1) It's stable across the prologue
19458          and epilogue, which makes it better than just a frame pointer,
19459          (2) With dwarf3, there exists a one-byte encoding that allows us
19460          to reference the .debug_frame data by proxy, but failing that,
19461          (3) We can at least reuse the code inspection and interpretation
19462          code that determines the CFA position at various points in the
19463          function.  */
19464       if (dwarf_version >= 3)
19465         {
19466           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19467           add_AT_loc (subr_die, DW_AT_frame_base, op);
19468         }
19469       else
19470         {
19471           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19472           if (list->dw_loc_next)
19473             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19474           else
19475             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19476         }
19477
19478       /* Compute a displacement from the "steady-state frame pointer" to
19479          the CFA.  The former is what all stack slots and argument slots
19480          will reference in the rtl; the later is what we've told the
19481          debugger about.  We'll need to adjust all frame_base references
19482          by this displacement.  */
19483       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19484
19485       if (cfun->static_chain_decl)
19486         add_AT_location_description (subr_die, DW_AT_static_link,
19487                  loc_list_from_tree (cfun->static_chain_decl, 2));
19488     }
19489
19490   /* Generate child dies for template paramaters.  */
19491   if (debug_info_level > DINFO_LEVEL_TERSE)
19492     gen_generic_params_dies (decl);
19493
19494   /* Now output descriptions of the arguments for this function. This gets
19495      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19496      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19497      `...' at the end of the formal parameter list.  In order to find out if
19498      there was a trailing ellipsis or not, we must instead look at the type
19499      associated with the FUNCTION_DECL.  This will be a node of type
19500      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19501      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19502      an ellipsis at the end.  */
19503
19504   /* In the case where we are describing a mere function declaration, all we
19505      need to do here (and all we *can* do here) is to describe the *types* of
19506      its formal parameters.  */
19507   if (debug_info_level <= DINFO_LEVEL_TERSE)
19508     ;
19509   else if (declaration)
19510     gen_formal_types_die (decl, subr_die);
19511   else
19512     {
19513       /* Generate DIEs to represent all known formal parameters.  */
19514       tree parm = DECL_ARGUMENTS (decl);
19515       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19516       tree generic_decl_parm = generic_decl
19517                                 ? DECL_ARGUMENTS (generic_decl)
19518                                 : NULL;
19519
19520       /* Now we want to walk the list of parameters of the function and
19521          emit their relevant DIEs.
19522
19523          We consider the case of DECL being an instance of a generic function
19524          as well as it being a normal function.
19525
19526          If DECL is an instance of a generic function we walk the
19527          parameters of the generic function declaration _and_ the parameters of
19528          DECL itself. This is useful because we want to emit specific DIEs for
19529          function parameter packs and those are declared as part of the
19530          generic function declaration. In that particular case,
19531          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19532          That DIE has children DIEs representing the set of arguments
19533          of the pack. Note that the set of pack arguments can be empty.
19534          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19535          children DIE.
19536
19537          Otherwise, we just consider the parameters of DECL.  */
19538       while (generic_decl_parm || parm)
19539         {
19540           if (generic_decl_parm
19541               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19542             gen_formal_parameter_pack_die (generic_decl_parm,
19543                                            parm, subr_die,
19544                                            &parm);
19545           else if (parm)
19546             {
19547               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19548
19549               if (parm == DECL_ARGUMENTS (decl)
19550                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19551                   && parm_die
19552                   && (dwarf_version >= 3 || !dwarf_strict))
19553                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19554
19555               parm = DECL_CHAIN (parm);
19556             }
19557
19558           if (generic_decl_parm)
19559             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19560         }
19561
19562       /* Decide whether we need an unspecified_parameters DIE at the end.
19563          There are 2 more cases to do this for: 1) the ansi ... declaration -
19564          this is detectable when the end of the arg list is not a
19565          void_type_node 2) an unprototyped function declaration (not a
19566          definition).  This just means that we have no info about the
19567          parameters at all.  */
19568       if (prototype_p (TREE_TYPE (decl)))
19569         {
19570           /* This is the prototyped case, check for....  */
19571           if (stdarg_p (TREE_TYPE (decl)))
19572             gen_unspecified_parameters_die (decl, subr_die);
19573         }
19574       else if (DECL_INITIAL (decl) == NULL_TREE)
19575         gen_unspecified_parameters_die (decl, subr_die);
19576     }
19577
19578   /* Output Dwarf info for all of the stuff within the body of the function
19579      (if it has one - it may be just a declaration).  */
19580   outer_scope = DECL_INITIAL (decl);
19581
19582   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19583      a function.  This BLOCK actually represents the outermost binding contour
19584      for the function, i.e. the contour in which the function's formal
19585      parameters and labels get declared. Curiously, it appears that the front
19586      end doesn't actually put the PARM_DECL nodes for the current function onto
19587      the BLOCK_VARS list for this outer scope, but are strung off of the
19588      DECL_ARGUMENTS list for the function instead.
19589
19590      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19591      the LABEL_DECL nodes for the function however, and we output DWARF info
19592      for those in decls_for_scope.  Just within the `outer_scope' there will be
19593      a BLOCK node representing the function's outermost pair of curly braces,
19594      and any blocks used for the base and member initializers of a C++
19595      constructor function.  */
19596   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19597     {
19598       /* Emit a DW_TAG_variable DIE for a named return value.  */
19599       if (DECL_NAME (DECL_RESULT (decl)))
19600         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19601
19602       current_function_has_inlines = 0;
19603       decls_for_scope (outer_scope, subr_die, 0);
19604     }
19605   /* Add the calling convention attribute if requested.  */
19606   add_calling_convention_attribute (subr_die, decl);
19607
19608 }
19609
19610 /* Returns a hash value for X (which really is a die_struct).  */
19611
19612 static hashval_t
19613 common_block_die_table_hash (const void *x)
19614 {
19615   const_dw_die_ref d = (const_dw_die_ref) x;
19616   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19617 }
19618
19619 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19620    as decl_id and die_parent of die_struct Y.  */
19621
19622 static int
19623 common_block_die_table_eq (const void *x, const void *y)
19624 {
19625   const_dw_die_ref d = (const_dw_die_ref) x;
19626   const_dw_die_ref e = (const_dw_die_ref) y;
19627   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19628 }
19629
19630 /* Generate a DIE to represent a declared data object.
19631    Either DECL or ORIGIN must be non-null.  */
19632
19633 static void
19634 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19635 {
19636   HOST_WIDE_INT off;
19637   tree com_decl;
19638   tree decl_or_origin = decl ? decl : origin;
19639   tree ultimate_origin;
19640   dw_die_ref var_die;
19641   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19642   dw_die_ref origin_die;
19643   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19644                       || class_or_namespace_scope_p (context_die));
19645   bool specialization_p = false;
19646
19647   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19648   if (decl || ultimate_origin)
19649     origin = ultimate_origin;
19650   com_decl = fortran_common (decl_or_origin, &off);
19651
19652   /* Symbol in common gets emitted as a child of the common block, in the form
19653      of a data member.  */
19654   if (com_decl)
19655     {
19656       dw_die_ref com_die;
19657       dw_loc_list_ref loc;
19658       die_node com_die_arg;
19659
19660       var_die = lookup_decl_die (decl_or_origin);
19661       if (var_die)
19662         {
19663           if (get_AT (var_die, DW_AT_location) == NULL)
19664             {
19665               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19666               if (loc)
19667                 {
19668                   if (off)
19669                     {
19670                       /* Optimize the common case.  */
19671                       if (single_element_loc_list_p (loc)
19672                           && loc->expr->dw_loc_opc == DW_OP_addr
19673                           && loc->expr->dw_loc_next == NULL
19674                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19675                              == SYMBOL_REF)
19676                         loc->expr->dw_loc_oprnd1.v.val_addr
19677                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19678                         else
19679                           loc_list_plus_const (loc, off);
19680                     }
19681                   add_AT_location_description (var_die, DW_AT_location, loc);
19682                   remove_AT (var_die, DW_AT_declaration);
19683                 }
19684             }
19685           return;
19686         }
19687
19688       if (common_block_die_table == NULL)
19689         common_block_die_table
19690           = htab_create_ggc (10, common_block_die_table_hash,
19691                              common_block_die_table_eq, NULL);
19692
19693       com_die_arg.decl_id = DECL_UID (com_decl);
19694       com_die_arg.die_parent = context_die;
19695       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19696       loc = loc_list_from_tree (com_decl, 2);
19697       if (com_die == NULL)
19698         {
19699           const char *cnam
19700             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19701           void **slot;
19702
19703           com_die = new_die (DW_TAG_common_block, context_die, decl);
19704           add_name_and_src_coords_attributes (com_die, com_decl);
19705           if (loc)
19706             {
19707               add_AT_location_description (com_die, DW_AT_location, loc);
19708               /* Avoid sharing the same loc descriptor between
19709                  DW_TAG_common_block and DW_TAG_variable.  */
19710               loc = loc_list_from_tree (com_decl, 2);
19711             }
19712           else if (DECL_EXTERNAL (decl))
19713             add_AT_flag (com_die, DW_AT_declaration, 1);
19714           add_pubname_string (cnam, com_die); /* ??? needed? */
19715           com_die->decl_id = DECL_UID (com_decl);
19716           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19717           *slot = (void *) com_die;
19718         }
19719       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19720         {
19721           add_AT_location_description (com_die, DW_AT_location, loc);
19722           loc = loc_list_from_tree (com_decl, 2);
19723           remove_AT (com_die, DW_AT_declaration);
19724         }
19725       var_die = new_die (DW_TAG_variable, com_die, decl);
19726       add_name_and_src_coords_attributes (var_die, decl);
19727       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19728                           TREE_THIS_VOLATILE (decl), context_die);
19729       add_AT_flag (var_die, DW_AT_external, 1);
19730       if (loc)
19731         {
19732           if (off)
19733             {
19734               /* Optimize the common case.  */
19735               if (single_element_loc_list_p (loc)
19736                   && loc->expr->dw_loc_opc == DW_OP_addr
19737                   && loc->expr->dw_loc_next == NULL
19738                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19739                 loc->expr->dw_loc_oprnd1.v.val_addr
19740                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19741               else
19742                 loc_list_plus_const (loc, off);
19743             }
19744           add_AT_location_description (var_die, DW_AT_location, loc);
19745         }
19746       else if (DECL_EXTERNAL (decl))
19747         add_AT_flag (var_die, DW_AT_declaration, 1);
19748       equate_decl_number_to_die (decl, var_die);
19749       return;
19750     }
19751
19752   /* If the compiler emitted a definition for the DECL declaration
19753      and if we already emitted a DIE for it, don't emit a second
19754      DIE for it again. Allow re-declarations of DECLs that are
19755      inside functions, though.  */
19756   if (old_die && declaration && !local_scope_p (context_die))
19757     return;
19758
19759   /* For static data members, the declaration in the class is supposed
19760      to have DW_TAG_member tag; the specification should still be
19761      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19762   if (declaration && class_scope_p (context_die))
19763     var_die = new_die (DW_TAG_member, context_die, decl);
19764   else
19765     var_die = new_die (DW_TAG_variable, context_die, decl);
19766
19767   origin_die = NULL;
19768   if (origin != NULL)
19769     origin_die = add_abstract_origin_attribute (var_die, origin);
19770
19771   /* Loop unrolling can create multiple blocks that refer to the same
19772      static variable, so we must test for the DW_AT_declaration flag.
19773
19774      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19775      copy decls and set the DECL_ABSTRACT flag on them instead of
19776      sharing them.
19777
19778      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19779
19780      ??? The declare_in_namespace support causes us to get two DIEs for one
19781      variable, both of which are declarations.  We want to avoid considering
19782      one to be a specification, so we must test that this DIE is not a
19783      declaration.  */
19784   else if (old_die && TREE_STATIC (decl) && ! declaration
19785            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19786     {
19787       /* This is a definition of a C++ class level static.  */
19788       add_AT_specification (var_die, old_die);
19789       specialization_p = true;
19790       if (DECL_NAME (decl))
19791         {
19792           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19793           struct dwarf_file_data * file_index = lookup_filename (s.file);
19794
19795           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19796             add_AT_file (var_die, DW_AT_decl_file, file_index);
19797
19798           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19799             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19800
19801           if (old_die->die_tag == DW_TAG_member)
19802             add_linkage_name (var_die, decl);
19803         }
19804     }
19805   else
19806     add_name_and_src_coords_attributes (var_die, decl);
19807
19808   if ((origin == NULL && !specialization_p)
19809       || (origin != NULL
19810           && !DECL_ABSTRACT (decl_or_origin)
19811           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19812                                        decl_function_context
19813                                                         (decl_or_origin))))
19814     {
19815       tree type = TREE_TYPE (decl_or_origin);
19816
19817       if (decl_by_reference_p (decl_or_origin))
19818         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19819       else
19820         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19821                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19822     }
19823
19824   if (origin == NULL && !specialization_p)
19825     {
19826       if (TREE_PUBLIC (decl))
19827         add_AT_flag (var_die, DW_AT_external, 1);
19828
19829       if (DECL_ARTIFICIAL (decl))
19830         add_AT_flag (var_die, DW_AT_artificial, 1);
19831
19832       add_accessibility_attribute (var_die, decl);
19833     }
19834
19835   if (declaration)
19836     add_AT_flag (var_die, DW_AT_declaration, 1);
19837
19838   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19839     equate_decl_number_to_die (decl, var_die);
19840
19841   if (! declaration
19842       && (! DECL_ABSTRACT (decl_or_origin)
19843           /* Local static vars are shared between all clones/inlines,
19844              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19845              already set.  */
19846           || (TREE_CODE (decl_or_origin) == VAR_DECL
19847               && TREE_STATIC (decl_or_origin)
19848               && DECL_RTL_SET_P (decl_or_origin)))
19849       /* When abstract origin already has DW_AT_location attribute, no need
19850          to add it again.  */
19851       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19852     {
19853       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19854           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19855         defer_location (decl_or_origin, var_die);
19856       else
19857         add_location_or_const_value_attribute (var_die, decl_or_origin,
19858                                                decl == NULL, DW_AT_location);
19859       add_pubname (decl_or_origin, var_die);
19860     }
19861   else
19862     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19863 }
19864
19865 /* Generate a DIE to represent a named constant.  */
19866
19867 static void
19868 gen_const_die (tree decl, dw_die_ref context_die)
19869 {
19870   dw_die_ref const_die;
19871   tree type = TREE_TYPE (decl);
19872
19873   const_die = new_die (DW_TAG_constant, context_die, decl);
19874   add_name_and_src_coords_attributes (const_die, decl);
19875   add_type_attribute (const_die, type, 1, 0, context_die);
19876   if (TREE_PUBLIC (decl))
19877     add_AT_flag (const_die, DW_AT_external, 1);
19878   if (DECL_ARTIFICIAL (decl))
19879     add_AT_flag (const_die, DW_AT_artificial, 1);
19880   tree_add_const_value_attribute_for_decl (const_die, decl);
19881 }
19882
19883 /* Generate a DIE to represent a label identifier.  */
19884
19885 static void
19886 gen_label_die (tree decl, dw_die_ref context_die)
19887 {
19888   tree origin = decl_ultimate_origin (decl);
19889   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19890   rtx insn;
19891   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19892
19893   if (origin != NULL)
19894     add_abstract_origin_attribute (lbl_die, origin);
19895   else
19896     add_name_and_src_coords_attributes (lbl_die, decl);
19897
19898   if (DECL_ABSTRACT (decl))
19899     equate_decl_number_to_die (decl, lbl_die);
19900   else
19901     {
19902       insn = DECL_RTL_IF_SET (decl);
19903
19904       /* Deleted labels are programmer specified labels which have been
19905          eliminated because of various optimizations.  We still emit them
19906          here so that it is possible to put breakpoints on them.  */
19907       if (insn
19908           && (LABEL_P (insn)
19909               || ((NOTE_P (insn)
19910                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19911         {
19912           /* When optimization is enabled (via -O) some parts of the compiler
19913              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19914              represent source-level labels which were explicitly declared by
19915              the user.  This really shouldn't be happening though, so catch
19916              it if it ever does happen.  */
19917           gcc_assert (!INSN_DELETED_P (insn));
19918
19919           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19920           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19921         }
19922     }
19923 }
19924
19925 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19926    attributes to the DIE for a block STMT, to describe where the inlined
19927    function was called from.  This is similar to add_src_coords_attributes.  */
19928
19929 static inline void
19930 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19931 {
19932   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19933
19934   if (dwarf_version >= 3 || !dwarf_strict)
19935     {
19936       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19937       add_AT_unsigned (die, DW_AT_call_line, s.line);
19938     }
19939 }
19940
19941
19942 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19943    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19944
19945 static inline void
19946 add_high_low_attributes (tree stmt, dw_die_ref die)
19947 {
19948   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19949
19950   if (BLOCK_FRAGMENT_CHAIN (stmt)
19951       && (dwarf_version >= 3 || !dwarf_strict))
19952     {
19953       tree chain;
19954
19955       if (inlined_function_outer_scope_p (stmt))
19956         {
19957           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19958                                        BLOCK_NUMBER (stmt));
19959           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19960         }
19961
19962       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19963
19964       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19965       do
19966         {
19967           add_ranges (chain);
19968           chain = BLOCK_FRAGMENT_CHAIN (chain);
19969         }
19970       while (chain);
19971       add_ranges (NULL);
19972     }
19973   else
19974     {
19975       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19976                                    BLOCK_NUMBER (stmt));
19977       add_AT_lbl_id (die, DW_AT_low_pc, label);
19978       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19979                                    BLOCK_NUMBER (stmt));
19980       add_AT_lbl_id (die, DW_AT_high_pc, label);
19981     }
19982 }
19983
19984 /* Generate a DIE for a lexical block.  */
19985
19986 static void
19987 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19988 {
19989   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19990
19991   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19992     add_high_low_attributes (stmt, stmt_die);
19993
19994   decls_for_scope (stmt, stmt_die, depth);
19995 }
19996
19997 /* Generate a DIE for an inlined subprogram.  */
19998
19999 static void
20000 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20001 {
20002   tree decl;
20003
20004   /* The instance of function that is effectively being inlined shall not
20005      be abstract.  */
20006   gcc_assert (! BLOCK_ABSTRACT (stmt));
20007
20008   decl = block_ultimate_origin (stmt);
20009
20010   /* Emit info for the abstract instance first, if we haven't yet.  We
20011      must emit this even if the block is abstract, otherwise when we
20012      emit the block below (or elsewhere), we may end up trying to emit
20013      a die whose origin die hasn't been emitted, and crashing.  */
20014   dwarf2out_abstract_function (decl);
20015
20016   if (! BLOCK_ABSTRACT (stmt))
20017     {
20018       dw_die_ref subr_die
20019         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20020
20021       add_abstract_origin_attribute (subr_die, decl);
20022       if (TREE_ASM_WRITTEN (stmt))
20023         add_high_low_attributes (stmt, subr_die);
20024       add_call_src_coords_attributes (stmt, subr_die);
20025
20026       decls_for_scope (stmt, subr_die, depth);
20027       current_function_has_inlines = 1;
20028     }
20029 }
20030
20031 /* Generate a DIE for a field in a record, or structure.  */
20032
20033 static void
20034 gen_field_die (tree decl, dw_die_ref context_die)
20035 {
20036   dw_die_ref decl_die;
20037
20038   if (TREE_TYPE (decl) == error_mark_node)
20039     return;
20040
20041   decl_die = new_die (DW_TAG_member, context_die, decl);
20042   add_name_and_src_coords_attributes (decl_die, decl);
20043   add_type_attribute (decl_die, member_declared_type (decl),
20044                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20045                       context_die);
20046
20047   if (DECL_BIT_FIELD_TYPE (decl))
20048     {
20049       add_byte_size_attribute (decl_die, decl);
20050       add_bit_size_attribute (decl_die, decl);
20051       add_bit_offset_attribute (decl_die, decl);
20052     }
20053
20054   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20055     add_data_member_location_attribute (decl_die, decl);
20056
20057   if (DECL_ARTIFICIAL (decl))
20058     add_AT_flag (decl_die, DW_AT_artificial, 1);
20059
20060   add_accessibility_attribute (decl_die, decl);
20061
20062   /* Equate decl number to die, so that we can look up this decl later on.  */
20063   equate_decl_number_to_die (decl, decl_die);
20064 }
20065
20066 #if 0
20067 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20068    Use modified_type_die instead.
20069    We keep this code here just in case these types of DIEs may be needed to
20070    represent certain things in other languages (e.g. Pascal) someday.  */
20071
20072 static void
20073 gen_pointer_type_die (tree type, dw_die_ref context_die)
20074 {
20075   dw_die_ref ptr_die
20076     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20077
20078   equate_type_number_to_die (type, ptr_die);
20079   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20080   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20081 }
20082
20083 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20084    Use modified_type_die instead.
20085    We keep this code here just in case these types of DIEs may be needed to
20086    represent certain things in other languages (e.g. Pascal) someday.  */
20087
20088 static void
20089 gen_reference_type_die (tree type, dw_die_ref context_die)
20090 {
20091   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20092
20093   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20094     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20095   else
20096     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20097
20098   equate_type_number_to_die (type, ref_die);
20099   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20100   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20101 }
20102 #endif
20103
20104 /* Generate a DIE for a pointer to a member type.  */
20105
20106 static void
20107 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20108 {
20109   dw_die_ref ptr_die
20110     = new_die (DW_TAG_ptr_to_member_type,
20111                scope_die_for (type, context_die), type);
20112
20113   equate_type_number_to_die (type, ptr_die);
20114   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20115                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20116   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20117 }
20118
20119 /* Generate the DIE for the compilation unit.  */
20120
20121 static dw_die_ref
20122 gen_compile_unit_die (const char *filename)
20123 {
20124   dw_die_ref die;
20125   char producer[250];
20126   const char *language_string = lang_hooks.name;
20127   int language;
20128
20129   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20130
20131   if (filename)
20132     {
20133       add_name_attribute (die, filename);
20134       /* Don't add cwd for <built-in>.  */
20135       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20136         add_comp_dir_attribute (die);
20137     }
20138
20139   sprintf (producer, "%s %s", language_string, version_string);
20140
20141 #ifdef MIPS_DEBUGGING_INFO
20142   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20143      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20144      not appear in the producer string, the debugger reaches the conclusion
20145      that the object file is stripped and has no debugging information.
20146      To get the MIPS/SGI debugger to believe that there is debugging
20147      information in the object file, we add a -g to the producer string.  */
20148   if (debug_info_level > DINFO_LEVEL_TERSE)
20149     strcat (producer, " -g");
20150 #endif
20151
20152   add_AT_string (die, DW_AT_producer, producer);
20153
20154   /* If our producer is LTO try to figure out a common language to use
20155      from the global list of translation units.  */
20156   if (strcmp (language_string, "GNU GIMPLE") == 0)
20157     {
20158       unsigned i;
20159       tree t;
20160       const char *common_lang = NULL;
20161
20162       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20163         {
20164           if (!TRANSLATION_UNIT_LANGUAGE (t))
20165             continue;
20166           if (!common_lang)
20167             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20168           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20169             ;
20170           else if (strncmp (common_lang, "GNU C", 5) == 0
20171                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20172             /* Mixing C and C++ is ok, use C++ in that case.  */
20173             common_lang = "GNU C++";
20174           else
20175             {
20176               /* Fall back to C.  */
20177               common_lang = NULL;
20178               break;
20179             }
20180         }
20181
20182       if (common_lang)
20183         language_string = common_lang;
20184     }
20185
20186   language = DW_LANG_C89;
20187   if (strcmp (language_string, "GNU C++") == 0)
20188     language = DW_LANG_C_plus_plus;
20189   else if (strcmp (language_string, "GNU F77") == 0)
20190     language = DW_LANG_Fortran77;
20191   else if (strcmp (language_string, "GNU Pascal") == 0)
20192     language = DW_LANG_Pascal83;
20193   else if (dwarf_version >= 3 || !dwarf_strict)
20194     {
20195       if (strcmp (language_string, "GNU Ada") == 0)
20196         language = DW_LANG_Ada95;
20197       else if (strcmp (language_string, "GNU Fortran") == 0)
20198         language = DW_LANG_Fortran95;
20199       else if (strcmp (language_string, "GNU Java") == 0)
20200         language = DW_LANG_Java;
20201       else if (strcmp (language_string, "GNU Objective-C") == 0)
20202         language = DW_LANG_ObjC;
20203       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20204         language = DW_LANG_ObjC_plus_plus;
20205     }
20206
20207   add_AT_unsigned (die, DW_AT_language, language);
20208
20209   switch (language)
20210     {
20211     case DW_LANG_Fortran77:
20212     case DW_LANG_Fortran90:
20213     case DW_LANG_Fortran95:
20214       /* Fortran has case insensitive identifiers and the front-end
20215          lowercases everything.  */
20216       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20217       break;
20218     default:
20219       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20220       break;
20221     }
20222   return die;
20223 }
20224
20225 /* Generate the DIE for a base class.  */
20226
20227 static void
20228 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20229 {
20230   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20231
20232   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20233   add_data_member_location_attribute (die, binfo);
20234
20235   if (BINFO_VIRTUAL_P (binfo))
20236     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20237
20238   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20239      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20240      the default has always been DW_ACCESS_private.  */
20241   if (access == access_public_node)
20242     {
20243       if (dwarf_version == 2
20244           || context_die->die_tag == DW_TAG_class_type)
20245       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20246     }
20247   else if (access == access_protected_node)
20248     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20249   else if (dwarf_version > 2
20250            && context_die->die_tag != DW_TAG_class_type)
20251     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20252 }
20253
20254 /* Generate a DIE for a class member.  */
20255
20256 static void
20257 gen_member_die (tree type, dw_die_ref context_die)
20258 {
20259   tree member;
20260   tree binfo = TYPE_BINFO (type);
20261   dw_die_ref child;
20262
20263   /* If this is not an incomplete type, output descriptions of each of its
20264      members. Note that as we output the DIEs necessary to represent the
20265      members of this record or union type, we will also be trying to output
20266      DIEs to represent the *types* of those members. However the `type'
20267      function (above) will specifically avoid generating type DIEs for member
20268      types *within* the list of member DIEs for this (containing) type except
20269      for those types (of members) which are explicitly marked as also being
20270      members of this (containing) type themselves.  The g++ front- end can
20271      force any given type to be treated as a member of some other (containing)
20272      type by setting the TYPE_CONTEXT of the given (member) type to point to
20273      the TREE node representing the appropriate (containing) type.  */
20274
20275   /* First output info about the base classes.  */
20276   if (binfo)
20277     {
20278       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20279       int i;
20280       tree base;
20281
20282       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20283         gen_inheritance_die (base,
20284                              (accesses ? VEC_index (tree, accesses, i)
20285                               : access_public_node), context_die);
20286     }
20287
20288   /* Now output info about the data members and type members.  */
20289   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20290     {
20291       /* If we thought we were generating minimal debug info for TYPE
20292          and then changed our minds, some of the member declarations
20293          may have already been defined.  Don't define them again, but
20294          do put them in the right order.  */
20295
20296       child = lookup_decl_die (member);
20297       if (child)
20298         splice_child_die (context_die, child);
20299       else
20300         gen_decl_die (member, NULL, context_die);
20301     }
20302
20303   /* Now output info about the function members (if any).  */
20304   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20305     {
20306       /* Don't include clones in the member list.  */
20307       if (DECL_ABSTRACT_ORIGIN (member))
20308         continue;
20309
20310       child = lookup_decl_die (member);
20311       if (child)
20312         splice_child_die (context_die, child);
20313       else
20314         gen_decl_die (member, NULL, context_die);
20315     }
20316 }
20317
20318 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20319    is set, we pretend that the type was never defined, so we only get the
20320    member DIEs needed by later specification DIEs.  */
20321
20322 static void
20323 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20324                                 enum debug_info_usage usage)
20325 {
20326   dw_die_ref type_die = lookup_type_die (type);
20327   dw_die_ref scope_die = 0;
20328   int nested = 0;
20329   int complete = (TYPE_SIZE (type)
20330                   && (! TYPE_STUB_DECL (type)
20331                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20332   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20333   complete = complete && should_emit_struct_debug (type, usage);
20334
20335   if (type_die && ! complete)
20336     return;
20337
20338   if (TYPE_CONTEXT (type) != NULL_TREE
20339       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20340           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20341     nested = 1;
20342
20343   scope_die = scope_die_for (type, context_die);
20344
20345   if (! type_die || (nested && is_cu_die (scope_die)))
20346     /* First occurrence of type or toplevel definition of nested class.  */
20347     {
20348       dw_die_ref old_die = type_die;
20349
20350       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20351                           ? record_type_tag (type) : DW_TAG_union_type,
20352                           scope_die, type);
20353       equate_type_number_to_die (type, type_die);
20354       if (old_die)
20355         add_AT_specification (type_die, old_die);
20356       else
20357         add_name_attribute (type_die, type_tag (type));
20358     }
20359   else
20360     remove_AT (type_die, DW_AT_declaration);
20361
20362   /* Generate child dies for template paramaters.  */
20363   if (debug_info_level > DINFO_LEVEL_TERSE
20364       && COMPLETE_TYPE_P (type))
20365     schedule_generic_params_dies_gen (type);
20366
20367   /* If this type has been completed, then give it a byte_size attribute and
20368      then give a list of members.  */
20369   if (complete && !ns_decl)
20370     {
20371       /* Prevent infinite recursion in cases where the type of some member of
20372          this type is expressed in terms of this type itself.  */
20373       TREE_ASM_WRITTEN (type) = 1;
20374       add_byte_size_attribute (type_die, type);
20375       if (TYPE_STUB_DECL (type) != NULL_TREE)
20376         {
20377           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20378           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20379         }
20380
20381       /* If the first reference to this type was as the return type of an
20382          inline function, then it may not have a parent.  Fix this now.  */
20383       if (type_die->die_parent == NULL)
20384         add_child_die (scope_die, type_die);
20385
20386       push_decl_scope (type);
20387       gen_member_die (type, type_die);
20388       pop_decl_scope ();
20389
20390       /* GNU extension: Record what type our vtable lives in.  */
20391       if (TYPE_VFIELD (type))
20392         {
20393           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20394
20395           gen_type_die (vtype, context_die);
20396           add_AT_die_ref (type_die, DW_AT_containing_type,
20397                           lookup_type_die (vtype));
20398         }
20399     }
20400   else
20401     {
20402       add_AT_flag (type_die, DW_AT_declaration, 1);
20403
20404       /* We don't need to do this for function-local types.  */
20405       if (TYPE_STUB_DECL (type)
20406           && ! decl_function_context (TYPE_STUB_DECL (type)))
20407         VEC_safe_push (tree, gc, incomplete_types, type);
20408     }
20409
20410   if (get_AT (type_die, DW_AT_name))
20411     add_pubtype (type, type_die);
20412 }
20413
20414 /* Generate a DIE for a subroutine _type_.  */
20415
20416 static void
20417 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20418 {
20419   tree return_type = TREE_TYPE (type);
20420   dw_die_ref subr_die
20421     = new_die (DW_TAG_subroutine_type,
20422                scope_die_for (type, context_die), type);
20423
20424   equate_type_number_to_die (type, subr_die);
20425   add_prototyped_attribute (subr_die, type);
20426   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20427   gen_formal_types_die (type, subr_die);
20428
20429   if (get_AT (subr_die, DW_AT_name))
20430     add_pubtype (type, subr_die);
20431 }
20432
20433 /* Generate a DIE for a type definition.  */
20434
20435 static void
20436 gen_typedef_die (tree decl, dw_die_ref context_die)
20437 {
20438   dw_die_ref type_die;
20439   tree origin;
20440
20441   if (TREE_ASM_WRITTEN (decl))
20442     return;
20443
20444   TREE_ASM_WRITTEN (decl) = 1;
20445   type_die = new_die (DW_TAG_typedef, context_die, decl);
20446   origin = decl_ultimate_origin (decl);
20447   if (origin != NULL)
20448     add_abstract_origin_attribute (type_die, origin);
20449   else
20450     {
20451       tree type;
20452
20453       add_name_and_src_coords_attributes (type_die, decl);
20454       if (DECL_ORIGINAL_TYPE (decl))
20455         {
20456           type = DECL_ORIGINAL_TYPE (decl);
20457
20458           gcc_assert (type != TREE_TYPE (decl));
20459           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20460         }
20461       else
20462         {
20463           type = TREE_TYPE (decl);
20464
20465           if (is_naming_typedef_decl (TYPE_NAME (type)))
20466             {
20467               /* Here, we are in the case of decl being a typedef naming
20468                  an anonymous type, e.g:
20469                      typedef struct {...} foo;
20470                  In that case TREE_TYPE (decl) is not a typedef variant
20471                  type and TYPE_NAME of the anonymous type is set to the
20472                  TYPE_DECL of the typedef. This construct is emitted by
20473                  the C++ FE.
20474
20475                  TYPE is the anonymous struct named by the typedef
20476                  DECL. As we need the DW_AT_type attribute of the
20477                  DW_TAG_typedef to point to the DIE of TYPE, let's
20478                  generate that DIE right away. add_type_attribute
20479                  called below will then pick (via lookup_type_die) that
20480                  anonymous struct DIE.  */
20481               if (!TREE_ASM_WRITTEN (type))
20482                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20483
20484               /* This is a GNU Extension.  We are adding a
20485                  DW_AT_linkage_name attribute to the DIE of the
20486                  anonymous struct TYPE.  The value of that attribute
20487                  is the name of the typedef decl naming the anonymous
20488                  struct.  This greatly eases the work of consumers of
20489                  this debug info.  */
20490               add_linkage_attr (lookup_type_die (type), decl);
20491             }
20492         }
20493
20494       add_type_attribute (type_die, type, TREE_READONLY (decl),
20495                           TREE_THIS_VOLATILE (decl), context_die);
20496
20497       if (is_naming_typedef_decl (decl))
20498         /* We want that all subsequent calls to lookup_type_die with
20499            TYPE in argument yield the DW_TAG_typedef we have just
20500            created.  */
20501         equate_type_number_to_die (type, type_die);
20502
20503       add_accessibility_attribute (type_die, decl);
20504     }
20505
20506   if (DECL_ABSTRACT (decl))
20507     equate_decl_number_to_die (decl, type_die);
20508
20509   if (get_AT (type_die, DW_AT_name))
20510     add_pubtype (decl, type_die);
20511 }
20512
20513 /* Generate a DIE for a struct, class, enum or union type.  */
20514
20515 static void
20516 gen_tagged_type_die (tree type,
20517                      dw_die_ref context_die,
20518                      enum debug_info_usage usage)
20519 {
20520   int need_pop;
20521
20522   if (type == NULL_TREE
20523       || !is_tagged_type (type))
20524     return;
20525
20526   /* If this is a nested type whose containing class hasn't been written
20527      out yet, writing it out will cover this one, too.  This does not apply
20528      to instantiations of member class templates; they need to be added to
20529      the containing class as they are generated.  FIXME: This hurts the
20530      idea of combining type decls from multiple TUs, since we can't predict
20531      what set of template instantiations we'll get.  */
20532   if (TYPE_CONTEXT (type)
20533       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20534       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20535     {
20536       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20537
20538       if (TREE_ASM_WRITTEN (type))
20539         return;
20540
20541       /* If that failed, attach ourselves to the stub.  */
20542       push_decl_scope (TYPE_CONTEXT (type));
20543       context_die = lookup_type_die (TYPE_CONTEXT (type));
20544       need_pop = 1;
20545     }
20546   else if (TYPE_CONTEXT (type) != NULL_TREE
20547            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20548     {
20549       /* If this type is local to a function that hasn't been written
20550          out yet, use a NULL context for now; it will be fixed up in
20551          decls_for_scope.  */
20552       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20553       /* A declaration DIE doesn't count; nested types need to go in the
20554          specification.  */
20555       if (context_die && is_declaration_die (context_die))
20556         context_die = NULL;
20557       need_pop = 0;
20558     }
20559   else
20560     {
20561       context_die = declare_in_namespace (type, context_die);
20562       need_pop = 0;
20563     }
20564
20565   if (TREE_CODE (type) == ENUMERAL_TYPE)
20566     {
20567       /* This might have been written out by the call to
20568          declare_in_namespace.  */
20569       if (!TREE_ASM_WRITTEN (type))
20570         gen_enumeration_type_die (type, context_die);
20571     }
20572   else
20573     gen_struct_or_union_type_die (type, context_die, usage);
20574
20575   if (need_pop)
20576     pop_decl_scope ();
20577
20578   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20579      it up if it is ever completed.  gen_*_type_die will set it for us
20580      when appropriate.  */
20581 }
20582
20583 /* Generate a type description DIE.  */
20584
20585 static void
20586 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20587                          enum debug_info_usage usage)
20588 {
20589   struct array_descr_info info;
20590
20591   if (type == NULL_TREE || type == error_mark_node)
20592     return;
20593
20594   if (TYPE_NAME (type) != NULL_TREE
20595       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20596       && is_redundant_typedef (TYPE_NAME (type))
20597       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20598     /* The DECL of this type is a typedef we don't want to emit debug
20599        info for but we want debug info for its underlying typedef.
20600        This can happen for e.g, the injected-class-name of a C++
20601        type.  */
20602     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20603
20604   /* If TYPE is a typedef type variant, let's generate debug info
20605      for the parent typedef which TYPE is a type of.  */
20606   if (typedef_variant_p (type))
20607     {
20608       if (TREE_ASM_WRITTEN (type))
20609         return;
20610
20611       /* Prevent broken recursion; we can't hand off to the same type.  */
20612       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20613
20614       /* Use the DIE of the containing namespace as the parent DIE of
20615          the type description DIE we want to generate.  */
20616       if (DECL_CONTEXT (TYPE_NAME (type))
20617           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20618         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20619
20620       TREE_ASM_WRITTEN (type) = 1;
20621
20622       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20623       return;
20624     }
20625
20626   /* If type is an anonymous tagged type named by a typedef, let's
20627      generate debug info for the typedef.  */
20628   if (is_naming_typedef_decl (TYPE_NAME (type)))
20629     {
20630       /* Use the DIE of the containing namespace as the parent DIE of
20631          the type description DIE we want to generate.  */
20632       if (DECL_CONTEXT (TYPE_NAME (type))
20633           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20634         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20635       
20636       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20637       return;
20638     }
20639
20640   /* If this is an array type with hidden descriptor, handle it first.  */
20641   if (!TREE_ASM_WRITTEN (type)
20642       && lang_hooks.types.get_array_descr_info
20643       && lang_hooks.types.get_array_descr_info (type, &info)
20644       && (dwarf_version >= 3 || !dwarf_strict))
20645     {
20646       gen_descr_array_type_die (type, &info, context_die);
20647       TREE_ASM_WRITTEN (type) = 1;
20648       return;
20649     }
20650
20651   /* We are going to output a DIE to represent the unqualified version
20652      of this type (i.e. without any const or volatile qualifiers) so
20653      get the main variant (i.e. the unqualified version) of this type
20654      now.  (Vectors are special because the debugging info is in the
20655      cloned type itself).  */
20656   if (TREE_CODE (type) != VECTOR_TYPE)
20657     type = type_main_variant (type);
20658
20659   if (TREE_ASM_WRITTEN (type))
20660     return;
20661
20662   switch (TREE_CODE (type))
20663     {
20664     case ERROR_MARK:
20665       break;
20666
20667     case POINTER_TYPE:
20668     case REFERENCE_TYPE:
20669       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20670          ensures that the gen_type_die recursion will terminate even if the
20671          type is recursive.  Recursive types are possible in Ada.  */
20672       /* ??? We could perhaps do this for all types before the switch
20673          statement.  */
20674       TREE_ASM_WRITTEN (type) = 1;
20675
20676       /* For these types, all that is required is that we output a DIE (or a
20677          set of DIEs) to represent the "basis" type.  */
20678       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20679                                 DINFO_USAGE_IND_USE);
20680       break;
20681
20682     case OFFSET_TYPE:
20683       /* This code is used for C++ pointer-to-data-member types.
20684          Output a description of the relevant class type.  */
20685       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20686                                         DINFO_USAGE_IND_USE);
20687
20688       /* Output a description of the type of the object pointed to.  */
20689       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20690                                         DINFO_USAGE_IND_USE);
20691
20692       /* Now output a DIE to represent this pointer-to-data-member type
20693          itself.  */
20694       gen_ptr_to_mbr_type_die (type, context_die);
20695       break;
20696
20697     case FUNCTION_TYPE:
20698       /* Force out return type (in case it wasn't forced out already).  */
20699       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20700                                         DINFO_USAGE_DIR_USE);
20701       gen_subroutine_type_die (type, context_die);
20702       break;
20703
20704     case METHOD_TYPE:
20705       /* Force out return type (in case it wasn't forced out already).  */
20706       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20707                                         DINFO_USAGE_DIR_USE);
20708       gen_subroutine_type_die (type, context_die);
20709       break;
20710
20711     case ARRAY_TYPE:
20712       gen_array_type_die (type, context_die);
20713       break;
20714
20715     case VECTOR_TYPE:
20716       gen_array_type_die (type, context_die);
20717       break;
20718
20719     case ENUMERAL_TYPE:
20720     case RECORD_TYPE:
20721     case UNION_TYPE:
20722     case QUAL_UNION_TYPE:
20723       gen_tagged_type_die (type, context_die, usage);
20724       return;
20725
20726     case VOID_TYPE:
20727     case INTEGER_TYPE:
20728     case REAL_TYPE:
20729     case FIXED_POINT_TYPE:
20730     case COMPLEX_TYPE:
20731     case BOOLEAN_TYPE:
20732       /* No DIEs needed for fundamental types.  */
20733       break;
20734
20735     case NULLPTR_TYPE:
20736     case LANG_TYPE:
20737       /* Just use DW_TAG_unspecified_type.  */
20738       {
20739         dw_die_ref type_die = lookup_type_die (type);
20740         if (type_die == NULL)
20741           {
20742             tree name = TYPE_NAME (type);
20743             if (TREE_CODE (name) == TYPE_DECL)
20744               name = DECL_NAME (name);
20745             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20746             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20747             equate_type_number_to_die (type, type_die);
20748           }
20749       }
20750       break;
20751
20752     default:
20753       gcc_unreachable ();
20754     }
20755
20756   TREE_ASM_WRITTEN (type) = 1;
20757 }
20758
20759 static void
20760 gen_type_die (tree type, dw_die_ref context_die)
20761 {
20762   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20763 }
20764
20765 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20766    things which are local to the given block.  */
20767
20768 static void
20769 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20770 {
20771   int must_output_die = 0;
20772   bool inlined_func;
20773
20774   /* Ignore blocks that are NULL.  */
20775   if (stmt == NULL_TREE)
20776     return;
20777
20778   inlined_func = inlined_function_outer_scope_p (stmt);
20779
20780   /* If the block is one fragment of a non-contiguous block, do not
20781      process the variables, since they will have been done by the
20782      origin block.  Do process subblocks.  */
20783   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20784     {
20785       tree sub;
20786
20787       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20788         gen_block_die (sub, context_die, depth + 1);
20789
20790       return;
20791     }
20792
20793   /* Determine if we need to output any Dwarf DIEs at all to represent this
20794      block.  */
20795   if (inlined_func)
20796     /* The outer scopes for inlinings *must* always be represented.  We
20797        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20798     must_output_die = 1;
20799   else
20800     {
20801       /* Determine if this block directly contains any "significant"
20802          local declarations which we will need to output DIEs for.  */
20803       if (debug_info_level > DINFO_LEVEL_TERSE)
20804         /* We are not in terse mode so *any* local declaration counts
20805            as being a "significant" one.  */
20806         must_output_die = ((BLOCK_VARS (stmt) != NULL
20807                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20808                            && (TREE_USED (stmt)
20809                                || TREE_ASM_WRITTEN (stmt)
20810                                || BLOCK_ABSTRACT (stmt)));
20811       else if ((TREE_USED (stmt)
20812                 || TREE_ASM_WRITTEN (stmt)
20813                 || BLOCK_ABSTRACT (stmt))
20814                && !dwarf2out_ignore_block (stmt))
20815         must_output_die = 1;
20816     }
20817
20818   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20819      DIE for any block which contains no significant local declarations at
20820      all.  Rather, in such cases we just call `decls_for_scope' so that any
20821      needed Dwarf info for any sub-blocks will get properly generated. Note
20822      that in terse mode, our definition of what constitutes a "significant"
20823      local declaration gets restricted to include only inlined function
20824      instances and local (nested) function definitions.  */
20825   if (must_output_die)
20826     {
20827       if (inlined_func)
20828         {
20829           /* If STMT block is abstract, that means we have been called
20830              indirectly from dwarf2out_abstract_function.
20831              That function rightfully marks the descendent blocks (of
20832              the abstract function it is dealing with) as being abstract,
20833              precisely to prevent us from emitting any
20834              DW_TAG_inlined_subroutine DIE as a descendent
20835              of an abstract function instance. So in that case, we should
20836              not call gen_inlined_subroutine_die.
20837
20838              Later though, when cgraph asks dwarf2out to emit info
20839              for the concrete instance of the function decl into which
20840              the concrete instance of STMT got inlined, the later will lead
20841              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20842           if (! BLOCK_ABSTRACT (stmt))
20843             gen_inlined_subroutine_die (stmt, context_die, depth);
20844         }
20845       else
20846         gen_lexical_block_die (stmt, context_die, depth);
20847     }
20848   else
20849     decls_for_scope (stmt, context_die, depth);
20850 }
20851
20852 /* Process variable DECL (or variable with origin ORIGIN) within
20853    block STMT and add it to CONTEXT_DIE.  */
20854 static void
20855 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20856 {
20857   dw_die_ref die;
20858   tree decl_or_origin = decl ? decl : origin;
20859
20860   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20861     die = lookup_decl_die (decl_or_origin);
20862   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20863            && TYPE_DECL_IS_STUB (decl_or_origin))
20864     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20865   else
20866     die = NULL;
20867
20868   if (die != NULL && die->die_parent == NULL)
20869     add_child_die (context_die, die);
20870   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20871     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20872                                          stmt, context_die);
20873   else
20874     gen_decl_die (decl, origin, context_die);
20875 }
20876
20877 /* Generate all of the decls declared within a given scope and (recursively)
20878    all of its sub-blocks.  */
20879
20880 static void
20881 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20882 {
20883   tree decl;
20884   unsigned int i;
20885   tree subblocks;
20886
20887   /* Ignore NULL blocks.  */
20888   if (stmt == NULL_TREE)
20889     return;
20890
20891   /* Output the DIEs to represent all of the data objects and typedefs
20892      declared directly within this block but not within any nested
20893      sub-blocks.  Also, nested function and tag DIEs have been
20894      generated with a parent of NULL; fix that up now.  */
20895   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20896     process_scope_var (stmt, decl, NULL_TREE, context_die);
20897   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20898     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20899                        context_die);
20900
20901   /* If we're at -g1, we're not interested in subblocks.  */
20902   if (debug_info_level <= DINFO_LEVEL_TERSE)
20903     return;
20904
20905   /* Output the DIEs to represent all sub-blocks (and the items declared
20906      therein) of this block.  */
20907   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20908        subblocks != NULL;
20909        subblocks = BLOCK_CHAIN (subblocks))
20910     gen_block_die (subblocks, context_die, depth + 1);
20911 }
20912
20913 /* Is this a typedef we can avoid emitting?  */
20914
20915 static inline int
20916 is_redundant_typedef (const_tree decl)
20917 {
20918   if (TYPE_DECL_IS_STUB (decl))
20919     return 1;
20920
20921   if (DECL_ARTIFICIAL (decl)
20922       && DECL_CONTEXT (decl)
20923       && is_tagged_type (DECL_CONTEXT (decl))
20924       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20925       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20926     /* Also ignore the artificial member typedef for the class name.  */
20927     return 1;
20928
20929   return 0;
20930 }
20931
20932 /* Return TRUE if TYPE is a typedef that names a type for linkage
20933    purposes. This kind of typedefs is produced by the C++ FE for
20934    constructs like:
20935
20936    typedef struct {...} foo;
20937
20938    In that case, there is no typedef variant type produced for foo.
20939    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20940    struct type.  */
20941
20942 static bool
20943 is_naming_typedef_decl (const_tree decl)
20944 {
20945   if (decl == NULL_TREE
20946       || TREE_CODE (decl) != TYPE_DECL
20947       || !is_tagged_type (TREE_TYPE (decl))
20948       || DECL_IS_BUILTIN (decl)
20949       || is_redundant_typedef (decl)
20950       /* It looks like Ada produces TYPE_DECLs that are very similar
20951          to C++ naming typedefs but that have different
20952          semantics. Let's be specific to c++ for now.  */
20953       || !is_cxx ())
20954     return FALSE;
20955
20956   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20957           && TYPE_NAME (TREE_TYPE (decl)) == decl
20958           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20959               != TYPE_NAME (TREE_TYPE (decl))));
20960 }
20961
20962 /* Returns the DIE for a context.  */
20963
20964 static inline dw_die_ref
20965 get_context_die (tree context)
20966 {
20967   if (context)
20968     {
20969       /* Find die that represents this context.  */
20970       if (TYPE_P (context))
20971         {
20972           context = TYPE_MAIN_VARIANT (context);
20973           return strip_naming_typedef (context, force_type_die (context));
20974         }
20975       else
20976         return force_decl_die (context);
20977     }
20978   return comp_unit_die ();
20979 }
20980
20981 /* Returns the DIE for decl.  A DIE will always be returned.  */
20982
20983 static dw_die_ref
20984 force_decl_die (tree decl)
20985 {
20986   dw_die_ref decl_die;
20987   unsigned saved_external_flag;
20988   tree save_fn = NULL_TREE;
20989   decl_die = lookup_decl_die (decl);
20990   if (!decl_die)
20991     {
20992       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20993
20994       decl_die = lookup_decl_die (decl);
20995       if (decl_die)
20996         return decl_die;
20997
20998       switch (TREE_CODE (decl))
20999         {
21000         case FUNCTION_DECL:
21001           /* Clear current_function_decl, so that gen_subprogram_die thinks
21002              that this is a declaration. At this point, we just want to force
21003              declaration die.  */
21004           save_fn = current_function_decl;
21005           current_function_decl = NULL_TREE;
21006           gen_subprogram_die (decl, context_die);
21007           current_function_decl = save_fn;
21008           break;
21009
21010         case VAR_DECL:
21011           /* Set external flag to force declaration die. Restore it after
21012            gen_decl_die() call.  */
21013           saved_external_flag = DECL_EXTERNAL (decl);
21014           DECL_EXTERNAL (decl) = 1;
21015           gen_decl_die (decl, NULL, context_die);
21016           DECL_EXTERNAL (decl) = saved_external_flag;
21017           break;
21018
21019         case NAMESPACE_DECL:
21020           if (dwarf_version >= 3 || !dwarf_strict)
21021             dwarf2out_decl (decl);
21022           else
21023             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21024             decl_die = comp_unit_die ();
21025           break;
21026
21027         case TRANSLATION_UNIT_DECL:
21028           decl_die = comp_unit_die ();
21029           break;
21030
21031         default:
21032           gcc_unreachable ();
21033         }
21034
21035       /* We should be able to find the DIE now.  */
21036       if (!decl_die)
21037         decl_die = lookup_decl_die (decl);
21038       gcc_assert (decl_die);
21039     }
21040
21041   return decl_die;
21042 }
21043
21044 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21045    always returned.  */
21046
21047 static dw_die_ref
21048 force_type_die (tree type)
21049 {
21050   dw_die_ref type_die;
21051
21052   type_die = lookup_type_die (type);
21053   if (!type_die)
21054     {
21055       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21056
21057       type_die = modified_type_die (type, TYPE_READONLY (type),
21058                                     TYPE_VOLATILE (type), context_die);
21059       gcc_assert (type_die);
21060     }
21061   return type_die;
21062 }
21063
21064 /* Force out any required namespaces to be able to output DECL,
21065    and return the new context_die for it, if it's changed.  */
21066
21067 static dw_die_ref
21068 setup_namespace_context (tree thing, dw_die_ref context_die)
21069 {
21070   tree context = (DECL_P (thing)
21071                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21072   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21073     /* Force out the namespace.  */
21074     context_die = force_decl_die (context);
21075
21076   return context_die;
21077 }
21078
21079 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21080    type) within its namespace, if appropriate.
21081
21082    For compatibility with older debuggers, namespace DIEs only contain
21083    declarations; all definitions are emitted at CU scope.  */
21084
21085 static dw_die_ref
21086 declare_in_namespace (tree thing, dw_die_ref context_die)
21087 {
21088   dw_die_ref ns_context;
21089
21090   if (debug_info_level <= DINFO_LEVEL_TERSE)
21091     return context_die;
21092
21093   /* If this decl is from an inlined function, then don't try to emit it in its
21094      namespace, as we will get confused.  It would have already been emitted
21095      when the abstract instance of the inline function was emitted anyways.  */
21096   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21097     return context_die;
21098
21099   ns_context = setup_namespace_context (thing, context_die);
21100
21101   if (ns_context != context_die)
21102     {
21103       if (is_fortran ())
21104         return ns_context;
21105       if (DECL_P (thing))
21106         gen_decl_die (thing, NULL, ns_context);
21107       else
21108         gen_type_die (thing, ns_context);
21109     }
21110   return context_die;
21111 }
21112
21113 /* Generate a DIE for a namespace or namespace alias.  */
21114
21115 static void
21116 gen_namespace_die (tree decl, dw_die_ref context_die)
21117 {
21118   dw_die_ref namespace_die;
21119
21120   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21121      they are an alias of.  */
21122   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21123     {
21124       /* Output a real namespace or module.  */
21125       context_die = setup_namespace_context (decl, comp_unit_die ());
21126       namespace_die = new_die (is_fortran ()
21127                                ? DW_TAG_module : DW_TAG_namespace,
21128                                context_die, decl);
21129       /* For Fortran modules defined in different CU don't add src coords.  */
21130       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21131         {
21132           const char *name = dwarf2_name (decl, 0);
21133           if (name)
21134             add_name_attribute (namespace_die, name);
21135         }
21136       else
21137         add_name_and_src_coords_attributes (namespace_die, decl);
21138       if (DECL_EXTERNAL (decl))
21139         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21140       equate_decl_number_to_die (decl, namespace_die);
21141     }
21142   else
21143     {
21144       /* Output a namespace alias.  */
21145
21146       /* Force out the namespace we are an alias of, if necessary.  */
21147       dw_die_ref origin_die
21148         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21149
21150       if (DECL_FILE_SCOPE_P (decl)
21151           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21152         context_die = setup_namespace_context (decl, comp_unit_die ());
21153       /* Now create the namespace alias DIE.  */
21154       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21155       add_name_and_src_coords_attributes (namespace_die, decl);
21156       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21157       equate_decl_number_to_die (decl, namespace_die);
21158     }
21159 }
21160
21161 /* Generate Dwarf debug information for a decl described by DECL.
21162    The return value is currently only meaningful for PARM_DECLs,
21163    for all other decls it returns NULL.  */
21164
21165 static dw_die_ref
21166 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21167 {
21168   tree decl_or_origin = decl ? decl : origin;
21169   tree class_origin = NULL, ultimate_origin;
21170
21171   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21172     return NULL;
21173
21174   switch (TREE_CODE (decl_or_origin))
21175     {
21176     case ERROR_MARK:
21177       break;
21178
21179     case CONST_DECL:
21180       if (!is_fortran () && !is_ada ())
21181         {
21182           /* The individual enumerators of an enum type get output when we output
21183              the Dwarf representation of the relevant enum type itself.  */
21184           break;
21185         }
21186
21187       /* Emit its type.  */
21188       gen_type_die (TREE_TYPE (decl), context_die);
21189
21190       /* And its containing namespace.  */
21191       context_die = declare_in_namespace (decl, context_die);
21192
21193       gen_const_die (decl, context_die);
21194       break;
21195
21196     case FUNCTION_DECL:
21197       /* Don't output any DIEs to represent mere function declarations,
21198          unless they are class members or explicit block externs.  */
21199       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21200           && DECL_FILE_SCOPE_P (decl_or_origin)
21201           && (current_function_decl == NULL_TREE
21202               || DECL_ARTIFICIAL (decl_or_origin)))
21203         break;
21204
21205 #if 0
21206       /* FIXME */
21207       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21208          on local redeclarations of global functions.  That seems broken.  */
21209       if (current_function_decl != decl)
21210         /* This is only a declaration.  */;
21211 #endif
21212
21213       /* If we're emitting a clone, emit info for the abstract instance.  */
21214       if (origin || DECL_ORIGIN (decl) != decl)
21215         dwarf2out_abstract_function (origin
21216                                      ? DECL_ORIGIN (origin)
21217                                      : DECL_ABSTRACT_ORIGIN (decl));
21218
21219       /* If we're emitting an out-of-line copy of an inline function,
21220          emit info for the abstract instance and set up to refer to it.  */
21221       else if (cgraph_function_possibly_inlined_p (decl)
21222                && ! DECL_ABSTRACT (decl)
21223                && ! class_or_namespace_scope_p (context_die)
21224                /* dwarf2out_abstract_function won't emit a die if this is just
21225                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21226                   that case, because that works only if we have a die.  */
21227                && DECL_INITIAL (decl) != NULL_TREE)
21228         {
21229           dwarf2out_abstract_function (decl);
21230           set_decl_origin_self (decl);
21231         }
21232
21233       /* Otherwise we're emitting the primary DIE for this decl.  */
21234       else if (debug_info_level > DINFO_LEVEL_TERSE)
21235         {
21236           /* Before we describe the FUNCTION_DECL itself, make sure that we
21237              have its containing type.  */
21238           if (!origin)
21239             origin = decl_class_context (decl);
21240           if (origin != NULL_TREE)
21241             gen_type_die (origin, context_die);
21242
21243           /* And its return type.  */
21244           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21245
21246           /* And its virtual context.  */
21247           if (DECL_VINDEX (decl) != NULL_TREE)
21248             gen_type_die (DECL_CONTEXT (decl), context_die);
21249
21250           /* Make sure we have a member DIE for decl.  */
21251           if (origin != NULL_TREE)
21252             gen_type_die_for_member (origin, decl, context_die);
21253
21254           /* And its containing namespace.  */
21255           context_die = declare_in_namespace (decl, context_die);
21256         }
21257
21258       /* Now output a DIE to represent the function itself.  */
21259       if (decl)
21260         gen_subprogram_die (decl, context_die);
21261       break;
21262
21263     case TYPE_DECL:
21264       /* If we are in terse mode, don't generate any DIEs to represent any
21265          actual typedefs.  */
21266       if (debug_info_level <= DINFO_LEVEL_TERSE)
21267         break;
21268
21269       /* In the special case of a TYPE_DECL node representing the declaration
21270          of some type tag, if the given TYPE_DECL is marked as having been
21271          instantiated from some other (original) TYPE_DECL node (e.g. one which
21272          was generated within the original definition of an inline function) we
21273          used to generate a special (abbreviated) DW_TAG_structure_type,
21274          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21275          should be actually referencing those DIEs, as variable DIEs with that
21276          type would be emitted already in the abstract origin, so it was always
21277          removed during unused type prunning.  Don't add anything in this
21278          case.  */
21279       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21280         break;
21281
21282       if (is_redundant_typedef (decl))
21283         gen_type_die (TREE_TYPE (decl), context_die);
21284       else
21285         /* Output a DIE to represent the typedef itself.  */
21286         gen_typedef_die (decl, context_die);
21287       break;
21288
21289     case LABEL_DECL:
21290       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21291         gen_label_die (decl, context_die);
21292       break;
21293
21294     case VAR_DECL:
21295     case RESULT_DECL:
21296       /* If we are in terse mode, don't generate any DIEs to represent any
21297          variable declarations or definitions.  */
21298       if (debug_info_level <= DINFO_LEVEL_TERSE)
21299         break;
21300
21301       /* Output any DIEs that are needed to specify the type of this data
21302          object.  */
21303       if (decl_by_reference_p (decl_or_origin))
21304         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21305       else
21306         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21307
21308       /* And its containing type.  */
21309       class_origin = decl_class_context (decl_or_origin);
21310       if (class_origin != NULL_TREE)
21311         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21312
21313       /* And its containing namespace.  */
21314       context_die = declare_in_namespace (decl_or_origin, context_die);
21315
21316       /* Now output the DIE to represent the data object itself.  This gets
21317          complicated because of the possibility that the VAR_DECL really
21318          represents an inlined instance of a formal parameter for an inline
21319          function.  */
21320       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21321       if (ultimate_origin != NULL_TREE
21322           && TREE_CODE (ultimate_origin) == PARM_DECL)
21323         gen_formal_parameter_die (decl, origin,
21324                                   true /* Emit name attribute.  */,
21325                                   context_die);
21326       else
21327         gen_variable_die (decl, origin, context_die);
21328       break;
21329
21330     case FIELD_DECL:
21331       /* Ignore the nameless fields that are used to skip bits but handle C++
21332          anonymous unions and structs.  */
21333       if (DECL_NAME (decl) != NULL_TREE
21334           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21335           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21336         {
21337           gen_type_die (member_declared_type (decl), context_die);
21338           gen_field_die (decl, context_die);
21339         }
21340       break;
21341
21342     case PARM_DECL:
21343       if (DECL_BY_REFERENCE (decl_or_origin))
21344         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21345       else
21346         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21347       return gen_formal_parameter_die (decl, origin,
21348                                        true /* Emit name attribute.  */,
21349                                        context_die);
21350
21351     case NAMESPACE_DECL:
21352     case IMPORTED_DECL:
21353       if (dwarf_version >= 3 || !dwarf_strict)
21354         gen_namespace_die (decl, context_die);
21355       break;
21356
21357     default:
21358       /* Probably some frontend-internal decl.  Assume we don't care.  */
21359       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21360       break;
21361     }
21362
21363   return NULL;
21364 }
21365 \f
21366 /* Output debug information for global decl DECL.  Called from toplev.c after
21367    compilation proper has finished.  */
21368
21369 static void
21370 dwarf2out_global_decl (tree decl)
21371 {
21372   /* Output DWARF2 information for file-scope tentative data object
21373      declarations, file-scope (extern) function declarations (which
21374      had no corresponding body) and file-scope tagged type declarations
21375      and definitions which have not yet been forced out.  */
21376   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21377     dwarf2out_decl (decl);
21378 }
21379
21380 /* Output debug information for type decl DECL.  Called from toplev.c
21381    and from language front ends (to record built-in types).  */
21382 static void
21383 dwarf2out_type_decl (tree decl, int local)
21384 {
21385   if (!local)
21386     dwarf2out_decl (decl);
21387 }
21388
21389 /* Output debug information for imported module or decl DECL.
21390    NAME is non-NULL name in the lexical block if the decl has been renamed.
21391    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21392    that DECL belongs to.
21393    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21394 static void
21395 dwarf2out_imported_module_or_decl_1 (tree decl,
21396                                      tree name,
21397                                      tree lexical_block,
21398                                      dw_die_ref lexical_block_die)
21399 {
21400   expanded_location xloc;
21401   dw_die_ref imported_die = NULL;
21402   dw_die_ref at_import_die;
21403
21404   if (TREE_CODE (decl) == IMPORTED_DECL)
21405     {
21406       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21407       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21408       gcc_assert (decl);
21409     }
21410   else
21411     xloc = expand_location (input_location);
21412
21413   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21414     {
21415       at_import_die = force_type_die (TREE_TYPE (decl));
21416       /* For namespace N { typedef void T; } using N::T; base_type_die
21417          returns NULL, but DW_TAG_imported_declaration requires
21418          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21419       if (!at_import_die)
21420         {
21421           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21422           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21423           at_import_die = lookup_type_die (TREE_TYPE (decl));
21424           gcc_assert (at_import_die);
21425         }
21426     }
21427   else
21428     {
21429       at_import_die = lookup_decl_die (decl);
21430       if (!at_import_die)
21431         {
21432           /* If we're trying to avoid duplicate debug info, we may not have
21433              emitted the member decl for this field.  Emit it now.  */
21434           if (TREE_CODE (decl) == FIELD_DECL)
21435             {
21436               tree type = DECL_CONTEXT (decl);
21437
21438               if (TYPE_CONTEXT (type)
21439                   && TYPE_P (TYPE_CONTEXT (type))
21440                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21441                                                 DINFO_USAGE_DIR_USE))
21442                 return;
21443               gen_type_die_for_member (type, decl,
21444                                        get_context_die (TYPE_CONTEXT (type)));
21445             }
21446           at_import_die = force_decl_die (decl);
21447         }
21448     }
21449
21450   if (TREE_CODE (decl) == NAMESPACE_DECL)
21451     {
21452       if (dwarf_version >= 3 || !dwarf_strict)
21453         imported_die = new_die (DW_TAG_imported_module,
21454                                 lexical_block_die,
21455                                 lexical_block);
21456       else
21457         return;
21458     }
21459   else
21460     imported_die = new_die (DW_TAG_imported_declaration,
21461                             lexical_block_die,
21462                             lexical_block);
21463
21464   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21465   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21466   if (name)
21467     add_AT_string (imported_die, DW_AT_name,
21468                    IDENTIFIER_POINTER (name));
21469   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21470 }
21471
21472 /* Output debug information for imported module or decl DECL.
21473    NAME is non-NULL name in context if the decl has been renamed.
21474    CHILD is true if decl is one of the renamed decls as part of
21475    importing whole module.  */
21476
21477 static void
21478 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21479                                    bool child)
21480 {
21481   /* dw_die_ref at_import_die;  */
21482   dw_die_ref scope_die;
21483
21484   if (debug_info_level <= DINFO_LEVEL_TERSE)
21485     return;
21486
21487   gcc_assert (decl);
21488
21489   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21490      We need decl DIE for reference and scope die. First, get DIE for the decl
21491      itself.  */
21492
21493   /* Get the scope die for decl context. Use comp_unit_die for global module
21494      or decl. If die is not found for non globals, force new die.  */
21495   if (context
21496       && TYPE_P (context)
21497       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21498     return;
21499
21500   if (!(dwarf_version >= 3 || !dwarf_strict))
21501     return;
21502
21503   scope_die = get_context_die (context);
21504
21505   if (child)
21506     {
21507       gcc_assert (scope_die->die_child);
21508       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21509       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21510       scope_die = scope_die->die_child;
21511     }
21512
21513   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21514   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21515
21516 }
21517
21518 /* Write the debugging output for DECL.  */
21519
21520 void
21521 dwarf2out_decl (tree decl)
21522 {
21523   dw_die_ref context_die = comp_unit_die ();
21524
21525   switch (TREE_CODE (decl))
21526     {
21527     case ERROR_MARK:
21528       return;
21529
21530     case FUNCTION_DECL:
21531       /* What we would really like to do here is to filter out all mere
21532          file-scope declarations of file-scope functions which are never
21533          referenced later within this translation unit (and keep all of ones
21534          that *are* referenced later on) but we aren't clairvoyant, so we have
21535          no idea which functions will be referenced in the future (i.e. later
21536          on within the current translation unit). So here we just ignore all
21537          file-scope function declarations which are not also definitions.  If
21538          and when the debugger needs to know something about these functions,
21539          it will have to hunt around and find the DWARF information associated
21540          with the definition of the function.
21541
21542          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21543          nodes represent definitions and which ones represent mere
21544          declarations.  We have to check DECL_INITIAL instead. That's because
21545          the C front-end supports some weird semantics for "extern inline"
21546          function definitions.  These can get inlined within the current
21547          translation unit (and thus, we need to generate Dwarf info for their
21548          abstract instances so that the Dwarf info for the concrete inlined
21549          instances can have something to refer to) but the compiler never
21550          generates any out-of-lines instances of such things (despite the fact
21551          that they *are* definitions).
21552
21553          The important point is that the C front-end marks these "extern
21554          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21555          them anyway. Note that the C++ front-end also plays some similar games
21556          for inline function definitions appearing within include files which
21557          also contain `#pragma interface' pragmas.  */
21558       if (DECL_INITIAL (decl) == NULL_TREE)
21559         return;
21560
21561       /* If we're a nested function, initially use a parent of NULL; if we're
21562          a plain function, this will be fixed up in decls_for_scope.  If
21563          we're a method, it will be ignored, since we already have a DIE.  */
21564       if (decl_function_context (decl)
21565           /* But if we're in terse mode, we don't care about scope.  */
21566           && debug_info_level > DINFO_LEVEL_TERSE)
21567         context_die = NULL;
21568       break;
21569
21570     case VAR_DECL:
21571       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21572          declaration and if the declaration was never even referenced from
21573          within this entire compilation unit.  We suppress these DIEs in
21574          order to save space in the .debug section (by eliminating entries
21575          which are probably useless).  Note that we must not suppress
21576          block-local extern declarations (whether used or not) because that
21577          would screw-up the debugger's name lookup mechanism and cause it to
21578          miss things which really ought to be in scope at a given point.  */
21579       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21580         return;
21581
21582       /* For local statics lookup proper context die.  */
21583       if (TREE_STATIC (decl) && decl_function_context (decl))
21584         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21585
21586       /* If we are in terse mode, don't generate any DIEs to represent any
21587          variable declarations or definitions.  */
21588       if (debug_info_level <= DINFO_LEVEL_TERSE)
21589         return;
21590       break;
21591
21592     case CONST_DECL:
21593       if (debug_info_level <= DINFO_LEVEL_TERSE)
21594         return;
21595       if (!is_fortran () && !is_ada ())
21596         return;
21597       if (TREE_STATIC (decl) && decl_function_context (decl))
21598         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21599       break;
21600
21601     case NAMESPACE_DECL:
21602     case IMPORTED_DECL:
21603       if (debug_info_level <= DINFO_LEVEL_TERSE)
21604         return;
21605       if (lookup_decl_die (decl) != NULL)
21606         return;
21607       break;
21608
21609     case TYPE_DECL:
21610       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21611       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21612         return;
21613
21614       /* Don't bother trying to generate any DIEs to represent any of the
21615          normal built-in types for the language we are compiling.  */
21616       if (DECL_IS_BUILTIN (decl))
21617         return;
21618
21619       /* If we are in terse mode, don't generate any DIEs for types.  */
21620       if (debug_info_level <= DINFO_LEVEL_TERSE)
21621         return;
21622
21623       /* If we're a function-scope tag, initially use a parent of NULL;
21624          this will be fixed up in decls_for_scope.  */
21625       if (decl_function_context (decl))
21626         context_die = NULL;
21627
21628       break;
21629
21630     default:
21631       return;
21632     }
21633
21634   gen_decl_die (decl, NULL, context_die);
21635 }
21636
21637 /* Write the debugging output for DECL.  */
21638
21639 static void
21640 dwarf2out_function_decl (tree decl)
21641 {
21642   dwarf2out_decl (decl);
21643
21644   htab_empty (decl_loc_table);
21645   htab_empty (cached_dw_loc_list_table);
21646 }
21647
21648 /* Output a marker (i.e. a label) for the beginning of the generated code for
21649    a lexical block.  */
21650
21651 static void
21652 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21653                        unsigned int blocknum)
21654 {
21655   switch_to_section (current_function_section ());
21656   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21657 }
21658
21659 /* Output a marker (i.e. a label) for the end of the generated code for a
21660    lexical block.  */
21661
21662 static void
21663 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21664 {
21665   switch_to_section (current_function_section ());
21666   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21667 }
21668
21669 /* Returns nonzero if it is appropriate not to emit any debugging
21670    information for BLOCK, because it doesn't contain any instructions.
21671
21672    Don't allow this for blocks with nested functions or local classes
21673    as we would end up with orphans, and in the presence of scheduling
21674    we may end up calling them anyway.  */
21675
21676 static bool
21677 dwarf2out_ignore_block (const_tree block)
21678 {
21679   tree decl;
21680   unsigned int i;
21681
21682   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21683     if (TREE_CODE (decl) == FUNCTION_DECL
21684         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21685       return 0;
21686   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21687     {
21688       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21689       if (TREE_CODE (decl) == FUNCTION_DECL
21690           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21691       return 0;
21692     }
21693
21694   return 1;
21695 }
21696
21697 /* Hash table routines for file_hash.  */
21698
21699 static int
21700 file_table_eq (const void *p1_p, const void *p2_p)
21701 {
21702   const struct dwarf_file_data *const p1 =
21703     (const struct dwarf_file_data *) p1_p;
21704   const char *const p2 = (const char *) p2_p;
21705   return filename_cmp (p1->filename, p2) == 0;
21706 }
21707
21708 static hashval_t
21709 file_table_hash (const void *p_p)
21710 {
21711   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21712   return htab_hash_string (p->filename);
21713 }
21714
21715 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21716    dwarf2out.c) and return its "index".  The index of each (known) filename is
21717    just a unique number which is associated with only that one filename.  We
21718    need such numbers for the sake of generating labels (in the .debug_sfnames
21719    section) and references to those files numbers (in the .debug_srcinfo
21720    and.debug_macinfo sections).  If the filename given as an argument is not
21721    found in our current list, add it to the list and assign it the next
21722    available unique index number.  In order to speed up searches, we remember
21723    the index of the filename was looked up last.  This handles the majority of
21724    all searches.  */
21725
21726 static struct dwarf_file_data *
21727 lookup_filename (const char *file_name)
21728 {
21729   void ** slot;
21730   struct dwarf_file_data * created;
21731
21732   /* Check to see if the file name that was searched on the previous
21733      call matches this file name.  If so, return the index.  */
21734   if (file_table_last_lookup
21735       && (file_name == file_table_last_lookup->filename
21736           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21737     return file_table_last_lookup;
21738
21739   /* Didn't match the previous lookup, search the table.  */
21740   slot = htab_find_slot_with_hash (file_table, file_name,
21741                                    htab_hash_string (file_name), INSERT);
21742   if (*slot)
21743     return (struct dwarf_file_data *) *slot;
21744
21745   created = ggc_alloc_dwarf_file_data ();
21746   created->filename = file_name;
21747   created->emitted_number = 0;
21748   *slot = created;
21749   return created;
21750 }
21751
21752 /* If the assembler will construct the file table, then translate the compiler
21753    internal file table number into the assembler file table number, and emit
21754    a .file directive if we haven't already emitted one yet.  The file table
21755    numbers are different because we prune debug info for unused variables and
21756    types, which may include filenames.  */
21757
21758 static int
21759 maybe_emit_file (struct dwarf_file_data * fd)
21760 {
21761   if (! fd->emitted_number)
21762     {
21763       if (last_emitted_file)
21764         fd->emitted_number = last_emitted_file->emitted_number + 1;
21765       else
21766         fd->emitted_number = 1;
21767       last_emitted_file = fd;
21768
21769       if (DWARF2_ASM_LINE_DEBUG_INFO)
21770         {
21771           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21772           output_quoted_string (asm_out_file,
21773                                 remap_debug_filename (fd->filename));
21774           fputc ('\n', asm_out_file);
21775         }
21776     }
21777
21778   return fd->emitted_number;
21779 }
21780
21781 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21782    That generation should happen after function debug info has been
21783    generated. The value of the attribute is the constant value of ARG.  */
21784
21785 static void
21786 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21787 {
21788   die_arg_entry entry;
21789
21790   if (!die || !arg)
21791     return;
21792
21793   if (!tmpl_value_parm_die_table)
21794     tmpl_value_parm_die_table
21795       = VEC_alloc (die_arg_entry, gc, 32);
21796
21797   entry.die = die;
21798   entry.arg = arg;
21799   VEC_safe_push (die_arg_entry, gc,
21800                  tmpl_value_parm_die_table,
21801                  &entry);
21802 }
21803
21804 /* Return TRUE if T is an instance of generic type, FALSE
21805    otherwise.  */
21806
21807 static bool
21808 generic_type_p (tree t)
21809 {
21810   if (t == NULL_TREE || !TYPE_P (t))
21811     return false;
21812   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21813 }
21814
21815 /* Schedule the generation of the generic parameter dies for the
21816   instance of generic type T. The proper generation itself is later
21817   done by gen_scheduled_generic_parms_dies. */
21818
21819 static void
21820 schedule_generic_params_dies_gen (tree t)
21821 {
21822   if (!generic_type_p (t))
21823     return;
21824
21825   if (generic_type_instances == NULL)
21826     generic_type_instances = VEC_alloc (tree, gc, 256);
21827
21828   VEC_safe_push (tree, gc, generic_type_instances, t);
21829 }
21830
21831 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21832    by append_entry_to_tmpl_value_parm_die_table. This function must
21833    be called after function DIEs have been generated.  */
21834
21835 static void
21836 gen_remaining_tmpl_value_param_die_attribute (void)
21837 {
21838   if (tmpl_value_parm_die_table)
21839     {
21840       unsigned i;
21841       die_arg_entry *e;
21842
21843       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21844         tree_add_const_value_attribute (e->die, e->arg);
21845     }
21846 }
21847
21848 /* Generate generic parameters DIEs for instances of generic types
21849    that have been previously scheduled by
21850    schedule_generic_params_dies_gen. This function must be called
21851    after all the types of the CU have been laid out.  */
21852
21853 static void
21854 gen_scheduled_generic_parms_dies (void)
21855 {
21856   unsigned i;
21857   tree t;
21858
21859   if (generic_type_instances == NULL)
21860     return;
21861   
21862   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21863     gen_generic_params_dies (t);
21864 }
21865
21866
21867 /* Replace DW_AT_name for the decl with name.  */
21868
21869 static void
21870 dwarf2out_set_name (tree decl, tree name)
21871 {
21872   dw_die_ref die;
21873   dw_attr_ref attr;
21874   const char *dname;
21875
21876   die = TYPE_SYMTAB_DIE (decl);
21877   if (!die)
21878     return;
21879
21880   dname = dwarf2_name (name, 0);
21881   if (!dname)
21882     return;
21883
21884   attr = get_AT (die, DW_AT_name);
21885   if (attr)
21886     {
21887       struct indirect_string_node *node;
21888
21889       node = find_AT_string (dname);
21890       /* replace the string.  */
21891       attr->dw_attr_val.v.val_str = node;
21892     }
21893
21894   else
21895     add_name_attribute (die, dname);
21896 }
21897
21898 /* Called by the final INSN scan whenever we see a direct function call.
21899    Make an entry into the direct call table, recording the point of call
21900    and a reference to the target function's debug entry.  */
21901
21902 static void
21903 dwarf2out_direct_call (tree targ)
21904 {
21905   dcall_entry e;
21906   tree origin = decl_ultimate_origin (targ);
21907
21908   /* If this is a clone, use the abstract origin as the target.  */
21909   if (origin)
21910     targ = origin;
21911
21912   e.poc_label_num = poc_label_num++;
21913   e.poc_decl = current_function_decl;
21914   e.targ_die = force_decl_die (targ);
21915   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21916
21917   /* Drop a label at the return point to mark the point of call.  */
21918   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21919 }
21920
21921 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21922
21923 static hashval_t
21924 vcall_insn_table_hash (const void *x)
21925 {
21926   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21927 }
21928
21929 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21930    insnd_uid of *Y.  */
21931
21932 static int
21933 vcall_insn_table_eq (const void *x, const void *y)
21934 {
21935   return (((const struct vcall_insn *) x)->insn_uid
21936           == ((const struct vcall_insn *) y)->insn_uid);
21937 }
21938
21939 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21940
21941 static void
21942 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21943 {
21944   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21945   struct vcall_insn **slot;
21946
21947   gcc_assert (item);
21948   item->insn_uid = insn_uid;
21949   item->vtable_slot = vtable_slot;
21950   slot = (struct vcall_insn **)
21951       htab_find_slot_with_hash (vcall_insn_table, &item,
21952                                 (hashval_t) insn_uid, INSERT);
21953   *slot = item;
21954 }
21955
21956 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21957
21958 static unsigned int
21959 lookup_vcall_insn (unsigned int insn_uid)
21960 {
21961   struct vcall_insn item;
21962   struct vcall_insn *p;
21963
21964   item.insn_uid = insn_uid;
21965   item.vtable_slot = 0;
21966   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21967                                                  (void *) &item,
21968                                                  (hashval_t) insn_uid);
21969   if (p == NULL)
21970     return (unsigned int) -1;
21971   return p->vtable_slot;
21972 }
21973
21974
21975 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21976    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21977    is the vtable slot index that we will need to put in the virtual call
21978    table later.  */
21979
21980 static void
21981 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21982 {
21983   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21984     {
21985       tree token = OBJ_TYPE_REF_TOKEN (addr);
21986       if (TREE_CODE (token) == INTEGER_CST)
21987         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21988     }
21989 }
21990
21991 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21992    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21993    with NEW_INSN.  */
21994
21995 static void
21996 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21997 {
21998   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21999
22000   if (vtable_slot != (unsigned int) -1)
22001     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
22002 }
22003
22004 /* Called by the final INSN scan whenever we see a virtual function call.
22005    Make an entry into the virtual call table, recording the point of call
22006    and the slot index of the vtable entry used to call the virtual member
22007    function.  The slot index was associated with the INSN_UID during the
22008    lowering to RTL.  */
22009
22010 static void
22011 dwarf2out_virtual_call (int insn_uid)
22012 {
22013   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
22014   vcall_entry e;
22015
22016   if (vtable_slot == (unsigned int) -1)
22017     return;
22018
22019   e.poc_label_num = poc_label_num++;
22020   e.vtable_slot = vtable_slot;
22021   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
22022
22023   /* Drop a label at the return point to mark the point of call.  */
22024   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
22025 }
22026
22027 /* Called by the final INSN scan whenever we see a var location.  We
22028    use it to drop labels in the right places, and throw the location in
22029    our lookup table.  */
22030
22031 static void
22032 dwarf2out_var_location (rtx loc_note)
22033 {
22034   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22035   struct var_loc_node *newloc;
22036   rtx next_real;
22037   static const char *last_label;
22038   static const char *last_postcall_label;
22039   static bool last_in_cold_section_p;
22040   tree decl;
22041
22042   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22043     return;
22044
22045   next_real = next_real_insn (loc_note);
22046   /* If there are no instructions which would be affected by this note,
22047      don't do anything.  */
22048   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
22049     return;
22050
22051   /* If there were any real insns between note we processed last time
22052      and this note (or if it is the first note), clear
22053      last_{,postcall_}label so that they are not reused this time.  */
22054   if (last_var_location_insn == NULL_RTX
22055       || last_var_location_insn != next_real
22056       || last_in_cold_section_p != in_cold_section_p)
22057     {
22058       last_label = NULL;
22059       last_postcall_label = NULL;
22060     }
22061
22062   decl = NOTE_VAR_LOCATION_DECL (loc_note);
22063   newloc = add_var_loc_to_decl (decl, loc_note,
22064                                 NOTE_DURING_CALL_P (loc_note)
22065                                 ? last_postcall_label : last_label);
22066   if (newloc == NULL)
22067     return;
22068
22069   /* If there were no real insns between note we processed last time
22070      and this note, use the label we emitted last time.  Otherwise
22071      create a new label and emit it.  */
22072   if (last_label == NULL)
22073     {
22074       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22075       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22076       loclabel_num++;
22077       last_label = ggc_strdup (loclabel);
22078     }
22079
22080   if (!NOTE_DURING_CALL_P (loc_note))
22081     newloc->label = last_label;
22082   else
22083     {
22084       if (!last_postcall_label)
22085         {
22086           sprintf (loclabel, "%s-1", last_label);
22087           last_postcall_label = ggc_strdup (loclabel);
22088         }
22089       newloc->label = last_postcall_label;
22090     }
22091
22092   last_var_location_insn = next_real;
22093   last_in_cold_section_p = in_cold_section_p;
22094 }
22095
22096 /* Note in one location list that text section has changed.  */
22097
22098 static int
22099 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22100 {
22101   var_loc_list *list = (var_loc_list *) *slot;
22102   if (list->first)
22103     list->last_before_switch
22104       = list->last->next ? list->last->next : list->last;
22105   return 1;
22106 }
22107
22108 /* Note in all location lists that text section has changed.  */
22109
22110 static void
22111 var_location_switch_text_section (void)
22112 {
22113   if (decl_loc_table == NULL)
22114     return;
22115
22116   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22117 }
22118
22119 /* We need to reset the locations at the beginning of each
22120    function. We can't do this in the end_function hook, because the
22121    declarations that use the locations won't have been output when
22122    that hook is called.  Also compute have_multiple_function_sections here.  */
22123
22124 static void
22125 dwarf2out_begin_function (tree fun)
22126 {
22127   if (function_section (fun) != text_section)
22128     have_multiple_function_sections = true;
22129   if (flag_reorder_blocks_and_partition && !cold_text_section)
22130     {
22131       gcc_assert (current_function_decl == fun);
22132       cold_text_section = unlikely_text_section ();
22133       switch_to_section (cold_text_section);
22134       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22135       switch_to_section (current_function_section ());
22136     }
22137
22138   dwarf2out_note_section_used ();
22139 }
22140
22141 /* Output a label to mark the beginning of a source code line entry
22142    and record information relating to this source line, in
22143    'line_info_table' for later output of the .debug_line section.  */
22144
22145 static void
22146 dwarf2out_source_line (unsigned int line, const char *filename,
22147                        int discriminator, bool is_stmt)
22148 {
22149   static bool last_is_stmt = true;
22150
22151   if (debug_info_level >= DINFO_LEVEL_NORMAL
22152       && line != 0)
22153     {
22154       int file_num = maybe_emit_file (lookup_filename (filename));
22155
22156       switch_to_section (current_function_section ());
22157
22158       /* If requested, emit something human-readable.  */
22159       if (flag_debug_asm)
22160         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22161                  filename, line);
22162
22163       if (DWARF2_ASM_LINE_DEBUG_INFO)
22164         {
22165           /* Emit the .loc directive understood by GNU as.  */
22166           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22167           if (is_stmt != last_is_stmt)
22168             {
22169               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22170               last_is_stmt = is_stmt;
22171             }
22172           if (SUPPORTS_DISCRIMINATOR && discriminator != 0
22173               && (dwarf_version >= 4 || !dwarf_strict))
22174             fprintf (asm_out_file, " discriminator %d", discriminator);
22175           fputc ('\n', asm_out_file);
22176
22177           /* Indicate that line number info exists.  */
22178           line_info_table_in_use++;
22179         }
22180       else if (function_section (current_function_decl) != text_section)
22181         {
22182           dw_separate_line_info_ref line_info;
22183           targetm.asm_out.internal_label (asm_out_file,
22184                                           SEPARATE_LINE_CODE_LABEL,
22185                                           separate_line_info_table_in_use);
22186
22187           /* Expand the line info table if necessary.  */
22188           if (separate_line_info_table_in_use
22189               == separate_line_info_table_allocated)
22190             {
22191               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22192               separate_line_info_table
22193                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22194                                  separate_line_info_table,
22195                                  separate_line_info_table_allocated);
22196               memset (separate_line_info_table
22197                        + separate_line_info_table_in_use,
22198                       0,
22199                       (LINE_INFO_TABLE_INCREMENT
22200                        * sizeof (dw_separate_line_info_entry)));
22201             }
22202
22203           /* Add the new entry at the end of the line_info_table.  */
22204           line_info
22205             = &separate_line_info_table[separate_line_info_table_in_use++];
22206           line_info->dw_file_num = file_num;
22207           line_info->dw_line_num = line;
22208           line_info->function = current_function_funcdef_no;
22209         }
22210       else
22211         {
22212           dw_line_info_ref line_info;
22213
22214           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22215                                      line_info_table_in_use);
22216
22217           /* Expand the line info table if necessary.  */
22218           if (line_info_table_in_use == line_info_table_allocated)
22219             {
22220               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22221               line_info_table
22222                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22223                                  line_info_table_allocated);
22224               memset (line_info_table + line_info_table_in_use, 0,
22225                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22226             }
22227
22228           /* Add the new entry at the end of the line_info_table.  */
22229           line_info = &line_info_table[line_info_table_in_use++];
22230           line_info->dw_file_num = file_num;
22231           line_info->dw_line_num = line;
22232         }
22233     }
22234 }
22235
22236 /* Record the beginning of a new source file.  */
22237
22238 static void
22239 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22240 {
22241   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22242     {
22243       /* Record the beginning of the file for break_out_includes.  */
22244       dw_die_ref bincl_die;
22245
22246       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22247       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22248     }
22249
22250   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22251     {
22252       macinfo_entry e;
22253       e.code = DW_MACINFO_start_file;
22254       e.lineno = lineno;
22255       e.info = xstrdup (filename);
22256       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22257     }
22258 }
22259
22260 /* Record the end of a source file.  */
22261
22262 static void
22263 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22264 {
22265   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22266     /* Record the end of the file for break_out_includes.  */
22267     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22268
22269   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22270     {
22271       macinfo_entry e;
22272       e.code = DW_MACINFO_end_file;
22273       e.lineno = lineno;
22274       e.info = NULL;
22275       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22276     }
22277 }
22278
22279 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22280    the tail part of the directive line, i.e. the part which is past the
22281    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22282
22283 static void
22284 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22285                   const char *buffer ATTRIBUTE_UNUSED)
22286 {
22287   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22288     {
22289       macinfo_entry e;
22290       e.code = DW_MACINFO_define;
22291       e.lineno = lineno;
22292       e.info = xstrdup (buffer);;
22293       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22294     }
22295 }
22296
22297 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22298    the tail part of the directive line, i.e. the part which is past the
22299    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22300
22301 static void
22302 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22303                  const char *buffer ATTRIBUTE_UNUSED)
22304 {
22305   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22306     {
22307       macinfo_entry e;
22308       e.code = DW_MACINFO_undef;
22309       e.lineno = lineno;
22310       e.info = xstrdup (buffer);;
22311       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22312     }
22313 }
22314
22315 static void
22316 output_macinfo (void)
22317 {
22318   unsigned i;
22319   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22320   macinfo_entry *ref;
22321
22322   if (! length)
22323     return;
22324
22325   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22326     {
22327       switch (ref->code)
22328         {
22329           case DW_MACINFO_start_file:
22330             {
22331               int file_num = maybe_emit_file (lookup_filename (ref->info));
22332               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22333               dw2_asm_output_data_uleb128 
22334                         (ref->lineno, "Included from line number %lu", 
22335                                                 (unsigned long)ref->lineno);
22336               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22337             }
22338             break;
22339           case DW_MACINFO_end_file:
22340             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22341             break;
22342           case DW_MACINFO_define:
22343             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22344             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22345                                                 (unsigned long)ref->lineno);
22346             dw2_asm_output_nstring (ref->info, -1, "The macro");
22347             break;
22348           case DW_MACINFO_undef:
22349             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22350             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22351                                                 (unsigned long)ref->lineno);
22352             dw2_asm_output_nstring (ref->info, -1, "The macro");
22353             break;
22354           default:
22355            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22356              ASM_COMMENT_START, (unsigned long)ref->code);
22357           break;
22358         }
22359     }
22360 }
22361
22362 /* Set up for Dwarf output at the start of compilation.  */
22363
22364 static void
22365 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22366 {
22367   /* Allocate the file_table.  */
22368   file_table = htab_create_ggc (50, file_table_hash,
22369                                 file_table_eq, NULL);
22370
22371   /* Allocate the decl_die_table.  */
22372   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22373                                     decl_die_table_eq, NULL);
22374
22375   /* Allocate the decl_loc_table.  */
22376   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22377                                     decl_loc_table_eq, NULL);
22378
22379   /* Allocate the cached_dw_loc_list_table.  */
22380   cached_dw_loc_list_table
22381     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22382                        cached_dw_loc_list_table_eq, NULL);
22383
22384   /* Allocate the initial hunk of the decl_scope_table.  */
22385   decl_scope_table = VEC_alloc (tree, gc, 256);
22386
22387   /* Allocate the initial hunk of the abbrev_die_table.  */
22388   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22389     (ABBREV_DIE_TABLE_INCREMENT);
22390   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22391   /* Zero-th entry is allocated, but unused.  */
22392   abbrev_die_table_in_use = 1;
22393
22394   /* Allocate the initial hunk of the line_info_table.  */
22395   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22396     (LINE_INFO_TABLE_INCREMENT);
22397   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22398
22399   /* Zero-th entry is allocated, but unused.  */
22400   line_info_table_in_use = 1;
22401
22402   /* Allocate the pubtypes and pubnames vectors.  */
22403   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22404   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22405
22406   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
22407   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
22408                                       vcall_insn_table_eq, NULL);
22409
22410   incomplete_types = VEC_alloc (tree, gc, 64);
22411
22412   used_rtx_array = VEC_alloc (rtx, gc, 32);
22413
22414   debug_info_section = get_section (DEBUG_INFO_SECTION,
22415                                     SECTION_DEBUG, NULL);
22416   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22417                                       SECTION_DEBUG, NULL);
22418   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22419                                        SECTION_DEBUG, NULL);
22420   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22421                                        SECTION_DEBUG, NULL);
22422   debug_line_section = get_section (DEBUG_LINE_SECTION,
22423                                     SECTION_DEBUG, NULL);
22424   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22425                                    SECTION_DEBUG, NULL);
22426   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22427                                         SECTION_DEBUG, NULL);
22428   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22429                                         SECTION_DEBUG, NULL);
22430   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
22431                                      SECTION_DEBUG, NULL);
22432   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
22433                                      SECTION_DEBUG, NULL);
22434   debug_str_section = get_section (DEBUG_STR_SECTION,
22435                                    DEBUG_STR_SECTION_FLAGS, NULL);
22436   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22437                                       SECTION_DEBUG, NULL);
22438   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22439                                      SECTION_DEBUG, NULL);
22440
22441   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22442   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22443                                DEBUG_ABBREV_SECTION_LABEL, 0);
22444   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22445   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22446                                COLD_TEXT_SECTION_LABEL, 0);
22447   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22448
22449   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22450                                DEBUG_INFO_SECTION_LABEL, 0);
22451   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22452                                DEBUG_LINE_SECTION_LABEL, 0);
22453   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22454                                DEBUG_RANGES_SECTION_LABEL, 0);
22455   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22456                                DEBUG_MACINFO_SECTION_LABEL, 0);
22457
22458   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22459     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22460
22461   switch_to_section (text_section);
22462   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22463 }
22464
22465 /* Called before cgraph_optimize starts outputtting functions, variables
22466    and toplevel asms into assembly.  */
22467
22468 static void
22469 dwarf2out_assembly_start (void)
22470 {
22471   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22472       && dwarf2out_do_cfi_asm ()
22473       && (!(flag_unwind_tables || flag_exceptions)
22474           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22475     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22476 }
22477
22478 /* A helper function for dwarf2out_finish called through
22479    htab_traverse.  Emit one queued .debug_str string.  */
22480
22481 static int
22482 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22483 {
22484   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22485
22486   if (node->label && node->refcount)
22487     {
22488       switch_to_section (debug_str_section);
22489       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22490       assemble_string (node->str, strlen (node->str) + 1);
22491     }
22492
22493   return 1;
22494 }
22495
22496 #if ENABLE_ASSERT_CHECKING
22497 /* Verify that all marks are clear.  */
22498
22499 static void
22500 verify_marks_clear (dw_die_ref die)
22501 {
22502   dw_die_ref c;
22503
22504   gcc_assert (! die->die_mark);
22505   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22506 }
22507 #endif /* ENABLE_ASSERT_CHECKING */
22508
22509 /* Clear the marks for a die and its children.
22510    Be cool if the mark isn't set.  */
22511
22512 static void
22513 prune_unmark_dies (dw_die_ref die)
22514 {
22515   dw_die_ref c;
22516
22517   if (die->die_mark)
22518     die->die_mark = 0;
22519   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22520 }
22521
22522 /* Given DIE that we're marking as used, find any other dies
22523    it references as attributes and mark them as used.  */
22524
22525 static void
22526 prune_unused_types_walk_attribs (dw_die_ref die)
22527 {
22528   dw_attr_ref a;
22529   unsigned ix;
22530
22531   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22532     {
22533       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22534         {
22535           /* A reference to another DIE.
22536              Make sure that it will get emitted.
22537              If it was broken out into a comdat group, don't follow it.  */
22538           if (dwarf_version < 4
22539               || a->dw_attr == DW_AT_specification
22540               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22541             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22542         }
22543       /* Set the string's refcount to 0 so that prune_unused_types_mark
22544          accounts properly for it.  */
22545       if (AT_class (a) == dw_val_class_str)
22546         a->dw_attr_val.v.val_str->refcount = 0;
22547     }
22548 }
22549
22550 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22551
22552 static void
22553 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22554 {
22555   dw_die_ref c;
22556
22557   if (die == NULL || die->die_child == NULL)
22558     return;
22559   c = die->die_child;
22560   do
22561     {
22562       switch (c->die_tag)
22563         {
22564         case DW_TAG_template_type_param:
22565         case DW_TAG_template_value_param:
22566         case DW_TAG_GNU_template_template_param:
22567         case DW_TAG_GNU_template_parameter_pack:
22568           prune_unused_types_mark (c, 1);
22569           break;
22570         default:
22571           break;
22572         }
22573       c = c->die_sib;
22574     } while (c && c != die->die_child);
22575 }
22576
22577 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22578    to DIE's children.  */
22579
22580 static void
22581 prune_unused_types_mark (dw_die_ref die, int dokids)
22582 {
22583   dw_die_ref c;
22584
22585   if (die->die_mark == 0)
22586     {
22587       /* We haven't done this node yet.  Mark it as used.  */
22588       die->die_mark = 1;
22589       /* If this is the DIE of a generic type instantiation,
22590          mark the children DIEs that describe its generic parms and
22591          args.  */
22592       prune_unused_types_mark_generic_parms_dies (die);
22593
22594       /* We also have to mark its parents as used.
22595          (But we don't want to mark our parents' kids due to this.)  */
22596       if (die->die_parent)
22597         prune_unused_types_mark (die->die_parent, 0);
22598
22599       /* Mark any referenced nodes.  */
22600       prune_unused_types_walk_attribs (die);
22601
22602       /* If this node is a specification,
22603          also mark the definition, if it exists.  */
22604       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22605         prune_unused_types_mark (die->die_definition, 1);
22606     }
22607
22608   if (dokids && die->die_mark != 2)
22609     {
22610       /* We need to walk the children, but haven't done so yet.
22611          Remember that we've walked the kids.  */
22612       die->die_mark = 2;
22613
22614       /* If this is an array type, we need to make sure our
22615          kids get marked, even if they're types.  If we're
22616          breaking out types into comdat sections, do this
22617          for all type definitions.  */
22618       if (die->die_tag == DW_TAG_array_type
22619           || (dwarf_version >= 4
22620               && is_type_die (die) && ! is_declaration_die (die)))
22621         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22622       else
22623         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22624     }
22625 }
22626
22627 /* For local classes, look if any static member functions were emitted
22628    and if so, mark them.  */
22629
22630 static void
22631 prune_unused_types_walk_local_classes (dw_die_ref die)
22632 {
22633   dw_die_ref c;
22634
22635   if (die->die_mark == 2)
22636     return;
22637
22638   switch (die->die_tag)
22639     {
22640     case DW_TAG_structure_type:
22641     case DW_TAG_union_type:
22642     case DW_TAG_class_type:
22643       break;
22644
22645     case DW_TAG_subprogram:
22646       if (!get_AT_flag (die, DW_AT_declaration)
22647           || die->die_definition != NULL)
22648         prune_unused_types_mark (die, 1);
22649       return;
22650
22651     default:
22652       return;
22653     }
22654
22655   /* Mark children.  */
22656   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22657 }
22658
22659 /* Walk the tree DIE and mark types that we actually use.  */
22660
22661 static void
22662 prune_unused_types_walk (dw_die_ref die)
22663 {
22664   dw_die_ref c;
22665
22666   /* Don't do anything if this node is already marked and
22667      children have been marked as well.  */
22668   if (die->die_mark == 2)
22669     return;
22670
22671   switch (die->die_tag)
22672     {
22673     case DW_TAG_structure_type:
22674     case DW_TAG_union_type:
22675     case DW_TAG_class_type:
22676       if (die->die_perennial_p)
22677         break;
22678
22679       for (c = die->die_parent; c; c = c->die_parent)
22680         if (c->die_tag == DW_TAG_subprogram)
22681           break;
22682
22683       /* Finding used static member functions inside of classes
22684          is needed just for local classes, because for other classes
22685          static member function DIEs with DW_AT_specification
22686          are emitted outside of the DW_TAG_*_type.  If we ever change
22687          it, we'd need to call this even for non-local classes.  */
22688       if (c)
22689         prune_unused_types_walk_local_classes (die);
22690
22691       /* It's a type node --- don't mark it.  */
22692       return;
22693
22694     case DW_TAG_const_type:
22695     case DW_TAG_packed_type:
22696     case DW_TAG_pointer_type:
22697     case DW_TAG_reference_type:
22698     case DW_TAG_rvalue_reference_type:
22699     case DW_TAG_volatile_type:
22700     case DW_TAG_typedef:
22701     case DW_TAG_array_type:
22702     case DW_TAG_interface_type:
22703     case DW_TAG_friend:
22704     case DW_TAG_variant_part:
22705     case DW_TAG_enumeration_type:
22706     case DW_TAG_subroutine_type:
22707     case DW_TAG_string_type:
22708     case DW_TAG_set_type:
22709     case DW_TAG_subrange_type:
22710     case DW_TAG_ptr_to_member_type:
22711     case DW_TAG_file_type:
22712       if (die->die_perennial_p)
22713         break;
22714
22715       /* It's a type node --- don't mark it.  */
22716       return;
22717
22718     default:
22719       /* Mark everything else.  */
22720       break;
22721   }
22722
22723   if (die->die_mark == 0)
22724     {
22725       die->die_mark = 1;
22726
22727       /* Now, mark any dies referenced from here.  */
22728       prune_unused_types_walk_attribs (die);
22729     }
22730
22731   die->die_mark = 2;
22732
22733   /* Mark children.  */
22734   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22735 }
22736
22737 /* Increment the string counts on strings referred to from DIE's
22738    attributes.  */
22739
22740 static void
22741 prune_unused_types_update_strings (dw_die_ref die)
22742 {
22743   dw_attr_ref a;
22744   unsigned ix;
22745
22746   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22747     if (AT_class (a) == dw_val_class_str)
22748       {
22749         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22750         s->refcount++;
22751         /* Avoid unnecessarily putting strings that are used less than
22752            twice in the hash table.  */
22753         if (s->refcount
22754             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22755           {
22756             void ** slot;
22757             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22758                                              htab_hash_string (s->str),
22759                                              INSERT);
22760             gcc_assert (*slot == NULL);
22761             *slot = s;
22762           }
22763       }
22764 }
22765
22766 /* Remove from the tree DIE any dies that aren't marked.  */
22767
22768 static void
22769 prune_unused_types_prune (dw_die_ref die)
22770 {
22771   dw_die_ref c;
22772
22773   gcc_assert (die->die_mark);
22774   prune_unused_types_update_strings (die);
22775
22776   if (! die->die_child)
22777     return;
22778
22779   c = die->die_child;
22780   do {
22781     dw_die_ref prev = c;
22782     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22783       if (c == die->die_child)
22784         {
22785           /* No marked children between 'prev' and the end of the list.  */
22786           if (prev == c)
22787             /* No marked children at all.  */
22788             die->die_child = NULL;
22789           else
22790             {
22791               prev->die_sib = c->die_sib;
22792               die->die_child = prev;
22793             }
22794           return;
22795         }
22796
22797     if (c != prev->die_sib)
22798       prev->die_sib = c;
22799     prune_unused_types_prune (c);
22800   } while (c != die->die_child);
22801 }
22802
22803 /* A helper function for dwarf2out_finish called through
22804    htab_traverse.  Clear .debug_str strings that we haven't already
22805    decided to emit.  */
22806
22807 static int
22808 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22809 {
22810   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22811
22812   if (!node->label || !node->refcount)
22813     htab_clear_slot (debug_str_hash, h);
22814
22815   return 1;
22816 }
22817
22818 /* Remove dies representing declarations that we never use.  */
22819
22820 static void
22821 prune_unused_types (void)
22822 {
22823   unsigned int i;
22824   limbo_die_node *node;
22825   comdat_type_node *ctnode;
22826   pubname_ref pub;
22827   dcall_entry *dcall;
22828
22829 #if ENABLE_ASSERT_CHECKING
22830   /* All the marks should already be clear.  */
22831   verify_marks_clear (comp_unit_die ());
22832   for (node = limbo_die_list; node; node = node->next)
22833     verify_marks_clear (node->die);
22834   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22835     verify_marks_clear (ctnode->root_die);
22836 #endif /* ENABLE_ASSERT_CHECKING */
22837
22838   /* Mark types that are used in global variables.  */
22839   premark_types_used_by_global_vars ();
22840
22841   /* Set the mark on nodes that are actually used.  */
22842   prune_unused_types_walk (comp_unit_die ());
22843   for (node = limbo_die_list; node; node = node->next)
22844     prune_unused_types_walk (node->die);
22845   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22846     {
22847       prune_unused_types_walk (ctnode->root_die);
22848       prune_unused_types_mark (ctnode->type_die, 1);
22849     }
22850
22851   /* Also set the mark on nodes referenced from the
22852      pubname_table.  */
22853   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22854     prune_unused_types_mark (pub->die, 1);
22855
22856   /* Mark nodes referenced from the direct call table.  */
22857   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22858     prune_unused_types_mark (dcall->targ_die, 1);
22859
22860   /* Get rid of nodes that aren't marked; and update the string counts.  */
22861   if (debug_str_hash && debug_str_hash_forced)
22862     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22863   else if (debug_str_hash)
22864     htab_empty (debug_str_hash);
22865   prune_unused_types_prune (comp_unit_die ());
22866   for (node = limbo_die_list; node; node = node->next)
22867     prune_unused_types_prune (node->die);
22868   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22869     prune_unused_types_prune (ctnode->root_die);
22870
22871   /* Leave the marks clear.  */
22872   prune_unmark_dies (comp_unit_die ());
22873   for (node = limbo_die_list; node; node = node->next)
22874     prune_unmark_dies (node->die);
22875   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22876     prune_unmark_dies (ctnode->root_die);
22877 }
22878
22879 /* Set the parameter to true if there are any relative pathnames in
22880    the file table.  */
22881 static int
22882 file_table_relative_p (void ** slot, void *param)
22883 {
22884   bool *p = (bool *) param;
22885   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22886   if (!IS_ABSOLUTE_PATH (d->filename))
22887     {
22888       *p = true;
22889       return 0;
22890     }
22891   return 1;
22892 }
22893
22894 /* Routines to manipulate hash table of comdat type units.  */
22895
22896 static hashval_t
22897 htab_ct_hash (const void *of)
22898 {
22899   hashval_t h;
22900   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22901
22902   memcpy (&h, type_node->signature, sizeof (h));
22903   return h;
22904 }
22905
22906 static int
22907 htab_ct_eq (const void *of1, const void *of2)
22908 {
22909   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22910   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22911
22912   return (! memcmp (type_node_1->signature, type_node_2->signature,
22913                     DWARF_TYPE_SIGNATURE_SIZE));
22914 }
22915
22916 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22917    to the location it would have been added, should we know its
22918    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22919    probably improve compactness of debug info, removing equivalent
22920    abbrevs, and hide any differences caused by deferring the
22921    computation of the assembler name, triggered by e.g. PCH.  */
22922
22923 static inline void
22924 move_linkage_attr (dw_die_ref die)
22925 {
22926   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22927   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22928
22929   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22930               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22931
22932   while (--ix > 0)
22933     {
22934       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22935
22936       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22937         break;
22938     }
22939
22940   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22941     {
22942       VEC_pop (dw_attr_node, die->die_attr);
22943       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22944     }
22945 }
22946
22947 /* Helper function for resolve_addr, attempt to resolve
22948    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22949    SYMBOL_REFs refer to variables emitted in the current CU.  */
22950
22951 static int
22952 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22953 {
22954   rtx rtl = *addr;
22955
22956   if (GET_CODE (rtl) == CONST_STRING)
22957     {
22958       size_t len = strlen (XSTR (rtl, 0)) + 1;
22959       tree t = build_string (len, XSTR (rtl, 0));
22960       tree tlen = build_int_cst (NULL_TREE, len - 1);
22961       TREE_TYPE (t)
22962         = build_array_type (char_type_node, build_index_type (tlen));
22963       rtl = lookup_constant_def (t);
22964       if (!rtl || !MEM_P (rtl))
22965         return 1;
22966       rtl = XEXP (rtl, 0);
22967       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22968       *addr = rtl;
22969       return 0;
22970     }
22971
22972   if (GET_CODE (rtl) == SYMBOL_REF
22973       && SYMBOL_REF_DECL (rtl)
22974       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22975     return 1;
22976
22977   if (GET_CODE (rtl) == CONST
22978       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22979     return 1;
22980
22981   return 0;
22982 }
22983
22984 /* Helper function for resolve_addr, handle one location
22985    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22986    the location list couldn't be resolved.  */
22987
22988 static bool
22989 resolve_addr_in_expr (dw_loc_descr_ref loc)
22990 {
22991   for (; loc; loc = loc->dw_loc_next)
22992     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22993          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22994         || (loc->dw_loc_opc == DW_OP_implicit_value
22995             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22996             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22997       return false;
22998     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22999              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23000       {
23001         dw_die_ref ref
23002           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23003         if (ref == NULL)
23004           return false;
23005         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23006         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23007         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23008       }
23009   return true;
23010 }
23011
23012 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23013    an address in .rodata section if the string literal is emitted there,
23014    or remove the containing location list or replace DW_AT_const_value
23015    with DW_AT_location and empty location expression, if it isn't found
23016    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23017    to something that has been emitted in the current CU.  */
23018
23019 static void
23020 resolve_addr (dw_die_ref die)
23021 {
23022   dw_die_ref c;
23023   dw_attr_ref a;
23024   dw_loc_list_ref *curr, *start, loc;
23025   unsigned ix;
23026
23027   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23028     switch (AT_class (a))
23029       {
23030       case dw_val_class_loc_list:
23031         start = curr = AT_loc_list_ptr (a);
23032         loc = *curr;
23033         gcc_assert (loc);
23034         /* The same list can be referenced more than once.  See if we have
23035            already recorded the result from a previous pass.  */
23036         if (loc->replaced)
23037           *curr = loc->dw_loc_next;
23038         else if (!loc->resolved_addr)
23039           {
23040             /* As things stand, we do not expect or allow one die to
23041                reference a suffix of another die's location list chain.
23042                References must be identical or completely separate.
23043                There is therefore no need to cache the result of this
23044                pass on any list other than the first; doing so
23045                would lead to unnecessary writes.  */
23046             while (*curr)
23047               {
23048                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23049                 if (!resolve_addr_in_expr ((*curr)->expr))
23050                   {
23051                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23052                     if (next && (*curr)->ll_symbol)
23053                       {
23054                         gcc_assert (!next->ll_symbol);
23055                         next->ll_symbol = (*curr)->ll_symbol;
23056                       }
23057                     *curr = next;
23058                   }
23059                 else
23060                   curr = &(*curr)->dw_loc_next;
23061               }
23062             if (loc == *start)
23063               loc->resolved_addr = 1;
23064             else
23065               {
23066                 loc->replaced = 1;
23067                 loc->dw_loc_next = *start;
23068               }
23069           }
23070         if (!*start)
23071           {
23072             remove_AT (die, a->dw_attr);
23073             ix--;
23074           }
23075         break;
23076       case dw_val_class_loc:
23077         if (!resolve_addr_in_expr (AT_loc (a)))
23078           {
23079             remove_AT (die, a->dw_attr);
23080             ix--;
23081           }
23082         break;
23083       case dw_val_class_addr:
23084         if (a->dw_attr == DW_AT_const_value
23085             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23086           {
23087             remove_AT (die, a->dw_attr);
23088             ix--;
23089           }
23090         break;
23091       default:
23092         break;
23093       }
23094
23095   FOR_EACH_CHILD (die, c, resolve_addr (c));
23096 }
23097 \f
23098 /* Helper routines for optimize_location_lists.
23099    This pass tries to share identical local lists in .debug_loc
23100    section.  */
23101
23102 /* Iteratively hash operands of LOC opcode.  */
23103
23104 static inline hashval_t
23105 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23106 {
23107   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23108   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23109
23110   switch (loc->dw_loc_opc)
23111     {
23112     case DW_OP_const4u:
23113     case DW_OP_const8u:
23114       if (loc->dtprel)
23115         goto hash_addr;
23116       /* FALLTHRU */
23117     case DW_OP_const1u:
23118     case DW_OP_const1s:
23119     case DW_OP_const2u:
23120     case DW_OP_const2s:
23121     case DW_OP_const4s:
23122     case DW_OP_const8s:
23123     case DW_OP_constu:
23124     case DW_OP_consts:
23125     case DW_OP_pick:
23126     case DW_OP_plus_uconst:
23127     case DW_OP_breg0:
23128     case DW_OP_breg1:
23129     case DW_OP_breg2:
23130     case DW_OP_breg3:
23131     case DW_OP_breg4:
23132     case DW_OP_breg5:
23133     case DW_OP_breg6:
23134     case DW_OP_breg7:
23135     case DW_OP_breg8:
23136     case DW_OP_breg9:
23137     case DW_OP_breg10:
23138     case DW_OP_breg11:
23139     case DW_OP_breg12:
23140     case DW_OP_breg13:
23141     case DW_OP_breg14:
23142     case DW_OP_breg15:
23143     case DW_OP_breg16:
23144     case DW_OP_breg17:
23145     case DW_OP_breg18:
23146     case DW_OP_breg19:
23147     case DW_OP_breg20:
23148     case DW_OP_breg21:
23149     case DW_OP_breg22:
23150     case DW_OP_breg23:
23151     case DW_OP_breg24:
23152     case DW_OP_breg25:
23153     case DW_OP_breg26:
23154     case DW_OP_breg27:
23155     case DW_OP_breg28:
23156     case DW_OP_breg29:
23157     case DW_OP_breg30:
23158     case DW_OP_breg31:
23159     case DW_OP_regx:
23160     case DW_OP_fbreg:
23161     case DW_OP_piece:
23162     case DW_OP_deref_size:
23163     case DW_OP_xderef_size:
23164       hash = iterative_hash_object (val1->v.val_int, hash);
23165       break;
23166     case DW_OP_skip:
23167     case DW_OP_bra:
23168       {
23169         int offset;
23170
23171         gcc_assert (val1->val_class == dw_val_class_loc);
23172         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23173         hash = iterative_hash_object (offset, hash);
23174       }
23175       break;
23176     case DW_OP_implicit_value:
23177       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23178       switch (val2->val_class)
23179         {
23180         case dw_val_class_const:
23181           hash = iterative_hash_object (val2->v.val_int, hash);
23182           break;
23183         case dw_val_class_vec:
23184           {
23185             unsigned int elt_size = val2->v.val_vec.elt_size;
23186             unsigned int len = val2->v.val_vec.length;
23187
23188             hash = iterative_hash_object (elt_size, hash);
23189             hash = iterative_hash_object (len, hash);
23190             hash = iterative_hash (val2->v.val_vec.array,
23191                                    len * elt_size, hash);
23192           }
23193           break;
23194         case dw_val_class_const_double:
23195           hash = iterative_hash_object (val2->v.val_double.low, hash);
23196           hash = iterative_hash_object (val2->v.val_double.high, hash);
23197           break;
23198         case dw_val_class_addr:
23199           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23200           break;
23201         default:
23202           gcc_unreachable ();
23203         }
23204       break;
23205     case DW_OP_bregx:
23206     case DW_OP_bit_piece:
23207       hash = iterative_hash_object (val1->v.val_int, hash);
23208       hash = iterative_hash_object (val2->v.val_int, hash);
23209       break;
23210     case DW_OP_addr:
23211     hash_addr:
23212       if (loc->dtprel)
23213         {
23214           unsigned char dtprel = 0xd1;
23215           hash = iterative_hash_object (dtprel, hash);
23216         }
23217       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23218       break;
23219     case DW_OP_GNU_implicit_pointer:
23220       hash = iterative_hash_object (val2->v.val_int, hash);
23221       break;
23222
23223     default:
23224       /* Other codes have no operands.  */
23225       break;
23226     }
23227   return hash;
23228 }
23229
23230 /* Iteratively hash the whole DWARF location expression LOC.  */
23231
23232 static inline hashval_t
23233 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23234 {
23235   dw_loc_descr_ref l;
23236   bool sizes_computed = false;
23237   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23238   size_of_locs (loc);
23239
23240   for (l = loc; l != NULL; l = l->dw_loc_next)
23241     {
23242       enum dwarf_location_atom opc = l->dw_loc_opc;
23243       hash = iterative_hash_object (opc, hash);
23244       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23245         {
23246           size_of_locs (loc);
23247           sizes_computed = true;
23248         }
23249       hash = hash_loc_operands (l, hash);
23250     }
23251   return hash;
23252 }
23253
23254 /* Compute hash of the whole location list LIST_HEAD.  */
23255
23256 static inline void
23257 hash_loc_list (dw_loc_list_ref list_head)
23258 {
23259   dw_loc_list_ref curr = list_head;
23260   hashval_t hash = 0;
23261
23262   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23263     {
23264       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23265       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23266       if (curr->section)
23267         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23268                                hash);
23269       hash = hash_locs (curr->expr, hash);
23270     }
23271   list_head->hash = hash;
23272 }
23273
23274 /* Return true if X and Y opcodes have the same operands.  */
23275
23276 static inline bool
23277 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23278 {
23279   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23280   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23281   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23282   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23283
23284   switch (x->dw_loc_opc)
23285     {
23286     case DW_OP_const4u:
23287     case DW_OP_const8u:
23288       if (x->dtprel)
23289         goto hash_addr;
23290       /* FALLTHRU */
23291     case DW_OP_const1u:
23292     case DW_OP_const1s:
23293     case DW_OP_const2u:
23294     case DW_OP_const2s:
23295     case DW_OP_const4s:
23296     case DW_OP_const8s:
23297     case DW_OP_constu:
23298     case DW_OP_consts:
23299     case DW_OP_pick:
23300     case DW_OP_plus_uconst:
23301     case DW_OP_breg0:
23302     case DW_OP_breg1:
23303     case DW_OP_breg2:
23304     case DW_OP_breg3:
23305     case DW_OP_breg4:
23306     case DW_OP_breg5:
23307     case DW_OP_breg6:
23308     case DW_OP_breg7:
23309     case DW_OP_breg8:
23310     case DW_OP_breg9:
23311     case DW_OP_breg10:
23312     case DW_OP_breg11:
23313     case DW_OP_breg12:
23314     case DW_OP_breg13:
23315     case DW_OP_breg14:
23316     case DW_OP_breg15:
23317     case DW_OP_breg16:
23318     case DW_OP_breg17:
23319     case DW_OP_breg18:
23320     case DW_OP_breg19:
23321     case DW_OP_breg20:
23322     case DW_OP_breg21:
23323     case DW_OP_breg22:
23324     case DW_OP_breg23:
23325     case DW_OP_breg24:
23326     case DW_OP_breg25:
23327     case DW_OP_breg26:
23328     case DW_OP_breg27:
23329     case DW_OP_breg28:
23330     case DW_OP_breg29:
23331     case DW_OP_breg30:
23332     case DW_OP_breg31:
23333     case DW_OP_regx:
23334     case DW_OP_fbreg:
23335     case DW_OP_piece:
23336     case DW_OP_deref_size:
23337     case DW_OP_xderef_size:
23338       return valx1->v.val_int == valy1->v.val_int;
23339     case DW_OP_skip:
23340     case DW_OP_bra:
23341       gcc_assert (valx1->val_class == dw_val_class_loc
23342                   && valy1->val_class == dw_val_class_loc
23343                   && x->dw_loc_addr == y->dw_loc_addr);
23344       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23345     case DW_OP_implicit_value:
23346       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23347           || valx2->val_class != valy2->val_class)
23348         return false;
23349       switch (valx2->val_class)
23350         {
23351         case dw_val_class_const:
23352           return valx2->v.val_int == valy2->v.val_int;
23353         case dw_val_class_vec:
23354           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23355                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23356                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23357                             valx2->v.val_vec.elt_size
23358                             * valx2->v.val_vec.length) == 0;
23359         case dw_val_class_const_double:
23360           return valx2->v.val_double.low == valy2->v.val_double.low
23361                  && valx2->v.val_double.high == valy2->v.val_double.high;
23362         case dw_val_class_addr:
23363           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23364         default:
23365           gcc_unreachable ();
23366         }
23367     case DW_OP_bregx:
23368     case DW_OP_bit_piece:
23369       return valx1->v.val_int == valy1->v.val_int
23370              && valx2->v.val_int == valy2->v.val_int;
23371     case DW_OP_addr:
23372     hash_addr:
23373       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23374     case DW_OP_GNU_implicit_pointer:
23375       return valx1->val_class == dw_val_class_die_ref
23376              && valx1->val_class == valy1->val_class
23377              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23378              && valx2->v.val_int == valy2->v.val_int;
23379     default:
23380       /* Other codes have no operands.  */
23381       return true;
23382     }
23383 }
23384
23385 /* Return true if DWARF location expressions X and Y are the same.  */
23386
23387 static inline bool
23388 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23389 {
23390   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23391     if (x->dw_loc_opc != y->dw_loc_opc
23392         || x->dtprel != y->dtprel
23393         || !compare_loc_operands (x, y))
23394       break;
23395   return x == NULL && y == NULL;
23396 }
23397
23398 /* Return precomputed hash of location list X.  */
23399
23400 static hashval_t
23401 loc_list_hash (const void *x)
23402 {
23403   return ((const struct dw_loc_list_struct *) x)->hash;
23404 }
23405
23406 /* Return 1 if location lists X and Y are the same.  */
23407
23408 static int
23409 loc_list_eq (const void *x, const void *y)
23410 {
23411   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23412   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23413   if (a == b)
23414     return 1;
23415   if (a->hash != b->hash)
23416     return 0;
23417   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23418     if (strcmp (a->begin, b->begin) != 0
23419         || strcmp (a->end, b->end) != 0
23420         || (a->section == NULL) != (b->section == NULL)
23421         || (a->section && strcmp (a->section, b->section) != 0)
23422         || !compare_locs (a->expr, b->expr))
23423       break;
23424   return a == NULL && b == NULL;
23425 }
23426
23427 /* Recursively optimize location lists referenced from DIE
23428    children and share them whenever possible.  */
23429
23430 static void
23431 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23432 {
23433   dw_die_ref c;
23434   dw_attr_ref a;
23435   unsigned ix;
23436   void **slot;
23437
23438   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23439     if (AT_class (a) == dw_val_class_loc_list)
23440       {
23441         dw_loc_list_ref list = AT_loc_list (a);
23442         /* TODO: perform some optimizations here, before hashing
23443            it and storing into the hash table.  */
23444         hash_loc_list (list);
23445         slot = htab_find_slot_with_hash (htab, list, list->hash,
23446                                          INSERT);
23447         if (*slot == NULL)
23448           *slot = (void *) list;
23449         else
23450           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23451       }
23452
23453   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23454 }
23455
23456 /* Optimize location lists referenced from DIE
23457    children and share them whenever possible.  */
23458
23459 static void
23460 optimize_location_lists (dw_die_ref die)
23461 {
23462   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23463   optimize_location_lists_1 (die, htab);
23464   htab_delete (htab);
23465 }
23466 \f
23467 /* Output stuff that dwarf requires at the end of every file,
23468    and generate the DWARF-2 debugging info.  */
23469
23470 static void
23471 dwarf2out_finish (const char *filename)
23472 {
23473   limbo_die_node *node, *next_node;
23474   comdat_type_node *ctnode;
23475   htab_t comdat_type_table;
23476   unsigned int i;
23477
23478   gen_scheduled_generic_parms_dies ();
23479   gen_remaining_tmpl_value_param_die_attribute ();
23480
23481   /* Add the name for the main input file now.  We delayed this from
23482      dwarf2out_init to avoid complications with PCH.  */
23483   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23484   if (!IS_ABSOLUTE_PATH (filename))
23485     add_comp_dir_attribute (comp_unit_die ());
23486   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23487     {
23488       bool p = false;
23489       htab_traverse (file_table, file_table_relative_p, &p);
23490       if (p)
23491         add_comp_dir_attribute (comp_unit_die ());
23492     }
23493
23494   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23495     {
23496       add_location_or_const_value_attribute (
23497         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23498         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23499         false,
23500         DW_AT_location);
23501     }
23502
23503   /* Traverse the limbo die list, and add parent/child links.  The only
23504      dies without parents that should be here are concrete instances of
23505      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23506      For concrete instances, we can get the parent die from the abstract
23507      instance.  */
23508   for (node = limbo_die_list; node; node = next_node)
23509     {
23510       dw_die_ref die = node->die;
23511       next_node = node->next;
23512
23513       if (die->die_parent == NULL)
23514         {
23515           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23516
23517           if (origin)
23518             add_child_die (origin->die_parent, die);
23519           else if (is_cu_die (die))
23520             ;
23521           else if (seen_error ())
23522             /* It's OK to be confused by errors in the input.  */
23523             add_child_die (comp_unit_die (), die);
23524           else
23525             {
23526               /* In certain situations, the lexical block containing a
23527                  nested function can be optimized away, which results
23528                  in the nested function die being orphaned.  Likewise
23529                  with the return type of that nested function.  Force
23530                  this to be a child of the containing function.
23531
23532                  It may happen that even the containing function got fully
23533                  inlined and optimized out.  In that case we are lost and
23534                  assign the empty child.  This should not be big issue as
23535                  the function is likely unreachable too.  */
23536               tree context = NULL_TREE;
23537
23538               gcc_assert (node->created_for);
23539
23540               if (DECL_P (node->created_for))
23541                 context = DECL_CONTEXT (node->created_for);
23542               else if (TYPE_P (node->created_for))
23543                 context = TYPE_CONTEXT (node->created_for);
23544
23545               gcc_assert (context
23546                           && (TREE_CODE (context) == FUNCTION_DECL
23547                               || TREE_CODE (context) == NAMESPACE_DECL));
23548
23549               origin = lookup_decl_die (context);
23550               if (origin)
23551                 add_child_die (origin, die);
23552               else
23553                 add_child_die (comp_unit_die (), die);
23554             }
23555         }
23556     }
23557
23558   limbo_die_list = NULL;
23559
23560   resolve_addr (comp_unit_die ());
23561
23562   for (node = deferred_asm_name; node; node = node->next)
23563     {
23564       tree decl = node->created_for;
23565       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23566         {
23567           add_linkage_attr (node->die, decl);
23568           move_linkage_attr (node->die);
23569         }
23570     }
23571
23572   deferred_asm_name = NULL;
23573
23574   /* Walk through the list of incomplete types again, trying once more to
23575      emit full debugging info for them.  */
23576   retry_incomplete_types ();
23577
23578   if (flag_eliminate_unused_debug_types)
23579     prune_unused_types ();
23580
23581   /* Generate separate CUs for each of the include files we've seen.
23582      They will go into limbo_die_list.  */
23583   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23584     break_out_includes (comp_unit_die ());
23585
23586   /* Generate separate COMDAT sections for type DIEs. */
23587   if (dwarf_version >= 4)
23588     {
23589       break_out_comdat_types (comp_unit_die ());
23590
23591       /* Each new type_unit DIE was added to the limbo die list when created.
23592          Since these have all been added to comdat_type_list, clear the
23593          limbo die list.  */
23594       limbo_die_list = NULL;
23595
23596       /* For each new comdat type unit, copy declarations for incomplete
23597          types to make the new unit self-contained (i.e., no direct
23598          references to the main compile unit).  */
23599       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23600         copy_decls_for_unworthy_types (ctnode->root_die);
23601       copy_decls_for_unworthy_types (comp_unit_die ());
23602
23603       /* In the process of copying declarations from one unit to another,
23604          we may have left some declarations behind that are no longer
23605          referenced.  Prune them.  */
23606       prune_unused_types ();
23607     }
23608
23609   /* Traverse the DIE's and add add sibling attributes to those DIE's
23610      that have children.  */
23611   add_sibling_attributes (comp_unit_die ());
23612   for (node = limbo_die_list; node; node = node->next)
23613     add_sibling_attributes (node->die);
23614   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23615     add_sibling_attributes (ctnode->root_die);
23616
23617   /* Output a terminator label for the .text section.  */
23618   switch_to_section (text_section);
23619   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23620   if (cold_text_section)
23621     {
23622       switch_to_section (cold_text_section);
23623       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23624     }
23625
23626   /* We can only use the low/high_pc attributes if all of the code was
23627      in .text.  */
23628   if (!have_multiple_function_sections 
23629       || (dwarf_version < 3 && dwarf_strict))
23630     {
23631       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23632       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23633     }
23634
23635   else
23636     {
23637       unsigned fde_idx = 0;
23638       bool range_list_added = false;
23639
23640       /* We need to give .debug_loc and .debug_ranges an appropriate
23641          "base address".  Use zero so that these addresses become
23642          absolute.  Historically, we've emitted the unexpected
23643          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23644          Emit both to give time for other tools to adapt.  */
23645       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23646       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23647
23648       if (text_section_used)
23649         add_ranges_by_labels (comp_unit_die (), text_section_label,
23650                               text_end_label, &range_list_added);
23651       if (cold_text_section_used)
23652         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23653                               cold_end_label, &range_list_added);
23654
23655       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23656         {
23657           dw_fde_ref fde = &fde_table[fde_idx];
23658
23659           if (!fde->in_std_section)
23660             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23661                                   fde->dw_fde_end, &range_list_added);
23662           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23663             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
23664                                   fde->dw_fde_second_end, &range_list_added);
23665         }
23666
23667       if (range_list_added)
23668         add_ranges (NULL);
23669     }
23670
23671   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23672     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23673                     debug_line_section_label);
23674
23675   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23676     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23677
23678   if (have_location_lists)
23679     optimize_location_lists (comp_unit_die ());
23680
23681   /* Output all of the compilation units.  We put the main one last so that
23682      the offsets are available to output_pubnames.  */
23683   for (node = limbo_die_list; node; node = node->next)
23684     output_comp_unit (node->die, 0);
23685
23686   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23687   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23688     {
23689       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23690
23691       /* Don't output duplicate types.  */
23692       if (*slot != HTAB_EMPTY_ENTRY)
23693         continue;
23694
23695       /* Add a pointer to the line table for the main compilation unit
23696          so that the debugger can make sense of DW_AT_decl_file
23697          attributes.  */
23698       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23699         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23700                         debug_line_section_label);
23701
23702       output_comdat_type_unit (ctnode);
23703       *slot = ctnode;
23704     }
23705   htab_delete (comdat_type_table);
23706
23707   /* Output the main compilation unit if non-empty or if .debug_macinfo
23708      will be emitted.  */
23709   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23710
23711   /* Output the abbreviation table.  */
23712   switch_to_section (debug_abbrev_section);
23713   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23714   output_abbrev_section ();
23715
23716   /* Output location list section if necessary.  */
23717   if (have_location_lists)
23718     {
23719       /* Output the location lists info.  */
23720       switch_to_section (debug_loc_section);
23721       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23722                                    DEBUG_LOC_SECTION_LABEL, 0);
23723       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23724       output_location_lists (comp_unit_die ());
23725     }
23726
23727   /* Output public names table if necessary.  */
23728   if (!VEC_empty (pubname_entry, pubname_table))
23729     {
23730       gcc_assert (info_section_emitted);
23731       switch_to_section (debug_pubnames_section);
23732       output_pubnames (pubname_table);
23733     }
23734
23735   /* Output public types table if necessary.  */
23736   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23737      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23738      simply won't look for the section.  */
23739   if (!VEC_empty (pubname_entry, pubtype_table))
23740     {
23741       bool empty = false;
23742       
23743       if (flag_eliminate_unused_debug_types)
23744         {
23745           /* The pubtypes table might be emptied by pruning unused items.  */
23746           unsigned i;
23747           pubname_ref p;
23748           empty = true;
23749           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23750             if (p->die->die_offset != 0)
23751               {
23752                 empty = false;
23753                 break;
23754               }
23755         }
23756       if (!empty)
23757         {
23758           gcc_assert (info_section_emitted);
23759           switch_to_section (debug_pubtypes_section);
23760           output_pubnames (pubtype_table);
23761         }
23762     }
23763
23764   /* Output direct and virtual call tables if necessary.  */
23765   if (!VEC_empty (dcall_entry, dcall_table))
23766     {
23767       switch_to_section (debug_dcall_section);
23768       output_dcall_table ();
23769     }
23770   if (!VEC_empty (vcall_entry, vcall_table))
23771     {
23772       switch_to_section (debug_vcall_section);
23773       output_vcall_table ();
23774     }
23775
23776   /* Output the address range information.  We only put functions in the
23777      arange table, so don't write it out if we don't have any.  */
23778   if (info_section_emitted)
23779     {
23780       unsigned long aranges_length = size_of_aranges ();
23781
23782       /* Empty .debug_aranges would contain just header and
23783          terminating 0,0.  */
23784       if (aranges_length
23785           != (unsigned long) (DWARF_ARANGES_HEADER_SIZE
23786                               + 2 * DWARF2_ADDR_SIZE))
23787         {
23788           switch_to_section (debug_aranges_section);
23789           output_aranges (aranges_length);
23790         }
23791     }
23792
23793   /* Output ranges section if necessary.  */
23794   if (ranges_table_in_use)
23795     {
23796       switch_to_section (debug_ranges_section);
23797       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23798       output_ranges ();
23799     }
23800
23801   /* Output the source line correspondence table.  We must do this
23802      even if there is no line information.  Otherwise, on an empty
23803      translation unit, we will generate a present, but empty,
23804      .debug_info section.  IRIX 6.5 `nm' will then complain when
23805      examining the file.  This is done late so that any filenames
23806      used by the debug_info section are marked as 'used'.  */
23807   switch_to_section (debug_line_section);
23808   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23809   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23810     output_line_info ();
23811
23812   /* Have to end the macro section.  */
23813   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23814     {
23815       switch_to_section (debug_macinfo_section);
23816       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23817       if (!VEC_empty (macinfo_entry, macinfo_table))
23818         output_macinfo ();
23819       dw2_asm_output_data (1, 0, "End compilation unit");
23820     }
23821
23822   /* If we emitted any DW_FORM_strp form attribute, output the string
23823      table too.  */
23824   if (debug_str_hash)
23825     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23826 }
23827
23828 #include "gt-dwarf2out.h"