OSDN Git Service

gcc/
[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_hot_section_label;
307   const char *dw_fde_hot_section_end_label;
308   const char *dw_fde_unlikely_section_label;
309   const char *dw_fde_unlikely_section_end_label;
310   dw_cfi_ref dw_fde_cfi;
311   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
312   HOST_WIDE_INT stack_realignment;
313   unsigned funcdef_number;
314   /* Dynamic realign argument pointer register.  */
315   unsigned int drap_reg;
316   /* Virtual dynamic realign argument pointer register.  */
317   unsigned int vdrap_reg;
318   /* These 3 flags are copied from rtl_data in function.h.  */
319   unsigned all_throwers_are_sibcalls : 1;
320   unsigned uses_eh_lsda : 1;
321   unsigned nothrow : 1;
322   /* Whether we did stack realign in this call frame.  */
323   unsigned stack_realign : 1;
324   /* Whether dynamic realign argument pointer register has been saved.  */
325   unsigned drap_reg_saved: 1;
326   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
327   unsigned in_std_section : 1;
328   /* True iff dw_fde_unlikely_section_label is in text_section or
329      cold_text_section.  */
330   unsigned cold_in_std_section : 1;
331   /* True iff switched sections.  */
332   unsigned dw_fde_switched_sections : 1;
333   /* True iff switching from cold to hot section.  */
334   unsigned dw_fde_switched_cold_to_hot : 1;
335 }
336 dw_fde_node;
337
338 /* Maximum size (in bytes) of an artificially generated label.  */
339 #define MAX_ARTIFICIAL_LABEL_BYTES      30
340
341 /* The size of addresses as they appear in the Dwarf 2 data.
342    Some architectures use word addresses to refer to code locations,
343    but Dwarf 2 info always uses byte addresses.  On such machines,
344    Dwarf 2 addresses need to be larger than the architecture's
345    pointers.  */
346 #ifndef DWARF2_ADDR_SIZE
347 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
348 #endif
349
350 /* The size in bytes of a DWARF field indicating an offset or length
351    relative to a debug info section, specified to be 4 bytes in the
352    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
353    as PTR_SIZE.  */
354
355 #ifndef DWARF_OFFSET_SIZE
356 #define DWARF_OFFSET_SIZE 4
357 #endif
358
359 /* The size in bytes of a DWARF 4 type signature.  */
360
361 #ifndef DWARF_TYPE_SIGNATURE_SIZE
362 #define DWARF_TYPE_SIGNATURE_SIZE 8
363 #endif
364
365 /* According to the (draft) DWARF 3 specification, the initial length
366    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
367    bytes are 0xffffffff, followed by the length stored in the next 8
368    bytes.
369
370    However, the SGI/MIPS ABI uses an initial length which is equal to
371    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
372
373 #ifndef DWARF_INITIAL_LENGTH_SIZE
374 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
375 #endif
376
377 /* Round SIZE up to the nearest BOUNDARY.  */
378 #define DWARF_ROUND(SIZE,BOUNDARY) \
379   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
380
381 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
382 #ifndef DWARF_CIE_DATA_ALIGNMENT
383 #ifdef STACK_GROWS_DOWNWARD
384 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
385 #else
386 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
387 #endif
388 #endif
389
390 /* CIE identifier.  */
391 #if HOST_BITS_PER_WIDE_INT >= 64
392 #define DWARF_CIE_ID \
393   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
394 #else
395 #define DWARF_CIE_ID DW_CIE_ID
396 #endif
397
398 /* A pointer to the base of a table that contains frame description
399    information for each routine.  */
400 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
401
402 /* Number of elements currently allocated for fde_table.  */
403 static GTY(()) unsigned fde_table_allocated;
404
405 /* Number of elements in fde_table currently in use.  */
406 static GTY(()) unsigned fde_table_in_use;
407
408 /* Size (in elements) of increments by which we may expand the
409    fde_table.  */
410 #define FDE_TABLE_INCREMENT 256
411
412 /* Get the current fde_table entry we should use.  */
413
414 static inline dw_fde_ref
415 current_fde (void)
416 {
417   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
418 }
419
420 /* A list of call frame insns for the CIE.  */
421 static GTY(()) dw_cfi_ref cie_cfi_head;
422
423 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
424    attribute that accelerates the lookup of the FDE associated
425    with the subprogram.  This variable holds the table index of the FDE
426    associated with the current function (body) definition.  */
427 static unsigned current_funcdef_fde;
428
429 struct GTY(()) indirect_string_node {
430   const char *str;
431   unsigned int refcount;
432   enum dwarf_form form;
433   char *label;
434 };
435
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
437
438 /* True if the compilation unit has location entries that reference
439    debug strings.  */
440 static GTY(()) bool debug_str_hash_forced = false;
441
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
444
445 /* True if the compilation unit places functions in more than one section.  */
446 static GTY(()) bool have_multiple_function_sections = false;
447
448 /* Whether the default text and cold text sections have been used at all.  */
449
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
452
453 /* The default cold text section.  */
454 static GTY(()) section *cold_text_section;
455
456 /* Forward declarations for functions defined in this file.  */
457
458 static char *stripattributes (const char *);
459 static const char *dwarf_cfi_name (unsigned);
460 static dw_cfi_ref new_cfi (void);
461 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
462 static void add_fde_cfi (const char *, dw_cfi_ref);
463 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
464 static void lookup_cfa (dw_cfa_location *);
465 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
466 static void initial_return_save (rtx);
467 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
468                                           HOST_WIDE_INT);
469 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
470 static void output_cfi_directive (dw_cfi_ref);
471 static void output_call_frame_info (int);
472 static void dwarf2out_note_section_used (void);
473 static bool clobbers_queued_reg_save (const_rtx);
474 static void dwarf2out_frame_debug_expr (rtx, const char *);
475
476 /* Support for complex CFA locations.  */
477 static void output_cfa_loc (dw_cfi_ref, int);
478 static void output_cfa_loc_raw (dw_cfi_ref);
479 static void get_cfa_from_loc_descr (dw_cfa_location *,
480                                     struct dw_loc_descr_struct *);
481 static struct dw_loc_descr_struct *build_cfa_loc
482   (dw_cfa_location *, HOST_WIDE_INT);
483 static struct dw_loc_descr_struct *build_cfa_aligned_loc
484   (HOST_WIDE_INT, HOST_WIDE_INT);
485 static void def_cfa_1 (const char *, dw_cfa_location *);
486 static struct dw_loc_descr_struct *mem_loc_descriptor
487   (rtx, enum machine_mode mode, enum var_init_status);
488
489 /* How to start an assembler comment.  */
490 #ifndef ASM_COMMENT_START
491 #define ASM_COMMENT_START ";#"
492 #endif
493
494 /* Data and reference forms for relocatable data.  */
495 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
496 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
497
498 #ifndef DEBUG_FRAME_SECTION
499 #define DEBUG_FRAME_SECTION     ".debug_frame"
500 #endif
501
502 #ifndef FUNC_BEGIN_LABEL
503 #define FUNC_BEGIN_LABEL        "LFB"
504 #endif
505
506 #ifndef FUNC_END_LABEL
507 #define FUNC_END_LABEL          "LFE"
508 #endif
509
510 #ifndef PROLOGUE_END_LABEL
511 #define PROLOGUE_END_LABEL      "LPE"
512 #endif
513
514 #ifndef EPILOGUE_BEGIN_LABEL
515 #define EPILOGUE_BEGIN_LABEL    "LEB"
516 #endif
517
518 #ifndef FRAME_BEGIN_LABEL
519 #define FRAME_BEGIN_LABEL       "Lframe"
520 #endif
521 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
522 #define CIE_END_LABEL           "LECIE"
523 #define FDE_LABEL               "LSFDE"
524 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
525 #define FDE_END_LABEL           "LEFDE"
526 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
527 #define LINE_NUMBER_END_LABEL   "LELT"
528 #define LN_PROLOG_AS_LABEL      "LASLTP"
529 #define LN_PROLOG_END_LABEL     "LELTP"
530 #define DIE_LABEL_PREFIX        "DW"
531
532 /* The DWARF 2 CFA column which tracks the return address.  Normally this
533    is the column for PC, or the first column after all of the hard
534    registers.  */
535 #ifndef DWARF_FRAME_RETURN_COLUMN
536 #ifdef PC_REGNUM
537 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
538 #else
539 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
540 #endif
541 #endif
542
543 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
544    default, we just provide columns for all registers.  */
545 #ifndef DWARF_FRAME_REGNUM
546 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
547 #endif
548 \f
549 /* Match the base name of a file to the base name of a compilation unit. */
550
551 static int
552 matches_main_base (const char *path)
553 {
554   /* Cache the last query. */
555   static const char *last_path = NULL;
556   static int last_match = 0;
557   if (path != last_path)
558     {
559       const char *base;
560       int length = base_of_path (path, &base);
561       last_path = path;
562       last_match = (length == main_input_baselength
563                     && memcmp (base, main_input_basename, length) == 0);
564     }
565   return last_match;
566 }
567
568 #ifdef DEBUG_DEBUG_STRUCT
569
570 static int
571 dump_struct_debug (tree type, enum debug_info_usage usage,
572                    enum debug_struct_file criterion, int generic,
573                    int matches, int result)
574 {
575   /* Find the type name. */
576   tree type_decl = TYPE_STUB_DECL (type);
577   tree t = type_decl;
578   const char *name = 0;
579   if (TREE_CODE (t) == TYPE_DECL)
580     t = DECL_NAME (t);
581   if (t)
582     name = IDENTIFIER_POINTER (t);
583
584   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
585            criterion,
586            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
587            matches ? "bas" : "hdr",
588            generic ? "gen" : "ord",
589            usage == DINFO_USAGE_DFN ? ";" :
590              usage == DINFO_USAGE_DIR_USE ? "." : "*",
591            result,
592            (void*) type_decl, name);
593   return result;
594 }
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596   dump_struct_debug (type, usage, criterion, generic, matches, result)
597
598 #else
599
600 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
601   (result)
602
603 #endif
604
605 static bool
606 should_emit_struct_debug (tree type, enum debug_info_usage usage)
607 {
608   enum debug_struct_file criterion;
609   tree type_decl;
610   bool generic = lang_hooks.types.generic_p (type);
611
612   if (generic)
613     criterion = debug_struct_generic[usage];
614   else
615     criterion = debug_struct_ordinary[usage];
616
617   if (criterion == DINFO_STRUCT_FILE_NONE)
618     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
619   if (criterion == DINFO_STRUCT_FILE_ANY)
620     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
621
622   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
623
624   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
625     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
626
627   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
628     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
629   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
630 }
631 \f
632 /* Hook used by __throw.  */
633
634 rtx
635 expand_builtin_dwarf_sp_column (void)
636 {
637   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
638   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
639 }
640
641 /* Return a pointer to a copy of the section string name S with all
642    attributes stripped off, and an asterisk prepended (for assemble_name).  */
643
644 static inline char *
645 stripattributes (const char *s)
646 {
647   char *stripped = XNEWVEC (char, strlen (s) + 2);
648   char *p = stripped;
649
650   *p++ = '*';
651
652   while (*s && *s != ',')
653     *p++ = *s++;
654
655   *p = '\0';
656   return stripped;
657 }
658
659 /* MEM is a memory reference for the register size table, each element of
660    which has mode MODE.  Initialize column C as a return address column.  */
661
662 static void
663 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
664 {
665   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
666   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
667   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
668 }
669
670 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
671
672 static inline HOST_WIDE_INT
673 div_data_align (HOST_WIDE_INT off)
674 {
675   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
676   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
677   return r;
678 }
679
680 /* Return true if we need a signed version of a given opcode
681    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
682
683 static inline bool
684 need_data_align_sf_opcode (HOST_WIDE_INT off)
685 {
686   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
687 }
688
689 /* Generate code to initialize the register size table.  */
690
691 void
692 expand_builtin_init_dwarf_reg_sizes (tree address)
693 {
694   unsigned int i;
695   enum machine_mode mode = TYPE_MODE (char_type_node);
696   rtx addr = expand_normal (address);
697   rtx mem = gen_rtx_MEM (BLKmode, addr);
698   bool wrote_return_column = false;
699
700   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
701     {
702       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
703
704       if (rnum < DWARF_FRAME_REGISTERS)
705         {
706           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
707           enum machine_mode save_mode = reg_raw_mode[i];
708           HOST_WIDE_INT size;
709
710           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
711             save_mode = choose_hard_reg_mode (i, 1, true);
712           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
713             {
714               if (save_mode == VOIDmode)
715                 continue;
716               wrote_return_column = true;
717             }
718           size = GET_MODE_SIZE (save_mode);
719           if (offset < 0)
720             continue;
721
722           emit_move_insn (adjust_address (mem, mode, offset),
723                           gen_int_mode (size, mode));
724         }
725     }
726
727   if (!wrote_return_column)
728     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
729
730 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
731   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
732 #endif
733
734   targetm.init_dwarf_reg_sizes_extra (address);
735 }
736
737 /* Convert a DWARF call frame info. operation to its string name */
738
739 static const char *
740 dwarf_cfi_name (unsigned int cfi_opc)
741 {
742   switch (cfi_opc)
743     {
744     case DW_CFA_advance_loc:
745       return "DW_CFA_advance_loc";
746     case DW_CFA_offset:
747       return "DW_CFA_offset";
748     case DW_CFA_restore:
749       return "DW_CFA_restore";
750     case DW_CFA_nop:
751       return "DW_CFA_nop";
752     case DW_CFA_set_loc:
753       return "DW_CFA_set_loc";
754     case DW_CFA_advance_loc1:
755       return "DW_CFA_advance_loc1";
756     case DW_CFA_advance_loc2:
757       return "DW_CFA_advance_loc2";
758     case DW_CFA_advance_loc4:
759       return "DW_CFA_advance_loc4";
760     case DW_CFA_offset_extended:
761       return "DW_CFA_offset_extended";
762     case DW_CFA_restore_extended:
763       return "DW_CFA_restore_extended";
764     case DW_CFA_undefined:
765       return "DW_CFA_undefined";
766     case DW_CFA_same_value:
767       return "DW_CFA_same_value";
768     case DW_CFA_register:
769       return "DW_CFA_register";
770     case DW_CFA_remember_state:
771       return "DW_CFA_remember_state";
772     case DW_CFA_restore_state:
773       return "DW_CFA_restore_state";
774     case DW_CFA_def_cfa:
775       return "DW_CFA_def_cfa";
776     case DW_CFA_def_cfa_register:
777       return "DW_CFA_def_cfa_register";
778     case DW_CFA_def_cfa_offset:
779       return "DW_CFA_def_cfa_offset";
780
781     /* DWARF 3 */
782     case DW_CFA_def_cfa_expression:
783       return "DW_CFA_def_cfa_expression";
784     case DW_CFA_expression:
785       return "DW_CFA_expression";
786     case DW_CFA_offset_extended_sf:
787       return "DW_CFA_offset_extended_sf";
788     case DW_CFA_def_cfa_sf:
789       return "DW_CFA_def_cfa_sf";
790     case DW_CFA_def_cfa_offset_sf:
791       return "DW_CFA_def_cfa_offset_sf";
792
793     /* SGI/MIPS specific */
794     case DW_CFA_MIPS_advance_loc8:
795       return "DW_CFA_MIPS_advance_loc8";
796
797     /* GNU extensions */
798     case DW_CFA_GNU_window_save:
799       return "DW_CFA_GNU_window_save";
800     case DW_CFA_GNU_args_size:
801       return "DW_CFA_GNU_args_size";
802     case DW_CFA_GNU_negative_offset_extended:
803       return "DW_CFA_GNU_negative_offset_extended";
804
805     default:
806       return "DW_CFA_<unknown>";
807     }
808 }
809
810 /* Return a pointer to a newly allocated Call Frame Instruction.  */
811
812 static inline dw_cfi_ref
813 new_cfi (void)
814 {
815   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
816
817   cfi->dw_cfi_next = NULL;
818   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
819   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
820
821   return cfi;
822 }
823
824 /* Add a Call Frame Instruction to list of instructions.  */
825
826 static inline void
827 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
828 {
829   dw_cfi_ref *p;
830   dw_fde_ref fde = current_fde ();
831
832   /* When DRAP is used, CFA is defined with an expression.  Redefine
833      CFA may lead to a different CFA value.   */
834   /* ??? Of course, this heuristic fails when we're annotating epilogues,
835      because of course we'll always want to redefine the CFA back to the
836      stack pointer on the way out.  Where should we move this check?  */
837   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
838     switch (cfi->dw_cfi_opc)
839       {
840         case DW_CFA_def_cfa_register:
841         case DW_CFA_def_cfa_offset:
842         case DW_CFA_def_cfa_offset_sf:
843         case DW_CFA_def_cfa:
844         case DW_CFA_def_cfa_sf:
845           gcc_unreachable ();
846
847         default:
848           break;
849       }
850
851   /* Find the end of the chain.  */
852   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
853     ;
854
855   *p = cfi;
856 }
857
858 /* Generate a new label for the CFI info to refer to.  FORCE is true
859    if a label needs to be output even when using .cfi_* directives.  */
860
861 char *
862 dwarf2out_cfi_label (bool force)
863 {
864   static char label[20];
865
866   if (!force && dwarf2out_do_cfi_asm ())
867     {
868       /* In this case, we will be emitting the asm directive instead of
869          the label, so just return a placeholder to keep the rest of the
870          interfaces happy.  */
871       strcpy (label, "<do not output>");
872     }
873   else
874     {
875       int num = dwarf2out_cfi_label_num++;
876       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
877       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
878     }
879
880   return label;
881 }
882
883 /* True if remember_state should be emitted before following CFI directive.  */
884 static bool emit_cfa_remember;
885
886 /* True if any CFI directives were emitted at the current insn.  */
887 static bool any_cfis_emitted;
888
889 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
890    or to the CIE if LABEL is NULL.  */
891
892 static void
893 add_fde_cfi (const char *label, dw_cfi_ref cfi)
894 {
895   dw_cfi_ref *list_head;
896
897   if (emit_cfa_remember)
898     {
899       dw_cfi_ref cfi_remember;
900
901       /* Emit the state save.  */
902       emit_cfa_remember = false;
903       cfi_remember = new_cfi ();
904       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
905       add_fde_cfi (label, cfi_remember);
906     }
907
908   list_head = &cie_cfi_head;
909
910   if (dwarf2out_do_cfi_asm ())
911     {
912       if (label)
913         {
914           dw_fde_ref fde = current_fde ();
915
916           gcc_assert (fde != NULL);
917
918           /* We still have to add the cfi to the list so that lookup_cfa
919              works later on.  When -g2 and above we even need to force
920              emitting of CFI labels and add to list a DW_CFA_set_loc for
921              convert_cfa_to_fb_loc_list purposes.  If we're generating
922              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
923              convert_cfa_to_fb_loc_list.  */
924           if (dwarf_version == 2
925               && debug_info_level > DINFO_LEVEL_TERSE
926               && (write_symbols == DWARF2_DEBUG
927                   || write_symbols == VMS_AND_DWARF2_DEBUG))
928             {
929               switch (cfi->dw_cfi_opc)
930                 {
931                 case DW_CFA_def_cfa_offset:
932                 case DW_CFA_def_cfa_offset_sf:
933                 case DW_CFA_def_cfa_register:
934                 case DW_CFA_def_cfa:
935                 case DW_CFA_def_cfa_sf:
936                 case DW_CFA_def_cfa_expression:
937                 case DW_CFA_restore_state:
938                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
939                     label = dwarf2out_cfi_label (true);
940
941                   if (fde->dw_fde_current_label == NULL
942                       || strcmp (label, fde->dw_fde_current_label) != 0)
943                     {
944                       dw_cfi_ref xcfi;
945
946                       label = xstrdup (label);
947
948                       /* Set the location counter to the new label.  */
949                       xcfi = new_cfi ();
950                       /* It doesn't metter whether DW_CFA_set_loc
951                          or DW_CFA_advance_loc4 is added here, those aren't
952                          emitted into assembly, only looked up by
953                          convert_cfa_to_fb_loc_list.  */
954                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
955                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
956                       add_cfi (&fde->dw_fde_cfi, xcfi);
957                       fde->dw_fde_current_label = label;
958                     }
959                   break;
960                 default:
961                   break;
962                 }
963             }
964
965           output_cfi_directive (cfi);
966
967           list_head = &fde->dw_fde_cfi;
968           any_cfis_emitted = true;
969         }
970       /* ??? If this is a CFI for the CIE, we don't emit.  This
971          assumes that the standard CIE contents that the assembler
972          uses matches the standard CIE contents that the compiler
973          uses.  This is probably a bad assumption.  I'm not quite
974          sure how to address this for now.  */
975     }
976   else if (label)
977     {
978       dw_fde_ref fde = current_fde ();
979
980       gcc_assert (fde != NULL);
981
982       if (*label == 0)
983         label = dwarf2out_cfi_label (false);
984
985       if (fde->dw_fde_current_label == NULL
986           || strcmp (label, fde->dw_fde_current_label) != 0)
987         {
988           dw_cfi_ref xcfi;
989
990           label = xstrdup (label);
991
992           /* Set the location counter to the new label.  */
993           xcfi = new_cfi ();
994           /* If we have a current label, advance from there, otherwise
995              set the location directly using set_loc.  */
996           xcfi->dw_cfi_opc = fde->dw_fde_current_label
997                              ? DW_CFA_advance_loc4
998                              : DW_CFA_set_loc;
999           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
1000           add_cfi (&fde->dw_fde_cfi, xcfi);
1001
1002           fde->dw_fde_current_label = label;
1003         }
1004
1005       list_head = &fde->dw_fde_cfi;
1006       any_cfis_emitted = true;
1007     }
1008
1009   add_cfi (list_head, cfi);
1010 }
1011
1012 /* Subroutine of lookup_cfa.  */
1013
1014 static void
1015 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1016 {
1017   switch (cfi->dw_cfi_opc)
1018     {
1019     case DW_CFA_def_cfa_offset:
1020     case DW_CFA_def_cfa_offset_sf:
1021       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1022       break;
1023     case DW_CFA_def_cfa_register:
1024       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1025       break;
1026     case DW_CFA_def_cfa:
1027     case DW_CFA_def_cfa_sf:
1028       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1029       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1030       break;
1031     case DW_CFA_def_cfa_expression:
1032       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1033       break;
1034
1035     case DW_CFA_remember_state:
1036       gcc_assert (!remember->in_use);
1037       *remember = *loc;
1038       remember->in_use = 1;
1039       break;
1040     case DW_CFA_restore_state:
1041       gcc_assert (remember->in_use);
1042       *loc = *remember;
1043       remember->in_use = 0;
1044       break;
1045
1046     default:
1047       break;
1048     }
1049 }
1050
1051 /* Find the previous value for the CFA.  */
1052
1053 static void
1054 lookup_cfa (dw_cfa_location *loc)
1055 {
1056   dw_cfi_ref cfi;
1057   dw_fde_ref fde;
1058   dw_cfa_location remember;
1059
1060   memset (loc, 0, sizeof (*loc));
1061   loc->reg = INVALID_REGNUM;
1062   remember = *loc;
1063
1064   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1065     lookup_cfa_1 (cfi, loc, &remember);
1066
1067   fde = current_fde ();
1068   if (fde)
1069     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1070       lookup_cfa_1 (cfi, loc, &remember);
1071 }
1072
1073 /* The current rule for calculating the DWARF2 canonical frame address.  */
1074 static dw_cfa_location cfa;
1075
1076 /* The register used for saving registers to the stack, and its offset
1077    from the CFA.  */
1078 static dw_cfa_location cfa_store;
1079
1080 /* The current save location around an epilogue.  */
1081 static dw_cfa_location cfa_remember;
1082
1083 /* The running total of the size of arguments pushed onto the stack.  */
1084 static HOST_WIDE_INT args_size;
1085
1086 /* The last args_size we actually output.  */
1087 static HOST_WIDE_INT old_args_size;
1088
1089 /* Entry point to update the canonical frame address (CFA).
1090    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1091    calculated from REG+OFFSET.  */
1092
1093 void
1094 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1095 {
1096   dw_cfa_location loc;
1097   loc.indirect = 0;
1098   loc.base_offset = 0;
1099   loc.reg = reg;
1100   loc.offset = offset;
1101   def_cfa_1 (label, &loc);
1102 }
1103
1104 /* Determine if two dw_cfa_location structures define the same data.  */
1105
1106 static bool
1107 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1108 {
1109   return (loc1->reg == loc2->reg
1110           && loc1->offset == loc2->offset
1111           && loc1->indirect == loc2->indirect
1112           && (loc1->indirect == 0
1113               || loc1->base_offset == loc2->base_offset));
1114 }
1115
1116 /* This routine does the actual work.  The CFA is now calculated from
1117    the dw_cfa_location structure.  */
1118
1119 static void
1120 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1121 {
1122   dw_cfi_ref cfi;
1123   dw_cfa_location old_cfa, loc;
1124
1125   cfa = *loc_p;
1126   loc = *loc_p;
1127
1128   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1129     cfa_store.offset = loc.offset;
1130
1131   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1132   lookup_cfa (&old_cfa);
1133
1134   /* If nothing changed, no need to issue any call frame instructions.  */
1135   if (cfa_equal_p (&loc, &old_cfa))
1136     return;
1137
1138   cfi = new_cfi ();
1139
1140   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1141     {
1142       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1143          the CFA register did not change but the offset did.  The data
1144          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1145          in the assembler via the .cfi_def_cfa_offset directive.  */
1146       if (loc.offset < 0)
1147         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1148       else
1149         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1150       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1151     }
1152
1153 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1154   else if (loc.offset == old_cfa.offset
1155            && old_cfa.reg != INVALID_REGNUM
1156            && !loc.indirect
1157            && !old_cfa.indirect)
1158     {
1159       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1160          indicating the CFA register has changed to <register> but the
1161          offset has not changed.  */
1162       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1163       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1164     }
1165 #endif
1166
1167   else if (loc.indirect == 0)
1168     {
1169       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1170          indicating the CFA register has changed to <register> with
1171          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1172          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1173          directive.  */
1174       if (loc.offset < 0)
1175         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1176       else
1177         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1178       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1179       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1180     }
1181   else
1182     {
1183       /* Construct a DW_CFA_def_cfa_expression instruction to
1184          calculate the CFA using a full location expression since no
1185          register-offset pair is available.  */
1186       struct dw_loc_descr_struct *loc_list;
1187
1188       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1189       loc_list = build_cfa_loc (&loc, 0);
1190       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1191     }
1192
1193   add_fde_cfi (label, cfi);
1194 }
1195
1196 /* Add the CFI for saving a register.  REG is the CFA column number.
1197    LABEL is passed to add_fde_cfi.
1198    If SREG is -1, the register is saved at OFFSET from the CFA;
1199    otherwise it is saved in SREG.  */
1200
1201 static void
1202 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1203 {
1204   dw_cfi_ref cfi = new_cfi ();
1205   dw_fde_ref fde = current_fde ();
1206
1207   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1208
1209   /* When stack is aligned, store REG using DW_CFA_expression with
1210      FP.  */
1211   if (fde
1212       && fde->stack_realign
1213       && sreg == INVALID_REGNUM)
1214     {
1215       cfi->dw_cfi_opc = DW_CFA_expression;
1216       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1217       cfi->dw_cfi_oprnd2.dw_cfi_loc
1218         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1219     }
1220   else if (sreg == INVALID_REGNUM)
1221     {
1222       if (need_data_align_sf_opcode (offset))
1223         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1224       else if (reg & ~0x3f)
1225         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1226       else
1227         cfi->dw_cfi_opc = DW_CFA_offset;
1228       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1229     }
1230   else if (sreg == reg)
1231     cfi->dw_cfi_opc = DW_CFA_same_value;
1232   else
1233     {
1234       cfi->dw_cfi_opc = DW_CFA_register;
1235       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1236     }
1237
1238   add_fde_cfi (label, cfi);
1239 }
1240
1241 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1242    This CFI tells the unwinder that it needs to restore the window registers
1243    from the previous frame's window save area.
1244
1245    ??? Perhaps we should note in the CIE where windows are saved (instead of
1246    assuming 0(cfa)) and what registers are in the window.  */
1247
1248 void
1249 dwarf2out_window_save (const char *label)
1250 {
1251   dw_cfi_ref cfi = new_cfi ();
1252
1253   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1254   add_fde_cfi (label, cfi);
1255 }
1256
1257 /* Entry point for saving a register to the stack.  REG is the GCC register
1258    number.  LABEL and OFFSET are passed to reg_save.  */
1259
1260 void
1261 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1262 {
1263   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1264 }
1265
1266 /* Entry point for saving the return address in the stack.
1267    LABEL and OFFSET are passed to reg_save.  */
1268
1269 void
1270 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1271 {
1272   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1273 }
1274
1275 /* Entry point for saving the return address in a register.
1276    LABEL and SREG are passed to reg_save.  */
1277
1278 void
1279 dwarf2out_return_reg (const char *label, unsigned int sreg)
1280 {
1281   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1282 }
1283
1284 /* Record the initial position of the return address.  RTL is
1285    INCOMING_RETURN_ADDR_RTX.  */
1286
1287 static void
1288 initial_return_save (rtx rtl)
1289 {
1290   unsigned int reg = INVALID_REGNUM;
1291   HOST_WIDE_INT offset = 0;
1292
1293   switch (GET_CODE (rtl))
1294     {
1295     case REG:
1296       /* RA is in a register.  */
1297       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1298       break;
1299
1300     case MEM:
1301       /* RA is on the stack.  */
1302       rtl = XEXP (rtl, 0);
1303       switch (GET_CODE (rtl))
1304         {
1305         case REG:
1306           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1307           offset = 0;
1308           break;
1309
1310         case PLUS:
1311           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1312           offset = INTVAL (XEXP (rtl, 1));
1313           break;
1314
1315         case MINUS:
1316           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1317           offset = -INTVAL (XEXP (rtl, 1));
1318           break;
1319
1320         default:
1321           gcc_unreachable ();
1322         }
1323
1324       break;
1325
1326     case PLUS:
1327       /* The return address is at some offset from any value we can
1328          actually load.  For instance, on the SPARC it is in %i7+8. Just
1329          ignore the offset for now; it doesn't matter for unwinding frames.  */
1330       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1331       initial_return_save (XEXP (rtl, 0));
1332       return;
1333
1334     default:
1335       gcc_unreachable ();
1336     }
1337
1338   if (reg != DWARF_FRAME_RETURN_COLUMN)
1339     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1340 }
1341
1342 /* Given a SET, calculate the amount of stack adjustment it
1343    contains.  */
1344
1345 static HOST_WIDE_INT
1346 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1347                      HOST_WIDE_INT cur_offset)
1348 {
1349   const_rtx src = SET_SRC (pattern);
1350   const_rtx dest = SET_DEST (pattern);
1351   HOST_WIDE_INT offset = 0;
1352   enum rtx_code code;
1353
1354   if (dest == stack_pointer_rtx)
1355     {
1356       code = GET_CODE (src);
1357
1358       /* Assume (set (reg sp) (reg whatever)) sets args_size
1359          level to 0.  */
1360       if (code == REG && src != stack_pointer_rtx)
1361         {
1362           offset = -cur_args_size;
1363 #ifndef STACK_GROWS_DOWNWARD
1364           offset = -offset;
1365 #endif
1366           return offset - cur_offset;
1367         }
1368
1369       if (! (code == PLUS || code == MINUS)
1370           || XEXP (src, 0) != stack_pointer_rtx
1371           || !CONST_INT_P (XEXP (src, 1)))
1372         return 0;
1373
1374       /* (set (reg sp) (plus (reg sp) (const_int))) */
1375       offset = INTVAL (XEXP (src, 1));
1376       if (code == PLUS)
1377         offset = -offset;
1378       return offset;
1379     }
1380
1381   if (MEM_P (src) && !MEM_P (dest))
1382     dest = src;
1383   if (MEM_P (dest))
1384     {
1385       /* (set (mem (pre_dec (reg sp))) (foo)) */
1386       src = XEXP (dest, 0);
1387       code = GET_CODE (src);
1388
1389       switch (code)
1390         {
1391         case PRE_MODIFY:
1392         case POST_MODIFY:
1393           if (XEXP (src, 0) == stack_pointer_rtx)
1394             {
1395               rtx val = XEXP (XEXP (src, 1), 1);
1396               /* We handle only adjustments by constant amount.  */
1397               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1398                           && CONST_INT_P (val));
1399               offset = -INTVAL (val);
1400               break;
1401             }
1402           return 0;
1403
1404         case PRE_DEC:
1405         case POST_DEC:
1406           if (XEXP (src, 0) == stack_pointer_rtx)
1407             {
1408               offset = GET_MODE_SIZE (GET_MODE (dest));
1409               break;
1410             }
1411           return 0;
1412
1413         case PRE_INC:
1414         case POST_INC:
1415           if (XEXP (src, 0) == stack_pointer_rtx)
1416             {
1417               offset = -GET_MODE_SIZE (GET_MODE (dest));
1418               break;
1419             }
1420           return 0;
1421
1422         default:
1423           return 0;
1424         }
1425     }
1426   else
1427     return 0;
1428
1429   return offset;
1430 }
1431
1432 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1433    indexed by INSN_UID.  */
1434
1435 static HOST_WIDE_INT *barrier_args_size;
1436
1437 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1438
1439 static HOST_WIDE_INT
1440 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1441                              VEC (rtx, heap) **next)
1442 {
1443   HOST_WIDE_INT offset = 0;
1444   int i;
1445
1446   if (! RTX_FRAME_RELATED_P (insn))
1447     {
1448       if (prologue_epilogue_contains (insn))
1449         /* Nothing */;
1450       else if (GET_CODE (PATTERN (insn)) == SET)
1451         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1452       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1453                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1454         {
1455           /* There may be stack adjustments inside compound insns.  Search
1456              for them.  */
1457           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1458             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1459               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1460                                              cur_args_size, offset);
1461         }
1462     }
1463   else
1464     {
1465       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1466
1467       if (expr)
1468         {
1469           expr = XEXP (expr, 0);
1470           if (GET_CODE (expr) == PARALLEL
1471               || GET_CODE (expr) == SEQUENCE)
1472             for (i = 1; i < XVECLEN (expr, 0); i++)
1473               {
1474                 rtx elem = XVECEXP (expr, 0, i);
1475
1476                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1477                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1478               }
1479         }
1480     }
1481
1482 #ifndef STACK_GROWS_DOWNWARD
1483   offset = -offset;
1484 #endif
1485
1486   cur_args_size += offset;
1487   if (cur_args_size < 0)
1488     cur_args_size = 0;
1489
1490   if (JUMP_P (insn))
1491     {
1492       rtx dest = JUMP_LABEL (insn);
1493
1494       if (dest)
1495         {
1496           if (barrier_args_size [INSN_UID (dest)] < 0)
1497             {
1498               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1499               VEC_safe_push (rtx, heap, *next, dest);
1500             }
1501         }
1502     }
1503
1504   return cur_args_size;
1505 }
1506
1507 /* Walk the whole function and compute args_size on BARRIERs.  */
1508
1509 static void
1510 compute_barrier_args_size (void)
1511 {
1512   int max_uid = get_max_uid (), i;
1513   rtx insn;
1514   VEC (rtx, heap) *worklist, *next, *tmp;
1515
1516   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1517   for (i = 0; i < max_uid; i++)
1518     barrier_args_size[i] = -1;
1519
1520   worklist = VEC_alloc (rtx, heap, 20);
1521   next = VEC_alloc (rtx, heap, 20);
1522   insn = get_insns ();
1523   barrier_args_size[INSN_UID (insn)] = 0;
1524   VEC_quick_push (rtx, worklist, insn);
1525   for (;;)
1526     {
1527       while (!VEC_empty (rtx, worklist))
1528         {
1529           rtx prev, body, first_insn;
1530           HOST_WIDE_INT cur_args_size;
1531
1532           first_insn = insn = VEC_pop (rtx, worklist);
1533           cur_args_size = barrier_args_size[INSN_UID (insn)];
1534           prev = prev_nonnote_insn (insn);
1535           if (prev && BARRIER_P (prev))
1536             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1537
1538           for (; insn; insn = NEXT_INSN (insn))
1539             {
1540               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1541                 continue;
1542               if (BARRIER_P (insn))
1543                 break;
1544
1545               if (LABEL_P (insn))
1546                 {
1547                   if (insn == first_insn)
1548                     continue;
1549                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1550                     {
1551                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1552                       continue;
1553                     }
1554                   else
1555                     {
1556                       /* The insns starting with this label have been
1557                          already scanned or are in the worklist.  */
1558                       break;
1559                     }
1560                 }
1561
1562               body = PATTERN (insn);
1563               if (GET_CODE (body) == SEQUENCE)
1564                 {
1565                   HOST_WIDE_INT dest_args_size = cur_args_size;
1566                   for (i = 1; i < XVECLEN (body, 0); i++)
1567                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1568                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1569                       dest_args_size
1570                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1571                                                        dest_args_size, &next);
1572                     else
1573                       cur_args_size
1574                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1575                                                        cur_args_size, &next);
1576
1577                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1578                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1579                                                  dest_args_size, &next);
1580                   else
1581                     cur_args_size
1582                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1583                                                      cur_args_size, &next);
1584                 }
1585               else
1586                 cur_args_size
1587                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1588             }
1589         }
1590
1591       if (VEC_empty (rtx, next))
1592         break;
1593
1594       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1595       tmp = next;
1596       next = worklist;
1597       worklist = tmp;
1598       VEC_truncate (rtx, next, 0);
1599     }
1600
1601   VEC_free (rtx, heap, worklist);
1602   VEC_free (rtx, heap, next);
1603 }
1604
1605 /* Add a CFI to update the running total of the size of arguments
1606    pushed onto the stack.  */
1607
1608 static void
1609 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1610 {
1611   dw_cfi_ref cfi;
1612
1613   if (size == old_args_size)
1614     return;
1615
1616   old_args_size = size;
1617
1618   cfi = new_cfi ();
1619   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1620   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1621   add_fde_cfi (label, cfi);
1622 }
1623
1624 /* Record a stack adjustment of OFFSET bytes.  */
1625
1626 static void
1627 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1628 {
1629   if (cfa.reg == STACK_POINTER_REGNUM)
1630     cfa.offset += offset;
1631
1632   if (cfa_store.reg == STACK_POINTER_REGNUM)
1633     cfa_store.offset += offset;
1634
1635   if (ACCUMULATE_OUTGOING_ARGS)
1636     return;
1637
1638 #ifndef STACK_GROWS_DOWNWARD
1639   offset = -offset;
1640 #endif
1641
1642   args_size += offset;
1643   if (args_size < 0)
1644     args_size = 0;
1645
1646   def_cfa_1 (label, &cfa);
1647   if (flag_asynchronous_unwind_tables)
1648     dwarf2out_args_size (label, args_size);
1649 }
1650
1651 /* Check INSN to see if it looks like a push or a stack adjustment, and
1652    make a note of it if it does.  EH uses this information to find out
1653    how much extra space it needs to pop off the stack.  */
1654
1655 static void
1656 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1657 {
1658   HOST_WIDE_INT offset;
1659   const char *label;
1660   int i;
1661
1662   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1663      with this function.  Proper support would require all frame-related
1664      insns to be marked, and to be able to handle saving state around
1665      epilogues textually in the middle of the function.  */
1666   if (prologue_epilogue_contains (insn))
1667     return;
1668
1669   /* If INSN is an instruction from target of an annulled branch, the
1670      effects are for the target only and so current argument size
1671      shouldn't change at all.  */
1672   if (final_sequence
1673       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1674       && INSN_FROM_TARGET_P (insn))
1675     return;
1676
1677   /* If only calls can throw, and we have a frame pointer,
1678      save up adjustments until we see the CALL_INSN.  */
1679   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1680     {
1681       if (CALL_P (insn) && !after_p)
1682         {
1683           /* Extract the size of the args from the CALL rtx itself.  */
1684           insn = PATTERN (insn);
1685           if (GET_CODE (insn) == PARALLEL)
1686             insn = XVECEXP (insn, 0, 0);
1687           if (GET_CODE (insn) == SET)
1688             insn = SET_SRC (insn);
1689           gcc_assert (GET_CODE (insn) == CALL);
1690           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1691         }
1692       return;
1693     }
1694
1695   if (CALL_P (insn) && !after_p)
1696     {
1697       if (!flag_asynchronous_unwind_tables)
1698         dwarf2out_args_size ("", args_size);
1699       return;
1700     }
1701   else if (BARRIER_P (insn))
1702     {
1703       /* Don't call compute_barrier_args_size () if the only
1704          BARRIER is at the end of function.  */
1705       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1706         compute_barrier_args_size ();
1707       if (barrier_args_size == NULL)
1708         offset = 0;
1709       else
1710         {
1711           offset = barrier_args_size[INSN_UID (insn)];
1712           if (offset < 0)
1713             offset = 0;
1714         }
1715
1716       offset -= args_size;
1717 #ifndef STACK_GROWS_DOWNWARD
1718       offset = -offset;
1719 #endif
1720     }
1721   else if (GET_CODE (PATTERN (insn)) == SET)
1722     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1723   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1724            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1725     {
1726       /* There may be stack adjustments inside compound insns.  Search
1727          for them.  */
1728       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1729         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1730           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1731                                          args_size, offset);
1732     }
1733   else
1734     return;
1735
1736   if (offset == 0)
1737     return;
1738
1739   label = dwarf2out_cfi_label (false);
1740   dwarf2out_stack_adjust (offset, label);
1741 }
1742
1743 /* We delay emitting a register save until either (a) we reach the end
1744    of the prologue or (b) the register is clobbered.  This clusters
1745    register saves so that there are fewer pc advances.  */
1746
1747 struct GTY(()) queued_reg_save {
1748   struct queued_reg_save *next;
1749   rtx reg;
1750   HOST_WIDE_INT cfa_offset;
1751   rtx saved_reg;
1752 };
1753
1754 static GTY(()) struct queued_reg_save *queued_reg_saves;
1755
1756 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1757 struct GTY(()) reg_saved_in_data {
1758   rtx orig_reg;
1759   rtx saved_in_reg;
1760 };
1761
1762 /* A list of registers saved in other registers.
1763    The list intentionally has a small maximum capacity of 4; if your
1764    port needs more than that, you might consider implementing a
1765    more efficient data structure.  */
1766 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1767 static GTY(()) size_t num_regs_saved_in_regs;
1768
1769 static const char *last_reg_save_label;
1770
1771 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1772    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1773
1774 static void
1775 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1776 {
1777   struct queued_reg_save *q;
1778
1779   /* Duplicates waste space, but it's also necessary to remove them
1780      for correctness, since the queue gets output in reverse
1781      order.  */
1782   for (q = queued_reg_saves; q != NULL; q = q->next)
1783     if (REGNO (q->reg) == REGNO (reg))
1784       break;
1785
1786   if (q == NULL)
1787     {
1788       q = ggc_alloc_queued_reg_save ();
1789       q->next = queued_reg_saves;
1790       queued_reg_saves = q;
1791     }
1792
1793   q->reg = reg;
1794   q->cfa_offset = offset;
1795   q->saved_reg = sreg;
1796
1797   last_reg_save_label = label;
1798 }
1799
1800 /* Output all the entries in QUEUED_REG_SAVES.  */
1801
1802 void
1803 dwarf2out_flush_queued_reg_saves (void)
1804 {
1805   struct queued_reg_save *q;
1806
1807   for (q = queued_reg_saves; q; q = q->next)
1808     {
1809       size_t i;
1810       unsigned int reg, sreg;
1811
1812       for (i = 0; i < num_regs_saved_in_regs; i++)
1813         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1814           break;
1815       if (q->saved_reg && i == num_regs_saved_in_regs)
1816         {
1817           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1818           num_regs_saved_in_regs++;
1819         }
1820       if (i != num_regs_saved_in_regs)
1821         {
1822           regs_saved_in_regs[i].orig_reg = q->reg;
1823           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1824         }
1825
1826       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1827       if (q->saved_reg)
1828         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1829       else
1830         sreg = INVALID_REGNUM;
1831       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1832     }
1833
1834   queued_reg_saves = NULL;
1835   last_reg_save_label = NULL;
1836 }
1837
1838 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1839    location for?  Or, does it clobber a register which we've previously
1840    said that some other register is saved in, and for which we now
1841    have a new location for?  */
1842
1843 static bool
1844 clobbers_queued_reg_save (const_rtx insn)
1845 {
1846   struct queued_reg_save *q;
1847
1848   for (q = queued_reg_saves; q; q = q->next)
1849     {
1850       size_t i;
1851       if (modified_in_p (q->reg, insn))
1852         return true;
1853       for (i = 0; i < num_regs_saved_in_regs; i++)
1854         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1855             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1856           return true;
1857     }
1858
1859   return false;
1860 }
1861
1862 /* Entry point for saving the first register into the second.  */
1863
1864 void
1865 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1866 {
1867   size_t i;
1868   unsigned int regno, sregno;
1869
1870   for (i = 0; i < num_regs_saved_in_regs; i++)
1871     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1872       break;
1873   if (i == num_regs_saved_in_regs)
1874     {
1875       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1876       num_regs_saved_in_regs++;
1877     }
1878   regs_saved_in_regs[i].orig_reg = reg;
1879   regs_saved_in_regs[i].saved_in_reg = sreg;
1880
1881   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1882   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1883   reg_save (label, regno, sregno, 0);
1884 }
1885
1886 /* What register, if any, is currently saved in REG?  */
1887
1888 static rtx
1889 reg_saved_in (rtx reg)
1890 {
1891   unsigned int regn = REGNO (reg);
1892   size_t i;
1893   struct queued_reg_save *q;
1894
1895   for (q = queued_reg_saves; q; q = q->next)
1896     if (q->saved_reg && regn == REGNO (q->saved_reg))
1897       return q->reg;
1898
1899   for (i = 0; i < num_regs_saved_in_regs; i++)
1900     if (regs_saved_in_regs[i].saved_in_reg
1901         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1902       return regs_saved_in_regs[i].orig_reg;
1903
1904   return NULL_RTX;
1905 }
1906
1907
1908 /* A temporary register holding an integral value used in adjusting SP
1909    or setting up the store_reg.  The "offset" field holds the integer
1910    value, not an offset.  */
1911 static dw_cfa_location cfa_temp;
1912
1913 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1914
1915 static void
1916 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1917 {
1918   memset (&cfa, 0, sizeof (cfa));
1919
1920   switch (GET_CODE (pat))
1921     {
1922     case PLUS:
1923       cfa.reg = REGNO (XEXP (pat, 0));
1924       cfa.offset = INTVAL (XEXP (pat, 1));
1925       break;
1926
1927     case REG:
1928       cfa.reg = REGNO (pat);
1929       break;
1930
1931     case MEM:
1932       cfa.indirect = 1;
1933       pat = XEXP (pat, 0);
1934       if (GET_CODE (pat) == PLUS)
1935         {
1936           cfa.base_offset = INTVAL (XEXP (pat, 1));
1937           pat = XEXP (pat, 0);
1938         }
1939       cfa.reg = REGNO (pat);
1940       break;
1941
1942     default:
1943       /* Recurse and define an expression.  */
1944       gcc_unreachable ();
1945     }
1946
1947   def_cfa_1 (label, &cfa);
1948 }
1949
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1951
1952 static void
1953 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1954 {
1955   rtx src, dest;
1956
1957   gcc_assert (GET_CODE (pat) == SET);
1958   dest = XEXP (pat, 0);
1959   src = XEXP (pat, 1);
1960
1961   switch (GET_CODE (src))
1962     {
1963     case PLUS:
1964       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1965       cfa.offset -= INTVAL (XEXP (src, 1));
1966       break;
1967
1968     case REG:
1969         break;
1970
1971     default:
1972         gcc_unreachable ();
1973     }
1974
1975   cfa.reg = REGNO (dest);
1976   gcc_assert (cfa.indirect == 0);
1977
1978   def_cfa_1 (label, &cfa);
1979 }
1980
1981 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1982
1983 static void
1984 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1985 {
1986   HOST_WIDE_INT offset;
1987   rtx src, addr, span;
1988
1989   src = XEXP (set, 1);
1990   addr = XEXP (set, 0);
1991   gcc_assert (MEM_P (addr));
1992   addr = XEXP (addr, 0);
1993
1994   /* As documented, only consider extremely simple addresses.  */
1995   switch (GET_CODE (addr))
1996     {
1997     case REG:
1998       gcc_assert (REGNO (addr) == cfa.reg);
1999       offset = -cfa.offset;
2000       break;
2001     case PLUS:
2002       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2003       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2004       break;
2005     default:
2006       gcc_unreachable ();
2007     }
2008
2009   span = targetm.dwarf_register_span (src);
2010
2011   /* ??? We'd like to use queue_reg_save, but we need to come up with
2012      a different flushing heuristic for epilogues.  */
2013   if (!span)
2014     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2015   else
2016     {
2017       /* We have a PARALLEL describing where the contents of SRC live.
2018          Queue register saves for each piece of the PARALLEL.  */
2019       int par_index;
2020       int limit;
2021       HOST_WIDE_INT span_offset = offset;
2022
2023       gcc_assert (GET_CODE (span) == PARALLEL);
2024
2025       limit = XVECLEN (span, 0);
2026       for (par_index = 0; par_index < limit; par_index++)
2027         {
2028           rtx elem = XVECEXP (span, 0, par_index);
2029
2030           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2031                     INVALID_REGNUM, span_offset);
2032           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2033         }
2034     }
2035 }
2036
2037 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2038
2039 static void
2040 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2041 {
2042   rtx src, dest;
2043   unsigned sregno, dregno;
2044
2045   src = XEXP (set, 1);
2046   dest = XEXP (set, 0);
2047
2048   if (src == pc_rtx)
2049     sregno = DWARF_FRAME_RETURN_COLUMN;
2050   else
2051     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2052
2053   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2054
2055   /* ??? We'd like to use queue_reg_save, but we need to come up with
2056      a different flushing heuristic for epilogues.  */
2057   reg_save (label, sregno, dregno, 0);
2058 }
2059
2060 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2061
2062 static void
2063 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2064 {
2065   rtx src, dest, span;
2066   dw_cfi_ref cfi = new_cfi ();
2067
2068   dest = SET_DEST (set);
2069   src = SET_SRC (set);
2070
2071   gcc_assert (REG_P (src));
2072   gcc_assert (MEM_P (dest));
2073
2074   span = targetm.dwarf_register_span (src);
2075   gcc_assert (!span);
2076
2077   cfi->dw_cfi_opc = DW_CFA_expression;
2078   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2079   cfi->dw_cfi_oprnd2.dw_cfi_loc
2080     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2081                           VAR_INIT_STATUS_INITIALIZED);
2082
2083   /* ??? We'd like to use queue_reg_save, were the interface different,
2084      and, as above, we could manage flushing for epilogues.  */
2085   add_fde_cfi (label, cfi);
2086 }
2087
2088 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2089
2090 static void
2091 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2092 {
2093   dw_cfi_ref cfi = new_cfi ();
2094   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2095
2096   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2097   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2098
2099   add_fde_cfi (label, cfi);
2100 }
2101
2102 /* Record call frame debugging information for an expression EXPR,
2103    which either sets SP or FP (adjusting how we calculate the frame
2104    address) or saves a register to the stack or another register.
2105    LABEL indicates the address of EXPR.
2106
2107    This function encodes a state machine mapping rtxes to actions on
2108    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2109    users need not read the source code.
2110
2111   The High-Level Picture
2112
2113   Changes in the register we use to calculate the CFA: Currently we
2114   assume that if you copy the CFA register into another register, we
2115   should take the other one as the new CFA register; this seems to
2116   work pretty well.  If it's wrong for some target, it's simple
2117   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2118
2119   Changes in the register we use for saving registers to the stack:
2120   This is usually SP, but not always.  Again, we deduce that if you
2121   copy SP into another register (and SP is not the CFA register),
2122   then the new register is the one we will be using for register
2123   saves.  This also seems to work.
2124
2125   Register saves: There's not much guesswork about this one; if
2126   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2127   register save, and the register used to calculate the destination
2128   had better be the one we think we're using for this purpose.
2129   It's also assumed that a copy from a call-saved register to another
2130   register is saving that register if RTX_FRAME_RELATED_P is set on
2131   that instruction.  If the copy is from a call-saved register to
2132   the *same* register, that means that the register is now the same
2133   value as in the caller.
2134
2135   Except: If the register being saved is the CFA register, and the
2136   offset is nonzero, we are saving the CFA, so we assume we have to
2137   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2138   the intent is to save the value of SP from the previous frame.
2139
2140   In addition, if a register has previously been saved to a different
2141   register,
2142
2143   Invariants / Summaries of Rules
2144
2145   cfa          current rule for calculating the CFA.  It usually
2146                consists of a register and an offset.
2147   cfa_store    register used by prologue code to save things to the stack
2148                cfa_store.offset is the offset from the value of
2149                cfa_store.reg to the actual CFA
2150   cfa_temp     register holding an integral value.  cfa_temp.offset
2151                stores the value, which will be used to adjust the
2152                stack pointer.  cfa_temp is also used like cfa_store,
2153                to track stores to the stack via fp or a temp reg.
2154
2155   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2156                with cfa.reg as the first operand changes the cfa.reg and its
2157                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2158                cfa_temp.offset.
2159
2160   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2161                expression yielding a constant.  This sets cfa_temp.reg
2162                and cfa_temp.offset.
2163
2164   Rule 5:      Create a new register cfa_store used to save items to the
2165                stack.
2166
2167   Rules 10-14: Save a register to the stack.  Define offset as the
2168                difference of the original location and cfa_store's
2169                location (or cfa_temp's location if cfa_temp is used).
2170
2171   Rules 16-20: If AND operation happens on sp in prologue, we assume
2172                stack is realigned.  We will use a group of DW_OP_XXX
2173                expressions to represent the location of the stored
2174                register instead of CFA+offset.
2175
2176   The Rules
2177
2178   "{a,b}" indicates a choice of a xor b.
2179   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2180
2181   Rule 1:
2182   (set <reg1> <reg2>:cfa.reg)
2183   effects: cfa.reg = <reg1>
2184            cfa.offset unchanged
2185            cfa_temp.reg = <reg1>
2186            cfa_temp.offset = cfa.offset
2187
2188   Rule 2:
2189   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2190                               {<const_int>,<reg>:cfa_temp.reg}))
2191   effects: cfa.reg = sp if fp used
2192            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2193            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2194              if cfa_store.reg==sp
2195
2196   Rule 3:
2197   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2198   effects: cfa.reg = fp
2199            cfa_offset += +/- <const_int>
2200
2201   Rule 4:
2202   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2203   constraints: <reg1> != fp
2204                <reg1> != sp
2205   effects: cfa.reg = <reg1>
2206            cfa_temp.reg = <reg1>
2207            cfa_temp.offset = cfa.offset
2208
2209   Rule 5:
2210   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2211   constraints: <reg1> != fp
2212                <reg1> != sp
2213   effects: cfa_store.reg = <reg1>
2214            cfa_store.offset = cfa.offset - cfa_temp.offset
2215
2216   Rule 6:
2217   (set <reg> <const_int>)
2218   effects: cfa_temp.reg = <reg>
2219            cfa_temp.offset = <const_int>
2220
2221   Rule 7:
2222   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2223   effects: cfa_temp.reg = <reg1>
2224            cfa_temp.offset |= <const_int>
2225
2226   Rule 8:
2227   (set <reg> (high <exp>))
2228   effects: none
2229
2230   Rule 9:
2231   (set <reg> (lo_sum <exp> <const_int>))
2232   effects: cfa_temp.reg = <reg>
2233            cfa_temp.offset = <const_int>
2234
2235   Rule 10:
2236   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2237   effects: cfa_store.offset -= <const_int>
2238            cfa.offset = cfa_store.offset if cfa.reg == sp
2239            cfa.reg = sp
2240            cfa.base_offset = -cfa_store.offset
2241
2242   Rule 11:
2243   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2244   effects: cfa_store.offset += -/+ mode_size(mem)
2245            cfa.offset = cfa_store.offset if cfa.reg == sp
2246            cfa.reg = sp
2247            cfa.base_offset = -cfa_store.offset
2248
2249   Rule 12:
2250   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2251
2252        <reg2>)
2253   effects: cfa.reg = <reg1>
2254            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2255
2256   Rule 13:
2257   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2258   effects: cfa.reg = <reg1>
2259            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2260
2261   Rule 14:
2262   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2263   effects: cfa.reg = <reg1>
2264            cfa.base_offset = -cfa_temp.offset
2265            cfa_temp.offset -= mode_size(mem)
2266
2267   Rule 15:
2268   (set <reg> {unspec, unspec_volatile})
2269   effects: target-dependent
2270
2271   Rule 16:
2272   (set sp (and: sp <const_int>))
2273   constraints: cfa_store.reg == sp
2274   effects: current_fde.stack_realign = 1
2275            cfa_store.offset = 0
2276            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2277
2278   Rule 17:
2279   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2280   effects: cfa_store.offset += -/+ mode_size(mem)
2281
2282   Rule 18:
2283   (set (mem ({pre_inc, pre_dec} sp)) fp)
2284   constraints: fde->stack_realign == 1
2285   effects: cfa_store.offset = 0
2286            cfa.reg != HARD_FRAME_POINTER_REGNUM
2287
2288   Rule 19:
2289   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2290   constraints: fde->stack_realign == 1
2291                && cfa.offset == 0
2292                && cfa.indirect == 0
2293                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2294   effects: Use DW_CFA_def_cfa_expression to define cfa
2295            cfa.reg == fde->drap_reg  */
2296
2297 static void
2298 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2299 {
2300   rtx src, dest, span;
2301   HOST_WIDE_INT offset;
2302   dw_fde_ref fde;
2303
2304   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2305      the PARALLEL independently. The first element is always processed if
2306      it is a SET. This is for backward compatibility.   Other elements
2307      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2308      flag is set in them.  */
2309   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2310     {
2311       int par_index;
2312       int limit = XVECLEN (expr, 0);
2313       rtx elem;
2314
2315       /* PARALLELs have strict read-modify-write semantics, so we
2316          ought to evaluate every rvalue before changing any lvalue.
2317          It's cumbersome to do that in general, but there's an
2318          easy approximation that is enough for all current users:
2319          handle register saves before register assignments.  */
2320       if (GET_CODE (expr) == PARALLEL)
2321         for (par_index = 0; par_index < limit; par_index++)
2322           {
2323             elem = XVECEXP (expr, 0, par_index);
2324             if (GET_CODE (elem) == SET
2325                 && MEM_P (SET_DEST (elem))
2326                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2327               dwarf2out_frame_debug_expr (elem, label);
2328           }
2329
2330       for (par_index = 0; par_index < limit; par_index++)
2331         {
2332           elem = XVECEXP (expr, 0, par_index);
2333           if (GET_CODE (elem) == SET
2334               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2335               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2336             dwarf2out_frame_debug_expr (elem, label);
2337           else if (GET_CODE (elem) == SET
2338                    && par_index != 0
2339                    && !RTX_FRAME_RELATED_P (elem))
2340             {
2341               /* Stack adjustment combining might combine some post-prologue
2342                  stack adjustment into a prologue stack adjustment.  */
2343               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2344
2345               if (offset != 0)
2346                 dwarf2out_stack_adjust (offset, label);
2347             }
2348         }
2349       return;
2350     }
2351
2352   gcc_assert (GET_CODE (expr) == SET);
2353
2354   src = SET_SRC (expr);
2355   dest = SET_DEST (expr);
2356
2357   if (REG_P (src))
2358     {
2359       rtx rsi = reg_saved_in (src);
2360       if (rsi)
2361         src = rsi;
2362     }
2363
2364   fde = current_fde ();
2365
2366   switch (GET_CODE (dest))
2367     {
2368     case REG:
2369       switch (GET_CODE (src))
2370         {
2371           /* Setting FP from SP.  */
2372         case REG:
2373           if (cfa.reg == (unsigned) REGNO (src))
2374             {
2375               /* Rule 1 */
2376               /* Update the CFA rule wrt SP or FP.  Make sure src is
2377                  relative to the current CFA register.
2378
2379                  We used to require that dest be either SP or FP, but the
2380                  ARM copies SP to a temporary register, and from there to
2381                  FP.  So we just rely on the backends to only set
2382                  RTX_FRAME_RELATED_P on appropriate insns.  */
2383               cfa.reg = REGNO (dest);
2384               cfa_temp.reg = cfa.reg;
2385               cfa_temp.offset = cfa.offset;
2386             }
2387           else
2388             {
2389               /* Saving a register in a register.  */
2390               gcc_assert (!fixed_regs [REGNO (dest)]
2391                           /* For the SPARC and its register window.  */
2392                           || (DWARF_FRAME_REGNUM (REGNO (src))
2393                               == DWARF_FRAME_RETURN_COLUMN));
2394
2395               /* After stack is aligned, we can only save SP in FP
2396                  if drap register is used.  In this case, we have
2397                  to restore stack pointer with the CFA value and we
2398                  don't generate this DWARF information.  */
2399               if (fde
2400                   && fde->stack_realign
2401                   && REGNO (src) == STACK_POINTER_REGNUM)
2402                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2403                             && fde->drap_reg != INVALID_REGNUM
2404                             && cfa.reg != REGNO (src));
2405               else
2406                 queue_reg_save (label, src, dest, 0);
2407             }
2408           break;
2409
2410         case PLUS:
2411         case MINUS:
2412         case LO_SUM:
2413           if (dest == stack_pointer_rtx)
2414             {
2415               /* Rule 2 */
2416               /* Adjusting SP.  */
2417               switch (GET_CODE (XEXP (src, 1)))
2418                 {
2419                 case CONST_INT:
2420                   offset = INTVAL (XEXP (src, 1));
2421                   break;
2422                 case REG:
2423                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2424                               == cfa_temp.reg);
2425                   offset = cfa_temp.offset;
2426                   break;
2427                 default:
2428                   gcc_unreachable ();
2429                 }
2430
2431               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2432                 {
2433                   /* Restoring SP from FP in the epilogue.  */
2434                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2435                   cfa.reg = STACK_POINTER_REGNUM;
2436                 }
2437               else if (GET_CODE (src) == LO_SUM)
2438                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2439                 ;
2440               else
2441                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2442
2443               if (GET_CODE (src) != MINUS)
2444                 offset = -offset;
2445               if (cfa.reg == STACK_POINTER_REGNUM)
2446                 cfa.offset += offset;
2447               if (cfa_store.reg == STACK_POINTER_REGNUM)
2448                 cfa_store.offset += offset;
2449             }
2450           else if (dest == hard_frame_pointer_rtx)
2451             {
2452               /* Rule 3 */
2453               /* Either setting the FP from an offset of the SP,
2454                  or adjusting the FP */
2455               gcc_assert (frame_pointer_needed);
2456
2457               gcc_assert (REG_P (XEXP (src, 0))
2458                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2459                           && CONST_INT_P (XEXP (src, 1)));
2460               offset = INTVAL (XEXP (src, 1));
2461               if (GET_CODE (src) != MINUS)
2462                 offset = -offset;
2463               cfa.offset += offset;
2464               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2465             }
2466           else
2467             {
2468               gcc_assert (GET_CODE (src) != MINUS);
2469
2470               /* Rule 4 */
2471               if (REG_P (XEXP (src, 0))
2472                   && REGNO (XEXP (src, 0)) == cfa.reg
2473                   && CONST_INT_P (XEXP (src, 1)))
2474                 {
2475                   /* Setting a temporary CFA register that will be copied
2476                      into the FP later on.  */
2477                   offset = - INTVAL (XEXP (src, 1));
2478                   cfa.offset += offset;
2479                   cfa.reg = REGNO (dest);
2480                   /* Or used to save regs to the stack.  */
2481                   cfa_temp.reg = cfa.reg;
2482                   cfa_temp.offset = cfa.offset;
2483                 }
2484
2485               /* Rule 5 */
2486               else if (REG_P (XEXP (src, 0))
2487                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2488                        && XEXP (src, 1) == stack_pointer_rtx)
2489                 {
2490                   /* Setting a scratch register that we will use instead
2491                      of SP for saving registers to the stack.  */
2492                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2493                   cfa_store.reg = REGNO (dest);
2494                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2495                 }
2496
2497               /* Rule 9 */
2498               else if (GET_CODE (src) == LO_SUM
2499                        && CONST_INT_P (XEXP (src, 1)))
2500                 {
2501                   cfa_temp.reg = REGNO (dest);
2502                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2503                 }
2504               else
2505                 gcc_unreachable ();
2506             }
2507           break;
2508
2509           /* Rule 6 */
2510         case CONST_INT:
2511           cfa_temp.reg = REGNO (dest);
2512           cfa_temp.offset = INTVAL (src);
2513           break;
2514
2515           /* Rule 7 */
2516         case IOR:
2517           gcc_assert (REG_P (XEXP (src, 0))
2518                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2519                       && CONST_INT_P (XEXP (src, 1)));
2520
2521           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2522             cfa_temp.reg = REGNO (dest);
2523           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2524           break;
2525
2526           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2527              which will fill in all of the bits.  */
2528           /* Rule 8 */
2529         case HIGH:
2530           break;
2531
2532           /* Rule 15 */
2533         case UNSPEC:
2534         case UNSPEC_VOLATILE:
2535           gcc_assert (targetm.dwarf_handle_frame_unspec);
2536           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2537           return;
2538
2539           /* Rule 16 */
2540         case AND:
2541           /* If this AND operation happens on stack pointer in prologue,
2542              we assume the stack is realigned and we extract the
2543              alignment.  */
2544           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2545             {
2546               /* We interpret reg_save differently with stack_realign set.
2547                  Thus we must flush whatever we have queued first.  */
2548               dwarf2out_flush_queued_reg_saves ();
2549
2550               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2551               fde->stack_realign = 1;
2552               fde->stack_realignment = INTVAL (XEXP (src, 1));
2553               cfa_store.offset = 0;
2554
2555               if (cfa.reg != STACK_POINTER_REGNUM
2556                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2557                 fde->drap_reg = cfa.reg;
2558             }
2559           return;
2560
2561         default:
2562           gcc_unreachable ();
2563         }
2564
2565       def_cfa_1 (label, &cfa);
2566       break;
2567
2568     case MEM:
2569
2570       /* Saving a register to the stack.  Make sure dest is relative to the
2571          CFA register.  */
2572       switch (GET_CODE (XEXP (dest, 0)))
2573         {
2574           /* Rule 10 */
2575           /* With a push.  */
2576         case PRE_MODIFY:
2577           /* We can't handle variable size modifications.  */
2578           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2579                       == CONST_INT);
2580           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2581
2582           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2583                       && cfa_store.reg == STACK_POINTER_REGNUM);
2584
2585           cfa_store.offset += offset;
2586           if (cfa.reg == STACK_POINTER_REGNUM)
2587             cfa.offset = cfa_store.offset;
2588
2589           offset = -cfa_store.offset;
2590           break;
2591
2592           /* Rule 11 */
2593         case PRE_INC:
2594         case PRE_DEC:
2595           offset = GET_MODE_SIZE (GET_MODE (dest));
2596           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2597             offset = -offset;
2598
2599           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2600                        == STACK_POINTER_REGNUM)
2601                       && cfa_store.reg == STACK_POINTER_REGNUM);
2602
2603           cfa_store.offset += offset;
2604
2605           /* Rule 18: If stack is aligned, we will use FP as a
2606              reference to represent the address of the stored
2607              regiser.  */
2608           if (fde
2609               && fde->stack_realign
2610               && src == hard_frame_pointer_rtx)
2611             {
2612               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2613               cfa_store.offset = 0;
2614             }
2615
2616           if (cfa.reg == STACK_POINTER_REGNUM)
2617             cfa.offset = cfa_store.offset;
2618
2619           offset = -cfa_store.offset;
2620           break;
2621
2622           /* Rule 12 */
2623           /* With an offset.  */
2624         case PLUS:
2625         case MINUS:
2626         case LO_SUM:
2627           {
2628             int regno;
2629
2630             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2631                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2632             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2633             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2634               offset = -offset;
2635
2636             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2637
2638             if (cfa.reg == (unsigned) regno)
2639               offset -= cfa.offset;
2640             else if (cfa_store.reg == (unsigned) regno)
2641               offset -= cfa_store.offset;
2642             else
2643               {
2644                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2645                 offset -= cfa_temp.offset;
2646               }
2647           }
2648           break;
2649
2650           /* Rule 13 */
2651           /* Without an offset.  */
2652         case REG:
2653           {
2654             int regno = REGNO (XEXP (dest, 0));
2655
2656             if (cfa.reg == (unsigned) regno)
2657               offset = -cfa.offset;
2658             else if (cfa_store.reg == (unsigned) regno)
2659               offset = -cfa_store.offset;
2660             else
2661               {
2662                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2663                 offset = -cfa_temp.offset;
2664               }
2665           }
2666           break;
2667
2668           /* Rule 14 */
2669         case POST_INC:
2670           gcc_assert (cfa_temp.reg
2671                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2672           offset = -cfa_temp.offset;
2673           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2674           break;
2675
2676         default:
2677           gcc_unreachable ();
2678         }
2679
2680         /* Rule 17 */
2681         /* If the source operand of this MEM operation is not a
2682            register, basically the source is return address.  Here
2683            we only care how much stack grew and we don't save it.  */
2684       if (!REG_P (src))
2685         break;
2686
2687       if (REGNO (src) != STACK_POINTER_REGNUM
2688           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2689           && (unsigned) REGNO (src) == cfa.reg)
2690         {
2691           /* We're storing the current CFA reg into the stack.  */
2692
2693           if (cfa.offset == 0)
2694             {
2695               /* Rule 19 */
2696               /* If stack is aligned, putting CFA reg into stack means
2697                  we can no longer use reg + offset to represent CFA.
2698                  Here we use DW_CFA_def_cfa_expression instead.  The
2699                  result of this expression equals to the original CFA
2700                  value.  */
2701               if (fde
2702                   && fde->stack_realign
2703                   && cfa.indirect == 0
2704                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2705                 {
2706                   dw_cfa_location cfa_exp;
2707
2708                   gcc_assert (fde->drap_reg == cfa.reg);
2709
2710                   cfa_exp.indirect = 1;
2711                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2712                   cfa_exp.base_offset = offset;
2713                   cfa_exp.offset = 0;
2714
2715                   fde->drap_reg_saved = 1;
2716
2717                   def_cfa_1 (label, &cfa_exp);
2718                   break;
2719                 }
2720
2721               /* If the source register is exactly the CFA, assume
2722                  we're saving SP like any other register; this happens
2723                  on the ARM.  */
2724               def_cfa_1 (label, &cfa);
2725               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2726               break;
2727             }
2728           else
2729             {
2730               /* Otherwise, we'll need to look in the stack to
2731                  calculate the CFA.  */
2732               rtx x = XEXP (dest, 0);
2733
2734               if (!REG_P (x))
2735                 x = XEXP (x, 0);
2736               gcc_assert (REG_P (x));
2737
2738               cfa.reg = REGNO (x);
2739               cfa.base_offset = offset;
2740               cfa.indirect = 1;
2741               def_cfa_1 (label, &cfa);
2742               break;
2743             }
2744         }
2745
2746       def_cfa_1 (label, &cfa);
2747       {
2748         span = targetm.dwarf_register_span (src);
2749
2750         if (!span)
2751           queue_reg_save (label, src, NULL_RTX, offset);
2752         else
2753           {
2754             /* We have a PARALLEL describing where the contents of SRC
2755                live.  Queue register saves for each piece of the
2756                PARALLEL.  */
2757             int par_index;
2758             int limit;
2759             HOST_WIDE_INT span_offset = offset;
2760
2761             gcc_assert (GET_CODE (span) == PARALLEL);
2762
2763             limit = XVECLEN (span, 0);
2764             for (par_index = 0; par_index < limit; par_index++)
2765               {
2766                 rtx elem = XVECEXP (span, 0, par_index);
2767
2768                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2769                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2770               }
2771           }
2772       }
2773       break;
2774
2775     default:
2776       gcc_unreachable ();
2777     }
2778 }
2779
2780 /* Record call frame debugging information for INSN, which either
2781    sets SP or FP (adjusting how we calculate the frame address) or saves a
2782    register to the stack.  If INSN is NULL_RTX, initialize our state.
2783
2784    If AFTER_P is false, we're being called before the insn is emitted,
2785    otherwise after.  Call instructions get invoked twice.  */
2786
2787 void
2788 dwarf2out_frame_debug (rtx insn, bool after_p)
2789 {
2790   const char *label;
2791   rtx note, n;
2792   bool handled_one = false;
2793
2794   if (insn == NULL_RTX)
2795     {
2796       size_t i;
2797
2798       /* Flush any queued register saves.  */
2799       dwarf2out_flush_queued_reg_saves ();
2800
2801       /* Set up state for generating call frame debug info.  */
2802       lookup_cfa (&cfa);
2803       gcc_assert (cfa.reg
2804                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2805
2806       cfa.reg = STACK_POINTER_REGNUM;
2807       cfa_store = cfa;
2808       cfa_temp.reg = -1;
2809       cfa_temp.offset = 0;
2810
2811       for (i = 0; i < num_regs_saved_in_regs; i++)
2812         {
2813           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2814           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2815         }
2816       num_regs_saved_in_regs = 0;
2817
2818       if (barrier_args_size)
2819         {
2820           XDELETEVEC (barrier_args_size);
2821           barrier_args_size = NULL;
2822         }
2823       return;
2824     }
2825
2826   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2827     dwarf2out_flush_queued_reg_saves ();
2828
2829   if (!RTX_FRAME_RELATED_P (insn))
2830     {
2831       /* ??? This should be done unconditionally since stack adjustments
2832          matter if the stack pointer is not the CFA register anymore but
2833          is still used to save registers.  */
2834       if (!ACCUMULATE_OUTGOING_ARGS)
2835         dwarf2out_notice_stack_adjust (insn, after_p);
2836       return;
2837     }
2838
2839   label = dwarf2out_cfi_label (false);
2840   any_cfis_emitted = false;
2841
2842   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2843     switch (REG_NOTE_KIND (note))
2844       {
2845       case REG_FRAME_RELATED_EXPR:
2846         insn = XEXP (note, 0);
2847         goto found;
2848
2849       case REG_CFA_DEF_CFA:
2850         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2851         handled_one = true;
2852         break;
2853
2854       case REG_CFA_ADJUST_CFA:
2855         n = XEXP (note, 0);
2856         if (n == NULL)
2857           {
2858             n = PATTERN (insn);
2859             if (GET_CODE (n) == PARALLEL)
2860               n = XVECEXP (n, 0, 0);
2861           }
2862         dwarf2out_frame_debug_adjust_cfa (n, label);
2863         handled_one = true;
2864         break;
2865
2866       case REG_CFA_OFFSET:
2867         n = XEXP (note, 0);
2868         if (n == NULL)
2869           n = single_set (insn);
2870         dwarf2out_frame_debug_cfa_offset (n, label);
2871         handled_one = true;
2872         break;
2873
2874       case REG_CFA_REGISTER:
2875         n = XEXP (note, 0);
2876         if (n == NULL)
2877           {
2878             n = PATTERN (insn);
2879             if (GET_CODE (n) == PARALLEL)
2880               n = XVECEXP (n, 0, 0);
2881           }
2882         dwarf2out_frame_debug_cfa_register (n, label);
2883         handled_one = true;
2884         break;
2885
2886       case REG_CFA_EXPRESSION:
2887         n = XEXP (note, 0);
2888         if (n == NULL)
2889           n = single_set (insn);
2890         dwarf2out_frame_debug_cfa_expression (n, label);
2891         handled_one = true;
2892         break;
2893
2894       case REG_CFA_RESTORE:
2895         n = XEXP (note, 0);
2896         if (n == NULL)
2897           {
2898             n = PATTERN (insn);
2899             if (GET_CODE (n) == PARALLEL)
2900               n = XVECEXP (n, 0, 0);
2901             n = XEXP (n, 0);
2902           }
2903         dwarf2out_frame_debug_cfa_restore (n, label);
2904         handled_one = true;
2905         break;
2906
2907       case REG_CFA_SET_VDRAP:
2908         n = XEXP (note, 0);
2909         if (REG_P (n))
2910           {
2911             dw_fde_ref fde = current_fde ();
2912             if (fde)
2913               {
2914                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2915                 if (REG_P (n))
2916                   fde->vdrap_reg = REGNO (n);
2917               }
2918           }
2919         handled_one = true;
2920         break;
2921
2922       default:
2923         break;
2924       }
2925   if (handled_one)
2926     {
2927       if (any_cfis_emitted)
2928         dwarf2out_flush_queued_reg_saves ();
2929       return;
2930     }
2931
2932   insn = PATTERN (insn);
2933  found:
2934   dwarf2out_frame_debug_expr (insn, label);
2935
2936   /* Check again.  A parallel can save and update the same register.
2937      We could probably check just once, here, but this is safer than
2938      removing the check above.  */
2939   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2940     dwarf2out_flush_queued_reg_saves ();
2941 }
2942
2943 /* Determine if we need to save and restore CFI information around this
2944    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2945    we do need to save/restore, then emit the save now, and insert a
2946    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2947
2948 void
2949 dwarf2out_cfi_begin_epilogue (rtx insn)
2950 {
2951   bool saw_frp = false;
2952   rtx i;
2953
2954   /* Scan forward to the return insn, noticing if there are possible
2955      frame related insns.  */
2956   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2957     {
2958       if (!INSN_P (i))
2959         continue;
2960
2961       /* Look for both regular and sibcalls to end the block.  */
2962       if (returnjump_p (i))
2963         break;
2964       if (CALL_P (i) && SIBLING_CALL_P (i))
2965         break;
2966
2967       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2968         {
2969           int idx;
2970           rtx seq = PATTERN (i);
2971
2972           if (returnjump_p (XVECEXP (seq, 0, 0)))
2973             break;
2974           if (CALL_P (XVECEXP (seq, 0, 0))
2975               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2976             break;
2977
2978           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2979             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2980               saw_frp = true;
2981         }
2982
2983       if (RTX_FRAME_RELATED_P (i))
2984         saw_frp = true;
2985     }
2986
2987   /* If the port doesn't emit epilogue unwind info, we don't need a
2988      save/restore pair.  */
2989   if (!saw_frp)
2990     return;
2991
2992   /* Otherwise, search forward to see if the return insn was the last
2993      basic block of the function.  If so, we don't need save/restore.  */
2994   gcc_assert (i != NULL);
2995   i = next_real_insn (i);
2996   if (i == NULL)
2997     return;
2998
2999   /* Insert the restore before that next real insn in the stream, and before
3000      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3001      properly nested.  This should be after any label or alignment.  This
3002      will be pushed into the CFI stream by the function below.  */
3003   while (1)
3004     {
3005       rtx p = PREV_INSN (i);
3006       if (!NOTE_P (p))
3007         break;
3008       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3009         break;
3010       i = p;
3011     }
3012   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3013
3014   emit_cfa_remember = true;
3015
3016   /* And emulate the state save.  */
3017   gcc_assert (!cfa_remember.in_use);
3018   cfa_remember = cfa;
3019   cfa_remember.in_use = 1;
3020 }
3021
3022 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3023    required.  */
3024
3025 void
3026 dwarf2out_frame_debug_restore_state (void)
3027 {
3028   dw_cfi_ref cfi = new_cfi ();
3029   const char *label = dwarf2out_cfi_label (false);
3030
3031   cfi->dw_cfi_opc = DW_CFA_restore_state;
3032   add_fde_cfi (label, cfi);
3033
3034   gcc_assert (cfa_remember.in_use);
3035   cfa = cfa_remember;
3036   cfa_remember.in_use = 0;
3037 }
3038
3039 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3040 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3041  (enum dwarf_call_frame_info cfi);
3042
3043 static enum dw_cfi_oprnd_type
3044 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3045 {
3046   switch (cfi)
3047     {
3048     case DW_CFA_nop:
3049     case DW_CFA_GNU_window_save:
3050     case DW_CFA_remember_state:
3051     case DW_CFA_restore_state:
3052       return dw_cfi_oprnd_unused;
3053
3054     case DW_CFA_set_loc:
3055     case DW_CFA_advance_loc1:
3056     case DW_CFA_advance_loc2:
3057     case DW_CFA_advance_loc4:
3058     case DW_CFA_MIPS_advance_loc8:
3059       return dw_cfi_oprnd_addr;
3060
3061     case DW_CFA_offset:
3062     case DW_CFA_offset_extended:
3063     case DW_CFA_def_cfa:
3064     case DW_CFA_offset_extended_sf:
3065     case DW_CFA_def_cfa_sf:
3066     case DW_CFA_restore:
3067     case DW_CFA_restore_extended:
3068     case DW_CFA_undefined:
3069     case DW_CFA_same_value:
3070     case DW_CFA_def_cfa_register:
3071     case DW_CFA_register:
3072     case DW_CFA_expression:
3073       return dw_cfi_oprnd_reg_num;
3074
3075     case DW_CFA_def_cfa_offset:
3076     case DW_CFA_GNU_args_size:
3077     case DW_CFA_def_cfa_offset_sf:
3078       return dw_cfi_oprnd_offset;
3079
3080     case DW_CFA_def_cfa_expression:
3081       return dw_cfi_oprnd_loc;
3082
3083     default:
3084       gcc_unreachable ();
3085     }
3086 }
3087
3088 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3089 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3090  (enum dwarf_call_frame_info cfi);
3091
3092 static enum dw_cfi_oprnd_type
3093 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3094 {
3095   switch (cfi)
3096     {
3097     case DW_CFA_def_cfa:
3098     case DW_CFA_def_cfa_sf:
3099     case DW_CFA_offset:
3100     case DW_CFA_offset_extended_sf:
3101     case DW_CFA_offset_extended:
3102       return dw_cfi_oprnd_offset;
3103
3104     case DW_CFA_register:
3105       return dw_cfi_oprnd_reg_num;
3106
3107     case DW_CFA_expression:
3108       return dw_cfi_oprnd_loc;
3109
3110     default:
3111       return dw_cfi_oprnd_unused;
3112     }
3113 }
3114
3115 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3116    switch to the data section instead, and write out a synthetic start label
3117    for collect2 the first time around.  */
3118
3119 static void
3120 switch_to_eh_frame_section (bool back)
3121 {
3122   tree label;
3123
3124 #ifdef EH_FRAME_SECTION_NAME
3125   if (eh_frame_section == 0)
3126     {
3127       int flags;
3128
3129       if (EH_TABLES_CAN_BE_READ_ONLY)
3130         {
3131           int fde_encoding;
3132           int per_encoding;
3133           int lsda_encoding;
3134
3135           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3136                                                        /*global=*/0);
3137           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3138                                                        /*global=*/1);
3139           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3140                                                         /*global=*/0);
3141           flags = ((! flag_pic
3142                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3143                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3144                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3145                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3146                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3147                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3148                    ? 0 : SECTION_WRITE);
3149         }
3150       else
3151         flags = SECTION_WRITE;
3152       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3153     }
3154 #endif /* EH_FRAME_SECTION_NAME */
3155
3156   if (eh_frame_section)
3157     switch_to_section (eh_frame_section);
3158   else
3159     {
3160       /* We have no special eh_frame section.  Put the information in
3161          the data section and emit special labels to guide collect2.  */
3162       switch_to_section (data_section);
3163
3164       if (!back)
3165         {
3166           label = get_file_function_name ("F");
3167           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3168           targetm.asm_out.globalize_label (asm_out_file,
3169                                            IDENTIFIER_POINTER (label));
3170           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3171         }
3172     }
3173 }
3174
3175 /* Switch [BACK] to the eh or debug frame table section, depending on
3176    FOR_EH.  */
3177
3178 static void
3179 switch_to_frame_table_section (int for_eh, bool back)
3180 {
3181   if (for_eh)
3182     switch_to_eh_frame_section (back);
3183   else
3184     {
3185       if (!debug_frame_section)
3186         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3187                                            SECTION_DEBUG, NULL);
3188       switch_to_section (debug_frame_section);
3189     }
3190 }
3191
3192 /* Output a Call Frame Information opcode and its operand(s).  */
3193
3194 static void
3195 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3196 {
3197   unsigned long r;
3198   HOST_WIDE_INT off;
3199
3200   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3201     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3202                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3203                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3204                          ((unsigned HOST_WIDE_INT)
3205                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3206   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3207     {
3208       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3209       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3210                            "DW_CFA_offset, column %#lx", r);
3211       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212       dw2_asm_output_data_uleb128 (off, NULL);
3213     }
3214   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3215     {
3216       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3217       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3218                            "DW_CFA_restore, column %#lx", r);
3219     }
3220   else
3221     {
3222       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3223                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3224
3225       switch (cfi->dw_cfi_opc)
3226         {
3227         case DW_CFA_set_loc:
3228           if (for_eh)
3229             dw2_asm_output_encoded_addr_rtx (
3230                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3231                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3232                 false, NULL);
3233           else
3234             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3235                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3236           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3237           break;
3238
3239         case DW_CFA_advance_loc1:
3240           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3241                                 fde->dw_fde_current_label, NULL);
3242           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3243           break;
3244
3245         case DW_CFA_advance_loc2:
3246           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3247                                 fde->dw_fde_current_label, NULL);
3248           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3249           break;
3250
3251         case DW_CFA_advance_loc4:
3252           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3253                                 fde->dw_fde_current_label, NULL);
3254           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3255           break;
3256
3257         case DW_CFA_MIPS_advance_loc8:
3258           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3259                                 fde->dw_fde_current_label, NULL);
3260           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3261           break;
3262
3263         case DW_CFA_offset_extended:
3264           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3265           dw2_asm_output_data_uleb128 (r, NULL);
3266           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3267           dw2_asm_output_data_uleb128 (off, NULL);
3268           break;
3269
3270         case DW_CFA_def_cfa:
3271           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3272           dw2_asm_output_data_uleb128 (r, NULL);
3273           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3274           break;
3275
3276         case DW_CFA_offset_extended_sf:
3277           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3278           dw2_asm_output_data_uleb128 (r, NULL);
3279           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3280           dw2_asm_output_data_sleb128 (off, NULL);
3281           break;
3282
3283         case DW_CFA_def_cfa_sf:
3284           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3285           dw2_asm_output_data_uleb128 (r, NULL);
3286           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3287           dw2_asm_output_data_sleb128 (off, NULL);
3288           break;
3289
3290         case DW_CFA_restore_extended:
3291         case DW_CFA_undefined:
3292         case DW_CFA_same_value:
3293         case DW_CFA_def_cfa_register:
3294           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3295           dw2_asm_output_data_uleb128 (r, NULL);
3296           break;
3297
3298         case DW_CFA_register:
3299           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3300           dw2_asm_output_data_uleb128 (r, NULL);
3301           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3302           dw2_asm_output_data_uleb128 (r, NULL);
3303           break;
3304
3305         case DW_CFA_def_cfa_offset:
3306         case DW_CFA_GNU_args_size:
3307           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3308           break;
3309
3310         case DW_CFA_def_cfa_offset_sf:
3311           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3312           dw2_asm_output_data_sleb128 (off, NULL);
3313           break;
3314
3315         case DW_CFA_GNU_window_save:
3316           break;
3317
3318         case DW_CFA_def_cfa_expression:
3319         case DW_CFA_expression:
3320           output_cfa_loc (cfi, for_eh);
3321           break;
3322
3323         case DW_CFA_GNU_negative_offset_extended:
3324           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3325           gcc_unreachable ();
3326
3327         default:
3328           break;
3329         }
3330     }
3331 }
3332
3333 /* Similar, but do it via assembler directives instead.  */
3334
3335 static void
3336 output_cfi_directive (dw_cfi_ref cfi)
3337 {
3338   unsigned long r, r2;
3339
3340   switch (cfi->dw_cfi_opc)
3341     {
3342     case DW_CFA_advance_loc:
3343     case DW_CFA_advance_loc1:
3344     case DW_CFA_advance_loc2:
3345     case DW_CFA_advance_loc4:
3346     case DW_CFA_MIPS_advance_loc8:
3347     case DW_CFA_set_loc:
3348       /* Should only be created by add_fde_cfi in a code path not
3349          followed when emitting via directives.  The assembler is
3350          going to take care of this for us.  */
3351       gcc_unreachable ();
3352
3353     case DW_CFA_offset:
3354     case DW_CFA_offset_extended:
3355     case DW_CFA_offset_extended_sf:
3356       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3357       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3358                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3359       break;
3360
3361     case DW_CFA_restore:
3362     case DW_CFA_restore_extended:
3363       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3364       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3365       break;
3366
3367     case DW_CFA_undefined:
3368       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3369       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3370       break;
3371
3372     case DW_CFA_same_value:
3373       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3374       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3375       break;
3376
3377     case DW_CFA_def_cfa:
3378     case DW_CFA_def_cfa_sf:
3379       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3380       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3381                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3382       break;
3383
3384     case DW_CFA_def_cfa_register:
3385       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3386       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3387       break;
3388
3389     case DW_CFA_register:
3390       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3391       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3392       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3393       break;
3394
3395     case DW_CFA_def_cfa_offset:
3396     case DW_CFA_def_cfa_offset_sf:
3397       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3398                HOST_WIDE_INT_PRINT_DEC"\n",
3399                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3400       break;
3401
3402     case DW_CFA_remember_state:
3403       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3404       break;
3405     case DW_CFA_restore_state:
3406       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3407       break;
3408
3409     case DW_CFA_GNU_args_size:
3410       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3411       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3412       if (flag_debug_asm)
3413         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3414                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415       fputc ('\n', asm_out_file);
3416       break;
3417
3418     case DW_CFA_GNU_window_save:
3419       fprintf (asm_out_file, "\t.cfi_window_save\n");
3420       break;
3421
3422     case DW_CFA_def_cfa_expression:
3423     case DW_CFA_expression:
3424       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3425       output_cfa_loc_raw (cfi);
3426       fputc ('\n', asm_out_file);
3427       break;
3428
3429     default:
3430       gcc_unreachable ();
3431     }
3432 }
3433
3434 DEF_VEC_P (dw_cfi_ref);
3435 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3436
3437 /* Output CFIs to bring current FDE to the same state as after executing
3438    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3439    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3440    other arguments to pass to output_cfi.  */
3441
3442 static void
3443 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3444 {
3445   struct dw_cfi_struct cfi_buf;
3446   dw_cfi_ref cfi2;
3447   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3448   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3449   unsigned int len, idx;
3450
3451   for (;; cfi = cfi->dw_cfi_next)
3452     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3453       {
3454       case DW_CFA_advance_loc:
3455       case DW_CFA_advance_loc1:
3456       case DW_CFA_advance_loc2:
3457       case DW_CFA_advance_loc4:
3458       case DW_CFA_MIPS_advance_loc8:
3459       case DW_CFA_set_loc:
3460         /* All advances should be ignored.  */
3461         break;
3462       case DW_CFA_remember_state:
3463         {
3464           dw_cfi_ref args_size = cfi_args_size;
3465
3466           /* Skip everything between .cfi_remember_state and
3467              .cfi_restore_state.  */
3468           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3469             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3470               break;
3471             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3472               args_size = cfi2;
3473             else
3474               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3475
3476           if (cfi2 == NULL)
3477             goto flush_all;
3478           else
3479             {
3480               cfi = cfi2;
3481               cfi_args_size = args_size;
3482             }
3483           break;
3484         }
3485       case DW_CFA_GNU_args_size:
3486         cfi_args_size = cfi;
3487         break;
3488       case DW_CFA_GNU_window_save:
3489         goto flush_all;
3490       case DW_CFA_offset:
3491       case DW_CFA_offset_extended:
3492       case DW_CFA_offset_extended_sf:
3493       case DW_CFA_restore:
3494       case DW_CFA_restore_extended:
3495       case DW_CFA_undefined:
3496       case DW_CFA_same_value:
3497       case DW_CFA_register:
3498       case DW_CFA_val_offset:
3499       case DW_CFA_val_offset_sf:
3500       case DW_CFA_expression:
3501       case DW_CFA_val_expression:
3502       case DW_CFA_GNU_negative_offset_extended:
3503         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3504           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3505                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3506         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3507         break;
3508       case DW_CFA_def_cfa:
3509       case DW_CFA_def_cfa_sf:
3510       case DW_CFA_def_cfa_expression:
3511         cfi_cfa = cfi;
3512         cfi_cfa_offset = cfi;
3513         break;
3514       case DW_CFA_def_cfa_register:
3515         cfi_cfa = cfi;
3516         break;
3517       case DW_CFA_def_cfa_offset:
3518       case DW_CFA_def_cfa_offset_sf:
3519         cfi_cfa_offset = cfi;
3520         break;
3521       case DW_CFA_nop:
3522         gcc_assert (cfi == NULL);
3523       flush_all:
3524         len = VEC_length (dw_cfi_ref, regs);
3525         for (idx = 0; idx < len; idx++)
3526           {
3527             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3528             if (cfi2 != NULL
3529                 && cfi2->dw_cfi_opc != DW_CFA_restore
3530                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3531               {
3532                 if (do_cfi_asm)
3533                   output_cfi_directive (cfi2);
3534                 else
3535                   output_cfi (cfi2, fde, for_eh);
3536               }
3537           }
3538         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3539           {
3540             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3541             cfi_buf = *cfi_cfa;
3542             switch (cfi_cfa_offset->dw_cfi_opc)
3543               {
3544               case DW_CFA_def_cfa_offset:
3545                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3546                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3547                 break;
3548               case DW_CFA_def_cfa_offset_sf:
3549                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3550                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3551                 break;
3552               case DW_CFA_def_cfa:
3553               case DW_CFA_def_cfa_sf:
3554                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3555                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3556                 break;
3557               default:
3558                 gcc_unreachable ();
3559               }
3560             cfi_cfa = &cfi_buf;
3561           }
3562         else if (cfi_cfa_offset)
3563           cfi_cfa = cfi_cfa_offset;
3564         if (cfi_cfa)
3565           {
3566             if (do_cfi_asm)
3567               output_cfi_directive (cfi_cfa);
3568             else
3569               output_cfi (cfi_cfa, fde, for_eh);
3570           }
3571         cfi_cfa = NULL;
3572         cfi_cfa_offset = NULL;
3573         if (cfi_args_size
3574             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3575           {
3576             if (do_cfi_asm)
3577               output_cfi_directive (cfi_args_size);
3578             else
3579               output_cfi (cfi_args_size, fde, for_eh);
3580           }
3581         cfi_args_size = NULL;
3582         if (cfi == NULL)
3583           {
3584             VEC_free (dw_cfi_ref, heap, regs);
3585             return;
3586           }
3587         else if (do_cfi_asm)
3588           output_cfi_directive (cfi);
3589         else
3590           output_cfi (cfi, fde, for_eh);
3591         break;
3592       default:
3593         gcc_unreachable ();
3594     }
3595 }
3596
3597 /* Output one FDE.  */
3598
3599 static void
3600 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3601             char *section_start_label, int fde_encoding, char *augmentation,
3602             bool any_lsda_needed, int lsda_encoding)
3603 {
3604   const char *begin, *end;
3605   static unsigned int j;
3606   char l1[20], l2[20];
3607   dw_cfi_ref cfi;
3608
3609   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3610                                      /* empty */ 0);
3611   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3612                                   for_eh + j);
3613   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3614   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3615   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3616     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3617                          " indicating 64-bit DWARF extension");
3618   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3619                         "FDE Length");
3620   ASM_OUTPUT_LABEL (asm_out_file, l1);
3621
3622   if (for_eh)
3623     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3624   else
3625     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3626                            debug_frame_section, "FDE CIE offset");
3627
3628   if (!fde->dw_fde_switched_sections)
3629     {
3630       begin = fde->dw_fde_begin;
3631       end = fde->dw_fde_end;
3632     }
3633   else
3634     {
3635       /* For the first section, prefer dw_fde_begin over
3636          dw_fde_{hot,cold}_section_label, as the latter
3637          might be separated from the real start of the
3638          function by alignment padding.  */
3639       if (!second)
3640         begin = fde->dw_fde_begin;
3641       else if (fde->dw_fde_switched_cold_to_hot)
3642         begin = fde->dw_fde_hot_section_label;
3643       else
3644         begin = fde->dw_fde_unlikely_section_label;
3645       if (second ^ fde->dw_fde_switched_cold_to_hot)
3646         end = fde->dw_fde_unlikely_section_end_label;
3647       else
3648         end = fde->dw_fde_hot_section_end_label;
3649     }
3650
3651   if (for_eh)
3652     {
3653       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3654       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3655       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3656                                        "FDE initial location");
3657       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3658                             end, begin, "FDE address range");
3659     }
3660   else
3661     {
3662       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3663       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3664     }
3665
3666   if (augmentation[0])
3667     {
3668       if (any_lsda_needed)
3669         {
3670           int size = size_of_encoded_value (lsda_encoding);
3671
3672           if (lsda_encoding == DW_EH_PE_aligned)
3673             {
3674               int offset = (  4         /* Length */
3675                             + 4         /* CIE offset */
3676                             + 2 * size_of_encoded_value (fde_encoding)
3677                             + 1         /* Augmentation size */ );
3678               int pad = -offset & (PTR_SIZE - 1);
3679
3680               size += pad;
3681               gcc_assert (size_of_uleb128 (size) == 1);
3682             }
3683
3684           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3685
3686           if (fde->uses_eh_lsda)
3687             {
3688               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3689                                            fde->funcdef_number);
3690               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3691                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3692                                                false,
3693                                                "Language Specific Data Area");
3694             }
3695           else
3696             {
3697               if (lsda_encoding == DW_EH_PE_aligned)
3698                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3699               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3700                                    "Language Specific Data Area (none)");
3701             }
3702         }
3703       else
3704         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3705     }
3706
3707   /* Loop through the Call Frame Instructions associated with
3708      this FDE.  */
3709   fde->dw_fde_current_label = begin;
3710   if (!fde->dw_fde_switched_sections)
3711     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3712       output_cfi (cfi, fde, for_eh);
3713   else if (!second)
3714     {
3715       if (fde->dw_fde_switch_cfi)
3716         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3717           {
3718             output_cfi (cfi, fde, for_eh);
3719             if (cfi == fde->dw_fde_switch_cfi)
3720               break;
3721           }
3722     }
3723   else
3724     {
3725       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3726
3727       if (fde->dw_fde_switch_cfi)
3728         {
3729           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3730           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3731           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3732           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3733         }
3734       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3735         output_cfi (cfi, fde, for_eh);
3736     }
3737
3738   /* If we are to emit a ref/link from function bodies to their frame tables,
3739      do it now.  This is typically performed to make sure that tables
3740      associated with functions are dragged with them and not discarded in
3741      garbage collecting links. We need to do this on a per function basis to
3742      cope with -ffunction-sections.  */
3743
3744 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3745   /* Switch to the function section, emit the ref to the tables, and
3746      switch *back* into the table section.  */
3747   switch_to_section (function_section (fde->decl));
3748   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3749   switch_to_frame_table_section (for_eh, true);
3750 #endif
3751
3752   /* Pad the FDE out to an address sized boundary.  */
3753   ASM_OUTPUT_ALIGN (asm_out_file,
3754                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3755   ASM_OUTPUT_LABEL (asm_out_file, l2);
3756
3757   j += 2;
3758 }
3759
3760 /* Return true if frame description entry FDE is needed for EH.  */
3761
3762 static bool
3763 fde_needed_for_eh_p (dw_fde_ref fde)
3764 {
3765   if (flag_asynchronous_unwind_tables)
3766     return true;
3767
3768   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3769     return true;
3770
3771   if (fde->uses_eh_lsda)
3772     return true;
3773
3774   /* If exceptions are enabled, we have collected nothrow info.  */
3775   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3776     return false;
3777
3778   return true;
3779 }
3780
3781 /* Output the call frame information used to record information
3782    that relates to calculating the frame pointer, and records the
3783    location of saved registers.  */
3784
3785 static void
3786 output_call_frame_info (int for_eh)
3787 {
3788   unsigned int i;
3789   dw_fde_ref fde;
3790   dw_cfi_ref cfi;
3791   char l1[20], l2[20], section_start_label[20];
3792   bool any_lsda_needed = false;
3793   char augmentation[6];
3794   int augmentation_size;
3795   int fde_encoding = DW_EH_PE_absptr;
3796   int per_encoding = DW_EH_PE_absptr;
3797   int lsda_encoding = DW_EH_PE_absptr;
3798   int return_reg;
3799   rtx personality = NULL;
3800   int dw_cie_version;
3801
3802   /* Don't emit a CIE if there won't be any FDEs.  */
3803   if (fde_table_in_use == 0)
3804     return;
3805
3806   /* Nothing to do if the assembler's doing it all.  */
3807   if (dwarf2out_do_cfi_asm ())
3808     return;
3809
3810   /* If we don't have any functions we'll want to unwind out of, don't emit
3811      any EH unwind information.  If we make FDEs linkonce, we may have to
3812      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3813      want to avoid having an FDE kept around when the function it refers to
3814      is discarded.  Example where this matters: a primary function template
3815      in C++ requires EH information, an explicit specialization doesn't.  */
3816   if (for_eh)
3817     {
3818       bool any_eh_needed = false;
3819
3820       for (i = 0; i < fde_table_in_use; i++)
3821         if (fde_table[i].uses_eh_lsda)
3822           any_eh_needed = any_lsda_needed = true;
3823         else if (fde_needed_for_eh_p (&fde_table[i]))
3824           any_eh_needed = true;
3825         else if (TARGET_USES_WEAK_UNWIND_INFO)
3826           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3827                                              1, 1);
3828
3829       if (!any_eh_needed)
3830         return;
3831     }
3832
3833   /* We're going to be generating comments, so turn on app.  */
3834   if (flag_debug_asm)
3835     app_enable ();
3836
3837   /* Switch to the proper frame section, first time.  */
3838   switch_to_frame_table_section (for_eh, false);
3839
3840   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3841   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3842
3843   /* Output the CIE.  */
3844   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3845   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3846   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3847     dw2_asm_output_data (4, 0xffffffff,
3848       "Initial length escape value indicating 64-bit DWARF extension");
3849   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3850                         "Length of Common Information Entry");
3851   ASM_OUTPUT_LABEL (asm_out_file, l1);
3852
3853   /* Now that the CIE pointer is PC-relative for EH,
3854      use 0 to identify the CIE.  */
3855   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3856                        (for_eh ? 0 : DWARF_CIE_ID),
3857                        "CIE Identifier Tag");
3858
3859   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3860      use CIE version 1, unless that would produce incorrect results
3861      due to overflowing the return register column.  */
3862   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3863   dw_cie_version = 1;
3864   if (return_reg >= 256 || dwarf_version > 2)
3865     dw_cie_version = 3;
3866   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3867
3868   augmentation[0] = 0;
3869   augmentation_size = 0;
3870
3871   personality = current_unit_personality;
3872   if (for_eh)
3873     {
3874       char *p;
3875
3876       /* Augmentation:
3877          z      Indicates that a uleb128 is present to size the
3878                 augmentation section.
3879          L      Indicates the encoding (and thus presence) of
3880                 an LSDA pointer in the FDE augmentation.
3881          R      Indicates a non-default pointer encoding for
3882                 FDE code pointers.
3883          P      Indicates the presence of an encoding + language
3884                 personality routine in the CIE augmentation.  */
3885
3886       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3887       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3888       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3889
3890       p = augmentation + 1;
3891       if (personality)
3892         {
3893           *p++ = 'P';
3894           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3895           assemble_external_libcall (personality);
3896         }
3897       if (any_lsda_needed)
3898         {
3899           *p++ = 'L';
3900           augmentation_size += 1;
3901         }
3902       if (fde_encoding != DW_EH_PE_absptr)
3903         {
3904           *p++ = 'R';
3905           augmentation_size += 1;
3906         }
3907       if (p > augmentation + 1)
3908         {
3909           augmentation[0] = 'z';
3910           *p = '\0';
3911         }
3912
3913       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3914       if (personality && per_encoding == DW_EH_PE_aligned)
3915         {
3916           int offset = (  4             /* Length */
3917                         + 4             /* CIE Id */
3918                         + 1             /* CIE version */
3919                         + strlen (augmentation) + 1     /* Augmentation */
3920                         + size_of_uleb128 (1)           /* Code alignment */
3921                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3922                         + 1             /* RA column */
3923                         + 1             /* Augmentation size */
3924                         + 1             /* Personality encoding */ );
3925           int pad = -offset & (PTR_SIZE - 1);
3926
3927           augmentation_size += pad;
3928
3929           /* Augmentations should be small, so there's scarce need to
3930              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3931           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3932         }
3933     }
3934
3935   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3936   if (dw_cie_version >= 4)
3937     {
3938       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3939       dw2_asm_output_data (1, 0, "CIE Segment Size");
3940     }
3941   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3942   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3943                                "CIE Data Alignment Factor");
3944
3945   if (dw_cie_version == 1)
3946     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3947   else
3948     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3949
3950   if (augmentation[0])
3951     {
3952       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3953       if (personality)
3954         {
3955           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3956                                eh_data_format_name (per_encoding));
3957           dw2_asm_output_encoded_addr_rtx (per_encoding,
3958                                            personality,
3959                                            true, NULL);
3960         }
3961
3962       if (any_lsda_needed)
3963         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3964                              eh_data_format_name (lsda_encoding));
3965
3966       if (fde_encoding != DW_EH_PE_absptr)
3967         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3968                              eh_data_format_name (fde_encoding));
3969     }
3970
3971   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3972     output_cfi (cfi, NULL, for_eh);
3973
3974   /* Pad the CIE out to an address sized boundary.  */
3975   ASM_OUTPUT_ALIGN (asm_out_file,
3976                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3977   ASM_OUTPUT_LABEL (asm_out_file, l2);
3978
3979   /* Loop through all of the FDE's.  */
3980   for (i = 0; i < fde_table_in_use; i++)
3981     {
3982       unsigned int k;
3983       fde = &fde_table[i];
3984
3985       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3986       if (for_eh && !fde_needed_for_eh_p (fde))
3987         continue;
3988
3989       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3990         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3991                     augmentation, any_lsda_needed, lsda_encoding);
3992     }
3993
3994   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3995     dw2_asm_output_data (4, 0, "End of Table");
3996 #ifdef MIPS_DEBUGGING_INFO
3997   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3998      get a value of 0.  Putting .align 0 after the label fixes it.  */
3999   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4000 #endif
4001
4002   /* Turn off app to make assembly quicker.  */
4003   if (flag_debug_asm)
4004     app_disable ();
4005 }
4006
4007 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4008
4009 static void
4010 dwarf2out_do_cfi_startproc (bool second)
4011 {
4012   int enc;
4013   rtx ref;
4014   rtx personality = get_personality_function (current_function_decl);
4015
4016   fprintf (asm_out_file, "\t.cfi_startproc\n");
4017
4018   if (personality)
4019     {
4020       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4021       ref = personality;
4022
4023       /* ??? The GAS support isn't entirely consistent.  We have to
4024          handle indirect support ourselves, but PC-relative is done
4025          in the assembler.  Further, the assembler can't handle any
4026          of the weirder relocation types.  */
4027       if (enc & DW_EH_PE_indirect)
4028         ref = dw2_force_const_mem (ref, true);
4029
4030       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4031       output_addr_const (asm_out_file, ref);
4032       fputc ('\n', asm_out_file);
4033     }
4034
4035   if (crtl->uses_eh_lsda)
4036     {
4037       char lab[20];
4038
4039       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4040       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4041                                    current_function_funcdef_no);
4042       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4043       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4044
4045       if (enc & DW_EH_PE_indirect)
4046         ref = dw2_force_const_mem (ref, true);
4047
4048       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4049       output_addr_const (asm_out_file, ref);
4050       fputc ('\n', asm_out_file);
4051     }
4052 }
4053
4054 /* Output a marker (i.e. a label) for the beginning of a function, before
4055    the prologue.  */
4056
4057 void
4058 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4059                           const char *file ATTRIBUTE_UNUSED)
4060 {
4061   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4062   char * dup_label;
4063   dw_fde_ref fde;
4064   section *fnsec;
4065   bool do_frame;
4066
4067   current_function_func_begin_label = NULL;
4068
4069   do_frame = dwarf2out_do_frame ();
4070
4071   /* ??? current_function_func_begin_label is also used by except.c for
4072      call-site information.  We must emit this label if it might be used.  */
4073   if (!do_frame
4074       && (!flag_exceptions
4075           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4076     return;
4077
4078   fnsec = function_section (current_function_decl);
4079   switch_to_section (fnsec);
4080   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4081                                current_function_funcdef_no);
4082   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4083                           current_function_funcdef_no);
4084   dup_label = xstrdup (label);
4085   current_function_func_begin_label = dup_label;
4086
4087   /* We can elide the fde allocation if we're not emitting debug info.  */
4088   if (!do_frame)
4089     return;
4090
4091   /* Expand the fde table if necessary.  */
4092   if (fde_table_in_use == fde_table_allocated)
4093     {
4094       fde_table_allocated += FDE_TABLE_INCREMENT;
4095       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4096       memset (fde_table + fde_table_in_use, 0,
4097               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4098     }
4099
4100   /* Record the FDE associated with this function.  */
4101   current_funcdef_fde = fde_table_in_use;
4102
4103   /* Add the new FDE at the end of the fde_table.  */
4104   fde = &fde_table[fde_table_in_use++];
4105   fde->decl = current_function_decl;
4106   fde->dw_fde_begin = dup_label;
4107   fde->dw_fde_current_label = dup_label;
4108   fde->dw_fde_hot_section_label = NULL;
4109   fde->dw_fde_hot_section_end_label = NULL;
4110   fde->dw_fde_unlikely_section_label = NULL;
4111   fde->dw_fde_unlikely_section_end_label = NULL;
4112   fde->dw_fde_switched_sections = 0;
4113   fde->dw_fde_switched_cold_to_hot = 0;
4114   fde->dw_fde_end = NULL;
4115   fde->dw_fde_vms_end_prologue = NULL;
4116   fde->dw_fde_vms_begin_epilogue = NULL;
4117   fde->dw_fde_cfi = NULL;
4118   fde->dw_fde_switch_cfi = NULL;
4119   fde->funcdef_number = current_function_funcdef_no;
4120   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4121   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4122   fde->nothrow = crtl->nothrow;
4123   fde->drap_reg = INVALID_REGNUM;
4124   fde->vdrap_reg = INVALID_REGNUM;
4125   if (flag_reorder_blocks_and_partition)
4126     {
4127       section *unlikelysec;
4128       if (first_function_block_is_cold)
4129         fde->in_std_section = 1;
4130       else
4131         fde->in_std_section
4132           = (fnsec == text_section
4133              || (cold_text_section && fnsec == cold_text_section));
4134       unlikelysec = unlikely_text_section ();
4135       fde->cold_in_std_section
4136         = (unlikelysec == text_section
4137            || (cold_text_section && unlikelysec == cold_text_section));
4138     }
4139   else
4140     {
4141       fde->in_std_section
4142         = (fnsec == text_section
4143            || (cold_text_section && fnsec == cold_text_section));
4144       fde->cold_in_std_section = 0;
4145     }
4146
4147   args_size = old_args_size = 0;
4148
4149   /* We only want to output line number information for the genuine dwarf2
4150      prologue case, not the eh frame case.  */
4151 #ifdef DWARF2_DEBUGGING_INFO
4152   if (file)
4153     dwarf2out_source_line (line, file, 0, true);
4154 #endif
4155
4156   if (dwarf2out_do_cfi_asm ())
4157     dwarf2out_do_cfi_startproc (false);
4158   else
4159     {
4160       rtx personality = get_personality_function (current_function_decl);
4161       if (!current_unit_personality)
4162         current_unit_personality = personality;
4163
4164       /* We cannot keep a current personality per function as without CFI
4165          asm, at the point where we emit the CFI data, there is no current
4166          function anymore.  */
4167       if (personality && current_unit_personality != personality)
4168         sorry ("multiple EH personalities are supported only with assemblers "
4169                "supporting .cfi_personality directive");
4170     }
4171 }
4172
4173 /* Output a marker (i.e. a label) for the end of the generated code
4174    for a function prologue.  This gets called *after* the prologue code has
4175    been generated.  */
4176
4177 void
4178 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4179                         const char *file ATTRIBUTE_UNUSED)
4180 {
4181   dw_fde_ref fde;
4182   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4183
4184   /* Output a label to mark the endpoint of the code generated for this
4185      function.  */
4186   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4187                                current_function_funcdef_no);
4188   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4189                           current_function_funcdef_no);
4190   fde = &fde_table[fde_table_in_use - 1];
4191   fde->dw_fde_vms_end_prologue = xstrdup (label);
4192 }
4193
4194 /* Output a marker (i.e. a label) for the beginning of the generated code
4195    for a function epilogue.  This gets called *before* the prologue code has
4196    been generated.  */
4197
4198 void
4199 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4200                           const char *file ATTRIBUTE_UNUSED)
4201 {
4202   dw_fde_ref fde;
4203   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4204
4205   fde = &fde_table[fde_table_in_use - 1];
4206   if (fde->dw_fde_vms_begin_epilogue)
4207     return;
4208
4209   /* Output a label to mark the endpoint of the code generated for this
4210      function.  */
4211   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4212                                current_function_funcdef_no);
4213   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4214                           current_function_funcdef_no);
4215   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4216 }
4217
4218 /* Output a marker (i.e. a label) for the absolute end of the generated code
4219    for a function definition.  This gets called *after* the epilogue code has
4220    been generated.  */
4221
4222 void
4223 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4224                         const char *file ATTRIBUTE_UNUSED)
4225 {
4226   dw_fde_ref fde;
4227   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4228
4229   last_var_location_insn = NULL_RTX;
4230
4231   if (dwarf2out_do_cfi_asm ())
4232     fprintf (asm_out_file, "\t.cfi_endproc\n");
4233
4234   /* Output a label to mark the endpoint of the code generated for this
4235      function.  */
4236   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4237                                current_function_funcdef_no);
4238   ASM_OUTPUT_LABEL (asm_out_file, label);
4239   fde = current_fde ();
4240   gcc_assert (fde != NULL);
4241   fde->dw_fde_end = xstrdup (label);
4242 }
4243
4244 void
4245 dwarf2out_frame_init (void)
4246 {
4247   /* Allocate the initial hunk of the fde_table.  */
4248   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4249   fde_table_allocated = FDE_TABLE_INCREMENT;
4250   fde_table_in_use = 0;
4251
4252   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4253      sake of lookup_cfa.  */
4254
4255   /* On entry, the Canonical Frame Address is at SP.  */
4256   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4257
4258   if (targetm.debug_unwind_info () == UI_DWARF2
4259       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4260     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4261 }
4262
4263 void
4264 dwarf2out_frame_finish (void)
4265 {
4266   /* Output call frame information.  */
4267   if (targetm.debug_unwind_info () == UI_DWARF2)
4268     output_call_frame_info (0);
4269
4270   /* Output another copy for the unwinder.  */
4271   if ((flag_unwind_tables || flag_exceptions)
4272       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4273     output_call_frame_info (1);
4274 }
4275
4276 /* Note that the current function section is being used for code.  */
4277
4278 static void
4279 dwarf2out_note_section_used (void)
4280 {
4281   section *sec = current_function_section ();
4282   if (sec == text_section)
4283     text_section_used = true;
4284   else if (sec == cold_text_section)
4285     cold_text_section_used = true;
4286 }
4287
4288 void
4289 dwarf2out_switch_text_section (void)
4290 {
4291   dw_fde_ref fde = current_fde ();
4292
4293   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4294
4295   fde->dw_fde_switched_sections = 1;
4296   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4297
4298   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4299   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4300   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4301   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4302   have_multiple_function_sections = true;
4303
4304   /* Reset the current label on switching text sections, so that we
4305      don't attempt to advance_loc4 between labels in different sections.  */
4306   fde->dw_fde_current_label = NULL;
4307
4308   /* There is no need to mark used sections when not debugging.  */
4309   if (cold_text_section != NULL)
4310     dwarf2out_note_section_used ();
4311
4312   if (dwarf2out_do_cfi_asm ())
4313     fprintf (asm_out_file, "\t.cfi_endproc\n");
4314
4315   /* Now do the real section switch.  */
4316   switch_to_section (current_function_section ());
4317
4318   if (dwarf2out_do_cfi_asm ())
4319     {
4320       dwarf2out_do_cfi_startproc (true);
4321       /* As this is a different FDE, insert all current CFI instructions
4322          again.  */
4323       output_cfis (fde->dw_fde_cfi, true, fde, true);
4324     }
4325   else
4326     {
4327       dw_cfi_ref cfi = fde->dw_fde_cfi;
4328
4329       cfi = fde->dw_fde_cfi;
4330       if (cfi)
4331         while (cfi->dw_cfi_next != NULL)
4332           cfi = cfi->dw_cfi_next;
4333       fde->dw_fde_switch_cfi = cfi;
4334     }
4335 }
4336 \f
4337 /* And now, the subset of the debugging information support code necessary
4338    for emitting location expressions.  */
4339
4340 /* Data about a single source file.  */
4341 struct GTY(()) dwarf_file_data {
4342   const char * filename;
4343   int emitted_number;
4344 };
4345
4346 typedef struct dw_val_struct *dw_val_ref;
4347 typedef struct die_struct *dw_die_ref;
4348 typedef const struct die_struct *const_dw_die_ref;
4349 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4350 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4351
4352 typedef struct GTY(()) deferred_locations_struct
4353 {
4354   tree variable;
4355   dw_die_ref die;
4356 } deferred_locations;
4357
4358 DEF_VEC_O(deferred_locations);
4359 DEF_VEC_ALLOC_O(deferred_locations,gc);
4360
4361 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4362
4363 DEF_VEC_P(dw_die_ref);
4364 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4365
4366 /* Each DIE may have a series of attribute/value pairs.  Values
4367    can take on several forms.  The forms that are used in this
4368    implementation are listed below.  */
4369
4370 enum dw_val_class
4371 {
4372   dw_val_class_addr,
4373   dw_val_class_offset,
4374   dw_val_class_loc,
4375   dw_val_class_loc_list,
4376   dw_val_class_range_list,
4377   dw_val_class_const,
4378   dw_val_class_unsigned_const,
4379   dw_val_class_const_double,
4380   dw_val_class_vec,
4381   dw_val_class_flag,
4382   dw_val_class_die_ref,
4383   dw_val_class_fde_ref,
4384   dw_val_class_lbl_id,
4385   dw_val_class_lineptr,
4386   dw_val_class_str,
4387   dw_val_class_macptr,
4388   dw_val_class_file,
4389   dw_val_class_data8,
4390   dw_val_class_decl_ref,
4391   dw_val_class_vms_delta
4392 };
4393
4394 /* Describe a floating point constant value, or a vector constant value.  */
4395
4396 typedef struct GTY(()) dw_vec_struct {
4397   unsigned char * GTY((length ("%h.length"))) array;
4398   unsigned length;
4399   unsigned elt_size;
4400 }
4401 dw_vec_const;
4402
4403 /* The dw_val_node describes an attribute's value, as it is
4404    represented internally.  */
4405
4406 typedef struct GTY(()) dw_val_struct {
4407   enum dw_val_class val_class;
4408   union dw_val_struct_union
4409     {
4410       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4411       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4412       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4413       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4414       HOST_WIDE_INT GTY ((default)) val_int;
4415       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4416       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4417       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4418       struct dw_val_die_union
4419         {
4420           dw_die_ref die;
4421           int external;
4422         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4423       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4424       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4425       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4426       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4427       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4428       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4429       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4430       struct dw_val_vms_delta_union
4431         {
4432           char * lbl1;
4433           char * lbl2;
4434         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4435     }
4436   GTY ((desc ("%1.val_class"))) v;
4437 }
4438 dw_val_node;
4439
4440 /* Locations in memory are described using a sequence of stack machine
4441    operations.  */
4442
4443 typedef struct GTY(()) dw_loc_descr_struct {
4444   dw_loc_descr_ref dw_loc_next;
4445   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4446   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4447      from DW_OP_addr with a dtp-relative symbol relocation.  */
4448   unsigned int dtprel : 1;
4449   int dw_loc_addr;
4450   dw_val_node dw_loc_oprnd1;
4451   dw_val_node dw_loc_oprnd2;
4452 }
4453 dw_loc_descr_node;
4454
4455 /* Location lists are ranges + location descriptions for that range,
4456    so you can track variables that are in different places over
4457    their entire life.  */
4458 typedef struct GTY(()) dw_loc_list_struct {
4459   dw_loc_list_ref dw_loc_next;
4460   const char *begin; /* Label for begin address of range */
4461   const char *end;  /* Label for end address of range */
4462   char *ll_symbol; /* Label for beginning of location list.
4463                       Only on head of list */
4464   const char *section; /* Section this loclist is relative to */
4465   dw_loc_descr_ref expr;
4466   hashval_t hash;
4467   bool emitted;
4468 } dw_loc_list_node;
4469
4470 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4471
4472 /* Convert a DWARF stack opcode into its string name.  */
4473
4474 static const char *
4475 dwarf_stack_op_name (unsigned int op)
4476 {
4477   switch (op)
4478     {
4479     case DW_OP_addr:
4480       return "DW_OP_addr";
4481     case DW_OP_deref:
4482       return "DW_OP_deref";
4483     case DW_OP_const1u:
4484       return "DW_OP_const1u";
4485     case DW_OP_const1s:
4486       return "DW_OP_const1s";
4487     case DW_OP_const2u:
4488       return "DW_OP_const2u";
4489     case DW_OP_const2s:
4490       return "DW_OP_const2s";
4491     case DW_OP_const4u:
4492       return "DW_OP_const4u";
4493     case DW_OP_const4s:
4494       return "DW_OP_const4s";
4495     case DW_OP_const8u:
4496       return "DW_OP_const8u";
4497     case DW_OP_const8s:
4498       return "DW_OP_const8s";
4499     case DW_OP_constu:
4500       return "DW_OP_constu";
4501     case DW_OP_consts:
4502       return "DW_OP_consts";
4503     case DW_OP_dup:
4504       return "DW_OP_dup";
4505     case DW_OP_drop:
4506       return "DW_OP_drop";
4507     case DW_OP_over:
4508       return "DW_OP_over";
4509     case DW_OP_pick:
4510       return "DW_OP_pick";
4511     case DW_OP_swap:
4512       return "DW_OP_swap";
4513     case DW_OP_rot:
4514       return "DW_OP_rot";
4515     case DW_OP_xderef:
4516       return "DW_OP_xderef";
4517     case DW_OP_abs:
4518       return "DW_OP_abs";
4519     case DW_OP_and:
4520       return "DW_OP_and";
4521     case DW_OP_div:
4522       return "DW_OP_div";
4523     case DW_OP_minus:
4524       return "DW_OP_minus";
4525     case DW_OP_mod:
4526       return "DW_OP_mod";
4527     case DW_OP_mul:
4528       return "DW_OP_mul";
4529     case DW_OP_neg:
4530       return "DW_OP_neg";
4531     case DW_OP_not:
4532       return "DW_OP_not";
4533     case DW_OP_or:
4534       return "DW_OP_or";
4535     case DW_OP_plus:
4536       return "DW_OP_plus";
4537     case DW_OP_plus_uconst:
4538       return "DW_OP_plus_uconst";
4539     case DW_OP_shl:
4540       return "DW_OP_shl";
4541     case DW_OP_shr:
4542       return "DW_OP_shr";
4543     case DW_OP_shra:
4544       return "DW_OP_shra";
4545     case DW_OP_xor:
4546       return "DW_OP_xor";
4547     case DW_OP_bra:
4548       return "DW_OP_bra";
4549     case DW_OP_eq:
4550       return "DW_OP_eq";
4551     case DW_OP_ge:
4552       return "DW_OP_ge";
4553     case DW_OP_gt:
4554       return "DW_OP_gt";
4555     case DW_OP_le:
4556       return "DW_OP_le";
4557     case DW_OP_lt:
4558       return "DW_OP_lt";
4559     case DW_OP_ne:
4560       return "DW_OP_ne";
4561     case DW_OP_skip:
4562       return "DW_OP_skip";
4563     case DW_OP_lit0:
4564       return "DW_OP_lit0";
4565     case DW_OP_lit1:
4566       return "DW_OP_lit1";
4567     case DW_OP_lit2:
4568       return "DW_OP_lit2";
4569     case DW_OP_lit3:
4570       return "DW_OP_lit3";
4571     case DW_OP_lit4:
4572       return "DW_OP_lit4";
4573     case DW_OP_lit5:
4574       return "DW_OP_lit5";
4575     case DW_OP_lit6:
4576       return "DW_OP_lit6";
4577     case DW_OP_lit7:
4578       return "DW_OP_lit7";
4579     case DW_OP_lit8:
4580       return "DW_OP_lit8";
4581     case DW_OP_lit9:
4582       return "DW_OP_lit9";
4583     case DW_OP_lit10:
4584       return "DW_OP_lit10";
4585     case DW_OP_lit11:
4586       return "DW_OP_lit11";
4587     case DW_OP_lit12:
4588       return "DW_OP_lit12";
4589     case DW_OP_lit13:
4590       return "DW_OP_lit13";
4591     case DW_OP_lit14:
4592       return "DW_OP_lit14";
4593     case DW_OP_lit15:
4594       return "DW_OP_lit15";
4595     case DW_OP_lit16:
4596       return "DW_OP_lit16";
4597     case DW_OP_lit17:
4598       return "DW_OP_lit17";
4599     case DW_OP_lit18:
4600       return "DW_OP_lit18";
4601     case DW_OP_lit19:
4602       return "DW_OP_lit19";
4603     case DW_OP_lit20:
4604       return "DW_OP_lit20";
4605     case DW_OP_lit21:
4606       return "DW_OP_lit21";
4607     case DW_OP_lit22:
4608       return "DW_OP_lit22";
4609     case DW_OP_lit23:
4610       return "DW_OP_lit23";
4611     case DW_OP_lit24:
4612       return "DW_OP_lit24";
4613     case DW_OP_lit25:
4614       return "DW_OP_lit25";
4615     case DW_OP_lit26:
4616       return "DW_OP_lit26";
4617     case DW_OP_lit27:
4618       return "DW_OP_lit27";
4619     case DW_OP_lit28:
4620       return "DW_OP_lit28";
4621     case DW_OP_lit29:
4622       return "DW_OP_lit29";
4623     case DW_OP_lit30:
4624       return "DW_OP_lit30";
4625     case DW_OP_lit31:
4626       return "DW_OP_lit31";
4627     case DW_OP_reg0:
4628       return "DW_OP_reg0";
4629     case DW_OP_reg1:
4630       return "DW_OP_reg1";
4631     case DW_OP_reg2:
4632       return "DW_OP_reg2";
4633     case DW_OP_reg3:
4634       return "DW_OP_reg3";
4635     case DW_OP_reg4:
4636       return "DW_OP_reg4";
4637     case DW_OP_reg5:
4638       return "DW_OP_reg5";
4639     case DW_OP_reg6:
4640       return "DW_OP_reg6";
4641     case DW_OP_reg7:
4642       return "DW_OP_reg7";
4643     case DW_OP_reg8:
4644       return "DW_OP_reg8";
4645     case DW_OP_reg9:
4646       return "DW_OP_reg9";
4647     case DW_OP_reg10:
4648       return "DW_OP_reg10";
4649     case DW_OP_reg11:
4650       return "DW_OP_reg11";
4651     case DW_OP_reg12:
4652       return "DW_OP_reg12";
4653     case DW_OP_reg13:
4654       return "DW_OP_reg13";
4655     case DW_OP_reg14:
4656       return "DW_OP_reg14";
4657     case DW_OP_reg15:
4658       return "DW_OP_reg15";
4659     case DW_OP_reg16:
4660       return "DW_OP_reg16";
4661     case DW_OP_reg17:
4662       return "DW_OP_reg17";
4663     case DW_OP_reg18:
4664       return "DW_OP_reg18";
4665     case DW_OP_reg19:
4666       return "DW_OP_reg19";
4667     case DW_OP_reg20:
4668       return "DW_OP_reg20";
4669     case DW_OP_reg21:
4670       return "DW_OP_reg21";
4671     case DW_OP_reg22:
4672       return "DW_OP_reg22";
4673     case DW_OP_reg23:
4674       return "DW_OP_reg23";
4675     case DW_OP_reg24:
4676       return "DW_OP_reg24";
4677     case DW_OP_reg25:
4678       return "DW_OP_reg25";
4679     case DW_OP_reg26:
4680       return "DW_OP_reg26";
4681     case DW_OP_reg27:
4682       return "DW_OP_reg27";
4683     case DW_OP_reg28:
4684       return "DW_OP_reg28";
4685     case DW_OP_reg29:
4686       return "DW_OP_reg29";
4687     case DW_OP_reg30:
4688       return "DW_OP_reg30";
4689     case DW_OP_reg31:
4690       return "DW_OP_reg31";
4691     case DW_OP_breg0:
4692       return "DW_OP_breg0";
4693     case DW_OP_breg1:
4694       return "DW_OP_breg1";
4695     case DW_OP_breg2:
4696       return "DW_OP_breg2";
4697     case DW_OP_breg3:
4698       return "DW_OP_breg3";
4699     case DW_OP_breg4:
4700       return "DW_OP_breg4";
4701     case DW_OP_breg5:
4702       return "DW_OP_breg5";
4703     case DW_OP_breg6:
4704       return "DW_OP_breg6";
4705     case DW_OP_breg7:
4706       return "DW_OP_breg7";
4707     case DW_OP_breg8:
4708       return "DW_OP_breg8";
4709     case DW_OP_breg9:
4710       return "DW_OP_breg9";
4711     case DW_OP_breg10:
4712       return "DW_OP_breg10";
4713     case DW_OP_breg11:
4714       return "DW_OP_breg11";
4715     case DW_OP_breg12:
4716       return "DW_OP_breg12";
4717     case DW_OP_breg13:
4718       return "DW_OP_breg13";
4719     case DW_OP_breg14:
4720       return "DW_OP_breg14";
4721     case DW_OP_breg15:
4722       return "DW_OP_breg15";
4723     case DW_OP_breg16:
4724       return "DW_OP_breg16";
4725     case DW_OP_breg17:
4726       return "DW_OP_breg17";
4727     case DW_OP_breg18:
4728       return "DW_OP_breg18";
4729     case DW_OP_breg19:
4730       return "DW_OP_breg19";
4731     case DW_OP_breg20:
4732       return "DW_OP_breg20";
4733     case DW_OP_breg21:
4734       return "DW_OP_breg21";
4735     case DW_OP_breg22:
4736       return "DW_OP_breg22";
4737     case DW_OP_breg23:
4738       return "DW_OP_breg23";
4739     case DW_OP_breg24:
4740       return "DW_OP_breg24";
4741     case DW_OP_breg25:
4742       return "DW_OP_breg25";
4743     case DW_OP_breg26:
4744       return "DW_OP_breg26";
4745     case DW_OP_breg27:
4746       return "DW_OP_breg27";
4747     case DW_OP_breg28:
4748       return "DW_OP_breg28";
4749     case DW_OP_breg29:
4750       return "DW_OP_breg29";
4751     case DW_OP_breg30:
4752       return "DW_OP_breg30";
4753     case DW_OP_breg31:
4754       return "DW_OP_breg31";
4755     case DW_OP_regx:
4756       return "DW_OP_regx";
4757     case DW_OP_fbreg:
4758       return "DW_OP_fbreg";
4759     case DW_OP_bregx:
4760       return "DW_OP_bregx";
4761     case DW_OP_piece:
4762       return "DW_OP_piece";
4763     case DW_OP_deref_size:
4764       return "DW_OP_deref_size";
4765     case DW_OP_xderef_size:
4766       return "DW_OP_xderef_size";
4767     case DW_OP_nop:
4768       return "DW_OP_nop";
4769
4770     case DW_OP_push_object_address:
4771       return "DW_OP_push_object_address";
4772     case DW_OP_call2:
4773       return "DW_OP_call2";
4774     case DW_OP_call4:
4775       return "DW_OP_call4";
4776     case DW_OP_call_ref:
4777       return "DW_OP_call_ref";
4778     case DW_OP_implicit_value:
4779       return "DW_OP_implicit_value";
4780     case DW_OP_stack_value:
4781       return "DW_OP_stack_value";
4782     case DW_OP_form_tls_address:
4783       return "DW_OP_form_tls_address";
4784     case DW_OP_call_frame_cfa:
4785       return "DW_OP_call_frame_cfa";
4786     case DW_OP_bit_piece:
4787       return "DW_OP_bit_piece";
4788
4789     case DW_OP_GNU_push_tls_address:
4790       return "DW_OP_GNU_push_tls_address";
4791     case DW_OP_GNU_uninit:
4792       return "DW_OP_GNU_uninit";
4793     case DW_OP_GNU_encoded_addr:
4794       return "DW_OP_GNU_encoded_addr";
4795     case DW_OP_GNU_implicit_pointer:
4796       return "DW_OP_GNU_implicit_pointer";
4797
4798     default:
4799       return "OP_<unknown>";
4800     }
4801 }
4802
4803 /* Return a pointer to a newly allocated location description.  Location
4804    descriptions are simple expression terms that can be strung
4805    together to form more complicated location (address) descriptions.  */
4806
4807 static inline dw_loc_descr_ref
4808 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4809                unsigned HOST_WIDE_INT oprnd2)
4810 {
4811   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4812
4813   descr->dw_loc_opc = op;
4814   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4815   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4816   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4817   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4818
4819   return descr;
4820 }
4821
4822 /* Return a pointer to a newly allocated location description for
4823    REG and OFFSET.  */
4824
4825 static inline dw_loc_descr_ref
4826 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4827 {
4828   if (reg <= 31)
4829     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4830                           offset, 0);
4831   else
4832     return new_loc_descr (DW_OP_bregx, reg, offset);
4833 }
4834
4835 /* Add a location description term to a location description expression.  */
4836
4837 static inline void
4838 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4839 {
4840   dw_loc_descr_ref *d;
4841
4842   /* Find the end of the chain.  */
4843   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4844     ;
4845
4846   *d = descr;
4847 }
4848
4849 /* Add a constant OFFSET to a location expression.  */
4850
4851 static void
4852 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4853 {
4854   dw_loc_descr_ref loc;
4855   HOST_WIDE_INT *p;
4856
4857   gcc_assert (*list_head != NULL);
4858
4859   if (!offset)
4860     return;
4861
4862   /* Find the end of the chain.  */
4863   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4864     ;
4865
4866   p = NULL;
4867   if (loc->dw_loc_opc == DW_OP_fbreg
4868       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4869     p = &loc->dw_loc_oprnd1.v.val_int;
4870   else if (loc->dw_loc_opc == DW_OP_bregx)
4871     p = &loc->dw_loc_oprnd2.v.val_int;
4872
4873   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4874      offset.  Don't optimize if an signed integer overflow would happen.  */
4875   if (p != NULL
4876       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4877           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4878     *p += offset;
4879
4880   else if (offset > 0)
4881     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4882
4883   else
4884     {
4885       loc->dw_loc_next = int_loc_descriptor (-offset);
4886       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4887     }
4888 }
4889
4890 /* Add a constant OFFSET to a location list.  */
4891
4892 static void
4893 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4894 {
4895   dw_loc_list_ref d;
4896   for (d = list_head; d != NULL; d = d->dw_loc_next)
4897     loc_descr_plus_const (&d->expr, offset);
4898 }
4899
4900 #define DWARF_REF_SIZE  \
4901   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4902
4903 /* Return the size of a location descriptor.  */
4904
4905 static unsigned long
4906 size_of_loc_descr (dw_loc_descr_ref loc)
4907 {
4908   unsigned long size = 1;
4909
4910   switch (loc->dw_loc_opc)
4911     {
4912     case DW_OP_addr:
4913       size += DWARF2_ADDR_SIZE;
4914       break;
4915     case DW_OP_const1u:
4916     case DW_OP_const1s:
4917       size += 1;
4918       break;
4919     case DW_OP_const2u:
4920     case DW_OP_const2s:
4921       size += 2;
4922       break;
4923     case DW_OP_const4u:
4924     case DW_OP_const4s:
4925       size += 4;
4926       break;
4927     case DW_OP_const8u:
4928     case DW_OP_const8s:
4929       size += 8;
4930       break;
4931     case DW_OP_constu:
4932       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4933       break;
4934     case DW_OP_consts:
4935       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4936       break;
4937     case DW_OP_pick:
4938       size += 1;
4939       break;
4940     case DW_OP_plus_uconst:
4941       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4942       break;
4943     case DW_OP_skip:
4944     case DW_OP_bra:
4945       size += 2;
4946       break;
4947     case DW_OP_breg0:
4948     case DW_OP_breg1:
4949     case DW_OP_breg2:
4950     case DW_OP_breg3:
4951     case DW_OP_breg4:
4952     case DW_OP_breg5:
4953     case DW_OP_breg6:
4954     case DW_OP_breg7:
4955     case DW_OP_breg8:
4956     case DW_OP_breg9:
4957     case DW_OP_breg10:
4958     case DW_OP_breg11:
4959     case DW_OP_breg12:
4960     case DW_OP_breg13:
4961     case DW_OP_breg14:
4962     case DW_OP_breg15:
4963     case DW_OP_breg16:
4964     case DW_OP_breg17:
4965     case DW_OP_breg18:
4966     case DW_OP_breg19:
4967     case DW_OP_breg20:
4968     case DW_OP_breg21:
4969     case DW_OP_breg22:
4970     case DW_OP_breg23:
4971     case DW_OP_breg24:
4972     case DW_OP_breg25:
4973     case DW_OP_breg26:
4974     case DW_OP_breg27:
4975     case DW_OP_breg28:
4976     case DW_OP_breg29:
4977     case DW_OP_breg30:
4978     case DW_OP_breg31:
4979       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4980       break;
4981     case DW_OP_regx:
4982       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4983       break;
4984     case DW_OP_fbreg:
4985       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4986       break;
4987     case DW_OP_bregx:
4988       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4989       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4990       break;
4991     case DW_OP_piece:
4992       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4993       break;
4994     case DW_OP_bit_piece:
4995       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4997       break;
4998     case DW_OP_deref_size:
4999     case DW_OP_xderef_size:
5000       size += 1;
5001       break;
5002     case DW_OP_call2:
5003       size += 2;
5004       break;
5005     case DW_OP_call4:
5006       size += 4;
5007       break;
5008     case DW_OP_call_ref:
5009       size += DWARF_REF_SIZE;
5010       break;
5011     case DW_OP_implicit_value:
5012       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5013               + loc->dw_loc_oprnd1.v.val_unsigned;
5014       break;
5015     case DW_OP_GNU_implicit_pointer:
5016       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5017       break;
5018     default:
5019       break;
5020     }
5021
5022   return size;
5023 }
5024
5025 /* Return the size of a series of location descriptors.  */
5026
5027 static unsigned long
5028 size_of_locs (dw_loc_descr_ref loc)
5029 {
5030   dw_loc_descr_ref l;
5031   unsigned long size;
5032
5033   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5034      field, to avoid writing to a PCH file.  */
5035   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5036     {
5037       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5038         break;
5039       size += size_of_loc_descr (l);
5040     }
5041   if (! l)
5042     return size;
5043
5044   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5045     {
5046       l->dw_loc_addr = size;
5047       size += size_of_loc_descr (l);
5048     }
5049
5050   return size;
5051 }
5052
5053 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5054 static void get_ref_die_offset_label (char *, dw_die_ref);
5055
5056 /* Output location description stack opcode's operands (if any).
5057    The for_eh_or_skip parameter controls whether register numbers are
5058    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5059    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5060    info).  This should be suppressed for the cases that have not been converted
5061    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5062
5063 static void
5064 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5065 {
5066   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5067   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5068
5069   switch (loc->dw_loc_opc)
5070     {
5071 #ifdef DWARF2_DEBUGGING_INFO
5072     case DW_OP_const2u:
5073     case DW_OP_const2s:
5074       dw2_asm_output_data (2, val1->v.val_int, NULL);
5075       break;
5076     case DW_OP_const4u:
5077       if (loc->dtprel)
5078         {
5079           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5080           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5081                                                val1->v.val_addr);
5082           fputc ('\n', asm_out_file);
5083           break;
5084         }
5085       /* FALLTHRU */
5086     case DW_OP_const4s:
5087       dw2_asm_output_data (4, val1->v.val_int, NULL);
5088       break;
5089     case DW_OP_const8u:
5090       if (loc->dtprel)
5091         {
5092           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5093           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5094                                                val1->v.val_addr);
5095           fputc ('\n', asm_out_file);
5096           break;
5097         }
5098       /* FALLTHRU */
5099     case DW_OP_const8s:
5100       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5101       dw2_asm_output_data (8, val1->v.val_int, NULL);
5102       break;
5103     case DW_OP_skip:
5104     case DW_OP_bra:
5105       {
5106         int offset;
5107
5108         gcc_assert (val1->val_class == dw_val_class_loc);
5109         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5110
5111         dw2_asm_output_data (2, offset, NULL);
5112       }
5113       break;
5114     case DW_OP_implicit_value:
5115       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5116       switch (val2->val_class)
5117         {
5118         case dw_val_class_const:
5119           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5120           break;
5121         case dw_val_class_vec:
5122           {
5123             unsigned int elt_size = val2->v.val_vec.elt_size;
5124             unsigned int len = val2->v.val_vec.length;
5125             unsigned int i;
5126             unsigned char *p;
5127
5128             if (elt_size > sizeof (HOST_WIDE_INT))
5129               {
5130                 elt_size /= 2;
5131                 len *= 2;
5132               }
5133             for (i = 0, p = val2->v.val_vec.array;
5134                  i < len;
5135                  i++, p += elt_size)
5136               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5137                                    "fp or vector constant word %u", i);
5138           }
5139           break;
5140         case dw_val_class_const_double:
5141           {
5142             unsigned HOST_WIDE_INT first, second;
5143
5144             if (WORDS_BIG_ENDIAN)
5145               {
5146                 first = val2->v.val_double.high;
5147                 second = val2->v.val_double.low;
5148               }
5149             else
5150               {
5151                 first = val2->v.val_double.low;
5152                 second = val2->v.val_double.high;
5153               }
5154             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5155                                  first, NULL);
5156             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5157                                  second, NULL);
5158           }
5159           break;
5160         case dw_val_class_addr:
5161           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5162           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5163           break;
5164         default:
5165           gcc_unreachable ();
5166         }
5167       break;
5168 #else
5169     case DW_OP_const2u:
5170     case DW_OP_const2s:
5171     case DW_OP_const4u:
5172     case DW_OP_const4s:
5173     case DW_OP_const8u:
5174     case DW_OP_const8s:
5175     case DW_OP_skip:
5176     case DW_OP_bra:
5177     case DW_OP_implicit_value:
5178       /* We currently don't make any attempt to make sure these are
5179          aligned properly like we do for the main unwind info, so
5180          don't support emitting things larger than a byte if we're
5181          only doing unwinding.  */
5182       gcc_unreachable ();
5183 #endif
5184     case DW_OP_const1u:
5185     case DW_OP_const1s:
5186       dw2_asm_output_data (1, val1->v.val_int, NULL);
5187       break;
5188     case DW_OP_constu:
5189       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5190       break;
5191     case DW_OP_consts:
5192       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5193       break;
5194     case DW_OP_pick:
5195       dw2_asm_output_data (1, val1->v.val_int, NULL);
5196       break;
5197     case DW_OP_plus_uconst:
5198       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5199       break;
5200     case DW_OP_breg0:
5201     case DW_OP_breg1:
5202     case DW_OP_breg2:
5203     case DW_OP_breg3:
5204     case DW_OP_breg4:
5205     case DW_OP_breg5:
5206     case DW_OP_breg6:
5207     case DW_OP_breg7:
5208     case DW_OP_breg8:
5209     case DW_OP_breg9:
5210     case DW_OP_breg10:
5211     case DW_OP_breg11:
5212     case DW_OP_breg12:
5213     case DW_OP_breg13:
5214     case DW_OP_breg14:
5215     case DW_OP_breg15:
5216     case DW_OP_breg16:
5217     case DW_OP_breg17:
5218     case DW_OP_breg18:
5219     case DW_OP_breg19:
5220     case DW_OP_breg20:
5221     case DW_OP_breg21:
5222     case DW_OP_breg22:
5223     case DW_OP_breg23:
5224     case DW_OP_breg24:
5225     case DW_OP_breg25:
5226     case DW_OP_breg26:
5227     case DW_OP_breg27:
5228     case DW_OP_breg28:
5229     case DW_OP_breg29:
5230     case DW_OP_breg30:
5231     case DW_OP_breg31:
5232       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5233       break;
5234     case DW_OP_regx:
5235       {
5236         unsigned r = val1->v.val_unsigned;
5237         if (for_eh_or_skip >= 0)
5238           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5239         gcc_assert (size_of_uleb128 (r) 
5240                     == size_of_uleb128 (val1->v.val_unsigned));
5241         dw2_asm_output_data_uleb128 (r, NULL);  
5242       }
5243       break;
5244     case DW_OP_fbreg:
5245       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5246       break;
5247     case DW_OP_bregx:
5248       {
5249         unsigned r = val1->v.val_unsigned;
5250         if (for_eh_or_skip >= 0)
5251           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5252         gcc_assert (size_of_uleb128 (r) 
5253                     == size_of_uleb128 (val1->v.val_unsigned));
5254         dw2_asm_output_data_uleb128 (r, NULL);  
5255         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5256       }
5257       break;
5258     case DW_OP_piece:
5259       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5260       break;
5261     case DW_OP_bit_piece:
5262       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5263       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5264       break;
5265     case DW_OP_deref_size:
5266     case DW_OP_xderef_size:
5267       dw2_asm_output_data (1, val1->v.val_int, NULL);
5268       break;
5269
5270     case DW_OP_addr:
5271       if (loc->dtprel)
5272         {
5273           if (targetm.asm_out.output_dwarf_dtprel)
5274             {
5275               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5276                                                    DWARF2_ADDR_SIZE,
5277                                                    val1->v.val_addr);
5278               fputc ('\n', asm_out_file);
5279             }
5280           else
5281             gcc_unreachable ();
5282         }
5283       else
5284         {
5285 #ifdef DWARF2_DEBUGGING_INFO
5286           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5287 #else
5288           gcc_unreachable ();
5289 #endif
5290         }
5291       break;
5292
5293     case DW_OP_GNU_implicit_pointer:
5294       {
5295         char label[MAX_ARTIFICIAL_LABEL_BYTES
5296                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5297         gcc_assert (val1->val_class == dw_val_class_die_ref);
5298         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5299         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5300         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5301       }
5302       break;
5303
5304     default:
5305       /* Other codes have no operands.  */
5306       break;
5307     }
5308 }
5309
5310 /* Output a sequence of location operations.  
5311    The for_eh_or_skip parameter controls whether register numbers are
5312    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5313    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5314    info).  This should be suppressed for the cases that have not been converted
5315    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5316
5317 static void
5318 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5319 {
5320   for (; loc != NULL; loc = loc->dw_loc_next)
5321     {
5322       enum dwarf_location_atom opc = loc->dw_loc_opc;
5323       /* Output the opcode.  */
5324       if (for_eh_or_skip >= 0 
5325           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5326         {
5327           unsigned r = (opc - DW_OP_breg0);
5328           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5329           gcc_assert (r <= 31);
5330           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5331         }
5332       else if (for_eh_or_skip >= 0 
5333                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5334         {
5335           unsigned r = (opc - DW_OP_reg0);
5336           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5337           gcc_assert (r <= 31);
5338           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5339         }
5340
5341       dw2_asm_output_data (1, opc,
5342                              "%s", dwarf_stack_op_name (opc));
5343
5344       /* Output the operand(s) (if any).  */
5345       output_loc_operands (loc, for_eh_or_skip);
5346     }
5347 }
5348
5349 /* Output location description stack opcode's operands (if any).
5350    The output is single bytes on a line, suitable for .cfi_escape.  */
5351
5352 static void
5353 output_loc_operands_raw (dw_loc_descr_ref loc)
5354 {
5355   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5356   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5357
5358   switch (loc->dw_loc_opc)
5359     {
5360     case DW_OP_addr:
5361     case DW_OP_implicit_value:
5362       /* We cannot output addresses in .cfi_escape, only bytes.  */
5363       gcc_unreachable ();
5364
5365     case DW_OP_const1u:
5366     case DW_OP_const1s:
5367     case DW_OP_pick:
5368     case DW_OP_deref_size:
5369     case DW_OP_xderef_size:
5370       fputc (',', asm_out_file);
5371       dw2_asm_output_data_raw (1, val1->v.val_int);
5372       break;
5373
5374     case DW_OP_const2u:
5375     case DW_OP_const2s:
5376       fputc (',', asm_out_file);
5377       dw2_asm_output_data_raw (2, val1->v.val_int);
5378       break;
5379
5380     case DW_OP_const4u:
5381     case DW_OP_const4s:
5382       fputc (',', asm_out_file);
5383       dw2_asm_output_data_raw (4, val1->v.val_int);
5384       break;
5385
5386     case DW_OP_const8u:
5387     case DW_OP_const8s:
5388       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5389       fputc (',', asm_out_file);
5390       dw2_asm_output_data_raw (8, val1->v.val_int);
5391       break;
5392
5393     case DW_OP_skip:
5394     case DW_OP_bra:
5395       {
5396         int offset;
5397
5398         gcc_assert (val1->val_class == dw_val_class_loc);
5399         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5400
5401         fputc (',', asm_out_file);
5402         dw2_asm_output_data_raw (2, offset);
5403       }
5404       break;
5405
5406     case DW_OP_regx:
5407       {
5408         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5409         gcc_assert (size_of_uleb128 (r) 
5410                     == size_of_uleb128 (val1->v.val_unsigned));
5411         fputc (',', asm_out_file);
5412         dw2_asm_output_data_uleb128_raw (r);
5413       }
5414       break;
5415       
5416     case DW_OP_constu:
5417     case DW_OP_plus_uconst:
5418     case DW_OP_piece:
5419       fputc (',', asm_out_file);
5420       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5421       break;
5422
5423     case DW_OP_bit_piece:
5424       fputc (',', asm_out_file);
5425       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5426       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5427       break;
5428
5429     case DW_OP_consts:
5430     case DW_OP_breg0:
5431     case DW_OP_breg1:
5432     case DW_OP_breg2:
5433     case DW_OP_breg3:
5434     case DW_OP_breg4:
5435     case DW_OP_breg5:
5436     case DW_OP_breg6:
5437     case DW_OP_breg7:
5438     case DW_OP_breg8:
5439     case DW_OP_breg9:
5440     case DW_OP_breg10:
5441     case DW_OP_breg11:
5442     case DW_OP_breg12:
5443     case DW_OP_breg13:
5444     case DW_OP_breg14:
5445     case DW_OP_breg15:
5446     case DW_OP_breg16:
5447     case DW_OP_breg17:
5448     case DW_OP_breg18:
5449     case DW_OP_breg19:
5450     case DW_OP_breg20:
5451     case DW_OP_breg21:
5452     case DW_OP_breg22:
5453     case DW_OP_breg23:
5454     case DW_OP_breg24:
5455     case DW_OP_breg25:
5456     case DW_OP_breg26:
5457     case DW_OP_breg27:
5458     case DW_OP_breg28:
5459     case DW_OP_breg29:
5460     case DW_OP_breg30:
5461     case DW_OP_breg31:
5462     case DW_OP_fbreg:
5463       fputc (',', asm_out_file);
5464       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5465       break;
5466
5467     case DW_OP_bregx:
5468       {
5469         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5470         gcc_assert (size_of_uleb128 (r) 
5471                     == size_of_uleb128 (val1->v.val_unsigned));
5472         fputc (',', asm_out_file);
5473         dw2_asm_output_data_uleb128_raw (r);
5474         fputc (',', asm_out_file);
5475         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5476       }
5477       break;
5478
5479     case DW_OP_GNU_implicit_pointer:
5480       gcc_unreachable ();
5481       break;
5482
5483     default:
5484       /* Other codes have no operands.  */
5485       break;
5486     }
5487 }
5488
5489 static void
5490 output_loc_sequence_raw (dw_loc_descr_ref loc)
5491 {
5492   while (1)
5493     {
5494       enum dwarf_location_atom opc = loc->dw_loc_opc;
5495       /* Output the opcode.  */
5496       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5497         {
5498           unsigned r = (opc - DW_OP_breg0);
5499           r = DWARF2_FRAME_REG_OUT (r, 1);
5500           gcc_assert (r <= 31);
5501           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5502         }
5503       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5504         {
5505           unsigned r = (opc - DW_OP_reg0);
5506           r = DWARF2_FRAME_REG_OUT (r, 1);
5507           gcc_assert (r <= 31);
5508           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5509         }
5510       /* Output the opcode.  */
5511       fprintf (asm_out_file, "%#x", opc);
5512       output_loc_operands_raw (loc);
5513
5514       if (!loc->dw_loc_next)
5515         break;
5516       loc = loc->dw_loc_next;
5517
5518       fputc (',', asm_out_file);
5519     }
5520 }
5521
5522 /* This routine will generate the correct assembly data for a location
5523    description based on a cfi entry with a complex address.  */
5524
5525 static void
5526 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5527 {
5528   dw_loc_descr_ref loc;
5529   unsigned long size;
5530
5531   if (cfi->dw_cfi_opc == DW_CFA_expression)
5532     {
5533       unsigned r = 
5534         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5535       dw2_asm_output_data (1, r, NULL);
5536       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5537     }
5538   else
5539     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5540
5541   /* Output the size of the block.  */
5542   size = size_of_locs (loc);
5543   dw2_asm_output_data_uleb128 (size, NULL);
5544
5545   /* Now output the operations themselves.  */
5546   output_loc_sequence (loc, for_eh);
5547 }
5548
5549 /* Similar, but used for .cfi_escape.  */
5550
5551 static void
5552 output_cfa_loc_raw (dw_cfi_ref cfi)
5553 {
5554   dw_loc_descr_ref loc;
5555   unsigned long size;
5556
5557   if (cfi->dw_cfi_opc == DW_CFA_expression)
5558     {
5559       unsigned r = 
5560         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5561       fprintf (asm_out_file, "%#x,", r);
5562       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5563     }
5564   else
5565     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5566
5567   /* Output the size of the block.  */
5568   size = size_of_locs (loc);
5569   dw2_asm_output_data_uleb128_raw (size);
5570   fputc (',', asm_out_file);
5571
5572   /* Now output the operations themselves.  */
5573   output_loc_sequence_raw (loc);
5574 }
5575
5576 /* This function builds a dwarf location descriptor sequence from a
5577    dw_cfa_location, adding the given OFFSET to the result of the
5578    expression.  */
5579
5580 static struct dw_loc_descr_struct *
5581 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5582 {
5583   struct dw_loc_descr_struct *head, *tmp;
5584
5585   offset += cfa->offset;
5586
5587   if (cfa->indirect)
5588     {
5589       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5590       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5591       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5592       add_loc_descr (&head, tmp);
5593       if (offset != 0)
5594         {
5595           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5596           add_loc_descr (&head, tmp);
5597         }
5598     }
5599   else
5600     head = new_reg_loc_descr (cfa->reg, offset);
5601
5602   return head;
5603 }
5604
5605 /* This function builds a dwarf location descriptor sequence for
5606    the address at OFFSET from the CFA when stack is aligned to
5607    ALIGNMENT byte.  */
5608
5609 static struct dw_loc_descr_struct *
5610 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5611 {
5612   struct dw_loc_descr_struct *head;
5613   unsigned int dwarf_fp
5614     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5615
5616  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5617   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5618     {
5619       head = new_reg_loc_descr (dwarf_fp, 0);
5620       add_loc_descr (&head, int_loc_descriptor (alignment));
5621       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5622       loc_descr_plus_const (&head, offset);
5623     }
5624   else
5625     head = new_reg_loc_descr (dwarf_fp, offset);
5626   return head;
5627 }
5628
5629 /* This function fills in aa dw_cfa_location structure from a dwarf location
5630    descriptor sequence.  */
5631
5632 static void
5633 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5634 {
5635   struct dw_loc_descr_struct *ptr;
5636   cfa->offset = 0;
5637   cfa->base_offset = 0;
5638   cfa->indirect = 0;
5639   cfa->reg = -1;
5640
5641   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5642     {
5643       enum dwarf_location_atom op = ptr->dw_loc_opc;
5644
5645       switch (op)
5646         {
5647         case DW_OP_reg0:
5648         case DW_OP_reg1:
5649         case DW_OP_reg2:
5650         case DW_OP_reg3:
5651         case DW_OP_reg4:
5652         case DW_OP_reg5:
5653         case DW_OP_reg6:
5654         case DW_OP_reg7:
5655         case DW_OP_reg8:
5656         case DW_OP_reg9:
5657         case DW_OP_reg10:
5658         case DW_OP_reg11:
5659         case DW_OP_reg12:
5660         case DW_OP_reg13:
5661         case DW_OP_reg14:
5662         case DW_OP_reg15:
5663         case DW_OP_reg16:
5664         case DW_OP_reg17:
5665         case DW_OP_reg18:
5666         case DW_OP_reg19:
5667         case DW_OP_reg20:
5668         case DW_OP_reg21:
5669         case DW_OP_reg22:
5670         case DW_OP_reg23:
5671         case DW_OP_reg24:
5672         case DW_OP_reg25:
5673         case DW_OP_reg26:
5674         case DW_OP_reg27:
5675         case DW_OP_reg28:
5676         case DW_OP_reg29:
5677         case DW_OP_reg30:
5678         case DW_OP_reg31:
5679           cfa->reg = op - DW_OP_reg0;
5680           break;
5681         case DW_OP_regx:
5682           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5683           break;
5684         case DW_OP_breg0:
5685         case DW_OP_breg1:
5686         case DW_OP_breg2:
5687         case DW_OP_breg3:
5688         case DW_OP_breg4:
5689         case DW_OP_breg5:
5690         case DW_OP_breg6:
5691         case DW_OP_breg7:
5692         case DW_OP_breg8:
5693         case DW_OP_breg9:
5694         case DW_OP_breg10:
5695         case DW_OP_breg11:
5696         case DW_OP_breg12:
5697         case DW_OP_breg13:
5698         case DW_OP_breg14:
5699         case DW_OP_breg15:
5700         case DW_OP_breg16:
5701         case DW_OP_breg17:
5702         case DW_OP_breg18:
5703         case DW_OP_breg19:
5704         case DW_OP_breg20:
5705         case DW_OP_breg21:
5706         case DW_OP_breg22:
5707         case DW_OP_breg23:
5708         case DW_OP_breg24:
5709         case DW_OP_breg25:
5710         case DW_OP_breg26:
5711         case DW_OP_breg27:
5712         case DW_OP_breg28:
5713         case DW_OP_breg29:
5714         case DW_OP_breg30:
5715         case DW_OP_breg31:
5716           cfa->reg = op - DW_OP_breg0;
5717           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5718           break;
5719         case DW_OP_bregx:
5720           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5721           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5722           break;
5723         case DW_OP_deref:
5724           cfa->indirect = 1;
5725           break;
5726         case DW_OP_plus_uconst:
5727           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5728           break;
5729         default:
5730           internal_error ("DW_LOC_OP %s not implemented",
5731                           dwarf_stack_op_name (ptr->dw_loc_opc));
5732         }
5733     }
5734 }
5735 \f
5736 /* And now, the support for symbolic debugging information.  */
5737
5738 /* .debug_str support.  */
5739 static int output_indirect_string (void **, void *);
5740
5741 static void dwarf2out_init (const char *);
5742 static void dwarf2out_finish (const char *);
5743 static void dwarf2out_assembly_start (void);
5744 static void dwarf2out_define (unsigned int, const char *);
5745 static void dwarf2out_undef (unsigned int, const char *);
5746 static void dwarf2out_start_source_file (unsigned, const char *);
5747 static void dwarf2out_end_source_file (unsigned);
5748 static void dwarf2out_function_decl (tree);
5749 static void dwarf2out_begin_block (unsigned, unsigned);
5750 static void dwarf2out_end_block (unsigned, unsigned);
5751 static bool dwarf2out_ignore_block (const_tree);
5752 static void dwarf2out_global_decl (tree);
5753 static void dwarf2out_type_decl (tree, int);
5754 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5755 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5756                                                  dw_die_ref);
5757 static void dwarf2out_abstract_function (tree);
5758 static void dwarf2out_var_location (rtx);
5759 static void dwarf2out_direct_call (tree);
5760 static void dwarf2out_virtual_call_token (tree, int);
5761 static void dwarf2out_copy_call_info (rtx, rtx);
5762 static void dwarf2out_virtual_call (int);
5763 static void dwarf2out_begin_function (tree);
5764 static void dwarf2out_set_name (tree, tree);
5765
5766 /* The debug hooks structure.  */
5767
5768 const struct gcc_debug_hooks dwarf2_debug_hooks =
5769 {
5770   dwarf2out_init,
5771   dwarf2out_finish,
5772   dwarf2out_assembly_start,
5773   dwarf2out_define,
5774   dwarf2out_undef,
5775   dwarf2out_start_source_file,
5776   dwarf2out_end_source_file,
5777   dwarf2out_begin_block,
5778   dwarf2out_end_block,
5779   dwarf2out_ignore_block,
5780   dwarf2out_source_line,
5781   dwarf2out_begin_prologue,
5782 #if VMS_DEBUGGING_INFO
5783   dwarf2out_vms_end_prologue,
5784   dwarf2out_vms_begin_epilogue,
5785 #else
5786   debug_nothing_int_charstar,
5787   debug_nothing_int_charstar,
5788 #endif
5789   dwarf2out_end_epilogue,
5790   dwarf2out_begin_function,
5791   debug_nothing_int,            /* end_function */
5792   dwarf2out_function_decl,      /* function_decl */
5793   dwarf2out_global_decl,
5794   dwarf2out_type_decl,          /* type_decl */
5795   dwarf2out_imported_module_or_decl,
5796   debug_nothing_tree,           /* deferred_inline_function */
5797   /* The DWARF 2 backend tries to reduce debugging bloat by not
5798      emitting the abstract description of inline functions until
5799      something tries to reference them.  */
5800   dwarf2out_abstract_function,  /* outlining_inline_function */
5801   debug_nothing_rtx,            /* label */
5802   debug_nothing_int,            /* handle_pch */
5803   dwarf2out_var_location,
5804   dwarf2out_switch_text_section,
5805   dwarf2out_direct_call,
5806   dwarf2out_virtual_call_token,
5807   dwarf2out_copy_call_info,
5808   dwarf2out_virtual_call,
5809   dwarf2out_set_name,
5810   1,                            /* start_end_main_source_file */
5811   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5812 };
5813 \f
5814 /* NOTE: In the comments in this file, many references are made to
5815    "Debugging Information Entries".  This term is abbreviated as `DIE'
5816    throughout the remainder of this file.  */
5817
5818 /* An internal representation of the DWARF output is built, and then
5819    walked to generate the DWARF debugging info.  The walk of the internal
5820    representation is done after the entire program has been compiled.
5821    The types below are used to describe the internal representation.  */
5822
5823 /* Various DIE's use offsets relative to the beginning of the
5824    .debug_info section to refer to each other.  */
5825
5826 typedef long int dw_offset;
5827
5828 /* Define typedefs here to avoid circular dependencies.  */
5829
5830 typedef struct dw_attr_struct *dw_attr_ref;
5831 typedef struct dw_line_info_struct *dw_line_info_ref;
5832 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5833 typedef struct pubname_struct *pubname_ref;
5834 typedef struct dw_ranges_struct *dw_ranges_ref;
5835 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5836 typedef struct comdat_type_struct *comdat_type_node_ref;
5837
5838 /* Each entry in the line_info_table maintains the file and
5839    line number associated with the label generated for that
5840    entry.  The label gives the PC value associated with
5841    the line number entry.  */
5842
5843 typedef struct GTY(()) dw_line_info_struct {
5844   unsigned long dw_file_num;
5845   unsigned long dw_line_num;
5846 }
5847 dw_line_info_entry;
5848
5849 /* Line information for functions in separate sections; each one gets its
5850    own sequence.  */
5851 typedef struct GTY(()) dw_separate_line_info_struct {
5852   unsigned long dw_file_num;
5853   unsigned long dw_line_num;
5854   unsigned long function;
5855 }
5856 dw_separate_line_info_entry;
5857
5858 /* Each DIE attribute has a field specifying the attribute kind,
5859    a link to the next attribute in the chain, and an attribute value.
5860    Attributes are typically linked below the DIE they modify.  */
5861
5862 typedef struct GTY(()) dw_attr_struct {
5863   enum dwarf_attribute dw_attr;
5864   dw_val_node dw_attr_val;
5865 }
5866 dw_attr_node;
5867
5868 DEF_VEC_O(dw_attr_node);
5869 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5870
5871 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5872    The children of each node form a circular list linked by
5873    die_sib.  die_child points to the node *before* the "first" child node.  */
5874
5875 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5876   union die_symbol_or_type_node
5877     {
5878       char * GTY ((tag ("0"))) die_symbol;
5879       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5880     }
5881   GTY ((desc ("dwarf_version >= 4"))) die_id;
5882   VEC(dw_attr_node,gc) * die_attr;
5883   dw_die_ref die_parent;
5884   dw_die_ref die_child;
5885   dw_die_ref die_sib;
5886   dw_die_ref die_definition; /* ref from a specification to its definition */
5887   dw_offset die_offset;
5888   unsigned long die_abbrev;
5889   int die_mark;
5890   /* Die is used and must not be pruned as unused.  */
5891   int die_perennial_p;
5892   unsigned int decl_id;
5893   enum dwarf_tag die_tag;
5894 }
5895 die_node;
5896
5897 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5898 #define FOR_EACH_CHILD(die, c, expr) do {       \
5899   c = die->die_child;                           \
5900   if (c) do {                                   \
5901     c = c->die_sib;                             \
5902     expr;                                       \
5903   } while (c != die->die_child);                \
5904 } while (0)
5905
5906 /* The pubname structure */
5907
5908 typedef struct GTY(()) pubname_struct {
5909   dw_die_ref die;
5910   const char *name;
5911 }
5912 pubname_entry;
5913
5914 DEF_VEC_O(pubname_entry);
5915 DEF_VEC_ALLOC_O(pubname_entry, gc);
5916
5917 struct GTY(()) dw_ranges_struct {
5918   /* If this is positive, it's a block number, otherwise it's a
5919      bitwise-negated index into dw_ranges_by_label.  */
5920   int num;
5921 };
5922
5923 /* A structure to hold a macinfo entry.  */
5924
5925 typedef struct GTY(()) macinfo_struct {
5926   unsigned HOST_WIDE_INT code;
5927   unsigned HOST_WIDE_INT lineno;
5928   const char *info;
5929 }
5930 macinfo_entry;
5931
5932 DEF_VEC_O(macinfo_entry);
5933 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5934
5935 struct GTY(()) dw_ranges_by_label_struct {
5936   const char *begin;
5937   const char *end;
5938 };
5939
5940 /* The comdat type node structure.  */
5941 typedef struct GTY(()) comdat_type_struct
5942 {
5943   dw_die_ref root_die;
5944   dw_die_ref type_die;
5945   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5946   struct comdat_type_struct *next;
5947 }
5948 comdat_type_node;
5949
5950 /* The limbo die list structure.  */
5951 typedef struct GTY(()) limbo_die_struct {
5952   dw_die_ref die;
5953   tree created_for;
5954   struct limbo_die_struct *next;
5955 }
5956 limbo_die_node;
5957
5958 typedef struct skeleton_chain_struct
5959 {
5960   dw_die_ref old_die;
5961   dw_die_ref new_die;
5962   struct skeleton_chain_struct *parent;
5963 }
5964 skeleton_chain_node;
5965
5966 /* How to start an assembler comment.  */
5967 #ifndef ASM_COMMENT_START
5968 #define ASM_COMMENT_START ";#"
5969 #endif
5970
5971 /* Define a macro which returns nonzero for a TYPE_DECL which was
5972    implicitly generated for a tagged type.
5973
5974    Note that unlike the gcc front end (which generates a NULL named
5975    TYPE_DECL node for each complete tagged type, each array type, and
5976    each function type node created) the g++ front end generates a
5977    _named_ TYPE_DECL node for each tagged type node created.
5978    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5979    generate a DW_TAG_typedef DIE for them.  */
5980
5981 #define TYPE_DECL_IS_STUB(decl)                         \
5982   (DECL_NAME (decl) == NULL_TREE                        \
5983    || (DECL_ARTIFICIAL (decl)                           \
5984        && is_tagged_type (TREE_TYPE (decl))             \
5985        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5986            /* This is necessary for stub decls that     \
5987               appear in nested inline functions.  */    \
5988            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5989                && (decl_ultimate_origin (decl)          \
5990                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5991
5992 /* Information concerning the compilation unit's programming
5993    language, and compiler version.  */
5994
5995 /* Fixed size portion of the DWARF compilation unit header.  */
5996 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5997   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5998
5999 /* Fixed size portion of the DWARF comdat type unit header.  */
6000 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6001   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6002    + DWARF_OFFSET_SIZE)
6003
6004 /* Fixed size portion of public names info.  */
6005 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6006
6007 /* Fixed size portion of the address range info.  */
6008 #define DWARF_ARANGES_HEADER_SIZE                                       \
6009   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6010                 DWARF2_ADDR_SIZE * 2)                                   \
6011    - DWARF_INITIAL_LENGTH_SIZE)
6012
6013 /* Size of padding portion in the address range info.  It must be
6014    aligned to twice the pointer size.  */
6015 #define DWARF_ARANGES_PAD_SIZE \
6016   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6017                 DWARF2_ADDR_SIZE * 2)                              \
6018    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6019
6020 /* Use assembler line directives if available.  */
6021 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6022 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6023 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6024 #else
6025 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6026 #endif
6027 #endif
6028
6029 /* Minimum line offset in a special line info. opcode.
6030    This value was chosen to give a reasonable range of values.  */
6031 #define DWARF_LINE_BASE  -10
6032
6033 /* First special line opcode - leave room for the standard opcodes.  */
6034 #define DWARF_LINE_OPCODE_BASE  10
6035
6036 /* Range of line offsets in a special line info. opcode.  */
6037 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6038
6039 /* Flag that indicates the initial value of the is_stmt_start flag.
6040    In the present implementation, we do not mark any lines as
6041    the beginning of a source statement, because that information
6042    is not made available by the GCC front-end.  */
6043 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6044
6045 /* Maximum number of operations per instruction bundle.  */
6046 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6047 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6048 #endif
6049
6050 /* This location is used by calc_die_sizes() to keep track
6051    the offset of each DIE within the .debug_info section.  */
6052 static unsigned long next_die_offset;
6053
6054 /* Record the root of the DIE's built for the current compilation unit.  */
6055 static GTY(()) dw_die_ref single_comp_unit_die;
6056
6057 /* A list of type DIEs that have been separated into comdat sections.  */
6058 static GTY(()) comdat_type_node *comdat_type_list;
6059
6060 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6061 static GTY(()) limbo_die_node *limbo_die_list;
6062
6063 /* A list of DIEs for which we may have to generate
6064    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6065 static GTY(()) limbo_die_node *deferred_asm_name;
6066
6067 /* Filenames referenced by this compilation unit.  */
6068 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6069
6070 /* A hash table of references to DIE's that describe declarations.
6071    The key is a DECL_UID() which is a unique number identifying each decl.  */
6072 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6073
6074 /* A hash table of references to DIE's that describe COMMON blocks.
6075    The key is DECL_UID() ^ die_parent.  */
6076 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6077
6078 typedef struct GTY(()) die_arg_entry_struct {
6079     dw_die_ref die;
6080     tree arg;
6081 } die_arg_entry;
6082
6083 DEF_VEC_O(die_arg_entry);
6084 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6085
6086 /* Node of the variable location list.  */
6087 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6088   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6089      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6090      in mode of the EXPR_LIST node and first EXPR_LIST operand
6091      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6092      location or NULL for padding.  For larger bitsizes,
6093      mode is 0 and first operand is a CONCAT with bitsize
6094      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6095      NULL as second operand.  */
6096   rtx GTY (()) loc;
6097   const char * GTY (()) label;
6098   struct var_loc_node * GTY (()) next;
6099 };
6100
6101 /* Variable location list.  */
6102 struct GTY (()) var_loc_list_def {
6103   struct var_loc_node * GTY (()) first;
6104
6105   /* Pointer to the last but one or last element of the
6106      chained list.  If the list is empty, both first and
6107      last are NULL, if the list contains just one node
6108      or the last node certainly is not redundant, it points
6109      to the last node, otherwise points to the last but one.
6110      Do not mark it for GC because it is marked through the chain.  */
6111   struct var_loc_node * GTY ((skip ("%h"))) last;
6112
6113   /* DECL_UID of the variable decl.  */
6114   unsigned int decl_id;
6115 };
6116 typedef struct var_loc_list_def var_loc_list;
6117
6118
6119 /* Table of decl location linked lists.  */
6120 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6121
6122 /* A pointer to the base of a list of references to DIE's that
6123    are uniquely identified by their tag, presence/absence of
6124    children DIE's, and list of attribute/value pairs.  */
6125 static GTY((length ("abbrev_die_table_allocated")))
6126   dw_die_ref *abbrev_die_table;
6127
6128 /* Number of elements currently allocated for abbrev_die_table.  */
6129 static GTY(()) unsigned abbrev_die_table_allocated;
6130
6131 /* Number of elements in type_die_table currently in use.  */
6132 static GTY(()) unsigned abbrev_die_table_in_use;
6133
6134 /* Size (in elements) of increments by which we may expand the
6135    abbrev_die_table.  */
6136 #define ABBREV_DIE_TABLE_INCREMENT 256
6137
6138 /* A pointer to the base of a table that contains line information
6139    for each source code line in .text in the compilation unit.  */
6140 static GTY((length ("line_info_table_allocated")))
6141      dw_line_info_ref line_info_table;
6142
6143 /* Number of elements currently allocated for line_info_table.  */
6144 static GTY(()) unsigned line_info_table_allocated;
6145
6146 /* Number of elements in line_info_table currently in use.  */
6147 static GTY(()) unsigned line_info_table_in_use;
6148
6149 /* A pointer to the base of a table that contains line information
6150    for each source code line outside of .text in the compilation unit.  */
6151 static GTY ((length ("separate_line_info_table_allocated")))
6152      dw_separate_line_info_ref separate_line_info_table;
6153
6154 /* Number of elements currently allocated for separate_line_info_table.  */
6155 static GTY(()) unsigned separate_line_info_table_allocated;
6156
6157 /* Number of elements in separate_line_info_table currently in use.  */
6158 static GTY(()) unsigned separate_line_info_table_in_use;
6159
6160 /* Size (in elements) of increments by which we may expand the
6161    line_info_table.  */
6162 #define LINE_INFO_TABLE_INCREMENT 1024
6163
6164 /* A flag to tell pubnames/types export if there is an info section to
6165    refer to.  */
6166 static bool info_section_emitted;
6167
6168 /* A pointer to the base of a table that contains a list of publicly
6169    accessible names.  */
6170 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6171
6172 /* A pointer to the base of a table that contains a list of publicly
6173    accessible types.  */
6174 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6175
6176 /* A pointer to the base of a table that contains a list of macro
6177    defines/undefines (and file start/end markers).  */
6178 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6179
6180 /* Array of dies for which we should generate .debug_arange info.  */
6181 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6182
6183 /* Number of elements currently allocated for arange_table.  */
6184 static GTY(()) unsigned arange_table_allocated;
6185
6186 /* Number of elements in arange_table currently in use.  */
6187 static GTY(()) unsigned arange_table_in_use;
6188
6189 /* Size (in elements) of increments by which we may expand the
6190    arange_table.  */
6191 #define ARANGE_TABLE_INCREMENT 64
6192
6193 /* Array of dies for which we should generate .debug_ranges info.  */
6194 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6195
6196 /* Number of elements currently allocated for ranges_table.  */
6197 static GTY(()) unsigned ranges_table_allocated;
6198
6199 /* Number of elements in ranges_table currently in use.  */
6200 static GTY(()) unsigned ranges_table_in_use;
6201
6202 /* Array of pairs of labels referenced in ranges_table.  */
6203 static GTY ((length ("ranges_by_label_allocated")))
6204      dw_ranges_by_label_ref ranges_by_label;
6205
6206 /* Number of elements currently allocated for ranges_by_label.  */
6207 static GTY(()) unsigned ranges_by_label_allocated;
6208
6209 /* Number of elements in ranges_by_label currently in use.  */
6210 static GTY(()) unsigned ranges_by_label_in_use;
6211
6212 /* Size (in elements) of increments by which we may expand the
6213    ranges_table.  */
6214 #define RANGES_TABLE_INCREMENT 64
6215
6216 /* Whether we have location lists that need outputting */
6217 static GTY(()) bool have_location_lists;
6218
6219 /* Unique label counter.  */
6220 static GTY(()) unsigned int loclabel_num;
6221
6222 /* Unique label counter for point-of-call tables.  */
6223 static GTY(()) unsigned int poc_label_num;
6224
6225 /* The direct call table structure.  */
6226
6227 typedef struct GTY(()) dcall_struct {
6228   unsigned int poc_label_num;
6229   tree poc_decl;
6230   dw_die_ref targ_die;
6231 }
6232 dcall_entry;
6233
6234 DEF_VEC_O(dcall_entry);
6235 DEF_VEC_ALLOC_O(dcall_entry, gc);
6236
6237 /* The virtual call table structure.  */
6238
6239 typedef struct GTY(()) vcall_struct {
6240   unsigned int poc_label_num;
6241   unsigned int vtable_slot;
6242 }
6243 vcall_entry;
6244
6245 DEF_VEC_O(vcall_entry);
6246 DEF_VEC_ALLOC_O(vcall_entry, gc);
6247
6248 /* Pointers to the direct and virtual call tables.  */
6249 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6250 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6251
6252 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6253
6254 struct GTY (()) vcall_insn {
6255   int insn_uid;
6256   unsigned int vtable_slot;
6257 };
6258
6259 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6260
6261 /* Record whether the function being analyzed contains inlined functions.  */
6262 static int current_function_has_inlines;
6263
6264 /* The last file entry emitted by maybe_emit_file().  */
6265 static GTY(()) struct dwarf_file_data * last_emitted_file;
6266
6267 /* Number of internal labels generated by gen_internal_sym().  */
6268 static GTY(()) int label_num;
6269
6270 /* Cached result of previous call to lookup_filename.  */
6271 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6272
6273 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6274
6275 /* Instances of generic types for which we need to generate debug
6276    info that describe their generic parameters and arguments. That
6277    generation needs to happen once all types are properly laid out so
6278    we do it at the end of compilation.  */
6279 static GTY(()) VEC(tree,gc) *generic_type_instances;
6280
6281 /* Offset from the "steady-state frame pointer" to the frame base,
6282    within the current function.  */
6283 static HOST_WIDE_INT frame_pointer_fb_offset;
6284
6285 /* Forward declarations for functions defined in this file.  */
6286
6287 static int is_pseudo_reg (const_rtx);
6288 static tree type_main_variant (tree);
6289 static int is_tagged_type (const_tree);
6290 static const char *dwarf_tag_name (unsigned);
6291 static const char *dwarf_attr_name (unsigned);
6292 static const char *dwarf_form_name (unsigned);
6293 static tree decl_ultimate_origin (const_tree);
6294 static tree decl_class_context (tree);
6295 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6296 static inline enum dw_val_class AT_class (dw_attr_ref);
6297 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6298 static inline unsigned AT_flag (dw_attr_ref);
6299 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6300 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6301 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6302 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6303 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6304                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6305 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6306                                unsigned int, unsigned char *);
6307 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6308 static hashval_t debug_str_do_hash (const void *);
6309 static int debug_str_eq (const void *, const void *);
6310 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6311 static inline const char *AT_string (dw_attr_ref);
6312 static enum dwarf_form AT_string_form (dw_attr_ref);
6313 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6314 static void add_AT_specification (dw_die_ref, dw_die_ref);
6315 static inline dw_die_ref AT_ref (dw_attr_ref);
6316 static inline int AT_ref_external (dw_attr_ref);
6317 static inline void set_AT_ref_external (dw_attr_ref, int);
6318 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6319 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6320 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6321 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6322                              dw_loc_list_ref);
6323 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6324 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6325 static inline rtx AT_addr (dw_attr_ref);
6326 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6327 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6328 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6329 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6330                            unsigned HOST_WIDE_INT);
6331 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6332                                unsigned long);
6333 static inline const char *AT_lbl (dw_attr_ref);
6334 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6335 static const char *get_AT_low_pc (dw_die_ref);
6336 static const char *get_AT_hi_pc (dw_die_ref);
6337 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6338 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6339 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6340 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6341 static bool is_cxx (void);
6342 static bool is_fortran (void);
6343 static bool is_ada (void);
6344 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6345 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6346 static void add_child_die (dw_die_ref, dw_die_ref);
6347 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6348 static dw_die_ref lookup_type_die (tree);
6349 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6350 static void equate_type_number_to_die (tree, dw_die_ref);
6351 static hashval_t decl_die_table_hash (const void *);
6352 static int decl_die_table_eq (const void *, const void *);
6353 static dw_die_ref lookup_decl_die (tree);
6354 static hashval_t common_block_die_table_hash (const void *);
6355 static int common_block_die_table_eq (const void *, const void *);
6356 static hashval_t decl_loc_table_hash (const void *);
6357 static int decl_loc_table_eq (const void *, const void *);
6358 static var_loc_list *lookup_decl_loc (const_tree);
6359 static void equate_decl_number_to_die (tree, dw_die_ref);
6360 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6361 static void print_spaces (FILE *);
6362 static void print_die (dw_die_ref, FILE *);
6363 static void print_dwarf_line_table (FILE *);
6364 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6365 static dw_die_ref pop_compile_unit (dw_die_ref);
6366 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6367 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6368 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6369 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6370 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6371 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6372 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6373                                    struct md5_ctx *, int *);
6374 struct checksum_attributes;
6375 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6376 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6377 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6378 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6379 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6380 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6381 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6382 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6383 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6384 static void compute_section_prefix (dw_die_ref);
6385 static int is_type_die (dw_die_ref);
6386 static int is_comdat_die (dw_die_ref);
6387 static int is_symbol_die (dw_die_ref);
6388 static void assign_symbol_names (dw_die_ref);
6389 static void break_out_includes (dw_die_ref);
6390 static int is_declaration_die (dw_die_ref);
6391 static int should_move_die_to_comdat (dw_die_ref);
6392 static dw_die_ref clone_as_declaration (dw_die_ref);
6393 static dw_die_ref clone_die (dw_die_ref);
6394 static dw_die_ref clone_tree (dw_die_ref);
6395 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6396 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6397 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6398 static dw_die_ref generate_skeleton (dw_die_ref);
6399 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6400                                                          dw_die_ref);
6401 static void break_out_comdat_types (dw_die_ref);
6402 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6403 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6404 static void copy_decls_for_unworthy_types (dw_die_ref);
6405
6406 static hashval_t htab_cu_hash (const void *);
6407 static int htab_cu_eq (const void *, const void *);
6408 static void htab_cu_del (void *);
6409 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6410 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6411 static void add_sibling_attributes (dw_die_ref);
6412 static void build_abbrev_table (dw_die_ref);
6413 static void output_location_lists (dw_die_ref);
6414 static int constant_size (unsigned HOST_WIDE_INT);
6415 static unsigned long size_of_die (dw_die_ref);
6416 static void calc_die_sizes (dw_die_ref);
6417 static void mark_dies (dw_die_ref);
6418 static void unmark_dies (dw_die_ref);
6419 static void unmark_all_dies (dw_die_ref);
6420 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6421 static unsigned long size_of_aranges (void);
6422 static enum dwarf_form value_format (dw_attr_ref);
6423 static void output_value_format (dw_attr_ref);
6424 static void output_abbrev_section (void);
6425 static void output_die_symbol (dw_die_ref);
6426 static void output_die (dw_die_ref);
6427 static void output_compilation_unit_header (void);
6428 static void output_comp_unit (dw_die_ref, int);
6429 static void output_comdat_type_unit (comdat_type_node *);
6430 static const char *dwarf2_name (tree, int);
6431 static void add_pubname (tree, dw_die_ref);
6432 static void add_pubname_string (const char *, dw_die_ref);
6433 static void add_pubtype (tree, dw_die_ref);
6434 static void output_pubnames (VEC (pubname_entry,gc) *);
6435 static void add_arange (tree, dw_die_ref);
6436 static void output_aranges (void);
6437 static unsigned int add_ranges_num (int);
6438 static unsigned int add_ranges (const_tree);
6439 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6440                                   bool *);
6441 static void output_ranges (void);
6442 static void output_line_info (void);
6443 static void output_file_names (void);
6444 static dw_die_ref base_type_die (tree);
6445 static int is_base_type (tree);
6446 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6447 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6448 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6449 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6450 static int type_is_enum (const_tree);
6451 static unsigned int dbx_reg_number (const_rtx);
6452 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6453 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6454 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6455                                                 enum var_init_status);
6456 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6457                                                      enum var_init_status);
6458 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6459                                          enum var_init_status);
6460 static int is_based_loc (const_rtx);
6461 static int resolve_one_addr (rtx *, void *);
6462 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6463                                                enum var_init_status);
6464 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6465                                         enum var_init_status);
6466 static dw_loc_list_ref loc_list_from_tree (tree, int);
6467 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6468 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6469 static tree field_type (const_tree);
6470 static unsigned int simple_type_align_in_bits (const_tree);
6471 static unsigned int simple_decl_align_in_bits (const_tree);
6472 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6473 static HOST_WIDE_INT field_byte_offset (const_tree);
6474 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6475                                          dw_loc_list_ref);
6476 static void add_data_member_location_attribute (dw_die_ref, tree);
6477 static bool add_const_value_attribute (dw_die_ref, rtx);
6478 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6479 static void insert_double (double_int, unsigned char *);
6480 static void insert_float (const_rtx, unsigned char *);
6481 static rtx rtl_for_decl_location (tree);
6482 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6483                                                    enum dwarf_attribute);
6484 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6485 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6486 static void add_name_attribute (dw_die_ref, const char *);
6487 static void add_comp_dir_attribute (dw_die_ref);
6488 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6489 static void add_subscript_info (dw_die_ref, tree, bool);
6490 static void add_byte_size_attribute (dw_die_ref, tree);
6491 static void add_bit_offset_attribute (dw_die_ref, tree);
6492 static void add_bit_size_attribute (dw_die_ref, tree);
6493 static void add_prototyped_attribute (dw_die_ref, tree);
6494 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6495 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6496 static void add_src_coords_attributes (dw_die_ref, tree);
6497 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6498 static void push_decl_scope (tree);
6499 static void pop_decl_scope (void);
6500 static dw_die_ref scope_die_for (tree, dw_die_ref);
6501 static inline int local_scope_p (dw_die_ref);
6502 static inline int class_scope_p (dw_die_ref);
6503 static inline int class_or_namespace_scope_p (dw_die_ref);
6504 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6505 static void add_calling_convention_attribute (dw_die_ref, tree);
6506 static const char *type_tag (const_tree);
6507 static tree member_declared_type (const_tree);
6508 #if 0
6509 static const char *decl_start_label (tree);
6510 #endif
6511 static void gen_array_type_die (tree, dw_die_ref);
6512 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6513 #if 0
6514 static void gen_entry_point_die (tree, dw_die_ref);
6515 #endif
6516 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6517 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6518 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6519 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6520 static void gen_formal_types_die (tree, dw_die_ref);
6521 static void gen_subprogram_die (tree, dw_die_ref);
6522 static void gen_variable_die (tree, tree, dw_die_ref);
6523 static void gen_const_die (tree, dw_die_ref);
6524 static void gen_label_die (tree, dw_die_ref);
6525 static void gen_lexical_block_die (tree, dw_die_ref, int);
6526 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6527 static void gen_field_die (tree, dw_die_ref);
6528 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6529 static dw_die_ref gen_compile_unit_die (const char *);
6530 static void gen_inheritance_die (tree, tree, dw_die_ref);
6531 static void gen_member_die (tree, dw_die_ref);
6532 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6533                                                 enum debug_info_usage);
6534 static void gen_subroutine_type_die (tree, dw_die_ref);
6535 static void gen_typedef_die (tree, dw_die_ref);
6536 static void gen_type_die (tree, dw_die_ref);
6537 static void gen_block_die (tree, dw_die_ref, int);
6538 static void decls_for_scope (tree, dw_die_ref, int);
6539 static int is_redundant_typedef (const_tree);
6540 static bool is_naming_typedef_decl (const_tree);
6541 static inline dw_die_ref get_context_die (tree);
6542 static void gen_namespace_die (tree, dw_die_ref);
6543 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6544 static dw_die_ref force_decl_die (tree);
6545 static dw_die_ref force_type_die (tree);
6546 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6547 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6548 static struct dwarf_file_data * lookup_filename (const char *);
6549 static void retry_incomplete_types (void);
6550 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6551 static void gen_generic_params_dies (tree);
6552 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6553 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6554 static void splice_child_die (dw_die_ref, dw_die_ref);
6555 static int file_info_cmp (const void *, const void *);
6556 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6557                                      const char *, const char *);
6558 static void output_loc_list (dw_loc_list_ref);
6559 static char *gen_internal_sym (const char *);
6560
6561 static void prune_unmark_dies (dw_die_ref);
6562 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6563 static void prune_unused_types_mark (dw_die_ref, int);
6564 static void prune_unused_types_walk (dw_die_ref);
6565 static void prune_unused_types_walk_attribs (dw_die_ref);
6566 static void prune_unused_types_prune (dw_die_ref);
6567 static void prune_unused_types (void);
6568 static int maybe_emit_file (struct dwarf_file_data *fd);
6569 static inline const char *AT_vms_delta1 (dw_attr_ref);
6570 static inline const char *AT_vms_delta2 (dw_attr_ref);
6571 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6572                                      const char *, const char *);
6573 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6574 static void gen_remaining_tmpl_value_param_die_attribute (void);
6575 static bool generic_type_p (tree);
6576 static void schedule_generic_params_dies_gen (tree t);
6577 static void gen_scheduled_generic_parms_dies (void);
6578
6579 /* Section names used to hold DWARF debugging information.  */
6580 #ifndef DEBUG_INFO_SECTION
6581 #define DEBUG_INFO_SECTION      ".debug_info"
6582 #endif
6583 #ifndef DEBUG_ABBREV_SECTION
6584 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6585 #endif
6586 #ifndef DEBUG_ARANGES_SECTION
6587 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6588 #endif
6589 #ifndef DEBUG_MACINFO_SECTION
6590 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6591 #endif
6592 #ifndef DEBUG_LINE_SECTION
6593 #define DEBUG_LINE_SECTION      ".debug_line"
6594 #endif
6595 #ifndef DEBUG_LOC_SECTION
6596 #define DEBUG_LOC_SECTION       ".debug_loc"
6597 #endif
6598 #ifndef DEBUG_PUBNAMES_SECTION
6599 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6600 #endif
6601 #ifndef DEBUG_PUBTYPES_SECTION
6602 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6603 #endif
6604 #ifndef DEBUG_DCALL_SECTION
6605 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6606 #endif
6607 #ifndef DEBUG_VCALL_SECTION
6608 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6609 #endif
6610 #ifndef DEBUG_STR_SECTION
6611 #define DEBUG_STR_SECTION       ".debug_str"
6612 #endif
6613 #ifndef DEBUG_RANGES_SECTION
6614 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6615 #endif
6616
6617 /* Standard ELF section names for compiled code and data.  */
6618 #ifndef TEXT_SECTION_NAME
6619 #define TEXT_SECTION_NAME       ".text"
6620 #endif
6621
6622 /* Section flags for .debug_str section.  */
6623 #define DEBUG_STR_SECTION_FLAGS \
6624   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6625    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6626    : SECTION_DEBUG)
6627
6628 /* Labels we insert at beginning sections we can reference instead of
6629    the section names themselves.  */
6630
6631 #ifndef TEXT_SECTION_LABEL
6632 #define TEXT_SECTION_LABEL              "Ltext"
6633 #endif
6634 #ifndef COLD_TEXT_SECTION_LABEL
6635 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6636 #endif
6637 #ifndef DEBUG_LINE_SECTION_LABEL
6638 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6639 #endif
6640 #ifndef DEBUG_INFO_SECTION_LABEL
6641 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6642 #endif
6643 #ifndef DEBUG_ABBREV_SECTION_LABEL
6644 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6645 #endif
6646 #ifndef DEBUG_LOC_SECTION_LABEL
6647 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6648 #endif
6649 #ifndef DEBUG_RANGES_SECTION_LABEL
6650 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6651 #endif
6652 #ifndef DEBUG_MACINFO_SECTION_LABEL
6653 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6654 #endif
6655
6656
6657 /* Definitions of defaults for formats and names of various special
6658    (artificial) labels which may be generated within this file (when the -g
6659    options is used and DWARF2_DEBUGGING_INFO is in effect.
6660    If necessary, these may be overridden from within the tm.h file, but
6661    typically, overriding these defaults is unnecessary.  */
6662
6663 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6664 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6665 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6666 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6667 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6668 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6669 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6670 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6671 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6672 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6673
6674 #ifndef TEXT_END_LABEL
6675 #define TEXT_END_LABEL          "Letext"
6676 #endif
6677 #ifndef COLD_END_LABEL
6678 #define COLD_END_LABEL          "Letext_cold"
6679 #endif
6680 #ifndef BLOCK_BEGIN_LABEL
6681 #define BLOCK_BEGIN_LABEL       "LBB"
6682 #endif
6683 #ifndef BLOCK_END_LABEL
6684 #define BLOCK_END_LABEL         "LBE"
6685 #endif
6686 #ifndef LINE_CODE_LABEL
6687 #define LINE_CODE_LABEL         "LM"
6688 #endif
6689 #ifndef SEPARATE_LINE_CODE_LABEL
6690 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6691 #endif
6692
6693 \f
6694 /* Return the root of the DIE's built for the current compilation unit.  */
6695 static dw_die_ref
6696 comp_unit_die (void)
6697 {
6698   if (!single_comp_unit_die)
6699     single_comp_unit_die = gen_compile_unit_die (NULL);
6700   return single_comp_unit_die;
6701 }
6702
6703 /* We allow a language front-end to designate a function that is to be
6704    called to "demangle" any name before it is put into a DIE.  */
6705
6706 static const char *(*demangle_name_func) (const char *);
6707
6708 void
6709 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6710 {
6711   demangle_name_func = func;
6712 }
6713
6714 /* Test if rtl node points to a pseudo register.  */
6715
6716 static inline int
6717 is_pseudo_reg (const_rtx rtl)
6718 {
6719   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6720           || (GET_CODE (rtl) == SUBREG
6721               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6722 }
6723
6724 /* Return a reference to a type, with its const and volatile qualifiers
6725    removed.  */
6726
6727 static inline tree
6728 type_main_variant (tree type)
6729 {
6730   type = TYPE_MAIN_VARIANT (type);
6731
6732   /* ??? There really should be only one main variant among any group of
6733      variants of a given type (and all of the MAIN_VARIANT values for all
6734      members of the group should point to that one type) but sometimes the C
6735      front-end messes this up for array types, so we work around that bug
6736      here.  */
6737   if (TREE_CODE (type) == ARRAY_TYPE)
6738     while (type != TYPE_MAIN_VARIANT (type))
6739       type = TYPE_MAIN_VARIANT (type);
6740
6741   return type;
6742 }
6743
6744 /* Return nonzero if the given type node represents a tagged type.  */
6745
6746 static inline int
6747 is_tagged_type (const_tree type)
6748 {
6749   enum tree_code code = TREE_CODE (type);
6750
6751   return (code == RECORD_TYPE || code == UNION_TYPE
6752           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6753 }
6754
6755 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6756
6757 static void
6758 get_ref_die_offset_label (char *label, dw_die_ref ref)
6759 {
6760   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6761 }
6762
6763 /* Convert a DIE tag into its string name.  */
6764
6765 static const char *
6766 dwarf_tag_name (unsigned int tag)
6767 {
6768   switch (tag)
6769     {
6770     case DW_TAG_padding:
6771       return "DW_TAG_padding";
6772     case DW_TAG_array_type:
6773       return "DW_TAG_array_type";
6774     case DW_TAG_class_type:
6775       return "DW_TAG_class_type";
6776     case DW_TAG_entry_point:
6777       return "DW_TAG_entry_point";
6778     case DW_TAG_enumeration_type:
6779       return "DW_TAG_enumeration_type";
6780     case DW_TAG_formal_parameter:
6781       return "DW_TAG_formal_parameter";
6782     case DW_TAG_imported_declaration:
6783       return "DW_TAG_imported_declaration";
6784     case DW_TAG_label:
6785       return "DW_TAG_label";
6786     case DW_TAG_lexical_block:
6787       return "DW_TAG_lexical_block";
6788     case DW_TAG_member:
6789       return "DW_TAG_member";
6790     case DW_TAG_pointer_type:
6791       return "DW_TAG_pointer_type";
6792     case DW_TAG_reference_type:
6793       return "DW_TAG_reference_type";
6794     case DW_TAG_compile_unit:
6795       return "DW_TAG_compile_unit";
6796     case DW_TAG_string_type:
6797       return "DW_TAG_string_type";
6798     case DW_TAG_structure_type:
6799       return "DW_TAG_structure_type";
6800     case DW_TAG_subroutine_type:
6801       return "DW_TAG_subroutine_type";
6802     case DW_TAG_typedef:
6803       return "DW_TAG_typedef";
6804     case DW_TAG_union_type:
6805       return "DW_TAG_union_type";
6806     case DW_TAG_unspecified_parameters:
6807       return "DW_TAG_unspecified_parameters";
6808     case DW_TAG_variant:
6809       return "DW_TAG_variant";
6810     case DW_TAG_common_block:
6811       return "DW_TAG_common_block";
6812     case DW_TAG_common_inclusion:
6813       return "DW_TAG_common_inclusion";
6814     case DW_TAG_inheritance:
6815       return "DW_TAG_inheritance";
6816     case DW_TAG_inlined_subroutine:
6817       return "DW_TAG_inlined_subroutine";
6818     case DW_TAG_module:
6819       return "DW_TAG_module";
6820     case DW_TAG_ptr_to_member_type:
6821       return "DW_TAG_ptr_to_member_type";
6822     case DW_TAG_set_type:
6823       return "DW_TAG_set_type";
6824     case DW_TAG_subrange_type:
6825       return "DW_TAG_subrange_type";
6826     case DW_TAG_with_stmt:
6827       return "DW_TAG_with_stmt";
6828     case DW_TAG_access_declaration:
6829       return "DW_TAG_access_declaration";
6830     case DW_TAG_base_type:
6831       return "DW_TAG_base_type";
6832     case DW_TAG_catch_block:
6833       return "DW_TAG_catch_block";
6834     case DW_TAG_const_type:
6835       return "DW_TAG_const_type";
6836     case DW_TAG_constant:
6837       return "DW_TAG_constant";
6838     case DW_TAG_enumerator:
6839       return "DW_TAG_enumerator";
6840     case DW_TAG_file_type:
6841       return "DW_TAG_file_type";
6842     case DW_TAG_friend:
6843       return "DW_TAG_friend";
6844     case DW_TAG_namelist:
6845       return "DW_TAG_namelist";
6846     case DW_TAG_namelist_item:
6847       return "DW_TAG_namelist_item";
6848     case DW_TAG_packed_type:
6849       return "DW_TAG_packed_type";
6850     case DW_TAG_subprogram:
6851       return "DW_TAG_subprogram";
6852     case DW_TAG_template_type_param:
6853       return "DW_TAG_template_type_param";
6854     case DW_TAG_template_value_param:
6855       return "DW_TAG_template_value_param";
6856     case DW_TAG_thrown_type:
6857       return "DW_TAG_thrown_type";
6858     case DW_TAG_try_block:
6859       return "DW_TAG_try_block";
6860     case DW_TAG_variant_part:
6861       return "DW_TAG_variant_part";
6862     case DW_TAG_variable:
6863       return "DW_TAG_variable";
6864     case DW_TAG_volatile_type:
6865       return "DW_TAG_volatile_type";
6866     case DW_TAG_dwarf_procedure:
6867       return "DW_TAG_dwarf_procedure";
6868     case DW_TAG_restrict_type:
6869       return "DW_TAG_restrict_type";
6870     case DW_TAG_interface_type:
6871       return "DW_TAG_interface_type";
6872     case DW_TAG_namespace:
6873       return "DW_TAG_namespace";
6874     case DW_TAG_imported_module:
6875       return "DW_TAG_imported_module";
6876     case DW_TAG_unspecified_type:
6877       return "DW_TAG_unspecified_type";
6878     case DW_TAG_partial_unit:
6879       return "DW_TAG_partial_unit";
6880     case DW_TAG_imported_unit:
6881       return "DW_TAG_imported_unit";
6882     case DW_TAG_condition:
6883       return "DW_TAG_condition";
6884     case DW_TAG_shared_type:
6885       return "DW_TAG_shared_type";
6886     case DW_TAG_type_unit:
6887       return "DW_TAG_type_unit";
6888     case DW_TAG_rvalue_reference_type:
6889       return "DW_TAG_rvalue_reference_type";
6890     case DW_TAG_template_alias:
6891       return "DW_TAG_template_alias";
6892     case DW_TAG_GNU_template_parameter_pack:
6893       return "DW_TAG_GNU_template_parameter_pack";
6894     case DW_TAG_GNU_formal_parameter_pack:
6895       return "DW_TAG_GNU_formal_parameter_pack";
6896     case DW_TAG_MIPS_loop:
6897       return "DW_TAG_MIPS_loop";
6898     case DW_TAG_format_label:
6899       return "DW_TAG_format_label";
6900     case DW_TAG_function_template:
6901       return "DW_TAG_function_template";
6902     case DW_TAG_class_template:
6903       return "DW_TAG_class_template";
6904     case DW_TAG_GNU_BINCL:
6905       return "DW_TAG_GNU_BINCL";
6906     case DW_TAG_GNU_EINCL:
6907       return "DW_TAG_GNU_EINCL";
6908     case DW_TAG_GNU_template_template_param:
6909       return "DW_TAG_GNU_template_template_param";
6910     default:
6911       return "DW_TAG_<unknown>";
6912     }
6913 }
6914
6915 /* Convert a DWARF attribute code into its string name.  */
6916
6917 static const char *
6918 dwarf_attr_name (unsigned int attr)
6919 {
6920   switch (attr)
6921     {
6922     case DW_AT_sibling:
6923       return "DW_AT_sibling";
6924     case DW_AT_location:
6925       return "DW_AT_location";
6926     case DW_AT_name:
6927       return "DW_AT_name";
6928     case DW_AT_ordering:
6929       return "DW_AT_ordering";
6930     case DW_AT_subscr_data:
6931       return "DW_AT_subscr_data";
6932     case DW_AT_byte_size:
6933       return "DW_AT_byte_size";
6934     case DW_AT_bit_offset:
6935       return "DW_AT_bit_offset";
6936     case DW_AT_bit_size:
6937       return "DW_AT_bit_size";
6938     case DW_AT_element_list:
6939       return "DW_AT_element_list";
6940     case DW_AT_stmt_list:
6941       return "DW_AT_stmt_list";
6942     case DW_AT_low_pc:
6943       return "DW_AT_low_pc";
6944     case DW_AT_high_pc:
6945       return "DW_AT_high_pc";
6946     case DW_AT_language:
6947       return "DW_AT_language";
6948     case DW_AT_member:
6949       return "DW_AT_member";
6950     case DW_AT_discr:
6951       return "DW_AT_discr";
6952     case DW_AT_discr_value:
6953       return "DW_AT_discr_value";
6954     case DW_AT_visibility:
6955       return "DW_AT_visibility";
6956     case DW_AT_import:
6957       return "DW_AT_import";
6958     case DW_AT_string_length:
6959       return "DW_AT_string_length";
6960     case DW_AT_common_reference:
6961       return "DW_AT_common_reference";
6962     case DW_AT_comp_dir:
6963       return "DW_AT_comp_dir";
6964     case DW_AT_const_value:
6965       return "DW_AT_const_value";
6966     case DW_AT_containing_type:
6967       return "DW_AT_containing_type";
6968     case DW_AT_default_value:
6969       return "DW_AT_default_value";
6970     case DW_AT_inline:
6971       return "DW_AT_inline";
6972     case DW_AT_is_optional:
6973       return "DW_AT_is_optional";
6974     case DW_AT_lower_bound:
6975       return "DW_AT_lower_bound";
6976     case DW_AT_producer:
6977       return "DW_AT_producer";
6978     case DW_AT_prototyped:
6979       return "DW_AT_prototyped";
6980     case DW_AT_return_addr:
6981       return "DW_AT_return_addr";
6982     case DW_AT_start_scope:
6983       return "DW_AT_start_scope";
6984     case DW_AT_bit_stride:
6985       return "DW_AT_bit_stride";
6986     case DW_AT_upper_bound:
6987       return "DW_AT_upper_bound";
6988     case DW_AT_abstract_origin:
6989       return "DW_AT_abstract_origin";
6990     case DW_AT_accessibility:
6991       return "DW_AT_accessibility";
6992     case DW_AT_address_class:
6993       return "DW_AT_address_class";
6994     case DW_AT_artificial:
6995       return "DW_AT_artificial";
6996     case DW_AT_base_types:
6997       return "DW_AT_base_types";
6998     case DW_AT_calling_convention:
6999       return "DW_AT_calling_convention";
7000     case DW_AT_count:
7001       return "DW_AT_count";
7002     case DW_AT_data_member_location:
7003       return "DW_AT_data_member_location";
7004     case DW_AT_decl_column:
7005       return "DW_AT_decl_column";
7006     case DW_AT_decl_file:
7007       return "DW_AT_decl_file";
7008     case DW_AT_decl_line:
7009       return "DW_AT_decl_line";
7010     case DW_AT_declaration:
7011       return "DW_AT_declaration";
7012     case DW_AT_discr_list:
7013       return "DW_AT_discr_list";
7014     case DW_AT_encoding:
7015       return "DW_AT_encoding";
7016     case DW_AT_external:
7017       return "DW_AT_external";
7018     case DW_AT_explicit:
7019       return "DW_AT_explicit";
7020     case DW_AT_frame_base:
7021       return "DW_AT_frame_base";
7022     case DW_AT_friend:
7023       return "DW_AT_friend";
7024     case DW_AT_identifier_case:
7025       return "DW_AT_identifier_case";
7026     case DW_AT_macro_info:
7027       return "DW_AT_macro_info";
7028     case DW_AT_namelist_items:
7029       return "DW_AT_namelist_items";
7030     case DW_AT_priority:
7031       return "DW_AT_priority";
7032     case DW_AT_segment:
7033       return "DW_AT_segment";
7034     case DW_AT_specification:
7035       return "DW_AT_specification";
7036     case DW_AT_static_link:
7037       return "DW_AT_static_link";
7038     case DW_AT_type:
7039       return "DW_AT_type";
7040     case DW_AT_use_location:
7041       return "DW_AT_use_location";
7042     case DW_AT_variable_parameter:
7043       return "DW_AT_variable_parameter";
7044     case DW_AT_virtuality:
7045       return "DW_AT_virtuality";
7046     case DW_AT_vtable_elem_location:
7047       return "DW_AT_vtable_elem_location";
7048
7049     case DW_AT_allocated:
7050       return "DW_AT_allocated";
7051     case DW_AT_associated:
7052       return "DW_AT_associated";
7053     case DW_AT_data_location:
7054       return "DW_AT_data_location";
7055     case DW_AT_byte_stride:
7056       return "DW_AT_byte_stride";
7057     case DW_AT_entry_pc:
7058       return "DW_AT_entry_pc";
7059     case DW_AT_use_UTF8:
7060       return "DW_AT_use_UTF8";
7061     case DW_AT_extension:
7062       return "DW_AT_extension";
7063     case DW_AT_ranges:
7064       return "DW_AT_ranges";
7065     case DW_AT_trampoline:
7066       return "DW_AT_trampoline";
7067     case DW_AT_call_column:
7068       return "DW_AT_call_column";
7069     case DW_AT_call_file:
7070       return "DW_AT_call_file";
7071     case DW_AT_call_line:
7072       return "DW_AT_call_line";
7073     case DW_AT_object_pointer:
7074       return "DW_AT_object_pointer";
7075
7076     case DW_AT_signature:
7077       return "DW_AT_signature";
7078     case DW_AT_main_subprogram:
7079       return "DW_AT_main_subprogram";
7080     case DW_AT_data_bit_offset:
7081       return "DW_AT_data_bit_offset";
7082     case DW_AT_const_expr:
7083       return "DW_AT_const_expr";
7084     case DW_AT_enum_class:
7085       return "DW_AT_enum_class";
7086     case DW_AT_linkage_name:
7087       return "DW_AT_linkage_name";
7088
7089     case DW_AT_MIPS_fde:
7090       return "DW_AT_MIPS_fde";
7091     case DW_AT_MIPS_loop_begin:
7092       return "DW_AT_MIPS_loop_begin";
7093     case DW_AT_MIPS_tail_loop_begin:
7094       return "DW_AT_MIPS_tail_loop_begin";
7095     case DW_AT_MIPS_epilog_begin:
7096       return "DW_AT_MIPS_epilog_begin";
7097 #if VMS_DEBUGGING_INFO
7098     case DW_AT_HP_prologue:
7099       return "DW_AT_HP_prologue";
7100 #else
7101     case DW_AT_MIPS_loop_unroll_factor:
7102       return "DW_AT_MIPS_loop_unroll_factor";
7103 #endif
7104     case DW_AT_MIPS_software_pipeline_depth:
7105       return "DW_AT_MIPS_software_pipeline_depth";
7106     case DW_AT_MIPS_linkage_name:
7107       return "DW_AT_MIPS_linkage_name";
7108 #if VMS_DEBUGGING_INFO
7109     case DW_AT_HP_epilogue:
7110       return "DW_AT_HP_epilogue";
7111 #else
7112     case DW_AT_MIPS_stride:
7113       return "DW_AT_MIPS_stride";
7114 #endif
7115     case DW_AT_MIPS_abstract_name:
7116       return "DW_AT_MIPS_abstract_name";
7117     case DW_AT_MIPS_clone_origin:
7118       return "DW_AT_MIPS_clone_origin";
7119     case DW_AT_MIPS_has_inlines:
7120       return "DW_AT_MIPS_has_inlines";
7121
7122     case DW_AT_sf_names:
7123       return "DW_AT_sf_names";
7124     case DW_AT_src_info:
7125       return "DW_AT_src_info";
7126     case DW_AT_mac_info:
7127       return "DW_AT_mac_info";
7128     case DW_AT_src_coords:
7129       return "DW_AT_src_coords";
7130     case DW_AT_body_begin:
7131       return "DW_AT_body_begin";
7132     case DW_AT_body_end:
7133       return "DW_AT_body_end";
7134     case DW_AT_GNU_vector:
7135       return "DW_AT_GNU_vector";
7136     case DW_AT_GNU_guarded_by:
7137       return "DW_AT_GNU_guarded_by";
7138     case DW_AT_GNU_pt_guarded_by:
7139       return "DW_AT_GNU_pt_guarded_by";
7140     case DW_AT_GNU_guarded:
7141       return "DW_AT_GNU_guarded";
7142     case DW_AT_GNU_pt_guarded:
7143       return "DW_AT_GNU_pt_guarded";
7144     case DW_AT_GNU_locks_excluded:
7145       return "DW_AT_GNU_locks_excluded";
7146     case DW_AT_GNU_exclusive_locks_required:
7147       return "DW_AT_GNU_exclusive_locks_required";
7148     case DW_AT_GNU_shared_locks_required:
7149       return "DW_AT_GNU_shared_locks_required";
7150     case DW_AT_GNU_odr_signature:
7151       return "DW_AT_GNU_odr_signature";
7152     case DW_AT_GNU_template_name:
7153       return "DW_AT_GNU_template_name";
7154
7155     case DW_AT_VMS_rtnbeg_pd_address:
7156       return "DW_AT_VMS_rtnbeg_pd_address";
7157
7158     default:
7159       return "DW_AT_<unknown>";
7160     }
7161 }
7162
7163 /* Convert a DWARF value form code into its string name.  */
7164
7165 static const char *
7166 dwarf_form_name (unsigned int form)
7167 {
7168   switch (form)
7169     {
7170     case DW_FORM_addr:
7171       return "DW_FORM_addr";
7172     case DW_FORM_block2:
7173       return "DW_FORM_block2";
7174     case DW_FORM_block4:
7175       return "DW_FORM_block4";
7176     case DW_FORM_data2:
7177       return "DW_FORM_data2";
7178     case DW_FORM_data4:
7179       return "DW_FORM_data4";
7180     case DW_FORM_data8:
7181       return "DW_FORM_data8";
7182     case DW_FORM_string:
7183       return "DW_FORM_string";
7184     case DW_FORM_block:
7185       return "DW_FORM_block";
7186     case DW_FORM_block1:
7187       return "DW_FORM_block1";
7188     case DW_FORM_data1:
7189       return "DW_FORM_data1";
7190     case DW_FORM_flag:
7191       return "DW_FORM_flag";
7192     case DW_FORM_sdata:
7193       return "DW_FORM_sdata";
7194     case DW_FORM_strp:
7195       return "DW_FORM_strp";
7196     case DW_FORM_udata:
7197       return "DW_FORM_udata";
7198     case DW_FORM_ref_addr:
7199       return "DW_FORM_ref_addr";
7200     case DW_FORM_ref1:
7201       return "DW_FORM_ref1";
7202     case DW_FORM_ref2:
7203       return "DW_FORM_ref2";
7204     case DW_FORM_ref4:
7205       return "DW_FORM_ref4";
7206     case DW_FORM_ref8:
7207       return "DW_FORM_ref8";
7208     case DW_FORM_ref_udata:
7209       return "DW_FORM_ref_udata";
7210     case DW_FORM_indirect:
7211       return "DW_FORM_indirect";
7212     case DW_FORM_sec_offset:
7213       return "DW_FORM_sec_offset";
7214     case DW_FORM_exprloc:
7215       return "DW_FORM_exprloc";
7216     case DW_FORM_flag_present:
7217       return "DW_FORM_flag_present";
7218     case DW_FORM_ref_sig8:
7219       return "DW_FORM_ref_sig8";
7220     default:
7221       return "DW_FORM_<unknown>";
7222     }
7223 }
7224 \f
7225 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7226    instance of an inlined instance of a decl which is local to an inline
7227    function, so we have to trace all of the way back through the origin chain
7228    to find out what sort of node actually served as the original seed for the
7229    given block.  */
7230
7231 static tree
7232 decl_ultimate_origin (const_tree decl)
7233 {
7234   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7235     return NULL_TREE;
7236
7237   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7238      nodes in the function to point to themselves; ignore that if
7239      we're trying to output the abstract instance of this function.  */
7240   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7241     return NULL_TREE;
7242
7243   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7244      most distant ancestor, this should never happen.  */
7245   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7246
7247   return DECL_ABSTRACT_ORIGIN (decl);
7248 }
7249
7250 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7251    of a virtual function may refer to a base class, so we check the 'this'
7252    parameter.  */
7253
7254 static tree
7255 decl_class_context (tree decl)
7256 {
7257   tree context = NULL_TREE;
7258
7259   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7260     context = DECL_CONTEXT (decl);
7261   else
7262     context = TYPE_MAIN_VARIANT
7263       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7264
7265   if (context && !TYPE_P (context))
7266     context = NULL_TREE;
7267
7268   return context;
7269 }
7270 \f
7271 /* Add an attribute/value pair to a DIE.  */
7272
7273 static inline void
7274 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7275 {
7276   /* Maybe this should be an assert?  */
7277   if (die == NULL)
7278     return;
7279
7280   if (die->die_attr == NULL)
7281     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7282   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7283 }
7284
7285 static inline enum dw_val_class
7286 AT_class (dw_attr_ref a)
7287 {
7288   return a->dw_attr_val.val_class;
7289 }
7290
7291 /* Add a flag value attribute to a DIE.  */
7292
7293 static inline void
7294 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7295 {
7296   dw_attr_node attr;
7297
7298   attr.dw_attr = attr_kind;
7299   attr.dw_attr_val.val_class = dw_val_class_flag;
7300   attr.dw_attr_val.v.val_flag = flag;
7301   add_dwarf_attr (die, &attr);
7302 }
7303
7304 static inline unsigned
7305 AT_flag (dw_attr_ref a)
7306 {
7307   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7308   return a->dw_attr_val.v.val_flag;
7309 }
7310
7311 /* Add a signed integer attribute value to a DIE.  */
7312
7313 static inline void
7314 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7315 {
7316   dw_attr_node attr;
7317
7318   attr.dw_attr = attr_kind;
7319   attr.dw_attr_val.val_class = dw_val_class_const;
7320   attr.dw_attr_val.v.val_int = int_val;
7321   add_dwarf_attr (die, &attr);
7322 }
7323
7324 static inline HOST_WIDE_INT
7325 AT_int (dw_attr_ref a)
7326 {
7327   gcc_assert (a && AT_class (a) == dw_val_class_const);
7328   return a->dw_attr_val.v.val_int;
7329 }
7330
7331 /* Add an unsigned integer attribute value to a DIE.  */
7332
7333 static inline void
7334 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7335                  unsigned HOST_WIDE_INT unsigned_val)
7336 {
7337   dw_attr_node attr;
7338
7339   attr.dw_attr = attr_kind;
7340   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7341   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7342   add_dwarf_attr (die, &attr);
7343 }
7344
7345 static inline unsigned HOST_WIDE_INT
7346 AT_unsigned (dw_attr_ref a)
7347 {
7348   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7349   return a->dw_attr_val.v.val_unsigned;
7350 }
7351
7352 /* Add an unsigned double integer attribute value to a DIE.  */
7353
7354 static inline void
7355 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7356                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7357 {
7358   dw_attr_node attr;
7359
7360   attr.dw_attr = attr_kind;
7361   attr.dw_attr_val.val_class = dw_val_class_const_double;
7362   attr.dw_attr_val.v.val_double.high = high;
7363   attr.dw_attr_val.v.val_double.low = low;
7364   add_dwarf_attr (die, &attr);
7365 }
7366
7367 /* Add a floating point attribute value to a DIE and return it.  */
7368
7369 static inline void
7370 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7371             unsigned int length, unsigned int elt_size, unsigned char *array)
7372 {
7373   dw_attr_node attr;
7374
7375   attr.dw_attr = attr_kind;
7376   attr.dw_attr_val.val_class = dw_val_class_vec;
7377   attr.dw_attr_val.v.val_vec.length = length;
7378   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7379   attr.dw_attr_val.v.val_vec.array = array;
7380   add_dwarf_attr (die, &attr);
7381 }
7382
7383 /* Add an 8-byte data attribute value to a DIE.  */
7384
7385 static inline void
7386 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7387               unsigned char data8[8])
7388 {
7389   dw_attr_node attr;
7390
7391   attr.dw_attr = attr_kind;
7392   attr.dw_attr_val.val_class = dw_val_class_data8;
7393   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7394   add_dwarf_attr (die, &attr);
7395 }
7396
7397 /* Hash and equality functions for debug_str_hash.  */
7398
7399 static hashval_t
7400 debug_str_do_hash (const void *x)
7401 {
7402   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7403 }
7404
7405 static int
7406 debug_str_eq (const void *x1, const void *x2)
7407 {
7408   return strcmp ((((const struct indirect_string_node *)x1)->str),
7409                  (const char *)x2) == 0;
7410 }
7411
7412 /* Add STR to the indirect string hash table.  */
7413
7414 static struct indirect_string_node *
7415 find_AT_string (const char *str)
7416 {
7417   struct indirect_string_node *node;
7418   void **slot;
7419
7420   if (! debug_str_hash)
7421     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7422                                       debug_str_eq, NULL);
7423
7424   slot = htab_find_slot_with_hash (debug_str_hash, str,
7425                                    htab_hash_string (str), INSERT);
7426   if (*slot == NULL)
7427     {
7428       node = ggc_alloc_cleared_indirect_string_node ();
7429       node->str = ggc_strdup (str);
7430       *slot = node;
7431     }
7432   else
7433     node = (struct indirect_string_node *) *slot;
7434
7435   node->refcount++;
7436   return node;
7437 }
7438
7439 /* Add a string attribute value to a DIE.  */
7440
7441 static inline void
7442 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7443 {
7444   dw_attr_node attr;
7445   struct indirect_string_node *node;
7446
7447   node = find_AT_string (str);
7448
7449   attr.dw_attr = attr_kind;
7450   attr.dw_attr_val.val_class = dw_val_class_str;
7451   attr.dw_attr_val.v.val_str = node;
7452   add_dwarf_attr (die, &attr);
7453 }
7454
7455 /* Create a label for an indirect string node, ensuring it is going to
7456    be output, unless its reference count goes down to zero.  */
7457
7458 static inline void
7459 gen_label_for_indirect_string (struct indirect_string_node *node)
7460 {
7461   char label[32];
7462
7463   if (node->label)
7464     return;
7465
7466   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7467   ++dw2_string_counter;
7468   node->label = xstrdup (label);
7469 }
7470
7471 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7472    debug string STR.  */
7473
7474 static inline rtx
7475 get_debug_string_label (const char *str)
7476 {
7477   struct indirect_string_node *node = find_AT_string (str);
7478
7479   debug_str_hash_forced = true;
7480
7481   gen_label_for_indirect_string (node);
7482
7483   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7484 }
7485
7486 static inline const char *
7487 AT_string (dw_attr_ref a)
7488 {
7489   gcc_assert (a && AT_class (a) == dw_val_class_str);
7490   return a->dw_attr_val.v.val_str->str;
7491 }
7492
7493 /* Find out whether a string should be output inline in DIE
7494    or out-of-line in .debug_str section.  */
7495
7496 static enum dwarf_form
7497 AT_string_form (dw_attr_ref a)
7498 {
7499   struct indirect_string_node *node;
7500   unsigned int len;
7501
7502   gcc_assert (a && AT_class (a) == dw_val_class_str);
7503
7504   node = a->dw_attr_val.v.val_str;
7505   if (node->form)
7506     return node->form;
7507
7508   len = strlen (node->str) + 1;
7509
7510   /* If the string is shorter or equal to the size of the reference, it is
7511      always better to put it inline.  */
7512   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7513     return node->form = DW_FORM_string;
7514
7515   /* If we cannot expect the linker to merge strings in .debug_str
7516      section, only put it into .debug_str if it is worth even in this
7517      single module.  */
7518   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7519       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7520       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7521     return node->form = DW_FORM_string;
7522
7523   gen_label_for_indirect_string (node);
7524
7525   return node->form = DW_FORM_strp;
7526 }
7527
7528 /* Add a DIE reference attribute value to a DIE.  */
7529
7530 static inline void
7531 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7532 {
7533   dw_attr_node attr;
7534
7535 #ifdef ENABLE_CHECKING
7536   gcc_assert (targ_die != NULL);
7537 #else
7538   /* With LTO we can end up trying to reference something we didn't create
7539      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7540   if (targ_die == NULL)
7541     return;
7542 #endif
7543
7544   attr.dw_attr = attr_kind;
7545   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7546   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7547   attr.dw_attr_val.v.val_die_ref.external = 0;
7548   add_dwarf_attr (die, &attr);
7549 }
7550
7551 /* Add an AT_specification attribute to a DIE, and also make the back
7552    pointer from the specification to the definition.  */
7553
7554 static inline void
7555 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7556 {
7557   add_AT_die_ref (die, DW_AT_specification, targ_die);
7558   gcc_assert (!targ_die->die_definition);
7559   targ_die->die_definition = die;
7560 }
7561
7562 static inline dw_die_ref
7563 AT_ref (dw_attr_ref a)
7564 {
7565   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7566   return a->dw_attr_val.v.val_die_ref.die;
7567 }
7568
7569 static inline int
7570 AT_ref_external (dw_attr_ref a)
7571 {
7572   if (a && AT_class (a) == dw_val_class_die_ref)
7573     return a->dw_attr_val.v.val_die_ref.external;
7574
7575   return 0;
7576 }
7577
7578 static inline void
7579 set_AT_ref_external (dw_attr_ref a, int i)
7580 {
7581   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7582   a->dw_attr_val.v.val_die_ref.external = i;
7583 }
7584
7585 /* Add an FDE reference attribute value to a DIE.  */
7586
7587 static inline void
7588 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7589 {
7590   dw_attr_node attr;
7591
7592   attr.dw_attr = attr_kind;
7593   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7594   attr.dw_attr_val.v.val_fde_index = targ_fde;
7595   add_dwarf_attr (die, &attr);
7596 }
7597
7598 /* Add a location description attribute value to a DIE.  */
7599
7600 static inline void
7601 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7602 {
7603   dw_attr_node attr;
7604
7605   attr.dw_attr = attr_kind;
7606   attr.dw_attr_val.val_class = dw_val_class_loc;
7607   attr.dw_attr_val.v.val_loc = loc;
7608   add_dwarf_attr (die, &attr);
7609 }
7610
7611 static inline dw_loc_descr_ref
7612 AT_loc (dw_attr_ref a)
7613 {
7614   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7615   return a->dw_attr_val.v.val_loc;
7616 }
7617
7618 static inline void
7619 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7620 {
7621   dw_attr_node attr;
7622
7623   attr.dw_attr = attr_kind;
7624   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7625   attr.dw_attr_val.v.val_loc_list = loc_list;
7626   add_dwarf_attr (die, &attr);
7627   have_location_lists = true;
7628 }
7629
7630 static inline dw_loc_list_ref
7631 AT_loc_list (dw_attr_ref a)
7632 {
7633   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7634   return a->dw_attr_val.v.val_loc_list;
7635 }
7636
7637 static inline dw_loc_list_ref *
7638 AT_loc_list_ptr (dw_attr_ref a)
7639 {
7640   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7641   return &a->dw_attr_val.v.val_loc_list;
7642 }
7643
7644 /* Add an address constant attribute value to a DIE.  */
7645
7646 static inline void
7647 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7648 {
7649   dw_attr_node attr;
7650
7651   attr.dw_attr = attr_kind;
7652   attr.dw_attr_val.val_class = dw_val_class_addr;
7653   attr.dw_attr_val.v.val_addr = addr;
7654   add_dwarf_attr (die, &attr);
7655 }
7656
7657 /* Get the RTX from to an address DIE attribute.  */
7658
7659 static inline rtx
7660 AT_addr (dw_attr_ref a)
7661 {
7662   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7663   return a->dw_attr_val.v.val_addr;
7664 }
7665
7666 /* Add a file attribute value to a DIE.  */
7667
7668 static inline void
7669 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7670              struct dwarf_file_data *fd)
7671 {
7672   dw_attr_node attr;
7673
7674   attr.dw_attr = attr_kind;
7675   attr.dw_attr_val.val_class = dw_val_class_file;
7676   attr.dw_attr_val.v.val_file = fd;
7677   add_dwarf_attr (die, &attr);
7678 }
7679
7680 /* Get the dwarf_file_data from a file DIE attribute.  */
7681
7682 static inline struct dwarf_file_data *
7683 AT_file (dw_attr_ref a)
7684 {
7685   gcc_assert (a && AT_class (a) == dw_val_class_file);
7686   return a->dw_attr_val.v.val_file;
7687 }
7688
7689 /* Add a vms delta attribute value to a DIE.  */
7690
7691 static inline void
7692 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7693                   const char *lbl1, const char *lbl2)
7694 {
7695   dw_attr_node attr;
7696
7697   attr.dw_attr = attr_kind;
7698   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7699   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7700   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7701   add_dwarf_attr (die, &attr);
7702 }
7703
7704 /* Add a label identifier attribute value to a DIE.  */
7705
7706 static inline void
7707 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7708 {
7709   dw_attr_node attr;
7710
7711   attr.dw_attr = attr_kind;
7712   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7713   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7714   add_dwarf_attr (die, &attr);
7715 }
7716
7717 /* Add a section offset attribute value to a DIE, an offset into the
7718    debug_line section.  */
7719
7720 static inline void
7721 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7722                 const char *label)
7723 {
7724   dw_attr_node attr;
7725
7726   attr.dw_attr = attr_kind;
7727   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7728   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7729   add_dwarf_attr (die, &attr);
7730 }
7731
7732 /* Add a section offset attribute value to a DIE, an offset into the
7733    debug_macinfo section.  */
7734
7735 static inline void
7736 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7737                const char *label)
7738 {
7739   dw_attr_node attr;
7740
7741   attr.dw_attr = attr_kind;
7742   attr.dw_attr_val.val_class = dw_val_class_macptr;
7743   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7744   add_dwarf_attr (die, &attr);
7745 }
7746
7747 /* Add an offset attribute value to a DIE.  */
7748
7749 static inline void
7750 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7751                unsigned HOST_WIDE_INT offset)
7752 {
7753   dw_attr_node attr;
7754
7755   attr.dw_attr = attr_kind;
7756   attr.dw_attr_val.val_class = dw_val_class_offset;
7757   attr.dw_attr_val.v.val_offset = offset;
7758   add_dwarf_attr (die, &attr);
7759 }
7760
7761 /* Add an range_list attribute value to a DIE.  */
7762
7763 static void
7764 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7765                    long unsigned int offset)
7766 {
7767   dw_attr_node attr;
7768
7769   attr.dw_attr = attr_kind;
7770   attr.dw_attr_val.val_class = dw_val_class_range_list;
7771   attr.dw_attr_val.v.val_offset = offset;
7772   add_dwarf_attr (die, &attr);
7773 }
7774
7775 /* Return the start label of a delta attribute.  */
7776
7777 static inline const char *
7778 AT_vms_delta1 (dw_attr_ref a)
7779 {
7780   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7781   return a->dw_attr_val.v.val_vms_delta.lbl1;
7782 }
7783
7784 /* Return the end label of a delta attribute.  */
7785
7786 static inline const char *
7787 AT_vms_delta2 (dw_attr_ref a)
7788 {
7789   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7790   return a->dw_attr_val.v.val_vms_delta.lbl2;
7791 }
7792
7793 static inline const char *
7794 AT_lbl (dw_attr_ref a)
7795 {
7796   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7797                     || AT_class (a) == dw_val_class_lineptr
7798                     || AT_class (a) == dw_val_class_macptr));
7799   return a->dw_attr_val.v.val_lbl_id;
7800 }
7801
7802 /* Get the attribute of type attr_kind.  */
7803
7804 static dw_attr_ref
7805 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7806 {
7807   dw_attr_ref a;
7808   unsigned ix;
7809   dw_die_ref spec = NULL;
7810
7811   if (! die)
7812     return NULL;
7813
7814   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7815     if (a->dw_attr == attr_kind)
7816       return a;
7817     else if (a->dw_attr == DW_AT_specification
7818              || a->dw_attr == DW_AT_abstract_origin)
7819       spec = AT_ref (a);
7820
7821   if (spec)
7822     return get_AT (spec, attr_kind);
7823
7824   return NULL;
7825 }
7826
7827 /* Return the "low pc" attribute value, typically associated with a subprogram
7828    DIE.  Return null if the "low pc" attribute is either not present, or if it
7829    cannot be represented as an assembler label identifier.  */
7830
7831 static inline const char *
7832 get_AT_low_pc (dw_die_ref die)
7833 {
7834   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7835
7836   return a ? AT_lbl (a) : NULL;
7837 }
7838
7839 /* Return the "high pc" attribute value, typically associated with a subprogram
7840    DIE.  Return null if the "high pc" attribute is either not present, or if it
7841    cannot be represented as an assembler label identifier.  */
7842
7843 static inline const char *
7844 get_AT_hi_pc (dw_die_ref die)
7845 {
7846   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7847
7848   return a ? AT_lbl (a) : NULL;
7849 }
7850
7851 /* Return the value of the string attribute designated by ATTR_KIND, or
7852    NULL if it is not present.  */
7853
7854 static inline const char *
7855 get_AT_string (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_string (a) : NULL;
7860 }
7861
7862 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7863    if it is not present.  */
7864
7865 static inline int
7866 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7867 {
7868   dw_attr_ref a = get_AT (die, attr_kind);
7869
7870   return a ? AT_flag (a) : 0;
7871 }
7872
7873 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7874    if it is not present.  */
7875
7876 static inline unsigned
7877 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7878 {
7879   dw_attr_ref a = get_AT (die, attr_kind);
7880
7881   return a ? AT_unsigned (a) : 0;
7882 }
7883
7884 static inline dw_die_ref
7885 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7886 {
7887   dw_attr_ref a = get_AT (die, attr_kind);
7888
7889   return a ? AT_ref (a) : NULL;
7890 }
7891
7892 static inline struct dwarf_file_data *
7893 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7894 {
7895   dw_attr_ref a = get_AT (die, attr_kind);
7896
7897   return a ? AT_file (a) : NULL;
7898 }
7899
7900 /* Return TRUE if the language is C++.  */
7901
7902 static inline bool
7903 is_cxx (void)
7904 {
7905   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7906
7907   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7908 }
7909
7910 /* Return TRUE if the language is Fortran.  */
7911
7912 static inline bool
7913 is_fortran (void)
7914 {
7915   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7916
7917   return (lang == DW_LANG_Fortran77
7918           || lang == DW_LANG_Fortran90
7919           || lang == DW_LANG_Fortran95);
7920 }
7921
7922 /* Return TRUE if the language is Ada.  */
7923
7924 static inline bool
7925 is_ada (void)
7926 {
7927   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7928
7929   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7930 }
7931
7932 /* Remove the specified attribute if present.  */
7933
7934 static void
7935 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7936 {
7937   dw_attr_ref a;
7938   unsigned ix;
7939
7940   if (! die)
7941     return;
7942
7943   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7944     if (a->dw_attr == attr_kind)
7945       {
7946         if (AT_class (a) == dw_val_class_str)
7947           if (a->dw_attr_val.v.val_str->refcount)
7948             a->dw_attr_val.v.val_str->refcount--;
7949
7950         /* VEC_ordered_remove should help reduce the number of abbrevs
7951            that are needed.  */
7952         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7953         return;
7954       }
7955 }
7956
7957 /* Remove CHILD from its parent.  PREV must have the property that
7958    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7959
7960 static void
7961 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7962 {
7963   gcc_assert (child->die_parent == prev->die_parent);
7964   gcc_assert (prev->die_sib == child);
7965   if (prev == child)
7966     {
7967       gcc_assert (child->die_parent->die_child == child);
7968       prev = NULL;
7969     }
7970   else
7971     prev->die_sib = child->die_sib;
7972   if (child->die_parent->die_child == child)
7973     child->die_parent->die_child = prev;
7974 }
7975
7976 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7977    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7978
7979 static void
7980 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7981 {
7982   dw_die_ref parent = old_child->die_parent;
7983
7984   gcc_assert (parent == prev->die_parent);
7985   gcc_assert (prev->die_sib == old_child);
7986
7987   new_child->die_parent = parent;
7988   if (prev == old_child)
7989     {
7990       gcc_assert (parent->die_child == old_child);
7991       new_child->die_sib = new_child;
7992     }
7993   else
7994     {
7995       prev->die_sib = new_child;
7996       new_child->die_sib = old_child->die_sib;
7997     }
7998   if (old_child->die_parent->die_child == old_child)
7999     old_child->die_parent->die_child = new_child;
8000 }
8001
8002 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8003
8004 static void
8005 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8006 {
8007   dw_die_ref c;
8008   new_parent->die_child = old_parent->die_child;
8009   old_parent->die_child = NULL;
8010   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8011 }
8012
8013 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8014    matches TAG.  */
8015
8016 static void
8017 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8018 {
8019   dw_die_ref c;
8020
8021   c = die->die_child;
8022   if (c) do {
8023     dw_die_ref prev = c;
8024     c = c->die_sib;
8025     while (c->die_tag == tag)
8026       {
8027         remove_child_with_prev (c, prev);
8028         /* Might have removed every child.  */
8029         if (c == c->die_sib)
8030           return;
8031         c = c->die_sib;
8032       }
8033   } while (c != die->die_child);
8034 }
8035
8036 /* Add a CHILD_DIE as the last child of DIE.  */
8037
8038 static void
8039 add_child_die (dw_die_ref die, dw_die_ref child_die)
8040 {
8041   /* FIXME this should probably be an assert.  */
8042   if (! die || ! child_die)
8043     return;
8044   gcc_assert (die != child_die);
8045
8046   child_die->die_parent = die;
8047   if (die->die_child)
8048     {
8049       child_die->die_sib = die->die_child->die_sib;
8050       die->die_child->die_sib = child_die;
8051     }
8052   else
8053     child_die->die_sib = child_die;
8054   die->die_child = child_die;
8055 }
8056
8057 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8058    is the specification, to the end of PARENT's list of children.
8059    This is done by removing and re-adding it.  */
8060
8061 static void
8062 splice_child_die (dw_die_ref parent, dw_die_ref child)
8063 {
8064   dw_die_ref p;
8065
8066   /* We want the declaration DIE from inside the class, not the
8067      specification DIE at toplevel.  */
8068   if (child->die_parent != parent)
8069     {
8070       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8071
8072       if (tmp)
8073         child = tmp;
8074     }
8075
8076   gcc_assert (child->die_parent == parent
8077               || (child->die_parent
8078                   == get_AT_ref (parent, DW_AT_specification)));
8079
8080   for (p = child->die_parent->die_child; ; p = p->die_sib)
8081     if (p->die_sib == child)
8082       {
8083         remove_child_with_prev (child, p);
8084         break;
8085       }
8086
8087   add_child_die (parent, child);
8088 }
8089
8090 /* Return a pointer to a newly created DIE node.  */
8091
8092 static inline dw_die_ref
8093 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8094 {
8095   dw_die_ref die = ggc_alloc_cleared_die_node ();
8096
8097   die->die_tag = tag_value;
8098
8099   if (parent_die != NULL)
8100     add_child_die (parent_die, die);
8101   else
8102     {
8103       limbo_die_node *limbo_node;
8104
8105       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8106       limbo_node->die = die;
8107       limbo_node->created_for = t;
8108       limbo_node->next = limbo_die_list;
8109       limbo_die_list = limbo_node;
8110     }
8111
8112   return die;
8113 }
8114
8115 /* Return the DIE associated with the given type specifier.  */
8116
8117 static inline dw_die_ref
8118 lookup_type_die (tree type)
8119 {
8120   return TYPE_SYMTAB_DIE (type);
8121 }
8122
8123 /* Like lookup_type_die, but if type is an anonymous type named by a
8124    typedef[1], return the DIE of the anonymous type instead the one of
8125    the naming typedef.  This is because in gen_typedef_die, we did
8126    equate the anonymous struct named by the typedef with the DIE of
8127    the naming typedef. So by default, lookup_type_die on an anonymous
8128    struct yields the DIE of the naming typedef.
8129
8130    [1]: Read the comment of is_naming_typedef_decl to learn about what
8131    a naming typedef is.  */
8132
8133 static inline dw_die_ref
8134 lookup_type_die_strip_naming_typedef (tree type)
8135 {
8136   dw_die_ref die = lookup_type_die (type);
8137   if (TREE_CODE (type) == RECORD_TYPE
8138       && die->die_tag == DW_TAG_typedef
8139       && is_naming_typedef_decl (TYPE_NAME (type)))
8140     die = get_AT_ref (die, DW_AT_type);
8141   return die;
8142 }
8143
8144 /* Equate a DIE to a given type specifier.  */
8145
8146 static inline void
8147 equate_type_number_to_die (tree type, dw_die_ref type_die)
8148 {
8149   TYPE_SYMTAB_DIE (type) = type_die;
8150 }
8151
8152 /* Returns a hash value for X (which really is a die_struct).  */
8153
8154 static hashval_t
8155 decl_die_table_hash (const void *x)
8156 {
8157   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8158 }
8159
8160 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8161
8162 static int
8163 decl_die_table_eq (const void *x, const void *y)
8164 {
8165   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8166 }
8167
8168 /* Return the DIE associated with a given declaration.  */
8169
8170 static inline dw_die_ref
8171 lookup_decl_die (tree decl)
8172 {
8173   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8174 }
8175
8176 /* Returns a hash value for X (which really is a var_loc_list).  */
8177
8178 static hashval_t
8179 decl_loc_table_hash (const void *x)
8180 {
8181   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8182 }
8183
8184 /* Return nonzero if decl_id of var_loc_list X is the same as
8185    UID of decl *Y.  */
8186
8187 static int
8188 decl_loc_table_eq (const void *x, const void *y)
8189 {
8190   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8191 }
8192
8193 /* Return the var_loc list associated with a given declaration.  */
8194
8195 static inline var_loc_list *
8196 lookup_decl_loc (const_tree decl)
8197 {
8198   if (!decl_loc_table)
8199     return NULL;
8200   return (var_loc_list *)
8201     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8202 }
8203
8204 /* Equate a DIE to a particular declaration.  */
8205
8206 static void
8207 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8208 {
8209   unsigned int decl_id = DECL_UID (decl);
8210   void **slot;
8211
8212   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8213   *slot = decl_die;
8214   decl_die->decl_id = decl_id;
8215 }
8216
8217 /* Return how many bits covers PIECE EXPR_LIST.  */
8218
8219 static int
8220 decl_piece_bitsize (rtx piece)
8221 {
8222   int ret = (int) GET_MODE (piece);
8223   if (ret)
8224     return ret;
8225   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8226               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8227   return INTVAL (XEXP (XEXP (piece, 0), 0));
8228 }
8229
8230 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8231
8232 static rtx *
8233 decl_piece_varloc_ptr (rtx piece)
8234 {
8235   if ((int) GET_MODE (piece))
8236     return &XEXP (piece, 0);
8237   else
8238     return &XEXP (XEXP (piece, 0), 1);
8239 }
8240
8241 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8242    Next is the chain of following piece nodes.  */
8243
8244 static rtx
8245 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8246 {
8247   if (bitsize <= (int) MAX_MACHINE_MODE)
8248     return alloc_EXPR_LIST (bitsize, loc_note, next);
8249   else
8250     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8251                                                GEN_INT (bitsize),
8252                                                loc_note), next);
8253 }
8254
8255 /* Return rtx that should be stored into loc field for
8256    LOC_NOTE and BITPOS/BITSIZE.  */
8257
8258 static rtx
8259 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8260                       HOST_WIDE_INT bitsize)
8261 {
8262   if (bitsize != -1)
8263     {
8264       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8265       if (bitpos != 0)
8266         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8267     }
8268   return loc_note;
8269 }
8270
8271 /* This function either modifies location piece list *DEST in
8272    place (if SRC and INNER is NULL), or copies location piece list
8273    *SRC to *DEST while modifying it.  Location BITPOS is modified
8274    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8275    not copied and if needed some padding around it is added.
8276    When modifying in place, DEST should point to EXPR_LIST where
8277    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8278    to the start of the whole list and INNER points to the EXPR_LIST
8279    where earlier pieces cover PIECE_BITPOS bits.  */
8280
8281 static void
8282 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8283                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8284                    HOST_WIDE_INT bitsize, rtx loc_note)
8285 {
8286   int diff;
8287   bool copy = inner != NULL;
8288
8289   if (copy)
8290     {
8291       /* First copy all nodes preceeding the current bitpos.  */
8292       while (src != inner)
8293         {
8294           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8295                                    decl_piece_bitsize (*src), NULL_RTX);
8296           dest = &XEXP (*dest, 1);
8297           src = &XEXP (*src, 1);
8298         }
8299     }
8300   /* Add padding if needed.  */
8301   if (bitpos != piece_bitpos)
8302     {
8303       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8304                                copy ? NULL_RTX : *dest);
8305       dest = &XEXP (*dest, 1);
8306     }
8307   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8308     {
8309       gcc_assert (!copy);
8310       /* A piece with correct bitpos and bitsize already exist,
8311          just update the location for it and return.  */
8312       *decl_piece_varloc_ptr (*dest) = loc_note;
8313       return;
8314     }
8315   /* Add the piece that changed.  */
8316   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8317   dest = &XEXP (*dest, 1);
8318   /* Skip over pieces that overlap it.  */
8319   diff = bitpos - piece_bitpos + bitsize;
8320   if (!copy)
8321     src = dest;
8322   while (diff > 0 && *src)
8323     {
8324       rtx piece = *src;
8325       diff -= decl_piece_bitsize (piece);
8326       if (copy)
8327         src = &XEXP (piece, 1);
8328       else
8329         {
8330           *src = XEXP (piece, 1);
8331           free_EXPR_LIST_node (piece);
8332         }
8333     }
8334   /* Add padding if needed.  */
8335   if (diff < 0 && *src)
8336     {
8337       if (!copy)
8338         dest = src;
8339       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8340       dest = &XEXP (*dest, 1);
8341     }
8342   if (!copy)
8343     return;
8344   /* Finally copy all nodes following it.  */
8345   while (*src)
8346     {
8347       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8348                                decl_piece_bitsize (*src), NULL_RTX);
8349       dest = &XEXP (*dest, 1);
8350       src = &XEXP (*src, 1);
8351     }
8352 }
8353
8354 /* Add a variable location node to the linked list for DECL.  */
8355
8356 static struct var_loc_node *
8357 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8358 {
8359   unsigned int decl_id;
8360   var_loc_list *temp;
8361   void **slot;
8362   struct var_loc_node *loc = NULL;
8363   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8364
8365   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8366     {
8367       tree realdecl = DECL_DEBUG_EXPR (decl);
8368       if (realdecl && handled_component_p (realdecl))
8369         {
8370           HOST_WIDE_INT maxsize;
8371           tree innerdecl;
8372           innerdecl
8373             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8374           if (!DECL_P (innerdecl)
8375               || DECL_IGNORED_P (innerdecl)
8376               || TREE_STATIC (innerdecl)
8377               || bitsize <= 0
8378               || bitpos + bitsize > 256
8379               || bitsize != maxsize)
8380             return NULL;
8381           decl = innerdecl;
8382         }
8383     }
8384
8385   decl_id = DECL_UID (decl);
8386   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8387   if (*slot == NULL)
8388     {
8389       temp = ggc_alloc_cleared_var_loc_list ();
8390       temp->decl_id = decl_id;
8391       *slot = temp;
8392     }
8393   else
8394     temp = (var_loc_list *) *slot;
8395
8396   if (temp->last)
8397     {
8398       struct var_loc_node *last = temp->last, *unused = NULL;
8399       rtx *piece_loc = NULL, last_loc_note;
8400       int piece_bitpos = 0;
8401       if (last->next)
8402         {
8403           last = last->next;
8404           gcc_assert (last->next == NULL);
8405         }
8406       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8407         {
8408           piece_loc = &last->loc;
8409           do
8410             {
8411               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8412               if (piece_bitpos + cur_bitsize > bitpos)
8413                 break;
8414               piece_bitpos += cur_bitsize;
8415               piece_loc = &XEXP (*piece_loc, 1);
8416             }
8417           while (*piece_loc);
8418         }
8419       /* TEMP->LAST here is either pointer to the last but one or
8420          last element in the chained list, LAST is pointer to the
8421          last element.  */
8422       if (label && strcmp (last->label, label) == 0)
8423         {
8424           /* For SRA optimized variables if there weren't any real
8425              insns since last note, just modify the last node.  */
8426           if (piece_loc != NULL)
8427             {
8428               adjust_piece_list (piece_loc, NULL, NULL,
8429                                  bitpos, piece_bitpos, bitsize, loc_note);
8430               return NULL;
8431             }
8432           /* If the last note doesn't cover any instructions, remove it.  */
8433           if (temp->last != last)
8434             {
8435               temp->last->next = NULL;
8436               unused = last;
8437               last = temp->last;
8438               gcc_assert (strcmp (last->label, label) != 0);
8439             }
8440           else
8441             {
8442               gcc_assert (temp->first == temp->last);
8443               memset (temp->last, '\0', sizeof (*temp->last));
8444               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8445               return temp->last;
8446             }
8447         }
8448       if (bitsize == -1 && NOTE_P (last->loc))
8449         last_loc_note = last->loc;
8450       else if (piece_loc != NULL
8451                && *piece_loc != NULL_RTX
8452                && piece_bitpos == bitpos
8453                && decl_piece_bitsize (*piece_loc) == bitsize)
8454         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8455       else
8456         last_loc_note = NULL_RTX;
8457       /* If the current location is the same as the end of the list,
8458          and either both or neither of the locations is uninitialized,
8459          we have nothing to do.  */
8460       if (last_loc_note == NULL_RTX
8461           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8462                             NOTE_VAR_LOCATION_LOC (loc_note)))
8463           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8464                != NOTE_VAR_LOCATION_STATUS (loc_note))
8465               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8466                    == VAR_INIT_STATUS_UNINITIALIZED)
8467                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8468                       == VAR_INIT_STATUS_UNINITIALIZED))))
8469         {
8470           /* Add LOC to the end of list and update LAST.  If the last
8471              element of the list has been removed above, reuse its
8472              memory for the new node, otherwise allocate a new one.  */
8473           if (unused)
8474             {
8475               loc = unused;
8476               memset (loc, '\0', sizeof (*loc));
8477             }
8478           else
8479             loc = ggc_alloc_cleared_var_loc_node ();
8480           if (bitsize == -1 || piece_loc == NULL)
8481             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8482           else
8483             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8484                                bitpos, piece_bitpos, bitsize, loc_note);
8485           last->next = loc;
8486           /* Ensure TEMP->LAST will point either to the new last but one
8487              element of the chain, or to the last element in it.  */
8488           if (last != temp->last)
8489             temp->last = last;
8490         }
8491       else if (unused)
8492         ggc_free (unused);
8493     }
8494   else
8495     {
8496       loc = ggc_alloc_cleared_var_loc_node ();
8497       temp->first = loc;
8498       temp->last = loc;
8499       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8500     }
8501   return loc;
8502 }
8503 \f
8504 /* Keep track of the number of spaces used to indent the
8505    output of the debugging routines that print the structure of
8506    the DIE internal representation.  */
8507 static int print_indent;
8508
8509 /* Indent the line the number of spaces given by print_indent.  */
8510
8511 static inline void
8512 print_spaces (FILE *outfile)
8513 {
8514   fprintf (outfile, "%*s", print_indent, "");
8515 }
8516
8517 /* Print a type signature in hex.  */
8518
8519 static inline void
8520 print_signature (FILE *outfile, char *sig)
8521 {
8522   int i;
8523
8524   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8525     fprintf (outfile, "%02x", sig[i] & 0xff);
8526 }
8527
8528 /* Print the information associated with a given DIE, and its children.
8529    This routine is a debugging aid only.  */
8530
8531 static void
8532 print_die (dw_die_ref die, FILE *outfile)
8533 {
8534   dw_attr_ref a;
8535   dw_die_ref c;
8536   unsigned ix;
8537
8538   print_spaces (outfile);
8539   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8540            die->die_offset, dwarf_tag_name (die->die_tag),
8541            (void*) die);
8542   print_spaces (outfile);
8543   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8544   fprintf (outfile, " offset: %ld", die->die_offset);
8545   fprintf (outfile, " mark: %d\n", die->die_mark);
8546
8547   if (dwarf_version >= 4 && die->die_id.die_type_node)
8548     {
8549       print_spaces (outfile);
8550       fprintf (outfile, "  signature: ");
8551       print_signature (outfile, die->die_id.die_type_node->signature);
8552       fprintf (outfile, "\n");
8553     }
8554
8555   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8556     {
8557       print_spaces (outfile);
8558       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8559
8560       switch (AT_class (a))
8561         {
8562         case dw_val_class_addr:
8563           fprintf (outfile, "address");
8564           break;
8565         case dw_val_class_offset:
8566           fprintf (outfile, "offset");
8567           break;
8568         case dw_val_class_loc:
8569           fprintf (outfile, "location descriptor");
8570           break;
8571         case dw_val_class_loc_list:
8572           fprintf (outfile, "location list -> label:%s",
8573                    AT_loc_list (a)->ll_symbol);
8574           break;
8575         case dw_val_class_range_list:
8576           fprintf (outfile, "range list");
8577           break;
8578         case dw_val_class_const:
8579           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8580           break;
8581         case dw_val_class_unsigned_const:
8582           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8583           break;
8584         case dw_val_class_const_double:
8585           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8586                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8587                    a->dw_attr_val.v.val_double.high,
8588                    a->dw_attr_val.v.val_double.low);
8589           break;
8590         case dw_val_class_vec:
8591           fprintf (outfile, "floating-point or vector constant");
8592           break;
8593         case dw_val_class_flag:
8594           fprintf (outfile, "%u", AT_flag (a));
8595           break;
8596         case dw_val_class_die_ref:
8597           if (AT_ref (a) != NULL)
8598             {
8599               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8600                 {
8601                   fprintf (outfile, "die -> signature: ");
8602                   print_signature (outfile,
8603                                    AT_ref (a)->die_id.die_type_node->signature);
8604                 }
8605               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8606                 fprintf (outfile, "die -> label: %s",
8607                          AT_ref (a)->die_id.die_symbol);
8608               else
8609                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8610               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8611             }
8612           else
8613             fprintf (outfile, "die -> <null>");
8614           break;
8615         case dw_val_class_vms_delta:
8616           fprintf (outfile, "delta: @slotcount(%s-%s)",
8617                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8618           break;
8619         case dw_val_class_lbl_id:
8620         case dw_val_class_lineptr:
8621         case dw_val_class_macptr:
8622           fprintf (outfile, "label: %s", AT_lbl (a));
8623           break;
8624         case dw_val_class_str:
8625           if (AT_string (a) != NULL)
8626             fprintf (outfile, "\"%s\"", AT_string (a));
8627           else
8628             fprintf (outfile, "<null>");
8629           break;
8630         case dw_val_class_file:
8631           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8632                    AT_file (a)->emitted_number);
8633           break;
8634         case dw_val_class_data8:
8635           {
8636             int i;
8637
8638             for (i = 0; i < 8; i++)
8639               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8640             break;
8641           }
8642         default:
8643           break;
8644         }
8645
8646       fprintf (outfile, "\n");
8647     }
8648
8649   if (die->die_child != NULL)
8650     {
8651       print_indent += 4;
8652       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8653       print_indent -= 4;
8654     }
8655   if (print_indent == 0)
8656     fprintf (outfile, "\n");
8657 }
8658
8659 /* Print the contents of the source code line number correspondence table.
8660    This routine is a debugging aid only.  */
8661
8662 static void
8663 print_dwarf_line_table (FILE *outfile)
8664 {
8665   unsigned i;
8666   dw_line_info_ref line_info;
8667
8668   fprintf (outfile, "\n\nDWARF source line information\n");
8669   for (i = 1; i < line_info_table_in_use; i++)
8670     {
8671       line_info = &line_info_table[i];
8672       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8673                line_info->dw_file_num,
8674                line_info->dw_line_num);
8675     }
8676
8677   fprintf (outfile, "\n\n");
8678 }
8679
8680 /* Print the information collected for a given DIE.  */
8681
8682 DEBUG_FUNCTION void
8683 debug_dwarf_die (dw_die_ref die)
8684 {
8685   print_die (die, stderr);
8686 }
8687
8688 /* Print all DWARF information collected for the compilation unit.
8689    This routine is a debugging aid only.  */
8690
8691 DEBUG_FUNCTION void
8692 debug_dwarf (void)
8693 {
8694   print_indent = 0;
8695   print_die (comp_unit_die (), stderr);
8696   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8697     print_dwarf_line_table (stderr);
8698 }
8699 \f
8700 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8701    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8702    DIE that marks the start of the DIEs for this include file.  */
8703
8704 static dw_die_ref
8705 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8706 {
8707   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8708   dw_die_ref new_unit = gen_compile_unit_die (filename);
8709
8710   new_unit->die_sib = old_unit;
8711   return new_unit;
8712 }
8713
8714 /* Close an include-file CU and reopen the enclosing one.  */
8715
8716 static dw_die_ref
8717 pop_compile_unit (dw_die_ref old_unit)
8718 {
8719   dw_die_ref new_unit = old_unit->die_sib;
8720
8721   old_unit->die_sib = NULL;
8722   return new_unit;
8723 }
8724
8725 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8726 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8727
8728 /* Calculate the checksum of a location expression.  */
8729
8730 static inline void
8731 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8732 {
8733   int tem;
8734
8735   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8736   CHECKSUM (tem);
8737   CHECKSUM (loc->dw_loc_oprnd1);
8738   CHECKSUM (loc->dw_loc_oprnd2);
8739 }
8740
8741 /* Calculate the checksum of an attribute.  */
8742
8743 static void
8744 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8745 {
8746   dw_loc_descr_ref loc;
8747   rtx r;
8748
8749   CHECKSUM (at->dw_attr);
8750
8751   /* We don't care that this was compiled with a different compiler
8752      snapshot; if the output is the same, that's what matters.  */
8753   if (at->dw_attr == DW_AT_producer)
8754     return;
8755
8756   switch (AT_class (at))
8757     {
8758     case dw_val_class_const:
8759       CHECKSUM (at->dw_attr_val.v.val_int);
8760       break;
8761     case dw_val_class_unsigned_const:
8762       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8763       break;
8764     case dw_val_class_const_double:
8765       CHECKSUM (at->dw_attr_val.v.val_double);
8766       break;
8767     case dw_val_class_vec:
8768       CHECKSUM (at->dw_attr_val.v.val_vec);
8769       break;
8770     case dw_val_class_flag:
8771       CHECKSUM (at->dw_attr_val.v.val_flag);
8772       break;
8773     case dw_val_class_str:
8774       CHECKSUM_STRING (AT_string (at));
8775       break;
8776
8777     case dw_val_class_addr:
8778       r = AT_addr (at);
8779       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8780       CHECKSUM_STRING (XSTR (r, 0));
8781       break;
8782
8783     case dw_val_class_offset:
8784       CHECKSUM (at->dw_attr_val.v.val_offset);
8785       break;
8786
8787     case dw_val_class_loc:
8788       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8789         loc_checksum (loc, ctx);
8790       break;
8791
8792     case dw_val_class_die_ref:
8793       die_checksum (AT_ref (at), ctx, mark);
8794       break;
8795
8796     case dw_val_class_fde_ref:
8797     case dw_val_class_vms_delta:
8798     case dw_val_class_lbl_id:
8799     case dw_val_class_lineptr:
8800     case dw_val_class_macptr:
8801       break;
8802
8803     case dw_val_class_file:
8804       CHECKSUM_STRING (AT_file (at)->filename);
8805       break;
8806
8807     case dw_val_class_data8:
8808       CHECKSUM (at->dw_attr_val.v.val_data8);
8809       break;
8810
8811     default:
8812       break;
8813     }
8814 }
8815
8816 /* Calculate the checksum of a DIE.  */
8817
8818 static void
8819 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8820 {
8821   dw_die_ref c;
8822   dw_attr_ref a;
8823   unsigned ix;
8824
8825   /* To avoid infinite recursion.  */
8826   if (die->die_mark)
8827     {
8828       CHECKSUM (die->die_mark);
8829       return;
8830     }
8831   die->die_mark = ++(*mark);
8832
8833   CHECKSUM (die->die_tag);
8834
8835   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8836     attr_checksum (a, ctx, mark);
8837
8838   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8839 }
8840
8841 #undef CHECKSUM
8842 #undef CHECKSUM_STRING
8843
8844 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8845 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8846 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8847 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8848 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8849 #define CHECKSUM_ATTR(FOO) \
8850   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8851
8852 /* Calculate the checksum of a number in signed LEB128 format.  */
8853
8854 static void
8855 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8856 {
8857   unsigned char byte;
8858   bool more;
8859
8860   while (1)
8861     {
8862       byte = (value & 0x7f);
8863       value >>= 7;
8864       more = !((value == 0 && (byte & 0x40) == 0)
8865                 || (value == -1 && (byte & 0x40) != 0));
8866       if (more)
8867         byte |= 0x80;
8868       CHECKSUM (byte);
8869       if (!more)
8870         break;
8871     }
8872 }
8873
8874 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8875
8876 static void
8877 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8878 {
8879   while (1)
8880     {
8881       unsigned char byte = (value & 0x7f);
8882       value >>= 7;
8883       if (value != 0)
8884         /* More bytes to follow.  */
8885         byte |= 0x80;
8886       CHECKSUM (byte);
8887       if (value == 0)
8888         break;
8889     }
8890 }
8891
8892 /* Checksum the context of the DIE.  This adds the names of any
8893    surrounding namespaces or structures to the checksum.  */
8894
8895 static void
8896 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8897 {
8898   const char *name;
8899   dw_die_ref spec;
8900   int tag = die->die_tag;
8901
8902   if (tag != DW_TAG_namespace
8903       && tag != DW_TAG_structure_type
8904       && tag != DW_TAG_class_type)
8905     return;
8906
8907   name = get_AT_string (die, DW_AT_name);
8908
8909   spec = get_AT_ref (die, DW_AT_specification);
8910   if (spec != NULL)
8911     die = spec;
8912
8913   if (die->die_parent != NULL)
8914     checksum_die_context (die->die_parent, ctx);
8915
8916   CHECKSUM_ULEB128 ('C');
8917   CHECKSUM_ULEB128 (tag);
8918   if (name != NULL)
8919     CHECKSUM_STRING (name);
8920 }
8921
8922 /* Calculate the checksum of a location expression.  */
8923
8924 static inline void
8925 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8926 {
8927   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8928      were emitted as a DW_FORM_sdata instead of a location expression.  */
8929   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8930     {
8931       CHECKSUM_ULEB128 (DW_FORM_sdata);
8932       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8933       return;
8934     }
8935
8936   /* Otherwise, just checksum the raw location expression.  */
8937   while (loc != NULL)
8938     {
8939       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8940       CHECKSUM (loc->dw_loc_oprnd1);
8941       CHECKSUM (loc->dw_loc_oprnd2);
8942       loc = loc->dw_loc_next;
8943     }
8944 }
8945
8946 /* Calculate the checksum of an attribute.  */
8947
8948 static void
8949 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8950                        struct md5_ctx *ctx, int *mark)
8951 {
8952   dw_loc_descr_ref loc;
8953   rtx r;
8954
8955   if (AT_class (at) == dw_val_class_die_ref)
8956     {
8957       dw_die_ref target_die = AT_ref (at);
8958
8959       /* For pointer and reference types, we checksum only the (qualified)
8960          name of the target type (if there is a name).  For friend entries,
8961          we checksum only the (qualified) name of the target type or function.
8962          This allows the checksum to remain the same whether the target type
8963          is complete or not.  */
8964       if ((at->dw_attr == DW_AT_type
8965            && (tag == DW_TAG_pointer_type
8966                || tag == DW_TAG_reference_type
8967                || tag == DW_TAG_rvalue_reference_type
8968                || tag == DW_TAG_ptr_to_member_type))
8969           || (at->dw_attr == DW_AT_friend
8970               && tag == DW_TAG_friend))
8971         {
8972           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8973
8974           if (name_attr != NULL)
8975             {
8976               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8977
8978               if (decl == NULL)
8979                 decl = target_die;
8980               CHECKSUM_ULEB128 ('N');
8981               CHECKSUM_ULEB128 (at->dw_attr);
8982               if (decl->die_parent != NULL)
8983                 checksum_die_context (decl->die_parent, ctx);
8984               CHECKSUM_ULEB128 ('E');
8985               CHECKSUM_STRING (AT_string (name_attr));
8986               return;
8987             }
8988         }
8989
8990       /* For all other references to another DIE, we check to see if the
8991          target DIE has already been visited.  If it has, we emit a
8992          backward reference; if not, we descend recursively.  */
8993       if (target_die->die_mark > 0)
8994         {
8995           CHECKSUM_ULEB128 ('R');
8996           CHECKSUM_ULEB128 (at->dw_attr);
8997           CHECKSUM_ULEB128 (target_die->die_mark);
8998         }
8999       else
9000         {
9001           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9002
9003           if (decl == NULL)
9004             decl = target_die;
9005           target_die->die_mark = ++(*mark);
9006           CHECKSUM_ULEB128 ('T');
9007           CHECKSUM_ULEB128 (at->dw_attr);
9008           if (decl->die_parent != NULL)
9009             checksum_die_context (decl->die_parent, ctx);
9010           die_checksum_ordered (target_die, ctx, mark);
9011         }
9012       return;
9013     }
9014
9015   CHECKSUM_ULEB128 ('A');
9016   CHECKSUM_ULEB128 (at->dw_attr);
9017
9018   switch (AT_class (at))
9019     {
9020     case dw_val_class_const:
9021       CHECKSUM_ULEB128 (DW_FORM_sdata);
9022       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9023       break;
9024
9025     case dw_val_class_unsigned_const:
9026       CHECKSUM_ULEB128 (DW_FORM_sdata);
9027       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9028       break;
9029
9030     case dw_val_class_const_double:
9031       CHECKSUM_ULEB128 (DW_FORM_block);
9032       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9033       CHECKSUM (at->dw_attr_val.v.val_double);
9034       break;
9035
9036     case dw_val_class_vec:
9037       CHECKSUM_ULEB128 (DW_FORM_block);
9038       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9039       CHECKSUM (at->dw_attr_val.v.val_vec);
9040       break;
9041
9042     case dw_val_class_flag:
9043       CHECKSUM_ULEB128 (DW_FORM_flag);
9044       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9045       break;
9046
9047     case dw_val_class_str:
9048       CHECKSUM_ULEB128 (DW_FORM_string);
9049       CHECKSUM_STRING (AT_string (at));
9050       break;
9051
9052     case dw_val_class_addr:
9053       r = AT_addr (at);
9054       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9055       CHECKSUM_ULEB128 (DW_FORM_string);
9056       CHECKSUM_STRING (XSTR (r, 0));
9057       break;
9058
9059     case dw_val_class_offset:
9060       CHECKSUM_ULEB128 (DW_FORM_sdata);
9061       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9062       break;
9063
9064     case dw_val_class_loc:
9065       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9066         loc_checksum_ordered (loc, ctx);
9067       break;
9068
9069     case dw_val_class_fde_ref:
9070     case dw_val_class_lbl_id:
9071     case dw_val_class_lineptr:
9072     case dw_val_class_macptr:
9073       break;
9074
9075     case dw_val_class_file:
9076       CHECKSUM_ULEB128 (DW_FORM_string);
9077       CHECKSUM_STRING (AT_file (at)->filename);
9078       break;
9079
9080     case dw_val_class_data8:
9081       CHECKSUM (at->dw_attr_val.v.val_data8);
9082       break;
9083
9084     default:
9085       break;
9086     }
9087 }
9088
9089 struct checksum_attributes
9090 {
9091   dw_attr_ref at_name;
9092   dw_attr_ref at_type;
9093   dw_attr_ref at_friend;
9094   dw_attr_ref at_accessibility;
9095   dw_attr_ref at_address_class;
9096   dw_attr_ref at_allocated;
9097   dw_attr_ref at_artificial;
9098   dw_attr_ref at_associated;
9099   dw_attr_ref at_binary_scale;
9100   dw_attr_ref at_bit_offset;
9101   dw_attr_ref at_bit_size;
9102   dw_attr_ref at_bit_stride;
9103   dw_attr_ref at_byte_size;
9104   dw_attr_ref at_byte_stride;
9105   dw_attr_ref at_const_value;
9106   dw_attr_ref at_containing_type;
9107   dw_attr_ref at_count;
9108   dw_attr_ref at_data_location;
9109   dw_attr_ref at_data_member_location;
9110   dw_attr_ref at_decimal_scale;
9111   dw_attr_ref at_decimal_sign;
9112   dw_attr_ref at_default_value;
9113   dw_attr_ref at_digit_count;
9114   dw_attr_ref at_discr;
9115   dw_attr_ref at_discr_list;
9116   dw_attr_ref at_discr_value;
9117   dw_attr_ref at_encoding;
9118   dw_attr_ref at_endianity;
9119   dw_attr_ref at_explicit;
9120   dw_attr_ref at_is_optional;
9121   dw_attr_ref at_location;
9122   dw_attr_ref at_lower_bound;
9123   dw_attr_ref at_mutable;
9124   dw_attr_ref at_ordering;
9125   dw_attr_ref at_picture_string;
9126   dw_attr_ref at_prototyped;
9127   dw_attr_ref at_small;
9128   dw_attr_ref at_segment;
9129   dw_attr_ref at_string_length;
9130   dw_attr_ref at_threads_scaled;
9131   dw_attr_ref at_upper_bound;
9132   dw_attr_ref at_use_location;
9133   dw_attr_ref at_use_UTF8;
9134   dw_attr_ref at_variable_parameter;
9135   dw_attr_ref at_virtuality;
9136   dw_attr_ref at_visibility;
9137   dw_attr_ref at_vtable_elem_location;
9138 };
9139
9140 /* Collect the attributes that we will want to use for the checksum.  */
9141
9142 static void
9143 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9144 {
9145   dw_attr_ref a;
9146   unsigned ix;
9147
9148   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9149     {
9150       switch (a->dw_attr)
9151         {
9152         case DW_AT_name:
9153           attrs->at_name = a;
9154           break;
9155         case DW_AT_type:
9156           attrs->at_type = a;
9157           break;
9158         case DW_AT_friend:
9159           attrs->at_friend = a;
9160           break;
9161         case DW_AT_accessibility:
9162           attrs->at_accessibility = a;
9163           break;
9164         case DW_AT_address_class:
9165           attrs->at_address_class = a;
9166           break;
9167         case DW_AT_allocated:
9168           attrs->at_allocated = a;
9169           break;
9170         case DW_AT_artificial:
9171           attrs->at_artificial = a;
9172           break;
9173         case DW_AT_associated:
9174           attrs->at_associated = a;
9175           break;
9176         case DW_AT_binary_scale:
9177           attrs->at_binary_scale = a;
9178           break;
9179         case DW_AT_bit_offset:
9180           attrs->at_bit_offset = a;
9181           break;
9182         case DW_AT_bit_size:
9183           attrs->at_bit_size = a;
9184           break;
9185         case DW_AT_bit_stride:
9186           attrs->at_bit_stride = a;
9187           break;
9188         case DW_AT_byte_size:
9189           attrs->at_byte_size = a;
9190           break;
9191         case DW_AT_byte_stride:
9192           attrs->at_byte_stride = a;
9193           break;
9194         case DW_AT_const_value:
9195           attrs->at_const_value = a;
9196           break;
9197         case DW_AT_containing_type:
9198           attrs->at_containing_type = a;
9199           break;
9200         case DW_AT_count:
9201           attrs->at_count = a;
9202           break;
9203         case DW_AT_data_location:
9204           attrs->at_data_location = a;
9205           break;
9206         case DW_AT_data_member_location:
9207           attrs->at_data_member_location = a;
9208           break;
9209         case DW_AT_decimal_scale:
9210           attrs->at_decimal_scale = a;
9211           break;
9212         case DW_AT_decimal_sign:
9213           attrs->at_decimal_sign = a;
9214           break;
9215         case DW_AT_default_value:
9216           attrs->at_default_value = a;
9217           break;
9218         case DW_AT_digit_count:
9219           attrs->at_digit_count = a;
9220           break;
9221         case DW_AT_discr:
9222           attrs->at_discr = a;
9223           break;
9224         case DW_AT_discr_list:
9225           attrs->at_discr_list = a;
9226           break;
9227         case DW_AT_discr_value:
9228           attrs->at_discr_value = a;
9229           break;
9230         case DW_AT_encoding:
9231           attrs->at_encoding = a;
9232           break;
9233         case DW_AT_endianity:
9234           attrs->at_endianity = a;
9235           break;
9236         case DW_AT_explicit:
9237           attrs->at_explicit = a;
9238           break;
9239         case DW_AT_is_optional:
9240           attrs->at_is_optional = a;
9241           break;
9242         case DW_AT_location:
9243           attrs->at_location = a;
9244           break;
9245         case DW_AT_lower_bound:
9246           attrs->at_lower_bound = a;
9247           break;
9248         case DW_AT_mutable:
9249           attrs->at_mutable = a;
9250           break;
9251         case DW_AT_ordering:
9252           attrs->at_ordering = a;
9253           break;
9254         case DW_AT_picture_string:
9255           attrs->at_picture_string = a;
9256           break;
9257         case DW_AT_prototyped:
9258           attrs->at_prototyped = a;
9259           break;
9260         case DW_AT_small:
9261           attrs->at_small = a;
9262           break;
9263         case DW_AT_segment:
9264           attrs->at_segment = a;
9265           break;
9266         case DW_AT_string_length:
9267           attrs->at_string_length = a;
9268           break;
9269         case DW_AT_threads_scaled:
9270           attrs->at_threads_scaled = a;
9271           break;
9272         case DW_AT_upper_bound:
9273           attrs->at_upper_bound = a;
9274           break;
9275         case DW_AT_use_location:
9276           attrs->at_use_location = a;
9277           break;
9278         case DW_AT_use_UTF8:
9279           attrs->at_use_UTF8 = a;
9280           break;
9281         case DW_AT_variable_parameter:
9282           attrs->at_variable_parameter = a;
9283           break;
9284         case DW_AT_virtuality:
9285           attrs->at_virtuality = a;
9286           break;
9287         case DW_AT_visibility:
9288           attrs->at_visibility = a;
9289           break;
9290         case DW_AT_vtable_elem_location:
9291           attrs->at_vtable_elem_location = a;
9292           break;
9293         default:
9294           break;
9295         }
9296     }
9297 }
9298
9299 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9300
9301 static void
9302 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9303 {
9304   dw_die_ref c;
9305   dw_die_ref decl;
9306   struct checksum_attributes attrs;
9307
9308   CHECKSUM_ULEB128 ('D');
9309   CHECKSUM_ULEB128 (die->die_tag);
9310
9311   memset (&attrs, 0, sizeof (attrs));
9312
9313   decl = get_AT_ref (die, DW_AT_specification);
9314   if (decl != NULL)
9315     collect_checksum_attributes (&attrs, decl);
9316   collect_checksum_attributes (&attrs, die);
9317
9318   CHECKSUM_ATTR (attrs.at_name);
9319   CHECKSUM_ATTR (attrs.at_accessibility);
9320   CHECKSUM_ATTR (attrs.at_address_class);
9321   CHECKSUM_ATTR (attrs.at_allocated);
9322   CHECKSUM_ATTR (attrs.at_artificial);
9323   CHECKSUM_ATTR (attrs.at_associated);
9324   CHECKSUM_ATTR (attrs.at_binary_scale);
9325   CHECKSUM_ATTR (attrs.at_bit_offset);
9326   CHECKSUM_ATTR (attrs.at_bit_size);
9327   CHECKSUM_ATTR (attrs.at_bit_stride);
9328   CHECKSUM_ATTR (attrs.at_byte_size);
9329   CHECKSUM_ATTR (attrs.at_byte_stride);
9330   CHECKSUM_ATTR (attrs.at_const_value);
9331   CHECKSUM_ATTR (attrs.at_containing_type);
9332   CHECKSUM_ATTR (attrs.at_count);
9333   CHECKSUM_ATTR (attrs.at_data_location);
9334   CHECKSUM_ATTR (attrs.at_data_member_location);
9335   CHECKSUM_ATTR (attrs.at_decimal_scale);
9336   CHECKSUM_ATTR (attrs.at_decimal_sign);
9337   CHECKSUM_ATTR (attrs.at_default_value);
9338   CHECKSUM_ATTR (attrs.at_digit_count);
9339   CHECKSUM_ATTR (attrs.at_discr);
9340   CHECKSUM_ATTR (attrs.at_discr_list);
9341   CHECKSUM_ATTR (attrs.at_discr_value);
9342   CHECKSUM_ATTR (attrs.at_encoding);
9343   CHECKSUM_ATTR (attrs.at_endianity);
9344   CHECKSUM_ATTR (attrs.at_explicit);
9345   CHECKSUM_ATTR (attrs.at_is_optional);
9346   CHECKSUM_ATTR (attrs.at_location);
9347   CHECKSUM_ATTR (attrs.at_lower_bound);
9348   CHECKSUM_ATTR (attrs.at_mutable);
9349   CHECKSUM_ATTR (attrs.at_ordering);
9350   CHECKSUM_ATTR (attrs.at_picture_string);
9351   CHECKSUM_ATTR (attrs.at_prototyped);
9352   CHECKSUM_ATTR (attrs.at_small);
9353   CHECKSUM_ATTR (attrs.at_segment);
9354   CHECKSUM_ATTR (attrs.at_string_length);
9355   CHECKSUM_ATTR (attrs.at_threads_scaled);
9356   CHECKSUM_ATTR (attrs.at_upper_bound);
9357   CHECKSUM_ATTR (attrs.at_use_location);
9358   CHECKSUM_ATTR (attrs.at_use_UTF8);
9359   CHECKSUM_ATTR (attrs.at_variable_parameter);
9360   CHECKSUM_ATTR (attrs.at_virtuality);
9361   CHECKSUM_ATTR (attrs.at_visibility);
9362   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9363   CHECKSUM_ATTR (attrs.at_type);
9364   CHECKSUM_ATTR (attrs.at_friend);
9365
9366   /* Checksum the child DIEs, except for nested types and member functions.  */
9367   c = die->die_child;
9368   if (c) do {
9369     dw_attr_ref name_attr;
9370
9371     c = c->die_sib;
9372     name_attr = get_AT (c, DW_AT_name);
9373     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9374         && name_attr != NULL)
9375       {
9376         CHECKSUM_ULEB128 ('S');
9377         CHECKSUM_ULEB128 (c->die_tag);
9378         CHECKSUM_STRING (AT_string (name_attr));
9379       }
9380     else
9381       {
9382         /* Mark this DIE so it gets processed when unmarking.  */
9383         if (c->die_mark == 0)
9384           c->die_mark = -1;
9385         die_checksum_ordered (c, ctx, mark);
9386       }
9387   } while (c != die->die_child);
9388
9389   CHECKSUM_ULEB128 (0);
9390 }
9391
9392 #undef CHECKSUM
9393 #undef CHECKSUM_STRING
9394 #undef CHECKSUM_ATTR
9395 #undef CHECKSUM_LEB128
9396 #undef CHECKSUM_ULEB128
9397
9398 /* Generate the type signature for DIE.  This is computed by generating an
9399    MD5 checksum over the DIE's tag, its relevant attributes, and its
9400    children.  Attributes that are references to other DIEs are processed
9401    by recursion, using the MARK field to prevent infinite recursion.
9402    If the DIE is nested inside a namespace or another type, we also
9403    need to include that context in the signature.  The lower 64 bits
9404    of the resulting MD5 checksum comprise the signature.  */
9405
9406 static void
9407 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9408 {
9409   int mark;
9410   const char *name;
9411   unsigned char checksum[16];
9412   struct md5_ctx ctx;
9413   dw_die_ref decl;
9414
9415   name = get_AT_string (die, DW_AT_name);
9416   decl = get_AT_ref (die, DW_AT_specification);
9417
9418   /* First, compute a signature for just the type name (and its surrounding
9419      context, if any.  This is stored in the type unit DIE for link-time
9420      ODR (one-definition rule) checking.  */
9421
9422   if (is_cxx() && name != NULL)
9423     {
9424       md5_init_ctx (&ctx);
9425
9426       /* Checksum the names of surrounding namespaces and structures.  */
9427       if (decl != NULL && decl->die_parent != NULL)
9428         checksum_die_context (decl->die_parent, &ctx);
9429
9430       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9431       md5_process_bytes (name, strlen (name) + 1, &ctx);
9432       md5_finish_ctx (&ctx, checksum);
9433
9434       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9435     }
9436
9437   /* Next, compute the complete type signature.  */
9438
9439   md5_init_ctx (&ctx);
9440   mark = 1;
9441   die->die_mark = mark;
9442
9443   /* Checksum the names of surrounding namespaces and structures.  */
9444   if (decl != NULL && decl->die_parent != NULL)
9445     checksum_die_context (decl->die_parent, &ctx);
9446
9447   /* Checksum the DIE and its children.  */
9448   die_checksum_ordered (die, &ctx, &mark);
9449   unmark_all_dies (die);
9450   md5_finish_ctx (&ctx, checksum);
9451
9452   /* Store the signature in the type node and link the type DIE and the
9453      type node together.  */
9454   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9455           DWARF_TYPE_SIGNATURE_SIZE);
9456   die->die_id.die_type_node = type_node;
9457   type_node->type_die = die;
9458
9459   /* If the DIE is a specification, link its declaration to the type node
9460      as well.  */
9461   if (decl != NULL)
9462     decl->die_id.die_type_node = type_node;
9463 }
9464
9465 /* Do the location expressions look same?  */
9466 static inline int
9467 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9468 {
9469   return loc1->dw_loc_opc == loc2->dw_loc_opc
9470          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9471          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9472 }
9473
9474 /* Do the values look the same?  */
9475 static int
9476 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9477 {
9478   dw_loc_descr_ref loc1, loc2;
9479   rtx r1, r2;
9480
9481   if (v1->val_class != v2->val_class)
9482     return 0;
9483
9484   switch (v1->val_class)
9485     {
9486     case dw_val_class_const:
9487       return v1->v.val_int == v2->v.val_int;
9488     case dw_val_class_unsigned_const:
9489       return v1->v.val_unsigned == v2->v.val_unsigned;
9490     case dw_val_class_const_double:
9491       return v1->v.val_double.high == v2->v.val_double.high
9492              && v1->v.val_double.low == v2->v.val_double.low;
9493     case dw_val_class_vec:
9494       if (v1->v.val_vec.length != v2->v.val_vec.length
9495           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9496         return 0;
9497       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9498                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9499         return 0;
9500       return 1;
9501     case dw_val_class_flag:
9502       return v1->v.val_flag == v2->v.val_flag;
9503     case dw_val_class_str:
9504       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9505
9506     case dw_val_class_addr:
9507       r1 = v1->v.val_addr;
9508       r2 = v2->v.val_addr;
9509       if (GET_CODE (r1) != GET_CODE (r2))
9510         return 0;
9511       return !rtx_equal_p (r1, r2);
9512
9513     case dw_val_class_offset:
9514       return v1->v.val_offset == v2->v.val_offset;
9515
9516     case dw_val_class_loc:
9517       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9518            loc1 && loc2;
9519            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9520         if (!same_loc_p (loc1, loc2, mark))
9521           return 0;
9522       return !loc1 && !loc2;
9523
9524     case dw_val_class_die_ref:
9525       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9526
9527     case dw_val_class_fde_ref:
9528     case dw_val_class_vms_delta:
9529     case dw_val_class_lbl_id:
9530     case dw_val_class_lineptr:
9531     case dw_val_class_macptr:
9532       return 1;
9533
9534     case dw_val_class_file:
9535       return v1->v.val_file == v2->v.val_file;
9536
9537     case dw_val_class_data8:
9538       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9539
9540     default:
9541       return 1;
9542     }
9543 }
9544
9545 /* Do the attributes look the same?  */
9546
9547 static int
9548 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9549 {
9550   if (at1->dw_attr != at2->dw_attr)
9551     return 0;
9552
9553   /* We don't care that this was compiled with a different compiler
9554      snapshot; if the output is the same, that's what matters. */
9555   if (at1->dw_attr == DW_AT_producer)
9556     return 1;
9557
9558   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9559 }
9560
9561 /* Do the dies look the same?  */
9562
9563 static int
9564 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9565 {
9566   dw_die_ref c1, c2;
9567   dw_attr_ref a1;
9568   unsigned ix;
9569
9570   /* To avoid infinite recursion.  */
9571   if (die1->die_mark)
9572     return die1->die_mark == die2->die_mark;
9573   die1->die_mark = die2->die_mark = ++(*mark);
9574
9575   if (die1->die_tag != die2->die_tag)
9576     return 0;
9577
9578   if (VEC_length (dw_attr_node, die1->die_attr)
9579       != VEC_length (dw_attr_node, die2->die_attr))
9580     return 0;
9581
9582   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9583     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9584       return 0;
9585
9586   c1 = die1->die_child;
9587   c2 = die2->die_child;
9588   if (! c1)
9589     {
9590       if (c2)
9591         return 0;
9592     }
9593   else
9594     for (;;)
9595       {
9596         if (!same_die_p (c1, c2, mark))
9597           return 0;
9598         c1 = c1->die_sib;
9599         c2 = c2->die_sib;
9600         if (c1 == die1->die_child)
9601           {
9602             if (c2 == die2->die_child)
9603               break;
9604             else
9605               return 0;
9606           }
9607     }
9608
9609   return 1;
9610 }
9611
9612 /* Do the dies look the same?  Wrapper around same_die_p.  */
9613
9614 static int
9615 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9616 {
9617   int mark = 0;
9618   int ret = same_die_p (die1, die2, &mark);
9619
9620   unmark_all_dies (die1);
9621   unmark_all_dies (die2);
9622
9623   return ret;
9624 }
9625
9626 /* The prefix to attach to symbols on DIEs in the current comdat debug
9627    info section.  */
9628 static char *comdat_symbol_id;
9629
9630 /* The index of the current symbol within the current comdat CU.  */
9631 static unsigned int comdat_symbol_number;
9632
9633 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9634    children, and set comdat_symbol_id accordingly.  */
9635
9636 static void
9637 compute_section_prefix (dw_die_ref unit_die)
9638 {
9639   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9640   const char *base = die_name ? lbasename (die_name) : "anonymous";
9641   char *name = XALLOCAVEC (char, strlen (base) + 64);
9642   char *p;
9643   int i, mark;
9644   unsigned char checksum[16];
9645   struct md5_ctx ctx;
9646
9647   /* Compute the checksum of the DIE, then append part of it as hex digits to
9648      the name filename of the unit.  */
9649
9650   md5_init_ctx (&ctx);
9651   mark = 0;
9652   die_checksum (unit_die, &ctx, &mark);
9653   unmark_all_dies (unit_die);
9654   md5_finish_ctx (&ctx, checksum);
9655
9656   sprintf (name, "%s.", base);
9657   clean_symbol_name (name);
9658
9659   p = name + strlen (name);
9660   for (i = 0; i < 4; i++)
9661     {
9662       sprintf (p, "%.2x", checksum[i]);
9663       p += 2;
9664     }
9665
9666   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9667   comdat_symbol_number = 0;
9668 }
9669
9670 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9671
9672 static int
9673 is_type_die (dw_die_ref die)
9674 {
9675   switch (die->die_tag)
9676     {
9677     case DW_TAG_array_type:
9678     case DW_TAG_class_type:
9679     case DW_TAG_interface_type:
9680     case DW_TAG_enumeration_type:
9681     case DW_TAG_pointer_type:
9682     case DW_TAG_reference_type:
9683     case DW_TAG_rvalue_reference_type:
9684     case DW_TAG_string_type:
9685     case DW_TAG_structure_type:
9686     case DW_TAG_subroutine_type:
9687     case DW_TAG_union_type:
9688     case DW_TAG_ptr_to_member_type:
9689     case DW_TAG_set_type:
9690     case DW_TAG_subrange_type:
9691     case DW_TAG_base_type:
9692     case DW_TAG_const_type:
9693     case DW_TAG_file_type:
9694     case DW_TAG_packed_type:
9695     case DW_TAG_volatile_type:
9696     case DW_TAG_typedef:
9697       return 1;
9698     default:
9699       return 0;
9700     }
9701 }
9702
9703 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9704    Basically, we want to choose the bits that are likely to be shared between
9705    compilations (types) and leave out the bits that are specific to individual
9706    compilations (functions).  */
9707
9708 static int
9709 is_comdat_die (dw_die_ref c)
9710 {
9711   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9712      we do for stabs.  The advantage is a greater likelihood of sharing between
9713      objects that don't include headers in the same order (and therefore would
9714      put the base types in a different comdat).  jason 8/28/00 */
9715
9716   if (c->die_tag == DW_TAG_base_type)
9717     return 0;
9718
9719   if (c->die_tag == DW_TAG_pointer_type
9720       || c->die_tag == DW_TAG_reference_type
9721       || c->die_tag == DW_TAG_rvalue_reference_type
9722       || c->die_tag == DW_TAG_const_type
9723       || c->die_tag == DW_TAG_volatile_type)
9724     {
9725       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9726
9727       return t ? is_comdat_die (t) : 0;
9728     }
9729
9730   return is_type_die (c);
9731 }
9732
9733 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9734    compilation unit.  */
9735
9736 static int
9737 is_symbol_die (dw_die_ref c)
9738 {
9739   return (is_type_die (c)
9740           || is_declaration_die (c)
9741           || c->die_tag == DW_TAG_namespace
9742           || c->die_tag == DW_TAG_module);
9743 }
9744
9745 /* Returns true iff C is a compile-unit DIE.  */
9746
9747 static inline bool
9748 is_cu_die (dw_die_ref c)
9749 {
9750   return c && c->die_tag == DW_TAG_compile_unit;
9751 }
9752
9753 static char *
9754 gen_internal_sym (const char *prefix)
9755 {
9756   char buf[256];
9757
9758   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9759   return xstrdup (buf);
9760 }
9761
9762 /* Assign symbols to all worthy DIEs under DIE.  */
9763
9764 static void
9765 assign_symbol_names (dw_die_ref die)
9766 {
9767   dw_die_ref c;
9768
9769   if (is_symbol_die (die))
9770     {
9771       if (comdat_symbol_id)
9772         {
9773           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9774
9775           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9776                    comdat_symbol_id, comdat_symbol_number++);
9777           die->die_id.die_symbol = xstrdup (p);
9778         }
9779       else
9780         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9781     }
9782
9783   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9784 }
9785
9786 struct cu_hash_table_entry
9787 {
9788   dw_die_ref cu;
9789   unsigned min_comdat_num, max_comdat_num;
9790   struct cu_hash_table_entry *next;
9791 };
9792
9793 /* Routines to manipulate hash table of CUs.  */
9794 static hashval_t
9795 htab_cu_hash (const void *of)
9796 {
9797   const struct cu_hash_table_entry *const entry =
9798     (const struct cu_hash_table_entry *) of;
9799
9800   return htab_hash_string (entry->cu->die_id.die_symbol);
9801 }
9802
9803 static int
9804 htab_cu_eq (const void *of1, const void *of2)
9805 {
9806   const struct cu_hash_table_entry *const entry1 =
9807     (const struct cu_hash_table_entry *) of1;
9808   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9809
9810   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9811 }
9812
9813 static void
9814 htab_cu_del (void *what)
9815 {
9816   struct cu_hash_table_entry *next,
9817     *entry = (struct cu_hash_table_entry *) what;
9818
9819   while (entry)
9820     {
9821       next = entry->next;
9822       free (entry);
9823       entry = next;
9824     }
9825 }
9826
9827 /* Check whether we have already seen this CU and set up SYM_NUM
9828    accordingly.  */
9829 static int
9830 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9831 {
9832   struct cu_hash_table_entry dummy;
9833   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9834
9835   dummy.max_comdat_num = 0;
9836
9837   slot = (struct cu_hash_table_entry **)
9838     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9839         INSERT);
9840   entry = *slot;
9841
9842   for (; entry; last = entry, entry = entry->next)
9843     {
9844       if (same_die_p_wrap (cu, entry->cu))
9845         break;
9846     }
9847
9848   if (entry)
9849     {
9850       *sym_num = entry->min_comdat_num;
9851       return 1;
9852     }
9853
9854   entry = XCNEW (struct cu_hash_table_entry);
9855   entry->cu = cu;
9856   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9857   entry->next = *slot;
9858   *slot = entry;
9859
9860   return 0;
9861 }
9862
9863 /* Record SYM_NUM to record of CU in HTABLE.  */
9864 static void
9865 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9866 {
9867   struct cu_hash_table_entry **slot, *entry;
9868
9869   slot = (struct cu_hash_table_entry **)
9870     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9871         NO_INSERT);
9872   entry = *slot;
9873
9874   entry->max_comdat_num = sym_num;
9875 }
9876
9877 /* Traverse the DIE (which is always comp_unit_die), and set up
9878    additional compilation units for each of the include files we see
9879    bracketed by BINCL/EINCL.  */
9880
9881 static void
9882 break_out_includes (dw_die_ref die)
9883 {
9884   dw_die_ref c;
9885   dw_die_ref unit = NULL;
9886   limbo_die_node *node, **pnode;
9887   htab_t cu_hash_table;
9888
9889   c = die->die_child;
9890   if (c) do {
9891     dw_die_ref prev = c;
9892     c = c->die_sib;
9893     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9894            || (unit && is_comdat_die (c)))
9895       {
9896         dw_die_ref next = c->die_sib;
9897
9898         /* This DIE is for a secondary CU; remove it from the main one.  */
9899         remove_child_with_prev (c, prev);
9900
9901         if (c->die_tag == DW_TAG_GNU_BINCL)
9902           unit = push_new_compile_unit (unit, c);
9903         else if (c->die_tag == DW_TAG_GNU_EINCL)
9904           unit = pop_compile_unit (unit);
9905         else
9906           add_child_die (unit, c);
9907         c = next;
9908         if (c == die->die_child)
9909           break;
9910       }
9911   } while (c != die->die_child);
9912
9913 #if 0
9914   /* We can only use this in debugging, since the frontend doesn't check
9915      to make sure that we leave every include file we enter.  */
9916   gcc_assert (!unit);
9917 #endif
9918
9919   assign_symbol_names (die);
9920   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9921   for (node = limbo_die_list, pnode = &limbo_die_list;
9922        node;
9923        node = node->next)
9924     {
9925       int is_dupl;
9926
9927       compute_section_prefix (node->die);
9928       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9929                         &comdat_symbol_number);
9930       assign_symbol_names (node->die);
9931       if (is_dupl)
9932         *pnode = node->next;
9933       else
9934         {
9935           pnode = &node->next;
9936           record_comdat_symbol_number (node->die, cu_hash_table,
9937                 comdat_symbol_number);
9938         }
9939     }
9940   htab_delete (cu_hash_table);
9941 }
9942
9943 /* Return non-zero if this DIE is a declaration.  */
9944
9945 static int
9946 is_declaration_die (dw_die_ref die)
9947 {
9948   dw_attr_ref a;
9949   unsigned ix;
9950
9951   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9952     if (a->dw_attr == DW_AT_declaration)
9953       return 1;
9954
9955   return 0;
9956 }
9957
9958 /* Return non-zero if this DIE is nested inside a subprogram.  */
9959
9960 static int
9961 is_nested_in_subprogram (dw_die_ref die)
9962 {
9963   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9964
9965   if (decl == NULL)
9966     decl = die;
9967   return local_scope_p (decl);
9968 }
9969
9970 /* Return non-zero if this is a type DIE that should be moved to a
9971    COMDAT .debug_types section.  */
9972
9973 static int
9974 should_move_die_to_comdat (dw_die_ref die)
9975 {
9976   switch (die->die_tag)
9977     {
9978     case DW_TAG_class_type:
9979     case DW_TAG_structure_type:
9980     case DW_TAG_enumeration_type:
9981     case DW_TAG_union_type:
9982       /* Don't move declarations, inlined instances, or types nested in a
9983          subprogram.  */
9984       if (is_declaration_die (die)
9985           || get_AT (die, DW_AT_abstract_origin)
9986           || is_nested_in_subprogram (die))
9987         return 0;
9988       return 1;
9989     case DW_TAG_array_type:
9990     case DW_TAG_interface_type:
9991     case DW_TAG_pointer_type:
9992     case DW_TAG_reference_type:
9993     case DW_TAG_rvalue_reference_type:
9994     case DW_TAG_string_type:
9995     case DW_TAG_subroutine_type:
9996     case DW_TAG_ptr_to_member_type:
9997     case DW_TAG_set_type:
9998     case DW_TAG_subrange_type:
9999     case DW_TAG_base_type:
10000     case DW_TAG_const_type:
10001     case DW_TAG_file_type:
10002     case DW_TAG_packed_type:
10003     case DW_TAG_volatile_type:
10004     case DW_TAG_typedef:
10005     default:
10006       return 0;
10007     }
10008 }
10009
10010 /* Make a clone of DIE.  */
10011
10012 static dw_die_ref
10013 clone_die (dw_die_ref die)
10014 {
10015   dw_die_ref clone;
10016   dw_attr_ref a;
10017   unsigned ix;
10018
10019   clone = ggc_alloc_cleared_die_node ();
10020   clone->die_tag = die->die_tag;
10021
10022   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10023     add_dwarf_attr (clone, a);
10024
10025   return clone;
10026 }
10027
10028 /* Make a clone of the tree rooted at DIE.  */
10029
10030 static dw_die_ref
10031 clone_tree (dw_die_ref die)
10032 {
10033   dw_die_ref c;
10034   dw_die_ref clone = clone_die (die);
10035
10036   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10037
10038   return clone;
10039 }
10040
10041 /* Make a clone of DIE as a declaration.  */
10042
10043 static dw_die_ref
10044 clone_as_declaration (dw_die_ref die)
10045 {
10046   dw_die_ref clone;
10047   dw_die_ref decl;
10048   dw_attr_ref a;
10049   unsigned ix;
10050
10051   /* If the DIE is already a declaration, just clone it.  */
10052   if (is_declaration_die (die))
10053     return clone_die (die);
10054
10055   /* If the DIE is a specification, just clone its declaration DIE.  */
10056   decl = get_AT_ref (die, DW_AT_specification);
10057   if (decl != NULL)
10058     return clone_die (decl);
10059
10060   clone = ggc_alloc_cleared_die_node ();
10061   clone->die_tag = die->die_tag;
10062
10063   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10064     {
10065       /* We don't want to copy over all attributes.
10066          For example we don't want DW_AT_byte_size because otherwise we will no
10067          longer have a declaration and GDB will treat it as a definition.  */
10068
10069       switch (a->dw_attr)
10070         {
10071         case DW_AT_artificial:
10072         case DW_AT_containing_type:
10073         case DW_AT_external:
10074         case DW_AT_name:
10075         case DW_AT_type:
10076         case DW_AT_virtuality:
10077         case DW_AT_linkage_name:
10078         case DW_AT_MIPS_linkage_name:
10079           add_dwarf_attr (clone, a);
10080           break;
10081         case DW_AT_byte_size:
10082         default:
10083           break;
10084         }
10085     }
10086
10087   if (die->die_id.die_type_node)
10088     add_AT_die_ref (clone, DW_AT_signature, die);
10089
10090   add_AT_flag (clone, DW_AT_declaration, 1);
10091   return clone;
10092 }
10093
10094 /* Copy the declaration context to the new compile unit DIE.  This includes
10095    any surrounding namespace or type declarations.  If the DIE has an
10096    AT_specification attribute, it also includes attributes and children
10097    attached to the specification.  */
10098
10099 static void
10100 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10101 {
10102   dw_die_ref decl;
10103   dw_die_ref new_decl;
10104
10105   decl = get_AT_ref (die, DW_AT_specification);
10106   if (decl == NULL)
10107     decl = die;
10108   else
10109     {
10110       unsigned ix;
10111       dw_die_ref c;
10112       dw_attr_ref a;
10113
10114       /* Copy the type node pointer from the new DIE to the original
10115          declaration DIE so we can forward references later.  */
10116       decl->die_id.die_type_node = die->die_id.die_type_node;
10117
10118       remove_AT (die, DW_AT_specification);
10119
10120       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10121         {
10122           if (a->dw_attr != DW_AT_name
10123               && a->dw_attr != DW_AT_declaration
10124               && a->dw_attr != DW_AT_external)
10125             add_dwarf_attr (die, a);
10126         }
10127
10128       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10129     }
10130
10131   if (decl->die_parent != NULL
10132       && decl->die_parent->die_tag != DW_TAG_compile_unit
10133       && decl->die_parent->die_tag != DW_TAG_type_unit)
10134     {
10135       new_decl = copy_ancestor_tree (unit, decl, NULL);
10136       if (new_decl != NULL)
10137         {
10138           remove_AT (new_decl, DW_AT_signature);
10139           add_AT_specification (die, new_decl);
10140         }
10141     }
10142 }
10143
10144 /* Generate the skeleton ancestor tree for the given NODE, then clone
10145    the DIE and add the clone into the tree.  */
10146
10147 static void
10148 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10149 {
10150   if (node->new_die != NULL)
10151     return;
10152
10153   node->new_die = clone_as_declaration (node->old_die);
10154
10155   if (node->parent != NULL)
10156     {
10157       generate_skeleton_ancestor_tree (node->parent);
10158       add_child_die (node->parent->new_die, node->new_die);
10159     }
10160 }
10161
10162 /* Generate a skeleton tree of DIEs containing any declarations that are
10163    found in the original tree.  We traverse the tree looking for declaration
10164    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10165
10166 static void
10167 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10168 {
10169   skeleton_chain_node node;
10170   dw_die_ref c;
10171   dw_die_ref first;
10172   dw_die_ref prev = NULL;
10173   dw_die_ref next = NULL;
10174
10175   node.parent = parent;
10176
10177   first = c = parent->old_die->die_child;
10178   if (c)
10179     next = c->die_sib;
10180   if (c) do {
10181     if (prev == NULL || prev->die_sib == c)
10182       prev = c;
10183     c = next;
10184     next = (c == first ? NULL : c->die_sib);
10185     node.old_die = c;
10186     node.new_die = NULL;
10187     if (is_declaration_die (c))
10188       {
10189         /* Clone the existing DIE, move the original to the skeleton
10190            tree (which is in the main CU), and put the clone, with
10191            all the original's children, where the original came from.  */
10192         dw_die_ref clone = clone_die (c);
10193         move_all_children (c, clone);
10194
10195         replace_child (c, clone, prev);
10196         generate_skeleton_ancestor_tree (parent);
10197         add_child_die (parent->new_die, c);
10198         node.new_die = c;
10199         c = clone;
10200       }
10201     generate_skeleton_bottom_up (&node);
10202   } while (next != NULL);
10203 }
10204
10205 /* Wrapper function for generate_skeleton_bottom_up.  */
10206
10207 static dw_die_ref
10208 generate_skeleton (dw_die_ref die)
10209 {
10210   skeleton_chain_node node;
10211
10212   node.old_die = die;
10213   node.new_die = NULL;
10214   node.parent = NULL;
10215
10216   /* If this type definition is nested inside another type,
10217      always leave at least a declaration in its place.  */
10218   if (die->die_parent != NULL && is_type_die (die->die_parent))
10219     node.new_die = clone_as_declaration (die);
10220
10221   generate_skeleton_bottom_up (&node);
10222   return node.new_die;
10223 }
10224
10225 /* Remove the DIE from its parent, possibly replacing it with a cloned
10226    declaration.  The original DIE will be moved to a new compile unit
10227    so that existing references to it follow it to the new location.  If
10228    any of the original DIE's descendants is a declaration, we need to
10229    replace the original DIE with a skeleton tree and move the
10230    declarations back into the skeleton tree.  */
10231
10232 static dw_die_ref
10233 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10234 {
10235   dw_die_ref skeleton;
10236
10237   skeleton = generate_skeleton (child);
10238   if (skeleton == NULL)
10239     remove_child_with_prev (child, prev);
10240   else
10241     {
10242       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10243       replace_child (child, skeleton, prev);
10244     }
10245
10246   return skeleton;
10247 }
10248
10249 /* Traverse the DIE and set up additional .debug_types sections for each
10250    type worthy of being placed in a COMDAT section.  */
10251
10252 static void
10253 break_out_comdat_types (dw_die_ref die)
10254 {
10255   dw_die_ref c;
10256   dw_die_ref first;
10257   dw_die_ref prev = NULL;
10258   dw_die_ref next = NULL;
10259   dw_die_ref unit = NULL;
10260
10261   first = c = die->die_child;
10262   if (c)
10263     next = c->die_sib;
10264   if (c) do {
10265     if (prev == NULL || prev->die_sib == c)
10266       prev = c;
10267     c = next;
10268     next = (c == first ? NULL : c->die_sib);
10269     if (should_move_die_to_comdat (c))
10270       {
10271         dw_die_ref replacement;
10272         comdat_type_node_ref type_node;
10273
10274         /* Create a new type unit DIE as the root for the new tree, and
10275            add it to the list of comdat types.  */
10276         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10277         add_AT_unsigned (unit, DW_AT_language,
10278                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10279         type_node = ggc_alloc_cleared_comdat_type_node ();
10280         type_node->root_die = unit;
10281         type_node->next = comdat_type_list;
10282         comdat_type_list = type_node;
10283
10284         /* Generate the type signature.  */
10285         generate_type_signature (c, type_node);
10286
10287         /* Copy the declaration context, attributes, and children of the
10288            declaration into the new compile unit DIE.  */
10289         copy_declaration_context (unit, c);
10290
10291         /* Remove this DIE from the main CU.  */
10292         replacement = remove_child_or_replace_with_skeleton (c, prev);
10293
10294         /* Break out nested types into their own type units.  */
10295         break_out_comdat_types (c);
10296
10297         /* Add the DIE to the new compunit.  */
10298         add_child_die (unit, c);
10299
10300         if (replacement != NULL)
10301           c = replacement;
10302       }
10303     else if (c->die_tag == DW_TAG_namespace
10304              || c->die_tag == DW_TAG_class_type
10305              || c->die_tag == DW_TAG_structure_type
10306              || c->die_tag == DW_TAG_union_type)
10307       {
10308         /* Look for nested types that can be broken out.  */
10309         break_out_comdat_types (c);
10310       }
10311   } while (next != NULL);
10312 }
10313
10314 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10315
10316 struct decl_table_entry
10317 {
10318   dw_die_ref orig;
10319   dw_die_ref copy;
10320 };
10321
10322 /* Routines to manipulate hash table of copied declarations.  */
10323
10324 static hashval_t
10325 htab_decl_hash (const void *of)
10326 {
10327   const struct decl_table_entry *const entry =
10328     (const struct decl_table_entry *) of;
10329
10330   return htab_hash_pointer (entry->orig);
10331 }
10332
10333 static int
10334 htab_decl_eq (const void *of1, const void *of2)
10335 {
10336   const struct decl_table_entry *const entry1 =
10337     (const struct decl_table_entry *) of1;
10338   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10339
10340   return entry1->orig == entry2;
10341 }
10342
10343 static void
10344 htab_decl_del (void *what)
10345 {
10346   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10347
10348   free (entry);
10349 }
10350
10351 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10352    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10353    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10354    to check if the ancestor has already been copied into UNIT.  */
10355
10356 static dw_die_ref
10357 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10358 {
10359   dw_die_ref parent = die->die_parent;
10360   dw_die_ref new_parent = unit;
10361   dw_die_ref copy;
10362   void **slot = NULL;
10363   struct decl_table_entry *entry = NULL;
10364
10365   if (decl_table)
10366     {
10367       /* Check if the entry has already been copied to UNIT.  */
10368       slot = htab_find_slot_with_hash (decl_table, die,
10369                                        htab_hash_pointer (die), INSERT);
10370       if (*slot != HTAB_EMPTY_ENTRY)
10371         {
10372           entry = (struct decl_table_entry *) *slot;
10373           return entry->copy;
10374         }
10375
10376       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10377       entry = XCNEW (struct decl_table_entry);
10378       entry->orig = die;
10379       entry->copy = NULL;
10380       *slot = entry;
10381     }
10382
10383   if (parent != NULL)
10384     {
10385       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10386       if (spec != NULL)
10387         parent = spec;
10388       if (parent->die_tag != DW_TAG_compile_unit
10389           && parent->die_tag != DW_TAG_type_unit)
10390         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10391     }
10392
10393   copy = clone_as_declaration (die);
10394   add_child_die (new_parent, copy);
10395
10396   if (decl_table != NULL)
10397     {
10398       /* Record the pointer to the copy.  */
10399       entry->copy = copy;
10400     }
10401
10402   return copy;
10403 }
10404
10405 /* Walk the DIE and its children, looking for references to incomplete
10406    or trivial types that are unmarked (i.e., that are not in the current
10407    type_unit).  */
10408
10409 static void
10410 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10411 {
10412   dw_die_ref c;
10413   dw_attr_ref a;
10414   unsigned ix;
10415
10416   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10417     {
10418       if (AT_class (a) == dw_val_class_die_ref)
10419         {
10420           dw_die_ref targ = AT_ref (a);
10421           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10422           void **slot;
10423           struct decl_table_entry *entry;
10424
10425           if (targ->die_mark != 0 || type_node != NULL)
10426             continue;
10427
10428           slot = htab_find_slot_with_hash (decl_table, targ,
10429                                            htab_hash_pointer (targ), INSERT);
10430
10431           if (*slot != HTAB_EMPTY_ENTRY)
10432             {
10433               /* TARG has already been copied, so we just need to
10434                  modify the reference to point to the copy.  */
10435               entry = (struct decl_table_entry *) *slot;
10436               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10437             }
10438           else
10439             {
10440               dw_die_ref parent = unit;
10441               dw_die_ref copy = clone_tree (targ);
10442
10443               /* Make sure the cloned tree is marked as part of the
10444                  type unit.  */
10445               mark_dies (copy);
10446
10447               /* Record in DECL_TABLE that TARG has been copied.
10448                  Need to do this now, before the recursive call,
10449                  because DECL_TABLE may be expanded and SLOT
10450                  would no longer be a valid pointer.  */
10451               entry = XCNEW (struct decl_table_entry);
10452               entry->orig = targ;
10453               entry->copy = copy;
10454               *slot = entry;
10455
10456               /* If TARG has surrounding context, copy its ancestor tree
10457                  into the new type unit.  */
10458               if (targ->die_parent != NULL
10459                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10460                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10461                 parent = copy_ancestor_tree (unit, targ->die_parent,
10462                                              decl_table);
10463
10464               add_child_die (parent, copy);
10465               a->dw_attr_val.v.val_die_ref.die = copy;
10466
10467               /* Make sure the newly-copied DIE is walked.  If it was
10468                  installed in a previously-added context, it won't
10469                  get visited otherwise.  */
10470               if (parent != unit)
10471                 {
10472                   /* Find the highest point of the newly-added tree,
10473                      mark each node along the way, and walk from there.  */
10474                   parent->die_mark = 1;
10475                   while (parent->die_parent
10476                          && parent->die_parent->die_mark == 0)
10477                     {
10478                       parent = parent->die_parent;
10479                       parent->die_mark = 1;
10480                     }
10481                   copy_decls_walk (unit, parent, decl_table);
10482                 }
10483             }
10484         }
10485     }
10486
10487   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10488 }
10489
10490 /* Copy declarations for "unworthy" types into the new comdat section.
10491    Incomplete types, modified types, and certain other types aren't broken
10492    out into comdat sections of their own, so they don't have a signature,
10493    and we need to copy the declaration into the same section so that we
10494    don't have an external reference.  */
10495
10496 static void
10497 copy_decls_for_unworthy_types (dw_die_ref unit)
10498 {
10499   htab_t decl_table;
10500
10501   mark_dies (unit);
10502   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10503   copy_decls_walk (unit, unit, decl_table);
10504   htab_delete (decl_table);
10505   unmark_dies (unit);
10506 }
10507
10508 /* Traverse the DIE and add a sibling attribute if it may have the
10509    effect of speeding up access to siblings.  To save some space,
10510    avoid generating sibling attributes for DIE's without children.  */
10511
10512 static void
10513 add_sibling_attributes (dw_die_ref die)
10514 {
10515   dw_die_ref c;
10516
10517   if (! die->die_child)
10518     return;
10519
10520   if (die->die_parent && die != die->die_parent->die_child)
10521     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10522
10523   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10524 }
10525
10526 /* Output all location lists for the DIE and its children.  */
10527
10528 static void
10529 output_location_lists (dw_die_ref die)
10530 {
10531   dw_die_ref c;
10532   dw_attr_ref a;
10533   unsigned ix;
10534
10535   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10536     if (AT_class (a) == dw_val_class_loc_list)
10537       output_loc_list (AT_loc_list (a));
10538
10539   FOR_EACH_CHILD (die, c, output_location_lists (c));
10540 }
10541
10542 /* The format of each DIE (and its attribute value pairs) is encoded in an
10543    abbreviation table.  This routine builds the abbreviation table and assigns
10544    a unique abbreviation id for each abbreviation entry.  The children of each
10545    die are visited recursively.  */
10546
10547 static void
10548 build_abbrev_table (dw_die_ref die)
10549 {
10550   unsigned long abbrev_id;
10551   unsigned int n_alloc;
10552   dw_die_ref c;
10553   dw_attr_ref a;
10554   unsigned ix;
10555
10556   /* Scan the DIE references, and mark as external any that refer to
10557      DIEs from other CUs (i.e. those which are not marked).  */
10558   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10559     if (AT_class (a) == dw_val_class_die_ref
10560         && AT_ref (a)->die_mark == 0)
10561       {
10562         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10563         set_AT_ref_external (a, 1);
10564       }
10565
10566   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10567     {
10568       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10569       dw_attr_ref die_a, abbrev_a;
10570       unsigned ix;
10571       bool ok = true;
10572
10573       if (abbrev->die_tag != die->die_tag)
10574         continue;
10575       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10576         continue;
10577
10578       if (VEC_length (dw_attr_node, abbrev->die_attr)
10579           != VEC_length (dw_attr_node, die->die_attr))
10580         continue;
10581
10582       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10583         {
10584           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10585           if ((abbrev_a->dw_attr != die_a->dw_attr)
10586               || (value_format (abbrev_a) != value_format (die_a)))
10587             {
10588               ok = false;
10589               break;
10590             }
10591         }
10592       if (ok)
10593         break;
10594     }
10595
10596   if (abbrev_id >= abbrev_die_table_in_use)
10597     {
10598       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10599         {
10600           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10601           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10602                                             n_alloc);
10603
10604           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10605                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10606           abbrev_die_table_allocated = n_alloc;
10607         }
10608
10609       ++abbrev_die_table_in_use;
10610       abbrev_die_table[abbrev_id] = die;
10611     }
10612
10613   die->die_abbrev = abbrev_id;
10614   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10615 }
10616 \f
10617 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10618
10619 static int
10620 constant_size (unsigned HOST_WIDE_INT value)
10621 {
10622   int log;
10623
10624   if (value == 0)
10625     log = 0;
10626   else
10627     log = floor_log2 (value);
10628
10629   log = log / 8;
10630   log = 1 << (floor_log2 (log) + 1);
10631
10632   return log;
10633 }
10634
10635 /* Return the size of a DIE as it is represented in the
10636    .debug_info section.  */
10637
10638 static unsigned long
10639 size_of_die (dw_die_ref die)
10640 {
10641   unsigned long size = 0;
10642   dw_attr_ref a;
10643   unsigned ix;
10644
10645   size += size_of_uleb128 (die->die_abbrev);
10646   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10647     {
10648       switch (AT_class (a))
10649         {
10650         case dw_val_class_addr:
10651           size += DWARF2_ADDR_SIZE;
10652           break;
10653         case dw_val_class_offset:
10654           size += DWARF_OFFSET_SIZE;
10655           break;
10656         case dw_val_class_loc:
10657           {
10658             unsigned long lsize = size_of_locs (AT_loc (a));
10659
10660             /* Block length.  */
10661             if (dwarf_version >= 4)
10662               size += size_of_uleb128 (lsize);
10663             else
10664               size += constant_size (lsize);
10665             size += lsize;
10666           }
10667           break;
10668         case dw_val_class_loc_list:
10669           size += DWARF_OFFSET_SIZE;
10670           break;
10671         case dw_val_class_range_list:
10672           size += DWARF_OFFSET_SIZE;
10673           break;
10674         case dw_val_class_const:
10675           size += size_of_sleb128 (AT_int (a));
10676           break;
10677         case dw_val_class_unsigned_const:
10678           size += constant_size (AT_unsigned (a));
10679           break;
10680         case dw_val_class_const_double:
10681           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10682           if (HOST_BITS_PER_WIDE_INT >= 64)
10683             size++; /* block */
10684           break;
10685         case dw_val_class_vec:
10686           size += constant_size (a->dw_attr_val.v.val_vec.length
10687                                  * a->dw_attr_val.v.val_vec.elt_size)
10688                   + a->dw_attr_val.v.val_vec.length
10689                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10690           break;
10691         case dw_val_class_flag:
10692           if (dwarf_version >= 4)
10693             /* Currently all add_AT_flag calls pass in 1 as last argument,
10694                so DW_FORM_flag_present can be used.  If that ever changes,
10695                we'll need to use DW_FORM_flag and have some optimization
10696                in build_abbrev_table that will change those to
10697                DW_FORM_flag_present if it is set to 1 in all DIEs using
10698                the same abbrev entry.  */
10699             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10700           else
10701             size += 1;
10702           break;
10703         case dw_val_class_die_ref:
10704           if (AT_ref_external (a))
10705             {
10706               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10707                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10708                  is sized by target address length, whereas in DWARF3
10709                  it's always sized as an offset.  */
10710               if (dwarf_version >= 4)
10711                 size += DWARF_TYPE_SIGNATURE_SIZE;
10712               else if (dwarf_version == 2)
10713                 size += DWARF2_ADDR_SIZE;
10714               else
10715                 size += DWARF_OFFSET_SIZE;
10716             }
10717           else
10718             size += DWARF_OFFSET_SIZE;
10719           break;
10720         case dw_val_class_fde_ref:
10721           size += DWARF_OFFSET_SIZE;
10722           break;
10723         case dw_val_class_lbl_id:
10724           size += DWARF2_ADDR_SIZE;
10725           break;
10726         case dw_val_class_lineptr:
10727         case dw_val_class_macptr:
10728           size += DWARF_OFFSET_SIZE;
10729           break;
10730         case dw_val_class_str:
10731           if (AT_string_form (a) == DW_FORM_strp)
10732             size += DWARF_OFFSET_SIZE;
10733           else
10734             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10735           break;
10736         case dw_val_class_file:
10737           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10738           break;
10739         case dw_val_class_data8:
10740           size += 8;
10741           break;
10742         case dw_val_class_vms_delta:
10743           size += DWARF_OFFSET_SIZE;
10744           break;
10745         default:
10746           gcc_unreachable ();
10747         }
10748     }
10749
10750   return size;
10751 }
10752
10753 /* Size the debugging information associated with a given DIE.  Visits the
10754    DIE's children recursively.  Updates the global variable next_die_offset, on
10755    each time through.  Uses the current value of next_die_offset to update the
10756    die_offset field in each DIE.  */
10757
10758 static void
10759 calc_die_sizes (dw_die_ref die)
10760 {
10761   dw_die_ref c;
10762
10763   die->die_offset = next_die_offset;
10764   next_die_offset += size_of_die (die);
10765
10766   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10767
10768   if (die->die_child != NULL)
10769     /* Count the null byte used to terminate sibling lists.  */
10770     next_die_offset += 1;
10771 }
10772
10773 /* Set the marks for a die and its children.  We do this so
10774    that we know whether or not a reference needs to use FORM_ref_addr; only
10775    DIEs in the same CU will be marked.  We used to clear out the offset
10776    and use that as the flag, but ran into ordering problems.  */
10777
10778 static void
10779 mark_dies (dw_die_ref die)
10780 {
10781   dw_die_ref c;
10782
10783   gcc_assert (!die->die_mark);
10784
10785   die->die_mark = 1;
10786   FOR_EACH_CHILD (die, c, mark_dies (c));
10787 }
10788
10789 /* Clear the marks for a die and its children.  */
10790
10791 static void
10792 unmark_dies (dw_die_ref die)
10793 {
10794   dw_die_ref c;
10795
10796   if (dwarf_version < 4)
10797     gcc_assert (die->die_mark);
10798
10799   die->die_mark = 0;
10800   FOR_EACH_CHILD (die, c, unmark_dies (c));
10801 }
10802
10803 /* Clear the marks for a die, its children and referred dies.  */
10804
10805 static void
10806 unmark_all_dies (dw_die_ref die)
10807 {
10808   dw_die_ref c;
10809   dw_attr_ref a;
10810   unsigned ix;
10811
10812   if (!die->die_mark)
10813     return;
10814   die->die_mark = 0;
10815
10816   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10817
10818   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10819     if (AT_class (a) == dw_val_class_die_ref)
10820       unmark_all_dies (AT_ref (a));
10821 }
10822
10823 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10824    generated for the compilation unit.  */
10825
10826 static unsigned long
10827 size_of_pubnames (VEC (pubname_entry, gc) * names)
10828 {
10829   unsigned long size;
10830   unsigned i;
10831   pubname_ref p;
10832
10833   size = DWARF_PUBNAMES_HEADER_SIZE;
10834   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10835     if (names != pubtype_table
10836         || p->die->die_offset != 0
10837         || !flag_eliminate_unused_debug_types)
10838       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10839
10840   size += DWARF_OFFSET_SIZE;
10841   return size;
10842 }
10843
10844 /* Return the size of the information in the .debug_aranges section.  */
10845
10846 static unsigned long
10847 size_of_aranges (void)
10848 {
10849   unsigned long size;
10850
10851   size = DWARF_ARANGES_HEADER_SIZE;
10852
10853   /* Count the address/length pair for this compilation unit.  */
10854   if (text_section_used)
10855     size += 2 * DWARF2_ADDR_SIZE;
10856   if (cold_text_section_used)
10857     size += 2 * DWARF2_ADDR_SIZE;
10858   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10859
10860   /* Count the two zero words used to terminated the address range table.  */
10861   size += 2 * DWARF2_ADDR_SIZE;
10862   return size;
10863 }
10864 \f
10865 /* Select the encoding of an attribute value.  */
10866
10867 static enum dwarf_form
10868 value_format (dw_attr_ref a)
10869 {
10870   switch (a->dw_attr_val.val_class)
10871     {
10872     case dw_val_class_addr:
10873       /* Only very few attributes allow DW_FORM_addr.  */
10874       switch (a->dw_attr)
10875         {
10876         case DW_AT_low_pc:
10877         case DW_AT_high_pc:
10878         case DW_AT_entry_pc:
10879         case DW_AT_trampoline:
10880           return DW_FORM_addr;
10881         default:
10882           break;
10883         }
10884       switch (DWARF2_ADDR_SIZE)
10885         {
10886         case 1:
10887           return DW_FORM_data1;
10888         case 2:
10889           return DW_FORM_data2;
10890         case 4:
10891           return DW_FORM_data4;
10892         case 8:
10893           return DW_FORM_data8;
10894         default:
10895           gcc_unreachable ();
10896         }
10897     case dw_val_class_range_list:
10898     case dw_val_class_loc_list:
10899       if (dwarf_version >= 4)
10900         return DW_FORM_sec_offset;
10901       /* FALLTHRU */
10902     case dw_val_class_vms_delta:
10903     case dw_val_class_offset:
10904       switch (DWARF_OFFSET_SIZE)
10905         {
10906         case 4:
10907           return DW_FORM_data4;
10908         case 8:
10909           return DW_FORM_data8;
10910         default:
10911           gcc_unreachable ();
10912         }
10913     case dw_val_class_loc:
10914       if (dwarf_version >= 4)
10915         return DW_FORM_exprloc;
10916       switch (constant_size (size_of_locs (AT_loc (a))))
10917         {
10918         case 1:
10919           return DW_FORM_block1;
10920         case 2:
10921           return DW_FORM_block2;
10922         default:
10923           gcc_unreachable ();
10924         }
10925     case dw_val_class_const:
10926       return DW_FORM_sdata;
10927     case dw_val_class_unsigned_const:
10928       switch (constant_size (AT_unsigned (a)))
10929         {
10930         case 1:
10931           return DW_FORM_data1;
10932         case 2:
10933           return DW_FORM_data2;
10934         case 4:
10935           return DW_FORM_data4;
10936         case 8:
10937           return DW_FORM_data8;
10938         default:
10939           gcc_unreachable ();
10940         }
10941     case dw_val_class_const_double:
10942       switch (HOST_BITS_PER_WIDE_INT)
10943         {
10944         case 8:
10945           return DW_FORM_data2;
10946         case 16:
10947           return DW_FORM_data4;
10948         case 32:
10949           return DW_FORM_data8;
10950         case 64:
10951         default:
10952           return DW_FORM_block1;
10953         }
10954     case dw_val_class_vec:
10955       switch (constant_size (a->dw_attr_val.v.val_vec.length
10956                              * a->dw_attr_val.v.val_vec.elt_size))
10957         {
10958         case 1:
10959           return DW_FORM_block1;
10960         case 2:
10961           return DW_FORM_block2;
10962         case 4:
10963           return DW_FORM_block4;
10964         default:
10965           gcc_unreachable ();
10966         }
10967     case dw_val_class_flag:
10968       if (dwarf_version >= 4)
10969         {
10970           /* Currently all add_AT_flag calls pass in 1 as last argument,
10971              so DW_FORM_flag_present can be used.  If that ever changes,
10972              we'll need to use DW_FORM_flag and have some optimization
10973              in build_abbrev_table that will change those to
10974              DW_FORM_flag_present if it is set to 1 in all DIEs using
10975              the same abbrev entry.  */
10976           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10977           return DW_FORM_flag_present;
10978         }
10979       return DW_FORM_flag;
10980     case dw_val_class_die_ref:
10981       if (AT_ref_external (a))
10982         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10983       else
10984         return DW_FORM_ref;
10985     case dw_val_class_fde_ref:
10986       return DW_FORM_data;
10987     case dw_val_class_lbl_id:
10988       return DW_FORM_addr;
10989     case dw_val_class_lineptr:
10990     case dw_val_class_macptr:
10991       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10992     case dw_val_class_str:
10993       return AT_string_form (a);
10994     case dw_val_class_file:
10995       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10996         {
10997         case 1:
10998           return DW_FORM_data1;
10999         case 2:
11000           return DW_FORM_data2;
11001         case 4:
11002           return DW_FORM_data4;
11003         default:
11004           gcc_unreachable ();
11005         }
11006
11007     case dw_val_class_data8:
11008       return DW_FORM_data8;
11009
11010     default:
11011       gcc_unreachable ();
11012     }
11013 }
11014
11015 /* Output the encoding of an attribute value.  */
11016
11017 static void
11018 output_value_format (dw_attr_ref a)
11019 {
11020   enum dwarf_form form = value_format (a);
11021
11022   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11023 }
11024
11025 /* Output the .debug_abbrev section which defines the DIE abbreviation
11026    table.  */
11027
11028 static void
11029 output_abbrev_section (void)
11030 {
11031   unsigned long abbrev_id;
11032
11033   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11034     {
11035       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11036       unsigned ix;
11037       dw_attr_ref a_attr;
11038
11039       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11040       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11041                                    dwarf_tag_name (abbrev->die_tag));
11042
11043       if (abbrev->die_child != NULL)
11044         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11045       else
11046         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11047
11048       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11049            ix++)
11050         {
11051           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11052                                        dwarf_attr_name (a_attr->dw_attr));
11053           output_value_format (a_attr);
11054         }
11055
11056       dw2_asm_output_data (1, 0, NULL);
11057       dw2_asm_output_data (1, 0, NULL);
11058     }
11059
11060   /* Terminate the table.  */
11061   dw2_asm_output_data (1, 0, NULL);
11062 }
11063
11064 /* Output a symbol we can use to refer to this DIE from another CU.  */
11065
11066 static inline void
11067 output_die_symbol (dw_die_ref die)
11068 {
11069   char *sym = die->die_id.die_symbol;
11070
11071   if (sym == 0)
11072     return;
11073
11074   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11075     /* We make these global, not weak; if the target doesn't support
11076        .linkonce, it doesn't support combining the sections, so debugging
11077        will break.  */
11078     targetm.asm_out.globalize_label (asm_out_file, sym);
11079
11080   ASM_OUTPUT_LABEL (asm_out_file, sym);
11081 }
11082
11083 /* Return a new location list, given the begin and end range, and the
11084    expression.  */
11085
11086 static inline dw_loc_list_ref
11087 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11088               const char *section)
11089 {
11090   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11091
11092   retlist->begin = begin;
11093   retlist->end = end;
11094   retlist->expr = expr;
11095   retlist->section = section;
11096
11097   return retlist;
11098 }
11099
11100 /* Generate a new internal symbol for this location list node, if it
11101    hasn't got one yet.  */
11102
11103 static inline void
11104 gen_llsym (dw_loc_list_ref list)
11105 {
11106   gcc_assert (!list->ll_symbol);
11107   list->ll_symbol = gen_internal_sym ("LLST");
11108 }
11109
11110 /* Output the location list given to us.  */
11111
11112 static void
11113 output_loc_list (dw_loc_list_ref list_head)
11114 {
11115   dw_loc_list_ref curr = list_head;
11116
11117   if (list_head->emitted)
11118     return;
11119   list_head->emitted = true;
11120
11121   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11122
11123   /* Walk the location list, and output each range + expression.  */
11124   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11125     {
11126       unsigned long size;
11127       /* Don't output an entry that starts and ends at the same address.  */
11128       if (strcmp (curr->begin, curr->end) == 0)
11129         continue;
11130       if (!have_multiple_function_sections)
11131         {
11132           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11133                                 "Location list begin address (%s)",
11134                                 list_head->ll_symbol);
11135           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11136                                 "Location list end address (%s)",
11137                                 list_head->ll_symbol);
11138         }
11139       else
11140         {
11141           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11142                                "Location list begin address (%s)",
11143                                list_head->ll_symbol);
11144           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11145                                "Location list end address (%s)",
11146                                list_head->ll_symbol);
11147         }
11148       size = size_of_locs (curr->expr);
11149
11150       /* Output the block length for this list of location operations.  */
11151       gcc_assert (size <= 0xffff);
11152       dw2_asm_output_data (2, size, "%s", "Location expression size");
11153
11154       output_loc_sequence (curr->expr, -1);
11155     }
11156
11157   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11158                        "Location list terminator begin (%s)",
11159                        list_head->ll_symbol);
11160   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11161                        "Location list terminator end (%s)",
11162                        list_head->ll_symbol);
11163 }
11164
11165 /* Output a type signature.  */
11166
11167 static inline void
11168 output_signature (const char *sig, const char *name)
11169 {
11170   int i;
11171
11172   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11173     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11174 }
11175
11176 /* Output the DIE and its attributes.  Called recursively to generate
11177    the definitions of each child DIE.  */
11178
11179 static void
11180 output_die (dw_die_ref die)
11181 {
11182   dw_attr_ref a;
11183   dw_die_ref c;
11184   unsigned long size;
11185   unsigned ix;
11186
11187   /* If someone in another CU might refer to us, set up a symbol for
11188      them to point to.  */
11189   if (dwarf_version < 4 && die->die_id.die_symbol)
11190     output_die_symbol (die);
11191
11192   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11193                                (unsigned long)die->die_offset,
11194                                dwarf_tag_name (die->die_tag));
11195
11196   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11197     {
11198       const char *name = dwarf_attr_name (a->dw_attr);
11199
11200       switch (AT_class (a))
11201         {
11202         case dw_val_class_addr:
11203           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11204           break;
11205
11206         case dw_val_class_offset:
11207           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11208                                "%s", name);
11209           break;
11210
11211         case dw_val_class_range_list:
11212           {
11213             char *p = strchr (ranges_section_label, '\0');
11214
11215             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11216                      a->dw_attr_val.v.val_offset);
11217             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11218                                    debug_ranges_section, "%s", name);
11219             *p = '\0';
11220           }
11221           break;
11222
11223         case dw_val_class_loc:
11224           size = size_of_locs (AT_loc (a));
11225
11226           /* Output the block length for this list of location operations.  */
11227           if (dwarf_version >= 4)
11228             dw2_asm_output_data_uleb128 (size, "%s", name);
11229           else
11230             dw2_asm_output_data (constant_size (size), size, "%s", name);
11231
11232           output_loc_sequence (AT_loc (a), -1);
11233           break;
11234
11235         case dw_val_class_const:
11236           /* ??? It would be slightly more efficient to use a scheme like is
11237              used for unsigned constants below, but gdb 4.x does not sign
11238              extend.  Gdb 5.x does sign extend.  */
11239           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11240           break;
11241
11242         case dw_val_class_unsigned_const:
11243           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11244                                AT_unsigned (a), "%s", name);
11245           break;
11246
11247         case dw_val_class_const_double:
11248           {
11249             unsigned HOST_WIDE_INT first, second;
11250
11251             if (HOST_BITS_PER_WIDE_INT >= 64)
11252               dw2_asm_output_data (1,
11253                                    2 * HOST_BITS_PER_WIDE_INT
11254                                    / HOST_BITS_PER_CHAR,
11255                                    NULL);
11256
11257             if (WORDS_BIG_ENDIAN)
11258               {
11259                 first = a->dw_attr_val.v.val_double.high;
11260                 second = a->dw_attr_val.v.val_double.low;
11261               }
11262             else
11263               {
11264                 first = a->dw_attr_val.v.val_double.low;
11265                 second = a->dw_attr_val.v.val_double.high;
11266               }
11267
11268             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11269                                  first, name);
11270             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11271                                  second, NULL);
11272           }
11273           break;
11274
11275         case dw_val_class_vec:
11276           {
11277             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11278             unsigned int len = a->dw_attr_val.v.val_vec.length;
11279             unsigned int i;
11280             unsigned char *p;
11281
11282             dw2_asm_output_data (constant_size (len * elt_size),
11283                                  len * elt_size, "%s", name);
11284             if (elt_size > sizeof (HOST_WIDE_INT))
11285               {
11286                 elt_size /= 2;
11287                 len *= 2;
11288               }
11289             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11290                  i < len;
11291                  i++, p += elt_size)
11292               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11293                                    "fp or vector constant word %u", i);
11294             break;
11295           }
11296
11297         case dw_val_class_flag:
11298           if (dwarf_version >= 4)
11299             {
11300               /* Currently all add_AT_flag calls pass in 1 as last argument,
11301                  so DW_FORM_flag_present can be used.  If that ever changes,
11302                  we'll need to use DW_FORM_flag and have some optimization
11303                  in build_abbrev_table that will change those to
11304                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11305                  the same abbrev entry.  */
11306               gcc_assert (AT_flag (a) == 1);
11307               if (flag_debug_asm)
11308                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11309                          ASM_COMMENT_START, name);
11310               break;
11311             }
11312           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11313           break;
11314
11315         case dw_val_class_loc_list:
11316           {
11317             char *sym = AT_loc_list (a)->ll_symbol;
11318
11319             gcc_assert (sym);
11320             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11321                                    "%s", name);
11322           }
11323           break;
11324
11325         case dw_val_class_die_ref:
11326           if (AT_ref_external (a))
11327             {
11328               if (dwarf_version >= 4)
11329                 {
11330                   comdat_type_node_ref type_node =
11331                     AT_ref (a)->die_id.die_type_node;
11332
11333                   gcc_assert (type_node);
11334                   output_signature (type_node->signature, name);
11335                 }
11336               else
11337                 {
11338                   char *sym = AT_ref (a)->die_id.die_symbol;
11339                   int size;
11340
11341                   gcc_assert (sym);
11342                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11343                      length, whereas in DWARF3 it's always sized as an
11344                      offset.  */
11345                   if (dwarf_version == 2)
11346                     size = DWARF2_ADDR_SIZE;
11347                   else
11348                     size = DWARF_OFFSET_SIZE;
11349                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11350                                          name);
11351                 }
11352             }
11353           else
11354             {
11355               gcc_assert (AT_ref (a)->die_offset);
11356               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11357                                    "%s", name);
11358             }
11359           break;
11360
11361         case dw_val_class_fde_ref:
11362           {
11363             char l1[20];
11364
11365             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11366                                          a->dw_attr_val.v.val_fde_index * 2);
11367             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11368                                    "%s", name);
11369           }
11370           break;
11371
11372         case dw_val_class_vms_delta:
11373           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11374                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11375                                     "%s", name);
11376           break;
11377
11378         case dw_val_class_lbl_id:
11379           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11380           break;
11381
11382         case dw_val_class_lineptr:
11383           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11384                                  debug_line_section, "%s", name);
11385           break;
11386
11387         case dw_val_class_macptr:
11388           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11389                                  debug_macinfo_section, "%s", name);
11390           break;
11391
11392         case dw_val_class_str:
11393           if (AT_string_form (a) == DW_FORM_strp)
11394             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11395                                    a->dw_attr_val.v.val_str->label,
11396                                    debug_str_section,
11397                                    "%s: \"%s\"", name, AT_string (a));
11398           else
11399             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11400           break;
11401
11402         case dw_val_class_file:
11403           {
11404             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11405
11406             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11407                                  a->dw_attr_val.v.val_file->filename);
11408             break;
11409           }
11410
11411         case dw_val_class_data8:
11412           {
11413             int i;
11414
11415             for (i = 0; i < 8; i++)
11416               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11417                                    i == 0 ? "%s" : NULL, name);
11418             break;
11419           }
11420
11421         default:
11422           gcc_unreachable ();
11423         }
11424     }
11425
11426   FOR_EACH_CHILD (die, c, output_die (c));
11427
11428   /* Add null byte to terminate sibling list.  */
11429   if (die->die_child != NULL)
11430     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11431                          (unsigned long) die->die_offset);
11432 }
11433
11434 /* Output the compilation unit that appears at the beginning of the
11435    .debug_info section, and precedes the DIE descriptions.  */
11436
11437 static void
11438 output_compilation_unit_header (void)
11439 {
11440   int ver = dwarf_version;
11441
11442   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11443     dw2_asm_output_data (4, 0xffffffff,
11444       "Initial length escape value indicating 64-bit DWARF extension");
11445   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11446                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11447                        "Length of Compilation Unit Info");
11448   dw2_asm_output_data (2, ver, "DWARF version number");
11449   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11450                          debug_abbrev_section,
11451                          "Offset Into Abbrev. Section");
11452   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11453 }
11454
11455 /* Output the compilation unit DIE and its children.  */
11456
11457 static void
11458 output_comp_unit (dw_die_ref die, int output_if_empty)
11459 {
11460   const char *secname;
11461   char *oldsym, *tmp;
11462
11463   /* Unless we are outputting main CU, we may throw away empty ones.  */
11464   if (!output_if_empty && die->die_child == NULL)
11465     return;
11466
11467   /* Even if there are no children of this DIE, we must output the information
11468      about the compilation unit.  Otherwise, on an empty translation unit, we
11469      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11470      will then complain when examining the file.  First mark all the DIEs in
11471      this CU so we know which get local refs.  */
11472   mark_dies (die);
11473
11474   build_abbrev_table (die);
11475
11476   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11477   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11478   calc_die_sizes (die);
11479
11480   oldsym = die->die_id.die_symbol;
11481   if (oldsym)
11482     {
11483       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11484
11485       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11486       secname = tmp;
11487       die->die_id.die_symbol = NULL;
11488       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11489     }
11490   else
11491     {
11492       switch_to_section (debug_info_section);
11493       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11494       info_section_emitted = true;
11495     }
11496
11497   /* Output debugging information.  */
11498   output_compilation_unit_header ();
11499   output_die (die);
11500
11501   /* Leave the marks on the main CU, so we can check them in
11502      output_pubnames.  */
11503   if (oldsym)
11504     {
11505       unmark_dies (die);
11506       die->die_id.die_symbol = oldsym;
11507     }
11508 }
11509
11510 /* Output a comdat type unit DIE and its children.  */
11511
11512 static void
11513 output_comdat_type_unit (comdat_type_node *node)
11514 {
11515   const char *secname;
11516   char *tmp;
11517   int i;
11518 #if defined (OBJECT_FORMAT_ELF)
11519   tree comdat_key;
11520 #endif
11521
11522   /* First mark all the DIEs in this CU so we know which get local refs.  */
11523   mark_dies (node->root_die);
11524
11525   build_abbrev_table (node->root_die);
11526
11527   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11528   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11529   calc_die_sizes (node->root_die);
11530
11531 #if defined (OBJECT_FORMAT_ELF)
11532   secname = ".debug_types";
11533   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11534   sprintf (tmp, "wt.");
11535   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11536     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11537   comdat_key = get_identifier (tmp);
11538   targetm.asm_out.named_section (secname,
11539                                  SECTION_DEBUG | SECTION_LINKONCE,
11540                                  comdat_key);
11541 #else
11542   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11543   sprintf (tmp, ".gnu.linkonce.wt.");
11544   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11545     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11546   secname = tmp;
11547   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11548 #endif
11549
11550   /* Output debugging information.  */
11551   output_compilation_unit_header ();
11552   output_signature (node->signature, "Type Signature");
11553   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11554                        "Offset to Type DIE");
11555   output_die (node->root_die);
11556
11557   unmark_dies (node->root_die);
11558 }
11559
11560 /* Return the DWARF2/3 pubname associated with a decl.  */
11561
11562 static const char *
11563 dwarf2_name (tree decl, int scope)
11564 {
11565   if (DECL_NAMELESS (decl))
11566     return NULL;
11567   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11568 }
11569
11570 /* Add a new entry to .debug_pubnames if appropriate.  */
11571
11572 static void
11573 add_pubname_string (const char *str, dw_die_ref die)
11574 {
11575   if (targetm.want_debug_pub_sections)
11576     {
11577       pubname_entry e;
11578
11579       e.die = die;
11580       e.name = xstrdup (str);
11581       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11582     }
11583 }
11584
11585 static void
11586 add_pubname (tree decl, dw_die_ref die)
11587 {
11588   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11589     {
11590       const char *name = dwarf2_name (decl, 1);
11591       if (name)
11592         add_pubname_string (name, die);
11593     }
11594 }
11595
11596 /* Add a new entry to .debug_pubtypes if appropriate.  */
11597
11598 static void
11599 add_pubtype (tree decl, dw_die_ref die)
11600 {
11601   pubname_entry e;
11602
11603   if (!targetm.want_debug_pub_sections)
11604     return;
11605
11606   e.name = NULL;
11607   if ((TREE_PUBLIC (decl)
11608        || is_cu_die (die->die_parent))
11609       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11610     {
11611       e.die = die;
11612       if (TYPE_P (decl))
11613         {
11614           if (TYPE_NAME (decl))
11615             {
11616               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11617                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11618               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11619                        && DECL_NAME (TYPE_NAME (decl)))
11620                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11621               else
11622                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11623             }
11624         }
11625       else
11626         {
11627           e.name = dwarf2_name (decl, 1);
11628           if (e.name)
11629             e.name = xstrdup (e.name);
11630         }
11631
11632       /* If we don't have a name for the type, there's no point in adding
11633          it to the table.  */
11634       if (e.name && e.name[0] != '\0')
11635         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11636     }
11637 }
11638
11639 /* Output the public names table used to speed up access to externally
11640    visible names; or the public types table used to find type definitions.  */
11641
11642 static void
11643 output_pubnames (VEC (pubname_entry, gc) * names)
11644 {
11645   unsigned i;
11646   unsigned long pubnames_length = size_of_pubnames (names);
11647   pubname_ref pub;
11648
11649   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11650     dw2_asm_output_data (4, 0xffffffff,
11651       "Initial length escape value indicating 64-bit DWARF extension");
11652   if (names == pubname_table)
11653     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11654                          "Length of Public Names Info");
11655   else
11656     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11657                          "Length of Public Type Names Info");
11658   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11659   dw2_asm_output_data (2, 2, "DWARF Version");
11660   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11661                          debug_info_section,
11662                          "Offset of Compilation Unit Info");
11663   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11664                        "Compilation Unit Length");
11665
11666   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11667     {
11668       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11669       if (names == pubname_table)
11670         gcc_assert (pub->die->die_mark);
11671
11672       if (names != pubtype_table
11673           || pub->die->die_offset != 0
11674           || !flag_eliminate_unused_debug_types)
11675         {
11676           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11677                                "DIE offset");
11678
11679           dw2_asm_output_nstring (pub->name, -1, "external name");
11680         }
11681     }
11682
11683   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11684 }
11685
11686 /* Add a new entry to .debug_aranges if appropriate.  */
11687
11688 static void
11689 add_arange (tree decl, dw_die_ref die)
11690 {
11691   if (! DECL_SECTION_NAME (decl))
11692     return;
11693
11694   if (arange_table_in_use == arange_table_allocated)
11695     {
11696       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11697       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11698                                     arange_table_allocated);
11699       memset (arange_table + arange_table_in_use, 0,
11700               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11701     }
11702
11703   arange_table[arange_table_in_use++] = die;
11704 }
11705
11706 /* Output the information that goes into the .debug_aranges table.
11707    Namely, define the beginning and ending address range of the
11708    text section generated for this compilation unit.  */
11709
11710 static void
11711 output_aranges (void)
11712 {
11713   unsigned i;
11714   unsigned long aranges_length = size_of_aranges ();
11715
11716   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11717     dw2_asm_output_data (4, 0xffffffff,
11718       "Initial length escape value indicating 64-bit DWARF extension");
11719   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11720                        "Length of Address Ranges Info");
11721   /* Version number for aranges is still 2, even in DWARF3.  */
11722   dw2_asm_output_data (2, 2, "DWARF Version");
11723   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11724                          debug_info_section,
11725                          "Offset of Compilation Unit Info");
11726   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11727   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11728
11729   /* We need to align to twice the pointer size here.  */
11730   if (DWARF_ARANGES_PAD_SIZE)
11731     {
11732       /* Pad using a 2 byte words so that padding is correct for any
11733          pointer size.  */
11734       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11735                            2 * DWARF2_ADDR_SIZE);
11736       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11737         dw2_asm_output_data (2, 0, NULL);
11738     }
11739
11740   /* It is necessary not to output these entries if the sections were
11741      not used; if the sections were not used, the length will be 0 and
11742      the address may end up as 0 if the section is discarded by ld
11743      --gc-sections, leaving an invalid (0, 0) entry that can be
11744      confused with the terminator.  */
11745   if (text_section_used)
11746     {
11747       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11748       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11749                             text_section_label, "Length");
11750     }
11751   if (cold_text_section_used)
11752     {
11753       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11754                            "Address");
11755       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11756                             cold_text_section_label, "Length");
11757     }
11758
11759   for (i = 0; i < arange_table_in_use; i++)
11760     {
11761       dw_die_ref die = arange_table[i];
11762
11763       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11764       gcc_assert (die->die_mark);
11765
11766       if (die->die_tag == DW_TAG_subprogram)
11767         {
11768           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11769                                "Address");
11770           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11771                                 get_AT_low_pc (die), "Length");
11772         }
11773       else
11774         {
11775           /* A static variable; extract the symbol from DW_AT_location.
11776              Note that this code isn't currently hit, as we only emit
11777              aranges for functions (jason 9/23/99).  */
11778           dw_attr_ref a = get_AT (die, DW_AT_location);
11779           dw_loc_descr_ref loc;
11780
11781           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11782
11783           loc = AT_loc (a);
11784           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11785
11786           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11787                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11788           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11789                                get_AT_unsigned (die, DW_AT_byte_size),
11790                                "Length");
11791         }
11792     }
11793
11794   /* Output the terminator words.  */
11795   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11796   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11797 }
11798
11799 /* Add a new entry to .debug_ranges.  Return the offset at which it
11800    was placed.  */
11801
11802 static unsigned int
11803 add_ranges_num (int num)
11804 {
11805   unsigned int in_use = ranges_table_in_use;
11806
11807   if (in_use == ranges_table_allocated)
11808     {
11809       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11810       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11811                                     ranges_table_allocated);
11812       memset (ranges_table + ranges_table_in_use, 0,
11813               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11814     }
11815
11816   ranges_table[in_use].num = num;
11817   ranges_table_in_use = in_use + 1;
11818
11819   return in_use * 2 * DWARF2_ADDR_SIZE;
11820 }
11821
11822 /* Add a new entry to .debug_ranges corresponding to a block, or a
11823    range terminator if BLOCK is NULL.  */
11824
11825 static unsigned int
11826 add_ranges (const_tree block)
11827 {
11828   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11829 }
11830
11831 /* Add a new entry to .debug_ranges corresponding to a pair of
11832    labels.  */
11833
11834 static void
11835 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11836                       bool *added)
11837 {
11838   unsigned int in_use = ranges_by_label_in_use;
11839   unsigned int offset;
11840
11841   if (in_use == ranges_by_label_allocated)
11842     {
11843       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11844       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11845                                        ranges_by_label,
11846                                        ranges_by_label_allocated);
11847       memset (ranges_by_label + ranges_by_label_in_use, 0,
11848               RANGES_TABLE_INCREMENT
11849               * sizeof (struct dw_ranges_by_label_struct));
11850     }
11851
11852   ranges_by_label[in_use].begin = begin;
11853   ranges_by_label[in_use].end = end;
11854   ranges_by_label_in_use = in_use + 1;
11855
11856   offset = add_ranges_num (-(int)in_use - 1);
11857   if (!*added)
11858     {
11859       add_AT_range_list (die, DW_AT_ranges, offset);
11860       *added = true;
11861     }
11862 }
11863
11864 static void
11865 output_ranges (void)
11866 {
11867   unsigned i;
11868   static const char *const start_fmt = "Offset %#x";
11869   const char *fmt = start_fmt;
11870
11871   for (i = 0; i < ranges_table_in_use; i++)
11872     {
11873       int block_num = ranges_table[i].num;
11874
11875       if (block_num > 0)
11876         {
11877           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11878           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11879
11880           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11881           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11882
11883           /* If all code is in the text section, then the compilation
11884              unit base address defaults to DW_AT_low_pc, which is the
11885              base of the text section.  */
11886           if (!have_multiple_function_sections)
11887             {
11888               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11889                                     text_section_label,
11890                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11891               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11892                                     text_section_label, NULL);
11893             }
11894
11895           /* Otherwise, the compilation unit base address is zero,
11896              which allows us to use absolute addresses, and not worry
11897              about whether the target supports cross-section
11898              arithmetic.  */
11899           else
11900             {
11901               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11902                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11903               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11904             }
11905
11906           fmt = NULL;
11907         }
11908
11909       /* Negative block_num stands for an index into ranges_by_label.  */
11910       else if (block_num < 0)
11911         {
11912           int lab_idx = - block_num - 1;
11913
11914           if (!have_multiple_function_sections)
11915             {
11916               gcc_unreachable ();
11917 #if 0
11918               /* If we ever use add_ranges_by_labels () for a single
11919                  function section, all we have to do is to take out
11920                  the #if 0 above.  */
11921               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11922                                     ranges_by_label[lab_idx].begin,
11923                                     text_section_label,
11924                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11925               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11926                                     ranges_by_label[lab_idx].end,
11927                                     text_section_label, NULL);
11928 #endif
11929             }
11930           else
11931             {
11932               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11933                                    ranges_by_label[lab_idx].begin,
11934                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11935               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11936                                    ranges_by_label[lab_idx].end,
11937                                    NULL);
11938             }
11939         }
11940       else
11941         {
11942           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11943           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11944           fmt = start_fmt;
11945         }
11946     }
11947 }
11948
11949 /* Data structure containing information about input files.  */
11950 struct file_info
11951 {
11952   const char *path;     /* Complete file name.  */
11953   const char *fname;    /* File name part.  */
11954   int length;           /* Length of entire string.  */
11955   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11956   int dir_idx;          /* Index in directory table.  */
11957 };
11958
11959 /* Data structure containing information about directories with source
11960    files.  */
11961 struct dir_info
11962 {
11963   const char *path;     /* Path including directory name.  */
11964   int length;           /* Path length.  */
11965   int prefix;           /* Index of directory entry which is a prefix.  */
11966   int count;            /* Number of files in this directory.  */
11967   int dir_idx;          /* Index of directory used as base.  */
11968 };
11969
11970 /* Callback function for file_info comparison.  We sort by looking at
11971    the directories in the path.  */
11972
11973 static int
11974 file_info_cmp (const void *p1, const void *p2)
11975 {
11976   const struct file_info *const s1 = (const struct file_info *) p1;
11977   const struct file_info *const s2 = (const struct file_info *) p2;
11978   const unsigned char *cp1;
11979   const unsigned char *cp2;
11980
11981   /* Take care of file names without directories.  We need to make sure that
11982      we return consistent values to qsort since some will get confused if
11983      we return the same value when identical operands are passed in opposite
11984      orders.  So if neither has a directory, return 0 and otherwise return
11985      1 or -1 depending on which one has the directory.  */
11986   if ((s1->path == s1->fname || s2->path == s2->fname))
11987     return (s2->path == s2->fname) - (s1->path == s1->fname);
11988
11989   cp1 = (const unsigned char *) s1->path;
11990   cp2 = (const unsigned char *) s2->path;
11991
11992   while (1)
11993     {
11994       ++cp1;
11995       ++cp2;
11996       /* Reached the end of the first path?  If so, handle like above.  */
11997       if ((cp1 == (const unsigned char *) s1->fname)
11998           || (cp2 == (const unsigned char *) s2->fname))
11999         return ((cp2 == (const unsigned char *) s2->fname)
12000                 - (cp1 == (const unsigned char *) s1->fname));
12001
12002       /* Character of current path component the same?  */
12003       else if (*cp1 != *cp2)
12004         return *cp1 - *cp2;
12005     }
12006 }
12007
12008 struct file_name_acquire_data
12009 {
12010   struct file_info *files;
12011   int used_files;
12012   int max_files;
12013 };
12014
12015 /* Traversal function for the hash table.  */
12016
12017 static int
12018 file_name_acquire (void ** slot, void *data)
12019 {
12020   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12021   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12022   struct file_info *fi;
12023   const char *f;
12024
12025   gcc_assert (fnad->max_files >= d->emitted_number);
12026
12027   if (! d->emitted_number)
12028     return 1;
12029
12030   gcc_assert (fnad->max_files != fnad->used_files);
12031
12032   fi = fnad->files + fnad->used_files++;
12033
12034   /* Skip all leading "./".  */
12035   f = d->filename;
12036   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12037     f += 2;
12038
12039   /* Create a new array entry.  */
12040   fi->path = f;
12041   fi->length = strlen (f);
12042   fi->file_idx = d;
12043
12044   /* Search for the file name part.  */
12045   f = strrchr (f, DIR_SEPARATOR);
12046 #if defined (DIR_SEPARATOR_2)
12047   {
12048     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12049
12050     if (g != NULL)
12051       {
12052         if (f == NULL || f < g)
12053           f = g;
12054       }
12055   }
12056 #endif
12057
12058   fi->fname = f == NULL ? fi->path : f + 1;
12059   return 1;
12060 }
12061
12062 /* Output the directory table and the file name table.  We try to minimize
12063    the total amount of memory needed.  A heuristic is used to avoid large
12064    slowdowns with many input files.  */
12065
12066 static void
12067 output_file_names (void)
12068 {
12069   struct file_name_acquire_data fnad;
12070   int numfiles;
12071   struct file_info *files;
12072   struct dir_info *dirs;
12073   int *saved;
12074   int *savehere;
12075   int *backmap;
12076   int ndirs;
12077   int idx_offset;
12078   int i;
12079
12080   if (!last_emitted_file)
12081     {
12082       dw2_asm_output_data (1, 0, "End directory table");
12083       dw2_asm_output_data (1, 0, "End file name table");
12084       return;
12085     }
12086
12087   numfiles = last_emitted_file->emitted_number;
12088
12089   /* Allocate the various arrays we need.  */
12090   files = XALLOCAVEC (struct file_info, numfiles);
12091   dirs = XALLOCAVEC (struct dir_info, numfiles);
12092
12093   fnad.files = files;
12094   fnad.used_files = 0;
12095   fnad.max_files = numfiles;
12096   htab_traverse (file_table, file_name_acquire, &fnad);
12097   gcc_assert (fnad.used_files == fnad.max_files);
12098
12099   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12100
12101   /* Find all the different directories used.  */
12102   dirs[0].path = files[0].path;
12103   dirs[0].length = files[0].fname - files[0].path;
12104   dirs[0].prefix = -1;
12105   dirs[0].count = 1;
12106   dirs[0].dir_idx = 0;
12107   files[0].dir_idx = 0;
12108   ndirs = 1;
12109
12110   for (i = 1; i < numfiles; i++)
12111     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12112         && memcmp (dirs[ndirs - 1].path, files[i].path,
12113                    dirs[ndirs - 1].length) == 0)
12114       {
12115         /* Same directory as last entry.  */
12116         files[i].dir_idx = ndirs - 1;
12117         ++dirs[ndirs - 1].count;
12118       }
12119     else
12120       {
12121         int j;
12122
12123         /* This is a new directory.  */
12124         dirs[ndirs].path = files[i].path;
12125         dirs[ndirs].length = files[i].fname - files[i].path;
12126         dirs[ndirs].count = 1;
12127         dirs[ndirs].dir_idx = ndirs;
12128         files[i].dir_idx = ndirs;
12129
12130         /* Search for a prefix.  */
12131         dirs[ndirs].prefix = -1;
12132         for (j = 0; j < ndirs; j++)
12133           if (dirs[j].length < dirs[ndirs].length
12134               && dirs[j].length > 1
12135               && (dirs[ndirs].prefix == -1
12136                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12137               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12138             dirs[ndirs].prefix = j;
12139
12140         ++ndirs;
12141       }
12142
12143   /* Now to the actual work.  We have to find a subset of the directories which
12144      allow expressing the file name using references to the directory table
12145      with the least amount of characters.  We do not do an exhaustive search
12146      where we would have to check out every combination of every single
12147      possible prefix.  Instead we use a heuristic which provides nearly optimal
12148      results in most cases and never is much off.  */
12149   saved = XALLOCAVEC (int, ndirs);
12150   savehere = XALLOCAVEC (int, ndirs);
12151
12152   memset (saved, '\0', ndirs * sizeof (saved[0]));
12153   for (i = 0; i < ndirs; i++)
12154     {
12155       int j;
12156       int total;
12157
12158       /* We can always save some space for the current directory.  But this
12159          does not mean it will be enough to justify adding the directory.  */
12160       savehere[i] = dirs[i].length;
12161       total = (savehere[i] - saved[i]) * dirs[i].count;
12162
12163       for (j = i + 1; j < ndirs; j++)
12164         {
12165           savehere[j] = 0;
12166           if (saved[j] < dirs[i].length)
12167             {
12168               /* Determine whether the dirs[i] path is a prefix of the
12169                  dirs[j] path.  */
12170               int k;
12171
12172               k = dirs[j].prefix;
12173               while (k != -1 && k != (int) i)
12174                 k = dirs[k].prefix;
12175
12176               if (k == (int) i)
12177                 {
12178                   /* Yes it is.  We can possibly save some memory by
12179                      writing the filenames in dirs[j] relative to
12180                      dirs[i].  */
12181                   savehere[j] = dirs[i].length;
12182                   total += (savehere[j] - saved[j]) * dirs[j].count;
12183                 }
12184             }
12185         }
12186
12187       /* Check whether we can save enough to justify adding the dirs[i]
12188          directory.  */
12189       if (total > dirs[i].length + 1)
12190         {
12191           /* It's worthwhile adding.  */
12192           for (j = i; j < ndirs; j++)
12193             if (savehere[j] > 0)
12194               {
12195                 /* Remember how much we saved for this directory so far.  */
12196                 saved[j] = savehere[j];
12197
12198                 /* Remember the prefix directory.  */
12199                 dirs[j].dir_idx = i;
12200               }
12201         }
12202     }
12203
12204   /* Emit the directory name table.  */
12205   idx_offset = dirs[0].length > 0 ? 1 : 0;
12206   for (i = 1 - idx_offset; i < ndirs; i++)
12207     dw2_asm_output_nstring (dirs[i].path,
12208                             dirs[i].length
12209                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12210                             "Directory Entry: %#x", i + idx_offset);
12211
12212   dw2_asm_output_data (1, 0, "End directory table");
12213
12214   /* We have to emit them in the order of emitted_number since that's
12215      used in the debug info generation.  To do this efficiently we
12216      generate a back-mapping of the indices first.  */
12217   backmap = XALLOCAVEC (int, numfiles);
12218   for (i = 0; i < numfiles; i++)
12219     backmap[files[i].file_idx->emitted_number - 1] = i;
12220
12221   /* Now write all the file names.  */
12222   for (i = 0; i < numfiles; i++)
12223     {
12224       int file_idx = backmap[i];
12225       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12226
12227 #ifdef VMS_DEBUGGING_INFO
12228 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12229
12230       /* Setting these fields can lead to debugger miscomparisons,
12231          but VMS Debug requires them to be set correctly.  */
12232
12233       int ver;
12234       long long cdt;
12235       long siz;
12236       int maxfilelen = strlen (files[file_idx].path)
12237                                + dirs[dir_idx].length
12238                                + MAX_VMS_VERSION_LEN + 1;
12239       char *filebuf = XALLOCAVEC (char, maxfilelen);
12240
12241       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12242       snprintf (filebuf, maxfilelen, "%s;%d",
12243                 files[file_idx].path + dirs[dir_idx].length, ver);
12244
12245       dw2_asm_output_nstring
12246         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12247
12248       /* Include directory index.  */
12249       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12250
12251       /* Modification time.  */
12252       dw2_asm_output_data_uleb128
12253         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12254           ? cdt : 0,
12255          NULL);
12256
12257       /* File length in bytes.  */
12258       dw2_asm_output_data_uleb128
12259         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12260           ? siz : 0,
12261          NULL);
12262 #else
12263       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12264                               "File Entry: %#x", (unsigned) i + 1);
12265
12266       /* Include directory index.  */
12267       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12268
12269       /* Modification time.  */
12270       dw2_asm_output_data_uleb128 (0, NULL);
12271
12272       /* File length in bytes.  */
12273       dw2_asm_output_data_uleb128 (0, NULL);
12274 #endif /* VMS_DEBUGGING_INFO */
12275     }
12276
12277   dw2_asm_output_data (1, 0, "End file name table");
12278 }
12279
12280
12281 /* Output the source line number correspondence information.  This
12282    information goes into the .debug_line section.  */
12283
12284 static void
12285 output_line_info (void)
12286 {
12287   char l1[20], l2[20], p1[20], p2[20];
12288   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12289   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12290   unsigned opc;
12291   unsigned n_op_args;
12292   unsigned long lt_index;
12293   unsigned long current_line;
12294   long line_offset;
12295   long line_delta;
12296   unsigned long current_file;
12297   unsigned long function;
12298   int ver = dwarf_version;
12299
12300   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12301   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12302   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12303   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12304
12305   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12306     dw2_asm_output_data (4, 0xffffffff,
12307       "Initial length escape value indicating 64-bit DWARF extension");
12308   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12309                         "Length of Source Line Info");
12310   ASM_OUTPUT_LABEL (asm_out_file, l1);
12311
12312   dw2_asm_output_data (2, ver, "DWARF Version");
12313   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12314   ASM_OUTPUT_LABEL (asm_out_file, p1);
12315
12316   /* Define the architecture-dependent minimum instruction length (in
12317    bytes).  In this implementation of DWARF, this field is used for
12318    information purposes only.  Since GCC generates assembly language,
12319    we have no a priori knowledge of how many instruction bytes are
12320    generated for each source line, and therefore can use only the
12321    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12322    commands.  Accordingly, we fix this as `1', which is "correct
12323    enough" for all architectures, and don't let the target override.  */
12324   dw2_asm_output_data (1, 1,
12325                        "Minimum Instruction Length");
12326
12327   if (ver >= 4)
12328     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12329                          "Maximum Operations Per Instruction");
12330   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12331                        "Default is_stmt_start flag");
12332   dw2_asm_output_data (1, DWARF_LINE_BASE,
12333                        "Line Base Value (Special Opcodes)");
12334   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12335                        "Line Range Value (Special Opcodes)");
12336   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12337                        "Special Opcode Base");
12338
12339   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12340     {
12341       switch (opc)
12342         {
12343         case DW_LNS_advance_pc:
12344         case DW_LNS_advance_line:
12345         case DW_LNS_set_file:
12346         case DW_LNS_set_column:
12347         case DW_LNS_fixed_advance_pc:
12348           n_op_args = 1;
12349           break;
12350         default:
12351           n_op_args = 0;
12352           break;
12353         }
12354
12355       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12356                            opc, n_op_args);
12357     }
12358
12359   /* Write out the information about the files we use.  */
12360   output_file_names ();
12361   ASM_OUTPUT_LABEL (asm_out_file, p2);
12362
12363   /* We used to set the address register to the first location in the text
12364      section here, but that didn't accomplish anything since we already
12365      have a line note for the opening brace of the first function.  */
12366
12367   /* Generate the line number to PC correspondence table, encoded as
12368      a series of state machine operations.  */
12369   current_file = 1;
12370   current_line = 1;
12371
12372   if (cfun && in_cold_section_p)
12373     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12374   else
12375     strcpy (prev_line_label, text_section_label);
12376   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12377     {
12378       dw_line_info_ref line_info = &line_info_table[lt_index];
12379
12380 #if 0
12381       /* Disable this optimization for now; GDB wants to see two line notes
12382          at the beginning of a function so it can find the end of the
12383          prologue.  */
12384
12385       /* Don't emit anything for redundant notes.  Just updating the
12386          address doesn't accomplish anything, because we already assume
12387          that anything after the last address is this line.  */
12388       if (line_info->dw_line_num == current_line
12389           && line_info->dw_file_num == current_file)
12390         continue;
12391 #endif
12392
12393       /* Emit debug info for the address of the current line.
12394
12395          Unfortunately, we have little choice here currently, and must always
12396          use the most general form.  GCC does not know the address delta
12397          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12398          attributes which will give an upper bound on the address range.  We
12399          could perhaps use length attributes to determine when it is safe to
12400          use DW_LNS_fixed_advance_pc.  */
12401
12402       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12403       if (0)
12404         {
12405           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12406           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12407                                "DW_LNS_fixed_advance_pc");
12408           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12409         }
12410       else
12411         {
12412           /* This can handle any delta.  This takes
12413              4+DWARF2_ADDR_SIZE bytes.  */
12414           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12415           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12416           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12417           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12418         }
12419
12420       strcpy (prev_line_label, line_label);
12421
12422       /* Emit debug info for the source file of the current line, if
12423          different from the previous line.  */
12424       if (line_info->dw_file_num != current_file)
12425         {
12426           current_file = line_info->dw_file_num;
12427           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12428           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12429         }
12430
12431       /* Emit debug info for the current line number, choosing the encoding
12432          that uses the least amount of space.  */
12433       if (line_info->dw_line_num != current_line)
12434         {
12435           line_offset = line_info->dw_line_num - current_line;
12436           line_delta = line_offset - DWARF_LINE_BASE;
12437           current_line = line_info->dw_line_num;
12438           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12439             /* This can handle deltas from -10 to 234, using the current
12440                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12441                takes 1 byte.  */
12442             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12443                                  "line %lu", current_line);
12444           else
12445             {
12446               /* This can handle any delta.  This takes at least 4 bytes,
12447                  depending on the value being encoded.  */
12448               dw2_asm_output_data (1, DW_LNS_advance_line,
12449                                    "advance to line %lu", current_line);
12450               dw2_asm_output_data_sleb128 (line_offset, NULL);
12451               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12452             }
12453         }
12454       else
12455         /* We still need to start a new row, so output a copy insn.  */
12456         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12457     }
12458
12459   /* Emit debug info for the address of the end of the function.  */
12460   if (0)
12461     {
12462       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12463                            "DW_LNS_fixed_advance_pc");
12464       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12465     }
12466   else
12467     {
12468       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12469       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12470       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12471       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12472     }
12473
12474   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12475   dw2_asm_output_data_uleb128 (1, NULL);
12476   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12477
12478   function = 0;
12479   current_file = 1;
12480   current_line = 1;
12481   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12482     {
12483       dw_separate_line_info_ref line_info
12484         = &separate_line_info_table[lt_index];
12485
12486 #if 0
12487       /* Don't emit anything for redundant notes.  */
12488       if (line_info->dw_line_num == current_line
12489           && line_info->dw_file_num == current_file
12490           && line_info->function == function)
12491         goto cont;
12492 #endif
12493
12494       /* Emit debug info for the address of the current line.  If this is
12495          a new function, or the first line of a function, then we need
12496          to handle it differently.  */
12497       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12498                                    lt_index);
12499       if (function != line_info->function)
12500         {
12501           function = line_info->function;
12502
12503           /* Set the address register to the first line in the function.  */
12504           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12505           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12506           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12507           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12508         }
12509       else
12510         {
12511           /* ??? See the DW_LNS_advance_pc comment above.  */
12512           if (0)
12513             {
12514               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12515                                    "DW_LNS_fixed_advance_pc");
12516               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12517             }
12518           else
12519             {
12520               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12521               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12522               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12523               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12524             }
12525         }
12526
12527       strcpy (prev_line_label, line_label);
12528
12529       /* Emit debug info for the source file of the current line, if
12530          different from the previous line.  */
12531       if (line_info->dw_file_num != current_file)
12532         {
12533           current_file = line_info->dw_file_num;
12534           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12535           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12536         }
12537
12538       /* Emit debug info for the current line number, choosing the encoding
12539          that uses the least amount of space.  */
12540       if (line_info->dw_line_num != current_line)
12541         {
12542           line_offset = line_info->dw_line_num - current_line;
12543           line_delta = line_offset - DWARF_LINE_BASE;
12544           current_line = line_info->dw_line_num;
12545           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12546             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12547                                  "line %lu", current_line);
12548           else
12549             {
12550               dw2_asm_output_data (1, DW_LNS_advance_line,
12551                                    "advance to line %lu", current_line);
12552               dw2_asm_output_data_sleb128 (line_offset, NULL);
12553               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12554             }
12555         }
12556       else
12557         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12558
12559 #if 0
12560     cont:
12561 #endif
12562
12563       lt_index++;
12564
12565       /* If we're done with a function, end its sequence.  */
12566       if (lt_index == separate_line_info_table_in_use
12567           || separate_line_info_table[lt_index].function != function)
12568         {
12569           current_file = 1;
12570           current_line = 1;
12571
12572           /* Emit debug info for the address of the end of the function.  */
12573           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12574           if (0)
12575             {
12576               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12577                                    "DW_LNS_fixed_advance_pc");
12578               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12579             }
12580           else
12581             {
12582               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12583               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12584               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12585               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12586             }
12587
12588           /* Output the marker for the end of this sequence.  */
12589           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12590           dw2_asm_output_data_uleb128 (1, NULL);
12591           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12592         }
12593     }
12594
12595   /* Output the marker for the end of the line number info.  */
12596   ASM_OUTPUT_LABEL (asm_out_file, l2);
12597 }
12598
12599 /* Return the size of the .debug_dcall table for the compilation unit.  */
12600
12601 static unsigned long
12602 size_of_dcall_table (void)
12603 {
12604   unsigned long size;
12605   unsigned int i;
12606   dcall_entry *p;
12607   tree last_poc_decl = NULL;
12608
12609   /* Header:  version + debug info section pointer + pointer size.  */
12610   size = 2 + DWARF_OFFSET_SIZE + 1;
12611
12612   /* Each entry:  code label + DIE offset.  */
12613   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12614     {
12615       gcc_assert (p->targ_die != NULL);
12616       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12617       if (p->poc_decl != last_poc_decl)
12618         {
12619           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12620           gcc_assert (poc_die);
12621           last_poc_decl = p->poc_decl;
12622           if (poc_die)
12623             size += (DWARF_OFFSET_SIZE
12624                      + size_of_uleb128 (poc_die->die_offset));
12625         }
12626       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12627     }
12628
12629   return size;
12630 }
12631
12632 /* Output the direct call table used to disambiguate PC values when
12633    identical function have been merged.  */
12634
12635 static void
12636 output_dcall_table (void)
12637 {
12638   unsigned i;
12639   unsigned long dcall_length = size_of_dcall_table ();
12640   dcall_entry *p;
12641   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12642   tree last_poc_decl = NULL;
12643
12644   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12645     dw2_asm_output_data (4, 0xffffffff,
12646       "Initial length escape value indicating 64-bit DWARF extension");
12647   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12648                        "Length of Direct Call Table");
12649   dw2_asm_output_data (2, 4, "Version number");
12650   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12651                          debug_info_section,
12652                          "Offset of Compilation Unit Info");
12653   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12654
12655   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12656     {
12657       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12658       if (p->poc_decl != last_poc_decl)
12659         {
12660           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12661           last_poc_decl = p->poc_decl;
12662           if (poc_die)
12663             {
12664               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12665               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12666                                            "Caller DIE offset");
12667             }
12668         }
12669       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12670       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12671       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12672                                    "Callee DIE offset");
12673     }
12674 }
12675 \f
12676 /* Return the size of the .debug_vcall table for the compilation unit.  */
12677
12678 static unsigned long
12679 size_of_vcall_table (void)
12680 {
12681   unsigned long size;
12682   unsigned int i;
12683   vcall_entry *p;
12684
12685   /* Header:  version + pointer size.  */
12686   size = 2 + 1;
12687
12688   /* Each entry:  code label + vtable slot index.  */
12689   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12690     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12691
12692   return size;
12693 }
12694
12695 /* Output the virtual call table used to disambiguate PC values when
12696    identical function have been merged.  */
12697
12698 static void
12699 output_vcall_table (void)
12700 {
12701   unsigned i;
12702   unsigned long vcall_length = size_of_vcall_table ();
12703   vcall_entry *p;
12704   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12705
12706   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12707     dw2_asm_output_data (4, 0xffffffff,
12708       "Initial length escape value indicating 64-bit DWARF extension");
12709   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12710                        "Length of Virtual Call Table");
12711   dw2_asm_output_data (2, 4, "Version number");
12712   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12713
12714   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12715     {
12716       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12717       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12718       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12719     }
12720 }
12721 \f
12722 /* Given a pointer to a tree node for some base type, return a pointer to
12723    a DIE that describes the given type.
12724
12725    This routine must only be called for GCC type nodes that correspond to
12726    Dwarf base (fundamental) types.  */
12727
12728 static dw_die_ref
12729 base_type_die (tree type)
12730 {
12731   dw_die_ref base_type_result;
12732   enum dwarf_type encoding;
12733
12734   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12735     return 0;
12736
12737   /* If this is a subtype that should not be emitted as a subrange type,
12738      use the base type.  See subrange_type_for_debug_p.  */
12739   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12740     type = TREE_TYPE (type);
12741
12742   switch (TREE_CODE (type))
12743     {
12744     case INTEGER_TYPE:
12745       if ((dwarf_version >= 4 || !dwarf_strict)
12746           && TYPE_NAME (type)
12747           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12748           && DECL_IS_BUILTIN (TYPE_NAME (type))
12749           && DECL_NAME (TYPE_NAME (type)))
12750         {
12751           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12752           if (strcmp (name, "char16_t") == 0
12753               || strcmp (name, "char32_t") == 0)
12754             {
12755               encoding = DW_ATE_UTF;
12756               break;
12757             }
12758         }
12759       if (TYPE_STRING_FLAG (type))
12760         {
12761           if (TYPE_UNSIGNED (type))
12762             encoding = DW_ATE_unsigned_char;
12763           else
12764             encoding = DW_ATE_signed_char;
12765         }
12766       else if (TYPE_UNSIGNED (type))
12767         encoding = DW_ATE_unsigned;
12768       else
12769         encoding = DW_ATE_signed;
12770       break;
12771
12772     case REAL_TYPE:
12773       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12774         {
12775           if (dwarf_version >= 3 || !dwarf_strict)
12776             encoding = DW_ATE_decimal_float;
12777           else
12778             encoding = DW_ATE_lo_user;
12779         }
12780       else
12781         encoding = DW_ATE_float;
12782       break;
12783
12784     case FIXED_POINT_TYPE:
12785       if (!(dwarf_version >= 3 || !dwarf_strict))
12786         encoding = DW_ATE_lo_user;
12787       else if (TYPE_UNSIGNED (type))
12788         encoding = DW_ATE_unsigned_fixed;
12789       else
12790         encoding = DW_ATE_signed_fixed;
12791       break;
12792
12793       /* Dwarf2 doesn't know anything about complex ints, so use
12794          a user defined type for it.  */
12795     case COMPLEX_TYPE:
12796       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12797         encoding = DW_ATE_complex_float;
12798       else
12799         encoding = DW_ATE_lo_user;
12800       break;
12801
12802     case BOOLEAN_TYPE:
12803       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12804       encoding = DW_ATE_boolean;
12805       break;
12806
12807     default:
12808       /* No other TREE_CODEs are Dwarf fundamental types.  */
12809       gcc_unreachable ();
12810     }
12811
12812   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12813
12814   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12815                    int_size_in_bytes (type));
12816   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12817
12818   return base_type_result;
12819 }
12820
12821 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12822    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12823
12824 static inline int
12825 is_base_type (tree type)
12826 {
12827   switch (TREE_CODE (type))
12828     {
12829     case ERROR_MARK:
12830     case VOID_TYPE:
12831     case INTEGER_TYPE:
12832     case REAL_TYPE:
12833     case FIXED_POINT_TYPE:
12834     case COMPLEX_TYPE:
12835     case BOOLEAN_TYPE:
12836       return 1;
12837
12838     case ARRAY_TYPE:
12839     case RECORD_TYPE:
12840     case UNION_TYPE:
12841     case QUAL_UNION_TYPE:
12842     case ENUMERAL_TYPE:
12843     case FUNCTION_TYPE:
12844     case METHOD_TYPE:
12845     case POINTER_TYPE:
12846     case REFERENCE_TYPE:
12847     case NULLPTR_TYPE:
12848     case OFFSET_TYPE:
12849     case LANG_TYPE:
12850     case VECTOR_TYPE:
12851       return 0;
12852
12853     default:
12854       gcc_unreachable ();
12855     }
12856
12857   return 0;
12858 }
12859
12860 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12861    node, return the size in bits for the type if it is a constant, or else
12862    return the alignment for the type if the type's size is not constant, or
12863    else return BITS_PER_WORD if the type actually turns out to be an
12864    ERROR_MARK node.  */
12865
12866 static inline unsigned HOST_WIDE_INT
12867 simple_type_size_in_bits (const_tree type)
12868 {
12869   if (TREE_CODE (type) == ERROR_MARK)
12870     return BITS_PER_WORD;
12871   else if (TYPE_SIZE (type) == NULL_TREE)
12872     return 0;
12873   else if (host_integerp (TYPE_SIZE (type), 1))
12874     return tree_low_cst (TYPE_SIZE (type), 1);
12875   else
12876     return TYPE_ALIGN (type);
12877 }
12878
12879 /* Similarly, but return a double_int instead of UHWI.  */
12880
12881 static inline double_int
12882 double_int_type_size_in_bits (const_tree type)
12883 {
12884   if (TREE_CODE (type) == ERROR_MARK)
12885     return uhwi_to_double_int (BITS_PER_WORD);
12886   else if (TYPE_SIZE (type) == NULL_TREE)
12887     return double_int_zero;
12888   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12889     return tree_to_double_int (TYPE_SIZE (type));
12890   else
12891     return uhwi_to_double_int (TYPE_ALIGN (type));
12892 }
12893
12894 /*  Given a pointer to a tree node for a subrange type, return a pointer
12895     to a DIE that describes the given type.  */
12896
12897 static dw_die_ref
12898 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12899 {
12900   dw_die_ref subrange_die;
12901   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12902
12903   if (context_die == NULL)
12904     context_die = comp_unit_die ();
12905
12906   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12907
12908   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12909     {
12910       /* The size of the subrange type and its base type do not match,
12911          so we need to generate a size attribute for the subrange type.  */
12912       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12913     }
12914
12915   if (low)
12916     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12917   if (high)
12918     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12919
12920   return subrange_die;
12921 }
12922
12923 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12924    entry that chains various modifiers in front of the given type.  */
12925
12926 static dw_die_ref
12927 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12928                    dw_die_ref context_die)
12929 {
12930   enum tree_code code = TREE_CODE (type);
12931   dw_die_ref mod_type_die;
12932   dw_die_ref sub_die = NULL;
12933   tree item_type = NULL;
12934   tree qualified_type;
12935   tree name, low, high;
12936
12937   if (code == ERROR_MARK)
12938     return NULL;
12939
12940   /* See if we already have the appropriately qualified variant of
12941      this type.  */
12942   qualified_type
12943     = get_qualified_type (type,
12944                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12945                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12946
12947   if (qualified_type == sizetype
12948       && TYPE_NAME (qualified_type)
12949       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12950     {
12951       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12952
12953       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12954                            && TYPE_PRECISION (t)
12955                            == TYPE_PRECISION (qualified_type)
12956                            && TYPE_UNSIGNED (t)
12957                            == TYPE_UNSIGNED (qualified_type));
12958       qualified_type = t;
12959     }
12960
12961   /* If we do, then we can just use its DIE, if it exists.  */
12962   if (qualified_type)
12963     {
12964       mod_type_die = lookup_type_die (qualified_type);
12965       if (mod_type_die)
12966         return mod_type_die;
12967     }
12968
12969   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12970
12971   /* Handle C typedef types.  */
12972   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12973       && !DECL_ARTIFICIAL (name))
12974     {
12975       tree dtype = TREE_TYPE (name);
12976
12977       if (qualified_type == dtype)
12978         {
12979           /* For a named type, use the typedef.  */
12980           gen_type_die (qualified_type, context_die);
12981           return lookup_type_die (qualified_type);
12982         }
12983       else if (is_const_type < TYPE_READONLY (dtype)
12984                || is_volatile_type < TYPE_VOLATILE (dtype)
12985                || (is_const_type <= TYPE_READONLY (dtype)
12986                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12987                    && DECL_ORIGINAL_TYPE (name) != type))
12988         /* cv-unqualified version of named type.  Just use the unnamed
12989            type to which it refers.  */
12990         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12991                                   is_const_type, is_volatile_type,
12992                                   context_die);
12993       /* Else cv-qualified version of named type; fall through.  */
12994     }
12995
12996   if (is_const_type
12997       /* If both is_const_type and is_volatile_type, prefer the path
12998          which leads to a qualified type.  */
12999       && (!is_volatile_type
13000           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13001           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13002     {
13003       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13004       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13005     }
13006   else if (is_volatile_type)
13007     {
13008       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13009       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13010     }
13011   else if (code == POINTER_TYPE)
13012     {
13013       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13014       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13015                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13016       item_type = TREE_TYPE (type);
13017       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13018         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13019                          TYPE_ADDR_SPACE (item_type));
13020     }
13021   else if (code == REFERENCE_TYPE)
13022     {
13023       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13024         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13025                                 type);
13026       else
13027         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13028       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13029                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13030       item_type = TREE_TYPE (type);
13031       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13032         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13033                          TYPE_ADDR_SPACE (item_type));
13034     }
13035   else if (code == INTEGER_TYPE
13036            && TREE_TYPE (type) != NULL_TREE
13037            && subrange_type_for_debug_p (type, &low, &high))
13038     {
13039       mod_type_die = subrange_type_die (type, low, high, context_die);
13040       item_type = TREE_TYPE (type);
13041     }
13042   else if (is_base_type (type))
13043     mod_type_die = base_type_die (type);
13044   else
13045     {
13046       gen_type_die (type, context_die);
13047
13048       /* We have to get the type_main_variant here (and pass that to the
13049          `lookup_type_die' routine) because the ..._TYPE node we have
13050          might simply be a *copy* of some original type node (where the
13051          copy was created to help us keep track of typedef names) and
13052          that copy might have a different TYPE_UID from the original
13053          ..._TYPE node.  */
13054       if (TREE_CODE (type) != VECTOR_TYPE)
13055         return lookup_type_die (type_main_variant (type));
13056       else
13057         /* Vectors have the debugging information in the type,
13058            not the main variant.  */
13059         return lookup_type_die (type);
13060     }
13061
13062   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13063      don't output a DW_TAG_typedef, since there isn't one in the
13064      user's program; just attach a DW_AT_name to the type.
13065      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13066      if the base type already has the same name.  */
13067   if (name
13068       && ((TREE_CODE (name) != TYPE_DECL
13069            && (qualified_type == TYPE_MAIN_VARIANT (type)
13070                || (!is_const_type && !is_volatile_type)))
13071           || (TREE_CODE (name) == TYPE_DECL
13072               && TREE_TYPE (name) == qualified_type
13073               && DECL_NAME (name))))
13074     {
13075       if (TREE_CODE (name) == TYPE_DECL)
13076         /* Could just call add_name_and_src_coords_attributes here,
13077            but since this is a builtin type it doesn't have any
13078            useful source coordinates anyway.  */
13079         name = DECL_NAME (name);
13080       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13081     }
13082   /* This probably indicates a bug.  */
13083   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13084     add_name_attribute (mod_type_die, "__unknown__");
13085
13086   if (qualified_type)
13087     equate_type_number_to_die (qualified_type, mod_type_die);
13088
13089   if (item_type)
13090     /* We must do this after the equate_type_number_to_die call, in case
13091        this is a recursive type.  This ensures that the modified_type_die
13092        recursion will terminate even if the type is recursive.  Recursive
13093        types are possible in Ada.  */
13094     sub_die = modified_type_die (item_type,
13095                                  TYPE_READONLY (item_type),
13096                                  TYPE_VOLATILE (item_type),
13097                                  context_die);
13098
13099   if (sub_die != NULL)
13100     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13101
13102   return mod_type_die;
13103 }
13104
13105 /* Generate DIEs for the generic parameters of T.
13106    T must be either a generic type or a generic function.
13107    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13108
13109 static void
13110 gen_generic_params_dies (tree t)
13111 {
13112   tree parms, args;
13113   int parms_num, i;
13114   dw_die_ref die = NULL;
13115
13116   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13117     return;
13118
13119   if (TYPE_P (t))
13120     die = lookup_type_die (t);
13121   else if (DECL_P (t))
13122     die = lookup_decl_die (t);
13123
13124   gcc_assert (die);
13125
13126   parms = lang_hooks.get_innermost_generic_parms (t);
13127   if (!parms)
13128     /* T has no generic parameter. It means T is neither a generic type
13129        or function. End of story.  */
13130     return;
13131
13132   parms_num = TREE_VEC_LENGTH (parms);
13133   args = lang_hooks.get_innermost_generic_args (t);
13134   for (i = 0; i < parms_num; i++)
13135     {
13136       tree parm, arg, arg_pack_elems;
13137
13138       parm = TREE_VEC_ELT (parms, i);
13139       arg = TREE_VEC_ELT (args, i);
13140       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13141       gcc_assert (parm && TREE_VALUE (parm) && arg);
13142
13143       if (parm && TREE_VALUE (parm) && arg)
13144         {
13145           /* If PARM represents a template parameter pack,
13146              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13147              by DW_TAG_template_*_parameter DIEs for the argument
13148              pack elements of ARG. Note that ARG would then be
13149              an argument pack.  */
13150           if (arg_pack_elems)
13151             template_parameter_pack_die (TREE_VALUE (parm),
13152                                          arg_pack_elems,
13153                                          die);
13154           else
13155             generic_parameter_die (TREE_VALUE (parm), arg,
13156                                    true /* Emit DW_AT_name */, die);
13157         }
13158     }
13159 }
13160
13161 /* Create and return a DIE for PARM which should be
13162    the representation of a generic type parameter.
13163    For instance, in the C++ front end, PARM would be a template parameter.
13164    ARG is the argument to PARM.
13165    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13166    name of the PARM.
13167    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13168    as a child node.  */
13169
13170 static dw_die_ref
13171 generic_parameter_die (tree parm, tree arg,
13172                        bool emit_name_p,
13173                        dw_die_ref parent_die)
13174 {
13175   dw_die_ref tmpl_die = NULL;
13176   const char *name = NULL;
13177
13178   if (!parm || !DECL_NAME (parm) || !arg)
13179     return NULL;
13180
13181   /* We support non-type generic parameters and arguments,
13182      type generic parameters and arguments, as well as
13183      generic generic parameters (a.k.a. template template parameters in C++)
13184      and arguments.  */
13185   if (TREE_CODE (parm) == PARM_DECL)
13186     /* PARM is a nontype generic parameter  */
13187     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13188   else if (TREE_CODE (parm) == TYPE_DECL)
13189     /* PARM is a type generic parameter.  */
13190     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13191   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13192     /* PARM is a generic generic parameter.
13193        Its DIE is a GNU extension. It shall have a
13194        DW_AT_name attribute to represent the name of the template template
13195        parameter, and a DW_AT_GNU_template_name attribute to represent the
13196        name of the template template argument.  */
13197     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13198                         parent_die, parm);
13199   else
13200     gcc_unreachable ();
13201
13202   if (tmpl_die)
13203     {
13204       tree tmpl_type;
13205
13206       /* If PARM is a generic parameter pack, it means we are
13207          emitting debug info for a template argument pack element.
13208          In other terms, ARG is a template argument pack element.
13209          In that case, we don't emit any DW_AT_name attribute for
13210          the die.  */
13211       if (emit_name_p)
13212         {
13213           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13214           gcc_assert (name);
13215           add_AT_string (tmpl_die, DW_AT_name, name);
13216         }
13217
13218       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13219         {
13220           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13221              TMPL_DIE should have a child DW_AT_type attribute that is set
13222              to the type of the argument to PARM, which is ARG.
13223              If PARM is a type generic parameter, TMPL_DIE should have a
13224              child DW_AT_type that is set to ARG.  */
13225           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13226           add_type_attribute (tmpl_die, tmpl_type, 0,
13227                               TREE_THIS_VOLATILE (tmpl_type),
13228                               parent_die);
13229         }
13230       else
13231         {
13232           /* So TMPL_DIE is a DIE representing a
13233              a generic generic template parameter, a.k.a template template
13234              parameter in C++ and arg is a template.  */
13235
13236           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13237              to the name of the argument.  */
13238           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13239           if (name)
13240             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13241         }
13242
13243       if (TREE_CODE (parm) == PARM_DECL)
13244         /* So PARM is a non-type generic parameter.
13245            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13246            attribute of TMPL_DIE which value represents the value
13247            of ARG.
13248            We must be careful here:
13249            The value of ARG might reference some function decls.
13250            We might currently be emitting debug info for a generic
13251            type and types are emitted before function decls, we don't
13252            know if the function decls referenced by ARG will actually be
13253            emitted after cgraph computations.
13254            So must defer the generation of the DW_AT_const_value to
13255            after cgraph is ready.  */
13256         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13257     }
13258
13259   return tmpl_die;
13260 }
13261
13262 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13263    PARM_PACK must be a template parameter pack. The returned DIE
13264    will be child DIE of PARENT_DIE.  */
13265
13266 static dw_die_ref
13267 template_parameter_pack_die (tree parm_pack,
13268                              tree parm_pack_args,
13269                              dw_die_ref parent_die)
13270 {
13271   dw_die_ref die;
13272   int j;
13273
13274   gcc_assert (parent_die && parm_pack);
13275
13276   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13277   add_name_and_src_coords_attributes (die, parm_pack);
13278   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13279     generic_parameter_die (parm_pack,
13280                            TREE_VEC_ELT (parm_pack_args, j),
13281                            false /* Don't emit DW_AT_name */,
13282                            die);
13283   return die;
13284 }
13285
13286 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13287    an enumerated type.  */
13288
13289 static inline int
13290 type_is_enum (const_tree type)
13291 {
13292   return TREE_CODE (type) == ENUMERAL_TYPE;
13293 }
13294
13295 /* Return the DBX register number described by a given RTL node.  */
13296
13297 static unsigned int
13298 dbx_reg_number (const_rtx rtl)
13299 {
13300   unsigned regno = REGNO (rtl);
13301
13302   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13303
13304 #ifdef LEAF_REG_REMAP
13305   if (current_function_uses_only_leaf_regs)
13306     {
13307       int leaf_reg = LEAF_REG_REMAP (regno);
13308       if (leaf_reg != -1)
13309         regno = (unsigned) leaf_reg;
13310     }
13311 #endif
13312
13313   return DBX_REGISTER_NUMBER (regno);
13314 }
13315
13316 /* Optionally add a DW_OP_piece term to a location description expression.
13317    DW_OP_piece is only added if the location description expression already
13318    doesn't end with DW_OP_piece.  */
13319
13320 static void
13321 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13322 {
13323   dw_loc_descr_ref loc;
13324
13325   if (*list_head != NULL)
13326     {
13327       /* Find the end of the chain.  */
13328       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13329         ;
13330
13331       if (loc->dw_loc_opc != DW_OP_piece)
13332         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13333     }
13334 }
13335
13336 /* Return a location descriptor that designates a machine register or
13337    zero if there is none.  */
13338
13339 static dw_loc_descr_ref
13340 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13341 {
13342   rtx regs;
13343
13344   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13345     return 0;
13346
13347   /* We only use "frame base" when we're sure we're talking about the
13348      post-prologue local stack frame.  We do this by *not* running
13349      register elimination until this point, and recognizing the special
13350      argument pointer and soft frame pointer rtx's.
13351      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13352   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13353       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13354     {
13355       dw_loc_descr_ref result = NULL;
13356
13357       if (dwarf_version >= 4 || !dwarf_strict)
13358         {
13359           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13360           if (result)
13361             add_loc_descr (&result,
13362                            new_loc_descr (DW_OP_stack_value, 0, 0));
13363         }
13364       return result;
13365     }
13366
13367   regs = targetm.dwarf_register_span (rtl);
13368
13369   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13370     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13371   else
13372     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13373 }
13374
13375 /* Return a location descriptor that designates a machine register for
13376    a given hard register number.  */
13377
13378 static dw_loc_descr_ref
13379 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13380 {
13381   dw_loc_descr_ref reg_loc_descr;
13382
13383   if (regno <= 31)
13384     reg_loc_descr
13385       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13386   else
13387     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13388
13389   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13390     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13391
13392   return reg_loc_descr;
13393 }
13394
13395 /* Given an RTL of a register, return a location descriptor that
13396    designates a value that spans more than one register.  */
13397
13398 static dw_loc_descr_ref
13399 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13400                              enum var_init_status initialized)
13401 {
13402   int nregs, size, i;
13403   unsigned reg;
13404   dw_loc_descr_ref loc_result = NULL;
13405
13406   reg = REGNO (rtl);
13407 #ifdef LEAF_REG_REMAP
13408   if (current_function_uses_only_leaf_regs)
13409     {
13410       int leaf_reg = LEAF_REG_REMAP (reg);
13411       if (leaf_reg != -1)
13412         reg = (unsigned) leaf_reg;
13413     }
13414 #endif
13415   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13416   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13417
13418   /* Simple, contiguous registers.  */
13419   if (regs == NULL_RTX)
13420     {
13421       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13422
13423       loc_result = NULL;
13424       while (nregs--)
13425         {
13426           dw_loc_descr_ref t;
13427
13428           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13429                                       VAR_INIT_STATUS_INITIALIZED);
13430           add_loc_descr (&loc_result, t);
13431           add_loc_descr_op_piece (&loc_result, size);
13432           ++reg;
13433         }
13434       return loc_result;
13435     }
13436
13437   /* Now onto stupid register sets in non contiguous locations.  */
13438
13439   gcc_assert (GET_CODE (regs) == PARALLEL);
13440
13441   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13442   loc_result = NULL;
13443
13444   for (i = 0; i < XVECLEN (regs, 0); ++i)
13445     {
13446       dw_loc_descr_ref t;
13447
13448       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13449                                   VAR_INIT_STATUS_INITIALIZED);
13450       add_loc_descr (&loc_result, t);
13451       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13452       add_loc_descr_op_piece (&loc_result, size);
13453     }
13454
13455   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13456     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13457   return loc_result;
13458 }
13459
13460 /* Return a location descriptor that designates a constant.  */
13461
13462 static dw_loc_descr_ref
13463 int_loc_descriptor (HOST_WIDE_INT i)
13464 {
13465   enum dwarf_location_atom op;
13466
13467   /* Pick the smallest representation of a constant, rather than just
13468      defaulting to the LEB encoding.  */
13469   if (i >= 0)
13470     {
13471       if (i <= 31)
13472         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13473       else if (i <= 0xff)
13474         op = DW_OP_const1u;
13475       else if (i <= 0xffff)
13476         op = DW_OP_const2u;
13477       else if (HOST_BITS_PER_WIDE_INT == 32
13478                || i <= 0xffffffff)
13479         op = DW_OP_const4u;
13480       else
13481         op = DW_OP_constu;
13482     }
13483   else
13484     {
13485       if (i >= -0x80)
13486         op = DW_OP_const1s;
13487       else if (i >= -0x8000)
13488         op = DW_OP_const2s;
13489       else if (HOST_BITS_PER_WIDE_INT == 32
13490                || i >= -0x80000000)
13491         op = DW_OP_const4s;
13492       else
13493         op = DW_OP_consts;
13494     }
13495
13496   return new_loc_descr (op, i, 0);
13497 }
13498
13499 /* Return loc description representing "address" of integer value.
13500    This can appear only as toplevel expression.  */
13501
13502 static dw_loc_descr_ref
13503 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13504 {
13505   int litsize;
13506   dw_loc_descr_ref loc_result = NULL;
13507
13508   if (!(dwarf_version >= 4 || !dwarf_strict))
13509     return NULL;
13510
13511   if (i >= 0)
13512     {
13513       if (i <= 31)
13514         litsize = 1;
13515       else if (i <= 0xff)
13516         litsize = 2;
13517       else if (i <= 0xffff)
13518         litsize = 3;
13519       else if (HOST_BITS_PER_WIDE_INT == 32
13520                || i <= 0xffffffff)
13521         litsize = 5;
13522       else
13523         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13524     }
13525   else
13526     {
13527       if (i >= -0x80)
13528         litsize = 2;
13529       else if (i >= -0x8000)
13530         litsize = 3;
13531       else if (HOST_BITS_PER_WIDE_INT == 32
13532                || i >= -0x80000000)
13533         litsize = 5;
13534       else
13535         litsize = 1 + size_of_sleb128 (i);
13536     }
13537   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13538      is more compact.  For DW_OP_stack_value we need:
13539      litsize + 1 (DW_OP_stack_value)
13540      and for DW_OP_implicit_value:
13541      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13542   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13543     {
13544       loc_result = int_loc_descriptor (i);
13545       add_loc_descr (&loc_result,
13546                      new_loc_descr (DW_OP_stack_value, 0, 0));
13547       return loc_result;
13548     }
13549
13550   loc_result = new_loc_descr (DW_OP_implicit_value,
13551                               size, 0);
13552   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13553   loc_result->dw_loc_oprnd2.v.val_int = i;
13554   return loc_result;
13555 }
13556
13557 /* Return a location descriptor that designates a base+offset location.  */
13558
13559 static dw_loc_descr_ref
13560 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13561                  enum var_init_status initialized)
13562 {
13563   unsigned int regno;
13564   dw_loc_descr_ref result;
13565   dw_fde_ref fde = current_fde ();
13566
13567   /* We only use "frame base" when we're sure we're talking about the
13568      post-prologue local stack frame.  We do this by *not* running
13569      register elimination until this point, and recognizing the special
13570      argument pointer and soft frame pointer rtx's.  */
13571   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13572     {
13573       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13574
13575       if (elim != reg)
13576         {
13577           if (GET_CODE (elim) == PLUS)
13578             {
13579               offset += INTVAL (XEXP (elim, 1));
13580               elim = XEXP (elim, 0);
13581             }
13582           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13583                        && (elim == hard_frame_pointer_rtx
13584                            || elim == stack_pointer_rtx))
13585                       || elim == (frame_pointer_needed
13586                                   ? hard_frame_pointer_rtx
13587                                   : stack_pointer_rtx));
13588
13589           /* If drap register is used to align stack, use frame
13590              pointer + offset to access stack variables.  If stack
13591              is aligned without drap, use stack pointer + offset to
13592              access stack variables.  */
13593           if (crtl->stack_realign_tried
13594               && reg == frame_pointer_rtx)
13595             {
13596               int base_reg
13597                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13598                                       ? HARD_FRAME_POINTER_REGNUM
13599                                       : STACK_POINTER_REGNUM);
13600               return new_reg_loc_descr (base_reg, offset);
13601             }
13602
13603           offset += frame_pointer_fb_offset;
13604           return new_loc_descr (DW_OP_fbreg, offset, 0);
13605         }
13606     }
13607   else if (!optimize
13608            && fde
13609            && (fde->drap_reg == REGNO (reg)
13610                || fde->vdrap_reg == REGNO (reg)))
13611     {
13612       /* Use cfa+offset to represent the location of arguments passed
13613          on the stack when drap is used to align stack.
13614          Only do this when not optimizing, for optimized code var-tracking
13615          is supposed to track where the arguments live and the register
13616          used as vdrap or drap in some spot might be used for something
13617          else in other part of the routine.  */
13618       return new_loc_descr (DW_OP_fbreg, offset, 0);
13619     }
13620
13621   regno = dbx_reg_number (reg);
13622   if (regno <= 31)
13623     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13624                             offset, 0);
13625   else
13626     result = new_loc_descr (DW_OP_bregx, regno, offset);
13627
13628   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13629     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13630
13631   return result;
13632 }
13633
13634 /* Return true if this RTL expression describes a base+offset calculation.  */
13635
13636 static inline int
13637 is_based_loc (const_rtx rtl)
13638 {
13639   return (GET_CODE (rtl) == PLUS
13640           && ((REG_P (XEXP (rtl, 0))
13641                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13642                && CONST_INT_P (XEXP (rtl, 1)))));
13643 }
13644
13645 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13646    failed.  */
13647
13648 static dw_loc_descr_ref
13649 tls_mem_loc_descriptor (rtx mem)
13650 {
13651   tree base;
13652   dw_loc_descr_ref loc_result;
13653
13654   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13655     return NULL;
13656
13657   base = get_base_address (MEM_EXPR (mem));
13658   if (base == NULL
13659       || TREE_CODE (base) != VAR_DECL
13660       || !DECL_THREAD_LOCAL_P (base))
13661     return NULL;
13662
13663   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13664   if (loc_result == NULL)
13665     return NULL;
13666
13667   if (INTVAL (MEM_OFFSET (mem)))
13668     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13669
13670   return loc_result;
13671 }
13672
13673 /* Output debug info about reason why we failed to expand expression as dwarf
13674    expression.  */
13675
13676 static void
13677 expansion_failed (tree expr, rtx rtl, char const *reason)
13678 {
13679   if (dump_file && (dump_flags & TDF_DETAILS))
13680     {
13681       fprintf (dump_file, "Failed to expand as dwarf: ");
13682       if (expr)
13683         print_generic_expr (dump_file, expr, dump_flags);
13684       if (rtl)
13685         {
13686           fprintf (dump_file, "\n");
13687           print_rtl (dump_file, rtl);
13688         }
13689       fprintf (dump_file, "\nReason: %s\n", reason);
13690     }
13691 }
13692
13693 /* Helper function for const_ok_for_output, called either directly
13694    or via for_each_rtx.  */
13695
13696 static int
13697 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13698 {
13699   rtx rtl = *rtlp;
13700
13701   if (GET_CODE (rtl) == UNSPEC)
13702     {
13703       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13704          we can't express it in the debug info.  */
13705 #ifdef ENABLE_CHECKING
13706       /* Don't complain about TLS UNSPECs, those are just too hard to
13707          delegitimize.  */
13708       if (XVECLEN (rtl, 0) != 1
13709           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13710           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13711           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13712           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13713         inform (current_function_decl
13714                 ? DECL_SOURCE_LOCATION (current_function_decl)
13715                 : UNKNOWN_LOCATION,
13716                 "non-delegitimized UNSPEC %d found in variable location",
13717                 XINT (rtl, 1));
13718 #endif
13719       expansion_failed (NULL_TREE, rtl,
13720                         "UNSPEC hasn't been delegitimized.\n");
13721       return 1;
13722     }
13723
13724   if (GET_CODE (rtl) != SYMBOL_REF)
13725     return 0;
13726
13727   if (CONSTANT_POOL_ADDRESS_P (rtl))
13728     {
13729       bool marked;
13730       get_pool_constant_mark (rtl, &marked);
13731       /* If all references to this pool constant were optimized away,
13732          it was not output and thus we can't represent it.  */
13733       if (!marked)
13734         {
13735           expansion_failed (NULL_TREE, rtl,
13736                             "Constant was removed from constant pool.\n");
13737           return 1;
13738         }
13739     }
13740
13741   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13742     return 1;
13743
13744   /* Avoid references to external symbols in debug info, on several targets
13745      the linker might even refuse to link when linking a shared library,
13746      and in many other cases the relocations for .debug_info/.debug_loc are
13747      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13748      to be defined within the same shared library or executable are fine.  */
13749   if (SYMBOL_REF_EXTERNAL_P (rtl))
13750     {
13751       tree decl = SYMBOL_REF_DECL (rtl);
13752
13753       if (decl == NULL || !targetm.binds_local_p (decl))
13754         {
13755           expansion_failed (NULL_TREE, rtl,
13756                             "Symbol not defined in current TU.\n");
13757           return 1;
13758         }
13759     }
13760
13761   return 0;
13762 }
13763
13764 /* Return true if constant RTL can be emitted in DW_OP_addr or
13765    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13766    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13767
13768 static bool
13769 const_ok_for_output (rtx rtl)
13770 {
13771   if (GET_CODE (rtl) == SYMBOL_REF)
13772     return const_ok_for_output_1 (&rtl, NULL) == 0;
13773
13774   if (GET_CODE (rtl) == CONST)
13775     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13776
13777   return true;
13778 }
13779
13780 /* The following routine converts the RTL for a variable or parameter
13781    (resident in memory) into an equivalent Dwarf representation of a
13782    mechanism for getting the address of that same variable onto the top of a
13783    hypothetical "address evaluation" stack.
13784
13785    When creating memory location descriptors, we are effectively transforming
13786    the RTL for a memory-resident object into its Dwarf postfix expression
13787    equivalent.  This routine recursively descends an RTL tree, turning
13788    it into Dwarf postfix code as it goes.
13789
13790    MODE is the mode of the memory reference, needed to handle some
13791    autoincrement addressing modes.
13792
13793    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13794    location list for RTL.
13795
13796    Return 0 if we can't represent the location.  */
13797
13798 static dw_loc_descr_ref
13799 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13800                     enum var_init_status initialized)
13801 {
13802   dw_loc_descr_ref mem_loc_result = NULL;
13803   enum dwarf_location_atom op;
13804   dw_loc_descr_ref op0, op1;
13805
13806   /* Note that for a dynamically sized array, the location we will generate a
13807      description of here will be the lowest numbered location which is
13808      actually within the array.  That's *not* necessarily the same as the
13809      zeroth element of the array.  */
13810
13811   rtl = targetm.delegitimize_address (rtl);
13812
13813   switch (GET_CODE (rtl))
13814     {
13815     case POST_INC:
13816     case POST_DEC:
13817     case POST_MODIFY:
13818       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13819
13820     case SUBREG:
13821       /* The case of a subreg may arise when we have a local (register)
13822          variable or a formal (register) parameter which doesn't quite fill
13823          up an entire register.  For now, just assume that it is
13824          legitimate to make the Dwarf info refer to the whole register which
13825          contains the given subreg.  */
13826       if (!subreg_lowpart_p (rtl))
13827         break;
13828       rtl = SUBREG_REG (rtl);
13829       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13830         break;
13831       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13832         break;
13833       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13834       break;
13835
13836     case REG:
13837       /* Whenever a register number forms a part of the description of the
13838          method for calculating the (dynamic) address of a memory resident
13839          object, DWARF rules require the register number be referred to as
13840          a "base register".  This distinction is not based in any way upon
13841          what category of register the hardware believes the given register
13842          belongs to.  This is strictly DWARF terminology we're dealing with
13843          here. Note that in cases where the location of a memory-resident
13844          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13845          OP_CONST (0)) the actual DWARF location descriptor that we generate
13846          may just be OP_BASEREG (basereg).  This may look deceptively like
13847          the object in question was allocated to a register (rather than in
13848          memory) so DWARF consumers need to be aware of the subtle
13849          distinction between OP_REG and OP_BASEREG.  */
13850       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13851         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13852       else if (stack_realign_drap
13853                && crtl->drap_reg
13854                && crtl->args.internal_arg_pointer == rtl
13855                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13856         {
13857           /* If RTL is internal_arg_pointer, which has been optimized
13858              out, use DRAP instead.  */
13859           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13860                                             VAR_INIT_STATUS_INITIALIZED);
13861         }
13862       break;
13863
13864     case SIGN_EXTEND:
13865     case ZERO_EXTEND:
13866       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13867                                 VAR_INIT_STATUS_INITIALIZED);
13868       if (op0 == 0)
13869         break;
13870       else
13871         {
13872           int shift = DWARF2_ADDR_SIZE
13873                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13874           shift *= BITS_PER_UNIT;
13875           if (GET_CODE (rtl) == SIGN_EXTEND)
13876             op = DW_OP_shra;
13877           else
13878             op = DW_OP_shr;
13879           mem_loc_result = op0;
13880           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13881           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13882           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13883           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13884         }
13885       break;
13886
13887     case MEM:
13888       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13889                                            VAR_INIT_STATUS_INITIALIZED);
13890       if (mem_loc_result == NULL)
13891         mem_loc_result = tls_mem_loc_descriptor (rtl);
13892       if (mem_loc_result != 0)
13893         {
13894           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13895             {
13896               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13897               return 0;
13898             }
13899           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13900             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13901           else
13902             add_loc_descr (&mem_loc_result,
13903                            new_loc_descr (DW_OP_deref_size,
13904                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13905         }
13906       else
13907         {
13908           rtx new_rtl = avoid_constant_pool_reference (rtl);
13909           if (new_rtl != rtl)
13910             return mem_loc_descriptor (new_rtl, mode, initialized);
13911         }
13912       break;
13913
13914     case LO_SUM:
13915          rtl = XEXP (rtl, 1);
13916
13917       /* ... fall through ...  */
13918
13919     case LABEL_REF:
13920       /* Some ports can transform a symbol ref into a label ref, because
13921          the symbol ref is too far away and has to be dumped into a constant
13922          pool.  */
13923     case CONST:
13924     case SYMBOL_REF:
13925       if (GET_CODE (rtl) == SYMBOL_REF
13926           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13927         {
13928           dw_loc_descr_ref temp;
13929
13930           /* If this is not defined, we have no way to emit the data.  */
13931           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13932             break;
13933
13934           /* We used to emit DW_OP_addr here, but that's wrong, since
13935              DW_OP_addr should be relocated by the debug info consumer,
13936              while DW_OP_GNU_push_tls_address operand should not.  */
13937           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13938                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13939           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13940           temp->dw_loc_oprnd1.v.val_addr = rtl;
13941           temp->dtprel = true;
13942
13943           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13944           add_loc_descr (&mem_loc_result, temp);
13945
13946           break;
13947         }
13948
13949       if (!const_ok_for_output (rtl))
13950         break;
13951
13952     symref:
13953       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13954       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13955       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13956       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13957       break;
13958
13959     case CONCAT:
13960     case CONCATN:
13961     case VAR_LOCATION:
13962     case DEBUG_IMPLICIT_PTR:
13963       expansion_failed (NULL_TREE, rtl,
13964                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13965       return 0;
13966
13967     case PRE_MODIFY:
13968       /* Extract the PLUS expression nested inside and fall into
13969          PLUS code below.  */
13970       rtl = XEXP (rtl, 1);
13971       goto plus;
13972
13973     case PRE_INC:
13974     case PRE_DEC:
13975       /* Turn these into a PLUS expression and fall into the PLUS code
13976          below.  */
13977       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13978                           GEN_INT (GET_CODE (rtl) == PRE_INC
13979                                    ? GET_MODE_UNIT_SIZE (mode)
13980                                    : -GET_MODE_UNIT_SIZE (mode)));
13981
13982       /* ... fall through ...  */
13983
13984     case PLUS:
13985     plus:
13986       if (is_based_loc (rtl))
13987         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13988                                           INTVAL (XEXP (rtl, 1)),
13989                                           VAR_INIT_STATUS_INITIALIZED);
13990       else
13991         {
13992           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13993                                                VAR_INIT_STATUS_INITIALIZED);
13994           if (mem_loc_result == 0)
13995             break;
13996
13997           if (CONST_INT_P (XEXP (rtl, 1)))
13998             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13999           else
14000             {
14001               dw_loc_descr_ref mem_loc_result2
14002                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14003                                       VAR_INIT_STATUS_INITIALIZED);
14004               if (mem_loc_result2 == 0)
14005                 break;
14006               add_loc_descr (&mem_loc_result, mem_loc_result2);
14007               add_loc_descr (&mem_loc_result,
14008                              new_loc_descr (DW_OP_plus, 0, 0));
14009             }
14010         }
14011       break;
14012
14013     /* If a pseudo-reg is optimized away, it is possible for it to
14014        be replaced with a MEM containing a multiply or shift.  */
14015     case MINUS:
14016       op = DW_OP_minus;
14017       goto do_binop;
14018
14019     case MULT:
14020       op = DW_OP_mul;
14021       goto do_binop;
14022
14023     case DIV:
14024       op = DW_OP_div;
14025       goto do_binop;
14026
14027     case UMOD:
14028       op = DW_OP_mod;
14029       goto do_binop;
14030
14031     case ASHIFT:
14032       op = DW_OP_shl;
14033       goto do_binop;
14034
14035     case ASHIFTRT:
14036       op = DW_OP_shra;
14037       goto do_binop;
14038
14039     case LSHIFTRT:
14040       op = DW_OP_shr;
14041       goto do_binop;
14042
14043     case AND:
14044       op = DW_OP_and;
14045       goto do_binop;
14046
14047     case IOR:
14048       op = DW_OP_or;
14049       goto do_binop;
14050
14051     case XOR:
14052       op = DW_OP_xor;
14053       goto do_binop;
14054
14055     do_binop:
14056       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14057                                 VAR_INIT_STATUS_INITIALIZED);
14058       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14059                                 VAR_INIT_STATUS_INITIALIZED);
14060
14061       if (op0 == 0 || op1 == 0)
14062         break;
14063
14064       mem_loc_result = op0;
14065       add_loc_descr (&mem_loc_result, op1);
14066       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14067       break;
14068
14069     case MOD:
14070       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14071                                 VAR_INIT_STATUS_INITIALIZED);
14072       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14073                                 VAR_INIT_STATUS_INITIALIZED);
14074
14075       if (op0 == 0 || op1 == 0)
14076         break;
14077
14078       mem_loc_result = op0;
14079       add_loc_descr (&mem_loc_result, op1);
14080       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14081       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14082       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14083       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14084       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14085       break;
14086
14087     case NOT:
14088       op = DW_OP_not;
14089       goto do_unop;
14090
14091     case ABS:
14092       op = DW_OP_abs;
14093       goto do_unop;
14094
14095     case NEG:
14096       op = DW_OP_neg;
14097       goto do_unop;
14098
14099     do_unop:
14100       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14101                                 VAR_INIT_STATUS_INITIALIZED);
14102
14103       if (op0 == 0)
14104         break;
14105
14106       mem_loc_result = op0;
14107       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14108       break;
14109
14110     case CONST_INT:
14111       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14112       break;
14113
14114     case EQ:
14115       op = DW_OP_eq;
14116       goto do_scompare;
14117
14118     case GE:
14119       op = DW_OP_ge;
14120       goto do_scompare;
14121
14122     case GT:
14123       op = DW_OP_gt;
14124       goto do_scompare;
14125
14126     case LE:
14127       op = DW_OP_le;
14128       goto do_scompare;
14129
14130     case LT:
14131       op = DW_OP_lt;
14132       goto do_scompare;
14133
14134     case NE:
14135       op = DW_OP_ne;
14136       goto do_scompare;
14137
14138     do_scompare:
14139       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14140           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14141         break;
14142       else
14143         {
14144           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14145
14146           if (op_mode == VOIDmode)
14147             op_mode = GET_MODE (XEXP (rtl, 1));
14148           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14149             break;
14150
14151           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14152                                     VAR_INIT_STATUS_INITIALIZED);
14153           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14154                                     VAR_INIT_STATUS_INITIALIZED);
14155
14156           if (op0 == 0 || op1 == 0)
14157             break;
14158
14159           if (op_mode != VOIDmode
14160               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14161             {
14162               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14163               shift *= BITS_PER_UNIT;
14164               /* For eq/ne, if the operands are known to be zero-extended,
14165                  there is no need to do the fancy shifting up.  */
14166               if (op == DW_OP_eq || op == DW_OP_ne)
14167                 {
14168                   dw_loc_descr_ref last0, last1;
14169                   for (last0 = op0;
14170                        last0->dw_loc_next != NULL;
14171                        last0 = last0->dw_loc_next)
14172                     ;
14173                   for (last1 = op1;
14174                        last1->dw_loc_next != NULL;
14175                        last1 = last1->dw_loc_next)
14176                     ;
14177                   /* deref_size zero extends, and for constants we can check
14178                      whether they are zero extended or not.  */
14179                   if (((last0->dw_loc_opc == DW_OP_deref_size
14180                         && last0->dw_loc_oprnd1.v.val_int
14181                            <= GET_MODE_SIZE (op_mode))
14182                        || (CONST_INT_P (XEXP (rtl, 0))
14183                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14184                                == (INTVAL (XEXP (rtl, 0))
14185                                    & GET_MODE_MASK (op_mode))))
14186                       && ((last1->dw_loc_opc == DW_OP_deref_size
14187                            && last1->dw_loc_oprnd1.v.val_int
14188                               <= GET_MODE_SIZE (op_mode))
14189                           || (CONST_INT_P (XEXP (rtl, 1))
14190                               && (unsigned HOST_WIDE_INT)
14191                                  INTVAL (XEXP (rtl, 1))
14192                                  == (INTVAL (XEXP (rtl, 1))
14193                                      & GET_MODE_MASK (op_mode)))))
14194                     goto do_compare;
14195                 }
14196               add_loc_descr (&op0, int_loc_descriptor (shift));
14197               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14198               if (CONST_INT_P (XEXP (rtl, 1)))
14199                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14200               else
14201                 {
14202                   add_loc_descr (&op1, int_loc_descriptor (shift));
14203                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14204                 }
14205             }
14206         }
14207
14208     do_compare:
14209       mem_loc_result = op0;
14210       add_loc_descr (&mem_loc_result, op1);
14211       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14212       if (STORE_FLAG_VALUE != 1)
14213         {
14214           add_loc_descr (&mem_loc_result,
14215                          int_loc_descriptor (STORE_FLAG_VALUE));
14216           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14217         }
14218       break;
14219
14220     case GEU:
14221       op = DW_OP_ge;
14222       goto do_ucompare;
14223
14224     case GTU:
14225       op = DW_OP_gt;
14226       goto do_ucompare;
14227
14228     case LEU:
14229       op = DW_OP_le;
14230       goto do_ucompare;
14231
14232     case LTU:
14233       op = DW_OP_lt;
14234       goto do_ucompare;
14235
14236     do_ucompare:
14237       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14238           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14239         break;
14240       else
14241         {
14242           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14243
14244           if (op_mode == VOIDmode)
14245             op_mode = GET_MODE (XEXP (rtl, 1));
14246           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14247             break;
14248
14249           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14250                                     VAR_INIT_STATUS_INITIALIZED);
14251           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14252                                     VAR_INIT_STATUS_INITIALIZED);
14253
14254           if (op0 == 0 || op1 == 0)
14255             break;
14256
14257           if (op_mode != VOIDmode
14258               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14259             {
14260               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14261               dw_loc_descr_ref last0, last1;
14262               for (last0 = op0;
14263                    last0->dw_loc_next != NULL;
14264                    last0 = last0->dw_loc_next)
14265                 ;
14266               for (last1 = op1;
14267                    last1->dw_loc_next != NULL;
14268                    last1 = last1->dw_loc_next)
14269                 ;
14270               if (CONST_INT_P (XEXP (rtl, 0)))
14271                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14272               /* deref_size zero extends, so no need to mask it again.  */
14273               else if (last0->dw_loc_opc != DW_OP_deref_size
14274                        || last0->dw_loc_oprnd1.v.val_int
14275                           > GET_MODE_SIZE (op_mode))
14276                 {
14277                   add_loc_descr (&op0, int_loc_descriptor (mask));
14278                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14279                 }
14280               if (CONST_INT_P (XEXP (rtl, 1)))
14281                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14282               /* deref_size zero extends, so no need to mask it again.  */
14283               else if (last1->dw_loc_opc != DW_OP_deref_size
14284                        || last1->dw_loc_oprnd1.v.val_int
14285                           > GET_MODE_SIZE (op_mode))
14286                 {
14287                   add_loc_descr (&op1, int_loc_descriptor (mask));
14288                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14289                 }
14290             }
14291           else
14292             {
14293               HOST_WIDE_INT bias = 1;
14294               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14295               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14296               if (CONST_INT_P (XEXP (rtl, 1)))
14297                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14298                                           + INTVAL (XEXP (rtl, 1)));
14299               else
14300                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14301                                                     bias, 0));
14302             }
14303         }
14304       goto do_compare;
14305
14306     case SMIN:
14307     case SMAX:
14308     case UMIN:
14309     case UMAX:
14310       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14311           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14312           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14313         break;
14314
14315       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14316                                 VAR_INIT_STATUS_INITIALIZED);
14317       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14318                                 VAR_INIT_STATUS_INITIALIZED);
14319
14320       if (op0 == 0 || op1 == 0)
14321         break;
14322
14323       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14324       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14325       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14326       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14327         {
14328           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14329             {
14330               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14331               add_loc_descr (&op0, int_loc_descriptor (mask));
14332               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14333               add_loc_descr (&op1, int_loc_descriptor (mask));
14334               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14335             }
14336           else
14337             {
14338               HOST_WIDE_INT bias = 1;
14339               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14340               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14341               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14342             }
14343         }
14344       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14345         {
14346           int shift = DWARF2_ADDR_SIZE
14347                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14348           shift *= BITS_PER_UNIT;
14349           add_loc_descr (&op0, int_loc_descriptor (shift));
14350           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14351           add_loc_descr (&op1, int_loc_descriptor (shift));
14352           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14353         }
14354
14355       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14356         op = DW_OP_lt;
14357       else
14358         op = DW_OP_gt;
14359       mem_loc_result = op0;
14360       add_loc_descr (&mem_loc_result, op1);
14361       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14362       {
14363         dw_loc_descr_ref bra_node, drop_node;
14364
14365         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14366         add_loc_descr (&mem_loc_result, bra_node);
14367         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14368         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14369         add_loc_descr (&mem_loc_result, drop_node);
14370         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14371         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14372       }
14373       break;
14374
14375     case ZERO_EXTRACT:
14376     case SIGN_EXTRACT:
14377       if (CONST_INT_P (XEXP (rtl, 1))
14378           && CONST_INT_P (XEXP (rtl, 2))
14379           && ((unsigned) INTVAL (XEXP (rtl, 1))
14380               + (unsigned) INTVAL (XEXP (rtl, 2))
14381               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14382           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14383           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14384         {
14385           int shift, size;
14386           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14387                                     VAR_INIT_STATUS_INITIALIZED);
14388           if (op0 == 0)
14389             break;
14390           if (GET_CODE (rtl) == SIGN_EXTRACT)
14391             op = DW_OP_shra;
14392           else
14393             op = DW_OP_shr;
14394           mem_loc_result = op0;
14395           size = INTVAL (XEXP (rtl, 1));
14396           shift = INTVAL (XEXP (rtl, 2));
14397           if (BITS_BIG_ENDIAN)
14398             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14399                     - shift - size;
14400           if (shift + size != (int) DWARF2_ADDR_SIZE)
14401             {
14402               add_loc_descr (&mem_loc_result,
14403                              int_loc_descriptor (DWARF2_ADDR_SIZE
14404                                                  - shift - size));
14405               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14406             }
14407           if (size != (int) DWARF2_ADDR_SIZE)
14408             {
14409               add_loc_descr (&mem_loc_result,
14410                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14411               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14412             }
14413         }
14414       break;
14415
14416     case IF_THEN_ELSE:
14417       {
14418         dw_loc_descr_ref op2, bra_node, drop_node;
14419         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14420                                   VAR_INIT_STATUS_INITIALIZED);
14421         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14422                                   VAR_INIT_STATUS_INITIALIZED);
14423         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14424                                   VAR_INIT_STATUS_INITIALIZED);
14425         if (op0 == NULL || op1 == NULL || op2 == NULL)
14426           break;
14427
14428         mem_loc_result = op1;
14429         add_loc_descr (&mem_loc_result, op2);
14430         add_loc_descr (&mem_loc_result, op0);
14431         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14432         add_loc_descr (&mem_loc_result, bra_node);
14433         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14434         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14435         add_loc_descr (&mem_loc_result, drop_node);
14436         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14437         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14438       }
14439       break;
14440
14441     case COMPARE:
14442     case ROTATE:
14443     case ROTATERT:
14444     case TRUNCATE:
14445       /* In theory, we could implement the above.  */
14446       /* DWARF cannot represent the unsigned compare operations
14447          natively.  */
14448     case SS_MULT:
14449     case US_MULT:
14450     case SS_DIV:
14451     case US_DIV:
14452     case SS_PLUS:
14453     case US_PLUS:
14454     case SS_MINUS:
14455     case US_MINUS:
14456     case SS_NEG:
14457     case US_NEG:
14458     case SS_ABS:
14459     case SS_ASHIFT:
14460     case US_ASHIFT:
14461     case SS_TRUNCATE:
14462     case US_TRUNCATE:
14463     case UDIV:
14464     case UNORDERED:
14465     case ORDERED:
14466     case UNEQ:
14467     case UNGE:
14468     case UNGT:
14469     case UNLE:
14470     case UNLT:
14471     case LTGT:
14472     case FLOAT_EXTEND:
14473     case FLOAT_TRUNCATE:
14474     case FLOAT:
14475     case UNSIGNED_FLOAT:
14476     case FIX:
14477     case UNSIGNED_FIX:
14478     case FRACT_CONVERT:
14479     case UNSIGNED_FRACT_CONVERT:
14480     case SAT_FRACT:
14481     case UNSIGNED_SAT_FRACT:
14482     case SQRT:
14483     case BSWAP:
14484     case FFS:
14485     case CLZ:
14486     case CTZ:
14487     case POPCOUNT:
14488     case PARITY:
14489     case ASM_OPERANDS:
14490     case VEC_MERGE:
14491     case VEC_SELECT:
14492     case VEC_CONCAT:
14493     case VEC_DUPLICATE:
14494     case UNSPEC:
14495     case HIGH:
14496       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14497          can't express it in the debug info.  This can happen e.g. with some
14498          TLS UNSPECs.  */
14499       break;
14500
14501     case CONST_STRING:
14502       resolve_one_addr (&rtl, NULL);
14503       goto symref;
14504
14505     default:
14506 #ifdef ENABLE_CHECKING
14507       print_rtl (stderr, rtl);
14508       gcc_unreachable ();
14509 #else
14510       break;
14511 #endif
14512     }
14513
14514   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14515     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14516
14517   return mem_loc_result;
14518 }
14519
14520 /* Return a descriptor that describes the concatenation of two locations.
14521    This is typically a complex variable.  */
14522
14523 static dw_loc_descr_ref
14524 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14525 {
14526   dw_loc_descr_ref cc_loc_result = NULL;
14527   dw_loc_descr_ref x0_ref
14528     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14529   dw_loc_descr_ref x1_ref
14530     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14531
14532   if (x0_ref == 0 || x1_ref == 0)
14533     return 0;
14534
14535   cc_loc_result = x0_ref;
14536   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14537
14538   add_loc_descr (&cc_loc_result, x1_ref);
14539   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14540
14541   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14542     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14543
14544   return cc_loc_result;
14545 }
14546
14547 /* Return a descriptor that describes the concatenation of N
14548    locations.  */
14549
14550 static dw_loc_descr_ref
14551 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14552 {
14553   unsigned int i;
14554   dw_loc_descr_ref cc_loc_result = NULL;
14555   unsigned int n = XVECLEN (concatn, 0);
14556
14557   for (i = 0; i < n; ++i)
14558     {
14559       dw_loc_descr_ref ref;
14560       rtx x = XVECEXP (concatn, 0, i);
14561
14562       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14563       if (ref == NULL)
14564         return NULL;
14565
14566       add_loc_descr (&cc_loc_result, ref);
14567       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14568     }
14569
14570   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14571     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14572
14573   return cc_loc_result;
14574 }
14575
14576 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14577    for DEBUG_IMPLICIT_PTR RTL.  */
14578
14579 static dw_loc_descr_ref
14580 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14581 {
14582   dw_loc_descr_ref ret;
14583   dw_die_ref ref;
14584
14585   if (dwarf_strict)
14586     return NULL;
14587   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14588               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14589               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14590   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14591   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14592   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14593   if (ref)
14594     {
14595       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14596       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14597       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14598     }
14599   else
14600     {
14601       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14602       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14603     }
14604   return ret;
14605 }
14606
14607 /* Output a proper Dwarf location descriptor for a variable or parameter
14608    which is either allocated in a register or in a memory location.  For a
14609    register, we just generate an OP_REG and the register number.  For a
14610    memory location we provide a Dwarf postfix expression describing how to
14611    generate the (dynamic) address of the object onto the address stack.
14612
14613    MODE is mode of the decl if this loc_descriptor is going to be used in
14614    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14615    allowed, VOIDmode otherwise.
14616
14617    If we don't know how to describe it, return 0.  */
14618
14619 static dw_loc_descr_ref
14620 loc_descriptor (rtx rtl, enum machine_mode mode,
14621                 enum var_init_status initialized)
14622 {
14623   dw_loc_descr_ref loc_result = NULL;
14624
14625   switch (GET_CODE (rtl))
14626     {
14627     case SUBREG:
14628       /* The case of a subreg may arise when we have a local (register)
14629          variable or a formal (register) parameter which doesn't quite fill
14630          up an entire register.  For now, just assume that it is
14631          legitimate to make the Dwarf info refer to the whole register which
14632          contains the given subreg.  */
14633       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14634       break;
14635
14636     case REG:
14637       loc_result = reg_loc_descriptor (rtl, initialized);
14638       break;
14639
14640     case MEM:
14641       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14642                                        initialized);
14643       if (loc_result == NULL)
14644         loc_result = tls_mem_loc_descriptor (rtl);
14645       if (loc_result == NULL)
14646         {
14647           rtx new_rtl = avoid_constant_pool_reference (rtl);
14648           if (new_rtl != rtl)
14649             loc_result = loc_descriptor (new_rtl, mode, initialized);
14650         }
14651       break;
14652
14653     case CONCAT:
14654       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14655                                           initialized);
14656       break;
14657
14658     case CONCATN:
14659       loc_result = concatn_loc_descriptor (rtl, initialized);
14660       break;
14661
14662     case VAR_LOCATION:
14663       /* Single part.  */
14664       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14665         {
14666           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14667           if (GET_CODE (loc) == EXPR_LIST)
14668             loc = XEXP (loc, 0);
14669           loc_result = loc_descriptor (loc, mode, initialized);
14670           break;
14671         }
14672
14673       rtl = XEXP (rtl, 1);
14674       /* FALLTHRU */
14675
14676     case PARALLEL:
14677       {
14678         rtvec par_elems = XVEC (rtl, 0);
14679         int num_elem = GET_NUM_ELEM (par_elems);
14680         enum machine_mode mode;
14681         int i;
14682
14683         /* Create the first one, so we have something to add to.  */
14684         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14685                                      VOIDmode, initialized);
14686         if (loc_result == NULL)
14687           return NULL;
14688         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14689         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14690         for (i = 1; i < num_elem; i++)
14691           {
14692             dw_loc_descr_ref temp;
14693
14694             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14695                                    VOIDmode, initialized);
14696             if (temp == NULL)
14697               return NULL;
14698             add_loc_descr (&loc_result, temp);
14699             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14700             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14701           }
14702       }
14703       break;
14704
14705     case CONST_INT:
14706       if (mode != VOIDmode && mode != BLKmode)
14707         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14708                                                     INTVAL (rtl));
14709       break;
14710
14711     case CONST_DOUBLE:
14712       if (mode == VOIDmode)
14713         mode = GET_MODE (rtl);
14714
14715       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14716         {
14717           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14718
14719           /* Note that a CONST_DOUBLE rtx could represent either an integer
14720              or a floating-point constant.  A CONST_DOUBLE is used whenever
14721              the constant requires more than one word in order to be
14722              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14723           loc_result = new_loc_descr (DW_OP_implicit_value,
14724                                       GET_MODE_SIZE (mode), 0);
14725           if (SCALAR_FLOAT_MODE_P (mode))
14726             {
14727               unsigned int length = GET_MODE_SIZE (mode);
14728               unsigned char *array
14729                   = (unsigned char*) ggc_alloc_atomic (length);
14730
14731               insert_float (rtl, array);
14732               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14733               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14734               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14735               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14736             }
14737           else
14738             {
14739               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14740               loc_result->dw_loc_oprnd2.v.val_double
14741                 = rtx_to_double_int (rtl);
14742             }
14743         }
14744       break;
14745
14746     case CONST_VECTOR:
14747       if (mode == VOIDmode)
14748         mode = GET_MODE (rtl);
14749
14750       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14751         {
14752           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14753           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14754           unsigned char *array = (unsigned char *)
14755             ggc_alloc_atomic (length * elt_size);
14756           unsigned int i;
14757           unsigned char *p;
14758
14759           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14760           switch (GET_MODE_CLASS (mode))
14761             {
14762             case MODE_VECTOR_INT:
14763               for (i = 0, p = array; i < length; i++, p += elt_size)
14764                 {
14765                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14766                   double_int val = rtx_to_double_int (elt);
14767
14768                   if (elt_size <= sizeof (HOST_WIDE_INT))
14769                     insert_int (double_int_to_shwi (val), elt_size, p);
14770                   else
14771                     {
14772                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14773                       insert_double (val, p);
14774                     }
14775                 }
14776               break;
14777
14778             case MODE_VECTOR_FLOAT:
14779               for (i = 0, p = array; i < length; i++, p += elt_size)
14780                 {
14781                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14782                   insert_float (elt, p);
14783                 }
14784               break;
14785
14786             default:
14787               gcc_unreachable ();
14788             }
14789
14790           loc_result = new_loc_descr (DW_OP_implicit_value,
14791                                       length * elt_size, 0);
14792           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14793           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14794           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14795           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14796         }
14797       break;
14798
14799     case CONST:
14800       if (mode == VOIDmode
14801           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14802           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14803           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14804         {
14805           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14806           break;
14807         }
14808       /* FALLTHROUGH */
14809     case SYMBOL_REF:
14810       if (!const_ok_for_output (rtl))
14811         break;
14812     case LABEL_REF:
14813       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14814           && (dwarf_version >= 4 || !dwarf_strict))
14815         {
14816           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14817           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14818           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14819           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14820           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14821         }
14822       break;
14823
14824     case DEBUG_IMPLICIT_PTR:
14825       loc_result = implicit_ptr_descriptor (rtl, 0);
14826       break;
14827
14828     case PLUS:
14829       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14830           && CONST_INT_P (XEXP (rtl, 1)))
14831         {
14832           loc_result
14833             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14834           break;
14835         }
14836       /* FALLTHRU */
14837     default:
14838       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14839           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14840           && (dwarf_version >= 4 || !dwarf_strict))
14841         {
14842           /* Value expression.  */
14843           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14844           if (loc_result)
14845             add_loc_descr (&loc_result,
14846                            new_loc_descr (DW_OP_stack_value, 0, 0));
14847         }
14848       break;
14849     }
14850
14851   return loc_result;
14852 }
14853
14854 /* We need to figure out what section we should use as the base for the
14855    address ranges where a given location is valid.
14856    1. If this particular DECL has a section associated with it, use that.
14857    2. If this function has a section associated with it, use that.
14858    3. Otherwise, use the text section.
14859    XXX: If you split a variable across multiple sections, we won't notice.  */
14860
14861 static const char *
14862 secname_for_decl (const_tree decl)
14863 {
14864   const char *secname;
14865
14866   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14867     {
14868       tree sectree = DECL_SECTION_NAME (decl);
14869       secname = TREE_STRING_POINTER (sectree);
14870     }
14871   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14872     {
14873       tree sectree = DECL_SECTION_NAME (current_function_decl);
14874       secname = TREE_STRING_POINTER (sectree);
14875     }
14876   else if (cfun && in_cold_section_p)
14877     secname = crtl->subsections.cold_section_label;
14878   else
14879     secname = text_section_label;
14880
14881   return secname;
14882 }
14883
14884 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14885
14886 static bool
14887 decl_by_reference_p (tree decl)
14888 {
14889   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14890            || TREE_CODE (decl) == VAR_DECL)
14891           && DECL_BY_REFERENCE (decl));
14892 }
14893
14894 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14895    for VARLOC.  */
14896
14897 static dw_loc_descr_ref
14898 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14899                enum var_init_status initialized)
14900 {
14901   int have_address = 0;
14902   dw_loc_descr_ref descr;
14903   enum machine_mode mode;
14904
14905   if (want_address != 2)
14906     {
14907       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14908       /* Single part.  */
14909       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14910         {
14911           varloc = PAT_VAR_LOCATION_LOC (varloc);
14912           if (GET_CODE (varloc) == EXPR_LIST)
14913             varloc = XEXP (varloc, 0);
14914           mode = GET_MODE (varloc);
14915           if (MEM_P (varloc))
14916             {
14917               rtx addr = XEXP (varloc, 0);
14918               descr = mem_loc_descriptor (addr, mode, initialized);
14919               if (descr)
14920                 have_address = 1;
14921               else
14922                 {
14923                   rtx x = avoid_constant_pool_reference (varloc);
14924                   if (x != varloc)
14925                     descr = mem_loc_descriptor (x, mode, initialized);
14926                 }
14927             }
14928           else
14929             descr = mem_loc_descriptor (varloc, mode, initialized);
14930         }
14931       else
14932         return 0;
14933     }
14934   else
14935     {
14936       if (GET_CODE (varloc) == VAR_LOCATION)
14937         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14938       else
14939         mode = DECL_MODE (loc);
14940       descr = loc_descriptor (varloc, mode, initialized);
14941       have_address = 1;
14942     }
14943
14944   if (!descr)
14945     return 0;
14946
14947   if (want_address == 2 && !have_address
14948       && (dwarf_version >= 4 || !dwarf_strict))
14949     {
14950       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14951         {
14952           expansion_failed (loc, NULL_RTX,
14953                             "DWARF address size mismatch");
14954           return 0;
14955         }
14956       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14957       have_address = 1;
14958     }
14959   /* Show if we can't fill the request for an address.  */
14960   if (want_address && !have_address)
14961     {
14962       expansion_failed (loc, NULL_RTX,
14963                         "Want address and only have value");
14964       return 0;
14965     }
14966
14967   /* If we've got an address and don't want one, dereference.  */
14968   if (!want_address && have_address)
14969     {
14970       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14971       enum dwarf_location_atom op;
14972
14973       if (size > DWARF2_ADDR_SIZE || size == -1)
14974         {
14975           expansion_failed (loc, NULL_RTX,
14976                             "DWARF address size mismatch");
14977           return 0;
14978         }
14979       else if (size == DWARF2_ADDR_SIZE)
14980         op = DW_OP_deref;
14981       else
14982         op = DW_OP_deref_size;
14983
14984       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14985     }
14986
14987   return descr;
14988 }
14989
14990 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14991    if it is not possible.  */
14992
14993 static dw_loc_descr_ref
14994 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14995 {
14996   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14997     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14998   else if (dwarf_version >= 3 || !dwarf_strict)
14999     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15000   else
15001     return NULL;
15002 }
15003
15004 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15005    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15006
15007 static dw_loc_descr_ref
15008 dw_sra_loc_expr (tree decl, rtx loc)
15009 {
15010   rtx p;
15011   unsigned int padsize = 0;
15012   dw_loc_descr_ref descr, *descr_tail;
15013   unsigned HOST_WIDE_INT decl_size;
15014   rtx varloc;
15015   enum var_init_status initialized;
15016
15017   if (DECL_SIZE (decl) == NULL
15018       || !host_integerp (DECL_SIZE (decl), 1))
15019     return NULL;
15020
15021   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15022   descr = NULL;
15023   descr_tail = &descr;
15024
15025   for (p = loc; p; p = XEXP (p, 1))
15026     {
15027       unsigned int bitsize = decl_piece_bitsize (p);
15028       rtx loc_note = *decl_piece_varloc_ptr (p);
15029       dw_loc_descr_ref cur_descr;
15030       dw_loc_descr_ref *tail, last = NULL;
15031       unsigned int opsize = 0;
15032
15033       if (loc_note == NULL_RTX
15034           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15035         {
15036           padsize += bitsize;
15037           continue;
15038         }
15039       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15040       varloc = NOTE_VAR_LOCATION (loc_note);
15041       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15042       if (cur_descr == NULL)
15043         {
15044           padsize += bitsize;
15045           continue;
15046         }
15047
15048       /* Check that cur_descr either doesn't use
15049          DW_OP_*piece operations, or their sum is equal
15050          to bitsize.  Otherwise we can't embed it.  */
15051       for (tail = &cur_descr; *tail != NULL;
15052            tail = &(*tail)->dw_loc_next)
15053         if ((*tail)->dw_loc_opc == DW_OP_piece)
15054           {
15055             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15056                       * BITS_PER_UNIT;
15057             last = *tail;
15058           }
15059         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15060           {
15061             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15062             last = *tail;
15063           }
15064
15065       if (last != NULL && opsize != bitsize)
15066         {
15067           padsize += bitsize;
15068           continue;
15069         }
15070
15071       /* If there is a hole, add DW_OP_*piece after empty DWARF
15072          expression, which means that those bits are optimized out.  */
15073       if (padsize)
15074         {
15075           if (padsize > decl_size)
15076             return NULL;
15077           decl_size -= padsize;
15078           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15079           if (*descr_tail == NULL)
15080             return NULL;
15081           descr_tail = &(*descr_tail)->dw_loc_next;
15082           padsize = 0;
15083         }
15084       *descr_tail = cur_descr;
15085       descr_tail = tail;
15086       if (bitsize > decl_size)
15087         return NULL;
15088       decl_size -= bitsize;
15089       if (last == NULL)
15090         {
15091           HOST_WIDE_INT offset = 0;
15092           if (GET_CODE (varloc) == VAR_LOCATION
15093               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15094             {
15095               varloc = PAT_VAR_LOCATION_LOC (varloc);
15096               if (GET_CODE (varloc) == EXPR_LIST)
15097                 varloc = XEXP (varloc, 0);
15098             }
15099           do 
15100             {
15101               if (GET_CODE (varloc) == CONST
15102                   || GET_CODE (varloc) == SIGN_EXTEND
15103                   || GET_CODE (varloc) == ZERO_EXTEND)
15104                 varloc = XEXP (varloc, 0);
15105               else if (GET_CODE (varloc) == SUBREG)
15106                 varloc = SUBREG_REG (varloc);
15107               else
15108                 break;
15109             }
15110           while (1);
15111           /* DW_OP_bit_size offset should be zero for register
15112              or implicit location descriptions and empty location
15113              descriptions, but for memory addresses needs big endian
15114              adjustment.  */
15115           if (MEM_P (varloc))
15116             {
15117               unsigned HOST_WIDE_INT memsize
15118                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15119               if (memsize != bitsize)
15120                 {
15121                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15122                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15123                     return NULL;
15124                   if (memsize < bitsize)
15125                     return NULL;
15126                   if (BITS_BIG_ENDIAN)
15127                     offset = memsize - bitsize;
15128                 }
15129             }
15130
15131           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15132           if (*descr_tail == NULL)
15133             return NULL;
15134           descr_tail = &(*descr_tail)->dw_loc_next;
15135         }
15136     }
15137
15138   /* If there were any non-empty expressions, add padding till the end of
15139      the decl.  */
15140   if (descr != NULL && decl_size != 0)
15141     {
15142       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15143       if (*descr_tail == NULL)
15144         return NULL;
15145     }
15146   return descr;
15147 }
15148
15149 /* Return the dwarf representation of the location list LOC_LIST of
15150    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15151    function.  */
15152
15153 static dw_loc_list_ref
15154 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15155 {
15156   const char *endname, *secname;
15157   rtx varloc;
15158   enum var_init_status initialized;
15159   struct var_loc_node *node;
15160   dw_loc_descr_ref descr;
15161   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15162   dw_loc_list_ref list = NULL;
15163   dw_loc_list_ref *listp = &list;
15164
15165   /* Now that we know what section we are using for a base,
15166      actually construct the list of locations.
15167      The first location information is what is passed to the
15168      function that creates the location list, and the remaining
15169      locations just get added on to that list.
15170      Note that we only know the start address for a location
15171      (IE location changes), so to build the range, we use
15172      the range [current location start, next location start].
15173      This means we have to special case the last node, and generate
15174      a range of [last location start, end of function label].  */
15175
15176   secname = secname_for_decl (decl);
15177
15178   for (node = loc_list->first; node; node = node->next)
15179     if (GET_CODE (node->loc) == EXPR_LIST
15180         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15181       {
15182         if (GET_CODE (node->loc) == EXPR_LIST)
15183           {
15184             /* This requires DW_OP_{,bit_}piece, which is not usable
15185                inside DWARF expressions.  */
15186             if (want_address != 2)
15187               continue;
15188             descr = dw_sra_loc_expr (decl, node->loc);
15189             if (descr == NULL)
15190               continue;
15191           }
15192         else
15193           {
15194             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15195             varloc = NOTE_VAR_LOCATION (node->loc);
15196             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15197           }
15198         if (descr)
15199           {
15200             /* The variable has a location between NODE->LABEL and
15201                NODE->NEXT->LABEL.  */
15202             if (node->next)
15203               endname = node->next->label;
15204             /* If the variable has a location at the last label
15205                it keeps its location until the end of function.  */
15206             else if (!current_function_decl)
15207               endname = text_end_label;
15208             else
15209               {
15210                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15211                                              current_function_funcdef_no);
15212                 endname = ggc_strdup (label_id);
15213               }
15214
15215             *listp = new_loc_list (descr, node->label, endname, secname);
15216             listp = &(*listp)->dw_loc_next;
15217           }
15218       }
15219
15220   /* Try to avoid the overhead of a location list emitting a location
15221      expression instead, but only if we didn't have more than one
15222      location entry in the first place.  If some entries were not
15223      representable, we don't want to pretend a single entry that was
15224      applies to the entire scope in which the variable is
15225      available.  */
15226   if (list && loc_list->first->next)
15227     gen_llsym (list);
15228
15229   return list;
15230 }
15231
15232 /* Return if the loc_list has only single element and thus can be represented
15233    as location description.   */
15234
15235 static bool
15236 single_element_loc_list_p (dw_loc_list_ref list)
15237 {
15238   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15239   return !list->ll_symbol;
15240 }
15241
15242 /* To each location in list LIST add loc descr REF.  */
15243
15244 static void
15245 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15246 {
15247   dw_loc_descr_ref copy;
15248   add_loc_descr (&list->expr, ref);
15249   list = list->dw_loc_next;
15250   while (list)
15251     {
15252       copy = ggc_alloc_dw_loc_descr_node ();
15253       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15254       add_loc_descr (&list->expr, copy);
15255       while (copy->dw_loc_next)
15256         {
15257           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15258           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15259           copy->dw_loc_next = new_copy;
15260           copy = new_copy;
15261         }
15262       list = list->dw_loc_next;
15263     }
15264 }
15265
15266 /* Given two lists RET and LIST
15267    produce location list that is result of adding expression in LIST
15268    to expression in RET on each possition in program.
15269    Might be destructive on both RET and LIST.
15270
15271    TODO: We handle only simple cases of RET or LIST having at most one
15272    element. General case would inolve sorting the lists in program order
15273    and merging them that will need some additional work.
15274    Adding that will improve quality of debug info especially for SRA-ed
15275    structures.  */
15276
15277 static void
15278 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15279 {
15280   if (!list)
15281     return;
15282   if (!*ret)
15283     {
15284       *ret = list;
15285       return;
15286     }
15287   if (!list->dw_loc_next)
15288     {
15289       add_loc_descr_to_each (*ret, list->expr);
15290       return;
15291     }
15292   if (!(*ret)->dw_loc_next)
15293     {
15294       add_loc_descr_to_each (list, (*ret)->expr);
15295       *ret = list;
15296       return;
15297     }
15298   expansion_failed (NULL_TREE, NULL_RTX,
15299                     "Don't know how to merge two non-trivial"
15300                     " location lists.\n");
15301   *ret = NULL;
15302   return;
15303 }
15304
15305 /* LOC is constant expression.  Try a luck, look it up in constant
15306    pool and return its loc_descr of its address.  */
15307
15308 static dw_loc_descr_ref
15309 cst_pool_loc_descr (tree loc)
15310 {
15311   /* Get an RTL for this, if something has been emitted.  */
15312   rtx rtl = lookup_constant_def (loc);
15313   enum machine_mode mode;
15314
15315   if (!rtl || !MEM_P (rtl))
15316     {
15317       gcc_assert (!rtl);
15318       return 0;
15319     }
15320   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15321
15322   /* TODO: We might get more coverage if we was actually delaying expansion
15323      of all expressions till end of compilation when constant pools are fully
15324      populated.  */
15325   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15326     {
15327       expansion_failed (loc, NULL_RTX,
15328                         "CST value in contant pool but not marked.");
15329       return 0;
15330     }
15331   mode = GET_MODE (rtl);
15332   rtl = XEXP (rtl, 0);
15333   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15334 }
15335
15336 /* Return dw_loc_list representing address of addr_expr LOC
15337    by looking for innder INDIRECT_REF expression and turing it
15338    into simple arithmetics.  */
15339
15340 static dw_loc_list_ref
15341 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15342 {
15343   tree obj, offset;
15344   HOST_WIDE_INT bitsize, bitpos, bytepos;
15345   enum machine_mode mode;
15346   int volatilep;
15347   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15348   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15349
15350   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15351                              &bitsize, &bitpos, &offset, &mode,
15352                              &unsignedp, &volatilep, false);
15353   STRIP_NOPS (obj);
15354   if (bitpos % BITS_PER_UNIT)
15355     {
15356       expansion_failed (loc, NULL_RTX, "bitfield access");
15357       return 0;
15358     }
15359   if (!INDIRECT_REF_P (obj))
15360     {
15361       expansion_failed (obj,
15362                         NULL_RTX, "no indirect ref in inner refrence");
15363       return 0;
15364     }
15365   if (!offset && !bitpos)
15366     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15367   else if (toplev
15368            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15369            && (dwarf_version >= 4 || !dwarf_strict))
15370     {
15371       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15372       if (!list_ret)
15373         return 0;
15374       if (offset)
15375         {
15376           /* Variable offset.  */
15377           list_ret1 = loc_list_from_tree (offset, 0);
15378           if (list_ret1 == 0)
15379             return 0;
15380           add_loc_list (&list_ret, list_ret1);
15381           if (!list_ret)
15382             return 0;
15383           add_loc_descr_to_each (list_ret,
15384                                  new_loc_descr (DW_OP_plus, 0, 0));
15385         }
15386       bytepos = bitpos / BITS_PER_UNIT;
15387       if (bytepos > 0)
15388         add_loc_descr_to_each (list_ret,
15389                                new_loc_descr (DW_OP_plus_uconst,
15390                                               bytepos, 0));
15391       else if (bytepos < 0)
15392         loc_list_plus_const (list_ret, bytepos);
15393       add_loc_descr_to_each (list_ret,
15394                              new_loc_descr (DW_OP_stack_value, 0, 0));
15395     }
15396   return list_ret;
15397 }
15398
15399
15400 /* Generate Dwarf location list representing LOC.
15401    If WANT_ADDRESS is false, expression computing LOC will be computed
15402    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15403    if WANT_ADDRESS is 2, expression computing address useable in location
15404      will be returned (i.e. DW_OP_reg can be used
15405      to refer to register values).  */
15406
15407 static dw_loc_list_ref
15408 loc_list_from_tree (tree loc, int want_address)
15409 {
15410   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15411   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15412   int have_address = 0;
15413   enum dwarf_location_atom op;
15414
15415   /* ??? Most of the time we do not take proper care for sign/zero
15416      extending the values properly.  Hopefully this won't be a real
15417      problem...  */
15418
15419   switch (TREE_CODE (loc))
15420     {
15421     case ERROR_MARK:
15422       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15423       return 0;
15424
15425     case PLACEHOLDER_EXPR:
15426       /* This case involves extracting fields from an object to determine the
15427          position of other fields.  We don't try to encode this here.  The
15428          only user of this is Ada, which encodes the needed information using
15429          the names of types.  */
15430       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15431       return 0;
15432
15433     case CALL_EXPR:
15434       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15435       /* There are no opcodes for these operations.  */
15436       return 0;
15437
15438     case PREINCREMENT_EXPR:
15439     case PREDECREMENT_EXPR:
15440     case POSTINCREMENT_EXPR:
15441     case POSTDECREMENT_EXPR:
15442       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15443       /* There are no opcodes for these operations.  */
15444       return 0;
15445
15446     case ADDR_EXPR:
15447       /* If we already want an address, see if there is INDIRECT_REF inside
15448          e.g. for &this->field.  */
15449       if (want_address)
15450         {
15451           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15452                        (loc, want_address == 2);
15453           if (list_ret)
15454             have_address = 1;
15455           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15456                    && (ret = cst_pool_loc_descr (loc)))
15457             have_address = 1;
15458         }
15459         /* Otherwise, process the argument and look for the address.  */
15460       if (!list_ret && !ret)
15461         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15462       else
15463         {
15464           if (want_address)
15465             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15466           return NULL;
15467         }
15468       break;
15469
15470     case VAR_DECL:
15471       if (DECL_THREAD_LOCAL_P (loc))
15472         {
15473           rtx rtl;
15474           enum dwarf_location_atom first_op;
15475           enum dwarf_location_atom second_op;
15476           bool dtprel = false;
15477
15478           if (targetm.have_tls)
15479             {
15480               /* If this is not defined, we have no way to emit the
15481                  data.  */
15482               if (!targetm.asm_out.output_dwarf_dtprel)
15483                 return 0;
15484
15485                /* The way DW_OP_GNU_push_tls_address is specified, we
15486                   can only look up addresses of objects in the current
15487                   module.  We used DW_OP_addr as first op, but that's
15488                   wrong, because DW_OP_addr is relocated by the debug
15489                   info consumer, while DW_OP_GNU_push_tls_address
15490                   operand shouldn't be.  */
15491               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15492                 return 0;
15493               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15494               dtprel = true;
15495               second_op = DW_OP_GNU_push_tls_address;
15496             }
15497           else
15498             {
15499               if (!targetm.emutls.debug_form_tls_address
15500                   || !(dwarf_version >= 3 || !dwarf_strict))
15501                 return 0;
15502               /* We stuffed the control variable into the DECL_VALUE_EXPR
15503                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15504                  no longer appear in gimple code.  We used the control
15505                  variable in specific so that we could pick it up here.  */
15506               loc = DECL_VALUE_EXPR (loc);
15507               first_op = DW_OP_addr;
15508               second_op = DW_OP_form_tls_address;
15509             }
15510
15511           rtl = rtl_for_decl_location (loc);
15512           if (rtl == NULL_RTX)
15513             return 0;
15514
15515           if (!MEM_P (rtl))
15516             return 0;
15517           rtl = XEXP (rtl, 0);
15518           if (! CONSTANT_P (rtl))
15519             return 0;
15520
15521           ret = new_loc_descr (first_op, 0, 0);
15522           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15523           ret->dw_loc_oprnd1.v.val_addr = rtl;
15524           ret->dtprel = dtprel;
15525
15526           ret1 = new_loc_descr (second_op, 0, 0);
15527           add_loc_descr (&ret, ret1);
15528
15529           have_address = 1;
15530           break;
15531         }
15532       /* FALLTHRU */
15533
15534     case PARM_DECL:
15535     case RESULT_DECL:
15536       if (DECL_HAS_VALUE_EXPR_P (loc))
15537         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15538                                    want_address);
15539       /* FALLTHRU */
15540
15541     case FUNCTION_DECL:
15542       {
15543         rtx rtl;
15544         var_loc_list *loc_list = lookup_decl_loc (loc);
15545
15546         if (loc_list && loc_list->first)
15547           {
15548             list_ret = dw_loc_list (loc_list, loc, want_address);
15549             have_address = want_address != 0;
15550             break;
15551           }
15552         rtl = rtl_for_decl_location (loc);
15553         if (rtl == NULL_RTX)
15554           {
15555             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15556             return 0;
15557           }
15558         else if (CONST_INT_P (rtl))
15559           {
15560             HOST_WIDE_INT val = INTVAL (rtl);
15561             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15562               val &= GET_MODE_MASK (DECL_MODE (loc));
15563             ret = int_loc_descriptor (val);
15564           }
15565         else if (GET_CODE (rtl) == CONST_STRING)
15566           {
15567             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15568             return 0;
15569           }
15570         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15571           {
15572             ret = new_loc_descr (DW_OP_addr, 0, 0);
15573             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15574             ret->dw_loc_oprnd1.v.val_addr = rtl;
15575           }
15576         else
15577           {
15578             enum machine_mode mode;
15579
15580             /* Certain constructs can only be represented at top-level.  */
15581             if (want_address == 2)
15582               {
15583                 ret = loc_descriptor (rtl, VOIDmode,
15584                                       VAR_INIT_STATUS_INITIALIZED);
15585                 have_address = 1;
15586               }
15587             else
15588               {
15589                 mode = GET_MODE (rtl);
15590                 if (MEM_P (rtl))
15591                   {
15592                     rtl = XEXP (rtl, 0);
15593                     have_address = 1;
15594                   }
15595                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15596               }
15597             if (!ret)
15598               expansion_failed (loc, rtl,
15599                                 "failed to produce loc descriptor for rtl");
15600           }
15601       }
15602       break;
15603
15604     case MEM_REF:
15605       /* ??? FIXME.  */
15606       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15607         return 0;
15608       /* Fallthru.  */
15609     case INDIRECT_REF:
15610       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15611       have_address = 1;
15612       break;
15613
15614     case COMPOUND_EXPR:
15615       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15616
15617     CASE_CONVERT:
15618     case VIEW_CONVERT_EXPR:
15619     case SAVE_EXPR:
15620     case MODIFY_EXPR:
15621       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15622
15623     case COMPONENT_REF:
15624     case BIT_FIELD_REF:
15625     case ARRAY_REF:
15626     case ARRAY_RANGE_REF:
15627     case REALPART_EXPR:
15628     case IMAGPART_EXPR:
15629       {
15630         tree obj, offset;
15631         HOST_WIDE_INT bitsize, bitpos, bytepos;
15632         enum machine_mode mode;
15633         int volatilep;
15634         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15635
15636         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15637                                    &unsignedp, &volatilep, false);
15638
15639         gcc_assert (obj != loc);
15640
15641         list_ret = loc_list_from_tree (obj,
15642                                        want_address == 2
15643                                        && !bitpos && !offset ? 2 : 1);
15644         /* TODO: We can extract value of the small expression via shifting even
15645            for nonzero bitpos.  */
15646         if (list_ret == 0)
15647           return 0;
15648         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15649           {
15650             expansion_failed (loc, NULL_RTX,
15651                               "bitfield access");
15652             return 0;
15653           }
15654
15655         if (offset != NULL_TREE)
15656           {
15657             /* Variable offset.  */
15658             list_ret1 = loc_list_from_tree (offset, 0);
15659             if (list_ret1 == 0)
15660               return 0;
15661             add_loc_list (&list_ret, list_ret1);
15662             if (!list_ret)
15663               return 0;
15664             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15665           }
15666
15667         bytepos = bitpos / BITS_PER_UNIT;
15668         if (bytepos > 0)
15669           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15670         else if (bytepos < 0)
15671           loc_list_plus_const (list_ret, bytepos);
15672
15673         have_address = 1;
15674         break;
15675       }
15676
15677     case INTEGER_CST:
15678       if ((want_address || !host_integerp (loc, 0))
15679           && (ret = cst_pool_loc_descr (loc)))
15680         have_address = 1;
15681       else if (want_address == 2
15682                && host_integerp (loc, 0)
15683                && (ret = address_of_int_loc_descriptor
15684                            (int_size_in_bytes (TREE_TYPE (loc)),
15685                             tree_low_cst (loc, 0))))
15686         have_address = 1;
15687       else if (host_integerp (loc, 0))
15688         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15689       else
15690         {
15691           expansion_failed (loc, NULL_RTX,
15692                             "Integer operand is not host integer");
15693           return 0;
15694         }
15695       break;
15696
15697     case CONSTRUCTOR:
15698     case REAL_CST:
15699     case STRING_CST:
15700     case COMPLEX_CST:
15701       if ((ret = cst_pool_loc_descr (loc)))
15702         have_address = 1;
15703       else
15704       /* We can construct small constants here using int_loc_descriptor.  */
15705         expansion_failed (loc, NULL_RTX,
15706                           "constructor or constant not in constant pool");
15707       break;
15708
15709     case TRUTH_AND_EXPR:
15710     case TRUTH_ANDIF_EXPR:
15711     case BIT_AND_EXPR:
15712       op = DW_OP_and;
15713       goto do_binop;
15714
15715     case TRUTH_XOR_EXPR:
15716     case BIT_XOR_EXPR:
15717       op = DW_OP_xor;
15718       goto do_binop;
15719
15720     case TRUTH_OR_EXPR:
15721     case TRUTH_ORIF_EXPR:
15722     case BIT_IOR_EXPR:
15723       op = DW_OP_or;
15724       goto do_binop;
15725
15726     case FLOOR_DIV_EXPR:
15727     case CEIL_DIV_EXPR:
15728     case ROUND_DIV_EXPR:
15729     case TRUNC_DIV_EXPR:
15730       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15731         return 0;
15732       op = DW_OP_div;
15733       goto do_binop;
15734
15735     case MINUS_EXPR:
15736       op = DW_OP_minus;
15737       goto do_binop;
15738
15739     case FLOOR_MOD_EXPR:
15740     case CEIL_MOD_EXPR:
15741     case ROUND_MOD_EXPR:
15742     case TRUNC_MOD_EXPR:
15743       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15744         {
15745           op = DW_OP_mod;
15746           goto do_binop;
15747         }
15748       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15749       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15750       if (list_ret == 0 || list_ret1 == 0)
15751         return 0;
15752
15753       add_loc_list (&list_ret, list_ret1);
15754       if (list_ret == 0)
15755         return 0;
15756       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15757       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15758       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15759       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15760       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15761       break;
15762
15763     case MULT_EXPR:
15764       op = DW_OP_mul;
15765       goto do_binop;
15766
15767     case LSHIFT_EXPR:
15768       op = DW_OP_shl;
15769       goto do_binop;
15770
15771     case RSHIFT_EXPR:
15772       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15773       goto do_binop;
15774
15775     case POINTER_PLUS_EXPR:
15776     case PLUS_EXPR:
15777       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15778         {
15779           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15780           if (list_ret == 0)
15781             return 0;
15782
15783           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15784           break;
15785         }
15786
15787       op = DW_OP_plus;
15788       goto do_binop;
15789
15790     case LE_EXPR:
15791       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15792         return 0;
15793
15794       op = DW_OP_le;
15795       goto do_binop;
15796
15797     case GE_EXPR:
15798       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15799         return 0;
15800
15801       op = DW_OP_ge;
15802       goto do_binop;
15803
15804     case LT_EXPR:
15805       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15806         return 0;
15807
15808       op = DW_OP_lt;
15809       goto do_binop;
15810
15811     case GT_EXPR:
15812       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15813         return 0;
15814
15815       op = DW_OP_gt;
15816       goto do_binop;
15817
15818     case EQ_EXPR:
15819       op = DW_OP_eq;
15820       goto do_binop;
15821
15822     case NE_EXPR:
15823       op = DW_OP_ne;
15824       goto do_binop;
15825
15826     do_binop:
15827       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15828       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15829       if (list_ret == 0 || list_ret1 == 0)
15830         return 0;
15831
15832       add_loc_list (&list_ret, list_ret1);
15833       if (list_ret == 0)
15834         return 0;
15835       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15836       break;
15837
15838     case TRUTH_NOT_EXPR:
15839     case BIT_NOT_EXPR:
15840       op = DW_OP_not;
15841       goto do_unop;
15842
15843     case ABS_EXPR:
15844       op = DW_OP_abs;
15845       goto do_unop;
15846
15847     case NEGATE_EXPR:
15848       op = DW_OP_neg;
15849       goto do_unop;
15850
15851     do_unop:
15852       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15853       if (list_ret == 0)
15854         return 0;
15855
15856       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15857       break;
15858
15859     case MIN_EXPR:
15860     case MAX_EXPR:
15861       {
15862         const enum tree_code code =
15863           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15864
15865         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15866                       build2 (code, integer_type_node,
15867                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15868                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15869       }
15870
15871       /* ... fall through ...  */
15872
15873     case COND_EXPR:
15874       {
15875         dw_loc_descr_ref lhs
15876           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15877         dw_loc_list_ref rhs
15878           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15879         dw_loc_descr_ref bra_node, jump_node, tmp;
15880
15881         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15882         if (list_ret == 0 || lhs == 0 || rhs == 0)
15883           return 0;
15884
15885         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15886         add_loc_descr_to_each (list_ret, bra_node);
15887
15888         add_loc_list (&list_ret, rhs);
15889         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15890         add_loc_descr_to_each (list_ret, jump_node);
15891
15892         add_loc_descr_to_each (list_ret, lhs);
15893         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15894         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15895
15896         /* ??? Need a node to point the skip at.  Use a nop.  */
15897         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15898         add_loc_descr_to_each (list_ret, tmp);
15899         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15900         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15901       }
15902       break;
15903
15904     case FIX_TRUNC_EXPR:
15905       return 0;
15906
15907     default:
15908       /* Leave front-end specific codes as simply unknown.  This comes
15909          up, for instance, with the C STMT_EXPR.  */
15910       if ((unsigned int) TREE_CODE (loc)
15911           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15912         {
15913           expansion_failed (loc, NULL_RTX,
15914                             "language specific tree node");
15915           return 0;
15916         }
15917
15918 #ifdef ENABLE_CHECKING
15919       /* Otherwise this is a generic code; we should just lists all of
15920          these explicitly.  We forgot one.  */
15921       gcc_unreachable ();
15922 #else
15923       /* In a release build, we want to degrade gracefully: better to
15924          generate incomplete debugging information than to crash.  */
15925       return NULL;
15926 #endif
15927     }
15928
15929   if (!ret && !list_ret)
15930     return 0;
15931
15932   if (want_address == 2 && !have_address
15933       && (dwarf_version >= 4 || !dwarf_strict))
15934     {
15935       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15936         {
15937           expansion_failed (loc, NULL_RTX,
15938                             "DWARF address size mismatch");
15939           return 0;
15940         }
15941       if (ret)
15942         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15943       else
15944         add_loc_descr_to_each (list_ret,
15945                                new_loc_descr (DW_OP_stack_value, 0, 0));
15946       have_address = 1;
15947     }
15948   /* Show if we can't fill the request for an address.  */
15949   if (want_address && !have_address)
15950     {
15951       expansion_failed (loc, NULL_RTX,
15952                         "Want address and only have value");
15953       return 0;
15954     }
15955
15956   gcc_assert (!ret || !list_ret);
15957
15958   /* If we've got an address and don't want one, dereference.  */
15959   if (!want_address && have_address)
15960     {
15961       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15962
15963       if (size > DWARF2_ADDR_SIZE || size == -1)
15964         {
15965           expansion_failed (loc, NULL_RTX,
15966                             "DWARF address size mismatch");
15967           return 0;
15968         }
15969       else if (size == DWARF2_ADDR_SIZE)
15970         op = DW_OP_deref;
15971       else
15972         op = DW_OP_deref_size;
15973
15974       if (ret)
15975         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15976       else
15977         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15978     }
15979   if (ret)
15980     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15981
15982   return list_ret;
15983 }
15984
15985 /* Same as above but return only single location expression.  */
15986 static dw_loc_descr_ref
15987 loc_descriptor_from_tree (tree loc, int want_address)
15988 {
15989   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15990   if (!ret)
15991     return NULL;
15992   if (ret->dw_loc_next)
15993     {
15994       expansion_failed (loc, NULL_RTX,
15995                         "Location list where only loc descriptor needed");
15996       return NULL;
15997     }
15998   return ret->expr;
15999 }
16000
16001 /* Given a value, round it up to the lowest multiple of `boundary'
16002    which is not less than the value itself.  */
16003
16004 static inline HOST_WIDE_INT
16005 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16006 {
16007   return (((value + boundary - 1) / boundary) * boundary);
16008 }
16009
16010 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16011    pointer to the declared type for the relevant field variable, or return
16012    `integer_type_node' if the given node turns out to be an
16013    ERROR_MARK node.  */
16014
16015 static inline tree
16016 field_type (const_tree decl)
16017 {
16018   tree type;
16019
16020   if (TREE_CODE (decl) == ERROR_MARK)
16021     return integer_type_node;
16022
16023   type = DECL_BIT_FIELD_TYPE (decl);
16024   if (type == NULL_TREE)
16025     type = TREE_TYPE (decl);
16026
16027   return type;
16028 }
16029
16030 /* Given a pointer to a tree node, return the alignment in bits for
16031    it, or else return BITS_PER_WORD if the node actually turns out to
16032    be an ERROR_MARK node.  */
16033
16034 static inline unsigned
16035 simple_type_align_in_bits (const_tree type)
16036 {
16037   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16038 }
16039
16040 static inline unsigned
16041 simple_decl_align_in_bits (const_tree decl)
16042 {
16043   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16044 }
16045
16046 /* Return the result of rounding T up to ALIGN.  */
16047
16048 static inline double_int
16049 round_up_to_align (double_int t, unsigned int align)
16050 {
16051   double_int alignd = uhwi_to_double_int (align);
16052   t = double_int_add (t, alignd);
16053   t = double_int_add (t, double_int_minus_one);
16054   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16055   t = double_int_mul (t, alignd);
16056   return t;
16057 }
16058
16059 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16060    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16061    or return 0 if we are unable to determine what that offset is, either
16062    because the argument turns out to be a pointer to an ERROR_MARK node, or
16063    because the offset is actually variable.  (We can't handle the latter case
16064    just yet).  */
16065
16066 static HOST_WIDE_INT
16067 field_byte_offset (const_tree decl)
16068 {
16069   double_int object_offset_in_bits;
16070   double_int object_offset_in_bytes;
16071   double_int bitpos_int;
16072
16073   if (TREE_CODE (decl) == ERROR_MARK)
16074     return 0;
16075
16076   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16077
16078   /* We cannot yet cope with fields whose positions are variable, so
16079      for now, when we see such things, we simply return 0.  Someday, we may
16080      be able to handle such cases, but it will be damn difficult.  */
16081   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16082     return 0;
16083
16084   bitpos_int = tree_to_double_int (bit_position (decl));
16085
16086 #ifdef PCC_BITFIELD_TYPE_MATTERS
16087   if (PCC_BITFIELD_TYPE_MATTERS)
16088     {
16089       tree type;
16090       tree field_size_tree;
16091       double_int deepest_bitpos;
16092       double_int field_size_in_bits;
16093       unsigned int type_align_in_bits;
16094       unsigned int decl_align_in_bits;
16095       double_int type_size_in_bits;
16096
16097       type = field_type (decl);
16098       type_size_in_bits = double_int_type_size_in_bits (type);
16099       type_align_in_bits = simple_type_align_in_bits (type);
16100
16101       field_size_tree = DECL_SIZE (decl);
16102
16103       /* The size could be unspecified if there was an error, or for
16104          a flexible array member.  */
16105       if (!field_size_tree)
16106         field_size_tree = bitsize_zero_node;
16107
16108       /* If the size of the field is not constant, use the type size.  */
16109       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16110         field_size_in_bits = tree_to_double_int (field_size_tree);
16111       else
16112         field_size_in_bits = type_size_in_bits;
16113
16114       decl_align_in_bits = simple_decl_align_in_bits (decl);
16115
16116       /* The GCC front-end doesn't make any attempt to keep track of the
16117          starting bit offset (relative to the start of the containing
16118          structure type) of the hypothetical "containing object" for a
16119          bit-field.  Thus, when computing the byte offset value for the
16120          start of the "containing object" of a bit-field, we must deduce
16121          this information on our own. This can be rather tricky to do in
16122          some cases.  For example, handling the following structure type
16123          definition when compiling for an i386/i486 target (which only
16124          aligns long long's to 32-bit boundaries) can be very tricky:
16125
16126          struct S { int field1; long long field2:31; };
16127
16128          Fortunately, there is a simple rule-of-thumb which can be used
16129          in such cases.  When compiling for an i386/i486, GCC will
16130          allocate 8 bytes for the structure shown above.  It decides to
16131          do this based upon one simple rule for bit-field allocation.
16132          GCC allocates each "containing object" for each bit-field at
16133          the first (i.e. lowest addressed) legitimate alignment boundary
16134          (based upon the required minimum alignment for the declared
16135          type of the field) which it can possibly use, subject to the
16136          condition that there is still enough available space remaining
16137          in the containing object (when allocated at the selected point)
16138          to fully accommodate all of the bits of the bit-field itself.
16139
16140          This simple rule makes it obvious why GCC allocates 8 bytes for
16141          each object of the structure type shown above.  When looking
16142          for a place to allocate the "containing object" for `field2',
16143          the compiler simply tries to allocate a 64-bit "containing
16144          object" at each successive 32-bit boundary (starting at zero)
16145          until it finds a place to allocate that 64- bit field such that
16146          at least 31 contiguous (and previously unallocated) bits remain
16147          within that selected 64 bit field.  (As it turns out, for the
16148          example above, the compiler finds it is OK to allocate the
16149          "containing object" 64-bit field at bit-offset zero within the
16150          structure type.)
16151
16152          Here we attempt to work backwards from the limited set of facts
16153          we're given, and we try to deduce from those facts, where GCC
16154          must have believed that the containing object started (within
16155          the structure type). The value we deduce is then used (by the
16156          callers of this routine) to generate DW_AT_location and
16157          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16158          the case of DW_AT_location, regular fields as well).  */
16159
16160       /* Figure out the bit-distance from the start of the structure to
16161          the "deepest" bit of the bit-field.  */
16162       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16163
16164       /* This is the tricky part.  Use some fancy footwork to deduce
16165          where the lowest addressed bit of the containing object must
16166          be.  */
16167       object_offset_in_bits
16168         = double_int_sub (deepest_bitpos, type_size_in_bits);
16169
16170       /* Round up to type_align by default.  This works best for
16171          bitfields.  */
16172       object_offset_in_bits
16173         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16174
16175       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16176         {
16177           object_offset_in_bits
16178             = double_int_sub (deepest_bitpos, type_size_in_bits);
16179
16180           /* Round up to decl_align instead.  */
16181           object_offset_in_bits
16182             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16183         }
16184     }
16185   else
16186 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16187     object_offset_in_bits = bitpos_int;
16188
16189   object_offset_in_bytes
16190     = double_int_div (object_offset_in_bits,
16191                       uhwi_to_double_int (BITS_PER_UNIT), true,
16192                       TRUNC_DIV_EXPR);
16193   return double_int_to_shwi (object_offset_in_bytes);
16194 }
16195 \f
16196 /* The following routines define various Dwarf attributes and any data
16197    associated with them.  */
16198
16199 /* Add a location description attribute value to a DIE.
16200
16201    This emits location attributes suitable for whole variables and
16202    whole parameters.  Note that the location attributes for struct fields are
16203    generated by the routine `data_member_location_attribute' below.  */
16204
16205 static inline void
16206 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16207                              dw_loc_list_ref descr)
16208 {
16209   if (descr == 0)
16210     return;
16211   if (single_element_loc_list_p (descr))
16212     add_AT_loc (die, attr_kind, descr->expr);
16213   else
16214     add_AT_loc_list (die, attr_kind, descr);
16215 }
16216
16217 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16218
16219 static void
16220 add_accessibility_attribute (dw_die_ref die, tree decl)
16221 {
16222   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16223      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16224      the default has always been DW_ACCESS_public.  */
16225   if (TREE_PROTECTED (decl))
16226     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16227   else if (TREE_PRIVATE (decl))
16228     {
16229       if (dwarf_version == 2
16230           || die->die_parent == NULL
16231           || die->die_parent->die_tag != DW_TAG_class_type)
16232         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16233     }
16234   else if (dwarf_version > 2
16235            && die->die_parent
16236            && die->die_parent->die_tag == DW_TAG_class_type)
16237     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16238 }
16239
16240 /* Attach the specialized form of location attribute used for data members of
16241    struct and union types.  In the special case of a FIELD_DECL node which
16242    represents a bit-field, the "offset" part of this special location
16243    descriptor must indicate the distance in bytes from the lowest-addressed
16244    byte of the containing struct or union type to the lowest-addressed byte of
16245    the "containing object" for the bit-field.  (See the `field_byte_offset'
16246    function above).
16247
16248    For any given bit-field, the "containing object" is a hypothetical object
16249    (of some integral or enum type) within which the given bit-field lives.  The
16250    type of this hypothetical "containing object" is always the same as the
16251    declared type of the individual bit-field itself (for GCC anyway... the
16252    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16253    bytes) of the hypothetical "containing object" which will be given in the
16254    DW_AT_byte_size attribute for this bit-field.  (See the
16255    `byte_size_attribute' function below.)  It is also used when calculating the
16256    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16257    function below.)  */
16258
16259 static void
16260 add_data_member_location_attribute (dw_die_ref die, tree decl)
16261 {
16262   HOST_WIDE_INT offset;
16263   dw_loc_descr_ref loc_descr = 0;
16264
16265   if (TREE_CODE (decl) == TREE_BINFO)
16266     {
16267       /* We're working on the TAG_inheritance for a base class.  */
16268       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16269         {
16270           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16271              aren't at a fixed offset from all (sub)objects of the same
16272              type.  We need to extract the appropriate offset from our
16273              vtable.  The following dwarf expression means
16274
16275                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16276
16277              This is specific to the V3 ABI, of course.  */
16278
16279           dw_loc_descr_ref tmp;
16280
16281           /* Make a copy of the object address.  */
16282           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16283           add_loc_descr (&loc_descr, tmp);
16284
16285           /* Extract the vtable address.  */
16286           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16287           add_loc_descr (&loc_descr, tmp);
16288
16289           /* Calculate the address of the offset.  */
16290           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16291           gcc_assert (offset < 0);
16292
16293           tmp = int_loc_descriptor (-offset);
16294           add_loc_descr (&loc_descr, tmp);
16295           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16296           add_loc_descr (&loc_descr, tmp);
16297
16298           /* Extract the offset.  */
16299           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16300           add_loc_descr (&loc_descr, tmp);
16301
16302           /* Add it to the object address.  */
16303           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16304           add_loc_descr (&loc_descr, tmp);
16305         }
16306       else
16307         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16308     }
16309   else
16310     offset = field_byte_offset (decl);
16311
16312   if (! loc_descr)
16313     {
16314       if (dwarf_version > 2)
16315         {
16316           /* Don't need to output a location expression, just the constant. */
16317           if (offset < 0)
16318             add_AT_int (die, DW_AT_data_member_location, offset);
16319           else
16320             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16321           return;
16322         }
16323       else
16324         {
16325           enum dwarf_location_atom op;
16326
16327           /* The DWARF2 standard says that we should assume that the structure
16328              address is already on the stack, so we can specify a structure
16329              field address by using DW_OP_plus_uconst.  */
16330
16331 #ifdef MIPS_DEBUGGING_INFO
16332           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16333              operator correctly.  It works only if we leave the offset on the
16334              stack.  */
16335           op = DW_OP_constu;
16336 #else
16337           op = DW_OP_plus_uconst;
16338 #endif
16339
16340           loc_descr = new_loc_descr (op, offset, 0);
16341         }
16342     }
16343
16344   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16345 }
16346
16347 /* Writes integer values to dw_vec_const array.  */
16348
16349 static void
16350 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16351 {
16352   while (size != 0)
16353     {
16354       *dest++ = val & 0xff;
16355       val >>= 8;
16356       --size;
16357     }
16358 }
16359
16360 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16361
16362 static HOST_WIDE_INT
16363 extract_int (const unsigned char *src, unsigned int size)
16364 {
16365   HOST_WIDE_INT val = 0;
16366
16367   src += size;
16368   while (size != 0)
16369     {
16370       val <<= 8;
16371       val |= *--src & 0xff;
16372       --size;
16373     }
16374   return val;
16375 }
16376
16377 /* Writes double_int values to dw_vec_const array.  */
16378
16379 static void
16380 insert_double (double_int val, unsigned char *dest)
16381 {
16382   unsigned char *p0 = dest;
16383   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16384
16385   if (WORDS_BIG_ENDIAN)
16386     {
16387       p0 = p1;
16388       p1 = dest;
16389     }
16390
16391   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16392   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16393 }
16394
16395 /* Writes floating point values to dw_vec_const array.  */
16396
16397 static void
16398 insert_float (const_rtx rtl, unsigned char *array)
16399 {
16400   REAL_VALUE_TYPE rv;
16401   long val[4];
16402   int i;
16403
16404   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16405   real_to_target (val, &rv, GET_MODE (rtl));
16406
16407   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16408   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16409     {
16410       insert_int (val[i], 4, array);
16411       array += 4;
16412     }
16413 }
16414
16415 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16416    does not have a "location" either in memory or in a register.  These
16417    things can arise in GNU C when a constant is passed as an actual parameter
16418    to an inlined function.  They can also arise in C++ where declared
16419    constants do not necessarily get memory "homes".  */
16420
16421 static bool
16422 add_const_value_attribute (dw_die_ref die, rtx rtl)
16423 {
16424   switch (GET_CODE (rtl))
16425     {
16426     case CONST_INT:
16427       {
16428         HOST_WIDE_INT val = INTVAL (rtl);
16429
16430         if (val < 0)
16431           add_AT_int (die, DW_AT_const_value, val);
16432         else
16433           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16434       }
16435       return true;
16436
16437     case CONST_DOUBLE:
16438       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16439          floating-point constant.  A CONST_DOUBLE is used whenever the
16440          constant requires more than one word in order to be adequately
16441          represented.  */
16442       {
16443         enum machine_mode mode = GET_MODE (rtl);
16444
16445         if (SCALAR_FLOAT_MODE_P (mode))
16446           {
16447             unsigned int length = GET_MODE_SIZE (mode);
16448             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16449
16450             insert_float (rtl, array);
16451             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16452           }
16453         else
16454           add_AT_double (die, DW_AT_const_value,
16455                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16456       }
16457       return true;
16458
16459     case CONST_VECTOR:
16460       {
16461         enum machine_mode mode = GET_MODE (rtl);
16462         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16463         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16464         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16465           (length * elt_size);
16466         unsigned int i;
16467         unsigned char *p;
16468
16469         switch (GET_MODE_CLASS (mode))
16470           {
16471           case MODE_VECTOR_INT:
16472             for (i = 0, p = array; i < length; i++, p += elt_size)
16473               {
16474                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16475                 double_int val = rtx_to_double_int (elt);
16476
16477                 if (elt_size <= sizeof (HOST_WIDE_INT))
16478                   insert_int (double_int_to_shwi (val), elt_size, p);
16479                 else
16480                   {
16481                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16482                     insert_double (val, p);
16483                   }
16484               }
16485             break;
16486
16487           case MODE_VECTOR_FLOAT:
16488             for (i = 0, p = array; i < length; i++, p += elt_size)
16489               {
16490                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16491                 insert_float (elt, p);
16492               }
16493             break;
16494
16495           default:
16496             gcc_unreachable ();
16497           }
16498
16499         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16500       }
16501       return true;
16502
16503     case CONST_STRING:
16504       if (dwarf_version >= 4 || !dwarf_strict)
16505         {
16506           dw_loc_descr_ref loc_result;
16507           resolve_one_addr (&rtl, NULL);
16508         rtl_addr:
16509           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16510           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16511           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16512           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16513           add_AT_loc (die, DW_AT_location, loc_result);
16514           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16515           return true;
16516         }
16517       return false;
16518
16519     case CONST:
16520       if (CONSTANT_P (XEXP (rtl, 0)))
16521         return add_const_value_attribute (die, XEXP (rtl, 0));
16522       /* FALLTHROUGH */
16523     case SYMBOL_REF:
16524       if (!const_ok_for_output (rtl))
16525         return false;
16526     case LABEL_REF:
16527       if (dwarf_version >= 4 || !dwarf_strict)
16528         goto rtl_addr;
16529       return false;
16530
16531     case PLUS:
16532       /* In cases where an inlined instance of an inline function is passed
16533          the address of an `auto' variable (which is local to the caller) we
16534          can get a situation where the DECL_RTL of the artificial local
16535          variable (for the inlining) which acts as a stand-in for the
16536          corresponding formal parameter (of the inline function) will look
16537          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16538          exactly a compile-time constant expression, but it isn't the address
16539          of the (artificial) local variable either.  Rather, it represents the
16540          *value* which the artificial local variable always has during its
16541          lifetime.  We currently have no way to represent such quasi-constant
16542          values in Dwarf, so for now we just punt and generate nothing.  */
16543       return false;
16544
16545     case HIGH:
16546     case CONST_FIXED:
16547       return false;
16548
16549     case MEM:
16550       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16551           && MEM_READONLY_P (rtl)
16552           && GET_MODE (rtl) == BLKmode)
16553         {
16554           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16555           return true;
16556         }
16557       return false;
16558
16559     default:
16560       /* No other kinds of rtx should be possible here.  */
16561       gcc_unreachable ();
16562     }
16563   return false;
16564 }
16565
16566 /* Determine whether the evaluation of EXPR references any variables
16567    or functions which aren't otherwise used (and therefore may not be
16568    output).  */
16569 static tree
16570 reference_to_unused (tree * tp, int * walk_subtrees,
16571                      void * data ATTRIBUTE_UNUSED)
16572 {
16573   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16574     *walk_subtrees = 0;
16575
16576   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16577       && ! TREE_ASM_WRITTEN (*tp))
16578     return *tp;
16579   /* ???  The C++ FE emits debug information for using decls, so
16580      putting gcc_unreachable here falls over.  See PR31899.  For now
16581      be conservative.  */
16582   else if (!cgraph_global_info_ready
16583            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16584     return *tp;
16585   else if (TREE_CODE (*tp) == VAR_DECL)
16586     {
16587       struct varpool_node *node = varpool_get_node (*tp);
16588       if (!node || !node->needed)
16589         return *tp;
16590     }
16591   else if (TREE_CODE (*tp) == FUNCTION_DECL
16592            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16593     {
16594       /* The call graph machinery must have finished analyzing,
16595          optimizing and gimplifying the CU by now.
16596          So if *TP has no call graph node associated
16597          to it, it means *TP will not be emitted.  */
16598       if (!cgraph_get_node (*tp))
16599         return *tp;
16600     }
16601   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16602     return *tp;
16603
16604   return NULL_TREE;
16605 }
16606
16607 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16608    for use in a later add_const_value_attribute call.  */
16609
16610 static rtx
16611 rtl_for_decl_init (tree init, tree type)
16612 {
16613   rtx rtl = NULL_RTX;
16614
16615   STRIP_NOPS (init);
16616
16617   /* If a variable is initialized with a string constant without embedded
16618      zeros, build CONST_STRING.  */
16619   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16620     {
16621       tree enttype = TREE_TYPE (type);
16622       tree domain = TYPE_DOMAIN (type);
16623       enum machine_mode mode = TYPE_MODE (enttype);
16624
16625       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16626           && domain
16627           && integer_zerop (TYPE_MIN_VALUE (domain))
16628           && compare_tree_int (TYPE_MAX_VALUE (domain),
16629                                TREE_STRING_LENGTH (init) - 1) == 0
16630           && ((size_t) TREE_STRING_LENGTH (init)
16631               == strlen (TREE_STRING_POINTER (init)) + 1))
16632         {
16633           rtl = gen_rtx_CONST_STRING (VOIDmode,
16634                                       ggc_strdup (TREE_STRING_POINTER (init)));
16635           rtl = gen_rtx_MEM (BLKmode, rtl);
16636           MEM_READONLY_P (rtl) = 1;
16637         }
16638     }
16639   /* Other aggregates, and complex values, could be represented using
16640      CONCAT: FIXME!  */
16641   else if (AGGREGATE_TYPE_P (type)
16642            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16643                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16644            || TREE_CODE (type) == COMPLEX_TYPE)
16645     ;
16646   /* Vectors only work if their mode is supported by the target.
16647      FIXME: generic vectors ought to work too.  */
16648   else if (TREE_CODE (type) == VECTOR_TYPE
16649            && !VECTOR_MODE_P (TYPE_MODE (type)))
16650     ;
16651   /* If the initializer is something that we know will expand into an
16652      immediate RTL constant, expand it now.  We must be careful not to
16653      reference variables which won't be output.  */
16654   else if (initializer_constant_valid_p (init, type)
16655            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16656     {
16657       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16658          possible.  */
16659       if (TREE_CODE (type) == VECTOR_TYPE)
16660         switch (TREE_CODE (init))
16661           {
16662           case VECTOR_CST:
16663             break;
16664           case CONSTRUCTOR:
16665             if (TREE_CONSTANT (init))
16666               {
16667                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16668                 bool constant_p = true;
16669                 tree value;
16670                 unsigned HOST_WIDE_INT ix;
16671
16672                 /* Even when ctor is constant, it might contain non-*_CST
16673                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16674                    belong into VECTOR_CST nodes.  */
16675                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16676                   if (!CONSTANT_CLASS_P (value))
16677                     {
16678                       constant_p = false;
16679                       break;
16680                     }
16681
16682                 if (constant_p)
16683                   {
16684                     init = build_vector_from_ctor (type, elts);
16685                     break;
16686                   }
16687               }
16688             /* FALLTHRU */
16689
16690           default:
16691             return NULL;
16692           }
16693
16694       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16695
16696       /* If expand_expr returns a MEM, it wasn't immediate.  */
16697       gcc_assert (!rtl || !MEM_P (rtl));
16698     }
16699
16700   return rtl;
16701 }
16702
16703 /* Generate RTL for the variable DECL to represent its location.  */
16704
16705 static rtx
16706 rtl_for_decl_location (tree decl)
16707 {
16708   rtx rtl;
16709
16710   /* Here we have to decide where we are going to say the parameter "lives"
16711      (as far as the debugger is concerned).  We only have a couple of
16712      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16713
16714      DECL_RTL normally indicates where the parameter lives during most of the
16715      activation of the function.  If optimization is enabled however, this
16716      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16717      that the parameter doesn't really live anywhere (as far as the code
16718      generation parts of GCC are concerned) during most of the function's
16719      activation.  That will happen (for example) if the parameter is never
16720      referenced within the function.
16721
16722      We could just generate a location descriptor here for all non-NULL
16723      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16724      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16725      where DECL_RTL is NULL or is a pseudo-reg.
16726
16727      Note however that we can only get away with using DECL_INCOMING_RTL as
16728      a backup substitute for DECL_RTL in certain limited cases.  In cases
16729      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16730      we can be sure that the parameter was passed using the same type as it is
16731      declared to have within the function, and that its DECL_INCOMING_RTL
16732      points us to a place where a value of that type is passed.
16733
16734      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16735      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16736      because in these cases DECL_INCOMING_RTL points us to a value of some
16737      type which is *different* from the type of the parameter itself.  Thus,
16738      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16739      such cases, the debugger would end up (for example) trying to fetch a
16740      `float' from a place which actually contains the first part of a
16741      `double'.  That would lead to really incorrect and confusing
16742      output at debug-time.
16743
16744      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16745      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16746      are a couple of exceptions however.  On little-endian machines we can
16747      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16748      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16749      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16750      when (on a little-endian machine) a non-prototyped function has a
16751      parameter declared to be of type `short' or `char'.  In such cases,
16752      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16753      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16754      passed `int' value.  If the debugger then uses that address to fetch
16755      a `short' or a `char' (on a little-endian machine) the result will be
16756      the correct data, so we allow for such exceptional cases below.
16757
16758      Note that our goal here is to describe the place where the given formal
16759      parameter lives during most of the function's activation (i.e. between the
16760      end of the prologue and the start of the epilogue).  We'll do that as best
16761      as we can. Note however that if the given formal parameter is modified
16762      sometime during the execution of the function, then a stack backtrace (at
16763      debug-time) will show the function as having been called with the *new*
16764      value rather than the value which was originally passed in.  This happens
16765      rarely enough that it is not a major problem, but it *is* a problem, and
16766      I'd like to fix it.
16767
16768      A future version of dwarf2out.c may generate two additional attributes for
16769      any given DW_TAG_formal_parameter DIE which will describe the "passed
16770      type" and the "passed location" for the given formal parameter in addition
16771      to the attributes we now generate to indicate the "declared type" and the
16772      "active location" for each parameter.  This additional set of attributes
16773      could be used by debuggers for stack backtraces. Separately, note that
16774      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16775      This happens (for example) for inlined-instances of inline function formal
16776      parameters which are never referenced.  This really shouldn't be
16777      happening.  All PARM_DECL nodes should get valid non-NULL
16778      DECL_INCOMING_RTL values.  FIXME.  */
16779
16780   /* Use DECL_RTL as the "location" unless we find something better.  */
16781   rtl = DECL_RTL_IF_SET (decl);
16782
16783   /* When generating abstract instances, ignore everything except
16784      constants, symbols living in memory, and symbols living in
16785      fixed registers.  */
16786   if (! reload_completed)
16787     {
16788       if (rtl
16789           && (CONSTANT_P (rtl)
16790               || (MEM_P (rtl)
16791                   && CONSTANT_P (XEXP (rtl, 0)))
16792               || (REG_P (rtl)
16793                   && TREE_CODE (decl) == VAR_DECL
16794                   && TREE_STATIC (decl))))
16795         {
16796           rtl = targetm.delegitimize_address (rtl);
16797           return rtl;
16798         }
16799       rtl = NULL_RTX;
16800     }
16801   else if (TREE_CODE (decl) == PARM_DECL)
16802     {
16803       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16804         {
16805           tree declared_type = TREE_TYPE (decl);
16806           tree passed_type = DECL_ARG_TYPE (decl);
16807           enum machine_mode dmode = TYPE_MODE (declared_type);
16808           enum machine_mode pmode = TYPE_MODE (passed_type);
16809
16810           /* This decl represents a formal parameter which was optimized out.
16811              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16812              all cases where (rtl == NULL_RTX) just below.  */
16813           if (dmode == pmode)
16814             rtl = DECL_INCOMING_RTL (decl);
16815           else if (SCALAR_INT_MODE_P (dmode)
16816                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16817                    && DECL_INCOMING_RTL (decl))
16818             {
16819               rtx inc = DECL_INCOMING_RTL (decl);
16820               if (REG_P (inc))
16821                 rtl = inc;
16822               else if (MEM_P (inc))
16823                 {
16824                   if (BYTES_BIG_ENDIAN)
16825                     rtl = adjust_address_nv (inc, dmode,
16826                                              GET_MODE_SIZE (pmode)
16827                                              - GET_MODE_SIZE (dmode));
16828                   else
16829                     rtl = inc;
16830                 }
16831             }
16832         }
16833
16834       /* If the parm was passed in registers, but lives on the stack, then
16835          make a big endian correction if the mode of the type of the
16836          parameter is not the same as the mode of the rtl.  */
16837       /* ??? This is the same series of checks that are made in dbxout.c before
16838          we reach the big endian correction code there.  It isn't clear if all
16839          of these checks are necessary here, but keeping them all is the safe
16840          thing to do.  */
16841       else if (MEM_P (rtl)
16842                && XEXP (rtl, 0) != const0_rtx
16843                && ! CONSTANT_P (XEXP (rtl, 0))
16844                /* Not passed in memory.  */
16845                && !MEM_P (DECL_INCOMING_RTL (decl))
16846                /* Not passed by invisible reference.  */
16847                && (!REG_P (XEXP (rtl, 0))
16848                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16849                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16850 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16851                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16852 #endif
16853                      )
16854                /* Big endian correction check.  */
16855                && BYTES_BIG_ENDIAN
16856                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16857                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16858                    < UNITS_PER_WORD))
16859         {
16860           int offset = (UNITS_PER_WORD
16861                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16862
16863           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16864                              plus_constant (XEXP (rtl, 0), offset));
16865         }
16866     }
16867   else if (TREE_CODE (decl) == VAR_DECL
16868            && rtl
16869            && MEM_P (rtl)
16870            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16871            && BYTES_BIG_ENDIAN)
16872     {
16873       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16874       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16875
16876       /* If a variable is declared "register" yet is smaller than
16877          a register, then if we store the variable to memory, it
16878          looks like we're storing a register-sized value, when in
16879          fact we are not.  We need to adjust the offset of the
16880          storage location to reflect the actual value's bytes,
16881          else gdb will not be able to display it.  */
16882       if (rsize > dsize)
16883         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16884                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16885     }
16886
16887   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16888      and will have been substituted directly into all expressions that use it.
16889      C does not have such a concept, but C++ and other languages do.  */
16890   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16891     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16892
16893   if (rtl)
16894     rtl = targetm.delegitimize_address (rtl);
16895
16896   /* If we don't look past the constant pool, we risk emitting a
16897      reference to a constant pool entry that isn't referenced from
16898      code, and thus is not emitted.  */
16899   if (rtl)
16900     rtl = avoid_constant_pool_reference (rtl);
16901
16902   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16903      in the current CU, resolve_addr will remove the expression referencing
16904      it.  */
16905   if (rtl == NULL_RTX
16906       && TREE_CODE (decl) == VAR_DECL
16907       && !DECL_EXTERNAL (decl)
16908       && TREE_STATIC (decl)
16909       && DECL_NAME (decl)
16910       && !DECL_HARD_REGISTER (decl)
16911       && DECL_MODE (decl) != VOIDmode)
16912     {
16913       rtl = make_decl_rtl_for_debug (decl);
16914       if (!MEM_P (rtl)
16915           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16916           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16917         rtl = NULL_RTX;
16918     }
16919
16920   return rtl;
16921 }
16922
16923 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16924    returned.  If so, the decl for the COMMON block is returned, and the
16925    value is the offset into the common block for the symbol.  */
16926
16927 static tree
16928 fortran_common (tree decl, HOST_WIDE_INT *value)
16929 {
16930   tree val_expr, cvar;
16931   enum machine_mode mode;
16932   HOST_WIDE_INT bitsize, bitpos;
16933   tree offset;
16934   int volatilep = 0, unsignedp = 0;
16935
16936   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16937      it does not have a value (the offset into the common area), or if it
16938      is thread local (as opposed to global) then it isn't common, and shouldn't
16939      be handled as such.  */
16940   if (TREE_CODE (decl) != VAR_DECL
16941       || !TREE_STATIC (decl)
16942       || !DECL_HAS_VALUE_EXPR_P (decl)
16943       || !is_fortran ())
16944     return NULL_TREE;
16945
16946   val_expr = DECL_VALUE_EXPR (decl);
16947   if (TREE_CODE (val_expr) != COMPONENT_REF)
16948     return NULL_TREE;
16949
16950   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16951                               &mode, &unsignedp, &volatilep, true);
16952
16953   if (cvar == NULL_TREE
16954       || TREE_CODE (cvar) != VAR_DECL
16955       || DECL_ARTIFICIAL (cvar)
16956       || !TREE_PUBLIC (cvar))
16957     return NULL_TREE;
16958
16959   *value = 0;
16960   if (offset != NULL)
16961     {
16962       if (!host_integerp (offset, 0))
16963         return NULL_TREE;
16964       *value = tree_low_cst (offset, 0);
16965     }
16966   if (bitpos != 0)
16967     *value += bitpos / BITS_PER_UNIT;
16968
16969   return cvar;
16970 }
16971
16972 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16973    data attribute for a variable or a parameter.  We generate the
16974    DW_AT_const_value attribute only in those cases where the given variable
16975    or parameter does not have a true "location" either in memory or in a
16976    register.  This can happen (for example) when a constant is passed as an
16977    actual argument in a call to an inline function.  (It's possible that
16978    these things can crop up in other ways also.)  Note that one type of
16979    constant value which can be passed into an inlined function is a constant
16980    pointer.  This can happen for example if an actual argument in an inlined
16981    function call evaluates to a compile-time constant address.  */
16982
16983 static bool
16984 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16985                                        enum dwarf_attribute attr)
16986 {
16987   rtx rtl;
16988   dw_loc_list_ref list;
16989   var_loc_list *loc_list;
16990
16991   if (TREE_CODE (decl) == ERROR_MARK)
16992     return false;
16993
16994   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16995               || TREE_CODE (decl) == RESULT_DECL);
16996
16997   /* Try to get some constant RTL for this decl, and use that as the value of
16998      the location.  */
16999
17000   rtl = rtl_for_decl_location (decl);
17001   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17002       && add_const_value_attribute (die, rtl))
17003     return true;
17004
17005   /* See if we have single element location list that is equivalent to
17006      a constant value.  That way we are better to use add_const_value_attribute
17007      rather than expanding constant value equivalent.  */
17008   loc_list = lookup_decl_loc (decl);
17009   if (loc_list
17010       && loc_list->first
17011       && loc_list->first->next == NULL
17012       && NOTE_P (loc_list->first->loc)
17013       && NOTE_VAR_LOCATION (loc_list->first->loc)
17014       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17015     {
17016       struct var_loc_node *node;
17017
17018       node = loc_list->first;
17019       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17020       if (GET_CODE (rtl) == EXPR_LIST)
17021         rtl = XEXP (rtl, 0);
17022       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17023           && add_const_value_attribute (die, rtl))
17024          return true;
17025     }
17026   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17027   if (list)
17028     {
17029       add_AT_location_description (die, attr, list);
17030       return true;
17031     }
17032   /* None of that worked, so it must not really have a location;
17033      try adding a constant value attribute from the DECL_INITIAL.  */
17034   return tree_add_const_value_attribute_for_decl (die, decl);
17035 }
17036
17037 /* Add VARIABLE and DIE into deferred locations list.  */
17038
17039 static void
17040 defer_location (tree variable, dw_die_ref die)
17041 {
17042   deferred_locations entry;
17043   entry.variable = variable;
17044   entry.die = die;
17045   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17046 }
17047
17048 /* Helper function for tree_add_const_value_attribute.  Natively encode
17049    initializer INIT into an array.  Return true if successful.  */
17050
17051 static bool
17052 native_encode_initializer (tree init, unsigned char *array, int size)
17053 {
17054   tree type;
17055
17056   if (init == NULL_TREE)
17057     return false;
17058
17059   STRIP_NOPS (init);
17060   switch (TREE_CODE (init))
17061     {
17062     case STRING_CST:
17063       type = TREE_TYPE (init);
17064       if (TREE_CODE (type) == ARRAY_TYPE)
17065         {
17066           tree enttype = TREE_TYPE (type);
17067           enum machine_mode mode = TYPE_MODE (enttype);
17068
17069           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17070             return false;
17071           if (int_size_in_bytes (type) != size)
17072             return false;
17073           if (size > TREE_STRING_LENGTH (init))
17074             {
17075               memcpy (array, TREE_STRING_POINTER (init),
17076                       TREE_STRING_LENGTH (init));
17077               memset (array + TREE_STRING_LENGTH (init),
17078                       '\0', size - TREE_STRING_LENGTH (init));
17079             }
17080           else
17081             memcpy (array, TREE_STRING_POINTER (init), size);
17082           return true;
17083         }
17084       return false;
17085     case CONSTRUCTOR:
17086       type = TREE_TYPE (init);
17087       if (int_size_in_bytes (type) != size)
17088         return false;
17089       if (TREE_CODE (type) == ARRAY_TYPE)
17090         {
17091           HOST_WIDE_INT min_index;
17092           unsigned HOST_WIDE_INT cnt;
17093           int curpos = 0, fieldsize;
17094           constructor_elt *ce;
17095
17096           if (TYPE_DOMAIN (type) == NULL_TREE
17097               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17098             return false;
17099
17100           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17101           if (fieldsize <= 0)
17102             return false;
17103
17104           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17105           memset (array, '\0', size);
17106           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17107             {
17108               tree val = ce->value;
17109               tree index = ce->index;
17110               int pos = curpos;
17111               if (index && TREE_CODE (index) == RANGE_EXPR)
17112                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17113                       * fieldsize;
17114               else if (index)
17115                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17116
17117               if (val)
17118                 {
17119                   STRIP_NOPS (val);
17120                   if (!native_encode_initializer (val, array + pos, fieldsize))
17121                     return false;
17122                 }
17123               curpos = pos + fieldsize;
17124               if (index && TREE_CODE (index) == RANGE_EXPR)
17125                 {
17126                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17127                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17128                   while (count > 0)
17129                     {
17130                       if (val)
17131                         memcpy (array + curpos, array + pos, fieldsize);
17132                       curpos += fieldsize;
17133                     }
17134                 }
17135               gcc_assert (curpos <= size);
17136             }
17137           return true;
17138         }
17139       else if (TREE_CODE (type) == RECORD_TYPE
17140                || TREE_CODE (type) == UNION_TYPE)
17141         {
17142           tree field = NULL_TREE;
17143           unsigned HOST_WIDE_INT cnt;
17144           constructor_elt *ce;
17145
17146           if (int_size_in_bytes (type) != size)
17147             return false;
17148
17149           if (TREE_CODE (type) == RECORD_TYPE)
17150             field = TYPE_FIELDS (type);
17151
17152           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17153             {
17154               tree val = ce->value;
17155               int pos, fieldsize;
17156
17157               if (ce->index != 0)
17158                 field = ce->index;
17159
17160               if (val)
17161                 STRIP_NOPS (val);
17162
17163               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17164                 return false;
17165
17166               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17167                   && TYPE_DOMAIN (TREE_TYPE (field))
17168                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17169                 return false;
17170               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17171                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17172                 return false;
17173               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17174               pos = int_byte_position (field);
17175               gcc_assert (pos + fieldsize <= size);
17176               if (val
17177                   && !native_encode_initializer (val, array + pos, fieldsize))
17178                 return false;
17179             }
17180           return true;
17181         }
17182       return false;
17183     case VIEW_CONVERT_EXPR:
17184     case NON_LVALUE_EXPR:
17185       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17186     default:
17187       return native_encode_expr (init, array, size) == size;
17188     }
17189 }
17190
17191 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17192    attribute is the const value T.  */
17193
17194 static bool
17195 tree_add_const_value_attribute (dw_die_ref die, tree t)
17196 {
17197   tree init;
17198   tree type = TREE_TYPE (t);
17199   rtx rtl;
17200
17201   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17202     return false;
17203
17204   init = t;
17205   gcc_assert (!DECL_P (init));
17206
17207   rtl = rtl_for_decl_init (init, type);
17208   if (rtl)
17209     return add_const_value_attribute (die, rtl);
17210   /* If the host and target are sane, try harder.  */
17211   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17212            && initializer_constant_valid_p (init, type))
17213     {
17214       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17215       if (size > 0 && (int) size == size)
17216         {
17217           unsigned char *array = (unsigned char *)
17218             ggc_alloc_cleared_atomic (size);
17219
17220           if (native_encode_initializer (init, array, size))
17221             {
17222               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17223               return true;
17224             }
17225         }
17226     }
17227   return false;
17228 }
17229
17230 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17231    attribute is the const value of T, where T is an integral constant
17232    variable with static storage duration
17233    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17234
17235 static bool
17236 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17237 {
17238
17239   if (!decl
17240       || (TREE_CODE (decl) != VAR_DECL
17241           && TREE_CODE (decl) != CONST_DECL))
17242     return false;
17243
17244     if (TREE_READONLY (decl)
17245         && ! TREE_THIS_VOLATILE (decl)
17246         && DECL_INITIAL (decl))
17247       /* OK */;
17248     else
17249       return false;
17250
17251   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17252   if (get_AT (var_die, DW_AT_const_value))
17253     return false;
17254
17255   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17256 }
17257
17258 /* Convert the CFI instructions for the current function into a
17259    location list.  This is used for DW_AT_frame_base when we targeting
17260    a dwarf2 consumer that does not support the dwarf3
17261    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17262    expressions.  */
17263
17264 static dw_loc_list_ref
17265 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17266 {
17267   dw_fde_ref fde;
17268   dw_loc_list_ref list, *list_tail;
17269   dw_cfi_ref cfi;
17270   dw_cfa_location last_cfa, next_cfa;
17271   const char *start_label, *last_label, *section;
17272   dw_cfa_location remember;
17273
17274   fde = current_fde ();
17275   gcc_assert (fde != NULL);
17276
17277   section = secname_for_decl (current_function_decl);
17278   list_tail = &list;
17279   list = NULL;
17280
17281   memset (&next_cfa, 0, sizeof (next_cfa));
17282   next_cfa.reg = INVALID_REGNUM;
17283   remember = next_cfa;
17284
17285   start_label = fde->dw_fde_begin;
17286
17287   /* ??? Bald assumption that the CIE opcode list does not contain
17288      advance opcodes.  */
17289   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17290     lookup_cfa_1 (cfi, &next_cfa, &remember);
17291
17292   last_cfa = next_cfa;
17293   last_label = start_label;
17294
17295   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17296     switch (cfi->dw_cfi_opc)
17297       {
17298       case DW_CFA_set_loc:
17299       case DW_CFA_advance_loc1:
17300       case DW_CFA_advance_loc2:
17301       case DW_CFA_advance_loc4:
17302         if (!cfa_equal_p (&last_cfa, &next_cfa))
17303           {
17304             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17305                                        start_label, last_label, section);
17306
17307             list_tail = &(*list_tail)->dw_loc_next;
17308             last_cfa = next_cfa;
17309             start_label = last_label;
17310           }
17311         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17312         break;
17313
17314       case DW_CFA_advance_loc:
17315         /* The encoding is complex enough that we should never emit this.  */
17316         gcc_unreachable ();
17317
17318       default:
17319         lookup_cfa_1 (cfi, &next_cfa, &remember);
17320         break;
17321       }
17322
17323   if (!cfa_equal_p (&last_cfa, &next_cfa))
17324     {
17325       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17326                                  start_label, last_label, section);
17327       list_tail = &(*list_tail)->dw_loc_next;
17328       start_label = last_label;
17329     }
17330
17331   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17332                              start_label, fde->dw_fde_end, section);
17333
17334   if (list && list->dw_loc_next)
17335     gen_llsym (list);
17336
17337   return list;
17338 }
17339
17340 /* Compute a displacement from the "steady-state frame pointer" to the
17341    frame base (often the same as the CFA), and store it in
17342    frame_pointer_fb_offset.  OFFSET is added to the displacement
17343    before the latter is negated.  */
17344
17345 static void
17346 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17347 {
17348   rtx reg, elim;
17349
17350 #ifdef FRAME_POINTER_CFA_OFFSET
17351   reg = frame_pointer_rtx;
17352   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17353 #else
17354   reg = arg_pointer_rtx;
17355   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17356 #endif
17357
17358   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17359   if (GET_CODE (elim) == PLUS)
17360     {
17361       offset += INTVAL (XEXP (elim, 1));
17362       elim = XEXP (elim, 0);
17363     }
17364
17365   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17366                && (elim == hard_frame_pointer_rtx
17367                    || elim == stack_pointer_rtx))
17368               || elim == (frame_pointer_needed
17369                           ? hard_frame_pointer_rtx
17370                           : stack_pointer_rtx));
17371
17372   frame_pointer_fb_offset = -offset;
17373 }
17374
17375 /* Generate a DW_AT_name attribute given some string value to be included as
17376    the value of the attribute.  */
17377
17378 static void
17379 add_name_attribute (dw_die_ref die, const char *name_string)
17380 {
17381   if (name_string != NULL && *name_string != 0)
17382     {
17383       if (demangle_name_func)
17384         name_string = (*demangle_name_func) (name_string);
17385
17386       add_AT_string (die, DW_AT_name, name_string);
17387     }
17388 }
17389
17390 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17391
17392 static void
17393 add_comp_dir_attribute (dw_die_ref die)
17394 {
17395   const char *wd = get_src_pwd ();
17396   char *wd1;
17397
17398   if (wd == NULL)
17399     return;
17400
17401   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17402     {
17403       int wdlen;
17404
17405       wdlen = strlen (wd);
17406       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17407       strcpy (wd1, wd);
17408       wd1 [wdlen] = DIR_SEPARATOR;
17409       wd1 [wdlen + 1] = 0;
17410       wd = wd1;
17411     }
17412
17413     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17414 }
17415
17416 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17417    default.  */
17418
17419 static int
17420 lower_bound_default (void)
17421 {
17422   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17423     {
17424     case DW_LANG_C:
17425     case DW_LANG_C89:
17426     case DW_LANG_C99:
17427     case DW_LANG_C_plus_plus:
17428     case DW_LANG_ObjC:
17429     case DW_LANG_ObjC_plus_plus:
17430     case DW_LANG_Java:
17431       return 0;
17432     case DW_LANG_Fortran77:
17433     case DW_LANG_Fortran90:
17434     case DW_LANG_Fortran95:
17435       return 1;
17436     case DW_LANG_UPC:
17437     case DW_LANG_D:
17438     case DW_LANG_Python:
17439       return dwarf_version >= 4 ? 0 : -1;
17440     case DW_LANG_Ada95:
17441     case DW_LANG_Ada83:
17442     case DW_LANG_Cobol74:
17443     case DW_LANG_Cobol85:
17444     case DW_LANG_Pascal83:
17445     case DW_LANG_Modula2:
17446     case DW_LANG_PLI:
17447       return dwarf_version >= 4 ? 1 : -1;
17448     default:
17449       return -1;
17450     }
17451 }
17452
17453 /* Given a tree node describing an array bound (either lower or upper) output
17454    a representation for that bound.  */
17455
17456 static void
17457 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17458 {
17459   switch (TREE_CODE (bound))
17460     {
17461     case ERROR_MARK:
17462       return;
17463
17464     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17465     case INTEGER_CST:
17466       {
17467         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17468         int dflt;
17469
17470         /* Use the default if possible.  */
17471         if (bound_attr == DW_AT_lower_bound
17472             && host_integerp (bound, 0)
17473             && (dflt = lower_bound_default ()) != -1
17474             && tree_low_cst (bound, 0) == dflt)
17475           ;
17476
17477         /* Otherwise represent the bound as an unsigned value with the
17478            precision of its type.  The precision and signedness of the
17479            type will be necessary to re-interpret it unambiguously.  */
17480         else if (prec < HOST_BITS_PER_WIDE_INT)
17481           {
17482             unsigned HOST_WIDE_INT mask
17483               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17484             add_AT_unsigned (subrange_die, bound_attr,
17485                              TREE_INT_CST_LOW (bound) & mask);
17486           }
17487         else if (prec == HOST_BITS_PER_WIDE_INT
17488                  || TREE_INT_CST_HIGH (bound) == 0)
17489           add_AT_unsigned (subrange_die, bound_attr,
17490                            TREE_INT_CST_LOW (bound));
17491         else
17492           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17493                          TREE_INT_CST_LOW (bound));
17494       }
17495       break;
17496
17497     CASE_CONVERT:
17498     case VIEW_CONVERT_EXPR:
17499       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17500       break;
17501
17502     case SAVE_EXPR:
17503       break;
17504
17505     case VAR_DECL:
17506     case PARM_DECL:
17507     case RESULT_DECL:
17508       {
17509         dw_die_ref decl_die = lookup_decl_die (bound);
17510
17511         /* ??? Can this happen, or should the variable have been bound
17512            first?  Probably it can, since I imagine that we try to create
17513            the types of parameters in the order in which they exist in
17514            the list, and won't have created a forward reference to a
17515            later parameter.  */
17516         if (decl_die != NULL)
17517           {
17518             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17519             break;
17520           }
17521       }
17522       /* FALLTHRU */
17523
17524     default:
17525       {
17526         /* Otherwise try to create a stack operation procedure to
17527            evaluate the value of the array bound.  */
17528
17529         dw_die_ref ctx, decl_die;
17530         dw_loc_list_ref list;
17531
17532         list = loc_list_from_tree (bound, 2);
17533         if (list == NULL || single_element_loc_list_p (list))
17534           {
17535             /* If DW_AT_*bound is not a reference nor constant, it is
17536                a DWARF expression rather than location description.
17537                For that loc_list_from_tree (bound, 0) is needed.
17538                If that fails to give a single element list,
17539                fall back to outputting this as a reference anyway.  */
17540             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17541             if (list2 && single_element_loc_list_p (list2))
17542               {
17543                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17544                 break;
17545               }
17546           }
17547         if (list == NULL)
17548           break;
17549
17550         if (current_function_decl == 0)
17551           ctx = comp_unit_die ();
17552         else
17553           ctx = lookup_decl_die (current_function_decl);
17554
17555         decl_die = new_die (DW_TAG_variable, ctx, bound);
17556         add_AT_flag (decl_die, DW_AT_artificial, 1);
17557         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17558         add_AT_location_description (decl_die, DW_AT_location, list);
17559         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17560         break;
17561       }
17562     }
17563 }
17564
17565 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17566    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17567    Note that the block of subscript information for an array type also
17568    includes information about the element type of the given array type.  */
17569
17570 static void
17571 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17572 {
17573   unsigned dimension_number;
17574   tree lower, upper;
17575   dw_die_ref subrange_die;
17576
17577   for (dimension_number = 0;
17578        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17579        type = TREE_TYPE (type), dimension_number++)
17580     {
17581       tree domain = TYPE_DOMAIN (type);
17582
17583       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17584         break;
17585
17586       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17587          and (in GNU C only) variable bounds.  Handle all three forms
17588          here.  */
17589       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17590       if (domain)
17591         {
17592           /* We have an array type with specified bounds.  */
17593           lower = TYPE_MIN_VALUE (domain);
17594           upper = TYPE_MAX_VALUE (domain);
17595
17596           /* Define the index type.  */
17597           if (TREE_TYPE (domain))
17598             {
17599               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17600                  TREE_TYPE field.  We can't emit debug info for this
17601                  because it is an unnamed integral type.  */
17602               if (TREE_CODE (domain) == INTEGER_TYPE
17603                   && TYPE_NAME (domain) == NULL_TREE
17604                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17605                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17606                 ;
17607               else
17608                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17609                                     type_die);
17610             }
17611
17612           /* ??? If upper is NULL, the array has unspecified length,
17613              but it does have a lower bound.  This happens with Fortran
17614                dimension arr(N:*)
17615              Since the debugger is definitely going to need to know N
17616              to produce useful results, go ahead and output the lower
17617              bound solo, and hope the debugger can cope.  */
17618
17619           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17620           if (upper)
17621             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17622         }
17623
17624       /* Otherwise we have an array type with an unspecified length.  The
17625          DWARF-2 spec does not say how to handle this; let's just leave out the
17626          bounds.  */
17627     }
17628 }
17629
17630 static void
17631 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17632 {
17633   unsigned size;
17634
17635   switch (TREE_CODE (tree_node))
17636     {
17637     case ERROR_MARK:
17638       size = 0;
17639       break;
17640     case ENUMERAL_TYPE:
17641     case RECORD_TYPE:
17642     case UNION_TYPE:
17643     case QUAL_UNION_TYPE:
17644       size = int_size_in_bytes (tree_node);
17645       break;
17646     case FIELD_DECL:
17647       /* For a data member of a struct or union, the DW_AT_byte_size is
17648          generally given as the number of bytes normally allocated for an
17649          object of the *declared* type of the member itself.  This is true
17650          even for bit-fields.  */
17651       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17652       break;
17653     default:
17654       gcc_unreachable ();
17655     }
17656
17657   /* Note that `size' might be -1 when we get to this point.  If it is, that
17658      indicates that the byte size of the entity in question is variable.  We
17659      have no good way of expressing this fact in Dwarf at the present time,
17660      so just let the -1 pass on through.  */
17661   add_AT_unsigned (die, DW_AT_byte_size, size);
17662 }
17663
17664 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17665    which specifies the distance in bits from the highest order bit of the
17666    "containing object" for the bit-field to the highest order bit of the
17667    bit-field itself.
17668
17669    For any given bit-field, the "containing object" is a hypothetical object
17670    (of some integral or enum type) within which the given bit-field lives.  The
17671    type of this hypothetical "containing object" is always the same as the
17672    declared type of the individual bit-field itself.  The determination of the
17673    exact location of the "containing object" for a bit-field is rather
17674    complicated.  It's handled by the `field_byte_offset' function (above).
17675
17676    Note that it is the size (in bytes) of the hypothetical "containing object"
17677    which will be given in the DW_AT_byte_size attribute for this bit-field.
17678    (See `byte_size_attribute' above).  */
17679
17680 static inline void
17681 add_bit_offset_attribute (dw_die_ref die, tree decl)
17682 {
17683   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17684   tree type = DECL_BIT_FIELD_TYPE (decl);
17685   HOST_WIDE_INT bitpos_int;
17686   HOST_WIDE_INT highest_order_object_bit_offset;
17687   HOST_WIDE_INT highest_order_field_bit_offset;
17688   HOST_WIDE_INT unsigned bit_offset;
17689
17690   /* Must be a field and a bit field.  */
17691   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17692
17693   /* We can't yet handle bit-fields whose offsets are variable, so if we
17694      encounter such things, just return without generating any attribute
17695      whatsoever.  Likewise for variable or too large size.  */
17696   if (! host_integerp (bit_position (decl), 0)
17697       || ! host_integerp (DECL_SIZE (decl), 1))
17698     return;
17699
17700   bitpos_int = int_bit_position (decl);
17701
17702   /* Note that the bit offset is always the distance (in bits) from the
17703      highest-order bit of the "containing object" to the highest-order bit of
17704      the bit-field itself.  Since the "high-order end" of any object or field
17705      is different on big-endian and little-endian machines, the computation
17706      below must take account of these differences.  */
17707   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17708   highest_order_field_bit_offset = bitpos_int;
17709
17710   if (! BYTES_BIG_ENDIAN)
17711     {
17712       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17713       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17714     }
17715
17716   bit_offset
17717     = (! BYTES_BIG_ENDIAN
17718        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17719        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17720
17721   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17722 }
17723
17724 /* For a FIELD_DECL node which represents a bit field, output an attribute
17725    which specifies the length in bits of the given field.  */
17726
17727 static inline void
17728 add_bit_size_attribute (dw_die_ref die, tree decl)
17729 {
17730   /* Must be a field and a bit field.  */
17731   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17732               && DECL_BIT_FIELD_TYPE (decl));
17733
17734   if (host_integerp (DECL_SIZE (decl), 1))
17735     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17736 }
17737
17738 /* If the compiled language is ANSI C, then add a 'prototyped'
17739    attribute, if arg types are given for the parameters of a function.  */
17740
17741 static inline void
17742 add_prototyped_attribute (dw_die_ref die, tree func_type)
17743 {
17744   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17745       && prototype_p (func_type))
17746     add_AT_flag (die, DW_AT_prototyped, 1);
17747 }
17748
17749 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17750    by looking in either the type declaration or object declaration
17751    equate table.  */
17752
17753 static inline dw_die_ref
17754 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17755 {
17756   dw_die_ref origin_die = NULL;
17757
17758   if (TREE_CODE (origin) != FUNCTION_DECL)
17759     {
17760       /* We may have gotten separated from the block for the inlined
17761          function, if we're in an exception handler or some such; make
17762          sure that the abstract function has been written out.
17763
17764          Doing this for nested functions is wrong, however; functions are
17765          distinct units, and our context might not even be inline.  */
17766       tree fn = origin;
17767
17768       if (TYPE_P (fn))
17769         fn = TYPE_STUB_DECL (fn);
17770
17771       fn = decl_function_context (fn);
17772       if (fn)
17773         dwarf2out_abstract_function (fn);
17774     }
17775
17776   if (DECL_P (origin))
17777     origin_die = lookup_decl_die (origin);
17778   else if (TYPE_P (origin))
17779     origin_die = lookup_type_die (origin);
17780
17781   /* XXX: Functions that are never lowered don't always have correct block
17782      trees (in the case of java, they simply have no block tree, in some other
17783      languages).  For these functions, there is nothing we can really do to
17784      output correct debug info for inlined functions in all cases.  Rather
17785      than die, we'll just produce deficient debug info now, in that we will
17786      have variables without a proper abstract origin.  In the future, when all
17787      functions are lowered, we should re-add a gcc_assert (origin_die)
17788      here.  */
17789
17790   if (origin_die)
17791     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17792   return origin_die;
17793 }
17794
17795 /* We do not currently support the pure_virtual attribute.  */
17796
17797 static inline void
17798 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17799 {
17800   if (DECL_VINDEX (func_decl))
17801     {
17802       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17803
17804       if (host_integerp (DECL_VINDEX (func_decl), 0))
17805         add_AT_loc (die, DW_AT_vtable_elem_location,
17806                     new_loc_descr (DW_OP_constu,
17807                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17808                                    0));
17809
17810       /* GNU extension: Record what type this method came from originally.  */
17811       if (debug_info_level > DINFO_LEVEL_TERSE
17812           && DECL_CONTEXT (func_decl))
17813         add_AT_die_ref (die, DW_AT_containing_type,
17814                         lookup_type_die (DECL_CONTEXT (func_decl)));
17815     }
17816 }
17817 \f
17818 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17819    given decl.  This used to be a vendor extension until after DWARF 4
17820    standardized it.  */
17821
17822 static void
17823 add_linkage_attr (dw_die_ref die, tree decl)
17824 {
17825   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17826
17827   /* Mimic what assemble_name_raw does with a leading '*'.  */
17828   if (name[0] == '*')
17829     name = &name[1];
17830
17831   if (dwarf_version >= 4)
17832     add_AT_string (die, DW_AT_linkage_name, name);
17833   else
17834     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17835 }
17836
17837 /* Add source coordinate attributes for the given decl.  */
17838
17839 static void
17840 add_src_coords_attributes (dw_die_ref die, tree decl)
17841 {
17842   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17843
17844   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17845   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17846 }
17847
17848 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17849
17850 static void
17851 add_linkage_name (dw_die_ref die, tree decl)
17852 {
17853   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17854        && TREE_PUBLIC (decl)
17855        && !DECL_ABSTRACT (decl)
17856        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17857        && die->die_tag != DW_TAG_member)
17858     {
17859       /* Defer until we have an assembler name set.  */
17860       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17861         {
17862           limbo_die_node *asm_name;
17863
17864           asm_name = ggc_alloc_cleared_limbo_die_node ();
17865           asm_name->die = die;
17866           asm_name->created_for = decl;
17867           asm_name->next = deferred_asm_name;
17868           deferred_asm_name = asm_name;
17869         }
17870       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17871         add_linkage_attr (die, decl);
17872     }
17873 }
17874
17875 /* Add a DW_AT_name attribute and source coordinate attribute for the
17876    given decl, but only if it actually has a name.  */
17877
17878 static void
17879 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17880 {
17881   tree decl_name;
17882
17883   decl_name = DECL_NAME (decl);
17884   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17885     {
17886       const char *name = dwarf2_name (decl, 0);
17887       if (name)
17888         add_name_attribute (die, name);
17889       if (! DECL_ARTIFICIAL (decl))
17890         add_src_coords_attributes (die, decl);
17891
17892       add_linkage_name (die, decl);
17893     }
17894
17895 #ifdef VMS_DEBUGGING_INFO
17896   /* Get the function's name, as described by its RTL.  This may be different
17897      from the DECL_NAME name used in the source file.  */
17898   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17899     {
17900       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17901                    XEXP (DECL_RTL (decl), 0));
17902       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17903     }
17904 #endif /* VMS_DEBUGGING_INFO */
17905 }
17906
17907 #ifdef VMS_DEBUGGING_INFO
17908 /* Output the debug main pointer die for VMS */
17909
17910 void
17911 dwarf2out_vms_debug_main_pointer (void)
17912 {
17913   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17914   dw_die_ref die;
17915
17916   /* Allocate the VMS debug main subprogram die.  */
17917   die = ggc_alloc_cleared_die_node ();
17918   die->die_tag = DW_TAG_subprogram;
17919   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17920   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17921                                current_function_funcdef_no);
17922   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17923
17924   /* Make it the first child of comp_unit_die ().  */
17925   die->die_parent = comp_unit_die ();
17926   if (comp_unit_die ()->die_child)
17927     {
17928       die->die_sib = comp_unit_die ()->die_child->die_sib;
17929       comp_unit_die ()->die_child->die_sib = die;
17930     }
17931   else
17932     {
17933       die->die_sib = die;
17934       comp_unit_die ()->die_child = die;
17935     }
17936 }
17937 #endif /* VMS_DEBUGGING_INFO */
17938
17939 /* Push a new declaration scope.  */
17940
17941 static void
17942 push_decl_scope (tree scope)
17943 {
17944   VEC_safe_push (tree, gc, decl_scope_table, scope);
17945 }
17946
17947 /* Pop a declaration scope.  */
17948
17949 static inline void
17950 pop_decl_scope (void)
17951 {
17952   VEC_pop (tree, decl_scope_table);
17953 }
17954
17955 /* Return the DIE for the scope that immediately contains this type.
17956    Non-named types get global scope.  Named types nested in other
17957    types get their containing scope if it's open, or global scope
17958    otherwise.  All other types (i.e. function-local named types) get
17959    the current active scope.  */
17960
17961 static dw_die_ref
17962 scope_die_for (tree t, dw_die_ref context_die)
17963 {
17964   dw_die_ref scope_die = NULL;
17965   tree containing_scope;
17966   int i;
17967
17968   /* Non-types always go in the current scope.  */
17969   gcc_assert (TYPE_P (t));
17970
17971   containing_scope = TYPE_CONTEXT (t);
17972
17973   /* Use the containing namespace if it was passed in (for a declaration).  */
17974   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17975     {
17976       if (context_die == lookup_decl_die (containing_scope))
17977         /* OK */;
17978       else
17979         containing_scope = NULL_TREE;
17980     }
17981
17982   /* Ignore function type "scopes" from the C frontend.  They mean that
17983      a tagged type is local to a parmlist of a function declarator, but
17984      that isn't useful to DWARF.  */
17985   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17986     containing_scope = NULL_TREE;
17987
17988   if (SCOPE_FILE_SCOPE_P (containing_scope))
17989     scope_die = comp_unit_die ();
17990   else if (TYPE_P (containing_scope))
17991     {
17992       /* For types, we can just look up the appropriate DIE.  But
17993          first we check to see if we're in the middle of emitting it
17994          so we know where the new DIE should go.  */
17995       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17996         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17997           break;
17998
17999       if (i < 0)
18000         {
18001           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18002                       || TREE_ASM_WRITTEN (containing_scope));
18003           /*We are not in the middle of emitting the type
18004             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18005           scope_die = lookup_type_die (containing_scope);
18006
18007           /* If none of the current dies are suitable, we get file scope.  */
18008           if (scope_die == NULL)
18009             scope_die = comp_unit_die ();
18010         }
18011       else
18012         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18013     }
18014   else
18015     scope_die = context_die;
18016
18017   return scope_die;
18018 }
18019
18020 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18021
18022 static inline int
18023 local_scope_p (dw_die_ref context_die)
18024 {
18025   for (; context_die; context_die = context_die->die_parent)
18026     if (context_die->die_tag == DW_TAG_inlined_subroutine
18027         || context_die->die_tag == DW_TAG_subprogram)
18028       return 1;
18029
18030   return 0;
18031 }
18032
18033 /* Returns nonzero if CONTEXT_DIE is a class.  */
18034
18035 static inline int
18036 class_scope_p (dw_die_ref context_die)
18037 {
18038   return (context_die
18039           && (context_die->die_tag == DW_TAG_structure_type
18040               || context_die->die_tag == DW_TAG_class_type
18041               || context_die->die_tag == DW_TAG_interface_type
18042               || context_die->die_tag == DW_TAG_union_type));
18043 }
18044
18045 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18046    whether or not to treat a DIE in this context as a declaration.  */
18047
18048 static inline int
18049 class_or_namespace_scope_p (dw_die_ref context_die)
18050 {
18051   return (class_scope_p (context_die)
18052           || (context_die && context_die->die_tag == DW_TAG_namespace));
18053 }
18054
18055 /* Many forms of DIEs require a "type description" attribute.  This
18056    routine locates the proper "type descriptor" die for the type given
18057    by 'type', and adds a DW_AT_type attribute below the given die.  */
18058
18059 static void
18060 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18061                     int decl_volatile, dw_die_ref context_die)
18062 {
18063   enum tree_code code  = TREE_CODE (type);
18064   dw_die_ref type_die  = NULL;
18065
18066   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18067      or fixed-point type, use the inner type.  This is because we have no
18068      support for unnamed types in base_type_die.  This can happen if this is
18069      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18070   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18071       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18072     type = TREE_TYPE (type), code = TREE_CODE (type);
18073
18074   if (code == ERROR_MARK
18075       /* Handle a special case.  For functions whose return type is void, we
18076          generate *no* type attribute.  (Note that no object may have type
18077          `void', so this only applies to function return types).  */
18078       || code == VOID_TYPE)
18079     return;
18080
18081   type_die = modified_type_die (type,
18082                                 decl_const || TYPE_READONLY (type),
18083                                 decl_volatile || TYPE_VOLATILE (type),
18084                                 context_die);
18085
18086   if (type_die != NULL)
18087     add_AT_die_ref (object_die, DW_AT_type, type_die);
18088 }
18089
18090 /* Given an object die, add the calling convention attribute for the
18091    function call type.  */
18092 static void
18093 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18094 {
18095   enum dwarf_calling_convention value = DW_CC_normal;
18096
18097   value = ((enum dwarf_calling_convention)
18098            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18099
18100   if (is_fortran ()
18101       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18102     {
18103       /* DWARF 2 doesn't provide a way to identify a program's source-level
18104         entry point.  DW_AT_calling_convention attributes are only meant
18105         to describe functions' calling conventions.  However, lacking a
18106         better way to signal the Fortran main program, we used this for 
18107         a long time, following existing custom.  Now, DWARF 4 has 
18108         DW_AT_main_subprogram, which we add below, but some tools still
18109         rely on the old way, which we thus keep.  */
18110       value = DW_CC_program;
18111
18112       if (dwarf_version >= 4 || !dwarf_strict)
18113         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18114     }
18115
18116   /* Only add the attribute if the backend requests it, and
18117      is not DW_CC_normal.  */
18118   if (value && (value != DW_CC_normal))
18119     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18120 }
18121
18122 /* Given a tree pointer to a struct, class, union, or enum type node, return
18123    a pointer to the (string) tag name for the given type, or zero if the type
18124    was declared without a tag.  */
18125
18126 static const char *
18127 type_tag (const_tree type)
18128 {
18129   const char *name = 0;
18130
18131   if (TYPE_NAME (type) != 0)
18132     {
18133       tree t = 0;
18134
18135       /* Find the IDENTIFIER_NODE for the type name.  */
18136       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18137           && !TYPE_NAMELESS (type))
18138         t = TYPE_NAME (type);
18139
18140       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18141          a TYPE_DECL node, regardless of whether or not a `typedef' was
18142          involved.  */
18143       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18144                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18145         {
18146           /* We want to be extra verbose.  Don't call dwarf_name if
18147              DECL_NAME isn't set.  The default hook for decl_printable_name
18148              doesn't like that, and in this context it's correct to return
18149              0, instead of "<anonymous>" or the like.  */
18150           if (DECL_NAME (TYPE_NAME (type))
18151               && !DECL_NAMELESS (TYPE_NAME (type)))
18152             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18153         }
18154
18155       /* Now get the name as a string, or invent one.  */
18156       if (!name && t != 0)
18157         name = IDENTIFIER_POINTER (t);
18158     }
18159
18160   return (name == 0 || *name == '\0') ? 0 : name;
18161 }
18162
18163 /* Return the type associated with a data member, make a special check
18164    for bit field types.  */
18165
18166 static inline tree
18167 member_declared_type (const_tree member)
18168 {
18169   return (DECL_BIT_FIELD_TYPE (member)
18170           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18171 }
18172
18173 /* Get the decl's label, as described by its RTL. This may be different
18174    from the DECL_NAME name used in the source file.  */
18175
18176 #if 0
18177 static const char *
18178 decl_start_label (tree decl)
18179 {
18180   rtx x;
18181   const char *fnname;
18182
18183   x = DECL_RTL (decl);
18184   gcc_assert (MEM_P (x));
18185
18186   x = XEXP (x, 0);
18187   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18188
18189   fnname = XSTR (x, 0);
18190   return fnname;
18191 }
18192 #endif
18193 \f
18194 /* These routines generate the internal representation of the DIE's for
18195    the compilation unit.  Debugging information is collected by walking
18196    the declaration trees passed in from dwarf2out_decl().  */
18197
18198 static void
18199 gen_array_type_die (tree type, dw_die_ref context_die)
18200 {
18201   dw_die_ref scope_die = scope_die_for (type, context_die);
18202   dw_die_ref array_die;
18203
18204   /* GNU compilers represent multidimensional array types as sequences of one
18205      dimensional array types whose element types are themselves array types.
18206      We sometimes squish that down to a single array_type DIE with multiple
18207      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18208      say that we are allowed to do this kind of compression in C, because
18209      there is no difference between an array of arrays and a multidimensional
18210      array.  We don't do this for Ada to remain as close as possible to the
18211      actual representation, which is especially important against the language
18212      flexibilty wrt arrays of variable size.  */
18213
18214   bool collapse_nested_arrays = !is_ada ();
18215   tree element_type;
18216
18217   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18218      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18219   if (TYPE_STRING_FLAG (type)
18220       && TREE_CODE (type) == ARRAY_TYPE
18221       && is_fortran ()
18222       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18223     {
18224       HOST_WIDE_INT size;
18225
18226       array_die = new_die (DW_TAG_string_type, scope_die, type);
18227       add_name_attribute (array_die, type_tag (type));
18228       equate_type_number_to_die (type, array_die);
18229       size = int_size_in_bytes (type);
18230       if (size >= 0)
18231         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18232       else if (TYPE_DOMAIN (type) != NULL_TREE
18233                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18234                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18235         {
18236           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18237           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18238
18239           size = int_size_in_bytes (TREE_TYPE (szdecl));
18240           if (loc && size > 0)
18241             {
18242               add_AT_location_description (array_die, DW_AT_string_length, loc);
18243               if (size != DWARF2_ADDR_SIZE)
18244                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18245             }
18246         }
18247       return;
18248     }
18249
18250   /* ??? The SGI dwarf reader fails for array of array of enum types
18251      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18252      array type comes before the outer array type.  We thus call gen_type_die
18253      before we new_die and must prevent nested array types collapsing for this
18254      target.  */
18255
18256 #ifdef MIPS_DEBUGGING_INFO
18257   gen_type_die (TREE_TYPE (type), context_die);
18258   collapse_nested_arrays = false;
18259 #endif
18260
18261   array_die = new_die (DW_TAG_array_type, scope_die, type);
18262   add_name_attribute (array_die, type_tag (type));
18263   equate_type_number_to_die (type, array_die);
18264
18265   if (TREE_CODE (type) == VECTOR_TYPE)
18266     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18267
18268   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18269   if (is_fortran ()
18270       && TREE_CODE (type) == ARRAY_TYPE
18271       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18272       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18273     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18274
18275 #if 0
18276   /* We default the array ordering.  SDB will probably do
18277      the right things even if DW_AT_ordering is not present.  It's not even
18278      an issue until we start to get into multidimensional arrays anyway.  If
18279      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18280      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18281      and when we find out that we need to put these in, we will only do so
18282      for multidimensional arrays.  */
18283   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18284 #endif
18285
18286 #ifdef MIPS_DEBUGGING_INFO
18287   /* The SGI compilers handle arrays of unknown bound by setting
18288      AT_declaration and not emitting any subrange DIEs.  */
18289   if (TREE_CODE (type) == ARRAY_TYPE
18290       && ! TYPE_DOMAIN (type))
18291     add_AT_flag (array_die, DW_AT_declaration, 1);
18292   else
18293 #endif
18294   if (TREE_CODE (type) == VECTOR_TYPE)
18295     {
18296       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18297       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18298       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18299       add_bound_info (subrange_die, DW_AT_upper_bound,
18300                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18301     }
18302   else
18303     add_subscript_info (array_die, type, collapse_nested_arrays);
18304
18305   /* Add representation of the type of the elements of this array type and
18306      emit the corresponding DIE if we haven't done it already.  */
18307   element_type = TREE_TYPE (type);
18308   if (collapse_nested_arrays)
18309     while (TREE_CODE (element_type) == ARRAY_TYPE)
18310       {
18311         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18312           break;
18313         element_type = TREE_TYPE (element_type);
18314       }
18315
18316 #ifndef MIPS_DEBUGGING_INFO
18317   gen_type_die (element_type, context_die);
18318 #endif
18319
18320   add_type_attribute (array_die, element_type, 0, 0, context_die);
18321
18322   if (get_AT (array_die, DW_AT_name))
18323     add_pubtype (type, array_die);
18324 }
18325
18326 static dw_loc_descr_ref
18327 descr_info_loc (tree val, tree base_decl)
18328 {
18329   HOST_WIDE_INT size;
18330   dw_loc_descr_ref loc, loc2;
18331   enum dwarf_location_atom op;
18332
18333   if (val == base_decl)
18334     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18335
18336   switch (TREE_CODE (val))
18337     {
18338     CASE_CONVERT:
18339       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18340     case VAR_DECL:
18341       return loc_descriptor_from_tree (val, 0);
18342     case INTEGER_CST:
18343       if (host_integerp (val, 0))
18344         return int_loc_descriptor (tree_low_cst (val, 0));
18345       break;
18346     case INDIRECT_REF:
18347       size = int_size_in_bytes (TREE_TYPE (val));
18348       if (size < 0)
18349         break;
18350       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18351       if (!loc)
18352         break;
18353       if (size == DWARF2_ADDR_SIZE)
18354         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18355       else
18356         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18357       return loc;
18358     case POINTER_PLUS_EXPR:
18359     case PLUS_EXPR:
18360       if (host_integerp (TREE_OPERAND (val, 1), 1)
18361           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18362              < 16384)
18363         {
18364           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18365           if (!loc)
18366             break;
18367           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18368         }
18369       else
18370         {
18371           op = DW_OP_plus;
18372         do_binop:
18373           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18374           if (!loc)
18375             break;
18376           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18377           if (!loc2)
18378             break;
18379           add_loc_descr (&loc, loc2);
18380           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18381         }
18382       return loc;
18383     case MINUS_EXPR:
18384       op = DW_OP_minus;
18385       goto do_binop;
18386     case MULT_EXPR:
18387       op = DW_OP_mul;
18388       goto do_binop;
18389     case EQ_EXPR:
18390       op = DW_OP_eq;
18391       goto do_binop;
18392     case NE_EXPR:
18393       op = DW_OP_ne;
18394       goto do_binop;
18395     default:
18396       break;
18397     }
18398   return NULL;
18399 }
18400
18401 static void
18402 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18403                       tree val, tree base_decl)
18404 {
18405   dw_loc_descr_ref loc;
18406
18407   if (host_integerp (val, 0))
18408     {
18409       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18410       return;
18411     }
18412
18413   loc = descr_info_loc (val, base_decl);
18414   if (!loc)
18415     return;
18416
18417   add_AT_loc (die, attr, loc);
18418 }
18419
18420 /* This routine generates DIE for array with hidden descriptor, details
18421    are filled into *info by a langhook.  */
18422
18423 static void
18424 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18425                           dw_die_ref context_die)
18426 {
18427   dw_die_ref scope_die = scope_die_for (type, context_die);
18428   dw_die_ref array_die;
18429   int dim;
18430
18431   array_die = new_die (DW_TAG_array_type, scope_die, type);
18432   add_name_attribute (array_die, type_tag (type));
18433   equate_type_number_to_die (type, array_die);
18434
18435   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18436   if (is_fortran ()
18437       && info->ndimensions >= 2)
18438     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18439
18440   if (info->data_location)
18441     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18442                           info->base_decl);
18443   if (info->associated)
18444     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18445                           info->base_decl);
18446   if (info->allocated)
18447     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18448                           info->base_decl);
18449
18450   for (dim = 0; dim < info->ndimensions; dim++)
18451     {
18452       dw_die_ref subrange_die
18453         = new_die (DW_TAG_subrange_type, array_die, NULL);
18454
18455       if (info->dimen[dim].lower_bound)
18456         {
18457           /* If it is the default value, omit it.  */
18458           int dflt;
18459
18460           if (host_integerp (info->dimen[dim].lower_bound, 0)
18461               && (dflt = lower_bound_default ()) != -1
18462               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18463             ;
18464           else
18465             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18466                                   info->dimen[dim].lower_bound,
18467                                   info->base_decl);
18468         }
18469       if (info->dimen[dim].upper_bound)
18470         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18471                               info->dimen[dim].upper_bound,
18472                               info->base_decl);
18473       if (info->dimen[dim].stride)
18474         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18475                               info->dimen[dim].stride,
18476                               info->base_decl);
18477     }
18478
18479   gen_type_die (info->element_type, context_die);
18480   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18481
18482   if (get_AT (array_die, DW_AT_name))
18483     add_pubtype (type, array_die);
18484 }
18485
18486 #if 0
18487 static void
18488 gen_entry_point_die (tree decl, dw_die_ref context_die)
18489 {
18490   tree origin = decl_ultimate_origin (decl);
18491   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18492
18493   if (origin != NULL)
18494     add_abstract_origin_attribute (decl_die, origin);
18495   else
18496     {
18497       add_name_and_src_coords_attributes (decl_die, decl);
18498       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18499                           0, 0, context_die);
18500     }
18501
18502   if (DECL_ABSTRACT (decl))
18503     equate_decl_number_to_die (decl, decl_die);
18504   else
18505     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18506 }
18507 #endif
18508
18509 /* Walk through the list of incomplete types again, trying once more to
18510    emit full debugging info for them.  */
18511
18512 static void
18513 retry_incomplete_types (void)
18514 {
18515   int i;
18516
18517   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18518     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18519                                   DINFO_USAGE_DIR_USE))
18520       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18521 }
18522
18523 /* Determine what tag to use for a record type.  */
18524
18525 static enum dwarf_tag
18526 record_type_tag (tree type)
18527 {
18528   if (! lang_hooks.types.classify_record)
18529     return DW_TAG_structure_type;
18530
18531   switch (lang_hooks.types.classify_record (type))
18532     {
18533     case RECORD_IS_STRUCT:
18534       return DW_TAG_structure_type;
18535
18536     case RECORD_IS_CLASS:
18537       return DW_TAG_class_type;
18538
18539     case RECORD_IS_INTERFACE:
18540       if (dwarf_version >= 3 || !dwarf_strict)
18541         return DW_TAG_interface_type;
18542       return DW_TAG_structure_type;
18543
18544     default:
18545       gcc_unreachable ();
18546     }
18547 }
18548
18549 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18550    include all of the information about the enumeration values also. Each
18551    enumerated type name/value is listed as a child of the enumerated type
18552    DIE.  */
18553
18554 static dw_die_ref
18555 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18556 {
18557   dw_die_ref type_die = lookup_type_die (type);
18558
18559   if (type_die == NULL)
18560     {
18561       type_die = new_die (DW_TAG_enumeration_type,
18562                           scope_die_for (type, context_die), type);
18563       equate_type_number_to_die (type, type_die);
18564       add_name_attribute (type_die, type_tag (type));
18565       if (dwarf_version >= 4 || !dwarf_strict)
18566         {
18567           if (ENUM_IS_SCOPED (type))
18568             add_AT_flag (type_die, DW_AT_enum_class, 1);
18569           if (ENUM_IS_OPAQUE (type))
18570             add_AT_flag (type_die, DW_AT_declaration, 1);
18571         }
18572     }
18573   else if (! TYPE_SIZE (type))
18574     return type_die;
18575   else
18576     remove_AT (type_die, DW_AT_declaration);
18577
18578   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18579      given enum type is incomplete, do not generate the DW_AT_byte_size
18580      attribute or the DW_AT_element_list attribute.  */
18581   if (TYPE_SIZE (type))
18582     {
18583       tree link;
18584
18585       TREE_ASM_WRITTEN (type) = 1;
18586       add_byte_size_attribute (type_die, type);
18587       if (TYPE_STUB_DECL (type) != NULL_TREE)
18588         {
18589           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18590           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18591         }
18592
18593       /* If the first reference to this type was as the return type of an
18594          inline function, then it may not have a parent.  Fix this now.  */
18595       if (type_die->die_parent == NULL)
18596         add_child_die (scope_die_for (type, context_die), type_die);
18597
18598       for (link = TYPE_VALUES (type);
18599            link != NULL; link = TREE_CHAIN (link))
18600         {
18601           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18602           tree value = TREE_VALUE (link);
18603
18604           add_name_attribute (enum_die,
18605                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18606
18607           if (TREE_CODE (value) == CONST_DECL)
18608             value = DECL_INITIAL (value);
18609
18610           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18611             /* DWARF2 does not provide a way of indicating whether or
18612                not enumeration constants are signed or unsigned.  GDB
18613                always assumes the values are signed, so we output all
18614                values as if they were signed.  That means that
18615                enumeration constants with very large unsigned values
18616                will appear to have negative values in the debugger.  */
18617             add_AT_int (enum_die, DW_AT_const_value,
18618                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18619         }
18620     }
18621   else
18622     add_AT_flag (type_die, DW_AT_declaration, 1);
18623
18624   if (get_AT (type_die, DW_AT_name))
18625     add_pubtype (type, type_die);
18626
18627   return type_die;
18628 }
18629
18630 /* Generate a DIE to represent either a real live formal parameter decl or to
18631    represent just the type of some formal parameter position in some function
18632    type.
18633
18634    Note that this routine is a bit unusual because its argument may be a
18635    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18636    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18637    node.  If it's the former then this function is being called to output a
18638    DIE to represent a formal parameter object (or some inlining thereof).  If
18639    it's the latter, then this function is only being called to output a
18640    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18641    argument type of some subprogram type.
18642    If EMIT_NAME_P is true, name and source coordinate attributes
18643    are emitted.  */
18644
18645 static dw_die_ref
18646 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18647                           dw_die_ref context_die)
18648 {
18649   tree node_or_origin = node ? node : origin;
18650   tree ultimate_origin;
18651   dw_die_ref parm_die
18652     = new_die (DW_TAG_formal_parameter, context_die, node);
18653
18654   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18655     {
18656     case tcc_declaration:
18657       ultimate_origin = decl_ultimate_origin (node_or_origin);
18658       if (node || ultimate_origin)
18659         origin = ultimate_origin;
18660       if (origin != NULL)
18661         add_abstract_origin_attribute (parm_die, origin);
18662       else if (emit_name_p)
18663         add_name_and_src_coords_attributes (parm_die, node);
18664       if (origin == NULL
18665           || (! DECL_ABSTRACT (node_or_origin)
18666               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18667                                            decl_function_context
18668                                                             (node_or_origin))))
18669         {
18670           tree type = TREE_TYPE (node_or_origin);
18671           if (decl_by_reference_p (node_or_origin))
18672             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18673                                 context_die);
18674           else
18675             add_type_attribute (parm_die, type,
18676                                 TREE_READONLY (node_or_origin),
18677                                 TREE_THIS_VOLATILE (node_or_origin),
18678                                 context_die);
18679         }
18680       if (origin == NULL && DECL_ARTIFICIAL (node))
18681         add_AT_flag (parm_die, DW_AT_artificial, 1);
18682
18683       if (node && node != origin)
18684         equate_decl_number_to_die (node, parm_die);
18685       if (! DECL_ABSTRACT (node_or_origin))
18686         add_location_or_const_value_attribute (parm_die, node_or_origin,
18687                                                DW_AT_location);
18688
18689       break;
18690
18691     case tcc_type:
18692       /* We were called with some kind of a ..._TYPE node.  */
18693       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18694       break;
18695
18696     default:
18697       gcc_unreachable ();
18698     }
18699
18700   return parm_die;
18701 }
18702
18703 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18704    children DW_TAG_formal_parameter DIEs representing the arguments of the
18705    parameter pack.
18706
18707    PARM_PACK must be a function parameter pack.
18708    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18709    must point to the subsequent arguments of the function PACK_ARG belongs to.
18710    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18711    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18712    following the last one for which a DIE was generated.  */
18713
18714 static dw_die_ref
18715 gen_formal_parameter_pack_die  (tree parm_pack,
18716                                 tree pack_arg,
18717                                 dw_die_ref subr_die,
18718                                 tree *next_arg)
18719 {
18720   tree arg;
18721   dw_die_ref parm_pack_die;
18722
18723   gcc_assert (parm_pack
18724               && lang_hooks.function_parameter_pack_p (parm_pack)
18725               && subr_die);
18726
18727   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18728   add_src_coords_attributes (parm_pack_die, parm_pack);
18729
18730   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18731     {
18732       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18733                                                                  parm_pack))
18734         break;
18735       gen_formal_parameter_die (arg, NULL,
18736                                 false /* Don't emit name attribute.  */,
18737                                 parm_pack_die);
18738     }
18739   if (next_arg)
18740     *next_arg = arg;
18741   return parm_pack_die;
18742 }
18743
18744 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18745    at the end of an (ANSI prototyped) formal parameters list.  */
18746
18747 static void
18748 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18749 {
18750   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18751 }
18752
18753 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18754    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18755    parameters as specified in some function type specification (except for
18756    those which appear as part of a function *definition*).  */
18757
18758 static void
18759 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18760 {
18761   tree link;
18762   tree formal_type = NULL;
18763   tree first_parm_type;
18764   tree arg;
18765
18766   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18767     {
18768       arg = DECL_ARGUMENTS (function_or_method_type);
18769       function_or_method_type = TREE_TYPE (function_or_method_type);
18770     }
18771   else
18772     arg = NULL_TREE;
18773
18774   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18775
18776   /* Make our first pass over the list of formal parameter types and output a
18777      DW_TAG_formal_parameter DIE for each one.  */
18778   for (link = first_parm_type; link; )
18779     {
18780       dw_die_ref parm_die;
18781
18782       formal_type = TREE_VALUE (link);
18783       if (formal_type == void_type_node)
18784         break;
18785
18786       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18787       parm_die = gen_formal_parameter_die (formal_type, NULL,
18788                                            true /* Emit name attribute.  */,
18789                                            context_die);
18790       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18791           && link == first_parm_type)
18792         {
18793           add_AT_flag (parm_die, DW_AT_artificial, 1);
18794           if (dwarf_version >= 3 || !dwarf_strict)
18795             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18796         }
18797       else if (arg && DECL_ARTIFICIAL (arg))
18798         add_AT_flag (parm_die, DW_AT_artificial, 1);
18799
18800       link = TREE_CHAIN (link);
18801       if (arg)
18802         arg = DECL_CHAIN (arg);
18803     }
18804
18805   /* If this function type has an ellipsis, add a
18806      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18807   if (formal_type != void_type_node)
18808     gen_unspecified_parameters_die (function_or_method_type, context_die);
18809
18810   /* Make our second (and final) pass over the list of formal parameter types
18811      and output DIEs to represent those types (as necessary).  */
18812   for (link = TYPE_ARG_TYPES (function_or_method_type);
18813        link && TREE_VALUE (link);
18814        link = TREE_CHAIN (link))
18815     gen_type_die (TREE_VALUE (link), context_die);
18816 }
18817
18818 /* We want to generate the DIE for TYPE so that we can generate the
18819    die for MEMBER, which has been defined; we will need to refer back
18820    to the member declaration nested within TYPE.  If we're trying to
18821    generate minimal debug info for TYPE, processing TYPE won't do the
18822    trick; we need to attach the member declaration by hand.  */
18823
18824 static void
18825 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18826 {
18827   gen_type_die (type, context_die);
18828
18829   /* If we're trying to avoid duplicate debug info, we may not have
18830      emitted the member decl for this function.  Emit it now.  */
18831   if (TYPE_STUB_DECL (type)
18832       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18833       && ! lookup_decl_die (member))
18834     {
18835       dw_die_ref type_die;
18836       gcc_assert (!decl_ultimate_origin (member));
18837
18838       push_decl_scope (type);
18839       type_die = lookup_type_die_strip_naming_typedef (type);
18840       if (TREE_CODE (member) == FUNCTION_DECL)
18841         gen_subprogram_die (member, type_die);
18842       else if (TREE_CODE (member) == FIELD_DECL)
18843         {
18844           /* Ignore the nameless fields that are used to skip bits but handle
18845              C++ anonymous unions and structs.  */
18846           if (DECL_NAME (member) != NULL_TREE
18847               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18848               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18849             {
18850               gen_type_die (member_declared_type (member), type_die);
18851               gen_field_die (member, type_die);
18852             }
18853         }
18854       else
18855         gen_variable_die (member, NULL_TREE, type_die);
18856
18857       pop_decl_scope ();
18858     }
18859 }
18860
18861 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18862    may later generate inlined and/or out-of-line instances of.  */
18863
18864 static void
18865 dwarf2out_abstract_function (tree decl)
18866 {
18867   dw_die_ref old_die;
18868   tree save_fn;
18869   tree context;
18870   int was_abstract;
18871   htab_t old_decl_loc_table;
18872
18873   /* Make sure we have the actual abstract inline, not a clone.  */
18874   decl = DECL_ORIGIN (decl);
18875
18876   old_die = lookup_decl_die (decl);
18877   if (old_die && get_AT (old_die, DW_AT_inline))
18878     /* We've already generated the abstract instance.  */
18879     return;
18880
18881   /* We can be called while recursively when seeing block defining inlined subroutine
18882      DIE.  Be sure to not clobber the outer location table nor use it or we would
18883      get locations in abstract instantces.  */
18884   old_decl_loc_table = decl_loc_table;
18885   decl_loc_table = NULL;
18886
18887   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18888      we don't get confused by DECL_ABSTRACT.  */
18889   if (debug_info_level > DINFO_LEVEL_TERSE)
18890     {
18891       context = decl_class_context (decl);
18892       if (context)
18893         gen_type_die_for_member
18894           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18895     }
18896
18897   /* Pretend we've just finished compiling this function.  */
18898   save_fn = current_function_decl;
18899   current_function_decl = decl;
18900   push_cfun (DECL_STRUCT_FUNCTION (decl));
18901
18902   was_abstract = DECL_ABSTRACT (decl);
18903   set_decl_abstract_flags (decl, 1);
18904   dwarf2out_decl (decl);
18905   if (! was_abstract)
18906     set_decl_abstract_flags (decl, 0);
18907
18908   current_function_decl = save_fn;
18909   decl_loc_table = old_decl_loc_table;
18910   pop_cfun ();
18911 }
18912
18913 /* Helper function of premark_used_types() which gets called through
18914    htab_traverse.
18915
18916    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18917    marked as unused by prune_unused_types.  */
18918
18919 static int
18920 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18921 {
18922   tree type;
18923   dw_die_ref die;
18924
18925   type = (tree) *slot;
18926   die = lookup_type_die (type);
18927   if (die != NULL)
18928     die->die_perennial_p = 1;
18929   return 1;
18930 }
18931
18932 /* Helper function of premark_types_used_by_global_vars which gets called
18933    through htab_traverse.
18934
18935    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18936    marked as unused by prune_unused_types. The DIE of the type is marked
18937    only if the global variable using the type will actually be emitted.  */
18938
18939 static int
18940 premark_types_used_by_global_vars_helper (void **slot,
18941                                           void *data ATTRIBUTE_UNUSED)
18942 {
18943   struct types_used_by_vars_entry *entry;
18944   dw_die_ref die;
18945
18946   entry = (struct types_used_by_vars_entry *) *slot;
18947   gcc_assert (entry->type != NULL
18948               && entry->var_decl != NULL);
18949   die = lookup_type_die (entry->type);
18950   if (die)
18951     {
18952       /* Ask cgraph if the global variable really is to be emitted.
18953          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18954       struct varpool_node *node = varpool_get_node (entry->var_decl);
18955       if (node && node->needed)
18956         {
18957           die->die_perennial_p = 1;
18958           /* Keep the parent DIEs as well.  */
18959           while ((die = die->die_parent) && die->die_perennial_p == 0)
18960             die->die_perennial_p = 1;
18961         }
18962     }
18963   return 1;
18964 }
18965
18966 /* Mark all members of used_types_hash as perennial.  */
18967
18968 static void
18969 premark_used_types (void)
18970 {
18971   if (cfun && cfun->used_types_hash)
18972     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18973 }
18974
18975 /* Mark all members of types_used_by_vars_entry as perennial.  */
18976
18977 static void
18978 premark_types_used_by_global_vars (void)
18979 {
18980   if (types_used_by_vars_hash)
18981     htab_traverse (types_used_by_vars_hash,
18982                    premark_types_used_by_global_vars_helper, NULL);
18983 }
18984
18985 /* Generate a DIE to represent a declared function (either file-scope or
18986    block-local).  */
18987
18988 static void
18989 gen_subprogram_die (tree decl, dw_die_ref context_die)
18990 {
18991   tree origin = decl_ultimate_origin (decl);
18992   dw_die_ref subr_die;
18993   tree outer_scope;
18994   dw_die_ref old_die = lookup_decl_die (decl);
18995   int declaration = (current_function_decl != decl
18996                      || class_or_namespace_scope_p (context_die));
18997
18998   premark_used_types ();
18999
19000   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19001      started to generate the abstract instance of an inline, decided to output
19002      its containing class, and proceeded to emit the declaration of the inline
19003      from the member list for the class.  If so, DECLARATION takes priority;
19004      we'll get back to the abstract instance when done with the class.  */
19005
19006   /* The class-scope declaration DIE must be the primary DIE.  */
19007   if (origin && declaration && class_or_namespace_scope_p (context_die))
19008     {
19009       origin = NULL;
19010       gcc_assert (!old_die);
19011     }
19012
19013   /* Now that the C++ front end lazily declares artificial member fns, we
19014      might need to retrofit the declaration into its class.  */
19015   if (!declaration && !origin && !old_die
19016       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19017       && !class_or_namespace_scope_p (context_die)
19018       && debug_info_level > DINFO_LEVEL_TERSE)
19019     old_die = force_decl_die (decl);
19020
19021   if (origin != NULL)
19022     {
19023       gcc_assert (!declaration || local_scope_p (context_die));
19024
19025       /* Fixup die_parent for the abstract instance of a nested
19026          inline function.  */
19027       if (old_die && old_die->die_parent == NULL)
19028         add_child_die (context_die, old_die);
19029
19030       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19031       add_abstract_origin_attribute (subr_die, origin);
19032     }
19033   else if (old_die)
19034     {
19035       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19036       struct dwarf_file_data * file_index = lookup_filename (s.file);
19037
19038       if (!get_AT_flag (old_die, DW_AT_declaration)
19039           /* We can have a normal definition following an inline one in the
19040              case of redefinition of GNU C extern inlines.
19041              It seems reasonable to use AT_specification in this case.  */
19042           && !get_AT (old_die, DW_AT_inline))
19043         {
19044           /* Detect and ignore this case, where we are trying to output
19045              something we have already output.  */
19046           return;
19047         }
19048
19049       /* If the definition comes from the same place as the declaration,
19050          maybe use the old DIE.  We always want the DIE for this function
19051          that has the *_pc attributes to be under comp_unit_die so the
19052          debugger can find it.  We also need to do this for abstract
19053          instances of inlines, since the spec requires the out-of-line copy
19054          to have the same parent.  For local class methods, this doesn't
19055          apply; we just use the old DIE.  */
19056       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19057           && (DECL_ARTIFICIAL (decl)
19058               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19059                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19060                       == (unsigned) s.line))))
19061         {
19062           subr_die = old_die;
19063
19064           /* Clear out the declaration attribute and the formal parameters.
19065              Do not remove all children, because it is possible that this
19066              declaration die was forced using force_decl_die(). In such
19067              cases die that forced declaration die (e.g. TAG_imported_module)
19068              is one of the children that we do not want to remove.  */
19069           remove_AT (subr_die, DW_AT_declaration);
19070           remove_AT (subr_die, DW_AT_object_pointer);
19071           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19072         }
19073       else
19074         {
19075           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19076           add_AT_specification (subr_die, old_die);
19077           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19078             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19079           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19080             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19081         }
19082     }
19083   else
19084     {
19085       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19086
19087       if (TREE_PUBLIC (decl))
19088         add_AT_flag (subr_die, DW_AT_external, 1);
19089
19090       add_name_and_src_coords_attributes (subr_die, decl);
19091       if (debug_info_level > DINFO_LEVEL_TERSE)
19092         {
19093           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19094           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19095                               0, 0, context_die);
19096         }
19097
19098       add_pure_or_virtual_attribute (subr_die, decl);
19099       if (DECL_ARTIFICIAL (decl))
19100         add_AT_flag (subr_die, DW_AT_artificial, 1);
19101
19102       add_accessibility_attribute (subr_die, decl);
19103     }
19104
19105   if (declaration)
19106     {
19107       if (!old_die || !get_AT (old_die, DW_AT_inline))
19108         {
19109           add_AT_flag (subr_die, DW_AT_declaration, 1);
19110
19111           /* If this is an explicit function declaration then generate
19112              a DW_AT_explicit attribute.  */
19113           if (lang_hooks.decls.function_decl_explicit_p (decl)
19114               && (dwarf_version >= 3 || !dwarf_strict))
19115             add_AT_flag (subr_die, DW_AT_explicit, 1);
19116
19117           /* The first time we see a member function, it is in the context of
19118              the class to which it belongs.  We make sure of this by emitting
19119              the class first.  The next time is the definition, which is
19120              handled above.  The two may come from the same source text.
19121
19122              Note that force_decl_die() forces function declaration die. It is
19123              later reused to represent definition.  */
19124           equate_decl_number_to_die (decl, subr_die);
19125         }
19126     }
19127   else if (DECL_ABSTRACT (decl))
19128     {
19129       if (DECL_DECLARED_INLINE_P (decl))
19130         {
19131           if (cgraph_function_possibly_inlined_p (decl))
19132             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19133           else
19134             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19135         }
19136       else
19137         {
19138           if (cgraph_function_possibly_inlined_p (decl))
19139             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19140           else
19141             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19142         }
19143
19144       if (DECL_DECLARED_INLINE_P (decl)
19145           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19146         add_AT_flag (subr_die, DW_AT_artificial, 1);
19147
19148       equate_decl_number_to_die (decl, subr_die);
19149     }
19150   else if (!DECL_EXTERNAL (decl))
19151     {
19152       HOST_WIDE_INT cfa_fb_offset;
19153
19154       if (!old_die || !get_AT (old_die, DW_AT_inline))
19155         equate_decl_number_to_die (decl, subr_die);
19156
19157       if (!flag_reorder_blocks_and_partition)
19158         {
19159           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19160           if (fde->dw_fde_begin)
19161             {
19162               /* We have already generated the labels.  */
19163               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19164               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19165             }
19166           else
19167             {
19168               /* Create start/end labels and add the range.  */
19169               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19170               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19171                                            current_function_funcdef_no);
19172               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19173               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19174                                            current_function_funcdef_no);
19175               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19176             }
19177
19178 #if VMS_DEBUGGING_INFO
19179       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19180          Section 2.3 Prologue and Epilogue Attributes:
19181          When a breakpoint is set on entry to a function, it is generally
19182          desirable for execution to be suspended, not on the very first
19183          instruction of the function, but rather at a point after the
19184          function's frame has been set up, after any language defined local
19185          declaration processing has been completed, and before execution of
19186          the first statement of the function begins. Debuggers generally
19187          cannot properly determine where this point is.  Similarly for a
19188          breakpoint set on exit from a function. The prologue and epilogue
19189          attributes allow a compiler to communicate the location(s) to use.  */
19190
19191       {
19192         if (fde->dw_fde_vms_end_prologue)
19193           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19194             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19195
19196         if (fde->dw_fde_vms_begin_epilogue)
19197           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19198             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19199       }
19200 #endif
19201
19202           add_pubname (decl, subr_die);
19203           add_arange (decl, subr_die);
19204         }
19205       else
19206         {  /* Generate pubnames entries for the split function code
19207               ranges.  */
19208           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19209
19210           if (fde->dw_fde_switched_sections)
19211             {
19212               if (dwarf_version >= 3 || !dwarf_strict)
19213                 {
19214                   /* We should use ranges for non-contiguous code section 
19215                      addresses.  Use the actual code range for the initial
19216                      section, since the HOT/COLD labels might precede an 
19217                      alignment offset.  */
19218                   bool range_list_added = false;
19219                   if (fde->in_std_section)
19220                     {
19221                       add_ranges_by_labels (subr_die,
19222                                             fde->dw_fde_begin,
19223                                             fde->dw_fde_end,
19224                                             &range_list_added);
19225                       add_ranges_by_labels (subr_die,
19226                                             fde->dw_fde_unlikely_section_label,
19227                                             fde->dw_fde_unlikely_section_end_label,
19228                                             &range_list_added);
19229                     }
19230                   else
19231                     {
19232                       add_ranges_by_labels (subr_die,
19233                                             fde->dw_fde_begin,
19234                                             fde->dw_fde_end,
19235                                             &range_list_added);
19236                       add_ranges_by_labels (subr_die,
19237                                             fde->dw_fde_hot_section_label,
19238                                             fde->dw_fde_hot_section_end_label,
19239                                             &range_list_added);
19240                     }
19241                   add_pubname (decl, subr_die);
19242                   if (range_list_added)
19243                     add_ranges (NULL);
19244                 }
19245               else
19246                 {
19247                   /* There is no real support in DW2 for this .. so we make
19248                      a work-around.  First, emit the pub name for the segment
19249                      containing the function label.  Then make and emit a
19250                      simplified subprogram DIE for the second segment with the
19251                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19252                      linkage name for the second die so that gdb will find both
19253                      sections when given "b foo".  */
19254                   const char *name = NULL;
19255                   tree decl_name = DECL_NAME (decl);
19256                   dw_die_ref seg_die;
19257
19258                   /* Do the 'primary' section.   */
19259                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19260                                  fde->dw_fde_begin);
19261                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19262                                  fde->dw_fde_end);
19263                   /* Add it.   */
19264                   add_pubname (decl, subr_die);
19265                   add_arange (decl, subr_die);
19266
19267                   /* Build a minimal DIE for the secondary section.  */
19268                   seg_die = new_die (DW_TAG_subprogram,
19269                                      subr_die->die_parent, decl);
19270
19271                   if (TREE_PUBLIC (decl))
19272                     add_AT_flag (seg_die, DW_AT_external, 1);
19273
19274                   if (decl_name != NULL 
19275                       && IDENTIFIER_POINTER (decl_name) != NULL)
19276                     {
19277                       name = dwarf2_name (decl, 1);
19278                       if (! DECL_ARTIFICIAL (decl))
19279                         add_src_coords_attributes (seg_die, decl);
19280
19281                       add_linkage_name (seg_die, decl);
19282                     }
19283                   gcc_assert (name!=NULL);
19284                   add_pure_or_virtual_attribute (seg_die, decl);
19285                   if (DECL_ARTIFICIAL (decl))
19286                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19287
19288                   if (fde->in_std_section)
19289                     {
19290                       name = concat ("__cold_sect_of_", name, NULL); 
19291                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19292                                      fde->dw_fde_unlikely_section_label);
19293                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19294                                      fde->dw_fde_unlikely_section_end_label); 
19295                     }
19296                   else 
19297                     {
19298                       name = concat ("__hot_sect_of_", name, NULL); 
19299                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19300                                      fde->dw_fde_hot_section_label);
19301                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19302                                      fde->dw_fde_hot_section_end_label); 
19303                     }
19304                   add_name_attribute (seg_die, name);
19305                   add_pubname_string (name, seg_die);
19306                   add_arange (decl, seg_die);
19307                 }
19308             }
19309           else
19310             {
19311               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19312               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19313               add_pubname (decl, subr_die);
19314               add_arange (decl, subr_die);
19315             }
19316         }
19317
19318 #ifdef MIPS_DEBUGGING_INFO
19319       /* Add a reference to the FDE for this routine.  */
19320       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19321 #endif
19322
19323       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19324
19325       /* We define the "frame base" as the function's CFA.  This is more
19326          convenient for several reasons: (1) It's stable across the prologue
19327          and epilogue, which makes it better than just a frame pointer,
19328          (2) With dwarf3, there exists a one-byte encoding that allows us
19329          to reference the .debug_frame data by proxy, but failing that,
19330          (3) We can at least reuse the code inspection and interpretation
19331          code that determines the CFA position at various points in the
19332          function.  */
19333       if (dwarf_version >= 3)
19334         {
19335           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19336           add_AT_loc (subr_die, DW_AT_frame_base, op);
19337         }
19338       else
19339         {
19340           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19341           if (list->dw_loc_next)
19342             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19343           else
19344             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19345         }
19346
19347       /* Compute a displacement from the "steady-state frame pointer" to
19348          the CFA.  The former is what all stack slots and argument slots
19349          will reference in the rtl; the later is what we've told the
19350          debugger about.  We'll need to adjust all frame_base references
19351          by this displacement.  */
19352       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19353
19354       if (cfun->static_chain_decl)
19355         add_AT_location_description (subr_die, DW_AT_static_link,
19356                  loc_list_from_tree (cfun->static_chain_decl, 2));
19357     }
19358
19359   /* Generate child dies for template paramaters.  */
19360   if (debug_info_level > DINFO_LEVEL_TERSE)
19361     gen_generic_params_dies (decl);
19362
19363   /* Now output descriptions of the arguments for this function. This gets
19364      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19365      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19366      `...' at the end of the formal parameter list.  In order to find out if
19367      there was a trailing ellipsis or not, we must instead look at the type
19368      associated with the FUNCTION_DECL.  This will be a node of type
19369      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19370      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19371      an ellipsis at the end.  */
19372
19373   /* In the case where we are describing a mere function declaration, all we
19374      need to do here (and all we *can* do here) is to describe the *types* of
19375      its formal parameters.  */
19376   if (debug_info_level <= DINFO_LEVEL_TERSE)
19377     ;
19378   else if (declaration)
19379     gen_formal_types_die (decl, subr_die);
19380   else
19381     {
19382       /* Generate DIEs to represent all known formal parameters.  */
19383       tree parm = DECL_ARGUMENTS (decl);
19384       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19385       tree generic_decl_parm = generic_decl
19386                                 ? DECL_ARGUMENTS (generic_decl)
19387                                 : NULL;
19388
19389       /* Now we want to walk the list of parameters of the function and
19390          emit their relevant DIEs.
19391
19392          We consider the case of DECL being an instance of a generic function
19393          as well as it being a normal function.
19394
19395          If DECL is an instance of a generic function we walk the
19396          parameters of the generic function declaration _and_ the parameters of
19397          DECL itself. This is useful because we want to emit specific DIEs for
19398          function parameter packs and those are declared as part of the
19399          generic function declaration. In that particular case,
19400          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19401          That DIE has children DIEs representing the set of arguments
19402          of the pack. Note that the set of pack arguments can be empty.
19403          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19404          children DIE.
19405
19406          Otherwise, we just consider the parameters of DECL.  */
19407       while (generic_decl_parm || parm)
19408         {
19409           if (generic_decl_parm
19410               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19411             gen_formal_parameter_pack_die (generic_decl_parm,
19412                                            parm, subr_die,
19413                                            &parm);
19414           else if (parm)
19415             {
19416               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19417
19418               if (parm == DECL_ARGUMENTS (decl)
19419                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19420                   && parm_die
19421                   && (dwarf_version >= 3 || !dwarf_strict))
19422                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19423
19424               parm = DECL_CHAIN (parm);
19425             }
19426
19427           if (generic_decl_parm)
19428             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19429         }
19430
19431       /* Decide whether we need an unspecified_parameters DIE at the end.
19432          There are 2 more cases to do this for: 1) the ansi ... declaration -
19433          this is detectable when the end of the arg list is not a
19434          void_type_node 2) an unprototyped function declaration (not a
19435          definition).  This just means that we have no info about the
19436          parameters at all.  */
19437       if (prototype_p (TREE_TYPE (decl)))
19438         {
19439           /* This is the prototyped case, check for....  */
19440           if (stdarg_p (TREE_TYPE (decl)))
19441             gen_unspecified_parameters_die (decl, subr_die);
19442         }
19443       else if (DECL_INITIAL (decl) == NULL_TREE)
19444         gen_unspecified_parameters_die (decl, subr_die);
19445     }
19446
19447   /* Output Dwarf info for all of the stuff within the body of the function
19448      (if it has one - it may be just a declaration).  */
19449   outer_scope = DECL_INITIAL (decl);
19450
19451   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19452      a function.  This BLOCK actually represents the outermost binding contour
19453      for the function, i.e. the contour in which the function's formal
19454      parameters and labels get declared. Curiously, it appears that the front
19455      end doesn't actually put the PARM_DECL nodes for the current function onto
19456      the BLOCK_VARS list for this outer scope, but are strung off of the
19457      DECL_ARGUMENTS list for the function instead.
19458
19459      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19460      the LABEL_DECL nodes for the function however, and we output DWARF info
19461      for those in decls_for_scope.  Just within the `outer_scope' there will be
19462      a BLOCK node representing the function's outermost pair of curly braces,
19463      and any blocks used for the base and member initializers of a C++
19464      constructor function.  */
19465   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19466     {
19467       /* Emit a DW_TAG_variable DIE for a named return value.  */
19468       if (DECL_NAME (DECL_RESULT (decl)))
19469         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19470
19471       current_function_has_inlines = 0;
19472       decls_for_scope (outer_scope, subr_die, 0);
19473     }
19474   /* Add the calling convention attribute if requested.  */
19475   add_calling_convention_attribute (subr_die, decl);
19476
19477 }
19478
19479 /* Returns a hash value for X (which really is a die_struct).  */
19480
19481 static hashval_t
19482 common_block_die_table_hash (const void *x)
19483 {
19484   const_dw_die_ref d = (const_dw_die_ref) x;
19485   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19486 }
19487
19488 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19489    as decl_id and die_parent of die_struct Y.  */
19490
19491 static int
19492 common_block_die_table_eq (const void *x, const void *y)
19493 {
19494   const_dw_die_ref d = (const_dw_die_ref) x;
19495   const_dw_die_ref e = (const_dw_die_ref) y;
19496   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19497 }
19498
19499 /* Generate a DIE to represent a declared data object.
19500    Either DECL or ORIGIN must be non-null.  */
19501
19502 static void
19503 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19504 {
19505   HOST_WIDE_INT off;
19506   tree com_decl;
19507   tree decl_or_origin = decl ? decl : origin;
19508   tree ultimate_origin;
19509   dw_die_ref var_die;
19510   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19511   dw_die_ref origin_die;
19512   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19513                       || class_or_namespace_scope_p (context_die));
19514   bool specialization_p = false;
19515
19516   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19517   if (decl || ultimate_origin)
19518     origin = ultimate_origin;
19519   com_decl = fortran_common (decl_or_origin, &off);
19520
19521   /* Symbol in common gets emitted as a child of the common block, in the form
19522      of a data member.  */
19523   if (com_decl)
19524     {
19525       dw_die_ref com_die;
19526       dw_loc_list_ref loc;
19527       die_node com_die_arg;
19528
19529       var_die = lookup_decl_die (decl_or_origin);
19530       if (var_die)
19531         {
19532           if (get_AT (var_die, DW_AT_location) == NULL)
19533             {
19534               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19535               if (loc)
19536                 {
19537                   if (off)
19538                     {
19539                       /* Optimize the common case.  */
19540                       if (single_element_loc_list_p (loc)
19541                           && loc->expr->dw_loc_opc == DW_OP_addr
19542                           && loc->expr->dw_loc_next == NULL
19543                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19544                              == SYMBOL_REF)
19545                         loc->expr->dw_loc_oprnd1.v.val_addr
19546                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19547                         else
19548                           loc_list_plus_const (loc, off);
19549                     }
19550                   add_AT_location_description (var_die, DW_AT_location, loc);
19551                   remove_AT (var_die, DW_AT_declaration);
19552                 }
19553             }
19554           return;
19555         }
19556
19557       if (common_block_die_table == NULL)
19558         common_block_die_table
19559           = htab_create_ggc (10, common_block_die_table_hash,
19560                              common_block_die_table_eq, NULL);
19561
19562       com_die_arg.decl_id = DECL_UID (com_decl);
19563       com_die_arg.die_parent = context_die;
19564       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19565       loc = loc_list_from_tree (com_decl, 2);
19566       if (com_die == NULL)
19567         {
19568           const char *cnam
19569             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19570           void **slot;
19571
19572           com_die = new_die (DW_TAG_common_block, context_die, decl);
19573           add_name_and_src_coords_attributes (com_die, com_decl);
19574           if (loc)
19575             {
19576               add_AT_location_description (com_die, DW_AT_location, loc);
19577               /* Avoid sharing the same loc descriptor between
19578                  DW_TAG_common_block and DW_TAG_variable.  */
19579               loc = loc_list_from_tree (com_decl, 2);
19580             }
19581           else if (DECL_EXTERNAL (decl))
19582             add_AT_flag (com_die, DW_AT_declaration, 1);
19583           add_pubname_string (cnam, com_die); /* ??? needed? */
19584           com_die->decl_id = DECL_UID (com_decl);
19585           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19586           *slot = (void *) com_die;
19587         }
19588       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19589         {
19590           add_AT_location_description (com_die, DW_AT_location, loc);
19591           loc = loc_list_from_tree (com_decl, 2);
19592           remove_AT (com_die, DW_AT_declaration);
19593         }
19594       var_die = new_die (DW_TAG_variable, com_die, decl);
19595       add_name_and_src_coords_attributes (var_die, decl);
19596       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19597                           TREE_THIS_VOLATILE (decl), context_die);
19598       add_AT_flag (var_die, DW_AT_external, 1);
19599       if (loc)
19600         {
19601           if (off)
19602             {
19603               /* Optimize the common case.  */
19604               if (single_element_loc_list_p (loc)
19605                   && loc->expr->dw_loc_opc == DW_OP_addr
19606                   && loc->expr->dw_loc_next == NULL
19607                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19608                 loc->expr->dw_loc_oprnd1.v.val_addr
19609                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19610               else
19611                 loc_list_plus_const (loc, off);
19612             }
19613           add_AT_location_description (var_die, DW_AT_location, loc);
19614         }
19615       else if (DECL_EXTERNAL (decl))
19616         add_AT_flag (var_die, DW_AT_declaration, 1);
19617       equate_decl_number_to_die (decl, var_die);
19618       return;
19619     }
19620
19621   /* If the compiler emitted a definition for the DECL declaration
19622      and if we already emitted a DIE for it, don't emit a second
19623      DIE for it again. Allow re-declarations of DECLs that are
19624      inside functions, though.  */
19625   if (old_die && declaration && !local_scope_p (context_die))
19626     return;
19627
19628   /* For static data members, the declaration in the class is supposed
19629      to have DW_TAG_member tag; the specification should still be
19630      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19631   if (declaration && class_scope_p (context_die))
19632     var_die = new_die (DW_TAG_member, context_die, decl);
19633   else
19634     var_die = new_die (DW_TAG_variable, context_die, decl);
19635
19636   origin_die = NULL;
19637   if (origin != NULL)
19638     origin_die = add_abstract_origin_attribute (var_die, origin);
19639
19640   /* Loop unrolling can create multiple blocks that refer to the same
19641      static variable, so we must test for the DW_AT_declaration flag.
19642
19643      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19644      copy decls and set the DECL_ABSTRACT flag on them instead of
19645      sharing them.
19646
19647      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19648
19649      ??? The declare_in_namespace support causes us to get two DIEs for one
19650      variable, both of which are declarations.  We want to avoid considering
19651      one to be a specification, so we must test that this DIE is not a
19652      declaration.  */
19653   else if (old_die && TREE_STATIC (decl) && ! declaration
19654            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19655     {
19656       /* This is a definition of a C++ class level static.  */
19657       add_AT_specification (var_die, old_die);
19658       specialization_p = true;
19659       if (DECL_NAME (decl))
19660         {
19661           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19662           struct dwarf_file_data * file_index = lookup_filename (s.file);
19663
19664           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19665             add_AT_file (var_die, DW_AT_decl_file, file_index);
19666
19667           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19668             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19669
19670           if (old_die->die_tag == DW_TAG_member)
19671             add_linkage_name (var_die, decl);
19672         }
19673     }
19674   else
19675     add_name_and_src_coords_attributes (var_die, decl);
19676
19677   if ((origin == NULL && !specialization_p)
19678       || (origin != NULL
19679           && !DECL_ABSTRACT (decl_or_origin)
19680           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19681                                        decl_function_context
19682                                                         (decl_or_origin))))
19683     {
19684       tree type = TREE_TYPE (decl_or_origin);
19685
19686       if (decl_by_reference_p (decl_or_origin))
19687         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19688       else
19689         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19690                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19691     }
19692
19693   if (origin == NULL && !specialization_p)
19694     {
19695       if (TREE_PUBLIC (decl))
19696         add_AT_flag (var_die, DW_AT_external, 1);
19697
19698       if (DECL_ARTIFICIAL (decl))
19699         add_AT_flag (var_die, DW_AT_artificial, 1);
19700
19701       add_accessibility_attribute (var_die, decl);
19702     }
19703
19704   if (declaration)
19705     add_AT_flag (var_die, DW_AT_declaration, 1);
19706
19707   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19708     equate_decl_number_to_die (decl, var_die);
19709
19710   if (! declaration
19711       && (! DECL_ABSTRACT (decl_or_origin)
19712           /* Local static vars are shared between all clones/inlines,
19713              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19714              already set.  */
19715           || (TREE_CODE (decl_or_origin) == VAR_DECL
19716               && TREE_STATIC (decl_or_origin)
19717               && DECL_RTL_SET_P (decl_or_origin)))
19718       /* When abstract origin already has DW_AT_location attribute, no need
19719          to add it again.  */
19720       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19721     {
19722       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19723           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19724         defer_location (decl_or_origin, var_die);
19725       else
19726         add_location_or_const_value_attribute (var_die,
19727                                                decl_or_origin,
19728                                                DW_AT_location);
19729       add_pubname (decl_or_origin, var_die);
19730     }
19731   else
19732     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19733 }
19734
19735 /* Generate a DIE to represent a named constant.  */
19736
19737 static void
19738 gen_const_die (tree decl, dw_die_ref context_die)
19739 {
19740   dw_die_ref const_die;
19741   tree type = TREE_TYPE (decl);
19742
19743   const_die = new_die (DW_TAG_constant, context_die, decl);
19744   add_name_and_src_coords_attributes (const_die, decl);
19745   add_type_attribute (const_die, type, 1, 0, context_die);
19746   if (TREE_PUBLIC (decl))
19747     add_AT_flag (const_die, DW_AT_external, 1);
19748   if (DECL_ARTIFICIAL (decl))
19749     add_AT_flag (const_die, DW_AT_artificial, 1);
19750   tree_add_const_value_attribute_for_decl (const_die, decl);
19751 }
19752
19753 /* Generate a DIE to represent a label identifier.  */
19754
19755 static void
19756 gen_label_die (tree decl, dw_die_ref context_die)
19757 {
19758   tree origin = decl_ultimate_origin (decl);
19759   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19760   rtx insn;
19761   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19762
19763   if (origin != NULL)
19764     add_abstract_origin_attribute (lbl_die, origin);
19765   else
19766     add_name_and_src_coords_attributes (lbl_die, decl);
19767
19768   if (DECL_ABSTRACT (decl))
19769     equate_decl_number_to_die (decl, lbl_die);
19770   else
19771     {
19772       insn = DECL_RTL_IF_SET (decl);
19773
19774       /* Deleted labels are programmer specified labels which have been
19775          eliminated because of various optimizations.  We still emit them
19776          here so that it is possible to put breakpoints on them.  */
19777       if (insn
19778           && (LABEL_P (insn)
19779               || ((NOTE_P (insn)
19780                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19781         {
19782           /* When optimization is enabled (via -O) some parts of the compiler
19783              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19784              represent source-level labels which were explicitly declared by
19785              the user.  This really shouldn't be happening though, so catch
19786              it if it ever does happen.  */
19787           gcc_assert (!INSN_DELETED_P (insn));
19788
19789           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19790           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19791         }
19792     }
19793 }
19794
19795 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19796    attributes to the DIE for a block STMT, to describe where the inlined
19797    function was called from.  This is similar to add_src_coords_attributes.  */
19798
19799 static inline void
19800 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19801 {
19802   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19803
19804   if (dwarf_version >= 3 || !dwarf_strict)
19805     {
19806       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19807       add_AT_unsigned (die, DW_AT_call_line, s.line);
19808     }
19809 }
19810
19811
19812 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19813    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19814
19815 static inline void
19816 add_high_low_attributes (tree stmt, dw_die_ref die)
19817 {
19818   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19819
19820   if (BLOCK_FRAGMENT_CHAIN (stmt)
19821       && (dwarf_version >= 3 || !dwarf_strict))
19822     {
19823       tree chain;
19824
19825       if (inlined_function_outer_scope_p (stmt))
19826         {
19827           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19828                                        BLOCK_NUMBER (stmt));
19829           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19830         }
19831
19832       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19833
19834       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19835       do
19836         {
19837           add_ranges (chain);
19838           chain = BLOCK_FRAGMENT_CHAIN (chain);
19839         }
19840       while (chain);
19841       add_ranges (NULL);
19842     }
19843   else
19844     {
19845       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19846                                    BLOCK_NUMBER (stmt));
19847       add_AT_lbl_id (die, DW_AT_low_pc, label);
19848       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19849                                    BLOCK_NUMBER (stmt));
19850       add_AT_lbl_id (die, DW_AT_high_pc, label);
19851     }
19852 }
19853
19854 /* Generate a DIE for a lexical block.  */
19855
19856 static void
19857 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19858 {
19859   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19860
19861   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19862     add_high_low_attributes (stmt, stmt_die);
19863
19864   decls_for_scope (stmt, stmt_die, depth);
19865 }
19866
19867 /* Generate a DIE for an inlined subprogram.  */
19868
19869 static void
19870 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19871 {
19872   tree decl;
19873
19874   /* The instance of function that is effectively being inlined shall not
19875      be abstract.  */
19876   gcc_assert (! BLOCK_ABSTRACT (stmt));
19877
19878   decl = block_ultimate_origin (stmt);
19879
19880   /* Emit info for the abstract instance first, if we haven't yet.  We
19881      must emit this even if the block is abstract, otherwise when we
19882      emit the block below (or elsewhere), we may end up trying to emit
19883      a die whose origin die hasn't been emitted, and crashing.  */
19884   dwarf2out_abstract_function (decl);
19885
19886   if (! BLOCK_ABSTRACT (stmt))
19887     {
19888       dw_die_ref subr_die
19889         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19890
19891       add_abstract_origin_attribute (subr_die, decl);
19892       if (TREE_ASM_WRITTEN (stmt))
19893         add_high_low_attributes (stmt, subr_die);
19894       add_call_src_coords_attributes (stmt, subr_die);
19895
19896       decls_for_scope (stmt, subr_die, depth);
19897       current_function_has_inlines = 1;
19898     }
19899 }
19900
19901 /* Generate a DIE for a field in a record, or structure.  */
19902
19903 static void
19904 gen_field_die (tree decl, dw_die_ref context_die)
19905 {
19906   dw_die_ref decl_die;
19907
19908   if (TREE_TYPE (decl) == error_mark_node)
19909     return;
19910
19911   decl_die = new_die (DW_TAG_member, context_die, decl);
19912   add_name_and_src_coords_attributes (decl_die, decl);
19913   add_type_attribute (decl_die, member_declared_type (decl),
19914                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19915                       context_die);
19916
19917   if (DECL_BIT_FIELD_TYPE (decl))
19918     {
19919       add_byte_size_attribute (decl_die, decl);
19920       add_bit_size_attribute (decl_die, decl);
19921       add_bit_offset_attribute (decl_die, decl);
19922     }
19923
19924   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19925     add_data_member_location_attribute (decl_die, decl);
19926
19927   if (DECL_ARTIFICIAL (decl))
19928     add_AT_flag (decl_die, DW_AT_artificial, 1);
19929
19930   add_accessibility_attribute (decl_die, decl);
19931
19932   /* Equate decl number to die, so that we can look up this decl later on.  */
19933   equate_decl_number_to_die (decl, decl_die);
19934 }
19935
19936 #if 0
19937 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19938    Use modified_type_die instead.
19939    We keep this code here just in case these types of DIEs may be needed to
19940    represent certain things in other languages (e.g. Pascal) someday.  */
19941
19942 static void
19943 gen_pointer_type_die (tree type, dw_die_ref context_die)
19944 {
19945   dw_die_ref ptr_die
19946     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19947
19948   equate_type_number_to_die (type, ptr_die);
19949   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19950   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19951 }
19952
19953 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19954    Use modified_type_die instead.
19955    We keep this code here just in case these types of DIEs may be needed to
19956    represent certain things in other languages (e.g. Pascal) someday.  */
19957
19958 static void
19959 gen_reference_type_die (tree type, dw_die_ref context_die)
19960 {
19961   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19962
19963   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19964     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19965   else
19966     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19967
19968   equate_type_number_to_die (type, ref_die);
19969   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19970   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19971 }
19972 #endif
19973
19974 /* Generate a DIE for a pointer to a member type.  */
19975
19976 static void
19977 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19978 {
19979   dw_die_ref ptr_die
19980     = new_die (DW_TAG_ptr_to_member_type,
19981                scope_die_for (type, context_die), type);
19982
19983   equate_type_number_to_die (type, ptr_die);
19984   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19985                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19986   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19987 }
19988
19989 /* Generate the DIE for the compilation unit.  */
19990
19991 static dw_die_ref
19992 gen_compile_unit_die (const char *filename)
19993 {
19994   dw_die_ref die;
19995   char producer[250];
19996   const char *language_string = lang_hooks.name;
19997   int language;
19998
19999   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20000
20001   if (filename)
20002     {
20003       add_name_attribute (die, filename);
20004       /* Don't add cwd for <built-in>.  */
20005       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20006         add_comp_dir_attribute (die);
20007     }
20008
20009   sprintf (producer, "%s %s", language_string, version_string);
20010
20011 #ifdef MIPS_DEBUGGING_INFO
20012   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20013      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20014      not appear in the producer string, the debugger reaches the conclusion
20015      that the object file is stripped and has no debugging information.
20016      To get the MIPS/SGI debugger to believe that there is debugging
20017      information in the object file, we add a -g to the producer string.  */
20018   if (debug_info_level > DINFO_LEVEL_TERSE)
20019     strcat (producer, " -g");
20020 #endif
20021
20022   add_AT_string (die, DW_AT_producer, producer);
20023
20024   /* If our producer is LTO try to figure out a common language to use
20025      from the global list of translation units.  */
20026   if (strcmp (language_string, "GNU GIMPLE") == 0)
20027     {
20028       unsigned i;
20029       tree t;
20030       const char *common_lang = NULL;
20031
20032       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20033         {
20034           if (!TRANSLATION_UNIT_LANGUAGE (t))
20035             continue;
20036           if (!common_lang)
20037             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20038           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20039             ;
20040           else if (strncmp (common_lang, "GNU C", 5) == 0
20041                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20042             /* Mixing C and C++ is ok, use C++ in that case.  */
20043             common_lang = "GNU C++";
20044           else
20045             {
20046               /* Fall back to C.  */
20047               common_lang = NULL;
20048               break;
20049             }
20050         }
20051
20052       if (common_lang)
20053         language_string = common_lang;
20054     }
20055
20056   language = DW_LANG_C89;
20057   if (strcmp (language_string, "GNU C++") == 0)
20058     language = DW_LANG_C_plus_plus;
20059   else if (strcmp (language_string, "GNU F77") == 0)
20060     language = DW_LANG_Fortran77;
20061   else if (strcmp (language_string, "GNU Pascal") == 0)
20062     language = DW_LANG_Pascal83;
20063   else if (dwarf_version >= 3 || !dwarf_strict)
20064     {
20065       if (strcmp (language_string, "GNU Ada") == 0)
20066         language = DW_LANG_Ada95;
20067       else if (strcmp (language_string, "GNU Fortran") == 0)
20068         language = DW_LANG_Fortran95;
20069       else if (strcmp (language_string, "GNU Java") == 0)
20070         language = DW_LANG_Java;
20071       else if (strcmp (language_string, "GNU Objective-C") == 0)
20072         language = DW_LANG_ObjC;
20073       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20074         language = DW_LANG_ObjC_plus_plus;
20075     }
20076
20077   add_AT_unsigned (die, DW_AT_language, language);
20078
20079   switch (language)
20080     {
20081     case DW_LANG_Fortran77:
20082     case DW_LANG_Fortran90:
20083     case DW_LANG_Fortran95:
20084       /* Fortran has case insensitive identifiers and the front-end
20085          lowercases everything.  */
20086       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20087       break;
20088     default:
20089       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20090       break;
20091     }
20092   return die;
20093 }
20094
20095 /* Generate the DIE for a base class.  */
20096
20097 static void
20098 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20099 {
20100   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20101
20102   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20103   add_data_member_location_attribute (die, binfo);
20104
20105   if (BINFO_VIRTUAL_P (binfo))
20106     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20107
20108   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20109      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20110      the default has always been DW_ACCESS_private.  */
20111   if (access == access_public_node)
20112     {
20113       if (dwarf_version == 2
20114           || context_die->die_tag == DW_TAG_class_type)
20115       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20116     }
20117   else if (access == access_protected_node)
20118     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20119   else if (dwarf_version > 2
20120            && context_die->die_tag != DW_TAG_class_type)
20121     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20122 }
20123
20124 /* Generate a DIE for a class member.  */
20125
20126 static void
20127 gen_member_die (tree type, dw_die_ref context_die)
20128 {
20129   tree member;
20130   tree binfo = TYPE_BINFO (type);
20131   dw_die_ref child;
20132
20133   /* If this is not an incomplete type, output descriptions of each of its
20134      members. Note that as we output the DIEs necessary to represent the
20135      members of this record or union type, we will also be trying to output
20136      DIEs to represent the *types* of those members. However the `type'
20137      function (above) will specifically avoid generating type DIEs for member
20138      types *within* the list of member DIEs for this (containing) type except
20139      for those types (of members) which are explicitly marked as also being
20140      members of this (containing) type themselves.  The g++ front- end can
20141      force any given type to be treated as a member of some other (containing)
20142      type by setting the TYPE_CONTEXT of the given (member) type to point to
20143      the TREE node representing the appropriate (containing) type.  */
20144
20145   /* First output info about the base classes.  */
20146   if (binfo)
20147     {
20148       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20149       int i;
20150       tree base;
20151
20152       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20153         gen_inheritance_die (base,
20154                              (accesses ? VEC_index (tree, accesses, i)
20155                               : access_public_node), context_die);
20156     }
20157
20158   /* Now output info about the data members and type members.  */
20159   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20160     {
20161       /* If we thought we were generating minimal debug info for TYPE
20162          and then changed our minds, some of the member declarations
20163          may have already been defined.  Don't define them again, but
20164          do put them in the right order.  */
20165
20166       child = lookup_decl_die (member);
20167       if (child)
20168         splice_child_die (context_die, child);
20169       else
20170         gen_decl_die (member, NULL, context_die);
20171     }
20172
20173   /* Now output info about the function members (if any).  */
20174   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20175     {
20176       /* Don't include clones in the member list.  */
20177       if (DECL_ABSTRACT_ORIGIN (member))
20178         continue;
20179
20180       child = lookup_decl_die (member);
20181       if (child)
20182         splice_child_die (context_die, child);
20183       else
20184         gen_decl_die (member, NULL, context_die);
20185     }
20186 }
20187
20188 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20189    is set, we pretend that the type was never defined, so we only get the
20190    member DIEs needed by later specification DIEs.  */
20191
20192 static void
20193 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20194                                 enum debug_info_usage usage)
20195 {
20196   dw_die_ref type_die = lookup_type_die (type);
20197   dw_die_ref scope_die = 0;
20198   int nested = 0;
20199   int complete = (TYPE_SIZE (type)
20200                   && (! TYPE_STUB_DECL (type)
20201                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20202   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20203   complete = complete && should_emit_struct_debug (type, usage);
20204
20205   if (type_die && ! complete)
20206     return;
20207
20208   if (TYPE_CONTEXT (type) != NULL_TREE
20209       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20210           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20211     nested = 1;
20212
20213   scope_die = scope_die_for (type, context_die);
20214
20215   if (! type_die || (nested && is_cu_die (scope_die)))
20216     /* First occurrence of type or toplevel definition of nested class.  */
20217     {
20218       dw_die_ref old_die = type_die;
20219
20220       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20221                           ? record_type_tag (type) : DW_TAG_union_type,
20222                           scope_die, type);
20223       equate_type_number_to_die (type, type_die);
20224       if (old_die)
20225         add_AT_specification (type_die, old_die);
20226       else
20227         add_name_attribute (type_die, type_tag (type));
20228     }
20229   else
20230     remove_AT (type_die, DW_AT_declaration);
20231
20232   /* Generate child dies for template paramaters.  */
20233   if (debug_info_level > DINFO_LEVEL_TERSE
20234       && COMPLETE_TYPE_P (type))
20235     schedule_generic_params_dies_gen (type);
20236
20237   /* If this type has been completed, then give it a byte_size attribute and
20238      then give a list of members.  */
20239   if (complete && !ns_decl)
20240     {
20241       /* Prevent infinite recursion in cases where the type of some member of
20242          this type is expressed in terms of this type itself.  */
20243       TREE_ASM_WRITTEN (type) = 1;
20244       add_byte_size_attribute (type_die, type);
20245       if (TYPE_STUB_DECL (type) != NULL_TREE)
20246         {
20247           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20248           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20249         }
20250
20251       /* If the first reference to this type was as the return type of an
20252          inline function, then it may not have a parent.  Fix this now.  */
20253       if (type_die->die_parent == NULL)
20254         add_child_die (scope_die, type_die);
20255
20256       push_decl_scope (type);
20257       gen_member_die (type, type_die);
20258       pop_decl_scope ();
20259
20260       /* GNU extension: Record what type our vtable lives in.  */
20261       if (TYPE_VFIELD (type))
20262         {
20263           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20264
20265           gen_type_die (vtype, context_die);
20266           add_AT_die_ref (type_die, DW_AT_containing_type,
20267                           lookup_type_die (vtype));
20268         }
20269     }
20270   else
20271     {
20272       add_AT_flag (type_die, DW_AT_declaration, 1);
20273
20274       /* We don't need to do this for function-local types.  */
20275       if (TYPE_STUB_DECL (type)
20276           && ! decl_function_context (TYPE_STUB_DECL (type)))
20277         VEC_safe_push (tree, gc, incomplete_types, type);
20278     }
20279
20280   if (get_AT (type_die, DW_AT_name))
20281     add_pubtype (type, type_die);
20282 }
20283
20284 /* Generate a DIE for a subroutine _type_.  */
20285
20286 static void
20287 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20288 {
20289   tree return_type = TREE_TYPE (type);
20290   dw_die_ref subr_die
20291     = new_die (DW_TAG_subroutine_type,
20292                scope_die_for (type, context_die), type);
20293
20294   equate_type_number_to_die (type, subr_die);
20295   add_prototyped_attribute (subr_die, type);
20296   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20297   gen_formal_types_die (type, subr_die);
20298
20299   if (get_AT (subr_die, DW_AT_name))
20300     add_pubtype (type, subr_die);
20301 }
20302
20303 /* Generate a DIE for a type definition.  */
20304
20305 static void
20306 gen_typedef_die (tree decl, dw_die_ref context_die)
20307 {
20308   dw_die_ref type_die;
20309   tree origin;
20310
20311   if (TREE_ASM_WRITTEN (decl))
20312     return;
20313
20314   TREE_ASM_WRITTEN (decl) = 1;
20315   type_die = new_die (DW_TAG_typedef, context_die, decl);
20316   origin = decl_ultimate_origin (decl);
20317   if (origin != NULL)
20318     add_abstract_origin_attribute (type_die, origin);
20319   else
20320     {
20321       tree type;
20322
20323       add_name_and_src_coords_attributes (type_die, decl);
20324       if (DECL_ORIGINAL_TYPE (decl))
20325         {
20326           type = DECL_ORIGINAL_TYPE (decl);
20327
20328           gcc_assert (type != TREE_TYPE (decl));
20329           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20330         }
20331       else
20332         {
20333           type = TREE_TYPE (decl);
20334
20335           if (is_naming_typedef_decl (TYPE_NAME (type)))
20336             {
20337               /* Here, we are in the case of decl being a typedef naming
20338                  an anonymous type, e.g:
20339                      typedef struct {...} foo;
20340                  In that case TREE_TYPE (decl) is not a typedef variant
20341                  type and TYPE_NAME of the anonymous type is set to the
20342                  TYPE_DECL of the typedef. This construct is emitted by
20343                  the C++ FE.
20344
20345                  TYPE is the anonymous struct named by the typedef
20346                  DECL. As we need the DW_AT_type attribute of the
20347                  DW_TAG_typedef to point to the DIE of TYPE, let's
20348                  generate that DIE right away. add_type_attribute
20349                  called below will then pick (via lookup_type_die) that
20350                  anonymous struct DIE.  */
20351               if (!TREE_ASM_WRITTEN (type))
20352                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20353             }
20354         }
20355
20356       add_type_attribute (type_die, type, TREE_READONLY (decl),
20357                           TREE_THIS_VOLATILE (decl), context_die);
20358
20359       if (is_naming_typedef_decl (decl))
20360         /* We want that all subsequent calls to lookup_type_die with
20361            TYPE in argument yield the DW_TAG_typedef we have just
20362            created.  */
20363         equate_type_number_to_die (type, type_die);
20364
20365       add_accessibility_attribute (type_die, decl);
20366     }
20367
20368   if (DECL_ABSTRACT (decl))
20369     equate_decl_number_to_die (decl, type_die);
20370
20371   if (get_AT (type_die, DW_AT_name))
20372     add_pubtype (decl, type_die);
20373 }
20374
20375 /* Generate a DIE for a struct, class, enum or union type.  */
20376
20377 static void
20378 gen_tagged_type_die (tree type,
20379                      dw_die_ref context_die,
20380                      enum debug_info_usage usage)
20381 {
20382   int need_pop;
20383
20384   if (type == NULL_TREE
20385       || !is_tagged_type (type))
20386     return;
20387
20388   /* If this is a nested type whose containing class hasn't been written
20389      out yet, writing it out will cover this one, too.  This does not apply
20390      to instantiations of member class templates; they need to be added to
20391      the containing class as they are generated.  FIXME: This hurts the
20392      idea of combining type decls from multiple TUs, since we can't predict
20393      what set of template instantiations we'll get.  */
20394   if (TYPE_CONTEXT (type)
20395       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20396       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20397     {
20398       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20399
20400       if (TREE_ASM_WRITTEN (type))
20401         return;
20402
20403       /* If that failed, attach ourselves to the stub.  */
20404       push_decl_scope (TYPE_CONTEXT (type));
20405       context_die = lookup_type_die (TYPE_CONTEXT (type));
20406       need_pop = 1;
20407     }
20408   else if (TYPE_CONTEXT (type) != NULL_TREE
20409            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20410     {
20411       /* If this type is local to a function that hasn't been written
20412          out yet, use a NULL context for now; it will be fixed up in
20413          decls_for_scope.  */
20414       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20415       /* A declaration DIE doesn't count; nested types need to go in the
20416          specification.  */
20417       if (context_die && is_declaration_die (context_die))
20418         context_die = NULL;
20419       need_pop = 0;
20420     }
20421   else
20422     {
20423       context_die = declare_in_namespace (type, context_die);
20424       need_pop = 0;
20425     }
20426
20427   if (TREE_CODE (type) == ENUMERAL_TYPE)
20428     {
20429       /* This might have been written out by the call to
20430          declare_in_namespace.  */
20431       if (!TREE_ASM_WRITTEN (type))
20432         gen_enumeration_type_die (type, context_die);
20433     }
20434   else
20435     gen_struct_or_union_type_die (type, context_die, usage);
20436
20437   if (need_pop)
20438     pop_decl_scope ();
20439
20440   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20441      it up if it is ever completed.  gen_*_type_die will set it for us
20442      when appropriate.  */
20443 }
20444
20445 /* Generate a type description DIE.  */
20446
20447 static void
20448 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20449                          enum debug_info_usage usage)
20450 {
20451   struct array_descr_info info;
20452
20453   if (type == NULL_TREE || type == error_mark_node)
20454     return;
20455
20456   if (TYPE_NAME (type) != NULL_TREE
20457       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20458       && is_redundant_typedef (TYPE_NAME (type))
20459       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20460     /* The DECL of this type is a typedef we don't want to emit debug
20461        info for but we want debug info for its underlying typedef.
20462        This can happen for e.g, the injected-class-name of a C++
20463        type.  */
20464     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20465
20466   /* If TYPE is a typedef type variant, let's generate debug info
20467      for the parent typedef which TYPE is a type of.  */
20468   if (typedef_variant_p (type))
20469     {
20470       if (TREE_ASM_WRITTEN (type))
20471         return;
20472
20473       /* Prevent broken recursion; we can't hand off to the same type.  */
20474       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20475
20476       /* Use the DIE of the containing namespace as the parent DIE of
20477          the type description DIE we want to generate.  */
20478       if (DECL_CONTEXT (TYPE_NAME (type))
20479           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20480         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20481
20482       TREE_ASM_WRITTEN (type) = 1;
20483
20484       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20485       return;
20486     }
20487
20488   /* If type is an anonymous tagged type named by a typedef, let's
20489      generate debug info for the typedef.  */
20490   if (is_naming_typedef_decl (TYPE_NAME (type)))
20491     {
20492       /* Use the DIE of the containing namespace as the parent DIE of
20493          the type description DIE we want to generate.  */
20494       if (DECL_CONTEXT (TYPE_NAME (type))
20495           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20496         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20497       
20498       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20499       return;
20500     }
20501
20502   /* If this is an array type with hidden descriptor, handle it first.  */
20503   if (!TREE_ASM_WRITTEN (type)
20504       && lang_hooks.types.get_array_descr_info
20505       && lang_hooks.types.get_array_descr_info (type, &info)
20506       && (dwarf_version >= 3 || !dwarf_strict))
20507     {
20508       gen_descr_array_type_die (type, &info, context_die);
20509       TREE_ASM_WRITTEN (type) = 1;
20510       return;
20511     }
20512
20513   /* We are going to output a DIE to represent the unqualified version
20514      of this type (i.e. without any const or volatile qualifiers) so
20515      get the main variant (i.e. the unqualified version) of this type
20516      now.  (Vectors are special because the debugging info is in the
20517      cloned type itself).  */
20518   if (TREE_CODE (type) != VECTOR_TYPE)
20519     type = type_main_variant (type);
20520
20521   if (TREE_ASM_WRITTEN (type))
20522     return;
20523
20524   switch (TREE_CODE (type))
20525     {
20526     case ERROR_MARK:
20527       break;
20528
20529     case POINTER_TYPE:
20530     case REFERENCE_TYPE:
20531       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20532          ensures that the gen_type_die recursion will terminate even if the
20533          type is recursive.  Recursive types are possible in Ada.  */
20534       /* ??? We could perhaps do this for all types before the switch
20535          statement.  */
20536       TREE_ASM_WRITTEN (type) = 1;
20537
20538       /* For these types, all that is required is that we output a DIE (or a
20539          set of DIEs) to represent the "basis" type.  */
20540       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20541                                 DINFO_USAGE_IND_USE);
20542       break;
20543
20544     case OFFSET_TYPE:
20545       /* This code is used for C++ pointer-to-data-member types.
20546          Output a description of the relevant class type.  */
20547       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20548                                         DINFO_USAGE_IND_USE);
20549
20550       /* Output a description of the type of the object pointed to.  */
20551       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20552                                         DINFO_USAGE_IND_USE);
20553
20554       /* Now output a DIE to represent this pointer-to-data-member type
20555          itself.  */
20556       gen_ptr_to_mbr_type_die (type, context_die);
20557       break;
20558
20559     case FUNCTION_TYPE:
20560       /* Force out return type (in case it wasn't forced out already).  */
20561       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20562                                         DINFO_USAGE_DIR_USE);
20563       gen_subroutine_type_die (type, context_die);
20564       break;
20565
20566     case METHOD_TYPE:
20567       /* Force out return type (in case it wasn't forced out already).  */
20568       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20569                                         DINFO_USAGE_DIR_USE);
20570       gen_subroutine_type_die (type, context_die);
20571       break;
20572
20573     case ARRAY_TYPE:
20574       gen_array_type_die (type, context_die);
20575       break;
20576
20577     case VECTOR_TYPE:
20578       gen_array_type_die (type, context_die);
20579       break;
20580
20581     case ENUMERAL_TYPE:
20582     case RECORD_TYPE:
20583     case UNION_TYPE:
20584     case QUAL_UNION_TYPE:
20585       gen_tagged_type_die (type, context_die, usage);
20586       return;
20587
20588     case VOID_TYPE:
20589     case INTEGER_TYPE:
20590     case REAL_TYPE:
20591     case FIXED_POINT_TYPE:
20592     case COMPLEX_TYPE:
20593     case BOOLEAN_TYPE:
20594       /* No DIEs needed for fundamental types.  */
20595       break;
20596
20597     case NULLPTR_TYPE:
20598     case LANG_TYPE:
20599       /* Just use DW_TAG_unspecified_type.  */
20600       {
20601         dw_die_ref type_die = lookup_type_die (type);
20602         if (type_die == NULL)
20603           {
20604             tree name = TYPE_NAME (type);
20605             if (TREE_CODE (name) == TYPE_DECL)
20606               name = DECL_NAME (name);
20607             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20608             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20609             equate_type_number_to_die (type, type_die);
20610           }
20611       }
20612       break;
20613
20614     default:
20615       gcc_unreachable ();
20616     }
20617
20618   TREE_ASM_WRITTEN (type) = 1;
20619 }
20620
20621 static void
20622 gen_type_die (tree type, dw_die_ref context_die)
20623 {
20624   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20625 }
20626
20627 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20628    things which are local to the given block.  */
20629
20630 static void
20631 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20632 {
20633   int must_output_die = 0;
20634   bool inlined_func;
20635
20636   /* Ignore blocks that are NULL.  */
20637   if (stmt == NULL_TREE)
20638     return;
20639
20640   inlined_func = inlined_function_outer_scope_p (stmt);
20641
20642   /* If the block is one fragment of a non-contiguous block, do not
20643      process the variables, since they will have been done by the
20644      origin block.  Do process subblocks.  */
20645   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20646     {
20647       tree sub;
20648
20649       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20650         gen_block_die (sub, context_die, depth + 1);
20651
20652       return;
20653     }
20654
20655   /* Determine if we need to output any Dwarf DIEs at all to represent this
20656      block.  */
20657   if (inlined_func)
20658     /* The outer scopes for inlinings *must* always be represented.  We
20659        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20660     must_output_die = 1;
20661   else
20662     {
20663       /* Determine if this block directly contains any "significant"
20664          local declarations which we will need to output DIEs for.  */
20665       if (debug_info_level > DINFO_LEVEL_TERSE)
20666         /* We are not in terse mode so *any* local declaration counts
20667            as being a "significant" one.  */
20668         must_output_die = ((BLOCK_VARS (stmt) != NULL
20669                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20670                            && (TREE_USED (stmt)
20671                                || TREE_ASM_WRITTEN (stmt)
20672                                || BLOCK_ABSTRACT (stmt)));
20673       else if ((TREE_USED (stmt)
20674                 || TREE_ASM_WRITTEN (stmt)
20675                 || BLOCK_ABSTRACT (stmt))
20676                && !dwarf2out_ignore_block (stmt))
20677         must_output_die = 1;
20678     }
20679
20680   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20681      DIE for any block which contains no significant local declarations at
20682      all.  Rather, in such cases we just call `decls_for_scope' so that any
20683      needed Dwarf info for any sub-blocks will get properly generated. Note
20684      that in terse mode, our definition of what constitutes a "significant"
20685      local declaration gets restricted to include only inlined function
20686      instances and local (nested) function definitions.  */
20687   if (must_output_die)
20688     {
20689       if (inlined_func)
20690         {
20691           /* If STMT block is abstract, that means we have been called
20692              indirectly from dwarf2out_abstract_function.
20693              That function rightfully marks the descendent blocks (of
20694              the abstract function it is dealing with) as being abstract,
20695              precisely to prevent us from emitting any
20696              DW_TAG_inlined_subroutine DIE as a descendent
20697              of an abstract function instance. So in that case, we should
20698              not call gen_inlined_subroutine_die.
20699
20700              Later though, when cgraph asks dwarf2out to emit info
20701              for the concrete instance of the function decl into which
20702              the concrete instance of STMT got inlined, the later will lead
20703              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20704           if (! BLOCK_ABSTRACT (stmt))
20705             gen_inlined_subroutine_die (stmt, context_die, depth);
20706         }
20707       else
20708         gen_lexical_block_die (stmt, context_die, depth);
20709     }
20710   else
20711     decls_for_scope (stmt, context_die, depth);
20712 }
20713
20714 /* Process variable DECL (or variable with origin ORIGIN) within
20715    block STMT and add it to CONTEXT_DIE.  */
20716 static void
20717 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20718 {
20719   dw_die_ref die;
20720   tree decl_or_origin = decl ? decl : origin;
20721
20722   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20723     die = lookup_decl_die (decl_or_origin);
20724   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20725            && TYPE_DECL_IS_STUB (decl_or_origin))
20726     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20727   else
20728     die = NULL;
20729
20730   if (die != NULL && die->die_parent == NULL)
20731     add_child_die (context_die, die);
20732   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20733     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20734                                          stmt, context_die);
20735   else
20736     gen_decl_die (decl, origin, context_die);
20737 }
20738
20739 /* Generate all of the decls declared within a given scope and (recursively)
20740    all of its sub-blocks.  */
20741
20742 static void
20743 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20744 {
20745   tree decl;
20746   unsigned int i;
20747   tree subblocks;
20748
20749   /* Ignore NULL blocks.  */
20750   if (stmt == NULL_TREE)
20751     return;
20752
20753   /* Output the DIEs to represent all of the data objects and typedefs
20754      declared directly within this block but not within any nested
20755      sub-blocks.  Also, nested function and tag DIEs have been
20756      generated with a parent of NULL; fix that up now.  */
20757   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20758     process_scope_var (stmt, decl, NULL_TREE, context_die);
20759   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20760     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20761                        context_die);
20762
20763   /* If we're at -g1, we're not interested in subblocks.  */
20764   if (debug_info_level <= DINFO_LEVEL_TERSE)
20765     return;
20766
20767   /* Output the DIEs to represent all sub-blocks (and the items declared
20768      therein) of this block.  */
20769   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20770        subblocks != NULL;
20771        subblocks = BLOCK_CHAIN (subblocks))
20772     gen_block_die (subblocks, context_die, depth + 1);
20773 }
20774
20775 /* Is this a typedef we can avoid emitting?  */
20776
20777 static inline int
20778 is_redundant_typedef (const_tree decl)
20779 {
20780   if (TYPE_DECL_IS_STUB (decl))
20781     return 1;
20782
20783   if (DECL_ARTIFICIAL (decl)
20784       && DECL_CONTEXT (decl)
20785       && is_tagged_type (DECL_CONTEXT (decl))
20786       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20787       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20788     /* Also ignore the artificial member typedef for the class name.  */
20789     return 1;
20790
20791   return 0;
20792 }
20793
20794 /* Return TRUE if TYPE is a typedef that names a type for linkage
20795    purposes. This kind of typedefs is produced by the C++ FE for
20796    constructs like:
20797
20798    typedef struct {...} foo;
20799
20800    In that case, there is no typedef variant type produced for foo.
20801    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20802    struct type.  */
20803
20804 static bool
20805 is_naming_typedef_decl (const_tree decl)
20806 {
20807   if (decl == NULL_TREE
20808       || TREE_CODE (decl) != TYPE_DECL
20809       || !is_tagged_type (TREE_TYPE (decl))
20810       || DECL_IS_BUILTIN (decl)
20811       || is_redundant_typedef (decl)
20812       /* It looks like Ada produces TYPE_DECLs that are very similar
20813          to C++ naming typedefs but that have different
20814          semantics. Let's be specific to c++ for now.  */
20815       || !is_cxx ())
20816     return FALSE;
20817
20818   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20819           && TYPE_NAME (TREE_TYPE (decl)) == decl
20820           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20821               != TYPE_NAME (TREE_TYPE (decl))));
20822 }
20823
20824 /* Returns the DIE for a context.  */
20825
20826 static inline dw_die_ref
20827 get_context_die (tree context)
20828 {
20829   if (context)
20830     {
20831       /* Find die that represents this context.  */
20832       if (TYPE_P (context))
20833         return force_type_die (TYPE_MAIN_VARIANT (context));
20834       else
20835         return force_decl_die (context);
20836     }
20837   return comp_unit_die ();
20838 }
20839
20840 /* Returns the DIE for decl.  A DIE will always be returned.  */
20841
20842 static dw_die_ref
20843 force_decl_die (tree decl)
20844 {
20845   dw_die_ref decl_die;
20846   unsigned saved_external_flag;
20847   tree save_fn = NULL_TREE;
20848   decl_die = lookup_decl_die (decl);
20849   if (!decl_die)
20850     {
20851       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20852
20853       decl_die = lookup_decl_die (decl);
20854       if (decl_die)
20855         return decl_die;
20856
20857       switch (TREE_CODE (decl))
20858         {
20859         case FUNCTION_DECL:
20860           /* Clear current_function_decl, so that gen_subprogram_die thinks
20861              that this is a declaration. At this point, we just want to force
20862              declaration die.  */
20863           save_fn = current_function_decl;
20864           current_function_decl = NULL_TREE;
20865           gen_subprogram_die (decl, context_die);
20866           current_function_decl = save_fn;
20867           break;
20868
20869         case VAR_DECL:
20870           /* Set external flag to force declaration die. Restore it after
20871            gen_decl_die() call.  */
20872           saved_external_flag = DECL_EXTERNAL (decl);
20873           DECL_EXTERNAL (decl) = 1;
20874           gen_decl_die (decl, NULL, context_die);
20875           DECL_EXTERNAL (decl) = saved_external_flag;
20876           break;
20877
20878         case NAMESPACE_DECL:
20879           if (dwarf_version >= 3 || !dwarf_strict)
20880             dwarf2out_decl (decl);
20881           else
20882             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20883             decl_die = comp_unit_die ();
20884           break;
20885
20886         case TRANSLATION_UNIT_DECL:
20887           decl_die = comp_unit_die ();
20888           break;
20889
20890         default:
20891           gcc_unreachable ();
20892         }
20893
20894       /* We should be able to find the DIE now.  */
20895       if (!decl_die)
20896         decl_die = lookup_decl_die (decl);
20897       gcc_assert (decl_die);
20898     }
20899
20900   return decl_die;
20901 }
20902
20903 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20904    always returned.  */
20905
20906 static dw_die_ref
20907 force_type_die (tree type)
20908 {
20909   dw_die_ref type_die;
20910
20911   type_die = lookup_type_die (type);
20912   if (!type_die)
20913     {
20914       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20915
20916       type_die = modified_type_die (type, TYPE_READONLY (type),
20917                                     TYPE_VOLATILE (type), context_die);
20918       gcc_assert (type_die);
20919     }
20920   return type_die;
20921 }
20922
20923 /* Force out any required namespaces to be able to output DECL,
20924    and return the new context_die for it, if it's changed.  */
20925
20926 static dw_die_ref
20927 setup_namespace_context (tree thing, dw_die_ref context_die)
20928 {
20929   tree context = (DECL_P (thing)
20930                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20931   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20932     /* Force out the namespace.  */
20933     context_die = force_decl_die (context);
20934
20935   return context_die;
20936 }
20937
20938 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20939    type) within its namespace, if appropriate.
20940
20941    For compatibility with older debuggers, namespace DIEs only contain
20942    declarations; all definitions are emitted at CU scope.  */
20943
20944 static dw_die_ref
20945 declare_in_namespace (tree thing, dw_die_ref context_die)
20946 {
20947   dw_die_ref ns_context;
20948
20949   if (debug_info_level <= DINFO_LEVEL_TERSE)
20950     return context_die;
20951
20952   /* If this decl is from an inlined function, then don't try to emit it in its
20953      namespace, as we will get confused.  It would have already been emitted
20954      when the abstract instance of the inline function was emitted anyways.  */
20955   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20956     return context_die;
20957
20958   ns_context = setup_namespace_context (thing, context_die);
20959
20960   if (ns_context != context_die)
20961     {
20962       if (is_fortran ())
20963         return ns_context;
20964       if (DECL_P (thing))
20965         gen_decl_die (thing, NULL, ns_context);
20966       else
20967         gen_type_die (thing, ns_context);
20968     }
20969   return context_die;
20970 }
20971
20972 /* Generate a DIE for a namespace or namespace alias.  */
20973
20974 static void
20975 gen_namespace_die (tree decl, dw_die_ref context_die)
20976 {
20977   dw_die_ref namespace_die;
20978
20979   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20980      they are an alias of.  */
20981   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20982     {
20983       /* Output a real namespace or module.  */
20984       context_die = setup_namespace_context (decl, comp_unit_die ());
20985       namespace_die = new_die (is_fortran ()
20986                                ? DW_TAG_module : DW_TAG_namespace,
20987                                context_die, decl);
20988       /* For Fortran modules defined in different CU don't add src coords.  */
20989       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20990         {
20991           const char *name = dwarf2_name (decl, 0);
20992           if (name)
20993             add_name_attribute (namespace_die, name);
20994         }
20995       else
20996         add_name_and_src_coords_attributes (namespace_die, decl);
20997       if (DECL_EXTERNAL (decl))
20998         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20999       equate_decl_number_to_die (decl, namespace_die);
21000     }
21001   else
21002     {
21003       /* Output a namespace alias.  */
21004
21005       /* Force out the namespace we are an alias of, if necessary.  */
21006       dw_die_ref origin_die
21007         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21008
21009       if (DECL_FILE_SCOPE_P (decl)
21010           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21011         context_die = setup_namespace_context (decl, comp_unit_die ());
21012       /* Now create the namespace alias DIE.  */
21013       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21014       add_name_and_src_coords_attributes (namespace_die, decl);
21015       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21016       equate_decl_number_to_die (decl, namespace_die);
21017     }
21018 }
21019
21020 /* Generate Dwarf debug information for a decl described by DECL.
21021    The return value is currently only meaningful for PARM_DECLs,
21022    for all other decls it returns NULL.  */
21023
21024 static dw_die_ref
21025 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21026 {
21027   tree decl_or_origin = decl ? decl : origin;
21028   tree class_origin = NULL, ultimate_origin;
21029
21030   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21031     return NULL;
21032
21033   switch (TREE_CODE (decl_or_origin))
21034     {
21035     case ERROR_MARK:
21036       break;
21037
21038     case CONST_DECL:
21039       if (!is_fortran () && !is_ada ())
21040         {
21041           /* The individual enumerators of an enum type get output when we output
21042              the Dwarf representation of the relevant enum type itself.  */
21043           break;
21044         }
21045
21046       /* Emit its type.  */
21047       gen_type_die (TREE_TYPE (decl), context_die);
21048
21049       /* And its containing namespace.  */
21050       context_die = declare_in_namespace (decl, context_die);
21051
21052       gen_const_die (decl, context_die);
21053       break;
21054
21055     case FUNCTION_DECL:
21056       /* Don't output any DIEs to represent mere function declarations,
21057          unless they are class members or explicit block externs.  */
21058       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21059           && DECL_FILE_SCOPE_P (decl_or_origin)
21060           && (current_function_decl == NULL_TREE
21061               || DECL_ARTIFICIAL (decl_or_origin)))
21062         break;
21063
21064 #if 0
21065       /* FIXME */
21066       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21067          on local redeclarations of global functions.  That seems broken.  */
21068       if (current_function_decl != decl)
21069         /* This is only a declaration.  */;
21070 #endif
21071
21072       /* If we're emitting a clone, emit info for the abstract instance.  */
21073       if (origin || DECL_ORIGIN (decl) != decl)
21074         dwarf2out_abstract_function (origin
21075                                      ? DECL_ORIGIN (origin)
21076                                      : DECL_ABSTRACT_ORIGIN (decl));
21077
21078       /* If we're emitting an out-of-line copy of an inline function,
21079          emit info for the abstract instance and set up to refer to it.  */
21080       else if (cgraph_function_possibly_inlined_p (decl)
21081                && ! DECL_ABSTRACT (decl)
21082                && ! class_or_namespace_scope_p (context_die)
21083                /* dwarf2out_abstract_function won't emit a die if this is just
21084                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21085                   that case, because that works only if we have a die.  */
21086                && DECL_INITIAL (decl) != NULL_TREE)
21087         {
21088           dwarf2out_abstract_function (decl);
21089           set_decl_origin_self (decl);
21090         }
21091
21092       /* Otherwise we're emitting the primary DIE for this decl.  */
21093       else if (debug_info_level > DINFO_LEVEL_TERSE)
21094         {
21095           /* Before we describe the FUNCTION_DECL itself, make sure that we
21096              have its containing type.  */
21097           if (!origin)
21098             origin = decl_class_context (decl);
21099           if (origin != NULL_TREE)
21100             gen_type_die (origin, context_die);
21101
21102           /* And its return type.  */
21103           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21104
21105           /* And its virtual context.  */
21106           if (DECL_VINDEX (decl) != NULL_TREE)
21107             gen_type_die (DECL_CONTEXT (decl), context_die);
21108
21109           /* Make sure we have a member DIE for decl.  */
21110           if (origin != NULL_TREE)
21111             gen_type_die_for_member (origin, decl, context_die);
21112
21113           /* And its containing namespace.  */
21114           context_die = declare_in_namespace (decl, context_die);
21115         }
21116
21117       /* Now output a DIE to represent the function itself.  */
21118       if (decl)
21119         gen_subprogram_die (decl, context_die);
21120       break;
21121
21122     case TYPE_DECL:
21123       /* If we are in terse mode, don't generate any DIEs to represent any
21124          actual typedefs.  */
21125       if (debug_info_level <= DINFO_LEVEL_TERSE)
21126         break;
21127
21128       /* In the special case of a TYPE_DECL node representing the declaration
21129          of some type tag, if the given TYPE_DECL is marked as having been
21130          instantiated from some other (original) TYPE_DECL node (e.g. one which
21131          was generated within the original definition of an inline function) we
21132          used to generate a special (abbreviated) DW_TAG_structure_type,
21133          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21134          should be actually referencing those DIEs, as variable DIEs with that
21135          type would be emitted already in the abstract origin, so it was always
21136          removed during unused type prunning.  Don't add anything in this
21137          case.  */
21138       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21139         break;
21140
21141       if (is_redundant_typedef (decl))
21142         gen_type_die (TREE_TYPE (decl), context_die);
21143       else
21144         /* Output a DIE to represent the typedef itself.  */
21145         gen_typedef_die (decl, context_die);
21146       break;
21147
21148     case LABEL_DECL:
21149       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21150         gen_label_die (decl, context_die);
21151       break;
21152
21153     case VAR_DECL:
21154     case RESULT_DECL:
21155       /* If we are in terse mode, don't generate any DIEs to represent any
21156          variable declarations or definitions.  */
21157       if (debug_info_level <= DINFO_LEVEL_TERSE)
21158         break;
21159
21160       /* Output any DIEs that are needed to specify the type of this data
21161          object.  */
21162       if (decl_by_reference_p (decl_or_origin))
21163         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21164       else
21165         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21166
21167       /* And its containing type.  */
21168       class_origin = decl_class_context (decl_or_origin);
21169       if (class_origin != NULL_TREE)
21170         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21171
21172       /* And its containing namespace.  */
21173       context_die = declare_in_namespace (decl_or_origin, context_die);
21174
21175       /* Now output the DIE to represent the data object itself.  This gets
21176          complicated because of the possibility that the VAR_DECL really
21177          represents an inlined instance of a formal parameter for an inline
21178          function.  */
21179       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21180       if (ultimate_origin != NULL_TREE
21181           && TREE_CODE (ultimate_origin) == PARM_DECL)
21182         gen_formal_parameter_die (decl, origin,
21183                                   true /* Emit name attribute.  */,
21184                                   context_die);
21185       else
21186         gen_variable_die (decl, origin, context_die);
21187       break;
21188
21189     case FIELD_DECL:
21190       /* Ignore the nameless fields that are used to skip bits but handle C++
21191          anonymous unions and structs.  */
21192       if (DECL_NAME (decl) != NULL_TREE
21193           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21194           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21195         {
21196           gen_type_die (member_declared_type (decl), context_die);
21197           gen_field_die (decl, context_die);
21198         }
21199       break;
21200
21201     case PARM_DECL:
21202       if (DECL_BY_REFERENCE (decl_or_origin))
21203         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21204       else
21205         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21206       return gen_formal_parameter_die (decl, origin,
21207                                        true /* Emit name attribute.  */,
21208                                        context_die);
21209
21210     case NAMESPACE_DECL:
21211     case IMPORTED_DECL:
21212       if (dwarf_version >= 3 || !dwarf_strict)
21213         gen_namespace_die (decl, context_die);
21214       break;
21215
21216     default:
21217       /* Probably some frontend-internal decl.  Assume we don't care.  */
21218       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21219       break;
21220     }
21221
21222   return NULL;
21223 }
21224 \f
21225 /* Output debug information for global decl DECL.  Called from toplev.c after
21226    compilation proper has finished.  */
21227
21228 static void
21229 dwarf2out_global_decl (tree decl)
21230 {
21231   /* Output DWARF2 information for file-scope tentative data object
21232      declarations, file-scope (extern) function declarations (which
21233      had no corresponding body) and file-scope tagged type declarations
21234      and definitions which have not yet been forced out.  */
21235   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21236     dwarf2out_decl (decl);
21237 }
21238
21239 /* Output debug information for type decl DECL.  Called from toplev.c
21240    and from language front ends (to record built-in types).  */
21241 static void
21242 dwarf2out_type_decl (tree decl, int local)
21243 {
21244   if (!local)
21245     dwarf2out_decl (decl);
21246 }
21247
21248 /* Output debug information for imported module or decl DECL.
21249    NAME is non-NULL name in the lexical block if the decl has been renamed.
21250    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21251    that DECL belongs to.
21252    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21253 static void
21254 dwarf2out_imported_module_or_decl_1 (tree decl,
21255                                      tree name,
21256                                      tree lexical_block,
21257                                      dw_die_ref lexical_block_die)
21258 {
21259   expanded_location xloc;
21260   dw_die_ref imported_die = NULL;
21261   dw_die_ref at_import_die;
21262
21263   if (TREE_CODE (decl) == IMPORTED_DECL)
21264     {
21265       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21266       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21267       gcc_assert (decl);
21268     }
21269   else
21270     xloc = expand_location (input_location);
21271
21272   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21273     {
21274       at_import_die = force_type_die (TREE_TYPE (decl));
21275       /* For namespace N { typedef void T; } using N::T; base_type_die
21276          returns NULL, but DW_TAG_imported_declaration requires
21277          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21278       if (!at_import_die)
21279         {
21280           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21281           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21282           at_import_die = lookup_type_die (TREE_TYPE (decl));
21283           gcc_assert (at_import_die);
21284         }
21285     }
21286   else
21287     {
21288       at_import_die = lookup_decl_die (decl);
21289       if (!at_import_die)
21290         {
21291           /* If we're trying to avoid duplicate debug info, we may not have
21292              emitted the member decl for this field.  Emit it now.  */
21293           if (TREE_CODE (decl) == FIELD_DECL)
21294             {
21295               tree type = DECL_CONTEXT (decl);
21296
21297               if (TYPE_CONTEXT (type)
21298                   && TYPE_P (TYPE_CONTEXT (type))
21299                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21300                                                 DINFO_USAGE_DIR_USE))
21301                 return;
21302               gen_type_die_for_member (type, decl,
21303                                        get_context_die (TYPE_CONTEXT (type)));
21304             }
21305           at_import_die = force_decl_die (decl);
21306         }
21307     }
21308
21309   if (TREE_CODE (decl) == NAMESPACE_DECL)
21310     {
21311       if (dwarf_version >= 3 || !dwarf_strict)
21312         imported_die = new_die (DW_TAG_imported_module,
21313                                 lexical_block_die,
21314                                 lexical_block);
21315       else
21316         return;
21317     }
21318   else
21319     imported_die = new_die (DW_TAG_imported_declaration,
21320                             lexical_block_die,
21321                             lexical_block);
21322
21323   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21324   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21325   if (name)
21326     add_AT_string (imported_die, DW_AT_name,
21327                    IDENTIFIER_POINTER (name));
21328   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21329 }
21330
21331 /* Output debug information for imported module or decl DECL.
21332    NAME is non-NULL name in context if the decl has been renamed.
21333    CHILD is true if decl is one of the renamed decls as part of
21334    importing whole module.  */
21335
21336 static void
21337 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21338                                    bool child)
21339 {
21340   /* dw_die_ref at_import_die;  */
21341   dw_die_ref scope_die;
21342
21343   if (debug_info_level <= DINFO_LEVEL_TERSE)
21344     return;
21345
21346   gcc_assert (decl);
21347
21348   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21349      We need decl DIE for reference and scope die. First, get DIE for the decl
21350      itself.  */
21351
21352   /* Get the scope die for decl context. Use comp_unit_die for global module
21353      or decl. If die is not found for non globals, force new die.  */
21354   if (context
21355       && TYPE_P (context)
21356       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21357     return;
21358
21359   if (!(dwarf_version >= 3 || !dwarf_strict))
21360     return;
21361
21362   scope_die = get_context_die (context);
21363
21364   if (child)
21365     {
21366       gcc_assert (scope_die->die_child);
21367       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21368       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21369       scope_die = scope_die->die_child;
21370     }
21371
21372   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21373   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21374
21375 }
21376
21377 /* Write the debugging output for DECL.  */
21378
21379 void
21380 dwarf2out_decl (tree decl)
21381 {
21382   dw_die_ref context_die = comp_unit_die ();
21383
21384   switch (TREE_CODE (decl))
21385     {
21386     case ERROR_MARK:
21387       return;
21388
21389     case FUNCTION_DECL:
21390       /* What we would really like to do here is to filter out all mere
21391          file-scope declarations of file-scope functions which are never
21392          referenced later within this translation unit (and keep all of ones
21393          that *are* referenced later on) but we aren't clairvoyant, so we have
21394          no idea which functions will be referenced in the future (i.e. later
21395          on within the current translation unit). So here we just ignore all
21396          file-scope function declarations which are not also definitions.  If
21397          and when the debugger needs to know something about these functions,
21398          it will have to hunt around and find the DWARF information associated
21399          with the definition of the function.
21400
21401          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21402          nodes represent definitions and which ones represent mere
21403          declarations.  We have to check DECL_INITIAL instead. That's because
21404          the C front-end supports some weird semantics for "extern inline"
21405          function definitions.  These can get inlined within the current
21406          translation unit (and thus, we need to generate Dwarf info for their
21407          abstract instances so that the Dwarf info for the concrete inlined
21408          instances can have something to refer to) but the compiler never
21409          generates any out-of-lines instances of such things (despite the fact
21410          that they *are* definitions).
21411
21412          The important point is that the C front-end marks these "extern
21413          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21414          them anyway. Note that the C++ front-end also plays some similar games
21415          for inline function definitions appearing within include files which
21416          also contain `#pragma interface' pragmas.  */
21417       if (DECL_INITIAL (decl) == NULL_TREE)
21418         return;
21419
21420       /* If we're a nested function, initially use a parent of NULL; if we're
21421          a plain function, this will be fixed up in decls_for_scope.  If
21422          we're a method, it will be ignored, since we already have a DIE.  */
21423       if (decl_function_context (decl)
21424           /* But if we're in terse mode, we don't care about scope.  */
21425           && debug_info_level > DINFO_LEVEL_TERSE)
21426         context_die = NULL;
21427       break;
21428
21429     case VAR_DECL:
21430       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21431          declaration and if the declaration was never even referenced from
21432          within this entire compilation unit.  We suppress these DIEs in
21433          order to save space in the .debug section (by eliminating entries
21434          which are probably useless).  Note that we must not suppress
21435          block-local extern declarations (whether used or not) because that
21436          would screw-up the debugger's name lookup mechanism and cause it to
21437          miss things which really ought to be in scope at a given point.  */
21438       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21439         return;
21440
21441       /* For local statics lookup proper context die.  */
21442       if (TREE_STATIC (decl) && decl_function_context (decl))
21443         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21444
21445       /* If we are in terse mode, don't generate any DIEs to represent any
21446          variable declarations or definitions.  */
21447       if (debug_info_level <= DINFO_LEVEL_TERSE)
21448         return;
21449       break;
21450
21451     case CONST_DECL:
21452       if (debug_info_level <= DINFO_LEVEL_TERSE)
21453         return;
21454       if (!is_fortran () && !is_ada ())
21455         return;
21456       if (TREE_STATIC (decl) && decl_function_context (decl))
21457         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21458       break;
21459
21460     case NAMESPACE_DECL:
21461     case IMPORTED_DECL:
21462       if (debug_info_level <= DINFO_LEVEL_TERSE)
21463         return;
21464       if (lookup_decl_die (decl) != NULL)
21465         return;
21466       break;
21467
21468     case TYPE_DECL:
21469       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21470       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21471         return;
21472
21473       /* Don't bother trying to generate any DIEs to represent any of the
21474          normal built-in types for the language we are compiling.  */
21475       if (DECL_IS_BUILTIN (decl))
21476         return;
21477
21478       /* If we are in terse mode, don't generate any DIEs for types.  */
21479       if (debug_info_level <= DINFO_LEVEL_TERSE)
21480         return;
21481
21482       /* If we're a function-scope tag, initially use a parent of NULL;
21483          this will be fixed up in decls_for_scope.  */
21484       if (decl_function_context (decl))
21485         context_die = NULL;
21486
21487       break;
21488
21489     default:
21490       return;
21491     }
21492
21493   gen_decl_die (decl, NULL, context_die);
21494 }
21495
21496 /* Write the debugging output for DECL.  */
21497
21498 static void
21499 dwarf2out_function_decl (tree decl)
21500 {
21501   dwarf2out_decl (decl);
21502
21503   htab_empty (decl_loc_table);
21504 }
21505
21506 /* Output a marker (i.e. a label) for the beginning of the generated code for
21507    a lexical block.  */
21508
21509 static void
21510 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21511                        unsigned int blocknum)
21512 {
21513   switch_to_section (current_function_section ());
21514   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21515 }
21516
21517 /* Output a marker (i.e. a label) for the end of the generated code for a
21518    lexical block.  */
21519
21520 static void
21521 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21522 {
21523   switch_to_section (current_function_section ());
21524   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21525 }
21526
21527 /* Returns nonzero if it is appropriate not to emit any debugging
21528    information for BLOCK, because it doesn't contain any instructions.
21529
21530    Don't allow this for blocks with nested functions or local classes
21531    as we would end up with orphans, and in the presence of scheduling
21532    we may end up calling them anyway.  */
21533
21534 static bool
21535 dwarf2out_ignore_block (const_tree block)
21536 {
21537   tree decl;
21538   unsigned int i;
21539
21540   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21541     if (TREE_CODE (decl) == FUNCTION_DECL
21542         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21543       return 0;
21544   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21545     {
21546       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21547       if (TREE_CODE (decl) == FUNCTION_DECL
21548           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21549       return 0;
21550     }
21551
21552   return 1;
21553 }
21554
21555 /* Hash table routines for file_hash.  */
21556
21557 static int
21558 file_table_eq (const void *p1_p, const void *p2_p)
21559 {
21560   const struct dwarf_file_data *const p1 =
21561     (const struct dwarf_file_data *) p1_p;
21562   const char *const p2 = (const char *) p2_p;
21563   return filename_cmp (p1->filename, p2) == 0;
21564 }
21565
21566 static hashval_t
21567 file_table_hash (const void *p_p)
21568 {
21569   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21570   return htab_hash_string (p->filename);
21571 }
21572
21573 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21574    dwarf2out.c) and return its "index".  The index of each (known) filename is
21575    just a unique number which is associated with only that one filename.  We
21576    need such numbers for the sake of generating labels (in the .debug_sfnames
21577    section) and references to those files numbers (in the .debug_srcinfo
21578    and.debug_macinfo sections).  If the filename given as an argument is not
21579    found in our current list, add it to the list and assign it the next
21580    available unique index number.  In order to speed up searches, we remember
21581    the index of the filename was looked up last.  This handles the majority of
21582    all searches.  */
21583
21584 static struct dwarf_file_data *
21585 lookup_filename (const char *file_name)
21586 {
21587   void ** slot;
21588   struct dwarf_file_data * created;
21589
21590   /* Check to see if the file name that was searched on the previous
21591      call matches this file name.  If so, return the index.  */
21592   if (file_table_last_lookup
21593       && (file_name == file_table_last_lookup->filename
21594           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21595     return file_table_last_lookup;
21596
21597   /* Didn't match the previous lookup, search the table.  */
21598   slot = htab_find_slot_with_hash (file_table, file_name,
21599                                    htab_hash_string (file_name), INSERT);
21600   if (*slot)
21601     return (struct dwarf_file_data *) *slot;
21602
21603   created = ggc_alloc_dwarf_file_data ();
21604   created->filename = file_name;
21605   created->emitted_number = 0;
21606   *slot = created;
21607   return created;
21608 }
21609
21610 /* If the assembler will construct the file table, then translate the compiler
21611    internal file table number into the assembler file table number, and emit
21612    a .file directive if we haven't already emitted one yet.  The file table
21613    numbers are different because we prune debug info for unused variables and
21614    types, which may include filenames.  */
21615
21616 static int
21617 maybe_emit_file (struct dwarf_file_data * fd)
21618 {
21619   if (! fd->emitted_number)
21620     {
21621       if (last_emitted_file)
21622         fd->emitted_number = last_emitted_file->emitted_number + 1;
21623       else
21624         fd->emitted_number = 1;
21625       last_emitted_file = fd;
21626
21627       if (DWARF2_ASM_LINE_DEBUG_INFO)
21628         {
21629           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21630           output_quoted_string (asm_out_file,
21631                                 remap_debug_filename (fd->filename));
21632           fputc ('\n', asm_out_file);
21633         }
21634     }
21635
21636   return fd->emitted_number;
21637 }
21638
21639 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21640    That generation should happen after function debug info has been
21641    generated. The value of the attribute is the constant value of ARG.  */
21642
21643 static void
21644 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21645 {
21646   die_arg_entry entry;
21647
21648   if (!die || !arg)
21649     return;
21650
21651   if (!tmpl_value_parm_die_table)
21652     tmpl_value_parm_die_table
21653       = VEC_alloc (die_arg_entry, gc, 32);
21654
21655   entry.die = die;
21656   entry.arg = arg;
21657   VEC_safe_push (die_arg_entry, gc,
21658                  tmpl_value_parm_die_table,
21659                  &entry);
21660 }
21661
21662 /* Return TRUE if T is an instance of generic type, FALSE
21663    otherwise.  */
21664
21665 static bool
21666 generic_type_p (tree t)
21667 {
21668   if (t == NULL_TREE || !TYPE_P (t))
21669     return false;
21670   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21671 }
21672
21673 /* Schedule the generation of the generic parameter dies for the
21674   instance of generic type T. The proper generation itself is later
21675   done by gen_scheduled_generic_parms_dies. */
21676
21677 static void
21678 schedule_generic_params_dies_gen (tree t)
21679 {
21680   if (!generic_type_p (t))
21681     return;
21682
21683   if (generic_type_instances == NULL)
21684     generic_type_instances = VEC_alloc (tree, gc, 256);
21685
21686   VEC_safe_push (tree, gc, generic_type_instances, t);
21687 }
21688
21689 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21690    by append_entry_to_tmpl_value_parm_die_table. This function must
21691    be called after function DIEs have been generated.  */
21692
21693 static void
21694 gen_remaining_tmpl_value_param_die_attribute (void)
21695 {
21696   if (tmpl_value_parm_die_table)
21697     {
21698       unsigned i;
21699       die_arg_entry *e;
21700
21701       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21702         tree_add_const_value_attribute (e->die, e->arg);
21703     }
21704 }
21705
21706 /* Generate generic parameters DIEs for instances of generic types
21707    that have been previously scheduled by
21708    schedule_generic_params_dies_gen. This function must be called
21709    after all the types of the CU have been laid out.  */
21710
21711 static void
21712 gen_scheduled_generic_parms_dies (void)
21713 {
21714   unsigned i;
21715   tree t;
21716
21717   if (generic_type_instances == NULL)
21718     return;
21719   
21720   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21721     gen_generic_params_dies (t);
21722 }
21723
21724
21725 /* Replace DW_AT_name for the decl with name.  */
21726
21727 static void
21728 dwarf2out_set_name (tree decl, tree name)
21729 {
21730   dw_die_ref die;
21731   dw_attr_ref attr;
21732   const char *dname;
21733
21734   die = TYPE_SYMTAB_DIE (decl);
21735   if (!die)
21736     return;
21737
21738   dname = dwarf2_name (name, 0);
21739   if (!dname)
21740     return;
21741
21742   attr = get_AT (die, DW_AT_name);
21743   if (attr)
21744     {
21745       struct indirect_string_node *node;
21746
21747       node = find_AT_string (dname);
21748       /* replace the string.  */
21749       attr->dw_attr_val.v.val_str = node;
21750     }
21751
21752   else
21753     add_name_attribute (die, dname);
21754 }
21755
21756 /* Called by the final INSN scan whenever we see a direct function call.
21757    Make an entry into the direct call table, recording the point of call
21758    and a reference to the target function's debug entry.  */
21759
21760 static void
21761 dwarf2out_direct_call (tree targ)
21762 {
21763   dcall_entry e;
21764   tree origin = decl_ultimate_origin (targ);
21765
21766   /* If this is a clone, use the abstract origin as the target.  */
21767   if (origin)
21768     targ = origin;
21769
21770   e.poc_label_num = poc_label_num++;
21771   e.poc_decl = current_function_decl;
21772   e.targ_die = force_decl_die (targ);
21773   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21774
21775   /* Drop a label at the return point to mark the point of call.  */
21776   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21777 }
21778
21779 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21780
21781 static hashval_t
21782 vcall_insn_table_hash (const void *x)
21783 {
21784   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21785 }
21786
21787 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21788    insnd_uid of *Y.  */
21789
21790 static int
21791 vcall_insn_table_eq (const void *x, const void *y)
21792 {
21793   return (((const struct vcall_insn *) x)->insn_uid
21794           == ((const struct vcall_insn *) y)->insn_uid);
21795 }
21796
21797 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21798
21799 static void
21800 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21801 {
21802   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21803   struct vcall_insn **slot;
21804
21805   gcc_assert (item);
21806   item->insn_uid = insn_uid;
21807   item->vtable_slot = vtable_slot;
21808   slot = (struct vcall_insn **)
21809       htab_find_slot_with_hash (vcall_insn_table, &item,
21810                                 (hashval_t) insn_uid, INSERT);
21811   *slot = item;
21812 }
21813
21814 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21815
21816 static unsigned int
21817 lookup_vcall_insn (unsigned int insn_uid)
21818 {
21819   struct vcall_insn item;
21820   struct vcall_insn *p;
21821
21822   item.insn_uid = insn_uid;
21823   item.vtable_slot = 0;
21824   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21825                                                  (void *) &item,
21826                                                  (hashval_t) insn_uid);
21827   if (p == NULL)
21828     return (unsigned int) -1;
21829   return p->vtable_slot;
21830 }
21831
21832
21833 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21834    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21835    is the vtable slot index that we will need to put in the virtual call
21836    table later.  */
21837
21838 static void
21839 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21840 {
21841   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21842     {
21843       tree token = OBJ_TYPE_REF_TOKEN (addr);
21844       if (TREE_CODE (token) == INTEGER_CST)
21845         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21846     }
21847 }
21848
21849 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21850    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21851    with NEW_INSN.  */
21852
21853 static void
21854 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21855 {
21856   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21857
21858   if (vtable_slot != (unsigned int) -1)
21859     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21860 }
21861
21862 /* Called by the final INSN scan whenever we see a virtual function call.
21863    Make an entry into the virtual call table, recording the point of call
21864    and the slot index of the vtable entry used to call the virtual member
21865    function.  The slot index was associated with the INSN_UID during the
21866    lowering to RTL.  */
21867
21868 static void
21869 dwarf2out_virtual_call (int insn_uid)
21870 {
21871   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21872   vcall_entry e;
21873
21874   if (vtable_slot == (unsigned int) -1)
21875     return;
21876
21877   e.poc_label_num = poc_label_num++;
21878   e.vtable_slot = vtable_slot;
21879   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21880
21881   /* Drop a label at the return point to mark the point of call.  */
21882   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21883 }
21884
21885 /* Called by the final INSN scan whenever we see a var location.  We
21886    use it to drop labels in the right places, and throw the location in
21887    our lookup table.  */
21888
21889 static void
21890 dwarf2out_var_location (rtx loc_note)
21891 {
21892   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21893   struct var_loc_node *newloc;
21894   rtx next_real;
21895   static const char *last_label;
21896   static const char *last_postcall_label;
21897   static bool last_in_cold_section_p;
21898   tree decl;
21899
21900   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21901     return;
21902
21903   next_real = next_real_insn (loc_note);
21904   /* If there are no instructions which would be affected by this note,
21905      don't do anything.  */
21906   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21907     return;
21908
21909   /* If there were any real insns between note we processed last time
21910      and this note (or if it is the first note), clear
21911      last_{,postcall_}label so that they are not reused this time.  */
21912   if (last_var_location_insn == NULL_RTX
21913       || last_var_location_insn != next_real
21914       || last_in_cold_section_p != in_cold_section_p)
21915     {
21916       last_label = NULL;
21917       last_postcall_label = NULL;
21918     }
21919
21920   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21921   newloc = add_var_loc_to_decl (decl, loc_note,
21922                                 NOTE_DURING_CALL_P (loc_note)
21923                                 ? last_postcall_label : last_label);
21924   if (newloc == NULL)
21925     return;
21926
21927   /* If there were no real insns between note we processed last time
21928      and this note, use the label we emitted last time.  Otherwise
21929      create a new label and emit it.  */
21930   if (last_label == NULL)
21931     {
21932       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21933       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21934       loclabel_num++;
21935       last_label = ggc_strdup (loclabel);
21936     }
21937
21938   if (!NOTE_DURING_CALL_P (loc_note))
21939     newloc->label = last_label;
21940   else
21941     {
21942       if (!last_postcall_label)
21943         {
21944           sprintf (loclabel, "%s-1", last_label);
21945           last_postcall_label = ggc_strdup (loclabel);
21946         }
21947       newloc->label = last_postcall_label;
21948     }
21949
21950   last_var_location_insn = next_real;
21951   last_in_cold_section_p = in_cold_section_p;
21952 }
21953
21954 /* We need to reset the locations at the beginning of each
21955    function. We can't do this in the end_function hook, because the
21956    declarations that use the locations won't have been output when
21957    that hook is called.  Also compute have_multiple_function_sections here.  */
21958
21959 static void
21960 dwarf2out_begin_function (tree fun)
21961 {
21962   if (function_section (fun) != text_section)
21963     have_multiple_function_sections = true;
21964   else if (flag_reorder_blocks_and_partition && !cold_text_section)
21965     {
21966       gcc_assert (current_function_decl == fun);
21967       cold_text_section = unlikely_text_section ();
21968       switch_to_section (cold_text_section);
21969       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21970       switch_to_section (current_function_section ());
21971     }
21972
21973   dwarf2out_note_section_used ();
21974 }
21975
21976 /* Output a label to mark the beginning of a source code line entry
21977    and record information relating to this source line, in
21978    'line_info_table' for later output of the .debug_line section.  */
21979
21980 static void
21981 dwarf2out_source_line (unsigned int line, const char *filename,
21982                        int discriminator, bool is_stmt)
21983 {
21984   static bool last_is_stmt = true;
21985
21986   if (debug_info_level >= DINFO_LEVEL_NORMAL
21987       && line != 0)
21988     {
21989       int file_num = maybe_emit_file (lookup_filename (filename));
21990
21991       switch_to_section (current_function_section ());
21992
21993       /* If requested, emit something human-readable.  */
21994       if (flag_debug_asm)
21995         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21996                  filename, line);
21997
21998       if (DWARF2_ASM_LINE_DEBUG_INFO)
21999         {
22000           /* Emit the .loc directive understood by GNU as.  */
22001           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22002           if (is_stmt != last_is_stmt)
22003             {
22004               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22005               last_is_stmt = is_stmt;
22006             }
22007           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22008             fprintf (asm_out_file, " discriminator %d", discriminator);
22009           fputc ('\n', asm_out_file);
22010
22011           /* Indicate that line number info exists.  */
22012           line_info_table_in_use++;
22013         }
22014       else if (function_section (current_function_decl) != text_section)
22015         {
22016           dw_separate_line_info_ref line_info;
22017           targetm.asm_out.internal_label (asm_out_file,
22018                                           SEPARATE_LINE_CODE_LABEL,
22019                                           separate_line_info_table_in_use);
22020
22021           /* Expand the line info table if necessary.  */
22022           if (separate_line_info_table_in_use
22023               == separate_line_info_table_allocated)
22024             {
22025               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22026               separate_line_info_table
22027                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22028                                  separate_line_info_table,
22029                                  separate_line_info_table_allocated);
22030               memset (separate_line_info_table
22031                        + separate_line_info_table_in_use,
22032                       0,
22033                       (LINE_INFO_TABLE_INCREMENT
22034                        * sizeof (dw_separate_line_info_entry)));
22035             }
22036
22037           /* Add the new entry at the end of the line_info_table.  */
22038           line_info
22039             = &separate_line_info_table[separate_line_info_table_in_use++];
22040           line_info->dw_file_num = file_num;
22041           line_info->dw_line_num = line;
22042           line_info->function = current_function_funcdef_no;
22043         }
22044       else
22045         {
22046           dw_line_info_ref line_info;
22047
22048           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22049                                      line_info_table_in_use);
22050
22051           /* Expand the line info table if necessary.  */
22052           if (line_info_table_in_use == line_info_table_allocated)
22053             {
22054               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22055               line_info_table
22056                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22057                                  line_info_table_allocated);
22058               memset (line_info_table + line_info_table_in_use, 0,
22059                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22060             }
22061
22062           /* Add the new entry at the end of the line_info_table.  */
22063           line_info = &line_info_table[line_info_table_in_use++];
22064           line_info->dw_file_num = file_num;
22065           line_info->dw_line_num = line;
22066         }
22067     }
22068 }
22069
22070 /* Record the beginning of a new source file.  */
22071
22072 static void
22073 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22074 {
22075   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22076     {
22077       /* Record the beginning of the file for break_out_includes.  */
22078       dw_die_ref bincl_die;
22079
22080       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22081       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22082     }
22083
22084   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22085     {
22086       macinfo_entry e;
22087       e.code = DW_MACINFO_start_file;
22088       e.lineno = lineno;
22089       e.info = xstrdup (filename);
22090       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22091     }
22092 }
22093
22094 /* Record the end of a source file.  */
22095
22096 static void
22097 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22098 {
22099   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22100     /* Record the end of the file for break_out_includes.  */
22101     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22102
22103   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22104     {
22105       macinfo_entry e;
22106       e.code = DW_MACINFO_end_file;
22107       e.lineno = lineno;
22108       e.info = NULL;
22109       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22110     }
22111 }
22112
22113 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22114    the tail part of the directive line, i.e. the part which is past the
22115    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22116
22117 static void
22118 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22119                   const char *buffer ATTRIBUTE_UNUSED)
22120 {
22121   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22122     {
22123       macinfo_entry e;
22124       e.code = DW_MACINFO_define;
22125       e.lineno = lineno;
22126       e.info = xstrdup (buffer);;
22127       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22128     }
22129 }
22130
22131 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22132    the tail part of the directive line, i.e. the part which is past the
22133    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22134
22135 static void
22136 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22137                  const char *buffer ATTRIBUTE_UNUSED)
22138 {
22139   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22140     {
22141       macinfo_entry e;
22142       e.code = DW_MACINFO_undef;
22143       e.lineno = lineno;
22144       e.info = xstrdup (buffer);;
22145       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22146     }
22147 }
22148
22149 static void
22150 output_macinfo (void)
22151 {
22152   unsigned i;
22153   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22154   macinfo_entry *ref;
22155
22156   if (! length)
22157     return;
22158
22159   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22160     {
22161       switch (ref->code)
22162         {
22163           case DW_MACINFO_start_file:
22164             {
22165               int file_num = maybe_emit_file (lookup_filename (ref->info));
22166               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22167               dw2_asm_output_data_uleb128 
22168                         (ref->lineno, "Included from line number %lu", 
22169                                                 (unsigned long)ref->lineno);
22170               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22171             }
22172             break;
22173           case DW_MACINFO_end_file:
22174             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22175             break;
22176           case DW_MACINFO_define:
22177             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22178             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22179                                                 (unsigned long)ref->lineno);
22180             dw2_asm_output_nstring (ref->info, -1, "The macro");
22181             break;
22182           case DW_MACINFO_undef:
22183             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22184             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22185                                                 (unsigned long)ref->lineno);
22186             dw2_asm_output_nstring (ref->info, -1, "The macro");
22187             break;
22188           default:
22189            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22190              ASM_COMMENT_START, (unsigned long)ref->code);
22191           break;
22192         }
22193     }
22194 }
22195
22196 /* Set up for Dwarf output at the start of compilation.  */
22197
22198 static void
22199 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22200 {
22201   /* Allocate the file_table.  */
22202   file_table = htab_create_ggc (50, file_table_hash,
22203                                 file_table_eq, NULL);
22204
22205   /* Allocate the decl_die_table.  */
22206   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22207                                     decl_die_table_eq, NULL);
22208
22209   /* Allocate the decl_loc_table.  */
22210   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22211                                     decl_loc_table_eq, NULL);
22212
22213   /* Allocate the initial hunk of the decl_scope_table.  */
22214   decl_scope_table = VEC_alloc (tree, gc, 256);
22215
22216   /* Allocate the initial hunk of the abbrev_die_table.  */
22217   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22218     (ABBREV_DIE_TABLE_INCREMENT);
22219   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22220   /* Zero-th entry is allocated, but unused.  */
22221   abbrev_die_table_in_use = 1;
22222
22223   /* Allocate the initial hunk of the line_info_table.  */
22224   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22225     (LINE_INFO_TABLE_INCREMENT);
22226   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22227
22228   /* Zero-th entry is allocated, but unused.  */
22229   line_info_table_in_use = 1;
22230
22231   /* Allocate the pubtypes and pubnames vectors.  */
22232   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22233   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22234
22235   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
22236   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
22237                                       vcall_insn_table_eq, NULL);
22238
22239   incomplete_types = VEC_alloc (tree, gc, 64);
22240
22241   used_rtx_array = VEC_alloc (rtx, gc, 32);
22242
22243   debug_info_section = get_section (DEBUG_INFO_SECTION,
22244                                     SECTION_DEBUG, NULL);
22245   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22246                                       SECTION_DEBUG, NULL);
22247   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22248                                        SECTION_DEBUG, NULL);
22249   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22250                                        SECTION_DEBUG, NULL);
22251   debug_line_section = get_section (DEBUG_LINE_SECTION,
22252                                     SECTION_DEBUG, NULL);
22253   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22254                                    SECTION_DEBUG, NULL);
22255   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22256                                         SECTION_DEBUG, NULL);
22257   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22258                                         SECTION_DEBUG, NULL);
22259   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
22260                                      SECTION_DEBUG, NULL);
22261   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
22262                                      SECTION_DEBUG, NULL);
22263   debug_str_section = get_section (DEBUG_STR_SECTION,
22264                                    DEBUG_STR_SECTION_FLAGS, NULL);
22265   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22266                                       SECTION_DEBUG, NULL);
22267   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22268                                      SECTION_DEBUG, NULL);
22269
22270   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22271   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22272                                DEBUG_ABBREV_SECTION_LABEL, 0);
22273   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22274   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22275                                COLD_TEXT_SECTION_LABEL, 0);
22276   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22277
22278   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22279                                DEBUG_INFO_SECTION_LABEL, 0);
22280   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22281                                DEBUG_LINE_SECTION_LABEL, 0);
22282   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22283                                DEBUG_RANGES_SECTION_LABEL, 0);
22284   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22285                                DEBUG_MACINFO_SECTION_LABEL, 0);
22286
22287   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22288     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22289
22290   switch_to_section (text_section);
22291   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22292 }
22293
22294 /* Called before cgraph_optimize starts outputtting functions, variables
22295    and toplevel asms into assembly.  */
22296
22297 static void
22298 dwarf2out_assembly_start (void)
22299 {
22300   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22301       && dwarf2out_do_cfi_asm ()
22302       && (!(flag_unwind_tables || flag_exceptions)
22303           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22304     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22305 }
22306
22307 /* A helper function for dwarf2out_finish called through
22308    htab_traverse.  Emit one queued .debug_str string.  */
22309
22310 static int
22311 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22312 {
22313   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22314
22315   if (node->label && node->refcount)
22316     {
22317       switch_to_section (debug_str_section);
22318       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22319       assemble_string (node->str, strlen (node->str) + 1);
22320     }
22321
22322   return 1;
22323 }
22324
22325 #if ENABLE_ASSERT_CHECKING
22326 /* Verify that all marks are clear.  */
22327
22328 static void
22329 verify_marks_clear (dw_die_ref die)
22330 {
22331   dw_die_ref c;
22332
22333   gcc_assert (! die->die_mark);
22334   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22335 }
22336 #endif /* ENABLE_ASSERT_CHECKING */
22337
22338 /* Clear the marks for a die and its children.
22339    Be cool if the mark isn't set.  */
22340
22341 static void
22342 prune_unmark_dies (dw_die_ref die)
22343 {
22344   dw_die_ref c;
22345
22346   if (die->die_mark)
22347     die->die_mark = 0;
22348   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22349 }
22350
22351 /* Given DIE that we're marking as used, find any other dies
22352    it references as attributes and mark them as used.  */
22353
22354 static void
22355 prune_unused_types_walk_attribs (dw_die_ref die)
22356 {
22357   dw_attr_ref a;
22358   unsigned ix;
22359
22360   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22361     {
22362       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22363         {
22364           /* A reference to another DIE.
22365              Make sure that it will get emitted.
22366              If it was broken out into a comdat group, don't follow it.  */
22367           if (dwarf_version < 4
22368               || a->dw_attr == DW_AT_specification
22369               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22370             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22371         }
22372       /* Set the string's refcount to 0 so that prune_unused_types_mark
22373          accounts properly for it.  */
22374       if (AT_class (a) == dw_val_class_str)
22375         a->dw_attr_val.v.val_str->refcount = 0;
22376     }
22377 }
22378
22379 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22380
22381 static void
22382 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22383 {
22384   dw_die_ref c;
22385
22386   if (die == NULL || die->die_child == NULL)
22387     return;
22388   c = die->die_child;
22389   do
22390     {
22391       switch (c->die_tag)
22392         {
22393         case DW_TAG_template_type_param:
22394         case DW_TAG_template_value_param:
22395         case DW_TAG_GNU_template_template_param:
22396         case DW_TAG_GNU_template_parameter_pack:
22397           prune_unused_types_mark (c, 1);
22398           break;
22399         default:
22400           break;
22401         }
22402       c = c->die_sib;
22403     } while (c && c != die->die_child);
22404 }
22405
22406 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22407    to DIE's children.  */
22408
22409 static void
22410 prune_unused_types_mark (dw_die_ref die, int dokids)
22411 {
22412   dw_die_ref c;
22413
22414   if (die->die_mark == 0)
22415     {
22416       /* We haven't done this node yet.  Mark it as used.  */
22417       die->die_mark = 1;
22418       /* If this is the DIE of a generic type instantiation,
22419          mark the children DIEs that describe its generic parms and
22420          args.  */
22421       prune_unused_types_mark_generic_parms_dies (die);
22422
22423       /* We also have to mark its parents as used.
22424          (But we don't want to mark our parents' kids due to this.)  */
22425       if (die->die_parent)
22426         prune_unused_types_mark (die->die_parent, 0);
22427
22428       /* Mark any referenced nodes.  */
22429       prune_unused_types_walk_attribs (die);
22430
22431       /* If this node is a specification,
22432          also mark the definition, if it exists.  */
22433       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22434         prune_unused_types_mark (die->die_definition, 1);
22435     }
22436
22437   if (dokids && die->die_mark != 2)
22438     {
22439       /* We need to walk the children, but haven't done so yet.
22440          Remember that we've walked the kids.  */
22441       die->die_mark = 2;
22442
22443       /* If this is an array type, we need to make sure our
22444          kids get marked, even if they're types.  If we're
22445          breaking out types into comdat sections, do this
22446          for all type definitions.  */
22447       if (die->die_tag == DW_TAG_array_type
22448           || (dwarf_version >= 4
22449               && is_type_die (die) && ! is_declaration_die (die)))
22450         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22451       else
22452         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22453     }
22454 }
22455
22456 /* For local classes, look if any static member functions were emitted
22457    and if so, mark them.  */
22458
22459 static void
22460 prune_unused_types_walk_local_classes (dw_die_ref die)
22461 {
22462   dw_die_ref c;
22463
22464   if (die->die_mark == 2)
22465     return;
22466
22467   switch (die->die_tag)
22468     {
22469     case DW_TAG_structure_type:
22470     case DW_TAG_union_type:
22471     case DW_TAG_class_type:
22472       break;
22473
22474     case DW_TAG_subprogram:
22475       if (!get_AT_flag (die, DW_AT_declaration)
22476           || die->die_definition != NULL)
22477         prune_unused_types_mark (die, 1);
22478       return;
22479
22480     default:
22481       return;
22482     }
22483
22484   /* Mark children.  */
22485   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22486 }
22487
22488 /* Walk the tree DIE and mark types that we actually use.  */
22489
22490 static void
22491 prune_unused_types_walk (dw_die_ref die)
22492 {
22493   dw_die_ref c;
22494
22495   /* Don't do anything if this node is already marked and
22496      children have been marked as well.  */
22497   if (die->die_mark == 2)
22498     return;
22499
22500   switch (die->die_tag)
22501     {
22502     case DW_TAG_structure_type:
22503     case DW_TAG_union_type:
22504     case DW_TAG_class_type:
22505       if (die->die_perennial_p)
22506         break;
22507
22508       for (c = die->die_parent; c; c = c->die_parent)
22509         if (c->die_tag == DW_TAG_subprogram)
22510           break;
22511
22512       /* Finding used static member functions inside of classes
22513          is needed just for local classes, because for other classes
22514          static member function DIEs with DW_AT_specification
22515          are emitted outside of the DW_TAG_*_type.  If we ever change
22516          it, we'd need to call this even for non-local classes.  */
22517       if (c)
22518         prune_unused_types_walk_local_classes (die);
22519
22520       /* It's a type node --- don't mark it.  */
22521       return;
22522
22523     case DW_TAG_const_type:
22524     case DW_TAG_packed_type:
22525     case DW_TAG_pointer_type:
22526     case DW_TAG_reference_type:
22527     case DW_TAG_rvalue_reference_type:
22528     case DW_TAG_volatile_type:
22529     case DW_TAG_typedef:
22530     case DW_TAG_array_type:
22531     case DW_TAG_interface_type:
22532     case DW_TAG_friend:
22533     case DW_TAG_variant_part:
22534     case DW_TAG_enumeration_type:
22535     case DW_TAG_subroutine_type:
22536     case DW_TAG_string_type:
22537     case DW_TAG_set_type:
22538     case DW_TAG_subrange_type:
22539     case DW_TAG_ptr_to_member_type:
22540     case DW_TAG_file_type:
22541       if (die->die_perennial_p)
22542         break;
22543
22544       /* It's a type node --- don't mark it.  */
22545       return;
22546
22547     default:
22548       /* Mark everything else.  */
22549       break;
22550   }
22551
22552   if (die->die_mark == 0)
22553     {
22554       die->die_mark = 1;
22555
22556       /* Now, mark any dies referenced from here.  */
22557       prune_unused_types_walk_attribs (die);
22558     }
22559
22560   die->die_mark = 2;
22561
22562   /* Mark children.  */
22563   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22564 }
22565
22566 /* Increment the string counts on strings referred to from DIE's
22567    attributes.  */
22568
22569 static void
22570 prune_unused_types_update_strings (dw_die_ref die)
22571 {
22572   dw_attr_ref a;
22573   unsigned ix;
22574
22575   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22576     if (AT_class (a) == dw_val_class_str)
22577       {
22578         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22579         s->refcount++;
22580         /* Avoid unnecessarily putting strings that are used less than
22581            twice in the hash table.  */
22582         if (s->refcount
22583             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22584           {
22585             void ** slot;
22586             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22587                                              htab_hash_string (s->str),
22588                                              INSERT);
22589             gcc_assert (*slot == NULL);
22590             *slot = s;
22591           }
22592       }
22593 }
22594
22595 /* Remove from the tree DIE any dies that aren't marked.  */
22596
22597 static void
22598 prune_unused_types_prune (dw_die_ref die)
22599 {
22600   dw_die_ref c;
22601
22602   gcc_assert (die->die_mark);
22603   prune_unused_types_update_strings (die);
22604
22605   if (! die->die_child)
22606     return;
22607
22608   c = die->die_child;
22609   do {
22610     dw_die_ref prev = c;
22611     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22612       if (c == die->die_child)
22613         {
22614           /* No marked children between 'prev' and the end of the list.  */
22615           if (prev == c)
22616             /* No marked children at all.  */
22617             die->die_child = NULL;
22618           else
22619             {
22620               prev->die_sib = c->die_sib;
22621               die->die_child = prev;
22622             }
22623           return;
22624         }
22625
22626     if (c != prev->die_sib)
22627       prev->die_sib = c;
22628     prune_unused_types_prune (c);
22629   } while (c != die->die_child);
22630 }
22631
22632 /* A helper function for dwarf2out_finish called through
22633    htab_traverse.  Clear .debug_str strings that we haven't already
22634    decided to emit.  */
22635
22636 static int
22637 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22638 {
22639   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22640
22641   if (!node->label || !node->refcount)
22642     htab_clear_slot (debug_str_hash, h);
22643
22644   return 1;
22645 }
22646
22647 /* Remove dies representing declarations that we never use.  */
22648
22649 static void
22650 prune_unused_types (void)
22651 {
22652   unsigned int i;
22653   limbo_die_node *node;
22654   comdat_type_node *ctnode;
22655   pubname_ref pub;
22656   dcall_entry *dcall;
22657
22658 #if ENABLE_ASSERT_CHECKING
22659   /* All the marks should already be clear.  */
22660   verify_marks_clear (comp_unit_die ());
22661   for (node = limbo_die_list; node; node = node->next)
22662     verify_marks_clear (node->die);
22663   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22664     verify_marks_clear (ctnode->root_die);
22665 #endif /* ENABLE_ASSERT_CHECKING */
22666
22667   /* Mark types that are used in global variables.  */
22668   premark_types_used_by_global_vars ();
22669
22670   /* Set the mark on nodes that are actually used.  */
22671   prune_unused_types_walk (comp_unit_die ());
22672   for (node = limbo_die_list; node; node = node->next)
22673     prune_unused_types_walk (node->die);
22674   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22675     {
22676       prune_unused_types_walk (ctnode->root_die);
22677       prune_unused_types_mark (ctnode->type_die, 1);
22678     }
22679
22680   /* Also set the mark on nodes referenced from the
22681      pubname_table or arange_table.  */
22682   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22683     prune_unused_types_mark (pub->die, 1);
22684   for (i = 0; i < arange_table_in_use; i++)
22685     prune_unused_types_mark (arange_table[i], 1);
22686
22687   /* Mark nodes referenced from the direct call table.  */
22688   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22689     prune_unused_types_mark (dcall->targ_die, 1);
22690
22691   /* Get rid of nodes that aren't marked; and update the string counts.  */
22692   if (debug_str_hash && debug_str_hash_forced)
22693     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22694   else if (debug_str_hash)
22695     htab_empty (debug_str_hash);
22696   prune_unused_types_prune (comp_unit_die ());
22697   for (node = limbo_die_list; node; node = node->next)
22698     prune_unused_types_prune (node->die);
22699   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22700     prune_unused_types_prune (ctnode->root_die);
22701
22702   /* Leave the marks clear.  */
22703   prune_unmark_dies (comp_unit_die ());
22704   for (node = limbo_die_list; node; node = node->next)
22705     prune_unmark_dies (node->die);
22706   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22707     prune_unmark_dies (ctnode->root_die);
22708 }
22709
22710 /* Set the parameter to true if there are any relative pathnames in
22711    the file table.  */
22712 static int
22713 file_table_relative_p (void ** slot, void *param)
22714 {
22715   bool *p = (bool *) param;
22716   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22717   if (!IS_ABSOLUTE_PATH (d->filename))
22718     {
22719       *p = true;
22720       return 0;
22721     }
22722   return 1;
22723 }
22724
22725 /* Routines to manipulate hash table of comdat type units.  */
22726
22727 static hashval_t
22728 htab_ct_hash (const void *of)
22729 {
22730   hashval_t h;
22731   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22732
22733   memcpy (&h, type_node->signature, sizeof (h));
22734   return h;
22735 }
22736
22737 static int
22738 htab_ct_eq (const void *of1, const void *of2)
22739 {
22740   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22741   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22742
22743   return (! memcmp (type_node_1->signature, type_node_2->signature,
22744                     DWARF_TYPE_SIGNATURE_SIZE));
22745 }
22746
22747 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22748    to the location it would have been added, should we know its
22749    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22750    probably improve compactness of debug info, removing equivalent
22751    abbrevs, and hide any differences caused by deferring the
22752    computation of the assembler name, triggered by e.g. PCH.  */
22753
22754 static inline void
22755 move_linkage_attr (dw_die_ref die)
22756 {
22757   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22758   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22759
22760   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22761               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22762
22763   while (--ix > 0)
22764     {
22765       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22766
22767       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22768         break;
22769     }
22770
22771   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22772     {
22773       VEC_pop (dw_attr_node, die->die_attr);
22774       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22775     }
22776 }
22777
22778 /* Helper function for resolve_addr, attempt to resolve
22779    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22780    SYMBOL_REFs refer to variables emitted in the current CU.  */
22781
22782 static int
22783 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22784 {
22785   rtx rtl = *addr;
22786
22787   if (GET_CODE (rtl) == CONST_STRING)
22788     {
22789       size_t len = strlen (XSTR (rtl, 0)) + 1;
22790       tree t = build_string (len, XSTR (rtl, 0));
22791       tree tlen = build_int_cst (NULL_TREE, len - 1);
22792       TREE_TYPE (t)
22793         = build_array_type (char_type_node, build_index_type (tlen));
22794       rtl = lookup_constant_def (t);
22795       if (!rtl || !MEM_P (rtl))
22796         return 1;
22797       rtl = XEXP (rtl, 0);
22798       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22799       *addr = rtl;
22800       return 0;
22801     }
22802
22803   if (GET_CODE (rtl) == SYMBOL_REF
22804       && SYMBOL_REF_DECL (rtl)
22805       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22806     return 1;
22807
22808   if (GET_CODE (rtl) == CONST
22809       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22810     return 1;
22811
22812   return 0;
22813 }
22814
22815 /* Helper function for resolve_addr, handle one location
22816    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22817    the location list couldn't be resolved.  */
22818
22819 static bool
22820 resolve_addr_in_expr (dw_loc_descr_ref loc)
22821 {
22822   for (; loc; loc = loc->dw_loc_next)
22823     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22824          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22825         || (loc->dw_loc_opc == DW_OP_implicit_value
22826             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22827             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22828       return false;
22829     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22830              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22831       {
22832         dw_die_ref ref
22833           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22834         if (ref == NULL)
22835           return false;
22836         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22837         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22838         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22839       }
22840   return true;
22841 }
22842
22843 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22844    an address in .rodata section if the string literal is emitted there,
22845    or remove the containing location list or replace DW_AT_const_value
22846    with DW_AT_location and empty location expression, if it isn't found
22847    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22848    to something that has been emitted in the current CU.  */
22849
22850 static void
22851 resolve_addr (dw_die_ref die)
22852 {
22853   dw_die_ref c;
22854   dw_attr_ref a;
22855   dw_loc_list_ref *curr;
22856   unsigned ix;
22857
22858   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22859     switch (AT_class (a))
22860       {
22861       case dw_val_class_loc_list:
22862         curr = AT_loc_list_ptr (a);
22863         while (*curr)
22864           {
22865             if (!resolve_addr_in_expr ((*curr)->expr))
22866               {
22867                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22868                 if (next && (*curr)->ll_symbol)
22869                   {
22870                     gcc_assert (!next->ll_symbol);
22871                     next->ll_symbol = (*curr)->ll_symbol;
22872                   }
22873                 *curr = next;
22874               }
22875             else
22876               curr = &(*curr)->dw_loc_next;
22877           }
22878         if (!AT_loc_list (a))
22879           {
22880             remove_AT (die, a->dw_attr);
22881             ix--;
22882           }
22883         break;
22884       case dw_val_class_loc:
22885         if (!resolve_addr_in_expr (AT_loc (a)))
22886           {
22887             remove_AT (die, a->dw_attr);
22888             ix--;
22889           }
22890         break;
22891       case dw_val_class_addr:
22892         if (a->dw_attr == DW_AT_const_value
22893             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22894           {
22895             remove_AT (die, a->dw_attr);
22896             ix--;
22897           }
22898         break;
22899       default:
22900         break;
22901       }
22902
22903   FOR_EACH_CHILD (die, c, resolve_addr (c));
22904 }
22905 \f
22906 /* Helper routines for optimize_location_lists.
22907    This pass tries to share identical local lists in .debug_loc
22908    section.  */
22909
22910 /* Iteratively hash operands of LOC opcode.  */
22911
22912 static inline hashval_t
22913 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22914 {
22915   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22916   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22917
22918   switch (loc->dw_loc_opc)
22919     {
22920     case DW_OP_const4u:
22921     case DW_OP_const8u:
22922       if (loc->dtprel)
22923         goto hash_addr;
22924       /* FALLTHRU */
22925     case DW_OP_const1u:
22926     case DW_OP_const1s:
22927     case DW_OP_const2u:
22928     case DW_OP_const2s:
22929     case DW_OP_const4s:
22930     case DW_OP_const8s:
22931     case DW_OP_constu:
22932     case DW_OP_consts:
22933     case DW_OP_pick:
22934     case DW_OP_plus_uconst:
22935     case DW_OP_breg0:
22936     case DW_OP_breg1:
22937     case DW_OP_breg2:
22938     case DW_OP_breg3:
22939     case DW_OP_breg4:
22940     case DW_OP_breg5:
22941     case DW_OP_breg6:
22942     case DW_OP_breg7:
22943     case DW_OP_breg8:
22944     case DW_OP_breg9:
22945     case DW_OP_breg10:
22946     case DW_OP_breg11:
22947     case DW_OP_breg12:
22948     case DW_OP_breg13:
22949     case DW_OP_breg14:
22950     case DW_OP_breg15:
22951     case DW_OP_breg16:
22952     case DW_OP_breg17:
22953     case DW_OP_breg18:
22954     case DW_OP_breg19:
22955     case DW_OP_breg20:
22956     case DW_OP_breg21:
22957     case DW_OP_breg22:
22958     case DW_OP_breg23:
22959     case DW_OP_breg24:
22960     case DW_OP_breg25:
22961     case DW_OP_breg26:
22962     case DW_OP_breg27:
22963     case DW_OP_breg28:
22964     case DW_OP_breg29:
22965     case DW_OP_breg30:
22966     case DW_OP_breg31:
22967     case DW_OP_regx:
22968     case DW_OP_fbreg:
22969     case DW_OP_piece:
22970     case DW_OP_deref_size:
22971     case DW_OP_xderef_size:
22972       hash = iterative_hash_object (val1->v.val_int, hash);
22973       break;
22974     case DW_OP_skip:
22975     case DW_OP_bra:
22976       {
22977         int offset;
22978
22979         gcc_assert (val1->val_class == dw_val_class_loc);
22980         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22981         hash = iterative_hash_object (offset, hash);
22982       }
22983       break;
22984     case DW_OP_implicit_value:
22985       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22986       switch (val2->val_class)
22987         {
22988         case dw_val_class_const:
22989           hash = iterative_hash_object (val2->v.val_int, hash);
22990           break;
22991         case dw_val_class_vec:
22992           {
22993             unsigned int elt_size = val2->v.val_vec.elt_size;
22994             unsigned int len = val2->v.val_vec.length;
22995
22996             hash = iterative_hash_object (elt_size, hash);
22997             hash = iterative_hash_object (len, hash);
22998             hash = iterative_hash (val2->v.val_vec.array,
22999                                    len * elt_size, hash);
23000           }
23001           break;
23002         case dw_val_class_const_double:
23003           hash = iterative_hash_object (val2->v.val_double.low, hash);
23004           hash = iterative_hash_object (val2->v.val_double.high, hash);
23005           break;
23006         case dw_val_class_addr:
23007           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23008           break;
23009         default:
23010           gcc_unreachable ();
23011         }
23012       break;
23013     case DW_OP_bregx:
23014     case DW_OP_bit_piece:
23015       hash = iterative_hash_object (val1->v.val_int, hash);
23016       hash = iterative_hash_object (val2->v.val_int, hash);
23017       break;
23018     case DW_OP_addr:
23019     hash_addr:
23020       if (loc->dtprel)
23021         {
23022           unsigned char dtprel = 0xd1;
23023           hash = iterative_hash_object (dtprel, hash);
23024         }
23025       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23026       break;
23027     case DW_OP_GNU_implicit_pointer:
23028       hash = iterative_hash_object (val2->v.val_int, hash);
23029       break;
23030
23031     default:
23032       /* Other codes have no operands.  */
23033       break;
23034     }
23035   return hash;
23036 }
23037
23038 /* Iteratively hash the whole DWARF location expression LOC.  */
23039
23040 static inline hashval_t
23041 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23042 {
23043   dw_loc_descr_ref l;
23044   bool sizes_computed = false;
23045   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23046   size_of_locs (loc);
23047
23048   for (l = loc; l != NULL; l = l->dw_loc_next)
23049     {
23050       enum dwarf_location_atom opc = l->dw_loc_opc;
23051       hash = iterative_hash_object (opc, hash);
23052       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23053         {
23054           size_of_locs (loc);
23055           sizes_computed = true;
23056         }
23057       hash = hash_loc_operands (l, hash);
23058     }
23059   return hash;
23060 }
23061
23062 /* Compute hash of the whole location list LIST_HEAD.  */
23063
23064 static inline void
23065 hash_loc_list (dw_loc_list_ref list_head)
23066 {
23067   dw_loc_list_ref curr = list_head;
23068   hashval_t hash = 0;
23069
23070   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23071     {
23072       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23073       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23074       if (curr->section)
23075         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23076                                hash);
23077       hash = hash_locs (curr->expr, hash);
23078     }
23079   list_head->hash = hash;
23080 }
23081
23082 /* Return true if X and Y opcodes have the same operands.  */
23083
23084 static inline bool
23085 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23086 {
23087   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23088   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23089   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23090   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23091
23092   switch (x->dw_loc_opc)
23093     {
23094     case DW_OP_const4u:
23095     case DW_OP_const8u:
23096       if (x->dtprel)
23097         goto hash_addr;
23098       /* FALLTHRU */
23099     case DW_OP_const1u:
23100     case DW_OP_const1s:
23101     case DW_OP_const2u:
23102     case DW_OP_const2s:
23103     case DW_OP_const4s:
23104     case DW_OP_const8s:
23105     case DW_OP_constu:
23106     case DW_OP_consts:
23107     case DW_OP_pick:
23108     case DW_OP_plus_uconst:
23109     case DW_OP_breg0:
23110     case DW_OP_breg1:
23111     case DW_OP_breg2:
23112     case DW_OP_breg3:
23113     case DW_OP_breg4:
23114     case DW_OP_breg5:
23115     case DW_OP_breg6:
23116     case DW_OP_breg7:
23117     case DW_OP_breg8:
23118     case DW_OP_breg9:
23119     case DW_OP_breg10:
23120     case DW_OP_breg11:
23121     case DW_OP_breg12:
23122     case DW_OP_breg13:
23123     case DW_OP_breg14:
23124     case DW_OP_breg15:
23125     case DW_OP_breg16:
23126     case DW_OP_breg17:
23127     case DW_OP_breg18:
23128     case DW_OP_breg19:
23129     case DW_OP_breg20:
23130     case DW_OP_breg21:
23131     case DW_OP_breg22:
23132     case DW_OP_breg23:
23133     case DW_OP_breg24:
23134     case DW_OP_breg25:
23135     case DW_OP_breg26:
23136     case DW_OP_breg27:
23137     case DW_OP_breg28:
23138     case DW_OP_breg29:
23139     case DW_OP_breg30:
23140     case DW_OP_breg31:
23141     case DW_OP_regx:
23142     case DW_OP_fbreg:
23143     case DW_OP_piece:
23144     case DW_OP_deref_size:
23145     case DW_OP_xderef_size:
23146       return valx1->v.val_int == valy1->v.val_int;
23147     case DW_OP_skip:
23148     case DW_OP_bra:
23149       gcc_assert (valx1->val_class == dw_val_class_loc
23150                   && valy1->val_class == dw_val_class_loc
23151                   && x->dw_loc_addr == y->dw_loc_addr);
23152       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23153     case DW_OP_implicit_value:
23154       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23155           || valx2->val_class != valy2->val_class)
23156         return false;
23157       switch (valx2->val_class)
23158         {
23159         case dw_val_class_const:
23160           return valx2->v.val_int == valy2->v.val_int;
23161         case dw_val_class_vec:
23162           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23163                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23164                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23165                             valx2->v.val_vec.elt_size
23166                             * valx2->v.val_vec.length) == 0;
23167         case dw_val_class_const_double:
23168           return valx2->v.val_double.low == valy2->v.val_double.low
23169                  && valx2->v.val_double.high == valy2->v.val_double.high;
23170         case dw_val_class_addr:
23171           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23172         default:
23173           gcc_unreachable ();
23174         }
23175     case DW_OP_bregx:
23176     case DW_OP_bit_piece:
23177       return valx1->v.val_int == valy1->v.val_int
23178              && valx2->v.val_int == valy2->v.val_int;
23179     case DW_OP_addr:
23180     hash_addr:
23181       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23182     case DW_OP_GNU_implicit_pointer:
23183       return valx1->val_class == dw_val_class_die_ref
23184              && valx1->val_class == valy1->val_class
23185              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23186              && valx2->v.val_int == valy2->v.val_int;
23187     default:
23188       /* Other codes have no operands.  */
23189       return true;
23190     }
23191 }
23192
23193 /* Return true if DWARF location expressions X and Y are the same.  */
23194
23195 static inline bool
23196 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23197 {
23198   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23199     if (x->dw_loc_opc != y->dw_loc_opc
23200         || x->dtprel != y->dtprel
23201         || !compare_loc_operands (x, y))
23202       break;
23203   return x == NULL && y == NULL;
23204 }
23205
23206 /* Return precomputed hash of location list X.  */
23207
23208 static hashval_t
23209 loc_list_hash (const void *x)
23210 {
23211   return ((const struct dw_loc_list_struct *) x)->hash;
23212 }
23213
23214 /* Return 1 if location lists X and Y are the same.  */
23215
23216 static int
23217 loc_list_eq (const void *x, const void *y)
23218 {
23219   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23220   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23221   if (a == b)
23222     return 1;
23223   if (a->hash != b->hash)
23224     return 0;
23225   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23226     if (strcmp (a->begin, b->begin) != 0
23227         || strcmp (a->end, b->end) != 0
23228         || (a->section == NULL) != (b->section == NULL)
23229         || (a->section && strcmp (a->section, b->section) != 0)
23230         || !compare_locs (a->expr, b->expr))
23231       break;
23232   return a == NULL && b == NULL;
23233 }
23234
23235 /* Recursively optimize location lists referenced from DIE
23236    children and share them whenever possible.  */
23237
23238 static void
23239 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23240 {
23241   dw_die_ref c;
23242   dw_attr_ref a;
23243   unsigned ix;
23244   void **slot;
23245
23246   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23247     if (AT_class (a) == dw_val_class_loc_list)
23248       {
23249         dw_loc_list_ref list = AT_loc_list (a);
23250         /* TODO: perform some optimizations here, before hashing
23251            it and storing into the hash table.  */
23252         hash_loc_list (list);
23253         slot = htab_find_slot_with_hash (htab, list, list->hash,
23254                                          INSERT);
23255         if (*slot == NULL)
23256           *slot = (void *) list;
23257         else
23258           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23259       }
23260
23261   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23262 }
23263
23264 /* Optimize location lists referenced from DIE
23265    children and share them whenever possible.  */
23266
23267 static void
23268 optimize_location_lists (dw_die_ref die)
23269 {
23270   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23271   optimize_location_lists_1 (die, htab);
23272   htab_delete (htab);
23273 }
23274 \f
23275 /* Output stuff that dwarf requires at the end of every file,
23276    and generate the DWARF-2 debugging info.  */
23277
23278 static void
23279 dwarf2out_finish (const char *filename)
23280 {
23281   limbo_die_node *node, *next_node;
23282   comdat_type_node *ctnode;
23283   htab_t comdat_type_table;
23284   unsigned int i;
23285
23286   gen_scheduled_generic_parms_dies ();
23287   gen_remaining_tmpl_value_param_die_attribute ();
23288
23289   /* Add the name for the main input file now.  We delayed this from
23290      dwarf2out_init to avoid complications with PCH.  */
23291   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23292   if (!IS_ABSOLUTE_PATH (filename))
23293     add_comp_dir_attribute (comp_unit_die ());
23294   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23295     {
23296       bool p = false;
23297       htab_traverse (file_table, file_table_relative_p, &p);
23298       if (p)
23299         add_comp_dir_attribute (comp_unit_die ());
23300     }
23301
23302   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23303     {
23304       add_location_or_const_value_attribute (
23305         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23306         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23307         DW_AT_location);
23308     }
23309
23310   /* Traverse the limbo die list, and add parent/child links.  The only
23311      dies without parents that should be here are concrete instances of
23312      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23313      For concrete instances, we can get the parent die from the abstract
23314      instance.  */
23315   for (node = limbo_die_list; node; node = next_node)
23316     {
23317       dw_die_ref die = node->die;
23318       next_node = node->next;
23319
23320       if (die->die_parent == NULL)
23321         {
23322           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23323
23324           if (origin)
23325             add_child_die (origin->die_parent, die);
23326           else if (is_cu_die (die))
23327             ;
23328           else if (seen_error ())
23329             /* It's OK to be confused by errors in the input.  */
23330             add_child_die (comp_unit_die (), die);
23331           else
23332             {
23333               /* In certain situations, the lexical block containing a
23334                  nested function can be optimized away, which results
23335                  in the nested function die being orphaned.  Likewise
23336                  with the return type of that nested function.  Force
23337                  this to be a child of the containing function.
23338
23339                  It may happen that even the containing function got fully
23340                  inlined and optimized out.  In that case we are lost and
23341                  assign the empty child.  This should not be big issue as
23342                  the function is likely unreachable too.  */
23343               tree context = NULL_TREE;
23344
23345               gcc_assert (node->created_for);
23346
23347               if (DECL_P (node->created_for))
23348                 context = DECL_CONTEXT (node->created_for);
23349               else if (TYPE_P (node->created_for))
23350                 context = TYPE_CONTEXT (node->created_for);
23351
23352               gcc_assert (context
23353                           && (TREE_CODE (context) == FUNCTION_DECL
23354                               || TREE_CODE (context) == NAMESPACE_DECL));
23355
23356               origin = lookup_decl_die (context);
23357               if (origin)
23358                 add_child_die (origin, die);
23359               else
23360                 add_child_die (comp_unit_die (), die);
23361             }
23362         }
23363     }
23364
23365   limbo_die_list = NULL;
23366
23367   resolve_addr (comp_unit_die ());
23368
23369   for (node = deferred_asm_name; node; node = node->next)
23370     {
23371       tree decl = node->created_for;
23372       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23373         {
23374           add_linkage_attr (node->die, decl);
23375           move_linkage_attr (node->die);
23376         }
23377     }
23378
23379   deferred_asm_name = NULL;
23380
23381   /* Walk through the list of incomplete types again, trying once more to
23382      emit full debugging info for them.  */
23383   retry_incomplete_types ();
23384
23385   if (flag_eliminate_unused_debug_types)
23386     prune_unused_types ();
23387
23388   /* Generate separate CUs for each of the include files we've seen.
23389      They will go into limbo_die_list.  */
23390   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23391     break_out_includes (comp_unit_die ());
23392
23393   /* Generate separate COMDAT sections for type DIEs. */
23394   if (dwarf_version >= 4)
23395     {
23396       break_out_comdat_types (comp_unit_die ());
23397
23398       /* Each new type_unit DIE was added to the limbo die list when created.
23399          Since these have all been added to comdat_type_list, clear the
23400          limbo die list.  */
23401       limbo_die_list = NULL;
23402
23403       /* For each new comdat type unit, copy declarations for incomplete
23404          types to make the new unit self-contained (i.e., no direct
23405          references to the main compile unit).  */
23406       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23407         copy_decls_for_unworthy_types (ctnode->root_die);
23408       copy_decls_for_unworthy_types (comp_unit_die ());
23409
23410       /* In the process of copying declarations from one unit to another,
23411          we may have left some declarations behind that are no longer
23412          referenced.  Prune them.  */
23413       prune_unused_types ();
23414     }
23415
23416   /* Traverse the DIE's and add add sibling attributes to those DIE's
23417      that have children.  */
23418   add_sibling_attributes (comp_unit_die ());
23419   for (node = limbo_die_list; node; node = node->next)
23420     add_sibling_attributes (node->die);
23421   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23422     add_sibling_attributes (ctnode->root_die);
23423
23424   /* Output a terminator label for the .text section.  */
23425   switch_to_section (text_section);
23426   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23427   if (cold_text_section)
23428     {
23429       switch_to_section (cold_text_section);
23430       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23431     }
23432
23433   /* We can only use the low/high_pc attributes if all of the code was
23434      in .text.  */
23435   if (!have_multiple_function_sections 
23436       || (dwarf_version < 3 && dwarf_strict))
23437     {
23438       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23439       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23440     }
23441
23442   else
23443     {
23444       unsigned fde_idx = 0;
23445       bool range_list_added = false;
23446
23447       /* We need to give .debug_loc and .debug_ranges an appropriate
23448          "base address".  Use zero so that these addresses become
23449          absolute.  Historically, we've emitted the unexpected
23450          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23451          Emit both to give time for other tools to adapt.  */
23452       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23453       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23454
23455       if (text_section_used)
23456         add_ranges_by_labels (comp_unit_die (), text_section_label,
23457                               text_end_label, &range_list_added);
23458       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23459         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23460                               cold_end_label, &range_list_added);
23461
23462       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23463         {
23464           dw_fde_ref fde = &fde_table[fde_idx];
23465
23466           if (fde->dw_fde_switched_sections)
23467             {
23468               if (!fde->in_std_section)
23469                 add_ranges_by_labels (comp_unit_die (),
23470                                       fde->dw_fde_hot_section_label,
23471                                       fde->dw_fde_hot_section_end_label,
23472                                       &range_list_added);
23473               if (!fde->cold_in_std_section)
23474                 add_ranges_by_labels (comp_unit_die (),
23475                                       fde->dw_fde_unlikely_section_label,
23476                                       fde->dw_fde_unlikely_section_end_label,
23477                                       &range_list_added);
23478             }
23479           else if (!fde->in_std_section)
23480             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23481                                   fde->dw_fde_end, &range_list_added);
23482         }
23483
23484       if (range_list_added)
23485         add_ranges (NULL);
23486     }
23487
23488   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23489     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23490                     debug_line_section_label);
23491
23492   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23493     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23494
23495   if (have_location_lists)
23496     optimize_location_lists (comp_unit_die ());
23497
23498   /* Output all of the compilation units.  We put the main one last so that
23499      the offsets are available to output_pubnames.  */
23500   for (node = limbo_die_list; node; node = node->next)
23501     output_comp_unit (node->die, 0);
23502
23503   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23504   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23505     {
23506       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23507
23508       /* Don't output duplicate types.  */
23509       if (*slot != HTAB_EMPTY_ENTRY)
23510         continue;
23511
23512       /* Add a pointer to the line table for the main compilation unit
23513          so that the debugger can make sense of DW_AT_decl_file
23514          attributes.  */
23515       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23516         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23517                         debug_line_section_label);
23518
23519       output_comdat_type_unit (ctnode);
23520       *slot = ctnode;
23521     }
23522   htab_delete (comdat_type_table);
23523
23524   /* Output the main compilation unit if non-empty or if .debug_macinfo
23525      will be emitted.  */
23526   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23527
23528   /* Output the abbreviation table.  */
23529   switch_to_section (debug_abbrev_section);
23530   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23531   output_abbrev_section ();
23532
23533   /* Output location list section if necessary.  */
23534   if (have_location_lists)
23535     {
23536       /* Output the location lists info.  */
23537       switch_to_section (debug_loc_section);
23538       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23539                                    DEBUG_LOC_SECTION_LABEL, 0);
23540       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23541       output_location_lists (comp_unit_die ());
23542     }
23543
23544   /* Output public names table if necessary.  */
23545   if (!VEC_empty (pubname_entry, pubname_table))
23546     {
23547       gcc_assert (info_section_emitted);
23548       switch_to_section (debug_pubnames_section);
23549       output_pubnames (pubname_table);
23550     }
23551
23552   /* Output public types table if necessary.  */
23553   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23554      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23555      simply won't look for the section.  */
23556   if (!VEC_empty (pubname_entry, pubtype_table))
23557     {
23558       bool empty = false;
23559       
23560       if (flag_eliminate_unused_debug_types)
23561         {
23562           /* The pubtypes table might be emptied by pruning unused items.  */
23563           unsigned i;
23564           pubname_ref p;
23565           empty = true;
23566           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23567             if (p->die->die_offset != 0)
23568               {
23569                 empty = false;
23570                 break;
23571               }
23572         }
23573       if (!empty)
23574         {
23575           gcc_assert (info_section_emitted);
23576           switch_to_section (debug_pubtypes_section);
23577           output_pubnames (pubtype_table);
23578         }
23579     }
23580
23581   /* Output direct and virtual call tables if necessary.  */
23582   if (!VEC_empty (dcall_entry, dcall_table))
23583     {
23584       switch_to_section (debug_dcall_section);
23585       output_dcall_table ();
23586     }
23587   if (!VEC_empty (vcall_entry, vcall_table))
23588     {
23589       switch_to_section (debug_vcall_section);
23590       output_vcall_table ();
23591     }
23592
23593   /* Output the address range information.  We only put functions in the arange
23594      table, so don't write it out if we don't have any.  */
23595   if (arange_table_in_use)
23596     {
23597       switch_to_section (debug_aranges_section);
23598       output_aranges ();
23599     }
23600
23601   /* Output ranges section if necessary.  */
23602   if (ranges_table_in_use)
23603     {
23604       switch_to_section (debug_ranges_section);
23605       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23606       output_ranges ();
23607     }
23608
23609   /* Output the source line correspondence table.  We must do this
23610      even if there is no line information.  Otherwise, on an empty
23611      translation unit, we will generate a present, but empty,
23612      .debug_info section.  IRIX 6.5 `nm' will then complain when
23613      examining the file.  This is done late so that any filenames
23614      used by the debug_info section are marked as 'used'.  */
23615   switch_to_section (debug_line_section);
23616   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23617   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23618     output_line_info ();
23619
23620   /* Have to end the macro section.  */
23621   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23622     {
23623       switch_to_section (debug_macinfo_section);
23624       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23625       if (!VEC_empty (macinfo_entry, macinfo_table))
23626         output_macinfo ();
23627       dw2_asm_output_data (1, 0, "End compilation unit");
23628     }
23629
23630   /* If we emitted any DW_FORM_strp form attribute, output the string
23631      table too.  */
23632   if (debug_str_hash)
23633     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23634 }
23635
23636 #include "gt-dwarf2out.h"