OSDN Git Service

2010-10-01 Richard Guenther <rguenther@suse.de>
[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 () == 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 () != 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 GTY(()) 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);
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 /* Hook used by __throw.  */
550
551 rtx
552 expand_builtin_dwarf_sp_column (void)
553 {
554   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
555   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
556 }
557
558 /* Return a pointer to a copy of the section string name S with all
559    attributes stripped off, and an asterisk prepended (for assemble_name).  */
560
561 static inline char *
562 stripattributes (const char *s)
563 {
564   char *stripped = XNEWVEC (char, strlen (s) + 2);
565   char *p = stripped;
566
567   *p++ = '*';
568
569   while (*s && *s != ',')
570     *p++ = *s++;
571
572   *p = '\0';
573   return stripped;
574 }
575
576 /* MEM is a memory reference for the register size table, each element of
577    which has mode MODE.  Initialize column C as a return address column.  */
578
579 static void
580 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
581 {
582   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
583   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
584   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
585 }
586
587 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
588
589 static inline HOST_WIDE_INT
590 div_data_align (HOST_WIDE_INT off)
591 {
592   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
593   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
594   return r;
595 }
596
597 /* Return true if we need a signed version of a given opcode
598    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
599
600 static inline bool
601 need_data_align_sf_opcode (HOST_WIDE_INT off)
602 {
603   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
604 }
605
606 /* Generate code to initialize the register size table.  */
607
608 void
609 expand_builtin_init_dwarf_reg_sizes (tree address)
610 {
611   unsigned int i;
612   enum machine_mode mode = TYPE_MODE (char_type_node);
613   rtx addr = expand_normal (address);
614   rtx mem = gen_rtx_MEM (BLKmode, addr);
615   bool wrote_return_column = false;
616
617   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
618     {
619       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
620
621       if (rnum < DWARF_FRAME_REGISTERS)
622         {
623           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
624           enum machine_mode save_mode = reg_raw_mode[i];
625           HOST_WIDE_INT size;
626
627           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
628             save_mode = choose_hard_reg_mode (i, 1, true);
629           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
630             {
631               if (save_mode == VOIDmode)
632                 continue;
633               wrote_return_column = true;
634             }
635           size = GET_MODE_SIZE (save_mode);
636           if (offset < 0)
637             continue;
638
639           emit_move_insn (adjust_address (mem, mode, offset),
640                           gen_int_mode (size, mode));
641         }
642     }
643
644   if (!wrote_return_column)
645     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
646
647 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
648   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
649 #endif
650
651   targetm.init_dwarf_reg_sizes_extra (address);
652 }
653
654 /* Convert a DWARF call frame info. operation to its string name */
655
656 static const char *
657 dwarf_cfi_name (unsigned int cfi_opc)
658 {
659   switch (cfi_opc)
660     {
661     case DW_CFA_advance_loc:
662       return "DW_CFA_advance_loc";
663     case DW_CFA_offset:
664       return "DW_CFA_offset";
665     case DW_CFA_restore:
666       return "DW_CFA_restore";
667     case DW_CFA_nop:
668       return "DW_CFA_nop";
669     case DW_CFA_set_loc:
670       return "DW_CFA_set_loc";
671     case DW_CFA_advance_loc1:
672       return "DW_CFA_advance_loc1";
673     case DW_CFA_advance_loc2:
674       return "DW_CFA_advance_loc2";
675     case DW_CFA_advance_loc4:
676       return "DW_CFA_advance_loc4";
677     case DW_CFA_offset_extended:
678       return "DW_CFA_offset_extended";
679     case DW_CFA_restore_extended:
680       return "DW_CFA_restore_extended";
681     case DW_CFA_undefined:
682       return "DW_CFA_undefined";
683     case DW_CFA_same_value:
684       return "DW_CFA_same_value";
685     case DW_CFA_register:
686       return "DW_CFA_register";
687     case DW_CFA_remember_state:
688       return "DW_CFA_remember_state";
689     case DW_CFA_restore_state:
690       return "DW_CFA_restore_state";
691     case DW_CFA_def_cfa:
692       return "DW_CFA_def_cfa";
693     case DW_CFA_def_cfa_register:
694       return "DW_CFA_def_cfa_register";
695     case DW_CFA_def_cfa_offset:
696       return "DW_CFA_def_cfa_offset";
697
698     /* DWARF 3 */
699     case DW_CFA_def_cfa_expression:
700       return "DW_CFA_def_cfa_expression";
701     case DW_CFA_expression:
702       return "DW_CFA_expression";
703     case DW_CFA_offset_extended_sf:
704       return "DW_CFA_offset_extended_sf";
705     case DW_CFA_def_cfa_sf:
706       return "DW_CFA_def_cfa_sf";
707     case DW_CFA_def_cfa_offset_sf:
708       return "DW_CFA_def_cfa_offset_sf";
709
710     /* SGI/MIPS specific */
711     case DW_CFA_MIPS_advance_loc8:
712       return "DW_CFA_MIPS_advance_loc8";
713
714     /* GNU extensions */
715     case DW_CFA_GNU_window_save:
716       return "DW_CFA_GNU_window_save";
717     case DW_CFA_GNU_args_size:
718       return "DW_CFA_GNU_args_size";
719     case DW_CFA_GNU_negative_offset_extended:
720       return "DW_CFA_GNU_negative_offset_extended";
721
722     default:
723       return "DW_CFA_<unknown>";
724     }
725 }
726
727 /* Return a pointer to a newly allocated Call Frame Instruction.  */
728
729 static inline dw_cfi_ref
730 new_cfi (void)
731 {
732   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
733
734   cfi->dw_cfi_next = NULL;
735   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
736   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
737
738   return cfi;
739 }
740
741 /* Add a Call Frame Instruction to list of instructions.  */
742
743 static inline void
744 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
745 {
746   dw_cfi_ref *p;
747   dw_fde_ref fde = current_fde ();
748
749   /* When DRAP is used, CFA is defined with an expression.  Redefine
750      CFA may lead to a different CFA value.   */
751   /* ??? Of course, this heuristic fails when we're annotating epilogues,
752      because of course we'll always want to redefine the CFA back to the
753      stack pointer on the way out.  Where should we move this check?  */
754   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
755     switch (cfi->dw_cfi_opc)
756       {
757         case DW_CFA_def_cfa_register:
758         case DW_CFA_def_cfa_offset:
759         case DW_CFA_def_cfa_offset_sf:
760         case DW_CFA_def_cfa:
761         case DW_CFA_def_cfa_sf:
762           gcc_unreachable ();
763
764         default:
765           break;
766       }
767
768   /* Find the end of the chain.  */
769   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
770     ;
771
772   *p = cfi;
773 }
774
775 /* Generate a new label for the CFI info to refer to.  FORCE is true
776    if a label needs to be output even when using .cfi_* directives.  */
777
778 char *
779 dwarf2out_cfi_label (bool force)
780 {
781   static char label[20];
782
783   if (!force && dwarf2out_do_cfi_asm ())
784     {
785       /* In this case, we will be emitting the asm directive instead of
786          the label, so just return a placeholder to keep the rest of the
787          interfaces happy.  */
788       strcpy (label, "<do not output>");
789     }
790   else
791     {
792       int num = dwarf2out_cfi_label_num++;
793       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
794       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
795     }
796
797   return label;
798 }
799
800 /* True if remember_state should be emitted before following CFI directive.  */
801 static bool emit_cfa_remember;
802
803 /* True if any CFI directives were emitted at the current insn.  */
804 static bool any_cfis_emitted;
805
806 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
807    or to the CIE if LABEL is NULL.  */
808
809 static void
810 add_fde_cfi (const char *label, dw_cfi_ref cfi)
811 {
812   dw_cfi_ref *list_head;
813
814   if (emit_cfa_remember)
815     {
816       dw_cfi_ref cfi_remember;
817
818       /* Emit the state save.  */
819       emit_cfa_remember = false;
820       cfi_remember = new_cfi ();
821       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
822       add_fde_cfi (label, cfi_remember);
823     }
824
825   list_head = &cie_cfi_head;
826
827   if (dwarf2out_do_cfi_asm ())
828     {
829       if (label)
830         {
831           dw_fde_ref fde = current_fde ();
832
833           gcc_assert (fde != NULL);
834
835           /* We still have to add the cfi to the list so that lookup_cfa
836              works later on.  When -g2 and above we even need to force
837              emitting of CFI labels and add to list a DW_CFA_set_loc for
838              convert_cfa_to_fb_loc_list purposes.  If we're generating
839              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
840              convert_cfa_to_fb_loc_list.  */
841           if (dwarf_version == 2
842               && debug_info_level > DINFO_LEVEL_TERSE
843               && (write_symbols == DWARF2_DEBUG
844                   || write_symbols == VMS_AND_DWARF2_DEBUG))
845             {
846               switch (cfi->dw_cfi_opc)
847                 {
848                 case DW_CFA_def_cfa_offset:
849                 case DW_CFA_def_cfa_offset_sf:
850                 case DW_CFA_def_cfa_register:
851                 case DW_CFA_def_cfa:
852                 case DW_CFA_def_cfa_sf:
853                 case DW_CFA_def_cfa_expression:
854                 case DW_CFA_restore_state:
855                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
856                     label = dwarf2out_cfi_label (true);
857
858                   if (fde->dw_fde_current_label == NULL
859                       || strcmp (label, fde->dw_fde_current_label) != 0)
860                     {
861                       dw_cfi_ref xcfi;
862
863                       label = xstrdup (label);
864
865                       /* Set the location counter to the new label.  */
866                       xcfi = new_cfi ();
867                       /* It doesn't metter whether DW_CFA_set_loc
868                          or DW_CFA_advance_loc4 is added here, those aren't
869                          emitted into assembly, only looked up by
870                          convert_cfa_to_fb_loc_list.  */
871                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
872                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
873                       add_cfi (&fde->dw_fde_cfi, xcfi);
874                       fde->dw_fde_current_label = label;
875                     }
876                   break;
877                 default:
878                   break;
879                 }
880             }
881
882           output_cfi_directive (cfi);
883
884           list_head = &fde->dw_fde_cfi;
885           any_cfis_emitted = true;
886         }
887       /* ??? If this is a CFI for the CIE, we don't emit.  This
888          assumes that the standard CIE contents that the assembler
889          uses matches the standard CIE contents that the compiler
890          uses.  This is probably a bad assumption.  I'm not quite
891          sure how to address this for now.  */
892     }
893   else if (label)
894     {
895       dw_fde_ref fde = current_fde ();
896
897       gcc_assert (fde != NULL);
898
899       if (*label == 0)
900         label = dwarf2out_cfi_label (false);
901
902       if (fde->dw_fde_current_label == NULL
903           || strcmp (label, fde->dw_fde_current_label) != 0)
904         {
905           dw_cfi_ref xcfi;
906
907           label = xstrdup (label);
908
909           /* Set the location counter to the new label.  */
910           xcfi = new_cfi ();
911           /* If we have a current label, advance from there, otherwise
912              set the location directly using set_loc.  */
913           xcfi->dw_cfi_opc = fde->dw_fde_current_label
914                              ? DW_CFA_advance_loc4
915                              : DW_CFA_set_loc;
916           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
917           add_cfi (&fde->dw_fde_cfi, xcfi);
918
919           fde->dw_fde_current_label = label;
920         }
921
922       list_head = &fde->dw_fde_cfi;
923       any_cfis_emitted = true;
924     }
925
926   add_cfi (list_head, cfi);
927 }
928
929 /* Subroutine of lookup_cfa.  */
930
931 static void
932 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
933 {
934   switch (cfi->dw_cfi_opc)
935     {
936     case DW_CFA_def_cfa_offset:
937     case DW_CFA_def_cfa_offset_sf:
938       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
939       break;
940     case DW_CFA_def_cfa_register:
941       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
942       break;
943     case DW_CFA_def_cfa:
944     case DW_CFA_def_cfa_sf:
945       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
946       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
947       break;
948     case DW_CFA_def_cfa_expression:
949       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
950       break;
951
952     case DW_CFA_remember_state:
953       gcc_assert (!remember->in_use);
954       *remember = *loc;
955       remember->in_use = 1;
956       break;
957     case DW_CFA_restore_state:
958       gcc_assert (remember->in_use);
959       *loc = *remember;
960       remember->in_use = 0;
961       break;
962
963     default:
964       break;
965     }
966 }
967
968 /* Find the previous value for the CFA.  */
969
970 static void
971 lookup_cfa (dw_cfa_location *loc)
972 {
973   dw_cfi_ref cfi;
974   dw_fde_ref fde;
975   dw_cfa_location remember;
976
977   memset (loc, 0, sizeof (*loc));
978   loc->reg = INVALID_REGNUM;
979   remember = *loc;
980
981   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
982     lookup_cfa_1 (cfi, loc, &remember);
983
984   fde = current_fde ();
985   if (fde)
986     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
987       lookup_cfa_1 (cfi, loc, &remember);
988 }
989
990 /* The current rule for calculating the DWARF2 canonical frame address.  */
991 static dw_cfa_location cfa;
992
993 /* The register used for saving registers to the stack, and its offset
994    from the CFA.  */
995 static dw_cfa_location cfa_store;
996
997 /* The current save location around an epilogue.  */
998 static dw_cfa_location cfa_remember;
999
1000 /* The running total of the size of arguments pushed onto the stack.  */
1001 static HOST_WIDE_INT args_size;
1002
1003 /* The last args_size we actually output.  */
1004 static HOST_WIDE_INT old_args_size;
1005
1006 /* Entry point to update the canonical frame address (CFA).
1007    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1008    calculated from REG+OFFSET.  */
1009
1010 void
1011 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1012 {
1013   dw_cfa_location loc;
1014   loc.indirect = 0;
1015   loc.base_offset = 0;
1016   loc.reg = reg;
1017   loc.offset = offset;
1018   def_cfa_1 (label, &loc);
1019 }
1020
1021 /* Determine if two dw_cfa_location structures define the same data.  */
1022
1023 static bool
1024 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1025 {
1026   return (loc1->reg == loc2->reg
1027           && loc1->offset == loc2->offset
1028           && loc1->indirect == loc2->indirect
1029           && (loc1->indirect == 0
1030               || loc1->base_offset == loc2->base_offset));
1031 }
1032
1033 /* This routine does the actual work.  The CFA is now calculated from
1034    the dw_cfa_location structure.  */
1035
1036 static void
1037 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1038 {
1039   dw_cfi_ref cfi;
1040   dw_cfa_location old_cfa, loc;
1041
1042   cfa = *loc_p;
1043   loc = *loc_p;
1044
1045   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1046     cfa_store.offset = loc.offset;
1047
1048   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1049   lookup_cfa (&old_cfa);
1050
1051   /* If nothing changed, no need to issue any call frame instructions.  */
1052   if (cfa_equal_p (&loc, &old_cfa))
1053     return;
1054
1055   cfi = new_cfi ();
1056
1057   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1058     {
1059       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1060          the CFA register did not change but the offset did.  The data
1061          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1062          in the assembler via the .cfi_def_cfa_offset directive.  */
1063       if (loc.offset < 0)
1064         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1065       else
1066         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1067       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1068     }
1069
1070 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1071   else if (loc.offset == old_cfa.offset
1072            && old_cfa.reg != INVALID_REGNUM
1073            && !loc.indirect
1074            && !old_cfa.indirect)
1075     {
1076       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1077          indicating the CFA register has changed to <register> but the
1078          offset has not changed.  */
1079       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1080       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1081     }
1082 #endif
1083
1084   else if (loc.indirect == 0)
1085     {
1086       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1087          indicating the CFA register has changed to <register> with
1088          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1089          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1090          directive.  */
1091       if (loc.offset < 0)
1092         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1093       else
1094         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1095       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1096       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1097     }
1098   else
1099     {
1100       /* Construct a DW_CFA_def_cfa_expression instruction to
1101          calculate the CFA using a full location expression since no
1102          register-offset pair is available.  */
1103       struct dw_loc_descr_struct *loc_list;
1104
1105       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1106       loc_list = build_cfa_loc (&loc, 0);
1107       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1108     }
1109
1110   add_fde_cfi (label, cfi);
1111 }
1112
1113 /* Add the CFI for saving a register.  REG is the CFA column number.
1114    LABEL is passed to add_fde_cfi.
1115    If SREG is -1, the register is saved at OFFSET from the CFA;
1116    otherwise it is saved in SREG.  */
1117
1118 static void
1119 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1120 {
1121   dw_cfi_ref cfi = new_cfi ();
1122   dw_fde_ref fde = current_fde ();
1123
1124   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1125
1126   /* When stack is aligned, store REG using DW_CFA_expression with
1127      FP.  */
1128   if (fde
1129       && fde->stack_realign
1130       && sreg == INVALID_REGNUM)
1131     {
1132       cfi->dw_cfi_opc = DW_CFA_expression;
1133       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1134       cfi->dw_cfi_oprnd2.dw_cfi_loc
1135         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1136     }
1137   else if (sreg == INVALID_REGNUM)
1138     {
1139       if (need_data_align_sf_opcode (offset))
1140         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1141       else if (reg & ~0x3f)
1142         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1143       else
1144         cfi->dw_cfi_opc = DW_CFA_offset;
1145       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1146     }
1147   else if (sreg == reg)
1148     cfi->dw_cfi_opc = DW_CFA_same_value;
1149   else
1150     {
1151       cfi->dw_cfi_opc = DW_CFA_register;
1152       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1153     }
1154
1155   add_fde_cfi (label, cfi);
1156 }
1157
1158 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1159    This CFI tells the unwinder that it needs to restore the window registers
1160    from the previous frame's window save area.
1161
1162    ??? Perhaps we should note in the CIE where windows are saved (instead of
1163    assuming 0(cfa)) and what registers are in the window.  */
1164
1165 void
1166 dwarf2out_window_save (const char *label)
1167 {
1168   dw_cfi_ref cfi = new_cfi ();
1169
1170   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1171   add_fde_cfi (label, cfi);
1172 }
1173
1174 /* Entry point for saving a register to the stack.  REG is the GCC register
1175    number.  LABEL and OFFSET are passed to reg_save.  */
1176
1177 void
1178 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1179 {
1180   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1181 }
1182
1183 /* Entry point for saving the return address in the stack.
1184    LABEL and OFFSET are passed to reg_save.  */
1185
1186 void
1187 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1188 {
1189   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1190 }
1191
1192 /* Entry point for saving the return address in a register.
1193    LABEL and SREG are passed to reg_save.  */
1194
1195 void
1196 dwarf2out_return_reg (const char *label, unsigned int sreg)
1197 {
1198   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1199 }
1200
1201 /* Record the initial position of the return address.  RTL is
1202    INCOMING_RETURN_ADDR_RTX.  */
1203
1204 static void
1205 initial_return_save (rtx rtl)
1206 {
1207   unsigned int reg = INVALID_REGNUM;
1208   HOST_WIDE_INT offset = 0;
1209
1210   switch (GET_CODE (rtl))
1211     {
1212     case REG:
1213       /* RA is in a register.  */
1214       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1215       break;
1216
1217     case MEM:
1218       /* RA is on the stack.  */
1219       rtl = XEXP (rtl, 0);
1220       switch (GET_CODE (rtl))
1221         {
1222         case REG:
1223           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1224           offset = 0;
1225           break;
1226
1227         case PLUS:
1228           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1229           offset = INTVAL (XEXP (rtl, 1));
1230           break;
1231
1232         case MINUS:
1233           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1234           offset = -INTVAL (XEXP (rtl, 1));
1235           break;
1236
1237         default:
1238           gcc_unreachable ();
1239         }
1240
1241       break;
1242
1243     case PLUS:
1244       /* The return address is at some offset from any value we can
1245          actually load.  For instance, on the SPARC it is in %i7+8. Just
1246          ignore the offset for now; it doesn't matter for unwinding frames.  */
1247       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1248       initial_return_save (XEXP (rtl, 0));
1249       return;
1250
1251     default:
1252       gcc_unreachable ();
1253     }
1254
1255   if (reg != DWARF_FRAME_RETURN_COLUMN)
1256     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1257 }
1258
1259 /* Given a SET, calculate the amount of stack adjustment it
1260    contains.  */
1261
1262 static HOST_WIDE_INT
1263 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1264                      HOST_WIDE_INT cur_offset)
1265 {
1266   const_rtx src = SET_SRC (pattern);
1267   const_rtx dest = SET_DEST (pattern);
1268   HOST_WIDE_INT offset = 0;
1269   enum rtx_code code;
1270
1271   if (dest == stack_pointer_rtx)
1272     {
1273       code = GET_CODE (src);
1274
1275       /* Assume (set (reg sp) (reg whatever)) sets args_size
1276          level to 0.  */
1277       if (code == REG && src != stack_pointer_rtx)
1278         {
1279           offset = -cur_args_size;
1280 #ifndef STACK_GROWS_DOWNWARD
1281           offset = -offset;
1282 #endif
1283           return offset - cur_offset;
1284         }
1285
1286       if (! (code == PLUS || code == MINUS)
1287           || XEXP (src, 0) != stack_pointer_rtx
1288           || !CONST_INT_P (XEXP (src, 1)))
1289         return 0;
1290
1291       /* (set (reg sp) (plus (reg sp) (const_int))) */
1292       offset = INTVAL (XEXP (src, 1));
1293       if (code == PLUS)
1294         offset = -offset;
1295       return offset;
1296     }
1297
1298   if (MEM_P (src) && !MEM_P (dest))
1299     dest = src;
1300   if (MEM_P (dest))
1301     {
1302       /* (set (mem (pre_dec (reg sp))) (foo)) */
1303       src = XEXP (dest, 0);
1304       code = GET_CODE (src);
1305
1306       switch (code)
1307         {
1308         case PRE_MODIFY:
1309         case POST_MODIFY:
1310           if (XEXP (src, 0) == stack_pointer_rtx)
1311             {
1312               rtx val = XEXP (XEXP (src, 1), 1);
1313               /* We handle only adjustments by constant amount.  */
1314               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1315                           && CONST_INT_P (val));
1316               offset = -INTVAL (val);
1317               break;
1318             }
1319           return 0;
1320
1321         case PRE_DEC:
1322         case POST_DEC:
1323           if (XEXP (src, 0) == stack_pointer_rtx)
1324             {
1325               offset = GET_MODE_SIZE (GET_MODE (dest));
1326               break;
1327             }
1328           return 0;
1329
1330         case PRE_INC:
1331         case POST_INC:
1332           if (XEXP (src, 0) == stack_pointer_rtx)
1333             {
1334               offset = -GET_MODE_SIZE (GET_MODE (dest));
1335               break;
1336             }
1337           return 0;
1338
1339         default:
1340           return 0;
1341         }
1342     }
1343   else
1344     return 0;
1345
1346   return offset;
1347 }
1348
1349 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1350    indexed by INSN_UID.  */
1351
1352 static HOST_WIDE_INT *barrier_args_size;
1353
1354 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1355
1356 static HOST_WIDE_INT
1357 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1358                              VEC (rtx, heap) **next)
1359 {
1360   HOST_WIDE_INT offset = 0;
1361   int i;
1362
1363   if (! RTX_FRAME_RELATED_P (insn))
1364     {
1365       if (prologue_epilogue_contains (insn))
1366         /* Nothing */;
1367       else if (GET_CODE (PATTERN (insn)) == SET)
1368         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1369       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1370                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1371         {
1372           /* There may be stack adjustments inside compound insns.  Search
1373              for them.  */
1374           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1375             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1376               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1377                                              cur_args_size, offset);
1378         }
1379     }
1380   else
1381     {
1382       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1383
1384       if (expr)
1385         {
1386           expr = XEXP (expr, 0);
1387           if (GET_CODE (expr) == PARALLEL
1388               || GET_CODE (expr) == SEQUENCE)
1389             for (i = 1; i < XVECLEN (expr, 0); i++)
1390               {
1391                 rtx elem = XVECEXP (expr, 0, i);
1392
1393                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1394                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1395               }
1396         }
1397     }
1398
1399 #ifndef STACK_GROWS_DOWNWARD
1400   offset = -offset;
1401 #endif
1402
1403   cur_args_size += offset;
1404   if (cur_args_size < 0)
1405     cur_args_size = 0;
1406
1407   if (JUMP_P (insn))
1408     {
1409       rtx dest = JUMP_LABEL (insn);
1410
1411       if (dest)
1412         {
1413           if (barrier_args_size [INSN_UID (dest)] < 0)
1414             {
1415               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1416               VEC_safe_push (rtx, heap, *next, dest);
1417             }
1418         }
1419     }
1420
1421   return cur_args_size;
1422 }
1423
1424 /* Walk the whole function and compute args_size on BARRIERs.  */
1425
1426 static void
1427 compute_barrier_args_size (void)
1428 {
1429   int max_uid = get_max_uid (), i;
1430   rtx insn;
1431   VEC (rtx, heap) *worklist, *next, *tmp;
1432
1433   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1434   for (i = 0; i < max_uid; i++)
1435     barrier_args_size[i] = -1;
1436
1437   worklist = VEC_alloc (rtx, heap, 20);
1438   next = VEC_alloc (rtx, heap, 20);
1439   insn = get_insns ();
1440   barrier_args_size[INSN_UID (insn)] = 0;
1441   VEC_quick_push (rtx, worklist, insn);
1442   for (;;)
1443     {
1444       while (!VEC_empty (rtx, worklist))
1445         {
1446           rtx prev, body, first_insn;
1447           HOST_WIDE_INT cur_args_size;
1448
1449           first_insn = insn = VEC_pop (rtx, worklist);
1450           cur_args_size = barrier_args_size[INSN_UID (insn)];
1451           prev = prev_nonnote_insn (insn);
1452           if (prev && BARRIER_P (prev))
1453             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1454
1455           for (; insn; insn = NEXT_INSN (insn))
1456             {
1457               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1458                 continue;
1459               if (BARRIER_P (insn))
1460                 break;
1461
1462               if (LABEL_P (insn))
1463                 {
1464                   if (insn == first_insn)
1465                     continue;
1466                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1467                     {
1468                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1469                       continue;
1470                     }
1471                   else
1472                     {
1473                       /* The insns starting with this label have been
1474                          already scanned or are in the worklist.  */
1475                       break;
1476                     }
1477                 }
1478
1479               body = PATTERN (insn);
1480               if (GET_CODE (body) == SEQUENCE)
1481                 {
1482                   HOST_WIDE_INT dest_args_size = cur_args_size;
1483                   for (i = 1; i < XVECLEN (body, 0); i++)
1484                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1485                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1486                       dest_args_size
1487                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1488                                                        dest_args_size, &next);
1489                     else
1490                       cur_args_size
1491                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1492                                                        cur_args_size, &next);
1493
1494                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1495                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1496                                                  dest_args_size, &next);
1497                   else
1498                     cur_args_size
1499                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1500                                                      cur_args_size, &next);
1501                 }
1502               else
1503                 cur_args_size
1504                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1505             }
1506         }
1507
1508       if (VEC_empty (rtx, next))
1509         break;
1510
1511       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1512       tmp = next;
1513       next = worklist;
1514       worklist = tmp;
1515       VEC_truncate (rtx, next, 0);
1516     }
1517
1518   VEC_free (rtx, heap, worklist);
1519   VEC_free (rtx, heap, next);
1520 }
1521
1522 /* Add a CFI to update the running total of the size of arguments
1523    pushed onto the stack.  */
1524
1525 static void
1526 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1527 {
1528   dw_cfi_ref cfi;
1529
1530   if (size == old_args_size)
1531     return;
1532
1533   old_args_size = size;
1534
1535   cfi = new_cfi ();
1536   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1537   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1538   add_fde_cfi (label, cfi);
1539 }
1540
1541 /* Record a stack adjustment of OFFSET bytes.  */
1542
1543 static void
1544 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1545 {
1546   if (cfa.reg == STACK_POINTER_REGNUM)
1547     cfa.offset += offset;
1548
1549   if (cfa_store.reg == STACK_POINTER_REGNUM)
1550     cfa_store.offset += offset;
1551
1552   if (ACCUMULATE_OUTGOING_ARGS)
1553     return;
1554
1555 #ifndef STACK_GROWS_DOWNWARD
1556   offset = -offset;
1557 #endif
1558
1559   args_size += offset;
1560   if (args_size < 0)
1561     args_size = 0;
1562
1563   def_cfa_1 (label, &cfa);
1564   if (flag_asynchronous_unwind_tables)
1565     dwarf2out_args_size (label, args_size);
1566 }
1567
1568 /* Check INSN to see if it looks like a push or a stack adjustment, and
1569    make a note of it if it does.  EH uses this information to find out
1570    how much extra space it needs to pop off the stack.  */
1571
1572 static void
1573 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1574 {
1575   HOST_WIDE_INT offset;
1576   const char *label;
1577   int i;
1578
1579   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1580      with this function.  Proper support would require all frame-related
1581      insns to be marked, and to be able to handle saving state around
1582      epilogues textually in the middle of the function.  */
1583   if (prologue_epilogue_contains (insn))
1584     return;
1585
1586   /* If INSN is an instruction from target of an annulled branch, the
1587      effects are for the target only and so current argument size
1588      shouldn't change at all.  */
1589   if (final_sequence
1590       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1591       && INSN_FROM_TARGET_P (insn))
1592     return;
1593
1594   /* If only calls can throw, and we have a frame pointer,
1595      save up adjustments until we see the CALL_INSN.  */
1596   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1597     {
1598       if (CALL_P (insn) && !after_p)
1599         {
1600           /* Extract the size of the args from the CALL rtx itself.  */
1601           insn = PATTERN (insn);
1602           if (GET_CODE (insn) == PARALLEL)
1603             insn = XVECEXP (insn, 0, 0);
1604           if (GET_CODE (insn) == SET)
1605             insn = SET_SRC (insn);
1606           gcc_assert (GET_CODE (insn) == CALL);
1607           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1608         }
1609       return;
1610     }
1611
1612   if (CALL_P (insn) && !after_p)
1613     {
1614       if (!flag_asynchronous_unwind_tables)
1615         dwarf2out_args_size ("", args_size);
1616       return;
1617     }
1618   else if (BARRIER_P (insn))
1619     {
1620       /* Don't call compute_barrier_args_size () if the only
1621          BARRIER is at the end of function.  */
1622       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1623         compute_barrier_args_size ();
1624       if (barrier_args_size == NULL)
1625         offset = 0;
1626       else
1627         {
1628           offset = barrier_args_size[INSN_UID (insn)];
1629           if (offset < 0)
1630             offset = 0;
1631         }
1632
1633       offset -= args_size;
1634 #ifndef STACK_GROWS_DOWNWARD
1635       offset = -offset;
1636 #endif
1637     }
1638   else if (GET_CODE (PATTERN (insn)) == SET)
1639     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1640   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1641            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1642     {
1643       /* There may be stack adjustments inside compound insns.  Search
1644          for them.  */
1645       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1646         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1647           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1648                                          args_size, offset);
1649     }
1650   else
1651     return;
1652
1653   if (offset == 0)
1654     return;
1655
1656   label = dwarf2out_cfi_label (false);
1657   dwarf2out_stack_adjust (offset, label);
1658 }
1659
1660 /* We delay emitting a register save until either (a) we reach the end
1661    of the prologue or (b) the register is clobbered.  This clusters
1662    register saves so that there are fewer pc advances.  */
1663
1664 struct GTY(()) queued_reg_save {
1665   struct queued_reg_save *next;
1666   rtx reg;
1667   HOST_WIDE_INT cfa_offset;
1668   rtx saved_reg;
1669 };
1670
1671 static GTY(()) struct queued_reg_save *queued_reg_saves;
1672
1673 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1674 struct GTY(()) reg_saved_in_data {
1675   rtx orig_reg;
1676   rtx saved_in_reg;
1677 };
1678
1679 /* A list of registers saved in other registers.
1680    The list intentionally has a small maximum capacity of 4; if your
1681    port needs more than that, you might consider implementing a
1682    more efficient data structure.  */
1683 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1684 static GTY(()) size_t num_regs_saved_in_regs;
1685
1686 static const char *last_reg_save_label;
1687
1688 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1689    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1690
1691 static void
1692 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1693 {
1694   struct queued_reg_save *q;
1695
1696   /* Duplicates waste space, but it's also necessary to remove them
1697      for correctness, since the queue gets output in reverse
1698      order.  */
1699   for (q = queued_reg_saves; q != NULL; q = q->next)
1700     if (REGNO (q->reg) == REGNO (reg))
1701       break;
1702
1703   if (q == NULL)
1704     {
1705       q = ggc_alloc_queued_reg_save ();
1706       q->next = queued_reg_saves;
1707       queued_reg_saves = q;
1708     }
1709
1710   q->reg = reg;
1711   q->cfa_offset = offset;
1712   q->saved_reg = sreg;
1713
1714   last_reg_save_label = label;
1715 }
1716
1717 /* Output all the entries in QUEUED_REG_SAVES.  */
1718
1719 void
1720 dwarf2out_flush_queued_reg_saves (void)
1721 {
1722   struct queued_reg_save *q;
1723
1724   for (q = queued_reg_saves; q; q = q->next)
1725     {
1726       size_t i;
1727       unsigned int reg, sreg;
1728
1729       for (i = 0; i < num_regs_saved_in_regs; i++)
1730         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1731           break;
1732       if (q->saved_reg && i == num_regs_saved_in_regs)
1733         {
1734           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1735           num_regs_saved_in_regs++;
1736         }
1737       if (i != num_regs_saved_in_regs)
1738         {
1739           regs_saved_in_regs[i].orig_reg = q->reg;
1740           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1741         }
1742
1743       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1744       if (q->saved_reg)
1745         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1746       else
1747         sreg = INVALID_REGNUM;
1748       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1749     }
1750
1751   queued_reg_saves = NULL;
1752   last_reg_save_label = NULL;
1753 }
1754
1755 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1756    location for?  Or, does it clobber a register which we've previously
1757    said that some other register is saved in, and for which we now
1758    have a new location for?  */
1759
1760 static bool
1761 clobbers_queued_reg_save (const_rtx insn)
1762 {
1763   struct queued_reg_save *q;
1764
1765   for (q = queued_reg_saves; q; q = q->next)
1766     {
1767       size_t i;
1768       if (modified_in_p (q->reg, insn))
1769         return true;
1770       for (i = 0; i < num_regs_saved_in_regs; i++)
1771         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1772             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1773           return true;
1774     }
1775
1776   return false;
1777 }
1778
1779 /* Entry point for saving the first register into the second.  */
1780
1781 void
1782 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1783 {
1784   size_t i;
1785   unsigned int regno, sregno;
1786
1787   for (i = 0; i < num_regs_saved_in_regs; i++)
1788     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1789       break;
1790   if (i == num_regs_saved_in_regs)
1791     {
1792       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1793       num_regs_saved_in_regs++;
1794     }
1795   regs_saved_in_regs[i].orig_reg = reg;
1796   regs_saved_in_regs[i].saved_in_reg = sreg;
1797
1798   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1799   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1800   reg_save (label, regno, sregno, 0);
1801 }
1802
1803 /* What register, if any, is currently saved in REG?  */
1804
1805 static rtx
1806 reg_saved_in (rtx reg)
1807 {
1808   unsigned int regn = REGNO (reg);
1809   size_t i;
1810   struct queued_reg_save *q;
1811
1812   for (q = queued_reg_saves; q; q = q->next)
1813     if (q->saved_reg && regn == REGNO (q->saved_reg))
1814       return q->reg;
1815
1816   for (i = 0; i < num_regs_saved_in_regs; i++)
1817     if (regs_saved_in_regs[i].saved_in_reg
1818         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1819       return regs_saved_in_regs[i].orig_reg;
1820
1821   return NULL_RTX;
1822 }
1823
1824
1825 /* A temporary register holding an integral value used in adjusting SP
1826    or setting up the store_reg.  The "offset" field holds the integer
1827    value, not an offset.  */
1828 static dw_cfa_location cfa_temp;
1829
1830 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1831
1832 static void
1833 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1834 {
1835   memset (&cfa, 0, sizeof (cfa));
1836
1837   switch (GET_CODE (pat))
1838     {
1839     case PLUS:
1840       cfa.reg = REGNO (XEXP (pat, 0));
1841       cfa.offset = INTVAL (XEXP (pat, 1));
1842       break;
1843
1844     case REG:
1845       cfa.reg = REGNO (pat);
1846       break;
1847
1848     case MEM:
1849       cfa.indirect = 1;
1850       pat = XEXP (pat, 0);
1851       if (GET_CODE (pat) == PLUS)
1852         {
1853           cfa.base_offset = INTVAL (XEXP (pat, 1));
1854           pat = XEXP (pat, 0);
1855         }
1856       cfa.reg = REGNO (pat);
1857       break;
1858
1859     default:
1860       /* Recurse and define an expression.  */
1861       gcc_unreachable ();
1862     }
1863
1864   def_cfa_1 (label, &cfa);
1865 }
1866
1867 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1868
1869 static void
1870 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1871 {
1872   rtx src, dest;
1873
1874   gcc_assert (GET_CODE (pat) == SET);
1875   dest = XEXP (pat, 0);
1876   src = XEXP (pat, 1);
1877
1878   switch (GET_CODE (src))
1879     {
1880     case PLUS:
1881       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1882       cfa.offset -= INTVAL (XEXP (src, 1));
1883       break;
1884
1885     case REG:
1886         break;
1887
1888     default:
1889         gcc_unreachable ();
1890     }
1891
1892   cfa.reg = REGNO (dest);
1893   gcc_assert (cfa.indirect == 0);
1894
1895   def_cfa_1 (label, &cfa);
1896 }
1897
1898 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1899
1900 static void
1901 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1902 {
1903   HOST_WIDE_INT offset;
1904   rtx src, addr, span;
1905
1906   src = XEXP (set, 1);
1907   addr = XEXP (set, 0);
1908   gcc_assert (MEM_P (addr));
1909   addr = XEXP (addr, 0);
1910
1911   /* As documented, only consider extremely simple addresses.  */
1912   switch (GET_CODE (addr))
1913     {
1914     case REG:
1915       gcc_assert (REGNO (addr) == cfa.reg);
1916       offset = -cfa.offset;
1917       break;
1918     case PLUS:
1919       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1920       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1921       break;
1922     default:
1923       gcc_unreachable ();
1924     }
1925
1926   span = targetm.dwarf_register_span (src);
1927
1928   /* ??? We'd like to use queue_reg_save, but we need to come up with
1929      a different flushing heuristic for epilogues.  */
1930   if (!span)
1931     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1932   else
1933     {
1934       /* We have a PARALLEL describing where the contents of SRC live.
1935          Queue register saves for each piece of the PARALLEL.  */
1936       int par_index;
1937       int limit;
1938       HOST_WIDE_INT span_offset = offset;
1939
1940       gcc_assert (GET_CODE (span) == PARALLEL);
1941
1942       limit = XVECLEN (span, 0);
1943       for (par_index = 0; par_index < limit; par_index++)
1944         {
1945           rtx elem = XVECEXP (span, 0, par_index);
1946
1947           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1948                     INVALID_REGNUM, span_offset);
1949           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1950         }
1951     }
1952 }
1953
1954 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1955
1956 static void
1957 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1958 {
1959   rtx src, dest;
1960   unsigned sregno, dregno;
1961
1962   src = XEXP (set, 1);
1963   dest = XEXP (set, 0);
1964
1965   if (src == pc_rtx)
1966     sregno = DWARF_FRAME_RETURN_COLUMN;
1967   else
1968     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1969
1970   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1971
1972   /* ??? We'd like to use queue_reg_save, but we need to come up with
1973      a different flushing heuristic for epilogues.  */
1974   reg_save (label, sregno, dregno, 0);
1975 }
1976
1977 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1978
1979 static void
1980 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1981 {
1982   rtx src, dest, span;
1983   dw_cfi_ref cfi = new_cfi ();
1984
1985   dest = SET_DEST (set);
1986   src = SET_SRC (set);
1987
1988   gcc_assert (REG_P (src));
1989   gcc_assert (MEM_P (dest));
1990
1991   span = targetm.dwarf_register_span (src);
1992   gcc_assert (!span);
1993
1994   cfi->dw_cfi_opc = DW_CFA_expression;
1995   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1996   cfi->dw_cfi_oprnd2.dw_cfi_loc
1997     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1998                           VAR_INIT_STATUS_INITIALIZED);
1999
2000   /* ??? We'd like to use queue_reg_save, were the interface different,
2001      and, as above, we could manage flushing for epilogues.  */
2002   add_fde_cfi (label, cfi);
2003 }
2004
2005 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2006
2007 static void
2008 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2009 {
2010   dw_cfi_ref cfi = new_cfi ();
2011   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2012
2013   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2014   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2015
2016   add_fde_cfi (label, cfi);
2017 }
2018
2019 /* Record call frame debugging information for an expression EXPR,
2020    which either sets SP or FP (adjusting how we calculate the frame
2021    address) or saves a register to the stack or another register.
2022    LABEL indicates the address of EXPR.
2023
2024    This function encodes a state machine mapping rtxes to actions on
2025    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2026    users need not read the source code.
2027
2028   The High-Level Picture
2029
2030   Changes in the register we use to calculate the CFA: Currently we
2031   assume that if you copy the CFA register into another register, we
2032   should take the other one as the new CFA register; this seems to
2033   work pretty well.  If it's wrong for some target, it's simple
2034   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2035
2036   Changes in the register we use for saving registers to the stack:
2037   This is usually SP, but not always.  Again, we deduce that if you
2038   copy SP into another register (and SP is not the CFA register),
2039   then the new register is the one we will be using for register
2040   saves.  This also seems to work.
2041
2042   Register saves: There's not much guesswork about this one; if
2043   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2044   register save, and the register used to calculate the destination
2045   had better be the one we think we're using for this purpose.
2046   It's also assumed that a copy from a call-saved register to another
2047   register is saving that register if RTX_FRAME_RELATED_P is set on
2048   that instruction.  If the copy is from a call-saved register to
2049   the *same* register, that means that the register is now the same
2050   value as in the caller.
2051
2052   Except: If the register being saved is the CFA register, and the
2053   offset is nonzero, we are saving the CFA, so we assume we have to
2054   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2055   the intent is to save the value of SP from the previous frame.
2056
2057   In addition, if a register has previously been saved to a different
2058   register,
2059
2060   Invariants / Summaries of Rules
2061
2062   cfa          current rule for calculating the CFA.  It usually
2063                consists of a register and an offset.
2064   cfa_store    register used by prologue code to save things to the stack
2065                cfa_store.offset is the offset from the value of
2066                cfa_store.reg to the actual CFA
2067   cfa_temp     register holding an integral value.  cfa_temp.offset
2068                stores the value, which will be used to adjust the
2069                stack pointer.  cfa_temp is also used like cfa_store,
2070                to track stores to the stack via fp or a temp reg.
2071
2072   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2073                with cfa.reg as the first operand changes the cfa.reg and its
2074                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2075                cfa_temp.offset.
2076
2077   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2078                expression yielding a constant.  This sets cfa_temp.reg
2079                and cfa_temp.offset.
2080
2081   Rule 5:      Create a new register cfa_store used to save items to the
2082                stack.
2083
2084   Rules 10-14: Save a register to the stack.  Define offset as the
2085                difference of the original location and cfa_store's
2086                location (or cfa_temp's location if cfa_temp is used).
2087
2088   Rules 16-20: If AND operation happens on sp in prologue, we assume
2089                stack is realigned.  We will use a group of DW_OP_XXX
2090                expressions to represent the location of the stored
2091                register instead of CFA+offset.
2092
2093   The Rules
2094
2095   "{a,b}" indicates a choice of a xor b.
2096   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2097
2098   Rule 1:
2099   (set <reg1> <reg2>:cfa.reg)
2100   effects: cfa.reg = <reg1>
2101            cfa.offset unchanged
2102            cfa_temp.reg = <reg1>
2103            cfa_temp.offset = cfa.offset
2104
2105   Rule 2:
2106   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2107                               {<const_int>,<reg>:cfa_temp.reg}))
2108   effects: cfa.reg = sp if fp used
2109            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2110            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2111              if cfa_store.reg==sp
2112
2113   Rule 3:
2114   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2115   effects: cfa.reg = fp
2116            cfa_offset += +/- <const_int>
2117
2118   Rule 4:
2119   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2120   constraints: <reg1> != fp
2121                <reg1> != sp
2122   effects: cfa.reg = <reg1>
2123            cfa_temp.reg = <reg1>
2124            cfa_temp.offset = cfa.offset
2125
2126   Rule 5:
2127   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2128   constraints: <reg1> != fp
2129                <reg1> != sp
2130   effects: cfa_store.reg = <reg1>
2131            cfa_store.offset = cfa.offset - cfa_temp.offset
2132
2133   Rule 6:
2134   (set <reg> <const_int>)
2135   effects: cfa_temp.reg = <reg>
2136            cfa_temp.offset = <const_int>
2137
2138   Rule 7:
2139   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2140   effects: cfa_temp.reg = <reg1>
2141            cfa_temp.offset |= <const_int>
2142
2143   Rule 8:
2144   (set <reg> (high <exp>))
2145   effects: none
2146
2147   Rule 9:
2148   (set <reg> (lo_sum <exp> <const_int>))
2149   effects: cfa_temp.reg = <reg>
2150            cfa_temp.offset = <const_int>
2151
2152   Rule 10:
2153   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2154   effects: cfa_store.offset -= <const_int>
2155            cfa.offset = cfa_store.offset if cfa.reg == sp
2156            cfa.reg = sp
2157            cfa.base_offset = -cfa_store.offset
2158
2159   Rule 11:
2160   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2161   effects: cfa_store.offset += -/+ mode_size(mem)
2162            cfa.offset = cfa_store.offset if cfa.reg == sp
2163            cfa.reg = sp
2164            cfa.base_offset = -cfa_store.offset
2165
2166   Rule 12:
2167   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2168
2169        <reg2>)
2170   effects: cfa.reg = <reg1>
2171            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2172
2173   Rule 13:
2174   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2175   effects: cfa.reg = <reg1>
2176            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2177
2178   Rule 14:
2179   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2180   effects: cfa.reg = <reg1>
2181            cfa.base_offset = -cfa_temp.offset
2182            cfa_temp.offset -= mode_size(mem)
2183
2184   Rule 15:
2185   (set <reg> {unspec, unspec_volatile})
2186   effects: target-dependent
2187
2188   Rule 16:
2189   (set sp (and: sp <const_int>))
2190   constraints: cfa_store.reg == sp
2191   effects: current_fde.stack_realign = 1
2192            cfa_store.offset = 0
2193            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2194
2195   Rule 17:
2196   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2197   effects: cfa_store.offset += -/+ mode_size(mem)
2198
2199   Rule 18:
2200   (set (mem ({pre_inc, pre_dec} sp)) fp)
2201   constraints: fde->stack_realign == 1
2202   effects: cfa_store.offset = 0
2203            cfa.reg != HARD_FRAME_POINTER_REGNUM
2204
2205   Rule 19:
2206   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2207   constraints: fde->stack_realign == 1
2208                && cfa.offset == 0
2209                && cfa.indirect == 0
2210                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2211   effects: Use DW_CFA_def_cfa_expression to define cfa
2212            cfa.reg == fde->drap_reg  */
2213
2214 static void
2215 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2216 {
2217   rtx src, dest, span;
2218   HOST_WIDE_INT offset;
2219   dw_fde_ref fde;
2220
2221   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2222      the PARALLEL independently. The first element is always processed if
2223      it is a SET. This is for backward compatibility.   Other elements
2224      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2225      flag is set in them.  */
2226   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2227     {
2228       int par_index;
2229       int limit = XVECLEN (expr, 0);
2230       rtx elem;
2231
2232       /* PARALLELs have strict read-modify-write semantics, so we
2233          ought to evaluate every rvalue before changing any lvalue.
2234          It's cumbersome to do that in general, but there's an
2235          easy approximation that is enough for all current users:
2236          handle register saves before register assignments.  */
2237       if (GET_CODE (expr) == PARALLEL)
2238         for (par_index = 0; par_index < limit; par_index++)
2239           {
2240             elem = XVECEXP (expr, 0, par_index);
2241             if (GET_CODE (elem) == SET
2242                 && MEM_P (SET_DEST (elem))
2243                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2244               dwarf2out_frame_debug_expr (elem, label);
2245           }
2246
2247       for (par_index = 0; par_index < limit; par_index++)
2248         {
2249           elem = XVECEXP (expr, 0, par_index);
2250           if (GET_CODE (elem) == SET
2251               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2252               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2253             dwarf2out_frame_debug_expr (elem, label);
2254           else if (GET_CODE (elem) == SET
2255                    && par_index != 0
2256                    && !RTX_FRAME_RELATED_P (elem))
2257             {
2258               /* Stack adjustment combining might combine some post-prologue
2259                  stack adjustment into a prologue stack adjustment.  */
2260               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2261
2262               if (offset != 0)
2263                 dwarf2out_stack_adjust (offset, label);
2264             }
2265         }
2266       return;
2267     }
2268
2269   gcc_assert (GET_CODE (expr) == SET);
2270
2271   src = SET_SRC (expr);
2272   dest = SET_DEST (expr);
2273
2274   if (REG_P (src))
2275     {
2276       rtx rsi = reg_saved_in (src);
2277       if (rsi)
2278         src = rsi;
2279     }
2280
2281   fde = current_fde ();
2282
2283   switch (GET_CODE (dest))
2284     {
2285     case REG:
2286       switch (GET_CODE (src))
2287         {
2288           /* Setting FP from SP.  */
2289         case REG:
2290           if (cfa.reg == (unsigned) REGNO (src))
2291             {
2292               /* Rule 1 */
2293               /* Update the CFA rule wrt SP or FP.  Make sure src is
2294                  relative to the current CFA register.
2295
2296                  We used to require that dest be either SP or FP, but the
2297                  ARM copies SP to a temporary register, and from there to
2298                  FP.  So we just rely on the backends to only set
2299                  RTX_FRAME_RELATED_P on appropriate insns.  */
2300               cfa.reg = REGNO (dest);
2301               cfa_temp.reg = cfa.reg;
2302               cfa_temp.offset = cfa.offset;
2303             }
2304           else
2305             {
2306               /* Saving a register in a register.  */
2307               gcc_assert (!fixed_regs [REGNO (dest)]
2308                           /* For the SPARC and its register window.  */
2309                           || (DWARF_FRAME_REGNUM (REGNO (src))
2310                               == DWARF_FRAME_RETURN_COLUMN));
2311
2312               /* After stack is aligned, we can only save SP in FP
2313                  if drap register is used.  In this case, we have
2314                  to restore stack pointer with the CFA value and we
2315                  don't generate this DWARF information.  */
2316               if (fde
2317                   && fde->stack_realign
2318                   && REGNO (src) == STACK_POINTER_REGNUM)
2319                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2320                             && fde->drap_reg != INVALID_REGNUM
2321                             && cfa.reg != REGNO (src));
2322               else
2323                 queue_reg_save (label, src, dest, 0);
2324             }
2325           break;
2326
2327         case PLUS:
2328         case MINUS:
2329         case LO_SUM:
2330           if (dest == stack_pointer_rtx)
2331             {
2332               /* Rule 2 */
2333               /* Adjusting SP.  */
2334               switch (GET_CODE (XEXP (src, 1)))
2335                 {
2336                 case CONST_INT:
2337                   offset = INTVAL (XEXP (src, 1));
2338                   break;
2339                 case REG:
2340                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2341                               == cfa_temp.reg);
2342                   offset = cfa_temp.offset;
2343                   break;
2344                 default:
2345                   gcc_unreachable ();
2346                 }
2347
2348               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2349                 {
2350                   /* Restoring SP from FP in the epilogue.  */
2351                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2352                   cfa.reg = STACK_POINTER_REGNUM;
2353                 }
2354               else if (GET_CODE (src) == LO_SUM)
2355                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2356                 ;
2357               else
2358                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2359
2360               if (GET_CODE (src) != MINUS)
2361                 offset = -offset;
2362               if (cfa.reg == STACK_POINTER_REGNUM)
2363                 cfa.offset += offset;
2364               if (cfa_store.reg == STACK_POINTER_REGNUM)
2365                 cfa_store.offset += offset;
2366             }
2367           else if (dest == hard_frame_pointer_rtx)
2368             {
2369               /* Rule 3 */
2370               /* Either setting the FP from an offset of the SP,
2371                  or adjusting the FP */
2372               gcc_assert (frame_pointer_needed);
2373
2374               gcc_assert (REG_P (XEXP (src, 0))
2375                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2376                           && CONST_INT_P (XEXP (src, 1)));
2377               offset = INTVAL (XEXP (src, 1));
2378               if (GET_CODE (src) != MINUS)
2379                 offset = -offset;
2380               cfa.offset += offset;
2381               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2382             }
2383           else
2384             {
2385               gcc_assert (GET_CODE (src) != MINUS);
2386
2387               /* Rule 4 */
2388               if (REG_P (XEXP (src, 0))
2389                   && REGNO (XEXP (src, 0)) == cfa.reg
2390                   && CONST_INT_P (XEXP (src, 1)))
2391                 {
2392                   /* Setting a temporary CFA register that will be copied
2393                      into the FP later on.  */
2394                   offset = - INTVAL (XEXP (src, 1));
2395                   cfa.offset += offset;
2396                   cfa.reg = REGNO (dest);
2397                   /* Or used to save regs to the stack.  */
2398                   cfa_temp.reg = cfa.reg;
2399                   cfa_temp.offset = cfa.offset;
2400                 }
2401
2402               /* Rule 5 */
2403               else if (REG_P (XEXP (src, 0))
2404                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2405                        && XEXP (src, 1) == stack_pointer_rtx)
2406                 {
2407                   /* Setting a scratch register that we will use instead
2408                      of SP for saving registers to the stack.  */
2409                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2410                   cfa_store.reg = REGNO (dest);
2411                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2412                 }
2413
2414               /* Rule 9 */
2415               else if (GET_CODE (src) == LO_SUM
2416                        && CONST_INT_P (XEXP (src, 1)))
2417                 {
2418                   cfa_temp.reg = REGNO (dest);
2419                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2420                 }
2421               else
2422                 gcc_unreachable ();
2423             }
2424           break;
2425
2426           /* Rule 6 */
2427         case CONST_INT:
2428           cfa_temp.reg = REGNO (dest);
2429           cfa_temp.offset = INTVAL (src);
2430           break;
2431
2432           /* Rule 7 */
2433         case IOR:
2434           gcc_assert (REG_P (XEXP (src, 0))
2435                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2436                       && CONST_INT_P (XEXP (src, 1)));
2437
2438           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2439             cfa_temp.reg = REGNO (dest);
2440           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2441           break;
2442
2443           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2444              which will fill in all of the bits.  */
2445           /* Rule 8 */
2446         case HIGH:
2447           break;
2448
2449           /* Rule 15 */
2450         case UNSPEC:
2451         case UNSPEC_VOLATILE:
2452           gcc_assert (targetm.dwarf_handle_frame_unspec);
2453           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2454           return;
2455
2456           /* Rule 16 */
2457         case AND:
2458           /* If this AND operation happens on stack pointer in prologue,
2459              we assume the stack is realigned and we extract the
2460              alignment.  */
2461           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2462             {
2463               /* We interpret reg_save differently with stack_realign set.
2464                  Thus we must flush whatever we have queued first.  */
2465               dwarf2out_flush_queued_reg_saves ();
2466
2467               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2468               fde->stack_realign = 1;
2469               fde->stack_realignment = INTVAL (XEXP (src, 1));
2470               cfa_store.offset = 0;
2471
2472               if (cfa.reg != STACK_POINTER_REGNUM
2473                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2474                 fde->drap_reg = cfa.reg;
2475             }
2476           return;
2477
2478         default:
2479           gcc_unreachable ();
2480         }
2481
2482       def_cfa_1 (label, &cfa);
2483       break;
2484
2485     case MEM:
2486
2487       /* Saving a register to the stack.  Make sure dest is relative to the
2488          CFA register.  */
2489       switch (GET_CODE (XEXP (dest, 0)))
2490         {
2491           /* Rule 10 */
2492           /* With a push.  */
2493         case PRE_MODIFY:
2494           /* We can't handle variable size modifications.  */
2495           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2496                       == CONST_INT);
2497           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2498
2499           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2500                       && cfa_store.reg == STACK_POINTER_REGNUM);
2501
2502           cfa_store.offset += offset;
2503           if (cfa.reg == STACK_POINTER_REGNUM)
2504             cfa.offset = cfa_store.offset;
2505
2506           offset = -cfa_store.offset;
2507           break;
2508
2509           /* Rule 11 */
2510         case PRE_INC:
2511         case PRE_DEC:
2512           offset = GET_MODE_SIZE (GET_MODE (dest));
2513           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2514             offset = -offset;
2515
2516           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2517                        == STACK_POINTER_REGNUM)
2518                       && cfa_store.reg == STACK_POINTER_REGNUM);
2519
2520           cfa_store.offset += offset;
2521
2522           /* Rule 18: If stack is aligned, we will use FP as a
2523              reference to represent the address of the stored
2524              regiser.  */
2525           if (fde
2526               && fde->stack_realign
2527               && src == hard_frame_pointer_rtx)
2528             {
2529               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2530               cfa_store.offset = 0;
2531             }
2532
2533           if (cfa.reg == STACK_POINTER_REGNUM)
2534             cfa.offset = cfa_store.offset;
2535
2536           offset = -cfa_store.offset;
2537           break;
2538
2539           /* Rule 12 */
2540           /* With an offset.  */
2541         case PLUS:
2542         case MINUS:
2543         case LO_SUM:
2544           {
2545             int regno;
2546
2547             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2548                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2549             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2550             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2551               offset = -offset;
2552
2553             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2554
2555             if (cfa_store.reg == (unsigned) regno)
2556               offset -= cfa_store.offset;
2557             else
2558               {
2559                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2560                 offset -= cfa_temp.offset;
2561               }
2562           }
2563           break;
2564
2565           /* Rule 13 */
2566           /* Without an offset.  */
2567         case REG:
2568           {
2569             int regno = REGNO (XEXP (dest, 0));
2570
2571             if (cfa_store.reg == (unsigned) regno)
2572               offset = -cfa_store.offset;
2573             else
2574               {
2575                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2576                 offset = -cfa_temp.offset;
2577               }
2578           }
2579           break;
2580
2581           /* Rule 14 */
2582         case POST_INC:
2583           gcc_assert (cfa_temp.reg
2584                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2585           offset = -cfa_temp.offset;
2586           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2587           break;
2588
2589         default:
2590           gcc_unreachable ();
2591         }
2592
2593         /* Rule 17 */
2594         /* If the source operand of this MEM operation is not a
2595            register, basically the source is return address.  Here
2596            we only care how much stack grew and we don't save it.  */
2597       if (!REG_P (src))
2598         break;
2599
2600       if (REGNO (src) != STACK_POINTER_REGNUM
2601           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2602           && (unsigned) REGNO (src) == cfa.reg)
2603         {
2604           /* We're storing the current CFA reg into the stack.  */
2605
2606           if (cfa.offset == 0)
2607             {
2608               /* Rule 19 */
2609               /* If stack is aligned, putting CFA reg into stack means
2610                  we can no longer use reg + offset to represent CFA.
2611                  Here we use DW_CFA_def_cfa_expression instead.  The
2612                  result of this expression equals to the original CFA
2613                  value.  */
2614               if (fde
2615                   && fde->stack_realign
2616                   && cfa.indirect == 0
2617                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2618                 {
2619                   dw_cfa_location cfa_exp;
2620
2621                   gcc_assert (fde->drap_reg == cfa.reg);
2622
2623                   cfa_exp.indirect = 1;
2624                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2625                   cfa_exp.base_offset = offset;
2626                   cfa_exp.offset = 0;
2627
2628                   fde->drap_reg_saved = 1;
2629
2630                   def_cfa_1 (label, &cfa_exp);
2631                   break;
2632                 }
2633
2634               /* If the source register is exactly the CFA, assume
2635                  we're saving SP like any other register; this happens
2636                  on the ARM.  */
2637               def_cfa_1 (label, &cfa);
2638               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2639               break;
2640             }
2641           else
2642             {
2643               /* Otherwise, we'll need to look in the stack to
2644                  calculate the CFA.  */
2645               rtx x = XEXP (dest, 0);
2646
2647               if (!REG_P (x))
2648                 x = XEXP (x, 0);
2649               gcc_assert (REG_P (x));
2650
2651               cfa.reg = REGNO (x);
2652               cfa.base_offset = offset;
2653               cfa.indirect = 1;
2654               def_cfa_1 (label, &cfa);
2655               break;
2656             }
2657         }
2658
2659       def_cfa_1 (label, &cfa);
2660       {
2661         span = targetm.dwarf_register_span (src);
2662
2663         if (!span)
2664           queue_reg_save (label, src, NULL_RTX, offset);
2665         else
2666           {
2667             /* We have a PARALLEL describing where the contents of SRC
2668                live.  Queue register saves for each piece of the
2669                PARALLEL.  */
2670             int par_index;
2671             int limit;
2672             HOST_WIDE_INT span_offset = offset;
2673
2674             gcc_assert (GET_CODE (span) == PARALLEL);
2675
2676             limit = XVECLEN (span, 0);
2677             for (par_index = 0; par_index < limit; par_index++)
2678               {
2679                 rtx elem = XVECEXP (span, 0, par_index);
2680
2681                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2682                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2683               }
2684           }
2685       }
2686       break;
2687
2688     default:
2689       gcc_unreachable ();
2690     }
2691 }
2692
2693 /* Record call frame debugging information for INSN, which either
2694    sets SP or FP (adjusting how we calculate the frame address) or saves a
2695    register to the stack.  If INSN is NULL_RTX, initialize our state.
2696
2697    If AFTER_P is false, we're being called before the insn is emitted,
2698    otherwise after.  Call instructions get invoked twice.  */
2699
2700 void
2701 dwarf2out_frame_debug (rtx insn, bool after_p)
2702 {
2703   const char *label;
2704   rtx note, n;
2705   bool handled_one = false;
2706
2707   if (insn == NULL_RTX)
2708     {
2709       size_t i;
2710
2711       /* Flush any queued register saves.  */
2712       dwarf2out_flush_queued_reg_saves ();
2713
2714       /* Set up state for generating call frame debug info.  */
2715       lookup_cfa (&cfa);
2716       gcc_assert (cfa.reg
2717                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2718
2719       cfa.reg = STACK_POINTER_REGNUM;
2720       cfa_store = cfa;
2721       cfa_temp.reg = -1;
2722       cfa_temp.offset = 0;
2723
2724       for (i = 0; i < num_regs_saved_in_regs; i++)
2725         {
2726           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2727           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2728         }
2729       num_regs_saved_in_regs = 0;
2730
2731       if (barrier_args_size)
2732         {
2733           XDELETEVEC (barrier_args_size);
2734           barrier_args_size = NULL;
2735         }
2736       return;
2737     }
2738
2739   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2740     dwarf2out_flush_queued_reg_saves ();
2741
2742   if (!RTX_FRAME_RELATED_P (insn))
2743     {
2744       /* ??? This should be done unconditionally since stack adjustments
2745          matter if the stack pointer is not the CFA register anymore but
2746          is still used to save registers.  */
2747       if (!ACCUMULATE_OUTGOING_ARGS)
2748         dwarf2out_notice_stack_adjust (insn, after_p);
2749       return;
2750     }
2751
2752   label = dwarf2out_cfi_label (false);
2753   any_cfis_emitted = false;
2754
2755   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2756     switch (REG_NOTE_KIND (note))
2757       {
2758       case REG_FRAME_RELATED_EXPR:
2759         insn = XEXP (note, 0);
2760         goto found;
2761
2762       case REG_CFA_DEF_CFA:
2763         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2764         handled_one = true;
2765         break;
2766
2767       case REG_CFA_ADJUST_CFA:
2768         n = XEXP (note, 0);
2769         if (n == NULL)
2770           {
2771             n = PATTERN (insn);
2772             if (GET_CODE (n) == PARALLEL)
2773               n = XVECEXP (n, 0, 0);
2774           }
2775         dwarf2out_frame_debug_adjust_cfa (n, label);
2776         handled_one = true;
2777         break;
2778
2779       case REG_CFA_OFFSET:
2780         n = XEXP (note, 0);
2781         if (n == NULL)
2782           n = single_set (insn);
2783         dwarf2out_frame_debug_cfa_offset (n, label);
2784         handled_one = true;
2785         break;
2786
2787       case REG_CFA_REGISTER:
2788         n = XEXP (note, 0);
2789         if (n == NULL)
2790           {
2791             n = PATTERN (insn);
2792             if (GET_CODE (n) == PARALLEL)
2793               n = XVECEXP (n, 0, 0);
2794           }
2795         dwarf2out_frame_debug_cfa_register (n, label);
2796         handled_one = true;
2797         break;
2798
2799       case REG_CFA_EXPRESSION:
2800         n = XEXP (note, 0);
2801         if (n == NULL)
2802           n = single_set (insn);
2803         dwarf2out_frame_debug_cfa_expression (n, label);
2804         handled_one = true;
2805         break;
2806
2807       case REG_CFA_RESTORE:
2808         n = XEXP (note, 0);
2809         if (n == NULL)
2810           {
2811             n = PATTERN (insn);
2812             if (GET_CODE (n) == PARALLEL)
2813               n = XVECEXP (n, 0, 0);
2814             n = XEXP (n, 0);
2815           }
2816         dwarf2out_frame_debug_cfa_restore (n, label);
2817         handled_one = true;
2818         break;
2819
2820       case REG_CFA_SET_VDRAP:
2821         n = XEXP (note, 0);
2822         if (REG_P (n))
2823           {
2824             dw_fde_ref fde = current_fde ();
2825             if (fde)
2826               {
2827                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2828                 if (REG_P (n))
2829                   fde->vdrap_reg = REGNO (n);
2830               }
2831           }
2832         handled_one = true;
2833         break;
2834
2835       default:
2836         break;
2837       }
2838   if (handled_one)
2839     {
2840       if (any_cfis_emitted)
2841         dwarf2out_flush_queued_reg_saves ();
2842       return;
2843     }
2844
2845   insn = PATTERN (insn);
2846  found:
2847   dwarf2out_frame_debug_expr (insn, label);
2848
2849   /* Check again.  A parallel can save and update the same register.
2850      We could probably check just once, here, but this is safer than
2851      removing the check above.  */
2852   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2853     dwarf2out_flush_queued_reg_saves ();
2854 }
2855
2856 /* Determine if we need to save and restore CFI information around this
2857    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2858    we do need to save/restore, then emit the save now, and insert a
2859    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2860
2861 void
2862 dwarf2out_cfi_begin_epilogue (rtx insn)
2863 {
2864   bool saw_frp = false;
2865   rtx i;
2866
2867   /* Scan forward to the return insn, noticing if there are possible
2868      frame related insns.  */
2869   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2870     {
2871       if (!INSN_P (i))
2872         continue;
2873
2874       /* Look for both regular and sibcalls to end the block.  */
2875       if (returnjump_p (i))
2876         break;
2877       if (CALL_P (i) && SIBLING_CALL_P (i))
2878         break;
2879
2880       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2881         {
2882           int idx;
2883           rtx seq = PATTERN (i);
2884
2885           if (returnjump_p (XVECEXP (seq, 0, 0)))
2886             break;
2887           if (CALL_P (XVECEXP (seq, 0, 0))
2888               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2889             break;
2890
2891           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2892             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2893               saw_frp = true;
2894         }
2895
2896       if (RTX_FRAME_RELATED_P (i))
2897         saw_frp = true;
2898     }
2899
2900   /* If the port doesn't emit epilogue unwind info, we don't need a
2901      save/restore pair.  */
2902   if (!saw_frp)
2903     return;
2904
2905   /* Otherwise, search forward to see if the return insn was the last
2906      basic block of the function.  If so, we don't need save/restore.  */
2907   gcc_assert (i != NULL);
2908   i = next_real_insn (i);
2909   if (i == NULL)
2910     return;
2911
2912   /* Insert the restore before that next real insn in the stream, and before
2913      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2914      properly nested.  This should be after any label or alignment.  This
2915      will be pushed into the CFI stream by the function below.  */
2916   while (1)
2917     {
2918       rtx p = PREV_INSN (i);
2919       if (!NOTE_P (p))
2920         break;
2921       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2922         break;
2923       i = p;
2924     }
2925   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2926
2927   emit_cfa_remember = true;
2928
2929   /* And emulate the state save.  */
2930   gcc_assert (!cfa_remember.in_use);
2931   cfa_remember = cfa;
2932   cfa_remember.in_use = 1;
2933 }
2934
2935 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2936    required.  */
2937
2938 void
2939 dwarf2out_frame_debug_restore_state (void)
2940 {
2941   dw_cfi_ref cfi = new_cfi ();
2942   const char *label = dwarf2out_cfi_label (false);
2943
2944   cfi->dw_cfi_opc = DW_CFA_restore_state;
2945   add_fde_cfi (label, cfi);
2946
2947   gcc_assert (cfa_remember.in_use);
2948   cfa = cfa_remember;
2949   cfa_remember.in_use = 0;
2950 }
2951
2952 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2953 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2954  (enum dwarf_call_frame_info cfi);
2955
2956 static enum dw_cfi_oprnd_type
2957 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2958 {
2959   switch (cfi)
2960     {
2961     case DW_CFA_nop:
2962     case DW_CFA_GNU_window_save:
2963     case DW_CFA_remember_state:
2964     case DW_CFA_restore_state:
2965       return dw_cfi_oprnd_unused;
2966
2967     case DW_CFA_set_loc:
2968     case DW_CFA_advance_loc1:
2969     case DW_CFA_advance_loc2:
2970     case DW_CFA_advance_loc4:
2971     case DW_CFA_MIPS_advance_loc8:
2972       return dw_cfi_oprnd_addr;
2973
2974     case DW_CFA_offset:
2975     case DW_CFA_offset_extended:
2976     case DW_CFA_def_cfa:
2977     case DW_CFA_offset_extended_sf:
2978     case DW_CFA_def_cfa_sf:
2979     case DW_CFA_restore:
2980     case DW_CFA_restore_extended:
2981     case DW_CFA_undefined:
2982     case DW_CFA_same_value:
2983     case DW_CFA_def_cfa_register:
2984     case DW_CFA_register:
2985     case DW_CFA_expression:
2986       return dw_cfi_oprnd_reg_num;
2987
2988     case DW_CFA_def_cfa_offset:
2989     case DW_CFA_GNU_args_size:
2990     case DW_CFA_def_cfa_offset_sf:
2991       return dw_cfi_oprnd_offset;
2992
2993     case DW_CFA_def_cfa_expression:
2994       return dw_cfi_oprnd_loc;
2995
2996     default:
2997       gcc_unreachable ();
2998     }
2999 }
3000
3001 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3002 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3003  (enum dwarf_call_frame_info cfi);
3004
3005 static enum dw_cfi_oprnd_type
3006 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3007 {
3008   switch (cfi)
3009     {
3010     case DW_CFA_def_cfa:
3011     case DW_CFA_def_cfa_sf:
3012     case DW_CFA_offset:
3013     case DW_CFA_offset_extended_sf:
3014     case DW_CFA_offset_extended:
3015       return dw_cfi_oprnd_offset;
3016
3017     case DW_CFA_register:
3018       return dw_cfi_oprnd_reg_num;
3019
3020     case DW_CFA_expression:
3021       return dw_cfi_oprnd_loc;
3022
3023     default:
3024       return dw_cfi_oprnd_unused;
3025     }
3026 }
3027
3028 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3029    switch to the data section instead, and write out a synthetic start label
3030    for collect2 the first time around.  */
3031
3032 static void
3033 switch_to_eh_frame_section (bool back)
3034 {
3035   tree label;
3036
3037 #ifdef EH_FRAME_SECTION_NAME
3038   if (eh_frame_section == 0)
3039     {
3040       int flags;
3041
3042       if (EH_TABLES_CAN_BE_READ_ONLY)
3043         {
3044           int fde_encoding;
3045           int per_encoding;
3046           int lsda_encoding;
3047
3048           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3049                                                        /*global=*/0);
3050           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3051                                                        /*global=*/1);
3052           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3053                                                         /*global=*/0);
3054           flags = ((! flag_pic
3055                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3056                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3057                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3058                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3059                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3060                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3061                    ? 0 : SECTION_WRITE);
3062         }
3063       else
3064         flags = SECTION_WRITE;
3065       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3066     }
3067 #endif /* EH_FRAME_SECTION_NAME */
3068
3069   if (eh_frame_section)
3070     switch_to_section (eh_frame_section);
3071   else
3072     {
3073       /* We have no special eh_frame section.  Put the information in
3074          the data section and emit special labels to guide collect2.  */
3075       switch_to_section (data_section);
3076
3077       if (!back)
3078         {
3079           label = get_file_function_name ("F");
3080           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3081           targetm.asm_out.globalize_label (asm_out_file,
3082                                            IDENTIFIER_POINTER (label));
3083           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3084         }
3085     }
3086 }
3087
3088 /* Switch [BACK] to the eh or debug frame table section, depending on
3089    FOR_EH.  */
3090
3091 static void
3092 switch_to_frame_table_section (int for_eh, bool back)
3093 {
3094   if (for_eh)
3095     switch_to_eh_frame_section (back);
3096   else
3097     {
3098       if (!debug_frame_section)
3099         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3100                                            SECTION_DEBUG, NULL);
3101       switch_to_section (debug_frame_section);
3102     }
3103 }
3104
3105 /* Output a Call Frame Information opcode and its operand(s).  */
3106
3107 static void
3108 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3109 {
3110   unsigned long r;
3111   HOST_WIDE_INT off;
3112
3113   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3114     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3115                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3116                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3117                          ((unsigned HOST_WIDE_INT)
3118                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3119   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3120     {
3121       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3122       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3123                            "DW_CFA_offset, column %#lx", r);
3124       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3125       dw2_asm_output_data_uleb128 (off, NULL);
3126     }
3127   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3128     {
3129       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3131                            "DW_CFA_restore, column %#lx", r);
3132     }
3133   else
3134     {
3135       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3136                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3137
3138       switch (cfi->dw_cfi_opc)
3139         {
3140         case DW_CFA_set_loc:
3141           if (for_eh)
3142             dw2_asm_output_encoded_addr_rtx (
3143                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3144                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3145                 false, NULL);
3146           else
3147             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3148                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3149           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3150           break;
3151
3152         case DW_CFA_advance_loc1:
3153           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3154                                 fde->dw_fde_current_label, NULL);
3155           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3156           break;
3157
3158         case DW_CFA_advance_loc2:
3159           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3160                                 fde->dw_fde_current_label, NULL);
3161           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3162           break;
3163
3164         case DW_CFA_advance_loc4:
3165           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3166                                 fde->dw_fde_current_label, NULL);
3167           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3168           break;
3169
3170         case DW_CFA_MIPS_advance_loc8:
3171           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3172                                 fde->dw_fde_current_label, NULL);
3173           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3174           break;
3175
3176         case DW_CFA_offset_extended:
3177           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3178           dw2_asm_output_data_uleb128 (r, NULL);
3179           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3180           dw2_asm_output_data_uleb128 (off, NULL);
3181           break;
3182
3183         case DW_CFA_def_cfa:
3184           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3185           dw2_asm_output_data_uleb128 (r, NULL);
3186           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3187           break;
3188
3189         case DW_CFA_offset_extended_sf:
3190           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3191           dw2_asm_output_data_uleb128 (r, NULL);
3192           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3193           dw2_asm_output_data_sleb128 (off, NULL);
3194           break;
3195
3196         case DW_CFA_def_cfa_sf:
3197           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3198           dw2_asm_output_data_uleb128 (r, NULL);
3199           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3200           dw2_asm_output_data_sleb128 (off, NULL);
3201           break;
3202
3203         case DW_CFA_restore_extended:
3204         case DW_CFA_undefined:
3205         case DW_CFA_same_value:
3206         case DW_CFA_def_cfa_register:
3207           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3208           dw2_asm_output_data_uleb128 (r, NULL);
3209           break;
3210
3211         case DW_CFA_register:
3212           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3213           dw2_asm_output_data_uleb128 (r, NULL);
3214           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3215           dw2_asm_output_data_uleb128 (r, NULL);
3216           break;
3217
3218         case DW_CFA_def_cfa_offset:
3219         case DW_CFA_GNU_args_size:
3220           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3221           break;
3222
3223         case DW_CFA_def_cfa_offset_sf:
3224           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3225           dw2_asm_output_data_sleb128 (off, NULL);
3226           break;
3227
3228         case DW_CFA_GNU_window_save:
3229           break;
3230
3231         case DW_CFA_def_cfa_expression:
3232         case DW_CFA_expression:
3233           output_cfa_loc (cfi);
3234           break;
3235
3236         case DW_CFA_GNU_negative_offset_extended:
3237           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3238           gcc_unreachable ();
3239
3240         default:
3241           break;
3242         }
3243     }
3244 }
3245
3246 /* Similar, but do it via assembler directives instead.  */
3247
3248 static void
3249 output_cfi_directive (dw_cfi_ref cfi)
3250 {
3251   unsigned long r, r2;
3252
3253   switch (cfi->dw_cfi_opc)
3254     {
3255     case DW_CFA_advance_loc:
3256     case DW_CFA_advance_loc1:
3257     case DW_CFA_advance_loc2:
3258     case DW_CFA_advance_loc4:
3259     case DW_CFA_MIPS_advance_loc8:
3260     case DW_CFA_set_loc:
3261       /* Should only be created by add_fde_cfi in a code path not
3262          followed when emitting via directives.  The assembler is
3263          going to take care of this for us.  */
3264       gcc_unreachable ();
3265
3266     case DW_CFA_offset:
3267     case DW_CFA_offset_extended:
3268     case DW_CFA_offset_extended_sf:
3269       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3270       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3271                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3272       break;
3273
3274     case DW_CFA_restore:
3275     case DW_CFA_restore_extended:
3276       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3277       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3278       break;
3279
3280     case DW_CFA_undefined:
3281       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3282       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3283       break;
3284
3285     case DW_CFA_same_value:
3286       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3287       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3288       break;
3289
3290     case DW_CFA_def_cfa:
3291     case DW_CFA_def_cfa_sf:
3292       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3293       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3294                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3295       break;
3296
3297     case DW_CFA_def_cfa_register:
3298       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3299       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3300       break;
3301
3302     case DW_CFA_register:
3303       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3304       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3305       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3306       break;
3307
3308     case DW_CFA_def_cfa_offset:
3309     case DW_CFA_def_cfa_offset_sf:
3310       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3311                HOST_WIDE_INT_PRINT_DEC"\n",
3312                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3313       break;
3314
3315     case DW_CFA_remember_state:
3316       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3317       break;
3318     case DW_CFA_restore_state:
3319       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3320       break;
3321
3322     case DW_CFA_GNU_args_size:
3323       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3324       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3325       if (flag_debug_asm)
3326         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3327                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3328       fputc ('\n', asm_out_file);
3329       break;
3330
3331     case DW_CFA_GNU_window_save:
3332       fprintf (asm_out_file, "\t.cfi_window_save\n");
3333       break;
3334
3335     case DW_CFA_def_cfa_expression:
3336     case DW_CFA_expression:
3337       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3338       output_cfa_loc_raw (cfi);
3339       fputc ('\n', asm_out_file);
3340       break;
3341
3342     default:
3343       gcc_unreachable ();
3344     }
3345 }
3346
3347 DEF_VEC_P (dw_cfi_ref);
3348 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3349
3350 /* Output CFIs to bring current FDE to the same state as after executing
3351    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3352    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3353    other arguments to pass to output_cfi.  */
3354
3355 static void
3356 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3357 {
3358   struct dw_cfi_struct cfi_buf;
3359   dw_cfi_ref cfi2;
3360   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3361   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3362   unsigned int len, idx;
3363
3364   for (;; cfi = cfi->dw_cfi_next)
3365     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3366       {
3367       case DW_CFA_advance_loc:
3368       case DW_CFA_advance_loc1:
3369       case DW_CFA_advance_loc2:
3370       case DW_CFA_advance_loc4:
3371       case DW_CFA_MIPS_advance_loc8:
3372       case DW_CFA_set_loc:
3373         /* All advances should be ignored.  */
3374         break;
3375       case DW_CFA_remember_state:
3376         {
3377           dw_cfi_ref args_size = cfi_args_size;
3378
3379           /* Skip everything between .cfi_remember_state and
3380              .cfi_restore_state.  */
3381           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3382             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3383               break;
3384             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3385               args_size = cfi2;
3386             else
3387               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3388
3389           if (cfi2 == NULL)
3390             goto flush_all;
3391           else
3392             {
3393               cfi = cfi2;
3394               cfi_args_size = args_size;
3395             }
3396           break;
3397         }
3398       case DW_CFA_GNU_args_size:
3399         cfi_args_size = cfi;
3400         break;
3401       case DW_CFA_GNU_window_save:
3402         goto flush_all;
3403       case DW_CFA_offset:
3404       case DW_CFA_offset_extended:
3405       case DW_CFA_offset_extended_sf:
3406       case DW_CFA_restore:
3407       case DW_CFA_restore_extended:
3408       case DW_CFA_undefined:
3409       case DW_CFA_same_value:
3410       case DW_CFA_register:
3411       case DW_CFA_val_offset:
3412       case DW_CFA_val_offset_sf:
3413       case DW_CFA_expression:
3414       case DW_CFA_val_expression:
3415       case DW_CFA_GNU_negative_offset_extended:
3416         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3417           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3418                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3419         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3420         break;
3421       case DW_CFA_def_cfa:
3422       case DW_CFA_def_cfa_sf:
3423       case DW_CFA_def_cfa_expression:
3424         cfi_cfa = cfi;
3425         cfi_cfa_offset = cfi;
3426         break;
3427       case DW_CFA_def_cfa_register:
3428         cfi_cfa = cfi;
3429         break;
3430       case DW_CFA_def_cfa_offset:
3431       case DW_CFA_def_cfa_offset_sf:
3432         cfi_cfa_offset = cfi;
3433         break;
3434       case DW_CFA_nop:
3435         gcc_assert (cfi == NULL);
3436       flush_all:
3437         len = VEC_length (dw_cfi_ref, regs);
3438         for (idx = 0; idx < len; idx++)
3439           {
3440             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3441             if (cfi2 != NULL
3442                 && cfi2->dw_cfi_opc != DW_CFA_restore
3443                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3444               {
3445                 if (do_cfi_asm)
3446                   output_cfi_directive (cfi2);
3447                 else
3448                   output_cfi (cfi2, fde, for_eh);
3449               }
3450           }
3451         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3452           {
3453             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3454             cfi_buf = *cfi_cfa;
3455             switch (cfi_cfa_offset->dw_cfi_opc)
3456               {
3457               case DW_CFA_def_cfa_offset:
3458                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3459                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3460                 break;
3461               case DW_CFA_def_cfa_offset_sf:
3462                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3463                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3464                 break;
3465               case DW_CFA_def_cfa:
3466               case DW_CFA_def_cfa_sf:
3467                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3468                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3469                 break;
3470               default:
3471                 gcc_unreachable ();
3472               }
3473             cfi_cfa = &cfi_buf;
3474           }
3475         else if (cfi_cfa_offset)
3476           cfi_cfa = cfi_cfa_offset;
3477         if (cfi_cfa)
3478           {
3479             if (do_cfi_asm)
3480               output_cfi_directive (cfi_cfa);
3481             else
3482               output_cfi (cfi_cfa, fde, for_eh);
3483           }
3484         cfi_cfa = NULL;
3485         cfi_cfa_offset = NULL;
3486         if (cfi_args_size
3487             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3488           {
3489             if (do_cfi_asm)
3490               output_cfi_directive (cfi_args_size);
3491             else
3492               output_cfi (cfi_args_size, fde, for_eh);
3493           }
3494         cfi_args_size = NULL;
3495         if (cfi == NULL)
3496           {
3497             VEC_free (dw_cfi_ref, heap, regs);
3498             return;
3499           }
3500         else if (do_cfi_asm)
3501           output_cfi_directive (cfi);
3502         else
3503           output_cfi (cfi, fde, for_eh);
3504         break;
3505       default:
3506         gcc_unreachable ();
3507     }
3508 }
3509
3510 /* Output one FDE.  */
3511
3512 static void
3513 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3514             char *section_start_label, int fde_encoding, char *augmentation,
3515             bool any_lsda_needed, int lsda_encoding)
3516 {
3517   const char *begin, *end;
3518   static unsigned int j;
3519   char l1[20], l2[20];
3520   dw_cfi_ref cfi;
3521
3522   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3523                                      /* empty */ 0);
3524   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3525                                   for_eh + j);
3526   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3527   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3528   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3529     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3530                          " indicating 64-bit DWARF extension");
3531   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3532                         "FDE Length");
3533   ASM_OUTPUT_LABEL (asm_out_file, l1);
3534
3535   if (for_eh)
3536     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3537   else
3538     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3539                            debug_frame_section, "FDE CIE offset");
3540
3541   if (!fde->dw_fde_switched_sections)
3542     {
3543       begin = fde->dw_fde_begin;
3544       end = fde->dw_fde_end;
3545     }
3546   else
3547     {
3548       /* For the first section, prefer dw_fde_begin over
3549          dw_fde_{hot,cold}_section_label, as the latter
3550          might be separated from the real start of the
3551          function by alignment padding.  */
3552       if (!second)
3553         begin = fde->dw_fde_begin;
3554       else if (fde->dw_fde_switched_cold_to_hot)
3555         begin = fde->dw_fde_hot_section_label;
3556       else
3557         begin = fde->dw_fde_unlikely_section_label;
3558       if (second ^ fde->dw_fde_switched_cold_to_hot)
3559         end = fde->dw_fde_unlikely_section_end_label;
3560       else
3561         end = fde->dw_fde_hot_section_end_label;
3562     }
3563
3564   if (for_eh)
3565     {
3566       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3567       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3568       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3569                                        "FDE initial location");
3570       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3571                             end, begin, "FDE address range");
3572     }
3573   else
3574     {
3575       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3576       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3577     }
3578
3579   if (augmentation[0])
3580     {
3581       if (any_lsda_needed)
3582         {
3583           int size = size_of_encoded_value (lsda_encoding);
3584
3585           if (lsda_encoding == DW_EH_PE_aligned)
3586             {
3587               int offset = (  4         /* Length */
3588                             + 4         /* CIE offset */
3589                             + 2 * size_of_encoded_value (fde_encoding)
3590                             + 1         /* Augmentation size */ );
3591               int pad = -offset & (PTR_SIZE - 1);
3592
3593               size += pad;
3594               gcc_assert (size_of_uleb128 (size) == 1);
3595             }
3596
3597           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3598
3599           if (fde->uses_eh_lsda)
3600             {
3601               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3602                                            fde->funcdef_number);
3603               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3604                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3605                                                false,
3606                                                "Language Specific Data Area");
3607             }
3608           else
3609             {
3610               if (lsda_encoding == DW_EH_PE_aligned)
3611                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3612               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3613                                    "Language Specific Data Area (none)");
3614             }
3615         }
3616       else
3617         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3618     }
3619
3620   /* Loop through the Call Frame Instructions associated with
3621      this FDE.  */
3622   fde->dw_fde_current_label = begin;
3623   if (!fde->dw_fde_switched_sections)
3624     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3625       output_cfi (cfi, fde, for_eh);
3626   else if (!second)
3627     {
3628       if (fde->dw_fde_switch_cfi)
3629         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3630           {
3631             output_cfi (cfi, fde, for_eh);
3632             if (cfi == fde->dw_fde_switch_cfi)
3633               break;
3634           }
3635     }
3636   else
3637     {
3638       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3639
3640       if (fde->dw_fde_switch_cfi)
3641         {
3642           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3643           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3644           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3645           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3646         }
3647       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3648         output_cfi (cfi, fde, for_eh);
3649     }
3650
3651   /* If we are to emit a ref/link from function bodies to their frame tables,
3652      do it now.  This is typically performed to make sure that tables
3653      associated with functions are dragged with them and not discarded in
3654      garbage collecting links. We need to do this on a per function basis to
3655      cope with -ffunction-sections.  */
3656
3657 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3658   /* Switch to the function section, emit the ref to the tables, and
3659      switch *back* into the table section.  */
3660   switch_to_section (function_section (fde->decl));
3661   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3662   switch_to_frame_table_section (for_eh, true);
3663 #endif
3664
3665   /* Pad the FDE out to an address sized boundary.  */
3666   ASM_OUTPUT_ALIGN (asm_out_file,
3667                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3668   ASM_OUTPUT_LABEL (asm_out_file, l2);
3669
3670   j += 2;
3671 }
3672
3673 /* Return true if frame description entry FDE is needed for EH.  */
3674
3675 static bool
3676 fde_needed_for_eh_p (dw_fde_ref fde)
3677 {
3678   if (flag_asynchronous_unwind_tables)
3679     return true;
3680
3681   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3682     return true;
3683
3684   if (fde->uses_eh_lsda)
3685     return true;
3686
3687   /* If exceptions are enabled, we have collected nothrow info.  */
3688   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3689     return false;
3690
3691   return true;
3692 }
3693
3694 /* Output the call frame information used to record information
3695    that relates to calculating the frame pointer, and records the
3696    location of saved registers.  */
3697
3698 static void
3699 output_call_frame_info (int for_eh)
3700 {
3701   unsigned int i;
3702   dw_fde_ref fde;
3703   dw_cfi_ref cfi;
3704   char l1[20], l2[20], section_start_label[20];
3705   bool any_lsda_needed = false;
3706   char augmentation[6];
3707   int augmentation_size;
3708   int fde_encoding = DW_EH_PE_absptr;
3709   int per_encoding = DW_EH_PE_absptr;
3710   int lsda_encoding = DW_EH_PE_absptr;
3711   int return_reg;
3712   rtx personality = NULL;
3713   int dw_cie_version;
3714
3715   /* Don't emit a CIE if there won't be any FDEs.  */
3716   if (fde_table_in_use == 0)
3717     return;
3718
3719   /* Nothing to do if the assembler's doing it all.  */
3720   if (dwarf2out_do_cfi_asm ())
3721     return;
3722
3723   /* If we don't have any functions we'll want to unwind out of, don't emit
3724      any EH unwind information.  If we make FDEs linkonce, we may have to
3725      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3726      want to avoid having an FDE kept around when the function it refers to
3727      is discarded.  Example where this matters: a primary function template
3728      in C++ requires EH information, an explicit specialization doesn't.  */
3729   if (for_eh)
3730     {
3731       bool any_eh_needed = false;
3732
3733       for (i = 0; i < fde_table_in_use; i++)
3734         if (fde_table[i].uses_eh_lsda)
3735           any_eh_needed = any_lsda_needed = true;
3736         else if (fde_needed_for_eh_p (&fde_table[i]))
3737           any_eh_needed = true;
3738         else if (TARGET_USES_WEAK_UNWIND_INFO)
3739           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3740                                              1, 1);
3741
3742       if (!any_eh_needed)
3743         return;
3744     }
3745
3746   /* We're going to be generating comments, so turn on app.  */
3747   if (flag_debug_asm)
3748     app_enable ();
3749
3750   /* Switch to the proper frame section, first time.  */
3751   switch_to_frame_table_section (for_eh, false);
3752
3753   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3754   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3755
3756   /* Output the CIE.  */
3757   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3758   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3759   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3760     dw2_asm_output_data (4, 0xffffffff,
3761       "Initial length escape value indicating 64-bit DWARF extension");
3762   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3763                         "Length of Common Information Entry");
3764   ASM_OUTPUT_LABEL (asm_out_file, l1);
3765
3766   /* Now that the CIE pointer is PC-relative for EH,
3767      use 0 to identify the CIE.  */
3768   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3769                        (for_eh ? 0 : DWARF_CIE_ID),
3770                        "CIE Identifier Tag");
3771
3772   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3773      use CIE version 1, unless that would produce incorrect results
3774      due to overflowing the return register column.  */
3775   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3776   dw_cie_version = 1;
3777   if (return_reg >= 256 || dwarf_version > 2)
3778     dw_cie_version = 3;
3779   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3780
3781   augmentation[0] = 0;
3782   augmentation_size = 0;
3783
3784   personality = current_unit_personality;
3785   if (for_eh)
3786     {
3787       char *p;
3788
3789       /* Augmentation:
3790          z      Indicates that a uleb128 is present to size the
3791                 augmentation section.
3792          L      Indicates the encoding (and thus presence) of
3793                 an LSDA pointer in the FDE augmentation.
3794          R      Indicates a non-default pointer encoding for
3795                 FDE code pointers.
3796          P      Indicates the presence of an encoding + language
3797                 personality routine in the CIE augmentation.  */
3798
3799       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3800       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3801       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3802
3803       p = augmentation + 1;
3804       if (personality)
3805         {
3806           *p++ = 'P';
3807           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3808           assemble_external_libcall (personality);
3809         }
3810       if (any_lsda_needed)
3811         {
3812           *p++ = 'L';
3813           augmentation_size += 1;
3814         }
3815       if (fde_encoding != DW_EH_PE_absptr)
3816         {
3817           *p++ = 'R';
3818           augmentation_size += 1;
3819         }
3820       if (p > augmentation + 1)
3821         {
3822           augmentation[0] = 'z';
3823           *p = '\0';
3824         }
3825
3826       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3827       if (personality && per_encoding == DW_EH_PE_aligned)
3828         {
3829           int offset = (  4             /* Length */
3830                         + 4             /* CIE Id */
3831                         + 1             /* CIE version */
3832                         + strlen (augmentation) + 1     /* Augmentation */
3833                         + size_of_uleb128 (1)           /* Code alignment */
3834                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3835                         + 1             /* RA column */
3836                         + 1             /* Augmentation size */
3837                         + 1             /* Personality encoding */ );
3838           int pad = -offset & (PTR_SIZE - 1);
3839
3840           augmentation_size += pad;
3841
3842           /* Augmentations should be small, so there's scarce need to
3843              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3844           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3845         }
3846     }
3847
3848   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3849   if (dw_cie_version >= 4)
3850     {
3851       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3852       dw2_asm_output_data (1, 0, "CIE Segment Size");
3853     }
3854   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3855   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3856                                "CIE Data Alignment Factor");
3857
3858   if (dw_cie_version == 1)
3859     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3860   else
3861     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3862
3863   if (augmentation[0])
3864     {
3865       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3866       if (personality)
3867         {
3868           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3869                                eh_data_format_name (per_encoding));
3870           dw2_asm_output_encoded_addr_rtx (per_encoding,
3871                                            personality,
3872                                            true, NULL);
3873         }
3874
3875       if (any_lsda_needed)
3876         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3877                              eh_data_format_name (lsda_encoding));
3878
3879       if (fde_encoding != DW_EH_PE_absptr)
3880         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3881                              eh_data_format_name (fde_encoding));
3882     }
3883
3884   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3885     output_cfi (cfi, NULL, for_eh);
3886
3887   /* Pad the CIE out to an address sized boundary.  */
3888   ASM_OUTPUT_ALIGN (asm_out_file,
3889                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3890   ASM_OUTPUT_LABEL (asm_out_file, l2);
3891
3892   /* Loop through all of the FDE's.  */
3893   for (i = 0; i < fde_table_in_use; i++)
3894     {
3895       unsigned int k;
3896       fde = &fde_table[i];
3897
3898       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3899       if (for_eh && !fde_needed_for_eh_p (fde))
3900         continue;
3901
3902       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3903         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3904                     augmentation, any_lsda_needed, lsda_encoding);
3905     }
3906
3907   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3908     dw2_asm_output_data (4, 0, "End of Table");
3909 #ifdef MIPS_DEBUGGING_INFO
3910   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3911      get a value of 0.  Putting .align 0 after the label fixes it.  */
3912   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3913 #endif
3914
3915   /* Turn off app to make assembly quicker.  */
3916   if (flag_debug_asm)
3917     app_disable ();
3918 }
3919
3920 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3921
3922 static void
3923 dwarf2out_do_cfi_startproc (bool second)
3924 {
3925   int enc;
3926   rtx ref;
3927   rtx personality = get_personality_function (current_function_decl);
3928
3929   fprintf (asm_out_file, "\t.cfi_startproc\n");
3930
3931   if (personality)
3932     {
3933       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3934       ref = personality;
3935
3936       /* ??? The GAS support isn't entirely consistent.  We have to
3937          handle indirect support ourselves, but PC-relative is done
3938          in the assembler.  Further, the assembler can't handle any
3939          of the weirder relocation types.  */
3940       if (enc & DW_EH_PE_indirect)
3941         ref = dw2_force_const_mem (ref, true);
3942
3943       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3944       output_addr_const (asm_out_file, ref);
3945       fputc ('\n', asm_out_file);
3946     }
3947
3948   if (crtl->uses_eh_lsda)
3949     {
3950       char lab[20];
3951
3952       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3953       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3954                                    current_function_funcdef_no);
3955       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3956       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3957
3958       if (enc & DW_EH_PE_indirect)
3959         ref = dw2_force_const_mem (ref, true);
3960
3961       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3962       output_addr_const (asm_out_file, ref);
3963       fputc ('\n', asm_out_file);
3964     }
3965 }
3966
3967 /* Output a marker (i.e. a label) for the beginning of a function, before
3968    the prologue.  */
3969
3970 void
3971 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3972                           const char *file ATTRIBUTE_UNUSED)
3973 {
3974   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3975   char * dup_label;
3976   dw_fde_ref fde;
3977   section *fnsec;
3978   bool do_frame;
3979
3980   current_function_func_begin_label = NULL;
3981
3982   do_frame = dwarf2out_do_frame ();
3983
3984   /* ??? current_function_func_begin_label is also used by except.c for
3985      call-site information.  We must emit this label if it might be used.  */
3986   if (!do_frame
3987       && (!flag_exceptions
3988           || targetm.except_unwind_info () != UI_TARGET))
3989     return;
3990
3991   fnsec = function_section (current_function_decl);
3992   switch_to_section (fnsec);
3993   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3994                                current_function_funcdef_no);
3995   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3996                           current_function_funcdef_no);
3997   dup_label = xstrdup (label);
3998   current_function_func_begin_label = dup_label;
3999
4000   /* We can elide the fde allocation if we're not emitting debug info.  */
4001   if (!do_frame)
4002     return;
4003
4004   /* Expand the fde table if necessary.  */
4005   if (fde_table_in_use == fde_table_allocated)
4006     {
4007       fde_table_allocated += FDE_TABLE_INCREMENT;
4008       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4009       memset (fde_table + fde_table_in_use, 0,
4010               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4011     }
4012
4013   /* Record the FDE associated with this function.  */
4014   current_funcdef_fde = fde_table_in_use;
4015
4016   /* Add the new FDE at the end of the fde_table.  */
4017   fde = &fde_table[fde_table_in_use++];
4018   fde->decl = current_function_decl;
4019   fde->dw_fde_begin = dup_label;
4020   fde->dw_fde_current_label = dup_label;
4021   fde->dw_fde_hot_section_label = NULL;
4022   fde->dw_fde_hot_section_end_label = NULL;
4023   fde->dw_fde_unlikely_section_label = NULL;
4024   fde->dw_fde_unlikely_section_end_label = NULL;
4025   fde->dw_fde_switched_sections = 0;
4026   fde->dw_fde_switched_cold_to_hot = 0;
4027   fde->dw_fde_end = NULL;
4028   fde->dw_fde_vms_end_prologue = NULL;
4029   fde->dw_fde_vms_begin_epilogue = NULL;
4030   fde->dw_fde_cfi = NULL;
4031   fde->dw_fde_switch_cfi = NULL;
4032   fde->funcdef_number = current_function_funcdef_no;
4033   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4034   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4035   fde->nothrow = crtl->nothrow;
4036   fde->drap_reg = INVALID_REGNUM;
4037   fde->vdrap_reg = INVALID_REGNUM;
4038   if (flag_reorder_blocks_and_partition)
4039     {
4040       section *unlikelysec;
4041       if (first_function_block_is_cold)
4042         fde->in_std_section = 1;
4043       else
4044         fde->in_std_section
4045           = (fnsec == text_section
4046              || (cold_text_section && fnsec == cold_text_section));
4047       unlikelysec = unlikely_text_section ();
4048       fde->cold_in_std_section
4049         = (unlikelysec == text_section
4050            || (cold_text_section && unlikelysec == cold_text_section));
4051     }
4052   else
4053     {
4054       fde->in_std_section
4055         = (fnsec == text_section
4056            || (cold_text_section && fnsec == cold_text_section));
4057       fde->cold_in_std_section = 0;
4058     }
4059
4060   args_size = old_args_size = 0;
4061
4062   /* We only want to output line number information for the genuine dwarf2
4063      prologue case, not the eh frame case.  */
4064 #ifdef DWARF2_DEBUGGING_INFO
4065   if (file)
4066     dwarf2out_source_line (line, file, 0, true);
4067 #endif
4068
4069   if (dwarf2out_do_cfi_asm ())
4070     dwarf2out_do_cfi_startproc (false);
4071   else
4072     {
4073       rtx personality = get_personality_function (current_function_decl);
4074       if (!current_unit_personality)
4075         current_unit_personality = personality;
4076
4077       /* We cannot keep a current personality per function as without CFI
4078          asm, at the point where we emit the CFI data, there is no current
4079          function anymore.  */
4080       if (personality && current_unit_personality != personality)
4081         sorry ("multiple EH personalities are supported only with assemblers "
4082                "supporting .cfi_personality directive");
4083     }
4084 }
4085
4086 /* Output a marker (i.e. a label) for the end of the generated code
4087    for a function prologue.  This gets called *after* the prologue code has
4088    been generated.  */
4089
4090 void
4091 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4092                         const char *file ATTRIBUTE_UNUSED)
4093 {
4094   dw_fde_ref fde;
4095   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4096
4097   /* Output a label to mark the endpoint of the code generated for this
4098      function.  */
4099   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4100                                current_function_funcdef_no);
4101   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4102                           current_function_funcdef_no);
4103   fde = &fde_table[fde_table_in_use - 1];
4104   fde->dw_fde_vms_end_prologue = xstrdup (label);
4105 }
4106
4107 /* Output a marker (i.e. a label) for the beginning of the generated code
4108    for a function epilogue.  This gets called *before* the prologue code has
4109    been generated.  */
4110
4111 void
4112 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4113                           const char *file ATTRIBUTE_UNUSED)
4114 {
4115   dw_fde_ref fde;
4116   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4117
4118   fde = &fde_table[fde_table_in_use - 1];
4119   if (fde->dw_fde_vms_begin_epilogue)
4120     return;
4121
4122   /* Output a label to mark the endpoint of the code generated for this
4123      function.  */
4124   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4125                                current_function_funcdef_no);
4126   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4127                           current_function_funcdef_no);
4128   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4129 }
4130
4131 /* Output a marker (i.e. a label) for the absolute end of the generated code
4132    for a function definition.  This gets called *after* the epilogue code has
4133    been generated.  */
4134
4135 void
4136 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4137                         const char *file ATTRIBUTE_UNUSED)
4138 {
4139   dw_fde_ref fde;
4140   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4141
4142   last_var_location_insn = NULL_RTX;
4143
4144   if (dwarf2out_do_cfi_asm ())
4145     fprintf (asm_out_file, "\t.cfi_endproc\n");
4146
4147   /* Output a label to mark the endpoint of the code generated for this
4148      function.  */
4149   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4150                                current_function_funcdef_no);
4151   ASM_OUTPUT_LABEL (asm_out_file, label);
4152   fde = current_fde ();
4153   gcc_assert (fde != NULL);
4154   fde->dw_fde_end = xstrdup (label);
4155 }
4156
4157 void
4158 dwarf2out_frame_init (void)
4159 {
4160   /* Allocate the initial hunk of the fde_table.  */
4161   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4162   fde_table_allocated = FDE_TABLE_INCREMENT;
4163   fde_table_in_use = 0;
4164
4165   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4166      sake of lookup_cfa.  */
4167
4168   /* On entry, the Canonical Frame Address is at SP.  */
4169   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4170
4171   if (targetm.debug_unwind_info () == UI_DWARF2
4172       || targetm.except_unwind_info () == UI_DWARF2)
4173     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4174 }
4175
4176 void
4177 dwarf2out_frame_finish (void)
4178 {
4179   /* Output call frame information.  */
4180   if (targetm.debug_unwind_info () == UI_DWARF2)
4181     output_call_frame_info (0);
4182
4183   /* Output another copy for the unwinder.  */
4184   if ((flag_unwind_tables || flag_exceptions)
4185       && targetm.except_unwind_info () == UI_DWARF2)
4186     output_call_frame_info (1);
4187 }
4188
4189 /* Note that the current function section is being used for code.  */
4190
4191 static void
4192 dwarf2out_note_section_used (void)
4193 {
4194   section *sec = current_function_section ();
4195   if (sec == text_section)
4196     text_section_used = true;
4197   else if (sec == cold_text_section)
4198     cold_text_section_used = true;
4199 }
4200
4201 void
4202 dwarf2out_switch_text_section (void)
4203 {
4204   dw_fde_ref fde = current_fde ();
4205
4206   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4207
4208   fde->dw_fde_switched_sections = 1;
4209   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4210
4211   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4212   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4213   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4214   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4215   have_multiple_function_sections = true;
4216
4217   /* Reset the current label on switching text sections, so that we
4218      don't attempt to advance_loc4 between labels in different sections.  */
4219   fde->dw_fde_current_label = NULL;
4220
4221   /* There is no need to mark used sections when not debugging.  */
4222   if (cold_text_section != NULL)
4223     dwarf2out_note_section_used ();
4224
4225   if (dwarf2out_do_cfi_asm ())
4226     fprintf (asm_out_file, "\t.cfi_endproc\n");
4227
4228   /* Now do the real section switch.  */
4229   switch_to_section (current_function_section ());
4230
4231   if (dwarf2out_do_cfi_asm ())
4232     {
4233       dwarf2out_do_cfi_startproc (true);
4234       /* As this is a different FDE, insert all current CFI instructions
4235          again.  */
4236       output_cfis (fde->dw_fde_cfi, true, fde, true);
4237     }
4238   else
4239     {
4240       dw_cfi_ref cfi = fde->dw_fde_cfi;
4241
4242       cfi = fde->dw_fde_cfi;
4243       if (cfi)
4244         while (cfi->dw_cfi_next != NULL)
4245           cfi = cfi->dw_cfi_next;
4246       fde->dw_fde_switch_cfi = cfi;
4247     }
4248 }
4249 \f
4250 /* And now, the subset of the debugging information support code necessary
4251    for emitting location expressions.  */
4252
4253 /* Data about a single source file.  */
4254 struct GTY(()) dwarf_file_data {
4255   const char * filename;
4256   int emitted_number;
4257 };
4258
4259 typedef struct dw_val_struct *dw_val_ref;
4260 typedef struct die_struct *dw_die_ref;
4261 typedef const struct die_struct *const_dw_die_ref;
4262 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4263 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4264
4265 typedef struct GTY(()) deferred_locations_struct
4266 {
4267   tree variable;
4268   dw_die_ref die;
4269 } deferred_locations;
4270
4271 DEF_VEC_O(deferred_locations);
4272 DEF_VEC_ALLOC_O(deferred_locations,gc);
4273
4274 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4275
4276 DEF_VEC_P(dw_die_ref);
4277 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4278
4279 /* Each DIE may have a series of attribute/value pairs.  Values
4280    can take on several forms.  The forms that are used in this
4281    implementation are listed below.  */
4282
4283 enum dw_val_class
4284 {
4285   dw_val_class_addr,
4286   dw_val_class_offset,
4287   dw_val_class_loc,
4288   dw_val_class_loc_list,
4289   dw_val_class_range_list,
4290   dw_val_class_const,
4291   dw_val_class_unsigned_const,
4292   dw_val_class_const_double,
4293   dw_val_class_vec,
4294   dw_val_class_flag,
4295   dw_val_class_die_ref,
4296   dw_val_class_fde_ref,
4297   dw_val_class_lbl_id,
4298   dw_val_class_lineptr,
4299   dw_val_class_str,
4300   dw_val_class_macptr,
4301   dw_val_class_file,
4302   dw_val_class_data8,
4303   dw_val_class_decl_ref,
4304   dw_val_class_vms_delta
4305 };
4306
4307 /* Describe a floating point constant value, or a vector constant value.  */
4308
4309 typedef struct GTY(()) dw_vec_struct {
4310   unsigned char * GTY((length ("%h.length"))) array;
4311   unsigned length;
4312   unsigned elt_size;
4313 }
4314 dw_vec_const;
4315
4316 /* The dw_val_node describes an attribute's value, as it is
4317    represented internally.  */
4318
4319 typedef struct GTY(()) dw_val_struct {
4320   enum dw_val_class val_class;
4321   union dw_val_struct_union
4322     {
4323       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4324       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4325       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4326       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4327       HOST_WIDE_INT GTY ((default)) val_int;
4328       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4329       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4330       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4331       struct dw_val_die_union
4332         {
4333           dw_die_ref die;
4334           int external;
4335         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4336       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4337       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4338       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4339       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4340       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4341       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4342       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4343       struct dw_val_vms_delta_union
4344         {
4345           char * lbl1;
4346           char * lbl2;
4347         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4348     }
4349   GTY ((desc ("%1.val_class"))) v;
4350 }
4351 dw_val_node;
4352
4353 /* Locations in memory are described using a sequence of stack machine
4354    operations.  */
4355
4356 typedef struct GTY(()) dw_loc_descr_struct {
4357   dw_loc_descr_ref dw_loc_next;
4358   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4359   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4360      from DW_OP_addr with a dtp-relative symbol relocation.  */
4361   unsigned int dtprel : 1;
4362   int dw_loc_addr;
4363   dw_val_node dw_loc_oprnd1;
4364   dw_val_node dw_loc_oprnd2;
4365 }
4366 dw_loc_descr_node;
4367
4368 /* Location lists are ranges + location descriptions for that range,
4369    so you can track variables that are in different places over
4370    their entire life.  */
4371 typedef struct GTY(()) dw_loc_list_struct {
4372   dw_loc_list_ref dw_loc_next;
4373   const char *begin; /* Label for begin address of range */
4374   const char *end;  /* Label for end address of range */
4375   char *ll_symbol; /* Label for beginning of location list.
4376                       Only on head of list */
4377   const char *section; /* Section this loclist is relative to */
4378   dw_loc_descr_ref expr;
4379 } dw_loc_list_node;
4380
4381 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4382
4383 /* Convert a DWARF stack opcode into its string name.  */
4384
4385 static const char *
4386 dwarf_stack_op_name (unsigned int op)
4387 {
4388   switch (op)
4389     {
4390     case DW_OP_addr:
4391       return "DW_OP_addr";
4392     case DW_OP_deref:
4393       return "DW_OP_deref";
4394     case DW_OP_const1u:
4395       return "DW_OP_const1u";
4396     case DW_OP_const1s:
4397       return "DW_OP_const1s";
4398     case DW_OP_const2u:
4399       return "DW_OP_const2u";
4400     case DW_OP_const2s:
4401       return "DW_OP_const2s";
4402     case DW_OP_const4u:
4403       return "DW_OP_const4u";
4404     case DW_OP_const4s:
4405       return "DW_OP_const4s";
4406     case DW_OP_const8u:
4407       return "DW_OP_const8u";
4408     case DW_OP_const8s:
4409       return "DW_OP_const8s";
4410     case DW_OP_constu:
4411       return "DW_OP_constu";
4412     case DW_OP_consts:
4413       return "DW_OP_consts";
4414     case DW_OP_dup:
4415       return "DW_OP_dup";
4416     case DW_OP_drop:
4417       return "DW_OP_drop";
4418     case DW_OP_over:
4419       return "DW_OP_over";
4420     case DW_OP_pick:
4421       return "DW_OP_pick";
4422     case DW_OP_swap:
4423       return "DW_OP_swap";
4424     case DW_OP_rot:
4425       return "DW_OP_rot";
4426     case DW_OP_xderef:
4427       return "DW_OP_xderef";
4428     case DW_OP_abs:
4429       return "DW_OP_abs";
4430     case DW_OP_and:
4431       return "DW_OP_and";
4432     case DW_OP_div:
4433       return "DW_OP_div";
4434     case DW_OP_minus:
4435       return "DW_OP_minus";
4436     case DW_OP_mod:
4437       return "DW_OP_mod";
4438     case DW_OP_mul:
4439       return "DW_OP_mul";
4440     case DW_OP_neg:
4441       return "DW_OP_neg";
4442     case DW_OP_not:
4443       return "DW_OP_not";
4444     case DW_OP_or:
4445       return "DW_OP_or";
4446     case DW_OP_plus:
4447       return "DW_OP_plus";
4448     case DW_OP_plus_uconst:
4449       return "DW_OP_plus_uconst";
4450     case DW_OP_shl:
4451       return "DW_OP_shl";
4452     case DW_OP_shr:
4453       return "DW_OP_shr";
4454     case DW_OP_shra:
4455       return "DW_OP_shra";
4456     case DW_OP_xor:
4457       return "DW_OP_xor";
4458     case DW_OP_bra:
4459       return "DW_OP_bra";
4460     case DW_OP_eq:
4461       return "DW_OP_eq";
4462     case DW_OP_ge:
4463       return "DW_OP_ge";
4464     case DW_OP_gt:
4465       return "DW_OP_gt";
4466     case DW_OP_le:
4467       return "DW_OP_le";
4468     case DW_OP_lt:
4469       return "DW_OP_lt";
4470     case DW_OP_ne:
4471       return "DW_OP_ne";
4472     case DW_OP_skip:
4473       return "DW_OP_skip";
4474     case DW_OP_lit0:
4475       return "DW_OP_lit0";
4476     case DW_OP_lit1:
4477       return "DW_OP_lit1";
4478     case DW_OP_lit2:
4479       return "DW_OP_lit2";
4480     case DW_OP_lit3:
4481       return "DW_OP_lit3";
4482     case DW_OP_lit4:
4483       return "DW_OP_lit4";
4484     case DW_OP_lit5:
4485       return "DW_OP_lit5";
4486     case DW_OP_lit6:
4487       return "DW_OP_lit6";
4488     case DW_OP_lit7:
4489       return "DW_OP_lit7";
4490     case DW_OP_lit8:
4491       return "DW_OP_lit8";
4492     case DW_OP_lit9:
4493       return "DW_OP_lit9";
4494     case DW_OP_lit10:
4495       return "DW_OP_lit10";
4496     case DW_OP_lit11:
4497       return "DW_OP_lit11";
4498     case DW_OP_lit12:
4499       return "DW_OP_lit12";
4500     case DW_OP_lit13:
4501       return "DW_OP_lit13";
4502     case DW_OP_lit14:
4503       return "DW_OP_lit14";
4504     case DW_OP_lit15:
4505       return "DW_OP_lit15";
4506     case DW_OP_lit16:
4507       return "DW_OP_lit16";
4508     case DW_OP_lit17:
4509       return "DW_OP_lit17";
4510     case DW_OP_lit18:
4511       return "DW_OP_lit18";
4512     case DW_OP_lit19:
4513       return "DW_OP_lit19";
4514     case DW_OP_lit20:
4515       return "DW_OP_lit20";
4516     case DW_OP_lit21:
4517       return "DW_OP_lit21";
4518     case DW_OP_lit22:
4519       return "DW_OP_lit22";
4520     case DW_OP_lit23:
4521       return "DW_OP_lit23";
4522     case DW_OP_lit24:
4523       return "DW_OP_lit24";
4524     case DW_OP_lit25:
4525       return "DW_OP_lit25";
4526     case DW_OP_lit26:
4527       return "DW_OP_lit26";
4528     case DW_OP_lit27:
4529       return "DW_OP_lit27";
4530     case DW_OP_lit28:
4531       return "DW_OP_lit28";
4532     case DW_OP_lit29:
4533       return "DW_OP_lit29";
4534     case DW_OP_lit30:
4535       return "DW_OP_lit30";
4536     case DW_OP_lit31:
4537       return "DW_OP_lit31";
4538     case DW_OP_reg0:
4539       return "DW_OP_reg0";
4540     case DW_OP_reg1:
4541       return "DW_OP_reg1";
4542     case DW_OP_reg2:
4543       return "DW_OP_reg2";
4544     case DW_OP_reg3:
4545       return "DW_OP_reg3";
4546     case DW_OP_reg4:
4547       return "DW_OP_reg4";
4548     case DW_OP_reg5:
4549       return "DW_OP_reg5";
4550     case DW_OP_reg6:
4551       return "DW_OP_reg6";
4552     case DW_OP_reg7:
4553       return "DW_OP_reg7";
4554     case DW_OP_reg8:
4555       return "DW_OP_reg8";
4556     case DW_OP_reg9:
4557       return "DW_OP_reg9";
4558     case DW_OP_reg10:
4559       return "DW_OP_reg10";
4560     case DW_OP_reg11:
4561       return "DW_OP_reg11";
4562     case DW_OP_reg12:
4563       return "DW_OP_reg12";
4564     case DW_OP_reg13:
4565       return "DW_OP_reg13";
4566     case DW_OP_reg14:
4567       return "DW_OP_reg14";
4568     case DW_OP_reg15:
4569       return "DW_OP_reg15";
4570     case DW_OP_reg16:
4571       return "DW_OP_reg16";
4572     case DW_OP_reg17:
4573       return "DW_OP_reg17";
4574     case DW_OP_reg18:
4575       return "DW_OP_reg18";
4576     case DW_OP_reg19:
4577       return "DW_OP_reg19";
4578     case DW_OP_reg20:
4579       return "DW_OP_reg20";
4580     case DW_OP_reg21:
4581       return "DW_OP_reg21";
4582     case DW_OP_reg22:
4583       return "DW_OP_reg22";
4584     case DW_OP_reg23:
4585       return "DW_OP_reg23";
4586     case DW_OP_reg24:
4587       return "DW_OP_reg24";
4588     case DW_OP_reg25:
4589       return "DW_OP_reg25";
4590     case DW_OP_reg26:
4591       return "DW_OP_reg26";
4592     case DW_OP_reg27:
4593       return "DW_OP_reg27";
4594     case DW_OP_reg28:
4595       return "DW_OP_reg28";
4596     case DW_OP_reg29:
4597       return "DW_OP_reg29";
4598     case DW_OP_reg30:
4599       return "DW_OP_reg30";
4600     case DW_OP_reg31:
4601       return "DW_OP_reg31";
4602     case DW_OP_breg0:
4603       return "DW_OP_breg0";
4604     case DW_OP_breg1:
4605       return "DW_OP_breg1";
4606     case DW_OP_breg2:
4607       return "DW_OP_breg2";
4608     case DW_OP_breg3:
4609       return "DW_OP_breg3";
4610     case DW_OP_breg4:
4611       return "DW_OP_breg4";
4612     case DW_OP_breg5:
4613       return "DW_OP_breg5";
4614     case DW_OP_breg6:
4615       return "DW_OP_breg6";
4616     case DW_OP_breg7:
4617       return "DW_OP_breg7";
4618     case DW_OP_breg8:
4619       return "DW_OP_breg8";
4620     case DW_OP_breg9:
4621       return "DW_OP_breg9";
4622     case DW_OP_breg10:
4623       return "DW_OP_breg10";
4624     case DW_OP_breg11:
4625       return "DW_OP_breg11";
4626     case DW_OP_breg12:
4627       return "DW_OP_breg12";
4628     case DW_OP_breg13:
4629       return "DW_OP_breg13";
4630     case DW_OP_breg14:
4631       return "DW_OP_breg14";
4632     case DW_OP_breg15:
4633       return "DW_OP_breg15";
4634     case DW_OP_breg16:
4635       return "DW_OP_breg16";
4636     case DW_OP_breg17:
4637       return "DW_OP_breg17";
4638     case DW_OP_breg18:
4639       return "DW_OP_breg18";
4640     case DW_OP_breg19:
4641       return "DW_OP_breg19";
4642     case DW_OP_breg20:
4643       return "DW_OP_breg20";
4644     case DW_OP_breg21:
4645       return "DW_OP_breg21";
4646     case DW_OP_breg22:
4647       return "DW_OP_breg22";
4648     case DW_OP_breg23:
4649       return "DW_OP_breg23";
4650     case DW_OP_breg24:
4651       return "DW_OP_breg24";
4652     case DW_OP_breg25:
4653       return "DW_OP_breg25";
4654     case DW_OP_breg26:
4655       return "DW_OP_breg26";
4656     case DW_OP_breg27:
4657       return "DW_OP_breg27";
4658     case DW_OP_breg28:
4659       return "DW_OP_breg28";
4660     case DW_OP_breg29:
4661       return "DW_OP_breg29";
4662     case DW_OP_breg30:
4663       return "DW_OP_breg30";
4664     case DW_OP_breg31:
4665       return "DW_OP_breg31";
4666     case DW_OP_regx:
4667       return "DW_OP_regx";
4668     case DW_OP_fbreg:
4669       return "DW_OP_fbreg";
4670     case DW_OP_bregx:
4671       return "DW_OP_bregx";
4672     case DW_OP_piece:
4673       return "DW_OP_piece";
4674     case DW_OP_deref_size:
4675       return "DW_OP_deref_size";
4676     case DW_OP_xderef_size:
4677       return "DW_OP_xderef_size";
4678     case DW_OP_nop:
4679       return "DW_OP_nop";
4680
4681     case DW_OP_push_object_address:
4682       return "DW_OP_push_object_address";
4683     case DW_OP_call2:
4684       return "DW_OP_call2";
4685     case DW_OP_call4:
4686       return "DW_OP_call4";
4687     case DW_OP_call_ref:
4688       return "DW_OP_call_ref";
4689     case DW_OP_implicit_value:
4690       return "DW_OP_implicit_value";
4691     case DW_OP_stack_value:
4692       return "DW_OP_stack_value";
4693     case DW_OP_form_tls_address:
4694       return "DW_OP_form_tls_address";
4695     case DW_OP_call_frame_cfa:
4696       return "DW_OP_call_frame_cfa";
4697     case DW_OP_bit_piece:
4698       return "DW_OP_bit_piece";
4699
4700     case DW_OP_GNU_push_tls_address:
4701       return "DW_OP_GNU_push_tls_address";
4702     case DW_OP_GNU_uninit:
4703       return "DW_OP_GNU_uninit";
4704     case DW_OP_GNU_encoded_addr:
4705       return "DW_OP_GNU_encoded_addr";
4706     case DW_OP_GNU_implicit_pointer:
4707       return "DW_OP_GNU_implicit_pointer";
4708
4709     default:
4710       return "OP_<unknown>";
4711     }
4712 }
4713
4714 /* Return a pointer to a newly allocated location description.  Location
4715    descriptions are simple expression terms that can be strung
4716    together to form more complicated location (address) descriptions.  */
4717
4718 static inline dw_loc_descr_ref
4719 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4720                unsigned HOST_WIDE_INT oprnd2)
4721 {
4722   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4723
4724   descr->dw_loc_opc = op;
4725   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4726   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4727   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4728   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4729
4730   return descr;
4731 }
4732
4733 /* Return a pointer to a newly allocated location description for
4734    REG and OFFSET.  */
4735
4736 static inline dw_loc_descr_ref
4737 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4738 {
4739   if (reg <= 31)
4740     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4741                           offset, 0);
4742   else
4743     return new_loc_descr (DW_OP_bregx, reg, offset);
4744 }
4745
4746 /* Add a location description term to a location description expression.  */
4747
4748 static inline void
4749 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4750 {
4751   dw_loc_descr_ref *d;
4752
4753   /* Find the end of the chain.  */
4754   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4755     ;
4756
4757   *d = descr;
4758 }
4759
4760 /* Add a constant OFFSET to a location expression.  */
4761
4762 static void
4763 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4764 {
4765   dw_loc_descr_ref loc;
4766   HOST_WIDE_INT *p;
4767
4768   gcc_assert (*list_head != NULL);
4769
4770   if (!offset)
4771     return;
4772
4773   /* Find the end of the chain.  */
4774   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4775     ;
4776
4777   p = NULL;
4778   if (loc->dw_loc_opc == DW_OP_fbreg
4779       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4780     p = &loc->dw_loc_oprnd1.v.val_int;
4781   else if (loc->dw_loc_opc == DW_OP_bregx)
4782     p = &loc->dw_loc_oprnd2.v.val_int;
4783
4784   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4785      offset.  Don't optimize if an signed integer overflow would happen.  */
4786   if (p != NULL
4787       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4788           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4789     *p += offset;
4790
4791   else if (offset > 0)
4792     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4793
4794   else
4795     {
4796       loc->dw_loc_next = int_loc_descriptor (-offset);
4797       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4798     }
4799 }
4800
4801 /* Add a constant OFFSET to a location list.  */
4802
4803 static void
4804 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4805 {
4806   dw_loc_list_ref d;
4807   for (d = list_head; d != NULL; d = d->dw_loc_next)
4808     loc_descr_plus_const (&d->expr, offset);
4809 }
4810
4811 #define DWARF_REF_SIZE  \
4812   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4813
4814 /* Return the size of a location descriptor.  */
4815
4816 static unsigned long
4817 size_of_loc_descr (dw_loc_descr_ref loc)
4818 {
4819   unsigned long size = 1;
4820
4821   switch (loc->dw_loc_opc)
4822     {
4823     case DW_OP_addr:
4824       size += DWARF2_ADDR_SIZE;
4825       break;
4826     case DW_OP_const1u:
4827     case DW_OP_const1s:
4828       size += 1;
4829       break;
4830     case DW_OP_const2u:
4831     case DW_OP_const2s:
4832       size += 2;
4833       break;
4834     case DW_OP_const4u:
4835     case DW_OP_const4s:
4836       size += 4;
4837       break;
4838     case DW_OP_const8u:
4839     case DW_OP_const8s:
4840       size += 8;
4841       break;
4842     case DW_OP_constu:
4843       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4844       break;
4845     case DW_OP_consts:
4846       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4847       break;
4848     case DW_OP_pick:
4849       size += 1;
4850       break;
4851     case DW_OP_plus_uconst:
4852       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4853       break;
4854     case DW_OP_skip:
4855     case DW_OP_bra:
4856       size += 2;
4857       break;
4858     case DW_OP_breg0:
4859     case DW_OP_breg1:
4860     case DW_OP_breg2:
4861     case DW_OP_breg3:
4862     case DW_OP_breg4:
4863     case DW_OP_breg5:
4864     case DW_OP_breg6:
4865     case DW_OP_breg7:
4866     case DW_OP_breg8:
4867     case DW_OP_breg9:
4868     case DW_OP_breg10:
4869     case DW_OP_breg11:
4870     case DW_OP_breg12:
4871     case DW_OP_breg13:
4872     case DW_OP_breg14:
4873     case DW_OP_breg15:
4874     case DW_OP_breg16:
4875     case DW_OP_breg17:
4876     case DW_OP_breg18:
4877     case DW_OP_breg19:
4878     case DW_OP_breg20:
4879     case DW_OP_breg21:
4880     case DW_OP_breg22:
4881     case DW_OP_breg23:
4882     case DW_OP_breg24:
4883     case DW_OP_breg25:
4884     case DW_OP_breg26:
4885     case DW_OP_breg27:
4886     case DW_OP_breg28:
4887     case DW_OP_breg29:
4888     case DW_OP_breg30:
4889     case DW_OP_breg31:
4890       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4891       break;
4892     case DW_OP_regx:
4893       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4894       break;
4895     case DW_OP_fbreg:
4896       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4897       break;
4898     case DW_OP_bregx:
4899       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4900       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4901       break;
4902     case DW_OP_piece:
4903       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4904       break;
4905     case DW_OP_bit_piece:
4906       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4907       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4908       break;
4909     case DW_OP_deref_size:
4910     case DW_OP_xderef_size:
4911       size += 1;
4912       break;
4913     case DW_OP_call2:
4914       size += 2;
4915       break;
4916     case DW_OP_call4:
4917       size += 4;
4918       break;
4919     case DW_OP_call_ref:
4920       size += DWARF_REF_SIZE;
4921       break;
4922     case DW_OP_implicit_value:
4923       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4924               + loc->dw_loc_oprnd1.v.val_unsigned;
4925       break;
4926     case DW_OP_GNU_implicit_pointer:
4927       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4928       break;
4929     default:
4930       break;
4931     }
4932
4933   return size;
4934 }
4935
4936 /* Return the size of a series of location descriptors.  */
4937
4938 static unsigned long
4939 size_of_locs (dw_loc_descr_ref loc)
4940 {
4941   dw_loc_descr_ref l;
4942   unsigned long size;
4943
4944   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4945      field, to avoid writing to a PCH file.  */
4946   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4947     {
4948       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4949         break;
4950       size += size_of_loc_descr (l);
4951     }
4952   if (! l)
4953     return size;
4954
4955   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4956     {
4957       l->dw_loc_addr = size;
4958       size += size_of_loc_descr (l);
4959     }
4960
4961   return size;
4962 }
4963
4964 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4965 static void get_ref_die_offset_label (char *, dw_die_ref);
4966
4967 /* Output location description stack opcode's operands (if any).  */
4968
4969 static void
4970 output_loc_operands (dw_loc_descr_ref loc)
4971 {
4972   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4973   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4974
4975   switch (loc->dw_loc_opc)
4976     {
4977 #ifdef DWARF2_DEBUGGING_INFO
4978     case DW_OP_const2u:
4979     case DW_OP_const2s:
4980       dw2_asm_output_data (2, val1->v.val_int, NULL);
4981       break;
4982     case DW_OP_const4u:
4983       if (loc->dtprel)
4984         {
4985           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4986           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4987                                                val1->v.val_addr);
4988           fputc ('\n', asm_out_file);
4989           break;
4990         }
4991       /* FALLTHRU */
4992     case DW_OP_const4s:
4993       dw2_asm_output_data (4, val1->v.val_int, NULL);
4994       break;
4995     case DW_OP_const8u:
4996       if (loc->dtprel)
4997         {
4998           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4999           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5000                                                val1->v.val_addr);
5001           fputc ('\n', asm_out_file);
5002           break;
5003         }
5004       /* FALLTHRU */
5005     case DW_OP_const8s:
5006       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5007       dw2_asm_output_data (8, val1->v.val_int, NULL);
5008       break;
5009     case DW_OP_skip:
5010     case DW_OP_bra:
5011       {
5012         int offset;
5013
5014         gcc_assert (val1->val_class == dw_val_class_loc);
5015         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5016
5017         dw2_asm_output_data (2, offset, NULL);
5018       }
5019       break;
5020     case DW_OP_implicit_value:
5021       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5022       switch (val2->val_class)
5023         {
5024         case dw_val_class_const:
5025           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5026           break;
5027         case dw_val_class_vec:
5028           {
5029             unsigned int elt_size = val2->v.val_vec.elt_size;
5030             unsigned int len = val2->v.val_vec.length;
5031             unsigned int i;
5032             unsigned char *p;
5033
5034             if (elt_size > sizeof (HOST_WIDE_INT))
5035               {
5036                 elt_size /= 2;
5037                 len *= 2;
5038               }
5039             for (i = 0, p = val2->v.val_vec.array;
5040                  i < len;
5041                  i++, p += elt_size)
5042               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5043                                    "fp or vector constant word %u", i);
5044           }
5045           break;
5046         case dw_val_class_const_double:
5047           {
5048             unsigned HOST_WIDE_INT first, second;
5049
5050             if (WORDS_BIG_ENDIAN)
5051               {
5052                 first = val2->v.val_double.high;
5053                 second = val2->v.val_double.low;
5054               }
5055             else
5056               {
5057                 first = val2->v.val_double.low;
5058                 second = val2->v.val_double.high;
5059               }
5060             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5061                                  first, NULL);
5062             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5063                                  second, NULL);
5064           }
5065           break;
5066         case dw_val_class_addr:
5067           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5068           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5069           break;
5070         default:
5071           gcc_unreachable ();
5072         }
5073       break;
5074 #else
5075     case DW_OP_const2u:
5076     case DW_OP_const2s:
5077     case DW_OP_const4u:
5078     case DW_OP_const4s:
5079     case DW_OP_const8u:
5080     case DW_OP_const8s:
5081     case DW_OP_skip:
5082     case DW_OP_bra:
5083     case DW_OP_implicit_value:
5084       /* We currently don't make any attempt to make sure these are
5085          aligned properly like we do for the main unwind info, so
5086          don't support emitting things larger than a byte if we're
5087          only doing unwinding.  */
5088       gcc_unreachable ();
5089 #endif
5090     case DW_OP_const1u:
5091     case DW_OP_const1s:
5092       dw2_asm_output_data (1, val1->v.val_int, NULL);
5093       break;
5094     case DW_OP_constu:
5095       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5096       break;
5097     case DW_OP_consts:
5098       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5099       break;
5100     case DW_OP_pick:
5101       dw2_asm_output_data (1, val1->v.val_int, NULL);
5102       break;
5103     case DW_OP_plus_uconst:
5104       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5105       break;
5106     case DW_OP_breg0:
5107     case DW_OP_breg1:
5108     case DW_OP_breg2:
5109     case DW_OP_breg3:
5110     case DW_OP_breg4:
5111     case DW_OP_breg5:
5112     case DW_OP_breg6:
5113     case DW_OP_breg7:
5114     case DW_OP_breg8:
5115     case DW_OP_breg9:
5116     case DW_OP_breg10:
5117     case DW_OP_breg11:
5118     case DW_OP_breg12:
5119     case DW_OP_breg13:
5120     case DW_OP_breg14:
5121     case DW_OP_breg15:
5122     case DW_OP_breg16:
5123     case DW_OP_breg17:
5124     case DW_OP_breg18:
5125     case DW_OP_breg19:
5126     case DW_OP_breg20:
5127     case DW_OP_breg21:
5128     case DW_OP_breg22:
5129     case DW_OP_breg23:
5130     case DW_OP_breg24:
5131     case DW_OP_breg25:
5132     case DW_OP_breg26:
5133     case DW_OP_breg27:
5134     case DW_OP_breg28:
5135     case DW_OP_breg29:
5136     case DW_OP_breg30:
5137     case DW_OP_breg31:
5138       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5139       break;
5140     case DW_OP_regx:
5141       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5142       break;
5143     case DW_OP_fbreg:
5144       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5145       break;
5146     case DW_OP_bregx:
5147       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5148       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5149       break;
5150     case DW_OP_piece:
5151       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5152       break;
5153     case DW_OP_bit_piece:
5154       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5155       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5156       break;
5157     case DW_OP_deref_size:
5158     case DW_OP_xderef_size:
5159       dw2_asm_output_data (1, val1->v.val_int, NULL);
5160       break;
5161
5162     case DW_OP_addr:
5163       if (loc->dtprel)
5164         {
5165           if (targetm.asm_out.output_dwarf_dtprel)
5166             {
5167               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5168                                                    DWARF2_ADDR_SIZE,
5169                                                    val1->v.val_addr);
5170               fputc ('\n', asm_out_file);
5171             }
5172           else
5173             gcc_unreachable ();
5174         }
5175       else
5176         {
5177 #ifdef DWARF2_DEBUGGING_INFO
5178           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5179 #else
5180           gcc_unreachable ();
5181 #endif
5182         }
5183       break;
5184
5185     case DW_OP_GNU_implicit_pointer:
5186       {
5187         char label[MAX_ARTIFICIAL_LABEL_BYTES
5188                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5189         gcc_assert (val1->val_class == dw_val_class_die_ref);
5190         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5191         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5192         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5193       }
5194       break;
5195
5196     default:
5197       /* Other codes have no operands.  */
5198       break;
5199     }
5200 }
5201
5202 /* Output a sequence of location operations.  */
5203
5204 static void
5205 output_loc_sequence (dw_loc_descr_ref loc)
5206 {
5207   for (; loc != NULL; loc = loc->dw_loc_next)
5208     {
5209       /* Output the opcode.  */
5210       dw2_asm_output_data (1, loc->dw_loc_opc,
5211                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5212
5213       /* Output the operand(s) (if any).  */
5214       output_loc_operands (loc);
5215     }
5216 }
5217
5218 /* Output location description stack opcode's operands (if any).
5219    The output is single bytes on a line, suitable for .cfi_escape.  */
5220
5221 static void
5222 output_loc_operands_raw (dw_loc_descr_ref loc)
5223 {
5224   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5225   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5226
5227   switch (loc->dw_loc_opc)
5228     {
5229     case DW_OP_addr:
5230     case DW_OP_implicit_value:
5231       /* We cannot output addresses in .cfi_escape, only bytes.  */
5232       gcc_unreachable ();
5233
5234     case DW_OP_const1u:
5235     case DW_OP_const1s:
5236     case DW_OP_pick:
5237     case DW_OP_deref_size:
5238     case DW_OP_xderef_size:
5239       fputc (',', asm_out_file);
5240       dw2_asm_output_data_raw (1, val1->v.val_int);
5241       break;
5242
5243     case DW_OP_const2u:
5244     case DW_OP_const2s:
5245       fputc (',', asm_out_file);
5246       dw2_asm_output_data_raw (2, val1->v.val_int);
5247       break;
5248
5249     case DW_OP_const4u:
5250     case DW_OP_const4s:
5251       fputc (',', asm_out_file);
5252       dw2_asm_output_data_raw (4, val1->v.val_int);
5253       break;
5254
5255     case DW_OP_const8u:
5256     case DW_OP_const8s:
5257       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5258       fputc (',', asm_out_file);
5259       dw2_asm_output_data_raw (8, val1->v.val_int);
5260       break;
5261
5262     case DW_OP_skip:
5263     case DW_OP_bra:
5264       {
5265         int offset;
5266
5267         gcc_assert (val1->val_class == dw_val_class_loc);
5268         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5269
5270         fputc (',', asm_out_file);
5271         dw2_asm_output_data_raw (2, offset);
5272       }
5273       break;
5274
5275     case DW_OP_constu:
5276     case DW_OP_plus_uconst:
5277     case DW_OP_regx:
5278     case DW_OP_piece:
5279       fputc (',', asm_out_file);
5280       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5281       break;
5282
5283     case DW_OP_bit_piece:
5284       fputc (',', asm_out_file);
5285       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5286       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5287       break;
5288
5289     case DW_OP_consts:
5290     case DW_OP_breg0:
5291     case DW_OP_breg1:
5292     case DW_OP_breg2:
5293     case DW_OP_breg3:
5294     case DW_OP_breg4:
5295     case DW_OP_breg5:
5296     case DW_OP_breg6:
5297     case DW_OP_breg7:
5298     case DW_OP_breg8:
5299     case DW_OP_breg9:
5300     case DW_OP_breg10:
5301     case DW_OP_breg11:
5302     case DW_OP_breg12:
5303     case DW_OP_breg13:
5304     case DW_OP_breg14:
5305     case DW_OP_breg15:
5306     case DW_OP_breg16:
5307     case DW_OP_breg17:
5308     case DW_OP_breg18:
5309     case DW_OP_breg19:
5310     case DW_OP_breg20:
5311     case DW_OP_breg21:
5312     case DW_OP_breg22:
5313     case DW_OP_breg23:
5314     case DW_OP_breg24:
5315     case DW_OP_breg25:
5316     case DW_OP_breg26:
5317     case DW_OP_breg27:
5318     case DW_OP_breg28:
5319     case DW_OP_breg29:
5320     case DW_OP_breg30:
5321     case DW_OP_breg31:
5322     case DW_OP_fbreg:
5323       fputc (',', asm_out_file);
5324       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5325       break;
5326
5327     case DW_OP_bregx:
5328       fputc (',', asm_out_file);
5329       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5330       fputc (',', asm_out_file);
5331       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5332       break;
5333
5334     case DW_OP_GNU_implicit_pointer:
5335       gcc_unreachable ();
5336       break;
5337
5338     default:
5339       /* Other codes have no operands.  */
5340       break;
5341     }
5342 }
5343
5344 static void
5345 output_loc_sequence_raw (dw_loc_descr_ref loc)
5346 {
5347   while (1)
5348     {
5349       /* Output the opcode.  */
5350       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5351       output_loc_operands_raw (loc);
5352
5353       if (!loc->dw_loc_next)
5354         break;
5355       loc = loc->dw_loc_next;
5356
5357       fputc (',', asm_out_file);
5358     }
5359 }
5360
5361 /* This routine will generate the correct assembly data for a location
5362    description based on a cfi entry with a complex address.  */
5363
5364 static void
5365 output_cfa_loc (dw_cfi_ref cfi)
5366 {
5367   dw_loc_descr_ref loc;
5368   unsigned long size;
5369
5370   if (cfi->dw_cfi_opc == DW_CFA_expression)
5371     {
5372       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5373       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5374     }
5375   else
5376     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5377
5378   /* Output the size of the block.  */
5379   size = size_of_locs (loc);
5380   dw2_asm_output_data_uleb128 (size, NULL);
5381
5382   /* Now output the operations themselves.  */
5383   output_loc_sequence (loc);
5384 }
5385
5386 /* Similar, but used for .cfi_escape.  */
5387
5388 static void
5389 output_cfa_loc_raw (dw_cfi_ref cfi)
5390 {
5391   dw_loc_descr_ref loc;
5392   unsigned long size;
5393
5394   if (cfi->dw_cfi_opc == DW_CFA_expression)
5395     {
5396       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5397       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5398     }
5399   else
5400     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5401
5402   /* Output the size of the block.  */
5403   size = size_of_locs (loc);
5404   dw2_asm_output_data_uleb128_raw (size);
5405   fputc (',', asm_out_file);
5406
5407   /* Now output the operations themselves.  */
5408   output_loc_sequence_raw (loc);
5409 }
5410
5411 /* This function builds a dwarf location descriptor sequence from a
5412    dw_cfa_location, adding the given OFFSET to the result of the
5413    expression.  */
5414
5415 static struct dw_loc_descr_struct *
5416 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5417 {
5418   struct dw_loc_descr_struct *head, *tmp;
5419
5420   offset += cfa->offset;
5421
5422   if (cfa->indirect)
5423     {
5424       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5425       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5426       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5427       add_loc_descr (&head, tmp);
5428       if (offset != 0)
5429         {
5430           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5431           add_loc_descr (&head, tmp);
5432         }
5433     }
5434   else
5435     head = new_reg_loc_descr (cfa->reg, offset);
5436
5437   return head;
5438 }
5439
5440 /* This function builds a dwarf location descriptor sequence for
5441    the address at OFFSET from the CFA when stack is aligned to
5442    ALIGNMENT byte.  */
5443
5444 static struct dw_loc_descr_struct *
5445 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5446 {
5447   struct dw_loc_descr_struct *head;
5448   unsigned int dwarf_fp
5449     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5450
5451  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5452   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5453     {
5454       head = new_reg_loc_descr (dwarf_fp, 0);
5455       add_loc_descr (&head, int_loc_descriptor (alignment));
5456       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5457       loc_descr_plus_const (&head, offset);
5458     }
5459   else
5460     head = new_reg_loc_descr (dwarf_fp, offset);
5461   return head;
5462 }
5463
5464 /* This function fills in aa dw_cfa_location structure from a dwarf location
5465    descriptor sequence.  */
5466
5467 static void
5468 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5469 {
5470   struct dw_loc_descr_struct *ptr;
5471   cfa->offset = 0;
5472   cfa->base_offset = 0;
5473   cfa->indirect = 0;
5474   cfa->reg = -1;
5475
5476   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5477     {
5478       enum dwarf_location_atom op = ptr->dw_loc_opc;
5479
5480       switch (op)
5481         {
5482         case DW_OP_reg0:
5483         case DW_OP_reg1:
5484         case DW_OP_reg2:
5485         case DW_OP_reg3:
5486         case DW_OP_reg4:
5487         case DW_OP_reg5:
5488         case DW_OP_reg6:
5489         case DW_OP_reg7:
5490         case DW_OP_reg8:
5491         case DW_OP_reg9:
5492         case DW_OP_reg10:
5493         case DW_OP_reg11:
5494         case DW_OP_reg12:
5495         case DW_OP_reg13:
5496         case DW_OP_reg14:
5497         case DW_OP_reg15:
5498         case DW_OP_reg16:
5499         case DW_OP_reg17:
5500         case DW_OP_reg18:
5501         case DW_OP_reg19:
5502         case DW_OP_reg20:
5503         case DW_OP_reg21:
5504         case DW_OP_reg22:
5505         case DW_OP_reg23:
5506         case DW_OP_reg24:
5507         case DW_OP_reg25:
5508         case DW_OP_reg26:
5509         case DW_OP_reg27:
5510         case DW_OP_reg28:
5511         case DW_OP_reg29:
5512         case DW_OP_reg30:
5513         case DW_OP_reg31:
5514           cfa->reg = op - DW_OP_reg0;
5515           break;
5516         case DW_OP_regx:
5517           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5518           break;
5519         case DW_OP_breg0:
5520         case DW_OP_breg1:
5521         case DW_OP_breg2:
5522         case DW_OP_breg3:
5523         case DW_OP_breg4:
5524         case DW_OP_breg5:
5525         case DW_OP_breg6:
5526         case DW_OP_breg7:
5527         case DW_OP_breg8:
5528         case DW_OP_breg9:
5529         case DW_OP_breg10:
5530         case DW_OP_breg11:
5531         case DW_OP_breg12:
5532         case DW_OP_breg13:
5533         case DW_OP_breg14:
5534         case DW_OP_breg15:
5535         case DW_OP_breg16:
5536         case DW_OP_breg17:
5537         case DW_OP_breg18:
5538         case DW_OP_breg19:
5539         case DW_OP_breg20:
5540         case DW_OP_breg21:
5541         case DW_OP_breg22:
5542         case DW_OP_breg23:
5543         case DW_OP_breg24:
5544         case DW_OP_breg25:
5545         case DW_OP_breg26:
5546         case DW_OP_breg27:
5547         case DW_OP_breg28:
5548         case DW_OP_breg29:
5549         case DW_OP_breg30:
5550         case DW_OP_breg31:
5551           cfa->reg = op - DW_OP_breg0;
5552           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5553           break;
5554         case DW_OP_bregx:
5555           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5556           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5557           break;
5558         case DW_OP_deref:
5559           cfa->indirect = 1;
5560           break;
5561         case DW_OP_plus_uconst:
5562           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5563           break;
5564         default:
5565           internal_error ("DW_LOC_OP %s not implemented",
5566                           dwarf_stack_op_name (ptr->dw_loc_opc));
5567         }
5568     }
5569 }
5570 \f
5571 /* And now, the support for symbolic debugging information.  */
5572
5573 /* .debug_str support.  */
5574 static int output_indirect_string (void **, void *);
5575
5576 static void dwarf2out_init (const char *);
5577 static void dwarf2out_finish (const char *);
5578 static void dwarf2out_assembly_start (void);
5579 static void dwarf2out_define (unsigned int, const char *);
5580 static void dwarf2out_undef (unsigned int, const char *);
5581 static void dwarf2out_start_source_file (unsigned, const char *);
5582 static void dwarf2out_end_source_file (unsigned);
5583 static void dwarf2out_function_decl (tree);
5584 static void dwarf2out_begin_block (unsigned, unsigned);
5585 static void dwarf2out_end_block (unsigned, unsigned);
5586 static bool dwarf2out_ignore_block (const_tree);
5587 static void dwarf2out_global_decl (tree);
5588 static void dwarf2out_type_decl (tree, int);
5589 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5590 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5591                                                  dw_die_ref);
5592 static void dwarf2out_abstract_function (tree);
5593 static void dwarf2out_var_location (rtx);
5594 static void dwarf2out_direct_call (tree);
5595 static void dwarf2out_virtual_call_token (tree, int);
5596 static void dwarf2out_copy_call_info (rtx, rtx);
5597 static void dwarf2out_virtual_call (int);
5598 static void dwarf2out_begin_function (tree);
5599 static void dwarf2out_set_name (tree, tree);
5600
5601 /* The debug hooks structure.  */
5602
5603 const struct gcc_debug_hooks dwarf2_debug_hooks =
5604 {
5605   dwarf2out_init,
5606   dwarf2out_finish,
5607   dwarf2out_assembly_start,
5608   dwarf2out_define,
5609   dwarf2out_undef,
5610   dwarf2out_start_source_file,
5611   dwarf2out_end_source_file,
5612   dwarf2out_begin_block,
5613   dwarf2out_end_block,
5614   dwarf2out_ignore_block,
5615   dwarf2out_source_line,
5616   dwarf2out_begin_prologue,
5617 #if VMS_DEBUGGING_INFO
5618   dwarf2out_vms_end_prologue,
5619   dwarf2out_vms_begin_epilogue,
5620 #else
5621   debug_nothing_int_charstar,
5622   debug_nothing_int_charstar,
5623 #endif
5624   dwarf2out_end_epilogue,
5625   dwarf2out_begin_function,
5626   debug_nothing_int,            /* end_function */
5627   dwarf2out_function_decl,      /* function_decl */
5628   dwarf2out_global_decl,
5629   dwarf2out_type_decl,          /* type_decl */
5630   dwarf2out_imported_module_or_decl,
5631   debug_nothing_tree,           /* deferred_inline_function */
5632   /* The DWARF 2 backend tries to reduce debugging bloat by not
5633      emitting the abstract description of inline functions until
5634      something tries to reference them.  */
5635   dwarf2out_abstract_function,  /* outlining_inline_function */
5636   debug_nothing_rtx,            /* label */
5637   debug_nothing_int,            /* handle_pch */
5638   dwarf2out_var_location,
5639   dwarf2out_switch_text_section,
5640   dwarf2out_direct_call,
5641   dwarf2out_virtual_call_token,
5642   dwarf2out_copy_call_info,
5643   dwarf2out_virtual_call,
5644   dwarf2out_set_name,
5645   1                             /* start_end_main_source_file */
5646 };
5647 \f
5648 /* NOTE: In the comments in this file, many references are made to
5649    "Debugging Information Entries".  This term is abbreviated as `DIE'
5650    throughout the remainder of this file.  */
5651
5652 /* An internal representation of the DWARF output is built, and then
5653    walked to generate the DWARF debugging info.  The walk of the internal
5654    representation is done after the entire program has been compiled.
5655    The types below are used to describe the internal representation.  */
5656
5657 /* Various DIE's use offsets relative to the beginning of the
5658    .debug_info section to refer to each other.  */
5659
5660 typedef long int dw_offset;
5661
5662 /* Define typedefs here to avoid circular dependencies.  */
5663
5664 typedef struct dw_attr_struct *dw_attr_ref;
5665 typedef struct dw_line_info_struct *dw_line_info_ref;
5666 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5667 typedef struct pubname_struct *pubname_ref;
5668 typedef struct dw_ranges_struct *dw_ranges_ref;
5669 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5670 typedef struct comdat_type_struct *comdat_type_node_ref;
5671
5672 /* Each entry in the line_info_table maintains the file and
5673    line number associated with the label generated for that
5674    entry.  The label gives the PC value associated with
5675    the line number entry.  */
5676
5677 typedef struct GTY(()) dw_line_info_struct {
5678   unsigned long dw_file_num;
5679   unsigned long dw_line_num;
5680 }
5681 dw_line_info_entry;
5682
5683 /* Line information for functions in separate sections; each one gets its
5684    own sequence.  */
5685 typedef struct GTY(()) dw_separate_line_info_struct {
5686   unsigned long dw_file_num;
5687   unsigned long dw_line_num;
5688   unsigned long function;
5689 }
5690 dw_separate_line_info_entry;
5691
5692 /* Each DIE attribute has a field specifying the attribute kind,
5693    a link to the next attribute in the chain, and an attribute value.
5694    Attributes are typically linked below the DIE they modify.  */
5695
5696 typedef struct GTY(()) dw_attr_struct {
5697   enum dwarf_attribute dw_attr;
5698   dw_val_node dw_attr_val;
5699 }
5700 dw_attr_node;
5701
5702 DEF_VEC_O(dw_attr_node);
5703 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5704
5705 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5706    The children of each node form a circular list linked by
5707    die_sib.  die_child points to the node *before* the "first" child node.  */
5708
5709 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5710   union die_symbol_or_type_node
5711     {
5712       char * GTY ((tag ("0"))) die_symbol;
5713       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5714     }
5715   GTY ((desc ("dwarf_version >= 4"))) die_id;
5716   VEC(dw_attr_node,gc) * die_attr;
5717   dw_die_ref die_parent;
5718   dw_die_ref die_child;
5719   dw_die_ref die_sib;
5720   dw_die_ref die_definition; /* ref from a specification to its definition */
5721   dw_offset die_offset;
5722   unsigned long die_abbrev;
5723   int die_mark;
5724   /* Die is used and must not be pruned as unused.  */
5725   int die_perennial_p;
5726   unsigned int decl_id;
5727   enum dwarf_tag die_tag;
5728 }
5729 die_node;
5730
5731 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5732 #define FOR_EACH_CHILD(die, c, expr) do {       \
5733   c = die->die_child;                           \
5734   if (c) do {                                   \
5735     c = c->die_sib;                             \
5736     expr;                                       \
5737   } while (c != die->die_child);                \
5738 } while (0)
5739
5740 /* The pubname structure */
5741
5742 typedef struct GTY(()) pubname_struct {
5743   dw_die_ref die;
5744   const char *name;
5745 }
5746 pubname_entry;
5747
5748 DEF_VEC_O(pubname_entry);
5749 DEF_VEC_ALLOC_O(pubname_entry, gc);
5750
5751 struct GTY(()) dw_ranges_struct {
5752   /* If this is positive, it's a block number, otherwise it's a
5753      bitwise-negated index into dw_ranges_by_label.  */
5754   int num;
5755 };
5756
5757 struct GTY(()) dw_ranges_by_label_struct {
5758   const char *begin;
5759   const char *end;
5760 };
5761
5762 /* The comdat type node structure.  */
5763 typedef struct GTY(()) comdat_type_struct
5764 {
5765   dw_die_ref root_die;
5766   dw_die_ref type_die;
5767   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5768   struct comdat_type_struct *next;
5769 }
5770 comdat_type_node;
5771
5772 /* The limbo die list structure.  */
5773 typedef struct GTY(()) limbo_die_struct {
5774   dw_die_ref die;
5775   tree created_for;
5776   struct limbo_die_struct *next;
5777 }
5778 limbo_die_node;
5779
5780 typedef struct GTY(()) skeleton_chain_struct
5781 {
5782   dw_die_ref old_die;
5783   dw_die_ref new_die;
5784   struct skeleton_chain_struct *parent;
5785 }
5786 skeleton_chain_node;
5787
5788 /* How to start an assembler comment.  */
5789 #ifndef ASM_COMMENT_START
5790 #define ASM_COMMENT_START ";#"
5791 #endif
5792
5793 /* Define a macro which returns nonzero for a TYPE_DECL which was
5794    implicitly generated for a tagged type.
5795
5796    Note that unlike the gcc front end (which generates a NULL named
5797    TYPE_DECL node for each complete tagged type, each array type, and
5798    each function type node created) the g++ front end generates a
5799    _named_ TYPE_DECL node for each tagged type node created.
5800    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5801    generate a DW_TAG_typedef DIE for them.  */
5802
5803 #define TYPE_DECL_IS_STUB(decl)                         \
5804   (DECL_NAME (decl) == NULL_TREE                        \
5805    || (DECL_ARTIFICIAL (decl)                           \
5806        && is_tagged_type (TREE_TYPE (decl))             \
5807        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5808            /* This is necessary for stub decls that     \
5809               appear in nested inline functions.  */    \
5810            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5811                && (decl_ultimate_origin (decl)          \
5812                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5813
5814 /* Information concerning the compilation unit's programming
5815    language, and compiler version.  */
5816
5817 /* Fixed size portion of the DWARF compilation unit header.  */
5818 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5819   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5820
5821 /* Fixed size portion of the DWARF comdat type unit header.  */
5822 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5823   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5824    + DWARF_OFFSET_SIZE)
5825
5826 /* Fixed size portion of public names info.  */
5827 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5828
5829 /* Fixed size portion of the address range info.  */
5830 #define DWARF_ARANGES_HEADER_SIZE                                       \
5831   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5832                 DWARF2_ADDR_SIZE * 2)                                   \
5833    - DWARF_INITIAL_LENGTH_SIZE)
5834
5835 /* Size of padding portion in the address range info.  It must be
5836    aligned to twice the pointer size.  */
5837 #define DWARF_ARANGES_PAD_SIZE \
5838   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5839                 DWARF2_ADDR_SIZE * 2)                              \
5840    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5841
5842 /* Use assembler line directives if available.  */
5843 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5844 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5845 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5846 #else
5847 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5848 #endif
5849 #endif
5850
5851 /* Minimum line offset in a special line info. opcode.
5852    This value was chosen to give a reasonable range of values.  */
5853 #define DWARF_LINE_BASE  -10
5854
5855 /* First special line opcode - leave room for the standard opcodes.  */
5856 #define DWARF_LINE_OPCODE_BASE  10
5857
5858 /* Range of line offsets in a special line info. opcode.  */
5859 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5860
5861 /* Flag that indicates the initial value of the is_stmt_start flag.
5862    In the present implementation, we do not mark any lines as
5863    the beginning of a source statement, because that information
5864    is not made available by the GCC front-end.  */
5865 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5866
5867 /* Maximum number of operations per instruction bundle.  */
5868 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5869 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5870 #endif
5871
5872 /* This location is used by calc_die_sizes() to keep track
5873    the offset of each DIE within the .debug_info section.  */
5874 static unsigned long next_die_offset;
5875
5876 /* Record the root of the DIE's built for the current compilation unit.  */
5877 static GTY(()) dw_die_ref single_comp_unit_die;
5878
5879 /* A list of type DIEs that have been separated into comdat sections.  */
5880 static GTY(()) comdat_type_node *comdat_type_list;
5881
5882 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5883 static GTY(()) limbo_die_node *limbo_die_list;
5884
5885 /* A list of DIEs for which we may have to generate
5886    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5887 static GTY(()) limbo_die_node *deferred_asm_name;
5888
5889 /* Filenames referenced by this compilation unit.  */
5890 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5891
5892 /* A hash table of references to DIE's that describe declarations.
5893    The key is a DECL_UID() which is a unique number identifying each decl.  */
5894 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5895
5896 /* A hash table of references to DIE's that describe COMMON blocks.
5897    The key is DECL_UID() ^ die_parent.  */
5898 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5899
5900 typedef struct GTY(()) die_arg_entry_struct {
5901     dw_die_ref die;
5902     tree arg;
5903 } die_arg_entry;
5904
5905 DEF_VEC_O(die_arg_entry);
5906 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5907
5908 /* Node of the variable location list.  */
5909 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5910   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5911      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5912      in mode of the EXPR_LIST node and first EXPR_LIST operand
5913      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5914      location or NULL for padding.  For larger bitsizes,
5915      mode is 0 and first operand is a CONCAT with bitsize
5916      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5917      NULL as second operand.  */
5918   rtx GTY (()) loc;
5919   const char * GTY (()) label;
5920   struct var_loc_node * GTY (()) next;
5921 };
5922
5923 /* Variable location list.  */
5924 struct GTY (()) var_loc_list_def {
5925   struct var_loc_node * GTY (()) first;
5926
5927   /* Pointer to the last but one or last element of the
5928      chained list.  If the list is empty, both first and
5929      last are NULL, if the list contains just one node
5930      or the last node certainly is not redundant, it points
5931      to the last node, otherwise points to the last but one.
5932      Do not mark it for GC because it is marked through the chain.  */
5933   struct var_loc_node * GTY ((skip ("%h"))) last;
5934
5935   /* DECL_UID of the variable decl.  */
5936   unsigned int decl_id;
5937 };
5938 typedef struct var_loc_list_def var_loc_list;
5939
5940
5941 /* Table of decl location linked lists.  */
5942 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5943
5944 /* A pointer to the base of a list of references to DIE's that
5945    are uniquely identified by their tag, presence/absence of
5946    children DIE's, and list of attribute/value pairs.  */
5947 static GTY((length ("abbrev_die_table_allocated")))
5948   dw_die_ref *abbrev_die_table;
5949
5950 /* Number of elements currently allocated for abbrev_die_table.  */
5951 static GTY(()) unsigned abbrev_die_table_allocated;
5952
5953 /* Number of elements in type_die_table currently in use.  */
5954 static GTY(()) unsigned abbrev_die_table_in_use;
5955
5956 /* Size (in elements) of increments by which we may expand the
5957    abbrev_die_table.  */
5958 #define ABBREV_DIE_TABLE_INCREMENT 256
5959
5960 /* A pointer to the base of a table that contains line information
5961    for each source code line in .text in the compilation unit.  */
5962 static GTY((length ("line_info_table_allocated")))
5963      dw_line_info_ref line_info_table;
5964
5965 /* Number of elements currently allocated for line_info_table.  */
5966 static GTY(()) unsigned line_info_table_allocated;
5967
5968 /* Number of elements in line_info_table currently in use.  */
5969 static GTY(()) unsigned line_info_table_in_use;
5970
5971 /* A pointer to the base of a table that contains line information
5972    for each source code line outside of .text in the compilation unit.  */
5973 static GTY ((length ("separate_line_info_table_allocated")))
5974      dw_separate_line_info_ref separate_line_info_table;
5975
5976 /* Number of elements currently allocated for separate_line_info_table.  */
5977 static GTY(()) unsigned separate_line_info_table_allocated;
5978
5979 /* Number of elements in separate_line_info_table currently in use.  */
5980 static GTY(()) unsigned separate_line_info_table_in_use;
5981
5982 /* Size (in elements) of increments by which we may expand the
5983    line_info_table.  */
5984 #define LINE_INFO_TABLE_INCREMENT 1024
5985
5986 /* A pointer to the base of a table that contains a list of publicly
5987    accessible names.  */
5988 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5989
5990 /* A pointer to the base of a table that contains a list of publicly
5991    accessible types.  */
5992 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5993
5994 /* Array of dies for which we should generate .debug_arange info.  */
5995 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5996
5997 /* Number of elements currently allocated for arange_table.  */
5998 static GTY(()) unsigned arange_table_allocated;
5999
6000 /* Number of elements in arange_table currently in use.  */
6001 static GTY(()) unsigned arange_table_in_use;
6002
6003 /* Size (in elements) of increments by which we may expand the
6004    arange_table.  */
6005 #define ARANGE_TABLE_INCREMENT 64
6006
6007 /* Array of dies for which we should generate .debug_ranges info.  */
6008 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6009
6010 /* Number of elements currently allocated for ranges_table.  */
6011 static GTY(()) unsigned ranges_table_allocated;
6012
6013 /* Number of elements in ranges_table currently in use.  */
6014 static GTY(()) unsigned ranges_table_in_use;
6015
6016 /* Array of pairs of labels referenced in ranges_table.  */
6017 static GTY ((length ("ranges_by_label_allocated")))
6018      dw_ranges_by_label_ref ranges_by_label;
6019
6020 /* Number of elements currently allocated for ranges_by_label.  */
6021 static GTY(()) unsigned ranges_by_label_allocated;
6022
6023 /* Number of elements in ranges_by_label currently in use.  */
6024 static GTY(()) unsigned ranges_by_label_in_use;
6025
6026 /* Size (in elements) of increments by which we may expand the
6027    ranges_table.  */
6028 #define RANGES_TABLE_INCREMENT 64
6029
6030 /* Whether we have location lists that need outputting */
6031 static GTY(()) bool have_location_lists;
6032
6033 /* Unique label counter.  */
6034 static GTY(()) unsigned int loclabel_num;
6035
6036 /* Unique label counter for point-of-call tables.  */
6037 static GTY(()) unsigned int poc_label_num;
6038
6039 /* The direct call table structure.  */
6040
6041 typedef struct GTY(()) dcall_struct {
6042   unsigned int poc_label_num;
6043   tree poc_decl;
6044   dw_die_ref targ_die;
6045 }
6046 dcall_entry;
6047
6048 DEF_VEC_O(dcall_entry);
6049 DEF_VEC_ALLOC_O(dcall_entry, gc);
6050
6051 /* The virtual call table structure.  */
6052
6053 typedef struct GTY(()) vcall_struct {
6054   unsigned int poc_label_num;
6055   unsigned int vtable_slot;
6056 }
6057 vcall_entry;
6058
6059 DEF_VEC_O(vcall_entry);
6060 DEF_VEC_ALLOC_O(vcall_entry, gc);
6061
6062 /* Pointers to the direct and virtual call tables.  */
6063 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6064 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6065
6066 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6067
6068 struct GTY (()) vcall_insn {
6069   int insn_uid;
6070   unsigned int vtable_slot;
6071 };
6072
6073 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6074
6075 /* Record whether the function being analyzed contains inlined functions.  */
6076 static int current_function_has_inlines;
6077
6078 /* The last file entry emitted by maybe_emit_file().  */
6079 static GTY(()) struct dwarf_file_data * last_emitted_file;
6080
6081 /* Number of internal labels generated by gen_internal_sym().  */
6082 static GTY(()) int label_num;
6083
6084 /* Cached result of previous call to lookup_filename.  */
6085 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6086
6087 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6088
6089 /* Offset from the "steady-state frame pointer" to the frame base,
6090    within the current function.  */
6091 static HOST_WIDE_INT frame_pointer_fb_offset;
6092
6093 /* Forward declarations for functions defined in this file.  */
6094
6095 static int is_pseudo_reg (const_rtx);
6096 static tree type_main_variant (tree);
6097 static int is_tagged_type (const_tree);
6098 static const char *dwarf_tag_name (unsigned);
6099 static const char *dwarf_attr_name (unsigned);
6100 static const char *dwarf_form_name (unsigned);
6101 static tree decl_ultimate_origin (const_tree);
6102 static tree decl_class_context (tree);
6103 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6104 static inline enum dw_val_class AT_class (dw_attr_ref);
6105 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6106 static inline unsigned AT_flag (dw_attr_ref);
6107 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6108 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6109 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6110 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6111 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6112                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6113 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6114                                unsigned int, unsigned char *);
6115 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6116 static hashval_t debug_str_do_hash (const void *);
6117 static int debug_str_eq (const void *, const void *);
6118 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6119 static inline const char *AT_string (dw_attr_ref);
6120 static enum dwarf_form AT_string_form (dw_attr_ref);
6121 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6122 static void add_AT_specification (dw_die_ref, dw_die_ref);
6123 static inline dw_die_ref AT_ref (dw_attr_ref);
6124 static inline int AT_ref_external (dw_attr_ref);
6125 static inline void set_AT_ref_external (dw_attr_ref, int);
6126 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6127 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6128 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6129 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6130                              dw_loc_list_ref);
6131 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6132 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6133 static inline rtx AT_addr (dw_attr_ref);
6134 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6135 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6136 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6137 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6138                            unsigned HOST_WIDE_INT);
6139 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6140                                unsigned long);
6141 static inline const char *AT_lbl (dw_attr_ref);
6142 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6143 static const char *get_AT_low_pc (dw_die_ref);
6144 static const char *get_AT_hi_pc (dw_die_ref);
6145 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6146 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6147 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6148 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6149 static bool is_cxx (void);
6150 static bool is_fortran (void);
6151 static bool is_ada (void);
6152 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6153 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6154 static void add_child_die (dw_die_ref, dw_die_ref);
6155 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6156 static dw_die_ref lookup_type_die (tree);
6157 static void equate_type_number_to_die (tree, dw_die_ref);
6158 static hashval_t decl_die_table_hash (const void *);
6159 static int decl_die_table_eq (const void *, const void *);
6160 static dw_die_ref lookup_decl_die (tree);
6161 static hashval_t common_block_die_table_hash (const void *);
6162 static int common_block_die_table_eq (const void *, const void *);
6163 static hashval_t decl_loc_table_hash (const void *);
6164 static int decl_loc_table_eq (const void *, const void *);
6165 static var_loc_list *lookup_decl_loc (const_tree);
6166 static void equate_decl_number_to_die (tree, dw_die_ref);
6167 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6168 static void print_spaces (FILE *);
6169 static void print_die (dw_die_ref, FILE *);
6170 static void print_dwarf_line_table (FILE *);
6171 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6172 static dw_die_ref pop_compile_unit (dw_die_ref);
6173 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6174 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6175 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6176 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6177 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6178 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6179 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6180                                    struct md5_ctx *, int *);
6181 struct checksum_attributes;
6182 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6183 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6184 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6185 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6186 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6187 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6188 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6189 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6190 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6191 static void compute_section_prefix (dw_die_ref);
6192 static int is_type_die (dw_die_ref);
6193 static int is_comdat_die (dw_die_ref);
6194 static int is_symbol_die (dw_die_ref);
6195 static void assign_symbol_names (dw_die_ref);
6196 static void break_out_includes (dw_die_ref);
6197 static int is_declaration_die (dw_die_ref);
6198 static int should_move_die_to_comdat (dw_die_ref);
6199 static dw_die_ref clone_as_declaration (dw_die_ref);
6200 static dw_die_ref clone_die (dw_die_ref);
6201 static dw_die_ref clone_tree (dw_die_ref);
6202 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6203 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6204 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6205 static dw_die_ref generate_skeleton (dw_die_ref);
6206 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6207                                                          dw_die_ref);
6208 static void break_out_comdat_types (dw_die_ref);
6209 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6210 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6211 static void copy_decls_for_unworthy_types (dw_die_ref);
6212
6213 static hashval_t htab_cu_hash (const void *);
6214 static int htab_cu_eq (const void *, const void *);
6215 static void htab_cu_del (void *);
6216 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6217 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6218 static void add_sibling_attributes (dw_die_ref);
6219 static void build_abbrev_table (dw_die_ref);
6220 static void output_location_lists (dw_die_ref);
6221 static int constant_size (unsigned HOST_WIDE_INT);
6222 static unsigned long size_of_die (dw_die_ref);
6223 static void calc_die_sizes (dw_die_ref);
6224 static void mark_dies (dw_die_ref);
6225 static void unmark_dies (dw_die_ref);
6226 static void unmark_all_dies (dw_die_ref);
6227 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6228 static unsigned long size_of_aranges (void);
6229 static enum dwarf_form value_format (dw_attr_ref);
6230 static void output_value_format (dw_attr_ref);
6231 static void output_abbrev_section (void);
6232 static void output_die_symbol (dw_die_ref);
6233 static void output_die (dw_die_ref);
6234 static void output_compilation_unit_header (void);
6235 static void output_comp_unit (dw_die_ref, int);
6236 static void output_comdat_type_unit (comdat_type_node *);
6237 static const char *dwarf2_name (tree, int);
6238 static void add_pubname (tree, dw_die_ref);
6239 static void add_pubname_string (const char *, dw_die_ref);
6240 static void add_pubtype (tree, dw_die_ref);
6241 static void output_pubnames (VEC (pubname_entry,gc) *);
6242 static void add_arange (tree, dw_die_ref);
6243 static void output_aranges (void);
6244 static unsigned int add_ranges_num (int);
6245 static unsigned int add_ranges (const_tree);
6246 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6247                                   bool *);
6248 static void output_ranges (void);
6249 static void output_line_info (void);
6250 static void output_file_names (void);
6251 static dw_die_ref base_type_die (tree);
6252 static int is_base_type (tree);
6253 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6254 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6255 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6256 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6257 static int type_is_enum (const_tree);
6258 static unsigned int dbx_reg_number (const_rtx);
6259 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6260 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6261 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6262                                                 enum var_init_status);
6263 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6264                                                      enum var_init_status);
6265 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6266                                          enum var_init_status);
6267 static int is_based_loc (const_rtx);
6268 static int resolve_one_addr (rtx *, void *);
6269 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6270                                                enum var_init_status);
6271 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6272                                         enum var_init_status);
6273 static dw_loc_list_ref loc_list_from_tree (tree, int);
6274 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6275 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6276 static tree field_type (const_tree);
6277 static unsigned int simple_type_align_in_bits (const_tree);
6278 static unsigned int simple_decl_align_in_bits (const_tree);
6279 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6280 static HOST_WIDE_INT field_byte_offset (const_tree);
6281 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6282                                          dw_loc_list_ref);
6283 static void add_data_member_location_attribute (dw_die_ref, tree);
6284 static bool add_const_value_attribute (dw_die_ref, rtx);
6285 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6286 static void insert_double (double_int, unsigned char *);
6287 static void insert_float (const_rtx, unsigned char *);
6288 static rtx rtl_for_decl_location (tree);
6289 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6290                                                    enum dwarf_attribute);
6291 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6292 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6293 static void add_name_attribute (dw_die_ref, const char *);
6294 static void add_comp_dir_attribute (dw_die_ref);
6295 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6296 static void add_subscript_info (dw_die_ref, tree, bool);
6297 static void add_byte_size_attribute (dw_die_ref, tree);
6298 static void add_bit_offset_attribute (dw_die_ref, tree);
6299 static void add_bit_size_attribute (dw_die_ref, tree);
6300 static void add_prototyped_attribute (dw_die_ref, tree);
6301 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6302 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6303 static void add_src_coords_attributes (dw_die_ref, tree);
6304 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6305 static void push_decl_scope (tree);
6306 static void pop_decl_scope (void);
6307 static dw_die_ref scope_die_for (tree, dw_die_ref);
6308 static inline int local_scope_p (dw_die_ref);
6309 static inline int class_scope_p (dw_die_ref);
6310 static inline int class_or_namespace_scope_p (dw_die_ref);
6311 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6312 static void add_calling_convention_attribute (dw_die_ref, tree);
6313 static const char *type_tag (const_tree);
6314 static tree member_declared_type (const_tree);
6315 #if 0
6316 static const char *decl_start_label (tree);
6317 #endif
6318 static void gen_array_type_die (tree, dw_die_ref);
6319 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6320 #if 0
6321 static void gen_entry_point_die (tree, dw_die_ref);
6322 #endif
6323 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6324 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6325 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6326 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6327 static void gen_formal_types_die (tree, dw_die_ref);
6328 static void gen_subprogram_die (tree, dw_die_ref);
6329 static void gen_variable_die (tree, tree, dw_die_ref);
6330 static void gen_const_die (tree, dw_die_ref);
6331 static void gen_label_die (tree, dw_die_ref);
6332 static void gen_lexical_block_die (tree, dw_die_ref, int);
6333 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6334 static void gen_field_die (tree, dw_die_ref);
6335 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6336 static dw_die_ref gen_compile_unit_die (const char *);
6337 static void gen_inheritance_die (tree, tree, dw_die_ref);
6338 static void gen_member_die (tree, dw_die_ref);
6339 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6340                                                 enum debug_info_usage);
6341 static void gen_subroutine_type_die (tree, dw_die_ref);
6342 static void gen_typedef_die (tree, dw_die_ref);
6343 static void gen_type_die (tree, dw_die_ref);
6344 static void gen_block_die (tree, dw_die_ref, int);
6345 static void decls_for_scope (tree, dw_die_ref, int);
6346 static int is_redundant_typedef (const_tree);
6347 static bool is_naming_typedef_decl (const_tree);
6348 static inline dw_die_ref get_context_die (tree);
6349 static void gen_namespace_die (tree, dw_die_ref);
6350 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6351 static dw_die_ref force_decl_die (tree);
6352 static dw_die_ref force_type_die (tree);
6353 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6354 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6355 static struct dwarf_file_data * lookup_filename (const char *);
6356 static void retry_incomplete_types (void);
6357 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6358 static void gen_generic_params_dies (tree);
6359 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6360 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6361 static void splice_child_die (dw_die_ref, dw_die_ref);
6362 static int file_info_cmp (const void *, const void *);
6363 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6364                                      const char *, const char *);
6365 static void output_loc_list (dw_loc_list_ref);
6366 static char *gen_internal_sym (const char *);
6367
6368 static void prune_unmark_dies (dw_die_ref);
6369 static void prune_unused_types_mark (dw_die_ref, int);
6370 static void prune_unused_types_walk (dw_die_ref);
6371 static void prune_unused_types_walk_attribs (dw_die_ref);
6372 static void prune_unused_types_prune (dw_die_ref);
6373 static void prune_unused_types (void);
6374 static int maybe_emit_file (struct dwarf_file_data *fd);
6375 static inline const char *AT_vms_delta1 (dw_attr_ref);
6376 static inline const char *AT_vms_delta2 (dw_attr_ref);
6377 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6378                                      const char *, const char *);
6379 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6380 static void gen_remaining_tmpl_value_param_die_attribute (void);
6381
6382 /* Section names used to hold DWARF debugging information.  */
6383 #ifndef DEBUG_INFO_SECTION
6384 #define DEBUG_INFO_SECTION      ".debug_info"
6385 #endif
6386 #ifndef DEBUG_ABBREV_SECTION
6387 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6388 #endif
6389 #ifndef DEBUG_ARANGES_SECTION
6390 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6391 #endif
6392 #ifndef DEBUG_MACINFO_SECTION
6393 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6394 #endif
6395 #ifndef DEBUG_LINE_SECTION
6396 #define DEBUG_LINE_SECTION      ".debug_line"
6397 #endif
6398 #ifndef DEBUG_LOC_SECTION
6399 #define DEBUG_LOC_SECTION       ".debug_loc"
6400 #endif
6401 #ifndef DEBUG_PUBNAMES_SECTION
6402 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6403 #endif
6404 #ifndef DEBUG_PUBTYPES_SECTION
6405 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6406 #endif
6407 #ifndef DEBUG_DCALL_SECTION
6408 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6409 #endif
6410 #ifndef DEBUG_VCALL_SECTION
6411 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6412 #endif
6413 #ifndef DEBUG_STR_SECTION
6414 #define DEBUG_STR_SECTION       ".debug_str"
6415 #endif
6416 #ifndef DEBUG_RANGES_SECTION
6417 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6418 #endif
6419
6420 /* Standard ELF section names for compiled code and data.  */
6421 #ifndef TEXT_SECTION_NAME
6422 #define TEXT_SECTION_NAME       ".text"
6423 #endif
6424
6425 /* Section flags for .debug_str section.  */
6426 #define DEBUG_STR_SECTION_FLAGS \
6427   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6428    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6429    : SECTION_DEBUG)
6430
6431 /* Labels we insert at beginning sections we can reference instead of
6432    the section names themselves.  */
6433
6434 #ifndef TEXT_SECTION_LABEL
6435 #define TEXT_SECTION_LABEL              "Ltext"
6436 #endif
6437 #ifndef COLD_TEXT_SECTION_LABEL
6438 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6439 #endif
6440 #ifndef DEBUG_LINE_SECTION_LABEL
6441 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6442 #endif
6443 #ifndef DEBUG_INFO_SECTION_LABEL
6444 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6445 #endif
6446 #ifndef DEBUG_ABBREV_SECTION_LABEL
6447 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6448 #endif
6449 #ifndef DEBUG_LOC_SECTION_LABEL
6450 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6451 #endif
6452 #ifndef DEBUG_RANGES_SECTION_LABEL
6453 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6454 #endif
6455 #ifndef DEBUG_MACINFO_SECTION_LABEL
6456 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6457 #endif
6458
6459
6460 /* Definitions of defaults for formats and names of various special
6461    (artificial) labels which may be generated within this file (when the -g
6462    options is used and DWARF2_DEBUGGING_INFO is in effect.
6463    If necessary, these may be overridden from within the tm.h file, but
6464    typically, overriding these defaults is unnecessary.  */
6465
6466 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6467 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6468 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6469 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6470 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6471 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6472 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6473 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6474 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6475 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6476
6477 #ifndef TEXT_END_LABEL
6478 #define TEXT_END_LABEL          "Letext"
6479 #endif
6480 #ifndef COLD_END_LABEL
6481 #define COLD_END_LABEL          "Letext_cold"
6482 #endif
6483 #ifndef BLOCK_BEGIN_LABEL
6484 #define BLOCK_BEGIN_LABEL       "LBB"
6485 #endif
6486 #ifndef BLOCK_END_LABEL
6487 #define BLOCK_END_LABEL         "LBE"
6488 #endif
6489 #ifndef LINE_CODE_LABEL
6490 #define LINE_CODE_LABEL         "LM"
6491 #endif
6492 #ifndef SEPARATE_LINE_CODE_LABEL
6493 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6494 #endif
6495
6496 \f
6497 /* Return the root of the DIE's built for the current compilation unit.  */
6498 static dw_die_ref
6499 comp_unit_die (void)
6500 {
6501   if (!single_comp_unit_die)
6502     single_comp_unit_die = gen_compile_unit_die (NULL);
6503   return single_comp_unit_die;
6504 }
6505
6506 /* We allow a language front-end to designate a function that is to be
6507    called to "demangle" any name before it is put into a DIE.  */
6508
6509 static const char *(*demangle_name_func) (const char *);
6510
6511 void
6512 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6513 {
6514   demangle_name_func = func;
6515 }
6516
6517 /* Test if rtl node points to a pseudo register.  */
6518
6519 static inline int
6520 is_pseudo_reg (const_rtx rtl)
6521 {
6522   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6523           || (GET_CODE (rtl) == SUBREG
6524               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6525 }
6526
6527 /* Return a reference to a type, with its const and volatile qualifiers
6528    removed.  */
6529
6530 static inline tree
6531 type_main_variant (tree type)
6532 {
6533   type = TYPE_MAIN_VARIANT (type);
6534
6535   /* ??? There really should be only one main variant among any group of
6536      variants of a given type (and all of the MAIN_VARIANT values for all
6537      members of the group should point to that one type) but sometimes the C
6538      front-end messes this up for array types, so we work around that bug
6539      here.  */
6540   if (TREE_CODE (type) == ARRAY_TYPE)
6541     while (type != TYPE_MAIN_VARIANT (type))
6542       type = TYPE_MAIN_VARIANT (type);
6543
6544   return type;
6545 }
6546
6547 /* Return nonzero if the given type node represents a tagged type.  */
6548
6549 static inline int
6550 is_tagged_type (const_tree type)
6551 {
6552   enum tree_code code = TREE_CODE (type);
6553
6554   return (code == RECORD_TYPE || code == UNION_TYPE
6555           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6556 }
6557
6558 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6559
6560 static void
6561 get_ref_die_offset_label (char *label, dw_die_ref ref)
6562 {
6563   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6564 }
6565
6566 /* Convert a DIE tag into its string name.  */
6567
6568 static const char *
6569 dwarf_tag_name (unsigned int tag)
6570 {
6571   switch (tag)
6572     {
6573     case DW_TAG_padding:
6574       return "DW_TAG_padding";
6575     case DW_TAG_array_type:
6576       return "DW_TAG_array_type";
6577     case DW_TAG_class_type:
6578       return "DW_TAG_class_type";
6579     case DW_TAG_entry_point:
6580       return "DW_TAG_entry_point";
6581     case DW_TAG_enumeration_type:
6582       return "DW_TAG_enumeration_type";
6583     case DW_TAG_formal_parameter:
6584       return "DW_TAG_formal_parameter";
6585     case DW_TAG_imported_declaration:
6586       return "DW_TAG_imported_declaration";
6587     case DW_TAG_label:
6588       return "DW_TAG_label";
6589     case DW_TAG_lexical_block:
6590       return "DW_TAG_lexical_block";
6591     case DW_TAG_member:
6592       return "DW_TAG_member";
6593     case DW_TAG_pointer_type:
6594       return "DW_TAG_pointer_type";
6595     case DW_TAG_reference_type:
6596       return "DW_TAG_reference_type";
6597     case DW_TAG_compile_unit:
6598       return "DW_TAG_compile_unit";
6599     case DW_TAG_string_type:
6600       return "DW_TAG_string_type";
6601     case DW_TAG_structure_type:
6602       return "DW_TAG_structure_type";
6603     case DW_TAG_subroutine_type:
6604       return "DW_TAG_subroutine_type";
6605     case DW_TAG_typedef:
6606       return "DW_TAG_typedef";
6607     case DW_TAG_union_type:
6608       return "DW_TAG_union_type";
6609     case DW_TAG_unspecified_parameters:
6610       return "DW_TAG_unspecified_parameters";
6611     case DW_TAG_variant:
6612       return "DW_TAG_variant";
6613     case DW_TAG_common_block:
6614       return "DW_TAG_common_block";
6615     case DW_TAG_common_inclusion:
6616       return "DW_TAG_common_inclusion";
6617     case DW_TAG_inheritance:
6618       return "DW_TAG_inheritance";
6619     case DW_TAG_inlined_subroutine:
6620       return "DW_TAG_inlined_subroutine";
6621     case DW_TAG_module:
6622       return "DW_TAG_module";
6623     case DW_TAG_ptr_to_member_type:
6624       return "DW_TAG_ptr_to_member_type";
6625     case DW_TAG_set_type:
6626       return "DW_TAG_set_type";
6627     case DW_TAG_subrange_type:
6628       return "DW_TAG_subrange_type";
6629     case DW_TAG_with_stmt:
6630       return "DW_TAG_with_stmt";
6631     case DW_TAG_access_declaration:
6632       return "DW_TAG_access_declaration";
6633     case DW_TAG_base_type:
6634       return "DW_TAG_base_type";
6635     case DW_TAG_catch_block:
6636       return "DW_TAG_catch_block";
6637     case DW_TAG_const_type:
6638       return "DW_TAG_const_type";
6639     case DW_TAG_constant:
6640       return "DW_TAG_constant";
6641     case DW_TAG_enumerator:
6642       return "DW_TAG_enumerator";
6643     case DW_TAG_file_type:
6644       return "DW_TAG_file_type";
6645     case DW_TAG_friend:
6646       return "DW_TAG_friend";
6647     case DW_TAG_namelist:
6648       return "DW_TAG_namelist";
6649     case DW_TAG_namelist_item:
6650       return "DW_TAG_namelist_item";
6651     case DW_TAG_packed_type:
6652       return "DW_TAG_packed_type";
6653     case DW_TAG_subprogram:
6654       return "DW_TAG_subprogram";
6655     case DW_TAG_template_type_param:
6656       return "DW_TAG_template_type_param";
6657     case DW_TAG_template_value_param:
6658       return "DW_TAG_template_value_param";
6659     case DW_TAG_thrown_type:
6660       return "DW_TAG_thrown_type";
6661     case DW_TAG_try_block:
6662       return "DW_TAG_try_block";
6663     case DW_TAG_variant_part:
6664       return "DW_TAG_variant_part";
6665     case DW_TAG_variable:
6666       return "DW_TAG_variable";
6667     case DW_TAG_volatile_type:
6668       return "DW_TAG_volatile_type";
6669     case DW_TAG_dwarf_procedure:
6670       return "DW_TAG_dwarf_procedure";
6671     case DW_TAG_restrict_type:
6672       return "DW_TAG_restrict_type";
6673     case DW_TAG_interface_type:
6674       return "DW_TAG_interface_type";
6675     case DW_TAG_namespace:
6676       return "DW_TAG_namespace";
6677     case DW_TAG_imported_module:
6678       return "DW_TAG_imported_module";
6679     case DW_TAG_unspecified_type:
6680       return "DW_TAG_unspecified_type";
6681     case DW_TAG_partial_unit:
6682       return "DW_TAG_partial_unit";
6683     case DW_TAG_imported_unit:
6684       return "DW_TAG_imported_unit";
6685     case DW_TAG_condition:
6686       return "DW_TAG_condition";
6687     case DW_TAG_shared_type:
6688       return "DW_TAG_shared_type";
6689     case DW_TAG_type_unit:
6690       return "DW_TAG_type_unit";
6691     case DW_TAG_rvalue_reference_type:
6692       return "DW_TAG_rvalue_reference_type";
6693     case DW_TAG_template_alias:
6694       return "DW_TAG_template_alias";
6695     case DW_TAG_GNU_template_parameter_pack:
6696       return "DW_TAG_GNU_template_parameter_pack";
6697     case DW_TAG_GNU_formal_parameter_pack:
6698       return "DW_TAG_GNU_formal_parameter_pack";
6699     case DW_TAG_MIPS_loop:
6700       return "DW_TAG_MIPS_loop";
6701     case DW_TAG_format_label:
6702       return "DW_TAG_format_label";
6703     case DW_TAG_function_template:
6704       return "DW_TAG_function_template";
6705     case DW_TAG_class_template:
6706       return "DW_TAG_class_template";
6707     case DW_TAG_GNU_BINCL:
6708       return "DW_TAG_GNU_BINCL";
6709     case DW_TAG_GNU_EINCL:
6710       return "DW_TAG_GNU_EINCL";
6711     case DW_TAG_GNU_template_template_param:
6712       return "DW_TAG_GNU_template_template_param";
6713     default:
6714       return "DW_TAG_<unknown>";
6715     }
6716 }
6717
6718 /* Convert a DWARF attribute code into its string name.  */
6719
6720 static const char *
6721 dwarf_attr_name (unsigned int attr)
6722 {
6723   switch (attr)
6724     {
6725     case DW_AT_sibling:
6726       return "DW_AT_sibling";
6727     case DW_AT_location:
6728       return "DW_AT_location";
6729     case DW_AT_name:
6730       return "DW_AT_name";
6731     case DW_AT_ordering:
6732       return "DW_AT_ordering";
6733     case DW_AT_subscr_data:
6734       return "DW_AT_subscr_data";
6735     case DW_AT_byte_size:
6736       return "DW_AT_byte_size";
6737     case DW_AT_bit_offset:
6738       return "DW_AT_bit_offset";
6739     case DW_AT_bit_size:
6740       return "DW_AT_bit_size";
6741     case DW_AT_element_list:
6742       return "DW_AT_element_list";
6743     case DW_AT_stmt_list:
6744       return "DW_AT_stmt_list";
6745     case DW_AT_low_pc:
6746       return "DW_AT_low_pc";
6747     case DW_AT_high_pc:
6748       return "DW_AT_high_pc";
6749     case DW_AT_language:
6750       return "DW_AT_language";
6751     case DW_AT_member:
6752       return "DW_AT_member";
6753     case DW_AT_discr:
6754       return "DW_AT_discr";
6755     case DW_AT_discr_value:
6756       return "DW_AT_discr_value";
6757     case DW_AT_visibility:
6758       return "DW_AT_visibility";
6759     case DW_AT_import:
6760       return "DW_AT_import";
6761     case DW_AT_string_length:
6762       return "DW_AT_string_length";
6763     case DW_AT_common_reference:
6764       return "DW_AT_common_reference";
6765     case DW_AT_comp_dir:
6766       return "DW_AT_comp_dir";
6767     case DW_AT_const_value:
6768       return "DW_AT_const_value";
6769     case DW_AT_containing_type:
6770       return "DW_AT_containing_type";
6771     case DW_AT_default_value:
6772       return "DW_AT_default_value";
6773     case DW_AT_inline:
6774       return "DW_AT_inline";
6775     case DW_AT_is_optional:
6776       return "DW_AT_is_optional";
6777     case DW_AT_lower_bound:
6778       return "DW_AT_lower_bound";
6779     case DW_AT_producer:
6780       return "DW_AT_producer";
6781     case DW_AT_prototyped:
6782       return "DW_AT_prototyped";
6783     case DW_AT_return_addr:
6784       return "DW_AT_return_addr";
6785     case DW_AT_start_scope:
6786       return "DW_AT_start_scope";
6787     case DW_AT_bit_stride:
6788       return "DW_AT_bit_stride";
6789     case DW_AT_upper_bound:
6790       return "DW_AT_upper_bound";
6791     case DW_AT_abstract_origin:
6792       return "DW_AT_abstract_origin";
6793     case DW_AT_accessibility:
6794       return "DW_AT_accessibility";
6795     case DW_AT_address_class:
6796       return "DW_AT_address_class";
6797     case DW_AT_artificial:
6798       return "DW_AT_artificial";
6799     case DW_AT_base_types:
6800       return "DW_AT_base_types";
6801     case DW_AT_calling_convention:
6802       return "DW_AT_calling_convention";
6803     case DW_AT_count:
6804       return "DW_AT_count";
6805     case DW_AT_data_member_location:
6806       return "DW_AT_data_member_location";
6807     case DW_AT_decl_column:
6808       return "DW_AT_decl_column";
6809     case DW_AT_decl_file:
6810       return "DW_AT_decl_file";
6811     case DW_AT_decl_line:
6812       return "DW_AT_decl_line";
6813     case DW_AT_declaration:
6814       return "DW_AT_declaration";
6815     case DW_AT_discr_list:
6816       return "DW_AT_discr_list";
6817     case DW_AT_encoding:
6818       return "DW_AT_encoding";
6819     case DW_AT_external:
6820       return "DW_AT_external";
6821     case DW_AT_explicit:
6822       return "DW_AT_explicit";
6823     case DW_AT_frame_base:
6824       return "DW_AT_frame_base";
6825     case DW_AT_friend:
6826       return "DW_AT_friend";
6827     case DW_AT_identifier_case:
6828       return "DW_AT_identifier_case";
6829     case DW_AT_macro_info:
6830       return "DW_AT_macro_info";
6831     case DW_AT_namelist_items:
6832       return "DW_AT_namelist_items";
6833     case DW_AT_priority:
6834       return "DW_AT_priority";
6835     case DW_AT_segment:
6836       return "DW_AT_segment";
6837     case DW_AT_specification:
6838       return "DW_AT_specification";
6839     case DW_AT_static_link:
6840       return "DW_AT_static_link";
6841     case DW_AT_type:
6842       return "DW_AT_type";
6843     case DW_AT_use_location:
6844       return "DW_AT_use_location";
6845     case DW_AT_variable_parameter:
6846       return "DW_AT_variable_parameter";
6847     case DW_AT_virtuality:
6848       return "DW_AT_virtuality";
6849     case DW_AT_vtable_elem_location:
6850       return "DW_AT_vtable_elem_location";
6851
6852     case DW_AT_allocated:
6853       return "DW_AT_allocated";
6854     case DW_AT_associated:
6855       return "DW_AT_associated";
6856     case DW_AT_data_location:
6857       return "DW_AT_data_location";
6858     case DW_AT_byte_stride:
6859       return "DW_AT_byte_stride";
6860     case DW_AT_entry_pc:
6861       return "DW_AT_entry_pc";
6862     case DW_AT_use_UTF8:
6863       return "DW_AT_use_UTF8";
6864     case DW_AT_extension:
6865       return "DW_AT_extension";
6866     case DW_AT_ranges:
6867       return "DW_AT_ranges";
6868     case DW_AT_trampoline:
6869       return "DW_AT_trampoline";
6870     case DW_AT_call_column:
6871       return "DW_AT_call_column";
6872     case DW_AT_call_file:
6873       return "DW_AT_call_file";
6874     case DW_AT_call_line:
6875       return "DW_AT_call_line";
6876     case DW_AT_object_pointer:
6877       return "DW_AT_object_pointer";
6878
6879     case DW_AT_signature:
6880       return "DW_AT_signature";
6881     case DW_AT_main_subprogram:
6882       return "DW_AT_main_subprogram";
6883     case DW_AT_data_bit_offset:
6884       return "DW_AT_data_bit_offset";
6885     case DW_AT_const_expr:
6886       return "DW_AT_const_expr";
6887     case DW_AT_enum_class:
6888       return "DW_AT_enum_class";
6889     case DW_AT_linkage_name:
6890       return "DW_AT_linkage_name";
6891
6892     case DW_AT_MIPS_fde:
6893       return "DW_AT_MIPS_fde";
6894     case DW_AT_MIPS_loop_begin:
6895       return "DW_AT_MIPS_loop_begin";
6896     case DW_AT_MIPS_tail_loop_begin:
6897       return "DW_AT_MIPS_tail_loop_begin";
6898     case DW_AT_MIPS_epilog_begin:
6899       return "DW_AT_MIPS_epilog_begin";
6900 #if VMS_DEBUGGING_INFO
6901     case DW_AT_HP_prologue:
6902       return "DW_AT_HP_prologue";
6903 #else
6904     case DW_AT_MIPS_loop_unroll_factor:
6905       return "DW_AT_MIPS_loop_unroll_factor";
6906 #endif
6907     case DW_AT_MIPS_software_pipeline_depth:
6908       return "DW_AT_MIPS_software_pipeline_depth";
6909     case DW_AT_MIPS_linkage_name:
6910       return "DW_AT_MIPS_linkage_name";
6911 #if VMS_DEBUGGING_INFO
6912     case DW_AT_HP_epilogue:
6913       return "DW_AT_HP_epilogue";
6914 #else
6915     case DW_AT_MIPS_stride:
6916       return "DW_AT_MIPS_stride";
6917 #endif
6918     case DW_AT_MIPS_abstract_name:
6919       return "DW_AT_MIPS_abstract_name";
6920     case DW_AT_MIPS_clone_origin:
6921       return "DW_AT_MIPS_clone_origin";
6922     case DW_AT_MIPS_has_inlines:
6923       return "DW_AT_MIPS_has_inlines";
6924
6925     case DW_AT_sf_names:
6926       return "DW_AT_sf_names";
6927     case DW_AT_src_info:
6928       return "DW_AT_src_info";
6929     case DW_AT_mac_info:
6930       return "DW_AT_mac_info";
6931     case DW_AT_src_coords:
6932       return "DW_AT_src_coords";
6933     case DW_AT_body_begin:
6934       return "DW_AT_body_begin";
6935     case DW_AT_body_end:
6936       return "DW_AT_body_end";
6937     case DW_AT_GNU_vector:
6938       return "DW_AT_GNU_vector";
6939     case DW_AT_GNU_guarded_by:
6940       return "DW_AT_GNU_guarded_by";
6941     case DW_AT_GNU_pt_guarded_by:
6942       return "DW_AT_GNU_pt_guarded_by";
6943     case DW_AT_GNU_guarded:
6944       return "DW_AT_GNU_guarded";
6945     case DW_AT_GNU_pt_guarded:
6946       return "DW_AT_GNU_pt_guarded";
6947     case DW_AT_GNU_locks_excluded:
6948       return "DW_AT_GNU_locks_excluded";
6949     case DW_AT_GNU_exclusive_locks_required:
6950       return "DW_AT_GNU_exclusive_locks_required";
6951     case DW_AT_GNU_shared_locks_required:
6952       return "DW_AT_GNU_shared_locks_required";
6953     case DW_AT_GNU_odr_signature:
6954       return "DW_AT_GNU_odr_signature";
6955     case DW_AT_GNU_template_name:
6956       return "DW_AT_GNU_template_name";
6957
6958     case DW_AT_VMS_rtnbeg_pd_address:
6959       return "DW_AT_VMS_rtnbeg_pd_address";
6960
6961     default:
6962       return "DW_AT_<unknown>";
6963     }
6964 }
6965
6966 /* Convert a DWARF value form code into its string name.  */
6967
6968 static const char *
6969 dwarf_form_name (unsigned int form)
6970 {
6971   switch (form)
6972     {
6973     case DW_FORM_addr:
6974       return "DW_FORM_addr";
6975     case DW_FORM_block2:
6976       return "DW_FORM_block2";
6977     case DW_FORM_block4:
6978       return "DW_FORM_block4";
6979     case DW_FORM_data2:
6980       return "DW_FORM_data2";
6981     case DW_FORM_data4:
6982       return "DW_FORM_data4";
6983     case DW_FORM_data8:
6984       return "DW_FORM_data8";
6985     case DW_FORM_string:
6986       return "DW_FORM_string";
6987     case DW_FORM_block:
6988       return "DW_FORM_block";
6989     case DW_FORM_block1:
6990       return "DW_FORM_block1";
6991     case DW_FORM_data1:
6992       return "DW_FORM_data1";
6993     case DW_FORM_flag:
6994       return "DW_FORM_flag";
6995     case DW_FORM_sdata:
6996       return "DW_FORM_sdata";
6997     case DW_FORM_strp:
6998       return "DW_FORM_strp";
6999     case DW_FORM_udata:
7000       return "DW_FORM_udata";
7001     case DW_FORM_ref_addr:
7002       return "DW_FORM_ref_addr";
7003     case DW_FORM_ref1:
7004       return "DW_FORM_ref1";
7005     case DW_FORM_ref2:
7006       return "DW_FORM_ref2";
7007     case DW_FORM_ref4:
7008       return "DW_FORM_ref4";
7009     case DW_FORM_ref8:
7010       return "DW_FORM_ref8";
7011     case DW_FORM_ref_udata:
7012       return "DW_FORM_ref_udata";
7013     case DW_FORM_indirect:
7014       return "DW_FORM_indirect";
7015     case DW_FORM_sec_offset:
7016       return "DW_FORM_sec_offset";
7017     case DW_FORM_exprloc:
7018       return "DW_FORM_exprloc";
7019     case DW_FORM_flag_present:
7020       return "DW_FORM_flag_present";
7021     case DW_FORM_ref_sig8:
7022       return "DW_FORM_ref_sig8";
7023     default:
7024       return "DW_FORM_<unknown>";
7025     }
7026 }
7027 \f
7028 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7029    instance of an inlined instance of a decl which is local to an inline
7030    function, so we have to trace all of the way back through the origin chain
7031    to find out what sort of node actually served as the original seed for the
7032    given block.  */
7033
7034 static tree
7035 decl_ultimate_origin (const_tree decl)
7036 {
7037   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7038     return NULL_TREE;
7039
7040   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7041      nodes in the function to point to themselves; ignore that if
7042      we're trying to output the abstract instance of this function.  */
7043   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7044     return NULL_TREE;
7045
7046   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7047      most distant ancestor, this should never happen.  */
7048   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7049
7050   return DECL_ABSTRACT_ORIGIN (decl);
7051 }
7052
7053 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7054    of a virtual function may refer to a base class, so we check the 'this'
7055    parameter.  */
7056
7057 static tree
7058 decl_class_context (tree decl)
7059 {
7060   tree context = NULL_TREE;
7061
7062   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7063     context = DECL_CONTEXT (decl);
7064   else
7065     context = TYPE_MAIN_VARIANT
7066       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7067
7068   if (context && !TYPE_P (context))
7069     context = NULL_TREE;
7070
7071   return context;
7072 }
7073 \f
7074 /* Add an attribute/value pair to a DIE.  */
7075
7076 static inline void
7077 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7078 {
7079   /* Maybe this should be an assert?  */
7080   if (die == NULL)
7081     return;
7082
7083   if (die->die_attr == NULL)
7084     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7085   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7086 }
7087
7088 static inline enum dw_val_class
7089 AT_class (dw_attr_ref a)
7090 {
7091   return a->dw_attr_val.val_class;
7092 }
7093
7094 /* Add a flag value attribute to a DIE.  */
7095
7096 static inline void
7097 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7098 {
7099   dw_attr_node attr;
7100
7101   attr.dw_attr = attr_kind;
7102   attr.dw_attr_val.val_class = dw_val_class_flag;
7103   attr.dw_attr_val.v.val_flag = flag;
7104   add_dwarf_attr (die, &attr);
7105 }
7106
7107 static inline unsigned
7108 AT_flag (dw_attr_ref a)
7109 {
7110   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7111   return a->dw_attr_val.v.val_flag;
7112 }
7113
7114 /* Add a signed integer attribute value to a DIE.  */
7115
7116 static inline void
7117 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7118 {
7119   dw_attr_node attr;
7120
7121   attr.dw_attr = attr_kind;
7122   attr.dw_attr_val.val_class = dw_val_class_const;
7123   attr.dw_attr_val.v.val_int = int_val;
7124   add_dwarf_attr (die, &attr);
7125 }
7126
7127 static inline HOST_WIDE_INT
7128 AT_int (dw_attr_ref a)
7129 {
7130   gcc_assert (a && AT_class (a) == dw_val_class_const);
7131   return a->dw_attr_val.v.val_int;
7132 }
7133
7134 /* Add an unsigned integer attribute value to a DIE.  */
7135
7136 static inline void
7137 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7138                  unsigned HOST_WIDE_INT unsigned_val)
7139 {
7140   dw_attr_node attr;
7141
7142   attr.dw_attr = attr_kind;
7143   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7144   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7145   add_dwarf_attr (die, &attr);
7146 }
7147
7148 static inline unsigned HOST_WIDE_INT
7149 AT_unsigned (dw_attr_ref a)
7150 {
7151   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7152   return a->dw_attr_val.v.val_unsigned;
7153 }
7154
7155 /* Add an unsigned double integer attribute value to a DIE.  */
7156
7157 static inline void
7158 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7159                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7160 {
7161   dw_attr_node attr;
7162
7163   attr.dw_attr = attr_kind;
7164   attr.dw_attr_val.val_class = dw_val_class_const_double;
7165   attr.dw_attr_val.v.val_double.high = high;
7166   attr.dw_attr_val.v.val_double.low = low;
7167   add_dwarf_attr (die, &attr);
7168 }
7169
7170 /* Add a floating point attribute value to a DIE and return it.  */
7171
7172 static inline void
7173 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7174             unsigned int length, unsigned int elt_size, unsigned char *array)
7175 {
7176   dw_attr_node attr;
7177
7178   attr.dw_attr = attr_kind;
7179   attr.dw_attr_val.val_class = dw_val_class_vec;
7180   attr.dw_attr_val.v.val_vec.length = length;
7181   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7182   attr.dw_attr_val.v.val_vec.array = array;
7183   add_dwarf_attr (die, &attr);
7184 }
7185
7186 /* Add an 8-byte data attribute value to a DIE.  */
7187
7188 static inline void
7189 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7190               unsigned char data8[8])
7191 {
7192   dw_attr_node attr;
7193
7194   attr.dw_attr = attr_kind;
7195   attr.dw_attr_val.val_class = dw_val_class_data8;
7196   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7197   add_dwarf_attr (die, &attr);
7198 }
7199
7200 /* Hash and equality functions for debug_str_hash.  */
7201
7202 static hashval_t
7203 debug_str_do_hash (const void *x)
7204 {
7205   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7206 }
7207
7208 static int
7209 debug_str_eq (const void *x1, const void *x2)
7210 {
7211   return strcmp ((((const struct indirect_string_node *)x1)->str),
7212                  (const char *)x2) == 0;
7213 }
7214
7215 /* Add STR to the indirect string hash table.  */
7216
7217 static struct indirect_string_node *
7218 find_AT_string (const char *str)
7219 {
7220   struct indirect_string_node *node;
7221   void **slot;
7222
7223   if (! debug_str_hash)
7224     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7225                                       debug_str_eq, NULL);
7226
7227   slot = htab_find_slot_with_hash (debug_str_hash, str,
7228                                    htab_hash_string (str), INSERT);
7229   if (*slot == NULL)
7230     {
7231       node = ggc_alloc_cleared_indirect_string_node ();
7232       node->str = ggc_strdup (str);
7233       *slot = node;
7234     }
7235   else
7236     node = (struct indirect_string_node *) *slot;
7237
7238   node->refcount++;
7239   return node;
7240 }
7241
7242 /* Add a string attribute value to a DIE.  */
7243
7244 static inline void
7245 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7246 {
7247   dw_attr_node attr;
7248   struct indirect_string_node *node;
7249
7250   node = find_AT_string (str);
7251
7252   attr.dw_attr = attr_kind;
7253   attr.dw_attr_val.val_class = dw_val_class_str;
7254   attr.dw_attr_val.v.val_str = node;
7255   add_dwarf_attr (die, &attr);
7256 }
7257
7258 /* Create a label for an indirect string node, ensuring it is going to
7259    be output, unless its reference count goes down to zero.  */
7260
7261 static inline void
7262 gen_label_for_indirect_string (struct indirect_string_node *node)
7263 {
7264   char label[32];
7265
7266   if (node->label)
7267     return;
7268
7269   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7270   ++dw2_string_counter;
7271   node->label = xstrdup (label);
7272 }
7273
7274 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7275    debug string STR.  */
7276
7277 static inline rtx
7278 get_debug_string_label (const char *str)
7279 {
7280   struct indirect_string_node *node = find_AT_string (str);
7281
7282   debug_str_hash_forced = true;
7283
7284   gen_label_for_indirect_string (node);
7285
7286   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7287 }
7288
7289 static inline const char *
7290 AT_string (dw_attr_ref a)
7291 {
7292   gcc_assert (a && AT_class (a) == dw_val_class_str);
7293   return a->dw_attr_val.v.val_str->str;
7294 }
7295
7296 /* Find out whether a string should be output inline in DIE
7297    or out-of-line in .debug_str section.  */
7298
7299 static enum dwarf_form
7300 AT_string_form (dw_attr_ref a)
7301 {
7302   struct indirect_string_node *node;
7303   unsigned int len;
7304
7305   gcc_assert (a && AT_class (a) == dw_val_class_str);
7306
7307   node = a->dw_attr_val.v.val_str;
7308   if (node->form)
7309     return node->form;
7310
7311   len = strlen (node->str) + 1;
7312
7313   /* If the string is shorter or equal to the size of the reference, it is
7314      always better to put it inline.  */
7315   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7316     return node->form = DW_FORM_string;
7317
7318   /* If we cannot expect the linker to merge strings in .debug_str
7319      section, only put it into .debug_str if it is worth even in this
7320      single module.  */
7321   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7322       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7323       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7324     return node->form = DW_FORM_string;
7325
7326   gen_label_for_indirect_string (node);
7327
7328   return node->form = DW_FORM_strp;
7329 }
7330
7331 /* Add a DIE reference attribute value to a DIE.  */
7332
7333 static inline void
7334 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7335 {
7336   dw_attr_node attr;
7337
7338   attr.dw_attr = attr_kind;
7339   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7340   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7341   attr.dw_attr_val.v.val_die_ref.external = 0;
7342   add_dwarf_attr (die, &attr);
7343 }
7344
7345 /* Add an AT_specification attribute to a DIE, and also make the back
7346    pointer from the specification to the definition.  */
7347
7348 static inline void
7349 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7350 {
7351   add_AT_die_ref (die, DW_AT_specification, targ_die);
7352   gcc_assert (!targ_die->die_definition);
7353   targ_die->die_definition = die;
7354 }
7355
7356 static inline dw_die_ref
7357 AT_ref (dw_attr_ref a)
7358 {
7359   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7360   return a->dw_attr_val.v.val_die_ref.die;
7361 }
7362
7363 static inline int
7364 AT_ref_external (dw_attr_ref a)
7365 {
7366   if (a && AT_class (a) == dw_val_class_die_ref)
7367     return a->dw_attr_val.v.val_die_ref.external;
7368
7369   return 0;
7370 }
7371
7372 static inline void
7373 set_AT_ref_external (dw_attr_ref a, int i)
7374 {
7375   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7376   a->dw_attr_val.v.val_die_ref.external = i;
7377 }
7378
7379 /* Add an FDE reference attribute value to a DIE.  */
7380
7381 static inline void
7382 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7383 {
7384   dw_attr_node attr;
7385
7386   attr.dw_attr = attr_kind;
7387   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7388   attr.dw_attr_val.v.val_fde_index = targ_fde;
7389   add_dwarf_attr (die, &attr);
7390 }
7391
7392 /* Add a location description attribute value to a DIE.  */
7393
7394 static inline void
7395 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7396 {
7397   dw_attr_node attr;
7398
7399   attr.dw_attr = attr_kind;
7400   attr.dw_attr_val.val_class = dw_val_class_loc;
7401   attr.dw_attr_val.v.val_loc = loc;
7402   add_dwarf_attr (die, &attr);
7403 }
7404
7405 static inline dw_loc_descr_ref
7406 AT_loc (dw_attr_ref a)
7407 {
7408   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7409   return a->dw_attr_val.v.val_loc;
7410 }
7411
7412 static inline void
7413 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7414 {
7415   dw_attr_node attr;
7416
7417   attr.dw_attr = attr_kind;
7418   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7419   attr.dw_attr_val.v.val_loc_list = loc_list;
7420   add_dwarf_attr (die, &attr);
7421   have_location_lists = true;
7422 }
7423
7424 static inline dw_loc_list_ref
7425 AT_loc_list (dw_attr_ref a)
7426 {
7427   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7428   return a->dw_attr_val.v.val_loc_list;
7429 }
7430
7431 static inline dw_loc_list_ref *
7432 AT_loc_list_ptr (dw_attr_ref a)
7433 {
7434   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7435   return &a->dw_attr_val.v.val_loc_list;
7436 }
7437
7438 /* Add an address constant attribute value to a DIE.  */
7439
7440 static inline void
7441 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7442 {
7443   dw_attr_node attr;
7444
7445   attr.dw_attr = attr_kind;
7446   attr.dw_attr_val.val_class = dw_val_class_addr;
7447   attr.dw_attr_val.v.val_addr = addr;
7448   add_dwarf_attr (die, &attr);
7449 }
7450
7451 /* Get the RTX from to an address DIE attribute.  */
7452
7453 static inline rtx
7454 AT_addr (dw_attr_ref a)
7455 {
7456   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7457   return a->dw_attr_val.v.val_addr;
7458 }
7459
7460 /* Add a file attribute value to a DIE.  */
7461
7462 static inline void
7463 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7464              struct dwarf_file_data *fd)
7465 {
7466   dw_attr_node attr;
7467
7468   attr.dw_attr = attr_kind;
7469   attr.dw_attr_val.val_class = dw_val_class_file;
7470   attr.dw_attr_val.v.val_file = fd;
7471   add_dwarf_attr (die, &attr);
7472 }
7473
7474 /* Get the dwarf_file_data from a file DIE attribute.  */
7475
7476 static inline struct dwarf_file_data *
7477 AT_file (dw_attr_ref a)
7478 {
7479   gcc_assert (a && AT_class (a) == dw_val_class_file);
7480   return a->dw_attr_val.v.val_file;
7481 }
7482
7483 /* Add a vms delta attribute value to a DIE.  */
7484
7485 static inline void
7486 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7487                   const char *lbl1, const char *lbl2)
7488 {
7489   dw_attr_node attr;
7490
7491   attr.dw_attr = attr_kind;
7492   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7493   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7494   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7495   add_dwarf_attr (die, &attr);
7496 }
7497
7498 /* Add a label identifier attribute value to a DIE.  */
7499
7500 static inline void
7501 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7502 {
7503   dw_attr_node attr;
7504
7505   attr.dw_attr = attr_kind;
7506   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7507   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7508   add_dwarf_attr (die, &attr);
7509 }
7510
7511 /* Add a section offset attribute value to a DIE, an offset into the
7512    debug_line section.  */
7513
7514 static inline void
7515 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7516                 const char *label)
7517 {
7518   dw_attr_node attr;
7519
7520   attr.dw_attr = attr_kind;
7521   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7522   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7523   add_dwarf_attr (die, &attr);
7524 }
7525
7526 /* Add a section offset attribute value to a DIE, an offset into the
7527    debug_macinfo section.  */
7528
7529 static inline void
7530 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7531                const char *label)
7532 {
7533   dw_attr_node attr;
7534
7535   attr.dw_attr = attr_kind;
7536   attr.dw_attr_val.val_class = dw_val_class_macptr;
7537   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7538   add_dwarf_attr (die, &attr);
7539 }
7540
7541 /* Add an offset attribute value to a DIE.  */
7542
7543 static inline void
7544 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7545                unsigned HOST_WIDE_INT offset)
7546 {
7547   dw_attr_node attr;
7548
7549   attr.dw_attr = attr_kind;
7550   attr.dw_attr_val.val_class = dw_val_class_offset;
7551   attr.dw_attr_val.v.val_offset = offset;
7552   add_dwarf_attr (die, &attr);
7553 }
7554
7555 /* Add an range_list attribute value to a DIE.  */
7556
7557 static void
7558 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7559                    long unsigned int offset)
7560 {
7561   dw_attr_node attr;
7562
7563   attr.dw_attr = attr_kind;
7564   attr.dw_attr_val.val_class = dw_val_class_range_list;
7565   attr.dw_attr_val.v.val_offset = offset;
7566   add_dwarf_attr (die, &attr);
7567 }
7568
7569 /* Return the start label of a delta attribute.  */
7570
7571 static inline const char *
7572 AT_vms_delta1 (dw_attr_ref a)
7573 {
7574   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7575   return a->dw_attr_val.v.val_vms_delta.lbl1;
7576 }
7577
7578 /* Return the end label of a delta attribute.  */
7579
7580 static inline const char *
7581 AT_vms_delta2 (dw_attr_ref a)
7582 {
7583   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7584   return a->dw_attr_val.v.val_vms_delta.lbl2;
7585 }
7586
7587 static inline const char *
7588 AT_lbl (dw_attr_ref a)
7589 {
7590   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7591                     || AT_class (a) == dw_val_class_lineptr
7592                     || AT_class (a) == dw_val_class_macptr));
7593   return a->dw_attr_val.v.val_lbl_id;
7594 }
7595
7596 /* Get the attribute of type attr_kind.  */
7597
7598 static dw_attr_ref
7599 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7600 {
7601   dw_attr_ref a;
7602   unsigned ix;
7603   dw_die_ref spec = NULL;
7604
7605   if (! die)
7606     return NULL;
7607
7608   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7609     if (a->dw_attr == attr_kind)
7610       return a;
7611     else if (a->dw_attr == DW_AT_specification
7612              || a->dw_attr == DW_AT_abstract_origin)
7613       spec = AT_ref (a);
7614
7615   if (spec)
7616     return get_AT (spec, attr_kind);
7617
7618   return NULL;
7619 }
7620
7621 /* Return the "low pc" attribute value, typically associated with a subprogram
7622    DIE.  Return null if the "low pc" attribute is either not present, or if it
7623    cannot be represented as an assembler label identifier.  */
7624
7625 static inline const char *
7626 get_AT_low_pc (dw_die_ref die)
7627 {
7628   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7629
7630   return a ? AT_lbl (a) : NULL;
7631 }
7632
7633 /* Return the "high pc" attribute value, typically associated with a subprogram
7634    DIE.  Return null if the "high pc" attribute is either not present, or if it
7635    cannot be represented as an assembler label identifier.  */
7636
7637 static inline const char *
7638 get_AT_hi_pc (dw_die_ref die)
7639 {
7640   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7641
7642   return a ? AT_lbl (a) : NULL;
7643 }
7644
7645 /* Return the value of the string attribute designated by ATTR_KIND, or
7646    NULL if it is not present.  */
7647
7648 static inline const char *
7649 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7650 {
7651   dw_attr_ref a = get_AT (die, attr_kind);
7652
7653   return a ? AT_string (a) : NULL;
7654 }
7655
7656 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7657    if it is not present.  */
7658
7659 static inline int
7660 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7661 {
7662   dw_attr_ref a = get_AT (die, attr_kind);
7663
7664   return a ? AT_flag (a) : 0;
7665 }
7666
7667 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7668    if it is not present.  */
7669
7670 static inline unsigned
7671 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7672 {
7673   dw_attr_ref a = get_AT (die, attr_kind);
7674
7675   return a ? AT_unsigned (a) : 0;
7676 }
7677
7678 static inline dw_die_ref
7679 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7680 {
7681   dw_attr_ref a = get_AT (die, attr_kind);
7682
7683   return a ? AT_ref (a) : NULL;
7684 }
7685
7686 static inline struct dwarf_file_data *
7687 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7688 {
7689   dw_attr_ref a = get_AT (die, attr_kind);
7690
7691   return a ? AT_file (a) : NULL;
7692 }
7693
7694 /* Return TRUE if the language is C++.  */
7695
7696 static inline bool
7697 is_cxx (void)
7698 {
7699   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7700
7701   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7702 }
7703
7704 /* Return TRUE if the language is Fortran.  */
7705
7706 static inline bool
7707 is_fortran (void)
7708 {
7709   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7710
7711   return (lang == DW_LANG_Fortran77
7712           || lang == DW_LANG_Fortran90
7713           || lang == DW_LANG_Fortran95);
7714 }
7715
7716 /* Return TRUE if the language is Ada.  */
7717
7718 static inline bool
7719 is_ada (void)
7720 {
7721   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7722
7723   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7724 }
7725
7726 /* Remove the specified attribute if present.  */
7727
7728 static void
7729 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7730 {
7731   dw_attr_ref a;
7732   unsigned ix;
7733
7734   if (! die)
7735     return;
7736
7737   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7738     if (a->dw_attr == attr_kind)
7739       {
7740         if (AT_class (a) == dw_val_class_str)
7741           if (a->dw_attr_val.v.val_str->refcount)
7742             a->dw_attr_val.v.val_str->refcount--;
7743
7744         /* VEC_ordered_remove should help reduce the number of abbrevs
7745            that are needed.  */
7746         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7747         return;
7748       }
7749 }
7750
7751 /* Remove CHILD from its parent.  PREV must have the property that
7752    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7753
7754 static void
7755 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7756 {
7757   gcc_assert (child->die_parent == prev->die_parent);
7758   gcc_assert (prev->die_sib == child);
7759   if (prev == child)
7760     {
7761       gcc_assert (child->die_parent->die_child == child);
7762       prev = NULL;
7763     }
7764   else
7765     prev->die_sib = child->die_sib;
7766   if (child->die_parent->die_child == child)
7767     child->die_parent->die_child = prev;
7768 }
7769
7770 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7771    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7772
7773 static void
7774 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7775 {
7776   dw_die_ref parent = old_child->die_parent;
7777
7778   gcc_assert (parent == prev->die_parent);
7779   gcc_assert (prev->die_sib == old_child);
7780
7781   new_child->die_parent = parent;
7782   if (prev == old_child)
7783     {
7784       gcc_assert (parent->die_child == old_child);
7785       new_child->die_sib = new_child;
7786     }
7787   else
7788     {
7789       prev->die_sib = new_child;
7790       new_child->die_sib = old_child->die_sib;
7791     }
7792   if (old_child->die_parent->die_child == old_child)
7793     old_child->die_parent->die_child = new_child;
7794 }
7795
7796 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7797
7798 static void
7799 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7800 {
7801   dw_die_ref c;
7802   new_parent->die_child = old_parent->die_child;
7803   old_parent->die_child = NULL;
7804   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7805 }
7806
7807 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7808    matches TAG.  */
7809
7810 static void
7811 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7812 {
7813   dw_die_ref c;
7814
7815   c = die->die_child;
7816   if (c) do {
7817     dw_die_ref prev = c;
7818     c = c->die_sib;
7819     while (c->die_tag == tag)
7820       {
7821         remove_child_with_prev (c, prev);
7822         /* Might have removed every child.  */
7823         if (c == c->die_sib)
7824           return;
7825         c = c->die_sib;
7826       }
7827   } while (c != die->die_child);
7828 }
7829
7830 /* Add a CHILD_DIE as the last child of DIE.  */
7831
7832 static void
7833 add_child_die (dw_die_ref die, dw_die_ref child_die)
7834 {
7835   /* FIXME this should probably be an assert.  */
7836   if (! die || ! child_die)
7837     return;
7838   gcc_assert (die != child_die);
7839
7840   child_die->die_parent = die;
7841   if (die->die_child)
7842     {
7843       child_die->die_sib = die->die_child->die_sib;
7844       die->die_child->die_sib = child_die;
7845     }
7846   else
7847     child_die->die_sib = child_die;
7848   die->die_child = child_die;
7849 }
7850
7851 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7852    is the specification, to the end of PARENT's list of children.
7853    This is done by removing and re-adding it.  */
7854
7855 static void
7856 splice_child_die (dw_die_ref parent, dw_die_ref child)
7857 {
7858   dw_die_ref p;
7859
7860   /* We want the declaration DIE from inside the class, not the
7861      specification DIE at toplevel.  */
7862   if (child->die_parent != parent)
7863     {
7864       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7865
7866       if (tmp)
7867         child = tmp;
7868     }
7869
7870   gcc_assert (child->die_parent == parent
7871               || (child->die_parent
7872                   == get_AT_ref (parent, DW_AT_specification)));
7873
7874   for (p = child->die_parent->die_child; ; p = p->die_sib)
7875     if (p->die_sib == child)
7876       {
7877         remove_child_with_prev (child, p);
7878         break;
7879       }
7880
7881   add_child_die (parent, child);
7882 }
7883
7884 /* Return a pointer to a newly created DIE node.  */
7885
7886 static inline dw_die_ref
7887 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7888 {
7889   dw_die_ref die = ggc_alloc_cleared_die_node ();
7890
7891   die->die_tag = tag_value;
7892
7893   if (parent_die != NULL)
7894     add_child_die (parent_die, die);
7895   else
7896     {
7897       limbo_die_node *limbo_node;
7898
7899       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7900       limbo_node->die = die;
7901       limbo_node->created_for = t;
7902       limbo_node->next = limbo_die_list;
7903       limbo_die_list = limbo_node;
7904     }
7905
7906   return die;
7907 }
7908
7909 /* Return the DIE associated with the given type specifier.  */
7910
7911 static inline dw_die_ref
7912 lookup_type_die (tree type)
7913 {
7914   return TYPE_SYMTAB_DIE (type);
7915 }
7916
7917 /* Equate a DIE to a given type specifier.  */
7918
7919 static inline void
7920 equate_type_number_to_die (tree type, dw_die_ref type_die)
7921 {
7922   TYPE_SYMTAB_DIE (type) = type_die;
7923 }
7924
7925 /* Returns a hash value for X (which really is a die_struct).  */
7926
7927 static hashval_t
7928 decl_die_table_hash (const void *x)
7929 {
7930   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7931 }
7932
7933 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7934
7935 static int
7936 decl_die_table_eq (const void *x, const void *y)
7937 {
7938   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7939 }
7940
7941 /* Return the DIE associated with a given declaration.  */
7942
7943 static inline dw_die_ref
7944 lookup_decl_die (tree decl)
7945 {
7946   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7947 }
7948
7949 /* Returns a hash value for X (which really is a var_loc_list).  */
7950
7951 static hashval_t
7952 decl_loc_table_hash (const void *x)
7953 {
7954   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7955 }
7956
7957 /* Return nonzero if decl_id of var_loc_list X is the same as
7958    UID of decl *Y.  */
7959
7960 static int
7961 decl_loc_table_eq (const void *x, const void *y)
7962 {
7963   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7964 }
7965
7966 /* Return the var_loc list associated with a given declaration.  */
7967
7968 static inline var_loc_list *
7969 lookup_decl_loc (const_tree decl)
7970 {
7971   if (!decl_loc_table)
7972     return NULL;
7973   return (var_loc_list *)
7974     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7975 }
7976
7977 /* Equate a DIE to a particular declaration.  */
7978
7979 static void
7980 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7981 {
7982   unsigned int decl_id = DECL_UID (decl);
7983   void **slot;
7984
7985   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7986   *slot = decl_die;
7987   decl_die->decl_id = decl_id;
7988 }
7989
7990 /* Return how many bits covers PIECE EXPR_LIST.  */
7991
7992 static int
7993 decl_piece_bitsize (rtx piece)
7994 {
7995   int ret = (int) GET_MODE (piece);
7996   if (ret)
7997     return ret;
7998   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7999               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8000   return INTVAL (XEXP (XEXP (piece, 0), 0));
8001 }
8002
8003 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8004
8005 static rtx *
8006 decl_piece_varloc_ptr (rtx piece)
8007 {
8008   if ((int) GET_MODE (piece))
8009     return &XEXP (piece, 0);
8010   else
8011     return &XEXP (XEXP (piece, 0), 1);
8012 }
8013
8014 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8015    Next is the chain of following piece nodes.  */
8016
8017 static rtx
8018 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8019 {
8020   if (bitsize <= (int) MAX_MACHINE_MODE)
8021     return alloc_EXPR_LIST (bitsize, loc_note, next);
8022   else
8023     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8024                                                GEN_INT (bitsize),
8025                                                loc_note), next);
8026 }
8027
8028 /* Return rtx that should be stored into loc field for
8029    LOC_NOTE and BITPOS/BITSIZE.  */
8030
8031 static rtx
8032 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8033                       HOST_WIDE_INT bitsize)
8034 {
8035   if (bitsize != -1)
8036     {
8037       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8038       if (bitpos != 0)
8039         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8040     }
8041   return loc_note;
8042 }
8043
8044 /* This function either modifies location piece list *DEST in
8045    place (if SRC and INNER is NULL), or copies location piece list
8046    *SRC to *DEST while modifying it.  Location BITPOS is modified
8047    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8048    not copied and if needed some padding around it is added.
8049    When modifying in place, DEST should point to EXPR_LIST where
8050    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8051    to the start of the whole list and INNER points to the EXPR_LIST
8052    where earlier pieces cover PIECE_BITPOS bits.  */
8053
8054 static void
8055 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8056                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8057                    HOST_WIDE_INT bitsize, rtx loc_note)
8058 {
8059   int diff;
8060   bool copy = inner != NULL;
8061
8062   if (copy)
8063     {
8064       /* First copy all nodes preceeding the current bitpos.  */
8065       while (src != inner)
8066         {
8067           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8068                                    decl_piece_bitsize (*src), NULL_RTX);
8069           dest = &XEXP (*dest, 1);
8070           src = &XEXP (*src, 1);
8071         }
8072     }
8073   /* Add padding if needed.  */
8074   if (bitpos != piece_bitpos)
8075     {
8076       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8077                                copy ? NULL_RTX : *dest);
8078       dest = &XEXP (*dest, 1);
8079     }
8080   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8081     {
8082       gcc_assert (!copy);
8083       /* A piece with correct bitpos and bitsize already exist,
8084          just update the location for it and return.  */
8085       *decl_piece_varloc_ptr (*dest) = loc_note;
8086       return;
8087     }
8088   /* Add the piece that changed.  */
8089   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8090   dest = &XEXP (*dest, 1);
8091   /* Skip over pieces that overlap it.  */
8092   diff = bitpos - piece_bitpos + bitsize;
8093   if (!copy)
8094     src = dest;
8095   while (diff > 0 && *src)
8096     {
8097       rtx piece = *src;
8098       diff -= decl_piece_bitsize (piece);
8099       if (copy)
8100         src = &XEXP (piece, 1);
8101       else
8102         {
8103           *src = XEXP (piece, 1);
8104           free_EXPR_LIST_node (piece);
8105         }
8106     }
8107   /* Add padding if needed.  */
8108   if (diff < 0 && *src)
8109     {
8110       if (!copy)
8111         dest = src;
8112       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8113       dest = &XEXP (*dest, 1);
8114     }
8115   if (!copy)
8116     return;
8117   /* Finally copy all nodes following it.  */
8118   while (*src)
8119     {
8120       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8121                                decl_piece_bitsize (*src), NULL_RTX);
8122       dest = &XEXP (*dest, 1);
8123       src = &XEXP (*src, 1);
8124     }
8125 }
8126
8127 /* Add a variable location node to the linked list for DECL.  */
8128
8129 static struct var_loc_node *
8130 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8131 {
8132   unsigned int decl_id;
8133   var_loc_list *temp;
8134   void **slot;
8135   struct var_loc_node *loc = NULL;
8136   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8137
8138   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8139     {
8140       tree realdecl = DECL_DEBUG_EXPR (decl);
8141       if (realdecl && handled_component_p (realdecl))
8142         {
8143           HOST_WIDE_INT maxsize;
8144           tree innerdecl;
8145           innerdecl
8146             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8147           if (!DECL_P (innerdecl)
8148               || DECL_IGNORED_P (innerdecl)
8149               || TREE_STATIC (innerdecl)
8150               || bitsize <= 0
8151               || bitpos + bitsize > 256
8152               || bitsize != maxsize)
8153             return NULL;
8154           decl = innerdecl;
8155         }
8156     }
8157
8158   decl_id = DECL_UID (decl);
8159   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8160   if (*slot == NULL)
8161     {
8162       temp = ggc_alloc_cleared_var_loc_list ();
8163       temp->decl_id = decl_id;
8164       *slot = temp;
8165     }
8166   else
8167     temp = (var_loc_list *) *slot;
8168
8169   if (temp->last)
8170     {
8171       struct var_loc_node *last = temp->last, *unused = NULL;
8172       rtx *piece_loc = NULL, last_loc_note;
8173       int piece_bitpos = 0;
8174       if (last->next)
8175         {
8176           last = last->next;
8177           gcc_assert (last->next == NULL);
8178         }
8179       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8180         {
8181           piece_loc = &last->loc;
8182           do
8183             {
8184               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8185               if (piece_bitpos + cur_bitsize > bitpos)
8186                 break;
8187               piece_bitpos += cur_bitsize;
8188               piece_loc = &XEXP (*piece_loc, 1);
8189             }
8190           while (*piece_loc);
8191         }
8192       /* TEMP->LAST here is either pointer to the last but one or
8193          last element in the chained list, LAST is pointer to the
8194          last element.  */
8195       if (label && strcmp (last->label, label) == 0)
8196         {
8197           /* For SRA optimized variables if there weren't any real
8198              insns since last note, just modify the last node.  */
8199           if (piece_loc != NULL)
8200             {
8201               adjust_piece_list (piece_loc, NULL, NULL,
8202                                  bitpos, piece_bitpos, bitsize, loc_note);
8203               return NULL;
8204             }
8205           /* If the last note doesn't cover any instructions, remove it.  */
8206           if (temp->last != last)
8207             {
8208               temp->last->next = NULL;
8209               unused = last;
8210               last = temp->last;
8211               gcc_assert (strcmp (last->label, label) != 0);
8212             }
8213           else
8214             {
8215               gcc_assert (temp->first == temp->last);
8216               memset (temp->last, '\0', sizeof (*temp->last));
8217               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8218               return temp->last;
8219             }
8220         }
8221       if (bitsize == -1 && NOTE_P (last->loc))
8222         last_loc_note = last->loc;
8223       else if (piece_loc != NULL
8224                && *piece_loc != NULL_RTX
8225                && piece_bitpos == bitpos
8226                && decl_piece_bitsize (*piece_loc) == bitsize)
8227         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8228       else
8229         last_loc_note = NULL_RTX;
8230       /* If the current location is the same as the end of the list,
8231          and either both or neither of the locations is uninitialized,
8232          we have nothing to do.  */
8233       if (last_loc_note == NULL_RTX
8234           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8235                             NOTE_VAR_LOCATION_LOC (loc_note)))
8236           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8237                != NOTE_VAR_LOCATION_STATUS (loc_note))
8238               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8239                    == VAR_INIT_STATUS_UNINITIALIZED)
8240                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8241                       == VAR_INIT_STATUS_UNINITIALIZED))))
8242         {
8243           /* Add LOC to the end of list and update LAST.  If the last
8244              element of the list has been removed above, reuse its
8245              memory for the new node, otherwise allocate a new one.  */
8246           if (unused)
8247             {
8248               loc = unused;
8249               memset (loc, '\0', sizeof (*loc));
8250             }
8251           else
8252             loc = ggc_alloc_cleared_var_loc_node ();
8253           if (bitsize == -1 || piece_loc == NULL)
8254             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8255           else
8256             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8257                                bitpos, piece_bitpos, bitsize, loc_note);
8258           last->next = loc;
8259           /* Ensure TEMP->LAST will point either to the new last but one
8260              element of the chain, or to the last element in it.  */
8261           if (last != temp->last)
8262             temp->last = last;
8263         }
8264       else if (unused)
8265         ggc_free (unused);
8266     }
8267   else
8268     {
8269       loc = ggc_alloc_cleared_var_loc_node ();
8270       temp->first = loc;
8271       temp->last = loc;
8272       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8273     }
8274   return loc;
8275 }
8276 \f
8277 /* Keep track of the number of spaces used to indent the
8278    output of the debugging routines that print the structure of
8279    the DIE internal representation.  */
8280 static int print_indent;
8281
8282 /* Indent the line the number of spaces given by print_indent.  */
8283
8284 static inline void
8285 print_spaces (FILE *outfile)
8286 {
8287   fprintf (outfile, "%*s", print_indent, "");
8288 }
8289
8290 /* Print a type signature in hex.  */
8291
8292 static inline void
8293 print_signature (FILE *outfile, char *sig)
8294 {
8295   int i;
8296
8297   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8298     fprintf (outfile, "%02x", sig[i] & 0xff);
8299 }
8300
8301 /* Print the information associated with a given DIE, and its children.
8302    This routine is a debugging aid only.  */
8303
8304 static void
8305 print_die (dw_die_ref die, FILE *outfile)
8306 {
8307   dw_attr_ref a;
8308   dw_die_ref c;
8309   unsigned ix;
8310
8311   print_spaces (outfile);
8312   fprintf (outfile, "DIE %4ld: %s\n",
8313            die->die_offset, dwarf_tag_name (die->die_tag));
8314   print_spaces (outfile);
8315   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8316   fprintf (outfile, " offset: %ld\n", die->die_offset);
8317   if (dwarf_version >= 4 && die->die_id.die_type_node)
8318     {
8319       print_spaces (outfile);
8320       fprintf (outfile, "  signature: ");
8321       print_signature (outfile, die->die_id.die_type_node->signature);
8322       fprintf (outfile, "\n");
8323     }
8324
8325   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8326     {
8327       print_spaces (outfile);
8328       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8329
8330       switch (AT_class (a))
8331         {
8332         case dw_val_class_addr:
8333           fprintf (outfile, "address");
8334           break;
8335         case dw_val_class_offset:
8336           fprintf (outfile, "offset");
8337           break;
8338         case dw_val_class_loc:
8339           fprintf (outfile, "location descriptor");
8340           break;
8341         case dw_val_class_loc_list:
8342           fprintf (outfile, "location list -> label:%s",
8343                    AT_loc_list (a)->ll_symbol);
8344           break;
8345         case dw_val_class_range_list:
8346           fprintf (outfile, "range list");
8347           break;
8348         case dw_val_class_const:
8349           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8350           break;
8351         case dw_val_class_unsigned_const:
8352           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8353           break;
8354         case dw_val_class_const_double:
8355           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8356                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8357                    a->dw_attr_val.v.val_double.high,
8358                    a->dw_attr_val.v.val_double.low);
8359           break;
8360         case dw_val_class_vec:
8361           fprintf (outfile, "floating-point or vector constant");
8362           break;
8363         case dw_val_class_flag:
8364           fprintf (outfile, "%u", AT_flag (a));
8365           break;
8366         case dw_val_class_die_ref:
8367           if (AT_ref (a) != NULL)
8368             {
8369               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8370                 {
8371                   fprintf (outfile, "die -> signature: ");
8372                   print_signature (outfile,
8373                                    AT_ref (a)->die_id.die_type_node->signature);
8374                 }
8375               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8376                 fprintf (outfile, "die -> label: %s",
8377                          AT_ref (a)->die_id.die_symbol);
8378               else
8379                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8380             }
8381           else
8382             fprintf (outfile, "die -> <null>");
8383           break;
8384         case dw_val_class_vms_delta:
8385           fprintf (outfile, "delta: @slotcount(%s-%s)",
8386                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8387           break;
8388         case dw_val_class_lbl_id:
8389         case dw_val_class_lineptr:
8390         case dw_val_class_macptr:
8391           fprintf (outfile, "label: %s", AT_lbl (a));
8392           break;
8393         case dw_val_class_str:
8394           if (AT_string (a) != NULL)
8395             fprintf (outfile, "\"%s\"", AT_string (a));
8396           else
8397             fprintf (outfile, "<null>");
8398           break;
8399         case dw_val_class_file:
8400           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8401                    AT_file (a)->emitted_number);
8402           break;
8403         case dw_val_class_data8:
8404           {
8405             int i;
8406
8407             for (i = 0; i < 8; i++)
8408               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8409             break;
8410           }
8411         default:
8412           break;
8413         }
8414
8415       fprintf (outfile, "\n");
8416     }
8417
8418   if (die->die_child != NULL)
8419     {
8420       print_indent += 4;
8421       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8422       print_indent -= 4;
8423     }
8424   if (print_indent == 0)
8425     fprintf (outfile, "\n");
8426 }
8427
8428 /* Print the contents of the source code line number correspondence table.
8429    This routine is a debugging aid only.  */
8430
8431 static void
8432 print_dwarf_line_table (FILE *outfile)
8433 {
8434   unsigned i;
8435   dw_line_info_ref line_info;
8436
8437   fprintf (outfile, "\n\nDWARF source line information\n");
8438   for (i = 1; i < line_info_table_in_use; i++)
8439     {
8440       line_info = &line_info_table[i];
8441       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8442                line_info->dw_file_num,
8443                line_info->dw_line_num);
8444     }
8445
8446   fprintf (outfile, "\n\n");
8447 }
8448
8449 /* Print the information collected for a given DIE.  */
8450
8451 DEBUG_FUNCTION void
8452 debug_dwarf_die (dw_die_ref die)
8453 {
8454   print_die (die, stderr);
8455 }
8456
8457 /* Print all DWARF information collected for the compilation unit.
8458    This routine is a debugging aid only.  */
8459
8460 DEBUG_FUNCTION void
8461 debug_dwarf (void)
8462 {
8463   print_indent = 0;
8464   print_die (comp_unit_die (), stderr);
8465   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8466     print_dwarf_line_table (stderr);
8467 }
8468 \f
8469 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8470    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8471    DIE that marks the start of the DIEs for this include file.  */
8472
8473 static dw_die_ref
8474 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8475 {
8476   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8477   dw_die_ref new_unit = gen_compile_unit_die (filename);
8478
8479   new_unit->die_sib = old_unit;
8480   return new_unit;
8481 }
8482
8483 /* Close an include-file CU and reopen the enclosing one.  */
8484
8485 static dw_die_ref
8486 pop_compile_unit (dw_die_ref old_unit)
8487 {
8488   dw_die_ref new_unit = old_unit->die_sib;
8489
8490   old_unit->die_sib = NULL;
8491   return new_unit;
8492 }
8493
8494 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8495 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8496
8497 /* Calculate the checksum of a location expression.  */
8498
8499 static inline void
8500 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8501 {
8502   int tem;
8503
8504   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8505   CHECKSUM (tem);
8506   CHECKSUM (loc->dw_loc_oprnd1);
8507   CHECKSUM (loc->dw_loc_oprnd2);
8508 }
8509
8510 /* Calculate the checksum of an attribute.  */
8511
8512 static void
8513 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8514 {
8515   dw_loc_descr_ref loc;
8516   rtx r;
8517
8518   CHECKSUM (at->dw_attr);
8519
8520   /* We don't care that this was compiled with a different compiler
8521      snapshot; if the output is the same, that's what matters.  */
8522   if (at->dw_attr == DW_AT_producer)
8523     return;
8524
8525   switch (AT_class (at))
8526     {
8527     case dw_val_class_const:
8528       CHECKSUM (at->dw_attr_val.v.val_int);
8529       break;
8530     case dw_val_class_unsigned_const:
8531       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8532       break;
8533     case dw_val_class_const_double:
8534       CHECKSUM (at->dw_attr_val.v.val_double);
8535       break;
8536     case dw_val_class_vec:
8537       CHECKSUM (at->dw_attr_val.v.val_vec);
8538       break;
8539     case dw_val_class_flag:
8540       CHECKSUM (at->dw_attr_val.v.val_flag);
8541       break;
8542     case dw_val_class_str:
8543       CHECKSUM_STRING (AT_string (at));
8544       break;
8545
8546     case dw_val_class_addr:
8547       r = AT_addr (at);
8548       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8549       CHECKSUM_STRING (XSTR (r, 0));
8550       break;
8551
8552     case dw_val_class_offset:
8553       CHECKSUM (at->dw_attr_val.v.val_offset);
8554       break;
8555
8556     case dw_val_class_loc:
8557       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8558         loc_checksum (loc, ctx);
8559       break;
8560
8561     case dw_val_class_die_ref:
8562       die_checksum (AT_ref (at), ctx, mark);
8563       break;
8564
8565     case dw_val_class_fde_ref:
8566     case dw_val_class_vms_delta:
8567     case dw_val_class_lbl_id:
8568     case dw_val_class_lineptr:
8569     case dw_val_class_macptr:
8570       break;
8571
8572     case dw_val_class_file:
8573       CHECKSUM_STRING (AT_file (at)->filename);
8574       break;
8575
8576     case dw_val_class_data8:
8577       CHECKSUM (at->dw_attr_val.v.val_data8);
8578       break;
8579
8580     default:
8581       break;
8582     }
8583 }
8584
8585 /* Calculate the checksum of a DIE.  */
8586
8587 static void
8588 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8589 {
8590   dw_die_ref c;
8591   dw_attr_ref a;
8592   unsigned ix;
8593
8594   /* To avoid infinite recursion.  */
8595   if (die->die_mark)
8596     {
8597       CHECKSUM (die->die_mark);
8598       return;
8599     }
8600   die->die_mark = ++(*mark);
8601
8602   CHECKSUM (die->die_tag);
8603
8604   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8605     attr_checksum (a, ctx, mark);
8606
8607   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8608 }
8609
8610 #undef CHECKSUM
8611 #undef CHECKSUM_STRING
8612
8613 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8614 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8615 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8616 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8617 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8618 #define CHECKSUM_ATTR(FOO) \
8619   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8620
8621 /* Calculate the checksum of a number in signed LEB128 format.  */
8622
8623 static void
8624 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8625 {
8626   unsigned char byte;
8627   bool more;
8628
8629   while (1)
8630     {
8631       byte = (value & 0x7f);
8632       value >>= 7;
8633       more = !((value == 0 && (byte & 0x40) == 0)
8634                 || (value == -1 && (byte & 0x40) != 0));
8635       if (more)
8636         byte |= 0x80;
8637       CHECKSUM (byte);
8638       if (!more)
8639         break;
8640     }
8641 }
8642
8643 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8644
8645 static void
8646 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8647 {
8648   while (1)
8649     {
8650       unsigned char byte = (value & 0x7f);
8651       value >>= 7;
8652       if (value != 0)
8653         /* More bytes to follow.  */
8654         byte |= 0x80;
8655       CHECKSUM (byte);
8656       if (value == 0)
8657         break;
8658     }
8659 }
8660
8661 /* Checksum the context of the DIE.  This adds the names of any
8662    surrounding namespaces or structures to the checksum.  */
8663
8664 static void
8665 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8666 {
8667   const char *name;
8668   dw_die_ref spec;
8669   int tag = die->die_tag;
8670
8671   if (tag != DW_TAG_namespace
8672       && tag != DW_TAG_structure_type
8673       && tag != DW_TAG_class_type)
8674     return;
8675
8676   name = get_AT_string (die, DW_AT_name);
8677
8678   spec = get_AT_ref (die, DW_AT_specification);
8679   if (spec != NULL)
8680     die = spec;
8681
8682   if (die->die_parent != NULL)
8683     checksum_die_context (die->die_parent, ctx);
8684
8685   CHECKSUM_ULEB128 ('C');
8686   CHECKSUM_ULEB128 (tag);
8687   if (name != NULL)
8688     CHECKSUM_STRING (name);
8689 }
8690
8691 /* Calculate the checksum of a location expression.  */
8692
8693 static inline void
8694 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8695 {
8696   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8697      were emitted as a DW_FORM_sdata instead of a location expression.  */
8698   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8699     {
8700       CHECKSUM_ULEB128 (DW_FORM_sdata);
8701       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8702       return;
8703     }
8704
8705   /* Otherwise, just checksum the raw location expression.  */
8706   while (loc != NULL)
8707     {
8708       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8709       CHECKSUM (loc->dw_loc_oprnd1);
8710       CHECKSUM (loc->dw_loc_oprnd2);
8711       loc = loc->dw_loc_next;
8712     }
8713 }
8714
8715 /* Calculate the checksum of an attribute.  */
8716
8717 static void
8718 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8719                        struct md5_ctx *ctx, int *mark)
8720 {
8721   dw_loc_descr_ref loc;
8722   rtx r;
8723
8724   if (AT_class (at) == dw_val_class_die_ref)
8725     {
8726       dw_die_ref target_die = AT_ref (at);
8727
8728       /* For pointer and reference types, we checksum only the (qualified)
8729          name of the target type (if there is a name).  For friend entries,
8730          we checksum only the (qualified) name of the target type or function.
8731          This allows the checksum to remain the same whether the target type
8732          is complete or not.  */
8733       if ((at->dw_attr == DW_AT_type
8734            && (tag == DW_TAG_pointer_type
8735                || tag == DW_TAG_reference_type
8736                || tag == DW_TAG_rvalue_reference_type
8737                || tag == DW_TAG_ptr_to_member_type))
8738           || (at->dw_attr == DW_AT_friend
8739               && tag == DW_TAG_friend))
8740         {
8741           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8742
8743           if (name_attr != NULL)
8744             {
8745               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8746
8747               if (decl == NULL)
8748                 decl = target_die;
8749               CHECKSUM_ULEB128 ('N');
8750               CHECKSUM_ULEB128 (at->dw_attr);
8751               if (decl->die_parent != NULL)
8752                 checksum_die_context (decl->die_parent, ctx);
8753               CHECKSUM_ULEB128 ('E');
8754               CHECKSUM_STRING (AT_string (name_attr));
8755               return;
8756             }
8757         }
8758
8759       /* For all other references to another DIE, we check to see if the
8760          target DIE has already been visited.  If it has, we emit a
8761          backward reference; if not, we descend recursively.  */
8762       if (target_die->die_mark > 0)
8763         {
8764           CHECKSUM_ULEB128 ('R');
8765           CHECKSUM_ULEB128 (at->dw_attr);
8766           CHECKSUM_ULEB128 (target_die->die_mark);
8767         }
8768       else
8769         {
8770           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8771
8772           if (decl == NULL)
8773             decl = target_die;
8774           target_die->die_mark = ++(*mark);
8775           CHECKSUM_ULEB128 ('T');
8776           CHECKSUM_ULEB128 (at->dw_attr);
8777           if (decl->die_parent != NULL)
8778             checksum_die_context (decl->die_parent, ctx);
8779           die_checksum_ordered (target_die, ctx, mark);
8780         }
8781       return;
8782     }
8783
8784   CHECKSUM_ULEB128 ('A');
8785   CHECKSUM_ULEB128 (at->dw_attr);
8786
8787   switch (AT_class (at))
8788     {
8789     case dw_val_class_const:
8790       CHECKSUM_ULEB128 (DW_FORM_sdata);
8791       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8792       break;
8793
8794     case dw_val_class_unsigned_const:
8795       CHECKSUM_ULEB128 (DW_FORM_sdata);
8796       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8797       break;
8798
8799     case dw_val_class_const_double:
8800       CHECKSUM_ULEB128 (DW_FORM_block);
8801       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8802       CHECKSUM (at->dw_attr_val.v.val_double);
8803       break;
8804
8805     case dw_val_class_vec:
8806       CHECKSUM_ULEB128 (DW_FORM_block);
8807       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8808       CHECKSUM (at->dw_attr_val.v.val_vec);
8809       break;
8810
8811     case dw_val_class_flag:
8812       CHECKSUM_ULEB128 (DW_FORM_flag);
8813       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8814       break;
8815
8816     case dw_val_class_str:
8817       CHECKSUM_ULEB128 (DW_FORM_string);
8818       CHECKSUM_STRING (AT_string (at));
8819       break;
8820
8821     case dw_val_class_addr:
8822       r = AT_addr (at);
8823       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8824       CHECKSUM_ULEB128 (DW_FORM_string);
8825       CHECKSUM_STRING (XSTR (r, 0));
8826       break;
8827
8828     case dw_val_class_offset:
8829       CHECKSUM_ULEB128 (DW_FORM_sdata);
8830       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8831       break;
8832
8833     case dw_val_class_loc:
8834       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8835         loc_checksum_ordered (loc, ctx);
8836       break;
8837
8838     case dw_val_class_fde_ref:
8839     case dw_val_class_lbl_id:
8840     case dw_val_class_lineptr:
8841     case dw_val_class_macptr:
8842       break;
8843
8844     case dw_val_class_file:
8845       CHECKSUM_ULEB128 (DW_FORM_string);
8846       CHECKSUM_STRING (AT_file (at)->filename);
8847       break;
8848
8849     case dw_val_class_data8:
8850       CHECKSUM (at->dw_attr_val.v.val_data8);
8851       break;
8852
8853     default:
8854       break;
8855     }
8856 }
8857
8858 struct checksum_attributes
8859 {
8860   dw_attr_ref at_name;
8861   dw_attr_ref at_type;
8862   dw_attr_ref at_friend;
8863   dw_attr_ref at_accessibility;
8864   dw_attr_ref at_address_class;
8865   dw_attr_ref at_allocated;
8866   dw_attr_ref at_artificial;
8867   dw_attr_ref at_associated;
8868   dw_attr_ref at_binary_scale;
8869   dw_attr_ref at_bit_offset;
8870   dw_attr_ref at_bit_size;
8871   dw_attr_ref at_bit_stride;
8872   dw_attr_ref at_byte_size;
8873   dw_attr_ref at_byte_stride;
8874   dw_attr_ref at_const_value;
8875   dw_attr_ref at_containing_type;
8876   dw_attr_ref at_count;
8877   dw_attr_ref at_data_location;
8878   dw_attr_ref at_data_member_location;
8879   dw_attr_ref at_decimal_scale;
8880   dw_attr_ref at_decimal_sign;
8881   dw_attr_ref at_default_value;
8882   dw_attr_ref at_digit_count;
8883   dw_attr_ref at_discr;
8884   dw_attr_ref at_discr_list;
8885   dw_attr_ref at_discr_value;
8886   dw_attr_ref at_encoding;
8887   dw_attr_ref at_endianity;
8888   dw_attr_ref at_explicit;
8889   dw_attr_ref at_is_optional;
8890   dw_attr_ref at_location;
8891   dw_attr_ref at_lower_bound;
8892   dw_attr_ref at_mutable;
8893   dw_attr_ref at_ordering;
8894   dw_attr_ref at_picture_string;
8895   dw_attr_ref at_prototyped;
8896   dw_attr_ref at_small;
8897   dw_attr_ref at_segment;
8898   dw_attr_ref at_string_length;
8899   dw_attr_ref at_threads_scaled;
8900   dw_attr_ref at_upper_bound;
8901   dw_attr_ref at_use_location;
8902   dw_attr_ref at_use_UTF8;
8903   dw_attr_ref at_variable_parameter;
8904   dw_attr_ref at_virtuality;
8905   dw_attr_ref at_visibility;
8906   dw_attr_ref at_vtable_elem_location;
8907 };
8908
8909 /* Collect the attributes that we will want to use for the checksum.  */
8910
8911 static void
8912 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8913 {
8914   dw_attr_ref a;
8915   unsigned ix;
8916
8917   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8918     {
8919       switch (a->dw_attr)
8920         {
8921         case DW_AT_name:
8922           attrs->at_name = a;
8923           break;
8924         case DW_AT_type:
8925           attrs->at_type = a;
8926           break;
8927         case DW_AT_friend:
8928           attrs->at_friend = a;
8929           break;
8930         case DW_AT_accessibility:
8931           attrs->at_accessibility = a;
8932           break;
8933         case DW_AT_address_class:
8934           attrs->at_address_class = a;
8935           break;
8936         case DW_AT_allocated:
8937           attrs->at_allocated = a;
8938           break;
8939         case DW_AT_artificial:
8940           attrs->at_artificial = a;
8941           break;
8942         case DW_AT_associated:
8943           attrs->at_associated = a;
8944           break;
8945         case DW_AT_binary_scale:
8946           attrs->at_binary_scale = a;
8947           break;
8948         case DW_AT_bit_offset:
8949           attrs->at_bit_offset = a;
8950           break;
8951         case DW_AT_bit_size:
8952           attrs->at_bit_size = a;
8953           break;
8954         case DW_AT_bit_stride:
8955           attrs->at_bit_stride = a;
8956           break;
8957         case DW_AT_byte_size:
8958           attrs->at_byte_size = a;
8959           break;
8960         case DW_AT_byte_stride:
8961           attrs->at_byte_stride = a;
8962           break;
8963         case DW_AT_const_value:
8964           attrs->at_const_value = a;
8965           break;
8966         case DW_AT_containing_type:
8967           attrs->at_containing_type = a;
8968           break;
8969         case DW_AT_count:
8970           attrs->at_count = a;
8971           break;
8972         case DW_AT_data_location:
8973           attrs->at_data_location = a;
8974           break;
8975         case DW_AT_data_member_location:
8976           attrs->at_data_member_location = a;
8977           break;
8978         case DW_AT_decimal_scale:
8979           attrs->at_decimal_scale = a;
8980           break;
8981         case DW_AT_decimal_sign:
8982           attrs->at_decimal_sign = a;
8983           break;
8984         case DW_AT_default_value:
8985           attrs->at_default_value = a;
8986           break;
8987         case DW_AT_digit_count:
8988           attrs->at_digit_count = a;
8989           break;
8990         case DW_AT_discr:
8991           attrs->at_discr = a;
8992           break;
8993         case DW_AT_discr_list:
8994           attrs->at_discr_list = a;
8995           break;
8996         case DW_AT_discr_value:
8997           attrs->at_discr_value = a;
8998           break;
8999         case DW_AT_encoding:
9000           attrs->at_encoding = a;
9001           break;
9002         case DW_AT_endianity:
9003           attrs->at_endianity = a;
9004           break;
9005         case DW_AT_explicit:
9006           attrs->at_explicit = a;
9007           break;
9008         case DW_AT_is_optional:
9009           attrs->at_is_optional = a;
9010           break;
9011         case DW_AT_location:
9012           attrs->at_location = a;
9013           break;
9014         case DW_AT_lower_bound:
9015           attrs->at_lower_bound = a;
9016           break;
9017         case DW_AT_mutable:
9018           attrs->at_mutable = a;
9019           break;
9020         case DW_AT_ordering:
9021           attrs->at_ordering = a;
9022           break;
9023         case DW_AT_picture_string:
9024           attrs->at_picture_string = a;
9025           break;
9026         case DW_AT_prototyped:
9027           attrs->at_prototyped = a;
9028           break;
9029         case DW_AT_small:
9030           attrs->at_small = a;
9031           break;
9032         case DW_AT_segment:
9033           attrs->at_segment = a;
9034           break;
9035         case DW_AT_string_length:
9036           attrs->at_string_length = a;
9037           break;
9038         case DW_AT_threads_scaled:
9039           attrs->at_threads_scaled = a;
9040           break;
9041         case DW_AT_upper_bound:
9042           attrs->at_upper_bound = a;
9043           break;
9044         case DW_AT_use_location:
9045           attrs->at_use_location = a;
9046           break;
9047         case DW_AT_use_UTF8:
9048           attrs->at_use_UTF8 = a;
9049           break;
9050         case DW_AT_variable_parameter:
9051           attrs->at_variable_parameter = a;
9052           break;
9053         case DW_AT_virtuality:
9054           attrs->at_virtuality = a;
9055           break;
9056         case DW_AT_visibility:
9057           attrs->at_visibility = a;
9058           break;
9059         case DW_AT_vtable_elem_location:
9060           attrs->at_vtable_elem_location = a;
9061           break;
9062         default:
9063           break;
9064         }
9065     }
9066 }
9067
9068 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9069
9070 static void
9071 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9072 {
9073   dw_die_ref c;
9074   dw_die_ref decl;
9075   struct checksum_attributes attrs;
9076
9077   CHECKSUM_ULEB128 ('D');
9078   CHECKSUM_ULEB128 (die->die_tag);
9079
9080   memset (&attrs, 0, sizeof (attrs));
9081
9082   decl = get_AT_ref (die, DW_AT_specification);
9083   if (decl != NULL)
9084     collect_checksum_attributes (&attrs, decl);
9085   collect_checksum_attributes (&attrs, die);
9086
9087   CHECKSUM_ATTR (attrs.at_name);
9088   CHECKSUM_ATTR (attrs.at_accessibility);
9089   CHECKSUM_ATTR (attrs.at_address_class);
9090   CHECKSUM_ATTR (attrs.at_allocated);
9091   CHECKSUM_ATTR (attrs.at_artificial);
9092   CHECKSUM_ATTR (attrs.at_associated);
9093   CHECKSUM_ATTR (attrs.at_binary_scale);
9094   CHECKSUM_ATTR (attrs.at_bit_offset);
9095   CHECKSUM_ATTR (attrs.at_bit_size);
9096   CHECKSUM_ATTR (attrs.at_bit_stride);
9097   CHECKSUM_ATTR (attrs.at_byte_size);
9098   CHECKSUM_ATTR (attrs.at_byte_stride);
9099   CHECKSUM_ATTR (attrs.at_const_value);
9100   CHECKSUM_ATTR (attrs.at_containing_type);
9101   CHECKSUM_ATTR (attrs.at_count);
9102   CHECKSUM_ATTR (attrs.at_data_location);
9103   CHECKSUM_ATTR (attrs.at_data_member_location);
9104   CHECKSUM_ATTR (attrs.at_decimal_scale);
9105   CHECKSUM_ATTR (attrs.at_decimal_sign);
9106   CHECKSUM_ATTR (attrs.at_default_value);
9107   CHECKSUM_ATTR (attrs.at_digit_count);
9108   CHECKSUM_ATTR (attrs.at_discr);
9109   CHECKSUM_ATTR (attrs.at_discr_list);
9110   CHECKSUM_ATTR (attrs.at_discr_value);
9111   CHECKSUM_ATTR (attrs.at_encoding);
9112   CHECKSUM_ATTR (attrs.at_endianity);
9113   CHECKSUM_ATTR (attrs.at_explicit);
9114   CHECKSUM_ATTR (attrs.at_is_optional);
9115   CHECKSUM_ATTR (attrs.at_location);
9116   CHECKSUM_ATTR (attrs.at_lower_bound);
9117   CHECKSUM_ATTR (attrs.at_mutable);
9118   CHECKSUM_ATTR (attrs.at_ordering);
9119   CHECKSUM_ATTR (attrs.at_picture_string);
9120   CHECKSUM_ATTR (attrs.at_prototyped);
9121   CHECKSUM_ATTR (attrs.at_small);
9122   CHECKSUM_ATTR (attrs.at_segment);
9123   CHECKSUM_ATTR (attrs.at_string_length);
9124   CHECKSUM_ATTR (attrs.at_threads_scaled);
9125   CHECKSUM_ATTR (attrs.at_upper_bound);
9126   CHECKSUM_ATTR (attrs.at_use_location);
9127   CHECKSUM_ATTR (attrs.at_use_UTF8);
9128   CHECKSUM_ATTR (attrs.at_variable_parameter);
9129   CHECKSUM_ATTR (attrs.at_virtuality);
9130   CHECKSUM_ATTR (attrs.at_visibility);
9131   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9132   CHECKSUM_ATTR (attrs.at_type);
9133   CHECKSUM_ATTR (attrs.at_friend);
9134
9135   /* Checksum the child DIEs, except for nested types and member functions.  */
9136   c = die->die_child;
9137   if (c) do {
9138     dw_attr_ref name_attr;
9139
9140     c = c->die_sib;
9141     name_attr = get_AT (c, DW_AT_name);
9142     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9143         && name_attr != NULL)
9144       {
9145         CHECKSUM_ULEB128 ('S');
9146         CHECKSUM_ULEB128 (c->die_tag);
9147         CHECKSUM_STRING (AT_string (name_attr));
9148       }
9149     else
9150       {
9151         /* Mark this DIE so it gets processed when unmarking.  */
9152         if (c->die_mark == 0)
9153           c->die_mark = -1;
9154         die_checksum_ordered (c, ctx, mark);
9155       }
9156   } while (c != die->die_child);
9157
9158   CHECKSUM_ULEB128 (0);
9159 }
9160
9161 #undef CHECKSUM
9162 #undef CHECKSUM_STRING
9163 #undef CHECKSUM_ATTR
9164 #undef CHECKSUM_LEB128
9165 #undef CHECKSUM_ULEB128
9166
9167 /* Generate the type signature for DIE.  This is computed by generating an
9168    MD5 checksum over the DIE's tag, its relevant attributes, and its
9169    children.  Attributes that are references to other DIEs are processed
9170    by recursion, using the MARK field to prevent infinite recursion.
9171    If the DIE is nested inside a namespace or another type, we also
9172    need to include that context in the signature.  The lower 64 bits
9173    of the resulting MD5 checksum comprise the signature.  */
9174
9175 static void
9176 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9177 {
9178   int mark;
9179   const char *name;
9180   unsigned char checksum[16];
9181   struct md5_ctx ctx;
9182   dw_die_ref decl;
9183
9184   name = get_AT_string (die, DW_AT_name);
9185   decl = get_AT_ref (die, DW_AT_specification);
9186
9187   /* First, compute a signature for just the type name (and its surrounding
9188      context, if any.  This is stored in the type unit DIE for link-time
9189      ODR (one-definition rule) checking.  */
9190
9191   if (is_cxx() && name != NULL)
9192     {
9193       md5_init_ctx (&ctx);
9194
9195       /* Checksum the names of surrounding namespaces and structures.  */
9196       if (decl != NULL && decl->die_parent != NULL)
9197         checksum_die_context (decl->die_parent, &ctx);
9198
9199       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9200       md5_process_bytes (name, strlen (name) + 1, &ctx);
9201       md5_finish_ctx (&ctx, checksum);
9202
9203       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9204     }
9205
9206   /* Next, compute the complete type signature.  */
9207
9208   md5_init_ctx (&ctx);
9209   mark = 1;
9210   die->die_mark = mark;
9211
9212   /* Checksum the names of surrounding namespaces and structures.  */
9213   if (decl != NULL && decl->die_parent != NULL)
9214     checksum_die_context (decl->die_parent, &ctx);
9215
9216   /* Checksum the DIE and its children.  */
9217   die_checksum_ordered (die, &ctx, &mark);
9218   unmark_all_dies (die);
9219   md5_finish_ctx (&ctx, checksum);
9220
9221   /* Store the signature in the type node and link the type DIE and the
9222      type node together.  */
9223   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9224           DWARF_TYPE_SIGNATURE_SIZE);
9225   die->die_id.die_type_node = type_node;
9226   type_node->type_die = die;
9227
9228   /* If the DIE is a specification, link its declaration to the type node
9229      as well.  */
9230   if (decl != NULL)
9231     decl->die_id.die_type_node = type_node;
9232 }
9233
9234 /* Do the location expressions look same?  */
9235 static inline int
9236 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9237 {
9238   return loc1->dw_loc_opc == loc2->dw_loc_opc
9239          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9240          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9241 }
9242
9243 /* Do the values look the same?  */
9244 static int
9245 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9246 {
9247   dw_loc_descr_ref loc1, loc2;
9248   rtx r1, r2;
9249
9250   if (v1->val_class != v2->val_class)
9251     return 0;
9252
9253   switch (v1->val_class)
9254     {
9255     case dw_val_class_const:
9256       return v1->v.val_int == v2->v.val_int;
9257     case dw_val_class_unsigned_const:
9258       return v1->v.val_unsigned == v2->v.val_unsigned;
9259     case dw_val_class_const_double:
9260       return v1->v.val_double.high == v2->v.val_double.high
9261              && v1->v.val_double.low == v2->v.val_double.low;
9262     case dw_val_class_vec:
9263       if (v1->v.val_vec.length != v2->v.val_vec.length
9264           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9265         return 0;
9266       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9267                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9268         return 0;
9269       return 1;
9270     case dw_val_class_flag:
9271       return v1->v.val_flag == v2->v.val_flag;
9272     case dw_val_class_str:
9273       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9274
9275     case dw_val_class_addr:
9276       r1 = v1->v.val_addr;
9277       r2 = v2->v.val_addr;
9278       if (GET_CODE (r1) != GET_CODE (r2))
9279         return 0;
9280       return !rtx_equal_p (r1, r2);
9281
9282     case dw_val_class_offset:
9283       return v1->v.val_offset == v2->v.val_offset;
9284
9285     case dw_val_class_loc:
9286       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9287            loc1 && loc2;
9288            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9289         if (!same_loc_p (loc1, loc2, mark))
9290           return 0;
9291       return !loc1 && !loc2;
9292
9293     case dw_val_class_die_ref:
9294       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9295
9296     case dw_val_class_fde_ref:
9297     case dw_val_class_vms_delta:
9298     case dw_val_class_lbl_id:
9299     case dw_val_class_lineptr:
9300     case dw_val_class_macptr:
9301       return 1;
9302
9303     case dw_val_class_file:
9304       return v1->v.val_file == v2->v.val_file;
9305
9306     case dw_val_class_data8:
9307       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9308
9309     default:
9310       return 1;
9311     }
9312 }
9313
9314 /* Do the attributes look the same?  */
9315
9316 static int
9317 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9318 {
9319   if (at1->dw_attr != at2->dw_attr)
9320     return 0;
9321
9322   /* We don't care that this was compiled with a different compiler
9323      snapshot; if the output is the same, that's what matters. */
9324   if (at1->dw_attr == DW_AT_producer)
9325     return 1;
9326
9327   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9328 }
9329
9330 /* Do the dies look the same?  */
9331
9332 static int
9333 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9334 {
9335   dw_die_ref c1, c2;
9336   dw_attr_ref a1;
9337   unsigned ix;
9338
9339   /* To avoid infinite recursion.  */
9340   if (die1->die_mark)
9341     return die1->die_mark == die2->die_mark;
9342   die1->die_mark = die2->die_mark = ++(*mark);
9343
9344   if (die1->die_tag != die2->die_tag)
9345     return 0;
9346
9347   if (VEC_length (dw_attr_node, die1->die_attr)
9348       != VEC_length (dw_attr_node, die2->die_attr))
9349     return 0;
9350
9351   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9352     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9353       return 0;
9354
9355   c1 = die1->die_child;
9356   c2 = die2->die_child;
9357   if (! c1)
9358     {
9359       if (c2)
9360         return 0;
9361     }
9362   else
9363     for (;;)
9364       {
9365         if (!same_die_p (c1, c2, mark))
9366           return 0;
9367         c1 = c1->die_sib;
9368         c2 = c2->die_sib;
9369         if (c1 == die1->die_child)
9370           {
9371             if (c2 == die2->die_child)
9372               break;
9373             else
9374               return 0;
9375           }
9376     }
9377
9378   return 1;
9379 }
9380
9381 /* Do the dies look the same?  Wrapper around same_die_p.  */
9382
9383 static int
9384 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9385 {
9386   int mark = 0;
9387   int ret = same_die_p (die1, die2, &mark);
9388
9389   unmark_all_dies (die1);
9390   unmark_all_dies (die2);
9391
9392   return ret;
9393 }
9394
9395 /* The prefix to attach to symbols on DIEs in the current comdat debug
9396    info section.  */
9397 static char *comdat_symbol_id;
9398
9399 /* The index of the current symbol within the current comdat CU.  */
9400 static unsigned int comdat_symbol_number;
9401
9402 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9403    children, and set comdat_symbol_id accordingly.  */
9404
9405 static void
9406 compute_section_prefix (dw_die_ref unit_die)
9407 {
9408   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9409   const char *base = die_name ? lbasename (die_name) : "anonymous";
9410   char *name = XALLOCAVEC (char, strlen (base) + 64);
9411   char *p;
9412   int i, mark;
9413   unsigned char checksum[16];
9414   struct md5_ctx ctx;
9415
9416   /* Compute the checksum of the DIE, then append part of it as hex digits to
9417      the name filename of the unit.  */
9418
9419   md5_init_ctx (&ctx);
9420   mark = 0;
9421   die_checksum (unit_die, &ctx, &mark);
9422   unmark_all_dies (unit_die);
9423   md5_finish_ctx (&ctx, checksum);
9424
9425   sprintf (name, "%s.", base);
9426   clean_symbol_name (name);
9427
9428   p = name + strlen (name);
9429   for (i = 0; i < 4; i++)
9430     {
9431       sprintf (p, "%.2x", checksum[i]);
9432       p += 2;
9433     }
9434
9435   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9436   comdat_symbol_number = 0;
9437 }
9438
9439 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9440
9441 static int
9442 is_type_die (dw_die_ref die)
9443 {
9444   switch (die->die_tag)
9445     {
9446     case DW_TAG_array_type:
9447     case DW_TAG_class_type:
9448     case DW_TAG_interface_type:
9449     case DW_TAG_enumeration_type:
9450     case DW_TAG_pointer_type:
9451     case DW_TAG_reference_type:
9452     case DW_TAG_rvalue_reference_type:
9453     case DW_TAG_string_type:
9454     case DW_TAG_structure_type:
9455     case DW_TAG_subroutine_type:
9456     case DW_TAG_union_type:
9457     case DW_TAG_ptr_to_member_type:
9458     case DW_TAG_set_type:
9459     case DW_TAG_subrange_type:
9460     case DW_TAG_base_type:
9461     case DW_TAG_const_type:
9462     case DW_TAG_file_type:
9463     case DW_TAG_packed_type:
9464     case DW_TAG_volatile_type:
9465     case DW_TAG_typedef:
9466       return 1;
9467     default:
9468       return 0;
9469     }
9470 }
9471
9472 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9473    Basically, we want to choose the bits that are likely to be shared between
9474    compilations (types) and leave out the bits that are specific to individual
9475    compilations (functions).  */
9476
9477 static int
9478 is_comdat_die (dw_die_ref c)
9479 {
9480   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9481      we do for stabs.  The advantage is a greater likelihood of sharing between
9482      objects that don't include headers in the same order (and therefore would
9483      put the base types in a different comdat).  jason 8/28/00 */
9484
9485   if (c->die_tag == DW_TAG_base_type)
9486     return 0;
9487
9488   if (c->die_tag == DW_TAG_pointer_type
9489       || c->die_tag == DW_TAG_reference_type
9490       || c->die_tag == DW_TAG_rvalue_reference_type
9491       || c->die_tag == DW_TAG_const_type
9492       || c->die_tag == DW_TAG_volatile_type)
9493     {
9494       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9495
9496       return t ? is_comdat_die (t) : 0;
9497     }
9498
9499   return is_type_die (c);
9500 }
9501
9502 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9503    compilation unit.  */
9504
9505 static int
9506 is_symbol_die (dw_die_ref c)
9507 {
9508   return (is_type_die (c)
9509           || is_declaration_die (c)
9510           || c->die_tag == DW_TAG_namespace
9511           || c->die_tag == DW_TAG_module);
9512 }
9513
9514 /* Returns true iff C is a compile-unit DIE.  */
9515
9516 static inline bool
9517 is_cu_die (dw_die_ref c)
9518 {
9519   return c && c->die_tag == DW_TAG_compile_unit;
9520 }
9521
9522 static char *
9523 gen_internal_sym (const char *prefix)
9524 {
9525   char buf[256];
9526
9527   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9528   return xstrdup (buf);
9529 }
9530
9531 /* Assign symbols to all worthy DIEs under DIE.  */
9532
9533 static void
9534 assign_symbol_names (dw_die_ref die)
9535 {
9536   dw_die_ref c;
9537
9538   if (is_symbol_die (die))
9539     {
9540       if (comdat_symbol_id)
9541         {
9542           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9543
9544           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9545                    comdat_symbol_id, comdat_symbol_number++);
9546           die->die_id.die_symbol = xstrdup (p);
9547         }
9548       else
9549         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9550     }
9551
9552   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9553 }
9554
9555 struct cu_hash_table_entry
9556 {
9557   dw_die_ref cu;
9558   unsigned min_comdat_num, max_comdat_num;
9559   struct cu_hash_table_entry *next;
9560 };
9561
9562 /* Routines to manipulate hash table of CUs.  */
9563 static hashval_t
9564 htab_cu_hash (const void *of)
9565 {
9566   const struct cu_hash_table_entry *const entry =
9567     (const struct cu_hash_table_entry *) of;
9568
9569   return htab_hash_string (entry->cu->die_id.die_symbol);
9570 }
9571
9572 static int
9573 htab_cu_eq (const void *of1, const void *of2)
9574 {
9575   const struct cu_hash_table_entry *const entry1 =
9576     (const struct cu_hash_table_entry *) of1;
9577   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9578
9579   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9580 }
9581
9582 static void
9583 htab_cu_del (void *what)
9584 {
9585   struct cu_hash_table_entry *next,
9586     *entry = (struct cu_hash_table_entry *) what;
9587
9588   while (entry)
9589     {
9590       next = entry->next;
9591       free (entry);
9592       entry = next;
9593     }
9594 }
9595
9596 /* Check whether we have already seen this CU and set up SYM_NUM
9597    accordingly.  */
9598 static int
9599 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9600 {
9601   struct cu_hash_table_entry dummy;
9602   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9603
9604   dummy.max_comdat_num = 0;
9605
9606   slot = (struct cu_hash_table_entry **)
9607     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9608         INSERT);
9609   entry = *slot;
9610
9611   for (; entry; last = entry, entry = entry->next)
9612     {
9613       if (same_die_p_wrap (cu, entry->cu))
9614         break;
9615     }
9616
9617   if (entry)
9618     {
9619       *sym_num = entry->min_comdat_num;
9620       return 1;
9621     }
9622
9623   entry = XCNEW (struct cu_hash_table_entry);
9624   entry->cu = cu;
9625   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9626   entry->next = *slot;
9627   *slot = entry;
9628
9629   return 0;
9630 }
9631
9632 /* Record SYM_NUM to record of CU in HTABLE.  */
9633 static void
9634 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9635 {
9636   struct cu_hash_table_entry **slot, *entry;
9637
9638   slot = (struct cu_hash_table_entry **)
9639     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9640         NO_INSERT);
9641   entry = *slot;
9642
9643   entry->max_comdat_num = sym_num;
9644 }
9645
9646 /* Traverse the DIE (which is always comp_unit_die), and set up
9647    additional compilation units for each of the include files we see
9648    bracketed by BINCL/EINCL.  */
9649
9650 static void
9651 break_out_includes (dw_die_ref die)
9652 {
9653   dw_die_ref c;
9654   dw_die_ref unit = NULL;
9655   limbo_die_node *node, **pnode;
9656   htab_t cu_hash_table;
9657
9658   c = die->die_child;
9659   if (c) do {
9660     dw_die_ref prev = c;
9661     c = c->die_sib;
9662     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9663            || (unit && is_comdat_die (c)))
9664       {
9665         dw_die_ref next = c->die_sib;
9666
9667         /* This DIE is for a secondary CU; remove it from the main one.  */
9668         remove_child_with_prev (c, prev);
9669
9670         if (c->die_tag == DW_TAG_GNU_BINCL)
9671           unit = push_new_compile_unit (unit, c);
9672         else if (c->die_tag == DW_TAG_GNU_EINCL)
9673           unit = pop_compile_unit (unit);
9674         else
9675           add_child_die (unit, c);
9676         c = next;
9677         if (c == die->die_child)
9678           break;
9679       }
9680   } while (c != die->die_child);
9681
9682 #if 0
9683   /* We can only use this in debugging, since the frontend doesn't check
9684      to make sure that we leave every include file we enter.  */
9685   gcc_assert (!unit);
9686 #endif
9687
9688   assign_symbol_names (die);
9689   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9690   for (node = limbo_die_list, pnode = &limbo_die_list;
9691        node;
9692        node = node->next)
9693     {
9694       int is_dupl;
9695
9696       compute_section_prefix (node->die);
9697       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9698                         &comdat_symbol_number);
9699       assign_symbol_names (node->die);
9700       if (is_dupl)
9701         *pnode = node->next;
9702       else
9703         {
9704           pnode = &node->next;
9705           record_comdat_symbol_number (node->die, cu_hash_table,
9706                 comdat_symbol_number);
9707         }
9708     }
9709   htab_delete (cu_hash_table);
9710 }
9711
9712 /* Return non-zero if this DIE is a declaration.  */
9713
9714 static int
9715 is_declaration_die (dw_die_ref die)
9716 {
9717   dw_attr_ref a;
9718   unsigned ix;
9719
9720   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9721     if (a->dw_attr == DW_AT_declaration)
9722       return 1;
9723
9724   return 0;
9725 }
9726
9727 /* Return non-zero if this DIE is nested inside a subprogram.  */
9728
9729 static int
9730 is_nested_in_subprogram (dw_die_ref die)
9731 {
9732   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9733
9734   if (decl == NULL)
9735     decl = die;
9736   return local_scope_p (decl);
9737 }
9738
9739 /* Return non-zero if this is a type DIE that should be moved to a
9740    COMDAT .debug_types section.  */
9741
9742 static int
9743 should_move_die_to_comdat (dw_die_ref die)
9744 {
9745   switch (die->die_tag)
9746     {
9747     case DW_TAG_class_type:
9748     case DW_TAG_structure_type:
9749     case DW_TAG_enumeration_type:
9750     case DW_TAG_union_type:
9751       /* Don't move declarations, inlined instances, or types nested in a
9752          subprogram.  */
9753       if (is_declaration_die (die)
9754           || get_AT (die, DW_AT_abstract_origin)
9755           || is_nested_in_subprogram (die))
9756         return 0;
9757       return 1;
9758     case DW_TAG_array_type:
9759     case DW_TAG_interface_type:
9760     case DW_TAG_pointer_type:
9761     case DW_TAG_reference_type:
9762     case DW_TAG_rvalue_reference_type:
9763     case DW_TAG_string_type:
9764     case DW_TAG_subroutine_type:
9765     case DW_TAG_ptr_to_member_type:
9766     case DW_TAG_set_type:
9767     case DW_TAG_subrange_type:
9768     case DW_TAG_base_type:
9769     case DW_TAG_const_type:
9770     case DW_TAG_file_type:
9771     case DW_TAG_packed_type:
9772     case DW_TAG_volatile_type:
9773     case DW_TAG_typedef:
9774     default:
9775       return 0;
9776     }
9777 }
9778
9779 /* Make a clone of DIE.  */
9780
9781 static dw_die_ref
9782 clone_die (dw_die_ref die)
9783 {
9784   dw_die_ref clone;
9785   dw_attr_ref a;
9786   unsigned ix;
9787
9788   clone = ggc_alloc_cleared_die_node ();
9789   clone->die_tag = die->die_tag;
9790
9791   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9792     add_dwarf_attr (clone, a);
9793
9794   return clone;
9795 }
9796
9797 /* Make a clone of the tree rooted at DIE.  */
9798
9799 static dw_die_ref
9800 clone_tree (dw_die_ref die)
9801 {
9802   dw_die_ref c;
9803   dw_die_ref clone = clone_die (die);
9804
9805   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9806
9807   return clone;
9808 }
9809
9810 /* Make a clone of DIE as a declaration.  */
9811
9812 static dw_die_ref
9813 clone_as_declaration (dw_die_ref die)
9814 {
9815   dw_die_ref clone;
9816   dw_die_ref decl;
9817   dw_attr_ref a;
9818   unsigned ix;
9819
9820   /* If the DIE is already a declaration, just clone it.  */
9821   if (is_declaration_die (die))
9822     return clone_die (die);
9823
9824   /* If the DIE is a specification, just clone its declaration DIE.  */
9825   decl = get_AT_ref (die, DW_AT_specification);
9826   if (decl != NULL)
9827     return clone_die (decl);
9828
9829   clone = ggc_alloc_cleared_die_node ();
9830   clone->die_tag = die->die_tag;
9831
9832   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9833     {
9834       /* We don't want to copy over all attributes.
9835          For example we don't want DW_AT_byte_size because otherwise we will no
9836          longer have a declaration and GDB will treat it as a definition.  */
9837
9838       switch (a->dw_attr)
9839         {
9840         case DW_AT_artificial:
9841         case DW_AT_containing_type:
9842         case DW_AT_external:
9843         case DW_AT_name:
9844         case DW_AT_type:
9845         case DW_AT_virtuality:
9846         case DW_AT_linkage_name:
9847         case DW_AT_MIPS_linkage_name:
9848           add_dwarf_attr (clone, a);
9849           break;
9850         case DW_AT_byte_size:
9851         default:
9852           break;
9853         }
9854     }
9855
9856   if (die->die_id.die_type_node)
9857     add_AT_die_ref (clone, DW_AT_signature, die);
9858
9859   add_AT_flag (clone, DW_AT_declaration, 1);
9860   return clone;
9861 }
9862
9863 /* Copy the declaration context to the new compile unit DIE.  This includes
9864    any surrounding namespace or type declarations.  If the DIE has an
9865    AT_specification attribute, it also includes attributes and children
9866    attached to the specification.  */
9867
9868 static void
9869 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9870 {
9871   dw_die_ref decl;
9872   dw_die_ref new_decl;
9873
9874   decl = get_AT_ref (die, DW_AT_specification);
9875   if (decl == NULL)
9876     decl = die;
9877   else
9878     {
9879       unsigned ix;
9880       dw_die_ref c;
9881       dw_attr_ref a;
9882
9883       /* Copy the type node pointer from the new DIE to the original
9884          declaration DIE so we can forward references later.  */
9885       decl->die_id.die_type_node = die->die_id.die_type_node;
9886
9887       remove_AT (die, DW_AT_specification);
9888
9889       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9890         {
9891           if (a->dw_attr != DW_AT_name
9892               && a->dw_attr != DW_AT_declaration
9893               && a->dw_attr != DW_AT_external)
9894             add_dwarf_attr (die, a);
9895         }
9896
9897       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9898     }
9899
9900   if (decl->die_parent != NULL
9901       && decl->die_parent->die_tag != DW_TAG_compile_unit
9902       && decl->die_parent->die_tag != DW_TAG_type_unit)
9903     {
9904       new_decl = copy_ancestor_tree (unit, decl, NULL);
9905       if (new_decl != NULL)
9906         {
9907           remove_AT (new_decl, DW_AT_signature);
9908           add_AT_specification (die, new_decl);
9909         }
9910     }
9911 }
9912
9913 /* Generate the skeleton ancestor tree for the given NODE, then clone
9914    the DIE and add the clone into the tree.  */
9915
9916 static void
9917 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9918 {
9919   if (node->new_die != NULL)
9920     return;
9921
9922   node->new_die = clone_as_declaration (node->old_die);
9923
9924   if (node->parent != NULL)
9925     {
9926       generate_skeleton_ancestor_tree (node->parent);
9927       add_child_die (node->parent->new_die, node->new_die);
9928     }
9929 }
9930
9931 /* Generate a skeleton tree of DIEs containing any declarations that are
9932    found in the original tree.  We traverse the tree looking for declaration
9933    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9934
9935 static void
9936 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9937 {
9938   skeleton_chain_node node;
9939   dw_die_ref c;
9940   dw_die_ref first;
9941   dw_die_ref prev = NULL;
9942   dw_die_ref next = NULL;
9943
9944   node.parent = parent;
9945
9946   first = c = parent->old_die->die_child;
9947   if (c)
9948     next = c->die_sib;
9949   if (c) do {
9950     if (prev == NULL || prev->die_sib == c)
9951       prev = c;
9952     c = next;
9953     next = (c == first ? NULL : c->die_sib);
9954     node.old_die = c;
9955     node.new_die = NULL;
9956     if (is_declaration_die (c))
9957       {
9958         /* Clone the existing DIE, move the original to the skeleton
9959            tree (which is in the main CU), and put the clone, with
9960            all the original's children, where the original came from.  */
9961         dw_die_ref clone = clone_die (c);
9962         move_all_children (c, clone);
9963
9964         replace_child (c, clone, prev);
9965         generate_skeleton_ancestor_tree (parent);
9966         add_child_die (parent->new_die, c);
9967         node.new_die = c;
9968         c = clone;
9969       }
9970     generate_skeleton_bottom_up (&node);
9971   } while (next != NULL);
9972 }
9973
9974 /* Wrapper function for generate_skeleton_bottom_up.  */
9975
9976 static dw_die_ref
9977 generate_skeleton (dw_die_ref die)
9978 {
9979   skeleton_chain_node node;
9980
9981   node.old_die = die;
9982   node.new_die = NULL;
9983   node.parent = NULL;
9984
9985   /* If this type definition is nested inside another type,
9986      always leave at least a declaration in its place.  */
9987   if (die->die_parent != NULL && is_type_die (die->die_parent))
9988     node.new_die = clone_as_declaration (die);
9989
9990   generate_skeleton_bottom_up (&node);
9991   return node.new_die;
9992 }
9993
9994 /* Remove the DIE from its parent, possibly replacing it with a cloned
9995    declaration.  The original DIE will be moved to a new compile unit
9996    so that existing references to it follow it to the new location.  If
9997    any of the original DIE's descendants is a declaration, we need to
9998    replace the original DIE with a skeleton tree and move the
9999    declarations back into the skeleton tree.  */
10000
10001 static dw_die_ref
10002 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10003 {
10004   dw_die_ref skeleton;
10005
10006   skeleton = generate_skeleton (child);
10007   if (skeleton == NULL)
10008     remove_child_with_prev (child, prev);
10009   else
10010     {
10011       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10012       replace_child (child, skeleton, prev);
10013     }
10014
10015   return skeleton;
10016 }
10017
10018 /* Traverse the DIE and set up additional .debug_types sections for each
10019    type worthy of being placed in a COMDAT section.  */
10020
10021 static void
10022 break_out_comdat_types (dw_die_ref die)
10023 {
10024   dw_die_ref c;
10025   dw_die_ref first;
10026   dw_die_ref prev = NULL;
10027   dw_die_ref next = NULL;
10028   dw_die_ref unit = NULL;
10029
10030   first = c = die->die_child;
10031   if (c)
10032     next = c->die_sib;
10033   if (c) do {
10034     if (prev == NULL || prev->die_sib == c)
10035       prev = c;
10036     c = next;
10037     next = (c == first ? NULL : c->die_sib);
10038     if (should_move_die_to_comdat (c))
10039       {
10040         dw_die_ref replacement;
10041         comdat_type_node_ref type_node;
10042
10043         /* Create a new type unit DIE as the root for the new tree, and
10044            add it to the list of comdat types.  */
10045         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10046         add_AT_unsigned (unit, DW_AT_language,
10047                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10048         type_node = ggc_alloc_cleared_comdat_type_node ();
10049         type_node->root_die = unit;
10050         type_node->next = comdat_type_list;
10051         comdat_type_list = type_node;
10052
10053         /* Generate the type signature.  */
10054         generate_type_signature (c, type_node);
10055
10056         /* Copy the declaration context, attributes, and children of the
10057            declaration into the new compile unit DIE.  */
10058         copy_declaration_context (unit, c);
10059
10060         /* Remove this DIE from the main CU.  */
10061         replacement = remove_child_or_replace_with_skeleton (c, prev);
10062
10063         /* Break out nested types into their own type units.  */
10064         break_out_comdat_types (c);
10065
10066         /* Add the DIE to the new compunit.  */
10067         add_child_die (unit, c);
10068
10069         if (replacement != NULL)
10070           c = replacement;
10071       }
10072     else if (c->die_tag == DW_TAG_namespace
10073              || c->die_tag == DW_TAG_class_type
10074              || c->die_tag == DW_TAG_structure_type
10075              || c->die_tag == DW_TAG_union_type)
10076       {
10077         /* Look for nested types that can be broken out.  */
10078         break_out_comdat_types (c);
10079       }
10080   } while (next != NULL);
10081 }
10082
10083 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10084
10085 struct decl_table_entry
10086 {
10087   dw_die_ref orig;
10088   dw_die_ref copy;
10089 };
10090
10091 /* Routines to manipulate hash table of copied declarations.  */
10092
10093 static hashval_t
10094 htab_decl_hash (const void *of)
10095 {
10096   const struct decl_table_entry *const entry =
10097     (const struct decl_table_entry *) of;
10098
10099   return htab_hash_pointer (entry->orig);
10100 }
10101
10102 static int
10103 htab_decl_eq (const void *of1, const void *of2)
10104 {
10105   const struct decl_table_entry *const entry1 =
10106     (const struct decl_table_entry *) of1;
10107   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10108
10109   return entry1->orig == entry2;
10110 }
10111
10112 static void
10113 htab_decl_del (void *what)
10114 {
10115   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10116
10117   free (entry);
10118 }
10119
10120 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10121    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10122    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10123    to check if the ancestor has already been copied into UNIT.  */
10124
10125 static dw_die_ref
10126 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10127 {
10128   dw_die_ref parent = die->die_parent;
10129   dw_die_ref new_parent = unit;
10130   dw_die_ref copy;
10131   void **slot = NULL;
10132   struct decl_table_entry *entry = NULL;
10133
10134   if (decl_table)
10135     {
10136       /* Check if the entry has already been copied to UNIT.  */
10137       slot = htab_find_slot_with_hash (decl_table, die,
10138                                        htab_hash_pointer (die), INSERT);
10139       if (*slot != HTAB_EMPTY_ENTRY)
10140         {
10141           entry = (struct decl_table_entry *) *slot;
10142           return entry->copy;
10143         }
10144
10145       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10146       entry = XCNEW (struct decl_table_entry);
10147       entry->orig = die;
10148       entry->copy = NULL;
10149       *slot = entry;
10150     }
10151
10152   if (parent != NULL)
10153     {
10154       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10155       if (spec != NULL)
10156         parent = spec;
10157       if (parent->die_tag != DW_TAG_compile_unit
10158           && parent->die_tag != DW_TAG_type_unit)
10159         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10160     }
10161
10162   copy = clone_as_declaration (die);
10163   add_child_die (new_parent, copy);
10164
10165   if (decl_table != NULL)
10166     {
10167       /* Record the pointer to the copy.  */
10168       entry->copy = copy;
10169     }
10170
10171   return copy;
10172 }
10173
10174 /* Walk the DIE and its children, looking for references to incomplete
10175    or trivial types that are unmarked (i.e., that are not in the current
10176    type_unit).  */
10177
10178 static void
10179 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10180 {
10181   dw_die_ref c;
10182   dw_attr_ref a;
10183   unsigned ix;
10184
10185   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10186     {
10187       if (AT_class (a) == dw_val_class_die_ref)
10188         {
10189           dw_die_ref targ = AT_ref (a);
10190           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10191           void **slot;
10192           struct decl_table_entry *entry;
10193
10194           if (targ->die_mark != 0 || type_node != NULL)
10195             continue;
10196
10197           slot = htab_find_slot_with_hash (decl_table, targ,
10198                                            htab_hash_pointer (targ), INSERT);
10199
10200           if (*slot != HTAB_EMPTY_ENTRY)
10201             {
10202               /* TARG has already been copied, so we just need to
10203                  modify the reference to point to the copy.  */
10204               entry = (struct decl_table_entry *) *slot;
10205               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10206             }
10207           else
10208             {
10209               dw_die_ref parent = unit;
10210               dw_die_ref copy = clone_tree (targ);
10211
10212               /* Make sure the cloned tree is marked as part of the
10213                  type unit.  */
10214               mark_dies (copy);
10215
10216               /* Record in DECL_TABLE that TARG has been copied.
10217                  Need to do this now, before the recursive call,
10218                  because DECL_TABLE may be expanded and SLOT
10219                  would no longer be a valid pointer.  */
10220               entry = XCNEW (struct decl_table_entry);
10221               entry->orig = targ;
10222               entry->copy = copy;
10223               *slot = entry;
10224
10225               /* If TARG has surrounding context, copy its ancestor tree
10226                  into the new type unit.  */
10227               if (targ->die_parent != NULL
10228                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10229                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10230                 parent = copy_ancestor_tree (unit, targ->die_parent,
10231                                              decl_table);
10232
10233               add_child_die (parent, copy);
10234               a->dw_attr_val.v.val_die_ref.die = copy;
10235
10236               /* Make sure the newly-copied DIE is walked.  If it was
10237                  installed in a previously-added context, it won't
10238                  get visited otherwise.  */
10239               if (parent != unit)
10240                 {
10241                   /* Find the highest point of the newly-added tree,
10242                      mark each node along the way, and walk from there.  */
10243                   parent->die_mark = 1;
10244                   while (parent->die_parent
10245                          && parent->die_parent->die_mark == 0)
10246                     {
10247                       parent = parent->die_parent;
10248                       parent->die_mark = 1;
10249                     }
10250                   copy_decls_walk (unit, parent, decl_table);
10251                 }
10252             }
10253         }
10254     }
10255
10256   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10257 }
10258
10259 /* Copy declarations for "unworthy" types into the new comdat section.
10260    Incomplete types, modified types, and certain other types aren't broken
10261    out into comdat sections of their own, so they don't have a signature,
10262    and we need to copy the declaration into the same section so that we
10263    don't have an external reference.  */
10264
10265 static void
10266 copy_decls_for_unworthy_types (dw_die_ref unit)
10267 {
10268   htab_t decl_table;
10269
10270   mark_dies (unit);
10271   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10272   copy_decls_walk (unit, unit, decl_table);
10273   htab_delete (decl_table);
10274   unmark_dies (unit);
10275 }
10276
10277 /* Traverse the DIE and add a sibling attribute if it may have the
10278    effect of speeding up access to siblings.  To save some space,
10279    avoid generating sibling attributes for DIE's without children.  */
10280
10281 static void
10282 add_sibling_attributes (dw_die_ref die)
10283 {
10284   dw_die_ref c;
10285
10286   if (! die->die_child)
10287     return;
10288
10289   if (die->die_parent && die != die->die_parent->die_child)
10290     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10291
10292   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10293 }
10294
10295 /* Output all location lists for the DIE and its children.  */
10296
10297 static void
10298 output_location_lists (dw_die_ref die)
10299 {
10300   dw_die_ref c;
10301   dw_attr_ref a;
10302   unsigned ix;
10303
10304   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10305     if (AT_class (a) == dw_val_class_loc_list)
10306       output_loc_list (AT_loc_list (a));
10307
10308   FOR_EACH_CHILD (die, c, output_location_lists (c));
10309 }
10310
10311 /* The format of each DIE (and its attribute value pairs) is encoded in an
10312    abbreviation table.  This routine builds the abbreviation table and assigns
10313    a unique abbreviation id for each abbreviation entry.  The children of each
10314    die are visited recursively.  */
10315
10316 static void
10317 build_abbrev_table (dw_die_ref die)
10318 {
10319   unsigned long abbrev_id;
10320   unsigned int n_alloc;
10321   dw_die_ref c;
10322   dw_attr_ref a;
10323   unsigned ix;
10324
10325   /* Scan the DIE references, and mark as external any that refer to
10326      DIEs from other CUs (i.e. those which are not marked).  */
10327   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10328     if (AT_class (a) == dw_val_class_die_ref
10329         && AT_ref (a)->die_mark == 0)
10330       {
10331         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10332         set_AT_ref_external (a, 1);
10333       }
10334
10335   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10336     {
10337       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10338       dw_attr_ref die_a, abbrev_a;
10339       unsigned ix;
10340       bool ok = true;
10341
10342       if (abbrev->die_tag != die->die_tag)
10343         continue;
10344       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10345         continue;
10346
10347       if (VEC_length (dw_attr_node, abbrev->die_attr)
10348           != VEC_length (dw_attr_node, die->die_attr))
10349         continue;
10350
10351       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10352         {
10353           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10354           if ((abbrev_a->dw_attr != die_a->dw_attr)
10355               || (value_format (abbrev_a) != value_format (die_a)))
10356             {
10357               ok = false;
10358               break;
10359             }
10360         }
10361       if (ok)
10362         break;
10363     }
10364
10365   if (abbrev_id >= abbrev_die_table_in_use)
10366     {
10367       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10368         {
10369           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10370           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10371                                             n_alloc);
10372
10373           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10374                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10375           abbrev_die_table_allocated = n_alloc;
10376         }
10377
10378       ++abbrev_die_table_in_use;
10379       abbrev_die_table[abbrev_id] = die;
10380     }
10381
10382   die->die_abbrev = abbrev_id;
10383   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10384 }
10385 \f
10386 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10387
10388 static int
10389 constant_size (unsigned HOST_WIDE_INT value)
10390 {
10391   int log;
10392
10393   if (value == 0)
10394     log = 0;
10395   else
10396     log = floor_log2 (value);
10397
10398   log = log / 8;
10399   log = 1 << (floor_log2 (log) + 1);
10400
10401   return log;
10402 }
10403
10404 /* Return the size of a DIE as it is represented in the
10405    .debug_info section.  */
10406
10407 static unsigned long
10408 size_of_die (dw_die_ref die)
10409 {
10410   unsigned long size = 0;
10411   dw_attr_ref a;
10412   unsigned ix;
10413
10414   size += size_of_uleb128 (die->die_abbrev);
10415   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10416     {
10417       switch (AT_class (a))
10418         {
10419         case dw_val_class_addr:
10420           size += DWARF2_ADDR_SIZE;
10421           break;
10422         case dw_val_class_offset:
10423           size += DWARF_OFFSET_SIZE;
10424           break;
10425         case dw_val_class_loc:
10426           {
10427             unsigned long lsize = size_of_locs (AT_loc (a));
10428
10429             /* Block length.  */
10430             if (dwarf_version >= 4)
10431               size += size_of_uleb128 (lsize);
10432             else
10433               size += constant_size (lsize);
10434             size += lsize;
10435           }
10436           break;
10437         case dw_val_class_loc_list:
10438           size += DWARF_OFFSET_SIZE;
10439           break;
10440         case dw_val_class_range_list:
10441           size += DWARF_OFFSET_SIZE;
10442           break;
10443         case dw_val_class_const:
10444           size += size_of_sleb128 (AT_int (a));
10445           break;
10446         case dw_val_class_unsigned_const:
10447           size += constant_size (AT_unsigned (a));
10448           break;
10449         case dw_val_class_const_double:
10450           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10451           if (HOST_BITS_PER_WIDE_INT >= 64)
10452             size++; /* block */
10453           break;
10454         case dw_val_class_vec:
10455           size += constant_size (a->dw_attr_val.v.val_vec.length
10456                                  * a->dw_attr_val.v.val_vec.elt_size)
10457                   + a->dw_attr_val.v.val_vec.length
10458                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10459           break;
10460         case dw_val_class_flag:
10461           if (dwarf_version >= 4)
10462             /* Currently all add_AT_flag calls pass in 1 as last argument,
10463                so DW_FORM_flag_present can be used.  If that ever changes,
10464                we'll need to use DW_FORM_flag and have some optimization
10465                in build_abbrev_table that will change those to
10466                DW_FORM_flag_present if it is set to 1 in all DIEs using
10467                the same abbrev entry.  */
10468             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10469           else
10470             size += 1;
10471           break;
10472         case dw_val_class_die_ref:
10473           if (AT_ref_external (a))
10474             {
10475               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10476                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10477                  is sized by target address length, whereas in DWARF3
10478                  it's always sized as an offset.  */
10479               if (dwarf_version >= 4)
10480                 size += DWARF_TYPE_SIGNATURE_SIZE;
10481               else if (dwarf_version == 2)
10482                 size += DWARF2_ADDR_SIZE;
10483               else
10484                 size += DWARF_OFFSET_SIZE;
10485             }
10486           else
10487             size += DWARF_OFFSET_SIZE;
10488           break;
10489         case dw_val_class_fde_ref:
10490           size += DWARF_OFFSET_SIZE;
10491           break;
10492         case dw_val_class_lbl_id:
10493           size += DWARF2_ADDR_SIZE;
10494           break;
10495         case dw_val_class_lineptr:
10496         case dw_val_class_macptr:
10497           size += DWARF_OFFSET_SIZE;
10498           break;
10499         case dw_val_class_str:
10500           if (AT_string_form (a) == DW_FORM_strp)
10501             size += DWARF_OFFSET_SIZE;
10502           else
10503             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10504           break;
10505         case dw_val_class_file:
10506           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10507           break;
10508         case dw_val_class_data8:
10509           size += 8;
10510           break;
10511         case dw_val_class_vms_delta:
10512           size += DWARF_OFFSET_SIZE;
10513           break;
10514         default:
10515           gcc_unreachable ();
10516         }
10517     }
10518
10519   return size;
10520 }
10521
10522 /* Size the debugging information associated with a given DIE.  Visits the
10523    DIE's children recursively.  Updates the global variable next_die_offset, on
10524    each time through.  Uses the current value of next_die_offset to update the
10525    die_offset field in each DIE.  */
10526
10527 static void
10528 calc_die_sizes (dw_die_ref die)
10529 {
10530   dw_die_ref c;
10531
10532   die->die_offset = next_die_offset;
10533   next_die_offset += size_of_die (die);
10534
10535   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10536
10537   if (die->die_child != NULL)
10538     /* Count the null byte used to terminate sibling lists.  */
10539     next_die_offset += 1;
10540 }
10541
10542 /* Set the marks for a die and its children.  We do this so
10543    that we know whether or not a reference needs to use FORM_ref_addr; only
10544    DIEs in the same CU will be marked.  We used to clear out the offset
10545    and use that as the flag, but ran into ordering problems.  */
10546
10547 static void
10548 mark_dies (dw_die_ref die)
10549 {
10550   dw_die_ref c;
10551
10552   gcc_assert (!die->die_mark);
10553
10554   die->die_mark = 1;
10555   FOR_EACH_CHILD (die, c, mark_dies (c));
10556 }
10557
10558 /* Clear the marks for a die and its children.  */
10559
10560 static void
10561 unmark_dies (dw_die_ref die)
10562 {
10563   dw_die_ref c;
10564
10565   if (dwarf_version < 4)
10566     gcc_assert (die->die_mark);
10567
10568   die->die_mark = 0;
10569   FOR_EACH_CHILD (die, c, unmark_dies (c));
10570 }
10571
10572 /* Clear the marks for a die, its children and referred dies.  */
10573
10574 static void
10575 unmark_all_dies (dw_die_ref die)
10576 {
10577   dw_die_ref c;
10578   dw_attr_ref a;
10579   unsigned ix;
10580
10581   if (!die->die_mark)
10582     return;
10583   die->die_mark = 0;
10584
10585   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10586
10587   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10588     if (AT_class (a) == dw_val_class_die_ref)
10589       unmark_all_dies (AT_ref (a));
10590 }
10591
10592 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10593    generated for the compilation unit.  */
10594
10595 static unsigned long
10596 size_of_pubnames (VEC (pubname_entry, gc) * names)
10597 {
10598   unsigned long size;
10599   unsigned i;
10600   pubname_ref p;
10601
10602   size = DWARF_PUBNAMES_HEADER_SIZE;
10603   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10604     if (names != pubtype_table
10605         || p->die->die_offset != 0
10606         || !flag_eliminate_unused_debug_types)
10607       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10608
10609   size += DWARF_OFFSET_SIZE;
10610   return size;
10611 }
10612
10613 /* Return the size of the information in the .debug_aranges section.  */
10614
10615 static unsigned long
10616 size_of_aranges (void)
10617 {
10618   unsigned long size;
10619
10620   size = DWARF_ARANGES_HEADER_SIZE;
10621
10622   /* Count the address/length pair for this compilation unit.  */
10623   if (text_section_used)
10624     size += 2 * DWARF2_ADDR_SIZE;
10625   if (cold_text_section_used)
10626     size += 2 * DWARF2_ADDR_SIZE;
10627   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10628
10629   /* Count the two zero words used to terminated the address range table.  */
10630   size += 2 * DWARF2_ADDR_SIZE;
10631   return size;
10632 }
10633 \f
10634 /* Select the encoding of an attribute value.  */
10635
10636 static enum dwarf_form
10637 value_format (dw_attr_ref a)
10638 {
10639   switch (a->dw_attr_val.val_class)
10640     {
10641     case dw_val_class_addr:
10642       /* Only very few attributes allow DW_FORM_addr.  */
10643       switch (a->dw_attr)
10644         {
10645         case DW_AT_low_pc:
10646         case DW_AT_high_pc:
10647         case DW_AT_entry_pc:
10648         case DW_AT_trampoline:
10649           return DW_FORM_addr;
10650         default:
10651           break;
10652         }
10653       switch (DWARF2_ADDR_SIZE)
10654         {
10655         case 1:
10656           return DW_FORM_data1;
10657         case 2:
10658           return DW_FORM_data2;
10659         case 4:
10660           return DW_FORM_data4;
10661         case 8:
10662           return DW_FORM_data8;
10663         default:
10664           gcc_unreachable ();
10665         }
10666     case dw_val_class_range_list:
10667     case dw_val_class_loc_list:
10668       if (dwarf_version >= 4)
10669         return DW_FORM_sec_offset;
10670       /* FALLTHRU */
10671     case dw_val_class_vms_delta:
10672     case dw_val_class_offset:
10673       switch (DWARF_OFFSET_SIZE)
10674         {
10675         case 4:
10676           return DW_FORM_data4;
10677         case 8:
10678           return DW_FORM_data8;
10679         default:
10680           gcc_unreachable ();
10681         }
10682     case dw_val_class_loc:
10683       if (dwarf_version >= 4)
10684         return DW_FORM_exprloc;
10685       switch (constant_size (size_of_locs (AT_loc (a))))
10686         {
10687         case 1:
10688           return DW_FORM_block1;
10689         case 2:
10690           return DW_FORM_block2;
10691         default:
10692           gcc_unreachable ();
10693         }
10694     case dw_val_class_const:
10695       return DW_FORM_sdata;
10696     case dw_val_class_unsigned_const:
10697       switch (constant_size (AT_unsigned (a)))
10698         {
10699         case 1:
10700           return DW_FORM_data1;
10701         case 2:
10702           return DW_FORM_data2;
10703         case 4:
10704           return DW_FORM_data4;
10705         case 8:
10706           return DW_FORM_data8;
10707         default:
10708           gcc_unreachable ();
10709         }
10710     case dw_val_class_const_double:
10711       switch (HOST_BITS_PER_WIDE_INT)
10712         {
10713         case 8:
10714           return DW_FORM_data2;
10715         case 16:
10716           return DW_FORM_data4;
10717         case 32:
10718           return DW_FORM_data8;
10719         case 64:
10720         default:
10721           return DW_FORM_block1;
10722         }
10723     case dw_val_class_vec:
10724       switch (constant_size (a->dw_attr_val.v.val_vec.length
10725                              * a->dw_attr_val.v.val_vec.elt_size))
10726         {
10727         case 1:
10728           return DW_FORM_block1;
10729         case 2:
10730           return DW_FORM_block2;
10731         case 4:
10732           return DW_FORM_block4;
10733         default:
10734           gcc_unreachable ();
10735         }
10736     case dw_val_class_flag:
10737       if (dwarf_version >= 4)
10738         {
10739           /* Currently all add_AT_flag calls pass in 1 as last argument,
10740              so DW_FORM_flag_present can be used.  If that ever changes,
10741              we'll need to use DW_FORM_flag and have some optimization
10742              in build_abbrev_table that will change those to
10743              DW_FORM_flag_present if it is set to 1 in all DIEs using
10744              the same abbrev entry.  */
10745           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10746           return DW_FORM_flag_present;
10747         }
10748       return DW_FORM_flag;
10749     case dw_val_class_die_ref:
10750       if (AT_ref_external (a))
10751         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10752       else
10753         return DW_FORM_ref;
10754     case dw_val_class_fde_ref:
10755       return DW_FORM_data;
10756     case dw_val_class_lbl_id:
10757       return DW_FORM_addr;
10758     case dw_val_class_lineptr:
10759     case dw_val_class_macptr:
10760       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10761     case dw_val_class_str:
10762       return AT_string_form (a);
10763     case dw_val_class_file:
10764       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10765         {
10766         case 1:
10767           return DW_FORM_data1;
10768         case 2:
10769           return DW_FORM_data2;
10770         case 4:
10771           return DW_FORM_data4;
10772         default:
10773           gcc_unreachable ();
10774         }
10775
10776     case dw_val_class_data8:
10777       return DW_FORM_data8;
10778
10779     default:
10780       gcc_unreachable ();
10781     }
10782 }
10783
10784 /* Output the encoding of an attribute value.  */
10785
10786 static void
10787 output_value_format (dw_attr_ref a)
10788 {
10789   enum dwarf_form form = value_format (a);
10790
10791   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10792 }
10793
10794 /* Output the .debug_abbrev section which defines the DIE abbreviation
10795    table.  */
10796
10797 static void
10798 output_abbrev_section (void)
10799 {
10800   unsigned long abbrev_id;
10801
10802   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10803     {
10804       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10805       unsigned ix;
10806       dw_attr_ref a_attr;
10807
10808       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10809       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10810                                    dwarf_tag_name (abbrev->die_tag));
10811
10812       if (abbrev->die_child != NULL)
10813         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10814       else
10815         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10816
10817       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10818            ix++)
10819         {
10820           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10821                                        dwarf_attr_name (a_attr->dw_attr));
10822           output_value_format (a_attr);
10823         }
10824
10825       dw2_asm_output_data (1, 0, NULL);
10826       dw2_asm_output_data (1, 0, NULL);
10827     }
10828
10829   /* Terminate the table.  */
10830   dw2_asm_output_data (1, 0, NULL);
10831 }
10832
10833 /* Output a symbol we can use to refer to this DIE from another CU.  */
10834
10835 static inline void
10836 output_die_symbol (dw_die_ref die)
10837 {
10838   char *sym = die->die_id.die_symbol;
10839
10840   if (sym == 0)
10841     return;
10842
10843   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10844     /* We make these global, not weak; if the target doesn't support
10845        .linkonce, it doesn't support combining the sections, so debugging
10846        will break.  */
10847     targetm.asm_out.globalize_label (asm_out_file, sym);
10848
10849   ASM_OUTPUT_LABEL (asm_out_file, sym);
10850 }
10851
10852 /* Return a new location list, given the begin and end range, and the
10853    expression.  */
10854
10855 static inline dw_loc_list_ref
10856 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10857               const char *section)
10858 {
10859   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10860
10861   retlist->begin = begin;
10862   retlist->end = end;
10863   retlist->expr = expr;
10864   retlist->section = section;
10865
10866   return retlist;
10867 }
10868
10869 /* Generate a new internal symbol for this location list node, if it
10870    hasn't got one yet.  */
10871
10872 static inline void
10873 gen_llsym (dw_loc_list_ref list)
10874 {
10875   gcc_assert (!list->ll_symbol);
10876   list->ll_symbol = gen_internal_sym ("LLST");
10877 }
10878
10879 /* Output the location list given to us.  */
10880
10881 static void
10882 output_loc_list (dw_loc_list_ref list_head)
10883 {
10884   dw_loc_list_ref curr = list_head;
10885
10886   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10887
10888   /* Walk the location list, and output each range + expression.  */
10889   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10890     {
10891       unsigned long size;
10892       /* Don't output an entry that starts and ends at the same address.  */
10893       if (strcmp (curr->begin, curr->end) == 0)
10894         continue;
10895       if (!have_multiple_function_sections)
10896         {
10897           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10898                                 "Location list begin address (%s)",
10899                                 list_head->ll_symbol);
10900           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10901                                 "Location list end address (%s)",
10902                                 list_head->ll_symbol);
10903         }
10904       else
10905         {
10906           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10907                                "Location list begin address (%s)",
10908                                list_head->ll_symbol);
10909           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10910                                "Location list end address (%s)",
10911                                list_head->ll_symbol);
10912         }
10913       size = size_of_locs (curr->expr);
10914
10915       /* Output the block length for this list of location operations.  */
10916       gcc_assert (size <= 0xffff);
10917       dw2_asm_output_data (2, size, "%s", "Location expression size");
10918
10919       output_loc_sequence (curr->expr);
10920     }
10921
10922   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10923                        "Location list terminator begin (%s)",
10924                        list_head->ll_symbol);
10925   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10926                        "Location list terminator end (%s)",
10927                        list_head->ll_symbol);
10928 }
10929
10930 /* Output a type signature.  */
10931
10932 static inline void
10933 output_signature (const char *sig, const char *name)
10934 {
10935   int i;
10936
10937   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10938     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10939 }
10940
10941 /* Output the DIE and its attributes.  Called recursively to generate
10942    the definitions of each child DIE.  */
10943
10944 static void
10945 output_die (dw_die_ref die)
10946 {
10947   dw_attr_ref a;
10948   dw_die_ref c;
10949   unsigned long size;
10950   unsigned ix;
10951
10952   /* If someone in another CU might refer to us, set up a symbol for
10953      them to point to.  */
10954   if (dwarf_version < 4 && die->die_id.die_symbol)
10955     output_die_symbol (die);
10956
10957   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10958                                (unsigned long)die->die_offset,
10959                                dwarf_tag_name (die->die_tag));
10960
10961   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10962     {
10963       const char *name = dwarf_attr_name (a->dw_attr);
10964
10965       switch (AT_class (a))
10966         {
10967         case dw_val_class_addr:
10968           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10969           break;
10970
10971         case dw_val_class_offset:
10972           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10973                                "%s", name);
10974           break;
10975
10976         case dw_val_class_range_list:
10977           {
10978             char *p = strchr (ranges_section_label, '\0');
10979
10980             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10981                      a->dw_attr_val.v.val_offset);
10982             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10983                                    debug_ranges_section, "%s", name);
10984             *p = '\0';
10985           }
10986           break;
10987
10988         case dw_val_class_loc:
10989           size = size_of_locs (AT_loc (a));
10990
10991           /* Output the block length for this list of location operations.  */
10992           if (dwarf_version >= 4)
10993             dw2_asm_output_data_uleb128 (size, "%s", name);
10994           else
10995             dw2_asm_output_data (constant_size (size), size, "%s", name);
10996
10997           output_loc_sequence (AT_loc (a));
10998           break;
10999
11000         case dw_val_class_const:
11001           /* ??? It would be slightly more efficient to use a scheme like is
11002              used for unsigned constants below, but gdb 4.x does not sign
11003              extend.  Gdb 5.x does sign extend.  */
11004           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11005           break;
11006
11007         case dw_val_class_unsigned_const:
11008           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11009                                AT_unsigned (a), "%s", name);
11010           break;
11011
11012         case dw_val_class_const_double:
11013           {
11014             unsigned HOST_WIDE_INT first, second;
11015
11016             if (HOST_BITS_PER_WIDE_INT >= 64)
11017               dw2_asm_output_data (1,
11018                                    2 * HOST_BITS_PER_WIDE_INT
11019                                    / HOST_BITS_PER_CHAR,
11020                                    NULL);
11021
11022             if (WORDS_BIG_ENDIAN)
11023               {
11024                 first = a->dw_attr_val.v.val_double.high;
11025                 second = a->dw_attr_val.v.val_double.low;
11026               }
11027             else
11028               {
11029                 first = a->dw_attr_val.v.val_double.low;
11030                 second = a->dw_attr_val.v.val_double.high;
11031               }
11032
11033             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11034                                  first, name);
11035             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11036                                  second, NULL);
11037           }
11038           break;
11039
11040         case dw_val_class_vec:
11041           {
11042             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11043             unsigned int len = a->dw_attr_val.v.val_vec.length;
11044             unsigned int i;
11045             unsigned char *p;
11046
11047             dw2_asm_output_data (constant_size (len * elt_size),
11048                                  len * elt_size, "%s", name);
11049             if (elt_size > sizeof (HOST_WIDE_INT))
11050               {
11051                 elt_size /= 2;
11052                 len *= 2;
11053               }
11054             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11055                  i < len;
11056                  i++, p += elt_size)
11057               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11058                                    "fp or vector constant word %u", i);
11059             break;
11060           }
11061
11062         case dw_val_class_flag:
11063           if (dwarf_version >= 4)
11064             {
11065               /* Currently all add_AT_flag calls pass in 1 as last argument,
11066                  so DW_FORM_flag_present can be used.  If that ever changes,
11067                  we'll need to use DW_FORM_flag and have some optimization
11068                  in build_abbrev_table that will change those to
11069                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11070                  the same abbrev entry.  */
11071               gcc_assert (AT_flag (a) == 1);
11072               if (flag_debug_asm)
11073                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11074                          ASM_COMMENT_START, name);
11075               break;
11076             }
11077           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11078           break;
11079
11080         case dw_val_class_loc_list:
11081           {
11082             char *sym = AT_loc_list (a)->ll_symbol;
11083
11084             gcc_assert (sym);
11085             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11086                                    "%s", name);
11087           }
11088           break;
11089
11090         case dw_val_class_die_ref:
11091           if (AT_ref_external (a))
11092             {
11093               if (dwarf_version >= 4)
11094                 {
11095                   comdat_type_node_ref type_node =
11096                     AT_ref (a)->die_id.die_type_node;
11097
11098                   gcc_assert (type_node);
11099                   output_signature (type_node->signature, name);
11100                 }
11101               else
11102                 {
11103                   char *sym = AT_ref (a)->die_id.die_symbol;
11104                   int size;
11105
11106                   gcc_assert (sym);
11107                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11108                      length, whereas in DWARF3 it's always sized as an
11109                      offset.  */
11110                   if (dwarf_version == 2)
11111                     size = DWARF2_ADDR_SIZE;
11112                   else
11113                     size = DWARF_OFFSET_SIZE;
11114                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11115                                          name);
11116                 }
11117             }
11118           else
11119             {
11120               gcc_assert (AT_ref (a)->die_offset);
11121               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11122                                    "%s", name);
11123             }
11124           break;
11125
11126         case dw_val_class_fde_ref:
11127           {
11128             char l1[20];
11129
11130             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11131                                          a->dw_attr_val.v.val_fde_index * 2);
11132             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11133                                    "%s", name);
11134           }
11135           break;
11136
11137         case dw_val_class_vms_delta:
11138           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11139                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11140                                     "%s", name);
11141           break;
11142
11143         case dw_val_class_lbl_id:
11144           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11145           break;
11146
11147         case dw_val_class_lineptr:
11148           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11149                                  debug_line_section, "%s", name);
11150           break;
11151
11152         case dw_val_class_macptr:
11153           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11154                                  debug_macinfo_section, "%s", name);
11155           break;
11156
11157         case dw_val_class_str:
11158           if (AT_string_form (a) == DW_FORM_strp)
11159             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11160                                    a->dw_attr_val.v.val_str->label,
11161                                    debug_str_section,
11162                                    "%s: \"%s\"", name, AT_string (a));
11163           else
11164             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11165           break;
11166
11167         case dw_val_class_file:
11168           {
11169             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11170
11171             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11172                                  a->dw_attr_val.v.val_file->filename);
11173             break;
11174           }
11175
11176         case dw_val_class_data8:
11177           {
11178             int i;
11179
11180             for (i = 0; i < 8; i++)
11181               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11182                                    i == 0 ? "%s" : NULL, name);
11183             break;
11184           }
11185
11186         default:
11187           gcc_unreachable ();
11188         }
11189     }
11190
11191   FOR_EACH_CHILD (die, c, output_die (c));
11192
11193   /* Add null byte to terminate sibling list.  */
11194   if (die->die_child != NULL)
11195     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11196                          (unsigned long) die->die_offset);
11197 }
11198
11199 /* Output the compilation unit that appears at the beginning of the
11200    .debug_info section, and precedes the DIE descriptions.  */
11201
11202 static void
11203 output_compilation_unit_header (void)
11204 {
11205   int ver = dwarf_version;
11206
11207   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11208     dw2_asm_output_data (4, 0xffffffff,
11209       "Initial length escape value indicating 64-bit DWARF extension");
11210   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11211                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11212                        "Length of Compilation Unit Info");
11213   dw2_asm_output_data (2, ver, "DWARF version number");
11214   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11215                          debug_abbrev_section,
11216                          "Offset Into Abbrev. Section");
11217   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11218 }
11219
11220 /* Output the compilation unit DIE and its children.  */
11221
11222 static void
11223 output_comp_unit (dw_die_ref die, int output_if_empty)
11224 {
11225   const char *secname;
11226   char *oldsym, *tmp;
11227
11228   /* Unless we are outputting main CU, we may throw away empty ones.  */
11229   if (!output_if_empty && die->die_child == NULL)
11230     return;
11231
11232   /* Even if there are no children of this DIE, we must output the information
11233      about the compilation unit.  Otherwise, on an empty translation unit, we
11234      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11235      will then complain when examining the file.  First mark all the DIEs in
11236      this CU so we know which get local refs.  */
11237   mark_dies (die);
11238
11239   build_abbrev_table (die);
11240
11241   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11242   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11243   calc_die_sizes (die);
11244
11245   oldsym = die->die_id.die_symbol;
11246   if (oldsym)
11247     {
11248       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11249
11250       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11251       secname = tmp;
11252       die->die_id.die_symbol = NULL;
11253       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11254     }
11255   else
11256     switch_to_section (debug_info_section);
11257
11258   /* Output debugging information.  */
11259   output_compilation_unit_header ();
11260   output_die (die);
11261
11262   /* Leave the marks on the main CU, so we can check them in
11263      output_pubnames.  */
11264   if (oldsym)
11265     {
11266       unmark_dies (die);
11267       die->die_id.die_symbol = oldsym;
11268     }
11269 }
11270
11271 /* Output a comdat type unit DIE and its children.  */
11272
11273 static void
11274 output_comdat_type_unit (comdat_type_node *node)
11275 {
11276   const char *secname;
11277   char *tmp;
11278   int i;
11279 #if defined (OBJECT_FORMAT_ELF)
11280   tree comdat_key;
11281 #endif
11282
11283   /* First mark all the DIEs in this CU so we know which get local refs.  */
11284   mark_dies (node->root_die);
11285
11286   build_abbrev_table (node->root_die);
11287
11288   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11289   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11290   calc_die_sizes (node->root_die);
11291
11292 #if defined (OBJECT_FORMAT_ELF)
11293   secname = ".debug_types";
11294   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11295   sprintf (tmp, "wt.");
11296   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11297     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11298   comdat_key = get_identifier (tmp);
11299   targetm.asm_out.named_section (secname,
11300                                  SECTION_DEBUG | SECTION_LINKONCE,
11301                                  comdat_key);
11302 #else
11303   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11304   sprintf (tmp, ".gnu.linkonce.wt.");
11305   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11306     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11307   secname = tmp;
11308   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11309 #endif
11310
11311   /* Output debugging information.  */
11312   output_compilation_unit_header ();
11313   output_signature (node->signature, "Type Signature");
11314   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11315                        "Offset to Type DIE");
11316   output_die (node->root_die);
11317
11318   unmark_dies (node->root_die);
11319 }
11320
11321 /* Return the DWARF2/3 pubname associated with a decl.  */
11322
11323 static const char *
11324 dwarf2_name (tree decl, int scope)
11325 {
11326   if (DECL_NAMELESS (decl))
11327     return NULL;
11328   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11329 }
11330
11331 /* Add a new entry to .debug_pubnames if appropriate.  */
11332
11333 static void
11334 add_pubname_string (const char *str, dw_die_ref die)
11335 {
11336   if (targetm.want_debug_pub_sections)
11337     {
11338       pubname_entry e;
11339
11340       e.die = die;
11341       e.name = xstrdup (str);
11342       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11343     }
11344 }
11345
11346 static void
11347 add_pubname (tree decl, dw_die_ref die)
11348 {
11349   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11350     {
11351       const char *name = dwarf2_name (decl, 1);
11352       if (name)
11353         add_pubname_string (name, die);
11354     }
11355 }
11356
11357 /* Add a new entry to .debug_pubtypes if appropriate.  */
11358
11359 static void
11360 add_pubtype (tree decl, dw_die_ref die)
11361 {
11362   pubname_entry e;
11363
11364   if (!targetm.want_debug_pub_sections)
11365     return;
11366
11367   e.name = NULL;
11368   if ((TREE_PUBLIC (decl)
11369        || is_cu_die (die->die_parent))
11370       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11371     {
11372       e.die = die;
11373       if (TYPE_P (decl))
11374         {
11375           if (TYPE_NAME (decl))
11376             {
11377               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11378                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11379               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11380                        && DECL_NAME (TYPE_NAME (decl)))
11381                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11382               else
11383                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11384             }
11385         }
11386       else
11387         {
11388           e.name = dwarf2_name (decl, 1);
11389           if (e.name)
11390             e.name = xstrdup (e.name);
11391         }
11392
11393       /* If we don't have a name for the type, there's no point in adding
11394          it to the table.  */
11395       if (e.name && e.name[0] != '\0')
11396         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11397     }
11398 }
11399
11400 /* Output the public names table used to speed up access to externally
11401    visible names; or the public types table used to find type definitions.  */
11402
11403 static void
11404 output_pubnames (VEC (pubname_entry, gc) * names)
11405 {
11406   unsigned i;
11407   unsigned long pubnames_length = size_of_pubnames (names);
11408   pubname_ref pub;
11409
11410   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11411     dw2_asm_output_data (4, 0xffffffff,
11412       "Initial length escape value indicating 64-bit DWARF extension");
11413   if (names == pubname_table)
11414     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11415                          "Length of Public Names Info");
11416   else
11417     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11418                          "Length of Public Type Names Info");
11419   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11420   dw2_asm_output_data (2, 2, "DWARF Version");
11421   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11422                          debug_info_section,
11423                          "Offset of Compilation Unit Info");
11424   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11425                        "Compilation Unit Length");
11426
11427   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11428     {
11429       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11430       if (names == pubname_table)
11431         gcc_assert (pub->die->die_mark);
11432
11433       if (names != pubtype_table
11434           || pub->die->die_offset != 0
11435           || !flag_eliminate_unused_debug_types)
11436         {
11437           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11438                                "DIE offset");
11439
11440           dw2_asm_output_nstring (pub->name, -1, "external name");
11441         }
11442     }
11443
11444   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11445 }
11446
11447 /* Add a new entry to .debug_aranges if appropriate.  */
11448
11449 static void
11450 add_arange (tree decl, dw_die_ref die)
11451 {
11452   if (! DECL_SECTION_NAME (decl))
11453     return;
11454
11455   if (arange_table_in_use == arange_table_allocated)
11456     {
11457       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11458       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11459                                     arange_table_allocated);
11460       memset (arange_table + arange_table_in_use, 0,
11461               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11462     }
11463
11464   arange_table[arange_table_in_use++] = die;
11465 }
11466
11467 /* Output the information that goes into the .debug_aranges table.
11468    Namely, define the beginning and ending address range of the
11469    text section generated for this compilation unit.  */
11470
11471 static void
11472 output_aranges (void)
11473 {
11474   unsigned i;
11475   unsigned long aranges_length = size_of_aranges ();
11476
11477   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11478     dw2_asm_output_data (4, 0xffffffff,
11479       "Initial length escape value indicating 64-bit DWARF extension");
11480   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11481                        "Length of Address Ranges Info");
11482   /* Version number for aranges is still 2, even in DWARF3.  */
11483   dw2_asm_output_data (2, 2, "DWARF Version");
11484   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11485                          debug_info_section,
11486                          "Offset of Compilation Unit Info");
11487   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11488   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11489
11490   /* We need to align to twice the pointer size here.  */
11491   if (DWARF_ARANGES_PAD_SIZE)
11492     {
11493       /* Pad using a 2 byte words so that padding is correct for any
11494          pointer size.  */
11495       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11496                            2 * DWARF2_ADDR_SIZE);
11497       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11498         dw2_asm_output_data (2, 0, NULL);
11499     }
11500
11501   /* It is necessary not to output these entries if the sections were
11502      not used; if the sections were not used, the length will be 0 and
11503      the address may end up as 0 if the section is discarded by ld
11504      --gc-sections, leaving an invalid (0, 0) entry that can be
11505      confused with the terminator.  */
11506   if (text_section_used)
11507     {
11508       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11509       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11510                             text_section_label, "Length");
11511     }
11512   if (cold_text_section_used)
11513     {
11514       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11515                            "Address");
11516       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11517                             cold_text_section_label, "Length");
11518     }
11519
11520   for (i = 0; i < arange_table_in_use; i++)
11521     {
11522       dw_die_ref die = arange_table[i];
11523
11524       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11525       gcc_assert (die->die_mark);
11526
11527       if (die->die_tag == DW_TAG_subprogram)
11528         {
11529           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11530                                "Address");
11531           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11532                                 get_AT_low_pc (die), "Length");
11533         }
11534       else
11535         {
11536           /* A static variable; extract the symbol from DW_AT_location.
11537              Note that this code isn't currently hit, as we only emit
11538              aranges for functions (jason 9/23/99).  */
11539           dw_attr_ref a = get_AT (die, DW_AT_location);
11540           dw_loc_descr_ref loc;
11541
11542           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11543
11544           loc = AT_loc (a);
11545           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11546
11547           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11548                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11549           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11550                                get_AT_unsigned (die, DW_AT_byte_size),
11551                                "Length");
11552         }
11553     }
11554
11555   /* Output the terminator words.  */
11556   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11557   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11558 }
11559
11560 /* Add a new entry to .debug_ranges.  Return the offset at which it
11561    was placed.  */
11562
11563 static unsigned int
11564 add_ranges_num (int num)
11565 {
11566   unsigned int in_use = ranges_table_in_use;
11567
11568   if (in_use == ranges_table_allocated)
11569     {
11570       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11571       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11572                                     ranges_table_allocated);
11573       memset (ranges_table + ranges_table_in_use, 0,
11574               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11575     }
11576
11577   ranges_table[in_use].num = num;
11578   ranges_table_in_use = in_use + 1;
11579
11580   return in_use * 2 * DWARF2_ADDR_SIZE;
11581 }
11582
11583 /* Add a new entry to .debug_ranges corresponding to a block, or a
11584    range terminator if BLOCK is NULL.  */
11585
11586 static unsigned int
11587 add_ranges (const_tree block)
11588 {
11589   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11590 }
11591
11592 /* Add a new entry to .debug_ranges corresponding to a pair of
11593    labels.  */
11594
11595 static void
11596 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11597                       bool *added)
11598 {
11599   unsigned int in_use = ranges_by_label_in_use;
11600   unsigned int offset;
11601
11602   if (in_use == ranges_by_label_allocated)
11603     {
11604       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11605       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11606                                        ranges_by_label,
11607                                        ranges_by_label_allocated);
11608       memset (ranges_by_label + ranges_by_label_in_use, 0,
11609               RANGES_TABLE_INCREMENT
11610               * sizeof (struct dw_ranges_by_label_struct));
11611     }
11612
11613   ranges_by_label[in_use].begin = begin;
11614   ranges_by_label[in_use].end = end;
11615   ranges_by_label_in_use = in_use + 1;
11616
11617   offset = add_ranges_num (-(int)in_use - 1);
11618   if (!*added)
11619     {
11620       add_AT_range_list (die, DW_AT_ranges, offset);
11621       *added = true;
11622     }
11623 }
11624
11625 static void
11626 output_ranges (void)
11627 {
11628   unsigned i;
11629   static const char *const start_fmt = "Offset %#x";
11630   const char *fmt = start_fmt;
11631
11632   for (i = 0; i < ranges_table_in_use; i++)
11633     {
11634       int block_num = ranges_table[i].num;
11635
11636       if (block_num > 0)
11637         {
11638           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11639           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11640
11641           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11642           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11643
11644           /* If all code is in the text section, then the compilation
11645              unit base address defaults to DW_AT_low_pc, which is the
11646              base of the text section.  */
11647           if (!have_multiple_function_sections)
11648             {
11649               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11650                                     text_section_label,
11651                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11652               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11653                                     text_section_label, NULL);
11654             }
11655
11656           /* Otherwise, the compilation unit base address is zero,
11657              which allows us to use absolute addresses, and not worry
11658              about whether the target supports cross-section
11659              arithmetic.  */
11660           else
11661             {
11662               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11663                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11664               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11665             }
11666
11667           fmt = NULL;
11668         }
11669
11670       /* Negative block_num stands for an index into ranges_by_label.  */
11671       else if (block_num < 0)
11672         {
11673           int lab_idx = - block_num - 1;
11674
11675           if (!have_multiple_function_sections)
11676             {
11677               gcc_unreachable ();
11678 #if 0
11679               /* If we ever use add_ranges_by_labels () for a single
11680                  function section, all we have to do is to take out
11681                  the #if 0 above.  */
11682               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11683                                     ranges_by_label[lab_idx].begin,
11684                                     text_section_label,
11685                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11686               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11687                                     ranges_by_label[lab_idx].end,
11688                                     text_section_label, NULL);
11689 #endif
11690             }
11691           else
11692             {
11693               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11694                                    ranges_by_label[lab_idx].begin,
11695                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11696               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11697                                    ranges_by_label[lab_idx].end,
11698                                    NULL);
11699             }
11700         }
11701       else
11702         {
11703           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11704           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11705           fmt = start_fmt;
11706         }
11707     }
11708 }
11709
11710 /* Data structure containing information about input files.  */
11711 struct file_info
11712 {
11713   const char *path;     /* Complete file name.  */
11714   const char *fname;    /* File name part.  */
11715   int length;           /* Length of entire string.  */
11716   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11717   int dir_idx;          /* Index in directory table.  */
11718 };
11719
11720 /* Data structure containing information about directories with source
11721    files.  */
11722 struct dir_info
11723 {
11724   const char *path;     /* Path including directory name.  */
11725   int length;           /* Path length.  */
11726   int prefix;           /* Index of directory entry which is a prefix.  */
11727   int count;            /* Number of files in this directory.  */
11728   int dir_idx;          /* Index of directory used as base.  */
11729 };
11730
11731 /* Callback function for file_info comparison.  We sort by looking at
11732    the directories in the path.  */
11733
11734 static int
11735 file_info_cmp (const void *p1, const void *p2)
11736 {
11737   const struct file_info *const s1 = (const struct file_info *) p1;
11738   const struct file_info *const s2 = (const struct file_info *) p2;
11739   const unsigned char *cp1;
11740   const unsigned char *cp2;
11741
11742   /* Take care of file names without directories.  We need to make sure that
11743      we return consistent values to qsort since some will get confused if
11744      we return the same value when identical operands are passed in opposite
11745      orders.  So if neither has a directory, return 0 and otherwise return
11746      1 or -1 depending on which one has the directory.  */
11747   if ((s1->path == s1->fname || s2->path == s2->fname))
11748     return (s2->path == s2->fname) - (s1->path == s1->fname);
11749
11750   cp1 = (const unsigned char *) s1->path;
11751   cp2 = (const unsigned char *) s2->path;
11752
11753   while (1)
11754     {
11755       ++cp1;
11756       ++cp2;
11757       /* Reached the end of the first path?  If so, handle like above.  */
11758       if ((cp1 == (const unsigned char *) s1->fname)
11759           || (cp2 == (const unsigned char *) s2->fname))
11760         return ((cp2 == (const unsigned char *) s2->fname)
11761                 - (cp1 == (const unsigned char *) s1->fname));
11762
11763       /* Character of current path component the same?  */
11764       else if (*cp1 != *cp2)
11765         return *cp1 - *cp2;
11766     }
11767 }
11768
11769 struct file_name_acquire_data
11770 {
11771   struct file_info *files;
11772   int used_files;
11773   int max_files;
11774 };
11775
11776 /* Traversal function for the hash table.  */
11777
11778 static int
11779 file_name_acquire (void ** slot, void *data)
11780 {
11781   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11782   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11783   struct file_info *fi;
11784   const char *f;
11785
11786   gcc_assert (fnad->max_files >= d->emitted_number);
11787
11788   if (! d->emitted_number)
11789     return 1;
11790
11791   gcc_assert (fnad->max_files != fnad->used_files);
11792
11793   fi = fnad->files + fnad->used_files++;
11794
11795   /* Skip all leading "./".  */
11796   f = d->filename;
11797   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11798     f += 2;
11799
11800   /* Create a new array entry.  */
11801   fi->path = f;
11802   fi->length = strlen (f);
11803   fi->file_idx = d;
11804
11805   /* Search for the file name part.  */
11806   f = strrchr (f, DIR_SEPARATOR);
11807 #if defined (DIR_SEPARATOR_2)
11808   {
11809     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11810
11811     if (g != NULL)
11812       {
11813         if (f == NULL || f < g)
11814           f = g;
11815       }
11816   }
11817 #endif
11818
11819   fi->fname = f == NULL ? fi->path : f + 1;
11820   return 1;
11821 }
11822
11823 /* Output the directory table and the file name table.  We try to minimize
11824    the total amount of memory needed.  A heuristic is used to avoid large
11825    slowdowns with many input files.  */
11826
11827 static void
11828 output_file_names (void)
11829 {
11830   struct file_name_acquire_data fnad;
11831   int numfiles;
11832   struct file_info *files;
11833   struct dir_info *dirs;
11834   int *saved;
11835   int *savehere;
11836   int *backmap;
11837   int ndirs;
11838   int idx_offset;
11839   int i;
11840
11841   if (!last_emitted_file)
11842     {
11843       dw2_asm_output_data (1, 0, "End directory table");
11844       dw2_asm_output_data (1, 0, "End file name table");
11845       return;
11846     }
11847
11848   numfiles = last_emitted_file->emitted_number;
11849
11850   /* Allocate the various arrays we need.  */
11851   files = XALLOCAVEC (struct file_info, numfiles);
11852   dirs = XALLOCAVEC (struct dir_info, numfiles);
11853
11854   fnad.files = files;
11855   fnad.used_files = 0;
11856   fnad.max_files = numfiles;
11857   htab_traverse (file_table, file_name_acquire, &fnad);
11858   gcc_assert (fnad.used_files == fnad.max_files);
11859
11860   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11861
11862   /* Find all the different directories used.  */
11863   dirs[0].path = files[0].path;
11864   dirs[0].length = files[0].fname - files[0].path;
11865   dirs[0].prefix = -1;
11866   dirs[0].count = 1;
11867   dirs[0].dir_idx = 0;
11868   files[0].dir_idx = 0;
11869   ndirs = 1;
11870
11871   for (i = 1; i < numfiles; i++)
11872     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11873         && memcmp (dirs[ndirs - 1].path, files[i].path,
11874                    dirs[ndirs - 1].length) == 0)
11875       {
11876         /* Same directory as last entry.  */
11877         files[i].dir_idx = ndirs - 1;
11878         ++dirs[ndirs - 1].count;
11879       }
11880     else
11881       {
11882         int j;
11883
11884         /* This is a new directory.  */
11885         dirs[ndirs].path = files[i].path;
11886         dirs[ndirs].length = files[i].fname - files[i].path;
11887         dirs[ndirs].count = 1;
11888         dirs[ndirs].dir_idx = ndirs;
11889         files[i].dir_idx = ndirs;
11890
11891         /* Search for a prefix.  */
11892         dirs[ndirs].prefix = -1;
11893         for (j = 0; j < ndirs; j++)
11894           if (dirs[j].length < dirs[ndirs].length
11895               && dirs[j].length > 1
11896               && (dirs[ndirs].prefix == -1
11897                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11898               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11899             dirs[ndirs].prefix = j;
11900
11901         ++ndirs;
11902       }
11903
11904   /* Now to the actual work.  We have to find a subset of the directories which
11905      allow expressing the file name using references to the directory table
11906      with the least amount of characters.  We do not do an exhaustive search
11907      where we would have to check out every combination of every single
11908      possible prefix.  Instead we use a heuristic which provides nearly optimal
11909      results in most cases and never is much off.  */
11910   saved = XALLOCAVEC (int, ndirs);
11911   savehere = XALLOCAVEC (int, ndirs);
11912
11913   memset (saved, '\0', ndirs * sizeof (saved[0]));
11914   for (i = 0; i < ndirs; i++)
11915     {
11916       int j;
11917       int total;
11918
11919       /* We can always save some space for the current directory.  But this
11920          does not mean it will be enough to justify adding the directory.  */
11921       savehere[i] = dirs[i].length;
11922       total = (savehere[i] - saved[i]) * dirs[i].count;
11923
11924       for (j = i + 1; j < ndirs; j++)
11925         {
11926           savehere[j] = 0;
11927           if (saved[j] < dirs[i].length)
11928             {
11929               /* Determine whether the dirs[i] path is a prefix of the
11930                  dirs[j] path.  */
11931               int k;
11932
11933               k = dirs[j].prefix;
11934               while (k != -1 && k != (int) i)
11935                 k = dirs[k].prefix;
11936
11937               if (k == (int) i)
11938                 {
11939                   /* Yes it is.  We can possibly save some memory by
11940                      writing the filenames in dirs[j] relative to
11941                      dirs[i].  */
11942                   savehere[j] = dirs[i].length;
11943                   total += (savehere[j] - saved[j]) * dirs[j].count;
11944                 }
11945             }
11946         }
11947
11948       /* Check whether we can save enough to justify adding the dirs[i]
11949          directory.  */
11950       if (total > dirs[i].length + 1)
11951         {
11952           /* It's worthwhile adding.  */
11953           for (j = i; j < ndirs; j++)
11954             if (savehere[j] > 0)
11955               {
11956                 /* Remember how much we saved for this directory so far.  */
11957                 saved[j] = savehere[j];
11958
11959                 /* Remember the prefix directory.  */
11960                 dirs[j].dir_idx = i;
11961               }
11962         }
11963     }
11964
11965   /* Emit the directory name table.  */
11966   idx_offset = dirs[0].length > 0 ? 1 : 0;
11967   for (i = 1 - idx_offset; i < ndirs; i++)
11968     dw2_asm_output_nstring (dirs[i].path,
11969                             dirs[i].length
11970                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11971                             "Directory Entry: %#x", i + idx_offset);
11972
11973   dw2_asm_output_data (1, 0, "End directory table");
11974
11975   /* We have to emit them in the order of emitted_number since that's
11976      used in the debug info generation.  To do this efficiently we
11977      generate a back-mapping of the indices first.  */
11978   backmap = XALLOCAVEC (int, numfiles);
11979   for (i = 0; i < numfiles; i++)
11980     backmap[files[i].file_idx->emitted_number - 1] = i;
11981
11982   /* Now write all the file names.  */
11983   for (i = 0; i < numfiles; i++)
11984     {
11985       int file_idx = backmap[i];
11986       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11987
11988 #ifdef VMS_DEBUGGING_INFO
11989 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11990
11991       /* Setting these fields can lead to debugger miscomparisons,
11992          but VMS Debug requires them to be set correctly.  */
11993
11994       int ver;
11995       long long cdt;
11996       long siz;
11997       int maxfilelen = strlen (files[file_idx].path)
11998                                + dirs[dir_idx].length
11999                                + MAX_VMS_VERSION_LEN + 1;
12000       char *filebuf = XALLOCAVEC (char, maxfilelen);
12001
12002       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12003       snprintf (filebuf, maxfilelen, "%s;%d",
12004                 files[file_idx].path + dirs[dir_idx].length, ver);
12005
12006       dw2_asm_output_nstring
12007         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12008
12009       /* Include directory index.  */
12010       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12011
12012       /* Modification time.  */
12013       dw2_asm_output_data_uleb128
12014         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12015           ? cdt : 0,
12016          NULL);
12017
12018       /* File length in bytes.  */
12019       dw2_asm_output_data_uleb128
12020         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12021           ? siz : 0,
12022          NULL);
12023 #else
12024       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12025                               "File Entry: %#x", (unsigned) i + 1);
12026
12027       /* Include directory index.  */
12028       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12029
12030       /* Modification time.  */
12031       dw2_asm_output_data_uleb128 (0, NULL);
12032
12033       /* File length in bytes.  */
12034       dw2_asm_output_data_uleb128 (0, NULL);
12035 #endif /* VMS_DEBUGGING_INFO */
12036     }
12037
12038   dw2_asm_output_data (1, 0, "End file name table");
12039 }
12040
12041
12042 /* Output the source line number correspondence information.  This
12043    information goes into the .debug_line section.  */
12044
12045 static void
12046 output_line_info (void)
12047 {
12048   char l1[20], l2[20], p1[20], p2[20];
12049   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12050   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12051   unsigned opc;
12052   unsigned n_op_args;
12053   unsigned long lt_index;
12054   unsigned long current_line;
12055   long line_offset;
12056   long line_delta;
12057   unsigned long current_file;
12058   unsigned long function;
12059   int ver = dwarf_version;
12060
12061   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12062   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12063   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12064   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12065
12066   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12067     dw2_asm_output_data (4, 0xffffffff,
12068       "Initial length escape value indicating 64-bit DWARF extension");
12069   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12070                         "Length of Source Line Info");
12071   ASM_OUTPUT_LABEL (asm_out_file, l1);
12072
12073   dw2_asm_output_data (2, ver, "DWARF Version");
12074   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12075   ASM_OUTPUT_LABEL (asm_out_file, p1);
12076
12077   /* Define the architecture-dependent minimum instruction length (in
12078    bytes).  In this implementation of DWARF, this field is used for
12079    information purposes only.  Since GCC generates assembly language,
12080    we have no a priori knowledge of how many instruction bytes are
12081    generated for each source line, and therefore can use only the
12082    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12083    commands.  Accordingly, we fix this as `1', which is "correct
12084    enough" for all architectures, and don't let the target override.  */
12085   dw2_asm_output_data (1, 1,
12086                        "Minimum Instruction Length");
12087
12088   if (ver >= 4)
12089     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12090                          "Maximum Operations Per Instruction");
12091   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12092                        "Default is_stmt_start flag");
12093   dw2_asm_output_data (1, DWARF_LINE_BASE,
12094                        "Line Base Value (Special Opcodes)");
12095   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12096                        "Line Range Value (Special Opcodes)");
12097   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12098                        "Special Opcode Base");
12099
12100   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12101     {
12102       switch (opc)
12103         {
12104         case DW_LNS_advance_pc:
12105         case DW_LNS_advance_line:
12106         case DW_LNS_set_file:
12107         case DW_LNS_set_column:
12108         case DW_LNS_fixed_advance_pc:
12109           n_op_args = 1;
12110           break;
12111         default:
12112           n_op_args = 0;
12113           break;
12114         }
12115
12116       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12117                            opc, n_op_args);
12118     }
12119
12120   /* Write out the information about the files we use.  */
12121   output_file_names ();
12122   ASM_OUTPUT_LABEL (asm_out_file, p2);
12123
12124   /* We used to set the address register to the first location in the text
12125      section here, but that didn't accomplish anything since we already
12126      have a line note for the opening brace of the first function.  */
12127
12128   /* Generate the line number to PC correspondence table, encoded as
12129      a series of state machine operations.  */
12130   current_file = 1;
12131   current_line = 1;
12132
12133   if (cfun && in_cold_section_p)
12134     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12135   else
12136     strcpy (prev_line_label, text_section_label);
12137   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12138     {
12139       dw_line_info_ref line_info = &line_info_table[lt_index];
12140
12141 #if 0
12142       /* Disable this optimization for now; GDB wants to see two line notes
12143          at the beginning of a function so it can find the end of the
12144          prologue.  */
12145
12146       /* Don't emit anything for redundant notes.  Just updating the
12147          address doesn't accomplish anything, because we already assume
12148          that anything after the last address is this line.  */
12149       if (line_info->dw_line_num == current_line
12150           && line_info->dw_file_num == current_file)
12151         continue;
12152 #endif
12153
12154       /* Emit debug info for the address of the current line.
12155
12156          Unfortunately, we have little choice here currently, and must always
12157          use the most general form.  GCC does not know the address delta
12158          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12159          attributes which will give an upper bound on the address range.  We
12160          could perhaps use length attributes to determine when it is safe to
12161          use DW_LNS_fixed_advance_pc.  */
12162
12163       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12164       if (0)
12165         {
12166           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12167           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12168                                "DW_LNS_fixed_advance_pc");
12169           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12170         }
12171       else
12172         {
12173           /* This can handle any delta.  This takes
12174              4+DWARF2_ADDR_SIZE bytes.  */
12175           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12176           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12177           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12178           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12179         }
12180
12181       strcpy (prev_line_label, line_label);
12182
12183       /* Emit debug info for the source file of the current line, if
12184          different from the previous line.  */
12185       if (line_info->dw_file_num != current_file)
12186         {
12187           current_file = line_info->dw_file_num;
12188           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12189           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12190         }
12191
12192       /* Emit debug info for the current line number, choosing the encoding
12193          that uses the least amount of space.  */
12194       if (line_info->dw_line_num != current_line)
12195         {
12196           line_offset = line_info->dw_line_num - current_line;
12197           line_delta = line_offset - DWARF_LINE_BASE;
12198           current_line = line_info->dw_line_num;
12199           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12200             /* This can handle deltas from -10 to 234, using the current
12201                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12202                takes 1 byte.  */
12203             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12204                                  "line %lu", current_line);
12205           else
12206             {
12207               /* This can handle any delta.  This takes at least 4 bytes,
12208                  depending on the value being encoded.  */
12209               dw2_asm_output_data (1, DW_LNS_advance_line,
12210                                    "advance to line %lu", current_line);
12211               dw2_asm_output_data_sleb128 (line_offset, NULL);
12212               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12213             }
12214         }
12215       else
12216         /* We still need to start a new row, so output a copy insn.  */
12217         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12218     }
12219
12220   /* Emit debug info for the address of the end of the function.  */
12221   if (0)
12222     {
12223       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12224                            "DW_LNS_fixed_advance_pc");
12225       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12226     }
12227   else
12228     {
12229       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12230       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12231       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12232       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12233     }
12234
12235   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12236   dw2_asm_output_data_uleb128 (1, NULL);
12237   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12238
12239   function = 0;
12240   current_file = 1;
12241   current_line = 1;
12242   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12243     {
12244       dw_separate_line_info_ref line_info
12245         = &separate_line_info_table[lt_index];
12246
12247 #if 0
12248       /* Don't emit anything for redundant notes.  */
12249       if (line_info->dw_line_num == current_line
12250           && line_info->dw_file_num == current_file
12251           && line_info->function == function)
12252         goto cont;
12253 #endif
12254
12255       /* Emit debug info for the address of the current line.  If this is
12256          a new function, or the first line of a function, then we need
12257          to handle it differently.  */
12258       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12259                                    lt_index);
12260       if (function != line_info->function)
12261         {
12262           function = line_info->function;
12263
12264           /* Set the address register to the first line in the function.  */
12265           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12266           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12267           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12268           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12269         }
12270       else
12271         {
12272           /* ??? See the DW_LNS_advance_pc comment above.  */
12273           if (0)
12274             {
12275               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12276                                    "DW_LNS_fixed_advance_pc");
12277               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12278             }
12279           else
12280             {
12281               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12282               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12283               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12284               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12285             }
12286         }
12287
12288       strcpy (prev_line_label, line_label);
12289
12290       /* Emit debug info for the source file of the current line, if
12291          different from the previous line.  */
12292       if (line_info->dw_file_num != current_file)
12293         {
12294           current_file = line_info->dw_file_num;
12295           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12296           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12297         }
12298
12299       /* Emit debug info for the current line number, choosing the encoding
12300          that uses the least amount of space.  */
12301       if (line_info->dw_line_num != current_line)
12302         {
12303           line_offset = line_info->dw_line_num - current_line;
12304           line_delta = line_offset - DWARF_LINE_BASE;
12305           current_line = line_info->dw_line_num;
12306           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12307             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12308                                  "line %lu", current_line);
12309           else
12310             {
12311               dw2_asm_output_data (1, DW_LNS_advance_line,
12312                                    "advance to line %lu", current_line);
12313               dw2_asm_output_data_sleb128 (line_offset, NULL);
12314               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12315             }
12316         }
12317       else
12318         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12319
12320 #if 0
12321     cont:
12322 #endif
12323
12324       lt_index++;
12325
12326       /* If we're done with a function, end its sequence.  */
12327       if (lt_index == separate_line_info_table_in_use
12328           || separate_line_info_table[lt_index].function != function)
12329         {
12330           current_file = 1;
12331           current_line = 1;
12332
12333           /* Emit debug info for the address of the end of the function.  */
12334           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12335           if (0)
12336             {
12337               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12338                                    "DW_LNS_fixed_advance_pc");
12339               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12340             }
12341           else
12342             {
12343               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12344               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12345               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12346               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12347             }
12348
12349           /* Output the marker for the end of this sequence.  */
12350           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12351           dw2_asm_output_data_uleb128 (1, NULL);
12352           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12353         }
12354     }
12355
12356   /* Output the marker for the end of the line number info.  */
12357   ASM_OUTPUT_LABEL (asm_out_file, l2);
12358 }
12359
12360 /* Return the size of the .debug_dcall table for the compilation unit.  */
12361
12362 static unsigned long
12363 size_of_dcall_table (void)
12364 {
12365   unsigned long size;
12366   unsigned int i;
12367   dcall_entry *p;
12368   tree last_poc_decl = NULL;
12369
12370   /* Header:  version + debug info section pointer + pointer size.  */
12371   size = 2 + DWARF_OFFSET_SIZE + 1;
12372
12373   /* Each entry:  code label + DIE offset.  */
12374   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12375     {
12376       gcc_assert (p->targ_die != NULL);
12377       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12378       if (p->poc_decl != last_poc_decl)
12379         {
12380           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12381           gcc_assert (poc_die);
12382           last_poc_decl = p->poc_decl;
12383           if (poc_die)
12384             size += (DWARF_OFFSET_SIZE
12385                      + size_of_uleb128 (poc_die->die_offset));
12386         }
12387       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12388     }
12389
12390   return size;
12391 }
12392
12393 /* Output the direct call table used to disambiguate PC values when
12394    identical function have been merged.  */
12395
12396 static void
12397 output_dcall_table (void)
12398 {
12399   unsigned i;
12400   unsigned long dcall_length = size_of_dcall_table ();
12401   dcall_entry *p;
12402   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12403   tree last_poc_decl = NULL;
12404
12405   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12406     dw2_asm_output_data (4, 0xffffffff,
12407       "Initial length escape value indicating 64-bit DWARF extension");
12408   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12409                        "Length of Direct Call Table");
12410   dw2_asm_output_data (2, 4, "Version number");
12411   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12412                          debug_info_section,
12413                          "Offset of Compilation Unit Info");
12414   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12415
12416   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12417     {
12418       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12419       if (p->poc_decl != last_poc_decl)
12420         {
12421           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12422           last_poc_decl = p->poc_decl;
12423           if (poc_die)
12424             {
12425               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12426               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12427                                            "Caller DIE offset");
12428             }
12429         }
12430       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12431       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12432       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12433                                    "Callee DIE offset");
12434     }
12435 }
12436 \f
12437 /* Return the size of the .debug_vcall table for the compilation unit.  */
12438
12439 static unsigned long
12440 size_of_vcall_table (void)
12441 {
12442   unsigned long size;
12443   unsigned int i;
12444   vcall_entry *p;
12445
12446   /* Header:  version + pointer size.  */
12447   size = 2 + 1;
12448
12449   /* Each entry:  code label + vtable slot index.  */
12450   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12451     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12452
12453   return size;
12454 }
12455
12456 /* Output the virtual call table used to disambiguate PC values when
12457    identical function have been merged.  */
12458
12459 static void
12460 output_vcall_table (void)
12461 {
12462   unsigned i;
12463   unsigned long vcall_length = size_of_vcall_table ();
12464   vcall_entry *p;
12465   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12466
12467   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12468     dw2_asm_output_data (4, 0xffffffff,
12469       "Initial length escape value indicating 64-bit DWARF extension");
12470   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12471                        "Length of Virtual Call Table");
12472   dw2_asm_output_data (2, 4, "Version number");
12473   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12474
12475   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12476     {
12477       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12478       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12479       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12480     }
12481 }
12482 \f
12483 /* Given a pointer to a tree node for some base type, return a pointer to
12484    a DIE that describes the given type.
12485
12486    This routine must only be called for GCC type nodes that correspond to
12487    Dwarf base (fundamental) types.  */
12488
12489 static dw_die_ref
12490 base_type_die (tree type)
12491 {
12492   dw_die_ref base_type_result;
12493   enum dwarf_type encoding;
12494
12495   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12496     return 0;
12497
12498   /* If this is a subtype that should not be emitted as a subrange type,
12499      use the base type.  See subrange_type_for_debug_p.  */
12500   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12501     type = TREE_TYPE (type);
12502
12503   switch (TREE_CODE (type))
12504     {
12505     case INTEGER_TYPE:
12506       if ((dwarf_version >= 4 || !dwarf_strict)
12507           && TYPE_NAME (type)
12508           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12509           && DECL_IS_BUILTIN (TYPE_NAME (type))
12510           && DECL_NAME (TYPE_NAME (type)))
12511         {
12512           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12513           if (strcmp (name, "char16_t") == 0
12514               || strcmp (name, "char32_t") == 0)
12515             {
12516               encoding = DW_ATE_UTF;
12517               break;
12518             }
12519         }
12520       if (TYPE_STRING_FLAG (type))
12521         {
12522           if (TYPE_UNSIGNED (type))
12523             encoding = DW_ATE_unsigned_char;
12524           else
12525             encoding = DW_ATE_signed_char;
12526         }
12527       else if (TYPE_UNSIGNED (type))
12528         encoding = DW_ATE_unsigned;
12529       else
12530         encoding = DW_ATE_signed;
12531       break;
12532
12533     case REAL_TYPE:
12534       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12535         {
12536           if (dwarf_version >= 3 || !dwarf_strict)
12537             encoding = DW_ATE_decimal_float;
12538           else
12539             encoding = DW_ATE_lo_user;
12540         }
12541       else
12542         encoding = DW_ATE_float;
12543       break;
12544
12545     case FIXED_POINT_TYPE:
12546       if (!(dwarf_version >= 3 || !dwarf_strict))
12547         encoding = DW_ATE_lo_user;
12548       else if (TYPE_UNSIGNED (type))
12549         encoding = DW_ATE_unsigned_fixed;
12550       else
12551         encoding = DW_ATE_signed_fixed;
12552       break;
12553
12554       /* Dwarf2 doesn't know anything about complex ints, so use
12555          a user defined type for it.  */
12556     case COMPLEX_TYPE:
12557       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12558         encoding = DW_ATE_complex_float;
12559       else
12560         encoding = DW_ATE_lo_user;
12561       break;
12562
12563     case BOOLEAN_TYPE:
12564       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12565       encoding = DW_ATE_boolean;
12566       break;
12567
12568     default:
12569       /* No other TREE_CODEs are Dwarf fundamental types.  */
12570       gcc_unreachable ();
12571     }
12572
12573   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12574
12575   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12576                    int_size_in_bytes (type));
12577   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12578
12579   return base_type_result;
12580 }
12581
12582 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12583    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12584
12585 static inline int
12586 is_base_type (tree type)
12587 {
12588   switch (TREE_CODE (type))
12589     {
12590     case ERROR_MARK:
12591     case VOID_TYPE:
12592     case INTEGER_TYPE:
12593     case REAL_TYPE:
12594     case FIXED_POINT_TYPE:
12595     case COMPLEX_TYPE:
12596     case BOOLEAN_TYPE:
12597       return 1;
12598
12599     case ARRAY_TYPE:
12600     case RECORD_TYPE:
12601     case UNION_TYPE:
12602     case QUAL_UNION_TYPE:
12603     case ENUMERAL_TYPE:
12604     case FUNCTION_TYPE:
12605     case METHOD_TYPE:
12606     case POINTER_TYPE:
12607     case REFERENCE_TYPE:
12608     case OFFSET_TYPE:
12609     case LANG_TYPE:
12610     case VECTOR_TYPE:
12611       return 0;
12612
12613     default:
12614       gcc_unreachable ();
12615     }
12616
12617   return 0;
12618 }
12619
12620 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12621    node, return the size in bits for the type if it is a constant, or else
12622    return the alignment for the type if the type's size is not constant, or
12623    else return BITS_PER_WORD if the type actually turns out to be an
12624    ERROR_MARK node.  */
12625
12626 static inline unsigned HOST_WIDE_INT
12627 simple_type_size_in_bits (const_tree type)
12628 {
12629   if (TREE_CODE (type) == ERROR_MARK)
12630     return BITS_PER_WORD;
12631   else if (TYPE_SIZE (type) == NULL_TREE)
12632     return 0;
12633   else if (host_integerp (TYPE_SIZE (type), 1))
12634     return tree_low_cst (TYPE_SIZE (type), 1);
12635   else
12636     return TYPE_ALIGN (type);
12637 }
12638
12639 /* Similarly, but return a double_int instead of UHWI.  */
12640
12641 static inline double_int
12642 double_int_type_size_in_bits (const_tree type)
12643 {
12644   if (TREE_CODE (type) == ERROR_MARK)
12645     return uhwi_to_double_int (BITS_PER_WORD);
12646   else if (TYPE_SIZE (type) == NULL_TREE)
12647     return double_int_zero;
12648   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12649     return tree_to_double_int (TYPE_SIZE (type));
12650   else
12651     return uhwi_to_double_int (TYPE_ALIGN (type));
12652 }
12653
12654 /*  Given a pointer to a tree node for a subrange type, return a pointer
12655     to a DIE that describes the given type.  */
12656
12657 static dw_die_ref
12658 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12659 {
12660   dw_die_ref subrange_die;
12661   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12662
12663   if (context_die == NULL)
12664     context_die = comp_unit_die ();
12665
12666   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12667
12668   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12669     {
12670       /* The size of the subrange type and its base type do not match,
12671          so we need to generate a size attribute for the subrange type.  */
12672       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12673     }
12674
12675   if (low)
12676     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12677   if (high)
12678     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12679
12680   return subrange_die;
12681 }
12682
12683 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12684    entry that chains various modifiers in front of the given type.  */
12685
12686 static dw_die_ref
12687 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12688                    dw_die_ref context_die)
12689 {
12690   enum tree_code code = TREE_CODE (type);
12691   dw_die_ref mod_type_die;
12692   dw_die_ref sub_die = NULL;
12693   tree item_type = NULL;
12694   tree qualified_type;
12695   tree name, low, high;
12696
12697   if (code == ERROR_MARK)
12698     return NULL;
12699
12700   /* See if we already have the appropriately qualified variant of
12701      this type.  */
12702   qualified_type
12703     = get_qualified_type (type,
12704                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12705                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12706
12707   if (qualified_type == sizetype
12708       && TYPE_NAME (qualified_type)
12709       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12710     {
12711 #ifdef ENABLE_CHECKING
12712       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12713                   == INTEGER_TYPE
12714                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12715                      == TYPE_PRECISION (qualified_type)
12716                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12717                      == TYPE_UNSIGNED (qualified_type));
12718 #endif
12719       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12720     }
12721
12722   /* If we do, then we can just use its DIE, if it exists.  */
12723   if (qualified_type)
12724     {
12725       mod_type_die = lookup_type_die (qualified_type);
12726       if (mod_type_die)
12727         return mod_type_die;
12728     }
12729
12730   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12731
12732   /* Handle C typedef types.  */
12733   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12734       && !DECL_ARTIFICIAL (name))
12735     {
12736       tree dtype = TREE_TYPE (name);
12737
12738       if (qualified_type == dtype)
12739         {
12740           /* For a named type, use the typedef.  */
12741           gen_type_die (qualified_type, context_die);
12742           return lookup_type_die (qualified_type);
12743         }
12744       else if (is_const_type < TYPE_READONLY (dtype)
12745                || is_volatile_type < TYPE_VOLATILE (dtype)
12746                || (is_const_type <= TYPE_READONLY (dtype)
12747                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12748                    && DECL_ORIGINAL_TYPE (name) != type))
12749         /* cv-unqualified version of named type.  Just use the unnamed
12750            type to which it refers.  */
12751         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12752                                   is_const_type, is_volatile_type,
12753                                   context_die);
12754       /* Else cv-qualified version of named type; fall through.  */
12755     }
12756
12757   if (is_const_type)
12758     {
12759       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12760       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12761     }
12762   else if (is_volatile_type)
12763     {
12764       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12765       sub_die = modified_type_die (type, 0, 0, context_die);
12766     }
12767   else if (code == POINTER_TYPE)
12768     {
12769       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12770       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12771                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12772       item_type = TREE_TYPE (type);
12773       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12774         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12775                          TYPE_ADDR_SPACE (item_type));
12776     }
12777   else if (code == REFERENCE_TYPE)
12778     {
12779       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12780         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12781                                 type);
12782       else
12783         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12784       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12785                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12786       item_type = TREE_TYPE (type);
12787       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12788         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12789                          TYPE_ADDR_SPACE (item_type));
12790     }
12791   else if (code == INTEGER_TYPE
12792            && TREE_TYPE (type) != NULL_TREE
12793            && subrange_type_for_debug_p (type, &low, &high))
12794     {
12795       mod_type_die = subrange_type_die (type, low, high, context_die);
12796       item_type = TREE_TYPE (type);
12797     }
12798   else if (is_base_type (type))
12799     mod_type_die = base_type_die (type);
12800   else
12801     {
12802       gen_type_die (type, context_die);
12803
12804       /* We have to get the type_main_variant here (and pass that to the
12805          `lookup_type_die' routine) because the ..._TYPE node we have
12806          might simply be a *copy* of some original type node (where the
12807          copy was created to help us keep track of typedef names) and
12808          that copy might have a different TYPE_UID from the original
12809          ..._TYPE node.  */
12810       if (TREE_CODE (type) != VECTOR_TYPE)
12811         return lookup_type_die (type_main_variant (type));
12812       else
12813         /* Vectors have the debugging information in the type,
12814            not the main variant.  */
12815         return lookup_type_die (type);
12816     }
12817
12818   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12819      don't output a DW_TAG_typedef, since there isn't one in the
12820      user's program; just attach a DW_AT_name to the type.
12821      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12822      if the base type already has the same name.  */
12823   if (name
12824       && ((TREE_CODE (name) != TYPE_DECL
12825            && (qualified_type == TYPE_MAIN_VARIANT (type)
12826                || (!is_const_type && !is_volatile_type)))
12827           || (TREE_CODE (name) == TYPE_DECL
12828               && TREE_TYPE (name) == qualified_type
12829               && DECL_NAME (name))))
12830     {
12831       if (TREE_CODE (name) == TYPE_DECL)
12832         /* Could just call add_name_and_src_coords_attributes here,
12833            but since this is a builtin type it doesn't have any
12834            useful source coordinates anyway.  */
12835         name = DECL_NAME (name);
12836       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12837     }
12838   /* This probably indicates a bug.  */
12839   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12840     add_name_attribute (mod_type_die, "__unknown__");
12841
12842   if (qualified_type)
12843     equate_type_number_to_die (qualified_type, mod_type_die);
12844
12845   if (item_type)
12846     /* We must do this after the equate_type_number_to_die call, in case
12847        this is a recursive type.  This ensures that the modified_type_die
12848        recursion will terminate even if the type is recursive.  Recursive
12849        types are possible in Ada.  */
12850     sub_die = modified_type_die (item_type,
12851                                  TYPE_READONLY (item_type),
12852                                  TYPE_VOLATILE (item_type),
12853                                  context_die);
12854
12855   if (sub_die != NULL)
12856     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12857
12858   return mod_type_die;
12859 }
12860
12861 /* Generate DIEs for the generic parameters of T.
12862    T must be either a generic type or a generic function.
12863    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12864
12865 static void
12866 gen_generic_params_dies (tree t)
12867 {
12868   tree parms, args;
12869   int parms_num, i;
12870   dw_die_ref die = NULL;
12871
12872   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12873     return;
12874
12875   if (TYPE_P (t))
12876     die = lookup_type_die (t);
12877   else if (DECL_P (t))
12878     die = lookup_decl_die (t);
12879
12880   gcc_assert (die);
12881
12882   parms = lang_hooks.get_innermost_generic_parms (t);
12883   if (!parms)
12884     /* T has no generic parameter. It means T is neither a generic type
12885        or function. End of story.  */
12886     return;
12887
12888   parms_num = TREE_VEC_LENGTH (parms);
12889   args = lang_hooks.get_innermost_generic_args (t);
12890   for (i = 0; i < parms_num; i++)
12891     {
12892       tree parm, arg, arg_pack_elems;
12893
12894       parm = TREE_VEC_ELT (parms, i);
12895       arg = TREE_VEC_ELT (args, i);
12896       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12897       gcc_assert (parm && TREE_VALUE (parm) && arg);
12898
12899       if (parm && TREE_VALUE (parm) && arg)
12900         {
12901           /* If PARM represents a template parameter pack,
12902              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12903              by DW_TAG_template_*_parameter DIEs for the argument
12904              pack elements of ARG. Note that ARG would then be
12905              an argument pack.  */
12906           if (arg_pack_elems)
12907             template_parameter_pack_die (TREE_VALUE (parm),
12908                                          arg_pack_elems,
12909                                          die);
12910           else
12911             generic_parameter_die (TREE_VALUE (parm), arg,
12912                                    true /* Emit DW_AT_name */, die);
12913         }
12914     }
12915 }
12916
12917 /* Create and return a DIE for PARM which should be
12918    the representation of a generic type parameter.
12919    For instance, in the C++ front end, PARM would be a template parameter.
12920    ARG is the argument to PARM.
12921    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12922    name of the PARM.
12923    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12924    as a child node.  */
12925
12926 static dw_die_ref
12927 generic_parameter_die (tree parm, tree arg,
12928                        bool emit_name_p,
12929                        dw_die_ref parent_die)
12930 {
12931   dw_die_ref tmpl_die = NULL;
12932   const char *name = NULL;
12933
12934   if (!parm || !DECL_NAME (parm) || !arg)
12935     return NULL;
12936
12937   /* We support non-type generic parameters and arguments,
12938      type generic parameters and arguments, as well as
12939      generic generic parameters (a.k.a. template template parameters in C++)
12940      and arguments.  */
12941   if (TREE_CODE (parm) == PARM_DECL)
12942     /* PARM is a nontype generic parameter  */
12943     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12944   else if (TREE_CODE (parm) == TYPE_DECL)
12945     /* PARM is a type generic parameter.  */
12946     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12947   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12948     /* PARM is a generic generic parameter.
12949        Its DIE is a GNU extension. It shall have a
12950        DW_AT_name attribute to represent the name of the template template
12951        parameter, and a DW_AT_GNU_template_name attribute to represent the
12952        name of the template template argument.  */
12953     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12954                         parent_die, parm);
12955   else
12956     gcc_unreachable ();
12957
12958   if (tmpl_die)
12959     {
12960       tree tmpl_type;
12961
12962       /* If PARM is a generic parameter pack, it means we are
12963          emitting debug info for a template argument pack element.
12964          In other terms, ARG is a template argument pack element.
12965          In that case, we don't emit any DW_AT_name attribute for
12966          the die.  */
12967       if (emit_name_p)
12968         {
12969           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12970           gcc_assert (name);
12971           add_AT_string (tmpl_die, DW_AT_name, name);
12972         }
12973
12974       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12975         {
12976           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12977              TMPL_DIE should have a child DW_AT_type attribute that is set
12978              to the type of the argument to PARM, which is ARG.
12979              If PARM is a type generic parameter, TMPL_DIE should have a
12980              child DW_AT_type that is set to ARG.  */
12981           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12982           add_type_attribute (tmpl_die, tmpl_type, 0,
12983                               TREE_THIS_VOLATILE (tmpl_type),
12984                               parent_die);
12985         }
12986       else
12987         {
12988           /* So TMPL_DIE is a DIE representing a
12989              a generic generic template parameter, a.k.a template template
12990              parameter in C++ and arg is a template.  */
12991
12992           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12993              to the name of the argument.  */
12994           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12995           if (name)
12996             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12997         }
12998
12999       if (TREE_CODE (parm) == PARM_DECL)
13000         /* So PARM is a non-type generic parameter.
13001            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13002            attribute of TMPL_DIE which value represents the value
13003            of ARG.
13004            We must be careful here:
13005            The value of ARG might reference some function decls.
13006            We might currently be emitting debug info for a generic
13007            type and types are emitted before function decls, we don't
13008            know if the function decls referenced by ARG will actually be
13009            emitted after cgraph computations.
13010            So must defer the generation of the DW_AT_const_value to
13011            after cgraph is ready.  */
13012         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13013     }
13014
13015   return tmpl_die;
13016 }
13017
13018 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13019    PARM_PACK must be a template parameter pack. The returned DIE
13020    will be child DIE of PARENT_DIE.  */
13021
13022 static dw_die_ref
13023 template_parameter_pack_die (tree parm_pack,
13024                              tree parm_pack_args,
13025                              dw_die_ref parent_die)
13026 {
13027   dw_die_ref die;
13028   int j;
13029
13030   gcc_assert (parent_die && parm_pack);
13031
13032   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13033   add_name_and_src_coords_attributes (die, parm_pack);
13034   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13035     generic_parameter_die (parm_pack,
13036                            TREE_VEC_ELT (parm_pack_args, j),
13037                            false /* Don't emit DW_AT_name */,
13038                            die);
13039   return die;
13040 }
13041
13042 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13043    an enumerated type.  */
13044
13045 static inline int
13046 type_is_enum (const_tree type)
13047 {
13048   return TREE_CODE (type) == ENUMERAL_TYPE;
13049 }
13050
13051 /* Return the DBX register number described by a given RTL node.  */
13052
13053 static unsigned int
13054 dbx_reg_number (const_rtx rtl)
13055 {
13056   unsigned regno = REGNO (rtl);
13057
13058   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13059
13060 #ifdef LEAF_REG_REMAP
13061   if (current_function_uses_only_leaf_regs)
13062     {
13063       int leaf_reg = LEAF_REG_REMAP (regno);
13064       if (leaf_reg != -1)
13065         regno = (unsigned) leaf_reg;
13066     }
13067 #endif
13068
13069   return DBX_REGISTER_NUMBER (regno);
13070 }
13071
13072 /* Optionally add a DW_OP_piece term to a location description expression.
13073    DW_OP_piece is only added if the location description expression already
13074    doesn't end with DW_OP_piece.  */
13075
13076 static void
13077 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13078 {
13079   dw_loc_descr_ref loc;
13080
13081   if (*list_head != NULL)
13082     {
13083       /* Find the end of the chain.  */
13084       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13085         ;
13086
13087       if (loc->dw_loc_opc != DW_OP_piece)
13088         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13089     }
13090 }
13091
13092 /* Return a location descriptor that designates a machine register or
13093    zero if there is none.  */
13094
13095 static dw_loc_descr_ref
13096 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13097 {
13098   rtx regs;
13099
13100   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13101     return 0;
13102
13103   /* We only use "frame base" when we're sure we're talking about the
13104      post-prologue local stack frame.  We do this by *not* running
13105      register elimination until this point, and recognizing the special
13106      argument pointer and soft frame pointer rtx's.
13107      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13108   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13109       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13110     {
13111       dw_loc_descr_ref result = NULL;
13112
13113       if (dwarf_version >= 4 || !dwarf_strict)
13114         {
13115           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13116           if (result)
13117             add_loc_descr (&result,
13118                            new_loc_descr (DW_OP_stack_value, 0, 0));
13119         }
13120       return result;
13121     }
13122
13123   regs = targetm.dwarf_register_span (rtl);
13124
13125   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13126     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13127   else
13128     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13129 }
13130
13131 /* Return a location descriptor that designates a machine register for
13132    a given hard register number.  */
13133
13134 static dw_loc_descr_ref
13135 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13136 {
13137   dw_loc_descr_ref reg_loc_descr;
13138
13139   if (regno <= 31)
13140     reg_loc_descr
13141       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13142   else
13143     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13144
13145   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13146     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13147
13148   return reg_loc_descr;
13149 }
13150
13151 /* Given an RTL of a register, return a location descriptor that
13152    designates a value that spans more than one register.  */
13153
13154 static dw_loc_descr_ref
13155 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13156                              enum var_init_status initialized)
13157 {
13158   int nregs, size, i;
13159   unsigned reg;
13160   dw_loc_descr_ref loc_result = NULL;
13161
13162   reg = REGNO (rtl);
13163 #ifdef LEAF_REG_REMAP
13164   if (current_function_uses_only_leaf_regs)
13165     {
13166       int leaf_reg = LEAF_REG_REMAP (reg);
13167       if (leaf_reg != -1)
13168         reg = (unsigned) leaf_reg;
13169     }
13170 #endif
13171   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13172   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13173
13174   /* Simple, contiguous registers.  */
13175   if (regs == NULL_RTX)
13176     {
13177       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13178
13179       loc_result = NULL;
13180       while (nregs--)
13181         {
13182           dw_loc_descr_ref t;
13183
13184           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13185                                       VAR_INIT_STATUS_INITIALIZED);
13186           add_loc_descr (&loc_result, t);
13187           add_loc_descr_op_piece (&loc_result, size);
13188           ++reg;
13189         }
13190       return loc_result;
13191     }
13192
13193   /* Now onto stupid register sets in non contiguous locations.  */
13194
13195   gcc_assert (GET_CODE (regs) == PARALLEL);
13196
13197   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13198   loc_result = NULL;
13199
13200   for (i = 0; i < XVECLEN (regs, 0); ++i)
13201     {
13202       dw_loc_descr_ref t;
13203
13204       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13205                                   VAR_INIT_STATUS_INITIALIZED);
13206       add_loc_descr (&loc_result, t);
13207       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13208       add_loc_descr_op_piece (&loc_result, size);
13209     }
13210
13211   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13212     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13213   return loc_result;
13214 }
13215
13216 /* Return a location descriptor that designates a constant.  */
13217
13218 static dw_loc_descr_ref
13219 int_loc_descriptor (HOST_WIDE_INT i)
13220 {
13221   enum dwarf_location_atom op;
13222
13223   /* Pick the smallest representation of a constant, rather than just
13224      defaulting to the LEB encoding.  */
13225   if (i >= 0)
13226     {
13227       if (i <= 31)
13228         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13229       else if (i <= 0xff)
13230         op = DW_OP_const1u;
13231       else if (i <= 0xffff)
13232         op = DW_OP_const2u;
13233       else if (HOST_BITS_PER_WIDE_INT == 32
13234                || i <= 0xffffffff)
13235         op = DW_OP_const4u;
13236       else
13237         op = DW_OP_constu;
13238     }
13239   else
13240     {
13241       if (i >= -0x80)
13242         op = DW_OP_const1s;
13243       else if (i >= -0x8000)
13244         op = DW_OP_const2s;
13245       else if (HOST_BITS_PER_WIDE_INT == 32
13246                || i >= -0x80000000)
13247         op = DW_OP_const4s;
13248       else
13249         op = DW_OP_consts;
13250     }
13251
13252   return new_loc_descr (op, i, 0);
13253 }
13254
13255 /* Return loc description representing "address" of integer value.
13256    This can appear only as toplevel expression.  */
13257
13258 static dw_loc_descr_ref
13259 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13260 {
13261   int litsize;
13262   dw_loc_descr_ref loc_result = NULL;
13263
13264   if (!(dwarf_version >= 4 || !dwarf_strict))
13265     return NULL;
13266
13267   if (i >= 0)
13268     {
13269       if (i <= 31)
13270         litsize = 1;
13271       else if (i <= 0xff)
13272         litsize = 2;
13273       else if (i <= 0xffff)
13274         litsize = 3;
13275       else if (HOST_BITS_PER_WIDE_INT == 32
13276                || i <= 0xffffffff)
13277         litsize = 5;
13278       else
13279         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13280     }
13281   else
13282     {
13283       if (i >= -0x80)
13284         litsize = 2;
13285       else if (i >= -0x8000)
13286         litsize = 3;
13287       else if (HOST_BITS_PER_WIDE_INT == 32
13288                || i >= -0x80000000)
13289         litsize = 5;
13290       else
13291         litsize = 1 + size_of_sleb128 (i);
13292     }
13293   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13294      is more compact.  For DW_OP_stack_value we need:
13295      litsize + 1 (DW_OP_stack_value)
13296      and for DW_OP_implicit_value:
13297      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13298   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13299     {
13300       loc_result = int_loc_descriptor (i);
13301       add_loc_descr (&loc_result,
13302                      new_loc_descr (DW_OP_stack_value, 0, 0));
13303       return loc_result;
13304     }
13305
13306   loc_result = new_loc_descr (DW_OP_implicit_value,
13307                               size, 0);
13308   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13309   loc_result->dw_loc_oprnd2.v.val_int = i;
13310   return loc_result;
13311 }
13312
13313 /* Return a location descriptor that designates a base+offset location.  */
13314
13315 static dw_loc_descr_ref
13316 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13317                  enum var_init_status initialized)
13318 {
13319   unsigned int regno;
13320   dw_loc_descr_ref result;
13321   dw_fde_ref fde = current_fde ();
13322
13323   /* We only use "frame base" when we're sure we're talking about the
13324      post-prologue local stack frame.  We do this by *not* running
13325      register elimination until this point, and recognizing the special
13326      argument pointer and soft frame pointer rtx's.  */
13327   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13328     {
13329       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13330
13331       if (elim != reg)
13332         {
13333           if (GET_CODE (elim) == PLUS)
13334             {
13335               offset += INTVAL (XEXP (elim, 1));
13336               elim = XEXP (elim, 0);
13337             }
13338           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13339                        && (elim == hard_frame_pointer_rtx
13340                            || elim == stack_pointer_rtx))
13341                       || elim == (frame_pointer_needed
13342                                   ? hard_frame_pointer_rtx
13343                                   : stack_pointer_rtx));
13344
13345           /* If drap register is used to align stack, use frame
13346              pointer + offset to access stack variables.  If stack
13347              is aligned without drap, use stack pointer + offset to
13348              access stack variables.  */
13349           if (crtl->stack_realign_tried
13350               && reg == frame_pointer_rtx)
13351             {
13352               int base_reg
13353                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13354                                       ? HARD_FRAME_POINTER_REGNUM
13355                                       : STACK_POINTER_REGNUM);
13356               return new_reg_loc_descr (base_reg, offset);
13357             }
13358
13359           offset += frame_pointer_fb_offset;
13360           return new_loc_descr (DW_OP_fbreg, offset, 0);
13361         }
13362     }
13363   else if (!optimize
13364            && fde
13365            && (fde->drap_reg == REGNO (reg)
13366                || fde->vdrap_reg == REGNO (reg)))
13367     {
13368       /* Use cfa+offset to represent the location of arguments passed
13369          on the stack when drap is used to align stack.
13370          Only do this when not optimizing, for optimized code var-tracking
13371          is supposed to track where the arguments live and the register
13372          used as vdrap or drap in some spot might be used for something
13373          else in other part of the routine.  */
13374       return new_loc_descr (DW_OP_fbreg, offset, 0);
13375     }
13376
13377   regno = dbx_reg_number (reg);
13378   if (regno <= 31)
13379     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13380                             offset, 0);
13381   else
13382     result = new_loc_descr (DW_OP_bregx, regno, offset);
13383
13384   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13385     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13386
13387   return result;
13388 }
13389
13390 /* Return true if this RTL expression describes a base+offset calculation.  */
13391
13392 static inline int
13393 is_based_loc (const_rtx rtl)
13394 {
13395   return (GET_CODE (rtl) == PLUS
13396           && ((REG_P (XEXP (rtl, 0))
13397                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13398                && CONST_INT_P (XEXP (rtl, 1)))));
13399 }
13400
13401 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13402    failed.  */
13403
13404 static dw_loc_descr_ref
13405 tls_mem_loc_descriptor (rtx mem)
13406 {
13407   tree base;
13408   dw_loc_descr_ref loc_result;
13409
13410   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13411     return NULL;
13412
13413   base = get_base_address (MEM_EXPR (mem));
13414   if (base == NULL
13415       || TREE_CODE (base) != VAR_DECL
13416       || !DECL_THREAD_LOCAL_P (base))
13417     return NULL;
13418
13419   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13420   if (loc_result == NULL)
13421     return NULL;
13422
13423   if (INTVAL (MEM_OFFSET (mem)))
13424     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13425
13426   return loc_result;
13427 }
13428
13429 /* Output debug info about reason why we failed to expand expression as dwarf
13430    expression.  */
13431
13432 static void
13433 expansion_failed (tree expr, rtx rtl, char const *reason)
13434 {
13435   if (dump_file && (dump_flags & TDF_DETAILS))
13436     {
13437       fprintf (dump_file, "Failed to expand as dwarf: ");
13438       if (expr)
13439         print_generic_expr (dump_file, expr, dump_flags);
13440       if (rtl)
13441         {
13442           fprintf (dump_file, "\n");
13443           print_rtl (dump_file, rtl);
13444         }
13445       fprintf (dump_file, "\nReason: %s\n", reason);
13446     }
13447 }
13448
13449 /* Helper function for const_ok_for_output, called either directly
13450    or via for_each_rtx.  */
13451
13452 static int
13453 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13454 {
13455   rtx rtl = *rtlp;
13456
13457   if (GET_CODE (rtl) == UNSPEC)
13458     {
13459       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13460          we can't express it in the debug info.  */
13461 #ifdef ENABLE_CHECKING
13462       inform (current_function_decl
13463               ? DECL_SOURCE_LOCATION (current_function_decl)
13464               : UNKNOWN_LOCATION,
13465               "non-delegitimized UNSPEC %d found in variable location",
13466               XINT (rtl, 1));
13467 #endif
13468       expansion_failed (NULL_TREE, rtl,
13469                         "UNSPEC hasn't been delegitimized.\n");
13470       return 1;
13471     }
13472
13473   if (GET_CODE (rtl) != SYMBOL_REF)
13474     return 0;
13475
13476   if (CONSTANT_POOL_ADDRESS_P (rtl))
13477     {
13478       bool marked;
13479       get_pool_constant_mark (rtl, &marked);
13480       /* If all references to this pool constant were optimized away,
13481          it was not output and thus we can't represent it.  */
13482       if (!marked)
13483         {
13484           expansion_failed (NULL_TREE, rtl,
13485                             "Constant was removed from constant pool.\n");
13486           return 1;
13487         }
13488     }
13489
13490   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13491     return 1;
13492
13493   /* Avoid references to external symbols in debug info, on several targets
13494      the linker might even refuse to link when linking a shared library,
13495      and in many other cases the relocations for .debug_info/.debug_loc are
13496      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13497      to be defined within the same shared library or executable are fine.  */
13498   if (SYMBOL_REF_EXTERNAL_P (rtl))
13499     {
13500       tree decl = SYMBOL_REF_DECL (rtl);
13501
13502       if (decl == NULL || !targetm.binds_local_p (decl))
13503         {
13504           expansion_failed (NULL_TREE, rtl,
13505                             "Symbol not defined in current TU.\n");
13506           return 1;
13507         }
13508     }
13509
13510   return 0;
13511 }
13512
13513 /* Return true if constant RTL can be emitted in DW_OP_addr or
13514    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13515    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13516
13517 static bool
13518 const_ok_for_output (rtx rtl)
13519 {
13520   if (GET_CODE (rtl) == SYMBOL_REF)
13521     return const_ok_for_output_1 (&rtl, NULL) == 0;
13522
13523   if (GET_CODE (rtl) == CONST)
13524     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13525
13526   return true;
13527 }
13528
13529 /* The following routine converts the RTL for a variable or parameter
13530    (resident in memory) into an equivalent Dwarf representation of a
13531    mechanism for getting the address of that same variable onto the top of a
13532    hypothetical "address evaluation" stack.
13533
13534    When creating memory location descriptors, we are effectively transforming
13535    the RTL for a memory-resident object into its Dwarf postfix expression
13536    equivalent.  This routine recursively descends an RTL tree, turning
13537    it into Dwarf postfix code as it goes.
13538
13539    MODE is the mode of the memory reference, needed to handle some
13540    autoincrement addressing modes.
13541
13542    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13543    location list for RTL.
13544
13545    Return 0 if we can't represent the location.  */
13546
13547 static dw_loc_descr_ref
13548 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13549                     enum var_init_status initialized)
13550 {
13551   dw_loc_descr_ref mem_loc_result = NULL;
13552   enum dwarf_location_atom op;
13553   dw_loc_descr_ref op0, op1;
13554
13555   /* Note that for a dynamically sized array, the location we will generate a
13556      description of here will be the lowest numbered location which is
13557      actually within the array.  That's *not* necessarily the same as the
13558      zeroth element of the array.  */
13559
13560   rtl = targetm.delegitimize_address (rtl);
13561
13562   switch (GET_CODE (rtl))
13563     {
13564     case POST_INC:
13565     case POST_DEC:
13566     case POST_MODIFY:
13567       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13568
13569     case SUBREG:
13570       /* The case of a subreg may arise when we have a local (register)
13571          variable or a formal (register) parameter which doesn't quite fill
13572          up an entire register.  For now, just assume that it is
13573          legitimate to make the Dwarf info refer to the whole register which
13574          contains the given subreg.  */
13575       if (!subreg_lowpart_p (rtl))
13576         break;
13577       rtl = SUBREG_REG (rtl);
13578       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13579         break;
13580       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13581         break;
13582       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13583       break;
13584
13585     case REG:
13586       /* Whenever a register number forms a part of the description of the
13587          method for calculating the (dynamic) address of a memory resident
13588          object, DWARF rules require the register number be referred to as
13589          a "base register".  This distinction is not based in any way upon
13590          what category of register the hardware believes the given register
13591          belongs to.  This is strictly DWARF terminology we're dealing with
13592          here. Note that in cases where the location of a memory-resident
13593          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13594          OP_CONST (0)) the actual DWARF location descriptor that we generate
13595          may just be OP_BASEREG (basereg).  This may look deceptively like
13596          the object in question was allocated to a register (rather than in
13597          memory) so DWARF consumers need to be aware of the subtle
13598          distinction between OP_REG and OP_BASEREG.  */
13599       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13600         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13601       else if (stack_realign_drap
13602                && crtl->drap_reg
13603                && crtl->args.internal_arg_pointer == rtl
13604                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13605         {
13606           /* If RTL is internal_arg_pointer, which has been optimized
13607              out, use DRAP instead.  */
13608           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13609                                             VAR_INIT_STATUS_INITIALIZED);
13610         }
13611       break;
13612
13613     case SIGN_EXTEND:
13614     case ZERO_EXTEND:
13615       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13616                                 VAR_INIT_STATUS_INITIALIZED);
13617       if (op0 == 0)
13618         break;
13619       else
13620         {
13621           int shift = DWARF2_ADDR_SIZE
13622                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13623           shift *= BITS_PER_UNIT;
13624           if (GET_CODE (rtl) == SIGN_EXTEND)
13625             op = DW_OP_shra;
13626           else
13627             op = DW_OP_shr;
13628           mem_loc_result = op0;
13629           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13630           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13631           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13632           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13633         }
13634       break;
13635
13636     case MEM:
13637       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13638                                            VAR_INIT_STATUS_INITIALIZED);
13639       if (mem_loc_result == NULL)
13640         mem_loc_result = tls_mem_loc_descriptor (rtl);
13641       if (mem_loc_result != 0)
13642         {
13643           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13644             {
13645               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13646               return 0;
13647             }
13648           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13649             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13650           else
13651             add_loc_descr (&mem_loc_result,
13652                            new_loc_descr (DW_OP_deref_size,
13653                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13654         }
13655       else
13656         {
13657           rtx new_rtl = avoid_constant_pool_reference (rtl);
13658           if (new_rtl != rtl)
13659             return mem_loc_descriptor (new_rtl, mode, initialized);
13660         }
13661       break;
13662
13663     case LO_SUM:
13664          rtl = XEXP (rtl, 1);
13665
13666       /* ... fall through ...  */
13667
13668     case LABEL_REF:
13669       /* Some ports can transform a symbol ref into a label ref, because
13670          the symbol ref is too far away and has to be dumped into a constant
13671          pool.  */
13672     case CONST:
13673     case SYMBOL_REF:
13674       if (GET_CODE (rtl) == SYMBOL_REF
13675           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13676         {
13677           dw_loc_descr_ref temp;
13678
13679           /* If this is not defined, we have no way to emit the data.  */
13680           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13681             break;
13682
13683           /* We used to emit DW_OP_addr here, but that's wrong, since
13684              DW_OP_addr should be relocated by the debug info consumer,
13685              while DW_OP_GNU_push_tls_address operand should not.  */
13686           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13687                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13688           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13689           temp->dw_loc_oprnd1.v.val_addr = rtl;
13690           temp->dtprel = true;
13691
13692           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13693           add_loc_descr (&mem_loc_result, temp);
13694
13695           break;
13696         }
13697
13698       if (!const_ok_for_output (rtl))
13699         break;
13700
13701     symref:
13702       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13703       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13704       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13705       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13706       break;
13707
13708     case CONCAT:
13709     case CONCATN:
13710     case VAR_LOCATION:
13711     case DEBUG_IMPLICIT_PTR:
13712       expansion_failed (NULL_TREE, rtl,
13713                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13714       return 0;
13715
13716     case PRE_MODIFY:
13717       /* Extract the PLUS expression nested inside and fall into
13718          PLUS code below.  */
13719       rtl = XEXP (rtl, 1);
13720       goto plus;
13721
13722     case PRE_INC:
13723     case PRE_DEC:
13724       /* Turn these into a PLUS expression and fall into the PLUS code
13725          below.  */
13726       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13727                           GEN_INT (GET_CODE (rtl) == PRE_INC
13728                                    ? GET_MODE_UNIT_SIZE (mode)
13729                                    : -GET_MODE_UNIT_SIZE (mode)));
13730
13731       /* ... fall through ...  */
13732
13733     case PLUS:
13734     plus:
13735       if (is_based_loc (rtl))
13736         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13737                                           INTVAL (XEXP (rtl, 1)),
13738                                           VAR_INIT_STATUS_INITIALIZED);
13739       else
13740         {
13741           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13742                                                VAR_INIT_STATUS_INITIALIZED);
13743           if (mem_loc_result == 0)
13744             break;
13745
13746           if (CONST_INT_P (XEXP (rtl, 1)))
13747             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13748           else
13749             {
13750               dw_loc_descr_ref mem_loc_result2
13751                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13752                                       VAR_INIT_STATUS_INITIALIZED);
13753               if (mem_loc_result2 == 0)
13754                 break;
13755               add_loc_descr (&mem_loc_result, mem_loc_result2);
13756               add_loc_descr (&mem_loc_result,
13757                              new_loc_descr (DW_OP_plus, 0, 0));
13758             }
13759         }
13760       break;
13761
13762     /* If a pseudo-reg is optimized away, it is possible for it to
13763        be replaced with a MEM containing a multiply or shift.  */
13764     case MINUS:
13765       op = DW_OP_minus;
13766       goto do_binop;
13767
13768     case MULT:
13769       op = DW_OP_mul;
13770       goto do_binop;
13771
13772     case DIV:
13773       op = DW_OP_div;
13774       goto do_binop;
13775
13776     case UMOD:
13777       op = DW_OP_mod;
13778       goto do_binop;
13779
13780     case ASHIFT:
13781       op = DW_OP_shl;
13782       goto do_binop;
13783
13784     case ASHIFTRT:
13785       op = DW_OP_shra;
13786       goto do_binop;
13787
13788     case LSHIFTRT:
13789       op = DW_OP_shr;
13790       goto do_binop;
13791
13792     case AND:
13793       op = DW_OP_and;
13794       goto do_binop;
13795
13796     case IOR:
13797       op = DW_OP_or;
13798       goto do_binop;
13799
13800     case XOR:
13801       op = DW_OP_xor;
13802       goto do_binop;
13803
13804     do_binop:
13805       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13806                                 VAR_INIT_STATUS_INITIALIZED);
13807       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13808                                 VAR_INIT_STATUS_INITIALIZED);
13809
13810       if (op0 == 0 || op1 == 0)
13811         break;
13812
13813       mem_loc_result = op0;
13814       add_loc_descr (&mem_loc_result, op1);
13815       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13816       break;
13817
13818     case MOD:
13819       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13820                                 VAR_INIT_STATUS_INITIALIZED);
13821       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13822                                 VAR_INIT_STATUS_INITIALIZED);
13823
13824       if (op0 == 0 || op1 == 0)
13825         break;
13826
13827       mem_loc_result = op0;
13828       add_loc_descr (&mem_loc_result, op1);
13829       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13830       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13831       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13832       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13833       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13834       break;
13835
13836     case NOT:
13837       op = DW_OP_not;
13838       goto do_unop;
13839
13840     case ABS:
13841       op = DW_OP_abs;
13842       goto do_unop;
13843
13844     case NEG:
13845       op = DW_OP_neg;
13846       goto do_unop;
13847
13848     do_unop:
13849       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13850                                 VAR_INIT_STATUS_INITIALIZED);
13851
13852       if (op0 == 0)
13853         break;
13854
13855       mem_loc_result = op0;
13856       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13857       break;
13858
13859     case CONST_INT:
13860       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13861       break;
13862
13863     case EQ:
13864       op = DW_OP_eq;
13865       goto do_scompare;
13866
13867     case GE:
13868       op = DW_OP_ge;
13869       goto do_scompare;
13870
13871     case GT:
13872       op = DW_OP_gt;
13873       goto do_scompare;
13874
13875     case LE:
13876       op = DW_OP_le;
13877       goto do_scompare;
13878
13879     case LT:
13880       op = DW_OP_lt;
13881       goto do_scompare;
13882
13883     case NE:
13884       op = DW_OP_ne;
13885       goto do_scompare;
13886
13887     do_scompare:
13888       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13889           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13890         break;
13891       else
13892         {
13893           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13894
13895           if (op_mode == VOIDmode)
13896             op_mode = GET_MODE (XEXP (rtl, 1));
13897           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13898             break;
13899
13900           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13901                                     VAR_INIT_STATUS_INITIALIZED);
13902           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13903                                     VAR_INIT_STATUS_INITIALIZED);
13904
13905           if (op0 == 0 || op1 == 0)
13906             break;
13907
13908           if (op_mode != VOIDmode
13909               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13910             {
13911               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13912               shift *= BITS_PER_UNIT;
13913               /* For eq/ne, if the operands are known to be zero-extended,
13914                  there is no need to do the fancy shifting up.  */
13915               if (op == DW_OP_eq || op == DW_OP_ne)
13916                 {
13917                   dw_loc_descr_ref last0, last1;
13918                   for (last0 = op0;
13919                        last0->dw_loc_next != NULL;
13920                        last0 = last0->dw_loc_next)
13921                     ;
13922                   for (last1 = op1;
13923                        last1->dw_loc_next != NULL;
13924                        last1 = last1->dw_loc_next)
13925                     ;
13926                   /* deref_size zero extends, and for constants we can check
13927                      whether they are zero extended or not.  */
13928                   if (((last0->dw_loc_opc == DW_OP_deref_size
13929                         && last0->dw_loc_oprnd1.v.val_int
13930                            <= GET_MODE_SIZE (op_mode))
13931                        || (CONST_INT_P (XEXP (rtl, 0))
13932                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13933                                == (INTVAL (XEXP (rtl, 0))
13934                                    & GET_MODE_MASK (op_mode))))
13935                       && ((last1->dw_loc_opc == DW_OP_deref_size
13936                            && last1->dw_loc_oprnd1.v.val_int
13937                               <= GET_MODE_SIZE (op_mode))
13938                           || (CONST_INT_P (XEXP (rtl, 1))
13939                               && (unsigned HOST_WIDE_INT)
13940                                  INTVAL (XEXP (rtl, 1))
13941                                  == (INTVAL (XEXP (rtl, 1))
13942                                      & GET_MODE_MASK (op_mode)))))
13943                     goto do_compare;
13944                 }
13945               add_loc_descr (&op0, int_loc_descriptor (shift));
13946               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13947               if (CONST_INT_P (XEXP (rtl, 1)))
13948                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13949               else
13950                 {
13951                   add_loc_descr (&op1, int_loc_descriptor (shift));
13952                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13953                 }
13954             }
13955         }
13956
13957     do_compare:
13958       mem_loc_result = op0;
13959       add_loc_descr (&mem_loc_result, op1);
13960       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13961       if (STORE_FLAG_VALUE != 1)
13962         {
13963           add_loc_descr (&mem_loc_result,
13964                          int_loc_descriptor (STORE_FLAG_VALUE));
13965           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13966         }
13967       break;
13968
13969     case GEU:
13970       op = DW_OP_ge;
13971       goto do_ucompare;
13972
13973     case GTU:
13974       op = DW_OP_gt;
13975       goto do_ucompare;
13976
13977     case LEU:
13978       op = DW_OP_le;
13979       goto do_ucompare;
13980
13981     case LTU:
13982       op = DW_OP_lt;
13983       goto do_ucompare;
13984
13985     do_ucompare:
13986       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13987           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13988         break;
13989       else
13990         {
13991           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13992
13993           if (op_mode == VOIDmode)
13994             op_mode = GET_MODE (XEXP (rtl, 1));
13995           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13996             break;
13997
13998           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13999                                     VAR_INIT_STATUS_INITIALIZED);
14000           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14001                                     VAR_INIT_STATUS_INITIALIZED);
14002
14003           if (op0 == 0 || op1 == 0)
14004             break;
14005
14006           if (op_mode != VOIDmode
14007               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14008             {
14009               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14010               dw_loc_descr_ref last0, last1;
14011               for (last0 = op0;
14012                    last0->dw_loc_next != NULL;
14013                    last0 = last0->dw_loc_next)
14014                 ;
14015               for (last1 = op1;
14016                    last1->dw_loc_next != NULL;
14017                    last1 = last1->dw_loc_next)
14018                 ;
14019               if (CONST_INT_P (XEXP (rtl, 0)))
14020                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14021               /* deref_size zero extends, so no need to mask it again.  */
14022               else if (last0->dw_loc_opc != DW_OP_deref_size
14023                        || last0->dw_loc_oprnd1.v.val_int
14024                           > GET_MODE_SIZE (op_mode))
14025                 {
14026                   add_loc_descr (&op0, int_loc_descriptor (mask));
14027                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14028                 }
14029               if (CONST_INT_P (XEXP (rtl, 1)))
14030                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14031               /* deref_size zero extends, so no need to mask it again.  */
14032               else if (last1->dw_loc_opc != DW_OP_deref_size
14033                        || last1->dw_loc_oprnd1.v.val_int
14034                           > GET_MODE_SIZE (op_mode))
14035                 {
14036                   add_loc_descr (&op1, int_loc_descriptor (mask));
14037                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14038                 }
14039             }
14040           else
14041             {
14042               HOST_WIDE_INT bias = 1;
14043               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14044               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14045               if (CONST_INT_P (XEXP (rtl, 1)))
14046                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14047                                           + INTVAL (XEXP (rtl, 1)));
14048               else
14049                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14050                                                     bias, 0));
14051             }
14052         }
14053       goto do_compare;
14054
14055     case SMIN:
14056     case SMAX:
14057     case UMIN:
14058     case UMAX:
14059       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14060           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14061           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14062         break;
14063
14064       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14065                                 VAR_INIT_STATUS_INITIALIZED);
14066       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14067                                 VAR_INIT_STATUS_INITIALIZED);
14068
14069       if (op0 == 0 || op1 == 0)
14070         break;
14071
14072       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14073       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14074       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14075       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14076         {
14077           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14078             {
14079               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14080               add_loc_descr (&op0, int_loc_descriptor (mask));
14081               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14082               add_loc_descr (&op1, int_loc_descriptor (mask));
14083               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14084             }
14085           else
14086             {
14087               HOST_WIDE_INT bias = 1;
14088               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14089               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14090               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14091             }
14092         }
14093       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14094         {
14095           int shift = DWARF2_ADDR_SIZE
14096                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14097           shift *= BITS_PER_UNIT;
14098           add_loc_descr (&op0, int_loc_descriptor (shift));
14099           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14100           add_loc_descr (&op1, int_loc_descriptor (shift));
14101           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14102         }
14103
14104       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14105         op = DW_OP_lt;
14106       else
14107         op = DW_OP_gt;
14108       mem_loc_result = op0;
14109       add_loc_descr (&mem_loc_result, op1);
14110       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14111       {
14112         dw_loc_descr_ref bra_node, drop_node;
14113
14114         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14115         add_loc_descr (&mem_loc_result, bra_node);
14116         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14117         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14118         add_loc_descr (&mem_loc_result, drop_node);
14119         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14120         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14121       }
14122       break;
14123
14124     case ZERO_EXTRACT:
14125     case SIGN_EXTRACT:
14126       if (CONST_INT_P (XEXP (rtl, 1))
14127           && CONST_INT_P (XEXP (rtl, 2))
14128           && ((unsigned) INTVAL (XEXP (rtl, 1))
14129               + (unsigned) INTVAL (XEXP (rtl, 2))
14130               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14131           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14132           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14133         {
14134           int shift, size;
14135           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14136                                     VAR_INIT_STATUS_INITIALIZED);
14137           if (op0 == 0)
14138             break;
14139           if (GET_CODE (rtl) == SIGN_EXTRACT)
14140             op = DW_OP_shra;
14141           else
14142             op = DW_OP_shr;
14143           mem_loc_result = op0;
14144           size = INTVAL (XEXP (rtl, 1));
14145           shift = INTVAL (XEXP (rtl, 2));
14146           if (BITS_BIG_ENDIAN)
14147             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14148                     - shift - size;
14149           if (shift + size != (int) DWARF2_ADDR_SIZE)
14150             {
14151               add_loc_descr (&mem_loc_result,
14152                              int_loc_descriptor (DWARF2_ADDR_SIZE
14153                                                  - shift - size));
14154               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14155             }
14156           if (size != (int) DWARF2_ADDR_SIZE)
14157             {
14158               add_loc_descr (&mem_loc_result,
14159                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14160               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14161             }
14162         }
14163       break;
14164
14165     case IF_THEN_ELSE:
14166       {
14167         dw_loc_descr_ref op2, bra_node, drop_node;
14168         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14169                                   VAR_INIT_STATUS_INITIALIZED);
14170         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14171                                   VAR_INIT_STATUS_INITIALIZED);
14172         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14173                                   VAR_INIT_STATUS_INITIALIZED);
14174         if (op0 == NULL || op1 == NULL || op2 == NULL)
14175           break;
14176
14177         mem_loc_result = op1;
14178         add_loc_descr (&mem_loc_result, op2);
14179         add_loc_descr (&mem_loc_result, op0);
14180         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14181         add_loc_descr (&mem_loc_result, bra_node);
14182         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14183         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14184         add_loc_descr (&mem_loc_result, drop_node);
14185         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14186         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14187       }
14188       break;
14189
14190     case COMPARE:
14191     case ROTATE:
14192     case ROTATERT:
14193     case TRUNCATE:
14194       /* In theory, we could implement the above.  */
14195       /* DWARF cannot represent the unsigned compare operations
14196          natively.  */
14197     case SS_MULT:
14198     case US_MULT:
14199     case SS_DIV:
14200     case US_DIV:
14201     case SS_PLUS:
14202     case US_PLUS:
14203     case SS_MINUS:
14204     case US_MINUS:
14205     case SS_NEG:
14206     case US_NEG:
14207     case SS_ABS:
14208     case SS_ASHIFT:
14209     case US_ASHIFT:
14210     case SS_TRUNCATE:
14211     case US_TRUNCATE:
14212     case UDIV:
14213     case UNORDERED:
14214     case ORDERED:
14215     case UNEQ:
14216     case UNGE:
14217     case UNGT:
14218     case UNLE:
14219     case UNLT:
14220     case LTGT:
14221     case FLOAT_EXTEND:
14222     case FLOAT_TRUNCATE:
14223     case FLOAT:
14224     case UNSIGNED_FLOAT:
14225     case FIX:
14226     case UNSIGNED_FIX:
14227     case FRACT_CONVERT:
14228     case UNSIGNED_FRACT_CONVERT:
14229     case SAT_FRACT:
14230     case UNSIGNED_SAT_FRACT:
14231     case SQRT:
14232     case BSWAP:
14233     case FFS:
14234     case CLZ:
14235     case CTZ:
14236     case POPCOUNT:
14237     case PARITY:
14238     case ASM_OPERANDS:
14239     case VEC_MERGE:
14240     case VEC_SELECT:
14241     case VEC_CONCAT:
14242     case VEC_DUPLICATE:
14243     case UNSPEC:
14244     case HIGH:
14245       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14246          can't express it in the debug info.  This can happen e.g. with some
14247          TLS UNSPECs.  */
14248       break;
14249
14250     case CONST_STRING:
14251       resolve_one_addr (&rtl, NULL);
14252       goto symref;
14253
14254     default:
14255 #ifdef ENABLE_CHECKING
14256       print_rtl (stderr, rtl);
14257       gcc_unreachable ();
14258 #else
14259       break;
14260 #endif
14261     }
14262
14263   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14264     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14265
14266   return mem_loc_result;
14267 }
14268
14269 /* Return a descriptor that describes the concatenation of two locations.
14270    This is typically a complex variable.  */
14271
14272 static dw_loc_descr_ref
14273 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14274 {
14275   dw_loc_descr_ref cc_loc_result = NULL;
14276   dw_loc_descr_ref x0_ref
14277     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14278   dw_loc_descr_ref x1_ref
14279     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14280
14281   if (x0_ref == 0 || x1_ref == 0)
14282     return 0;
14283
14284   cc_loc_result = x0_ref;
14285   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14286
14287   add_loc_descr (&cc_loc_result, x1_ref);
14288   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14289
14290   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14291     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14292
14293   return cc_loc_result;
14294 }
14295
14296 /* Return a descriptor that describes the concatenation of N
14297    locations.  */
14298
14299 static dw_loc_descr_ref
14300 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14301 {
14302   unsigned int i;
14303   dw_loc_descr_ref cc_loc_result = NULL;
14304   unsigned int n = XVECLEN (concatn, 0);
14305
14306   for (i = 0; i < n; ++i)
14307     {
14308       dw_loc_descr_ref ref;
14309       rtx x = XVECEXP (concatn, 0, i);
14310
14311       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14312       if (ref == NULL)
14313         return NULL;
14314
14315       add_loc_descr (&cc_loc_result, ref);
14316       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14317     }
14318
14319   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14320     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14321
14322   return cc_loc_result;
14323 }
14324
14325 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14326    for DEBUG_IMPLICIT_PTR RTL.  */
14327
14328 static dw_loc_descr_ref
14329 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14330 {
14331   dw_loc_descr_ref ret;
14332   dw_die_ref ref;
14333
14334   if (dwarf_strict)
14335     return NULL;
14336   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14337               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14338               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14339   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14340   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14341   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14342   if (ref)
14343     {
14344       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14345       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14346       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14347     }
14348   else
14349     {
14350       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14351       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14352     }
14353   return ret;
14354 }
14355
14356 /* Output a proper Dwarf location descriptor for a variable or parameter
14357    which is either allocated in a register or in a memory location.  For a
14358    register, we just generate an OP_REG and the register number.  For a
14359    memory location we provide a Dwarf postfix expression describing how to
14360    generate the (dynamic) address of the object onto the address stack.
14361
14362    MODE is mode of the decl if this loc_descriptor is going to be used in
14363    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14364    allowed, VOIDmode otherwise.
14365
14366    If we don't know how to describe it, return 0.  */
14367
14368 static dw_loc_descr_ref
14369 loc_descriptor (rtx rtl, enum machine_mode mode,
14370                 enum var_init_status initialized)
14371 {
14372   dw_loc_descr_ref loc_result = NULL;
14373
14374   switch (GET_CODE (rtl))
14375     {
14376     case SUBREG:
14377       /* The case of a subreg may arise when we have a local (register)
14378          variable or a formal (register) parameter which doesn't quite fill
14379          up an entire register.  For now, just assume that it is
14380          legitimate to make the Dwarf info refer to the whole register which
14381          contains the given subreg.  */
14382       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14383       break;
14384
14385     case REG:
14386       loc_result = reg_loc_descriptor (rtl, initialized);
14387       break;
14388
14389     case MEM:
14390       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14391                                        initialized);
14392       if (loc_result == NULL)
14393         loc_result = tls_mem_loc_descriptor (rtl);
14394       if (loc_result == NULL)
14395         {
14396           rtx new_rtl = avoid_constant_pool_reference (rtl);
14397           if (new_rtl != rtl)
14398             loc_result = loc_descriptor (new_rtl, mode, initialized);
14399         }
14400       break;
14401
14402     case CONCAT:
14403       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14404                                           initialized);
14405       break;
14406
14407     case CONCATN:
14408       loc_result = concatn_loc_descriptor (rtl, initialized);
14409       break;
14410
14411     case VAR_LOCATION:
14412       /* Single part.  */
14413       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14414         {
14415           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14416           if (GET_CODE (loc) == EXPR_LIST)
14417             loc = XEXP (loc, 0);
14418           loc_result = loc_descriptor (loc, mode, initialized);
14419           break;
14420         }
14421
14422       rtl = XEXP (rtl, 1);
14423       /* FALLTHRU */
14424
14425     case PARALLEL:
14426       {
14427         rtvec par_elems = XVEC (rtl, 0);
14428         int num_elem = GET_NUM_ELEM (par_elems);
14429         enum machine_mode mode;
14430         int i;
14431
14432         /* Create the first one, so we have something to add to.  */
14433         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14434                                      VOIDmode, initialized);
14435         if (loc_result == NULL)
14436           return NULL;
14437         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14438         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14439         for (i = 1; i < num_elem; i++)
14440           {
14441             dw_loc_descr_ref temp;
14442
14443             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14444                                    VOIDmode, initialized);
14445             if (temp == NULL)
14446               return NULL;
14447             add_loc_descr (&loc_result, temp);
14448             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14449             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14450           }
14451       }
14452       break;
14453
14454     case CONST_INT:
14455       if (mode != VOIDmode && mode != BLKmode)
14456         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14457                                                     INTVAL (rtl));
14458       break;
14459
14460     case CONST_DOUBLE:
14461       if (mode == VOIDmode)
14462         mode = GET_MODE (rtl);
14463
14464       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14465         {
14466           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14467
14468           /* Note that a CONST_DOUBLE rtx could represent either an integer
14469              or a floating-point constant.  A CONST_DOUBLE is used whenever
14470              the constant requires more than one word in order to be
14471              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14472           loc_result = new_loc_descr (DW_OP_implicit_value,
14473                                       GET_MODE_SIZE (mode), 0);
14474           if (SCALAR_FLOAT_MODE_P (mode))
14475             {
14476               unsigned int length = GET_MODE_SIZE (mode);
14477               unsigned char *array
14478                   = (unsigned char*) ggc_alloc_atomic (length);
14479
14480               insert_float (rtl, array);
14481               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14482               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14483               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14484               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14485             }
14486           else
14487             {
14488               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14489               loc_result->dw_loc_oprnd2.v.val_double
14490                 = rtx_to_double_int (rtl);
14491             }
14492         }
14493       break;
14494
14495     case CONST_VECTOR:
14496       if (mode == VOIDmode)
14497         mode = GET_MODE (rtl);
14498
14499       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14500         {
14501           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14502           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14503           unsigned char *array = (unsigned char *)
14504             ggc_alloc_atomic (length * elt_size);
14505           unsigned int i;
14506           unsigned char *p;
14507
14508           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14509           switch (GET_MODE_CLASS (mode))
14510             {
14511             case MODE_VECTOR_INT:
14512               for (i = 0, p = array; i < length; i++, p += elt_size)
14513                 {
14514                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14515                   double_int val = rtx_to_double_int (elt);
14516
14517                   if (elt_size <= sizeof (HOST_WIDE_INT))
14518                     insert_int (double_int_to_shwi (val), elt_size, p);
14519                   else
14520                     {
14521                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14522                       insert_double (val, p);
14523                     }
14524                 }
14525               break;
14526
14527             case MODE_VECTOR_FLOAT:
14528               for (i = 0, p = array; i < length; i++, p += elt_size)
14529                 {
14530                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14531                   insert_float (elt, p);
14532                 }
14533               break;
14534
14535             default:
14536               gcc_unreachable ();
14537             }
14538
14539           loc_result = new_loc_descr (DW_OP_implicit_value,
14540                                       length * elt_size, 0);
14541           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14542           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14543           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14544           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14545         }
14546       break;
14547
14548     case CONST:
14549       if (mode == VOIDmode
14550           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14551           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14552           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14553         {
14554           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14555           break;
14556         }
14557       /* FALLTHROUGH */
14558     case SYMBOL_REF:
14559       if (!const_ok_for_output (rtl))
14560         break;
14561     case LABEL_REF:
14562       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14563           && (dwarf_version >= 4 || !dwarf_strict))
14564         {
14565           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14566           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14567           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14568           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14569           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14570         }
14571       break;
14572
14573     case DEBUG_IMPLICIT_PTR:
14574       loc_result = implicit_ptr_descriptor (rtl, 0);
14575       break;
14576
14577     case PLUS:
14578       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14579           && CONST_INT_P (XEXP (rtl, 1)))
14580         {
14581           loc_result
14582             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14583           break;
14584         }
14585       /* FALLTHRU */
14586     default:
14587       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14588           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14589           && (dwarf_version >= 4 || !dwarf_strict))
14590         {
14591           /* Value expression.  */
14592           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14593           if (loc_result)
14594             add_loc_descr (&loc_result,
14595                            new_loc_descr (DW_OP_stack_value, 0, 0));
14596         }
14597       break;
14598     }
14599
14600   return loc_result;
14601 }
14602
14603 /* We need to figure out what section we should use as the base for the
14604    address ranges where a given location is valid.
14605    1. If this particular DECL has a section associated with it, use that.
14606    2. If this function has a section associated with it, use that.
14607    3. Otherwise, use the text section.
14608    XXX: If you split a variable across multiple sections, we won't notice.  */
14609
14610 static const char *
14611 secname_for_decl (const_tree decl)
14612 {
14613   const char *secname;
14614
14615   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14616     {
14617       tree sectree = DECL_SECTION_NAME (decl);
14618       secname = TREE_STRING_POINTER (sectree);
14619     }
14620   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14621     {
14622       tree sectree = DECL_SECTION_NAME (current_function_decl);
14623       secname = TREE_STRING_POINTER (sectree);
14624     }
14625   else if (cfun && in_cold_section_p)
14626     secname = crtl->subsections.cold_section_label;
14627   else
14628     secname = text_section_label;
14629
14630   return secname;
14631 }
14632
14633 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14634
14635 static bool
14636 decl_by_reference_p (tree decl)
14637 {
14638   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14639            || TREE_CODE (decl) == VAR_DECL)
14640           && DECL_BY_REFERENCE (decl));
14641 }
14642
14643 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14644    for VARLOC.  */
14645
14646 static dw_loc_descr_ref
14647 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14648                enum var_init_status initialized)
14649 {
14650   int have_address = 0;
14651   dw_loc_descr_ref descr;
14652   enum machine_mode mode;
14653
14654   if (want_address != 2)
14655     {
14656       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14657       /* Single part.  */
14658       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14659         {
14660           varloc = PAT_VAR_LOCATION_LOC (varloc);
14661           if (GET_CODE (varloc) == EXPR_LIST)
14662             varloc = XEXP (varloc, 0);
14663           mode = GET_MODE (varloc);
14664           if (MEM_P (varloc))
14665             {
14666               rtx addr = XEXP (varloc, 0);
14667               descr = mem_loc_descriptor (addr, mode, initialized);
14668               if (descr)
14669                 have_address = 1;
14670               else
14671                 {
14672                   rtx x = avoid_constant_pool_reference (varloc);
14673                   if (x != varloc)
14674                     descr = mem_loc_descriptor (x, mode, initialized);
14675                 }
14676             }
14677           else
14678             descr = mem_loc_descriptor (varloc, mode, initialized);
14679         }
14680       else
14681         return 0;
14682     }
14683   else
14684     {
14685       if (GET_CODE (varloc) == VAR_LOCATION)
14686         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14687       else
14688         mode = DECL_MODE (loc);
14689       descr = loc_descriptor (varloc, mode, initialized);
14690       have_address = 1;
14691     }
14692
14693   if (!descr)
14694     return 0;
14695
14696   if (want_address == 2 && !have_address
14697       && (dwarf_version >= 4 || !dwarf_strict))
14698     {
14699       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14700         {
14701           expansion_failed (loc, NULL_RTX,
14702                             "DWARF address size mismatch");
14703           return 0;
14704         }
14705       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14706       have_address = 1;
14707     }
14708   /* Show if we can't fill the request for an address.  */
14709   if (want_address && !have_address)
14710     {
14711       expansion_failed (loc, NULL_RTX,
14712                         "Want address and only have value");
14713       return 0;
14714     }
14715
14716   /* If we've got an address and don't want one, dereference.  */
14717   if (!want_address && have_address)
14718     {
14719       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14720       enum dwarf_location_atom op;
14721
14722       if (size > DWARF2_ADDR_SIZE || size == -1)
14723         {
14724           expansion_failed (loc, NULL_RTX,
14725                             "DWARF address size mismatch");
14726           return 0;
14727         }
14728       else if (size == DWARF2_ADDR_SIZE)
14729         op = DW_OP_deref;
14730       else
14731         op = DW_OP_deref_size;
14732
14733       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14734     }
14735
14736   return descr;
14737 }
14738
14739 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14740    if it is not possible.  */
14741
14742 static dw_loc_descr_ref
14743 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14744 {
14745   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14746     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14747   else if (dwarf_version >= 3 || !dwarf_strict)
14748     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14749   else
14750     return NULL;
14751 }
14752
14753 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14754    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14755
14756 static dw_loc_descr_ref
14757 dw_sra_loc_expr (tree decl, rtx loc)
14758 {
14759   rtx p;
14760   unsigned int padsize = 0;
14761   dw_loc_descr_ref descr, *descr_tail;
14762   unsigned HOST_WIDE_INT decl_size;
14763   rtx varloc;
14764   enum var_init_status initialized;
14765
14766   if (DECL_SIZE (decl) == NULL
14767       || !host_integerp (DECL_SIZE (decl), 1))
14768     return NULL;
14769
14770   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14771   descr = NULL;
14772   descr_tail = &descr;
14773
14774   for (p = loc; p; p = XEXP (p, 1))
14775     {
14776       unsigned int bitsize = decl_piece_bitsize (p);
14777       rtx loc_note = *decl_piece_varloc_ptr (p);
14778       dw_loc_descr_ref cur_descr;
14779       dw_loc_descr_ref *tail, last = NULL;
14780       unsigned int opsize = 0;
14781
14782       if (loc_note == NULL_RTX
14783           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14784         {
14785           padsize += bitsize;
14786           continue;
14787         }
14788       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14789       varloc = NOTE_VAR_LOCATION (loc_note);
14790       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14791       if (cur_descr == NULL)
14792         {
14793           padsize += bitsize;
14794           continue;
14795         }
14796
14797       /* Check that cur_descr either doesn't use
14798          DW_OP_*piece operations, or their sum is equal
14799          to bitsize.  Otherwise we can't embed it.  */
14800       for (tail = &cur_descr; *tail != NULL;
14801            tail = &(*tail)->dw_loc_next)
14802         if ((*tail)->dw_loc_opc == DW_OP_piece)
14803           {
14804             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14805                       * BITS_PER_UNIT;
14806             last = *tail;
14807           }
14808         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14809           {
14810             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14811             last = *tail;
14812           }
14813
14814       if (last != NULL && opsize != bitsize)
14815         {
14816           padsize += bitsize;
14817           continue;
14818         }
14819
14820       /* If there is a hole, add DW_OP_*piece after empty DWARF
14821          expression, which means that those bits are optimized out.  */
14822       if (padsize)
14823         {
14824           if (padsize > decl_size)
14825             return NULL;
14826           decl_size -= padsize;
14827           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14828           if (*descr_tail == NULL)
14829             return NULL;
14830           descr_tail = &(*descr_tail)->dw_loc_next;
14831           padsize = 0;
14832         }
14833       *descr_tail = cur_descr;
14834       descr_tail = tail;
14835       if (bitsize > decl_size)
14836         return NULL;
14837       decl_size -= bitsize;
14838       if (last == NULL)
14839         {
14840           HOST_WIDE_INT offset = 0;
14841           if (GET_CODE (varloc) == VAR_LOCATION
14842               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14843             {
14844               varloc = PAT_VAR_LOCATION_LOC (varloc);
14845               if (GET_CODE (varloc) == EXPR_LIST)
14846                 varloc = XEXP (varloc, 0);
14847             }
14848           do 
14849             {
14850               if (GET_CODE (varloc) == CONST
14851                   || GET_CODE (varloc) == SIGN_EXTEND
14852                   || GET_CODE (varloc) == ZERO_EXTEND)
14853                 varloc = XEXP (varloc, 0);
14854               else if (GET_CODE (varloc) == SUBREG)
14855                 varloc = SUBREG_REG (varloc);
14856               else
14857                 break;
14858             }
14859           while (1);
14860           /* DW_OP_bit_size offset should be zero for register
14861              or implicit location descriptions and empty location
14862              descriptions, but for memory addresses needs big endian
14863              adjustment.  */
14864           if (MEM_P (varloc))
14865             {
14866               unsigned HOST_WIDE_INT memsize
14867                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14868               if (memsize != bitsize)
14869                 {
14870                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14871                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14872                     return NULL;
14873                   if (memsize < bitsize)
14874                     return NULL;
14875                   if (BITS_BIG_ENDIAN)
14876                     offset = memsize - bitsize;
14877                 }
14878             }
14879
14880           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14881           if (*descr_tail == NULL)
14882             return NULL;
14883           descr_tail = &(*descr_tail)->dw_loc_next;
14884         }
14885     }
14886
14887   /* If there were any non-empty expressions, add padding till the end of
14888      the decl.  */
14889   if (descr != NULL && decl_size != 0)
14890     {
14891       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14892       if (*descr_tail == NULL)
14893         return NULL;
14894     }
14895   return descr;
14896 }
14897
14898 /* Return the dwarf representation of the location list LOC_LIST of
14899    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14900    function.  */
14901
14902 static dw_loc_list_ref
14903 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14904 {
14905   const char *endname, *secname;
14906   rtx varloc;
14907   enum var_init_status initialized;
14908   struct var_loc_node *node;
14909   dw_loc_descr_ref descr;
14910   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14911   dw_loc_list_ref list = NULL;
14912   dw_loc_list_ref *listp = &list;
14913
14914   /* Now that we know what section we are using for a base,
14915      actually construct the list of locations.
14916      The first location information is what is passed to the
14917      function that creates the location list, and the remaining
14918      locations just get added on to that list.
14919      Note that we only know the start address for a location
14920      (IE location changes), so to build the range, we use
14921      the range [current location start, next location start].
14922      This means we have to special case the last node, and generate
14923      a range of [last location start, end of function label].  */
14924
14925   secname = secname_for_decl (decl);
14926
14927   for (node = loc_list->first; node; node = node->next)
14928     if (GET_CODE (node->loc) == EXPR_LIST
14929         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14930       {
14931         if (GET_CODE (node->loc) == EXPR_LIST)
14932           {
14933             /* This requires DW_OP_{,bit_}piece, which is not usable
14934                inside DWARF expressions.  */
14935             if (want_address != 2)
14936               continue;
14937             descr = dw_sra_loc_expr (decl, node->loc);
14938             if (descr == NULL)
14939               continue;
14940           }
14941         else
14942           {
14943             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14944             varloc = NOTE_VAR_LOCATION (node->loc);
14945             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14946           }
14947         if (descr)
14948           {
14949             /* The variable has a location between NODE->LABEL and
14950                NODE->NEXT->LABEL.  */
14951             if (node->next)
14952               endname = node->next->label;
14953             /* If the variable has a location at the last label
14954                it keeps its location until the end of function.  */
14955             else if (!current_function_decl)
14956               endname = text_end_label;
14957             else
14958               {
14959                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14960                                              current_function_funcdef_no);
14961                 endname = ggc_strdup (label_id);
14962               }
14963
14964             *listp = new_loc_list (descr, node->label, endname, secname);
14965             listp = &(*listp)->dw_loc_next;
14966           }
14967       }
14968
14969   /* Try to avoid the overhead of a location list emitting a location
14970      expression instead, but only if we didn't have more than one
14971      location entry in the first place.  If some entries were not
14972      representable, we don't want to pretend a single entry that was
14973      applies to the entire scope in which the variable is
14974      available.  */
14975   if (list && loc_list->first->next)
14976     gen_llsym (list);
14977
14978   return list;
14979 }
14980
14981 /* Return if the loc_list has only single element and thus can be represented
14982    as location description.   */
14983
14984 static bool
14985 single_element_loc_list_p (dw_loc_list_ref list)
14986 {
14987   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14988   return !list->ll_symbol;
14989 }
14990
14991 /* To each location in list LIST add loc descr REF.  */
14992
14993 static void
14994 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14995 {
14996   dw_loc_descr_ref copy;
14997   add_loc_descr (&list->expr, ref);
14998   list = list->dw_loc_next;
14999   while (list)
15000     {
15001       copy = ggc_alloc_dw_loc_descr_node ();
15002       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15003       add_loc_descr (&list->expr, copy);
15004       while (copy->dw_loc_next)
15005         {
15006           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15007           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15008           copy->dw_loc_next = new_copy;
15009           copy = new_copy;
15010         }
15011       list = list->dw_loc_next;
15012     }
15013 }
15014
15015 /* Given two lists RET and LIST
15016    produce location list that is result of adding expression in LIST
15017    to expression in RET on each possition in program.
15018    Might be destructive on both RET and LIST.
15019
15020    TODO: We handle only simple cases of RET or LIST having at most one
15021    element. General case would inolve sorting the lists in program order
15022    and merging them that will need some additional work.
15023    Adding that will improve quality of debug info especially for SRA-ed
15024    structures.  */
15025
15026 static void
15027 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15028 {
15029   if (!list)
15030     return;
15031   if (!*ret)
15032     {
15033       *ret = list;
15034       return;
15035     }
15036   if (!list->dw_loc_next)
15037     {
15038       add_loc_descr_to_each (*ret, list->expr);
15039       return;
15040     }
15041   if (!(*ret)->dw_loc_next)
15042     {
15043       add_loc_descr_to_each (list, (*ret)->expr);
15044       *ret = list;
15045       return;
15046     }
15047   expansion_failed (NULL_TREE, NULL_RTX,
15048                     "Don't know how to merge two non-trivial"
15049                     " location lists.\n");
15050   *ret = NULL;
15051   return;
15052 }
15053
15054 /* LOC is constant expression.  Try a luck, look it up in constant
15055    pool and return its loc_descr of its address.  */
15056
15057 static dw_loc_descr_ref
15058 cst_pool_loc_descr (tree loc)
15059 {
15060   /* Get an RTL for this, if something has been emitted.  */
15061   rtx rtl = lookup_constant_def (loc);
15062   enum machine_mode mode;
15063
15064   if (!rtl || !MEM_P (rtl))
15065     {
15066       gcc_assert (!rtl);
15067       return 0;
15068     }
15069   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15070
15071   /* TODO: We might get more coverage if we was actually delaying expansion
15072      of all expressions till end of compilation when constant pools are fully
15073      populated.  */
15074   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15075     {
15076       expansion_failed (loc, NULL_RTX,
15077                         "CST value in contant pool but not marked.");
15078       return 0;
15079     }
15080   mode = GET_MODE (rtl);
15081   rtl = XEXP (rtl, 0);
15082   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15083 }
15084
15085 /* Return dw_loc_list representing address of addr_expr LOC
15086    by looking for innder INDIRECT_REF expression and turing it
15087    into simple arithmetics.  */
15088
15089 static dw_loc_list_ref
15090 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15091 {
15092   tree obj, offset;
15093   HOST_WIDE_INT bitsize, bitpos, bytepos;
15094   enum machine_mode mode;
15095   int volatilep;
15096   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15097   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15098
15099   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15100                              &bitsize, &bitpos, &offset, &mode,
15101                              &unsignedp, &volatilep, false);
15102   STRIP_NOPS (obj);
15103   if (bitpos % BITS_PER_UNIT)
15104     {
15105       expansion_failed (loc, NULL_RTX, "bitfield access");
15106       return 0;
15107     }
15108   if (!INDIRECT_REF_P (obj))
15109     {
15110       expansion_failed (obj,
15111                         NULL_RTX, "no indirect ref in inner refrence");
15112       return 0;
15113     }
15114   if (!offset && !bitpos)
15115     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15116   else if (toplev
15117            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15118            && (dwarf_version >= 4 || !dwarf_strict))
15119     {
15120       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15121       if (!list_ret)
15122         return 0;
15123       if (offset)
15124         {
15125           /* Variable offset.  */
15126           list_ret1 = loc_list_from_tree (offset, 0);
15127           if (list_ret1 == 0)
15128             return 0;
15129           add_loc_list (&list_ret, list_ret1);
15130           if (!list_ret)
15131             return 0;
15132           add_loc_descr_to_each (list_ret,
15133                                  new_loc_descr (DW_OP_plus, 0, 0));
15134         }
15135       bytepos = bitpos / BITS_PER_UNIT;
15136       if (bytepos > 0)
15137         add_loc_descr_to_each (list_ret,
15138                                new_loc_descr (DW_OP_plus_uconst,
15139                                               bytepos, 0));
15140       else if (bytepos < 0)
15141         loc_list_plus_const (list_ret, bytepos);
15142       add_loc_descr_to_each (list_ret,
15143                              new_loc_descr (DW_OP_stack_value, 0, 0));
15144     }
15145   return list_ret;
15146 }
15147
15148
15149 /* Generate Dwarf location list representing LOC.
15150    If WANT_ADDRESS is false, expression computing LOC will be computed
15151    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15152    if WANT_ADDRESS is 2, expression computing address useable in location
15153      will be returned (i.e. DW_OP_reg can be used
15154      to refer to register values).  */
15155
15156 static dw_loc_list_ref
15157 loc_list_from_tree (tree loc, int want_address)
15158 {
15159   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15160   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15161   int have_address = 0;
15162   enum dwarf_location_atom op;
15163
15164   /* ??? Most of the time we do not take proper care for sign/zero
15165      extending the values properly.  Hopefully this won't be a real
15166      problem...  */
15167
15168   switch (TREE_CODE (loc))
15169     {
15170     case ERROR_MARK:
15171       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15172       return 0;
15173
15174     case PLACEHOLDER_EXPR:
15175       /* This case involves extracting fields from an object to determine the
15176          position of other fields.  We don't try to encode this here.  The
15177          only user of this is Ada, which encodes the needed information using
15178          the names of types.  */
15179       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15180       return 0;
15181
15182     case CALL_EXPR:
15183       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15184       /* There are no opcodes for these operations.  */
15185       return 0;
15186
15187     case PREINCREMENT_EXPR:
15188     case PREDECREMENT_EXPR:
15189     case POSTINCREMENT_EXPR:
15190     case POSTDECREMENT_EXPR:
15191       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15192       /* There are no opcodes for these operations.  */
15193       return 0;
15194
15195     case ADDR_EXPR:
15196       /* If we already want an address, see if there is INDIRECT_REF inside
15197          e.g. for &this->field.  */
15198       if (want_address)
15199         {
15200           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15201                        (loc, want_address == 2);
15202           if (list_ret)
15203             have_address = 1;
15204           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15205                    && (ret = cst_pool_loc_descr (loc)))
15206             have_address = 1;
15207         }
15208         /* Otherwise, process the argument and look for the address.  */
15209       if (!list_ret && !ret)
15210         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15211       else
15212         {
15213           if (want_address)
15214             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15215           return NULL;
15216         }
15217       break;
15218
15219     case VAR_DECL:
15220       if (DECL_THREAD_LOCAL_P (loc))
15221         {
15222           rtx rtl;
15223           enum dwarf_location_atom first_op;
15224           enum dwarf_location_atom second_op;
15225           bool dtprel = false;
15226
15227           if (targetm.have_tls)
15228             {
15229               /* If this is not defined, we have no way to emit the
15230                  data.  */
15231               if (!targetm.asm_out.output_dwarf_dtprel)
15232                 return 0;
15233
15234                /* The way DW_OP_GNU_push_tls_address is specified, we
15235                   can only look up addresses of objects in the current
15236                   module.  We used DW_OP_addr as first op, but that's
15237                   wrong, because DW_OP_addr is relocated by the debug
15238                   info consumer, while DW_OP_GNU_push_tls_address
15239                   operand shouldn't be.  */
15240               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15241                 return 0;
15242               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15243               dtprel = true;
15244               second_op = DW_OP_GNU_push_tls_address;
15245             }
15246           else
15247             {
15248               if (!targetm.emutls.debug_form_tls_address
15249                   || !(dwarf_version >= 3 || !dwarf_strict))
15250                 return 0;
15251               /* We stuffed the control variable into the DECL_VALUE_EXPR
15252                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15253                  no longer appear in gimple code.  We used the control
15254                  variable in specific so that we could pick it up here.  */
15255               loc = DECL_VALUE_EXPR (loc);
15256               first_op = DW_OP_addr;
15257               second_op = DW_OP_form_tls_address;
15258             }
15259
15260           rtl = rtl_for_decl_location (loc);
15261           if (rtl == NULL_RTX)
15262             return 0;
15263
15264           if (!MEM_P (rtl))
15265             return 0;
15266           rtl = XEXP (rtl, 0);
15267           if (! CONSTANT_P (rtl))
15268             return 0;
15269
15270           ret = new_loc_descr (first_op, 0, 0);
15271           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15272           ret->dw_loc_oprnd1.v.val_addr = rtl;
15273           ret->dtprel = dtprel;
15274
15275           ret1 = new_loc_descr (second_op, 0, 0);
15276           add_loc_descr (&ret, ret1);
15277
15278           have_address = 1;
15279           break;
15280         }
15281       /* FALLTHRU */
15282
15283     case PARM_DECL:
15284       if (DECL_HAS_VALUE_EXPR_P (loc))
15285         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15286                                    want_address);
15287       /* FALLTHRU */
15288
15289     case RESULT_DECL:
15290     case FUNCTION_DECL:
15291       {
15292         rtx rtl;
15293         var_loc_list *loc_list = lookup_decl_loc (loc);
15294
15295         if (loc_list && loc_list->first)
15296           {
15297             list_ret = dw_loc_list (loc_list, loc, want_address);
15298             have_address = want_address != 0;
15299             break;
15300           }
15301         rtl = rtl_for_decl_location (loc);
15302         if (rtl == NULL_RTX)
15303           {
15304             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15305             return 0;
15306           }
15307         else if (CONST_INT_P (rtl))
15308           {
15309             HOST_WIDE_INT val = INTVAL (rtl);
15310             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15311               val &= GET_MODE_MASK (DECL_MODE (loc));
15312             ret = int_loc_descriptor (val);
15313           }
15314         else if (GET_CODE (rtl) == CONST_STRING)
15315           {
15316             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15317             return 0;
15318           }
15319         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15320           {
15321             ret = new_loc_descr (DW_OP_addr, 0, 0);
15322             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15323             ret->dw_loc_oprnd1.v.val_addr = rtl;
15324           }
15325         else
15326           {
15327             enum machine_mode mode;
15328
15329             /* Certain constructs can only be represented at top-level.  */
15330             if (want_address == 2)
15331               {
15332                 ret = loc_descriptor (rtl, VOIDmode,
15333                                       VAR_INIT_STATUS_INITIALIZED);
15334                 have_address = 1;
15335               }
15336             else
15337               {
15338                 mode = GET_MODE (rtl);
15339                 if (MEM_P (rtl))
15340                   {
15341                     rtl = XEXP (rtl, 0);
15342                     have_address = 1;
15343                   }
15344                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15345               }
15346             if (!ret)
15347               expansion_failed (loc, rtl,
15348                                 "failed to produce loc descriptor for rtl");
15349           }
15350       }
15351       break;
15352
15353     case MEM_REF:
15354       /* ??? FIXME.  */
15355       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15356         return 0;
15357       /* Fallthru.  */
15358     case INDIRECT_REF:
15359       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15360       have_address = 1;
15361       break;
15362
15363     case COMPOUND_EXPR:
15364       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15365
15366     CASE_CONVERT:
15367     case VIEW_CONVERT_EXPR:
15368     case SAVE_EXPR:
15369     case MODIFY_EXPR:
15370       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15371
15372     case COMPONENT_REF:
15373     case BIT_FIELD_REF:
15374     case ARRAY_REF:
15375     case ARRAY_RANGE_REF:
15376     case REALPART_EXPR:
15377     case IMAGPART_EXPR:
15378       {
15379         tree obj, offset;
15380         HOST_WIDE_INT bitsize, bitpos, bytepos;
15381         enum machine_mode mode;
15382         int volatilep;
15383         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15384
15385         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15386                                    &unsignedp, &volatilep, false);
15387
15388         gcc_assert (obj != loc);
15389
15390         list_ret = loc_list_from_tree (obj,
15391                                        want_address == 2
15392                                        && !bitpos && !offset ? 2 : 1);
15393         /* TODO: We can extract value of the small expression via shifting even
15394            for nonzero bitpos.  */
15395         if (list_ret == 0)
15396           return 0;
15397         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15398           {
15399             expansion_failed (loc, NULL_RTX,
15400                               "bitfield access");
15401             return 0;
15402           }
15403
15404         if (offset != NULL_TREE)
15405           {
15406             /* Variable offset.  */
15407             list_ret1 = loc_list_from_tree (offset, 0);
15408             if (list_ret1 == 0)
15409               return 0;
15410             add_loc_list (&list_ret, list_ret1);
15411             if (!list_ret)
15412               return 0;
15413             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15414           }
15415
15416         bytepos = bitpos / BITS_PER_UNIT;
15417         if (bytepos > 0)
15418           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15419         else if (bytepos < 0)
15420           loc_list_plus_const (list_ret, bytepos);
15421
15422         have_address = 1;
15423         break;
15424       }
15425
15426     case INTEGER_CST:
15427       if ((want_address || !host_integerp (loc, 0))
15428           && (ret = cst_pool_loc_descr (loc)))
15429         have_address = 1;
15430       else if (want_address == 2
15431                && host_integerp (loc, 0)
15432                && (ret = address_of_int_loc_descriptor
15433                            (int_size_in_bytes (TREE_TYPE (loc)),
15434                             tree_low_cst (loc, 0))))
15435         have_address = 1;
15436       else if (host_integerp (loc, 0))
15437         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15438       else
15439         {
15440           expansion_failed (loc, NULL_RTX,
15441                             "Integer operand is not host integer");
15442           return 0;
15443         }
15444       break;
15445
15446     case CONSTRUCTOR:
15447     case REAL_CST:
15448     case STRING_CST:
15449     case COMPLEX_CST:
15450       if ((ret = cst_pool_loc_descr (loc)))
15451         have_address = 1;
15452       else
15453       /* We can construct small constants here using int_loc_descriptor.  */
15454         expansion_failed (loc, NULL_RTX,
15455                           "constructor or constant not in constant pool");
15456       break;
15457
15458     case TRUTH_AND_EXPR:
15459     case TRUTH_ANDIF_EXPR:
15460     case BIT_AND_EXPR:
15461       op = DW_OP_and;
15462       goto do_binop;
15463
15464     case TRUTH_XOR_EXPR:
15465     case BIT_XOR_EXPR:
15466       op = DW_OP_xor;
15467       goto do_binop;
15468
15469     case TRUTH_OR_EXPR:
15470     case TRUTH_ORIF_EXPR:
15471     case BIT_IOR_EXPR:
15472       op = DW_OP_or;
15473       goto do_binop;
15474
15475     case FLOOR_DIV_EXPR:
15476     case CEIL_DIV_EXPR:
15477     case ROUND_DIV_EXPR:
15478     case TRUNC_DIV_EXPR:
15479       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15480         return 0;
15481       op = DW_OP_div;
15482       goto do_binop;
15483
15484     case MINUS_EXPR:
15485       op = DW_OP_minus;
15486       goto do_binop;
15487
15488     case FLOOR_MOD_EXPR:
15489     case CEIL_MOD_EXPR:
15490     case ROUND_MOD_EXPR:
15491     case TRUNC_MOD_EXPR:
15492       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15493         {
15494           op = DW_OP_mod;
15495           goto do_binop;
15496         }
15497       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15498       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15499       if (list_ret == 0 || list_ret1 == 0)
15500         return 0;
15501
15502       add_loc_list (&list_ret, list_ret1);
15503       if (list_ret == 0)
15504         return 0;
15505       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15506       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15507       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15508       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15509       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15510       break;
15511
15512     case MULT_EXPR:
15513       op = DW_OP_mul;
15514       goto do_binop;
15515
15516     case LSHIFT_EXPR:
15517       op = DW_OP_shl;
15518       goto do_binop;
15519
15520     case RSHIFT_EXPR:
15521       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15522       goto do_binop;
15523
15524     case POINTER_PLUS_EXPR:
15525     case PLUS_EXPR:
15526       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15527         {
15528           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15529           if (list_ret == 0)
15530             return 0;
15531
15532           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15533           break;
15534         }
15535
15536       op = DW_OP_plus;
15537       goto do_binop;
15538
15539     case LE_EXPR:
15540       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15541         return 0;
15542
15543       op = DW_OP_le;
15544       goto do_binop;
15545
15546     case GE_EXPR:
15547       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15548         return 0;
15549
15550       op = DW_OP_ge;
15551       goto do_binop;
15552
15553     case LT_EXPR:
15554       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15555         return 0;
15556
15557       op = DW_OP_lt;
15558       goto do_binop;
15559
15560     case GT_EXPR:
15561       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15562         return 0;
15563
15564       op = DW_OP_gt;
15565       goto do_binop;
15566
15567     case EQ_EXPR:
15568       op = DW_OP_eq;
15569       goto do_binop;
15570
15571     case NE_EXPR:
15572       op = DW_OP_ne;
15573       goto do_binop;
15574
15575     do_binop:
15576       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15577       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15578       if (list_ret == 0 || list_ret1 == 0)
15579         return 0;
15580
15581       add_loc_list (&list_ret, list_ret1);
15582       if (list_ret == 0)
15583         return 0;
15584       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15585       break;
15586
15587     case TRUTH_NOT_EXPR:
15588     case BIT_NOT_EXPR:
15589       op = DW_OP_not;
15590       goto do_unop;
15591
15592     case ABS_EXPR:
15593       op = DW_OP_abs;
15594       goto do_unop;
15595
15596     case NEGATE_EXPR:
15597       op = DW_OP_neg;
15598       goto do_unop;
15599
15600     do_unop:
15601       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15602       if (list_ret == 0)
15603         return 0;
15604
15605       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15606       break;
15607
15608     case MIN_EXPR:
15609     case MAX_EXPR:
15610       {
15611         const enum tree_code code =
15612           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15613
15614         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15615                       build2 (code, integer_type_node,
15616                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15617                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15618       }
15619
15620       /* ... fall through ...  */
15621
15622     case COND_EXPR:
15623       {
15624         dw_loc_descr_ref lhs
15625           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15626         dw_loc_list_ref rhs
15627           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15628         dw_loc_descr_ref bra_node, jump_node, tmp;
15629
15630         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15631         if (list_ret == 0 || lhs == 0 || rhs == 0)
15632           return 0;
15633
15634         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15635         add_loc_descr_to_each (list_ret, bra_node);
15636
15637         add_loc_list (&list_ret, rhs);
15638         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15639         add_loc_descr_to_each (list_ret, jump_node);
15640
15641         add_loc_descr_to_each (list_ret, lhs);
15642         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15643         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15644
15645         /* ??? Need a node to point the skip at.  Use a nop.  */
15646         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15647         add_loc_descr_to_each (list_ret, tmp);
15648         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15649         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15650       }
15651       break;
15652
15653     case FIX_TRUNC_EXPR:
15654       return 0;
15655
15656     default:
15657       /* Leave front-end specific codes as simply unknown.  This comes
15658          up, for instance, with the C STMT_EXPR.  */
15659       if ((unsigned int) TREE_CODE (loc)
15660           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15661         {
15662           expansion_failed (loc, NULL_RTX,
15663                             "language specific tree node");
15664           return 0;
15665         }
15666
15667 #ifdef ENABLE_CHECKING
15668       /* Otherwise this is a generic code; we should just lists all of
15669          these explicitly.  We forgot one.  */
15670       gcc_unreachable ();
15671 #else
15672       /* In a release build, we want to degrade gracefully: better to
15673          generate incomplete debugging information than to crash.  */
15674       return NULL;
15675 #endif
15676     }
15677
15678   if (!ret && !list_ret)
15679     return 0;
15680
15681   if (want_address == 2 && !have_address
15682       && (dwarf_version >= 4 || !dwarf_strict))
15683     {
15684       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15685         {
15686           expansion_failed (loc, NULL_RTX,
15687                             "DWARF address size mismatch");
15688           return 0;
15689         }
15690       if (ret)
15691         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15692       else
15693         add_loc_descr_to_each (list_ret,
15694                                new_loc_descr (DW_OP_stack_value, 0, 0));
15695       have_address = 1;
15696     }
15697   /* Show if we can't fill the request for an address.  */
15698   if (want_address && !have_address)
15699     {
15700       expansion_failed (loc, NULL_RTX,
15701                         "Want address and only have value");
15702       return 0;
15703     }
15704
15705   gcc_assert (!ret || !list_ret);
15706
15707   /* If we've got an address and don't want one, dereference.  */
15708   if (!want_address && have_address)
15709     {
15710       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15711
15712       if (size > DWARF2_ADDR_SIZE || size == -1)
15713         {
15714           expansion_failed (loc, NULL_RTX,
15715                             "DWARF address size mismatch");
15716           return 0;
15717         }
15718       else if (size == DWARF2_ADDR_SIZE)
15719         op = DW_OP_deref;
15720       else
15721         op = DW_OP_deref_size;
15722
15723       if (ret)
15724         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15725       else
15726         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15727     }
15728   if (ret)
15729     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15730
15731   return list_ret;
15732 }
15733
15734 /* Same as above but return only single location expression.  */
15735 static dw_loc_descr_ref
15736 loc_descriptor_from_tree (tree loc, int want_address)
15737 {
15738   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15739   if (!ret)
15740     return NULL;
15741   if (ret->dw_loc_next)
15742     {
15743       expansion_failed (loc, NULL_RTX,
15744                         "Location list where only loc descriptor needed");
15745       return NULL;
15746     }
15747   return ret->expr;
15748 }
15749
15750 /* Given a value, round it up to the lowest multiple of `boundary'
15751    which is not less than the value itself.  */
15752
15753 static inline HOST_WIDE_INT
15754 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15755 {
15756   return (((value + boundary - 1) / boundary) * boundary);
15757 }
15758
15759 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15760    pointer to the declared type for the relevant field variable, or return
15761    `integer_type_node' if the given node turns out to be an
15762    ERROR_MARK node.  */
15763
15764 static inline tree
15765 field_type (const_tree decl)
15766 {
15767   tree type;
15768
15769   if (TREE_CODE (decl) == ERROR_MARK)
15770     return integer_type_node;
15771
15772   type = DECL_BIT_FIELD_TYPE (decl);
15773   if (type == NULL_TREE)
15774     type = TREE_TYPE (decl);
15775
15776   return type;
15777 }
15778
15779 /* Given a pointer to a tree node, return the alignment in bits for
15780    it, or else return BITS_PER_WORD if the node actually turns out to
15781    be an ERROR_MARK node.  */
15782
15783 static inline unsigned
15784 simple_type_align_in_bits (const_tree type)
15785 {
15786   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15787 }
15788
15789 static inline unsigned
15790 simple_decl_align_in_bits (const_tree decl)
15791 {
15792   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15793 }
15794
15795 /* Return the result of rounding T up to ALIGN.  */
15796
15797 static inline double_int
15798 round_up_to_align (double_int t, unsigned int align)
15799 {
15800   double_int alignd = uhwi_to_double_int (align);
15801   t = double_int_add (t, alignd);
15802   t = double_int_add (t, double_int_minus_one);
15803   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15804   t = double_int_mul (t, alignd);
15805   return t;
15806 }
15807
15808 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15809    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15810    or return 0 if we are unable to determine what that offset is, either
15811    because the argument turns out to be a pointer to an ERROR_MARK node, or
15812    because the offset is actually variable.  (We can't handle the latter case
15813    just yet).  */
15814
15815 static HOST_WIDE_INT
15816 field_byte_offset (const_tree decl)
15817 {
15818   double_int object_offset_in_bits;
15819   double_int object_offset_in_bytes;
15820   double_int bitpos_int;
15821
15822   if (TREE_CODE (decl) == ERROR_MARK)
15823     return 0;
15824
15825   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15826
15827   /* We cannot yet cope with fields whose positions are variable, so
15828      for now, when we see such things, we simply return 0.  Someday, we may
15829      be able to handle such cases, but it will be damn difficult.  */
15830   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15831     return 0;
15832
15833   bitpos_int = tree_to_double_int (bit_position (decl));
15834
15835 #ifdef PCC_BITFIELD_TYPE_MATTERS
15836   if (PCC_BITFIELD_TYPE_MATTERS)
15837     {
15838       tree type;
15839       tree field_size_tree;
15840       double_int deepest_bitpos;
15841       double_int field_size_in_bits;
15842       unsigned int type_align_in_bits;
15843       unsigned int decl_align_in_bits;
15844       double_int type_size_in_bits;
15845
15846       type = field_type (decl);
15847       type_size_in_bits = double_int_type_size_in_bits (type);
15848       type_align_in_bits = simple_type_align_in_bits (type);
15849
15850       field_size_tree = DECL_SIZE (decl);
15851
15852       /* The size could be unspecified if there was an error, or for
15853          a flexible array member.  */
15854       if (!field_size_tree)
15855         field_size_tree = bitsize_zero_node;
15856
15857       /* If the size of the field is not constant, use the type size.  */
15858       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15859         field_size_in_bits = tree_to_double_int (field_size_tree);
15860       else
15861         field_size_in_bits = type_size_in_bits;
15862
15863       decl_align_in_bits = simple_decl_align_in_bits (decl);
15864
15865       /* The GCC front-end doesn't make any attempt to keep track of the
15866          starting bit offset (relative to the start of the containing
15867          structure type) of the hypothetical "containing object" for a
15868          bit-field.  Thus, when computing the byte offset value for the
15869          start of the "containing object" of a bit-field, we must deduce
15870          this information on our own. This can be rather tricky to do in
15871          some cases.  For example, handling the following structure type
15872          definition when compiling for an i386/i486 target (which only
15873          aligns long long's to 32-bit boundaries) can be very tricky:
15874
15875          struct S { int field1; long long field2:31; };
15876
15877          Fortunately, there is a simple rule-of-thumb which can be used
15878          in such cases.  When compiling for an i386/i486, GCC will
15879          allocate 8 bytes for the structure shown above.  It decides to
15880          do this based upon one simple rule for bit-field allocation.
15881          GCC allocates each "containing object" for each bit-field at
15882          the first (i.e. lowest addressed) legitimate alignment boundary
15883          (based upon the required minimum alignment for the declared
15884          type of the field) which it can possibly use, subject to the
15885          condition that there is still enough available space remaining
15886          in the containing object (when allocated at the selected point)
15887          to fully accommodate all of the bits of the bit-field itself.
15888
15889          This simple rule makes it obvious why GCC allocates 8 bytes for
15890          each object of the structure type shown above.  When looking
15891          for a place to allocate the "containing object" for `field2',
15892          the compiler simply tries to allocate a 64-bit "containing
15893          object" at each successive 32-bit boundary (starting at zero)
15894          until it finds a place to allocate that 64- bit field such that
15895          at least 31 contiguous (and previously unallocated) bits remain
15896          within that selected 64 bit field.  (As it turns out, for the
15897          example above, the compiler finds it is OK to allocate the
15898          "containing object" 64-bit field at bit-offset zero within the
15899          structure type.)
15900
15901          Here we attempt to work backwards from the limited set of facts
15902          we're given, and we try to deduce from those facts, where GCC
15903          must have believed that the containing object started (within
15904          the structure type). The value we deduce is then used (by the
15905          callers of this routine) to generate DW_AT_location and
15906          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15907          the case of DW_AT_location, regular fields as well).  */
15908
15909       /* Figure out the bit-distance from the start of the structure to
15910          the "deepest" bit of the bit-field.  */
15911       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15912
15913       /* This is the tricky part.  Use some fancy footwork to deduce
15914          where the lowest addressed bit of the containing object must
15915          be.  */
15916       object_offset_in_bits
15917         = double_int_sub (deepest_bitpos, type_size_in_bits);
15918
15919       /* Round up to type_align by default.  This works best for
15920          bitfields.  */
15921       object_offset_in_bits
15922         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15923
15924       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15925         {
15926           object_offset_in_bits
15927             = double_int_sub (deepest_bitpos, type_size_in_bits);
15928
15929           /* Round up to decl_align instead.  */
15930           object_offset_in_bits
15931             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15932         }
15933     }
15934   else
15935 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15936     object_offset_in_bits = bitpos_int;
15937
15938   object_offset_in_bytes
15939     = double_int_div (object_offset_in_bits,
15940                       uhwi_to_double_int (BITS_PER_UNIT), true,
15941                       TRUNC_DIV_EXPR);
15942   return double_int_to_shwi (object_offset_in_bytes);
15943 }
15944 \f
15945 /* The following routines define various Dwarf attributes and any data
15946    associated with them.  */
15947
15948 /* Add a location description attribute value to a DIE.
15949
15950    This emits location attributes suitable for whole variables and
15951    whole parameters.  Note that the location attributes for struct fields are
15952    generated by the routine `data_member_location_attribute' below.  */
15953
15954 static inline void
15955 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15956                              dw_loc_list_ref descr)
15957 {
15958   if (descr == 0)
15959     return;
15960   if (single_element_loc_list_p (descr))
15961     add_AT_loc (die, attr_kind, descr->expr);
15962   else
15963     add_AT_loc_list (die, attr_kind, descr);
15964 }
15965
15966 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15967
15968 static void
15969 add_accessibility_attribute (dw_die_ref die, tree decl)
15970 {
15971   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15972      children, otherwise the default is DW_ACCESS_public.  In DWARF2
15973      the default has always been DW_ACCESS_public.  */
15974   if (TREE_PROTECTED (decl))
15975     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15976   else if (TREE_PRIVATE (decl))
15977     {
15978       if (dwarf_version == 2
15979           || die->die_parent == NULL
15980           || die->die_parent->die_tag != DW_TAG_class_type)
15981         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15982     }
15983   else if (dwarf_version > 2
15984            && die->die_parent
15985            && die->die_parent->die_tag == DW_TAG_class_type)
15986     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15987 }
15988
15989 /* Attach the specialized form of location attribute used for data members of
15990    struct and union types.  In the special case of a FIELD_DECL node which
15991    represents a bit-field, the "offset" part of this special location
15992    descriptor must indicate the distance in bytes from the lowest-addressed
15993    byte of the containing struct or union type to the lowest-addressed byte of
15994    the "containing object" for the bit-field.  (See the `field_byte_offset'
15995    function above).
15996
15997    For any given bit-field, the "containing object" is a hypothetical object
15998    (of some integral or enum type) within which the given bit-field lives.  The
15999    type of this hypothetical "containing object" is always the same as the
16000    declared type of the individual bit-field itself (for GCC anyway... the
16001    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16002    bytes) of the hypothetical "containing object" which will be given in the
16003    DW_AT_byte_size attribute for this bit-field.  (See the
16004    `byte_size_attribute' function below.)  It is also used when calculating the
16005    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16006    function below.)  */
16007
16008 static void
16009 add_data_member_location_attribute (dw_die_ref die, tree decl)
16010 {
16011   HOST_WIDE_INT offset;
16012   dw_loc_descr_ref loc_descr = 0;
16013
16014   if (TREE_CODE (decl) == TREE_BINFO)
16015     {
16016       /* We're working on the TAG_inheritance for a base class.  */
16017       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16018         {
16019           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16020              aren't at a fixed offset from all (sub)objects of the same
16021              type.  We need to extract the appropriate offset from our
16022              vtable.  The following dwarf expression means
16023
16024                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16025
16026              This is specific to the V3 ABI, of course.  */
16027
16028           dw_loc_descr_ref tmp;
16029
16030           /* Make a copy of the object address.  */
16031           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16032           add_loc_descr (&loc_descr, tmp);
16033
16034           /* Extract the vtable address.  */
16035           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16036           add_loc_descr (&loc_descr, tmp);
16037
16038           /* Calculate the address of the offset.  */
16039           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16040           gcc_assert (offset < 0);
16041
16042           tmp = int_loc_descriptor (-offset);
16043           add_loc_descr (&loc_descr, tmp);
16044           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16045           add_loc_descr (&loc_descr, tmp);
16046
16047           /* Extract the offset.  */
16048           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16049           add_loc_descr (&loc_descr, tmp);
16050
16051           /* Add it to the object address.  */
16052           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16053           add_loc_descr (&loc_descr, tmp);
16054         }
16055       else
16056         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16057     }
16058   else
16059     offset = field_byte_offset (decl);
16060
16061   if (! loc_descr)
16062     {
16063       if (dwarf_version > 2)
16064         {
16065           /* Don't need to output a location expression, just the constant. */
16066           if (offset < 0)
16067             add_AT_int (die, DW_AT_data_member_location, offset);
16068           else
16069             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16070           return;
16071         }
16072       else
16073         {
16074           enum dwarf_location_atom op;
16075
16076           /* The DWARF2 standard says that we should assume that the structure
16077              address is already on the stack, so we can specify a structure
16078              field address by using DW_OP_plus_uconst.  */
16079
16080 #ifdef MIPS_DEBUGGING_INFO
16081           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16082              operator correctly.  It works only if we leave the offset on the
16083              stack.  */
16084           op = DW_OP_constu;
16085 #else
16086           op = DW_OP_plus_uconst;
16087 #endif
16088
16089           loc_descr = new_loc_descr (op, offset, 0);
16090         }
16091     }
16092
16093   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16094 }
16095
16096 /* Writes integer values to dw_vec_const array.  */
16097
16098 static void
16099 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16100 {
16101   while (size != 0)
16102     {
16103       *dest++ = val & 0xff;
16104       val >>= 8;
16105       --size;
16106     }
16107 }
16108
16109 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16110
16111 static HOST_WIDE_INT
16112 extract_int (const unsigned char *src, unsigned int size)
16113 {
16114   HOST_WIDE_INT val = 0;
16115
16116   src += size;
16117   while (size != 0)
16118     {
16119       val <<= 8;
16120       val |= *--src & 0xff;
16121       --size;
16122     }
16123   return val;
16124 }
16125
16126 /* Writes double_int values to dw_vec_const array.  */
16127
16128 static void
16129 insert_double (double_int val, unsigned char *dest)
16130 {
16131   unsigned char *p0 = dest;
16132   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16133
16134   if (WORDS_BIG_ENDIAN)
16135     {
16136       p0 = p1;
16137       p1 = dest;
16138     }
16139
16140   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16141   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16142 }
16143
16144 /* Writes floating point values to dw_vec_const array.  */
16145
16146 static void
16147 insert_float (const_rtx rtl, unsigned char *array)
16148 {
16149   REAL_VALUE_TYPE rv;
16150   long val[4];
16151   int i;
16152
16153   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16154   real_to_target (val, &rv, GET_MODE (rtl));
16155
16156   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16157   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16158     {
16159       insert_int (val[i], 4, array);
16160       array += 4;
16161     }
16162 }
16163
16164 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16165    does not have a "location" either in memory or in a register.  These
16166    things can arise in GNU C when a constant is passed as an actual parameter
16167    to an inlined function.  They can also arise in C++ where declared
16168    constants do not necessarily get memory "homes".  */
16169
16170 static bool
16171 add_const_value_attribute (dw_die_ref die, rtx rtl)
16172 {
16173   switch (GET_CODE (rtl))
16174     {
16175     case CONST_INT:
16176       {
16177         HOST_WIDE_INT val = INTVAL (rtl);
16178
16179         if (val < 0)
16180           add_AT_int (die, DW_AT_const_value, val);
16181         else
16182           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16183       }
16184       return true;
16185
16186     case CONST_DOUBLE:
16187       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16188          floating-point constant.  A CONST_DOUBLE is used whenever the
16189          constant requires more than one word in order to be adequately
16190          represented.  */
16191       {
16192         enum machine_mode mode = GET_MODE (rtl);
16193
16194         if (SCALAR_FLOAT_MODE_P (mode))
16195           {
16196             unsigned int length = GET_MODE_SIZE (mode);
16197             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16198
16199             insert_float (rtl, array);
16200             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16201           }
16202         else
16203           add_AT_double (die, DW_AT_const_value,
16204                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16205       }
16206       return true;
16207
16208     case CONST_VECTOR:
16209       {
16210         enum machine_mode mode = GET_MODE (rtl);
16211         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16212         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16213         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16214           (length * elt_size);
16215         unsigned int i;
16216         unsigned char *p;
16217
16218         switch (GET_MODE_CLASS (mode))
16219           {
16220           case MODE_VECTOR_INT:
16221             for (i = 0, p = array; i < length; i++, p += elt_size)
16222               {
16223                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16224                 double_int val = rtx_to_double_int (elt);
16225
16226                 if (elt_size <= sizeof (HOST_WIDE_INT))
16227                   insert_int (double_int_to_shwi (val), elt_size, p);
16228                 else
16229                   {
16230                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16231                     insert_double (val, p);
16232                   }
16233               }
16234             break;
16235
16236           case MODE_VECTOR_FLOAT:
16237             for (i = 0, p = array; i < length; i++, p += elt_size)
16238               {
16239                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16240                 insert_float (elt, p);
16241               }
16242             break;
16243
16244           default:
16245             gcc_unreachable ();
16246           }
16247
16248         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16249       }
16250       return true;
16251
16252     case CONST_STRING:
16253       if (dwarf_version >= 4 || !dwarf_strict)
16254         {
16255           dw_loc_descr_ref loc_result;
16256           resolve_one_addr (&rtl, NULL);
16257         rtl_addr:
16258           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16259           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16260           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16261           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16262           add_AT_loc (die, DW_AT_location, loc_result);
16263           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16264           return true;
16265         }
16266       return false;
16267
16268     case CONST:
16269       if (CONSTANT_P (XEXP (rtl, 0)))
16270         return add_const_value_attribute (die, XEXP (rtl, 0));
16271       /* FALLTHROUGH */
16272     case SYMBOL_REF:
16273       if (!const_ok_for_output (rtl))
16274         return false;
16275     case LABEL_REF:
16276       if (dwarf_version >= 4 || !dwarf_strict)
16277         goto rtl_addr;
16278       return false;
16279
16280     case PLUS:
16281       /* In cases where an inlined instance of an inline function is passed
16282          the address of an `auto' variable (which is local to the caller) we
16283          can get a situation where the DECL_RTL of the artificial local
16284          variable (for the inlining) which acts as a stand-in for the
16285          corresponding formal parameter (of the inline function) will look
16286          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16287          exactly a compile-time constant expression, but it isn't the address
16288          of the (artificial) local variable either.  Rather, it represents the
16289          *value* which the artificial local variable always has during its
16290          lifetime.  We currently have no way to represent such quasi-constant
16291          values in Dwarf, so for now we just punt and generate nothing.  */
16292       return false;
16293
16294     case HIGH:
16295     case CONST_FIXED:
16296       return false;
16297
16298     case MEM:
16299       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16300           && MEM_READONLY_P (rtl)
16301           && GET_MODE (rtl) == BLKmode)
16302         {
16303           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16304           return true;
16305         }
16306       return false;
16307
16308     default:
16309       /* No other kinds of rtx should be possible here.  */
16310       gcc_unreachable ();
16311     }
16312   return false;
16313 }
16314
16315 /* Determine whether the evaluation of EXPR references any variables
16316    or functions which aren't otherwise used (and therefore may not be
16317    output).  */
16318 static tree
16319 reference_to_unused (tree * tp, int * walk_subtrees,
16320                      void * data ATTRIBUTE_UNUSED)
16321 {
16322   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16323     *walk_subtrees = 0;
16324
16325   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16326       && ! TREE_ASM_WRITTEN (*tp))
16327     return *tp;
16328   /* ???  The C++ FE emits debug information for using decls, so
16329      putting gcc_unreachable here falls over.  See PR31899.  For now
16330      be conservative.  */
16331   else if (!cgraph_global_info_ready
16332            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16333     return *tp;
16334   else if (TREE_CODE (*tp) == VAR_DECL)
16335     {
16336       struct varpool_node *node = varpool_get_node (*tp);
16337       if (!node || !node->needed)
16338         return *tp;
16339     }
16340   else if (TREE_CODE (*tp) == FUNCTION_DECL
16341            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16342     {
16343       /* The call graph machinery must have finished analyzing,
16344          optimizing and gimplifying the CU by now.
16345          So if *TP has no call graph node associated
16346          to it, it means *TP will not be emitted.  */
16347       if (!cgraph_get_node (*tp))
16348         return *tp;
16349     }
16350   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16351     return *tp;
16352
16353   return NULL_TREE;
16354 }
16355
16356 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16357    for use in a later add_const_value_attribute call.  */
16358
16359 static rtx
16360 rtl_for_decl_init (tree init, tree type)
16361 {
16362   rtx rtl = NULL_RTX;
16363
16364   /* If a variable is initialized with a string constant without embedded
16365      zeros, build CONST_STRING.  */
16366   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16367     {
16368       tree enttype = TREE_TYPE (type);
16369       tree domain = TYPE_DOMAIN (type);
16370       enum machine_mode mode = TYPE_MODE (enttype);
16371
16372       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16373           && domain
16374           && integer_zerop (TYPE_MIN_VALUE (domain))
16375           && compare_tree_int (TYPE_MAX_VALUE (domain),
16376                                TREE_STRING_LENGTH (init) - 1) == 0
16377           && ((size_t) TREE_STRING_LENGTH (init)
16378               == strlen (TREE_STRING_POINTER (init)) + 1))
16379         {
16380           rtl = gen_rtx_CONST_STRING (VOIDmode,
16381                                       ggc_strdup (TREE_STRING_POINTER (init)));
16382           rtl = gen_rtx_MEM (BLKmode, rtl);
16383           MEM_READONLY_P (rtl) = 1;
16384         }
16385     }
16386   /* Other aggregates, and complex values, could be represented using
16387      CONCAT: FIXME!  */
16388   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16389     ;
16390   /* Vectors only work if their mode is supported by the target.
16391      FIXME: generic vectors ought to work too.  */
16392   else if (TREE_CODE (type) == VECTOR_TYPE
16393            && !VECTOR_MODE_P (TYPE_MODE (type)))
16394     ;
16395   /* If the initializer is something that we know will expand into an
16396      immediate RTL constant, expand it now.  We must be careful not to
16397      reference variables which won't be output.  */
16398   else if (initializer_constant_valid_p (init, type)
16399            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16400     {
16401       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16402          possible.  */
16403       if (TREE_CODE (type) == VECTOR_TYPE)
16404         switch (TREE_CODE (init))
16405           {
16406           case VECTOR_CST:
16407             break;
16408           case CONSTRUCTOR:
16409             if (TREE_CONSTANT (init))
16410               {
16411                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16412                 bool constant_p = true;
16413                 tree value;
16414                 unsigned HOST_WIDE_INT ix;
16415
16416                 /* Even when ctor is constant, it might contain non-*_CST
16417                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16418                    belong into VECTOR_CST nodes.  */
16419                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16420                   if (!CONSTANT_CLASS_P (value))
16421                     {
16422                       constant_p = false;
16423                       break;
16424                     }
16425
16426                 if (constant_p)
16427                   {
16428                     init = build_vector_from_ctor (type, elts);
16429                     break;
16430                   }
16431               }
16432             /* FALLTHRU */
16433
16434           default:
16435             return NULL;
16436           }
16437
16438       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16439
16440       /* If expand_expr returns a MEM, it wasn't immediate.  */
16441       gcc_assert (!rtl || !MEM_P (rtl));
16442     }
16443
16444   return rtl;
16445 }
16446
16447 /* Generate RTL for the variable DECL to represent its location.  */
16448
16449 static rtx
16450 rtl_for_decl_location (tree decl)
16451 {
16452   rtx rtl;
16453
16454   /* Here we have to decide where we are going to say the parameter "lives"
16455      (as far as the debugger is concerned).  We only have a couple of
16456      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16457
16458      DECL_RTL normally indicates where the parameter lives during most of the
16459      activation of the function.  If optimization is enabled however, this
16460      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16461      that the parameter doesn't really live anywhere (as far as the code
16462      generation parts of GCC are concerned) during most of the function's
16463      activation.  That will happen (for example) if the parameter is never
16464      referenced within the function.
16465
16466      We could just generate a location descriptor here for all non-NULL
16467      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16468      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16469      where DECL_RTL is NULL or is a pseudo-reg.
16470
16471      Note however that we can only get away with using DECL_INCOMING_RTL as
16472      a backup substitute for DECL_RTL in certain limited cases.  In cases
16473      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16474      we can be sure that the parameter was passed using the same type as it is
16475      declared to have within the function, and that its DECL_INCOMING_RTL
16476      points us to a place where a value of that type is passed.
16477
16478      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16479      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16480      because in these cases DECL_INCOMING_RTL points us to a value of some
16481      type which is *different* from the type of the parameter itself.  Thus,
16482      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16483      such cases, the debugger would end up (for example) trying to fetch a
16484      `float' from a place which actually contains the first part of a
16485      `double'.  That would lead to really incorrect and confusing
16486      output at debug-time.
16487
16488      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16489      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16490      are a couple of exceptions however.  On little-endian machines we can
16491      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16492      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16493      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16494      when (on a little-endian machine) a non-prototyped function has a
16495      parameter declared to be of type `short' or `char'.  In such cases,
16496      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16497      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16498      passed `int' value.  If the debugger then uses that address to fetch
16499      a `short' or a `char' (on a little-endian machine) the result will be
16500      the correct data, so we allow for such exceptional cases below.
16501
16502      Note that our goal here is to describe the place where the given formal
16503      parameter lives during most of the function's activation (i.e. between the
16504      end of the prologue and the start of the epilogue).  We'll do that as best
16505      as we can. Note however that if the given formal parameter is modified
16506      sometime during the execution of the function, then a stack backtrace (at
16507      debug-time) will show the function as having been called with the *new*
16508      value rather than the value which was originally passed in.  This happens
16509      rarely enough that it is not a major problem, but it *is* a problem, and
16510      I'd like to fix it.
16511
16512      A future version of dwarf2out.c may generate two additional attributes for
16513      any given DW_TAG_formal_parameter DIE which will describe the "passed
16514      type" and the "passed location" for the given formal parameter in addition
16515      to the attributes we now generate to indicate the "declared type" and the
16516      "active location" for each parameter.  This additional set of attributes
16517      could be used by debuggers for stack backtraces. Separately, note that
16518      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16519      This happens (for example) for inlined-instances of inline function formal
16520      parameters which are never referenced.  This really shouldn't be
16521      happening.  All PARM_DECL nodes should get valid non-NULL
16522      DECL_INCOMING_RTL values.  FIXME.  */
16523
16524   /* Use DECL_RTL as the "location" unless we find something better.  */
16525   rtl = DECL_RTL_IF_SET (decl);
16526
16527   /* When generating abstract instances, ignore everything except
16528      constants, symbols living in memory, and symbols living in
16529      fixed registers.  */
16530   if (! reload_completed)
16531     {
16532       if (rtl
16533           && (CONSTANT_P (rtl)
16534               || (MEM_P (rtl)
16535                   && CONSTANT_P (XEXP (rtl, 0)))
16536               || (REG_P (rtl)
16537                   && TREE_CODE (decl) == VAR_DECL
16538                   && TREE_STATIC (decl))))
16539         {
16540           rtl = targetm.delegitimize_address (rtl);
16541           return rtl;
16542         }
16543       rtl = NULL_RTX;
16544     }
16545   else if (TREE_CODE (decl) == PARM_DECL)
16546     {
16547       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16548         {
16549           tree declared_type = TREE_TYPE (decl);
16550           tree passed_type = DECL_ARG_TYPE (decl);
16551           enum machine_mode dmode = TYPE_MODE (declared_type);
16552           enum machine_mode pmode = TYPE_MODE (passed_type);
16553
16554           /* This decl represents a formal parameter which was optimized out.
16555              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16556              all cases where (rtl == NULL_RTX) just below.  */
16557           if (dmode == pmode)
16558             rtl = DECL_INCOMING_RTL (decl);
16559           else if (SCALAR_INT_MODE_P (dmode)
16560                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16561                    && DECL_INCOMING_RTL (decl))
16562             {
16563               rtx inc = DECL_INCOMING_RTL (decl);
16564               if (REG_P (inc))
16565                 rtl = inc;
16566               else if (MEM_P (inc))
16567                 {
16568                   if (BYTES_BIG_ENDIAN)
16569                     rtl = adjust_address_nv (inc, dmode,
16570                                              GET_MODE_SIZE (pmode)
16571                                              - GET_MODE_SIZE (dmode));
16572                   else
16573                     rtl = inc;
16574                 }
16575             }
16576         }
16577
16578       /* If the parm was passed in registers, but lives on the stack, then
16579          make a big endian correction if the mode of the type of the
16580          parameter is not the same as the mode of the rtl.  */
16581       /* ??? This is the same series of checks that are made in dbxout.c before
16582          we reach the big endian correction code there.  It isn't clear if all
16583          of these checks are necessary here, but keeping them all is the safe
16584          thing to do.  */
16585       else if (MEM_P (rtl)
16586                && XEXP (rtl, 0) != const0_rtx
16587                && ! CONSTANT_P (XEXP (rtl, 0))
16588                /* Not passed in memory.  */
16589                && !MEM_P (DECL_INCOMING_RTL (decl))
16590                /* Not passed by invisible reference.  */
16591                && (!REG_P (XEXP (rtl, 0))
16592                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16593                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16594 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16595                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16596 #endif
16597                      )
16598                /* Big endian correction check.  */
16599                && BYTES_BIG_ENDIAN
16600                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16601                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16602                    < UNITS_PER_WORD))
16603         {
16604           int offset = (UNITS_PER_WORD
16605                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16606
16607           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16608                              plus_constant (XEXP (rtl, 0), offset));
16609         }
16610     }
16611   else if (TREE_CODE (decl) == VAR_DECL
16612            && rtl
16613            && MEM_P (rtl)
16614            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16615            && BYTES_BIG_ENDIAN)
16616     {
16617       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16618       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16619
16620       /* If a variable is declared "register" yet is smaller than
16621          a register, then if we store the variable to memory, it
16622          looks like we're storing a register-sized value, when in
16623          fact we are not.  We need to adjust the offset of the
16624          storage location to reflect the actual value's bytes,
16625          else gdb will not be able to display it.  */
16626       if (rsize > dsize)
16627         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16628                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16629     }
16630
16631   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16632      and will have been substituted directly into all expressions that use it.
16633      C does not have such a concept, but C++ and other languages do.  */
16634   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16635     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16636
16637   if (rtl)
16638     rtl = targetm.delegitimize_address (rtl);
16639
16640   /* If we don't look past the constant pool, we risk emitting a
16641      reference to a constant pool entry that isn't referenced from
16642      code, and thus is not emitted.  */
16643   if (rtl)
16644     rtl = avoid_constant_pool_reference (rtl);
16645
16646   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16647      in the current CU, resolve_addr will remove the expression referencing
16648      it.  */
16649   if (rtl == NULL_RTX
16650       && TREE_CODE (decl) == VAR_DECL
16651       && !DECL_EXTERNAL (decl)
16652       && TREE_STATIC (decl)
16653       && DECL_NAME (decl)
16654       && !DECL_HARD_REGISTER (decl)
16655       && DECL_MODE (decl) != VOIDmode)
16656     {
16657       rtl = make_decl_rtl_for_debug (decl);
16658       if (!MEM_P (rtl)
16659           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16660           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16661         rtl = NULL_RTX;
16662     }
16663
16664   return rtl;
16665 }
16666
16667 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16668    returned.  If so, the decl for the COMMON block is returned, and the
16669    value is the offset into the common block for the symbol.  */
16670
16671 static tree
16672 fortran_common (tree decl, HOST_WIDE_INT *value)
16673 {
16674   tree val_expr, cvar;
16675   enum machine_mode mode;
16676   HOST_WIDE_INT bitsize, bitpos;
16677   tree offset;
16678   int volatilep = 0, unsignedp = 0;
16679
16680   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16681      it does not have a value (the offset into the common area), or if it
16682      is thread local (as opposed to global) then it isn't common, and shouldn't
16683      be handled as such.  */
16684   if (TREE_CODE (decl) != VAR_DECL
16685       || !TREE_STATIC (decl)
16686       || !DECL_HAS_VALUE_EXPR_P (decl)
16687       || !is_fortran ())
16688     return NULL_TREE;
16689
16690   val_expr = DECL_VALUE_EXPR (decl);
16691   if (TREE_CODE (val_expr) != COMPONENT_REF)
16692     return NULL_TREE;
16693
16694   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16695                               &mode, &unsignedp, &volatilep, true);
16696
16697   if (cvar == NULL_TREE
16698       || TREE_CODE (cvar) != VAR_DECL
16699       || DECL_ARTIFICIAL (cvar)
16700       || !TREE_PUBLIC (cvar))
16701     return NULL_TREE;
16702
16703   *value = 0;
16704   if (offset != NULL)
16705     {
16706       if (!host_integerp (offset, 0))
16707         return NULL_TREE;
16708       *value = tree_low_cst (offset, 0);
16709     }
16710   if (bitpos != 0)
16711     *value += bitpos / BITS_PER_UNIT;
16712
16713   return cvar;
16714 }
16715
16716 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16717    data attribute for a variable or a parameter.  We generate the
16718    DW_AT_const_value attribute only in those cases where the given variable
16719    or parameter does not have a true "location" either in memory or in a
16720    register.  This can happen (for example) when a constant is passed as an
16721    actual argument in a call to an inline function.  (It's possible that
16722    these things can crop up in other ways also.)  Note that one type of
16723    constant value which can be passed into an inlined function is a constant
16724    pointer.  This can happen for example if an actual argument in an inlined
16725    function call evaluates to a compile-time constant address.  */
16726
16727 static bool
16728 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16729                                        enum dwarf_attribute attr)
16730 {
16731   rtx rtl;
16732   dw_loc_list_ref list;
16733   var_loc_list *loc_list;
16734
16735   if (TREE_CODE (decl) == ERROR_MARK)
16736     return false;
16737
16738   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16739               || TREE_CODE (decl) == RESULT_DECL);
16740
16741   /* Try to get some constant RTL for this decl, and use that as the value of
16742      the location.  */
16743
16744   rtl = rtl_for_decl_location (decl);
16745   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16746       && add_const_value_attribute (die, rtl))
16747     return true;
16748
16749   /* See if we have single element location list that is equivalent to
16750      a constant value.  That way we are better to use add_const_value_attribute
16751      rather than expanding constant value equivalent.  */
16752   loc_list = lookup_decl_loc (decl);
16753   if (loc_list
16754       && loc_list->first
16755       && loc_list->first->next == NULL
16756       && NOTE_P (loc_list->first->loc)
16757       && NOTE_VAR_LOCATION (loc_list->first->loc)
16758       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16759     {
16760       struct var_loc_node *node;
16761
16762       node = loc_list->first;
16763       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16764       if (GET_CODE (rtl) == EXPR_LIST)
16765         rtl = XEXP (rtl, 0);
16766       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16767           && add_const_value_attribute (die, rtl))
16768          return true;
16769     }
16770   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16771   if (list)
16772     {
16773       add_AT_location_description (die, attr, list);
16774       return true;
16775     }
16776   /* None of that worked, so it must not really have a location;
16777      try adding a constant value attribute from the DECL_INITIAL.  */
16778   return tree_add_const_value_attribute_for_decl (die, decl);
16779 }
16780
16781 /* Add VARIABLE and DIE into deferred locations list.  */
16782
16783 static void
16784 defer_location (tree variable, dw_die_ref die)
16785 {
16786   deferred_locations entry;
16787   entry.variable = variable;
16788   entry.die = die;
16789   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16790 }
16791
16792 /* Helper function for tree_add_const_value_attribute.  Natively encode
16793    initializer INIT into an array.  Return true if successful.  */
16794
16795 static bool
16796 native_encode_initializer (tree init, unsigned char *array, int size)
16797 {
16798   tree type;
16799
16800   if (init == NULL_TREE)
16801     return false;
16802
16803   STRIP_NOPS (init);
16804   switch (TREE_CODE (init))
16805     {
16806     case STRING_CST:
16807       type = TREE_TYPE (init);
16808       if (TREE_CODE (type) == ARRAY_TYPE)
16809         {
16810           tree enttype = TREE_TYPE (type);
16811           enum machine_mode mode = TYPE_MODE (enttype);
16812
16813           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16814             return false;
16815           if (int_size_in_bytes (type) != size)
16816             return false;
16817           if (size > TREE_STRING_LENGTH (init))
16818             {
16819               memcpy (array, TREE_STRING_POINTER (init),
16820                       TREE_STRING_LENGTH (init));
16821               memset (array + TREE_STRING_LENGTH (init),
16822                       '\0', size - TREE_STRING_LENGTH (init));
16823             }
16824           else
16825             memcpy (array, TREE_STRING_POINTER (init), size);
16826           return true;
16827         }
16828       return false;
16829     case CONSTRUCTOR:
16830       type = TREE_TYPE (init);
16831       if (int_size_in_bytes (type) != size)
16832         return false;
16833       if (TREE_CODE (type) == ARRAY_TYPE)
16834         {
16835           HOST_WIDE_INT min_index;
16836           unsigned HOST_WIDE_INT cnt;
16837           int curpos = 0, fieldsize;
16838           constructor_elt *ce;
16839
16840           if (TYPE_DOMAIN (type) == NULL_TREE
16841               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16842             return false;
16843
16844           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16845           if (fieldsize <= 0)
16846             return false;
16847
16848           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16849           memset (array, '\0', size);
16850           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16851             {
16852               tree val = ce->value;
16853               tree index = ce->index;
16854               int pos = curpos;
16855               if (index && TREE_CODE (index) == RANGE_EXPR)
16856                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16857                       * fieldsize;
16858               else if (index)
16859                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16860
16861               if (val)
16862                 {
16863                   STRIP_NOPS (val);
16864                   if (!native_encode_initializer (val, array + pos, fieldsize))
16865                     return false;
16866                 }
16867               curpos = pos + fieldsize;
16868               if (index && TREE_CODE (index) == RANGE_EXPR)
16869                 {
16870                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16871                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16872                   while (count > 0)
16873                     {
16874                       if (val)
16875                         memcpy (array + curpos, array + pos, fieldsize);
16876                       curpos += fieldsize;
16877                     }
16878                 }
16879               gcc_assert (curpos <= size);
16880             }
16881           return true;
16882         }
16883       else if (TREE_CODE (type) == RECORD_TYPE
16884                || TREE_CODE (type) == UNION_TYPE)
16885         {
16886           tree field = NULL_TREE;
16887           unsigned HOST_WIDE_INT cnt;
16888           constructor_elt *ce;
16889
16890           if (int_size_in_bytes (type) != size)
16891             return false;
16892
16893           if (TREE_CODE (type) == RECORD_TYPE)
16894             field = TYPE_FIELDS (type);
16895
16896           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16897             {
16898               tree val = ce->value;
16899               int pos, fieldsize;
16900
16901               if (ce->index != 0)
16902                 field = ce->index;
16903
16904               if (val)
16905                 STRIP_NOPS (val);
16906
16907               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16908                 return false;
16909
16910               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16911                   && TYPE_DOMAIN (TREE_TYPE (field))
16912                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16913                 return false;
16914               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16915                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16916                 return false;
16917               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16918               pos = int_byte_position (field);
16919               gcc_assert (pos + fieldsize <= size);
16920               if (val
16921                   && !native_encode_initializer (val, array + pos, fieldsize))
16922                 return false;
16923             }
16924           return true;
16925         }
16926       return false;
16927     case VIEW_CONVERT_EXPR:
16928     case NON_LVALUE_EXPR:
16929       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16930     default:
16931       return native_encode_expr (init, array, size) == size;
16932     }
16933 }
16934
16935 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16936    attribute is the const value T.  */
16937
16938 static bool
16939 tree_add_const_value_attribute (dw_die_ref die, tree t)
16940 {
16941   tree init;
16942   tree type = TREE_TYPE (t);
16943   rtx rtl;
16944
16945   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16946     return false;
16947
16948   init = t;
16949   gcc_assert (!DECL_P (init));
16950
16951   rtl = rtl_for_decl_init (init, type);
16952   if (rtl)
16953     return add_const_value_attribute (die, rtl);
16954   /* If the host and target are sane, try harder.  */
16955   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16956            && initializer_constant_valid_p (init, type))
16957     {
16958       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16959       if (size > 0 && (int) size == size)
16960         {
16961           unsigned char *array = (unsigned char *)
16962             ggc_alloc_cleared_atomic (size);
16963
16964           if (native_encode_initializer (init, array, size))
16965             {
16966               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16967               return true;
16968             }
16969         }
16970     }
16971   return false;
16972 }
16973
16974 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16975    attribute is the const value of T, where T is an integral constant
16976    variable with static storage duration
16977    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16978
16979 static bool
16980 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16981 {
16982
16983   if (!decl
16984       || (TREE_CODE (decl) != VAR_DECL
16985           && TREE_CODE (decl) != CONST_DECL))
16986     return false;
16987
16988     if (TREE_READONLY (decl)
16989         && ! TREE_THIS_VOLATILE (decl)
16990         && DECL_INITIAL (decl))
16991       /* OK */;
16992     else
16993       return false;
16994
16995   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16996   if (get_AT (var_die, DW_AT_const_value))
16997     return false;
16998
16999   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17000 }
17001
17002 /* Convert the CFI instructions for the current function into a
17003    location list.  This is used for DW_AT_frame_base when we targeting
17004    a dwarf2 consumer that does not support the dwarf3
17005    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17006    expressions.  */
17007
17008 static dw_loc_list_ref
17009 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17010 {
17011   dw_fde_ref fde;
17012   dw_loc_list_ref list, *list_tail;
17013   dw_cfi_ref cfi;
17014   dw_cfa_location last_cfa, next_cfa;
17015   const char *start_label, *last_label, *section;
17016   dw_cfa_location remember;
17017
17018   fde = current_fde ();
17019   gcc_assert (fde != NULL);
17020
17021   section = secname_for_decl (current_function_decl);
17022   list_tail = &list;
17023   list = NULL;
17024
17025   memset (&next_cfa, 0, sizeof (next_cfa));
17026   next_cfa.reg = INVALID_REGNUM;
17027   remember = next_cfa;
17028
17029   start_label = fde->dw_fde_begin;
17030
17031   /* ??? Bald assumption that the CIE opcode list does not contain
17032      advance opcodes.  */
17033   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17034     lookup_cfa_1 (cfi, &next_cfa, &remember);
17035
17036   last_cfa = next_cfa;
17037   last_label = start_label;
17038
17039   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17040     switch (cfi->dw_cfi_opc)
17041       {
17042       case DW_CFA_set_loc:
17043       case DW_CFA_advance_loc1:
17044       case DW_CFA_advance_loc2:
17045       case DW_CFA_advance_loc4:
17046         if (!cfa_equal_p (&last_cfa, &next_cfa))
17047           {
17048             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17049                                        start_label, last_label, section);
17050
17051             list_tail = &(*list_tail)->dw_loc_next;
17052             last_cfa = next_cfa;
17053             start_label = last_label;
17054           }
17055         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17056         break;
17057
17058       case DW_CFA_advance_loc:
17059         /* The encoding is complex enough that we should never emit this.  */
17060         gcc_unreachable ();
17061
17062       default:
17063         lookup_cfa_1 (cfi, &next_cfa, &remember);
17064         break;
17065       }
17066
17067   if (!cfa_equal_p (&last_cfa, &next_cfa))
17068     {
17069       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17070                                  start_label, last_label, section);
17071       list_tail = &(*list_tail)->dw_loc_next;
17072       start_label = last_label;
17073     }
17074
17075   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17076                              start_label, fde->dw_fde_end, section);
17077
17078   if (list && list->dw_loc_next)
17079     gen_llsym (list);
17080
17081   return list;
17082 }
17083
17084 /* Compute a displacement from the "steady-state frame pointer" to the
17085    frame base (often the same as the CFA), and store it in
17086    frame_pointer_fb_offset.  OFFSET is added to the displacement
17087    before the latter is negated.  */
17088
17089 static void
17090 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17091 {
17092   rtx reg, elim;
17093
17094 #ifdef FRAME_POINTER_CFA_OFFSET
17095   reg = frame_pointer_rtx;
17096   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17097 #else
17098   reg = arg_pointer_rtx;
17099   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17100 #endif
17101
17102   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17103   if (GET_CODE (elim) == PLUS)
17104     {
17105       offset += INTVAL (XEXP (elim, 1));
17106       elim = XEXP (elim, 0);
17107     }
17108
17109   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17110                && (elim == hard_frame_pointer_rtx
17111                    || elim == stack_pointer_rtx))
17112               || elim == (frame_pointer_needed
17113                           ? hard_frame_pointer_rtx
17114                           : stack_pointer_rtx));
17115
17116   frame_pointer_fb_offset = -offset;
17117 }
17118
17119 /* Generate a DW_AT_name attribute given some string value to be included as
17120    the value of the attribute.  */
17121
17122 static void
17123 add_name_attribute (dw_die_ref die, const char *name_string)
17124 {
17125   if (name_string != NULL && *name_string != 0)
17126     {
17127       if (demangle_name_func)
17128         name_string = (*demangle_name_func) (name_string);
17129
17130       add_AT_string (die, DW_AT_name, name_string);
17131     }
17132 }
17133
17134 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17135
17136 static void
17137 add_comp_dir_attribute (dw_die_ref die)
17138 {
17139   const char *wd = get_src_pwd ();
17140   char *wd1;
17141
17142   if (wd == NULL)
17143     return;
17144
17145   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17146     {
17147       int wdlen;
17148
17149       wdlen = strlen (wd);
17150       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17151       strcpy (wd1, wd);
17152       wd1 [wdlen] = DIR_SEPARATOR;
17153       wd1 [wdlen + 1] = 0;
17154       wd = wd1;
17155     }
17156
17157     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17158 }
17159
17160 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17161    default.  */
17162
17163 static int
17164 lower_bound_default (void)
17165 {
17166   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17167     {
17168     case DW_LANG_C:
17169     case DW_LANG_C89:
17170     case DW_LANG_C99:
17171     case DW_LANG_C_plus_plus:
17172     case DW_LANG_ObjC:
17173     case DW_LANG_ObjC_plus_plus:
17174     case DW_LANG_Java:
17175       return 0;
17176     case DW_LANG_Fortran77:
17177     case DW_LANG_Fortran90:
17178     case DW_LANG_Fortran95:
17179       return 1;
17180     case DW_LANG_UPC:
17181     case DW_LANG_D:
17182     case DW_LANG_Python:
17183       return dwarf_version >= 4 ? 0 : -1;
17184     case DW_LANG_Ada95:
17185     case DW_LANG_Ada83:
17186     case DW_LANG_Cobol74:
17187     case DW_LANG_Cobol85:
17188     case DW_LANG_Pascal83:
17189     case DW_LANG_Modula2:
17190     case DW_LANG_PLI:
17191       return dwarf_version >= 4 ? 1 : -1;
17192     default:
17193       return -1;
17194     }
17195 }
17196
17197 /* Given a tree node describing an array bound (either lower or upper) output
17198    a representation for that bound.  */
17199
17200 static void
17201 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17202 {
17203   switch (TREE_CODE (bound))
17204     {
17205     case ERROR_MARK:
17206       return;
17207
17208     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17209     case INTEGER_CST:
17210       {
17211         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17212         int dflt;
17213
17214         /* Use the default if possible.  */
17215         if (bound_attr == DW_AT_lower_bound
17216             && host_integerp (bound, 0)
17217             && (dflt = lower_bound_default ()) != -1
17218             && tree_low_cst (bound, 0) == dflt)
17219           ;
17220
17221         /* Otherwise represent the bound as an unsigned value with the
17222            precision of its type.  The precision and signedness of the
17223            type will be necessary to re-interpret it unambiguously.  */
17224         else if (prec < HOST_BITS_PER_WIDE_INT)
17225           {
17226             unsigned HOST_WIDE_INT mask
17227               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17228             add_AT_unsigned (subrange_die, bound_attr,
17229                              TREE_INT_CST_LOW (bound) & mask);
17230           }
17231         else if (prec == HOST_BITS_PER_WIDE_INT
17232                  || TREE_INT_CST_HIGH (bound) == 0)
17233           add_AT_unsigned (subrange_die, bound_attr,
17234                            TREE_INT_CST_LOW (bound));
17235         else
17236           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17237                          TREE_INT_CST_LOW (bound));
17238       }
17239       break;
17240
17241     CASE_CONVERT:
17242     case VIEW_CONVERT_EXPR:
17243       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17244       break;
17245
17246     case SAVE_EXPR:
17247       break;
17248
17249     case VAR_DECL:
17250     case PARM_DECL:
17251     case RESULT_DECL:
17252       {
17253         dw_die_ref decl_die = lookup_decl_die (bound);
17254
17255         /* ??? Can this happen, or should the variable have been bound
17256            first?  Probably it can, since I imagine that we try to create
17257            the types of parameters in the order in which they exist in
17258            the list, and won't have created a forward reference to a
17259            later parameter.  */
17260         if (decl_die != NULL)
17261           {
17262             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17263             break;
17264           }
17265       }
17266       /* FALLTHRU */
17267
17268     default:
17269       {
17270         /* Otherwise try to create a stack operation procedure to
17271            evaluate the value of the array bound.  */
17272
17273         dw_die_ref ctx, decl_die;
17274         dw_loc_list_ref list;
17275
17276         list = loc_list_from_tree (bound, 2);
17277         if (list == NULL || single_element_loc_list_p (list))
17278           {
17279             /* If DW_AT_*bound is not a reference nor constant, it is
17280                a DWARF expression rather than location description.
17281                For that loc_list_from_tree (bound, 0) is needed.
17282                If that fails to give a single element list,
17283                fall back to outputting this as a reference anyway.  */
17284             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17285             if (list2 && single_element_loc_list_p (list2))
17286               {
17287                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17288                 break;
17289               }
17290           }
17291         if (list == NULL)
17292           break;
17293
17294         if (current_function_decl == 0)
17295           ctx = comp_unit_die ();
17296         else
17297           ctx = lookup_decl_die (current_function_decl);
17298
17299         decl_die = new_die (DW_TAG_variable, ctx, bound);
17300         add_AT_flag (decl_die, DW_AT_artificial, 1);
17301         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17302         add_AT_location_description (decl_die, DW_AT_location, list);
17303         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17304         break;
17305       }
17306     }
17307 }
17308
17309 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17310    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17311    Note that the block of subscript information for an array type also
17312    includes information about the element type of the given array type.  */
17313
17314 static void
17315 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17316 {
17317   unsigned dimension_number;
17318   tree lower, upper;
17319   dw_die_ref subrange_die;
17320
17321   for (dimension_number = 0;
17322        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17323        type = TREE_TYPE (type), dimension_number++)
17324     {
17325       tree domain = TYPE_DOMAIN (type);
17326
17327       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17328         break;
17329
17330       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17331          and (in GNU C only) variable bounds.  Handle all three forms
17332          here.  */
17333       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17334       if (domain)
17335         {
17336           /* We have an array type with specified bounds.  */
17337           lower = TYPE_MIN_VALUE (domain);
17338           upper = TYPE_MAX_VALUE (domain);
17339
17340           /* Define the index type.  */
17341           if (TREE_TYPE (domain))
17342             {
17343               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17344                  TREE_TYPE field.  We can't emit debug info for this
17345                  because it is an unnamed integral type.  */
17346               if (TREE_CODE (domain) == INTEGER_TYPE
17347                   && TYPE_NAME (domain) == NULL_TREE
17348                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17349                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17350                 ;
17351               else
17352                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17353                                     type_die);
17354             }
17355
17356           /* ??? If upper is NULL, the array has unspecified length,
17357              but it does have a lower bound.  This happens with Fortran
17358                dimension arr(N:*)
17359              Since the debugger is definitely going to need to know N
17360              to produce useful results, go ahead and output the lower
17361              bound solo, and hope the debugger can cope.  */
17362
17363           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17364           if (upper)
17365             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17366         }
17367
17368       /* Otherwise we have an array type with an unspecified length.  The
17369          DWARF-2 spec does not say how to handle this; let's just leave out the
17370          bounds.  */
17371     }
17372 }
17373
17374 static void
17375 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17376 {
17377   unsigned size;
17378
17379   switch (TREE_CODE (tree_node))
17380     {
17381     case ERROR_MARK:
17382       size = 0;
17383       break;
17384     case ENUMERAL_TYPE:
17385     case RECORD_TYPE:
17386     case UNION_TYPE:
17387     case QUAL_UNION_TYPE:
17388       size = int_size_in_bytes (tree_node);
17389       break;
17390     case FIELD_DECL:
17391       /* For a data member of a struct or union, the DW_AT_byte_size is
17392          generally given as the number of bytes normally allocated for an
17393          object of the *declared* type of the member itself.  This is true
17394          even for bit-fields.  */
17395       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17396       break;
17397     default:
17398       gcc_unreachable ();
17399     }
17400
17401   /* Note that `size' might be -1 when we get to this point.  If it is, that
17402      indicates that the byte size of the entity in question is variable.  We
17403      have no good way of expressing this fact in Dwarf at the present time,
17404      so just let the -1 pass on through.  */
17405   add_AT_unsigned (die, DW_AT_byte_size, size);
17406 }
17407
17408 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17409    which specifies the distance in bits from the highest order bit of the
17410    "containing object" for the bit-field to the highest order bit of the
17411    bit-field itself.
17412
17413    For any given bit-field, the "containing object" is a hypothetical object
17414    (of some integral or enum type) within which the given bit-field lives.  The
17415    type of this hypothetical "containing object" is always the same as the
17416    declared type of the individual bit-field itself.  The determination of the
17417    exact location of the "containing object" for a bit-field is rather
17418    complicated.  It's handled by the `field_byte_offset' function (above).
17419
17420    Note that it is the size (in bytes) of the hypothetical "containing object"
17421    which will be given in the DW_AT_byte_size attribute for this bit-field.
17422    (See `byte_size_attribute' above).  */
17423
17424 static inline void
17425 add_bit_offset_attribute (dw_die_ref die, tree decl)
17426 {
17427   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17428   tree type = DECL_BIT_FIELD_TYPE (decl);
17429   HOST_WIDE_INT bitpos_int;
17430   HOST_WIDE_INT highest_order_object_bit_offset;
17431   HOST_WIDE_INT highest_order_field_bit_offset;
17432   HOST_WIDE_INT unsigned bit_offset;
17433
17434   /* Must be a field and a bit field.  */
17435   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17436
17437   /* We can't yet handle bit-fields whose offsets are variable, so if we
17438      encounter such things, just return without generating any attribute
17439      whatsoever.  Likewise for variable or too large size.  */
17440   if (! host_integerp (bit_position (decl), 0)
17441       || ! host_integerp (DECL_SIZE (decl), 1))
17442     return;
17443
17444   bitpos_int = int_bit_position (decl);
17445
17446   /* Note that the bit offset is always the distance (in bits) from the
17447      highest-order bit of the "containing object" to the highest-order bit of
17448      the bit-field itself.  Since the "high-order end" of any object or field
17449      is different on big-endian and little-endian machines, the computation
17450      below must take account of these differences.  */
17451   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17452   highest_order_field_bit_offset = bitpos_int;
17453
17454   if (! BYTES_BIG_ENDIAN)
17455     {
17456       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17457       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17458     }
17459
17460   bit_offset
17461     = (! BYTES_BIG_ENDIAN
17462        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17463        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17464
17465   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17466 }
17467
17468 /* For a FIELD_DECL node which represents a bit field, output an attribute
17469    which specifies the length in bits of the given field.  */
17470
17471 static inline void
17472 add_bit_size_attribute (dw_die_ref die, tree decl)
17473 {
17474   /* Must be a field and a bit field.  */
17475   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17476               && DECL_BIT_FIELD_TYPE (decl));
17477
17478   if (host_integerp (DECL_SIZE (decl), 1))
17479     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17480 }
17481
17482 /* If the compiled language is ANSI C, then add a 'prototyped'
17483    attribute, if arg types are given for the parameters of a function.  */
17484
17485 static inline void
17486 add_prototyped_attribute (dw_die_ref die, tree func_type)
17487 {
17488   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17489       && TYPE_ARG_TYPES (func_type) != NULL)
17490     add_AT_flag (die, DW_AT_prototyped, 1);
17491 }
17492
17493 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17494    by looking in either the type declaration or object declaration
17495    equate table.  */
17496
17497 static inline dw_die_ref
17498 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17499 {
17500   dw_die_ref origin_die = NULL;
17501
17502   if (TREE_CODE (origin) != FUNCTION_DECL)
17503     {
17504       /* We may have gotten separated from the block for the inlined
17505          function, if we're in an exception handler or some such; make
17506          sure that the abstract function has been written out.
17507
17508          Doing this for nested functions is wrong, however; functions are
17509          distinct units, and our context might not even be inline.  */
17510       tree fn = origin;
17511
17512       if (TYPE_P (fn))
17513         fn = TYPE_STUB_DECL (fn);
17514
17515       fn = decl_function_context (fn);
17516       if (fn)
17517         dwarf2out_abstract_function (fn);
17518     }
17519
17520   if (DECL_P (origin))
17521     origin_die = lookup_decl_die (origin);
17522   else if (TYPE_P (origin))
17523     origin_die = lookup_type_die (origin);
17524
17525   /* XXX: Functions that are never lowered don't always have correct block
17526      trees (in the case of java, they simply have no block tree, in some other
17527      languages).  For these functions, there is nothing we can really do to
17528      output correct debug info for inlined functions in all cases.  Rather
17529      than die, we'll just produce deficient debug info now, in that we will
17530      have variables without a proper abstract origin.  In the future, when all
17531      functions are lowered, we should re-add a gcc_assert (origin_die)
17532      here.  */
17533
17534   if (origin_die)
17535     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17536   return origin_die;
17537 }
17538
17539 /* We do not currently support the pure_virtual attribute.  */
17540
17541 static inline void
17542 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17543 {
17544   if (DECL_VINDEX (func_decl))
17545     {
17546       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17547
17548       if (host_integerp (DECL_VINDEX (func_decl), 0))
17549         add_AT_loc (die, DW_AT_vtable_elem_location,
17550                     new_loc_descr (DW_OP_constu,
17551                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17552                                    0));
17553
17554       /* GNU extension: Record what type this method came from originally.  */
17555       if (debug_info_level > DINFO_LEVEL_TERSE
17556           && DECL_CONTEXT (func_decl))
17557         add_AT_die_ref (die, DW_AT_containing_type,
17558                         lookup_type_die (DECL_CONTEXT (func_decl)));
17559     }
17560 }
17561 \f
17562 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17563    given decl.  This used to be a vendor extension until after DWARF 4
17564    standardized it.  */
17565
17566 static void
17567 add_linkage_attr (dw_die_ref die, tree decl)
17568 {
17569   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17570
17571   /* Mimic what assemble_name_raw does with a leading '*'.  */
17572   if (name[0] == '*')
17573     name = &name[1];
17574
17575   if (dwarf_version >= 4)
17576     add_AT_string (die, DW_AT_linkage_name, name);
17577   else
17578     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17579 }
17580
17581 /* Add source coordinate attributes for the given decl.  */
17582
17583 static void
17584 add_src_coords_attributes (dw_die_ref die, tree decl)
17585 {
17586   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17587
17588   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17589   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17590 }
17591
17592 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17593
17594 static void
17595 add_linkage_name (dw_die_ref die, tree decl)
17596 {
17597   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17598        && TREE_PUBLIC (decl)
17599        && !DECL_ABSTRACT (decl)
17600        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17601        && die->die_tag != DW_TAG_member)
17602     {
17603       /* Defer until we have an assembler name set.  */
17604       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17605         {
17606           limbo_die_node *asm_name;
17607
17608           asm_name = ggc_alloc_cleared_limbo_die_node ();
17609           asm_name->die = die;
17610           asm_name->created_for = decl;
17611           asm_name->next = deferred_asm_name;
17612           deferred_asm_name = asm_name;
17613         }
17614       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17615         add_linkage_attr (die, decl);
17616     }
17617 }
17618
17619 /* Add a DW_AT_name attribute and source coordinate attribute for the
17620    given decl, but only if it actually has a name.  */
17621
17622 static void
17623 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17624 {
17625   tree decl_name;
17626
17627   decl_name = DECL_NAME (decl);
17628   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17629     {
17630       const char *name = dwarf2_name (decl, 0);
17631       if (name)
17632         add_name_attribute (die, name);
17633       if (! DECL_ARTIFICIAL (decl))
17634         add_src_coords_attributes (die, decl);
17635
17636       add_linkage_name (die, decl);
17637     }
17638
17639 #ifdef VMS_DEBUGGING_INFO
17640   /* Get the function's name, as described by its RTL.  This may be different
17641      from the DECL_NAME name used in the source file.  */
17642   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17643     {
17644       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17645                    XEXP (DECL_RTL (decl), 0));
17646       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17647     }
17648 #endif /* VMS_DEBUGGING_INFO */
17649 }
17650
17651 #ifdef VMS_DEBUGGING_INFO
17652 /* Output the debug main pointer die for VMS */
17653
17654 void
17655 dwarf2out_vms_debug_main_pointer (void)
17656 {
17657   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17658   dw_die_ref die;
17659
17660   /* Allocate the VMS debug main subprogram die.  */
17661   die = ggc_alloc_cleared_die_node ();
17662   die->die_tag = DW_TAG_subprogram;
17663   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17664   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17665                                current_function_funcdef_no);
17666   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17667
17668   /* Make it the first child of comp_unit_die ().  */
17669   die->die_parent = comp_unit_die ();
17670   if (comp_unit_die ()->die_child)
17671     {
17672       die->die_sib = comp_unit_die ()->die_child->die_sib;
17673       comp_unit_die ()->die_child->die_sib = die;
17674     }
17675   else
17676     {
17677       die->die_sib = die;
17678       comp_unit_die ()->die_child = die;
17679     }
17680 }
17681 #endif /* VMS_DEBUGGING_INFO */
17682
17683 /* Push a new declaration scope.  */
17684
17685 static void
17686 push_decl_scope (tree scope)
17687 {
17688   VEC_safe_push (tree, gc, decl_scope_table, scope);
17689 }
17690
17691 /* Pop a declaration scope.  */
17692
17693 static inline void
17694 pop_decl_scope (void)
17695 {
17696   VEC_pop (tree, decl_scope_table);
17697 }
17698
17699 /* Return the DIE for the scope that immediately contains this type.
17700    Non-named types get global scope.  Named types nested in other
17701    types get their containing scope if it's open, or global scope
17702    otherwise.  All other types (i.e. function-local named types) get
17703    the current active scope.  */
17704
17705 static dw_die_ref
17706 scope_die_for (tree t, dw_die_ref context_die)
17707 {
17708   dw_die_ref scope_die = NULL;
17709   tree containing_scope;
17710   int i;
17711
17712   /* Non-types always go in the current scope.  */
17713   gcc_assert (TYPE_P (t));
17714
17715   containing_scope = TYPE_CONTEXT (t);
17716
17717   /* Use the containing namespace if it was passed in (for a declaration).  */
17718   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17719     {
17720       if (context_die == lookup_decl_die (containing_scope))
17721         /* OK */;
17722       else
17723         containing_scope = NULL_TREE;
17724     }
17725
17726   /* Ignore function type "scopes" from the C frontend.  They mean that
17727      a tagged type is local to a parmlist of a function declarator, but
17728      that isn't useful to DWARF.  */
17729   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17730     containing_scope = NULL_TREE;
17731
17732   if (containing_scope == NULL_TREE)
17733     scope_die = comp_unit_die ();
17734   else if (TYPE_P (containing_scope))
17735     {
17736       /* For types, we can just look up the appropriate DIE.  But
17737          first we check to see if we're in the middle of emitting it
17738          so we know where the new DIE should go.  */
17739       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17740         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17741           break;
17742
17743       if (i < 0)
17744         {
17745           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17746                       || TREE_ASM_WRITTEN (containing_scope));
17747           /*We are not in the middle of emitting the type
17748             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17749           scope_die = lookup_type_die (containing_scope);
17750
17751           /* If none of the current dies are suitable, we get file scope.  */
17752           if (scope_die == NULL)
17753             scope_die = comp_unit_die ();
17754         }
17755       else
17756         scope_die = lookup_type_die (containing_scope);
17757     }
17758   else
17759     scope_die = context_die;
17760
17761   return scope_die;
17762 }
17763
17764 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17765
17766 static inline int
17767 local_scope_p (dw_die_ref context_die)
17768 {
17769   for (; context_die; context_die = context_die->die_parent)
17770     if (context_die->die_tag == DW_TAG_inlined_subroutine
17771         || context_die->die_tag == DW_TAG_subprogram)
17772       return 1;
17773
17774   return 0;
17775 }
17776
17777 /* Returns nonzero if CONTEXT_DIE is a class.  */
17778
17779 static inline int
17780 class_scope_p (dw_die_ref context_die)
17781 {
17782   return (context_die
17783           && (context_die->die_tag == DW_TAG_structure_type
17784               || context_die->die_tag == DW_TAG_class_type
17785               || context_die->die_tag == DW_TAG_interface_type
17786               || context_die->die_tag == DW_TAG_union_type));
17787 }
17788
17789 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17790    whether or not to treat a DIE in this context as a declaration.  */
17791
17792 static inline int
17793 class_or_namespace_scope_p (dw_die_ref context_die)
17794 {
17795   return (class_scope_p (context_die)
17796           || (context_die && context_die->die_tag == DW_TAG_namespace));
17797 }
17798
17799 /* Many forms of DIEs require a "type description" attribute.  This
17800    routine locates the proper "type descriptor" die for the type given
17801    by 'type', and adds a DW_AT_type attribute below the given die.  */
17802
17803 static void
17804 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17805                     int decl_volatile, dw_die_ref context_die)
17806 {
17807   enum tree_code code  = TREE_CODE (type);
17808   dw_die_ref type_die  = NULL;
17809
17810   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17811      or fixed-point type, use the inner type.  This is because we have no
17812      support for unnamed types in base_type_die.  This can happen if this is
17813      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17814   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17815       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17816     type = TREE_TYPE (type), code = TREE_CODE (type);
17817
17818   if (code == ERROR_MARK
17819       /* Handle a special case.  For functions whose return type is void, we
17820          generate *no* type attribute.  (Note that no object may have type
17821          `void', so this only applies to function return types).  */
17822       || code == VOID_TYPE)
17823     return;
17824
17825   type_die = modified_type_die (type,
17826                                 decl_const || TYPE_READONLY (type),
17827                                 decl_volatile || TYPE_VOLATILE (type),
17828                                 context_die);
17829
17830   if (type_die != NULL)
17831     add_AT_die_ref (object_die, DW_AT_type, type_die);
17832 }
17833
17834 /* Given an object die, add the calling convention attribute for the
17835    function call type.  */
17836 static void
17837 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17838 {
17839   enum dwarf_calling_convention value = DW_CC_normal;
17840
17841   value = ((enum dwarf_calling_convention)
17842            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17843
17844   /* DWARF doesn't provide a way to identify a program's source-level
17845      entry point.  DW_AT_calling_convention attributes are only meant
17846      to describe functions' calling conventions.  However, lacking a
17847      better way to signal the Fortran main program, we use this for the
17848      time being, following existing custom.  */
17849   if (is_fortran ()
17850       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17851     value = DW_CC_program;
17852
17853   /* Only add the attribute if the backend requests it, and
17854      is not DW_CC_normal.  */
17855   if (value && (value != DW_CC_normal))
17856     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17857 }
17858
17859 /* Given a tree pointer to a struct, class, union, or enum type node, return
17860    a pointer to the (string) tag name for the given type, or zero if the type
17861    was declared without a tag.  */
17862
17863 static const char *
17864 type_tag (const_tree type)
17865 {
17866   const char *name = 0;
17867
17868   if (TYPE_NAME (type) != 0)
17869     {
17870       tree t = 0;
17871
17872       /* Find the IDENTIFIER_NODE for the type name.  */
17873       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17874           && !TYPE_NAMELESS (type))
17875         t = TYPE_NAME (type);
17876
17877       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17878          a TYPE_DECL node, regardless of whether or not a `typedef' was
17879          involved.  */
17880       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17881                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17882         {
17883           /* We want to be extra verbose.  Don't call dwarf_name if
17884              DECL_NAME isn't set.  The default hook for decl_printable_name
17885              doesn't like that, and in this context it's correct to return
17886              0, instead of "<anonymous>" or the like.  */
17887           if (DECL_NAME (TYPE_NAME (type))
17888               && !DECL_NAMELESS (TYPE_NAME (type)))
17889             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17890         }
17891
17892       /* Now get the name as a string, or invent one.  */
17893       if (!name && t != 0)
17894         name = IDENTIFIER_POINTER (t);
17895     }
17896
17897   return (name == 0 || *name == '\0') ? 0 : name;
17898 }
17899
17900 /* Return the type associated with a data member, make a special check
17901    for bit field types.  */
17902
17903 static inline tree
17904 member_declared_type (const_tree member)
17905 {
17906   return (DECL_BIT_FIELD_TYPE (member)
17907           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17908 }
17909
17910 /* Get the decl's label, as described by its RTL. This may be different
17911    from the DECL_NAME name used in the source file.  */
17912
17913 #if 0
17914 static const char *
17915 decl_start_label (tree decl)
17916 {
17917   rtx x;
17918   const char *fnname;
17919
17920   x = DECL_RTL (decl);
17921   gcc_assert (MEM_P (x));
17922
17923   x = XEXP (x, 0);
17924   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17925
17926   fnname = XSTR (x, 0);
17927   return fnname;
17928 }
17929 #endif
17930 \f
17931 /* These routines generate the internal representation of the DIE's for
17932    the compilation unit.  Debugging information is collected by walking
17933    the declaration trees passed in from dwarf2out_decl().  */
17934
17935 static void
17936 gen_array_type_die (tree type, dw_die_ref context_die)
17937 {
17938   dw_die_ref scope_die = scope_die_for (type, context_die);
17939   dw_die_ref array_die;
17940
17941   /* GNU compilers represent multidimensional array types as sequences of one
17942      dimensional array types whose element types are themselves array types.
17943      We sometimes squish that down to a single array_type DIE with multiple
17944      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17945      say that we are allowed to do this kind of compression in C, because
17946      there is no difference between an array of arrays and a multidimensional
17947      array.  We don't do this for Ada to remain as close as possible to the
17948      actual representation, which is especially important against the language
17949      flexibilty wrt arrays of variable size.  */
17950
17951   bool collapse_nested_arrays = !is_ada ();
17952   tree element_type;
17953
17954   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17955      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17956   if (TYPE_STRING_FLAG (type)
17957       && TREE_CODE (type) == ARRAY_TYPE
17958       && is_fortran ()
17959       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17960     {
17961       HOST_WIDE_INT size;
17962
17963       array_die = new_die (DW_TAG_string_type, scope_die, type);
17964       add_name_attribute (array_die, type_tag (type));
17965       equate_type_number_to_die (type, array_die);
17966       size = int_size_in_bytes (type);
17967       if (size >= 0)
17968         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17969       else if (TYPE_DOMAIN (type) != NULL_TREE
17970                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17971                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17972         {
17973           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17974           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17975
17976           size = int_size_in_bytes (TREE_TYPE (szdecl));
17977           if (loc && size > 0)
17978             {
17979               add_AT_location_description (array_die, DW_AT_string_length, loc);
17980               if (size != DWARF2_ADDR_SIZE)
17981                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17982             }
17983         }
17984       return;
17985     }
17986
17987   /* ??? The SGI dwarf reader fails for array of array of enum types
17988      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17989      array type comes before the outer array type.  We thus call gen_type_die
17990      before we new_die and must prevent nested array types collapsing for this
17991      target.  */
17992
17993 #ifdef MIPS_DEBUGGING_INFO
17994   gen_type_die (TREE_TYPE (type), context_die);
17995   collapse_nested_arrays = false;
17996 #endif
17997
17998   array_die = new_die (DW_TAG_array_type, scope_die, type);
17999   add_name_attribute (array_die, type_tag (type));
18000   equate_type_number_to_die (type, array_die);
18001
18002   if (TREE_CODE (type) == VECTOR_TYPE)
18003     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18004
18005   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18006   if (is_fortran ()
18007       && TREE_CODE (type) == ARRAY_TYPE
18008       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18009       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18010     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18011
18012 #if 0
18013   /* We default the array ordering.  SDB will probably do
18014      the right things even if DW_AT_ordering is not present.  It's not even
18015      an issue until we start to get into multidimensional arrays anyway.  If
18016      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18017      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18018      and when we find out that we need to put these in, we will only do so
18019      for multidimensional arrays.  */
18020   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18021 #endif
18022
18023 #ifdef MIPS_DEBUGGING_INFO
18024   /* The SGI compilers handle arrays of unknown bound by setting
18025      AT_declaration and not emitting any subrange DIEs.  */
18026   if (TREE_CODE (type) == ARRAY_TYPE
18027       && ! TYPE_DOMAIN (type))
18028     add_AT_flag (array_die, DW_AT_declaration, 1);
18029   else
18030 #endif
18031   if (TREE_CODE (type) == VECTOR_TYPE)
18032     {
18033       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18034       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18035       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18036       add_bound_info (subrange_die, DW_AT_upper_bound,
18037                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18038     }
18039   else
18040     add_subscript_info (array_die, type, collapse_nested_arrays);
18041
18042   /* Add representation of the type of the elements of this array type and
18043      emit the corresponding DIE if we haven't done it already.  */
18044   element_type = TREE_TYPE (type);
18045   if (collapse_nested_arrays)
18046     while (TREE_CODE (element_type) == ARRAY_TYPE)
18047       {
18048         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18049           break;
18050         element_type = TREE_TYPE (element_type);
18051       }
18052
18053 #ifndef MIPS_DEBUGGING_INFO
18054   gen_type_die (element_type, context_die);
18055 #endif
18056
18057   add_type_attribute (array_die, element_type, 0, 0, context_die);
18058
18059   if (get_AT (array_die, DW_AT_name))
18060     add_pubtype (type, array_die);
18061 }
18062
18063 static dw_loc_descr_ref
18064 descr_info_loc (tree val, tree base_decl)
18065 {
18066   HOST_WIDE_INT size;
18067   dw_loc_descr_ref loc, loc2;
18068   enum dwarf_location_atom op;
18069
18070   if (val == base_decl)
18071     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18072
18073   switch (TREE_CODE (val))
18074     {
18075     CASE_CONVERT:
18076       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18077     case VAR_DECL:
18078       return loc_descriptor_from_tree (val, 0);
18079     case INTEGER_CST:
18080       if (host_integerp (val, 0))
18081         return int_loc_descriptor (tree_low_cst (val, 0));
18082       break;
18083     case INDIRECT_REF:
18084       size = int_size_in_bytes (TREE_TYPE (val));
18085       if (size < 0)
18086         break;
18087       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18088       if (!loc)
18089         break;
18090       if (size == DWARF2_ADDR_SIZE)
18091         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18092       else
18093         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18094       return loc;
18095     case POINTER_PLUS_EXPR:
18096     case PLUS_EXPR:
18097       if (host_integerp (TREE_OPERAND (val, 1), 1)
18098           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18099              < 16384)
18100         {
18101           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18102           if (!loc)
18103             break;
18104           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18105         }
18106       else
18107         {
18108           op = DW_OP_plus;
18109         do_binop:
18110           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18111           if (!loc)
18112             break;
18113           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18114           if (!loc2)
18115             break;
18116           add_loc_descr (&loc, loc2);
18117           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18118         }
18119       return loc;
18120     case MINUS_EXPR:
18121       op = DW_OP_minus;
18122       goto do_binop;
18123     case MULT_EXPR:
18124       op = DW_OP_mul;
18125       goto do_binop;
18126     case EQ_EXPR:
18127       op = DW_OP_eq;
18128       goto do_binop;
18129     case NE_EXPR:
18130       op = DW_OP_ne;
18131       goto do_binop;
18132     default:
18133       break;
18134     }
18135   return NULL;
18136 }
18137
18138 static void
18139 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18140                       tree val, tree base_decl)
18141 {
18142   dw_loc_descr_ref loc;
18143
18144   if (host_integerp (val, 0))
18145     {
18146       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18147       return;
18148     }
18149
18150   loc = descr_info_loc (val, base_decl);
18151   if (!loc)
18152     return;
18153
18154   add_AT_loc (die, attr, loc);
18155 }
18156
18157 /* This routine generates DIE for array with hidden descriptor, details
18158    are filled into *info by a langhook.  */
18159
18160 static void
18161 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18162                           dw_die_ref context_die)
18163 {
18164   dw_die_ref scope_die = scope_die_for (type, context_die);
18165   dw_die_ref array_die;
18166   int dim;
18167
18168   array_die = new_die (DW_TAG_array_type, scope_die, type);
18169   add_name_attribute (array_die, type_tag (type));
18170   equate_type_number_to_die (type, array_die);
18171
18172   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18173   if (is_fortran ()
18174       && info->ndimensions >= 2)
18175     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18176
18177   if (info->data_location)
18178     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18179                           info->base_decl);
18180   if (info->associated)
18181     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18182                           info->base_decl);
18183   if (info->allocated)
18184     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18185                           info->base_decl);
18186
18187   for (dim = 0; dim < info->ndimensions; dim++)
18188     {
18189       dw_die_ref subrange_die
18190         = new_die (DW_TAG_subrange_type, array_die, NULL);
18191
18192       if (info->dimen[dim].lower_bound)
18193         {
18194           /* If it is the default value, omit it.  */
18195           int dflt;
18196
18197           if (host_integerp (info->dimen[dim].lower_bound, 0)
18198               && (dflt = lower_bound_default ()) != -1
18199               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18200             ;
18201           else
18202             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18203                                   info->dimen[dim].lower_bound,
18204                                   info->base_decl);
18205         }
18206       if (info->dimen[dim].upper_bound)
18207         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18208                               info->dimen[dim].upper_bound,
18209                               info->base_decl);
18210       if (info->dimen[dim].stride)
18211         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18212                               info->dimen[dim].stride,
18213                               info->base_decl);
18214     }
18215
18216   gen_type_die (info->element_type, context_die);
18217   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18218
18219   if (get_AT (array_die, DW_AT_name))
18220     add_pubtype (type, array_die);
18221 }
18222
18223 #if 0
18224 static void
18225 gen_entry_point_die (tree decl, dw_die_ref context_die)
18226 {
18227   tree origin = decl_ultimate_origin (decl);
18228   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18229
18230   if (origin != NULL)
18231     add_abstract_origin_attribute (decl_die, origin);
18232   else
18233     {
18234       add_name_and_src_coords_attributes (decl_die, decl);
18235       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18236                           0, 0, context_die);
18237     }
18238
18239   if (DECL_ABSTRACT (decl))
18240     equate_decl_number_to_die (decl, decl_die);
18241   else
18242     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18243 }
18244 #endif
18245
18246 /* Walk through the list of incomplete types again, trying once more to
18247    emit full debugging info for them.  */
18248
18249 static void
18250 retry_incomplete_types (void)
18251 {
18252   int i;
18253
18254   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18255     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18256                                   DINFO_USAGE_DIR_USE))
18257       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18258 }
18259
18260 /* Determine what tag to use for a record type.  */
18261
18262 static enum dwarf_tag
18263 record_type_tag (tree type)
18264 {
18265   if (! lang_hooks.types.classify_record)
18266     return DW_TAG_structure_type;
18267
18268   switch (lang_hooks.types.classify_record (type))
18269     {
18270     case RECORD_IS_STRUCT:
18271       return DW_TAG_structure_type;
18272
18273     case RECORD_IS_CLASS:
18274       return DW_TAG_class_type;
18275
18276     case RECORD_IS_INTERFACE:
18277       if (dwarf_version >= 3 || !dwarf_strict)
18278         return DW_TAG_interface_type;
18279       return DW_TAG_structure_type;
18280
18281     default:
18282       gcc_unreachable ();
18283     }
18284 }
18285
18286 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18287    include all of the information about the enumeration values also. Each
18288    enumerated type name/value is listed as a child of the enumerated type
18289    DIE.  */
18290
18291 static dw_die_ref
18292 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18293 {
18294   dw_die_ref type_die = lookup_type_die (type);
18295
18296   if (type_die == NULL)
18297     {
18298       type_die = new_die (DW_TAG_enumeration_type,
18299                           scope_die_for (type, context_die), type);
18300       equate_type_number_to_die (type, type_die);
18301       add_name_attribute (type_die, type_tag (type));
18302       if ((dwarf_version >= 4 || !dwarf_strict)
18303           && ENUM_IS_SCOPED (type))
18304         add_AT_flag (type_die, DW_AT_enum_class, 1);
18305     }
18306   else if (! TYPE_SIZE (type))
18307     return type_die;
18308   else
18309     remove_AT (type_die, DW_AT_declaration);
18310
18311   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18312      given enum type is incomplete, do not generate the DW_AT_byte_size
18313      attribute or the DW_AT_element_list attribute.  */
18314   if (TYPE_SIZE (type))
18315     {
18316       tree link;
18317
18318       TREE_ASM_WRITTEN (type) = 1;
18319       add_byte_size_attribute (type_die, type);
18320       if (TYPE_STUB_DECL (type) != NULL_TREE)
18321         {
18322           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18323           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18324         }
18325
18326       /* If the first reference to this type was as the return type of an
18327          inline function, then it may not have a parent.  Fix this now.  */
18328       if (type_die->die_parent == NULL)
18329         add_child_die (scope_die_for (type, context_die), type_die);
18330
18331       for (link = TYPE_VALUES (type);
18332            link != NULL; link = TREE_CHAIN (link))
18333         {
18334           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18335           tree value = TREE_VALUE (link);
18336
18337           add_name_attribute (enum_die,
18338                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18339
18340           if (TREE_CODE (value) == CONST_DECL)
18341             value = DECL_INITIAL (value);
18342
18343           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18344             /* DWARF2 does not provide a way of indicating whether or
18345                not enumeration constants are signed or unsigned.  GDB
18346                always assumes the values are signed, so we output all
18347                values as if they were signed.  That means that
18348                enumeration constants with very large unsigned values
18349                will appear to have negative values in the debugger.  */
18350             add_AT_int (enum_die, DW_AT_const_value,
18351                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18352         }
18353     }
18354   else
18355     add_AT_flag (type_die, DW_AT_declaration, 1);
18356
18357   if (get_AT (type_die, DW_AT_name))
18358     add_pubtype (type, type_die);
18359
18360   return type_die;
18361 }
18362
18363 /* Generate a DIE to represent either a real live formal parameter decl or to
18364    represent just the type of some formal parameter position in some function
18365    type.
18366
18367    Note that this routine is a bit unusual because its argument may be a
18368    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18369    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18370    node.  If it's the former then this function is being called to output a
18371    DIE to represent a formal parameter object (or some inlining thereof).  If
18372    it's the latter, then this function is only being called to output a
18373    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18374    argument type of some subprogram type.
18375    If EMIT_NAME_P is true, name and source coordinate attributes
18376    are emitted.  */
18377
18378 static dw_die_ref
18379 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18380                           dw_die_ref context_die)
18381 {
18382   tree node_or_origin = node ? node : origin;
18383   tree ultimate_origin;
18384   dw_die_ref parm_die
18385     = new_die (DW_TAG_formal_parameter, context_die, node);
18386
18387   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18388     {
18389     case tcc_declaration:
18390       ultimate_origin = decl_ultimate_origin (node_or_origin);
18391       if (node || ultimate_origin)
18392         origin = ultimate_origin;
18393       if (origin != NULL)
18394         add_abstract_origin_attribute (parm_die, origin);
18395       else if (emit_name_p)
18396         add_name_and_src_coords_attributes (parm_die, node);
18397       if (origin == NULL
18398           || (! DECL_ABSTRACT (node_or_origin)
18399               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18400                                            decl_function_context
18401                                                             (node_or_origin))))
18402         {
18403           tree type = TREE_TYPE (node_or_origin);
18404           if (decl_by_reference_p (node_or_origin))
18405             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18406                                 context_die);
18407           else
18408             add_type_attribute (parm_die, type,
18409                                 TREE_READONLY (node_or_origin),
18410                                 TREE_THIS_VOLATILE (node_or_origin),
18411                                 context_die);
18412         }
18413       if (origin == NULL && DECL_ARTIFICIAL (node))
18414         add_AT_flag (parm_die, DW_AT_artificial, 1);
18415
18416       if (node && node != origin)
18417         equate_decl_number_to_die (node, parm_die);
18418       if (! DECL_ABSTRACT (node_or_origin))
18419         add_location_or_const_value_attribute (parm_die, node_or_origin,
18420                                                DW_AT_location);
18421
18422       break;
18423
18424     case tcc_type:
18425       /* We were called with some kind of a ..._TYPE node.  */
18426       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18427       break;
18428
18429     default:
18430       gcc_unreachable ();
18431     }
18432
18433   return parm_die;
18434 }
18435
18436 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18437    children DW_TAG_formal_parameter DIEs representing the arguments of the
18438    parameter pack.
18439
18440    PARM_PACK must be a function parameter pack.
18441    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18442    must point to the subsequent arguments of the function PACK_ARG belongs to.
18443    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18444    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18445    following the last one for which a DIE was generated.  */
18446
18447 static dw_die_ref
18448 gen_formal_parameter_pack_die  (tree parm_pack,
18449                                 tree pack_arg,
18450                                 dw_die_ref subr_die,
18451                                 tree *next_arg)
18452 {
18453   tree arg;
18454   dw_die_ref parm_pack_die;
18455
18456   gcc_assert (parm_pack
18457               && lang_hooks.function_parameter_pack_p (parm_pack)
18458               && subr_die);
18459
18460   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18461   add_src_coords_attributes (parm_pack_die, parm_pack);
18462
18463   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18464     {
18465       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18466                                                                  parm_pack))
18467         break;
18468       gen_formal_parameter_die (arg, NULL,
18469                                 false /* Don't emit name attribute.  */,
18470                                 parm_pack_die);
18471     }
18472   if (next_arg)
18473     *next_arg = arg;
18474   return parm_pack_die;
18475 }
18476
18477 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18478    at the end of an (ANSI prototyped) formal parameters list.  */
18479
18480 static void
18481 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18482 {
18483   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18484 }
18485
18486 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18487    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18488    parameters as specified in some function type specification (except for
18489    those which appear as part of a function *definition*).  */
18490
18491 static void
18492 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18493 {
18494   tree link;
18495   tree formal_type = NULL;
18496   tree first_parm_type;
18497   tree arg;
18498
18499   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18500     {
18501       arg = DECL_ARGUMENTS (function_or_method_type);
18502       function_or_method_type = TREE_TYPE (function_or_method_type);
18503     }
18504   else
18505     arg = NULL_TREE;
18506
18507   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18508
18509   /* Make our first pass over the list of formal parameter types and output a
18510      DW_TAG_formal_parameter DIE for each one.  */
18511   for (link = first_parm_type; link; )
18512     {
18513       dw_die_ref parm_die;
18514
18515       formal_type = TREE_VALUE (link);
18516       if (formal_type == void_type_node)
18517         break;
18518
18519       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18520       parm_die = gen_formal_parameter_die (formal_type, NULL,
18521                                            true /* Emit name attribute.  */,
18522                                            context_die);
18523       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18524           && link == first_parm_type)
18525         {
18526           add_AT_flag (parm_die, DW_AT_artificial, 1);
18527           if (dwarf_version >= 3 || !dwarf_strict)
18528             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18529         }
18530       else if (arg && DECL_ARTIFICIAL (arg))
18531         add_AT_flag (parm_die, DW_AT_artificial, 1);
18532
18533       link = TREE_CHAIN (link);
18534       if (arg)
18535         arg = DECL_CHAIN (arg);
18536     }
18537
18538   /* If this function type has an ellipsis, add a
18539      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18540   if (formal_type != void_type_node)
18541     gen_unspecified_parameters_die (function_or_method_type, context_die);
18542
18543   /* Make our second (and final) pass over the list of formal parameter types
18544      and output DIEs to represent those types (as necessary).  */
18545   for (link = TYPE_ARG_TYPES (function_or_method_type);
18546        link && TREE_VALUE (link);
18547        link = TREE_CHAIN (link))
18548     gen_type_die (TREE_VALUE (link), context_die);
18549 }
18550
18551 /* We want to generate the DIE for TYPE so that we can generate the
18552    die for MEMBER, which has been defined; we will need to refer back
18553    to the member declaration nested within TYPE.  If we're trying to
18554    generate minimal debug info for TYPE, processing TYPE won't do the
18555    trick; we need to attach the member declaration by hand.  */
18556
18557 static void
18558 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18559 {
18560   gen_type_die (type, context_die);
18561
18562   /* If we're trying to avoid duplicate debug info, we may not have
18563      emitted the member decl for this function.  Emit it now.  */
18564   if (TYPE_STUB_DECL (type)
18565       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18566       && ! lookup_decl_die (member))
18567     {
18568       dw_die_ref type_die;
18569       gcc_assert (!decl_ultimate_origin (member));
18570
18571       push_decl_scope (type);
18572       type_die = lookup_type_die (type);
18573       if (TREE_CODE (member) == FUNCTION_DECL)
18574         gen_subprogram_die (member, type_die);
18575       else if (TREE_CODE (member) == FIELD_DECL)
18576         {
18577           /* Ignore the nameless fields that are used to skip bits but handle
18578              C++ anonymous unions and structs.  */
18579           if (DECL_NAME (member) != NULL_TREE
18580               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18581               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18582             {
18583               gen_type_die (member_declared_type (member), type_die);
18584               gen_field_die (member, type_die);
18585             }
18586         }
18587       else
18588         gen_variable_die (member, NULL_TREE, type_die);
18589
18590       pop_decl_scope ();
18591     }
18592 }
18593
18594 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18595    may later generate inlined and/or out-of-line instances of.  */
18596
18597 static void
18598 dwarf2out_abstract_function (tree decl)
18599 {
18600   dw_die_ref old_die;
18601   tree save_fn;
18602   tree context;
18603   int was_abstract;
18604   htab_t old_decl_loc_table;
18605
18606   /* Make sure we have the actual abstract inline, not a clone.  */
18607   decl = DECL_ORIGIN (decl);
18608
18609   old_die = lookup_decl_die (decl);
18610   if (old_die && get_AT (old_die, DW_AT_inline))
18611     /* We've already generated the abstract instance.  */
18612     return;
18613
18614   /* We can be called while recursively when seeing block defining inlined subroutine
18615      DIE.  Be sure to not clobber the outer location table nor use it or we would
18616      get locations in abstract instantces.  */
18617   old_decl_loc_table = decl_loc_table;
18618   decl_loc_table = NULL;
18619
18620   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18621      we don't get confused by DECL_ABSTRACT.  */
18622   if (debug_info_level > DINFO_LEVEL_TERSE)
18623     {
18624       context = decl_class_context (decl);
18625       if (context)
18626         gen_type_die_for_member
18627           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18628     }
18629
18630   /* Pretend we've just finished compiling this function.  */
18631   save_fn = current_function_decl;
18632   current_function_decl = decl;
18633   push_cfun (DECL_STRUCT_FUNCTION (decl));
18634
18635   was_abstract = DECL_ABSTRACT (decl);
18636   set_decl_abstract_flags (decl, 1);
18637   dwarf2out_decl (decl);
18638   if (! was_abstract)
18639     set_decl_abstract_flags (decl, 0);
18640
18641   current_function_decl = save_fn;
18642   decl_loc_table = old_decl_loc_table;
18643   pop_cfun ();
18644 }
18645
18646 /* Helper function of premark_used_types() which gets called through
18647    htab_traverse.
18648
18649    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18650    marked as unused by prune_unused_types.  */
18651
18652 static int
18653 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18654 {
18655   tree type;
18656   dw_die_ref die;
18657
18658   type = (tree) *slot;
18659   die = lookup_type_die (type);
18660   if (die != NULL)
18661     die->die_perennial_p = 1;
18662   return 1;
18663 }
18664
18665 /* Helper function of premark_types_used_by_global_vars which gets called
18666    through htab_traverse.
18667
18668    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18669    marked as unused by prune_unused_types. The DIE of the type is marked
18670    only if the global variable using the type will actually be emitted.  */
18671
18672 static int
18673 premark_types_used_by_global_vars_helper (void **slot,
18674                                           void *data ATTRIBUTE_UNUSED)
18675 {
18676   struct types_used_by_vars_entry *entry;
18677   dw_die_ref die;
18678
18679   entry = (struct types_used_by_vars_entry *) *slot;
18680   gcc_assert (entry->type != NULL
18681               && entry->var_decl != NULL);
18682   die = lookup_type_die (entry->type);
18683   if (die)
18684     {
18685       /* Ask cgraph if the global variable really is to be emitted.
18686          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18687       struct varpool_node *node = varpool_get_node (entry->var_decl);
18688       if (node && node->needed)
18689         {
18690           die->die_perennial_p = 1;
18691           /* Keep the parent DIEs as well.  */
18692           while ((die = die->die_parent) && die->die_perennial_p == 0)
18693             die->die_perennial_p = 1;
18694         }
18695     }
18696   return 1;
18697 }
18698
18699 /* Mark all members of used_types_hash as perennial.  */
18700
18701 static void
18702 premark_used_types (void)
18703 {
18704   if (cfun && cfun->used_types_hash)
18705     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18706 }
18707
18708 /* Mark all members of types_used_by_vars_entry as perennial.  */
18709
18710 static void
18711 premark_types_used_by_global_vars (void)
18712 {
18713   if (types_used_by_vars_hash)
18714     htab_traverse (types_used_by_vars_hash,
18715                    premark_types_used_by_global_vars_helper, NULL);
18716 }
18717
18718 /* Generate a DIE to represent a declared function (either file-scope or
18719    block-local).  */
18720
18721 static void
18722 gen_subprogram_die (tree decl, dw_die_ref context_die)
18723 {
18724   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18725   tree origin = decl_ultimate_origin (decl);
18726   dw_die_ref subr_die;
18727   tree fn_arg_types;
18728   tree outer_scope;
18729   dw_die_ref old_die = lookup_decl_die (decl);
18730   int declaration = (current_function_decl != decl
18731                      || class_or_namespace_scope_p (context_die));
18732
18733   premark_used_types ();
18734
18735   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18736      started to generate the abstract instance of an inline, decided to output
18737      its containing class, and proceeded to emit the declaration of the inline
18738      from the member list for the class.  If so, DECLARATION takes priority;
18739      we'll get back to the abstract instance when done with the class.  */
18740
18741   /* The class-scope declaration DIE must be the primary DIE.  */
18742   if (origin && declaration && class_or_namespace_scope_p (context_die))
18743     {
18744       origin = NULL;
18745       gcc_assert (!old_die);
18746     }
18747
18748   /* Now that the C++ front end lazily declares artificial member fns, we
18749      might need to retrofit the declaration into its class.  */
18750   if (!declaration && !origin && !old_die
18751       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18752       && !class_or_namespace_scope_p (context_die)
18753       && debug_info_level > DINFO_LEVEL_TERSE)
18754     old_die = force_decl_die (decl);
18755
18756   if (origin != NULL)
18757     {
18758       gcc_assert (!declaration || local_scope_p (context_die));
18759
18760       /* Fixup die_parent for the abstract instance of a nested
18761          inline function.  */
18762       if (old_die && old_die->die_parent == NULL)
18763         add_child_die (context_die, old_die);
18764
18765       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18766       add_abstract_origin_attribute (subr_die, origin);
18767     }
18768   else if (old_die)
18769     {
18770       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18771       struct dwarf_file_data * file_index = lookup_filename (s.file);
18772
18773       if (!get_AT_flag (old_die, DW_AT_declaration)
18774           /* We can have a normal definition following an inline one in the
18775              case of redefinition of GNU C extern inlines.
18776              It seems reasonable to use AT_specification in this case.  */
18777           && !get_AT (old_die, DW_AT_inline))
18778         {
18779           /* Detect and ignore this case, where we are trying to output
18780              something we have already output.  */
18781           return;
18782         }
18783
18784       /* If the definition comes from the same place as the declaration,
18785          maybe use the old DIE.  We always want the DIE for this function
18786          that has the *_pc attributes to be under comp_unit_die so the
18787          debugger can find it.  We also need to do this for abstract
18788          instances of inlines, since the spec requires the out-of-line copy
18789          to have the same parent.  For local class methods, this doesn't
18790          apply; we just use the old DIE.  */
18791       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18792           && (DECL_ARTIFICIAL (decl)
18793               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18794                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18795                       == (unsigned) s.line))))
18796         {
18797           subr_die = old_die;
18798
18799           /* Clear out the declaration attribute and the formal parameters.
18800              Do not remove all children, because it is possible that this
18801              declaration die was forced using force_decl_die(). In such
18802              cases die that forced declaration die (e.g. TAG_imported_module)
18803              is one of the children that we do not want to remove.  */
18804           remove_AT (subr_die, DW_AT_declaration);
18805           remove_AT (subr_die, DW_AT_object_pointer);
18806           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18807         }
18808       else
18809         {
18810           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18811           add_AT_specification (subr_die, old_die);
18812           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18813             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18814           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18815             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18816         }
18817     }
18818   else
18819     {
18820       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18821
18822       if (TREE_PUBLIC (decl))
18823         add_AT_flag (subr_die, DW_AT_external, 1);
18824
18825       add_name_and_src_coords_attributes (subr_die, decl);
18826       if (debug_info_level > DINFO_LEVEL_TERSE)
18827         {
18828           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18829           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18830                               0, 0, context_die);
18831         }
18832
18833       add_pure_or_virtual_attribute (subr_die, decl);
18834       if (DECL_ARTIFICIAL (decl))
18835         add_AT_flag (subr_die, DW_AT_artificial, 1);
18836
18837       add_accessibility_attribute (subr_die, decl);
18838     }
18839
18840   if (declaration)
18841     {
18842       if (!old_die || !get_AT (old_die, DW_AT_inline))
18843         {
18844           add_AT_flag (subr_die, DW_AT_declaration, 1);
18845
18846           /* If this is an explicit function declaration then generate
18847              a DW_AT_explicit attribute.  */
18848           if (lang_hooks.decls.function_decl_explicit_p (decl)
18849               && (dwarf_version >= 3 || !dwarf_strict))
18850             add_AT_flag (subr_die, DW_AT_explicit, 1);
18851
18852           /* The first time we see a member function, it is in the context of
18853              the class to which it belongs.  We make sure of this by emitting
18854              the class first.  The next time is the definition, which is
18855              handled above.  The two may come from the same source text.
18856
18857              Note that force_decl_die() forces function declaration die. It is
18858              later reused to represent definition.  */
18859           equate_decl_number_to_die (decl, subr_die);
18860         }
18861     }
18862   else if (DECL_ABSTRACT (decl))
18863     {
18864       if (DECL_DECLARED_INLINE_P (decl))
18865         {
18866           if (cgraph_function_possibly_inlined_p (decl))
18867             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18868           else
18869             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18870         }
18871       else
18872         {
18873           if (cgraph_function_possibly_inlined_p (decl))
18874             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18875           else
18876             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18877         }
18878
18879       if (DECL_DECLARED_INLINE_P (decl)
18880           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18881         add_AT_flag (subr_die, DW_AT_artificial, 1);
18882
18883       equate_decl_number_to_die (decl, subr_die);
18884     }
18885   else if (!DECL_EXTERNAL (decl))
18886     {
18887       HOST_WIDE_INT cfa_fb_offset;
18888
18889       if (!old_die || !get_AT (old_die, DW_AT_inline))
18890         equate_decl_number_to_die (decl, subr_die);
18891
18892       if (!flag_reorder_blocks_and_partition)
18893         {
18894           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18895                                        current_function_funcdef_no);
18896           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18897           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18898                                        current_function_funcdef_no);
18899           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18900
18901 #if VMS_DEBUGGING_INFO
18902       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18903          Section 2.3 Prologue and Epilogue Attributes:
18904          When a breakpoint is set on entry to a function, it is generally
18905          desirable for execution to be suspended, not on the very first
18906          instruction of the function, but rather at a point after the
18907          function's frame has been set up, after any language defined local
18908          declaration processing has been completed, and before execution of
18909          the first statement of the function begins. Debuggers generally
18910          cannot properly determine where this point is.  Similarly for a
18911          breakpoint set on exit from a function. The prologue and epilogue
18912          attributes allow a compiler to communicate the location(s) to use.  */
18913
18914       {
18915         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18916
18917         if (fde->dw_fde_vms_end_prologue)
18918           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18919             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18920
18921         if (fde->dw_fde_vms_begin_epilogue)
18922           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18923             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18924       }
18925 #endif
18926
18927           add_pubname (decl, subr_die);
18928           add_arange (decl, subr_die);
18929         }
18930       else
18931         {  /* Do nothing for now; maybe need to duplicate die, one for
18932               hot section and one for cold section, then use the hot/cold
18933               section begin/end labels to generate the aranges...  */
18934           /*
18935             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18936             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18937             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18938             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18939
18940             add_pubname (decl, subr_die);
18941             add_arange (decl, subr_die);
18942             add_arange (decl, subr_die);
18943            */
18944         }
18945
18946 #ifdef MIPS_DEBUGGING_INFO
18947       /* Add a reference to the FDE for this routine.  */
18948       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18949 #endif
18950
18951       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18952
18953       /* We define the "frame base" as the function's CFA.  This is more
18954          convenient for several reasons: (1) It's stable across the prologue
18955          and epilogue, which makes it better than just a frame pointer,
18956          (2) With dwarf3, there exists a one-byte encoding that allows us
18957          to reference the .debug_frame data by proxy, but failing that,
18958          (3) We can at least reuse the code inspection and interpretation
18959          code that determines the CFA position at various points in the
18960          function.  */
18961       if (dwarf_version >= 3)
18962         {
18963           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18964           add_AT_loc (subr_die, DW_AT_frame_base, op);
18965         }
18966       else
18967         {
18968           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18969           if (list->dw_loc_next)
18970             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18971           else
18972             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18973         }
18974
18975       /* Compute a displacement from the "steady-state frame pointer" to
18976          the CFA.  The former is what all stack slots and argument slots
18977          will reference in the rtl; the later is what we've told the
18978          debugger about.  We'll need to adjust all frame_base references
18979          by this displacement.  */
18980       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18981
18982       if (cfun->static_chain_decl)
18983         add_AT_location_description (subr_die, DW_AT_static_link,
18984                  loc_list_from_tree (cfun->static_chain_decl, 2));
18985     }
18986
18987   /* Generate child dies for template paramaters.  */
18988   if (debug_info_level > DINFO_LEVEL_TERSE)
18989     gen_generic_params_dies (decl);
18990
18991   /* Now output descriptions of the arguments for this function. This gets
18992      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18993      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18994      `...' at the end of the formal parameter list.  In order to find out if
18995      there was a trailing ellipsis or not, we must instead look at the type
18996      associated with the FUNCTION_DECL.  This will be a node of type
18997      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18998      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18999      an ellipsis at the end.  */
19000
19001   /* In the case where we are describing a mere function declaration, all we
19002      need to do here (and all we *can* do here) is to describe the *types* of
19003      its formal parameters.  */
19004   if (debug_info_level <= DINFO_LEVEL_TERSE)
19005     ;
19006   else if (declaration)
19007     gen_formal_types_die (decl, subr_die);
19008   else
19009     {
19010       /* Generate DIEs to represent all known formal parameters.  */
19011       tree parm = DECL_ARGUMENTS (decl);
19012       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19013       tree generic_decl_parm = generic_decl
19014                                 ? DECL_ARGUMENTS (generic_decl)
19015                                 : NULL;
19016
19017       /* Now we want to walk the list of parameters of the function and
19018          emit their relevant DIEs.
19019
19020          We consider the case of DECL being an instance of a generic function
19021          as well as it being a normal function.
19022
19023          If DECL is an instance of a generic function we walk the
19024          parameters of the generic function declaration _and_ the parameters of
19025          DECL itself. This is useful because we want to emit specific DIEs for
19026          function parameter packs and those are declared as part of the
19027          generic function declaration. In that particular case,
19028          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19029          That DIE has children DIEs representing the set of arguments
19030          of the pack. Note that the set of pack arguments can be empty.
19031          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19032          children DIE.
19033
19034          Otherwise, we just consider the parameters of DECL.  */
19035       while (generic_decl_parm || parm)
19036         {
19037           if (generic_decl_parm
19038               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19039             gen_formal_parameter_pack_die (generic_decl_parm,
19040                                            parm, subr_die,
19041                                            &parm);
19042           else if (parm)
19043             {
19044               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19045
19046               if (parm == DECL_ARGUMENTS (decl)
19047                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19048                   && parm_die
19049                   && (dwarf_version >= 3 || !dwarf_strict))
19050                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19051
19052               parm = DECL_CHAIN (parm);
19053             }
19054
19055           if (generic_decl_parm)
19056             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19057         }
19058
19059       /* Decide whether we need an unspecified_parameters DIE at the end.
19060          There are 2 more cases to do this for: 1) the ansi ... declaration -
19061          this is detectable when the end of the arg list is not a
19062          void_type_node 2) an unprototyped function declaration (not a
19063          definition).  This just means that we have no info about the
19064          parameters at all.  */
19065       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19066       if (fn_arg_types != NULL)
19067         {
19068           /* This is the prototyped case, check for....  */
19069           if (stdarg_p (TREE_TYPE (decl)))
19070             gen_unspecified_parameters_die (decl, subr_die);
19071         }
19072       else if (DECL_INITIAL (decl) == NULL_TREE)
19073         gen_unspecified_parameters_die (decl, subr_die);
19074     }
19075
19076   /* Output Dwarf info for all of the stuff within the body of the function
19077      (if it has one - it may be just a declaration).  */
19078   outer_scope = DECL_INITIAL (decl);
19079
19080   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19081      a function.  This BLOCK actually represents the outermost binding contour
19082      for the function, i.e. the contour in which the function's formal
19083      parameters and labels get declared. Curiously, it appears that the front
19084      end doesn't actually put the PARM_DECL nodes for the current function onto
19085      the BLOCK_VARS list for this outer scope, but are strung off of the
19086      DECL_ARGUMENTS list for the function instead.
19087
19088      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19089      the LABEL_DECL nodes for the function however, and we output DWARF info
19090      for those in decls_for_scope.  Just within the `outer_scope' there will be
19091      a BLOCK node representing the function's outermost pair of curly braces,
19092      and any blocks used for the base and member initializers of a C++
19093      constructor function.  */
19094   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19095     {
19096       /* Emit a DW_TAG_variable DIE for a named return value.  */
19097       if (DECL_NAME (DECL_RESULT (decl)))
19098         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19099
19100       current_function_has_inlines = 0;
19101       decls_for_scope (outer_scope, subr_die, 0);
19102     }
19103   /* Add the calling convention attribute if requested.  */
19104   add_calling_convention_attribute (subr_die, decl);
19105
19106 }
19107
19108 /* Returns a hash value for X (which really is a die_struct).  */
19109
19110 static hashval_t
19111 common_block_die_table_hash (const void *x)
19112 {
19113   const_dw_die_ref d = (const_dw_die_ref) x;
19114   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19115 }
19116
19117 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19118    as decl_id and die_parent of die_struct Y.  */
19119
19120 static int
19121 common_block_die_table_eq (const void *x, const void *y)
19122 {
19123   const_dw_die_ref d = (const_dw_die_ref) x;
19124   const_dw_die_ref e = (const_dw_die_ref) y;
19125   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19126 }
19127
19128 /* Generate a DIE to represent a declared data object.
19129    Either DECL or ORIGIN must be non-null.  */
19130
19131 static void
19132 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19133 {
19134   HOST_WIDE_INT off;
19135   tree com_decl;
19136   tree decl_or_origin = decl ? decl : origin;
19137   tree ultimate_origin;
19138   dw_die_ref var_die;
19139   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19140   dw_die_ref origin_die;
19141   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19142                       || class_or_namespace_scope_p (context_die));
19143   bool specialization_p = false;
19144
19145   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19146   if (decl || ultimate_origin)
19147     origin = ultimate_origin;
19148   com_decl = fortran_common (decl_or_origin, &off);
19149
19150   /* Symbol in common gets emitted as a child of the common block, in the form
19151      of a data member.  */
19152   if (com_decl)
19153     {
19154       dw_die_ref com_die;
19155       dw_loc_list_ref loc;
19156       die_node com_die_arg;
19157
19158       var_die = lookup_decl_die (decl_or_origin);
19159       if (var_die)
19160         {
19161           if (get_AT (var_die, DW_AT_location) == NULL)
19162             {
19163               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19164               if (loc)
19165                 {
19166                   if (off)
19167                     {
19168                       /* Optimize the common case.  */
19169                       if (single_element_loc_list_p (loc)
19170                           && loc->expr->dw_loc_opc == DW_OP_addr
19171                           && loc->expr->dw_loc_next == NULL
19172                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19173                              == SYMBOL_REF)
19174                         loc->expr->dw_loc_oprnd1.v.val_addr
19175                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19176                         else
19177                           loc_list_plus_const (loc, off);
19178                     }
19179                   add_AT_location_description (var_die, DW_AT_location, loc);
19180                   remove_AT (var_die, DW_AT_declaration);
19181                 }
19182             }
19183           return;
19184         }
19185
19186       if (common_block_die_table == NULL)
19187         common_block_die_table
19188           = htab_create_ggc (10, common_block_die_table_hash,
19189                              common_block_die_table_eq, NULL);
19190
19191       com_die_arg.decl_id = DECL_UID (com_decl);
19192       com_die_arg.die_parent = context_die;
19193       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19194       loc = loc_list_from_tree (com_decl, 2);
19195       if (com_die == NULL)
19196         {
19197           const char *cnam
19198             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19199           void **slot;
19200
19201           com_die = new_die (DW_TAG_common_block, context_die, decl);
19202           add_name_and_src_coords_attributes (com_die, com_decl);
19203           if (loc)
19204             {
19205               add_AT_location_description (com_die, DW_AT_location, loc);
19206               /* Avoid sharing the same loc descriptor between
19207                  DW_TAG_common_block and DW_TAG_variable.  */
19208               loc = loc_list_from_tree (com_decl, 2);
19209             }
19210           else if (DECL_EXTERNAL (decl))
19211             add_AT_flag (com_die, DW_AT_declaration, 1);
19212           add_pubname_string (cnam, com_die); /* ??? needed? */
19213           com_die->decl_id = DECL_UID (com_decl);
19214           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19215           *slot = (void *) com_die;
19216         }
19217       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19218         {
19219           add_AT_location_description (com_die, DW_AT_location, loc);
19220           loc = loc_list_from_tree (com_decl, 2);
19221           remove_AT (com_die, DW_AT_declaration);
19222         }
19223       var_die = new_die (DW_TAG_variable, com_die, decl);
19224       add_name_and_src_coords_attributes (var_die, decl);
19225       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19226                           TREE_THIS_VOLATILE (decl), context_die);
19227       add_AT_flag (var_die, DW_AT_external, 1);
19228       if (loc)
19229         {
19230           if (off)
19231             {
19232               /* Optimize the common case.  */
19233               if (single_element_loc_list_p (loc)
19234                   && loc->expr->dw_loc_opc == DW_OP_addr
19235                   && loc->expr->dw_loc_next == NULL
19236                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19237                 loc->expr->dw_loc_oprnd1.v.val_addr
19238                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19239               else
19240                 loc_list_plus_const (loc, off);
19241             }
19242           add_AT_location_description (var_die, DW_AT_location, loc);
19243         }
19244       else if (DECL_EXTERNAL (decl))
19245         add_AT_flag (var_die, DW_AT_declaration, 1);
19246       equate_decl_number_to_die (decl, var_die);
19247       return;
19248     }
19249
19250   /* If the compiler emitted a definition for the DECL declaration
19251      and if we already emitted a DIE for it, don't emit a second
19252      DIE for it again. Allow re-declarations of DECLs that are
19253      inside functions, though.  */
19254   if (old_die && declaration && !local_scope_p (context_die))
19255     return;
19256
19257   /* For static data members, the declaration in the class is supposed
19258      to have DW_TAG_member tag; the specification should still be
19259      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19260   if (declaration && class_scope_p (context_die))
19261     var_die = new_die (DW_TAG_member, context_die, decl);
19262   else
19263     var_die = new_die (DW_TAG_variable, context_die, decl);
19264
19265   origin_die = NULL;
19266   if (origin != NULL)
19267     origin_die = add_abstract_origin_attribute (var_die, origin);
19268
19269   /* Loop unrolling can create multiple blocks that refer to the same
19270      static variable, so we must test for the DW_AT_declaration flag.
19271
19272      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19273      copy decls and set the DECL_ABSTRACT flag on them instead of
19274      sharing them.
19275
19276      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19277
19278      ??? The declare_in_namespace support causes us to get two DIEs for one
19279      variable, both of which are declarations.  We want to avoid considering
19280      one to be a specification, so we must test that this DIE is not a
19281      declaration.  */
19282   else if (old_die && TREE_STATIC (decl) && ! declaration
19283            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19284     {
19285       /* This is a definition of a C++ class level static.  */
19286       add_AT_specification (var_die, old_die);
19287       specialization_p = true;
19288       if (DECL_NAME (decl))
19289         {
19290           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19291           struct dwarf_file_data * file_index = lookup_filename (s.file);
19292
19293           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19294             add_AT_file (var_die, DW_AT_decl_file, file_index);
19295
19296           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19297             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19298
19299           if (old_die->die_tag == DW_TAG_member)
19300             add_linkage_name (var_die, decl);
19301         }
19302     }
19303   else
19304     add_name_and_src_coords_attributes (var_die, decl);
19305
19306   if ((origin == NULL && !specialization_p)
19307       || (origin != NULL
19308           && !DECL_ABSTRACT (decl_or_origin)
19309           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19310                                        decl_function_context
19311                                                         (decl_or_origin))))
19312     {
19313       tree type = TREE_TYPE (decl_or_origin);
19314
19315       if (decl_by_reference_p (decl_or_origin))
19316         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19317       else
19318         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19319                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19320     }
19321
19322   if (origin == NULL && !specialization_p)
19323     {
19324       if (TREE_PUBLIC (decl))
19325         add_AT_flag (var_die, DW_AT_external, 1);
19326
19327       if (DECL_ARTIFICIAL (decl))
19328         add_AT_flag (var_die, DW_AT_artificial, 1);
19329
19330       add_accessibility_attribute (var_die, decl);
19331     }
19332
19333   if (declaration)
19334     add_AT_flag (var_die, DW_AT_declaration, 1);
19335
19336   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19337     equate_decl_number_to_die (decl, var_die);
19338
19339   if (! declaration
19340       && (! DECL_ABSTRACT (decl_or_origin)
19341           /* Local static vars are shared between all clones/inlines,
19342              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19343              already set.  */
19344           || (TREE_CODE (decl_or_origin) == VAR_DECL
19345               && TREE_STATIC (decl_or_origin)
19346               && DECL_RTL_SET_P (decl_or_origin)))
19347       /* When abstract origin already has DW_AT_location attribute, no need
19348          to add it again.  */
19349       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19350     {
19351       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19352           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19353         defer_location (decl_or_origin, var_die);
19354       else
19355         add_location_or_const_value_attribute (var_die,
19356                                                decl_or_origin,
19357                                                DW_AT_location);
19358       add_pubname (decl_or_origin, var_die);
19359     }
19360   else
19361     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19362 }
19363
19364 /* Generate a DIE to represent a named constant.  */
19365
19366 static void
19367 gen_const_die (tree decl, dw_die_ref context_die)
19368 {
19369   dw_die_ref const_die;
19370   tree type = TREE_TYPE (decl);
19371
19372   const_die = new_die (DW_TAG_constant, context_die, decl);
19373   add_name_and_src_coords_attributes (const_die, decl);
19374   add_type_attribute (const_die, type, 1, 0, context_die);
19375   if (TREE_PUBLIC (decl))
19376     add_AT_flag (const_die, DW_AT_external, 1);
19377   if (DECL_ARTIFICIAL (decl))
19378     add_AT_flag (const_die, DW_AT_artificial, 1);
19379   tree_add_const_value_attribute_for_decl (const_die, decl);
19380 }
19381
19382 /* Generate a DIE to represent a label identifier.  */
19383
19384 static void
19385 gen_label_die (tree decl, dw_die_ref context_die)
19386 {
19387   tree origin = decl_ultimate_origin (decl);
19388   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19389   rtx insn;
19390   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19391
19392   if (origin != NULL)
19393     add_abstract_origin_attribute (lbl_die, origin);
19394   else
19395     add_name_and_src_coords_attributes (lbl_die, decl);
19396
19397   if (DECL_ABSTRACT (decl))
19398     equate_decl_number_to_die (decl, lbl_die);
19399   else
19400     {
19401       insn = DECL_RTL_IF_SET (decl);
19402
19403       /* Deleted labels are programmer specified labels which have been
19404          eliminated because of various optimizations.  We still emit them
19405          here so that it is possible to put breakpoints on them.  */
19406       if (insn
19407           && (LABEL_P (insn)
19408               || ((NOTE_P (insn)
19409                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19410         {
19411           /* When optimization is enabled (via -O) some parts of the compiler
19412              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19413              represent source-level labels which were explicitly declared by
19414              the user.  This really shouldn't be happening though, so catch
19415              it if it ever does happen.  */
19416           gcc_assert (!INSN_DELETED_P (insn));
19417
19418           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19419           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19420         }
19421     }
19422 }
19423
19424 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19425    attributes to the DIE for a block STMT, to describe where the inlined
19426    function was called from.  This is similar to add_src_coords_attributes.  */
19427
19428 static inline void
19429 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19430 {
19431   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19432
19433   if (dwarf_version >= 3 || !dwarf_strict)
19434     {
19435       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19436       add_AT_unsigned (die, DW_AT_call_line, s.line);
19437     }
19438 }
19439
19440
19441 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19442    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19443
19444 static inline void
19445 add_high_low_attributes (tree stmt, dw_die_ref die)
19446 {
19447   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19448
19449   if (BLOCK_FRAGMENT_CHAIN (stmt)
19450       && (dwarf_version >= 3 || !dwarf_strict))
19451     {
19452       tree chain;
19453
19454       if (inlined_function_outer_scope_p (stmt))
19455         {
19456           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19457                                        BLOCK_NUMBER (stmt));
19458           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19459         }
19460
19461       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19462
19463       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19464       do
19465         {
19466           add_ranges (chain);
19467           chain = BLOCK_FRAGMENT_CHAIN (chain);
19468         }
19469       while (chain);
19470       add_ranges (NULL);
19471     }
19472   else
19473     {
19474       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19475                                    BLOCK_NUMBER (stmt));
19476       add_AT_lbl_id (die, DW_AT_low_pc, label);
19477       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19478                                    BLOCK_NUMBER (stmt));
19479       add_AT_lbl_id (die, DW_AT_high_pc, label);
19480     }
19481 }
19482
19483 /* Generate a DIE for a lexical block.  */
19484
19485 static void
19486 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19487 {
19488   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19489
19490   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19491     add_high_low_attributes (stmt, stmt_die);
19492
19493   decls_for_scope (stmt, stmt_die, depth);
19494 }
19495
19496 /* Generate a DIE for an inlined subprogram.  */
19497
19498 static void
19499 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19500 {
19501   tree decl;
19502
19503   /* The instance of function that is effectively being inlined shall not
19504      be abstract.  */
19505   gcc_assert (! BLOCK_ABSTRACT (stmt));
19506
19507   decl = block_ultimate_origin (stmt);
19508
19509   /* Emit info for the abstract instance first, if we haven't yet.  We
19510      must emit this even if the block is abstract, otherwise when we
19511      emit the block below (or elsewhere), we may end up trying to emit
19512      a die whose origin die hasn't been emitted, and crashing.  */
19513   dwarf2out_abstract_function (decl);
19514
19515   if (! BLOCK_ABSTRACT (stmt))
19516     {
19517       dw_die_ref subr_die
19518         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19519
19520       add_abstract_origin_attribute (subr_die, decl);
19521       if (TREE_ASM_WRITTEN (stmt))
19522         add_high_low_attributes (stmt, subr_die);
19523       add_call_src_coords_attributes (stmt, subr_die);
19524
19525       decls_for_scope (stmt, subr_die, depth);
19526       current_function_has_inlines = 1;
19527     }
19528 }
19529
19530 /* Generate a DIE for a field in a record, or structure.  */
19531
19532 static void
19533 gen_field_die (tree decl, dw_die_ref context_die)
19534 {
19535   dw_die_ref decl_die;
19536
19537   if (TREE_TYPE (decl) == error_mark_node)
19538     return;
19539
19540   decl_die = new_die (DW_TAG_member, context_die, decl);
19541   add_name_and_src_coords_attributes (decl_die, decl);
19542   add_type_attribute (decl_die, member_declared_type (decl),
19543                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19544                       context_die);
19545
19546   if (DECL_BIT_FIELD_TYPE (decl))
19547     {
19548       add_byte_size_attribute (decl_die, decl);
19549       add_bit_size_attribute (decl_die, decl);
19550       add_bit_offset_attribute (decl_die, decl);
19551     }
19552
19553   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19554     add_data_member_location_attribute (decl_die, decl);
19555
19556   if (DECL_ARTIFICIAL (decl))
19557     add_AT_flag (decl_die, DW_AT_artificial, 1);
19558
19559   add_accessibility_attribute (decl_die, decl);
19560
19561   /* Equate decl number to die, so that we can look up this decl later on.  */
19562   equate_decl_number_to_die (decl, decl_die);
19563 }
19564
19565 #if 0
19566 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19567    Use modified_type_die instead.
19568    We keep this code here just in case these types of DIEs may be needed to
19569    represent certain things in other languages (e.g. Pascal) someday.  */
19570
19571 static void
19572 gen_pointer_type_die (tree type, dw_die_ref context_die)
19573 {
19574   dw_die_ref ptr_die
19575     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19576
19577   equate_type_number_to_die (type, ptr_die);
19578   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19579   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19580 }
19581
19582 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19583    Use modified_type_die instead.
19584    We keep this code here just in case these types of DIEs may be needed to
19585    represent certain things in other languages (e.g. Pascal) someday.  */
19586
19587 static void
19588 gen_reference_type_die (tree type, dw_die_ref context_die)
19589 {
19590   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19591
19592   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19593     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19594   else
19595     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19596
19597   equate_type_number_to_die (type, ref_die);
19598   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19599   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19600 }
19601 #endif
19602
19603 /* Generate a DIE for a pointer to a member type.  */
19604
19605 static void
19606 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19607 {
19608   dw_die_ref ptr_die
19609     = new_die (DW_TAG_ptr_to_member_type,
19610                scope_die_for (type, context_die), type);
19611
19612   equate_type_number_to_die (type, ptr_die);
19613   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19614                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19615   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19616 }
19617
19618 /* Generate the DIE for the compilation unit.  */
19619
19620 static dw_die_ref
19621 gen_compile_unit_die (const char *filename)
19622 {
19623   dw_die_ref die;
19624   char producer[250];
19625   const char *language_string = lang_hooks.name;
19626   int language;
19627
19628   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19629
19630   if (filename)
19631     {
19632       add_name_attribute (die, filename);
19633       /* Don't add cwd for <built-in>.  */
19634       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19635         add_comp_dir_attribute (die);
19636     }
19637
19638   sprintf (producer, "%s %s", language_string, version_string);
19639
19640 #ifdef MIPS_DEBUGGING_INFO
19641   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19642      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19643      not appear in the producer string, the debugger reaches the conclusion
19644      that the object file is stripped and has no debugging information.
19645      To get the MIPS/SGI debugger to believe that there is debugging
19646      information in the object file, we add a -g to the producer string.  */
19647   if (debug_info_level > DINFO_LEVEL_TERSE)
19648     strcat (producer, " -g");
19649 #endif
19650
19651   add_AT_string (die, DW_AT_producer, producer);
19652
19653   /* If our producer is LTO try to figure out a common language to use
19654      from the global list of translation units.  */
19655   if (strcmp (language_string, "GNU GIMPLE") == 0)
19656     {
19657       unsigned i;
19658       tree t;
19659       const char *common_lang = NULL;
19660
19661       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19662         {
19663           if (!TRANSLATION_UNIT_LANGUAGE (t))
19664             continue;
19665           if (!common_lang)
19666             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19667           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19668             ;
19669           else if (strncmp (common_lang, "GNU C", 5) == 0
19670                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19671             /* Mixing C and C++ is ok, use C++ in that case.  */
19672             common_lang = "GNU C++";
19673           else
19674             {
19675               /* Fall back to C.  */
19676               common_lang = NULL;
19677               break;
19678             }
19679         }
19680
19681       if (common_lang)
19682         language_string = common_lang;
19683     }
19684
19685   language = DW_LANG_C89;
19686   if (strcmp (language_string, "GNU C++") == 0)
19687     language = DW_LANG_C_plus_plus;
19688   else if (strcmp (language_string, "GNU F77") == 0)
19689     language = DW_LANG_Fortran77;
19690   else if (strcmp (language_string, "GNU Pascal") == 0)
19691     language = DW_LANG_Pascal83;
19692   else if (dwarf_version >= 3 || !dwarf_strict)
19693     {
19694       if (strcmp (language_string, "GNU Ada") == 0)
19695         language = DW_LANG_Ada95;
19696       else if (strcmp (language_string, "GNU Fortran") == 0)
19697         language = DW_LANG_Fortran95;
19698       else if (strcmp (language_string, "GNU Java") == 0)
19699         language = DW_LANG_Java;
19700       else if (strcmp (language_string, "GNU Objective-C") == 0)
19701         language = DW_LANG_ObjC;
19702       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19703         language = DW_LANG_ObjC_plus_plus;
19704     }
19705
19706   add_AT_unsigned (die, DW_AT_language, language);
19707
19708   switch (language)
19709     {
19710     case DW_LANG_Fortran77:
19711     case DW_LANG_Fortran90:
19712     case DW_LANG_Fortran95:
19713       /* Fortran has case insensitive identifiers and the front-end
19714          lowercases everything.  */
19715       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19716       break;
19717     default:
19718       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19719       break;
19720     }
19721   return die;
19722 }
19723
19724 /* Generate the DIE for a base class.  */
19725
19726 static void
19727 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19728 {
19729   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19730
19731   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19732   add_data_member_location_attribute (die, binfo);
19733
19734   if (BINFO_VIRTUAL_P (binfo))
19735     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19736
19737   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19738      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19739      the default has always been DW_ACCESS_private.  */
19740   if (access == access_public_node)
19741     {
19742       if (dwarf_version == 2
19743           || context_die->die_tag == DW_TAG_class_type)
19744       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19745     }
19746   else if (access == access_protected_node)
19747     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19748   else if (dwarf_version > 2
19749            && context_die->die_tag != DW_TAG_class_type)
19750     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19751 }
19752
19753 /* Generate a DIE for a class member.  */
19754
19755 static void
19756 gen_member_die (tree type, dw_die_ref context_die)
19757 {
19758   tree member;
19759   tree binfo = TYPE_BINFO (type);
19760   dw_die_ref child;
19761
19762   /* If this is not an incomplete type, output descriptions of each of its
19763      members. Note that as we output the DIEs necessary to represent the
19764      members of this record or union type, we will also be trying to output
19765      DIEs to represent the *types* of those members. However the `type'
19766      function (above) will specifically avoid generating type DIEs for member
19767      types *within* the list of member DIEs for this (containing) type except
19768      for those types (of members) which are explicitly marked as also being
19769      members of this (containing) type themselves.  The g++ front- end can
19770      force any given type to be treated as a member of some other (containing)
19771      type by setting the TYPE_CONTEXT of the given (member) type to point to
19772      the TREE node representing the appropriate (containing) type.  */
19773
19774   /* First output info about the base classes.  */
19775   if (binfo)
19776     {
19777       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19778       int i;
19779       tree base;
19780
19781       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19782         gen_inheritance_die (base,
19783                              (accesses ? VEC_index (tree, accesses, i)
19784                               : access_public_node), context_die);
19785     }
19786
19787   /* Now output info about the data members and type members.  */
19788   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19789     {
19790       /* If we thought we were generating minimal debug info for TYPE
19791          and then changed our minds, some of the member declarations
19792          may have already been defined.  Don't define them again, but
19793          do put them in the right order.  */
19794
19795       child = lookup_decl_die (member);
19796       if (child)
19797         splice_child_die (context_die, child);
19798       else
19799         gen_decl_die (member, NULL, context_die);
19800     }
19801
19802   /* Now output info about the function members (if any).  */
19803   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19804     {
19805       /* Don't include clones in the member list.  */
19806       if (DECL_ABSTRACT_ORIGIN (member))
19807         continue;
19808
19809       child = lookup_decl_die (member);
19810       if (child)
19811         splice_child_die (context_die, child);
19812       else
19813         gen_decl_die (member, NULL, context_die);
19814     }
19815 }
19816
19817 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19818    is set, we pretend that the type was never defined, so we only get the
19819    member DIEs needed by later specification DIEs.  */
19820
19821 static void
19822 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19823                                 enum debug_info_usage usage)
19824 {
19825   dw_die_ref type_die = lookup_type_die (type);
19826   dw_die_ref scope_die = 0;
19827   int nested = 0;
19828   int complete = (TYPE_SIZE (type)
19829                   && (! TYPE_STUB_DECL (type)
19830                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19831   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19832   complete = complete && should_emit_struct_debug (type, usage);
19833
19834   if (type_die && ! complete)
19835     return;
19836
19837   if (TYPE_CONTEXT (type) != NULL_TREE
19838       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19839           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19840     nested = 1;
19841
19842   scope_die = scope_die_for (type, context_die);
19843
19844   if (! type_die || (nested && is_cu_die (scope_die)))
19845     /* First occurrence of type or toplevel definition of nested class.  */
19846     {
19847       dw_die_ref old_die = type_die;
19848
19849       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19850                           ? record_type_tag (type) : DW_TAG_union_type,
19851                           scope_die, type);
19852       equate_type_number_to_die (type, type_die);
19853       if (old_die)
19854         add_AT_specification (type_die, old_die);
19855       else
19856         add_name_attribute (type_die, type_tag (type));
19857     }
19858   else
19859     remove_AT (type_die, DW_AT_declaration);
19860
19861   /* Generate child dies for template paramaters.  */
19862   if (debug_info_level > DINFO_LEVEL_TERSE
19863       && COMPLETE_TYPE_P (type))
19864     gen_generic_params_dies (type);
19865
19866   /* If this type has been completed, then give it a byte_size attribute and
19867      then give a list of members.  */
19868   if (complete && !ns_decl)
19869     {
19870       /* Prevent infinite recursion in cases where the type of some member of
19871          this type is expressed in terms of this type itself.  */
19872       TREE_ASM_WRITTEN (type) = 1;
19873       add_byte_size_attribute (type_die, type);
19874       if (TYPE_STUB_DECL (type) != NULL_TREE)
19875         {
19876           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19877           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19878         }
19879
19880       /* If the first reference to this type was as the return type of an
19881          inline function, then it may not have a parent.  Fix this now.  */
19882       if (type_die->die_parent == NULL)
19883         add_child_die (scope_die, type_die);
19884
19885       push_decl_scope (type);
19886       gen_member_die (type, type_die);
19887       pop_decl_scope ();
19888
19889       /* GNU extension: Record what type our vtable lives in.  */
19890       if (TYPE_VFIELD (type))
19891         {
19892           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19893
19894           gen_type_die (vtype, context_die);
19895           add_AT_die_ref (type_die, DW_AT_containing_type,
19896                           lookup_type_die (vtype));
19897         }
19898     }
19899   else
19900     {
19901       add_AT_flag (type_die, DW_AT_declaration, 1);
19902
19903       /* We don't need to do this for function-local types.  */
19904       if (TYPE_STUB_DECL (type)
19905           && ! decl_function_context (TYPE_STUB_DECL (type)))
19906         VEC_safe_push (tree, gc, incomplete_types, type);
19907     }
19908
19909   if (get_AT (type_die, DW_AT_name))
19910     add_pubtype (type, type_die);
19911 }
19912
19913 /* Generate a DIE for a subroutine _type_.  */
19914
19915 static void
19916 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19917 {
19918   tree return_type = TREE_TYPE (type);
19919   dw_die_ref subr_die
19920     = new_die (DW_TAG_subroutine_type,
19921                scope_die_for (type, context_die), type);
19922
19923   equate_type_number_to_die (type, subr_die);
19924   add_prototyped_attribute (subr_die, type);
19925   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19926   gen_formal_types_die (type, subr_die);
19927
19928   if (get_AT (subr_die, DW_AT_name))
19929     add_pubtype (type, subr_die);
19930 }
19931
19932 /* Generate a DIE for a type definition.  */
19933
19934 static void
19935 gen_typedef_die (tree decl, dw_die_ref context_die)
19936 {
19937   dw_die_ref type_die;
19938   tree origin;
19939
19940   if (TREE_ASM_WRITTEN (decl))
19941     return;
19942
19943   TREE_ASM_WRITTEN (decl) = 1;
19944   type_die = new_die (DW_TAG_typedef, context_die, decl);
19945   origin = decl_ultimate_origin (decl);
19946   if (origin != NULL)
19947     add_abstract_origin_attribute (type_die, origin);
19948   else
19949     {
19950       tree type;
19951
19952       add_name_and_src_coords_attributes (type_die, decl);
19953       if (DECL_ORIGINAL_TYPE (decl))
19954         {
19955           type = DECL_ORIGINAL_TYPE (decl);
19956
19957           gcc_assert (type != TREE_TYPE (decl));
19958           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19959         }
19960       else
19961         {
19962           type = TREE_TYPE (decl);
19963
19964           if (is_naming_typedef_decl (TYPE_NAME (type)))
19965             {
19966               /* Here, we are in the case of decl being a typedef naming
19967                  an anonymous type, e.g:
19968                      typedef struct {...} foo;
19969                  In that case TREE_TYPE (decl) is not a typedef variant
19970                  type and TYPE_NAME of the anonymous type is set to the
19971                  TYPE_DECL of the typedef. This construct is emitted by
19972                  the C++ FE.
19973
19974                  TYPE is the anonymous struct named by the typedef
19975                  DECL. As we need the DW_AT_type attribute of the
19976                  DW_TAG_typedef to point to the DIE of TYPE, let's
19977                  generate that DIE right away. add_type_attribute
19978                  called below will then pick (via lookup_type_die) that
19979                  anonymous struct DIE.  */
19980               if (!TREE_ASM_WRITTEN (type))
19981                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19982             }
19983         }
19984
19985       add_type_attribute (type_die, type, TREE_READONLY (decl),
19986                           TREE_THIS_VOLATILE (decl), context_die);
19987
19988       if (is_naming_typedef_decl (decl))
19989         /* We want that all subsequent calls to lookup_type_die with
19990            TYPE in argument yield the DW_TAG_typedef we have just
19991            created.  */
19992         equate_type_number_to_die (type, type_die);
19993
19994       add_accessibility_attribute (type_die, decl);
19995     }
19996
19997   if (DECL_ABSTRACT (decl))
19998     equate_decl_number_to_die (decl, type_die);
19999
20000   if (get_AT (type_die, DW_AT_name))
20001     add_pubtype (decl, type_die);
20002 }
20003
20004 /* Generate a DIE for a struct, class, enum or union type.  */
20005
20006 static void
20007 gen_tagged_type_die (tree type,
20008                      dw_die_ref context_die,
20009                      enum debug_info_usage usage)
20010 {
20011   int need_pop;
20012
20013   if (type == NULL_TREE
20014       || !is_tagged_type (type))
20015     return;
20016
20017   /* If this is a nested type whose containing class hasn't been written
20018      out yet, writing it out will cover this one, too.  This does not apply
20019      to instantiations of member class templates; they need to be added to
20020      the containing class as they are generated.  FIXME: This hurts the
20021      idea of combining type decls from multiple TUs, since we can't predict
20022      what set of template instantiations we'll get.  */
20023   if (TYPE_CONTEXT (type)
20024       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20025       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20026     {
20027       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20028
20029       if (TREE_ASM_WRITTEN (type))
20030         return;
20031
20032       /* If that failed, attach ourselves to the stub.  */
20033       push_decl_scope (TYPE_CONTEXT (type));
20034       context_die = lookup_type_die (TYPE_CONTEXT (type));
20035       need_pop = 1;
20036     }
20037   else if (TYPE_CONTEXT (type) != NULL_TREE
20038            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20039     {
20040       /* If this type is local to a function that hasn't been written
20041          out yet, use a NULL context for now; it will be fixed up in
20042          decls_for_scope.  */
20043       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20044       need_pop = 0;
20045     }
20046   else
20047     {
20048       context_die = declare_in_namespace (type, context_die);
20049       need_pop = 0;
20050     }
20051
20052   if (TREE_CODE (type) == ENUMERAL_TYPE)
20053     {
20054       /* This might have been written out by the call to
20055          declare_in_namespace.  */
20056       if (!TREE_ASM_WRITTEN (type))
20057         gen_enumeration_type_die (type, context_die);
20058     }
20059   else
20060     gen_struct_or_union_type_die (type, context_die, usage);
20061
20062   if (need_pop)
20063     pop_decl_scope ();
20064
20065   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20066      it up if it is ever completed.  gen_*_type_die will set it for us
20067      when appropriate.  */
20068 }
20069
20070 /* Generate a type description DIE.  */
20071
20072 static void
20073 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20074                                 enum debug_info_usage usage)
20075 {
20076   struct array_descr_info info;
20077
20078   if (type == NULL_TREE || type == error_mark_node)
20079     return;
20080
20081   /* If TYPE is a typedef type variant, let's generate debug info
20082      for the parent typedef which TYPE is a type of.  */
20083   if (typedef_variant_p (type))
20084     {
20085       if (TREE_ASM_WRITTEN (type))
20086         return;
20087
20088       /* Prevent broken recursion; we can't hand off to the same type.  */
20089       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20090
20091       /* Use the DIE of the containing namespace as the parent DIE of
20092          the type description DIE we want to generate.  */
20093       if (DECL_CONTEXT (TYPE_NAME (type))
20094           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20095         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20096
20097       TREE_ASM_WRITTEN (type) = 1;
20098
20099       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20100       return;
20101     }
20102
20103   /* If type is an anonymous tagged type named by a typedef, let's
20104      generate debug info for the typedef.  */
20105   if (is_naming_typedef_decl (TYPE_NAME (type)))
20106     {
20107       /* Use the DIE of the containing namespace as the parent DIE of
20108          the type description DIE we want to generate.  */
20109       if (DECL_CONTEXT (TYPE_NAME (type))
20110           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20111         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20112       
20113       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20114       return;
20115     }
20116
20117   /* If this is an array type with hidden descriptor, handle it first.  */
20118   if (!TREE_ASM_WRITTEN (type)
20119       && lang_hooks.types.get_array_descr_info
20120       && lang_hooks.types.get_array_descr_info (type, &info)
20121       && (dwarf_version >= 3 || !dwarf_strict))
20122     {
20123       gen_descr_array_type_die (type, &info, context_die);
20124       TREE_ASM_WRITTEN (type) = 1;
20125       return;
20126     }
20127
20128   /* We are going to output a DIE to represent the unqualified version
20129      of this type (i.e. without any const or volatile qualifiers) so
20130      get the main variant (i.e. the unqualified version) of this type
20131      now.  (Vectors are special because the debugging info is in the
20132      cloned type itself).  */
20133   if (TREE_CODE (type) != VECTOR_TYPE)
20134     type = type_main_variant (type);
20135
20136   if (TREE_ASM_WRITTEN (type))
20137     return;
20138
20139   switch (TREE_CODE (type))
20140     {
20141     case ERROR_MARK:
20142       break;
20143
20144     case POINTER_TYPE:
20145     case REFERENCE_TYPE:
20146       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20147          ensures that the gen_type_die recursion will terminate even if the
20148          type is recursive.  Recursive types are possible in Ada.  */
20149       /* ??? We could perhaps do this for all types before the switch
20150          statement.  */
20151       TREE_ASM_WRITTEN (type) = 1;
20152
20153       /* For these types, all that is required is that we output a DIE (or a
20154          set of DIEs) to represent the "basis" type.  */
20155       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20156                                 DINFO_USAGE_IND_USE);
20157       break;
20158
20159     case OFFSET_TYPE:
20160       /* This code is used for C++ pointer-to-data-member types.
20161          Output a description of the relevant class type.  */
20162       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20163                                         DINFO_USAGE_IND_USE);
20164
20165       /* Output a description of the type of the object pointed to.  */
20166       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20167                                         DINFO_USAGE_IND_USE);
20168
20169       /* Now output a DIE to represent this pointer-to-data-member type
20170          itself.  */
20171       gen_ptr_to_mbr_type_die (type, context_die);
20172       break;
20173
20174     case FUNCTION_TYPE:
20175       /* Force out return type (in case it wasn't forced out already).  */
20176       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20177                                         DINFO_USAGE_DIR_USE);
20178       gen_subroutine_type_die (type, context_die);
20179       break;
20180
20181     case METHOD_TYPE:
20182       /* Force out return type (in case it wasn't forced out already).  */
20183       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20184                                         DINFO_USAGE_DIR_USE);
20185       gen_subroutine_type_die (type, context_die);
20186       break;
20187
20188     case ARRAY_TYPE:
20189       gen_array_type_die (type, context_die);
20190       break;
20191
20192     case VECTOR_TYPE:
20193       gen_array_type_die (type, context_die);
20194       break;
20195
20196     case ENUMERAL_TYPE:
20197     case RECORD_TYPE:
20198     case UNION_TYPE:
20199     case QUAL_UNION_TYPE:
20200       gen_tagged_type_die (type, context_die, usage);
20201       return;
20202
20203     case VOID_TYPE:
20204     case INTEGER_TYPE:
20205     case REAL_TYPE:
20206     case FIXED_POINT_TYPE:
20207     case COMPLEX_TYPE:
20208     case BOOLEAN_TYPE:
20209       /* No DIEs needed for fundamental types.  */
20210       break;
20211
20212     case LANG_TYPE:
20213       /* Just use DW_TAG_unspecified_type.  */
20214       {
20215         dw_die_ref type_die = lookup_type_die (type);
20216         if (type_die == NULL)
20217           {
20218             tree name = TYPE_NAME (type);
20219             if (TREE_CODE (name) == TYPE_DECL)
20220               name = DECL_NAME (name);
20221             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20222             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20223             equate_type_number_to_die (type, type_die);
20224           }
20225       }
20226       break;
20227
20228     default:
20229       gcc_unreachable ();
20230     }
20231
20232   TREE_ASM_WRITTEN (type) = 1;
20233 }
20234
20235 static void
20236 gen_type_die (tree type, dw_die_ref context_die)
20237 {
20238   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20239 }
20240
20241 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20242    things which are local to the given block.  */
20243
20244 static void
20245 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20246 {
20247   int must_output_die = 0;
20248   bool inlined_func;
20249
20250   /* Ignore blocks that are NULL.  */
20251   if (stmt == NULL_TREE)
20252     return;
20253
20254   inlined_func = inlined_function_outer_scope_p (stmt);
20255
20256   /* If the block is one fragment of a non-contiguous block, do not
20257      process the variables, since they will have been done by the
20258      origin block.  Do process subblocks.  */
20259   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20260     {
20261       tree sub;
20262
20263       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20264         gen_block_die (sub, context_die, depth + 1);
20265
20266       return;
20267     }
20268
20269   /* Determine if we need to output any Dwarf DIEs at all to represent this
20270      block.  */
20271   if (inlined_func)
20272     /* The outer scopes for inlinings *must* always be represented.  We
20273        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20274     must_output_die = 1;
20275   else
20276     {
20277       /* Determine if this block directly contains any "significant"
20278          local declarations which we will need to output DIEs for.  */
20279       if (debug_info_level > DINFO_LEVEL_TERSE)
20280         /* We are not in terse mode so *any* local declaration counts
20281            as being a "significant" one.  */
20282         must_output_die = ((BLOCK_VARS (stmt) != NULL
20283                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20284                            && (TREE_USED (stmt)
20285                                || TREE_ASM_WRITTEN (stmt)
20286                                || BLOCK_ABSTRACT (stmt)));
20287       else if ((TREE_USED (stmt)
20288                 || TREE_ASM_WRITTEN (stmt)
20289                 || BLOCK_ABSTRACT (stmt))
20290                && !dwarf2out_ignore_block (stmt))
20291         must_output_die = 1;
20292     }
20293
20294   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20295      DIE for any block which contains no significant local declarations at
20296      all.  Rather, in such cases we just call `decls_for_scope' so that any
20297      needed Dwarf info for any sub-blocks will get properly generated. Note
20298      that in terse mode, our definition of what constitutes a "significant"
20299      local declaration gets restricted to include only inlined function
20300      instances and local (nested) function definitions.  */
20301   if (must_output_die)
20302     {
20303       if (inlined_func)
20304         {
20305           /* If STMT block is abstract, that means we have been called
20306              indirectly from dwarf2out_abstract_function.
20307              That function rightfully marks the descendent blocks (of
20308              the abstract function it is dealing with) as being abstract,
20309              precisely to prevent us from emitting any
20310              DW_TAG_inlined_subroutine DIE as a descendent
20311              of an abstract function instance. So in that case, we should
20312              not call gen_inlined_subroutine_die.
20313
20314              Later though, when cgraph asks dwarf2out to emit info
20315              for the concrete instance of the function decl into which
20316              the concrete instance of STMT got inlined, the later will lead
20317              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20318           if (! BLOCK_ABSTRACT (stmt))
20319             gen_inlined_subroutine_die (stmt, context_die, depth);
20320         }
20321       else
20322         gen_lexical_block_die (stmt, context_die, depth);
20323     }
20324   else
20325     decls_for_scope (stmt, context_die, depth);
20326 }
20327
20328 /* Process variable DECL (or variable with origin ORIGIN) within
20329    block STMT and add it to CONTEXT_DIE.  */
20330 static void
20331 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20332 {
20333   dw_die_ref die;
20334   tree decl_or_origin = decl ? decl : origin;
20335
20336   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20337     die = lookup_decl_die (decl_or_origin);
20338   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20339            && TYPE_DECL_IS_STUB (decl_or_origin))
20340     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20341   else
20342     die = NULL;
20343
20344   if (die != NULL && die->die_parent == NULL)
20345     add_child_die (context_die, die);
20346   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20347     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20348                                          stmt, context_die);
20349   else
20350     gen_decl_die (decl, origin, context_die);
20351 }
20352
20353 /* Generate all of the decls declared within a given scope and (recursively)
20354    all of its sub-blocks.  */
20355
20356 static void
20357 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20358 {
20359   tree decl;
20360   unsigned int i;
20361   tree subblocks;
20362
20363   /* Ignore NULL blocks.  */
20364   if (stmt == NULL_TREE)
20365     return;
20366
20367   /* Output the DIEs to represent all of the data objects and typedefs
20368      declared directly within this block but not within any nested
20369      sub-blocks.  Also, nested function and tag DIEs have been
20370      generated with a parent of NULL; fix that up now.  */
20371   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20372     process_scope_var (stmt, decl, NULL_TREE, context_die);
20373   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20374     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20375                        context_die);
20376
20377   /* If we're at -g1, we're not interested in subblocks.  */
20378   if (debug_info_level <= DINFO_LEVEL_TERSE)
20379     return;
20380
20381   /* Output the DIEs to represent all sub-blocks (and the items declared
20382      therein) of this block.  */
20383   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20384        subblocks != NULL;
20385        subblocks = BLOCK_CHAIN (subblocks))
20386     gen_block_die (subblocks, context_die, depth + 1);
20387 }
20388
20389 /* Is this a typedef we can avoid emitting?  */
20390
20391 static inline int
20392 is_redundant_typedef (const_tree decl)
20393 {
20394   if (TYPE_DECL_IS_STUB (decl))
20395     return 1;
20396
20397   if (DECL_ARTIFICIAL (decl)
20398       && DECL_CONTEXT (decl)
20399       && is_tagged_type (DECL_CONTEXT (decl))
20400       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20401       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20402     /* Also ignore the artificial member typedef for the class name.  */
20403     return 1;
20404
20405   return 0;
20406 }
20407
20408 /* Return TRUE if TYPE is a typedef that names a type for linkage
20409    purposes. This kind of typedefs is produced by the C++ FE for
20410    constructs like:
20411
20412    typedef struct {...} foo;
20413
20414    In that case, there is no typedef variant type produced for foo.
20415    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20416    struct type.  */
20417
20418 static bool
20419 is_naming_typedef_decl (const_tree decl)
20420 {
20421   if (decl == NULL_TREE
20422       || TREE_CODE (decl) != TYPE_DECL
20423       || !is_tagged_type (TREE_TYPE (decl))
20424       || DECL_IS_BUILTIN (decl)
20425       || is_redundant_typedef (decl)
20426       /* It looks like Ada produces TYPE_DECLs that are very similar
20427          to C++ naming typedefs but that have different
20428          semantics. Let's be specific to c++ for now.  */
20429       || !is_cxx ())
20430     return FALSE;
20431
20432   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20433           && TYPE_NAME (TREE_TYPE (decl)) == decl
20434           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20435               != TYPE_NAME (TREE_TYPE (decl))));
20436 }
20437
20438 /* Returns the DIE for a context.  */
20439
20440 static inline dw_die_ref
20441 get_context_die (tree context)
20442 {
20443   if (context)
20444     {
20445       /* Find die that represents this context.  */
20446       if (TYPE_P (context))
20447         return force_type_die (TYPE_MAIN_VARIANT (context));
20448       else
20449         return force_decl_die (context);
20450     }
20451   return comp_unit_die ();
20452 }
20453
20454 /* Returns the DIE for decl.  A DIE will always be returned.  */
20455
20456 static dw_die_ref
20457 force_decl_die (tree decl)
20458 {
20459   dw_die_ref decl_die;
20460   unsigned saved_external_flag;
20461   tree save_fn = NULL_TREE;
20462   decl_die = lookup_decl_die (decl);
20463   if (!decl_die)
20464     {
20465       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20466
20467       decl_die = lookup_decl_die (decl);
20468       if (decl_die)
20469         return decl_die;
20470
20471       switch (TREE_CODE (decl))
20472         {
20473         case FUNCTION_DECL:
20474           /* Clear current_function_decl, so that gen_subprogram_die thinks
20475              that this is a declaration. At this point, we just want to force
20476              declaration die.  */
20477           save_fn = current_function_decl;
20478           current_function_decl = NULL_TREE;
20479           gen_subprogram_die (decl, context_die);
20480           current_function_decl = save_fn;
20481           break;
20482
20483         case VAR_DECL:
20484           /* Set external flag to force declaration die. Restore it after
20485            gen_decl_die() call.  */
20486           saved_external_flag = DECL_EXTERNAL (decl);
20487           DECL_EXTERNAL (decl) = 1;
20488           gen_decl_die (decl, NULL, context_die);
20489           DECL_EXTERNAL (decl) = saved_external_flag;
20490           break;
20491
20492         case NAMESPACE_DECL:
20493           if (dwarf_version >= 3 || !dwarf_strict)
20494             dwarf2out_decl (decl);
20495           else
20496             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20497             decl_die = comp_unit_die ();
20498           break;
20499
20500         case TRANSLATION_UNIT_DECL:
20501           decl_die = comp_unit_die ();
20502           break;
20503
20504         default:
20505           gcc_unreachable ();
20506         }
20507
20508       /* We should be able to find the DIE now.  */
20509       if (!decl_die)
20510         decl_die = lookup_decl_die (decl);
20511       gcc_assert (decl_die);
20512     }
20513
20514   return decl_die;
20515 }
20516
20517 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20518    always returned.  */
20519
20520 static dw_die_ref
20521 force_type_die (tree type)
20522 {
20523   dw_die_ref type_die;
20524
20525   type_die = lookup_type_die (type);
20526   if (!type_die)
20527     {
20528       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20529
20530       type_die = modified_type_die (type, TYPE_READONLY (type),
20531                                     TYPE_VOLATILE (type), context_die);
20532       gcc_assert (type_die);
20533     }
20534   return type_die;
20535 }
20536
20537 /* Force out any required namespaces to be able to output DECL,
20538    and return the new context_die for it, if it's changed.  */
20539
20540 static dw_die_ref
20541 setup_namespace_context (tree thing, dw_die_ref context_die)
20542 {
20543   tree context = (DECL_P (thing)
20544                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20545   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20546     /* Force out the namespace.  */
20547     context_die = force_decl_die (context);
20548
20549   return context_die;
20550 }
20551
20552 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20553    type) within its namespace, if appropriate.
20554
20555    For compatibility with older debuggers, namespace DIEs only contain
20556    declarations; all definitions are emitted at CU scope.  */
20557
20558 static dw_die_ref
20559 declare_in_namespace (tree thing, dw_die_ref context_die)
20560 {
20561   dw_die_ref ns_context;
20562
20563   if (debug_info_level <= DINFO_LEVEL_TERSE)
20564     return context_die;
20565
20566   /* If this decl is from an inlined function, then don't try to emit it in its
20567      namespace, as we will get confused.  It would have already been emitted
20568      when the abstract instance of the inline function was emitted anyways.  */
20569   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20570     return context_die;
20571
20572   ns_context = setup_namespace_context (thing, context_die);
20573
20574   if (ns_context != context_die)
20575     {
20576       if (is_fortran ())
20577         return ns_context;
20578       if (DECL_P (thing))
20579         gen_decl_die (thing, NULL, ns_context);
20580       else
20581         gen_type_die (thing, ns_context);
20582     }
20583   return context_die;
20584 }
20585
20586 /* Generate a DIE for a namespace or namespace alias.  */
20587
20588 static void
20589 gen_namespace_die (tree decl, dw_die_ref context_die)
20590 {
20591   dw_die_ref namespace_die;
20592
20593   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20594      they are an alias of.  */
20595   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20596     {
20597       /* Output a real namespace or module.  */
20598       context_die = setup_namespace_context (decl, comp_unit_die ());
20599       namespace_die = new_die (is_fortran ()
20600                                ? DW_TAG_module : DW_TAG_namespace,
20601                                context_die, decl);
20602       /* For Fortran modules defined in different CU don't add src coords.  */
20603       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20604         {
20605           const char *name = dwarf2_name (decl, 0);
20606           if (name)
20607             add_name_attribute (namespace_die, name);
20608         }
20609       else
20610         add_name_and_src_coords_attributes (namespace_die, decl);
20611       if (DECL_EXTERNAL (decl))
20612         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20613       equate_decl_number_to_die (decl, namespace_die);
20614     }
20615   else
20616     {
20617       /* Output a namespace alias.  */
20618
20619       /* Force out the namespace we are an alias of, if necessary.  */
20620       dw_die_ref origin_die
20621         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20622
20623       if (DECL_FILE_SCOPE_P (decl)
20624           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20625         context_die = setup_namespace_context (decl, comp_unit_die ());
20626       /* Now create the namespace alias DIE.  */
20627       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20628       add_name_and_src_coords_attributes (namespace_die, decl);
20629       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20630       equate_decl_number_to_die (decl, namespace_die);
20631     }
20632 }
20633
20634 /* Generate Dwarf debug information for a decl described by DECL.
20635    The return value is currently only meaningful for PARM_DECLs,
20636    for all other decls it returns NULL.  */
20637
20638 static dw_die_ref
20639 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20640 {
20641   tree decl_or_origin = decl ? decl : origin;
20642   tree class_origin = NULL, ultimate_origin;
20643
20644   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20645     return NULL;
20646
20647   switch (TREE_CODE (decl_or_origin))
20648     {
20649     case ERROR_MARK:
20650       break;
20651
20652     case CONST_DECL:
20653       if (!is_fortran () && !is_ada ())
20654         {
20655           /* The individual enumerators of an enum type get output when we output
20656              the Dwarf representation of the relevant enum type itself.  */
20657           break;
20658         }
20659
20660       /* Emit its type.  */
20661       gen_type_die (TREE_TYPE (decl), context_die);
20662
20663       /* And its containing namespace.  */
20664       context_die = declare_in_namespace (decl, context_die);
20665
20666       gen_const_die (decl, context_die);
20667       break;
20668
20669     case FUNCTION_DECL:
20670       /* Don't output any DIEs to represent mere function declarations,
20671          unless they are class members or explicit block externs.  */
20672       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20673           && DECL_FILE_SCOPE_P (decl_or_origin)
20674           && (current_function_decl == NULL_TREE
20675               || DECL_ARTIFICIAL (decl_or_origin)))
20676         break;
20677
20678 #if 0
20679       /* FIXME */
20680       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20681          on local redeclarations of global functions.  That seems broken.  */
20682       if (current_function_decl != decl)
20683         /* This is only a declaration.  */;
20684 #endif
20685
20686       /* If we're emitting a clone, emit info for the abstract instance.  */
20687       if (origin || DECL_ORIGIN (decl) != decl)
20688         dwarf2out_abstract_function (origin
20689                                      ? DECL_ORIGIN (origin)
20690                                      : DECL_ABSTRACT_ORIGIN (decl));
20691
20692       /* If we're emitting an out-of-line copy of an inline function,
20693          emit info for the abstract instance and set up to refer to it.  */
20694       else if (cgraph_function_possibly_inlined_p (decl)
20695                && ! DECL_ABSTRACT (decl)
20696                && ! class_or_namespace_scope_p (context_die)
20697                /* dwarf2out_abstract_function won't emit a die if this is just
20698                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20699                   that case, because that works only if we have a die.  */
20700                && DECL_INITIAL (decl) != NULL_TREE)
20701         {
20702           dwarf2out_abstract_function (decl);
20703           set_decl_origin_self (decl);
20704         }
20705
20706       /* Otherwise we're emitting the primary DIE for this decl.  */
20707       else if (debug_info_level > DINFO_LEVEL_TERSE)
20708         {
20709           /* Before we describe the FUNCTION_DECL itself, make sure that we
20710              have its containing type.  */
20711           if (!origin)
20712             origin = decl_class_context (decl);
20713           if (origin != NULL_TREE)
20714             gen_type_die (origin, context_die);
20715
20716           /* And its return type.  */
20717           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20718
20719           /* And its virtual context.  */
20720           if (DECL_VINDEX (decl) != NULL_TREE)
20721             gen_type_die (DECL_CONTEXT (decl), context_die);
20722
20723           /* Make sure we have a member DIE for decl.  */
20724           if (origin != NULL_TREE)
20725             gen_type_die_for_member (origin, decl, context_die);
20726
20727           /* And its containing namespace.  */
20728           context_die = declare_in_namespace (decl, context_die);
20729         }
20730
20731       /* Now output a DIE to represent the function itself.  */
20732       if (decl)
20733         gen_subprogram_die (decl, context_die);
20734       break;
20735
20736     case TYPE_DECL:
20737       /* If we are in terse mode, don't generate any DIEs to represent any
20738          actual typedefs.  */
20739       if (debug_info_level <= DINFO_LEVEL_TERSE)
20740         break;
20741
20742       /* In the special case of a TYPE_DECL node representing the declaration
20743          of some type tag, if the given TYPE_DECL is marked as having been
20744          instantiated from some other (original) TYPE_DECL node (e.g. one which
20745          was generated within the original definition of an inline function) we
20746          used to generate a special (abbreviated) DW_TAG_structure_type,
20747          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20748          should be actually referencing those DIEs, as variable DIEs with that
20749          type would be emitted already in the abstract origin, so it was always
20750          removed during unused type prunning.  Don't add anything in this
20751          case.  */
20752       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20753         break;
20754
20755       if (is_redundant_typedef (decl))
20756         gen_type_die (TREE_TYPE (decl), context_die);
20757       else
20758         /* Output a DIE to represent the typedef itself.  */
20759         gen_typedef_die (decl, context_die);
20760       break;
20761
20762     case LABEL_DECL:
20763       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20764         gen_label_die (decl, context_die);
20765       break;
20766
20767     case VAR_DECL:
20768     case RESULT_DECL:
20769       /* If we are in terse mode, don't generate any DIEs to represent any
20770          variable declarations or definitions.  */
20771       if (debug_info_level <= DINFO_LEVEL_TERSE)
20772         break;
20773
20774       /* Output any DIEs that are needed to specify the type of this data
20775          object.  */
20776       if (decl_by_reference_p (decl_or_origin))
20777         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20778       else
20779         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20780
20781       /* And its containing type.  */
20782       class_origin = decl_class_context (decl_or_origin);
20783       if (class_origin != NULL_TREE)
20784         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20785
20786       /* And its containing namespace.  */
20787       context_die = declare_in_namespace (decl_or_origin, context_die);
20788
20789       /* Now output the DIE to represent the data object itself.  This gets
20790          complicated because of the possibility that the VAR_DECL really
20791          represents an inlined instance of a formal parameter for an inline
20792          function.  */
20793       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20794       if (ultimate_origin != NULL_TREE
20795           && TREE_CODE (ultimate_origin) == PARM_DECL)
20796         gen_formal_parameter_die (decl, origin,
20797                                   true /* Emit name attribute.  */,
20798                                   context_die);
20799       else
20800         gen_variable_die (decl, origin, context_die);
20801       break;
20802
20803     case FIELD_DECL:
20804       /* Ignore the nameless fields that are used to skip bits but handle C++
20805          anonymous unions and structs.  */
20806       if (DECL_NAME (decl) != NULL_TREE
20807           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20808           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20809         {
20810           gen_type_die (member_declared_type (decl), context_die);
20811           gen_field_die (decl, context_die);
20812         }
20813       break;
20814
20815     case PARM_DECL:
20816       if (DECL_BY_REFERENCE (decl_or_origin))
20817         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20818       else
20819         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20820       return gen_formal_parameter_die (decl, origin,
20821                                        true /* Emit name attribute.  */,
20822                                        context_die);
20823
20824     case NAMESPACE_DECL:
20825     case IMPORTED_DECL:
20826       if (dwarf_version >= 3 || !dwarf_strict)
20827         gen_namespace_die (decl, context_die);
20828       break;
20829
20830     default:
20831       /* Probably some frontend-internal decl.  Assume we don't care.  */
20832       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20833       break;
20834     }
20835
20836   return NULL;
20837 }
20838 \f
20839 /* Output debug information for global decl DECL.  Called from toplev.c after
20840    compilation proper has finished.  */
20841
20842 static void
20843 dwarf2out_global_decl (tree decl)
20844 {
20845   /* Output DWARF2 information for file-scope tentative data object
20846      declarations, file-scope (extern) function declarations (which
20847      had no corresponding body) and file-scope tagged type declarations
20848      and definitions which have not yet been forced out.  */
20849   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20850     dwarf2out_decl (decl);
20851 }
20852
20853 /* Output debug information for type decl DECL.  Called from toplev.c
20854    and from language front ends (to record built-in types).  */
20855 static void
20856 dwarf2out_type_decl (tree decl, int local)
20857 {
20858   if (!local)
20859     dwarf2out_decl (decl);
20860 }
20861
20862 /* Output debug information for imported module or decl DECL.
20863    NAME is non-NULL name in the lexical block if the decl has been renamed.
20864    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20865    that DECL belongs to.
20866    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20867 static void
20868 dwarf2out_imported_module_or_decl_1 (tree decl,
20869                                      tree name,
20870                                      tree lexical_block,
20871                                      dw_die_ref lexical_block_die)
20872 {
20873   expanded_location xloc;
20874   dw_die_ref imported_die = NULL;
20875   dw_die_ref at_import_die;
20876
20877   if (TREE_CODE (decl) == IMPORTED_DECL)
20878     {
20879       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20880       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20881       gcc_assert (decl);
20882     }
20883   else
20884     xloc = expand_location (input_location);
20885
20886   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20887     {
20888       at_import_die = force_type_die (TREE_TYPE (decl));
20889       /* For namespace N { typedef void T; } using N::T; base_type_die
20890          returns NULL, but DW_TAG_imported_declaration requires
20891          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20892       if (!at_import_die)
20893         {
20894           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20895           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20896           at_import_die = lookup_type_die (TREE_TYPE (decl));
20897           gcc_assert (at_import_die);
20898         }
20899     }
20900   else
20901     {
20902       at_import_die = lookup_decl_die (decl);
20903       if (!at_import_die)
20904         {
20905           /* If we're trying to avoid duplicate debug info, we may not have
20906              emitted the member decl for this field.  Emit it now.  */
20907           if (TREE_CODE (decl) == FIELD_DECL)
20908             {
20909               tree type = DECL_CONTEXT (decl);
20910
20911               if (TYPE_CONTEXT (type)
20912                   && TYPE_P (TYPE_CONTEXT (type))
20913                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20914                                                 DINFO_USAGE_DIR_USE))
20915                 return;
20916               gen_type_die_for_member (type, decl,
20917                                        get_context_die (TYPE_CONTEXT (type)));
20918             }
20919           at_import_die = force_decl_die (decl);
20920         }
20921     }
20922
20923   if (TREE_CODE (decl) == NAMESPACE_DECL)
20924     {
20925       if (dwarf_version >= 3 || !dwarf_strict)
20926         imported_die = new_die (DW_TAG_imported_module,
20927                                 lexical_block_die,
20928                                 lexical_block);
20929       else
20930         return;
20931     }
20932   else
20933     imported_die = new_die (DW_TAG_imported_declaration,
20934                             lexical_block_die,
20935                             lexical_block);
20936
20937   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20938   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20939   if (name)
20940     add_AT_string (imported_die, DW_AT_name,
20941                    IDENTIFIER_POINTER (name));
20942   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20943 }
20944
20945 /* Output debug information for imported module or decl DECL.
20946    NAME is non-NULL name in context if the decl has been renamed.
20947    CHILD is true if decl is one of the renamed decls as part of
20948    importing whole module.  */
20949
20950 static void
20951 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20952                                    bool child)
20953 {
20954   /* dw_die_ref at_import_die;  */
20955   dw_die_ref scope_die;
20956
20957   if (debug_info_level <= DINFO_LEVEL_TERSE)
20958     return;
20959
20960   gcc_assert (decl);
20961
20962   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20963      We need decl DIE for reference and scope die. First, get DIE for the decl
20964      itself.  */
20965
20966   /* Get the scope die for decl context. Use comp_unit_die for global module
20967      or decl. If die is not found for non globals, force new die.  */
20968   if (context
20969       && TYPE_P (context)
20970       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20971     return;
20972
20973   if (!(dwarf_version >= 3 || !dwarf_strict))
20974     return;
20975
20976   scope_die = get_context_die (context);
20977
20978   if (child)
20979     {
20980       gcc_assert (scope_die->die_child);
20981       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20982       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20983       scope_die = scope_die->die_child;
20984     }
20985
20986   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20987   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20988
20989 }
20990
20991 /* Write the debugging output for DECL.  */
20992
20993 void
20994 dwarf2out_decl (tree decl)
20995 {
20996   dw_die_ref context_die = comp_unit_die ();
20997
20998   switch (TREE_CODE (decl))
20999     {
21000     case ERROR_MARK:
21001       return;
21002
21003     case FUNCTION_DECL:
21004       /* What we would really like to do here is to filter out all mere
21005          file-scope declarations of file-scope functions which are never
21006          referenced later within this translation unit (and keep all of ones
21007          that *are* referenced later on) but we aren't clairvoyant, so we have
21008          no idea which functions will be referenced in the future (i.e. later
21009          on within the current translation unit). So here we just ignore all
21010          file-scope function declarations which are not also definitions.  If
21011          and when the debugger needs to know something about these functions,
21012          it will have to hunt around and find the DWARF information associated
21013          with the definition of the function.
21014
21015          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21016          nodes represent definitions and which ones represent mere
21017          declarations.  We have to check DECL_INITIAL instead. That's because
21018          the C front-end supports some weird semantics for "extern inline"
21019          function definitions.  These can get inlined within the current
21020          translation unit (and thus, we need to generate Dwarf info for their
21021          abstract instances so that the Dwarf info for the concrete inlined
21022          instances can have something to refer to) but the compiler never
21023          generates any out-of-lines instances of such things (despite the fact
21024          that they *are* definitions).
21025
21026          The important point is that the C front-end marks these "extern
21027          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21028          them anyway. Note that the C++ front-end also plays some similar games
21029          for inline function definitions appearing within include files which
21030          also contain `#pragma interface' pragmas.  */
21031       if (DECL_INITIAL (decl) == NULL_TREE)
21032         return;
21033
21034       /* If we're a nested function, initially use a parent of NULL; if we're
21035          a plain function, this will be fixed up in decls_for_scope.  If
21036          we're a method, it will be ignored, since we already have a DIE.  */
21037       if (decl_function_context (decl)
21038           /* But if we're in terse mode, we don't care about scope.  */
21039           && debug_info_level > DINFO_LEVEL_TERSE)
21040         context_die = NULL;
21041       break;
21042
21043     case VAR_DECL:
21044       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21045          declaration and if the declaration was never even referenced from
21046          within this entire compilation unit.  We suppress these DIEs in
21047          order to save space in the .debug section (by eliminating entries
21048          which are probably useless).  Note that we must not suppress
21049          block-local extern declarations (whether used or not) because that
21050          would screw-up the debugger's name lookup mechanism and cause it to
21051          miss things which really ought to be in scope at a given point.  */
21052       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21053         return;
21054
21055       /* For local statics lookup proper context die.  */
21056       if (TREE_STATIC (decl) && decl_function_context (decl))
21057         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21058
21059       /* If we are in terse mode, don't generate any DIEs to represent any
21060          variable declarations or definitions.  */
21061       if (debug_info_level <= DINFO_LEVEL_TERSE)
21062         return;
21063       break;
21064
21065     case CONST_DECL:
21066       if (debug_info_level <= DINFO_LEVEL_TERSE)
21067         return;
21068       if (!is_fortran () && !is_ada ())
21069         return;
21070       if (TREE_STATIC (decl) && decl_function_context (decl))
21071         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21072       break;
21073
21074     case NAMESPACE_DECL:
21075     case IMPORTED_DECL:
21076       if (debug_info_level <= DINFO_LEVEL_TERSE)
21077         return;
21078       if (lookup_decl_die (decl) != NULL)
21079         return;
21080       break;
21081
21082     case TYPE_DECL:
21083       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21084       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21085         return;
21086
21087       /* Don't bother trying to generate any DIEs to represent any of the
21088          normal built-in types for the language we are compiling.  */
21089       if (DECL_IS_BUILTIN (decl))
21090         return;
21091
21092       /* If we are in terse mode, don't generate any DIEs for types.  */
21093       if (debug_info_level <= DINFO_LEVEL_TERSE)
21094         return;
21095
21096       /* If we're a function-scope tag, initially use a parent of NULL;
21097          this will be fixed up in decls_for_scope.  */
21098       if (decl_function_context (decl))
21099         context_die = NULL;
21100
21101       break;
21102
21103     default:
21104       return;
21105     }
21106
21107   gen_decl_die (decl, NULL, context_die);
21108 }
21109
21110 /* Write the debugging output for DECL.  */
21111
21112 static void
21113 dwarf2out_function_decl (tree decl)
21114 {
21115   dwarf2out_decl (decl);
21116
21117   htab_empty (decl_loc_table);
21118 }
21119
21120 /* Output a marker (i.e. a label) for the beginning of the generated code for
21121    a lexical block.  */
21122
21123 static void
21124 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21125                        unsigned int blocknum)
21126 {
21127   switch_to_section (current_function_section ());
21128   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21129 }
21130
21131 /* Output a marker (i.e. a label) for the end of the generated code for a
21132    lexical block.  */
21133
21134 static void
21135 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21136 {
21137   switch_to_section (current_function_section ());
21138   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21139 }
21140
21141 /* Returns nonzero if it is appropriate not to emit any debugging
21142    information for BLOCK, because it doesn't contain any instructions.
21143
21144    Don't allow this for blocks with nested functions or local classes
21145    as we would end up with orphans, and in the presence of scheduling
21146    we may end up calling them anyway.  */
21147
21148 static bool
21149 dwarf2out_ignore_block (const_tree block)
21150 {
21151   tree decl;
21152   unsigned int i;
21153
21154   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21155     if (TREE_CODE (decl) == FUNCTION_DECL
21156         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21157       return 0;
21158   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21159     {
21160       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21161       if (TREE_CODE (decl) == FUNCTION_DECL
21162           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21163       return 0;
21164     }
21165
21166   return 1;
21167 }
21168
21169 /* Hash table routines for file_hash.  */
21170
21171 static int
21172 file_table_eq (const void *p1_p, const void *p2_p)
21173 {
21174   const struct dwarf_file_data *const p1 =
21175     (const struct dwarf_file_data *) p1_p;
21176   const char *const p2 = (const char *) p2_p;
21177   return strcmp (p1->filename, p2) == 0;
21178 }
21179
21180 static hashval_t
21181 file_table_hash (const void *p_p)
21182 {
21183   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21184   return htab_hash_string (p->filename);
21185 }
21186
21187 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21188    dwarf2out.c) and return its "index".  The index of each (known) filename is
21189    just a unique number which is associated with only that one filename.  We
21190    need such numbers for the sake of generating labels (in the .debug_sfnames
21191    section) and references to those files numbers (in the .debug_srcinfo
21192    and.debug_macinfo sections).  If the filename given as an argument is not
21193    found in our current list, add it to the list and assign it the next
21194    available unique index number.  In order to speed up searches, we remember
21195    the index of the filename was looked up last.  This handles the majority of
21196    all searches.  */
21197
21198 static struct dwarf_file_data *
21199 lookup_filename (const char *file_name)
21200 {
21201   void ** slot;
21202   struct dwarf_file_data * created;
21203
21204   /* Check to see if the file name that was searched on the previous
21205      call matches this file name.  If so, return the index.  */
21206   if (file_table_last_lookup
21207       && (file_name == file_table_last_lookup->filename
21208           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21209     return file_table_last_lookup;
21210
21211   /* Didn't match the previous lookup, search the table.  */
21212   slot = htab_find_slot_with_hash (file_table, file_name,
21213                                    htab_hash_string (file_name), INSERT);
21214   if (*slot)
21215     return (struct dwarf_file_data *) *slot;
21216
21217   created = ggc_alloc_dwarf_file_data ();
21218   created->filename = file_name;
21219   created->emitted_number = 0;
21220   *slot = created;
21221   return created;
21222 }
21223
21224 /* If the assembler will construct the file table, then translate the compiler
21225    internal file table number into the assembler file table number, and emit
21226    a .file directive if we haven't already emitted one yet.  The file table
21227    numbers are different because we prune debug info for unused variables and
21228    types, which may include filenames.  */
21229
21230 static int
21231 maybe_emit_file (struct dwarf_file_data * fd)
21232 {
21233   if (! fd->emitted_number)
21234     {
21235       if (last_emitted_file)
21236         fd->emitted_number = last_emitted_file->emitted_number + 1;
21237       else
21238         fd->emitted_number = 1;
21239       last_emitted_file = fd;
21240
21241       if (DWARF2_ASM_LINE_DEBUG_INFO)
21242         {
21243           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21244           output_quoted_string (asm_out_file,
21245                                 remap_debug_filename (fd->filename));
21246           fputc ('\n', asm_out_file);
21247         }
21248     }
21249
21250   return fd->emitted_number;
21251 }
21252
21253 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21254    That generation should happen after function debug info has been
21255    generated. The value of the attribute is the constant value of ARG.  */
21256
21257 static void
21258 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21259 {
21260   die_arg_entry entry;
21261
21262   if (!die || !arg)
21263     return;
21264
21265   if (!tmpl_value_parm_die_table)
21266     tmpl_value_parm_die_table
21267       = VEC_alloc (die_arg_entry, gc, 32);
21268
21269   entry.die = die;
21270   entry.arg = arg;
21271   VEC_safe_push (die_arg_entry, gc,
21272                  tmpl_value_parm_die_table,
21273                  &entry);
21274 }
21275
21276 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21277    by append_entry_to_tmpl_value_parm_die_table. This function must
21278    be called after function DIEs have been generated.  */
21279
21280 static void
21281 gen_remaining_tmpl_value_param_die_attribute (void)
21282 {
21283   if (tmpl_value_parm_die_table)
21284     {
21285       unsigned i;
21286       die_arg_entry *e;
21287
21288       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21289         tree_add_const_value_attribute (e->die, e->arg);
21290     }
21291 }
21292
21293
21294 /* Replace DW_AT_name for the decl with name.  */
21295
21296 static void
21297 dwarf2out_set_name (tree decl, tree name)
21298 {
21299   dw_die_ref die;
21300   dw_attr_ref attr;
21301   const char *dname;
21302
21303   die = TYPE_SYMTAB_DIE (decl);
21304   if (!die)
21305     return;
21306
21307   dname = dwarf2_name (name, 0);
21308   if (!dname)
21309     return;
21310
21311   attr = get_AT (die, DW_AT_name);
21312   if (attr)
21313     {
21314       struct indirect_string_node *node;
21315
21316       node = find_AT_string (dname);
21317       /* replace the string.  */
21318       attr->dw_attr_val.v.val_str = node;
21319     }
21320
21321   else
21322     add_name_attribute (die, dname);
21323 }
21324
21325 /* Called by the final INSN scan whenever we see a direct function call.
21326    Make an entry into the direct call table, recording the point of call
21327    and a reference to the target function's debug entry.  */
21328
21329 static void
21330 dwarf2out_direct_call (tree targ)
21331 {
21332   dcall_entry e;
21333   tree origin = decl_ultimate_origin (targ);
21334
21335   /* If this is a clone, use the abstract origin as the target.  */
21336   if (origin)
21337     targ = origin;
21338
21339   e.poc_label_num = poc_label_num++;
21340   e.poc_decl = current_function_decl;
21341   e.targ_die = force_decl_die (targ);
21342   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21343
21344   /* Drop a label at the return point to mark the point of call.  */
21345   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21346 }
21347
21348 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21349
21350 static hashval_t
21351 vcall_insn_table_hash (const void *x)
21352 {
21353   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21354 }
21355
21356 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21357    insnd_uid of *Y.  */
21358
21359 static int
21360 vcall_insn_table_eq (const void *x, const void *y)
21361 {
21362   return (((const struct vcall_insn *) x)->insn_uid
21363           == ((const struct vcall_insn *) y)->insn_uid);
21364 }
21365
21366 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21367
21368 static void
21369 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21370 {
21371   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21372   struct vcall_insn **slot;
21373
21374   gcc_assert (item);
21375   item->insn_uid = insn_uid;
21376   item->vtable_slot = vtable_slot;
21377   slot = (struct vcall_insn **)
21378       htab_find_slot_with_hash (vcall_insn_table, &item,
21379                                 (hashval_t) insn_uid, INSERT);
21380   *slot = item;
21381 }
21382
21383 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21384
21385 static unsigned int
21386 lookup_vcall_insn (unsigned int insn_uid)
21387 {
21388   struct vcall_insn item;
21389   struct vcall_insn *p;
21390
21391   item.insn_uid = insn_uid;
21392   item.vtable_slot = 0;
21393   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21394                                                  (void *) &item,
21395                                                  (hashval_t) insn_uid);
21396   if (p == NULL)
21397     return (unsigned int) -1;
21398   return p->vtable_slot;
21399 }
21400
21401
21402 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21403    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21404    is the vtable slot index that we will need to put in the virtual call
21405    table later.  */
21406
21407 static void
21408 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21409 {
21410   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21411     {
21412       tree token = OBJ_TYPE_REF_TOKEN (addr);
21413       if (TREE_CODE (token) == INTEGER_CST)
21414         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21415     }
21416 }
21417
21418 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21419    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21420    with NEW_INSN.  */
21421
21422 static void
21423 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21424 {
21425   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21426
21427   if (vtable_slot != (unsigned int) -1)
21428     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21429 }
21430
21431 /* Called by the final INSN scan whenever we see a virtual function call.
21432    Make an entry into the virtual call table, recording the point of call
21433    and the slot index of the vtable entry used to call the virtual member
21434    function.  The slot index was associated with the INSN_UID during the
21435    lowering to RTL.  */
21436
21437 static void
21438 dwarf2out_virtual_call (int insn_uid)
21439 {
21440   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21441   vcall_entry e;
21442
21443   if (vtable_slot == (unsigned int) -1)
21444     return;
21445
21446   e.poc_label_num = poc_label_num++;
21447   e.vtable_slot = vtable_slot;
21448   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21449
21450   /* Drop a label at the return point to mark the point of call.  */
21451   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21452 }
21453
21454 /* Called by the final INSN scan whenever we see a var location.  We
21455    use it to drop labels in the right places, and throw the location in
21456    our lookup table.  */
21457
21458 static void
21459 dwarf2out_var_location (rtx loc_note)
21460 {
21461   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21462   struct var_loc_node *newloc;
21463   rtx next_real;
21464   static const char *last_label;
21465   static const char *last_postcall_label;
21466   static bool last_in_cold_section_p;
21467   tree decl;
21468
21469   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21470     return;
21471
21472   next_real = next_real_insn (loc_note);
21473   /* If there are no instructions which would be affected by this note,
21474      don't do anything.  */
21475   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21476     return;
21477
21478   /* If there were any real insns between note we processed last time
21479      and this note (or if it is the first note), clear
21480      last_{,postcall_}label so that they are not reused this time.  */
21481   if (last_var_location_insn == NULL_RTX
21482       || last_var_location_insn != next_real
21483       || last_in_cold_section_p != in_cold_section_p)
21484     {
21485       last_label = NULL;
21486       last_postcall_label = NULL;
21487     }
21488
21489   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21490   newloc = add_var_loc_to_decl (decl, loc_note,
21491                                 NOTE_DURING_CALL_P (loc_note)
21492                                 ? last_postcall_label : last_label);
21493   if (newloc == NULL)
21494     return;
21495
21496   /* If there were no real insns between note we processed last time
21497      and this note, use the label we emitted last time.  Otherwise
21498      create a new label and emit it.  */
21499   if (last_label == NULL)
21500     {
21501       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21502       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21503       loclabel_num++;
21504       last_label = ggc_strdup (loclabel);
21505     }
21506
21507   if (!NOTE_DURING_CALL_P (loc_note))
21508     newloc->label = last_label;
21509   else
21510     {
21511       if (!last_postcall_label)
21512         {
21513           sprintf (loclabel, "%s-1", last_label);
21514           last_postcall_label = ggc_strdup (loclabel);
21515         }
21516       newloc->label = last_postcall_label;
21517     }
21518
21519   last_var_location_insn = next_real;
21520   last_in_cold_section_p = in_cold_section_p;
21521 }
21522
21523 /* We need to reset the locations at the beginning of each
21524    function. We can't do this in the end_function hook, because the
21525    declarations that use the locations won't have been output when
21526    that hook is called.  Also compute have_multiple_function_sections here.  */
21527
21528 static void
21529 dwarf2out_begin_function (tree fun)
21530 {
21531   if (function_section (fun) != text_section)
21532     have_multiple_function_sections = true;
21533
21534   dwarf2out_note_section_used ();
21535 }
21536
21537 /* Output a label to mark the beginning of a source code line entry
21538    and record information relating to this source line, in
21539    'line_info_table' for later output of the .debug_line section.  */
21540
21541 static void
21542 dwarf2out_source_line (unsigned int line, const char *filename,
21543                        int discriminator, bool is_stmt)
21544 {
21545   static bool last_is_stmt = true;
21546
21547   if (debug_info_level >= DINFO_LEVEL_NORMAL
21548       && line != 0)
21549     {
21550       int file_num = maybe_emit_file (lookup_filename (filename));
21551
21552       switch_to_section (current_function_section ());
21553
21554       /* If requested, emit something human-readable.  */
21555       if (flag_debug_asm)
21556         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21557                  filename, line);
21558
21559       if (DWARF2_ASM_LINE_DEBUG_INFO)
21560         {
21561           /* Emit the .loc directive understood by GNU as.  */
21562           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21563           if (is_stmt != last_is_stmt)
21564             {
21565               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21566               last_is_stmt = is_stmt;
21567             }
21568           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21569             fprintf (asm_out_file, " discriminator %d", discriminator);
21570           fputc ('\n', asm_out_file);
21571
21572           /* Indicate that line number info exists.  */
21573           line_info_table_in_use++;
21574         }
21575       else if (function_section (current_function_decl) != text_section)
21576         {
21577           dw_separate_line_info_ref line_info;
21578           targetm.asm_out.internal_label (asm_out_file,
21579                                           SEPARATE_LINE_CODE_LABEL,
21580                                           separate_line_info_table_in_use);
21581
21582           /* Expand the line info table if necessary.  */
21583           if (separate_line_info_table_in_use
21584               == separate_line_info_table_allocated)
21585             {
21586               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21587               separate_line_info_table
21588                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21589                                  separate_line_info_table,
21590                                  separate_line_info_table_allocated);
21591               memset (separate_line_info_table
21592                        + separate_line_info_table_in_use,
21593                       0,
21594                       (LINE_INFO_TABLE_INCREMENT
21595                        * sizeof (dw_separate_line_info_entry)));
21596             }
21597
21598           /* Add the new entry at the end of the line_info_table.  */
21599           line_info
21600             = &separate_line_info_table[separate_line_info_table_in_use++];
21601           line_info->dw_file_num = file_num;
21602           line_info->dw_line_num = line;
21603           line_info->function = current_function_funcdef_no;
21604         }
21605       else
21606         {
21607           dw_line_info_ref line_info;
21608
21609           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21610                                      line_info_table_in_use);
21611
21612           /* Expand the line info table if necessary.  */
21613           if (line_info_table_in_use == line_info_table_allocated)
21614             {
21615               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21616               line_info_table
21617                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21618                                  line_info_table_allocated);
21619               memset (line_info_table + line_info_table_in_use, 0,
21620                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21621             }
21622
21623           /* Add the new entry at the end of the line_info_table.  */
21624           line_info = &line_info_table[line_info_table_in_use++];
21625           line_info->dw_file_num = file_num;
21626           line_info->dw_line_num = line;
21627         }
21628     }
21629 }
21630
21631 /* Record the beginning of a new source file.  */
21632
21633 static void
21634 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21635 {
21636   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21637     {
21638       /* Record the beginning of the file for break_out_includes.  */
21639       dw_die_ref bincl_die;
21640
21641       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21642       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21643     }
21644
21645   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21646     {
21647       int file_num = maybe_emit_file (lookup_filename (filename));
21648
21649       switch_to_section (debug_macinfo_section);
21650       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21651       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21652                                    lineno);
21653
21654       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21655     }
21656 }
21657
21658 /* Record the end of a source file.  */
21659
21660 static void
21661 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21662 {
21663   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21664     /* Record the end of the file for break_out_includes.  */
21665     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21666
21667   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21668     {
21669       switch_to_section (debug_macinfo_section);
21670       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21671     }
21672 }
21673
21674 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21675    the tail part of the directive line, i.e. the part which is past the
21676    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21677
21678 static void
21679 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21680                   const char *buffer ATTRIBUTE_UNUSED)
21681 {
21682   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21683     {
21684       switch_to_section (debug_macinfo_section);
21685       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21686       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21687       dw2_asm_output_nstring (buffer, -1, "The macro");
21688     }
21689 }
21690
21691 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21692    the tail part of the directive line, i.e. the part which is past the
21693    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21694
21695 static void
21696 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21697                  const char *buffer ATTRIBUTE_UNUSED)
21698 {
21699   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21700     {
21701       switch_to_section (debug_macinfo_section);
21702       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21703       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21704       dw2_asm_output_nstring (buffer, -1, "The macro");
21705     }
21706 }
21707
21708 /* Set up for Dwarf output at the start of compilation.  */
21709
21710 static void
21711 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21712 {
21713   /* Allocate the file_table.  */
21714   file_table = htab_create_ggc (50, file_table_hash,
21715                                 file_table_eq, NULL);
21716
21717   /* Allocate the decl_die_table.  */
21718   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21719                                     decl_die_table_eq, NULL);
21720
21721   /* Allocate the decl_loc_table.  */
21722   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21723                                     decl_loc_table_eq, NULL);
21724
21725   /* Allocate the initial hunk of the decl_scope_table.  */
21726   decl_scope_table = VEC_alloc (tree, gc, 256);
21727
21728   /* Allocate the initial hunk of the abbrev_die_table.  */
21729   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21730     (ABBREV_DIE_TABLE_INCREMENT);
21731   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21732   /* Zero-th entry is allocated, but unused.  */
21733   abbrev_die_table_in_use = 1;
21734
21735   /* Allocate the initial hunk of the line_info_table.  */
21736   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21737     (LINE_INFO_TABLE_INCREMENT);
21738   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21739
21740   /* Zero-th entry is allocated, but unused.  */
21741   line_info_table_in_use = 1;
21742
21743   /* Allocate the pubtypes and pubnames vectors.  */
21744   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21745   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21746
21747   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21748   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21749                                       vcall_insn_table_eq, NULL);
21750
21751   incomplete_types = VEC_alloc (tree, gc, 64);
21752
21753   used_rtx_array = VEC_alloc (rtx, gc, 32);
21754
21755   debug_info_section = get_section (DEBUG_INFO_SECTION,
21756                                     SECTION_DEBUG, NULL);
21757   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21758                                       SECTION_DEBUG, NULL);
21759   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21760                                        SECTION_DEBUG, NULL);
21761   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21762                                        SECTION_DEBUG, NULL);
21763   debug_line_section = get_section (DEBUG_LINE_SECTION,
21764                                     SECTION_DEBUG, NULL);
21765   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21766                                    SECTION_DEBUG, NULL);
21767   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21768                                         SECTION_DEBUG, NULL);
21769   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21770                                         SECTION_DEBUG, NULL);
21771   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21772                                      SECTION_DEBUG, NULL);
21773   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21774                                      SECTION_DEBUG, NULL);
21775   debug_str_section = get_section (DEBUG_STR_SECTION,
21776                                    DEBUG_STR_SECTION_FLAGS, NULL);
21777   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21778                                       SECTION_DEBUG, NULL);
21779   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21780                                      SECTION_DEBUG, NULL);
21781
21782   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21783   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21784                                DEBUG_ABBREV_SECTION_LABEL, 0);
21785   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21786   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21787                                COLD_TEXT_SECTION_LABEL, 0);
21788   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21789
21790   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21791                                DEBUG_INFO_SECTION_LABEL, 0);
21792   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21793                                DEBUG_LINE_SECTION_LABEL, 0);
21794   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21795                                DEBUG_RANGES_SECTION_LABEL, 0);
21796   switch_to_section (debug_abbrev_section);
21797   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21798   switch_to_section (debug_info_section);
21799   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21800   switch_to_section (debug_line_section);
21801   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21802
21803   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21804     {
21805       switch_to_section (debug_macinfo_section);
21806       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21807                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21808       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21809     }
21810
21811   switch_to_section (text_section);
21812   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21813   if (flag_reorder_blocks_and_partition)
21814     {
21815       cold_text_section = unlikely_text_section ();
21816       switch_to_section (cold_text_section);
21817       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21818     }
21819
21820 }
21821
21822 /* Called before cgraph_optimize starts outputtting functions, variables
21823    and toplevel asms into assembly.  */
21824
21825 static void
21826 dwarf2out_assembly_start (void)
21827 {
21828   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21829       && dwarf2out_do_cfi_asm ()
21830       && (!(flag_unwind_tables || flag_exceptions)
21831           || targetm.except_unwind_info () != UI_DWARF2))
21832     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21833 }
21834
21835 /* A helper function for dwarf2out_finish called through
21836    htab_traverse.  Emit one queued .debug_str string.  */
21837
21838 static int
21839 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21840 {
21841   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21842
21843   if (node->label && node->refcount)
21844     {
21845       switch_to_section (debug_str_section);
21846       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21847       assemble_string (node->str, strlen (node->str) + 1);
21848     }
21849
21850   return 1;
21851 }
21852
21853 #if ENABLE_ASSERT_CHECKING
21854 /* Verify that all marks are clear.  */
21855
21856 static void
21857 verify_marks_clear (dw_die_ref die)
21858 {
21859   dw_die_ref c;
21860
21861   gcc_assert (! die->die_mark);
21862   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21863 }
21864 #endif /* ENABLE_ASSERT_CHECKING */
21865
21866 /* Clear the marks for a die and its children.
21867    Be cool if the mark isn't set.  */
21868
21869 static void
21870 prune_unmark_dies (dw_die_ref die)
21871 {
21872   dw_die_ref c;
21873
21874   if (die->die_mark)
21875     die->die_mark = 0;
21876   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21877 }
21878
21879 /* Given DIE that we're marking as used, find any other dies
21880    it references as attributes and mark them as used.  */
21881
21882 static void
21883 prune_unused_types_walk_attribs (dw_die_ref die)
21884 {
21885   dw_attr_ref a;
21886   unsigned ix;
21887
21888   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21889     {
21890       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21891         {
21892           /* A reference to another DIE.
21893              Make sure that it will get emitted.
21894              If it was broken out into a comdat group, don't follow it.  */
21895           if (dwarf_version < 4
21896               || a->dw_attr == DW_AT_specification
21897               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21898             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21899         }
21900       /* Set the string's refcount to 0 so that prune_unused_types_mark
21901          accounts properly for it.  */
21902       if (AT_class (a) == dw_val_class_str)
21903         a->dw_attr_val.v.val_str->refcount = 0;
21904     }
21905 }
21906
21907
21908 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21909    to DIE's children.  */
21910
21911 static void
21912 prune_unused_types_mark (dw_die_ref die, int dokids)
21913 {
21914   dw_die_ref c;
21915
21916   if (die->die_mark == 0)
21917     {
21918       /* We haven't done this node yet.  Mark it as used.  */
21919       die->die_mark = 1;
21920
21921       /* We also have to mark its parents as used.
21922          (But we don't want to mark our parents' kids due to this.)  */
21923       if (die->die_parent)
21924         prune_unused_types_mark (die->die_parent, 0);
21925
21926       /* Mark any referenced nodes.  */
21927       prune_unused_types_walk_attribs (die);
21928
21929       /* If this node is a specification,
21930          also mark the definition, if it exists.  */
21931       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21932         prune_unused_types_mark (die->die_definition, 1);
21933     }
21934
21935   if (dokids && die->die_mark != 2)
21936     {
21937       /* We need to walk the children, but haven't done so yet.
21938          Remember that we've walked the kids.  */
21939       die->die_mark = 2;
21940
21941       /* If this is an array type, we need to make sure our
21942          kids get marked, even if they're types.  If we're
21943          breaking out types into comdat sections, do this
21944          for all type definitions.  */
21945       if (die->die_tag == DW_TAG_array_type
21946           || (dwarf_version >= 4
21947               && is_type_die (die) && ! is_declaration_die (die)))
21948         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21949       else
21950         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21951     }
21952 }
21953
21954 /* For local classes, look if any static member functions were emitted
21955    and if so, mark them.  */
21956
21957 static void
21958 prune_unused_types_walk_local_classes (dw_die_ref die)
21959 {
21960   dw_die_ref c;
21961
21962   if (die->die_mark == 2)
21963     return;
21964
21965   switch (die->die_tag)
21966     {
21967     case DW_TAG_structure_type:
21968     case DW_TAG_union_type:
21969     case DW_TAG_class_type:
21970       break;
21971
21972     case DW_TAG_subprogram:
21973       if (!get_AT_flag (die, DW_AT_declaration)
21974           || die->die_definition != NULL)
21975         prune_unused_types_mark (die, 1);
21976       return;
21977
21978     default:
21979       return;
21980     }
21981
21982   /* Mark children.  */
21983   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21984 }
21985
21986 /* Walk the tree DIE and mark types that we actually use.  */
21987
21988 static void
21989 prune_unused_types_walk (dw_die_ref die)
21990 {
21991   dw_die_ref c;
21992
21993   /* Don't do anything if this node is already marked and
21994      children have been marked as well.  */
21995   if (die->die_mark == 2)
21996     return;
21997
21998   switch (die->die_tag)
21999     {
22000     case DW_TAG_structure_type:
22001     case DW_TAG_union_type:
22002     case DW_TAG_class_type:
22003       if (die->die_perennial_p)
22004         break;
22005
22006       for (c = die->die_parent; c; c = c->die_parent)
22007         if (c->die_tag == DW_TAG_subprogram)
22008           break;
22009
22010       /* Finding used static member functions inside of classes
22011          is needed just for local classes, because for other classes
22012          static member function DIEs with DW_AT_specification
22013          are emitted outside of the DW_TAG_*_type.  If we ever change
22014          it, we'd need to call this even for non-local classes.  */
22015       if (c)
22016         prune_unused_types_walk_local_classes (die);
22017
22018       /* It's a type node --- don't mark it.  */
22019       return;
22020
22021     case DW_TAG_const_type:
22022     case DW_TAG_packed_type:
22023     case DW_TAG_pointer_type:
22024     case DW_TAG_reference_type:
22025     case DW_TAG_rvalue_reference_type:
22026     case DW_TAG_volatile_type:
22027     case DW_TAG_typedef:
22028     case DW_TAG_array_type:
22029     case DW_TAG_interface_type:
22030     case DW_TAG_friend:
22031     case DW_TAG_variant_part:
22032     case DW_TAG_enumeration_type:
22033     case DW_TAG_subroutine_type:
22034     case DW_TAG_string_type:
22035     case DW_TAG_set_type:
22036     case DW_TAG_subrange_type:
22037     case DW_TAG_ptr_to_member_type:
22038     case DW_TAG_file_type:
22039       if (die->die_perennial_p)
22040         break;
22041
22042       /* It's a type node --- don't mark it.  */
22043       return;
22044
22045     default:
22046       /* Mark everything else.  */
22047       break;
22048   }
22049
22050   if (die->die_mark == 0)
22051     {
22052       die->die_mark = 1;
22053
22054       /* Now, mark any dies referenced from here.  */
22055       prune_unused_types_walk_attribs (die);
22056     }
22057
22058   die->die_mark = 2;
22059
22060   /* Mark children.  */
22061   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22062 }
22063
22064 /* Increment the string counts on strings referred to from DIE's
22065    attributes.  */
22066
22067 static void
22068 prune_unused_types_update_strings (dw_die_ref die)
22069 {
22070   dw_attr_ref a;
22071   unsigned ix;
22072
22073   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22074     if (AT_class (a) == dw_val_class_str)
22075       {
22076         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22077         s->refcount++;
22078         /* Avoid unnecessarily putting strings that are used less than
22079            twice in the hash table.  */
22080         if (s->refcount
22081             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22082           {
22083             void ** slot;
22084             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22085                                              htab_hash_string (s->str),
22086                                              INSERT);
22087             gcc_assert (*slot == NULL);
22088             *slot = s;
22089           }
22090       }
22091 }
22092
22093 /* Remove from the tree DIE any dies that aren't marked.  */
22094
22095 static void
22096 prune_unused_types_prune (dw_die_ref die)
22097 {
22098   dw_die_ref c;
22099
22100   gcc_assert (die->die_mark);
22101   prune_unused_types_update_strings (die);
22102
22103   if (! die->die_child)
22104     return;
22105
22106   c = die->die_child;
22107   do {
22108     dw_die_ref prev = c;
22109     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22110       if (c == die->die_child)
22111         {
22112           /* No marked children between 'prev' and the end of the list.  */
22113           if (prev == c)
22114             /* No marked children at all.  */
22115             die->die_child = NULL;
22116           else
22117             {
22118               prev->die_sib = c->die_sib;
22119               die->die_child = prev;
22120             }
22121           return;
22122         }
22123
22124     if (c != prev->die_sib)
22125       prev->die_sib = c;
22126     prune_unused_types_prune (c);
22127   } while (c != die->die_child);
22128 }
22129
22130 /* A helper function for dwarf2out_finish called through
22131    htab_traverse.  Clear .debug_str strings that we haven't already
22132    decided to emit.  */
22133
22134 static int
22135 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22136 {
22137   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22138
22139   if (!node->label || !node->refcount)
22140     htab_clear_slot (debug_str_hash, h);
22141
22142   return 1;
22143 }
22144
22145 /* Remove dies representing declarations that we never use.  */
22146
22147 static void
22148 prune_unused_types (void)
22149 {
22150   unsigned int i;
22151   limbo_die_node *node;
22152   comdat_type_node *ctnode;
22153   pubname_ref pub;
22154   dcall_entry *dcall;
22155
22156 #if ENABLE_ASSERT_CHECKING
22157   /* All the marks should already be clear.  */
22158   verify_marks_clear (comp_unit_die ());
22159   for (node = limbo_die_list; node; node = node->next)
22160     verify_marks_clear (node->die);
22161   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22162     verify_marks_clear (ctnode->root_die);
22163 #endif /* ENABLE_ASSERT_CHECKING */
22164
22165   /* Mark types that are used in global variables.  */
22166   premark_types_used_by_global_vars ();
22167
22168   /* Set the mark on nodes that are actually used.  */
22169   prune_unused_types_walk (comp_unit_die ());
22170   for (node = limbo_die_list; node; node = node->next)
22171     prune_unused_types_walk (node->die);
22172   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22173     {
22174       prune_unused_types_walk (ctnode->root_die);
22175       prune_unused_types_mark (ctnode->type_die, 1);
22176     }
22177
22178   /* Also set the mark on nodes referenced from the
22179      pubname_table or arange_table.  */
22180   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22181     prune_unused_types_mark (pub->die, 1);
22182   for (i = 0; i < arange_table_in_use; i++)
22183     prune_unused_types_mark (arange_table[i], 1);
22184
22185   /* Mark nodes referenced from the direct call table.  */
22186   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22187     prune_unused_types_mark (dcall->targ_die, 1);
22188
22189   /* Get rid of nodes that aren't marked; and update the string counts.  */
22190   if (debug_str_hash && debug_str_hash_forced)
22191     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22192   else if (debug_str_hash)
22193     htab_empty (debug_str_hash);
22194   prune_unused_types_prune (comp_unit_die ());
22195   for (node = limbo_die_list; node; node = node->next)
22196     prune_unused_types_prune (node->die);
22197   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22198     prune_unused_types_prune (ctnode->root_die);
22199
22200   /* Leave the marks clear.  */
22201   prune_unmark_dies (comp_unit_die ());
22202   for (node = limbo_die_list; node; node = node->next)
22203     prune_unmark_dies (node->die);
22204   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22205     prune_unmark_dies (ctnode->root_die);
22206 }
22207
22208 /* Set the parameter to true if there are any relative pathnames in
22209    the file table.  */
22210 static int
22211 file_table_relative_p (void ** slot, void *param)
22212 {
22213   bool *p = (bool *) param;
22214   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22215   if (!IS_ABSOLUTE_PATH (d->filename))
22216     {
22217       *p = true;
22218       return 0;
22219     }
22220   return 1;
22221 }
22222
22223 /* Routines to manipulate hash table of comdat type units.  */
22224
22225 static hashval_t
22226 htab_ct_hash (const void *of)
22227 {
22228   hashval_t h;
22229   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22230
22231   memcpy (&h, type_node->signature, sizeof (h));
22232   return h;
22233 }
22234
22235 static int
22236 htab_ct_eq (const void *of1, const void *of2)
22237 {
22238   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22239   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22240
22241   return (! memcmp (type_node_1->signature, type_node_2->signature,
22242                     DWARF_TYPE_SIGNATURE_SIZE));
22243 }
22244
22245 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22246    to the location it would have been added, should we know its
22247    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22248    probably improve compactness of debug info, removing equivalent
22249    abbrevs, and hide any differences caused by deferring the
22250    computation of the assembler name, triggered by e.g. PCH.  */
22251
22252 static inline void
22253 move_linkage_attr (dw_die_ref die)
22254 {
22255   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22256   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22257
22258   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22259               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22260
22261   while (--ix > 0)
22262     {
22263       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22264
22265       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22266         break;
22267     }
22268
22269   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22270     {
22271       VEC_pop (dw_attr_node, die->die_attr);
22272       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22273     }
22274 }
22275
22276 /* Helper function for resolve_addr, attempt to resolve
22277    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22278    SYMBOL_REFs refer to variables emitted in the current CU.  */
22279
22280 static int
22281 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22282 {
22283   rtx rtl = *addr;
22284
22285   if (GET_CODE (rtl) == CONST_STRING)
22286     {
22287       size_t len = strlen (XSTR (rtl, 0)) + 1;
22288       tree t = build_string (len, XSTR (rtl, 0));
22289       tree tlen = build_int_cst (NULL_TREE, len - 1);
22290       TREE_TYPE (t)
22291         = build_array_type (char_type_node, build_index_type (tlen));
22292       rtl = lookup_constant_def (t);
22293       if (!rtl || !MEM_P (rtl))
22294         return 1;
22295       rtl = XEXP (rtl, 0);
22296       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22297       *addr = rtl;
22298       return 0;
22299     }
22300
22301   if (GET_CODE (rtl) == SYMBOL_REF
22302       && SYMBOL_REF_DECL (rtl)
22303       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22304     return 1;
22305
22306   if (GET_CODE (rtl) == CONST
22307       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22308     return 1;
22309
22310   return 0;
22311 }
22312
22313 /* Helper function for resolve_addr, handle one location
22314    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22315    the location list couldn't be resolved.  */
22316
22317 static bool
22318 resolve_addr_in_expr (dw_loc_descr_ref loc)
22319 {
22320   for (; loc; loc = loc->dw_loc_next)
22321     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22322          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22323         || (loc->dw_loc_opc == DW_OP_implicit_value
22324             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22325             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22326       return false;
22327     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22328              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22329       {
22330         dw_die_ref ref
22331           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22332         if (ref == NULL)
22333           return false;
22334         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22335         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22336         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22337       }
22338   return true;
22339 }
22340
22341 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22342    an address in .rodata section if the string literal is emitted there,
22343    or remove the containing location list or replace DW_AT_const_value
22344    with DW_AT_location and empty location expression, if it isn't found
22345    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22346    to something that has been emitted in the current CU.  */
22347
22348 static void
22349 resolve_addr (dw_die_ref die)
22350 {
22351   dw_die_ref c;
22352   dw_attr_ref a;
22353   dw_loc_list_ref *curr;
22354   unsigned ix;
22355
22356   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22357     switch (AT_class (a))
22358       {
22359       case dw_val_class_loc_list:
22360         curr = AT_loc_list_ptr (a);
22361         while (*curr)
22362           {
22363             if (!resolve_addr_in_expr ((*curr)->expr))
22364               {
22365                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22366                 if (next && (*curr)->ll_symbol)
22367                   {
22368                     gcc_assert (!next->ll_symbol);
22369                     next->ll_symbol = (*curr)->ll_symbol;
22370                   }
22371                 *curr = next;
22372               }
22373             else
22374               curr = &(*curr)->dw_loc_next;
22375           }
22376         if (!AT_loc_list (a))
22377           {
22378             remove_AT (die, a->dw_attr);
22379             ix--;
22380           }
22381         break;
22382       case dw_val_class_loc:
22383         if (!resolve_addr_in_expr (AT_loc (a)))
22384           {
22385             remove_AT (die, a->dw_attr);
22386             ix--;
22387           }
22388         break;
22389       case dw_val_class_addr:
22390         if (a->dw_attr == DW_AT_const_value
22391             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22392           {
22393             remove_AT (die, a->dw_attr);
22394             ix--;
22395           }
22396         break;
22397       default:
22398         break;
22399       }
22400
22401   FOR_EACH_CHILD (die, c, resolve_addr (c));
22402 }
22403
22404 /* Output stuff that dwarf requires at the end of every file,
22405    and generate the DWARF-2 debugging info.  */
22406
22407 static void
22408 dwarf2out_finish (const char *filename)
22409 {
22410   limbo_die_node *node, *next_node;
22411   comdat_type_node *ctnode;
22412   htab_t comdat_type_table;
22413   dw_die_ref die = 0;
22414   unsigned int i;
22415
22416   gen_remaining_tmpl_value_param_die_attribute ();
22417
22418   /* Add the name for the main input file now.  We delayed this from
22419      dwarf2out_init to avoid complications with PCH.  */
22420   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22421   if (!IS_ABSOLUTE_PATH (filename))
22422     add_comp_dir_attribute (comp_unit_die ());
22423   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22424     {
22425       bool p = false;
22426       htab_traverse (file_table, file_table_relative_p, &p);
22427       if (p)
22428         add_comp_dir_attribute (comp_unit_die ());
22429     }
22430
22431   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22432     {
22433       add_location_or_const_value_attribute (
22434         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22435         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22436         DW_AT_location);
22437     }
22438
22439   /* Traverse the limbo die list, and add parent/child links.  The only
22440      dies without parents that should be here are concrete instances of
22441      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22442      For concrete instances, we can get the parent die from the abstract
22443      instance.  */
22444   for (node = limbo_die_list; node; node = next_node)
22445     {
22446       next_node = node->next;
22447       die = node->die;
22448
22449       if (die->die_parent == NULL)
22450         {
22451           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22452
22453           if (origin)
22454             add_child_die (origin->die_parent, die);
22455           else if (is_cu_die (die))
22456             ;
22457           else if (seen_error ())
22458             /* It's OK to be confused by errors in the input.  */
22459             add_child_die (comp_unit_die (), die);
22460           else
22461             {
22462               /* In certain situations, the lexical block containing a
22463                  nested function can be optimized away, which results
22464                  in the nested function die being orphaned.  Likewise
22465                  with the return type of that nested function.  Force
22466                  this to be a child of the containing function.
22467
22468                  It may happen that even the containing function got fully
22469                  inlined and optimized out.  In that case we are lost and
22470                  assign the empty child.  This should not be big issue as
22471                  the function is likely unreachable too.  */
22472               tree context = NULL_TREE;
22473
22474               gcc_assert (node->created_for);
22475
22476               if (DECL_P (node->created_for))
22477                 context = DECL_CONTEXT (node->created_for);
22478               else if (TYPE_P (node->created_for))
22479                 context = TYPE_CONTEXT (node->created_for);
22480
22481               gcc_assert (context
22482                           && (TREE_CODE (context) == FUNCTION_DECL
22483                               || TREE_CODE (context) == NAMESPACE_DECL));
22484
22485               origin = lookup_decl_die (context);
22486               if (origin)
22487                 add_child_die (origin, die);
22488               else
22489                 add_child_die (comp_unit_die (), die);
22490             }
22491         }
22492     }
22493
22494   limbo_die_list = NULL;
22495
22496   resolve_addr (comp_unit_die ());
22497
22498   for (node = deferred_asm_name; node; node = node->next)
22499     {
22500       tree decl = node->created_for;
22501       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22502         {
22503           add_linkage_attr (node->die, decl);
22504           move_linkage_attr (node->die);
22505         }
22506     }
22507
22508   deferred_asm_name = NULL;
22509
22510   /* Walk through the list of incomplete types again, trying once more to
22511      emit full debugging info for them.  */
22512   retry_incomplete_types ();
22513
22514   if (flag_eliminate_unused_debug_types)
22515     prune_unused_types ();
22516
22517   /* Generate separate CUs for each of the include files we've seen.
22518      They will go into limbo_die_list.  */
22519   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22520     break_out_includes (comp_unit_die ());
22521
22522   /* Generate separate COMDAT sections for type DIEs. */
22523   if (dwarf_version >= 4)
22524     {
22525       break_out_comdat_types (comp_unit_die ());
22526
22527       /* Each new type_unit DIE was added to the limbo die list when created.
22528          Since these have all been added to comdat_type_list, clear the
22529          limbo die list.  */
22530       limbo_die_list = NULL;
22531
22532       /* For each new comdat type unit, copy declarations for incomplete
22533          types to make the new unit self-contained (i.e., no direct
22534          references to the main compile unit).  */
22535       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22536         copy_decls_for_unworthy_types (ctnode->root_die);
22537       copy_decls_for_unworthy_types (comp_unit_die ());
22538
22539       /* In the process of copying declarations from one unit to another,
22540          we may have left some declarations behind that are no longer
22541          referenced.  Prune them.  */
22542       prune_unused_types ();
22543     }
22544
22545   /* Traverse the DIE's and add add sibling attributes to those DIE's
22546      that have children.  */
22547   add_sibling_attributes (comp_unit_die ());
22548   for (node = limbo_die_list; node; node = node->next)
22549     add_sibling_attributes (node->die);
22550   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22551     add_sibling_attributes (ctnode->root_die);
22552
22553   /* Output a terminator label for the .text section.  */
22554   switch_to_section (text_section);
22555   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22556   if (flag_reorder_blocks_and_partition)
22557     {
22558       switch_to_section (unlikely_text_section ());
22559       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22560     }
22561
22562   /* We can only use the low/high_pc attributes if all of the code was
22563      in .text.  */
22564   if (!have_multiple_function_sections
22565       || !(dwarf_version >= 3 || !dwarf_strict))
22566     {
22567       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22568       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22569     }
22570
22571   else
22572     {
22573       unsigned fde_idx = 0;
22574       bool range_list_added = false;
22575
22576       /* We need to give .debug_loc and .debug_ranges an appropriate
22577          "base address".  Use zero so that these addresses become
22578          absolute.  Historically, we've emitted the unexpected
22579          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22580          Emit both to give time for other tools to adapt.  */
22581       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22582       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22583
22584       if (text_section_used)
22585         add_ranges_by_labels (comp_unit_die (), text_section_label,
22586                               text_end_label, &range_list_added);
22587       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22588         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22589                               cold_end_label, &range_list_added);
22590
22591       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22592         {
22593           dw_fde_ref fde = &fde_table[fde_idx];
22594
22595           if (fde->dw_fde_switched_sections)
22596             {
22597               if (!fde->in_std_section)
22598                 add_ranges_by_labels (comp_unit_die (),
22599                                       fde->dw_fde_hot_section_label,
22600                                       fde->dw_fde_hot_section_end_label,
22601                                       &range_list_added);
22602               if (!fde->cold_in_std_section)
22603                 add_ranges_by_labels (comp_unit_die (),
22604                                       fde->dw_fde_unlikely_section_label,
22605                                       fde->dw_fde_unlikely_section_end_label,
22606                                       &range_list_added);
22607             }
22608           else if (!fde->in_std_section)
22609             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22610                                   fde->dw_fde_end, &range_list_added);
22611         }
22612
22613       if (range_list_added)
22614         add_ranges (NULL);
22615     }
22616
22617   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22618     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22619                     debug_line_section_label);
22620
22621   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22622     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
22623
22624   /* Output all of the compilation units.  We put the main one last so that
22625      the offsets are available to output_pubnames.  */
22626   for (node = limbo_die_list; node; node = node->next)
22627     output_comp_unit (node->die, 0);
22628
22629   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22630   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22631     {
22632       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22633
22634       /* Don't output duplicate types.  */
22635       if (*slot != HTAB_EMPTY_ENTRY)
22636         continue;
22637
22638       /* Add a pointer to the line table for the main compilation unit
22639          so that the debugger can make sense of DW_AT_decl_file
22640          attributes.  */
22641       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22642         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22643                         debug_line_section_label);
22644
22645       output_comdat_type_unit (ctnode);
22646       *slot = ctnode;
22647     }
22648   htab_delete (comdat_type_table);
22649
22650   /* Output the main compilation unit if non-empty or if .debug_macinfo
22651      has been emitted.  */
22652   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22653
22654   /* Output the abbreviation table.  */
22655   switch_to_section (debug_abbrev_section);
22656   output_abbrev_section ();
22657
22658   /* Output location list section if necessary.  */
22659   if (have_location_lists)
22660     {
22661       /* Output the location lists info.  */
22662       switch_to_section (debug_loc_section);
22663       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22664                                    DEBUG_LOC_SECTION_LABEL, 0);
22665       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22666       output_location_lists (die);
22667     }
22668
22669   /* Output public names table if necessary.  */
22670   if (!VEC_empty (pubname_entry, pubname_table))
22671     {
22672       switch_to_section (debug_pubnames_section);
22673       output_pubnames (pubname_table);
22674     }
22675
22676   /* Output public types table if necessary.  */
22677   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22678      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22679      simply won't look for the section.  */
22680   if (!VEC_empty (pubname_entry, pubtype_table))
22681     {
22682       switch_to_section (debug_pubtypes_section);
22683       output_pubnames (pubtype_table);
22684     }
22685
22686   /* Output direct and virtual call tables if necessary.  */
22687   if (!VEC_empty (dcall_entry, dcall_table))
22688     {
22689       switch_to_section (debug_dcall_section);
22690       output_dcall_table ();
22691     }
22692   if (!VEC_empty (vcall_entry, vcall_table))
22693     {
22694       switch_to_section (debug_vcall_section);
22695       output_vcall_table ();
22696     }
22697
22698   /* Output the address range information.  We only put functions in the arange
22699      table, so don't write it out if we don't have any.  */
22700   if (fde_table_in_use)
22701     {
22702       switch_to_section (debug_aranges_section);
22703       output_aranges ();
22704     }
22705
22706   /* Output ranges section if necessary.  */
22707   if (ranges_table_in_use)
22708     {
22709       switch_to_section (debug_ranges_section);
22710       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22711       output_ranges ();
22712     }
22713
22714   /* Output the source line correspondence table.  We must do this
22715      even if there is no line information.  Otherwise, on an empty
22716      translation unit, we will generate a present, but empty,
22717      .debug_info section.  IRIX 6.5 `nm' will then complain when
22718      examining the file.  This is done late so that any filenames
22719      used by the debug_info section are marked as 'used'.  */
22720   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22721     {
22722       switch_to_section (debug_line_section);
22723       output_line_info ();
22724     }
22725
22726   /* Have to end the macro section.  */
22727   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22728     {
22729       switch_to_section (debug_macinfo_section);
22730       dw2_asm_output_data (1, 0, "End compilation unit");
22731     }
22732
22733   /* If we emitted any DW_FORM_strp form attribute, output the string
22734      table too.  */
22735   if (debug_str_hash)
22736     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22737 }
22738
22739 #include "gt-dwarf2out.h"