OSDN Git Service

* tree-ssa.c (find_released_ssa_name): Handle NULL wi.
[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 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.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 "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.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
95 #ifdef DWARF2_DEBUGGING_INFO
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97
98 static rtx last_var_location_insn;
99 #endif
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 #ifndef DWARF2_FRAME_INFO
118 # ifdef DWARF2_DEBUGGING_INFO
119 #  define DWARF2_FRAME_INFO \
120   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
121 # else
122 #  define DWARF2_FRAME_INFO 0
123 # endif
124 #endif
125
126 /* Map register numbers held in the call frame info that gcc has
127    collected using DWARF_FRAME_REGNUM to those that should be output in
128    .debug_frame and .eh_frame.  */
129 #ifndef DWARF2_FRAME_REG_OUT
130 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
131 #endif
132
133 /* Save the result of dwarf2out_do_frame across PCH.  */
134 static GTY(()) bool saved_do_cfi_asm = 0;
135
136 /* Decide whether we want to emit frame unwind information for the current
137    translation unit.  */
138
139 int
140 dwarf2out_do_frame (void)
141 {
142   /* We want to emit correct CFA location expressions or lists, so we
143      have to return true if we're going to output debug info, even if
144      we're not going to output frame or unwind info.  */
145   return (write_symbols == DWARF2_DEBUG
146           || write_symbols == VMS_AND_DWARF2_DEBUG
147           || DWARF2_FRAME_INFO || saved_do_cfi_asm
148 #ifdef DWARF2_UNWIND_INFO
149           || (DWARF2_UNWIND_INFO
150               && (flag_unwind_tables
151                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
152 #endif
153           );
154 }
155
156 /* Decide whether to emit frame unwind via assembler directives.  */
157
158 int
159 dwarf2out_do_cfi_asm (void)
160 {
161   int enc;
162
163 #ifdef MIPS_DEBUGGING_INFO
164   return false;
165 #endif
166   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
167     return false;
168   if (saved_do_cfi_asm)
169     return true;
170   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
171     return false;
172
173   /* Make sure the personality encoding is one the assembler can support.
174      In particular, aligned addresses can't be handled.  */
175   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
176   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
177     return false;
178   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
179   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
180     return false;
181
182   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
183     {
184 #ifdef TARGET_UNWIND_INFO
185       return false;
186 #else
187       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
188         return false;
189 #endif
190     }
191
192   saved_do_cfi_asm = true;
193   return true;
194 }
195
196 /* The size of the target's pointer type.  */
197 #ifndef PTR_SIZE
198 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
199 #endif
200
201 /* Array of RTXes referenced by the debugging information, which therefore
202    must be kept around forever.  */
203 static GTY(()) VEC(rtx,gc) *used_rtx_array;
204
205 /* A pointer to the base of a list of incomplete types which might be
206    completed at some later time.  incomplete_types_list needs to be a
207    VEC(tree,gc) because we want to tell the garbage collector about
208    it.  */
209 static GTY(()) VEC(tree,gc) *incomplete_types;
210
211 /* A pointer to the base of a table of references to declaration
212    scopes.  This table is a display which tracks the nesting
213    of declaration scopes at the current scope and containing
214    scopes.  This table is used to find the proper place to
215    define type declaration DIE's.  */
216 static GTY(()) VEC(tree,gc) *decl_scope_table;
217
218 /* Pointers to various DWARF2 sections.  */
219 static GTY(()) section *debug_info_section;
220 static GTY(()) section *debug_abbrev_section;
221 static GTY(()) section *debug_aranges_section;
222 static GTY(()) section *debug_macinfo_section;
223 static GTY(()) section *debug_line_section;
224 static GTY(()) section *debug_loc_section;
225 static GTY(()) section *debug_pubnames_section;
226 static GTY(()) section *debug_pubtypes_section;
227 static GTY(()) section *debug_dcall_section;
228 static GTY(()) section *debug_vcall_section;
229 static GTY(()) section *debug_str_section;
230 static GTY(()) section *debug_ranges_section;
231 static GTY(()) section *debug_frame_section;
232
233 /* Personality decl of current unit.  Used only when assembler does not support
234    personality CFI.  */
235 static GTY(()) rtx current_unit_personality;
236
237 /* How to start an assembler comment.  */
238 #ifndef ASM_COMMENT_START
239 #define ASM_COMMENT_START ";#"
240 #endif
241
242 typedef struct dw_cfi_struct *dw_cfi_ref;
243 typedef struct dw_fde_struct *dw_fde_ref;
244 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
245
246 /* Call frames are described using a sequence of Call Frame
247    Information instructions.  The register number, offset
248    and address fields are provided as possible operands;
249    their use is selected by the opcode field.  */
250
251 enum dw_cfi_oprnd_type {
252   dw_cfi_oprnd_unused,
253   dw_cfi_oprnd_reg_num,
254   dw_cfi_oprnd_offset,
255   dw_cfi_oprnd_addr,
256   dw_cfi_oprnd_loc
257 };
258
259 typedef union GTY(()) dw_cfi_oprnd_struct {
260   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
261   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
262   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
263   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
264 }
265 dw_cfi_oprnd;
266
267 typedef struct GTY(()) dw_cfi_struct {
268   dw_cfi_ref dw_cfi_next;
269   enum dwarf_call_frame_info dw_cfi_opc;
270   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
271     dw_cfi_oprnd1;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd2;
274 }
275 dw_cfi_node;
276
277 /* This is how we define the location of the CFA. We use to handle it
278    as REG + OFFSET all the time,  but now it can be more complex.
279    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
280    Instead of passing around REG and OFFSET, we pass a copy
281    of this structure.  */
282 typedef struct GTY(()) cfa_loc {
283   HOST_WIDE_INT offset;
284   HOST_WIDE_INT base_offset;
285   unsigned int reg;
286   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
287   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
288 } dw_cfa_location;
289
290 /* All call frame descriptions (FDE's) in the GCC generated DWARF
291    refer to a single Common Information Entry (CIE), defined at
292    the beginning of the .debug_frame section.  This use of a single
293    CIE obviates the need to keep track of multiple CIE's
294    in the DWARF generation routines below.  */
295
296 typedef struct GTY(()) dw_fde_struct {
297   tree decl;
298   const char *dw_fde_begin;
299   const char *dw_fde_current_label;
300   const char *dw_fde_end;
301   const char *dw_fde_hot_section_label;
302   const char *dw_fde_hot_section_end_label;
303   const char *dw_fde_unlikely_section_label;
304   const char *dw_fde_unlikely_section_end_label;
305   dw_cfi_ref dw_fde_cfi;
306   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
307   unsigned funcdef_number;
308   HOST_WIDE_INT stack_realignment;
309   /* Dynamic realign argument pointer register.  */
310   unsigned int drap_reg;
311   /* Virtual dynamic realign argument pointer register.  */
312   unsigned int vdrap_reg;
313   unsigned all_throwers_are_sibcalls : 1;
314   unsigned nothrow : 1;
315   unsigned uses_eh_lsda : 1;
316   /* Whether we did stack realign in this call frame.  */
317   unsigned stack_realign : 1;
318   /* Whether dynamic realign argument pointer register has been saved.  */
319   unsigned drap_reg_saved: 1;
320   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
321   unsigned in_std_section : 1;
322   /* True iff dw_fde_unlikely_section_label is in text_section or
323      cold_text_section.  */
324   unsigned cold_in_std_section : 1;
325   /* True iff switched sections.  */
326   unsigned dw_fde_switched_sections : 1;
327   /* True iff switching from cold to hot section.  */
328   unsigned dw_fde_switched_cold_to_hot : 1;
329 }
330 dw_fde_node;
331
332 /* Maximum size (in bytes) of an artificially generated label.  */
333 #define MAX_ARTIFICIAL_LABEL_BYTES      30
334
335 /* The size of addresses as they appear in the Dwarf 2 data.
336    Some architectures use word addresses to refer to code locations,
337    but Dwarf 2 info always uses byte addresses.  On such machines,
338    Dwarf 2 addresses need to be larger than the architecture's
339    pointers.  */
340 #ifndef DWARF2_ADDR_SIZE
341 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
342 #endif
343
344 /* The size in bytes of a DWARF field indicating an offset or length
345    relative to a debug info section, specified to be 4 bytes in the
346    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
347    as PTR_SIZE.  */
348
349 #ifndef DWARF_OFFSET_SIZE
350 #define DWARF_OFFSET_SIZE 4
351 #endif
352
353 /* The size in bytes of a DWARF 4 type signature.  */
354
355 #ifndef DWARF_TYPE_SIGNATURE_SIZE
356 #define DWARF_TYPE_SIGNATURE_SIZE 8
357 #endif
358
359 /* According to the (draft) DWARF 3 specification, the initial length
360    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
361    bytes are 0xffffffff, followed by the length stored in the next 8
362    bytes.
363
364    However, the SGI/MIPS ABI uses an initial length which is equal to
365    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
366
367 #ifndef DWARF_INITIAL_LENGTH_SIZE
368 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
369 #endif
370
371 /* Round SIZE up to the nearest BOUNDARY.  */
372 #define DWARF_ROUND(SIZE,BOUNDARY) \
373   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
374
375 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
376 #ifndef DWARF_CIE_DATA_ALIGNMENT
377 #ifdef STACK_GROWS_DOWNWARD
378 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
379 #else
380 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
381 #endif
382 #endif
383
384 /* CIE identifier.  */
385 #if HOST_BITS_PER_WIDE_INT >= 64
386 #define DWARF_CIE_ID \
387   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
388 #else
389 #define DWARF_CIE_ID DW_CIE_ID
390 #endif
391
392 /* A pointer to the base of a table that contains frame description
393    information for each routine.  */
394 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
395
396 /* Number of elements currently allocated for fde_table.  */
397 static GTY(()) unsigned fde_table_allocated;
398
399 /* Number of elements in fde_table currently in use.  */
400 static GTY(()) unsigned fde_table_in_use;
401
402 /* Size (in elements) of increments by which we may expand the
403    fde_table.  */
404 #define FDE_TABLE_INCREMENT 256
405
406 /* Get the current fde_table entry we should use.  */
407
408 static inline dw_fde_ref
409 current_fde (void)
410 {
411   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
412 }
413
414 /* A list of call frame insns for the CIE.  */
415 static GTY(()) dw_cfi_ref cie_cfi_head;
416
417 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
418 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
419    attribute that accelerates the lookup of the FDE associated
420    with the subprogram.  This variable holds the table index of the FDE
421    associated with the current function (body) definition.  */
422 static unsigned current_funcdef_fde;
423 #endif
424
425 struct GTY(()) indirect_string_node {
426   const char *str;
427   unsigned int refcount;
428   enum dwarf_form form;
429   char *label;
430 };
431
432 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
433
434 /* True if the compilation unit has location entries that reference
435    debug strings.  */
436 static GTY(()) bool debug_str_hash_forced = false;
437
438 static GTY(()) int dw2_string_counter;
439 static GTY(()) unsigned long dwarf2out_cfi_label_num;
440
441 /* True if the compilation unit places functions in more than one section.  */
442 static GTY(()) bool have_multiple_function_sections = false;
443
444 /* Whether the default text and cold text sections have been used at all.  */
445
446 static GTY(()) bool text_section_used = false;
447 static GTY(()) bool cold_text_section_used = false;
448
449 /* The default cold text section.  */
450 static GTY(()) section *cold_text_section;
451
452 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
453
454 /* Forward declarations for functions defined in this file.  */
455
456 static char *stripattributes (const char *);
457 static const char *dwarf_cfi_name (unsigned);
458 static dw_cfi_ref new_cfi (void);
459 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
460 static void add_fde_cfi (const char *, dw_cfi_ref);
461 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
462 static void lookup_cfa (dw_cfa_location *);
463 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
464 #ifdef DWARF2_UNWIND_INFO
465 static void initial_return_save (rtx);
466 #endif
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 void flush_queued_reg_saves (void);
474 static bool clobbers_queued_reg_save (const_rtx);
475 static void dwarf2out_frame_debug_expr (rtx, const char *);
476
477 /* Support for complex CFA locations.  */
478 static void output_cfa_loc (dw_cfi_ref);
479 static void output_cfa_loc_raw (dw_cfi_ref);
480 static void get_cfa_from_loc_descr (dw_cfa_location *,
481                                     struct dw_loc_descr_struct *);
482 static struct dw_loc_descr_struct *build_cfa_loc
483   (dw_cfa_location *, HOST_WIDE_INT);
484 static struct dw_loc_descr_struct *build_cfa_aligned_loc
485   (HOST_WIDE_INT, HOST_WIDE_INT);
486 static void def_cfa_1 (const char *, dw_cfa_location *);
487
488 /* How to start an assembler comment.  */
489 #ifndef ASM_COMMENT_START
490 #define ASM_COMMENT_START ";#"
491 #endif
492
493 /* Data and reference forms for relocatable data.  */
494 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
495 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
496
497 #ifndef DEBUG_FRAME_SECTION
498 #define DEBUG_FRAME_SECTION     ".debug_frame"
499 #endif
500
501 #ifndef FUNC_BEGIN_LABEL
502 #define FUNC_BEGIN_LABEL        "LFB"
503 #endif
504
505 #ifndef FUNC_END_LABEL
506 #define FUNC_END_LABEL          "LFE"
507 #endif
508
509 #ifndef FRAME_BEGIN_LABEL
510 #define FRAME_BEGIN_LABEL       "Lframe"
511 #endif
512 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
513 #define CIE_END_LABEL           "LECIE"
514 #define FDE_LABEL               "LSFDE"
515 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
516 #define FDE_END_LABEL           "LEFDE"
517 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
518 #define LINE_NUMBER_END_LABEL   "LELT"
519 #define LN_PROLOG_AS_LABEL      "LASLTP"
520 #define LN_PROLOG_END_LABEL     "LELTP"
521 #define DIE_LABEL_PREFIX        "DW"
522
523 /* The DWARF 2 CFA column which tracks the return address.  Normally this
524    is the column for PC, or the first column after all of the hard
525    registers.  */
526 #ifndef DWARF_FRAME_RETURN_COLUMN
527 #ifdef PC_REGNUM
528 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
529 #else
530 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
531 #endif
532 #endif
533
534 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
535    default, we just provide columns for all registers.  */
536 #ifndef DWARF_FRAME_REGNUM
537 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
538 #endif
539 \f
540 /* Hook used by __throw.  */
541
542 rtx
543 expand_builtin_dwarf_sp_column (void)
544 {
545   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
546   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
547 }
548
549 /* Return a pointer to a copy of the section string name S with all
550    attributes stripped off, and an asterisk prepended (for assemble_name).  */
551
552 static inline char *
553 stripattributes (const char *s)
554 {
555   char *stripped = XNEWVEC (char, strlen (s) + 2);
556   char *p = stripped;
557
558   *p++ = '*';
559
560   while (*s && *s != ',')
561     *p++ = *s++;
562
563   *p = '\0';
564   return stripped;
565 }
566
567 /* MEM is a memory reference for the register size table, each element of
568    which has mode MODE.  Initialize column C as a return address column.  */
569
570 static void
571 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
572 {
573   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
574   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
575   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
576 }
577
578 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
579
580 static inline HOST_WIDE_INT
581 div_data_align (HOST_WIDE_INT off)
582 {
583   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
584   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
585   return r;
586 }
587
588 /* Return true if we need a signed version of a given opcode
589    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
590
591 static inline bool
592 need_data_align_sf_opcode (HOST_WIDE_INT off)
593 {
594   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
595 }
596
597 /* Generate code to initialize the register size table.  */
598
599 void
600 expand_builtin_init_dwarf_reg_sizes (tree address)
601 {
602   unsigned int i;
603   enum machine_mode mode = TYPE_MODE (char_type_node);
604   rtx addr = expand_normal (address);
605   rtx mem = gen_rtx_MEM (BLKmode, addr);
606   bool wrote_return_column = false;
607
608   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
609     {
610       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
611
612       if (rnum < DWARF_FRAME_REGISTERS)
613         {
614           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
615           enum machine_mode save_mode = reg_raw_mode[i];
616           HOST_WIDE_INT size;
617
618           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
619             save_mode = choose_hard_reg_mode (i, 1, true);
620           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
621             {
622               if (save_mode == VOIDmode)
623                 continue;
624               wrote_return_column = true;
625             }
626           size = GET_MODE_SIZE (save_mode);
627           if (offset < 0)
628             continue;
629
630           emit_move_insn (adjust_address (mem, mode, offset),
631                           gen_int_mode (size, mode));
632         }
633     }
634
635   if (!wrote_return_column)
636     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
637
638 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
639   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
640 #endif
641
642   targetm.init_dwarf_reg_sizes_extra (address);
643 }
644
645 /* Convert a DWARF call frame info. operation to its string name */
646
647 static const char *
648 dwarf_cfi_name (unsigned int cfi_opc)
649 {
650   switch (cfi_opc)
651     {
652     case DW_CFA_advance_loc:
653       return "DW_CFA_advance_loc";
654     case DW_CFA_offset:
655       return "DW_CFA_offset";
656     case DW_CFA_restore:
657       return "DW_CFA_restore";
658     case DW_CFA_nop:
659       return "DW_CFA_nop";
660     case DW_CFA_set_loc:
661       return "DW_CFA_set_loc";
662     case DW_CFA_advance_loc1:
663       return "DW_CFA_advance_loc1";
664     case DW_CFA_advance_loc2:
665       return "DW_CFA_advance_loc2";
666     case DW_CFA_advance_loc4:
667       return "DW_CFA_advance_loc4";
668     case DW_CFA_offset_extended:
669       return "DW_CFA_offset_extended";
670     case DW_CFA_restore_extended:
671       return "DW_CFA_restore_extended";
672     case DW_CFA_undefined:
673       return "DW_CFA_undefined";
674     case DW_CFA_same_value:
675       return "DW_CFA_same_value";
676     case DW_CFA_register:
677       return "DW_CFA_register";
678     case DW_CFA_remember_state:
679       return "DW_CFA_remember_state";
680     case DW_CFA_restore_state:
681       return "DW_CFA_restore_state";
682     case DW_CFA_def_cfa:
683       return "DW_CFA_def_cfa";
684     case DW_CFA_def_cfa_register:
685       return "DW_CFA_def_cfa_register";
686     case DW_CFA_def_cfa_offset:
687       return "DW_CFA_def_cfa_offset";
688
689     /* DWARF 3 */
690     case DW_CFA_def_cfa_expression:
691       return "DW_CFA_def_cfa_expression";
692     case DW_CFA_expression:
693       return "DW_CFA_expression";
694     case DW_CFA_offset_extended_sf:
695       return "DW_CFA_offset_extended_sf";
696     case DW_CFA_def_cfa_sf:
697       return "DW_CFA_def_cfa_sf";
698     case DW_CFA_def_cfa_offset_sf:
699       return "DW_CFA_def_cfa_offset_sf";
700
701     /* SGI/MIPS specific */
702     case DW_CFA_MIPS_advance_loc8:
703       return "DW_CFA_MIPS_advance_loc8";
704
705     /* GNU extensions */
706     case DW_CFA_GNU_window_save:
707       return "DW_CFA_GNU_window_save";
708     case DW_CFA_GNU_args_size:
709       return "DW_CFA_GNU_args_size";
710     case DW_CFA_GNU_negative_offset_extended:
711       return "DW_CFA_GNU_negative_offset_extended";
712
713     default:
714       return "DW_CFA_<unknown>";
715     }
716 }
717
718 /* Return a pointer to a newly allocated Call Frame Instruction.  */
719
720 static inline dw_cfi_ref
721 new_cfi (void)
722 {
723   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
724
725   cfi->dw_cfi_next = NULL;
726   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
727   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
728
729   return cfi;
730 }
731
732 /* Add a Call Frame Instruction to list of instructions.  */
733
734 static inline void
735 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
736 {
737   dw_cfi_ref *p;
738   dw_fde_ref fde = current_fde ();
739
740   /* When DRAP is used, CFA is defined with an expression.  Redefine
741      CFA may lead to a different CFA value.   */
742   /* ??? Of course, this heuristic fails when we're annotating epilogues,
743      because of course we'll always want to redefine the CFA back to the
744      stack pointer on the way out.  Where should we move this check?  */
745   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
746     switch (cfi->dw_cfi_opc)
747       {
748         case DW_CFA_def_cfa_register:
749         case DW_CFA_def_cfa_offset:
750         case DW_CFA_def_cfa_offset_sf:
751         case DW_CFA_def_cfa:
752         case DW_CFA_def_cfa_sf:
753           gcc_unreachable ();
754
755         default:
756           break;
757       }
758
759   /* Find the end of the chain.  */
760   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
761     ;
762
763   *p = cfi;
764 }
765
766 /* Generate a new label for the CFI info to refer to.  FORCE is true
767    if a label needs to be output even when using .cfi_* directives.  */
768
769 char *
770 dwarf2out_cfi_label (bool force)
771 {
772   static char label[20];
773
774   if (!force && dwarf2out_do_cfi_asm ())
775     {
776       /* In this case, we will be emitting the asm directive instead of
777          the label, so just return a placeholder to keep the rest of the
778          interfaces happy.  */
779       strcpy (label, "<do not output>");
780     }
781   else
782     {
783       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
784       ASM_OUTPUT_LABEL (asm_out_file, label);
785     }
786
787   return label;
788 }
789
790 /* True if remember_state should be emitted before following CFI directive.  */
791 static bool emit_cfa_remember;
792
793 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
794    or to the CIE if LABEL is NULL.  */
795
796 static void
797 add_fde_cfi (const char *label, dw_cfi_ref cfi)
798 {
799   dw_cfi_ref *list_head;
800
801   if (emit_cfa_remember)
802     {
803       dw_cfi_ref cfi_remember;
804
805       /* Emit the state save.  */
806       emit_cfa_remember = false;
807       cfi_remember = new_cfi (); 
808       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
809       add_fde_cfi (label, cfi_remember);
810     }
811
812   list_head = &cie_cfi_head;
813
814   if (dwarf2out_do_cfi_asm ())
815     {
816       if (label)
817         {
818           dw_fde_ref fde = current_fde ();
819
820           gcc_assert (fde != NULL);
821
822           /* We still have to add the cfi to the list so that lookup_cfa
823              works later on.  When -g2 and above we even need to force
824              emitting of CFI labels and add to list a DW_CFA_set_loc for
825              convert_cfa_to_fb_loc_list purposes.  If we're generating
826              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
827              convert_cfa_to_fb_loc_list.  */
828           if (dwarf_version == 2
829               && debug_info_level > DINFO_LEVEL_TERSE
830               && (write_symbols == DWARF2_DEBUG
831                   || write_symbols == VMS_AND_DWARF2_DEBUG))
832             {
833               switch (cfi->dw_cfi_opc)
834                 {
835                 case DW_CFA_def_cfa_offset:
836                 case DW_CFA_def_cfa_offset_sf:
837                 case DW_CFA_def_cfa_register:
838                 case DW_CFA_def_cfa:
839                 case DW_CFA_def_cfa_sf:
840                 case DW_CFA_def_cfa_expression:
841                 case DW_CFA_restore_state:
842                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
843                     label = dwarf2out_cfi_label (true);
844
845                   if (fde->dw_fde_current_label == NULL
846                       || strcmp (label, fde->dw_fde_current_label) != 0)
847                     {
848                       dw_cfi_ref xcfi;
849
850                       label = xstrdup (label);
851
852                       /* Set the location counter to the new label.  */
853                       xcfi = new_cfi ();
854                       /* It doesn't metter whether DW_CFA_set_loc
855                          or DW_CFA_advance_loc4 is added here, those aren't
856                          emitted into assembly, only looked up by
857                          convert_cfa_to_fb_loc_list.  */
858                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
859                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
860                       add_cfi (&fde->dw_fde_cfi, xcfi);
861                       fde->dw_fde_current_label = label;
862                     }
863                   break;
864                 default:
865                   break;
866                 }
867             }
868
869           output_cfi_directive (cfi);
870
871           list_head = &fde->dw_fde_cfi;
872         }
873       /* ??? If this is a CFI for the CIE, we don't emit.  This
874          assumes that the standard CIE contents that the assembler
875          uses matches the standard CIE contents that the compiler
876          uses.  This is probably a bad assumption.  I'm not quite
877          sure how to address this for now.  */
878     }
879   else if (label)
880     {
881       dw_fde_ref fde = current_fde ();
882
883       gcc_assert (fde != NULL);
884
885       if (*label == 0)
886         label = dwarf2out_cfi_label (false);
887
888       if (fde->dw_fde_current_label == NULL
889           || strcmp (label, fde->dw_fde_current_label) != 0)
890         {
891           dw_cfi_ref xcfi;
892
893           label = xstrdup (label);
894
895           /* Set the location counter to the new label.  */
896           xcfi = new_cfi ();
897           /* If we have a current label, advance from there, otherwise
898              set the location directly using set_loc.  */
899           xcfi->dw_cfi_opc = fde->dw_fde_current_label
900                              ? DW_CFA_advance_loc4
901                              : DW_CFA_set_loc;
902           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
903           add_cfi (&fde->dw_fde_cfi, xcfi);
904
905           fde->dw_fde_current_label = label;
906         }
907
908       list_head = &fde->dw_fde_cfi;
909     }
910
911   add_cfi (list_head, cfi);
912 }
913
914 /* Subroutine of lookup_cfa.  */
915
916 static void
917 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
918 {
919   switch (cfi->dw_cfi_opc)
920     {
921     case DW_CFA_def_cfa_offset:
922     case DW_CFA_def_cfa_offset_sf:
923       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
924       break;
925     case DW_CFA_def_cfa_register:
926       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
927       break;
928     case DW_CFA_def_cfa:
929     case DW_CFA_def_cfa_sf:
930       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
931       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
932       break;
933     case DW_CFA_def_cfa_expression:
934       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
935       break;
936
937     case DW_CFA_remember_state:
938       gcc_assert (!remember->in_use);
939       *remember = *loc;
940       remember->in_use = 1;
941       break;
942     case DW_CFA_restore_state:
943       gcc_assert (remember->in_use);
944       *loc = *remember;
945       remember->in_use = 0;
946       break;
947
948     default:
949       break;
950     }
951 }
952
953 /* Find the previous value for the CFA.  */
954
955 static void
956 lookup_cfa (dw_cfa_location *loc)
957 {
958   dw_cfi_ref cfi;
959   dw_fde_ref fde;
960   dw_cfa_location remember;
961
962   memset (loc, 0, sizeof (*loc));
963   loc->reg = INVALID_REGNUM;
964   remember = *loc;
965
966   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
967     lookup_cfa_1 (cfi, loc, &remember);
968
969   fde = current_fde ();
970   if (fde)
971     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
972       lookup_cfa_1 (cfi, loc, &remember);
973 }
974
975 /* The current rule for calculating the DWARF2 canonical frame address.  */
976 static dw_cfa_location cfa;
977
978 /* The register used for saving registers to the stack, and its offset
979    from the CFA.  */
980 static dw_cfa_location cfa_store;
981
982 /* The current save location around an epilogue.  */
983 static dw_cfa_location cfa_remember;
984
985 /* The running total of the size of arguments pushed onto the stack.  */
986 static HOST_WIDE_INT args_size;
987
988 /* The last args_size we actually output.  */
989 static HOST_WIDE_INT old_args_size;
990
991 /* Entry point to update the canonical frame address (CFA).
992    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
993    calculated from REG+OFFSET.  */
994
995 void
996 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
997 {
998   dw_cfa_location loc;
999   loc.indirect = 0;
1000   loc.base_offset = 0;
1001   loc.reg = reg;
1002   loc.offset = offset;
1003   def_cfa_1 (label, &loc);
1004 }
1005
1006 /* Determine if two dw_cfa_location structures define the same data.  */
1007
1008 static bool
1009 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1010 {
1011   return (loc1->reg == loc2->reg
1012           && loc1->offset == loc2->offset
1013           && loc1->indirect == loc2->indirect
1014           && (loc1->indirect == 0
1015               || loc1->base_offset == loc2->base_offset));
1016 }
1017
1018 /* This routine does the actual work.  The CFA is now calculated from
1019    the dw_cfa_location structure.  */
1020
1021 static void
1022 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1023 {
1024   dw_cfi_ref cfi;
1025   dw_cfa_location old_cfa, loc;
1026
1027   cfa = *loc_p;
1028   loc = *loc_p;
1029
1030   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1031     cfa_store.offset = loc.offset;
1032
1033   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1034   lookup_cfa (&old_cfa);
1035
1036   /* If nothing changed, no need to issue any call frame instructions.  */
1037   if (cfa_equal_p (&loc, &old_cfa))
1038     return;
1039
1040   cfi = new_cfi ();
1041
1042   if (loc.reg == old_cfa.reg && !loc.indirect)
1043     {
1044       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1045          the CFA register did not change but the offset did.  The data 
1046          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1047          in the assembler via the .cfi_def_cfa_offset directive.  */
1048       if (loc.offset < 0)
1049         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1050       else
1051         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1052       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1053     }
1054
1055 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1056   else if (loc.offset == old_cfa.offset
1057            && old_cfa.reg != INVALID_REGNUM
1058            && !loc.indirect)
1059     {
1060       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1061          indicating the CFA register has changed to <register> but the
1062          offset has not changed.  */
1063       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1064       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1065     }
1066 #endif
1067
1068   else if (loc.indirect == 0)
1069     {
1070       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1071          indicating the CFA register has changed to <register> with
1072          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1073          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1074          directive.  */
1075       if (loc.offset < 0)
1076         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1077       else
1078         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1079       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1080       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1081     }
1082   else
1083     {
1084       /* Construct a DW_CFA_def_cfa_expression instruction to
1085          calculate the CFA using a full location expression since no
1086          register-offset pair is available.  */
1087       struct dw_loc_descr_struct *loc_list;
1088
1089       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1090       loc_list = build_cfa_loc (&loc, 0);
1091       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1092     }
1093
1094   add_fde_cfi (label, cfi);
1095 }
1096
1097 /* Add the CFI for saving a register.  REG is the CFA column number.
1098    LABEL is passed to add_fde_cfi.
1099    If SREG is -1, the register is saved at OFFSET from the CFA;
1100    otherwise it is saved in SREG.  */
1101
1102 static void
1103 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1104 {
1105   dw_cfi_ref cfi = new_cfi ();
1106   dw_fde_ref fde = current_fde ();
1107
1108   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1109
1110   /* When stack is aligned, store REG using DW_CFA_expression with
1111      FP.  */
1112   if (fde
1113       && fde->stack_realign
1114       && sreg == INVALID_REGNUM)
1115     {
1116       cfi->dw_cfi_opc = DW_CFA_expression;
1117       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1118       cfi->dw_cfi_oprnd1.dw_cfi_loc
1119         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1120     }
1121   else if (sreg == INVALID_REGNUM)
1122     {
1123       if (need_data_align_sf_opcode (offset))
1124         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1125       else if (reg & ~0x3f)
1126         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1127       else
1128         cfi->dw_cfi_opc = DW_CFA_offset;
1129       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1130     }
1131   else if (sreg == reg)
1132     cfi->dw_cfi_opc = DW_CFA_same_value;
1133   else
1134     {
1135       cfi->dw_cfi_opc = DW_CFA_register;
1136       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1137     }
1138
1139   add_fde_cfi (label, cfi);
1140 }
1141
1142 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1143    This CFI tells the unwinder that it needs to restore the window registers
1144    from the previous frame's window save area.
1145
1146    ??? Perhaps we should note in the CIE where windows are saved (instead of
1147    assuming 0(cfa)) and what registers are in the window.  */
1148
1149 void
1150 dwarf2out_window_save (const char *label)
1151 {
1152   dw_cfi_ref cfi = new_cfi ();
1153
1154   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1155   add_fde_cfi (label, cfi);
1156 }
1157
1158 /* Entry point for saving a register to the stack.  REG is the GCC register
1159    number.  LABEL and OFFSET are passed to reg_save.  */
1160
1161 void
1162 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1163 {
1164   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1165 }
1166
1167 /* Entry point for saving the return address in the stack.
1168    LABEL and OFFSET are passed to reg_save.  */
1169
1170 void
1171 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1172 {
1173   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1174 }
1175
1176 /* Entry point for saving the return address in a register.
1177    LABEL and SREG are passed to reg_save.  */
1178
1179 void
1180 dwarf2out_return_reg (const char *label, unsigned int sreg)
1181 {
1182   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1183 }
1184
1185 #ifdef DWARF2_UNWIND_INFO
1186 /* Record the initial position of the return address.  RTL is
1187    INCOMING_RETURN_ADDR_RTX.  */
1188
1189 static void
1190 initial_return_save (rtx rtl)
1191 {
1192   unsigned int reg = INVALID_REGNUM;
1193   HOST_WIDE_INT offset = 0;
1194
1195   switch (GET_CODE (rtl))
1196     {
1197     case REG:
1198       /* RA is in a register.  */
1199       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1200       break;
1201
1202     case MEM:
1203       /* RA is on the stack.  */
1204       rtl = XEXP (rtl, 0);
1205       switch (GET_CODE (rtl))
1206         {
1207         case REG:
1208           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1209           offset = 0;
1210           break;
1211
1212         case PLUS:
1213           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1214           offset = INTVAL (XEXP (rtl, 1));
1215           break;
1216
1217         case MINUS:
1218           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1219           offset = -INTVAL (XEXP (rtl, 1));
1220           break;
1221
1222         default:
1223           gcc_unreachable ();
1224         }
1225
1226       break;
1227
1228     case PLUS:
1229       /* The return address is at some offset from any value we can
1230          actually load.  For instance, on the SPARC it is in %i7+8. Just
1231          ignore the offset for now; it doesn't matter for unwinding frames.  */
1232       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1233       initial_return_save (XEXP (rtl, 0));
1234       return;
1235
1236     default:
1237       gcc_unreachable ();
1238     }
1239
1240   if (reg != DWARF_FRAME_RETURN_COLUMN)
1241     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1242 }
1243 #endif
1244
1245 /* Given a SET, calculate the amount of stack adjustment it
1246    contains.  */
1247
1248 static HOST_WIDE_INT
1249 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1250                      HOST_WIDE_INT cur_offset)
1251 {
1252   const_rtx src = SET_SRC (pattern);
1253   const_rtx dest = SET_DEST (pattern);
1254   HOST_WIDE_INT offset = 0;
1255   enum rtx_code code;
1256
1257   if (dest == stack_pointer_rtx)
1258     {
1259       code = GET_CODE (src);
1260
1261       /* Assume (set (reg sp) (reg whatever)) sets args_size
1262          level to 0.  */
1263       if (code == REG && src != stack_pointer_rtx)
1264         {
1265           offset = -cur_args_size;
1266 #ifndef STACK_GROWS_DOWNWARD
1267           offset = -offset;
1268 #endif
1269           return offset - cur_offset;
1270         }
1271
1272       if (! (code == PLUS || code == MINUS)
1273           || XEXP (src, 0) != stack_pointer_rtx
1274           || !CONST_INT_P (XEXP (src, 1)))
1275         return 0;
1276
1277       /* (set (reg sp) (plus (reg sp) (const_int))) */
1278       offset = INTVAL (XEXP (src, 1));
1279       if (code == PLUS)
1280         offset = -offset;
1281       return offset;
1282     }
1283
1284   if (MEM_P (src) && !MEM_P (dest))
1285     dest = src;
1286   if (MEM_P (dest))
1287     {
1288       /* (set (mem (pre_dec (reg sp))) (foo)) */
1289       src = XEXP (dest, 0);
1290       code = GET_CODE (src);
1291
1292       switch (code)
1293         {
1294         case PRE_MODIFY:
1295         case POST_MODIFY:
1296           if (XEXP (src, 0) == stack_pointer_rtx)
1297             {
1298               rtx val = XEXP (XEXP (src, 1), 1);
1299               /* We handle only adjustments by constant amount.  */
1300               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1301                           && CONST_INT_P (val));
1302               offset = -INTVAL (val);
1303               break;
1304             }
1305           return 0;
1306
1307         case PRE_DEC:
1308         case POST_DEC:
1309           if (XEXP (src, 0) == stack_pointer_rtx)
1310             {
1311               offset = GET_MODE_SIZE (GET_MODE (dest));
1312               break;
1313             }
1314           return 0;
1315
1316         case PRE_INC:
1317         case POST_INC:
1318           if (XEXP (src, 0) == stack_pointer_rtx)
1319             {
1320               offset = -GET_MODE_SIZE (GET_MODE (dest));
1321               break;
1322             }
1323           return 0;
1324
1325         default:
1326           return 0;
1327         }
1328     }
1329   else
1330     return 0;
1331
1332   return offset;
1333 }
1334
1335 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1336    indexed by INSN_UID.  */
1337
1338 static HOST_WIDE_INT *barrier_args_size;
1339
1340 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1341
1342 static HOST_WIDE_INT
1343 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1344                              VEC (rtx, heap) **next)
1345 {
1346   HOST_WIDE_INT offset = 0;
1347   int i;
1348
1349   if (! RTX_FRAME_RELATED_P (insn))
1350     {
1351       if (prologue_epilogue_contains (insn))
1352         /* Nothing */;
1353       else if (GET_CODE (PATTERN (insn)) == SET)
1354         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1355       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1356                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1357         {
1358           /* There may be stack adjustments inside compound insns.  Search
1359              for them.  */
1360           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1361             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1362               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1363                                              cur_args_size, offset);
1364         }
1365     }
1366   else
1367     {
1368       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1369
1370       if (expr)
1371         {
1372           expr = XEXP (expr, 0);
1373           if (GET_CODE (expr) == PARALLEL
1374               || GET_CODE (expr) == SEQUENCE)
1375             for (i = 1; i < XVECLEN (expr, 0); i++)
1376               {
1377                 rtx elem = XVECEXP (expr, 0, i);
1378
1379                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1380                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1381               }
1382         }
1383     }
1384
1385 #ifndef STACK_GROWS_DOWNWARD
1386   offset = -offset;
1387 #endif
1388
1389   cur_args_size += offset;
1390   if (cur_args_size < 0)
1391     cur_args_size = 0;
1392
1393   if (JUMP_P (insn))
1394     {
1395       rtx dest = JUMP_LABEL (insn);
1396
1397       if (dest)
1398         {
1399           if (barrier_args_size [INSN_UID (dest)] < 0)
1400             {
1401               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1402               VEC_safe_push (rtx, heap, *next, dest);
1403             }
1404         }
1405     }
1406
1407   return cur_args_size;
1408 }
1409
1410 /* Walk the whole function and compute args_size on BARRIERs.  */
1411
1412 static void
1413 compute_barrier_args_size (void)
1414 {
1415   int max_uid = get_max_uid (), i;
1416   rtx insn;
1417   VEC (rtx, heap) *worklist, *next, *tmp;
1418
1419   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1420   for (i = 0; i < max_uid; i++)
1421     barrier_args_size[i] = -1;
1422
1423   worklist = VEC_alloc (rtx, heap, 20);
1424   next = VEC_alloc (rtx, heap, 20);
1425   insn = get_insns ();
1426   barrier_args_size[INSN_UID (insn)] = 0;
1427   VEC_quick_push (rtx, worklist, insn);
1428   for (;;)
1429     {
1430       while (!VEC_empty (rtx, worklist))
1431         {
1432           rtx prev, body, first_insn;
1433           HOST_WIDE_INT cur_args_size;
1434
1435           first_insn = insn = VEC_pop (rtx, worklist);
1436           cur_args_size = barrier_args_size[INSN_UID (insn)];
1437           prev = prev_nonnote_insn (insn);
1438           if (prev && BARRIER_P (prev))
1439             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1440
1441           for (; insn; insn = NEXT_INSN (insn))
1442             {
1443               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1444                 continue;
1445               if (BARRIER_P (insn))
1446                 break;
1447
1448               if (LABEL_P (insn))
1449                 {
1450                   if (insn == first_insn)
1451                     continue;
1452                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1453                     {
1454                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1455                       continue;
1456                     }
1457                   else
1458                     {
1459                       /* The insns starting with this label have been
1460                          already scanned or are in the worklist.  */
1461                       break;
1462                     }
1463                 }
1464
1465               body = PATTERN (insn);
1466               if (GET_CODE (body) == SEQUENCE)
1467                 {
1468                   HOST_WIDE_INT dest_args_size = cur_args_size;
1469                   for (i = 1; i < XVECLEN (body, 0); i++)
1470                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1471                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1472                       dest_args_size
1473                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1474                                                        dest_args_size, &next);
1475                     else
1476                       cur_args_size
1477                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1478                                                        cur_args_size, &next);
1479
1480                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1481                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1482                                                  dest_args_size, &next);
1483                   else
1484                     cur_args_size
1485                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1486                                                      cur_args_size, &next);
1487                 }
1488               else
1489                 cur_args_size
1490                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1491             }
1492         }
1493
1494       if (VEC_empty (rtx, next))
1495         break;
1496
1497       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1498       tmp = next;
1499       next = worklist;
1500       worklist = tmp;
1501       VEC_truncate (rtx, next, 0);
1502     }
1503
1504   VEC_free (rtx, heap, worklist);
1505   VEC_free (rtx, heap, next);
1506 }
1507
1508 /* Add a CFI to update the running total of the size of arguments
1509    pushed onto the stack.  */
1510
1511 static void
1512 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1513 {
1514   dw_cfi_ref cfi;
1515
1516   if (size == old_args_size)
1517     return;
1518
1519   old_args_size = size;
1520
1521   cfi = new_cfi ();
1522   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1523   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1524   add_fde_cfi (label, cfi);
1525 }
1526
1527 /* Adjust args_size based on stack adjustment OFFSET.  */
1528
1529 static void
1530 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1531 {
1532   if (cfa.reg == STACK_POINTER_REGNUM)
1533     cfa.offset += offset;
1534
1535   if (cfa_store.reg == STACK_POINTER_REGNUM)
1536     cfa_store.offset += offset;
1537
1538 #ifndef STACK_GROWS_DOWNWARD
1539   offset = -offset;
1540 #endif
1541
1542   args_size += offset;
1543   if (args_size < 0)
1544     args_size = 0;
1545
1546   def_cfa_1 (label, &cfa);
1547   if (flag_asynchronous_unwind_tables)
1548     dwarf2out_args_size (label, args_size);
1549 }
1550
1551 /* Check INSN to see if it looks like a push or a stack adjustment, and
1552    make a note of it if it does.  EH uses this information to find out how
1553    much extra space it needs to pop off the stack.  */
1554
1555 static void
1556 dwarf2out_stack_adjust (rtx insn, bool after_p)
1557 {
1558   HOST_WIDE_INT offset;
1559   const char *label;
1560   int i;
1561
1562   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1563      with this function.  Proper support would require all frame-related
1564      insns to be marked, and to be able to handle saving state around
1565      epilogues textually in the middle of the function.  */
1566   if (prologue_epilogue_contains (insn))
1567     return;
1568
1569   /* If INSN is an instruction from target of an annulled branch, the
1570      effects are for the target only and so current argument size
1571      shouldn't change at all.  */
1572   if (final_sequence
1573       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1574       && INSN_FROM_TARGET_P (insn))
1575     return;
1576
1577   /* If only calls can throw, and we have a frame pointer,
1578      save up adjustments until we see the CALL_INSN.  */
1579   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1580     {
1581       if (CALL_P (insn) && !after_p)
1582         {
1583           /* Extract the size of the args from the CALL rtx itself.  */
1584           insn = PATTERN (insn);
1585           if (GET_CODE (insn) == PARALLEL)
1586             insn = XVECEXP (insn, 0, 0);
1587           if (GET_CODE (insn) == SET)
1588             insn = SET_SRC (insn);
1589           gcc_assert (GET_CODE (insn) == CALL);
1590           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1591         }
1592       return;
1593     }
1594
1595   if (CALL_P (insn) && !after_p)
1596     {
1597       if (!flag_asynchronous_unwind_tables)
1598         dwarf2out_args_size ("", args_size);
1599       return;
1600     }
1601   else if (BARRIER_P (insn))
1602     {
1603       /* Don't call compute_barrier_args_size () if the only
1604          BARRIER is at the end of function.  */
1605       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1606         compute_barrier_args_size ();
1607       if (barrier_args_size == NULL)
1608         offset = 0;
1609       else
1610         {
1611           offset = barrier_args_size[INSN_UID (insn)];
1612           if (offset < 0)
1613             offset = 0;
1614         }
1615
1616       offset -= args_size;
1617 #ifndef STACK_GROWS_DOWNWARD
1618       offset = -offset;
1619 #endif
1620     }
1621   else if (GET_CODE (PATTERN (insn)) == SET)
1622     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1623   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1624            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1625     {
1626       /* There may be stack adjustments inside compound insns.  Search
1627          for them.  */
1628       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1629         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1630           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1631                                          args_size, offset);
1632     }
1633   else
1634     return;
1635
1636   if (offset == 0)
1637     return;
1638
1639   label = dwarf2out_cfi_label (false);
1640   dwarf2out_args_size_adjust (offset, label);
1641 }
1642
1643 #endif
1644
1645 /* We delay emitting a register save until either (a) we reach the end
1646    of the prologue or (b) the register is clobbered.  This clusters
1647    register saves so that there are fewer pc advances.  */
1648
1649 struct GTY(()) queued_reg_save {
1650   struct queued_reg_save *next;
1651   rtx reg;
1652   HOST_WIDE_INT cfa_offset;
1653   rtx saved_reg;
1654 };
1655
1656 static GTY(()) struct queued_reg_save *queued_reg_saves;
1657
1658 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1659 struct GTY(()) reg_saved_in_data {
1660   rtx orig_reg;
1661   rtx saved_in_reg;
1662 };
1663
1664 /* A list of registers saved in other registers.
1665    The list intentionally has a small maximum capacity of 4; if your
1666    port needs more than that, you might consider implementing a
1667    more efficient data structure.  */
1668 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1669 static GTY(()) size_t num_regs_saved_in_regs;
1670
1671 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1672 static const char *last_reg_save_label;
1673
1674 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1675    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1676
1677 static void
1678 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1679 {
1680   struct queued_reg_save *q;
1681
1682   /* Duplicates waste space, but it's also necessary to remove them
1683      for correctness, since the queue gets output in reverse
1684      order.  */
1685   for (q = queued_reg_saves; q != NULL; q = q->next)
1686     if (REGNO (q->reg) == REGNO (reg))
1687       break;
1688
1689   if (q == NULL)
1690     {
1691       q = GGC_NEW (struct queued_reg_save);
1692       q->next = queued_reg_saves;
1693       queued_reg_saves = q;
1694     }
1695
1696   q->reg = reg;
1697   q->cfa_offset = offset;
1698   q->saved_reg = sreg;
1699
1700   last_reg_save_label = label;
1701 }
1702
1703 /* Output all the entries in QUEUED_REG_SAVES.  */
1704
1705 static void
1706 flush_queued_reg_saves (void)
1707 {
1708   struct queued_reg_save *q;
1709
1710   for (q = queued_reg_saves; q; q = q->next)
1711     {
1712       size_t i;
1713       unsigned int reg, sreg;
1714
1715       for (i = 0; i < num_regs_saved_in_regs; i++)
1716         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1717           break;
1718       if (q->saved_reg && i == num_regs_saved_in_regs)
1719         {
1720           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1721           num_regs_saved_in_regs++;
1722         }
1723       if (i != num_regs_saved_in_regs)
1724         {
1725           regs_saved_in_regs[i].orig_reg = q->reg;
1726           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1727         }
1728
1729       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1730       if (q->saved_reg)
1731         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1732       else
1733         sreg = INVALID_REGNUM;
1734       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1735     }
1736
1737   queued_reg_saves = NULL;
1738   last_reg_save_label = NULL;
1739 }
1740
1741 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1742    location for?  Or, does it clobber a register which we've previously
1743    said that some other register is saved in, and for which we now
1744    have a new location for?  */
1745
1746 static bool
1747 clobbers_queued_reg_save (const_rtx insn)
1748 {
1749   struct queued_reg_save *q;
1750
1751   for (q = queued_reg_saves; q; q = q->next)
1752     {
1753       size_t i;
1754       if (modified_in_p (q->reg, insn))
1755         return true;
1756       for (i = 0; i < num_regs_saved_in_regs; i++)
1757         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1758             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1759           return true;
1760     }
1761
1762   return false;
1763 }
1764
1765 /* Entry point for saving the first register into the second.  */
1766
1767 void
1768 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1769 {
1770   size_t i;
1771   unsigned int regno, sregno;
1772
1773   for (i = 0; i < num_regs_saved_in_regs; i++)
1774     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1775       break;
1776   if (i == num_regs_saved_in_regs)
1777     {
1778       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1779       num_regs_saved_in_regs++;
1780     }
1781   regs_saved_in_regs[i].orig_reg = reg;
1782   regs_saved_in_regs[i].saved_in_reg = sreg;
1783
1784   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1785   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1786   reg_save (label, regno, sregno, 0);
1787 }
1788
1789 /* What register, if any, is currently saved in REG?  */
1790
1791 static rtx
1792 reg_saved_in (rtx reg)
1793 {
1794   unsigned int regn = REGNO (reg);
1795   size_t i;
1796   struct queued_reg_save *q;
1797
1798   for (q = queued_reg_saves; q; q = q->next)
1799     if (q->saved_reg && regn == REGNO (q->saved_reg))
1800       return q->reg;
1801
1802   for (i = 0; i < num_regs_saved_in_regs; i++)
1803     if (regs_saved_in_regs[i].saved_in_reg
1804         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1805       return regs_saved_in_regs[i].orig_reg;
1806
1807   return NULL_RTX;
1808 }
1809
1810
1811 /* A temporary register holding an integral value used in adjusting SP
1812    or setting up the store_reg.  The "offset" field holds the integer
1813    value, not an offset.  */
1814 static dw_cfa_location cfa_temp;
1815
1816 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1817
1818 static void
1819 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1820 {
1821   memset (&cfa, 0, sizeof (cfa));
1822
1823   switch (GET_CODE (pat))
1824     {
1825     case PLUS:
1826       cfa.reg = REGNO (XEXP (pat, 0));
1827       cfa.offset = INTVAL (XEXP (pat, 1));
1828       break;
1829
1830     case REG:
1831       cfa.reg = REGNO (pat);
1832       break;
1833
1834     default:
1835       /* Recurse and define an expression.  */
1836       gcc_unreachable ();
1837     }
1838
1839   def_cfa_1 (label, &cfa);
1840 }
1841
1842 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1843
1844 static void
1845 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1846 {
1847   rtx src, dest;
1848
1849   gcc_assert (GET_CODE (pat) == SET);
1850   dest = XEXP (pat, 0);
1851   src = XEXP (pat, 1);
1852
1853   switch (GET_CODE (src))
1854     {
1855     case PLUS:
1856       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1857       cfa.offset -= INTVAL (XEXP (src, 1));
1858       break;
1859
1860     case REG:
1861         break;
1862
1863     default:
1864         gcc_unreachable ();
1865     }
1866
1867   cfa.reg = REGNO (dest);
1868   gcc_assert (cfa.indirect == 0);
1869
1870   def_cfa_1 (label, &cfa);
1871 }
1872
1873 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1874
1875 static void
1876 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1877 {
1878   HOST_WIDE_INT offset;
1879   rtx src, addr, span;
1880
1881   src = XEXP (set, 1);
1882   addr = XEXP (set, 0);
1883   gcc_assert (MEM_P (addr));
1884   addr = XEXP (addr, 0);
1885   
1886   /* As documented, only consider extremely simple addresses.  */
1887   switch (GET_CODE (addr))
1888     {
1889     case REG:
1890       gcc_assert (REGNO (addr) == cfa.reg);
1891       offset = -cfa.offset;
1892       break;
1893     case PLUS:
1894       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1895       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1896       break;
1897     default:
1898       gcc_unreachable ();
1899     }
1900
1901   span = targetm.dwarf_register_span (src);
1902
1903   /* ??? We'd like to use queue_reg_save, but we need to come up with
1904      a different flushing heuristic for epilogues.  */
1905   if (!span)
1906     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1907   else
1908     {
1909       /* We have a PARALLEL describing where the contents of SRC live.
1910          Queue register saves for each piece of the PARALLEL.  */
1911       int par_index;
1912       int limit;
1913       HOST_WIDE_INT span_offset = offset;
1914
1915       gcc_assert (GET_CODE (span) == PARALLEL);
1916
1917       limit = XVECLEN (span, 0);
1918       for (par_index = 0; par_index < limit; par_index++)
1919         {
1920           rtx elem = XVECEXP (span, 0, par_index);
1921
1922           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1923                     INVALID_REGNUM, span_offset);
1924           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1925         }
1926     }
1927 }
1928
1929 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1930
1931 static void
1932 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1933 {
1934   rtx src, dest;
1935   unsigned sregno, dregno;
1936
1937   src = XEXP (set, 1);
1938   dest = XEXP (set, 0);
1939
1940   if (src == pc_rtx)
1941     sregno = DWARF_FRAME_RETURN_COLUMN;
1942   else
1943     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1944
1945   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1946
1947   /* ??? We'd like to use queue_reg_save, but we need to come up with
1948      a different flushing heuristic for epilogues.  */
1949   reg_save (label, sregno, dregno, 0);
1950 }
1951
1952 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1953
1954 static void
1955 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1956 {
1957   dw_cfi_ref cfi = new_cfi ();
1958   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1959
1960   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1961   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1962
1963   add_fde_cfi (label, cfi);
1964 }
1965
1966 /* Record call frame debugging information for an expression EXPR,
1967    which either sets SP or FP (adjusting how we calculate the frame
1968    address) or saves a register to the stack or another register.
1969    LABEL indicates the address of EXPR.
1970
1971    This function encodes a state machine mapping rtxes to actions on
1972    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1973    users need not read the source code.
1974
1975   The High-Level Picture
1976
1977   Changes in the register we use to calculate the CFA: Currently we
1978   assume that if you copy the CFA register into another register, we
1979   should take the other one as the new CFA register; this seems to
1980   work pretty well.  If it's wrong for some target, it's simple
1981   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1982
1983   Changes in the register we use for saving registers to the stack:
1984   This is usually SP, but not always.  Again, we deduce that if you
1985   copy SP into another register (and SP is not the CFA register),
1986   then the new register is the one we will be using for register
1987   saves.  This also seems to work.
1988
1989   Register saves: There's not much guesswork about this one; if
1990   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1991   register save, and the register used to calculate the destination
1992   had better be the one we think we're using for this purpose.
1993   It's also assumed that a copy from a call-saved register to another
1994   register is saving that register if RTX_FRAME_RELATED_P is set on
1995   that instruction.  If the copy is from a call-saved register to
1996   the *same* register, that means that the register is now the same
1997   value as in the caller.
1998
1999   Except: If the register being saved is the CFA register, and the
2000   offset is nonzero, we are saving the CFA, so we assume we have to
2001   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2002   the intent is to save the value of SP from the previous frame.
2003
2004   In addition, if a register has previously been saved to a different
2005   register,
2006
2007   Invariants / Summaries of Rules
2008
2009   cfa          current rule for calculating the CFA.  It usually
2010                consists of a register and an offset.
2011   cfa_store    register used by prologue code to save things to the stack
2012                cfa_store.offset is the offset from the value of
2013                cfa_store.reg to the actual CFA
2014   cfa_temp     register holding an integral value.  cfa_temp.offset
2015                stores the value, which will be used to adjust the
2016                stack pointer.  cfa_temp is also used like cfa_store,
2017                to track stores to the stack via fp or a temp reg.
2018
2019   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2020                with cfa.reg as the first operand changes the cfa.reg and its
2021                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2022                cfa_temp.offset.
2023
2024   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2025                expression yielding a constant.  This sets cfa_temp.reg
2026                and cfa_temp.offset.
2027
2028   Rule 5:      Create a new register cfa_store used to save items to the
2029                stack.
2030
2031   Rules 10-14: Save a register to the stack.  Define offset as the
2032                difference of the original location and cfa_store's
2033                location (or cfa_temp's location if cfa_temp is used).
2034
2035   Rules 16-20: If AND operation happens on sp in prologue, we assume
2036                stack is realigned.  We will use a group of DW_OP_XXX
2037                expressions to represent the location of the stored
2038                register instead of CFA+offset.
2039
2040   The Rules
2041
2042   "{a,b}" indicates a choice of a xor b.
2043   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2044
2045   Rule 1:
2046   (set <reg1> <reg2>:cfa.reg)
2047   effects: cfa.reg = <reg1>
2048            cfa.offset unchanged
2049            cfa_temp.reg = <reg1>
2050            cfa_temp.offset = cfa.offset
2051
2052   Rule 2:
2053   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2054                               {<const_int>,<reg>:cfa_temp.reg}))
2055   effects: cfa.reg = sp if fp used
2056            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2057            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2058              if cfa_store.reg==sp
2059
2060   Rule 3:
2061   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2062   effects: cfa.reg = fp
2063            cfa_offset += +/- <const_int>
2064
2065   Rule 4:
2066   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2067   constraints: <reg1> != fp
2068                <reg1> != sp
2069   effects: cfa.reg = <reg1>
2070            cfa_temp.reg = <reg1>
2071            cfa_temp.offset = cfa.offset
2072
2073   Rule 5:
2074   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2075   constraints: <reg1> != fp
2076                <reg1> != sp
2077   effects: cfa_store.reg = <reg1>
2078            cfa_store.offset = cfa.offset - cfa_temp.offset
2079
2080   Rule 6:
2081   (set <reg> <const_int>)
2082   effects: cfa_temp.reg = <reg>
2083            cfa_temp.offset = <const_int>
2084
2085   Rule 7:
2086   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2087   effects: cfa_temp.reg = <reg1>
2088            cfa_temp.offset |= <const_int>
2089
2090   Rule 8:
2091   (set <reg> (high <exp>))
2092   effects: none
2093
2094   Rule 9:
2095   (set <reg> (lo_sum <exp> <const_int>))
2096   effects: cfa_temp.reg = <reg>
2097            cfa_temp.offset = <const_int>
2098
2099   Rule 10:
2100   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2101   effects: cfa_store.offset -= <const_int>
2102            cfa.offset = cfa_store.offset if cfa.reg == sp
2103            cfa.reg = sp
2104            cfa.base_offset = -cfa_store.offset
2105
2106   Rule 11:
2107   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2108   effects: cfa_store.offset += -/+ mode_size(mem)
2109            cfa.offset = cfa_store.offset if cfa.reg == sp
2110            cfa.reg = sp
2111            cfa.base_offset = -cfa_store.offset
2112
2113   Rule 12:
2114   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2115
2116        <reg2>)
2117   effects: cfa.reg = <reg1>
2118            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2119
2120   Rule 13:
2121   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2122   effects: cfa.reg = <reg1>
2123            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2124
2125   Rule 14:
2126   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2127   effects: cfa.reg = <reg1>
2128            cfa.base_offset = -cfa_temp.offset
2129            cfa_temp.offset -= mode_size(mem)
2130
2131   Rule 15:
2132   (set <reg> {unspec, unspec_volatile})
2133   effects: target-dependent
2134
2135   Rule 16:
2136   (set sp (and: sp <const_int>))
2137   constraints: cfa_store.reg == sp
2138   effects: current_fde.stack_realign = 1
2139            cfa_store.offset = 0
2140            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2141
2142   Rule 17:
2143   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2144   effects: cfa_store.offset += -/+ mode_size(mem)
2145
2146   Rule 18:
2147   (set (mem ({pre_inc, pre_dec} sp)) fp)
2148   constraints: fde->stack_realign == 1
2149   effects: cfa_store.offset = 0
2150            cfa.reg != HARD_FRAME_POINTER_REGNUM
2151
2152   Rule 19:
2153   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2154   constraints: fde->stack_realign == 1
2155                && cfa.offset == 0
2156                && cfa.indirect == 0
2157                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2158   effects: Use DW_CFA_def_cfa_expression to define cfa
2159            cfa.reg == fde->drap_reg
2160
2161   Rule 20:
2162   (set reg fde->drap_reg)
2163   constraints: fde->vdrap_reg == INVALID_REGNUM
2164   effects: fde->vdrap_reg = reg.
2165   (set mem fde->drap_reg)
2166   constraints: fde->drap_reg_saved == 1
2167   effects: none.  */
2168
2169 static void
2170 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2171 {
2172   rtx src, dest, span;
2173   HOST_WIDE_INT offset;
2174   dw_fde_ref fde;
2175
2176   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2177      the PARALLEL independently. The first element is always processed if
2178      it is a SET. This is for backward compatibility.   Other elements
2179      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2180      flag is set in them.  */
2181   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2182     {
2183       int par_index;
2184       int limit = XVECLEN (expr, 0);
2185       rtx elem;
2186
2187       /* PARALLELs have strict read-modify-write semantics, so we
2188          ought to evaluate every rvalue before changing any lvalue.
2189          It's cumbersome to do that in general, but there's an
2190          easy approximation that is enough for all current users:
2191          handle register saves before register assignments.  */
2192       if (GET_CODE (expr) == PARALLEL)
2193         for (par_index = 0; par_index < limit; par_index++)
2194           {
2195             elem = XVECEXP (expr, 0, par_index);
2196             if (GET_CODE (elem) == SET
2197                 && MEM_P (SET_DEST (elem))
2198                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2199               dwarf2out_frame_debug_expr (elem, label);
2200           }
2201
2202       for (par_index = 0; par_index < limit; par_index++)
2203         {
2204           elem = XVECEXP (expr, 0, par_index);
2205           if (GET_CODE (elem) == SET
2206               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2207               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2208             dwarf2out_frame_debug_expr (elem, label);
2209           else if (!ACCUMULATE_OUTGOING_ARGS
2210                    && GET_CODE (elem) == SET
2211                    && par_index != 0
2212                    && !RTX_FRAME_RELATED_P (elem))
2213             {
2214               /* Stack adjustment combining might combine some post-prologue
2215                  stack adjustment into a prologue stack adjustment.  */
2216               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2217
2218               if (offset != 0)
2219                 dwarf2out_args_size_adjust (offset, label);
2220             }
2221         }
2222       return;
2223     }
2224
2225   gcc_assert (GET_CODE (expr) == SET);
2226
2227   src = SET_SRC (expr);
2228   dest = SET_DEST (expr);
2229
2230   if (REG_P (src))
2231     {
2232       rtx rsi = reg_saved_in (src);
2233       if (rsi)
2234         src = rsi;
2235     }
2236
2237   fde = current_fde ();
2238
2239   if (REG_P (src)
2240       && fde
2241       && fde->drap_reg == REGNO (src)
2242       && (fde->drap_reg_saved
2243           || REG_P (dest)))
2244     {
2245       /* Rule 20 */
2246       /* If we are saving dynamic realign argument pointer to a
2247          register, the destination is virtual dynamic realign
2248          argument pointer.  It may be used to access argument.  */
2249       if (REG_P (dest))
2250         {
2251           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2252           fde->vdrap_reg = REGNO (dest);
2253         }
2254       return;
2255     }
2256
2257   switch (GET_CODE (dest))
2258     {
2259     case REG:
2260       switch (GET_CODE (src))
2261         {
2262           /* Setting FP from SP.  */
2263         case REG:
2264           if (cfa.reg == (unsigned) REGNO (src))
2265             {
2266               /* Rule 1 */
2267               /* Update the CFA rule wrt SP or FP.  Make sure src is
2268                  relative to the current CFA register.
2269
2270                  We used to require that dest be either SP or FP, but the
2271                  ARM copies SP to a temporary register, and from there to
2272                  FP.  So we just rely on the backends to only set
2273                  RTX_FRAME_RELATED_P on appropriate insns.  */
2274               cfa.reg = REGNO (dest);
2275               cfa_temp.reg = cfa.reg;
2276               cfa_temp.offset = cfa.offset;
2277             }
2278           else
2279             {
2280               /* Saving a register in a register.  */
2281               gcc_assert (!fixed_regs [REGNO (dest)]
2282                           /* For the SPARC and its register window.  */
2283                           || (DWARF_FRAME_REGNUM (REGNO (src))
2284                               == DWARF_FRAME_RETURN_COLUMN));
2285
2286               /* After stack is aligned, we can only save SP in FP
2287                  if drap register is used.  In this case, we have
2288                  to restore stack pointer with the CFA value and we
2289                  don't generate this DWARF information.  */
2290               if (fde
2291                   && fde->stack_realign
2292                   && REGNO (src) == STACK_POINTER_REGNUM)
2293                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2294                             && fde->drap_reg != INVALID_REGNUM
2295                             && cfa.reg != REGNO (src));
2296               else
2297                 queue_reg_save (label, src, dest, 0);
2298             }
2299           break;
2300
2301         case PLUS:
2302         case MINUS:
2303         case LO_SUM:
2304           if (dest == stack_pointer_rtx)
2305             {
2306               /* Rule 2 */
2307               /* Adjusting SP.  */
2308               switch (GET_CODE (XEXP (src, 1)))
2309                 {
2310                 case CONST_INT:
2311                   offset = INTVAL (XEXP (src, 1));
2312                   break;
2313                 case REG:
2314                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2315                               == cfa_temp.reg);
2316                   offset = cfa_temp.offset;
2317                   break;
2318                 default:
2319                   gcc_unreachable ();
2320                 }
2321
2322               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2323                 {
2324                   /* Restoring SP from FP in the epilogue.  */
2325                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2326                   cfa.reg = STACK_POINTER_REGNUM;
2327                 }
2328               else if (GET_CODE (src) == LO_SUM)
2329                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2330                 ;
2331               else
2332                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2333
2334               if (GET_CODE (src) != MINUS)
2335                 offset = -offset;
2336               if (cfa.reg == STACK_POINTER_REGNUM)
2337                 cfa.offset += offset;
2338               if (cfa_store.reg == STACK_POINTER_REGNUM)
2339                 cfa_store.offset += offset;
2340             }
2341           else if (dest == hard_frame_pointer_rtx)
2342             {
2343               /* Rule 3 */
2344               /* Either setting the FP from an offset of the SP,
2345                  or adjusting the FP */
2346               gcc_assert (frame_pointer_needed);
2347
2348               gcc_assert (REG_P (XEXP (src, 0))
2349                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2350                           && CONST_INT_P (XEXP (src, 1)));
2351               offset = INTVAL (XEXP (src, 1));
2352               if (GET_CODE (src) != MINUS)
2353                 offset = -offset;
2354               cfa.offset += offset;
2355               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2356             }
2357           else
2358             {
2359               gcc_assert (GET_CODE (src) != MINUS);
2360
2361               /* Rule 4 */
2362               if (REG_P (XEXP (src, 0))
2363                   && REGNO (XEXP (src, 0)) == cfa.reg
2364                   && CONST_INT_P (XEXP (src, 1)))
2365                 {
2366                   /* Setting a temporary CFA register that will be copied
2367                      into the FP later on.  */
2368                   offset = - INTVAL (XEXP (src, 1));
2369                   cfa.offset += offset;
2370                   cfa.reg = REGNO (dest);
2371                   /* Or used to save regs to the stack.  */
2372                   cfa_temp.reg = cfa.reg;
2373                   cfa_temp.offset = cfa.offset;
2374                 }
2375
2376               /* Rule 5 */
2377               else if (REG_P (XEXP (src, 0))
2378                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2379                        && XEXP (src, 1) == stack_pointer_rtx)
2380                 {
2381                   /* Setting a scratch register that we will use instead
2382                      of SP for saving registers to the stack.  */
2383                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2384                   cfa_store.reg = REGNO (dest);
2385                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2386                 }
2387
2388               /* Rule 9 */
2389               else if (GET_CODE (src) == LO_SUM
2390                        && CONST_INT_P (XEXP (src, 1)))
2391                 {
2392                   cfa_temp.reg = REGNO (dest);
2393                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2394                 }
2395               else
2396                 gcc_unreachable ();
2397             }
2398           break;
2399
2400           /* Rule 6 */
2401         case CONST_INT:
2402           cfa_temp.reg = REGNO (dest);
2403           cfa_temp.offset = INTVAL (src);
2404           break;
2405
2406           /* Rule 7 */
2407         case IOR:
2408           gcc_assert (REG_P (XEXP (src, 0))
2409                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2410                       && CONST_INT_P (XEXP (src, 1)));
2411
2412           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2413             cfa_temp.reg = REGNO (dest);
2414           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2415           break;
2416
2417           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2418              which will fill in all of the bits.  */
2419           /* Rule 8 */
2420         case HIGH:
2421           break;
2422
2423           /* Rule 15 */
2424         case UNSPEC:
2425         case UNSPEC_VOLATILE:
2426           gcc_assert (targetm.dwarf_handle_frame_unspec);
2427           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2428           return;
2429
2430           /* Rule 16 */
2431         case AND:
2432           /* If this AND operation happens on stack pointer in prologue,
2433              we assume the stack is realigned and we extract the
2434              alignment.  */
2435           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2436             {
2437               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2438               fde->stack_realign = 1;
2439               fde->stack_realignment = INTVAL (XEXP (src, 1));
2440               cfa_store.offset = 0;
2441
2442               if (cfa.reg != STACK_POINTER_REGNUM
2443                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2444                 fde->drap_reg = cfa.reg;
2445             }
2446           return;
2447
2448         default:
2449           gcc_unreachable ();
2450         }
2451
2452       def_cfa_1 (label, &cfa);
2453       break;
2454
2455     case MEM:
2456
2457       /* Saving a register to the stack.  Make sure dest is relative to the
2458          CFA register.  */
2459       switch (GET_CODE (XEXP (dest, 0)))
2460         {
2461           /* Rule 10 */
2462           /* With a push.  */
2463         case PRE_MODIFY:
2464           /* We can't handle variable size modifications.  */
2465           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2466                       == CONST_INT);
2467           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2468
2469           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2470                       && cfa_store.reg == STACK_POINTER_REGNUM);
2471
2472           cfa_store.offset += offset;
2473           if (cfa.reg == STACK_POINTER_REGNUM)
2474             cfa.offset = cfa_store.offset;
2475
2476           offset = -cfa_store.offset;
2477           break;
2478
2479           /* Rule 11 */
2480         case PRE_INC:
2481         case PRE_DEC:
2482           offset = GET_MODE_SIZE (GET_MODE (dest));
2483           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2484             offset = -offset;
2485
2486           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2487                        == STACK_POINTER_REGNUM)
2488                       && cfa_store.reg == STACK_POINTER_REGNUM);
2489
2490           cfa_store.offset += offset;
2491
2492           /* Rule 18: If stack is aligned, we will use FP as a
2493              reference to represent the address of the stored
2494              regiser.  */
2495           if (fde
2496               && fde->stack_realign
2497               && src == hard_frame_pointer_rtx)
2498             {
2499               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2500               cfa_store.offset = 0;
2501             }
2502
2503           if (cfa.reg == STACK_POINTER_REGNUM)
2504             cfa.offset = cfa_store.offset;
2505
2506           offset = -cfa_store.offset;
2507           break;
2508
2509           /* Rule 12 */
2510           /* With an offset.  */
2511         case PLUS:
2512         case MINUS:
2513         case LO_SUM:
2514           {
2515             int regno;
2516
2517             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2518                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2519             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2520             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2521               offset = -offset;
2522
2523             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2524
2525             if (cfa_store.reg == (unsigned) regno)
2526               offset -= cfa_store.offset;
2527             else
2528               {
2529                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2530                 offset -= cfa_temp.offset;
2531               }
2532           }
2533           break;
2534
2535           /* Rule 13 */
2536           /* Without an offset.  */
2537         case REG:
2538           {
2539             int regno = REGNO (XEXP (dest, 0));
2540
2541             if (cfa_store.reg == (unsigned) regno)
2542               offset = -cfa_store.offset;
2543             else
2544               {
2545                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2546                 offset = -cfa_temp.offset;
2547               }
2548           }
2549           break;
2550
2551           /* Rule 14 */
2552         case POST_INC:
2553           gcc_assert (cfa_temp.reg
2554                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2555           offset = -cfa_temp.offset;
2556           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2557           break;
2558
2559         default:
2560           gcc_unreachable ();
2561         }
2562
2563         /* Rule 17 */
2564         /* If the source operand of this MEM operation is not a
2565            register, basically the source is return address.  Here
2566            we only care how much stack grew and we don't save it.  */
2567       if (!REG_P (src))
2568         break;
2569
2570       if (REGNO (src) != STACK_POINTER_REGNUM
2571           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2572           && (unsigned) REGNO (src) == cfa.reg)
2573         {
2574           /* We're storing the current CFA reg into the stack.  */
2575
2576           if (cfa.offset == 0)
2577             {
2578               /* Rule 19 */
2579               /* If stack is aligned, putting CFA reg into stack means
2580                  we can no longer use reg + offset to represent CFA.
2581                  Here we use DW_CFA_def_cfa_expression instead.  The
2582                  result of this expression equals to the original CFA
2583                  value.  */
2584               if (fde
2585                   && fde->stack_realign
2586                   && cfa.indirect == 0
2587                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2588                 {
2589                   dw_cfa_location cfa_exp;
2590
2591                   gcc_assert (fde->drap_reg == cfa.reg);
2592
2593                   cfa_exp.indirect = 1;
2594                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2595                   cfa_exp.base_offset = offset;
2596                   cfa_exp.offset = 0;
2597
2598                   fde->drap_reg_saved = 1;
2599
2600                   def_cfa_1 (label, &cfa_exp);
2601                   break;
2602                 }
2603
2604               /* If the source register is exactly the CFA, assume
2605                  we're saving SP like any other register; this happens
2606                  on the ARM.  */
2607               def_cfa_1 (label, &cfa);
2608               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2609               break;
2610             }
2611           else
2612             {
2613               /* Otherwise, we'll need to look in the stack to
2614                  calculate the CFA.  */
2615               rtx x = XEXP (dest, 0);
2616
2617               if (!REG_P (x))
2618                 x = XEXP (x, 0);
2619               gcc_assert (REG_P (x));
2620
2621               cfa.reg = REGNO (x);
2622               cfa.base_offset = offset;
2623               cfa.indirect = 1;
2624               def_cfa_1 (label, &cfa);
2625               break;
2626             }
2627         }
2628
2629       def_cfa_1 (label, &cfa);
2630       {
2631         span = targetm.dwarf_register_span (src);
2632
2633         if (!span)
2634           queue_reg_save (label, src, NULL_RTX, offset);
2635         else
2636           {
2637             /* We have a PARALLEL describing where the contents of SRC
2638                live.  Queue register saves for each piece of the
2639                PARALLEL.  */
2640             int par_index;
2641             int limit;
2642             HOST_WIDE_INT span_offset = offset;
2643
2644             gcc_assert (GET_CODE (span) == PARALLEL);
2645
2646             limit = XVECLEN (span, 0);
2647             for (par_index = 0; par_index < limit; par_index++)
2648               {
2649                 rtx elem = XVECEXP (span, 0, par_index);
2650
2651                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2652                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2653               }
2654           }
2655       }
2656       break;
2657
2658     default:
2659       gcc_unreachable ();
2660     }
2661 }
2662
2663 /* Record call frame debugging information for INSN, which either
2664    sets SP or FP (adjusting how we calculate the frame address) or saves a
2665    register to the stack.  If INSN is NULL_RTX, initialize our state.
2666
2667    If AFTER_P is false, we're being called before the insn is emitted,
2668    otherwise after.  Call instructions get invoked twice.  */
2669
2670 void
2671 dwarf2out_frame_debug (rtx insn, bool after_p)
2672 {
2673   const char *label;
2674   rtx note, n;
2675   bool handled_one = false;
2676
2677   if (insn == NULL_RTX)
2678     {
2679       size_t i;
2680
2681       /* Flush any queued register saves.  */
2682       flush_queued_reg_saves ();
2683
2684       /* Set up state for generating call frame debug info.  */
2685       lookup_cfa (&cfa);
2686       gcc_assert (cfa.reg
2687                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2688
2689       cfa.reg = STACK_POINTER_REGNUM;
2690       cfa_store = cfa;
2691       cfa_temp.reg = -1;
2692       cfa_temp.offset = 0;
2693
2694       for (i = 0; i < num_regs_saved_in_regs; i++)
2695         {
2696           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2697           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2698         }
2699       num_regs_saved_in_regs = 0;
2700
2701       if (barrier_args_size)
2702         {
2703           XDELETEVEC (barrier_args_size);
2704           barrier_args_size = NULL;
2705         }
2706       return;
2707     }
2708
2709   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2710     flush_queued_reg_saves ();
2711
2712   if (! RTX_FRAME_RELATED_P (insn))
2713     {
2714       if (!ACCUMULATE_OUTGOING_ARGS)
2715         dwarf2out_stack_adjust (insn, after_p);
2716       return;
2717     }
2718
2719   label = dwarf2out_cfi_label (false);
2720
2721   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2722     switch (REG_NOTE_KIND (note))
2723       {
2724       case REG_FRAME_RELATED_EXPR:
2725         insn = XEXP (note, 0);
2726         goto found;
2727
2728       case REG_CFA_DEF_CFA:
2729         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2730         handled_one = true;
2731         break;
2732
2733       case REG_CFA_ADJUST_CFA:
2734         n = XEXP (note, 0);
2735         if (n == NULL)
2736           {
2737             n = PATTERN (insn);
2738             if (GET_CODE (n) == PARALLEL)
2739               n = XVECEXP (n, 0, 0);
2740           }
2741         dwarf2out_frame_debug_adjust_cfa (n, label);
2742         handled_one = true;
2743         break;
2744
2745       case REG_CFA_OFFSET:
2746         n = XEXP (note, 0);
2747         if (n == NULL)
2748           n = single_set (insn);
2749         dwarf2out_frame_debug_cfa_offset (n, label);
2750         handled_one = true;
2751         break;
2752
2753       case REG_CFA_REGISTER:
2754         n = XEXP (note, 0);
2755         if (n == NULL)
2756           {
2757             n = PATTERN (insn);
2758             if (GET_CODE (n) == PARALLEL)
2759               n = XVECEXP (n, 0, 0);
2760           }
2761         dwarf2out_frame_debug_cfa_register (n, label);
2762         handled_one = true;
2763         break;
2764
2765       case REG_CFA_RESTORE:
2766         n = XEXP (note, 0);
2767         if (n == NULL)
2768           {
2769             n = PATTERN (insn);
2770             if (GET_CODE (n) == PARALLEL)
2771               n = XVECEXP (n, 0, 0);
2772             n = XEXP (n, 0);
2773           }
2774         dwarf2out_frame_debug_cfa_restore (n, label);
2775         handled_one = true;
2776         break;
2777
2778       default:
2779         break;
2780       }
2781   if (handled_one)
2782     return;
2783
2784   insn = PATTERN (insn);
2785  found:
2786   dwarf2out_frame_debug_expr (insn, label);
2787 }
2788
2789 /* Determine if we need to save and restore CFI information around this
2790    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2791    we do need to save/restore, then emit the save now, and insert a
2792    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2793
2794 void
2795 dwarf2out_begin_epilogue (rtx insn)
2796 {
2797   bool saw_frp = false;
2798   rtx i;
2799
2800   /* Scan forward to the return insn, noticing if there are possible
2801      frame related insns.  */
2802   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2803     {
2804       if (!INSN_P (i))
2805         continue;
2806
2807       /* Look for both regular and sibcalls to end the block.  */
2808       if (returnjump_p (i))
2809         break;
2810       if (CALL_P (i) && SIBLING_CALL_P (i))
2811         break;
2812
2813       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2814         {
2815           int idx;
2816           rtx seq = PATTERN (i);
2817
2818           if (returnjump_p (XVECEXP (seq, 0, 0)))
2819             break;
2820           if (CALL_P (XVECEXP (seq, 0, 0))
2821               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2822             break;
2823
2824           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2825             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2826               saw_frp = true;
2827         }
2828
2829       if (RTX_FRAME_RELATED_P (i))
2830         saw_frp = true;
2831     }
2832
2833   /* If the port doesn't emit epilogue unwind info, we don't need a
2834      save/restore pair.  */
2835   if (!saw_frp)
2836     return;
2837
2838   /* Otherwise, search forward to see if the return insn was the last
2839      basic block of the function.  If so, we don't need save/restore.  */
2840   gcc_assert (i != NULL);
2841   i = next_real_insn (i);
2842   if (i == NULL)
2843     return;
2844
2845   /* Insert the restore before that next real insn in the stream, and before
2846      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2847      properly nested.  This should be after any label or alignment.  This
2848      will be pushed into the CFI stream by the function below.  */
2849   while (1)
2850     {
2851       rtx p = PREV_INSN (i);
2852       if (!NOTE_P (p))
2853         break;
2854       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2855         break;
2856       i = p;
2857     }
2858   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2859
2860   emit_cfa_remember = true;
2861
2862   /* And emulate the state save.  */
2863   gcc_assert (!cfa_remember.in_use);
2864   cfa_remember = cfa;
2865   cfa_remember.in_use = 1;
2866 }
2867
2868 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2869
2870 void
2871 dwarf2out_frame_debug_restore_state (void)
2872 {
2873   dw_cfi_ref cfi = new_cfi (); 
2874   const char *label = dwarf2out_cfi_label (false);
2875
2876   cfi->dw_cfi_opc = DW_CFA_restore_state;
2877   add_fde_cfi (label, cfi);
2878
2879   gcc_assert (cfa_remember.in_use);
2880   cfa = cfa_remember;
2881   cfa_remember.in_use = 0;
2882 }
2883
2884 #endif
2885
2886 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2887 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2888  (enum dwarf_call_frame_info cfi);
2889
2890 static enum dw_cfi_oprnd_type
2891 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2892 {
2893   switch (cfi)
2894     {
2895     case DW_CFA_nop:
2896     case DW_CFA_GNU_window_save:
2897     case DW_CFA_remember_state:
2898     case DW_CFA_restore_state:
2899       return dw_cfi_oprnd_unused;
2900
2901     case DW_CFA_set_loc:
2902     case DW_CFA_advance_loc1:
2903     case DW_CFA_advance_loc2:
2904     case DW_CFA_advance_loc4:
2905     case DW_CFA_MIPS_advance_loc8:
2906       return dw_cfi_oprnd_addr;
2907
2908     case DW_CFA_offset:
2909     case DW_CFA_offset_extended:
2910     case DW_CFA_def_cfa:
2911     case DW_CFA_offset_extended_sf:
2912     case DW_CFA_def_cfa_sf:
2913     case DW_CFA_restore:
2914     case DW_CFA_restore_extended:
2915     case DW_CFA_undefined:
2916     case DW_CFA_same_value:
2917     case DW_CFA_def_cfa_register:
2918     case DW_CFA_register:
2919       return dw_cfi_oprnd_reg_num;
2920
2921     case DW_CFA_def_cfa_offset:
2922     case DW_CFA_GNU_args_size:
2923     case DW_CFA_def_cfa_offset_sf:
2924       return dw_cfi_oprnd_offset;
2925
2926     case DW_CFA_def_cfa_expression:
2927     case DW_CFA_expression:
2928       return dw_cfi_oprnd_loc;
2929
2930     default:
2931       gcc_unreachable ();
2932     }
2933 }
2934
2935 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2936 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2937  (enum dwarf_call_frame_info cfi);
2938
2939 static enum dw_cfi_oprnd_type
2940 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2941 {
2942   switch (cfi)
2943     {
2944     case DW_CFA_def_cfa:
2945     case DW_CFA_def_cfa_sf:
2946     case DW_CFA_offset:
2947     case DW_CFA_offset_extended_sf:
2948     case DW_CFA_offset_extended:
2949       return dw_cfi_oprnd_offset;
2950
2951     case DW_CFA_register:
2952       return dw_cfi_oprnd_reg_num;
2953
2954     default:
2955       return dw_cfi_oprnd_unused;
2956     }
2957 }
2958
2959 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2960
2961 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2962    switch to the data section instead, and write out a synthetic start label
2963    for collect2 the first time around.  */
2964
2965 static void
2966 switch_to_eh_frame_section (bool back)
2967 {
2968   tree label;
2969
2970 #ifdef EH_FRAME_SECTION_NAME
2971   if (eh_frame_section == 0)
2972     {
2973       int flags;
2974
2975       if (EH_TABLES_CAN_BE_READ_ONLY)
2976         {
2977           int fde_encoding;
2978           int per_encoding;
2979           int lsda_encoding;
2980
2981           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2982                                                        /*global=*/0);
2983           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2984                                                        /*global=*/1);
2985           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2986                                                         /*global=*/0);
2987           flags = ((! flag_pic
2988                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2989                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2990                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2991                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2992                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2993                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2994                    ? 0 : SECTION_WRITE);
2995         }
2996       else
2997         flags = SECTION_WRITE;
2998       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2999     }
3000 #endif
3001
3002   if (eh_frame_section)
3003     switch_to_section (eh_frame_section);
3004   else
3005     {
3006       /* We have no special eh_frame section.  Put the information in
3007          the data section and emit special labels to guide collect2.  */
3008       switch_to_section (data_section);
3009
3010       if (!back)
3011         {
3012           label = get_file_function_name ("F");
3013           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3014           targetm.asm_out.globalize_label (asm_out_file,
3015                                            IDENTIFIER_POINTER (label));
3016           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3017         }
3018     }
3019 }
3020
3021 /* Switch [BACK] to the eh or debug frame table section, depending on
3022    FOR_EH.  */
3023
3024 static void
3025 switch_to_frame_table_section (int for_eh, bool back)
3026 {
3027   if (for_eh)
3028     switch_to_eh_frame_section (back);
3029   else
3030     {
3031       if (!debug_frame_section)
3032         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3033                                            SECTION_DEBUG, NULL);
3034       switch_to_section (debug_frame_section);
3035     }
3036 }
3037
3038 /* Output a Call Frame Information opcode and its operand(s).  */
3039
3040 static void
3041 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3042 {
3043   unsigned long r;
3044   HOST_WIDE_INT off;
3045
3046   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3047     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3048                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3049                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3050                          ((unsigned HOST_WIDE_INT)
3051                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3052   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3053     {
3054       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3055       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3056                            "DW_CFA_offset, column 0x%lx", r);
3057       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3058       dw2_asm_output_data_uleb128 (off, NULL);
3059     }
3060   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3061     {
3062       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3063       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3064                            "DW_CFA_restore, column 0x%lx", r);
3065     }
3066   else
3067     {
3068       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3069                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3070
3071       switch (cfi->dw_cfi_opc)
3072         {
3073         case DW_CFA_set_loc:
3074           if (for_eh)
3075             dw2_asm_output_encoded_addr_rtx (
3076                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3077                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3078                 false, NULL);
3079           else
3080             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3081                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3082           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3083           break;
3084
3085         case DW_CFA_advance_loc1:
3086           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3087                                 fde->dw_fde_current_label, NULL);
3088           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3089           break;
3090
3091         case DW_CFA_advance_loc2:
3092           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3093                                 fde->dw_fde_current_label, NULL);
3094           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3095           break;
3096
3097         case DW_CFA_advance_loc4:
3098           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3099                                 fde->dw_fde_current_label, NULL);
3100           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3101           break;
3102
3103         case DW_CFA_MIPS_advance_loc8:
3104           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3105                                 fde->dw_fde_current_label, NULL);
3106           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3107           break;
3108
3109         case DW_CFA_offset_extended:
3110           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3111           dw2_asm_output_data_uleb128 (r, NULL);
3112           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3113           dw2_asm_output_data_uleb128 (off, NULL);
3114           break;
3115
3116         case DW_CFA_def_cfa:
3117           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3118           dw2_asm_output_data_uleb128 (r, NULL);
3119           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3120           break;
3121
3122         case DW_CFA_offset_extended_sf:
3123           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3124           dw2_asm_output_data_uleb128 (r, NULL);
3125           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3126           dw2_asm_output_data_sleb128 (off, NULL);
3127           break;
3128
3129         case DW_CFA_def_cfa_sf:
3130           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3131           dw2_asm_output_data_uleb128 (r, NULL);
3132           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3133           dw2_asm_output_data_sleb128 (off, NULL);
3134           break;
3135
3136         case DW_CFA_restore_extended:
3137         case DW_CFA_undefined:
3138         case DW_CFA_same_value:
3139         case DW_CFA_def_cfa_register:
3140           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3141           dw2_asm_output_data_uleb128 (r, NULL);
3142           break;
3143
3144         case DW_CFA_register:
3145           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3146           dw2_asm_output_data_uleb128 (r, NULL);
3147           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3148           dw2_asm_output_data_uleb128 (r, NULL);
3149           break;
3150
3151         case DW_CFA_def_cfa_offset:
3152         case DW_CFA_GNU_args_size:
3153           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3154           break;
3155
3156         case DW_CFA_def_cfa_offset_sf:
3157           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3158           dw2_asm_output_data_sleb128 (off, NULL);
3159           break;
3160
3161         case DW_CFA_GNU_window_save:
3162           break;
3163
3164         case DW_CFA_def_cfa_expression:
3165         case DW_CFA_expression:
3166           output_cfa_loc (cfi);
3167           break;
3168
3169         case DW_CFA_GNU_negative_offset_extended:
3170           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3171           gcc_unreachable ();
3172
3173         default:
3174           break;
3175         }
3176     }
3177 }
3178
3179 /* Similar, but do it via assembler directives instead.  */
3180
3181 static void
3182 output_cfi_directive (dw_cfi_ref cfi)
3183 {
3184   unsigned long r, r2;
3185
3186   switch (cfi->dw_cfi_opc)
3187     {
3188     case DW_CFA_advance_loc:
3189     case DW_CFA_advance_loc1:
3190     case DW_CFA_advance_loc2:
3191     case DW_CFA_advance_loc4:
3192     case DW_CFA_MIPS_advance_loc8:
3193     case DW_CFA_set_loc:
3194       /* Should only be created by add_fde_cfi in a code path not
3195          followed when emitting via directives.  The assembler is
3196          going to take care of this for us.  */
3197       gcc_unreachable ();
3198
3199     case DW_CFA_offset:
3200     case DW_CFA_offset_extended:
3201     case DW_CFA_offset_extended_sf:
3202       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3203       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3204                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3205       break;
3206
3207     case DW_CFA_restore:
3208     case DW_CFA_restore_extended:
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3210       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3211       break;
3212
3213     case DW_CFA_undefined:
3214       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3215       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3216       break;
3217
3218     case DW_CFA_same_value:
3219       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3220       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3221       break;
3222
3223     case DW_CFA_def_cfa:
3224     case DW_CFA_def_cfa_sf:
3225       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3226       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3227                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3228       break;
3229
3230     case DW_CFA_def_cfa_register:
3231       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3232       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3233       break;
3234
3235     case DW_CFA_register:
3236       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3237       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3238       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3239       break;
3240
3241     case DW_CFA_def_cfa_offset:
3242     case DW_CFA_def_cfa_offset_sf:
3243       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3244                HOST_WIDE_INT_PRINT_DEC"\n",
3245                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3246       break;
3247
3248     case DW_CFA_remember_state:
3249       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3250       break;
3251     case DW_CFA_restore_state:
3252       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3253       break;
3254
3255     case DW_CFA_GNU_args_size:
3256       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3257       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3258       if (flag_debug_asm)
3259         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3260                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3261       fputc ('\n', asm_out_file);
3262       break;
3263
3264     case DW_CFA_GNU_window_save:
3265       fprintf (asm_out_file, "\t.cfi_window_save\n");
3266       break;
3267
3268     case DW_CFA_def_cfa_expression:
3269     case DW_CFA_expression:
3270       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3271       output_cfa_loc_raw (cfi);
3272       fputc ('\n', asm_out_file);
3273       break;
3274
3275     default:
3276       gcc_unreachable ();
3277     }
3278 }
3279
3280 DEF_VEC_P (dw_cfi_ref);
3281 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3282
3283 /* Output CFIs to bring current FDE to the same state as after executing
3284    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3285    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3286    other arguments to pass to output_cfi.  */
3287
3288 static void
3289 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3290 {
3291   struct dw_cfi_struct cfi_buf;
3292   dw_cfi_ref cfi2;
3293   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3294   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3295   unsigned int len, idx;
3296
3297   for (;; cfi = cfi->dw_cfi_next)
3298     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3299       {
3300       case DW_CFA_advance_loc:
3301       case DW_CFA_advance_loc1:
3302       case DW_CFA_advance_loc2:
3303       case DW_CFA_advance_loc4:
3304       case DW_CFA_MIPS_advance_loc8:
3305       case DW_CFA_set_loc:
3306         /* All advances should be ignored.  */
3307         break;
3308       case DW_CFA_remember_state:
3309         {
3310           dw_cfi_ref args_size = cfi_args_size;
3311
3312           /* Skip everything between .cfi_remember_state and
3313              .cfi_restore_state.  */
3314           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3315             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3316               break;
3317             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3318               args_size = cfi2;
3319             else
3320               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3321
3322           if (cfi2 == NULL)
3323             goto flush_all;
3324           else
3325             {
3326               cfi = cfi2;
3327               cfi_args_size = args_size;
3328             }
3329           break;
3330         }
3331       case DW_CFA_GNU_args_size:
3332         cfi_args_size = cfi;
3333         break;
3334       case DW_CFA_GNU_window_save:
3335         goto flush_all;
3336       case DW_CFA_offset:
3337       case DW_CFA_offset_extended:
3338       case DW_CFA_offset_extended_sf:
3339       case DW_CFA_restore:
3340       case DW_CFA_restore_extended:
3341       case DW_CFA_undefined:
3342       case DW_CFA_same_value:
3343       case DW_CFA_register:
3344       case DW_CFA_val_offset:
3345       case DW_CFA_val_offset_sf:
3346       case DW_CFA_expression:
3347       case DW_CFA_val_expression:
3348       case DW_CFA_GNU_negative_offset_extended:
3349         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3350           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3351                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3352         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3353         break;
3354       case DW_CFA_def_cfa:
3355       case DW_CFA_def_cfa_sf:
3356       case DW_CFA_def_cfa_expression:
3357         cfi_cfa = cfi;
3358         cfi_cfa_offset = cfi;
3359         break;
3360       case DW_CFA_def_cfa_register:
3361         cfi_cfa = cfi;
3362         break;
3363       case DW_CFA_def_cfa_offset:
3364       case DW_CFA_def_cfa_offset_sf:
3365         cfi_cfa_offset = cfi;
3366         break;
3367       case DW_CFA_nop:
3368         gcc_assert (cfi == NULL);
3369       flush_all:
3370         len = VEC_length (dw_cfi_ref, regs);
3371         for (idx = 0; idx < len; idx++)
3372           {
3373             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3374             if (cfi2 != NULL
3375                 && cfi2->dw_cfi_opc != DW_CFA_restore
3376                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3377               {
3378                 if (do_cfi_asm)
3379                   output_cfi_directive (cfi2);
3380                 else
3381                   output_cfi (cfi2, fde, for_eh);
3382               }
3383           }
3384         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3385           {
3386             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3387             cfi_buf = *cfi_cfa;
3388             switch (cfi_cfa_offset->dw_cfi_opc)
3389               {
3390               case DW_CFA_def_cfa_offset:
3391                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3392                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3393                 break;
3394               case DW_CFA_def_cfa_offset_sf:
3395                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3396                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3397                 break;
3398               case DW_CFA_def_cfa:
3399               case DW_CFA_def_cfa_sf:
3400                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3401                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3402                 break;
3403               default:
3404                 gcc_unreachable ();
3405               }
3406             cfi_cfa = &cfi_buf;
3407           }
3408         else if (cfi_cfa_offset)
3409           cfi_cfa = cfi_cfa_offset;
3410         if (cfi_cfa)
3411           {
3412             if (do_cfi_asm)
3413               output_cfi_directive (cfi_cfa);
3414             else
3415               output_cfi (cfi_cfa, fde, for_eh);
3416           }
3417         cfi_cfa = NULL;
3418         cfi_cfa_offset = NULL;
3419         if (cfi_args_size
3420             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3421           {
3422             if (do_cfi_asm)
3423               output_cfi_directive (cfi_args_size);
3424             else
3425               output_cfi (cfi_args_size, fde, for_eh);
3426           }
3427         cfi_args_size = NULL;
3428         if (cfi == NULL)
3429           {
3430             VEC_free (dw_cfi_ref, heap, regs);
3431             return;
3432           }
3433         else if (do_cfi_asm)
3434           output_cfi_directive (cfi);
3435         else
3436           output_cfi (cfi, fde, for_eh);
3437         break;
3438       default:
3439         gcc_unreachable ();
3440     }
3441 }
3442
3443 /* Output one FDE.  */
3444
3445 static void
3446 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3447             char *section_start_label, int fde_encoding, char *augmentation,
3448             bool any_lsda_needed, int lsda_encoding)
3449 {
3450   const char *begin, *end;
3451   static unsigned int j;
3452   char l1[20], l2[20];
3453   dw_cfi_ref cfi;
3454
3455   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3456                                 /* empty */ 0);
3457   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3458                                   for_eh + j);
3459   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3460   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3461   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3462     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3463                          " indicating 64-bit DWARF extension");
3464   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3465                         "FDE Length");
3466   ASM_OUTPUT_LABEL (asm_out_file, l1);
3467
3468   if (for_eh)
3469     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3470   else
3471     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3472                            debug_frame_section, "FDE CIE offset");
3473
3474   if (!fde->dw_fde_switched_sections)
3475     {
3476       begin = fde->dw_fde_begin;
3477       end = fde->dw_fde_end;
3478     }
3479   else
3480     {
3481       /* For the first section, prefer dw_fde_begin over
3482          dw_fde_{hot,cold}_section_label, as the latter
3483          might be separated from the real start of the
3484          function by alignment padding.  */
3485       if (!second)
3486         begin = fde->dw_fde_begin;
3487       else if (fde->dw_fde_switched_cold_to_hot)
3488         begin = fde->dw_fde_hot_section_label;
3489       else
3490         begin = fde->dw_fde_unlikely_section_label;
3491       if (second ^ fde->dw_fde_switched_cold_to_hot)
3492         end = fde->dw_fde_unlikely_section_end_label;
3493       else
3494         end = fde->dw_fde_hot_section_end_label;
3495     }
3496
3497   if (for_eh)
3498     {
3499       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3500       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3501       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3502                                        "FDE initial location");
3503       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3504                             end, begin, "FDE address range");
3505     }
3506   else
3507     {
3508       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3509       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3510     }
3511
3512   if (augmentation[0])
3513     {
3514       if (any_lsda_needed)
3515         {
3516           int size = size_of_encoded_value (lsda_encoding);
3517
3518           if (lsda_encoding == DW_EH_PE_aligned)
3519             {
3520               int offset = (  4         /* Length */
3521                             + 4         /* CIE offset */
3522                             + 2 * size_of_encoded_value (fde_encoding)
3523                             + 1         /* Augmentation size */ );
3524               int pad = -offset & (PTR_SIZE - 1);
3525
3526               size += pad;
3527               gcc_assert (size_of_uleb128 (size) == 1);
3528             }
3529
3530           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3531
3532           if (fde->uses_eh_lsda)
3533             {
3534               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3535                                            fde->funcdef_number);
3536               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3537                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3538                                                false,
3539                                                "Language Specific Data Area");
3540             }
3541           else
3542             {
3543               if (lsda_encoding == DW_EH_PE_aligned)
3544                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3545               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3546                                    "Language Specific Data Area (none)");
3547             }
3548         }
3549       else
3550         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3551     }
3552
3553   /* Loop through the Call Frame Instructions associated with
3554      this FDE.  */
3555   fde->dw_fde_current_label = begin;
3556   if (!fde->dw_fde_switched_sections)
3557     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3558       output_cfi (cfi, fde, for_eh);
3559   else if (!second)
3560     {
3561       if (fde->dw_fde_switch_cfi)
3562         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3563           {
3564             output_cfi (cfi, fde, for_eh);
3565             if (cfi == fde->dw_fde_switch_cfi)
3566               break;
3567           }
3568     }
3569   else
3570     {
3571       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3572
3573       if (fde->dw_fde_switch_cfi)
3574         {
3575           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3576           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3577           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3578           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3579         }
3580       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3581         output_cfi (cfi, fde, for_eh);
3582     }
3583
3584   /* If we are to emit a ref/link from function bodies to their frame tables,
3585      do it now.  This is typically performed to make sure that tables
3586      associated with functions are dragged with them and not discarded in
3587      garbage collecting links. We need to do this on a per function basis to
3588      cope with -ffunction-sections.  */
3589
3590 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3591   /* Switch to the function section, emit the ref to the tables, and
3592      switch *back* into the table section.  */
3593   switch_to_section (function_section (fde->decl));
3594   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3595   switch_to_frame_table_section (for_eh, true);
3596 #endif
3597
3598   /* Pad the FDE out to an address sized boundary.  */
3599   ASM_OUTPUT_ALIGN (asm_out_file,
3600                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3601   ASM_OUTPUT_LABEL (asm_out_file, l2);
3602
3603   j += 2;
3604 }
3605
3606 /* Output the call frame information used to record information
3607    that relates to calculating the frame pointer, and records the
3608    location of saved registers.  */
3609
3610 static void
3611 output_call_frame_info (int for_eh)
3612 {
3613   unsigned int i;
3614   dw_fde_ref fde;
3615   dw_cfi_ref cfi;
3616   char l1[20], l2[20], section_start_label[20];
3617   bool any_lsda_needed = false;
3618   char augmentation[6];
3619   int augmentation_size;
3620   int fde_encoding = DW_EH_PE_absptr;
3621   int per_encoding = DW_EH_PE_absptr;
3622   int lsda_encoding = DW_EH_PE_absptr;
3623   int return_reg;
3624   rtx personality = NULL;
3625   int dw_cie_version;
3626
3627   /* Don't emit a CIE if there won't be any FDEs.  */
3628   if (fde_table_in_use == 0)
3629     return;
3630
3631   /* Nothing to do if the assembler's doing it all.  */
3632   if (dwarf2out_do_cfi_asm ())
3633     return;
3634
3635   /* If we make FDEs linkonce, we may have to emit an empty label for
3636      an FDE that wouldn't otherwise be emitted.  We want to avoid
3637      having an FDE kept around when the function it refers to is
3638      discarded.  Example where this matters: a primary function
3639      template in C++ requires EH information, but an explicit
3640      specialization doesn't.  */
3641   if (TARGET_USES_WEAK_UNWIND_INFO
3642       && ! flag_asynchronous_unwind_tables
3643       && flag_exceptions
3644       && for_eh)
3645     for (i = 0; i < fde_table_in_use; i++)
3646       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3647           && !fde_table[i].uses_eh_lsda
3648           && ! DECL_WEAK (fde_table[i].decl))
3649         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3650                                       for_eh, /* empty */ 1);
3651
3652   /* If we don't have any functions we'll want to unwind out of, don't
3653      emit any EH unwind information.  Note that if exceptions aren't
3654      enabled, we won't have collected nothrow information, and if we
3655      asked for asynchronous tables, we always want this info.  */
3656   if (for_eh)
3657     {
3658       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3659
3660       for (i = 0; i < fde_table_in_use; i++)
3661         if (fde_table[i].uses_eh_lsda)
3662           any_eh_needed = any_lsda_needed = true;
3663         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3664           any_eh_needed = true;
3665         else if (! fde_table[i].nothrow
3666                  && ! fde_table[i].all_throwers_are_sibcalls)
3667           any_eh_needed = true;
3668
3669       if (! any_eh_needed)
3670         return;
3671     }
3672
3673   /* We're going to be generating comments, so turn on app.  */
3674   if (flag_debug_asm)
3675     app_enable ();
3676
3677   /* Switch to the proper frame section, first time.  */
3678   switch_to_frame_table_section (for_eh, false);
3679
3680   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3681   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3682
3683   /* Output the CIE.  */
3684   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3685   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3686   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3687     dw2_asm_output_data (4, 0xffffffff,
3688       "Initial length escape value indicating 64-bit DWARF extension");
3689   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3690                         "Length of Common Information Entry");
3691   ASM_OUTPUT_LABEL (asm_out_file, l1);
3692
3693   /* Now that the CIE pointer is PC-relative for EH,
3694      use 0 to identify the CIE.  */
3695   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3696                        (for_eh ? 0 : DWARF_CIE_ID),
3697                        "CIE Identifier Tag");
3698
3699   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3700      use CIE version 1, unless that would produce incorrect results
3701      due to overflowing the return register column.  */
3702   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3703   dw_cie_version = 1;
3704   if (return_reg >= 256 || dwarf_version > 2)
3705     dw_cie_version = 3;
3706   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3707
3708   augmentation[0] = 0;
3709   augmentation_size = 0;
3710
3711   personality = current_unit_personality;
3712   if (for_eh)
3713     {
3714       char *p;
3715
3716       /* Augmentation:
3717          z      Indicates that a uleb128 is present to size the
3718                 augmentation section.
3719          L      Indicates the encoding (and thus presence) of
3720                 an LSDA pointer in the FDE augmentation.
3721          R      Indicates a non-default pointer encoding for
3722                 FDE code pointers.
3723          P      Indicates the presence of an encoding + language
3724                 personality routine in the CIE augmentation.  */
3725
3726       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3727       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3728       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3729
3730       p = augmentation + 1;
3731       if (personality)
3732         {
3733           *p++ = 'P';
3734           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3735           assemble_external_libcall (personality);
3736         }
3737       if (any_lsda_needed)
3738         {
3739           *p++ = 'L';
3740           augmentation_size += 1;
3741         }
3742       if (fde_encoding != DW_EH_PE_absptr)
3743         {
3744           *p++ = 'R';
3745           augmentation_size += 1;
3746         }
3747       if (p > augmentation + 1)
3748         {
3749           augmentation[0] = 'z';
3750           *p = '\0';
3751         }
3752
3753       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3754       if (personality && per_encoding == DW_EH_PE_aligned)
3755         {
3756           int offset = (  4             /* Length */
3757                         + 4             /* CIE Id */
3758                         + 1             /* CIE version */
3759                         + strlen (augmentation) + 1     /* Augmentation */
3760                         + size_of_uleb128 (1)           /* Code alignment */
3761                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3762                         + 1             /* RA column */
3763                         + 1             /* Augmentation size */
3764                         + 1             /* Personality encoding */ );
3765           int pad = -offset & (PTR_SIZE - 1);
3766
3767           augmentation_size += pad;
3768
3769           /* Augmentations should be small, so there's scarce need to
3770              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3771           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3772         }
3773     }
3774
3775   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3776   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3777   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3778                                "CIE Data Alignment Factor");
3779
3780   if (dw_cie_version == 1)
3781     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3782   else
3783     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3784
3785   if (augmentation[0])
3786     {
3787       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3788       if (personality)
3789         {
3790           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3791                                eh_data_format_name (per_encoding));
3792           dw2_asm_output_encoded_addr_rtx (per_encoding,
3793                                            personality,
3794                                            true, NULL);
3795         }
3796
3797       if (any_lsda_needed)
3798         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3799                              eh_data_format_name (lsda_encoding));
3800
3801       if (fde_encoding != DW_EH_PE_absptr)
3802         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3803                              eh_data_format_name (fde_encoding));
3804     }
3805
3806   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3807     output_cfi (cfi, NULL, for_eh);
3808
3809   /* Pad the CIE out to an address sized boundary.  */
3810   ASM_OUTPUT_ALIGN (asm_out_file,
3811                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3812   ASM_OUTPUT_LABEL (asm_out_file, l2);
3813
3814   /* Loop through all of the FDE's.  */
3815   for (i = 0; i < fde_table_in_use; i++)
3816     {
3817       unsigned int k;
3818       fde = &fde_table[i];
3819
3820       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3821       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3822           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3823           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3824           && !fde->uses_eh_lsda)
3825         continue;
3826
3827       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3828         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3829                     augmentation, any_lsda_needed, lsda_encoding);
3830     }
3831
3832   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3833     dw2_asm_output_data (4, 0, "End of Table");
3834 #ifdef MIPS_DEBUGGING_INFO
3835   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3836      get a value of 0.  Putting .align 0 after the label fixes it.  */
3837   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3838 #endif
3839
3840   /* Turn off app to make assembly quicker.  */
3841   if (flag_debug_asm)
3842     app_disable ();
3843 }
3844
3845 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3846
3847 static void
3848 dwarf2out_do_cfi_startproc (bool second)
3849 {
3850   int enc;
3851   rtx ref;
3852   rtx personality = get_personality_function (current_function_decl);
3853
3854   fprintf (asm_out_file, "\t.cfi_startproc\n");
3855
3856   if (personality)
3857     {
3858       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3859       ref = personality;
3860
3861       /* ??? The GAS support isn't entirely consistent.  We have to
3862          handle indirect support ourselves, but PC-relative is done
3863          in the assembler.  Further, the assembler can't handle any
3864          of the weirder relocation types.  */
3865       if (enc & DW_EH_PE_indirect)
3866         ref = dw2_force_const_mem (ref, true);
3867
3868       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3869       output_addr_const (asm_out_file, ref);
3870       fputc ('\n', asm_out_file);
3871     }
3872
3873   if (crtl->uses_eh_lsda)
3874     {
3875       char lab[20];
3876
3877       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3878       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3879                                    current_function_funcdef_no);
3880       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3881       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3882
3883       if (enc & DW_EH_PE_indirect)
3884         ref = dw2_force_const_mem (ref, true);
3885
3886       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3887       output_addr_const (asm_out_file, ref);
3888       fputc ('\n', asm_out_file);
3889     }
3890 }
3891
3892 /* Output a marker (i.e. a label) for the beginning of a function, before
3893    the prologue.  */
3894
3895 void
3896 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3897                           const char *file ATTRIBUTE_UNUSED)
3898 {
3899   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3900   char * dup_label;
3901   dw_fde_ref fde;
3902   section *fnsec;
3903
3904   current_function_func_begin_label = NULL;
3905
3906 #ifdef TARGET_UNWIND_INFO
3907   /* ??? current_function_func_begin_label is also used by except.c
3908      for call-site information.  We must emit this label if it might
3909      be used.  */
3910   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3911       && ! dwarf2out_do_frame ())
3912     return;
3913 #else
3914   if (! dwarf2out_do_frame ())
3915     return;
3916 #endif
3917
3918   fnsec = function_section (current_function_decl);
3919   switch_to_section (fnsec);
3920   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3921                                current_function_funcdef_no);
3922   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3923                           current_function_funcdef_no);
3924   dup_label = xstrdup (label);
3925   current_function_func_begin_label = dup_label;
3926
3927 #ifdef TARGET_UNWIND_INFO
3928   /* We can elide the fde allocation if we're not emitting debug info.  */
3929   if (! dwarf2out_do_frame ())
3930     return;
3931 #endif
3932
3933   /* Expand the fde table if necessary.  */
3934   if (fde_table_in_use == fde_table_allocated)
3935     {
3936       fde_table_allocated += FDE_TABLE_INCREMENT;
3937       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3938       memset (fde_table + fde_table_in_use, 0,
3939               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3940     }
3941
3942   /* Record the FDE associated with this function.  */
3943   current_funcdef_fde = fde_table_in_use;
3944
3945   /* Add the new FDE at the end of the fde_table.  */
3946   fde = &fde_table[fde_table_in_use++];
3947   fde->decl = current_function_decl;
3948   fde->dw_fde_begin = dup_label;
3949   fde->dw_fde_current_label = dup_label;
3950   fde->dw_fde_hot_section_label = NULL;
3951   fde->dw_fde_hot_section_end_label = NULL;
3952   fde->dw_fde_unlikely_section_label = NULL;
3953   fde->dw_fde_unlikely_section_end_label = NULL;
3954   fde->dw_fde_switched_sections = 0;
3955   fde->dw_fde_switched_cold_to_hot = 0;
3956   fde->dw_fde_end = NULL;
3957   fde->dw_fde_cfi = NULL;
3958   fde->dw_fde_switch_cfi = NULL;
3959   fde->funcdef_number = current_function_funcdef_no;
3960   fde->nothrow = crtl->nothrow;
3961   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3962   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3963   fde->drap_reg = INVALID_REGNUM;
3964   fde->vdrap_reg = INVALID_REGNUM;
3965   if (flag_reorder_blocks_and_partition)
3966     {
3967       section *unlikelysec;
3968       if (first_function_block_is_cold)
3969         fde->in_std_section = 1;
3970       else
3971         fde->in_std_section
3972           = (fnsec == text_section
3973              || (cold_text_section && fnsec == cold_text_section));
3974       unlikelysec = unlikely_text_section ();
3975       fde->cold_in_std_section
3976         = (unlikelysec == text_section
3977            || (cold_text_section && unlikelysec == cold_text_section));
3978     }
3979   else
3980     {
3981       fde->in_std_section
3982         = (fnsec == text_section
3983            || (cold_text_section && fnsec == cold_text_section));
3984       fde->cold_in_std_section = 0;
3985     }
3986
3987   args_size = old_args_size = 0;
3988
3989   /* We only want to output line number information for the genuine dwarf2
3990      prologue case, not the eh frame case.  */
3991 #ifdef DWARF2_DEBUGGING_INFO
3992   if (file)
3993     dwarf2out_source_line (line, file, 0, true);
3994 #endif
3995
3996   if (dwarf2out_do_cfi_asm ())
3997     dwarf2out_do_cfi_startproc (false);
3998   else
3999     {
4000       rtx personality = get_personality_function (current_function_decl);
4001       if (!current_unit_personality)
4002         current_unit_personality = personality;
4003
4004       /* We cannot keep a current personality per function as without CFI
4005          asm at the point where we emit the CFI data there is no current
4006          function anymore.  */
4007       if (personality
4008           && current_unit_personality != personality)
4009         sorry ("Multiple EH personalities are supported only with assemblers "
4010                "supporting .cfi.personality directive.");
4011     }
4012 }
4013
4014 /* Output a marker (i.e. a label) for the absolute end of the generated code
4015    for a function definition.  This gets called *after* the epilogue code has
4016    been generated.  */
4017
4018 void
4019 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4020                         const char *file ATTRIBUTE_UNUSED)
4021 {
4022   dw_fde_ref fde;
4023   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4024
4025 #ifdef DWARF2_DEBUGGING_INFO
4026   last_var_location_insn = NULL_RTX;
4027 #endif
4028
4029   if (dwarf2out_do_cfi_asm ())
4030     fprintf (asm_out_file, "\t.cfi_endproc\n");
4031
4032   /* Output a label to mark the endpoint of the code generated for this
4033      function.  */
4034   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4035                                current_function_funcdef_no);
4036   ASM_OUTPUT_LABEL (asm_out_file, label);
4037   fde = current_fde ();
4038   gcc_assert (fde != NULL);
4039   fde->dw_fde_end = xstrdup (label);
4040 }
4041
4042 void
4043 dwarf2out_frame_init (void)
4044 {
4045   /* Allocate the initial hunk of the fde_table.  */
4046   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4047   fde_table_allocated = FDE_TABLE_INCREMENT;
4048   fde_table_in_use = 0;
4049
4050   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4051      sake of lookup_cfa.  */
4052
4053   /* On entry, the Canonical Frame Address is at SP.  */
4054   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4055
4056 #ifdef DWARF2_UNWIND_INFO
4057   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4058     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4059 #endif
4060 }
4061
4062 void
4063 dwarf2out_frame_finish (void)
4064 {
4065   /* Output call frame information.  */
4066   if (DWARF2_FRAME_INFO)
4067     output_call_frame_info (0);
4068
4069 #ifndef TARGET_UNWIND_INFO
4070   /* Output another copy for the unwinder.  */
4071   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4072     output_call_frame_info (1);
4073 #endif
4074 }
4075
4076 /* Note that the current function section is being used for code.  */
4077
4078 static void
4079 dwarf2out_note_section_used (void)
4080 {
4081   section *sec = current_function_section ();
4082   if (sec == text_section)
4083     text_section_used = true;
4084   else if (sec == cold_text_section)
4085     cold_text_section_used = true;
4086 }
4087
4088 void
4089 dwarf2out_switch_text_section (void)
4090 {
4091   dw_fde_ref fde = current_fde ();
4092
4093   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4094
4095   fde->dw_fde_switched_sections = 1;
4096   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4097
4098   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4099   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4100   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4101   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4102   have_multiple_function_sections = true;
4103
4104   /* Reset the current label on switching text sections, so that we
4105      don't attempt to advance_loc4 between labels in different sections.  */
4106   fde->dw_fde_current_label = NULL;
4107
4108   /* There is no need to mark used sections when not debugging.  */
4109   if (cold_text_section != NULL)
4110     dwarf2out_note_section_used ();
4111
4112   if (dwarf2out_do_cfi_asm ())
4113     fprintf (asm_out_file, "\t.cfi_endproc\n");
4114
4115   /* Now do the real section switch.  */
4116   switch_to_section (current_function_section ());
4117
4118   if (dwarf2out_do_cfi_asm ())
4119     {
4120       dwarf2out_do_cfi_startproc (true);
4121       /* As this is a different FDE, insert all current CFI instructions
4122          again.  */
4123       output_cfis (fde->dw_fde_cfi, true, fde, true);
4124     }
4125   else
4126     {
4127       dw_cfi_ref cfi = fde->dw_fde_cfi;
4128
4129       cfi = fde->dw_fde_cfi;
4130       if (cfi)
4131         while (cfi->dw_cfi_next != NULL)
4132           cfi = cfi->dw_cfi_next;
4133       fde->dw_fde_switch_cfi = cfi;
4134     }
4135 }
4136 #endif
4137 \f
4138 /* And now, the subset of the debugging information support code necessary
4139    for emitting location expressions.  */
4140
4141 /* Data about a single source file.  */
4142 struct GTY(()) dwarf_file_data {
4143   const char * filename;
4144   int emitted_number;
4145 };
4146
4147 typedef struct dw_val_struct *dw_val_ref;
4148 typedef struct die_struct *dw_die_ref;
4149 typedef const struct die_struct *const_dw_die_ref;
4150 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4151 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4152
4153 typedef struct GTY(()) deferred_locations_struct
4154 {
4155   tree variable;
4156   dw_die_ref die;
4157 } deferred_locations;
4158
4159 DEF_VEC_O(deferred_locations);
4160 DEF_VEC_ALLOC_O(deferred_locations,gc);
4161
4162 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4163
4164 DEF_VEC_P(dw_die_ref);
4165 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4166
4167 /* Each DIE may have a series of attribute/value pairs.  Values
4168    can take on several forms.  The forms that are used in this
4169    implementation are listed below.  */
4170
4171 enum dw_val_class
4172 {
4173   dw_val_class_addr,
4174   dw_val_class_offset,
4175   dw_val_class_loc,
4176   dw_val_class_loc_list,
4177   dw_val_class_range_list,
4178   dw_val_class_const,
4179   dw_val_class_unsigned_const,
4180   dw_val_class_const_double,
4181   dw_val_class_vec,
4182   dw_val_class_flag,
4183   dw_val_class_die_ref,
4184   dw_val_class_fde_ref,
4185   dw_val_class_lbl_id,
4186   dw_val_class_lineptr,
4187   dw_val_class_str,
4188   dw_val_class_macptr,
4189   dw_val_class_file,
4190   dw_val_class_data8
4191 };
4192
4193 /* Describe a floating point constant value, or a vector constant value.  */
4194
4195 typedef struct GTY(()) dw_vec_struct {
4196   unsigned char * GTY((length ("%h.length"))) array;
4197   unsigned length;
4198   unsigned elt_size;
4199 }
4200 dw_vec_const;
4201
4202 /* The dw_val_node describes an attribute's value, as it is
4203    represented internally.  */
4204
4205 typedef struct GTY(()) dw_val_struct {
4206   enum dw_val_class val_class;
4207   union dw_val_struct_union
4208     {
4209       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4210       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4211       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4212       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4213       HOST_WIDE_INT GTY ((default)) val_int;
4214       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4215       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4216       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4217       struct dw_val_die_union
4218         {
4219           dw_die_ref die;
4220           int external;
4221         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4222       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4223       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4224       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4225       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4226       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4227       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4228     }
4229   GTY ((desc ("%1.val_class"))) v;
4230 }
4231 dw_val_node;
4232
4233 /* Locations in memory are described using a sequence of stack machine
4234    operations.  */
4235
4236 typedef struct GTY(()) dw_loc_descr_struct {
4237   dw_loc_descr_ref dw_loc_next;
4238   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4239   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4240      from DW_OP_addr with a dtp-relative symbol relocation.  */
4241   unsigned int dtprel : 1;
4242   int dw_loc_addr;
4243   dw_val_node dw_loc_oprnd1;
4244   dw_val_node dw_loc_oprnd2;
4245 }
4246 dw_loc_descr_node;
4247
4248 /* Location lists are ranges + location descriptions for that range,
4249    so you can track variables that are in different places over
4250    their entire life.  */
4251 typedef struct GTY(()) dw_loc_list_struct {
4252   dw_loc_list_ref dw_loc_next;
4253   const char *begin; /* Label for begin address of range */
4254   const char *end;  /* Label for end address of range */
4255   char *ll_symbol; /* Label for beginning of location list.
4256                       Only on head of list */
4257   const char *section; /* Section this loclist is relative to */
4258   dw_loc_descr_ref expr;
4259 } dw_loc_list_node;
4260
4261 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4262
4263 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4264
4265 /* Convert a DWARF stack opcode into its string name.  */
4266
4267 static const char *
4268 dwarf_stack_op_name (unsigned int op)
4269 {
4270   switch (op)
4271     {
4272     case DW_OP_addr:
4273       return "DW_OP_addr";
4274     case DW_OP_deref:
4275       return "DW_OP_deref";
4276     case DW_OP_const1u:
4277       return "DW_OP_const1u";
4278     case DW_OP_const1s:
4279       return "DW_OP_const1s";
4280     case DW_OP_const2u:
4281       return "DW_OP_const2u";
4282     case DW_OP_const2s:
4283       return "DW_OP_const2s";
4284     case DW_OP_const4u:
4285       return "DW_OP_const4u";
4286     case DW_OP_const4s:
4287       return "DW_OP_const4s";
4288     case DW_OP_const8u:
4289       return "DW_OP_const8u";
4290     case DW_OP_const8s:
4291       return "DW_OP_const8s";
4292     case DW_OP_constu:
4293       return "DW_OP_constu";
4294     case DW_OP_consts:
4295       return "DW_OP_consts";
4296     case DW_OP_dup:
4297       return "DW_OP_dup";
4298     case DW_OP_drop:
4299       return "DW_OP_drop";
4300     case DW_OP_over:
4301       return "DW_OP_over";
4302     case DW_OP_pick:
4303       return "DW_OP_pick";
4304     case DW_OP_swap:
4305       return "DW_OP_swap";
4306     case DW_OP_rot:
4307       return "DW_OP_rot";
4308     case DW_OP_xderef:
4309       return "DW_OP_xderef";
4310     case DW_OP_abs:
4311       return "DW_OP_abs";
4312     case DW_OP_and:
4313       return "DW_OP_and";
4314     case DW_OP_div:
4315       return "DW_OP_div";
4316     case DW_OP_minus:
4317       return "DW_OP_minus";
4318     case DW_OP_mod:
4319       return "DW_OP_mod";
4320     case DW_OP_mul:
4321       return "DW_OP_mul";
4322     case DW_OP_neg:
4323       return "DW_OP_neg";
4324     case DW_OP_not:
4325       return "DW_OP_not";
4326     case DW_OP_or:
4327       return "DW_OP_or";
4328     case DW_OP_plus:
4329       return "DW_OP_plus";
4330     case DW_OP_plus_uconst:
4331       return "DW_OP_plus_uconst";
4332     case DW_OP_shl:
4333       return "DW_OP_shl";
4334     case DW_OP_shr:
4335       return "DW_OP_shr";
4336     case DW_OP_shra:
4337       return "DW_OP_shra";
4338     case DW_OP_xor:
4339       return "DW_OP_xor";
4340     case DW_OP_bra:
4341       return "DW_OP_bra";
4342     case DW_OP_eq:
4343       return "DW_OP_eq";
4344     case DW_OP_ge:
4345       return "DW_OP_ge";
4346     case DW_OP_gt:
4347       return "DW_OP_gt";
4348     case DW_OP_le:
4349       return "DW_OP_le";
4350     case DW_OP_lt:
4351       return "DW_OP_lt";
4352     case DW_OP_ne:
4353       return "DW_OP_ne";
4354     case DW_OP_skip:
4355       return "DW_OP_skip";
4356     case DW_OP_lit0:
4357       return "DW_OP_lit0";
4358     case DW_OP_lit1:
4359       return "DW_OP_lit1";
4360     case DW_OP_lit2:
4361       return "DW_OP_lit2";
4362     case DW_OP_lit3:
4363       return "DW_OP_lit3";
4364     case DW_OP_lit4:
4365       return "DW_OP_lit4";
4366     case DW_OP_lit5:
4367       return "DW_OP_lit5";
4368     case DW_OP_lit6:
4369       return "DW_OP_lit6";
4370     case DW_OP_lit7:
4371       return "DW_OP_lit7";
4372     case DW_OP_lit8:
4373       return "DW_OP_lit8";
4374     case DW_OP_lit9:
4375       return "DW_OP_lit9";
4376     case DW_OP_lit10:
4377       return "DW_OP_lit10";
4378     case DW_OP_lit11:
4379       return "DW_OP_lit11";
4380     case DW_OP_lit12:
4381       return "DW_OP_lit12";
4382     case DW_OP_lit13:
4383       return "DW_OP_lit13";
4384     case DW_OP_lit14:
4385       return "DW_OP_lit14";
4386     case DW_OP_lit15:
4387       return "DW_OP_lit15";
4388     case DW_OP_lit16:
4389       return "DW_OP_lit16";
4390     case DW_OP_lit17:
4391       return "DW_OP_lit17";
4392     case DW_OP_lit18:
4393       return "DW_OP_lit18";
4394     case DW_OP_lit19:
4395       return "DW_OP_lit19";
4396     case DW_OP_lit20:
4397       return "DW_OP_lit20";
4398     case DW_OP_lit21:
4399       return "DW_OP_lit21";
4400     case DW_OP_lit22:
4401       return "DW_OP_lit22";
4402     case DW_OP_lit23:
4403       return "DW_OP_lit23";
4404     case DW_OP_lit24:
4405       return "DW_OP_lit24";
4406     case DW_OP_lit25:
4407       return "DW_OP_lit25";
4408     case DW_OP_lit26:
4409       return "DW_OP_lit26";
4410     case DW_OP_lit27:
4411       return "DW_OP_lit27";
4412     case DW_OP_lit28:
4413       return "DW_OP_lit28";
4414     case DW_OP_lit29:
4415       return "DW_OP_lit29";
4416     case DW_OP_lit30:
4417       return "DW_OP_lit30";
4418     case DW_OP_lit31:
4419       return "DW_OP_lit31";
4420     case DW_OP_reg0:
4421       return "DW_OP_reg0";
4422     case DW_OP_reg1:
4423       return "DW_OP_reg1";
4424     case DW_OP_reg2:
4425       return "DW_OP_reg2";
4426     case DW_OP_reg3:
4427       return "DW_OP_reg3";
4428     case DW_OP_reg4:
4429       return "DW_OP_reg4";
4430     case DW_OP_reg5:
4431       return "DW_OP_reg5";
4432     case DW_OP_reg6:
4433       return "DW_OP_reg6";
4434     case DW_OP_reg7:
4435       return "DW_OP_reg7";
4436     case DW_OP_reg8:
4437       return "DW_OP_reg8";
4438     case DW_OP_reg9:
4439       return "DW_OP_reg9";
4440     case DW_OP_reg10:
4441       return "DW_OP_reg10";
4442     case DW_OP_reg11:
4443       return "DW_OP_reg11";
4444     case DW_OP_reg12:
4445       return "DW_OP_reg12";
4446     case DW_OP_reg13:
4447       return "DW_OP_reg13";
4448     case DW_OP_reg14:
4449       return "DW_OP_reg14";
4450     case DW_OP_reg15:
4451       return "DW_OP_reg15";
4452     case DW_OP_reg16:
4453       return "DW_OP_reg16";
4454     case DW_OP_reg17:
4455       return "DW_OP_reg17";
4456     case DW_OP_reg18:
4457       return "DW_OP_reg18";
4458     case DW_OP_reg19:
4459       return "DW_OP_reg19";
4460     case DW_OP_reg20:
4461       return "DW_OP_reg20";
4462     case DW_OP_reg21:
4463       return "DW_OP_reg21";
4464     case DW_OP_reg22:
4465       return "DW_OP_reg22";
4466     case DW_OP_reg23:
4467       return "DW_OP_reg23";
4468     case DW_OP_reg24:
4469       return "DW_OP_reg24";
4470     case DW_OP_reg25:
4471       return "DW_OP_reg25";
4472     case DW_OP_reg26:
4473       return "DW_OP_reg26";
4474     case DW_OP_reg27:
4475       return "DW_OP_reg27";
4476     case DW_OP_reg28:
4477       return "DW_OP_reg28";
4478     case DW_OP_reg29:
4479       return "DW_OP_reg29";
4480     case DW_OP_reg30:
4481       return "DW_OP_reg30";
4482     case DW_OP_reg31:
4483       return "DW_OP_reg31";
4484     case DW_OP_breg0:
4485       return "DW_OP_breg0";
4486     case DW_OP_breg1:
4487       return "DW_OP_breg1";
4488     case DW_OP_breg2:
4489       return "DW_OP_breg2";
4490     case DW_OP_breg3:
4491       return "DW_OP_breg3";
4492     case DW_OP_breg4:
4493       return "DW_OP_breg4";
4494     case DW_OP_breg5:
4495       return "DW_OP_breg5";
4496     case DW_OP_breg6:
4497       return "DW_OP_breg6";
4498     case DW_OP_breg7:
4499       return "DW_OP_breg7";
4500     case DW_OP_breg8:
4501       return "DW_OP_breg8";
4502     case DW_OP_breg9:
4503       return "DW_OP_breg9";
4504     case DW_OP_breg10:
4505       return "DW_OP_breg10";
4506     case DW_OP_breg11:
4507       return "DW_OP_breg11";
4508     case DW_OP_breg12:
4509       return "DW_OP_breg12";
4510     case DW_OP_breg13:
4511       return "DW_OP_breg13";
4512     case DW_OP_breg14:
4513       return "DW_OP_breg14";
4514     case DW_OP_breg15:
4515       return "DW_OP_breg15";
4516     case DW_OP_breg16:
4517       return "DW_OP_breg16";
4518     case DW_OP_breg17:
4519       return "DW_OP_breg17";
4520     case DW_OP_breg18:
4521       return "DW_OP_breg18";
4522     case DW_OP_breg19:
4523       return "DW_OP_breg19";
4524     case DW_OP_breg20:
4525       return "DW_OP_breg20";
4526     case DW_OP_breg21:
4527       return "DW_OP_breg21";
4528     case DW_OP_breg22:
4529       return "DW_OP_breg22";
4530     case DW_OP_breg23:
4531       return "DW_OP_breg23";
4532     case DW_OP_breg24:
4533       return "DW_OP_breg24";
4534     case DW_OP_breg25:
4535       return "DW_OP_breg25";
4536     case DW_OP_breg26:
4537       return "DW_OP_breg26";
4538     case DW_OP_breg27:
4539       return "DW_OP_breg27";
4540     case DW_OP_breg28:
4541       return "DW_OP_breg28";
4542     case DW_OP_breg29:
4543       return "DW_OP_breg29";
4544     case DW_OP_breg30:
4545       return "DW_OP_breg30";
4546     case DW_OP_breg31:
4547       return "DW_OP_breg31";
4548     case DW_OP_regx:
4549       return "DW_OP_regx";
4550     case DW_OP_fbreg:
4551       return "DW_OP_fbreg";
4552     case DW_OP_bregx:
4553       return "DW_OP_bregx";
4554     case DW_OP_piece:
4555       return "DW_OP_piece";
4556     case DW_OP_deref_size:
4557       return "DW_OP_deref_size";
4558     case DW_OP_xderef_size:
4559       return "DW_OP_xderef_size";
4560     case DW_OP_nop:
4561       return "DW_OP_nop";
4562
4563     case DW_OP_push_object_address:
4564       return "DW_OP_push_object_address";
4565     case DW_OP_call2:
4566       return "DW_OP_call2";
4567     case DW_OP_call4:
4568       return "DW_OP_call4";
4569     case DW_OP_call_ref:
4570       return "DW_OP_call_ref";
4571     case DW_OP_implicit_value:
4572       return "DW_OP_implicit_value";
4573     case DW_OP_stack_value:
4574       return "DW_OP_stack_value";
4575     case DW_OP_form_tls_address:
4576       return "DW_OP_form_tls_address";
4577     case DW_OP_call_frame_cfa:
4578       return "DW_OP_call_frame_cfa";
4579     case DW_OP_bit_piece:
4580       return "DW_OP_bit_piece";
4581
4582     case DW_OP_GNU_push_tls_address:
4583       return "DW_OP_GNU_push_tls_address";
4584     case DW_OP_GNU_uninit:
4585       return "DW_OP_GNU_uninit";
4586     case DW_OP_GNU_encoded_addr:
4587       return "DW_OP_GNU_encoded_addr";
4588
4589     default:
4590       return "OP_<unknown>";
4591     }
4592 }
4593
4594 /* Return a pointer to a newly allocated location description.  Location
4595    descriptions are simple expression terms that can be strung
4596    together to form more complicated location (address) descriptions.  */
4597
4598 static inline dw_loc_descr_ref
4599 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4600                unsigned HOST_WIDE_INT oprnd2)
4601 {
4602   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4603
4604   descr->dw_loc_opc = op;
4605   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4606   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4607   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4608   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4609
4610   return descr;
4611 }
4612
4613 /* Return a pointer to a newly allocated location description for
4614    REG and OFFSET.  */
4615
4616 static inline dw_loc_descr_ref
4617 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4618 {
4619   if (reg <= 31)
4620     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4621                           offset, 0);
4622   else
4623     return new_loc_descr (DW_OP_bregx, reg, offset);
4624 }
4625
4626 /* Add a location description term to a location description expression.  */
4627
4628 static inline void
4629 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4630 {
4631   dw_loc_descr_ref *d;
4632
4633   /* Find the end of the chain.  */
4634   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4635     ;
4636
4637   *d = descr;
4638 }
4639
4640 /* Add a constant OFFSET to a location expression.  */
4641
4642 static void
4643 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4644 {
4645   dw_loc_descr_ref loc;
4646   HOST_WIDE_INT *p;
4647
4648   gcc_assert (*list_head != NULL);
4649
4650   if (!offset)
4651     return;
4652
4653   /* Find the end of the chain.  */
4654   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4655     ;
4656
4657   p = NULL;
4658   if (loc->dw_loc_opc == DW_OP_fbreg
4659       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4660     p = &loc->dw_loc_oprnd1.v.val_int;
4661   else if (loc->dw_loc_opc == DW_OP_bregx)
4662     p = &loc->dw_loc_oprnd2.v.val_int;
4663
4664   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4665      offset.  Don't optimize if an signed integer overflow would happen.  */
4666   if (p != NULL
4667       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4668           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4669     *p += offset;
4670
4671   else if (offset > 0)
4672     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4673
4674   else
4675     {
4676       loc->dw_loc_next = int_loc_descriptor (offset);
4677       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4678     }
4679 }
4680
4681 #ifdef DWARF2_DEBUGGING_INFO
4682 /* Add a constant OFFSET to a location list.  */
4683
4684 static void
4685 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4686 {
4687   dw_loc_list_ref d;
4688   for (d = list_head; d != NULL; d = d->dw_loc_next)
4689     loc_descr_plus_const (&d->expr, offset);
4690 }
4691 #endif
4692
4693 /* Return the size of a location descriptor.  */
4694
4695 static unsigned long
4696 size_of_loc_descr (dw_loc_descr_ref loc)
4697 {
4698   unsigned long size = 1;
4699
4700   switch (loc->dw_loc_opc)
4701     {
4702     case DW_OP_addr:
4703       size += DWARF2_ADDR_SIZE;
4704       break;
4705     case DW_OP_const1u:
4706     case DW_OP_const1s:
4707       size += 1;
4708       break;
4709     case DW_OP_const2u:
4710     case DW_OP_const2s:
4711       size += 2;
4712       break;
4713     case DW_OP_const4u:
4714     case DW_OP_const4s:
4715       size += 4;
4716       break;
4717     case DW_OP_const8u:
4718     case DW_OP_const8s:
4719       size += 8;
4720       break;
4721     case DW_OP_constu:
4722       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4723       break;
4724     case DW_OP_consts:
4725       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4726       break;
4727     case DW_OP_pick:
4728       size += 1;
4729       break;
4730     case DW_OP_plus_uconst:
4731       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4732       break;
4733     case DW_OP_skip:
4734     case DW_OP_bra:
4735       size += 2;
4736       break;
4737     case DW_OP_breg0:
4738     case DW_OP_breg1:
4739     case DW_OP_breg2:
4740     case DW_OP_breg3:
4741     case DW_OP_breg4:
4742     case DW_OP_breg5:
4743     case DW_OP_breg6:
4744     case DW_OP_breg7:
4745     case DW_OP_breg8:
4746     case DW_OP_breg9:
4747     case DW_OP_breg10:
4748     case DW_OP_breg11:
4749     case DW_OP_breg12:
4750     case DW_OP_breg13:
4751     case DW_OP_breg14:
4752     case DW_OP_breg15:
4753     case DW_OP_breg16:
4754     case DW_OP_breg17:
4755     case DW_OP_breg18:
4756     case DW_OP_breg19:
4757     case DW_OP_breg20:
4758     case DW_OP_breg21:
4759     case DW_OP_breg22:
4760     case DW_OP_breg23:
4761     case DW_OP_breg24:
4762     case DW_OP_breg25:
4763     case DW_OP_breg26:
4764     case DW_OP_breg27:
4765     case DW_OP_breg28:
4766     case DW_OP_breg29:
4767     case DW_OP_breg30:
4768     case DW_OP_breg31:
4769       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4770       break;
4771     case DW_OP_regx:
4772       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4773       break;
4774     case DW_OP_fbreg:
4775       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4776       break;
4777     case DW_OP_bregx:
4778       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4779       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4780       break;
4781     case DW_OP_piece:
4782       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4783       break;
4784     case DW_OP_deref_size:
4785     case DW_OP_xderef_size:
4786       size += 1;
4787       break;
4788     case DW_OP_call2:
4789       size += 2;
4790       break;
4791     case DW_OP_call4:
4792       size += 4;
4793       break;
4794     case DW_OP_call_ref:
4795       size += DWARF2_ADDR_SIZE;
4796       break;
4797     case DW_OP_implicit_value:
4798       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4799               + loc->dw_loc_oprnd1.v.val_unsigned;
4800       break;
4801     default:
4802       break;
4803     }
4804
4805   return size;
4806 }
4807
4808 /* Return the size of a series of location descriptors.  */
4809
4810 static unsigned long
4811 size_of_locs (dw_loc_descr_ref loc)
4812 {
4813   dw_loc_descr_ref l;
4814   unsigned long size;
4815
4816   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4817      field, to avoid writing to a PCH file.  */
4818   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4819     {
4820       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4821         break;
4822       size += size_of_loc_descr (l);
4823     }
4824   if (! l)
4825     return size;
4826
4827   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4828     {
4829       l->dw_loc_addr = size;
4830       size += size_of_loc_descr (l);
4831     }
4832
4833   return size;
4834 }
4835
4836 #ifdef DWARF2_DEBUGGING_INFO
4837 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4838 #endif
4839
4840 /* Output location description stack opcode's operands (if any).  */
4841
4842 static void
4843 output_loc_operands (dw_loc_descr_ref loc)
4844 {
4845   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4846   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4847
4848   switch (loc->dw_loc_opc)
4849     {
4850 #ifdef DWARF2_DEBUGGING_INFO
4851     case DW_OP_const2u:
4852     case DW_OP_const2s:
4853       dw2_asm_output_data (2, val1->v.val_int, NULL);
4854       break;
4855     case DW_OP_const4u:
4856     case DW_OP_const4s:
4857       dw2_asm_output_data (4, val1->v.val_int, NULL);
4858       break;
4859     case DW_OP_const8u:
4860     case DW_OP_const8s:
4861       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4862       dw2_asm_output_data (8, val1->v.val_int, NULL);
4863       break;
4864     case DW_OP_skip:
4865     case DW_OP_bra:
4866       {
4867         int offset;
4868
4869         gcc_assert (val1->val_class == dw_val_class_loc);
4870         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4871
4872         dw2_asm_output_data (2, offset, NULL);
4873       }
4874       break;
4875     case DW_OP_implicit_value:
4876       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4877       switch (val2->val_class)
4878         {
4879         case dw_val_class_const:
4880           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4881           break;
4882         case dw_val_class_vec:
4883           {
4884             unsigned int elt_size = val2->v.val_vec.elt_size;
4885             unsigned int len = val2->v.val_vec.length;
4886             unsigned int i;
4887             unsigned char *p;
4888
4889             if (elt_size > sizeof (HOST_WIDE_INT))
4890               {
4891                 elt_size /= 2;
4892                 len *= 2;
4893               }
4894             for (i = 0, p = val2->v.val_vec.array;
4895                  i < len;
4896                  i++, p += elt_size)
4897               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4898                                    "fp or vector constant word %u", i);
4899           }
4900           break;
4901         case dw_val_class_const_double:
4902           {
4903             unsigned HOST_WIDE_INT first, second;
4904
4905             if (WORDS_BIG_ENDIAN)
4906               {
4907                 first = val2->v.val_double.high;
4908                 second = val2->v.val_double.low;
4909               }
4910             else
4911               {
4912                 first = val2->v.val_double.low;
4913                 second = val2->v.val_double.high;
4914               }
4915             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4916                                  first, NULL);
4917             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4918                                  second, NULL);
4919           }
4920           break;
4921         case dw_val_class_addr:
4922           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4923           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4924           break;
4925         default:
4926           gcc_unreachable ();
4927         }
4928       break;
4929 #else
4930     case DW_OP_const2u:
4931     case DW_OP_const2s:
4932     case DW_OP_const4u:
4933     case DW_OP_const4s:
4934     case DW_OP_const8u:
4935     case DW_OP_const8s:
4936     case DW_OP_skip:
4937     case DW_OP_bra:
4938     case DW_OP_implicit_value:
4939       /* We currently don't make any attempt to make sure these are
4940          aligned properly like we do for the main unwind info, so
4941          don't support emitting things larger than a byte if we're
4942          only doing unwinding.  */
4943       gcc_unreachable ();
4944 #endif
4945     case DW_OP_const1u:
4946     case DW_OP_const1s:
4947       dw2_asm_output_data (1, val1->v.val_int, NULL);
4948       break;
4949     case DW_OP_constu:
4950       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4951       break;
4952     case DW_OP_consts:
4953       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4954       break;
4955     case DW_OP_pick:
4956       dw2_asm_output_data (1, val1->v.val_int, NULL);
4957       break;
4958     case DW_OP_plus_uconst:
4959       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4960       break;
4961     case DW_OP_breg0:
4962     case DW_OP_breg1:
4963     case DW_OP_breg2:
4964     case DW_OP_breg3:
4965     case DW_OP_breg4:
4966     case DW_OP_breg5:
4967     case DW_OP_breg6:
4968     case DW_OP_breg7:
4969     case DW_OP_breg8:
4970     case DW_OP_breg9:
4971     case DW_OP_breg10:
4972     case DW_OP_breg11:
4973     case DW_OP_breg12:
4974     case DW_OP_breg13:
4975     case DW_OP_breg14:
4976     case DW_OP_breg15:
4977     case DW_OP_breg16:
4978     case DW_OP_breg17:
4979     case DW_OP_breg18:
4980     case DW_OP_breg19:
4981     case DW_OP_breg20:
4982     case DW_OP_breg21:
4983     case DW_OP_breg22:
4984     case DW_OP_breg23:
4985     case DW_OP_breg24:
4986     case DW_OP_breg25:
4987     case DW_OP_breg26:
4988     case DW_OP_breg27:
4989     case DW_OP_breg28:
4990     case DW_OP_breg29:
4991     case DW_OP_breg30:
4992     case DW_OP_breg31:
4993       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4994       break;
4995     case DW_OP_regx:
4996       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4997       break;
4998     case DW_OP_fbreg:
4999       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5000       break;
5001     case DW_OP_bregx:
5002       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5003       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5004       break;
5005     case DW_OP_piece:
5006       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5007       break;
5008     case DW_OP_deref_size:
5009     case DW_OP_xderef_size:
5010       dw2_asm_output_data (1, val1->v.val_int, NULL);
5011       break;
5012
5013     case DW_OP_addr:
5014       if (loc->dtprel)
5015         {
5016           if (targetm.asm_out.output_dwarf_dtprel)
5017             {
5018               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5019                                                    DWARF2_ADDR_SIZE,
5020                                                    val1->v.val_addr);
5021               fputc ('\n', asm_out_file);
5022             }
5023           else
5024             gcc_unreachable ();
5025         }
5026       else
5027         {
5028 #ifdef DWARF2_DEBUGGING_INFO
5029           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5030 #else
5031           gcc_unreachable ();
5032 #endif
5033         }
5034       break;
5035
5036     default:
5037       /* Other codes have no operands.  */
5038       break;
5039     }
5040 }
5041
5042 /* Output a sequence of location operations.  */
5043
5044 static void
5045 output_loc_sequence (dw_loc_descr_ref loc)
5046 {
5047   for (; loc != NULL; loc = loc->dw_loc_next)
5048     {
5049       /* Output the opcode.  */
5050       dw2_asm_output_data (1, loc->dw_loc_opc,
5051                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5052
5053       /* Output the operand(s) (if any).  */
5054       output_loc_operands (loc);
5055     }
5056 }
5057
5058 /* Output location description stack opcode's operands (if any).
5059    The output is single bytes on a line, suitable for .cfi_escape.  */
5060
5061 static void
5062 output_loc_operands_raw (dw_loc_descr_ref loc)
5063 {
5064   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5065   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5066
5067   switch (loc->dw_loc_opc)
5068     {
5069     case DW_OP_addr:
5070     case DW_OP_implicit_value:
5071       /* We cannot output addresses in .cfi_escape, only bytes.  */
5072       gcc_unreachable ();
5073
5074     case DW_OP_const1u:
5075     case DW_OP_const1s:
5076     case DW_OP_pick:
5077     case DW_OP_deref_size:
5078     case DW_OP_xderef_size:
5079       fputc (',', asm_out_file);
5080       dw2_asm_output_data_raw (1, val1->v.val_int);
5081       break;
5082
5083     case DW_OP_const2u:
5084     case DW_OP_const2s:
5085       fputc (',', asm_out_file);
5086       dw2_asm_output_data_raw (2, val1->v.val_int);
5087       break;
5088
5089     case DW_OP_const4u:
5090     case DW_OP_const4s:
5091       fputc (',', asm_out_file);
5092       dw2_asm_output_data_raw (4, val1->v.val_int);
5093       break;
5094
5095     case DW_OP_const8u:
5096     case DW_OP_const8s:
5097       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5098       fputc (',', asm_out_file);
5099       dw2_asm_output_data_raw (8, val1->v.val_int);
5100       break;
5101
5102     case DW_OP_skip:
5103     case DW_OP_bra:
5104       {
5105         int offset;
5106
5107         gcc_assert (val1->val_class == dw_val_class_loc);
5108         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5109
5110         fputc (',', asm_out_file);
5111         dw2_asm_output_data_raw (2, offset);
5112       }
5113       break;
5114
5115     case DW_OP_constu:
5116     case DW_OP_plus_uconst:
5117     case DW_OP_regx:
5118     case DW_OP_piece:
5119       fputc (',', asm_out_file);
5120       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5121       break;
5122
5123     case DW_OP_consts:
5124     case DW_OP_breg0:
5125     case DW_OP_breg1:
5126     case DW_OP_breg2:
5127     case DW_OP_breg3:
5128     case DW_OP_breg4:
5129     case DW_OP_breg5:
5130     case DW_OP_breg6:
5131     case DW_OP_breg7:
5132     case DW_OP_breg8:
5133     case DW_OP_breg9:
5134     case DW_OP_breg10:
5135     case DW_OP_breg11:
5136     case DW_OP_breg12:
5137     case DW_OP_breg13:
5138     case DW_OP_breg14:
5139     case DW_OP_breg15:
5140     case DW_OP_breg16:
5141     case DW_OP_breg17:
5142     case DW_OP_breg18:
5143     case DW_OP_breg19:
5144     case DW_OP_breg20:
5145     case DW_OP_breg21:
5146     case DW_OP_breg22:
5147     case DW_OP_breg23:
5148     case DW_OP_breg24:
5149     case DW_OP_breg25:
5150     case DW_OP_breg26:
5151     case DW_OP_breg27:
5152     case DW_OP_breg28:
5153     case DW_OP_breg29:
5154     case DW_OP_breg30:
5155     case DW_OP_breg31:
5156     case DW_OP_fbreg:
5157       fputc (',', asm_out_file);
5158       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5159       break;
5160
5161     case DW_OP_bregx:
5162       fputc (',', asm_out_file);
5163       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5164       fputc (',', asm_out_file);
5165       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5166       break;
5167
5168     default:
5169       /* Other codes have no operands.  */
5170       break;
5171     }
5172 }
5173
5174 static void
5175 output_loc_sequence_raw (dw_loc_descr_ref loc)
5176 {
5177   while (1)
5178     {
5179       /* Output the opcode.  */
5180       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5181       output_loc_operands_raw (loc);
5182
5183       if (!loc->dw_loc_next)
5184         break;
5185       loc = loc->dw_loc_next;
5186
5187       fputc (',', asm_out_file);
5188     }
5189 }
5190
5191 /* This routine will generate the correct assembly data for a location
5192    description based on a cfi entry with a complex address.  */
5193
5194 static void
5195 output_cfa_loc (dw_cfi_ref cfi)
5196 {
5197   dw_loc_descr_ref loc;
5198   unsigned long size;
5199
5200   if (cfi->dw_cfi_opc == DW_CFA_expression)
5201     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5202
5203   /* Output the size of the block.  */
5204   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5205   size = size_of_locs (loc);
5206   dw2_asm_output_data_uleb128 (size, NULL);
5207
5208   /* Now output the operations themselves.  */
5209   output_loc_sequence (loc);
5210 }
5211
5212 /* Similar, but used for .cfi_escape.  */
5213
5214 static void
5215 output_cfa_loc_raw (dw_cfi_ref cfi)
5216 {
5217   dw_loc_descr_ref loc;
5218   unsigned long size;
5219
5220   if (cfi->dw_cfi_opc == DW_CFA_expression)
5221     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5222
5223   /* Output the size of the block.  */
5224   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5225   size = size_of_locs (loc);
5226   dw2_asm_output_data_uleb128_raw (size);
5227   fputc (',', asm_out_file);
5228
5229   /* Now output the operations themselves.  */
5230   output_loc_sequence_raw (loc);
5231 }
5232
5233 /* This function builds a dwarf location descriptor sequence from a
5234    dw_cfa_location, adding the given OFFSET to the result of the
5235    expression.  */
5236
5237 static struct dw_loc_descr_struct *
5238 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5239 {
5240   struct dw_loc_descr_struct *head, *tmp;
5241
5242   offset += cfa->offset;
5243
5244   if (cfa->indirect)
5245     {
5246       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5247       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5248       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5249       add_loc_descr (&head, tmp);
5250       if (offset != 0)
5251         {
5252           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5253           add_loc_descr (&head, tmp);
5254         }
5255     }
5256   else
5257     head = new_reg_loc_descr (cfa->reg, offset);
5258
5259   return head;
5260 }
5261
5262 /* This function builds a dwarf location descriptor sequence for
5263    the address at OFFSET from the CFA when stack is aligned to
5264    ALIGNMENT byte.  */
5265
5266 static struct dw_loc_descr_struct *
5267 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5268 {
5269   struct dw_loc_descr_struct *head;
5270   unsigned int dwarf_fp
5271     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5272
5273  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5274   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5275     {
5276       head = new_reg_loc_descr (dwarf_fp, 0);
5277       add_loc_descr (&head, int_loc_descriptor (alignment));
5278       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5279       loc_descr_plus_const (&head, offset);
5280     }
5281   else
5282     head = new_reg_loc_descr (dwarf_fp, offset);
5283   return head;
5284 }
5285
5286 /* This function fills in aa dw_cfa_location structure from a dwarf location
5287    descriptor sequence.  */
5288
5289 static void
5290 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5291 {
5292   struct dw_loc_descr_struct *ptr;
5293   cfa->offset = 0;
5294   cfa->base_offset = 0;
5295   cfa->indirect = 0;
5296   cfa->reg = -1;
5297
5298   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5299     {
5300       enum dwarf_location_atom op = ptr->dw_loc_opc;
5301
5302       switch (op)
5303         {
5304         case DW_OP_reg0:
5305         case DW_OP_reg1:
5306         case DW_OP_reg2:
5307         case DW_OP_reg3:
5308         case DW_OP_reg4:
5309         case DW_OP_reg5:
5310         case DW_OP_reg6:
5311         case DW_OP_reg7:
5312         case DW_OP_reg8:
5313         case DW_OP_reg9:
5314         case DW_OP_reg10:
5315         case DW_OP_reg11:
5316         case DW_OP_reg12:
5317         case DW_OP_reg13:
5318         case DW_OP_reg14:
5319         case DW_OP_reg15:
5320         case DW_OP_reg16:
5321         case DW_OP_reg17:
5322         case DW_OP_reg18:
5323         case DW_OP_reg19:
5324         case DW_OP_reg20:
5325         case DW_OP_reg21:
5326         case DW_OP_reg22:
5327         case DW_OP_reg23:
5328         case DW_OP_reg24:
5329         case DW_OP_reg25:
5330         case DW_OP_reg26:
5331         case DW_OP_reg27:
5332         case DW_OP_reg28:
5333         case DW_OP_reg29:
5334         case DW_OP_reg30:
5335         case DW_OP_reg31:
5336           cfa->reg = op - DW_OP_reg0;
5337           break;
5338         case DW_OP_regx:
5339           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5340           break;
5341         case DW_OP_breg0:
5342         case DW_OP_breg1:
5343         case DW_OP_breg2:
5344         case DW_OP_breg3:
5345         case DW_OP_breg4:
5346         case DW_OP_breg5:
5347         case DW_OP_breg6:
5348         case DW_OP_breg7:
5349         case DW_OP_breg8:
5350         case DW_OP_breg9:
5351         case DW_OP_breg10:
5352         case DW_OP_breg11:
5353         case DW_OP_breg12:
5354         case DW_OP_breg13:
5355         case DW_OP_breg14:
5356         case DW_OP_breg15:
5357         case DW_OP_breg16:
5358         case DW_OP_breg17:
5359         case DW_OP_breg18:
5360         case DW_OP_breg19:
5361         case DW_OP_breg20:
5362         case DW_OP_breg21:
5363         case DW_OP_breg22:
5364         case DW_OP_breg23:
5365         case DW_OP_breg24:
5366         case DW_OP_breg25:
5367         case DW_OP_breg26:
5368         case DW_OP_breg27:
5369         case DW_OP_breg28:
5370         case DW_OP_breg29:
5371         case DW_OP_breg30:
5372         case DW_OP_breg31:
5373           cfa->reg = op - DW_OP_breg0;
5374           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5375           break;
5376         case DW_OP_bregx:
5377           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5378           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5379           break;
5380         case DW_OP_deref:
5381           cfa->indirect = 1;
5382           break;
5383         case DW_OP_plus_uconst:
5384           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5385           break;
5386         default:
5387           internal_error ("DW_LOC_OP %s not implemented",
5388                           dwarf_stack_op_name (ptr->dw_loc_opc));
5389         }
5390     }
5391 }
5392 #endif /* .debug_frame support */
5393 \f
5394 /* And now, the support for symbolic debugging information.  */
5395 #ifdef DWARF2_DEBUGGING_INFO
5396
5397 /* .debug_str support.  */
5398 static int output_indirect_string (void **, void *);
5399
5400 static void dwarf2out_init (const char *);
5401 static void dwarf2out_finish (const char *);
5402 static void dwarf2out_assembly_start (void);
5403 static void dwarf2out_define (unsigned int, const char *);
5404 static void dwarf2out_undef (unsigned int, const char *);
5405 static void dwarf2out_start_source_file (unsigned, const char *);
5406 static void dwarf2out_end_source_file (unsigned);
5407 static void dwarf2out_begin_block (unsigned, unsigned);
5408 static void dwarf2out_end_block (unsigned, unsigned);
5409 static bool dwarf2out_ignore_block (const_tree);
5410 static void dwarf2out_global_decl (tree);
5411 static void dwarf2out_type_decl (tree, int);
5412 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5413 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5414                                                  dw_die_ref);
5415 static void dwarf2out_abstract_function (tree);
5416 static void dwarf2out_var_location (rtx);
5417 static void dwarf2out_direct_call (tree);
5418 static void dwarf2out_virtual_call_token (tree, int);
5419 static void dwarf2out_copy_call_info (rtx, rtx);
5420 static void dwarf2out_virtual_call (int);
5421 static void dwarf2out_begin_function (tree);
5422 static void dwarf2out_set_name (tree, tree);
5423
5424 /* The debug hooks structure.  */
5425
5426 const struct gcc_debug_hooks dwarf2_debug_hooks =
5427 {
5428   dwarf2out_init,
5429   dwarf2out_finish,
5430   dwarf2out_assembly_start,
5431   dwarf2out_define,
5432   dwarf2out_undef,
5433   dwarf2out_start_source_file,
5434   dwarf2out_end_source_file,
5435   dwarf2out_begin_block,
5436   dwarf2out_end_block,
5437   dwarf2out_ignore_block,
5438   dwarf2out_source_line,
5439   dwarf2out_begin_prologue,
5440   debug_nothing_int_charstar,   /* end_prologue */
5441   dwarf2out_end_epilogue,
5442   dwarf2out_begin_function,
5443   debug_nothing_int,            /* end_function */
5444   dwarf2out_decl,               /* function_decl */
5445   dwarf2out_global_decl,
5446   dwarf2out_type_decl,          /* type_decl */
5447   dwarf2out_imported_module_or_decl,
5448   debug_nothing_tree,           /* deferred_inline_function */
5449   /* The DWARF 2 backend tries to reduce debugging bloat by not
5450      emitting the abstract description of inline functions until
5451      something tries to reference them.  */
5452   dwarf2out_abstract_function,  /* outlining_inline_function */
5453   debug_nothing_rtx,            /* label */
5454   debug_nothing_int,            /* handle_pch */
5455   dwarf2out_var_location,
5456   dwarf2out_switch_text_section,
5457   dwarf2out_direct_call,
5458   dwarf2out_virtual_call_token,
5459   dwarf2out_copy_call_info,
5460   dwarf2out_virtual_call,
5461   dwarf2out_set_name,
5462   1                             /* start_end_main_source_file */
5463 };
5464 #endif
5465 \f
5466 /* NOTE: In the comments in this file, many references are made to
5467    "Debugging Information Entries".  This term is abbreviated as `DIE'
5468    throughout the remainder of this file.  */
5469
5470 /* An internal representation of the DWARF output is built, and then
5471    walked to generate the DWARF debugging info.  The walk of the internal
5472    representation is done after the entire program has been compiled.
5473    The types below are used to describe the internal representation.  */
5474
5475 /* Various DIE's use offsets relative to the beginning of the
5476    .debug_info section to refer to each other.  */
5477
5478 typedef long int dw_offset;
5479
5480 /* Define typedefs here to avoid circular dependencies.  */
5481
5482 typedef struct dw_attr_struct *dw_attr_ref;
5483 typedef struct dw_line_info_struct *dw_line_info_ref;
5484 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5485 typedef struct pubname_struct *pubname_ref;
5486 typedef struct dw_ranges_struct *dw_ranges_ref;
5487 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5488 typedef struct comdat_type_struct *comdat_type_node_ref;
5489
5490 /* Each entry in the line_info_table maintains the file and
5491    line number associated with the label generated for that
5492    entry.  The label gives the PC value associated with
5493    the line number entry.  */
5494
5495 typedef struct GTY(()) dw_line_info_struct {
5496   unsigned long dw_file_num;
5497   unsigned long dw_line_num;
5498 }
5499 dw_line_info_entry;
5500
5501 /* Line information for functions in separate sections; each one gets its
5502    own sequence.  */
5503 typedef struct GTY(()) dw_separate_line_info_struct {
5504   unsigned long dw_file_num;
5505   unsigned long dw_line_num;
5506   unsigned long function;
5507 }
5508 dw_separate_line_info_entry;
5509
5510 /* Each DIE attribute has a field specifying the attribute kind,
5511    a link to the next attribute in the chain, and an attribute value.
5512    Attributes are typically linked below the DIE they modify.  */
5513
5514 typedef struct GTY(()) dw_attr_struct {
5515   enum dwarf_attribute dw_attr;
5516   dw_val_node dw_attr_val;
5517 }
5518 dw_attr_node;
5519
5520 DEF_VEC_O(dw_attr_node);
5521 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5522
5523 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5524    The children of each node form a circular list linked by
5525    die_sib.  die_child points to the node *before* the "first" child node.  */
5526
5527 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5528   enum dwarf_tag die_tag;
5529   union die_symbol_or_type_node
5530     {
5531       char * GTY ((tag ("0"))) die_symbol;
5532       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5533     }
5534   GTY ((desc ("dwarf_version >= 4"))) die_id;
5535   VEC(dw_attr_node,gc) * die_attr;
5536   dw_die_ref die_parent;
5537   dw_die_ref die_child;
5538   dw_die_ref die_sib;
5539   dw_die_ref die_definition; /* ref from a specification to its definition */
5540   dw_offset die_offset;
5541   unsigned long die_abbrev;
5542   int die_mark;
5543   /* Die is used and must not be pruned as unused.  */
5544   int die_perennial_p;
5545   unsigned int decl_id;
5546 }
5547 die_node;
5548
5549 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5550 #define FOR_EACH_CHILD(die, c, expr) do {       \
5551   c = die->die_child;                           \
5552   if (c) do {                                   \
5553     c = c->die_sib;                             \
5554     expr;                                       \
5555   } while (c != die->die_child);                \
5556 } while (0)
5557
5558 /* The pubname structure */
5559
5560 typedef struct GTY(()) pubname_struct {
5561   dw_die_ref die;
5562   const char *name;
5563 }
5564 pubname_entry;
5565
5566 DEF_VEC_O(pubname_entry);
5567 DEF_VEC_ALLOC_O(pubname_entry, gc);
5568
5569 struct GTY(()) dw_ranges_struct {
5570   /* If this is positive, it's a block number, otherwise it's a
5571      bitwise-negated index into dw_ranges_by_label.  */
5572   int num;
5573 };
5574
5575 struct GTY(()) dw_ranges_by_label_struct {
5576   const char *begin;
5577   const char *end;
5578 };
5579
5580 /* The comdat type node structure.  */
5581 typedef struct GTY(()) comdat_type_struct
5582 {
5583   dw_die_ref root_die;
5584   dw_die_ref type_die;
5585   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5586   struct comdat_type_struct *next;
5587 }
5588 comdat_type_node;
5589
5590 /* The limbo die list structure.  */
5591 typedef struct GTY(()) limbo_die_struct {
5592   dw_die_ref die;
5593   tree created_for;
5594   struct limbo_die_struct *next;
5595 }
5596 limbo_die_node;
5597
5598 typedef struct GTY(()) skeleton_chain_struct
5599 {
5600   dw_die_ref old_die;
5601   dw_die_ref new_die;
5602   struct skeleton_chain_struct *parent;
5603 }
5604 skeleton_chain_node;
5605
5606 /* How to start an assembler comment.  */
5607 #ifndef ASM_COMMENT_START
5608 #define ASM_COMMENT_START ";#"
5609 #endif
5610
5611 /* Define a macro which returns nonzero for a TYPE_DECL which was
5612    implicitly generated for a tagged type.
5613
5614    Note that unlike the gcc front end (which generates a NULL named
5615    TYPE_DECL node for each complete tagged type, each array type, and
5616    each function type node created) the g++ front end generates a
5617    _named_ TYPE_DECL node for each tagged type node created.
5618    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5619    generate a DW_TAG_typedef DIE for them.  */
5620
5621 #define TYPE_DECL_IS_STUB(decl)                         \
5622   (DECL_NAME (decl) == NULL_TREE                        \
5623    || (DECL_ARTIFICIAL (decl)                           \
5624        && is_tagged_type (TREE_TYPE (decl))             \
5625        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5626            /* This is necessary for stub decls that     \
5627               appear in nested inline functions.  */    \
5628            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5629                && (decl_ultimate_origin (decl)          \
5630                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5631
5632 /* Information concerning the compilation unit's programming
5633    language, and compiler version.  */
5634
5635 /* Fixed size portion of the DWARF compilation unit header.  */
5636 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5637   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5638
5639 /* Fixed size portion of the DWARF comdat type unit header.  */
5640 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5641   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5642    + DWARF_OFFSET_SIZE)
5643
5644 /* Fixed size portion of public names info.  */
5645 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5646
5647 /* Fixed size portion of the address range info.  */
5648 #define DWARF_ARANGES_HEADER_SIZE                                       \
5649   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5650                 DWARF2_ADDR_SIZE * 2)                                   \
5651    - DWARF_INITIAL_LENGTH_SIZE)
5652
5653 /* Size of padding portion in the address range info.  It must be
5654    aligned to twice the pointer size.  */
5655 #define DWARF_ARANGES_PAD_SIZE \
5656   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5657                 DWARF2_ADDR_SIZE * 2)                              \
5658    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5659
5660 /* Use assembler line directives if available.  */
5661 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5662 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5663 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5664 #else
5665 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5666 #endif
5667 #endif
5668
5669 /* Minimum line offset in a special line info. opcode.
5670    This value was chosen to give a reasonable range of values.  */
5671 #define DWARF_LINE_BASE  -10
5672
5673 /* First special line opcode - leave room for the standard opcodes.  */
5674 #define DWARF_LINE_OPCODE_BASE  10
5675
5676 /* Range of line offsets in a special line info. opcode.  */
5677 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5678
5679 /* Flag that indicates the initial value of the is_stmt_start flag.
5680    In the present implementation, we do not mark any lines as
5681    the beginning of a source statement, because that information
5682    is not made available by the GCC front-end.  */
5683 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5684
5685 #ifdef DWARF2_DEBUGGING_INFO
5686 /* This location is used by calc_die_sizes() to keep track
5687    the offset of each DIE within the .debug_info section.  */
5688 static unsigned long next_die_offset;
5689 #endif
5690
5691 /* Record the root of the DIE's built for the current compilation unit.  */
5692 static GTY(()) dw_die_ref comp_unit_die;
5693
5694 /* A list of type DIEs that have been separated into comdat sections.  */
5695 static GTY(()) comdat_type_node *comdat_type_list;
5696
5697 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5698 static GTY(()) limbo_die_node *limbo_die_list;
5699
5700 /* A list of DIEs for which we may have to generate
5701    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5702    set.  */
5703 static GTY(()) limbo_die_node *deferred_asm_name;
5704
5705 /* Filenames referenced by this compilation unit.  */
5706 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5707
5708 /* A hash table of references to DIE's that describe declarations.
5709    The key is a DECL_UID() which is a unique number identifying each decl.  */
5710 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5711
5712 /* A hash table of references to DIE's that describe COMMON blocks.
5713    The key is DECL_UID() ^ die_parent.  */
5714 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5715
5716 typedef struct GTY(()) die_arg_entry_struct {
5717     dw_die_ref die;
5718     tree arg;
5719 } die_arg_entry;
5720
5721 DEF_VEC_O(die_arg_entry);
5722 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5723
5724 /* Node of the variable location list.  */
5725 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5726   rtx GTY (()) var_loc_note;
5727   const char * GTY (()) label;
5728   const char * GTY (()) section_label;
5729   struct var_loc_node * GTY (()) next;
5730 };
5731
5732 /* Variable location list.  */
5733 struct GTY (()) var_loc_list_def {
5734   struct var_loc_node * GTY (()) first;
5735
5736   /* Do not mark the last element of the chained list because
5737      it is marked through the chain.  */
5738   struct var_loc_node * GTY ((skip ("%h"))) last;
5739
5740   /* DECL_UID of the variable decl.  */
5741   unsigned int decl_id;
5742 };
5743 typedef struct var_loc_list_def var_loc_list;
5744
5745
5746 /* Table of decl location linked lists.  */
5747 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5748
5749 /* A pointer to the base of a list of references to DIE's that
5750    are uniquely identified by their tag, presence/absence of
5751    children DIE's, and list of attribute/value pairs.  */
5752 static GTY((length ("abbrev_die_table_allocated")))
5753   dw_die_ref *abbrev_die_table;
5754
5755 /* Number of elements currently allocated for abbrev_die_table.  */
5756 static GTY(()) unsigned abbrev_die_table_allocated;
5757
5758 /* Number of elements in type_die_table currently in use.  */
5759 static GTY(()) unsigned abbrev_die_table_in_use;
5760
5761 /* Size (in elements) of increments by which we may expand the
5762    abbrev_die_table.  */
5763 #define ABBREV_DIE_TABLE_INCREMENT 256
5764
5765 /* A pointer to the base of a table that contains line information
5766    for each source code line in .text in the compilation unit.  */
5767 static GTY((length ("line_info_table_allocated")))
5768      dw_line_info_ref line_info_table;
5769
5770 /* Number of elements currently allocated for line_info_table.  */
5771 static GTY(()) unsigned line_info_table_allocated;
5772
5773 /* Number of elements in line_info_table currently in use.  */
5774 static GTY(()) unsigned line_info_table_in_use;
5775
5776 /* A pointer to the base of a table that contains line information
5777    for each source code line outside of .text in the compilation unit.  */
5778 static GTY ((length ("separate_line_info_table_allocated")))
5779      dw_separate_line_info_ref separate_line_info_table;
5780
5781 /* Number of elements currently allocated for separate_line_info_table.  */
5782 static GTY(()) unsigned separate_line_info_table_allocated;
5783
5784 /* Number of elements in separate_line_info_table currently in use.  */
5785 static GTY(()) unsigned separate_line_info_table_in_use;
5786
5787 /* Size (in elements) of increments by which we may expand the
5788    line_info_table.  */
5789 #define LINE_INFO_TABLE_INCREMENT 1024
5790
5791 /* A pointer to the base of a table that contains a list of publicly
5792    accessible names.  */
5793 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5794
5795 /* A pointer to the base of a table that contains a list of publicly
5796    accessible types.  */
5797 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5798
5799 /* Array of dies for which we should generate .debug_arange info.  */
5800 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5801
5802 /* Number of elements currently allocated for arange_table.  */
5803 static GTY(()) unsigned arange_table_allocated;
5804
5805 /* Number of elements in arange_table currently in use.  */
5806 static GTY(()) unsigned arange_table_in_use;
5807
5808 /* Size (in elements) of increments by which we may expand the
5809    arange_table.  */
5810 #define ARANGE_TABLE_INCREMENT 64
5811
5812 /* Array of dies for which we should generate .debug_ranges info.  */
5813 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5814
5815 /* Number of elements currently allocated for ranges_table.  */
5816 static GTY(()) unsigned ranges_table_allocated;
5817
5818 /* Number of elements in ranges_table currently in use.  */
5819 static GTY(()) unsigned ranges_table_in_use;
5820
5821 /* Array of pairs of labels referenced in ranges_table.  */
5822 static GTY ((length ("ranges_by_label_allocated")))
5823      dw_ranges_by_label_ref ranges_by_label;
5824
5825 /* Number of elements currently allocated for ranges_by_label.  */
5826 static GTY(()) unsigned ranges_by_label_allocated;
5827
5828 /* Number of elements in ranges_by_label currently in use.  */
5829 static GTY(()) unsigned ranges_by_label_in_use;
5830
5831 /* Size (in elements) of increments by which we may expand the
5832    ranges_table.  */
5833 #define RANGES_TABLE_INCREMENT 64
5834
5835 /* Whether we have location lists that need outputting */
5836 static GTY(()) bool have_location_lists;
5837
5838 /* Unique label counter.  */
5839 static GTY(()) unsigned int loclabel_num;
5840
5841 /* Unique label counter for point-of-call tables.  */
5842 static GTY(()) unsigned int poc_label_num;
5843
5844 /* The direct call table structure.  */
5845
5846 typedef struct GTY(()) dcall_struct {
5847   unsigned int poc_label_num;
5848   tree poc_decl;
5849   dw_die_ref targ_die;
5850 }
5851 dcall_entry;
5852
5853 DEF_VEC_O(dcall_entry);
5854 DEF_VEC_ALLOC_O(dcall_entry, gc);
5855
5856 /* The virtual call table structure.  */
5857
5858 typedef struct GTY(()) vcall_struct {
5859   unsigned int poc_label_num;
5860   unsigned int vtable_slot;
5861 }
5862 vcall_entry;
5863
5864 DEF_VEC_O(vcall_entry);
5865 DEF_VEC_ALLOC_O(vcall_entry, gc);
5866
5867 /* Pointers to the direct and virtual call tables.  */
5868 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
5869 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
5870
5871 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
5872
5873 struct GTY (()) vcall_insn {
5874   int insn_uid;
5875   unsigned int vtable_slot;
5876 };
5877
5878 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
5879
5880 #ifdef DWARF2_DEBUGGING_INFO
5881 /* Record whether the function being analyzed contains inlined functions.  */
5882 static int current_function_has_inlines;
5883 #endif
5884 #if 0 && defined (MIPS_DEBUGGING_INFO)
5885 static int comp_unit_has_inlines;
5886 #endif
5887
5888 /* The last file entry emitted by maybe_emit_file().  */
5889 static GTY(()) struct dwarf_file_data * last_emitted_file;
5890
5891 /* Number of internal labels generated by gen_internal_sym().  */
5892 static GTY(()) int label_num;
5893
5894 /* Cached result of previous call to lookup_filename.  */
5895 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5896
5897 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5898
5899 #ifdef DWARF2_DEBUGGING_INFO
5900
5901 /* Offset from the "steady-state frame pointer" to the frame base,
5902    within the current function.  */
5903 static HOST_WIDE_INT frame_pointer_fb_offset;
5904
5905 /* Forward declarations for functions defined in this file.  */
5906
5907 static int is_pseudo_reg (const_rtx);
5908 static tree type_main_variant (tree);
5909 static int is_tagged_type (const_tree);
5910 static const char *dwarf_tag_name (unsigned);
5911 static const char *dwarf_attr_name (unsigned);
5912 static const char *dwarf_form_name (unsigned);
5913 static tree decl_ultimate_origin (const_tree);
5914 static tree decl_class_context (tree);
5915 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5916 static inline enum dw_val_class AT_class (dw_attr_ref);
5917 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5918 static inline unsigned AT_flag (dw_attr_ref);
5919 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5920 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5921 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5922 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5923 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
5924                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
5925 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5926                                unsigned int, unsigned char *);
5927 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
5928 static hashval_t debug_str_do_hash (const void *);
5929 static int debug_str_eq (const void *, const void *);
5930 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5931 static inline const char *AT_string (dw_attr_ref);
5932 static enum dwarf_form AT_string_form (dw_attr_ref);
5933 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5934 static void add_AT_specification (dw_die_ref, dw_die_ref);
5935 static inline dw_die_ref AT_ref (dw_attr_ref);
5936 static inline int AT_ref_external (dw_attr_ref);
5937 static inline void set_AT_ref_external (dw_attr_ref, int);
5938 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5939 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5940 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5941 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5942                              dw_loc_list_ref);
5943 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5944 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5945 static inline rtx AT_addr (dw_attr_ref);
5946 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5947 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5948 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5949 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5950                            unsigned HOST_WIDE_INT);
5951 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5952                                unsigned long);
5953 static inline const char *AT_lbl (dw_attr_ref);
5954 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5955 static const char *get_AT_low_pc (dw_die_ref);
5956 static const char *get_AT_hi_pc (dw_die_ref);
5957 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5958 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5959 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5960 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5961 static bool is_c_family (void);
5962 static bool is_cxx (void);
5963 static bool is_java (void);
5964 static bool is_fortran (void);
5965 static bool is_ada (void);
5966 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5967 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5968 static void add_child_die (dw_die_ref, dw_die_ref);
5969 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5970 static dw_die_ref lookup_type_die (tree);
5971 static void equate_type_number_to_die (tree, dw_die_ref);
5972 static hashval_t decl_die_table_hash (const void *);
5973 static int decl_die_table_eq (const void *, const void *);
5974 static dw_die_ref lookup_decl_die (tree);
5975 static hashval_t common_block_die_table_hash (const void *);
5976 static int common_block_die_table_eq (const void *, const void *);
5977 static hashval_t decl_loc_table_hash (const void *);
5978 static int decl_loc_table_eq (const void *, const void *);
5979 static var_loc_list *lookup_decl_loc (const_tree);
5980 static void equate_decl_number_to_die (tree, dw_die_ref);
5981 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5982 static void print_spaces (FILE *);
5983 static void print_die (dw_die_ref, FILE *);
5984 static void print_dwarf_line_table (FILE *);
5985 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5986 static dw_die_ref pop_compile_unit (dw_die_ref);
5987 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5988 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5989 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5990 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
5991 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
5992 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
5993 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
5994                                    struct md5_ctx *, int *);
5995 struct checksum_attributes;
5996 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
5997 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
5998 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
5999 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6000 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6001 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6002 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6003 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6004 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6005 static void compute_section_prefix (dw_die_ref);
6006 static int is_type_die (dw_die_ref);
6007 static int is_comdat_die (dw_die_ref);
6008 static int is_symbol_die (dw_die_ref);
6009 static void assign_symbol_names (dw_die_ref);
6010 static void break_out_includes (dw_die_ref);
6011 static int is_declaration_die (dw_die_ref);
6012 static int should_move_die_to_comdat (dw_die_ref);
6013 static dw_die_ref clone_as_declaration (dw_die_ref);
6014 static dw_die_ref clone_die (dw_die_ref);
6015 static dw_die_ref clone_tree (dw_die_ref);
6016 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6017 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6018 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6019 static dw_die_ref generate_skeleton (dw_die_ref);
6020 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6021                                                          dw_die_ref);
6022 static void break_out_comdat_types (dw_die_ref);
6023 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6024 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6025 static void copy_decls_for_unworthy_types (dw_die_ref);
6026
6027 static hashval_t htab_cu_hash (const void *);
6028 static int htab_cu_eq (const void *, const void *);
6029 static void htab_cu_del (void *);
6030 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6031 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6032 static void add_sibling_attributes (dw_die_ref);
6033 static void build_abbrev_table (dw_die_ref);
6034 static void output_location_lists (dw_die_ref);
6035 static int constant_size (unsigned HOST_WIDE_INT);
6036 static unsigned long size_of_die (dw_die_ref);
6037 static void calc_die_sizes (dw_die_ref);
6038 static void mark_dies (dw_die_ref);
6039 static void unmark_dies (dw_die_ref);
6040 static void unmark_all_dies (dw_die_ref);
6041 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6042 static unsigned long size_of_aranges (void);
6043 static enum dwarf_form value_format (dw_attr_ref);
6044 static void output_value_format (dw_attr_ref);
6045 static void output_abbrev_section (void);
6046 static void output_die_symbol (dw_die_ref);
6047 static void output_die (dw_die_ref);
6048 static void output_compilation_unit_header (void);
6049 static void output_comp_unit (dw_die_ref, int);
6050 static void output_comdat_type_unit (comdat_type_node *);
6051 static const char *dwarf2_name (tree, int);
6052 static void add_pubname (tree, dw_die_ref);
6053 static void add_pubname_string (const char *, dw_die_ref);
6054 static void add_pubtype (tree, dw_die_ref);
6055 static void output_pubnames (VEC (pubname_entry,gc) *);
6056 static void add_arange (tree, dw_die_ref);
6057 static void output_aranges (void);
6058 static unsigned int add_ranges_num (int);
6059 static unsigned int add_ranges (const_tree);
6060 static unsigned int add_ranges_by_labels (const char *, const char *);
6061 static void output_ranges (void);
6062 static void output_line_info (void);
6063 static void output_file_names (void);
6064 static dw_die_ref base_type_die (tree);
6065 static int is_base_type (tree);
6066 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6067 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6068 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6069 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6070 static int type_is_enum (const_tree);
6071 static unsigned int dbx_reg_number (const_rtx);
6072 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6073 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6074 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6075                                                 enum var_init_status);
6076 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6077                                                      enum var_init_status);
6078 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6079                                          enum var_init_status);
6080 static int is_based_loc (const_rtx);
6081 static int resolve_one_addr (rtx *, void *);
6082 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6083                                             enum var_init_status);
6084 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6085                                                enum var_init_status);
6086 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6087                                         enum var_init_status);
6088 static dw_loc_list_ref loc_list_from_tree (tree, int);
6089 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6090 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6091 static tree field_type (const_tree);
6092 static unsigned int simple_type_align_in_bits (const_tree);
6093 static unsigned int simple_decl_align_in_bits (const_tree);
6094 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6095 static HOST_WIDE_INT field_byte_offset (const_tree);
6096 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6097                                          dw_loc_list_ref);
6098 static void add_data_member_location_attribute (dw_die_ref, tree);
6099 static bool add_const_value_attribute (dw_die_ref, rtx);
6100 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6101 static void insert_float (const_rtx, unsigned char *);
6102 static rtx rtl_for_decl_location (tree);
6103 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6104                                                    enum dwarf_attribute);
6105 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6106 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6107 static void add_name_attribute (dw_die_ref, const char *);
6108 static void add_comp_dir_attribute (dw_die_ref);
6109 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6110 static void add_subscript_info (dw_die_ref, tree, bool);
6111 static void add_byte_size_attribute (dw_die_ref, tree);
6112 static void add_bit_offset_attribute (dw_die_ref, tree);
6113 static void add_bit_size_attribute (dw_die_ref, tree);
6114 static void add_prototyped_attribute (dw_die_ref, tree);
6115 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6116 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6117 static void add_src_coords_attributes (dw_die_ref, tree);
6118 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6119 static void push_decl_scope (tree);
6120 static void pop_decl_scope (void);
6121 static dw_die_ref scope_die_for (tree, dw_die_ref);
6122 static inline int local_scope_p (dw_die_ref);
6123 static inline int class_scope_p (dw_die_ref);
6124 static inline int class_or_namespace_scope_p (dw_die_ref);
6125 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6126 static void add_calling_convention_attribute (dw_die_ref, tree);
6127 static const char *type_tag (const_tree);
6128 static tree member_declared_type (const_tree);
6129 #if 0
6130 static const char *decl_start_label (tree);
6131 #endif
6132 static void gen_array_type_die (tree, dw_die_ref);
6133 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6134 #if 0
6135 static void gen_entry_point_die (tree, dw_die_ref);
6136 #endif
6137 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6138 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6139 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6140 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6141 static void gen_formal_types_die (tree, dw_die_ref);
6142 static void gen_subprogram_die (tree, dw_die_ref);
6143 static void gen_variable_die (tree, tree, dw_die_ref);
6144 static void gen_const_die (tree, dw_die_ref);
6145 static void gen_label_die (tree, dw_die_ref);
6146 static void gen_lexical_block_die (tree, dw_die_ref, int);
6147 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6148 static void gen_field_die (tree, dw_die_ref);
6149 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6150 static dw_die_ref gen_compile_unit_die (const char *);
6151 static void gen_inheritance_die (tree, tree, dw_die_ref);
6152 static void gen_member_die (tree, dw_die_ref);
6153 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6154                                                 enum debug_info_usage);
6155 static void gen_subroutine_type_die (tree, dw_die_ref);
6156 static void gen_typedef_die (tree, dw_die_ref);
6157 static void gen_type_die (tree, dw_die_ref);
6158 static void gen_block_die (tree, dw_die_ref, int);
6159 static void decls_for_scope (tree, dw_die_ref, int);
6160 static int is_redundant_typedef (const_tree);
6161 static inline dw_die_ref get_context_die (tree);
6162 static void gen_namespace_die (tree, dw_die_ref);
6163 static void gen_decl_die (tree, tree, dw_die_ref);
6164 static dw_die_ref force_decl_die (tree);
6165 static dw_die_ref force_type_die (tree);
6166 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6167 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6168 static struct dwarf_file_data * lookup_filename (const char *);
6169 static void retry_incomplete_types (void);
6170 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6171 static void gen_generic_params_dies (tree);
6172 static void splice_child_die (dw_die_ref, dw_die_ref);
6173 static int file_info_cmp (const void *, const void *);
6174 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6175                                      const char *, const char *, unsigned);
6176 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6177                                        const char *, const char *,
6178                                        const char *);
6179 static void output_loc_list (dw_loc_list_ref);
6180 static char *gen_internal_sym (const char *);
6181
6182 static void prune_unmark_dies (dw_die_ref);
6183 static void prune_unused_types_mark (dw_die_ref, int);
6184 static void prune_unused_types_walk (dw_die_ref);
6185 static void prune_unused_types_walk_attribs (dw_die_ref);
6186 static void prune_unused_types_prune (dw_die_ref);
6187 static void prune_unused_types (void);
6188 static int maybe_emit_file (struct dwarf_file_data *fd);
6189 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6190 static void gen_remaining_tmpl_value_param_die_attribute (void);
6191
6192 /* Section names used to hold DWARF debugging information.  */
6193 #ifndef DEBUG_INFO_SECTION
6194 #define DEBUG_INFO_SECTION      ".debug_info"
6195 #endif
6196 #ifndef DEBUG_ABBREV_SECTION
6197 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6198 #endif
6199 #ifndef DEBUG_ARANGES_SECTION
6200 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6201 #endif
6202 #ifndef DEBUG_MACINFO_SECTION
6203 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6204 #endif
6205 #ifndef DEBUG_LINE_SECTION
6206 #define DEBUG_LINE_SECTION      ".debug_line"
6207 #endif
6208 #ifndef DEBUG_LOC_SECTION
6209 #define DEBUG_LOC_SECTION       ".debug_loc"
6210 #endif
6211 #ifndef DEBUG_PUBNAMES_SECTION
6212 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6213 #endif
6214 #ifndef DEBUG_PUBTYPES_SECTION
6215 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6216 #endif
6217 #ifndef DEBUG_DCALL_SECTION
6218 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6219 #endif
6220 #ifndef DEBUG_VCALL_SECTION
6221 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6222 #endif
6223 #ifndef DEBUG_STR_SECTION
6224 #define DEBUG_STR_SECTION       ".debug_str"
6225 #endif
6226 #ifndef DEBUG_RANGES_SECTION
6227 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6228 #endif
6229
6230 /* Standard ELF section names for compiled code and data.  */
6231 #ifndef TEXT_SECTION_NAME
6232 #define TEXT_SECTION_NAME       ".text"
6233 #endif
6234
6235 /* Section flags for .debug_str section.  */
6236 #define DEBUG_STR_SECTION_FLAGS \
6237   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6238    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6239    : SECTION_DEBUG)
6240
6241 /* Labels we insert at beginning sections we can reference instead of
6242    the section names themselves.  */
6243
6244 #ifndef TEXT_SECTION_LABEL
6245 #define TEXT_SECTION_LABEL              "Ltext"
6246 #endif
6247 #ifndef COLD_TEXT_SECTION_LABEL
6248 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6249 #endif
6250 #ifndef DEBUG_LINE_SECTION_LABEL
6251 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6252 #endif
6253 #ifndef DEBUG_INFO_SECTION_LABEL
6254 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6255 #endif
6256 #ifndef DEBUG_ABBREV_SECTION_LABEL
6257 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6258 #endif
6259 #ifndef DEBUG_LOC_SECTION_LABEL
6260 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6261 #endif
6262 #ifndef DEBUG_RANGES_SECTION_LABEL
6263 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6264 #endif
6265 #ifndef DEBUG_MACINFO_SECTION_LABEL
6266 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6267 #endif
6268
6269 /* Definitions of defaults for formats and names of various special
6270    (artificial) labels which may be generated within this file (when the -g
6271    options is used and DWARF2_DEBUGGING_INFO is in effect.
6272    If necessary, these may be overridden from within the tm.h file, but
6273    typically, overriding these defaults is unnecessary.  */
6274
6275 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6276 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6277 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6278 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6279 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6280 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6281 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6282 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6283 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6284 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6285
6286 #ifndef TEXT_END_LABEL
6287 #define TEXT_END_LABEL          "Letext"
6288 #endif
6289 #ifndef COLD_END_LABEL
6290 #define COLD_END_LABEL          "Letext_cold"
6291 #endif
6292 #ifndef BLOCK_BEGIN_LABEL
6293 #define BLOCK_BEGIN_LABEL       "LBB"
6294 #endif
6295 #ifndef BLOCK_END_LABEL
6296 #define BLOCK_END_LABEL         "LBE"
6297 #endif
6298 #ifndef LINE_CODE_LABEL
6299 #define LINE_CODE_LABEL         "LM"
6300 #endif
6301 #ifndef SEPARATE_LINE_CODE_LABEL
6302 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6303 #endif
6304
6305 \f
6306 /* We allow a language front-end to designate a function that is to be
6307    called to "demangle" any name before it is put into a DIE.  */
6308
6309 static const char *(*demangle_name_func) (const char *);
6310
6311 void
6312 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6313 {
6314   demangle_name_func = func;
6315 }
6316
6317 /* Test if rtl node points to a pseudo register.  */
6318
6319 static inline int
6320 is_pseudo_reg (const_rtx rtl)
6321 {
6322   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6323           || (GET_CODE (rtl) == SUBREG
6324               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6325 }
6326
6327 /* Return a reference to a type, with its const and volatile qualifiers
6328    removed.  */
6329
6330 static inline tree
6331 type_main_variant (tree type)
6332 {
6333   type = TYPE_MAIN_VARIANT (type);
6334
6335   /* ??? There really should be only one main variant among any group of
6336      variants of a given type (and all of the MAIN_VARIANT values for all
6337      members of the group should point to that one type) but sometimes the C
6338      front-end messes this up for array types, so we work around that bug
6339      here.  */
6340   if (TREE_CODE (type) == ARRAY_TYPE)
6341     while (type != TYPE_MAIN_VARIANT (type))
6342       type = TYPE_MAIN_VARIANT (type);
6343
6344   return type;
6345 }
6346
6347 /* Return nonzero if the given type node represents a tagged type.  */
6348
6349 static inline int
6350 is_tagged_type (const_tree type)
6351 {
6352   enum tree_code code = TREE_CODE (type);
6353
6354   return (code == RECORD_TYPE || code == UNION_TYPE
6355           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6356 }
6357
6358 /* Convert a DIE tag into its string name.  */
6359
6360 static const char *
6361 dwarf_tag_name (unsigned int tag)
6362 {
6363   switch (tag)
6364     {
6365     case DW_TAG_padding:
6366       return "DW_TAG_padding";
6367     case DW_TAG_array_type:
6368       return "DW_TAG_array_type";
6369     case DW_TAG_class_type:
6370       return "DW_TAG_class_type";
6371     case DW_TAG_entry_point:
6372       return "DW_TAG_entry_point";
6373     case DW_TAG_enumeration_type:
6374       return "DW_TAG_enumeration_type";
6375     case DW_TAG_formal_parameter:
6376       return "DW_TAG_formal_parameter";
6377     case DW_TAG_imported_declaration:
6378       return "DW_TAG_imported_declaration";
6379     case DW_TAG_label:
6380       return "DW_TAG_label";
6381     case DW_TAG_lexical_block:
6382       return "DW_TAG_lexical_block";
6383     case DW_TAG_member:
6384       return "DW_TAG_member";
6385     case DW_TAG_pointer_type:
6386       return "DW_TAG_pointer_type";
6387     case DW_TAG_reference_type:
6388       return "DW_TAG_reference_type";
6389     case DW_TAG_compile_unit:
6390       return "DW_TAG_compile_unit";
6391     case DW_TAG_string_type:
6392       return "DW_TAG_string_type";
6393     case DW_TAG_structure_type:
6394       return "DW_TAG_structure_type";
6395     case DW_TAG_subroutine_type:
6396       return "DW_TAG_subroutine_type";
6397     case DW_TAG_typedef:
6398       return "DW_TAG_typedef";
6399     case DW_TAG_union_type:
6400       return "DW_TAG_union_type";
6401     case DW_TAG_unspecified_parameters:
6402       return "DW_TAG_unspecified_parameters";
6403     case DW_TAG_variant:
6404       return "DW_TAG_variant";
6405     case DW_TAG_common_block:
6406       return "DW_TAG_common_block";
6407     case DW_TAG_common_inclusion:
6408       return "DW_TAG_common_inclusion";
6409     case DW_TAG_inheritance:
6410       return "DW_TAG_inheritance";
6411     case DW_TAG_inlined_subroutine:
6412       return "DW_TAG_inlined_subroutine";
6413     case DW_TAG_module:
6414       return "DW_TAG_module";
6415     case DW_TAG_ptr_to_member_type:
6416       return "DW_TAG_ptr_to_member_type";
6417     case DW_TAG_set_type:
6418       return "DW_TAG_set_type";
6419     case DW_TAG_subrange_type:
6420       return "DW_TAG_subrange_type";
6421     case DW_TAG_with_stmt:
6422       return "DW_TAG_with_stmt";
6423     case DW_TAG_access_declaration:
6424       return "DW_TAG_access_declaration";
6425     case DW_TAG_base_type:
6426       return "DW_TAG_base_type";
6427     case DW_TAG_catch_block:
6428       return "DW_TAG_catch_block";
6429     case DW_TAG_const_type:
6430       return "DW_TAG_const_type";
6431     case DW_TAG_constant:
6432       return "DW_TAG_constant";
6433     case DW_TAG_enumerator:
6434       return "DW_TAG_enumerator";
6435     case DW_TAG_file_type:
6436       return "DW_TAG_file_type";
6437     case DW_TAG_friend:
6438       return "DW_TAG_friend";
6439     case DW_TAG_namelist:
6440       return "DW_TAG_namelist";
6441     case DW_TAG_namelist_item:
6442       return "DW_TAG_namelist_item";
6443     case DW_TAG_packed_type:
6444       return "DW_TAG_packed_type";
6445     case DW_TAG_subprogram:
6446       return "DW_TAG_subprogram";
6447     case DW_TAG_template_type_param:
6448       return "DW_TAG_template_type_param";
6449     case DW_TAG_template_value_param:
6450       return "DW_TAG_template_value_param";
6451     case DW_TAG_thrown_type:
6452       return "DW_TAG_thrown_type";
6453     case DW_TAG_try_block:
6454       return "DW_TAG_try_block";
6455     case DW_TAG_variant_part:
6456       return "DW_TAG_variant_part";
6457     case DW_TAG_variable:
6458       return "DW_TAG_variable";
6459     case DW_TAG_volatile_type:
6460       return "DW_TAG_volatile_type";
6461     case DW_TAG_dwarf_procedure:
6462       return "DW_TAG_dwarf_procedure";
6463     case DW_TAG_restrict_type:
6464       return "DW_TAG_restrict_type";
6465     case DW_TAG_interface_type:
6466       return "DW_TAG_interface_type";
6467     case DW_TAG_namespace:
6468       return "DW_TAG_namespace";
6469     case DW_TAG_imported_module:
6470       return "DW_TAG_imported_module";
6471     case DW_TAG_unspecified_type:
6472       return "DW_TAG_unspecified_type";
6473     case DW_TAG_partial_unit:
6474       return "DW_TAG_partial_unit";
6475     case DW_TAG_imported_unit:
6476       return "DW_TAG_imported_unit";
6477     case DW_TAG_condition:
6478       return "DW_TAG_condition";
6479     case DW_TAG_shared_type:
6480       return "DW_TAG_shared_type";
6481     case DW_TAG_type_unit:
6482       return "DW_TAG_type_unit";
6483     case DW_TAG_rvalue_reference_type:
6484       return "DW_TAG_rvalue_reference_type";
6485     case DW_TAG_template_alias:
6486       return "DW_TAG_template_alias";
6487     case DW_TAG_GNU_template_parameter_pack:
6488       return "DW_TAG_GNU_template_parameter_pack";
6489     case DW_TAG_GNU_formal_parameter_pack:
6490       return "DW_TAG_GNU_formal_parameter_pack";
6491     case DW_TAG_MIPS_loop:
6492       return "DW_TAG_MIPS_loop";
6493     case DW_TAG_format_label:
6494       return "DW_TAG_format_label";
6495     case DW_TAG_function_template:
6496       return "DW_TAG_function_template";
6497     case DW_TAG_class_template:
6498       return "DW_TAG_class_template";
6499     case DW_TAG_GNU_BINCL:
6500       return "DW_TAG_GNU_BINCL";
6501     case DW_TAG_GNU_EINCL:
6502       return "DW_TAG_GNU_EINCL";
6503     case DW_TAG_GNU_template_template_param:
6504       return "DW_TAG_GNU_template_template_param";
6505     default:
6506       return "DW_TAG_<unknown>";
6507     }
6508 }
6509
6510 /* Convert a DWARF attribute code into its string name.  */
6511
6512 static const char *
6513 dwarf_attr_name (unsigned int attr)
6514 {
6515   switch (attr)
6516     {
6517     case DW_AT_sibling:
6518       return "DW_AT_sibling";
6519     case DW_AT_location:
6520       return "DW_AT_location";
6521     case DW_AT_name:
6522       return "DW_AT_name";
6523     case DW_AT_ordering:
6524       return "DW_AT_ordering";
6525     case DW_AT_subscr_data:
6526       return "DW_AT_subscr_data";
6527     case DW_AT_byte_size:
6528       return "DW_AT_byte_size";
6529     case DW_AT_bit_offset:
6530       return "DW_AT_bit_offset";
6531     case DW_AT_bit_size:
6532       return "DW_AT_bit_size";
6533     case DW_AT_element_list:
6534       return "DW_AT_element_list";
6535     case DW_AT_stmt_list:
6536       return "DW_AT_stmt_list";
6537     case DW_AT_low_pc:
6538       return "DW_AT_low_pc";
6539     case DW_AT_high_pc:
6540       return "DW_AT_high_pc";
6541     case DW_AT_language:
6542       return "DW_AT_language";
6543     case DW_AT_member:
6544       return "DW_AT_member";
6545     case DW_AT_discr:
6546       return "DW_AT_discr";
6547     case DW_AT_discr_value:
6548       return "DW_AT_discr_value";
6549     case DW_AT_visibility:
6550       return "DW_AT_visibility";
6551     case DW_AT_import:
6552       return "DW_AT_import";
6553     case DW_AT_string_length:
6554       return "DW_AT_string_length";
6555     case DW_AT_common_reference:
6556       return "DW_AT_common_reference";
6557     case DW_AT_comp_dir:
6558       return "DW_AT_comp_dir";
6559     case DW_AT_const_value:
6560       return "DW_AT_const_value";
6561     case DW_AT_containing_type:
6562       return "DW_AT_containing_type";
6563     case DW_AT_default_value:
6564       return "DW_AT_default_value";
6565     case DW_AT_inline:
6566       return "DW_AT_inline";
6567     case DW_AT_is_optional:
6568       return "DW_AT_is_optional";
6569     case DW_AT_lower_bound:
6570       return "DW_AT_lower_bound";
6571     case DW_AT_producer:
6572       return "DW_AT_producer";
6573     case DW_AT_prototyped:
6574       return "DW_AT_prototyped";
6575     case DW_AT_return_addr:
6576       return "DW_AT_return_addr";
6577     case DW_AT_start_scope:
6578       return "DW_AT_start_scope";
6579     case DW_AT_bit_stride:
6580       return "DW_AT_bit_stride";
6581     case DW_AT_upper_bound:
6582       return "DW_AT_upper_bound";
6583     case DW_AT_abstract_origin:
6584       return "DW_AT_abstract_origin";
6585     case DW_AT_accessibility:
6586       return "DW_AT_accessibility";
6587     case DW_AT_address_class:
6588       return "DW_AT_address_class";
6589     case DW_AT_artificial:
6590       return "DW_AT_artificial";
6591     case DW_AT_base_types:
6592       return "DW_AT_base_types";
6593     case DW_AT_calling_convention:
6594       return "DW_AT_calling_convention";
6595     case DW_AT_count:
6596       return "DW_AT_count";
6597     case DW_AT_data_member_location:
6598       return "DW_AT_data_member_location";
6599     case DW_AT_decl_column:
6600       return "DW_AT_decl_column";
6601     case DW_AT_decl_file:
6602       return "DW_AT_decl_file";
6603     case DW_AT_decl_line:
6604       return "DW_AT_decl_line";
6605     case DW_AT_declaration:
6606       return "DW_AT_declaration";
6607     case DW_AT_discr_list:
6608       return "DW_AT_discr_list";
6609     case DW_AT_encoding:
6610       return "DW_AT_encoding";
6611     case DW_AT_external:
6612       return "DW_AT_external";
6613     case DW_AT_explicit:
6614       return "DW_AT_explicit";
6615     case DW_AT_frame_base:
6616       return "DW_AT_frame_base";
6617     case DW_AT_friend:
6618       return "DW_AT_friend";
6619     case DW_AT_identifier_case:
6620       return "DW_AT_identifier_case";
6621     case DW_AT_macro_info:
6622       return "DW_AT_macro_info";
6623     case DW_AT_namelist_items:
6624       return "DW_AT_namelist_items";
6625     case DW_AT_priority:
6626       return "DW_AT_priority";
6627     case DW_AT_segment:
6628       return "DW_AT_segment";
6629     case DW_AT_specification:
6630       return "DW_AT_specification";
6631     case DW_AT_static_link:
6632       return "DW_AT_static_link";
6633     case DW_AT_type:
6634       return "DW_AT_type";
6635     case DW_AT_use_location:
6636       return "DW_AT_use_location";
6637     case DW_AT_variable_parameter:
6638       return "DW_AT_variable_parameter";
6639     case DW_AT_virtuality:
6640       return "DW_AT_virtuality";
6641     case DW_AT_vtable_elem_location:
6642       return "DW_AT_vtable_elem_location";
6643
6644     case DW_AT_allocated:
6645       return "DW_AT_allocated";
6646     case DW_AT_associated:
6647       return "DW_AT_associated";
6648     case DW_AT_data_location:
6649       return "DW_AT_data_location";
6650     case DW_AT_byte_stride:
6651       return "DW_AT_byte_stride";
6652     case DW_AT_entry_pc:
6653       return "DW_AT_entry_pc";
6654     case DW_AT_use_UTF8:
6655       return "DW_AT_use_UTF8";
6656     case DW_AT_extension:
6657       return "DW_AT_extension";
6658     case DW_AT_ranges:
6659       return "DW_AT_ranges";
6660     case DW_AT_trampoline:
6661       return "DW_AT_trampoline";
6662     case DW_AT_call_column:
6663       return "DW_AT_call_column";
6664     case DW_AT_call_file:
6665       return "DW_AT_call_file";
6666     case DW_AT_call_line:
6667       return "DW_AT_call_line";
6668
6669     case DW_AT_signature:
6670       return "DW_AT_signature";
6671     case DW_AT_main_subprogram:
6672       return "DW_AT_main_subprogram";
6673     case DW_AT_data_bit_offset:
6674       return "DW_AT_data_bit_offset";
6675     case DW_AT_const_expr:
6676       return "DW_AT_const_expr";
6677     case DW_AT_enum_class:
6678       return "DW_AT_enum_class";
6679     case DW_AT_linkage_name:
6680       return "DW_AT_linkage_name";
6681
6682     case DW_AT_MIPS_fde:
6683       return "DW_AT_MIPS_fde";
6684     case DW_AT_MIPS_loop_begin:
6685       return "DW_AT_MIPS_loop_begin";
6686     case DW_AT_MIPS_tail_loop_begin:
6687       return "DW_AT_MIPS_tail_loop_begin";
6688     case DW_AT_MIPS_epilog_begin:
6689       return "DW_AT_MIPS_epilog_begin";
6690     case DW_AT_MIPS_loop_unroll_factor:
6691       return "DW_AT_MIPS_loop_unroll_factor";
6692     case DW_AT_MIPS_software_pipeline_depth:
6693       return "DW_AT_MIPS_software_pipeline_depth";
6694     case DW_AT_MIPS_linkage_name:
6695       return "DW_AT_MIPS_linkage_name";
6696     case DW_AT_MIPS_stride:
6697       return "DW_AT_MIPS_stride";
6698     case DW_AT_MIPS_abstract_name:
6699       return "DW_AT_MIPS_abstract_name";
6700     case DW_AT_MIPS_clone_origin:
6701       return "DW_AT_MIPS_clone_origin";
6702     case DW_AT_MIPS_has_inlines:
6703       return "DW_AT_MIPS_has_inlines";
6704
6705     case DW_AT_sf_names:
6706       return "DW_AT_sf_names";
6707     case DW_AT_src_info:
6708       return "DW_AT_src_info";
6709     case DW_AT_mac_info:
6710       return "DW_AT_mac_info";
6711     case DW_AT_src_coords:
6712       return "DW_AT_src_coords";
6713     case DW_AT_body_begin:
6714       return "DW_AT_body_begin";
6715     case DW_AT_body_end:
6716       return "DW_AT_body_end";
6717     case DW_AT_GNU_vector:
6718       return "DW_AT_GNU_vector";
6719     case DW_AT_GNU_guarded_by:
6720       return "DW_AT_GNU_guarded_by";
6721     case DW_AT_GNU_pt_guarded_by:
6722       return "DW_AT_GNU_pt_guarded_by";
6723     case DW_AT_GNU_guarded:
6724       return "DW_AT_GNU_guarded";
6725     case DW_AT_GNU_pt_guarded:
6726       return "DW_AT_GNU_pt_guarded";
6727     case DW_AT_GNU_locks_excluded:
6728       return "DW_AT_GNU_locks_excluded";
6729     case DW_AT_GNU_exclusive_locks_required:
6730       return "DW_AT_GNU_exclusive_locks_required";
6731     case DW_AT_GNU_shared_locks_required:
6732       return "DW_AT_GNU_shared_locks_required";
6733     case DW_AT_GNU_odr_signature:
6734       return "DW_AT_GNU_odr_signature";
6735     case DW_AT_GNU_template_name:
6736       return "DW_AT_GNU_template_name";
6737
6738     case DW_AT_VMS_rtnbeg_pd_address:
6739       return "DW_AT_VMS_rtnbeg_pd_address";
6740
6741     default:
6742       return "DW_AT_<unknown>";
6743     }
6744 }
6745
6746 /* Convert a DWARF value form code into its string name.  */
6747
6748 static const char *
6749 dwarf_form_name (unsigned int form)
6750 {
6751   switch (form)
6752     {
6753     case DW_FORM_addr:
6754       return "DW_FORM_addr";
6755     case DW_FORM_block2:
6756       return "DW_FORM_block2";
6757     case DW_FORM_block4:
6758       return "DW_FORM_block4";
6759     case DW_FORM_data2:
6760       return "DW_FORM_data2";
6761     case DW_FORM_data4:
6762       return "DW_FORM_data4";
6763     case DW_FORM_data8:
6764       return "DW_FORM_data8";
6765     case DW_FORM_string:
6766       return "DW_FORM_string";
6767     case DW_FORM_block:
6768       return "DW_FORM_block";
6769     case DW_FORM_block1:
6770       return "DW_FORM_block1";
6771     case DW_FORM_data1:
6772       return "DW_FORM_data1";
6773     case DW_FORM_flag:
6774       return "DW_FORM_flag";
6775     case DW_FORM_sdata:
6776       return "DW_FORM_sdata";
6777     case DW_FORM_strp:
6778       return "DW_FORM_strp";
6779     case DW_FORM_udata:
6780       return "DW_FORM_udata";
6781     case DW_FORM_ref_addr:
6782       return "DW_FORM_ref_addr";
6783     case DW_FORM_ref1:
6784       return "DW_FORM_ref1";
6785     case DW_FORM_ref2:
6786       return "DW_FORM_ref2";
6787     case DW_FORM_ref4:
6788       return "DW_FORM_ref4";
6789     case DW_FORM_ref8:
6790       return "DW_FORM_ref8";
6791     case DW_FORM_ref_udata:
6792       return "DW_FORM_ref_udata";
6793     case DW_FORM_indirect:
6794       return "DW_FORM_indirect";
6795     case DW_FORM_sec_offset:
6796       return "DW_FORM_sec_offset";
6797     case DW_FORM_exprloc:
6798       return "DW_FORM_exprloc";
6799     case DW_FORM_flag_present:
6800       return "DW_FORM_flag_present";
6801     case DW_FORM_ref_sig8:
6802       return "DW_FORM_ref_sig8";
6803     default:
6804       return "DW_FORM_<unknown>";
6805     }
6806 }
6807 \f
6808 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6809    instance of an inlined instance of a decl which is local to an inline
6810    function, so we have to trace all of the way back through the origin chain
6811    to find out what sort of node actually served as the original seed for the
6812    given block.  */
6813
6814 static tree
6815 decl_ultimate_origin (const_tree decl)
6816 {
6817   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6818     return NULL_TREE;
6819
6820   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6821      nodes in the function to point to themselves; ignore that if
6822      we're trying to output the abstract instance of this function.  */
6823   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6824     return NULL_TREE;
6825
6826   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6827      most distant ancestor, this should never happen.  */
6828   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6829
6830   return DECL_ABSTRACT_ORIGIN (decl);
6831 }
6832
6833 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6834    of a virtual function may refer to a base class, so we check the 'this'
6835    parameter.  */
6836
6837 static tree
6838 decl_class_context (tree decl)
6839 {
6840   tree context = NULL_TREE;
6841
6842   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6843     context = DECL_CONTEXT (decl);
6844   else
6845     context = TYPE_MAIN_VARIANT
6846       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6847
6848   if (context && !TYPE_P (context))
6849     context = NULL_TREE;
6850
6851   return context;
6852 }
6853 \f
6854 /* Add an attribute/value pair to a DIE.  */
6855
6856 static inline void
6857 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6858 {
6859   /* Maybe this should be an assert?  */
6860   if (die == NULL)
6861     return;
6862
6863   if (die->die_attr == NULL)
6864     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6865   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6866 }
6867
6868 static inline enum dw_val_class
6869 AT_class (dw_attr_ref a)
6870 {
6871   return a->dw_attr_val.val_class;
6872 }
6873
6874 /* Add a flag value attribute to a DIE.  */
6875
6876 static inline void
6877 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6878 {
6879   dw_attr_node attr;
6880
6881   attr.dw_attr = attr_kind;
6882   attr.dw_attr_val.val_class = dw_val_class_flag;
6883   attr.dw_attr_val.v.val_flag = flag;
6884   add_dwarf_attr (die, &attr);
6885 }
6886
6887 static inline unsigned
6888 AT_flag (dw_attr_ref a)
6889 {
6890   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6891   return a->dw_attr_val.v.val_flag;
6892 }
6893
6894 /* Add a signed integer attribute value to a DIE.  */
6895
6896 static inline void
6897 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6898 {
6899   dw_attr_node attr;
6900
6901   attr.dw_attr = attr_kind;
6902   attr.dw_attr_val.val_class = dw_val_class_const;
6903   attr.dw_attr_val.v.val_int = int_val;
6904   add_dwarf_attr (die, &attr);
6905 }
6906
6907 static inline HOST_WIDE_INT
6908 AT_int (dw_attr_ref a)
6909 {
6910   gcc_assert (a && AT_class (a) == dw_val_class_const);
6911   return a->dw_attr_val.v.val_int;
6912 }
6913
6914 /* Add an unsigned integer attribute value to a DIE.  */
6915
6916 static inline void
6917 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6918                  unsigned HOST_WIDE_INT unsigned_val)
6919 {
6920   dw_attr_node attr;
6921
6922   attr.dw_attr = attr_kind;
6923   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6924   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6925   add_dwarf_attr (die, &attr);
6926 }
6927
6928 static inline unsigned HOST_WIDE_INT
6929 AT_unsigned (dw_attr_ref a)
6930 {
6931   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6932   return a->dw_attr_val.v.val_unsigned;
6933 }
6934
6935 /* Add an unsigned double integer attribute value to a DIE.  */
6936
6937 static inline void
6938 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
6939                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
6940 {
6941   dw_attr_node attr;
6942
6943   attr.dw_attr = attr_kind;
6944   attr.dw_attr_val.val_class = dw_val_class_const_double;
6945   attr.dw_attr_val.v.val_double.high = high;
6946   attr.dw_attr_val.v.val_double.low = low;
6947   add_dwarf_attr (die, &attr);
6948 }
6949
6950 /* Add a floating point attribute value to a DIE and return it.  */
6951
6952 static inline void
6953 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6954             unsigned int length, unsigned int elt_size, unsigned char *array)
6955 {
6956   dw_attr_node attr;
6957
6958   attr.dw_attr = attr_kind;
6959   attr.dw_attr_val.val_class = dw_val_class_vec;
6960   attr.dw_attr_val.v.val_vec.length = length;
6961   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6962   attr.dw_attr_val.v.val_vec.array = array;
6963   add_dwarf_attr (die, &attr);
6964 }
6965
6966 /* Add an 8-byte data attribute value to a DIE.  */
6967
6968 static inline void
6969 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
6970               unsigned char data8[8])
6971 {
6972   dw_attr_node attr;
6973
6974   attr.dw_attr = attr_kind;
6975   attr.dw_attr_val.val_class = dw_val_class_data8;
6976   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
6977   add_dwarf_attr (die, &attr);
6978 }
6979
6980 /* Hash and equality functions for debug_str_hash.  */
6981
6982 static hashval_t
6983 debug_str_do_hash (const void *x)
6984 {
6985   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6986 }
6987
6988 static int
6989 debug_str_eq (const void *x1, const void *x2)
6990 {
6991   return strcmp ((((const struct indirect_string_node *)x1)->str),
6992                  (const char *)x2) == 0;
6993 }
6994
6995 /* Add STR to the indirect string hash table.  */
6996
6997 static struct indirect_string_node *
6998 find_AT_string (const char *str)
6999 {
7000   struct indirect_string_node *node;
7001   void **slot;
7002
7003   if (! debug_str_hash)
7004     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7005                                       debug_str_eq, NULL);
7006
7007   slot = htab_find_slot_with_hash (debug_str_hash, str,
7008                                    htab_hash_string (str), INSERT);
7009   if (*slot == NULL)
7010     {
7011       node = (struct indirect_string_node *)
7012                ggc_alloc_cleared (sizeof (struct indirect_string_node));
7013       node->str = ggc_strdup (str);
7014       *slot = node;
7015     }
7016   else
7017     node = (struct indirect_string_node *) *slot;
7018
7019   node->refcount++;
7020   return node;
7021 }
7022
7023 /* Add a string attribute value to a DIE.  */
7024
7025 static inline void
7026 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7027 {
7028   dw_attr_node attr;
7029   struct indirect_string_node *node;
7030
7031   node = find_AT_string (str);
7032
7033   attr.dw_attr = attr_kind;
7034   attr.dw_attr_val.val_class = dw_val_class_str;
7035   attr.dw_attr_val.v.val_str = node;
7036   add_dwarf_attr (die, &attr);
7037 }
7038
7039 /* Create a label for an indirect string node, ensuring it is going to
7040    be output, unless its reference count goes down to zero.  */
7041
7042 static inline void
7043 gen_label_for_indirect_string (struct indirect_string_node *node)
7044 {
7045   char label[32];
7046
7047   if (node->label)
7048     return;
7049
7050   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7051   ++dw2_string_counter;
7052   node->label = xstrdup (label);
7053 }
7054
7055 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7056    debug string STR.  */
7057
7058 static inline rtx
7059 get_debug_string_label (const char *str)
7060 {
7061   struct indirect_string_node *node = find_AT_string (str);
7062
7063   debug_str_hash_forced = true;
7064
7065   gen_label_for_indirect_string (node);
7066
7067   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7068 }
7069
7070 static inline const char *
7071 AT_string (dw_attr_ref a)
7072 {
7073   gcc_assert (a && AT_class (a) == dw_val_class_str);
7074   return a->dw_attr_val.v.val_str->str;
7075 }
7076
7077 /* Find out whether a string should be output inline in DIE
7078    or out-of-line in .debug_str section.  */
7079
7080 static enum dwarf_form
7081 AT_string_form (dw_attr_ref a)
7082 {
7083   struct indirect_string_node *node;
7084   unsigned int len;
7085
7086   gcc_assert (a && AT_class (a) == dw_val_class_str);
7087
7088   node = a->dw_attr_val.v.val_str;
7089   if (node->form)
7090     return node->form;
7091
7092   len = strlen (node->str) + 1;
7093
7094   /* If the string is shorter or equal to the size of the reference, it is
7095      always better to put it inline.  */
7096   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7097     return node->form = DW_FORM_string;
7098
7099   /* If we cannot expect the linker to merge strings in .debug_str
7100      section, only put it into .debug_str if it is worth even in this
7101      single module.  */
7102   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7103       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7104       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7105     return node->form = DW_FORM_string;
7106
7107   gen_label_for_indirect_string (node);
7108
7109   return node->form = DW_FORM_strp;
7110 }
7111
7112 /* Add a DIE reference attribute value to a DIE.  */
7113
7114 static inline void
7115 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7116 {
7117   dw_attr_node attr;
7118
7119   attr.dw_attr = attr_kind;
7120   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7121   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7122   attr.dw_attr_val.v.val_die_ref.external = 0;
7123   add_dwarf_attr (die, &attr);
7124 }
7125
7126 /* Add an AT_specification attribute to a DIE, and also make the back
7127    pointer from the specification to the definition.  */
7128
7129 static inline void
7130 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7131 {
7132   add_AT_die_ref (die, DW_AT_specification, targ_die);
7133   gcc_assert (!targ_die->die_definition);
7134   targ_die->die_definition = die;
7135 }
7136
7137 static inline dw_die_ref
7138 AT_ref (dw_attr_ref a)
7139 {
7140   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7141   return a->dw_attr_val.v.val_die_ref.die;
7142 }
7143
7144 static inline int
7145 AT_ref_external (dw_attr_ref a)
7146 {
7147   if (a && AT_class (a) == dw_val_class_die_ref)
7148     return a->dw_attr_val.v.val_die_ref.external;
7149
7150   return 0;
7151 }
7152
7153 static inline void
7154 set_AT_ref_external (dw_attr_ref a, int i)
7155 {
7156   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7157   a->dw_attr_val.v.val_die_ref.external = i;
7158 }
7159
7160 /* Add an FDE reference attribute value to a DIE.  */
7161
7162 static inline void
7163 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7164 {
7165   dw_attr_node attr;
7166
7167   attr.dw_attr = attr_kind;
7168   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7169   attr.dw_attr_val.v.val_fde_index = targ_fde;
7170   add_dwarf_attr (die, &attr);
7171 }
7172
7173 /* Add a location description attribute value to a DIE.  */
7174
7175 static inline void
7176 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7177 {
7178   dw_attr_node attr;
7179
7180   attr.dw_attr = attr_kind;
7181   attr.dw_attr_val.val_class = dw_val_class_loc;
7182   attr.dw_attr_val.v.val_loc = loc;
7183   add_dwarf_attr (die, &attr);
7184 }
7185
7186 static inline dw_loc_descr_ref
7187 AT_loc (dw_attr_ref a)
7188 {
7189   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7190   return a->dw_attr_val.v.val_loc;
7191 }
7192
7193 static inline void
7194 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7195 {
7196   dw_attr_node attr;
7197
7198   attr.dw_attr = attr_kind;
7199   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7200   attr.dw_attr_val.v.val_loc_list = loc_list;
7201   add_dwarf_attr (die, &attr);
7202   have_location_lists = true;
7203 }
7204
7205 static inline dw_loc_list_ref
7206 AT_loc_list (dw_attr_ref a)
7207 {
7208   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7209   return a->dw_attr_val.v.val_loc_list;
7210 }
7211
7212 /* Add an address constant attribute value to a DIE.  */
7213
7214 static inline void
7215 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7216 {
7217   dw_attr_node attr;
7218
7219   attr.dw_attr = attr_kind;
7220   attr.dw_attr_val.val_class = dw_val_class_addr;
7221   attr.dw_attr_val.v.val_addr = addr;
7222   add_dwarf_attr (die, &attr);
7223 }
7224
7225 /* Get the RTX from to an address DIE attribute.  */
7226
7227 static inline rtx
7228 AT_addr (dw_attr_ref a)
7229 {
7230   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7231   return a->dw_attr_val.v.val_addr;
7232 }
7233
7234 /* Add a file attribute value to a DIE.  */
7235
7236 static inline void
7237 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7238              struct dwarf_file_data *fd)
7239 {
7240   dw_attr_node attr;
7241
7242   attr.dw_attr = attr_kind;
7243   attr.dw_attr_val.val_class = dw_val_class_file;
7244   attr.dw_attr_val.v.val_file = fd;
7245   add_dwarf_attr (die, &attr);
7246 }
7247
7248 /* Get the dwarf_file_data from a file DIE attribute.  */
7249
7250 static inline struct dwarf_file_data *
7251 AT_file (dw_attr_ref a)
7252 {
7253   gcc_assert (a && AT_class (a) == dw_val_class_file);
7254   return a->dw_attr_val.v.val_file;
7255 }
7256
7257 /* Add a label identifier attribute value to a DIE.  */
7258
7259 static inline void
7260 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7261 {
7262   dw_attr_node attr;
7263
7264   attr.dw_attr = attr_kind;
7265   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7266   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7267   add_dwarf_attr (die, &attr);
7268 }
7269
7270 /* Add a section offset attribute value to a DIE, an offset into the
7271    debug_line section.  */
7272
7273 static inline void
7274 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7275                 const char *label)
7276 {
7277   dw_attr_node attr;
7278
7279   attr.dw_attr = attr_kind;
7280   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7281   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7282   add_dwarf_attr (die, &attr);
7283 }
7284
7285 /* Add a section offset attribute value to a DIE, an offset into the
7286    debug_macinfo section.  */
7287
7288 static inline void
7289 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7290                const char *label)
7291 {
7292   dw_attr_node attr;
7293
7294   attr.dw_attr = attr_kind;
7295   attr.dw_attr_val.val_class = dw_val_class_macptr;
7296   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7297   add_dwarf_attr (die, &attr);
7298 }
7299
7300 /* Add an offset attribute value to a DIE.  */
7301
7302 static inline void
7303 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7304                unsigned HOST_WIDE_INT offset)
7305 {
7306   dw_attr_node attr;
7307
7308   attr.dw_attr = attr_kind;
7309   attr.dw_attr_val.val_class = dw_val_class_offset;
7310   attr.dw_attr_val.v.val_offset = offset;
7311   add_dwarf_attr (die, &attr);
7312 }
7313
7314 /* Add an range_list attribute value to a DIE.  */
7315
7316 static void
7317 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7318                    long unsigned int offset)
7319 {
7320   dw_attr_node attr;
7321
7322   attr.dw_attr = attr_kind;
7323   attr.dw_attr_val.val_class = dw_val_class_range_list;
7324   attr.dw_attr_val.v.val_offset = offset;
7325   add_dwarf_attr (die, &attr);
7326 }
7327
7328 static inline const char *
7329 AT_lbl (dw_attr_ref a)
7330 {
7331   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7332                     || AT_class (a) == dw_val_class_lineptr
7333                     || AT_class (a) == dw_val_class_macptr));
7334   return a->dw_attr_val.v.val_lbl_id;
7335 }
7336
7337 /* Get the attribute of type attr_kind.  */
7338
7339 static dw_attr_ref
7340 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7341 {
7342   dw_attr_ref a;
7343   unsigned ix;
7344   dw_die_ref spec = NULL;
7345
7346   if (! die)
7347     return NULL;
7348
7349   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7350     if (a->dw_attr == attr_kind)
7351       return a;
7352     else if (a->dw_attr == DW_AT_specification
7353              || a->dw_attr == DW_AT_abstract_origin)
7354       spec = AT_ref (a);
7355
7356   if (spec)
7357     return get_AT (spec, attr_kind);
7358
7359   return NULL;
7360 }
7361
7362 /* Return the "low pc" attribute value, typically associated with a subprogram
7363    DIE.  Return null if the "low pc" attribute is either not present, or if it
7364    cannot be represented as an assembler label identifier.  */
7365
7366 static inline const char *
7367 get_AT_low_pc (dw_die_ref die)
7368 {
7369   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7370
7371   return a ? AT_lbl (a) : NULL;
7372 }
7373
7374 /* Return the "high pc" attribute value, typically associated with a subprogram
7375    DIE.  Return null if the "high pc" attribute is either not present, or if it
7376    cannot be represented as an assembler label identifier.  */
7377
7378 static inline const char *
7379 get_AT_hi_pc (dw_die_ref die)
7380 {
7381   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7382
7383   return a ? AT_lbl (a) : NULL;
7384 }
7385
7386 /* Return the value of the string attribute designated by ATTR_KIND, or
7387    NULL if it is not present.  */
7388
7389 static inline const char *
7390 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7391 {
7392   dw_attr_ref a = get_AT (die, attr_kind);
7393
7394   return a ? AT_string (a) : NULL;
7395 }
7396
7397 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7398    if it is not present.  */
7399
7400 static inline int
7401 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7402 {
7403   dw_attr_ref a = get_AT (die, attr_kind);
7404
7405   return a ? AT_flag (a) : 0;
7406 }
7407
7408 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7409    if it is not present.  */
7410
7411 static inline unsigned
7412 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7413 {
7414   dw_attr_ref a = get_AT (die, attr_kind);
7415
7416   return a ? AT_unsigned (a) : 0;
7417 }
7418
7419 static inline dw_die_ref
7420 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7421 {
7422   dw_attr_ref a = get_AT (die, attr_kind);
7423
7424   return a ? AT_ref (a) : NULL;
7425 }
7426
7427 static inline struct dwarf_file_data *
7428 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7429 {
7430   dw_attr_ref a = get_AT (die, attr_kind);
7431
7432   return a ? AT_file (a) : NULL;
7433 }
7434
7435 /* Return TRUE if the language is C or C++.  */
7436
7437 static inline bool
7438 is_c_family (void)
7439 {
7440   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7441
7442   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7443           || lang == DW_LANG_C99
7444           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7445 }
7446
7447 /* Return TRUE if the language is C++.  */
7448
7449 static inline bool
7450 is_cxx (void)
7451 {
7452   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7453
7454   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7455 }
7456
7457 /* Return TRUE if the language is Fortran.  */
7458
7459 static inline bool
7460 is_fortran (void)
7461 {
7462   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7463
7464   return (lang == DW_LANG_Fortran77
7465           || lang == DW_LANG_Fortran90
7466           || lang == DW_LANG_Fortran95);
7467 }
7468
7469 /* Return TRUE if the language is Java.  */
7470
7471 static inline bool
7472 is_java (void)
7473 {
7474   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7475
7476   return lang == DW_LANG_Java;
7477 }
7478
7479 /* Return TRUE if the language is Ada.  */
7480
7481 static inline bool
7482 is_ada (void)
7483 {
7484   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7485
7486   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7487 }
7488
7489 /* Remove the specified attribute if present.  */
7490
7491 static void
7492 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7493 {
7494   dw_attr_ref a;
7495   unsigned ix;
7496
7497   if (! die)
7498     return;
7499
7500   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7501     if (a->dw_attr == attr_kind)
7502       {
7503         if (AT_class (a) == dw_val_class_str)
7504           if (a->dw_attr_val.v.val_str->refcount)
7505             a->dw_attr_val.v.val_str->refcount--;
7506
7507         /* VEC_ordered_remove should help reduce the number of abbrevs
7508            that are needed.  */
7509         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7510         return;
7511       }
7512 }
7513
7514 /* Remove CHILD from its parent.  PREV must have the property that
7515    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7516
7517 static void
7518 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7519 {
7520   gcc_assert (child->die_parent == prev->die_parent);
7521   gcc_assert (prev->die_sib == child);
7522   if (prev == child)
7523     {
7524       gcc_assert (child->die_parent->die_child == child);
7525       prev = NULL;
7526     }
7527   else
7528     prev->die_sib = child->die_sib;
7529   if (child->die_parent->die_child == child)
7530     child->die_parent->die_child = prev;
7531 }
7532
7533 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7534    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7535
7536 static void
7537 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7538 {
7539   dw_die_ref parent = old_child->die_parent;
7540
7541   gcc_assert (parent == prev->die_parent);
7542   gcc_assert (prev->die_sib == old_child);
7543
7544   new_child->die_parent = parent;
7545   if (prev == old_child)
7546     {
7547       gcc_assert (parent->die_child == old_child);
7548       new_child->die_sib = new_child;
7549     }
7550   else
7551     {
7552       prev->die_sib = new_child;
7553       new_child->die_sib = old_child->die_sib;
7554     }
7555   if (old_child->die_parent->die_child == old_child)
7556     old_child->die_parent->die_child = new_child;
7557 }
7558
7559 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7560
7561 static void
7562 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7563 {
7564   dw_die_ref c;
7565   new_parent->die_child = old_parent->die_child;
7566   old_parent->die_child = NULL;
7567   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7568 }
7569
7570 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7571    matches TAG.  */
7572
7573 static void
7574 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7575 {
7576   dw_die_ref c;
7577
7578   c = die->die_child;
7579   if (c) do {
7580     dw_die_ref prev = c;
7581     c = c->die_sib;
7582     while (c->die_tag == tag)
7583       {
7584         remove_child_with_prev (c, prev);
7585         /* Might have removed every child.  */
7586         if (c == c->die_sib)
7587           return;
7588         c = c->die_sib;
7589       }
7590   } while (c != die->die_child);
7591 }
7592
7593 /* Add a CHILD_DIE as the last child of DIE.  */
7594
7595 static void
7596 add_child_die (dw_die_ref die, dw_die_ref child_die)
7597 {
7598   /* FIXME this should probably be an assert.  */
7599   if (! die || ! child_die)
7600     return;
7601   gcc_assert (die != child_die);
7602
7603   child_die->die_parent = die;
7604   if (die->die_child)
7605     {
7606       child_die->die_sib = die->die_child->die_sib;
7607       die->die_child->die_sib = child_die;
7608     }
7609   else
7610     child_die->die_sib = child_die;
7611   die->die_child = child_die;
7612 }
7613
7614 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7615    is the specification, to the end of PARENT's list of children.
7616    This is done by removing and re-adding it.  */
7617
7618 static void
7619 splice_child_die (dw_die_ref parent, dw_die_ref child)
7620 {
7621   dw_die_ref p;
7622
7623   /* We want the declaration DIE from inside the class, not the
7624      specification DIE at toplevel.  */
7625   if (child->die_parent != parent)
7626     {
7627       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7628
7629       if (tmp)
7630         child = tmp;
7631     }
7632
7633   gcc_assert (child->die_parent == parent
7634               || (child->die_parent
7635                   == get_AT_ref (parent, DW_AT_specification)));
7636
7637   for (p = child->die_parent->die_child; ; p = p->die_sib)
7638     if (p->die_sib == child)
7639       {
7640         remove_child_with_prev (child, p);
7641         break;
7642       }
7643
7644   add_child_die (parent, child);
7645 }
7646
7647 /* Return a pointer to a newly created DIE node.  */
7648
7649 static inline dw_die_ref
7650 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7651 {
7652   dw_die_ref die = GGC_CNEW (die_node);
7653
7654   die->die_tag = tag_value;
7655
7656   if (parent_die != NULL)
7657     add_child_die (parent_die, die);
7658   else
7659     {
7660       limbo_die_node *limbo_node;
7661
7662       limbo_node = GGC_CNEW (limbo_die_node);
7663       limbo_node->die = die;
7664       limbo_node->created_for = t;
7665       limbo_node->next = limbo_die_list;
7666       limbo_die_list = limbo_node;
7667     }
7668
7669   return die;
7670 }
7671
7672 /* Return the DIE associated with the given type specifier.  */
7673
7674 static inline dw_die_ref
7675 lookup_type_die (tree type)
7676 {
7677   return TYPE_SYMTAB_DIE (type);
7678 }
7679
7680 /* Equate a DIE to a given type specifier.  */
7681
7682 static inline void
7683 equate_type_number_to_die (tree type, dw_die_ref type_die)
7684 {
7685   TYPE_SYMTAB_DIE (type) = type_die;
7686 }
7687
7688 /* Returns a hash value for X (which really is a die_struct).  */
7689
7690 static hashval_t
7691 decl_die_table_hash (const void *x)
7692 {
7693   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7694 }
7695
7696 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7697
7698 static int
7699 decl_die_table_eq (const void *x, const void *y)
7700 {
7701   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7702 }
7703
7704 /* Return the DIE associated with a given declaration.  */
7705
7706 static inline dw_die_ref
7707 lookup_decl_die (tree decl)
7708 {
7709   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7710 }
7711
7712 /* Returns a hash value for X (which really is a var_loc_list).  */
7713
7714 static hashval_t
7715 decl_loc_table_hash (const void *x)
7716 {
7717   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7718 }
7719
7720 /* Return nonzero if decl_id of var_loc_list X is the same as
7721    UID of decl *Y.  */
7722
7723 static int
7724 decl_loc_table_eq (const void *x, const void *y)
7725 {
7726   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7727 }
7728
7729 /* Return the var_loc list associated with a given declaration.  */
7730
7731 static inline var_loc_list *
7732 lookup_decl_loc (const_tree decl)
7733 {
7734   if (!decl_loc_table)
7735     return NULL;
7736   return (var_loc_list *)
7737     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7738 }
7739
7740 /* Equate a DIE to a particular declaration.  */
7741
7742 static void
7743 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7744 {
7745   unsigned int decl_id = DECL_UID (decl);
7746   void **slot;
7747
7748   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7749   *slot = decl_die;
7750   decl_die->decl_id = decl_id;
7751 }
7752
7753 /* Add a variable location node to the linked list for DECL.  */
7754
7755 static void
7756 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7757 {
7758   unsigned int decl_id = DECL_UID (decl);
7759   var_loc_list *temp;
7760   void **slot;
7761
7762   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7763   if (*slot == NULL)
7764     {
7765       temp = GGC_CNEW (var_loc_list);
7766       temp->decl_id = decl_id;
7767       *slot = temp;
7768     }
7769   else
7770     temp = (var_loc_list *) *slot;
7771
7772   if (temp->last)
7773     {
7774       /* If the current location is the same as the end of the list,
7775          and either both or neither of the locations is uninitialized,
7776          we have nothing to do.  */
7777       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7778                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7779           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7780                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7781               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7782                    == VAR_INIT_STATUS_UNINITIALIZED)
7783                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7784                       == VAR_INIT_STATUS_UNINITIALIZED))))
7785         {
7786           /* Add LOC to the end of list and update LAST.  */
7787           temp->last->next = loc;
7788           temp->last = loc;
7789         }
7790     }
7791   /* Do not add empty location to the beginning of the list.  */
7792   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7793     {
7794       temp->first = loc;
7795       temp->last = loc;
7796     }
7797 }
7798 \f
7799 /* Keep track of the number of spaces used to indent the
7800    output of the debugging routines that print the structure of
7801    the DIE internal representation.  */
7802 static int print_indent;
7803
7804 /* Indent the line the number of spaces given by print_indent.  */
7805
7806 static inline void
7807 print_spaces (FILE *outfile)
7808 {
7809   fprintf (outfile, "%*s", print_indent, "");
7810 }
7811
7812 /* Print a type signature in hex.  */
7813
7814 static inline void
7815 print_signature (FILE *outfile, char *sig)
7816 {
7817   int i;
7818
7819   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7820     fprintf (outfile, "%02x", sig[i] & 0xff);
7821 }
7822
7823 /* Print the information associated with a given DIE, and its children.
7824    This routine is a debugging aid only.  */
7825
7826 static void
7827 print_die (dw_die_ref die, FILE *outfile)
7828 {
7829   dw_attr_ref a;
7830   dw_die_ref c;
7831   unsigned ix;
7832
7833   print_spaces (outfile);
7834   fprintf (outfile, "DIE %4ld: %s\n",
7835            die->die_offset, dwarf_tag_name (die->die_tag));
7836   print_spaces (outfile);
7837   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7838   fprintf (outfile, " offset: %ld\n", die->die_offset);
7839   if (dwarf_version >= 4 && die->die_id.die_type_node)
7840     {
7841       print_spaces (outfile);
7842       fprintf (outfile, "  signature: ");
7843       print_signature (outfile, die->die_id.die_type_node->signature);
7844       fprintf (outfile, "\n");
7845     }
7846
7847   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7848     {
7849       print_spaces (outfile);
7850       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7851
7852       switch (AT_class (a))
7853         {
7854         case dw_val_class_addr:
7855           fprintf (outfile, "address");
7856           break;
7857         case dw_val_class_offset:
7858           fprintf (outfile, "offset");
7859           break;
7860         case dw_val_class_loc:
7861           fprintf (outfile, "location descriptor");
7862           break;
7863         case dw_val_class_loc_list:
7864           fprintf (outfile, "location list -> label:%s",
7865                    AT_loc_list (a)->ll_symbol);
7866           break;
7867         case dw_val_class_range_list:
7868           fprintf (outfile, "range list");
7869           break;
7870         case dw_val_class_const:
7871           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7872           break;
7873         case dw_val_class_unsigned_const:
7874           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7875           break;
7876         case dw_val_class_const_double:
7877           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
7878                             HOST_WIDE_INT_PRINT_UNSIGNED")",
7879                    a->dw_attr_val.v.val_double.high,
7880                    a->dw_attr_val.v.val_double.low);
7881           break;
7882         case dw_val_class_vec:
7883           fprintf (outfile, "floating-point or vector constant");
7884           break;
7885         case dw_val_class_flag:
7886           fprintf (outfile, "%u", AT_flag (a));
7887           break;
7888         case dw_val_class_die_ref:
7889           if (AT_ref (a) != NULL)
7890             {
7891               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
7892                 {
7893                   fprintf (outfile, "die -> signature: ");
7894                   print_signature (outfile,
7895                                    AT_ref (a)->die_id.die_type_node->signature);
7896                 }
7897               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
7898                 fprintf (outfile, "die -> label: %s",
7899                          AT_ref (a)->die_id.die_symbol);
7900               else
7901                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7902             }
7903           else
7904             fprintf (outfile, "die -> <null>");
7905           break;
7906         case dw_val_class_lbl_id:
7907         case dw_val_class_lineptr:
7908         case dw_val_class_macptr:
7909           fprintf (outfile, "label: %s", AT_lbl (a));
7910           break;
7911         case dw_val_class_str:
7912           if (AT_string (a) != NULL)
7913             fprintf (outfile, "\"%s\"", AT_string (a));
7914           else
7915             fprintf (outfile, "<null>");
7916           break;
7917         case dw_val_class_file:
7918           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7919                    AT_file (a)->emitted_number);
7920           break;
7921         case dw_val_class_data8:
7922           {
7923             int i;
7924
7925             for (i = 0; i < 8; i++)
7926               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
7927             break;
7928           }
7929         default:
7930           break;
7931         }
7932
7933       fprintf (outfile, "\n");
7934     }
7935
7936   if (die->die_child != NULL)
7937     {
7938       print_indent += 4;
7939       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7940       print_indent -= 4;
7941     }
7942   if (print_indent == 0)
7943     fprintf (outfile, "\n");
7944 }
7945
7946 /* Print the contents of the source code line number correspondence table.
7947    This routine is a debugging aid only.  */
7948
7949 static void
7950 print_dwarf_line_table (FILE *outfile)
7951 {
7952   unsigned i;
7953   dw_line_info_ref line_info;
7954
7955   fprintf (outfile, "\n\nDWARF source line information\n");
7956   for (i = 1; i < line_info_table_in_use; i++)
7957     {
7958       line_info = &line_info_table[i];
7959       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7960                line_info->dw_file_num,
7961                line_info->dw_line_num);
7962     }
7963
7964   fprintf (outfile, "\n\n");
7965 }
7966
7967 /* Print the information collected for a given DIE.  */
7968
7969 void
7970 debug_dwarf_die (dw_die_ref die)
7971 {
7972   print_die (die, stderr);
7973 }
7974
7975 /* Print all DWARF information collected for the compilation unit.
7976    This routine is a debugging aid only.  */
7977
7978 void
7979 debug_dwarf (void)
7980 {
7981   print_indent = 0;
7982   print_die (comp_unit_die, stderr);
7983   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7984     print_dwarf_line_table (stderr);
7985 }
7986 \f
7987 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7988    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7989    DIE that marks the start of the DIEs for this include file.  */
7990
7991 static dw_die_ref
7992 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7993 {
7994   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7995   dw_die_ref new_unit = gen_compile_unit_die (filename);
7996
7997   new_unit->die_sib = old_unit;
7998   return new_unit;
7999 }
8000
8001 /* Close an include-file CU and reopen the enclosing one.  */
8002
8003 static dw_die_ref
8004 pop_compile_unit (dw_die_ref old_unit)
8005 {
8006   dw_die_ref new_unit = old_unit->die_sib;
8007
8008   old_unit->die_sib = NULL;
8009   return new_unit;
8010 }
8011
8012 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8013 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8014
8015 /* Calculate the checksum of a location expression.  */
8016
8017 static inline void
8018 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8019 {
8020   int tem;
8021
8022   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8023   CHECKSUM (tem);
8024   CHECKSUM (loc->dw_loc_oprnd1);
8025   CHECKSUM (loc->dw_loc_oprnd2);
8026 }
8027
8028 /* Calculate the checksum of an attribute.  */
8029
8030 static void
8031 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8032 {
8033   dw_loc_descr_ref loc;
8034   rtx r;
8035
8036   CHECKSUM (at->dw_attr);
8037
8038   /* We don't care that this was compiled with a different compiler
8039      snapshot; if the output is the same, that's what matters.  */
8040   if (at->dw_attr == DW_AT_producer)
8041     return;
8042
8043   switch (AT_class (at))
8044     {
8045     case dw_val_class_const:
8046       CHECKSUM (at->dw_attr_val.v.val_int);
8047       break;
8048     case dw_val_class_unsigned_const:
8049       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8050       break;
8051     case dw_val_class_const_double:
8052       CHECKSUM (at->dw_attr_val.v.val_double);
8053       break;
8054     case dw_val_class_vec:
8055       CHECKSUM (at->dw_attr_val.v.val_vec);
8056       break;
8057     case dw_val_class_flag:
8058       CHECKSUM (at->dw_attr_val.v.val_flag);
8059       break;
8060     case dw_val_class_str:
8061       CHECKSUM_STRING (AT_string (at));
8062       break;
8063
8064     case dw_val_class_addr:
8065       r = AT_addr (at);
8066       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8067       CHECKSUM_STRING (XSTR (r, 0));
8068       break;
8069
8070     case dw_val_class_offset:
8071       CHECKSUM (at->dw_attr_val.v.val_offset);
8072       break;
8073
8074     case dw_val_class_loc:
8075       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8076         loc_checksum (loc, ctx);
8077       break;
8078
8079     case dw_val_class_die_ref:
8080       die_checksum (AT_ref (at), ctx, mark);
8081       break;
8082
8083     case dw_val_class_fde_ref:
8084     case dw_val_class_lbl_id:
8085     case dw_val_class_lineptr:
8086     case dw_val_class_macptr:
8087       break;
8088
8089     case dw_val_class_file:
8090       CHECKSUM_STRING (AT_file (at)->filename);
8091       break;
8092
8093     case dw_val_class_data8:
8094       CHECKSUM (at->dw_attr_val.v.val_data8);
8095       break;
8096
8097     default:
8098       break;
8099     }
8100 }
8101
8102 /* Calculate the checksum of a DIE.  */
8103
8104 static void
8105 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8106 {
8107   dw_die_ref c;
8108   dw_attr_ref a;
8109   unsigned ix;
8110
8111   /* To avoid infinite recursion.  */
8112   if (die->die_mark)
8113     {
8114       CHECKSUM (die->die_mark);
8115       return;
8116     }
8117   die->die_mark = ++(*mark);
8118
8119   CHECKSUM (die->die_tag);
8120
8121   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8122     attr_checksum (a, ctx, mark);
8123
8124   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8125 }
8126
8127 #undef CHECKSUM
8128 #undef CHECKSUM_STRING
8129
8130 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8131 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8132 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8133 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8134 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8135 #define CHECKSUM_ATTR(FOO) \
8136   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8137
8138 /* Calculate the checksum of a number in signed LEB128 format.  */
8139
8140 static void
8141 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8142 {
8143   unsigned char byte;
8144   bool more;
8145
8146   while (1)
8147     {
8148       byte = (value & 0x7f);
8149       value >>= 7;
8150       more = !((value == 0 && (byte & 0x40) == 0)
8151                 || (value == -1 && (byte & 0x40) != 0));
8152       if (more)
8153         byte |= 0x80;
8154       CHECKSUM (byte);
8155       if (!more)
8156         break;
8157     }
8158 }
8159
8160 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8161
8162 static void
8163 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8164 {
8165   while (1)
8166     {
8167       unsigned char byte = (value & 0x7f);
8168       value >>= 7;
8169       if (value != 0)
8170         /* More bytes to follow.  */
8171         byte |= 0x80;
8172       CHECKSUM (byte);
8173       if (value == 0)
8174         break;
8175     }
8176 }
8177
8178 /* Checksum the context of the DIE.  This adds the names of any
8179    surrounding namespaces or structures to the checksum.  */
8180
8181 static void
8182 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8183 {
8184   const char *name;
8185   dw_die_ref spec;
8186   int tag = die->die_tag;
8187
8188   if (tag != DW_TAG_namespace
8189       && tag != DW_TAG_structure_type
8190       && tag != DW_TAG_class_type)
8191     return;
8192
8193   name = get_AT_string (die, DW_AT_name);
8194
8195   spec = get_AT_ref (die, DW_AT_specification);
8196   if (spec != NULL)
8197     die = spec;
8198
8199   if (die->die_parent != NULL)
8200     checksum_die_context (die->die_parent, ctx);
8201
8202   CHECKSUM_ULEB128 ('C');
8203   CHECKSUM_ULEB128 (tag);
8204   if (name != NULL)
8205     CHECKSUM_STRING (name);
8206 }
8207
8208 /* Calculate the checksum of a location expression.  */
8209
8210 static inline void
8211 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8212 {
8213   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8214      were emitted as a DW_FORM_sdata instead of a location expression.  */
8215   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8216     {
8217       CHECKSUM_ULEB128 (DW_FORM_sdata);
8218       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8219       return;
8220     }
8221
8222   /* Otherwise, just checksum the raw location expression.  */
8223   while (loc != NULL)
8224     {
8225       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8226       CHECKSUM (loc->dw_loc_oprnd1);
8227       CHECKSUM (loc->dw_loc_oprnd2);
8228       loc = loc->dw_loc_next;
8229     }
8230 }
8231
8232 /* Calculate the checksum of an attribute.  */
8233
8234 static void
8235 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8236                        struct md5_ctx *ctx, int *mark)
8237 {
8238   dw_loc_descr_ref loc;
8239   rtx r;
8240
8241   if (AT_class (at) == dw_val_class_die_ref)
8242     {
8243       dw_die_ref target_die = AT_ref (at);
8244
8245       /* For pointer and reference types, we checksum only the (qualified)
8246          name of the target type (if there is a name).  For friend entries,
8247          we checksum only the (qualified) name of the target type or function.
8248          This allows the checksum to remain the same whether the target type
8249          is complete or not.  */
8250       if ((at->dw_attr == DW_AT_type
8251            && (tag == DW_TAG_pointer_type
8252                || tag == DW_TAG_reference_type
8253                || tag == DW_TAG_ptr_to_member_type))
8254           || (at->dw_attr == DW_AT_friend
8255               && tag == DW_TAG_friend))
8256         {
8257           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8258
8259           if (name_attr != NULL)
8260             {
8261               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8262
8263               if (decl == NULL)
8264                 decl = target_die;
8265               CHECKSUM_ULEB128 ('N');
8266               CHECKSUM_ULEB128 (at->dw_attr);
8267               if (decl->die_parent != NULL)
8268                 checksum_die_context (decl->die_parent, ctx);
8269               CHECKSUM_ULEB128 ('E');
8270               CHECKSUM_STRING (AT_string (name_attr));
8271               return;
8272             }
8273         }
8274
8275       /* For all other references to another DIE, we check to see if the
8276          target DIE has already been visited.  If it has, we emit a
8277          backward reference; if not, we descend recursively.  */
8278       if (target_die->die_mark > 0)
8279         {
8280           CHECKSUM_ULEB128 ('R');
8281           CHECKSUM_ULEB128 (at->dw_attr);
8282           CHECKSUM_ULEB128 (target_die->die_mark);
8283         }
8284       else
8285         {
8286           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8287
8288           if (decl == NULL)
8289             decl = target_die;
8290           target_die->die_mark = ++(*mark);
8291           CHECKSUM_ULEB128 ('T');
8292           CHECKSUM_ULEB128 (at->dw_attr);
8293           if (decl->die_parent != NULL)
8294             checksum_die_context (decl->die_parent, ctx);
8295           die_checksum_ordered (target_die, ctx, mark);
8296         }
8297       return;
8298     }
8299
8300   CHECKSUM_ULEB128 ('A');
8301   CHECKSUM_ULEB128 (at->dw_attr);
8302
8303   switch (AT_class (at))
8304     {
8305     case dw_val_class_const:
8306       CHECKSUM_ULEB128 (DW_FORM_sdata);
8307       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8308       break;
8309
8310     case dw_val_class_unsigned_const:
8311       CHECKSUM_ULEB128 (DW_FORM_sdata);
8312       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8313       break;
8314
8315     case dw_val_class_const_double:
8316       CHECKSUM_ULEB128 (DW_FORM_block);
8317       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8318       CHECKSUM (at->dw_attr_val.v.val_double);
8319       break;
8320
8321     case dw_val_class_vec:
8322       CHECKSUM_ULEB128 (DW_FORM_block);
8323       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8324       CHECKSUM (at->dw_attr_val.v.val_vec);
8325       break;
8326
8327     case dw_val_class_flag:
8328       CHECKSUM_ULEB128 (DW_FORM_flag);
8329       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8330       break;
8331
8332     case dw_val_class_str:
8333       CHECKSUM_ULEB128 (DW_FORM_string);
8334       CHECKSUM_STRING (AT_string (at));
8335       break;
8336
8337     case dw_val_class_addr:
8338       r = AT_addr (at);
8339       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8340       CHECKSUM_ULEB128 (DW_FORM_string);
8341       CHECKSUM_STRING (XSTR (r, 0));
8342       break;
8343
8344     case dw_val_class_offset:
8345       CHECKSUM_ULEB128 (DW_FORM_sdata);
8346       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8347       break;
8348
8349     case dw_val_class_loc:
8350       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8351         loc_checksum_ordered (loc, ctx);
8352       break;
8353
8354     case dw_val_class_fde_ref:
8355     case dw_val_class_lbl_id:
8356     case dw_val_class_lineptr:
8357     case dw_val_class_macptr:
8358       break;
8359
8360     case dw_val_class_file:
8361       CHECKSUM_ULEB128 (DW_FORM_string);
8362       CHECKSUM_STRING (AT_file (at)->filename);
8363       break;
8364
8365     case dw_val_class_data8:
8366       CHECKSUM (at->dw_attr_val.v.val_data8);
8367       break;
8368
8369     default:
8370       break;
8371     }
8372 }
8373
8374 struct checksum_attributes
8375 {
8376   dw_attr_ref at_name;
8377   dw_attr_ref at_type;
8378   dw_attr_ref at_friend;
8379   dw_attr_ref at_accessibility;
8380   dw_attr_ref at_address_class;
8381   dw_attr_ref at_allocated;
8382   dw_attr_ref at_artificial;
8383   dw_attr_ref at_associated;
8384   dw_attr_ref at_binary_scale;
8385   dw_attr_ref at_bit_offset;
8386   dw_attr_ref at_bit_size;
8387   dw_attr_ref at_bit_stride;
8388   dw_attr_ref at_byte_size;
8389   dw_attr_ref at_byte_stride;
8390   dw_attr_ref at_const_value;
8391   dw_attr_ref at_containing_type;
8392   dw_attr_ref at_count;
8393   dw_attr_ref at_data_location;
8394   dw_attr_ref at_data_member_location;
8395   dw_attr_ref at_decimal_scale;
8396   dw_attr_ref at_decimal_sign;
8397   dw_attr_ref at_default_value;
8398   dw_attr_ref at_digit_count;
8399   dw_attr_ref at_discr;
8400   dw_attr_ref at_discr_list;
8401   dw_attr_ref at_discr_value;
8402   dw_attr_ref at_encoding;
8403   dw_attr_ref at_endianity;
8404   dw_attr_ref at_explicit;
8405   dw_attr_ref at_is_optional;
8406   dw_attr_ref at_location;
8407   dw_attr_ref at_lower_bound;
8408   dw_attr_ref at_mutable;
8409   dw_attr_ref at_ordering;
8410   dw_attr_ref at_picture_string;
8411   dw_attr_ref at_prototyped;
8412   dw_attr_ref at_small;
8413   dw_attr_ref at_segment;
8414   dw_attr_ref at_string_length;
8415   dw_attr_ref at_threads_scaled;
8416   dw_attr_ref at_upper_bound;
8417   dw_attr_ref at_use_location;
8418   dw_attr_ref at_use_UTF8;
8419   dw_attr_ref at_variable_parameter;
8420   dw_attr_ref at_virtuality;
8421   dw_attr_ref at_visibility;
8422   dw_attr_ref at_vtable_elem_location;
8423 };
8424
8425 /* Collect the attributes that we will want to use for the checksum.  */
8426
8427 static void
8428 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8429 {
8430   dw_attr_ref a;
8431   unsigned ix;
8432
8433   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8434     {
8435       switch (a->dw_attr)
8436         {
8437         case DW_AT_name:
8438           attrs->at_name = a;
8439           break;
8440         case DW_AT_type:
8441           attrs->at_type = a;
8442           break;
8443         case DW_AT_friend:
8444           attrs->at_friend = a;
8445           break;
8446         case DW_AT_accessibility:
8447           attrs->at_accessibility = a;
8448           break;
8449         case DW_AT_address_class:
8450           attrs->at_address_class = a;
8451           break;
8452         case DW_AT_allocated:
8453           attrs->at_allocated = a;
8454           break;
8455         case DW_AT_artificial:
8456           attrs->at_artificial = a;
8457           break;
8458         case DW_AT_associated:
8459           attrs->at_associated = a;
8460           break;
8461         case DW_AT_binary_scale:
8462           attrs->at_binary_scale = a;
8463           break;
8464         case DW_AT_bit_offset:
8465           attrs->at_bit_offset = a;
8466           break;
8467         case DW_AT_bit_size:
8468           attrs->at_bit_size = a;
8469           break;
8470         case DW_AT_bit_stride:
8471           attrs->at_bit_stride = a;
8472           break;
8473         case DW_AT_byte_size:
8474           attrs->at_byte_size = a;
8475           break;
8476         case DW_AT_byte_stride:
8477           attrs->at_byte_stride = a;
8478           break;
8479         case DW_AT_const_value:
8480           attrs->at_const_value = a;
8481           break;
8482         case DW_AT_containing_type:
8483           attrs->at_containing_type = a;
8484           break;
8485         case DW_AT_count:
8486           attrs->at_count = a;
8487           break;
8488         case DW_AT_data_location:
8489           attrs->at_data_location = a;
8490           break;
8491         case DW_AT_data_member_location:
8492           attrs->at_data_member_location = a;
8493           break;
8494         case DW_AT_decimal_scale:
8495           attrs->at_decimal_scale = a;
8496           break;
8497         case DW_AT_decimal_sign:
8498           attrs->at_decimal_sign = a;
8499           break;
8500         case DW_AT_default_value:
8501           attrs->at_default_value = a;
8502           break;
8503         case DW_AT_digit_count:
8504           attrs->at_digit_count = a;
8505           break;
8506         case DW_AT_discr:
8507           attrs->at_discr = a;
8508           break;
8509         case DW_AT_discr_list:
8510           attrs->at_discr_list = a;
8511           break;
8512         case DW_AT_discr_value:
8513           attrs->at_discr_value = a;
8514           break;
8515         case DW_AT_encoding:
8516           attrs->at_encoding = a;
8517           break;
8518         case DW_AT_endianity:
8519           attrs->at_endianity = a;
8520           break;
8521         case DW_AT_explicit:
8522           attrs->at_explicit = a;
8523           break;
8524         case DW_AT_is_optional:
8525           attrs->at_is_optional = a;
8526           break;
8527         case DW_AT_location:
8528           attrs->at_location = a;
8529           break;
8530         case DW_AT_lower_bound:
8531           attrs->at_lower_bound = a;
8532           break;
8533         case DW_AT_mutable:
8534           attrs->at_mutable = a;
8535           break;
8536         case DW_AT_ordering:
8537           attrs->at_ordering = a;
8538           break;
8539         case DW_AT_picture_string:
8540           attrs->at_picture_string = a;
8541           break;
8542         case DW_AT_prototyped:
8543           attrs->at_prototyped = a;
8544           break;
8545         case DW_AT_small:
8546           attrs->at_small = a;
8547           break;
8548         case DW_AT_segment:
8549           attrs->at_segment = a;
8550           break;
8551         case DW_AT_string_length:
8552           attrs->at_string_length = a;
8553           break;
8554         case DW_AT_threads_scaled:
8555           attrs->at_threads_scaled = a;
8556           break;
8557         case DW_AT_upper_bound:
8558           attrs->at_upper_bound = a;
8559           break;
8560         case DW_AT_use_location:
8561           attrs->at_use_location = a;
8562           break;
8563         case DW_AT_use_UTF8:
8564           attrs->at_use_UTF8 = a;
8565           break;
8566         case DW_AT_variable_parameter:
8567           attrs->at_variable_parameter = a;
8568           break;
8569         case DW_AT_virtuality:
8570           attrs->at_virtuality = a;
8571           break;
8572         case DW_AT_visibility:
8573           attrs->at_visibility = a;
8574           break;
8575         case DW_AT_vtable_elem_location:
8576           attrs->at_vtable_elem_location = a;
8577           break;
8578         default:
8579           break;
8580         }
8581     }
8582 }
8583
8584 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8585
8586 static void
8587 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8588 {
8589   dw_die_ref c;
8590   dw_die_ref decl;
8591   struct checksum_attributes attrs;
8592
8593   CHECKSUM_ULEB128 ('D');
8594   CHECKSUM_ULEB128 (die->die_tag);
8595
8596   memset (&attrs, 0, sizeof (attrs));
8597
8598   decl = get_AT_ref (die, DW_AT_specification);
8599   if (decl != NULL)
8600     collect_checksum_attributes (&attrs, decl);
8601   collect_checksum_attributes (&attrs, die);
8602
8603   CHECKSUM_ATTR (attrs.at_name);
8604   CHECKSUM_ATTR (attrs.at_accessibility);
8605   CHECKSUM_ATTR (attrs.at_address_class);
8606   CHECKSUM_ATTR (attrs.at_allocated);
8607   CHECKSUM_ATTR (attrs.at_artificial);
8608   CHECKSUM_ATTR (attrs.at_associated);
8609   CHECKSUM_ATTR (attrs.at_binary_scale);
8610   CHECKSUM_ATTR (attrs.at_bit_offset);
8611   CHECKSUM_ATTR (attrs.at_bit_size);
8612   CHECKSUM_ATTR (attrs.at_bit_stride);
8613   CHECKSUM_ATTR (attrs.at_byte_size);
8614   CHECKSUM_ATTR (attrs.at_byte_stride);
8615   CHECKSUM_ATTR (attrs.at_const_value);
8616   CHECKSUM_ATTR (attrs.at_containing_type);
8617   CHECKSUM_ATTR (attrs.at_count);
8618   CHECKSUM_ATTR (attrs.at_data_location);
8619   CHECKSUM_ATTR (attrs.at_data_member_location);
8620   CHECKSUM_ATTR (attrs.at_decimal_scale);
8621   CHECKSUM_ATTR (attrs.at_decimal_sign);
8622   CHECKSUM_ATTR (attrs.at_default_value);
8623   CHECKSUM_ATTR (attrs.at_digit_count);
8624   CHECKSUM_ATTR (attrs.at_discr);
8625   CHECKSUM_ATTR (attrs.at_discr_list);
8626   CHECKSUM_ATTR (attrs.at_discr_value);
8627   CHECKSUM_ATTR (attrs.at_encoding);
8628   CHECKSUM_ATTR (attrs.at_endianity);
8629   CHECKSUM_ATTR (attrs.at_explicit);
8630   CHECKSUM_ATTR (attrs.at_is_optional);
8631   CHECKSUM_ATTR (attrs.at_location);
8632   CHECKSUM_ATTR (attrs.at_lower_bound);
8633   CHECKSUM_ATTR (attrs.at_mutable);
8634   CHECKSUM_ATTR (attrs.at_ordering);
8635   CHECKSUM_ATTR (attrs.at_picture_string);
8636   CHECKSUM_ATTR (attrs.at_prototyped);
8637   CHECKSUM_ATTR (attrs.at_small);
8638   CHECKSUM_ATTR (attrs.at_segment);
8639   CHECKSUM_ATTR (attrs.at_string_length);
8640   CHECKSUM_ATTR (attrs.at_threads_scaled);
8641   CHECKSUM_ATTR (attrs.at_upper_bound);
8642   CHECKSUM_ATTR (attrs.at_use_location);
8643   CHECKSUM_ATTR (attrs.at_use_UTF8);
8644   CHECKSUM_ATTR (attrs.at_variable_parameter);
8645   CHECKSUM_ATTR (attrs.at_virtuality);
8646   CHECKSUM_ATTR (attrs.at_visibility);
8647   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
8648   CHECKSUM_ATTR (attrs.at_type);
8649   CHECKSUM_ATTR (attrs.at_friend);
8650
8651   /* Checksum the child DIEs, except for nested types and member functions.  */
8652   c = die->die_child;
8653   if (c) do {
8654     dw_attr_ref name_attr;
8655
8656     c = c->die_sib;
8657     name_attr = get_AT (c, DW_AT_name);
8658     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
8659         && name_attr != NULL)
8660       {
8661         CHECKSUM_ULEB128 ('S');
8662         CHECKSUM_ULEB128 (c->die_tag);
8663         CHECKSUM_STRING (AT_string (name_attr));
8664       }
8665     else
8666       {
8667         /* Mark this DIE so it gets processed when unmarking.  */
8668         if (c->die_mark == 0)
8669           c->die_mark = -1;
8670         die_checksum_ordered (c, ctx, mark);
8671       }
8672   } while (c != die->die_child);
8673
8674   CHECKSUM_ULEB128 (0);
8675 }
8676
8677 #undef CHECKSUM
8678 #undef CHECKSUM_STRING
8679 #undef CHECKSUM_ATTR
8680 #undef CHECKSUM_LEB128
8681 #undef CHECKSUM_ULEB128
8682
8683 /* Generate the type signature for DIE.  This is computed by generating an
8684    MD5 checksum over the DIE's tag, its relevant attributes, and its
8685    children.  Attributes that are references to other DIEs are processed
8686    by recursion, using the MARK field to prevent infinite recursion.
8687    If the DIE is nested inside a namespace or another type, we also
8688    need to include that context in the signature.  The lower 64 bits
8689    of the resulting MD5 checksum comprise the signature.  */
8690
8691 static void
8692 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
8693 {
8694   int mark;
8695   const char *name;
8696   unsigned char checksum[16];
8697   struct md5_ctx ctx;
8698   dw_die_ref decl;
8699
8700   name = get_AT_string (die, DW_AT_name);
8701   decl = get_AT_ref (die, DW_AT_specification);
8702
8703   /* First, compute a signature for just the type name (and its surrounding
8704      context, if any.  This is stored in the type unit DIE for link-time
8705      ODR (one-definition rule) checking.  */
8706
8707   if (is_cxx() && name != NULL)
8708     {
8709       md5_init_ctx (&ctx);
8710
8711       /* Checksum the names of surrounding namespaces and structures.  */
8712       if (decl != NULL && decl->die_parent != NULL)
8713         checksum_die_context (decl->die_parent, &ctx);
8714
8715       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
8716       md5_process_bytes (name, strlen (name) + 1, &ctx);
8717       md5_finish_ctx (&ctx, checksum);
8718
8719       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
8720     }
8721
8722   /* Next, compute the complete type signature.  */
8723
8724   md5_init_ctx (&ctx);
8725   mark = 1;
8726   die->die_mark = mark;
8727
8728   /* Checksum the names of surrounding namespaces and structures.  */
8729   if (decl != NULL && decl->die_parent != NULL)
8730     checksum_die_context (decl->die_parent, &ctx);
8731
8732   /* Checksum the DIE and its children.  */
8733   die_checksum_ordered (die, &ctx, &mark);
8734   unmark_all_dies (die);
8735   md5_finish_ctx (&ctx, checksum);
8736
8737   /* Store the signature in the type node and link the type DIE and the
8738      type node together.  */
8739   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
8740           DWARF_TYPE_SIGNATURE_SIZE);
8741   die->die_id.die_type_node = type_node;
8742   type_node->type_die = die;
8743
8744   /* If the DIE is a specification, link its declaration to the type node
8745      as well.  */
8746   if (decl != NULL)
8747     decl->die_id.die_type_node = type_node;
8748 }
8749
8750 /* Do the location expressions look same?  */
8751 static inline int
8752 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
8753 {
8754   return loc1->dw_loc_opc == loc2->dw_loc_opc
8755          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
8756          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
8757 }
8758
8759 /* Do the values look the same?  */
8760 static int
8761 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
8762 {
8763   dw_loc_descr_ref loc1, loc2;
8764   rtx r1, r2;
8765
8766   if (v1->val_class != v2->val_class)
8767     return 0;
8768
8769   switch (v1->val_class)
8770     {
8771     case dw_val_class_const:
8772       return v1->v.val_int == v2->v.val_int;
8773     case dw_val_class_unsigned_const:
8774       return v1->v.val_unsigned == v2->v.val_unsigned;
8775     case dw_val_class_const_double:
8776       return v1->v.val_double.high == v2->v.val_double.high
8777              && v1->v.val_double.low == v2->v.val_double.low;
8778     case dw_val_class_vec:
8779       if (v1->v.val_vec.length != v2->v.val_vec.length
8780           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
8781         return 0;
8782       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
8783                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
8784         return 0;
8785       return 1;
8786     case dw_val_class_flag:
8787       return v1->v.val_flag == v2->v.val_flag;
8788     case dw_val_class_str:
8789       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
8790
8791     case dw_val_class_addr:
8792       r1 = v1->v.val_addr;
8793       r2 = v2->v.val_addr;
8794       if (GET_CODE (r1) != GET_CODE (r2))
8795         return 0;
8796       return !rtx_equal_p (r1, r2);
8797
8798     case dw_val_class_offset:
8799       return v1->v.val_offset == v2->v.val_offset;
8800
8801     case dw_val_class_loc:
8802       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
8803            loc1 && loc2;
8804            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
8805         if (!same_loc_p (loc1, loc2, mark))
8806           return 0;
8807       return !loc1 && !loc2;
8808
8809     case dw_val_class_die_ref:
8810       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
8811
8812     case dw_val_class_fde_ref:
8813     case dw_val_class_lbl_id:
8814     case dw_val_class_lineptr:
8815     case dw_val_class_macptr:
8816       return 1;
8817
8818     case dw_val_class_file:
8819       return v1->v.val_file == v2->v.val_file;
8820
8821     case dw_val_class_data8:
8822       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
8823
8824     default:
8825       return 1;
8826     }
8827 }
8828
8829 /* Do the attributes look the same?  */
8830
8831 static int
8832 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
8833 {
8834   if (at1->dw_attr != at2->dw_attr)
8835     return 0;
8836
8837   /* We don't care that this was compiled with a different compiler
8838      snapshot; if the output is the same, that's what matters. */
8839   if (at1->dw_attr == DW_AT_producer)
8840     return 1;
8841
8842   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
8843 }
8844
8845 /* Do the dies look the same?  */
8846
8847 static int
8848 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
8849 {
8850   dw_die_ref c1, c2;
8851   dw_attr_ref a1;
8852   unsigned ix;
8853
8854   /* To avoid infinite recursion.  */
8855   if (die1->die_mark)
8856     return die1->die_mark == die2->die_mark;
8857   die1->die_mark = die2->die_mark = ++(*mark);
8858
8859   if (die1->die_tag != die2->die_tag)
8860     return 0;
8861
8862   if (VEC_length (dw_attr_node, die1->die_attr)
8863       != VEC_length (dw_attr_node, die2->die_attr))
8864     return 0;
8865
8866   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
8867     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
8868       return 0;
8869
8870   c1 = die1->die_child;
8871   c2 = die2->die_child;
8872   if (! c1)
8873     {
8874       if (c2)
8875         return 0;
8876     }
8877   else
8878     for (;;)
8879       {
8880         if (!same_die_p (c1, c2, mark))
8881           return 0;
8882         c1 = c1->die_sib;
8883         c2 = c2->die_sib;
8884         if (c1 == die1->die_child)
8885           {
8886             if (c2 == die2->die_child)
8887               break;
8888             else
8889               return 0;
8890           }
8891     }
8892
8893   return 1;
8894 }
8895
8896 /* Do the dies look the same?  Wrapper around same_die_p.  */
8897
8898 static int
8899 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8900 {
8901   int mark = 0;
8902   int ret = same_die_p (die1, die2, &mark);
8903
8904   unmark_all_dies (die1);
8905   unmark_all_dies (die2);
8906
8907   return ret;
8908 }
8909
8910 /* The prefix to attach to symbols on DIEs in the current comdat debug
8911    info section.  */
8912 static char *comdat_symbol_id;
8913
8914 /* The index of the current symbol within the current comdat CU.  */
8915 static unsigned int comdat_symbol_number;
8916
8917 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8918    children, and set comdat_symbol_id accordingly.  */
8919
8920 static void
8921 compute_section_prefix (dw_die_ref unit_die)
8922 {
8923   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8924   const char *base = die_name ? lbasename (die_name) : "anonymous";
8925   char *name = XALLOCAVEC (char, strlen (base) + 64);
8926   char *p;
8927   int i, mark;
8928   unsigned char checksum[16];
8929   struct md5_ctx ctx;
8930
8931   /* Compute the checksum of the DIE, then append part of it as hex digits to
8932      the name filename of the unit.  */
8933
8934   md5_init_ctx (&ctx);
8935   mark = 0;
8936   die_checksum (unit_die, &ctx, &mark);
8937   unmark_all_dies (unit_die);
8938   md5_finish_ctx (&ctx, checksum);
8939
8940   sprintf (name, "%s.", base);
8941   clean_symbol_name (name);
8942
8943   p = name + strlen (name);
8944   for (i = 0; i < 4; i++)
8945     {
8946       sprintf (p, "%.2x", checksum[i]);
8947       p += 2;
8948     }
8949
8950   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
8951   comdat_symbol_number = 0;
8952 }
8953
8954 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8955
8956 static int
8957 is_type_die (dw_die_ref die)
8958 {
8959   switch (die->die_tag)
8960     {
8961     case DW_TAG_array_type:
8962     case DW_TAG_class_type:
8963     case DW_TAG_interface_type:
8964     case DW_TAG_enumeration_type:
8965     case DW_TAG_pointer_type:
8966     case DW_TAG_reference_type:
8967     case DW_TAG_string_type:
8968     case DW_TAG_structure_type:
8969     case DW_TAG_subroutine_type:
8970     case DW_TAG_union_type:
8971     case DW_TAG_ptr_to_member_type:
8972     case DW_TAG_set_type:
8973     case DW_TAG_subrange_type:
8974     case DW_TAG_base_type:
8975     case DW_TAG_const_type:
8976     case DW_TAG_file_type:
8977     case DW_TAG_packed_type:
8978     case DW_TAG_volatile_type:
8979     case DW_TAG_typedef:
8980       return 1;
8981     default:
8982       return 0;
8983     }
8984 }
8985
8986 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8987    Basically, we want to choose the bits that are likely to be shared between
8988    compilations (types) and leave out the bits that are specific to individual
8989    compilations (functions).  */
8990
8991 static int
8992 is_comdat_die (dw_die_ref c)
8993 {
8994   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8995      we do for stabs.  The advantage is a greater likelihood of sharing between
8996      objects that don't include headers in the same order (and therefore would
8997      put the base types in a different comdat).  jason 8/28/00 */
8998
8999   if (c->die_tag == DW_TAG_base_type)
9000     return 0;
9001
9002   if (c->die_tag == DW_TAG_pointer_type
9003       || c->die_tag == DW_TAG_reference_type
9004       || c->die_tag == DW_TAG_const_type
9005       || c->die_tag == DW_TAG_volatile_type)
9006     {
9007       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9008
9009       return t ? is_comdat_die (t) : 0;
9010     }
9011
9012   return is_type_die (c);
9013 }
9014
9015 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9016    compilation unit.  */
9017
9018 static int
9019 is_symbol_die (dw_die_ref c)
9020 {
9021   return (is_type_die (c)
9022           || is_declaration_die (c)
9023           || c->die_tag == DW_TAG_namespace
9024           || c->die_tag == DW_TAG_module);
9025 }
9026
9027 static char *
9028 gen_internal_sym (const char *prefix)
9029 {
9030   char buf[256];
9031
9032   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9033   return xstrdup (buf);
9034 }
9035
9036 /* Assign symbols to all worthy DIEs under DIE.  */
9037
9038 static void
9039 assign_symbol_names (dw_die_ref die)
9040 {
9041   dw_die_ref c;
9042
9043   if (is_symbol_die (die))
9044     {
9045       if (comdat_symbol_id)
9046         {
9047           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9048
9049           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9050                    comdat_symbol_id, comdat_symbol_number++);
9051           die->die_id.die_symbol = xstrdup (p);
9052         }
9053       else
9054         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9055     }
9056
9057   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9058 }
9059
9060 struct cu_hash_table_entry
9061 {
9062   dw_die_ref cu;
9063   unsigned min_comdat_num, max_comdat_num;
9064   struct cu_hash_table_entry *next;
9065 };
9066
9067 /* Routines to manipulate hash table of CUs.  */
9068 static hashval_t
9069 htab_cu_hash (const void *of)
9070 {
9071   const struct cu_hash_table_entry *const entry =
9072     (const struct cu_hash_table_entry *) of;
9073
9074   return htab_hash_string (entry->cu->die_id.die_symbol);
9075 }
9076
9077 static int
9078 htab_cu_eq (const void *of1, const void *of2)
9079 {
9080   const struct cu_hash_table_entry *const entry1 =
9081     (const struct cu_hash_table_entry *) of1;
9082   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9083
9084   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9085 }
9086
9087 static void
9088 htab_cu_del (void *what)
9089 {
9090   struct cu_hash_table_entry *next,
9091     *entry = (struct cu_hash_table_entry *) what;
9092
9093   while (entry)
9094     {
9095       next = entry->next;
9096       free (entry);
9097       entry = next;
9098     }
9099 }
9100
9101 /* Check whether we have already seen this CU and set up SYM_NUM
9102    accordingly.  */
9103 static int
9104 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9105 {
9106   struct cu_hash_table_entry dummy;
9107   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9108
9109   dummy.max_comdat_num = 0;
9110
9111   slot = (struct cu_hash_table_entry **)
9112     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9113         INSERT);
9114   entry = *slot;
9115
9116   for (; entry; last = entry, entry = entry->next)
9117     {
9118       if (same_die_p_wrap (cu, entry->cu))
9119         break;
9120     }
9121
9122   if (entry)
9123     {
9124       *sym_num = entry->min_comdat_num;
9125       return 1;
9126     }
9127
9128   entry = XCNEW (struct cu_hash_table_entry);
9129   entry->cu = cu;
9130   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9131   entry->next = *slot;
9132   *slot = entry;
9133
9134   return 0;
9135 }
9136
9137 /* Record SYM_NUM to record of CU in HTABLE.  */
9138 static void
9139 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9140 {
9141   struct cu_hash_table_entry **slot, *entry;
9142
9143   slot = (struct cu_hash_table_entry **)
9144     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9145         NO_INSERT);
9146   entry = *slot;
9147
9148   entry->max_comdat_num = sym_num;
9149 }
9150
9151 /* Traverse the DIE (which is always comp_unit_die), and set up
9152    additional compilation units for each of the include files we see
9153    bracketed by BINCL/EINCL.  */
9154
9155 static void
9156 break_out_includes (dw_die_ref die)
9157 {
9158   dw_die_ref c;
9159   dw_die_ref unit = NULL;
9160   limbo_die_node *node, **pnode;
9161   htab_t cu_hash_table;
9162
9163   c = die->die_child;
9164   if (c) do {
9165     dw_die_ref prev = c;
9166     c = c->die_sib;
9167     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9168            || (unit && is_comdat_die (c)))
9169       {
9170         dw_die_ref next = c->die_sib;
9171
9172         /* This DIE is for a secondary CU; remove it from the main one.  */
9173         remove_child_with_prev (c, prev);
9174
9175         if (c->die_tag == DW_TAG_GNU_BINCL)
9176           unit = push_new_compile_unit (unit, c);
9177         else if (c->die_tag == DW_TAG_GNU_EINCL)
9178           unit = pop_compile_unit (unit);
9179         else
9180           add_child_die (unit, c);
9181         c = next;
9182         if (c == die->die_child)
9183           break;
9184       }
9185   } while (c != die->die_child);
9186
9187 #if 0
9188   /* We can only use this in debugging, since the frontend doesn't check
9189      to make sure that we leave every include file we enter.  */
9190   gcc_assert (!unit);
9191 #endif
9192
9193   assign_symbol_names (die);
9194   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9195   for (node = limbo_die_list, pnode = &limbo_die_list;
9196        node;
9197        node = node->next)
9198     {
9199       int is_dupl;
9200
9201       compute_section_prefix (node->die);
9202       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9203                         &comdat_symbol_number);
9204       assign_symbol_names (node->die);
9205       if (is_dupl)
9206         *pnode = node->next;
9207       else
9208         {
9209           pnode = &node->next;
9210           record_comdat_symbol_number (node->die, cu_hash_table,
9211                 comdat_symbol_number);
9212         }
9213     }
9214   htab_delete (cu_hash_table);
9215 }
9216
9217 /* Return non-zero if this DIE is a declaration.  */
9218
9219 static int
9220 is_declaration_die (dw_die_ref die)
9221 {
9222   dw_attr_ref a;
9223   unsigned ix;
9224
9225   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9226     if (a->dw_attr == DW_AT_declaration)
9227       return 1;
9228
9229   return 0;
9230 }
9231
9232 /* Return non-zero if this is a type DIE that should be moved to a
9233    COMDAT .debug_types section.  */
9234
9235 static int
9236 should_move_die_to_comdat (dw_die_ref die)
9237 {
9238   switch (die->die_tag)
9239     {
9240     case DW_TAG_class_type:
9241     case DW_TAG_structure_type:
9242     case DW_TAG_enumeration_type:
9243     case DW_TAG_union_type:
9244       /* Don't move declarations or inlined instances.  */
9245       if (is_declaration_die (die) || get_AT (die, DW_AT_abstract_origin))
9246         return 0;
9247       return 1;
9248     case DW_TAG_array_type:
9249     case DW_TAG_interface_type:
9250     case DW_TAG_pointer_type:
9251     case DW_TAG_reference_type:
9252     case DW_TAG_string_type:
9253     case DW_TAG_subroutine_type:
9254     case DW_TAG_ptr_to_member_type:
9255     case DW_TAG_set_type:
9256     case DW_TAG_subrange_type:
9257     case DW_TAG_base_type:
9258     case DW_TAG_const_type:
9259     case DW_TAG_file_type:
9260     case DW_TAG_packed_type:
9261     case DW_TAG_volatile_type:
9262     case DW_TAG_typedef:
9263     default:
9264       return 0;
9265     }
9266 }
9267
9268 /* Make a clone of DIE.  */
9269
9270 static dw_die_ref
9271 clone_die (dw_die_ref die)
9272 {
9273   dw_die_ref clone;
9274   dw_attr_ref a;
9275   unsigned ix;
9276
9277   clone = GGC_CNEW (die_node);
9278   clone->die_tag = die->die_tag;
9279
9280   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9281     add_dwarf_attr (clone, a);
9282
9283   return clone;
9284 }
9285
9286 /* Make a clone of the tree rooted at DIE.  */
9287
9288 static dw_die_ref
9289 clone_tree (dw_die_ref die)
9290 {
9291   dw_die_ref c;
9292   dw_die_ref clone = clone_die (die);
9293
9294   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9295
9296   return clone;
9297 }
9298
9299 /* Make a clone of DIE as a declaration.  */
9300
9301 static dw_die_ref
9302 clone_as_declaration (dw_die_ref die)
9303 {
9304   dw_die_ref clone;
9305   dw_die_ref decl;
9306   dw_attr_ref a;
9307   unsigned ix;
9308
9309   /* If the DIE is already a declaration, just clone it.  */
9310   if (is_declaration_die (die))
9311     return clone_die (die);
9312
9313   /* If the DIE is a specification, just clone its declaration DIE.  */
9314   decl = get_AT_ref (die, DW_AT_specification);
9315   if (decl != NULL)
9316     return clone_die (decl);
9317
9318   clone = GGC_CNEW (die_node);
9319   clone->die_tag = die->die_tag;
9320
9321   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9322     {
9323       /* We don't want to copy over all attributes.
9324          For example we don't want DW_AT_byte_size because otherwise we will no
9325          longer have a declaration and GDB will treat it as a definition.  */
9326
9327       switch (a->dw_attr)
9328         {
9329         case DW_AT_artificial:
9330         case DW_AT_containing_type:
9331         case DW_AT_external:
9332         case DW_AT_name:
9333         case DW_AT_type:
9334         case DW_AT_virtuality:
9335         case DW_AT_MIPS_linkage_name:
9336           add_dwarf_attr (clone, a);
9337           break;
9338         case DW_AT_byte_size:
9339         default:
9340           break;
9341         }
9342     }
9343
9344   if (die->die_id.die_type_node)
9345     add_AT_die_ref (clone, DW_AT_signature, die);
9346
9347   add_AT_flag (clone, DW_AT_declaration, 1);
9348   return clone;
9349 }
9350
9351 /* Copy the declaration context to the new compile unit DIE.  This includes
9352    any surrounding namespace or type declarations.  If the DIE has an
9353    AT_specification attribute, it also includes attributes and children
9354    attached to the specification.  */
9355
9356 static void
9357 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9358 {
9359   dw_die_ref decl;
9360   dw_die_ref new_decl;
9361
9362   decl = get_AT_ref (die, DW_AT_specification);
9363   if (decl == NULL)
9364     decl = die;
9365   else
9366     {
9367       unsigned ix;
9368       dw_die_ref c;
9369       dw_attr_ref a;
9370
9371       /* Copy the type node pointer from the new DIE to the original
9372          declaration DIE so we can forward references later.  */
9373       decl->die_id.die_type_node = die->die_id.die_type_node;
9374
9375       remove_AT (die, DW_AT_specification);
9376
9377       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9378         {
9379           if (a->dw_attr != DW_AT_name
9380               && a->dw_attr != DW_AT_declaration
9381               && a->dw_attr != DW_AT_external)
9382             add_dwarf_attr (die, a);
9383         }
9384
9385       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9386     }
9387
9388   if (decl->die_parent != NULL
9389       && decl->die_parent->die_tag != DW_TAG_compile_unit
9390       && decl->die_parent->die_tag != DW_TAG_type_unit)
9391     {
9392       new_decl = copy_ancestor_tree (unit, decl, NULL);
9393       if (new_decl != NULL)
9394         {
9395           remove_AT (new_decl, DW_AT_signature);
9396           add_AT_specification (die, new_decl);
9397         }
9398     }
9399 }
9400
9401 /* Generate the skeleton ancestor tree for the given NODE, then clone
9402    the DIE and add the clone into the tree.  */
9403
9404 static void
9405 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9406 {
9407   if (node->new_die != NULL)
9408     return;
9409
9410   node->new_die = clone_as_declaration (node->old_die);
9411
9412   if (node->parent != NULL)
9413     {
9414       generate_skeleton_ancestor_tree (node->parent);
9415       add_child_die (node->parent->new_die, node->new_die);
9416     }
9417 }
9418
9419 /* Generate a skeleton tree of DIEs containing any declarations that are
9420    found in the original tree.  We traverse the tree looking for declaration
9421    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9422
9423 static void
9424 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9425 {
9426   skeleton_chain_node node;
9427   dw_die_ref c;
9428   dw_die_ref first;
9429   dw_die_ref prev = NULL;
9430   dw_die_ref next = NULL;
9431
9432   node.parent = parent;
9433
9434   first = c = parent->old_die->die_child;
9435   if (c)
9436     next = c->die_sib;
9437   if (c) do {
9438     if (prev == NULL || prev->die_sib == c)
9439       prev = c;
9440     c = next;
9441     next = (c == first ? NULL : c->die_sib);
9442     node.old_die = c;
9443     node.new_die = NULL;
9444     if (is_declaration_die (c))
9445       {
9446         /* Clone the existing DIE, move the original to the skeleton
9447            tree (which is in the main CU), and put the clone, with
9448            all the original's children, where the original came from.  */
9449         dw_die_ref clone = clone_die (c);
9450         move_all_children (c, clone);
9451
9452         replace_child (c, clone, prev);
9453         generate_skeleton_ancestor_tree (parent);
9454         add_child_die (parent->new_die, c);
9455         node.new_die = c;
9456         c = clone;
9457       }
9458     generate_skeleton_bottom_up (&node);
9459   } while (next != NULL);
9460 }
9461
9462 /* Wrapper function for generate_skeleton_bottom_up.  */
9463
9464 static dw_die_ref
9465 generate_skeleton (dw_die_ref die)
9466 {
9467   skeleton_chain_node node;
9468
9469   node.old_die = die;
9470   node.new_die = NULL;
9471   node.parent = NULL;
9472
9473   /* If this type definition is nested inside another type,
9474      always leave at least a declaration in its place.  */
9475   if (die->die_parent != NULL && is_type_die (die->die_parent))
9476     node.new_die = clone_as_declaration (die);
9477
9478   generate_skeleton_bottom_up (&node);
9479   return node.new_die;
9480 }
9481
9482 /* Remove the DIE from its parent, possibly replacing it with a cloned
9483    declaration.  The original DIE will be moved to a new compile unit
9484    so that existing references to it follow it to the new location.  If
9485    any of the original DIE's descendants is a declaration, we need to
9486    replace the original DIE with a skeleton tree and move the
9487    declarations back into the skeleton tree.  */
9488
9489 static dw_die_ref
9490 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9491 {
9492   dw_die_ref skeleton;
9493
9494   skeleton = generate_skeleton (child);
9495   if (skeleton == NULL)
9496     remove_child_with_prev (child, prev);
9497   else
9498     {
9499       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9500       replace_child (child, skeleton, prev);
9501     }
9502
9503   return skeleton;
9504 }
9505
9506 /* Traverse the DIE and set up additional .debug_types sections for each
9507    type worthy of being placed in a COMDAT section.  */
9508
9509 static void
9510 break_out_comdat_types (dw_die_ref die)
9511 {
9512   dw_die_ref c;
9513   dw_die_ref first;
9514   dw_die_ref prev = NULL;
9515   dw_die_ref next = NULL;
9516   dw_die_ref unit = NULL;
9517
9518   first = c = die->die_child;
9519   if (c)
9520     next = c->die_sib;
9521   if (c) do {
9522     if (prev == NULL || prev->die_sib == c)
9523       prev = c;
9524     c = next;
9525     next = (c == first ? NULL : c->die_sib);
9526     if (should_move_die_to_comdat (c))
9527       {
9528         dw_die_ref replacement;
9529         comdat_type_node_ref type_node;
9530
9531         /* Create a new type unit DIE as the root for the new tree, and
9532            add it to the list of comdat types.  */
9533         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9534         add_AT_unsigned (unit, DW_AT_language,
9535                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9536         type_node = GGC_CNEW (comdat_type_node);
9537         type_node->root_die = unit;
9538         type_node->next = comdat_type_list;
9539         comdat_type_list = type_node;
9540
9541         /* Generate the type signature.  */
9542         generate_type_signature (c, type_node);
9543
9544         /* Copy the declaration context, attributes, and children of the
9545            declaration into the new compile unit DIE.  */
9546         copy_declaration_context (unit, c);
9547
9548         /* Remove this DIE from the main CU.  */
9549         replacement = remove_child_or_replace_with_skeleton (c, prev);
9550
9551         /* Break out nested types into their own type units.  */
9552         break_out_comdat_types (c);
9553
9554         /* Add the DIE to the new compunit.  */
9555         add_child_die (unit, c);
9556
9557         if (replacement != NULL)
9558           c = replacement;
9559       }
9560     else if (c->die_tag == DW_TAG_namespace
9561              || c->die_tag == DW_TAG_class_type
9562              || c->die_tag == DW_TAG_structure_type
9563              || c->die_tag == DW_TAG_union_type)
9564       {
9565         /* Look for nested types that can be broken out.  */
9566         break_out_comdat_types (c);
9567       }
9568   } while (next != NULL);
9569 }
9570
9571 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
9572
9573 struct decl_table_entry
9574 {
9575   dw_die_ref orig;
9576   dw_die_ref copy;
9577 };
9578
9579 /* Routines to manipulate hash table of copied declarations.  */
9580
9581 static hashval_t
9582 htab_decl_hash (const void *of)
9583 {
9584   const struct decl_table_entry *const entry =
9585     (const struct decl_table_entry *) of;
9586
9587   return htab_hash_pointer (entry->orig);
9588 }
9589
9590 static int
9591 htab_decl_eq (const void *of1, const void *of2)
9592 {
9593   const struct decl_table_entry *const entry1 =
9594     (const struct decl_table_entry *) of1;
9595   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9596
9597   return entry1->orig == entry2;
9598 }
9599
9600 static void
9601 htab_decl_del (void *what)
9602 {
9603   struct decl_table_entry *entry = (struct decl_table_entry *) what;
9604
9605   free (entry);
9606 }
9607
9608 /* Copy DIE and its ancestors, up to, but not including, the compile unit 
9609    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
9610    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
9611    to check if the ancestor has already been copied into UNIT.  */
9612
9613 static dw_die_ref
9614 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9615 {
9616   dw_die_ref parent = die->die_parent;
9617   dw_die_ref new_parent = unit;
9618   dw_die_ref copy;
9619   void **slot = NULL;
9620   struct decl_table_entry *entry = NULL;
9621
9622   if (decl_table)
9623     {
9624       /* Check if the entry has already been copied to UNIT.  */
9625       slot = htab_find_slot_with_hash (decl_table, die,
9626                                        htab_hash_pointer (die), INSERT);
9627       if (*slot != HTAB_EMPTY_ENTRY)
9628         {
9629           entry = (struct decl_table_entry *) *slot;
9630           return entry->copy;
9631         }
9632
9633       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
9634       entry = XCNEW (struct decl_table_entry);
9635       entry->orig = die;
9636       entry->copy = NULL;
9637       *slot = entry;
9638     }
9639
9640   if (parent != NULL)
9641     {
9642       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
9643       if (spec != NULL)
9644         parent = spec;
9645       if (parent->die_tag != DW_TAG_compile_unit
9646           && parent->die_tag != DW_TAG_type_unit)
9647         new_parent = copy_ancestor_tree (unit, parent, decl_table);
9648     }
9649
9650   copy = clone_as_declaration (die);
9651   add_child_die (new_parent, copy);
9652
9653   if (decl_table != NULL)
9654     {
9655       /* Make sure the copy is marked as part of the type unit.  */
9656       copy->die_mark = 1;
9657       /* Record the pointer to the copy.  */
9658       entry->copy = copy;
9659     }
9660
9661   return copy;
9662 }
9663
9664 /* Walk the DIE and its children, looking for references to incomplete
9665    or trivial types that are unmarked (i.e., that are not in the current
9666    type_unit).  */
9667
9668 static void
9669 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9670 {
9671   dw_die_ref c;
9672   dw_attr_ref a;
9673   unsigned ix;
9674
9675   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9676     {
9677       if (AT_class (a) == dw_val_class_die_ref)
9678         {
9679           dw_die_ref targ = AT_ref (a);
9680           comdat_type_node_ref type_node = targ->die_id.die_type_node;
9681           void **slot;
9682           struct decl_table_entry *entry;
9683
9684           if (targ->die_mark != 0 || type_node != NULL)
9685             continue;
9686
9687           slot = htab_find_slot_with_hash (decl_table, targ,
9688                                            htab_hash_pointer (targ), INSERT);
9689
9690           if (*slot != HTAB_EMPTY_ENTRY)
9691             {
9692               /* TARG has already been copied, so we just need to
9693                  modify the reference to point to the copy.  */
9694               entry = (struct decl_table_entry *) *slot;
9695               a->dw_attr_val.v.val_die_ref.die = entry->copy;
9696             }
9697           else
9698             {
9699               dw_die_ref parent = unit;
9700               dw_die_ref copy = clone_tree (targ);
9701
9702               /* Make sure the cloned tree is marked as part of the
9703                  type unit.  */
9704               mark_dies (copy);
9705
9706               /* Record in DECL_TABLE that TARG has been copied.
9707                  Need to do this now, before the recursive call,
9708                  because DECL_TABLE may be expanded and SLOT
9709                  would no longer be a valid pointer.  */
9710               entry = XCNEW (struct decl_table_entry);
9711               entry->orig = targ;
9712               entry->copy = copy;
9713               *slot = entry;
9714
9715               /* If TARG has surrounding context, copy its ancestor tree
9716                  into the new type unit.  */
9717               if (targ->die_parent != NULL
9718                   && targ->die_parent->die_tag != DW_TAG_compile_unit
9719                   && targ->die_parent->die_tag != DW_TAG_type_unit)
9720                 parent = copy_ancestor_tree (unit, targ->die_parent,
9721                                              decl_table);
9722
9723               add_child_die (parent, copy);
9724               a->dw_attr_val.v.val_die_ref.die = copy;
9725
9726               /* Make sure the newly-copied DIE is walked.  If it was
9727                  installed in a previously-added context, it won't
9728                  get visited otherwise.  */
9729               if (parent != unit)
9730                 copy_decls_walk (unit, parent, decl_table);
9731             }
9732         }
9733     }
9734
9735   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
9736 }
9737
9738 /* Copy declarations for "unworthy" types into the new comdat section.
9739    Incomplete types, modified types, and certain other types aren't broken
9740    out into comdat sections of their own, so they don't have a signature,
9741    and we need to copy the declaration into the same section so that we
9742    don't have an external reference.  */
9743
9744 static void
9745 copy_decls_for_unworthy_types (dw_die_ref unit)
9746 {
9747   htab_t decl_table;
9748
9749   mark_dies (unit);
9750   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
9751   copy_decls_walk (unit, unit, decl_table);
9752   htab_delete (decl_table);
9753   unmark_dies (unit);
9754 }
9755
9756 /* Traverse the DIE and add a sibling attribute if it may have the
9757    effect of speeding up access to siblings.  To save some space,
9758    avoid generating sibling attributes for DIE's without children.  */
9759
9760 static void
9761 add_sibling_attributes (dw_die_ref die)
9762 {
9763   dw_die_ref c;
9764
9765   if (! die->die_child)
9766     return;
9767
9768   if (die->die_parent && die != die->die_parent->die_child)
9769     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
9770
9771   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
9772 }
9773
9774 /* Output all location lists for the DIE and its children.  */
9775
9776 static void
9777 output_location_lists (dw_die_ref die)
9778 {
9779   dw_die_ref c;
9780   dw_attr_ref a;
9781   unsigned ix;
9782
9783   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9784     if (AT_class (a) == dw_val_class_loc_list)
9785       output_loc_list (AT_loc_list (a));
9786
9787   FOR_EACH_CHILD (die, c, output_location_lists (c));
9788 }
9789
9790 /* The format of each DIE (and its attribute value pairs) is encoded in an
9791    abbreviation table.  This routine builds the abbreviation table and assigns
9792    a unique abbreviation id for each abbreviation entry.  The children of each
9793    die are visited recursively.  */
9794
9795 static void
9796 build_abbrev_table (dw_die_ref die)
9797 {
9798   unsigned long abbrev_id;
9799   unsigned int n_alloc;
9800   dw_die_ref c;
9801   dw_attr_ref a;
9802   unsigned ix;
9803
9804   /* Scan the DIE references, and mark as external any that refer to
9805      DIEs from other CUs (i.e. those which are not marked).  */
9806   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9807     if (AT_class (a) == dw_val_class_die_ref
9808         && AT_ref (a)->die_mark == 0)
9809       {
9810         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
9811         set_AT_ref_external (a, 1);
9812       }
9813
9814   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
9815     {
9816       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
9817       dw_attr_ref die_a, abbrev_a;
9818       unsigned ix;
9819       bool ok = true;
9820
9821       if (abbrev->die_tag != die->die_tag)
9822         continue;
9823       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9824         continue;
9825
9826       if (VEC_length (dw_attr_node, abbrev->die_attr)
9827           != VEC_length (dw_attr_node, die->die_attr))
9828         continue;
9829
9830       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
9831         {
9832           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
9833           if ((abbrev_a->dw_attr != die_a->dw_attr)
9834               || (value_format (abbrev_a) != value_format (die_a)))
9835             {
9836               ok = false;
9837               break;
9838             }
9839         }
9840       if (ok)
9841         break;
9842     }
9843
9844   if (abbrev_id >= abbrev_die_table_in_use)
9845     {
9846       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
9847         {
9848           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
9849           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
9850                                             n_alloc);
9851
9852           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
9853                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
9854           abbrev_die_table_allocated = n_alloc;
9855         }
9856
9857       ++abbrev_die_table_in_use;
9858       abbrev_die_table[abbrev_id] = die;
9859     }
9860
9861   die->die_abbrev = abbrev_id;
9862   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
9863 }
9864 \f
9865 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
9866
9867 static int
9868 constant_size (unsigned HOST_WIDE_INT value)
9869 {
9870   int log;
9871
9872   if (value == 0)
9873     log = 0;
9874   else
9875     log = floor_log2 (value);
9876
9877   log = log / 8;
9878   log = 1 << (floor_log2 (log) + 1);
9879
9880   return log;
9881 }
9882
9883 /* Return the size of a DIE as it is represented in the
9884    .debug_info section.  */
9885
9886 static unsigned long
9887 size_of_die (dw_die_ref die)
9888 {
9889   unsigned long size = 0;
9890   dw_attr_ref a;
9891   unsigned ix;
9892
9893   size += size_of_uleb128 (die->die_abbrev);
9894   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9895     {
9896       switch (AT_class (a))
9897         {
9898         case dw_val_class_addr:
9899           size += DWARF2_ADDR_SIZE;
9900           break;
9901         case dw_val_class_offset:
9902           size += DWARF_OFFSET_SIZE;
9903           break;
9904         case dw_val_class_loc:
9905           {
9906             unsigned long lsize = size_of_locs (AT_loc (a));
9907
9908             /* Block length.  */
9909             size += constant_size (lsize);
9910             size += lsize;
9911           }
9912           break;
9913         case dw_val_class_loc_list:
9914           size += DWARF_OFFSET_SIZE;
9915           break;
9916         case dw_val_class_range_list:
9917           size += DWARF_OFFSET_SIZE;
9918           break;
9919         case dw_val_class_const:
9920           size += size_of_sleb128 (AT_int (a));
9921           break;
9922         case dw_val_class_unsigned_const:
9923           size += constant_size (AT_unsigned (a));
9924           break;
9925         case dw_val_class_const_double:
9926           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9927           if (HOST_BITS_PER_WIDE_INT >= 64)
9928             size++; /* block */
9929           break;
9930         case dw_val_class_vec:
9931           size += constant_size (a->dw_attr_val.v.val_vec.length
9932                                  * a->dw_attr_val.v.val_vec.elt_size)
9933                   + a->dw_attr_val.v.val_vec.length
9934                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
9935           break;
9936         case dw_val_class_flag:
9937           size += 1;
9938           break;
9939         case dw_val_class_die_ref:
9940           if (AT_ref_external (a))
9941             {
9942               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
9943                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
9944                  is sized by target address length, whereas in DWARF3
9945                  it's always sized as an offset.  */
9946               if (dwarf_version >= 4)
9947                 size += DWARF_TYPE_SIGNATURE_SIZE;
9948               else if (dwarf_version == 2)
9949                 size += DWARF2_ADDR_SIZE;
9950               else
9951                 size += DWARF_OFFSET_SIZE;
9952             }
9953           else
9954             size += DWARF_OFFSET_SIZE;
9955           break;
9956         case dw_val_class_fde_ref:
9957           size += DWARF_OFFSET_SIZE;
9958           break;
9959         case dw_val_class_lbl_id:
9960           size += DWARF2_ADDR_SIZE;
9961           break;
9962         case dw_val_class_lineptr:
9963         case dw_val_class_macptr:
9964           size += DWARF_OFFSET_SIZE;
9965           break;
9966         case dw_val_class_str:
9967           if (AT_string_form (a) == DW_FORM_strp)
9968             size += DWARF_OFFSET_SIZE;
9969           else
9970             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9971           break;
9972         case dw_val_class_file:
9973           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9974           break;
9975         case dw_val_class_data8:
9976           size += 8;
9977           break;
9978         default:
9979           gcc_unreachable ();
9980         }
9981     }
9982
9983   return size;
9984 }
9985
9986 /* Size the debugging information associated with a given DIE.  Visits the
9987    DIE's children recursively.  Updates the global variable next_die_offset, on
9988    each time through.  Uses the current value of next_die_offset to update the
9989    die_offset field in each DIE.  */
9990
9991 static void
9992 calc_die_sizes (dw_die_ref die)
9993 {
9994   dw_die_ref c;
9995
9996   die->die_offset = next_die_offset;
9997   next_die_offset += size_of_die (die);
9998
9999   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10000
10001   if (die->die_child != NULL)
10002     /* Count the null byte used to terminate sibling lists.  */
10003     next_die_offset += 1;
10004 }
10005
10006 /* Set the marks for a die and its children.  We do this so
10007    that we know whether or not a reference needs to use FORM_ref_addr; only
10008    DIEs in the same CU will be marked.  We used to clear out the offset
10009    and use that as the flag, but ran into ordering problems.  */
10010
10011 static void
10012 mark_dies (dw_die_ref die)
10013 {
10014   dw_die_ref c;
10015
10016   gcc_assert (!die->die_mark);
10017
10018   die->die_mark = 1;
10019   FOR_EACH_CHILD (die, c, mark_dies (c));
10020 }
10021
10022 /* Clear the marks for a die and its children.  */
10023
10024 static void
10025 unmark_dies (dw_die_ref die)
10026 {
10027   dw_die_ref c;
10028
10029   if (dwarf_version < 4)
10030     gcc_assert (die->die_mark);
10031
10032   die->die_mark = 0;
10033   FOR_EACH_CHILD (die, c, unmark_dies (c));
10034 }
10035
10036 /* Clear the marks for a die, its children and referred dies.  */
10037
10038 static void
10039 unmark_all_dies (dw_die_ref die)
10040 {
10041   dw_die_ref c;
10042   dw_attr_ref a;
10043   unsigned ix;
10044
10045   if (!die->die_mark)
10046     return;
10047   die->die_mark = 0;
10048
10049   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10050
10051   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10052     if (AT_class (a) == dw_val_class_die_ref)
10053       unmark_all_dies (AT_ref (a));
10054 }
10055
10056 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10057    generated for the compilation unit.  */
10058
10059 static unsigned long
10060 size_of_pubnames (VEC (pubname_entry, gc) * names)
10061 {
10062   unsigned long size;
10063   unsigned i;
10064   pubname_ref p;
10065
10066   size = DWARF_PUBNAMES_HEADER_SIZE;
10067   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10068     if (names != pubtype_table
10069         || p->die->die_offset != 0
10070         || !flag_eliminate_unused_debug_types)
10071       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10072
10073   size += DWARF_OFFSET_SIZE;
10074   return size;
10075 }
10076
10077 /* Return the size of the information in the .debug_aranges section.  */
10078
10079 static unsigned long
10080 size_of_aranges (void)
10081 {
10082   unsigned long size;
10083
10084   size = DWARF_ARANGES_HEADER_SIZE;
10085
10086   /* Count the address/length pair for this compilation unit.  */
10087   if (text_section_used)
10088     size += 2 * DWARF2_ADDR_SIZE;
10089   if (cold_text_section_used)
10090     size += 2 * DWARF2_ADDR_SIZE;
10091   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10092
10093   /* Count the two zero words used to terminated the address range table.  */
10094   size += 2 * DWARF2_ADDR_SIZE;
10095   return size;
10096 }
10097 \f
10098 /* Select the encoding of an attribute value.  */
10099
10100 static enum dwarf_form
10101 value_format (dw_attr_ref a)
10102 {
10103   switch (a->dw_attr_val.val_class)
10104     {
10105     case dw_val_class_addr:
10106       /* Only very few attributes allow DW_FORM_addr.  */
10107       switch (a->dw_attr)
10108         {
10109         case DW_AT_low_pc:
10110         case DW_AT_high_pc:
10111         case DW_AT_entry_pc:
10112         case DW_AT_trampoline:
10113           return DW_FORM_addr;
10114         default:
10115           break;
10116         }
10117       switch (DWARF2_ADDR_SIZE)
10118         {
10119         case 1:
10120           return DW_FORM_data1;
10121         case 2:
10122           return DW_FORM_data2;
10123         case 4:
10124           return DW_FORM_data4;
10125         case 8:
10126           return DW_FORM_data8;
10127         default:
10128           gcc_unreachable ();
10129         }
10130     case dw_val_class_range_list:
10131     case dw_val_class_offset:
10132     case dw_val_class_loc_list:
10133       switch (DWARF_OFFSET_SIZE)
10134         {
10135         case 4:
10136           return DW_FORM_data4;
10137         case 8:
10138           return DW_FORM_data8;
10139         default:
10140           gcc_unreachable ();
10141         }
10142     case dw_val_class_loc:
10143       switch (constant_size (size_of_locs (AT_loc (a))))
10144         {
10145         case 1:
10146           return DW_FORM_block1;
10147         case 2:
10148           return DW_FORM_block2;
10149         default:
10150           gcc_unreachable ();
10151         }
10152     case dw_val_class_const:
10153       return DW_FORM_sdata;
10154     case dw_val_class_unsigned_const:
10155       switch (constant_size (AT_unsigned (a)))
10156         {
10157         case 1:
10158           return DW_FORM_data1;
10159         case 2:
10160           return DW_FORM_data2;
10161         case 4:
10162           return DW_FORM_data4;
10163         case 8:
10164           return DW_FORM_data8;
10165         default:
10166           gcc_unreachable ();
10167         }
10168     case dw_val_class_const_double:
10169       switch (HOST_BITS_PER_WIDE_INT)
10170         {
10171         case 8:
10172           return DW_FORM_data2;
10173         case 16:
10174           return DW_FORM_data4;
10175         case 32:
10176           return DW_FORM_data8;
10177         case 64:
10178         default:
10179           return DW_FORM_block1;
10180         }
10181     case dw_val_class_vec:
10182       switch (constant_size (a->dw_attr_val.v.val_vec.length
10183                              * a->dw_attr_val.v.val_vec.elt_size))
10184         {
10185         case 1:
10186           return DW_FORM_block1;
10187         case 2:
10188           return DW_FORM_block2;
10189         case 4:
10190           return DW_FORM_block4;
10191         default:
10192           gcc_unreachable ();
10193         }
10194     case dw_val_class_flag:
10195       return DW_FORM_flag;
10196     case dw_val_class_die_ref:
10197       if (AT_ref_external (a))
10198         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10199       else
10200         return DW_FORM_ref;
10201     case dw_val_class_fde_ref:
10202       return DW_FORM_data;
10203     case dw_val_class_lbl_id:
10204       return DW_FORM_addr;
10205     case dw_val_class_lineptr:
10206     case dw_val_class_macptr:
10207       return DW_FORM_data;
10208     case dw_val_class_str:
10209       return AT_string_form (a);
10210     case dw_val_class_file:
10211       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10212         {
10213         case 1:
10214           return DW_FORM_data1;
10215         case 2:
10216           return DW_FORM_data2;
10217         case 4:
10218           return DW_FORM_data4;
10219         default:
10220           gcc_unreachable ();
10221         }
10222
10223     case dw_val_class_data8:
10224       return DW_FORM_data8;
10225
10226     default:
10227       gcc_unreachable ();
10228     }
10229 }
10230
10231 /* Output the encoding of an attribute value.  */
10232
10233 static void
10234 output_value_format (dw_attr_ref a)
10235 {
10236   enum dwarf_form form = value_format (a);
10237
10238   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10239 }
10240
10241 /* Output the .debug_abbrev section which defines the DIE abbreviation
10242    table.  */
10243
10244 static void
10245 output_abbrev_section (void)
10246 {
10247   unsigned long abbrev_id;
10248
10249   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10250     {
10251       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10252       unsigned ix;
10253       dw_attr_ref a_attr;
10254
10255       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10256       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10257                                    dwarf_tag_name (abbrev->die_tag));
10258
10259       if (abbrev->die_child != NULL)
10260         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10261       else
10262         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10263
10264       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10265            ix++)
10266         {
10267           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10268                                        dwarf_attr_name (a_attr->dw_attr));
10269           output_value_format (a_attr);
10270         }
10271
10272       dw2_asm_output_data (1, 0, NULL);
10273       dw2_asm_output_data (1, 0, NULL);
10274     }
10275
10276   /* Terminate the table.  */
10277   dw2_asm_output_data (1, 0, NULL);
10278 }
10279
10280 /* Output a symbol we can use to refer to this DIE from another CU.  */
10281
10282 static inline void
10283 output_die_symbol (dw_die_ref die)
10284 {
10285   char *sym = die->die_id.die_symbol;
10286
10287   if (sym == 0)
10288     return;
10289
10290   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10291     /* We make these global, not weak; if the target doesn't support
10292        .linkonce, it doesn't support combining the sections, so debugging
10293        will break.  */
10294     targetm.asm_out.globalize_label (asm_out_file, sym);
10295
10296   ASM_OUTPUT_LABEL (asm_out_file, sym);
10297 }
10298
10299 /* Return a new location list, given the begin and end range, and the
10300    expression. gensym tells us whether to generate a new internal symbol for
10301    this location list node, which is done for the head of the list only.  */
10302
10303 static inline dw_loc_list_ref
10304 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10305               const char *section, unsigned int gensym)
10306 {
10307   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
10308
10309   retlist->begin = begin;
10310   retlist->end = end;
10311   retlist->expr = expr;
10312   retlist->section = section;
10313   if (gensym)
10314     retlist->ll_symbol = gen_internal_sym ("LLST");
10315
10316   return retlist;
10317 }
10318
10319 /* Add a location description expression to a location list.  */
10320
10321 static inline void
10322 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
10323                            const char *begin, const char *end,
10324                            const char *section)
10325 {
10326   dw_loc_list_ref *d;
10327
10328   /* Find the end of the chain.  */
10329   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
10330     ;
10331
10332   /* Add a new location list node to the list.  */
10333   *d = new_loc_list (descr, begin, end, section, 0);
10334 }
10335
10336 /* Output the location list given to us.  */
10337
10338 static void
10339 output_loc_list (dw_loc_list_ref list_head)
10340 {
10341   dw_loc_list_ref curr = list_head;
10342
10343   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10344
10345   /* Walk the location list, and output each range + expression.  */
10346   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10347     {
10348       unsigned long size;
10349       /* Don't output an entry that starts and ends at the same address.  */
10350       if (strcmp (curr->begin, curr->end) == 0)
10351         continue;
10352       if (!have_multiple_function_sections)
10353         {
10354           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10355                                 "Location list begin address (%s)",
10356                                 list_head->ll_symbol);
10357           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10358                                 "Location list end address (%s)",
10359                                 list_head->ll_symbol);
10360         }
10361       else
10362         {
10363           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10364                                "Location list begin address (%s)",
10365                                list_head->ll_symbol);
10366           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10367                                "Location list end address (%s)",
10368                                list_head->ll_symbol);
10369         }
10370       size = size_of_locs (curr->expr);
10371
10372       /* Output the block length for this list of location operations.  */
10373       gcc_assert (size <= 0xffff);
10374       dw2_asm_output_data (2, size, "%s", "Location expression size");
10375
10376       output_loc_sequence (curr->expr);
10377     }
10378
10379   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10380                        "Location list terminator begin (%s)",
10381                        list_head->ll_symbol);
10382   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10383                        "Location list terminator end (%s)",
10384                        list_head->ll_symbol);
10385 }
10386
10387 /* Output a type signature.  */
10388
10389 static inline void
10390 output_signature (const char *sig, const char *name)
10391 {
10392   int i;
10393
10394   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10395     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10396 }
10397
10398 /* Output the DIE and its attributes.  Called recursively to generate
10399    the definitions of each child DIE.  */
10400
10401 static void
10402 output_die (dw_die_ref die)
10403 {
10404   dw_attr_ref a;
10405   dw_die_ref c;
10406   unsigned long size;
10407   unsigned ix;
10408
10409   /* If someone in another CU might refer to us, set up a symbol for
10410      them to point to.  */
10411   if (dwarf_version < 4 && die->die_id.die_symbol)
10412     output_die_symbol (die);
10413
10414   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
10415                                (unsigned long)die->die_offset,
10416                                dwarf_tag_name (die->die_tag));
10417
10418   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10419     {
10420       const char *name = dwarf_attr_name (a->dw_attr);
10421
10422       switch (AT_class (a))
10423         {
10424         case dw_val_class_addr:
10425           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10426           break;
10427
10428         case dw_val_class_offset:
10429           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10430                                "%s", name);
10431           break;
10432
10433         case dw_val_class_range_list:
10434           {
10435             char *p = strchr (ranges_section_label, '\0');
10436
10437             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10438                      a->dw_attr_val.v.val_offset);
10439             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10440                                    debug_ranges_section, "%s", name);
10441             *p = '\0';
10442           }
10443           break;
10444
10445         case dw_val_class_loc:
10446           size = size_of_locs (AT_loc (a));
10447
10448           /* Output the block length for this list of location operations.  */
10449           dw2_asm_output_data (constant_size (size), size, "%s", name);
10450
10451           output_loc_sequence (AT_loc (a));
10452           break;
10453
10454         case dw_val_class_const:
10455           /* ??? It would be slightly more efficient to use a scheme like is
10456              used for unsigned constants below, but gdb 4.x does not sign
10457              extend.  Gdb 5.x does sign extend.  */
10458           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10459           break;
10460
10461         case dw_val_class_unsigned_const:
10462           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10463                                AT_unsigned (a), "%s", name);
10464           break;
10465
10466         case dw_val_class_const_double:
10467           {
10468             unsigned HOST_WIDE_INT first, second;
10469
10470             if (HOST_BITS_PER_WIDE_INT >= 64)
10471               dw2_asm_output_data (1,
10472                                    2 * HOST_BITS_PER_WIDE_INT
10473                                    / HOST_BITS_PER_CHAR,
10474                                    NULL);
10475
10476             if (WORDS_BIG_ENDIAN)
10477               {
10478                 first = a->dw_attr_val.v.val_double.high;
10479                 second = a->dw_attr_val.v.val_double.low;
10480               }
10481             else
10482               {
10483                 first = a->dw_attr_val.v.val_double.low;
10484                 second = a->dw_attr_val.v.val_double.high;
10485               }
10486
10487             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10488                                  first, name);
10489             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10490                                  second, NULL);
10491           }
10492           break;
10493
10494         case dw_val_class_vec:
10495           {
10496             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10497             unsigned int len = a->dw_attr_val.v.val_vec.length;
10498             unsigned int i;
10499             unsigned char *p;
10500
10501             dw2_asm_output_data (constant_size (len * elt_size),
10502                                  len * elt_size, "%s", name);
10503             if (elt_size > sizeof (HOST_WIDE_INT))
10504               {
10505                 elt_size /= 2;
10506                 len *= 2;
10507               }
10508             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10509                  i < len;
10510                  i++, p += elt_size)
10511               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10512                                    "fp or vector constant word %u", i);
10513             break;
10514           }
10515
10516         case dw_val_class_flag:
10517           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10518           break;
10519
10520         case dw_val_class_loc_list:
10521           {
10522             char *sym = AT_loc_list (a)->ll_symbol;
10523
10524             gcc_assert (sym);
10525             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10526                                    "%s", name);
10527           }
10528           break;
10529
10530         case dw_val_class_die_ref:
10531           if (AT_ref_external (a))
10532             {
10533               if (dwarf_version >= 4)
10534                 {
10535                   comdat_type_node_ref type_node =
10536                     AT_ref (a)->die_id.die_type_node;
10537
10538                   gcc_assert (type_node);
10539                   output_signature (type_node->signature, name);
10540                 }
10541               else
10542                 {
10543                   char *sym = AT_ref (a)->die_id.die_symbol;
10544                   int size;
10545
10546                   gcc_assert (sym);
10547                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
10548                      length, whereas in DWARF3 it's always sized as an
10549                      offset.  */
10550                   if (dwarf_version == 2)
10551                     size = DWARF2_ADDR_SIZE;
10552                   else
10553                     size = DWARF_OFFSET_SIZE;
10554                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10555                                          name);
10556                 }
10557             }
10558           else
10559             {
10560               gcc_assert (AT_ref (a)->die_offset);
10561               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10562                                    "%s", name);
10563             }
10564           break;
10565
10566         case dw_val_class_fde_ref:
10567           {
10568             char l1[20];
10569
10570             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10571                                          a->dw_attr_val.v.val_fde_index * 2);
10572             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10573                                    "%s", name);
10574           }
10575           break;
10576
10577         case dw_val_class_lbl_id:
10578           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10579           break;
10580
10581         case dw_val_class_lineptr:
10582           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10583                                  debug_line_section, "%s", name);
10584           break;
10585
10586         case dw_val_class_macptr:
10587           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10588                                  debug_macinfo_section, "%s", name);
10589           break;
10590
10591         case dw_val_class_str:
10592           if (AT_string_form (a) == DW_FORM_strp)
10593             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10594                                    a->dw_attr_val.v.val_str->label,
10595                                    debug_str_section,
10596                                    "%s: \"%s\"", name, AT_string (a));
10597           else
10598             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10599           break;
10600
10601         case dw_val_class_file:
10602           {
10603             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10604
10605             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10606                                  a->dw_attr_val.v.val_file->filename);
10607             break;
10608           }
10609
10610         case dw_val_class_data8:
10611           {
10612             int i;
10613
10614             for (i = 0; i < 8; i++)
10615               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10616                                    i == 0 ? "%s" : NULL, name);
10617             break;
10618           }
10619
10620         default:
10621           gcc_unreachable ();
10622         }
10623     }
10624
10625   FOR_EACH_CHILD (die, c, output_die (c));
10626
10627   /* Add null byte to terminate sibling list.  */
10628   if (die->die_child != NULL)
10629     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
10630                          (unsigned long) die->die_offset);
10631 }
10632
10633 /* Output the compilation unit that appears at the beginning of the
10634    .debug_info section, and precedes the DIE descriptions.  */
10635
10636 static void
10637 output_compilation_unit_header (void)
10638 {
10639   int ver = dwarf_version;
10640
10641   /* Don't mark the output as DWARF-4 until we make full use of the
10642      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
10643      selects only a few extensions from the DWARF-4 spec.  */
10644   if (ver > 3)
10645     ver = 3;
10646   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10647     dw2_asm_output_data (4, 0xffffffff,
10648       "Initial length escape value indicating 64-bit DWARF extension");
10649   dw2_asm_output_data (DWARF_OFFSET_SIZE,
10650                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10651                        "Length of Compilation Unit Info");
10652   dw2_asm_output_data (2, ver, "DWARF version number");
10653   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10654                          debug_abbrev_section,
10655                          "Offset Into Abbrev. Section");
10656   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10657 }
10658
10659 /* Output the compilation unit DIE and its children.  */
10660
10661 static void
10662 output_comp_unit (dw_die_ref die, int output_if_empty)
10663 {
10664   const char *secname;
10665   char *oldsym, *tmp;
10666
10667   /* Unless we are outputting main CU, we may throw away empty ones.  */
10668   if (!output_if_empty && die->die_child == NULL)
10669     return;
10670
10671   /* Even if there are no children of this DIE, we must output the information
10672      about the compilation unit.  Otherwise, on an empty translation unit, we
10673      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
10674      will then complain when examining the file.  First mark all the DIEs in
10675      this CU so we know which get local refs.  */
10676   mark_dies (die);
10677
10678   build_abbrev_table (die);
10679
10680   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10681   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10682   calc_die_sizes (die);
10683
10684   oldsym = die->die_id.die_symbol;
10685   if (oldsym)
10686     {
10687       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10688
10689       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10690       secname = tmp;
10691       die->die_id.die_symbol = NULL;
10692       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10693     }
10694   else
10695     switch_to_section (debug_info_section);
10696
10697   /* Output debugging information.  */
10698   output_compilation_unit_header ();
10699   output_die (die);
10700
10701   /* Leave the marks on the main CU, so we can check them in
10702      output_pubnames.  */
10703   if (oldsym)
10704     {
10705       unmark_dies (die);
10706       die->die_id.die_symbol = oldsym;
10707     }
10708 }
10709
10710 /* Output a comdat type unit DIE and its children.  */
10711
10712 static void
10713 output_comdat_type_unit (comdat_type_node *node)
10714 {
10715   const char *secname;
10716   char *tmp;
10717   int i;
10718 #if defined (OBJECT_FORMAT_ELF)
10719   tree comdat_key;
10720 #endif
10721
10722   /* First mark all the DIEs in this CU so we know which get local refs.  */
10723   mark_dies (node->root_die);
10724
10725   build_abbrev_table (node->root_die);
10726
10727   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10728   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10729   calc_die_sizes (node->root_die);
10730
10731 #if defined (OBJECT_FORMAT_ELF)
10732   secname = ".debug_types";
10733   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10734   sprintf (tmp, "wt.");
10735   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10736     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10737   comdat_key = get_identifier (tmp);
10738   targetm.asm_out.named_section (secname,
10739                                  SECTION_DEBUG | SECTION_LINKONCE,
10740                                  comdat_key);
10741 #else
10742   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10743   sprintf (tmp, ".gnu.linkonce.wt.");
10744   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10745     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10746   secname = tmp;
10747   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10748 #endif
10749
10750   /* Output debugging information.  */
10751   output_compilation_unit_header ();
10752   output_signature (node->signature, "Type Signature");
10753   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10754                        "Offset to Type DIE");
10755   output_die (node->root_die);
10756
10757   unmark_dies (node->root_die);
10758 }
10759
10760 /* Return the DWARF2/3 pubname associated with a decl.  */
10761
10762 static const char *
10763 dwarf2_name (tree decl, int scope)
10764 {
10765   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10766 }
10767
10768 /* Add a new entry to .debug_pubnames if appropriate.  */
10769
10770 static void
10771 add_pubname_string (const char *str, dw_die_ref die)
10772 {
10773   pubname_entry e;
10774
10775   e.die = die;
10776   e.name = xstrdup (str);
10777   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
10778 }
10779
10780 static void
10781 add_pubname (tree decl, dw_die_ref die)
10782 {
10783   if (TREE_PUBLIC (decl))
10784     {
10785       const char *name = dwarf2_name (decl, 1);
10786       if (name)
10787         add_pubname_string (name, die);
10788     }
10789 }
10790
10791 /* Add a new entry to .debug_pubtypes if appropriate.  */
10792
10793 static void
10794 add_pubtype (tree decl, dw_die_ref die)
10795 {
10796   pubname_entry e;
10797
10798   e.name = NULL;
10799   if ((TREE_PUBLIC (decl)
10800        || die->die_parent == comp_unit_die)
10801       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10802     {
10803       e.die = die;
10804       if (TYPE_P (decl))
10805         {
10806           if (TYPE_NAME (decl))
10807             {
10808               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
10809                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
10810               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
10811                        && DECL_NAME (TYPE_NAME (decl)))
10812                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
10813               else
10814                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
10815             }
10816         }
10817       else
10818         {
10819           e.name = dwarf2_name (decl, 1);
10820           if (e.name)
10821             e.name = xstrdup (e.name);
10822         }
10823
10824       /* If we don't have a name for the type, there's no point in adding
10825          it to the table.  */
10826       if (e.name && e.name[0] != '\0')
10827         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
10828     }
10829 }
10830
10831 /* Output the public names table used to speed up access to externally
10832    visible names; or the public types table used to find type definitions.  */
10833
10834 static void
10835 output_pubnames (VEC (pubname_entry, gc) * names)
10836 {
10837   unsigned i;
10838   unsigned long pubnames_length = size_of_pubnames (names);
10839   pubname_ref pub;
10840
10841   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10842     dw2_asm_output_data (4, 0xffffffff,
10843       "Initial length escape value indicating 64-bit DWARF extension");
10844   if (names == pubname_table)
10845     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10846                          "Length of Public Names Info");
10847   else
10848     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10849                          "Length of Public Type Names Info");
10850   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
10851   dw2_asm_output_data (2, 2, "DWARF Version");
10852   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10853                          debug_info_section,
10854                          "Offset of Compilation Unit Info");
10855   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10856                        "Compilation Unit Length");
10857
10858   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
10859     {
10860       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
10861       if (names == pubname_table)
10862         gcc_assert (pub->die->die_mark);
10863
10864       if (names != pubtype_table
10865           || pub->die->die_offset != 0
10866           || !flag_eliminate_unused_debug_types)
10867         {
10868           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
10869                                "DIE offset");
10870
10871           dw2_asm_output_nstring (pub->name, -1, "external name");
10872         }
10873     }
10874
10875   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10876 }
10877
10878 /* Add a new entry to .debug_aranges if appropriate.  */
10879
10880 static void
10881 add_arange (tree decl, dw_die_ref die)
10882 {
10883   if (! DECL_SECTION_NAME (decl))
10884     return;
10885
10886   if (arange_table_in_use == arange_table_allocated)
10887     {
10888       arange_table_allocated += ARANGE_TABLE_INCREMENT;
10889       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
10890                                     arange_table_allocated);
10891       memset (arange_table + arange_table_in_use, 0,
10892               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
10893     }
10894
10895   arange_table[arange_table_in_use++] = die;
10896 }
10897
10898 /* Output the information that goes into the .debug_aranges table.
10899    Namely, define the beginning and ending address range of the
10900    text section generated for this compilation unit.  */
10901
10902 static void
10903 output_aranges (void)
10904 {
10905   unsigned i;
10906   unsigned long aranges_length = size_of_aranges ();
10907
10908   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10909     dw2_asm_output_data (4, 0xffffffff,
10910       "Initial length escape value indicating 64-bit DWARF extension");
10911   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10912                        "Length of Address Ranges Info");
10913   /* Version number for aranges is still 2, even in DWARF3.  */
10914   dw2_asm_output_data (2, 2, "DWARF Version");
10915   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10916                          debug_info_section,
10917                          "Offset of Compilation Unit Info");
10918   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10919   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10920
10921   /* We need to align to twice the pointer size here.  */
10922   if (DWARF_ARANGES_PAD_SIZE)
10923     {
10924       /* Pad using a 2 byte words so that padding is correct for any
10925          pointer size.  */
10926       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10927                            2 * DWARF2_ADDR_SIZE);
10928       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10929         dw2_asm_output_data (2, 0, NULL);
10930     }
10931
10932   /* It is necessary not to output these entries if the sections were
10933      not used; if the sections were not used, the length will be 0 and
10934      the address may end up as 0 if the section is discarded by ld
10935      --gc-sections, leaving an invalid (0, 0) entry that can be
10936      confused with the terminator.  */
10937   if (text_section_used)
10938     {
10939       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10940       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10941                             text_section_label, "Length");
10942     }
10943   if (cold_text_section_used)
10944     {
10945       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10946                            "Address");
10947       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10948                             cold_text_section_label, "Length");
10949     }
10950
10951   for (i = 0; i < arange_table_in_use; i++)
10952     {
10953       dw_die_ref die = arange_table[i];
10954
10955       /* We shouldn't see aranges for DIEs outside of the main CU.  */
10956       gcc_assert (die->die_mark);
10957
10958       if (die->die_tag == DW_TAG_subprogram)
10959         {
10960           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
10961                                "Address");
10962           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
10963                                 get_AT_low_pc (die), "Length");
10964         }
10965       else
10966         {
10967           /* A static variable; extract the symbol from DW_AT_location.
10968              Note that this code isn't currently hit, as we only emit
10969              aranges for functions (jason 9/23/99).  */
10970           dw_attr_ref a = get_AT (die, DW_AT_location);
10971           dw_loc_descr_ref loc;
10972
10973           gcc_assert (a && AT_class (a) == dw_val_class_loc);
10974
10975           loc = AT_loc (a);
10976           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
10977
10978           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
10979                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
10980           dw2_asm_output_data (DWARF2_ADDR_SIZE,
10981                                get_AT_unsigned (die, DW_AT_byte_size),
10982                                "Length");
10983         }
10984     }
10985
10986   /* Output the terminator words.  */
10987   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10988   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10989 }
10990
10991 /* Add a new entry to .debug_ranges.  Return the offset at which it
10992    was placed.  */
10993
10994 static unsigned int
10995 add_ranges_num (int num)
10996 {
10997   unsigned int in_use = ranges_table_in_use;
10998
10999   if (in_use == ranges_table_allocated)
11000     {
11001       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11002       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11003                                     ranges_table_allocated);
11004       memset (ranges_table + ranges_table_in_use, 0,
11005               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11006     }
11007
11008   ranges_table[in_use].num = num;
11009   ranges_table_in_use = in_use + 1;
11010
11011   return in_use * 2 * DWARF2_ADDR_SIZE;
11012 }
11013
11014 /* Add a new entry to .debug_ranges corresponding to a block, or a
11015    range terminator if BLOCK is NULL.  */
11016
11017 static unsigned int
11018 add_ranges (const_tree block)
11019 {
11020   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11021 }
11022
11023 /* Add a new entry to .debug_ranges corresponding to a pair of
11024    labels.  */
11025
11026 static unsigned int
11027 add_ranges_by_labels (const char *begin, const char *end)
11028 {
11029   unsigned int in_use = ranges_by_label_in_use;
11030
11031   if (in_use == ranges_by_label_allocated)
11032     {
11033       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11034       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11035                                        ranges_by_label,
11036                                        ranges_by_label_allocated);
11037       memset (ranges_by_label + ranges_by_label_in_use, 0,
11038               RANGES_TABLE_INCREMENT
11039               * sizeof (struct dw_ranges_by_label_struct));
11040     }
11041
11042   ranges_by_label[in_use].begin = begin;
11043   ranges_by_label[in_use].end = end;
11044   ranges_by_label_in_use = in_use + 1;
11045
11046   return add_ranges_num (-(int)in_use - 1);
11047 }
11048
11049 static void
11050 output_ranges (void)
11051 {
11052   unsigned i;
11053   static const char *const start_fmt = "Offset 0x%x";
11054   const char *fmt = start_fmt;
11055
11056   for (i = 0; i < ranges_table_in_use; i++)
11057     {
11058       int block_num = ranges_table[i].num;
11059
11060       if (block_num > 0)
11061         {
11062           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11063           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11064
11065           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11066           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11067
11068           /* If all code is in the text section, then the compilation
11069              unit base address defaults to DW_AT_low_pc, which is the
11070              base of the text section.  */
11071           if (!have_multiple_function_sections)
11072             {
11073               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11074                                     text_section_label,
11075                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11076               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11077                                     text_section_label, NULL);
11078             }
11079
11080           /* Otherwise, the compilation unit base address is zero,
11081              which allows us to use absolute addresses, and not worry
11082              about whether the target supports cross-section
11083              arithmetic.  */
11084           else
11085             {
11086               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11087                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11088               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11089             }
11090
11091           fmt = NULL;
11092         }
11093
11094       /* Negative block_num stands for an index into ranges_by_label.  */
11095       else if (block_num < 0)
11096         {
11097           int lab_idx = - block_num - 1;
11098
11099           if (!have_multiple_function_sections)
11100             {
11101               gcc_unreachable ();
11102 #if 0
11103               /* If we ever use add_ranges_by_labels () for a single
11104                  function section, all we have to do is to take out
11105                  the #if 0 above.  */
11106               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11107                                     ranges_by_label[lab_idx].begin,
11108                                     text_section_label,
11109                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11110               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11111                                     ranges_by_label[lab_idx].end,
11112                                     text_section_label, NULL);
11113 #endif
11114             }
11115           else
11116             {
11117               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11118                                    ranges_by_label[lab_idx].begin,
11119                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11120               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11121                                    ranges_by_label[lab_idx].end,
11122                                    NULL);
11123             }
11124         }
11125       else
11126         {
11127           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11128           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11129           fmt = start_fmt;
11130         }
11131     }
11132 }
11133
11134 /* Data structure containing information about input files.  */
11135 struct file_info
11136 {
11137   const char *path;     /* Complete file name.  */
11138   const char *fname;    /* File name part.  */
11139   int length;           /* Length of entire string.  */
11140   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11141   int dir_idx;          /* Index in directory table.  */
11142 };
11143
11144 /* Data structure containing information about directories with source
11145    files.  */
11146 struct dir_info
11147 {
11148   const char *path;     /* Path including directory name.  */
11149   int length;           /* Path length.  */
11150   int prefix;           /* Index of directory entry which is a prefix.  */
11151   int count;            /* Number of files in this directory.  */
11152   int dir_idx;          /* Index of directory used as base.  */
11153 };
11154
11155 /* Callback function for file_info comparison.  We sort by looking at
11156    the directories in the path.  */
11157
11158 static int
11159 file_info_cmp (const void *p1, const void *p2)
11160 {
11161   const struct file_info *const s1 = (const struct file_info *) p1;
11162   const struct file_info *const s2 = (const struct file_info *) p2;
11163   const unsigned char *cp1;
11164   const unsigned char *cp2;
11165
11166   /* Take care of file names without directories.  We need to make sure that
11167      we return consistent values to qsort since some will get confused if
11168      we return the same value when identical operands are passed in opposite
11169      orders.  So if neither has a directory, return 0 and otherwise return
11170      1 or -1 depending on which one has the directory.  */
11171   if ((s1->path == s1->fname || s2->path == s2->fname))
11172     return (s2->path == s2->fname) - (s1->path == s1->fname);
11173
11174   cp1 = (const unsigned char *) s1->path;
11175   cp2 = (const unsigned char *) s2->path;
11176
11177   while (1)
11178     {
11179       ++cp1;
11180       ++cp2;
11181       /* Reached the end of the first path?  If so, handle like above.  */
11182       if ((cp1 == (const unsigned char *) s1->fname)
11183           || (cp2 == (const unsigned char *) s2->fname))
11184         return ((cp2 == (const unsigned char *) s2->fname)
11185                 - (cp1 == (const unsigned char *) s1->fname));
11186
11187       /* Character of current path component the same?  */
11188       else if (*cp1 != *cp2)
11189         return *cp1 - *cp2;
11190     }
11191 }
11192
11193 struct file_name_acquire_data
11194 {
11195   struct file_info *files;
11196   int used_files;
11197   int max_files;
11198 };
11199
11200 /* Traversal function for the hash table.  */
11201
11202 static int
11203 file_name_acquire (void ** slot, void *data)
11204 {
11205   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11206   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11207   struct file_info *fi;
11208   const char *f;
11209
11210   gcc_assert (fnad->max_files >= d->emitted_number);
11211
11212   if (! d->emitted_number)
11213     return 1;
11214
11215   gcc_assert (fnad->max_files != fnad->used_files);
11216
11217   fi = fnad->files + fnad->used_files++;
11218
11219   /* Skip all leading "./".  */
11220   f = d->filename;
11221   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11222     f += 2;
11223
11224   /* Create a new array entry.  */
11225   fi->path = f;
11226   fi->length = strlen (f);
11227   fi->file_idx = d;
11228
11229   /* Search for the file name part.  */
11230   f = strrchr (f, DIR_SEPARATOR);
11231 #if defined (DIR_SEPARATOR_2)
11232   {
11233     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11234
11235     if (g != NULL)
11236       {
11237         if (f == NULL || f < g)
11238           f = g;
11239       }
11240   }
11241 #endif
11242
11243   fi->fname = f == NULL ? fi->path : f + 1;
11244   return 1;
11245 }
11246
11247 /* Output the directory table and the file name table.  We try to minimize
11248    the total amount of memory needed.  A heuristic is used to avoid large
11249    slowdowns with many input files.  */
11250
11251 static void
11252 output_file_names (void)
11253 {
11254   struct file_name_acquire_data fnad;
11255   int numfiles;
11256   struct file_info *files;
11257   struct dir_info *dirs;
11258   int *saved;
11259   int *savehere;
11260   int *backmap;
11261   int ndirs;
11262   int idx_offset;
11263   int i;
11264   int idx;
11265
11266   if (!last_emitted_file)
11267     {
11268       dw2_asm_output_data (1, 0, "End directory table");
11269       dw2_asm_output_data (1, 0, "End file name table");
11270       return;
11271     }
11272
11273   numfiles = last_emitted_file->emitted_number;
11274
11275   /* Allocate the various arrays we need.  */
11276   files = XALLOCAVEC (struct file_info, numfiles);
11277   dirs = XALLOCAVEC (struct dir_info, numfiles);
11278
11279   fnad.files = files;
11280   fnad.used_files = 0;
11281   fnad.max_files = numfiles;
11282   htab_traverse (file_table, file_name_acquire, &fnad);
11283   gcc_assert (fnad.used_files == fnad.max_files);
11284
11285   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11286
11287   /* Find all the different directories used.  */
11288   dirs[0].path = files[0].path;
11289   dirs[0].length = files[0].fname - files[0].path;
11290   dirs[0].prefix = -1;
11291   dirs[0].count = 1;
11292   dirs[0].dir_idx = 0;
11293   files[0].dir_idx = 0;
11294   ndirs = 1;
11295
11296   for (i = 1; i < numfiles; i++)
11297     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11298         && memcmp (dirs[ndirs - 1].path, files[i].path,
11299                    dirs[ndirs - 1].length) == 0)
11300       {
11301         /* Same directory as last entry.  */
11302         files[i].dir_idx = ndirs - 1;
11303         ++dirs[ndirs - 1].count;
11304       }
11305     else
11306       {
11307         int j;
11308
11309         /* This is a new directory.  */
11310         dirs[ndirs].path = files[i].path;
11311         dirs[ndirs].length = files[i].fname - files[i].path;
11312         dirs[ndirs].count = 1;
11313         dirs[ndirs].dir_idx = ndirs;
11314         files[i].dir_idx = ndirs;
11315
11316         /* Search for a prefix.  */
11317         dirs[ndirs].prefix = -1;
11318         for (j = 0; j < ndirs; j++)
11319           if (dirs[j].length < dirs[ndirs].length
11320               && dirs[j].length > 1
11321               && (dirs[ndirs].prefix == -1
11322                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11323               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11324             dirs[ndirs].prefix = j;
11325
11326         ++ndirs;
11327       }
11328
11329   /* Now to the actual work.  We have to find a subset of the directories which
11330      allow expressing the file name using references to the directory table
11331      with the least amount of characters.  We do not do an exhaustive search
11332      where we would have to check out every combination of every single
11333      possible prefix.  Instead we use a heuristic which provides nearly optimal
11334      results in most cases and never is much off.  */
11335   saved = XALLOCAVEC (int, ndirs);
11336   savehere = XALLOCAVEC (int, ndirs);
11337
11338   memset (saved, '\0', ndirs * sizeof (saved[0]));
11339   for (i = 0; i < ndirs; i++)
11340     {
11341       int j;
11342       int total;
11343
11344       /* We can always save some space for the current directory.  But this
11345          does not mean it will be enough to justify adding the directory.  */
11346       savehere[i] = dirs[i].length;
11347       total = (savehere[i] - saved[i]) * dirs[i].count;
11348
11349       for (j = i + 1; j < ndirs; j++)
11350         {
11351           savehere[j] = 0;
11352           if (saved[j] < dirs[i].length)
11353             {
11354               /* Determine whether the dirs[i] path is a prefix of the
11355                  dirs[j] path.  */
11356               int k;
11357
11358               k = dirs[j].prefix;
11359               while (k != -1 && k != (int) i)
11360                 k = dirs[k].prefix;
11361
11362               if (k == (int) i)
11363                 {
11364                   /* Yes it is.  We can possibly save some memory by
11365                      writing the filenames in dirs[j] relative to
11366                      dirs[i].  */
11367                   savehere[j] = dirs[i].length;
11368                   total += (savehere[j] - saved[j]) * dirs[j].count;
11369                 }
11370             }
11371         }
11372
11373       /* Check whether we can save enough to justify adding the dirs[i]
11374          directory.  */
11375       if (total > dirs[i].length + 1)
11376         {
11377           /* It's worthwhile adding.  */
11378           for (j = i; j < ndirs; j++)
11379             if (savehere[j] > 0)
11380               {
11381                 /* Remember how much we saved for this directory so far.  */
11382                 saved[j] = savehere[j];
11383
11384                 /* Remember the prefix directory.  */
11385                 dirs[j].dir_idx = i;
11386               }
11387         }
11388     }
11389
11390   /* Emit the directory name table.  */
11391   idx = 1;
11392   idx_offset = dirs[0].length > 0 ? 1 : 0;
11393   for (i = 1 - idx_offset; i < ndirs; i++)
11394     dw2_asm_output_nstring (dirs[i].path,
11395                             dirs[i].length
11396                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11397                             "Directory Entry: 0x%x", i + idx_offset);
11398
11399   dw2_asm_output_data (1, 0, "End directory table");
11400
11401   /* We have to emit them in the order of emitted_number since that's
11402      used in the debug info generation.  To do this efficiently we
11403      generate a back-mapping of the indices first.  */
11404   backmap = XALLOCAVEC (int, numfiles);
11405   for (i = 0; i < numfiles; i++)
11406     backmap[files[i].file_idx->emitted_number - 1] = i;
11407
11408   /* Now write all the file names.  */
11409   for (i = 0; i < numfiles; i++)
11410     {
11411       int file_idx = backmap[i];
11412       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11413
11414 #ifdef VMS_DEBUGGING_INFO
11415 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11416
11417       /* Setting these fields can lead to debugger miscomparisons,
11418          but VMS Debug requires them to be set correctly.  */
11419
11420       int ver;
11421       long long cdt;
11422       long siz;
11423       int maxfilelen = strlen (files[file_idx].path)
11424                                + dirs[dir_idx].length
11425                                + MAX_VMS_VERSION_LEN + 1;
11426       char *filebuf = XALLOCAVEC (char, maxfilelen);
11427
11428       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11429       snprintf (filebuf, maxfilelen, "%s;%d",
11430                 files[file_idx].path + dirs[dir_idx].length, ver);
11431
11432       dw2_asm_output_nstring
11433         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
11434
11435       /* Include directory index.  */
11436       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11437
11438       /* Modification time.  */
11439       dw2_asm_output_data_uleb128
11440         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11441           ? cdt : 0,
11442          NULL);
11443
11444       /* File length in bytes.  */
11445       dw2_asm_output_data_uleb128
11446         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11447           ? siz : 0,
11448          NULL);
11449 #else
11450       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11451                               "File Entry: 0x%x", (unsigned) i + 1);
11452
11453       /* Include directory index.  */
11454       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11455
11456       /* Modification time.  */
11457       dw2_asm_output_data_uleb128 (0, NULL);
11458
11459       /* File length in bytes.  */
11460       dw2_asm_output_data_uleb128 (0, NULL);
11461 #endif
11462     }
11463
11464   dw2_asm_output_data (1, 0, "End file name table");
11465 }
11466
11467
11468 /* Output the source line number correspondence information.  This
11469    information goes into the .debug_line section.  */
11470
11471 static void
11472 output_line_info (void)
11473 {
11474   char l1[20], l2[20], p1[20], p2[20];
11475   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11476   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11477   unsigned opc;
11478   unsigned n_op_args;
11479   unsigned long lt_index;
11480   unsigned long current_line;
11481   long line_offset;
11482   long line_delta;
11483   unsigned long current_file;
11484   unsigned long function;
11485   int ver = dwarf_version;
11486
11487   /* Don't mark the output as DWARF-4 until we make full use of the
11488      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
11489      selects only a few extensions from the DWARF-4 spec.  */
11490   if (ver > 3)
11491     ver = 3;
11492
11493   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11494   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11495   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11496   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11497
11498   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11499     dw2_asm_output_data (4, 0xffffffff,
11500       "Initial length escape value indicating 64-bit DWARF extension");
11501   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11502                         "Length of Source Line Info");
11503   ASM_OUTPUT_LABEL (asm_out_file, l1);
11504
11505   dw2_asm_output_data (2, ver, "DWARF Version");
11506   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11507   ASM_OUTPUT_LABEL (asm_out_file, p1);
11508
11509   /* Define the architecture-dependent minimum instruction length (in
11510    bytes).  In this implementation of DWARF, this field is used for
11511    information purposes only.  Since GCC generates assembly language,
11512    we have no a priori knowledge of how many instruction bytes are
11513    generated for each source line, and therefore can use only the
11514    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
11515    commands.  Accordingly, we fix this as `1', which is "correct
11516    enough" for all architectures, and don't let the target override.  */
11517   dw2_asm_output_data (1, 1,
11518                        "Minimum Instruction Length");
11519
11520   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11521                        "Default is_stmt_start flag");
11522   dw2_asm_output_data (1, DWARF_LINE_BASE,
11523                        "Line Base Value (Special Opcodes)");
11524   dw2_asm_output_data (1, DWARF_LINE_RANGE,
11525                        "Line Range Value (Special Opcodes)");
11526   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11527                        "Special Opcode Base");
11528
11529   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11530     {
11531       switch (opc)
11532         {
11533         case DW_LNS_advance_pc:
11534         case DW_LNS_advance_line:
11535         case DW_LNS_set_file:
11536         case DW_LNS_set_column:
11537         case DW_LNS_fixed_advance_pc:
11538           n_op_args = 1;
11539           break;
11540         default:
11541           n_op_args = 0;
11542           break;
11543         }
11544
11545       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
11546                            opc, n_op_args);
11547     }
11548
11549   /* Write out the information about the files we use.  */
11550   output_file_names ();
11551   ASM_OUTPUT_LABEL (asm_out_file, p2);
11552
11553   /* We used to set the address register to the first location in the text
11554      section here, but that didn't accomplish anything since we already
11555      have a line note for the opening brace of the first function.  */
11556
11557   /* Generate the line number to PC correspondence table, encoded as
11558      a series of state machine operations.  */
11559   current_file = 1;
11560   current_line = 1;
11561
11562   if (cfun && in_cold_section_p)
11563     strcpy (prev_line_label, crtl->subsections.cold_section_label);
11564   else
11565     strcpy (prev_line_label, text_section_label);
11566   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
11567     {
11568       dw_line_info_ref line_info = &line_info_table[lt_index];
11569
11570 #if 0
11571       /* Disable this optimization for now; GDB wants to see two line notes
11572          at the beginning of a function so it can find the end of the
11573          prologue.  */
11574
11575       /* Don't emit anything for redundant notes.  Just updating the
11576          address doesn't accomplish anything, because we already assume
11577          that anything after the last address is this line.  */
11578       if (line_info->dw_line_num == current_line
11579           && line_info->dw_file_num == current_file)
11580         continue;
11581 #endif
11582
11583       /* Emit debug info for the address of the current line.
11584
11585          Unfortunately, we have little choice here currently, and must always
11586          use the most general form.  GCC does not know the address delta
11587          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
11588          attributes which will give an upper bound on the address range.  We
11589          could perhaps use length attributes to determine when it is safe to
11590          use DW_LNS_fixed_advance_pc.  */
11591
11592       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
11593       if (0)
11594         {
11595           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
11596           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11597                                "DW_LNS_fixed_advance_pc");
11598           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11599         }
11600       else
11601         {
11602           /* This can handle any delta.  This takes
11603              4+DWARF2_ADDR_SIZE bytes.  */
11604           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11605           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11606           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11607           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11608         }
11609
11610       strcpy (prev_line_label, line_label);
11611
11612       /* Emit debug info for the source file of the current line, if
11613          different from the previous line.  */
11614       if (line_info->dw_file_num != current_file)
11615         {
11616           current_file = line_info->dw_file_num;
11617           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11618           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11619         }
11620
11621       /* Emit debug info for the current line number, choosing the encoding
11622          that uses the least amount of space.  */
11623       if (line_info->dw_line_num != current_line)
11624         {
11625           line_offset = line_info->dw_line_num - current_line;
11626           line_delta = line_offset - DWARF_LINE_BASE;
11627           current_line = line_info->dw_line_num;
11628           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11629             /* This can handle deltas from -10 to 234, using the current
11630                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
11631                takes 1 byte.  */
11632             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11633                                  "line %lu", current_line);
11634           else
11635             {
11636               /* This can handle any delta.  This takes at least 4 bytes,
11637                  depending on the value being encoded.  */
11638               dw2_asm_output_data (1, DW_LNS_advance_line,
11639                                    "advance to line %lu", current_line);
11640               dw2_asm_output_data_sleb128 (line_offset, NULL);
11641               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11642             }
11643         }
11644       else
11645         /* We still need to start a new row, so output a copy insn.  */
11646         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11647     }
11648
11649   /* Emit debug info for the address of the end of the function.  */
11650   if (0)
11651     {
11652       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11653                            "DW_LNS_fixed_advance_pc");
11654       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
11655     }
11656   else
11657     {
11658       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11659       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11660       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11661       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
11662     }
11663
11664   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11665   dw2_asm_output_data_uleb128 (1, NULL);
11666   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11667
11668   function = 0;
11669   current_file = 1;
11670   current_line = 1;
11671   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
11672     {
11673       dw_separate_line_info_ref line_info
11674         = &separate_line_info_table[lt_index];
11675
11676 #if 0
11677       /* Don't emit anything for redundant notes.  */
11678       if (line_info->dw_line_num == current_line
11679           && line_info->dw_file_num == current_file
11680           && line_info->function == function)
11681         goto cont;
11682 #endif
11683
11684       /* Emit debug info for the address of the current line.  If this is
11685          a new function, or the first line of a function, then we need
11686          to handle it differently.  */
11687       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
11688                                    lt_index);
11689       if (function != line_info->function)
11690         {
11691           function = line_info->function;
11692
11693           /* Set the address register to the first line in the function.  */
11694           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11695           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11696           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11697           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11698         }
11699       else
11700         {
11701           /* ??? See the DW_LNS_advance_pc comment above.  */
11702           if (0)
11703             {
11704               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11705                                    "DW_LNS_fixed_advance_pc");
11706               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11707             }
11708           else
11709             {
11710               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11711               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11712               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11713               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11714             }
11715         }
11716
11717       strcpy (prev_line_label, line_label);
11718
11719       /* Emit debug info for the source file of the current line, if
11720          different from the previous line.  */
11721       if (line_info->dw_file_num != current_file)
11722         {
11723           current_file = line_info->dw_file_num;
11724           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11725           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11726         }
11727
11728       /* Emit debug info for the current line number, choosing the encoding
11729          that uses the least amount of space.  */
11730       if (line_info->dw_line_num != current_line)
11731         {
11732           line_offset = line_info->dw_line_num - current_line;
11733           line_delta = line_offset - DWARF_LINE_BASE;
11734           current_line = line_info->dw_line_num;
11735           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11736             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11737                                  "line %lu", current_line);
11738           else
11739             {
11740               dw2_asm_output_data (1, DW_LNS_advance_line,
11741                                    "advance to line %lu", current_line);
11742               dw2_asm_output_data_sleb128 (line_offset, NULL);
11743               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11744             }
11745         }
11746       else
11747         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11748
11749 #if 0
11750     cont:
11751 #endif
11752
11753       lt_index++;
11754
11755       /* If we're done with a function, end its sequence.  */
11756       if (lt_index == separate_line_info_table_in_use
11757           || separate_line_info_table[lt_index].function != function)
11758         {
11759           current_file = 1;
11760           current_line = 1;
11761
11762           /* Emit debug info for the address of the end of the function.  */
11763           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
11764           if (0)
11765             {
11766               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11767                                    "DW_LNS_fixed_advance_pc");
11768               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11769             }
11770           else
11771             {
11772               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11773               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11774               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11775               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11776             }
11777
11778           /* Output the marker for the end of this sequence.  */
11779           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11780           dw2_asm_output_data_uleb128 (1, NULL);
11781           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11782         }
11783     }
11784
11785   /* Output the marker for the end of the line number info.  */
11786   ASM_OUTPUT_LABEL (asm_out_file, l2);
11787 }
11788
11789 /* Return the size of the .debug_dcall table for the compilation unit.  */
11790
11791 static unsigned long
11792 size_of_dcall_table (void)
11793 {
11794   unsigned long size;
11795   unsigned int i;
11796   dcall_entry *p;
11797   tree last_poc_decl = NULL;
11798
11799   /* Header:  version + debug info section pointer + pointer size.  */
11800   size = 2 + DWARF_OFFSET_SIZE + 1;
11801
11802   /* Each entry:  code label + DIE offset.  */
11803   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
11804     {
11805       gcc_assert (p->targ_die != NULL);
11806       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
11807       if (p->poc_decl != last_poc_decl)
11808         {
11809           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
11810           gcc_assert (poc_die);
11811           last_poc_decl = p->poc_decl;
11812           if (poc_die)
11813             size += (DWARF_OFFSET_SIZE
11814                      + size_of_uleb128 (poc_die->die_offset));
11815         }
11816       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
11817     }
11818
11819   return size;
11820 }
11821
11822 /* Output the direct call table used to disambiguate PC values when
11823    identical function have been merged.  */
11824
11825 static void
11826 output_dcall_table (void)
11827 {
11828   unsigned i;
11829   unsigned long dcall_length = size_of_dcall_table ();
11830   dcall_entry *p;
11831   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
11832   tree last_poc_decl = NULL;
11833
11834   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11835     dw2_asm_output_data (4, 0xffffffff,
11836       "Initial length escape value indicating 64-bit DWARF extension");
11837   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
11838                        "Length of Direct Call Table");
11839   dw2_asm_output_data (2, 4, "Version number");
11840   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11841                          debug_info_section,
11842                          "Offset of Compilation Unit Info");
11843   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11844
11845   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
11846     {
11847       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
11848       if (p->poc_decl != last_poc_decl)
11849         {
11850           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
11851           last_poc_decl = p->poc_decl;
11852           if (poc_die)
11853             {
11854               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
11855               dw2_asm_output_data_uleb128 (poc_die->die_offset,
11856                                            "Caller DIE offset");
11857             }
11858         }
11859       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
11860       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
11861       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
11862                                    "Callee DIE offset");
11863     }
11864 }
11865 \f
11866 /* Return the size of the .debug_vcall table for the compilation unit.  */
11867
11868 static unsigned long
11869 size_of_vcall_table (void)
11870 {
11871   unsigned long size;
11872   unsigned int i;
11873   vcall_entry *p;
11874
11875   /* Header:  version + pointer size.  */
11876   size = 2 + 1;
11877
11878   /* Each entry:  code label + vtable slot index.  */
11879   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
11880     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
11881
11882   return size;
11883 }
11884
11885 /* Output the virtual call table used to disambiguate PC values when
11886    identical function have been merged.  */
11887
11888 static void
11889 output_vcall_table (void)
11890 {
11891   unsigned i;
11892   unsigned long vcall_length = size_of_vcall_table ();
11893   vcall_entry *p;
11894   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
11895
11896   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11897     dw2_asm_output_data (4, 0xffffffff,
11898       "Initial length escape value indicating 64-bit DWARF extension");
11899   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
11900                        "Length of Virtual Call Table");
11901   dw2_asm_output_data (2, 4, "Version number");
11902   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11903
11904   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
11905     {
11906       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
11907       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
11908       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
11909     }
11910 }
11911 \f
11912 /* Given a pointer to a tree node for some base type, return a pointer to
11913    a DIE that describes the given type.
11914
11915    This routine must only be called for GCC type nodes that correspond to
11916    Dwarf base (fundamental) types.  */
11917
11918 static dw_die_ref
11919 base_type_die (tree type)
11920 {
11921   dw_die_ref base_type_result;
11922   enum dwarf_type encoding;
11923
11924   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11925     return 0;
11926
11927   /* If this is a subtype that should not be emitted as a subrange type,
11928      use the base type.  See subrange_type_for_debug_p.  */
11929   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11930     type = TREE_TYPE (type);
11931
11932   switch (TREE_CODE (type))
11933     {
11934     case INTEGER_TYPE:
11935       if (TYPE_STRING_FLAG (type))
11936         {
11937           if (TYPE_UNSIGNED (type))
11938             encoding = DW_ATE_unsigned_char;
11939           else
11940             encoding = DW_ATE_signed_char;
11941         }
11942       else if (TYPE_UNSIGNED (type))
11943         encoding = DW_ATE_unsigned;
11944       else
11945         encoding = DW_ATE_signed;
11946       break;
11947
11948     case REAL_TYPE:
11949       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
11950         {
11951           if (dwarf_version >= 3 || !dwarf_strict)
11952             encoding = DW_ATE_decimal_float;
11953           else
11954             encoding = DW_ATE_lo_user;
11955         }
11956       else
11957         encoding = DW_ATE_float;
11958       break;
11959
11960     case FIXED_POINT_TYPE:
11961       if (!(dwarf_version >= 3 || !dwarf_strict))
11962         encoding = DW_ATE_lo_user;
11963       else if (TYPE_UNSIGNED (type))
11964         encoding = DW_ATE_unsigned_fixed;
11965       else
11966         encoding = DW_ATE_signed_fixed;
11967       break;
11968
11969       /* Dwarf2 doesn't know anything about complex ints, so use
11970          a user defined type for it.  */
11971     case COMPLEX_TYPE:
11972       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
11973         encoding = DW_ATE_complex_float;
11974       else
11975         encoding = DW_ATE_lo_user;
11976       break;
11977
11978     case BOOLEAN_TYPE:
11979       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
11980       encoding = DW_ATE_boolean;
11981       break;
11982
11983     default:
11984       /* No other TREE_CODEs are Dwarf fundamental types.  */
11985       gcc_unreachable ();
11986     }
11987
11988   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
11989
11990   /* This probably indicates a bug.  */
11991   if (! TYPE_NAME (type))
11992     add_name_attribute (base_type_result, "__unknown__");
11993
11994   add_AT_unsigned (base_type_result, DW_AT_byte_size,
11995                    int_size_in_bytes (type));
11996   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
11997
11998   return base_type_result;
11999 }
12000
12001 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12002    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12003
12004 static inline int
12005 is_base_type (tree type)
12006 {
12007   switch (TREE_CODE (type))
12008     {
12009     case ERROR_MARK:
12010     case VOID_TYPE:
12011     case INTEGER_TYPE:
12012     case REAL_TYPE:
12013     case FIXED_POINT_TYPE:
12014     case COMPLEX_TYPE:
12015     case BOOLEAN_TYPE:
12016       return 1;
12017
12018     case ARRAY_TYPE:
12019     case RECORD_TYPE:
12020     case UNION_TYPE:
12021     case QUAL_UNION_TYPE:
12022     case ENUMERAL_TYPE:
12023     case FUNCTION_TYPE:
12024     case METHOD_TYPE:
12025     case POINTER_TYPE:
12026     case REFERENCE_TYPE:
12027     case OFFSET_TYPE:
12028     case LANG_TYPE:
12029     case VECTOR_TYPE:
12030       return 0;
12031
12032     default:
12033       gcc_unreachable ();
12034     }
12035
12036   return 0;
12037 }
12038
12039 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12040    node, return the size in bits for the type if it is a constant, or else
12041    return the alignment for the type if the type's size is not constant, or
12042    else return BITS_PER_WORD if the type actually turns out to be an
12043    ERROR_MARK node.  */
12044
12045 static inline unsigned HOST_WIDE_INT
12046 simple_type_size_in_bits (const_tree type)
12047 {
12048   if (TREE_CODE (type) == ERROR_MARK)
12049     return BITS_PER_WORD;
12050   else if (TYPE_SIZE (type) == NULL_TREE)
12051     return 0;
12052   else if (host_integerp (TYPE_SIZE (type), 1))
12053     return tree_low_cst (TYPE_SIZE (type), 1);
12054   else
12055     return TYPE_ALIGN (type);
12056 }
12057
12058 /*  Given a pointer to a tree node for a subrange type, return a pointer
12059     to a DIE that describes the given type.  */
12060
12061 static dw_die_ref
12062 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12063 {
12064   dw_die_ref subrange_die;
12065   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12066
12067   if (context_die == NULL)
12068     context_die = comp_unit_die;
12069
12070   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12071
12072   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12073     {
12074       /* The size of the subrange type and its base type do not match,
12075          so we need to generate a size attribute for the subrange type.  */
12076       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12077     }
12078
12079   if (low)
12080     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12081   if (high)
12082     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12083
12084   return subrange_die;
12085 }
12086
12087 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12088    entry that chains various modifiers in front of the given type.  */
12089
12090 static dw_die_ref
12091 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12092                    dw_die_ref context_die)
12093 {
12094   enum tree_code code = TREE_CODE (type);
12095   dw_die_ref mod_type_die;
12096   dw_die_ref sub_die = NULL;
12097   tree item_type = NULL;
12098   tree qualified_type;
12099   tree name, low, high;
12100
12101   if (code == ERROR_MARK)
12102     return NULL;
12103
12104   /* See if we already have the appropriately qualified variant of
12105      this type.  */
12106   qualified_type
12107     = get_qualified_type (type,
12108                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12109                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12110
12111   /* If we do, then we can just use its DIE, if it exists.  */
12112   if (qualified_type)
12113     {
12114       mod_type_die = lookup_type_die (qualified_type);
12115       if (mod_type_die)
12116         return mod_type_die;
12117     }
12118
12119   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12120
12121   /* Handle C typedef types.  */
12122   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
12123     {
12124       tree dtype = TREE_TYPE (name);
12125
12126       if (qualified_type == dtype)
12127         {
12128           /* For a named type, use the typedef.  */
12129           gen_type_die (qualified_type, context_die);
12130           return lookup_type_die (qualified_type);
12131         }
12132       else if (is_const_type < TYPE_READONLY (dtype)
12133                || is_volatile_type < TYPE_VOLATILE (dtype)
12134                || (is_const_type <= TYPE_READONLY (dtype)
12135                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12136                    && DECL_ORIGINAL_TYPE (name) != type))
12137         /* cv-unqualified version of named type.  Just use the unnamed
12138            type to which it refers.  */
12139         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12140                                   is_const_type, is_volatile_type,
12141                                   context_die);
12142       /* Else cv-qualified version of named type; fall through.  */
12143     }
12144
12145   if (is_const_type)
12146     {
12147       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12148       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12149     }
12150   else if (is_volatile_type)
12151     {
12152       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12153       sub_die = modified_type_die (type, 0, 0, context_die);
12154     }
12155   else if (code == POINTER_TYPE)
12156     {
12157       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12158       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12159                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12160       item_type = TREE_TYPE (type);
12161       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12162         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12163                          TYPE_ADDR_SPACE (item_type));
12164     }
12165   else if (code == REFERENCE_TYPE)
12166     {
12167       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12168       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12169                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12170       item_type = TREE_TYPE (type);
12171       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12172         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12173                          TYPE_ADDR_SPACE (item_type));
12174     }
12175   else if (code == INTEGER_TYPE
12176            && TREE_TYPE (type) != NULL_TREE
12177            && subrange_type_for_debug_p (type, &low, &high))
12178     {
12179       mod_type_die = subrange_type_die (type, low, high, context_die);
12180       item_type = TREE_TYPE (type);
12181     }
12182   else if (is_base_type (type))
12183     mod_type_die = base_type_die (type);
12184   else
12185     {
12186       gen_type_die (type, context_die);
12187
12188       /* We have to get the type_main_variant here (and pass that to the
12189          `lookup_type_die' routine) because the ..._TYPE node we have
12190          might simply be a *copy* of some original type node (where the
12191          copy was created to help us keep track of typedef names) and
12192          that copy might have a different TYPE_UID from the original
12193          ..._TYPE node.  */
12194       if (TREE_CODE (type) != VECTOR_TYPE)
12195         return lookup_type_die (type_main_variant (type));
12196       else
12197         /* Vectors have the debugging information in the type,
12198            not the main variant.  */
12199         return lookup_type_die (type);
12200     }
12201
12202   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12203      don't output a DW_TAG_typedef, since there isn't one in the
12204      user's program; just attach a DW_AT_name to the type.
12205      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12206      if the base type already has the same name.  */
12207   if (name
12208       && ((TREE_CODE (name) != TYPE_DECL
12209            && (qualified_type == TYPE_MAIN_VARIANT (type)
12210                || (!is_const_type && !is_volatile_type)))
12211           || (TREE_CODE (name) == TYPE_DECL
12212               && TREE_TYPE (name) == qualified_type
12213               && DECL_NAME (name))))
12214     {
12215       if (TREE_CODE (name) == TYPE_DECL)
12216         /* Could just call add_name_and_src_coords_attributes here,
12217            but since this is a builtin type it doesn't have any
12218            useful source coordinates anyway.  */
12219         name = DECL_NAME (name);
12220       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12221     }
12222
12223   if (qualified_type)
12224     equate_type_number_to_die (qualified_type, mod_type_die);
12225
12226   if (item_type)
12227     /* We must do this after the equate_type_number_to_die call, in case
12228        this is a recursive type.  This ensures that the modified_type_die
12229        recursion will terminate even if the type is recursive.  Recursive
12230        types are possible in Ada.  */
12231     sub_die = modified_type_die (item_type,
12232                                  TYPE_READONLY (item_type),
12233                                  TYPE_VOLATILE (item_type),
12234                                  context_die);
12235
12236   if (sub_die != NULL)
12237     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12238
12239   return mod_type_die;
12240 }
12241
12242 /* Generate DIEs for the generic parameters of T.
12243    T must be either a generic type or a generic function.
12244    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12245
12246 static void
12247 gen_generic_params_dies (tree t)
12248 {
12249   tree parms, args;
12250   int parms_num, i;
12251   dw_die_ref die = NULL;
12252
12253   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12254     return;
12255
12256   if (TYPE_P (t))
12257     die = lookup_type_die (t);
12258   else if (DECL_P (t))
12259     die = lookup_decl_die (t);
12260
12261   gcc_assert (die);
12262
12263   parms = lang_hooks.get_innermost_generic_parms (t);
12264   if (!parms)
12265     /* T has no generic parameter. It means T is neither a generic type
12266        or function. End of story.  */
12267     return;
12268
12269   parms_num = TREE_VEC_LENGTH (parms);
12270   args = lang_hooks.get_innermost_generic_args (t);
12271   for (i = 0; i < parms_num; i++)
12272     {
12273       tree parm, arg, arg_pack_elems;
12274
12275       parm = TREE_VEC_ELT (parms, i);
12276       arg = TREE_VEC_ELT (args, i);
12277       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12278       gcc_assert (parm && TREE_VALUE (parm) && arg);
12279
12280       if (parm && TREE_VALUE (parm) && arg)
12281         {
12282           /* If PARM represents a template parameter pack,
12283              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12284              by DW_TAG_template_*_parameter DIEs for the argument
12285              pack elements of ARG. Note that ARG would then be
12286              an argument pack.  */
12287           if (arg_pack_elems)
12288             template_parameter_pack_die (TREE_VALUE (parm),
12289                                          arg_pack_elems,
12290                                          die);
12291           else
12292             generic_parameter_die (TREE_VALUE (parm), arg,
12293                                    true /* Emit DW_AT_name */, die);
12294         }
12295     }
12296 }
12297
12298 /* Create and return a DIE for PARM which should be
12299    the representation of a generic type parameter.
12300    For instance, in the C++ front end, PARM would be a template parameter.
12301    ARG is the argument to PARM.
12302    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12303    name of the PARM.
12304    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12305    as a child node.  */
12306
12307 static dw_die_ref
12308 generic_parameter_die (tree parm, tree arg,
12309                        bool emit_name_p,
12310                        dw_die_ref parent_die)
12311 {
12312   dw_die_ref tmpl_die = NULL;
12313   const char *name = NULL;
12314
12315   if (!parm || !DECL_NAME (parm) || !arg)
12316     return NULL;
12317
12318   /* We support non-type generic parameters and arguments,
12319      type generic parameters and arguments, as well as
12320      generic generic parameters (a.k.a. template template parameters in C++)
12321      and arguments.  */
12322   if (TREE_CODE (parm) == PARM_DECL)
12323     /* PARM is a nontype generic parameter  */
12324     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12325   else if (TREE_CODE (parm) == TYPE_DECL)
12326     /* PARM is a type generic parameter.  */
12327     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12328   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12329     /* PARM is a generic generic parameter.
12330        Its DIE is a GNU extension. It shall have a
12331        DW_AT_name attribute to represent the name of the template template
12332        parameter, and a DW_AT_GNU_template_name attribute to represent the
12333        name of the template template argument.  */
12334     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12335                         parent_die, parm);
12336   else
12337     gcc_unreachable ();
12338
12339   if (tmpl_die)
12340     {
12341       tree tmpl_type;
12342
12343       /* If PARM is a generic parameter pack, it means we are
12344          emitting debug info for a template argument pack element.
12345          In other terms, ARG is a template argument pack element.
12346          In that case, we don't emit any DW_AT_name attribute for
12347          the die.  */
12348       if (emit_name_p)
12349         {
12350           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12351           gcc_assert (name);
12352           add_AT_string (tmpl_die, DW_AT_name, name);
12353         }
12354
12355       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12356         {
12357           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12358              TMPL_DIE should have a child DW_AT_type attribute that is set
12359              to the type of the argument to PARM, which is ARG.
12360              If PARM is a type generic parameter, TMPL_DIE should have a
12361              child DW_AT_type that is set to ARG.  */
12362           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12363           add_type_attribute (tmpl_die, tmpl_type, 0,
12364                               TREE_THIS_VOLATILE (tmpl_type),
12365                               parent_die);
12366         }
12367       else
12368         {
12369           /* So TMPL_DIE is a DIE representing a
12370              a generic generic template parameter, a.k.a template template
12371              parameter in C++ and arg is a template.  */
12372
12373           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12374              to the name of the argument.  */
12375           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12376           if (name)
12377             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12378         }
12379
12380       if (TREE_CODE (parm) == PARM_DECL)
12381         /* So PARM is a non-type generic parameter.
12382            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12383            attribute of TMPL_DIE which value represents the value
12384            of ARG.
12385            We must be careful here:
12386            The value of ARG might reference some function decls.
12387            We might currently be emitting debug info for a generic
12388            type and types are emitted before function decls, we don't
12389            know if the function decls referenced by ARG will actually be
12390            emitted after cgraph computations.
12391            So must defer the generation of the DW_AT_const_value to
12392            after cgraph is ready.  */
12393         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12394     }
12395
12396   return tmpl_die;
12397 }
12398
12399 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12400    PARM_PACK must be a template parameter pack. The returned DIE
12401    will be child DIE of PARENT_DIE.  */
12402
12403 static dw_die_ref
12404 template_parameter_pack_die (tree parm_pack,
12405                              tree parm_pack_args,
12406                              dw_die_ref parent_die)
12407 {
12408   dw_die_ref die;
12409   int j;
12410
12411   gcc_assert (parent_die && parm_pack);
12412
12413   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12414   add_name_and_src_coords_attributes (die, parm_pack);
12415   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12416     generic_parameter_die (parm_pack,
12417                            TREE_VEC_ELT (parm_pack_args, j),
12418                            false /* Don't emit DW_AT_name */,
12419                            die);
12420   return die;
12421 }
12422
12423 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12424    an enumerated type.  */
12425
12426 static inline int
12427 type_is_enum (const_tree type)
12428 {
12429   return TREE_CODE (type) == ENUMERAL_TYPE;
12430 }
12431
12432 /* Return the DBX register number described by a given RTL node.  */
12433
12434 static unsigned int
12435 dbx_reg_number (const_rtx rtl)
12436 {
12437   unsigned regno = REGNO (rtl);
12438
12439   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12440
12441 #ifdef LEAF_REG_REMAP
12442   if (current_function_uses_only_leaf_regs)
12443     {
12444       int leaf_reg = LEAF_REG_REMAP (regno);
12445       if (leaf_reg != -1)
12446         regno = (unsigned) leaf_reg;
12447     }
12448 #endif
12449
12450   return DBX_REGISTER_NUMBER (regno);
12451 }
12452
12453 /* Optionally add a DW_OP_piece term to a location description expression.
12454    DW_OP_piece is only added if the location description expression already
12455    doesn't end with DW_OP_piece.  */
12456
12457 static void
12458 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12459 {
12460   dw_loc_descr_ref loc;
12461
12462   if (*list_head != NULL)
12463     {
12464       /* Find the end of the chain.  */
12465       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12466         ;
12467
12468       if (loc->dw_loc_opc != DW_OP_piece)
12469         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12470     }
12471 }
12472
12473 /* Return a location descriptor that designates a machine register or
12474    zero if there is none.  */
12475
12476 static dw_loc_descr_ref
12477 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12478 {
12479   rtx regs;
12480
12481   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12482     return 0;
12483
12484   regs = targetm.dwarf_register_span (rtl);
12485
12486   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
12487     return multiple_reg_loc_descriptor (rtl, regs, initialized);
12488   else
12489     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
12490 }
12491
12492 /* Return a location descriptor that designates a machine register for
12493    a given hard register number.  */
12494
12495 static dw_loc_descr_ref
12496 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12497 {
12498   dw_loc_descr_ref reg_loc_descr;
12499
12500   if (regno <= 31)
12501     reg_loc_descr
12502       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12503   else
12504     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12505
12506   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12507     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12508
12509   return reg_loc_descr;
12510 }
12511
12512 /* Given an RTL of a register, return a location descriptor that
12513    designates a value that spans more than one register.  */
12514
12515 static dw_loc_descr_ref
12516 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12517                              enum var_init_status initialized)
12518 {
12519   int nregs, size, i;
12520   unsigned reg;
12521   dw_loc_descr_ref loc_result = NULL;
12522
12523   reg = REGNO (rtl);
12524 #ifdef LEAF_REG_REMAP
12525   if (current_function_uses_only_leaf_regs)
12526     {
12527       int leaf_reg = LEAF_REG_REMAP (reg);
12528       if (leaf_reg != -1)
12529         reg = (unsigned) leaf_reg;
12530     }
12531 #endif
12532   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12533   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
12534
12535   /* Simple, contiguous registers.  */
12536   if (regs == NULL_RTX)
12537     {
12538       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
12539
12540       loc_result = NULL;
12541       while (nregs--)
12542         {
12543           dw_loc_descr_ref t;
12544
12545           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
12546                                       VAR_INIT_STATUS_INITIALIZED);
12547           add_loc_descr (&loc_result, t);
12548           add_loc_descr_op_piece (&loc_result, size);
12549           ++reg;
12550         }
12551       return loc_result;
12552     }
12553
12554   /* Now onto stupid register sets in non contiguous locations.  */
12555
12556   gcc_assert (GET_CODE (regs) == PARALLEL);
12557
12558   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12559   loc_result = NULL;
12560
12561   for (i = 0; i < XVECLEN (regs, 0); ++i)
12562     {
12563       dw_loc_descr_ref t;
12564
12565       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
12566                                   VAR_INIT_STATUS_INITIALIZED);
12567       add_loc_descr (&loc_result, t);
12568       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12569       add_loc_descr_op_piece (&loc_result, size);
12570     }
12571
12572   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12573     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12574   return loc_result;
12575 }
12576
12577 #endif /* DWARF2_DEBUGGING_INFO */
12578
12579 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
12580
12581 /* Return a location descriptor that designates a constant.  */
12582
12583 static dw_loc_descr_ref
12584 int_loc_descriptor (HOST_WIDE_INT i)
12585 {
12586   enum dwarf_location_atom op;
12587
12588   /* Pick the smallest representation of a constant, rather than just
12589      defaulting to the LEB encoding.  */
12590   if (i >= 0)
12591     {
12592       if (i <= 31)
12593         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
12594       else if (i <= 0xff)
12595         op = DW_OP_const1u;
12596       else if (i <= 0xffff)
12597         op = DW_OP_const2u;
12598       else if (HOST_BITS_PER_WIDE_INT == 32
12599                || i <= 0xffffffff)
12600         op = DW_OP_const4u;
12601       else
12602         op = DW_OP_constu;
12603     }
12604   else
12605     {
12606       if (i >= -0x80)
12607         op = DW_OP_const1s;
12608       else if (i >= -0x8000)
12609         op = DW_OP_const2s;
12610       else if (HOST_BITS_PER_WIDE_INT == 32
12611                || i >= -0x80000000)
12612         op = DW_OP_const4s;
12613       else
12614         op = DW_OP_consts;
12615     }
12616
12617   return new_loc_descr (op, i, 0);
12618 }
12619 #endif
12620
12621 #ifdef DWARF2_DEBUGGING_INFO
12622 /* Return loc description representing "address" of integer value.
12623    This can appear only as toplevel expression.  */
12624
12625 static dw_loc_descr_ref
12626 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12627 {
12628   int litsize;
12629   dw_loc_descr_ref loc_result = NULL;
12630
12631   if (!(dwarf_version >= 4 || !dwarf_strict))
12632     return NULL;
12633
12634   if (i >= 0)
12635     {
12636       if (i <= 31)
12637         litsize = 1;
12638       else if (i <= 0xff)
12639         litsize = 2;
12640       else if (i <= 0xffff)
12641         litsize = 3;
12642       else if (HOST_BITS_PER_WIDE_INT == 32
12643                || i <= 0xffffffff)
12644         litsize = 5;
12645       else
12646         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12647     }
12648   else
12649     {
12650       if (i >= -0x80)
12651         litsize = 2;
12652       else if (i >= -0x8000)
12653         litsize = 3;
12654       else if (HOST_BITS_PER_WIDE_INT == 32
12655                || i >= -0x80000000)
12656         litsize = 5;
12657       else
12658         litsize = 1 + size_of_sleb128 (i);
12659     }
12660   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12661      is more compact.  For DW_OP_stack_value we need:
12662      litsize + 1 (DW_OP_stack_value)
12663      and for DW_OP_implicit_value:
12664      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12665   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12666     {
12667       loc_result = int_loc_descriptor (i);
12668       add_loc_descr (&loc_result,
12669                      new_loc_descr (DW_OP_stack_value, 0, 0));
12670       return loc_result;
12671     }
12672
12673   loc_result = new_loc_descr (DW_OP_implicit_value,
12674                               size, 0);
12675   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12676   loc_result->dw_loc_oprnd2.v.val_int = i;
12677   return loc_result;
12678 }
12679
12680 /* Return a location descriptor that designates a base+offset location.  */
12681
12682 static dw_loc_descr_ref
12683 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12684                  enum var_init_status initialized)
12685 {
12686   unsigned int regno;
12687   dw_loc_descr_ref result;
12688   dw_fde_ref fde = current_fde ();
12689
12690   /* We only use "frame base" when we're sure we're talking about the
12691      post-prologue local stack frame.  We do this by *not* running
12692      register elimination until this point, and recognizing the special
12693      argument pointer and soft frame pointer rtx's.  */
12694   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12695     {
12696       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12697
12698       if (elim != reg)
12699         {
12700           if (GET_CODE (elim) == PLUS)
12701             {
12702               offset += INTVAL (XEXP (elim, 1));
12703               elim = XEXP (elim, 0);
12704             }
12705           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12706                        && (elim == hard_frame_pointer_rtx
12707                            || elim == stack_pointer_rtx))
12708                       || elim == (frame_pointer_needed
12709                                   ? hard_frame_pointer_rtx
12710                                   : stack_pointer_rtx));
12711
12712           /* If drap register is used to align stack, use frame
12713              pointer + offset to access stack variables.  If stack
12714              is aligned without drap, use stack pointer + offset to
12715              access stack variables.  */
12716           if (crtl->stack_realign_tried
12717               && reg == frame_pointer_rtx)
12718             {
12719               int base_reg
12720                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12721                                       ? HARD_FRAME_POINTER_REGNUM
12722                                       : STACK_POINTER_REGNUM);
12723               return new_reg_loc_descr (base_reg, offset);
12724             }
12725
12726           offset += frame_pointer_fb_offset;
12727           return new_loc_descr (DW_OP_fbreg, offset, 0);
12728         }
12729     }
12730   else if (fde
12731            && fde->drap_reg != INVALID_REGNUM
12732            && (fde->drap_reg == REGNO (reg)
12733                || fde->vdrap_reg == REGNO (reg)))
12734     {
12735       /* Use cfa+offset to represent the location of arguments passed
12736          on stack when drap is used to align stack.  */
12737       return new_loc_descr (DW_OP_fbreg, offset, 0);
12738     }
12739
12740   regno = dbx_reg_number (reg);
12741   if (regno <= 31)
12742     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12743                             offset, 0);
12744   else
12745     result = new_loc_descr (DW_OP_bregx, regno, offset);
12746
12747   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12748     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12749
12750   return result;
12751 }
12752
12753 /* Return true if this RTL expression describes a base+offset calculation.  */
12754
12755 static inline int
12756 is_based_loc (const_rtx rtl)
12757 {
12758   return (GET_CODE (rtl) == PLUS
12759           && ((REG_P (XEXP (rtl, 0))
12760                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12761                && CONST_INT_P (XEXP (rtl, 1)))));
12762 }
12763
12764 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12765    failed.  */
12766
12767 static dw_loc_descr_ref
12768 tls_mem_loc_descriptor (rtx mem)
12769 {
12770   tree base;
12771   dw_loc_descr_ref loc_result;
12772
12773   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
12774     return NULL;
12775
12776   base = get_base_address (MEM_EXPR (mem));
12777   if (base == NULL
12778       || TREE_CODE (base) != VAR_DECL
12779       || !DECL_THREAD_LOCAL_P (base))
12780     return NULL;
12781
12782   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
12783   if (loc_result == NULL)
12784     return NULL;
12785
12786   if (INTVAL (MEM_OFFSET (mem)))
12787     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
12788
12789   return loc_result;
12790 }
12791
12792 /* Output debug info about reason why we failed to expand expression as dwarf
12793    expression.  */
12794
12795 static void
12796 expansion_failed (tree expr, rtx rtl, char const *reason)
12797 {
12798   if (dump_file && (dump_flags & TDF_DETAILS))
12799     {
12800       fprintf (dump_file, "Failed to expand as dwarf: ");
12801       if (expr)
12802         print_generic_expr (dump_file, expr, dump_flags);
12803       if (rtl)
12804         {
12805           fprintf (dump_file, "\n");
12806           print_rtl (dump_file, rtl);
12807         }
12808       fprintf (dump_file, "\nReason: %s\n", reason);
12809     }
12810 }
12811
12812 /* Helper function for const_ok_for_output, called either directly
12813    or via for_each_rtx.  */
12814
12815 static int
12816 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
12817 {
12818   rtx rtl = *rtlp;
12819
12820   if (GET_CODE (rtl) != SYMBOL_REF)
12821     return 0;
12822
12823   if (CONSTANT_POOL_ADDRESS_P (rtl))
12824     {
12825       bool marked;
12826       get_pool_constant_mark (rtl, &marked);
12827       /* If all references to this pool constant were optimized away,
12828          it was not output and thus we can't represent it.  */
12829       if (!marked)
12830         {
12831           expansion_failed (NULL_TREE, rtl,
12832                             "Constant was removed from constant pool.\n");
12833           return 1;
12834         }
12835     }
12836
12837   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12838     return 1;
12839
12840   /* Avoid references to external symbols in debug info, on several targets
12841      the linker might even refuse to link when linking a shared library,
12842      and in many other cases the relocations for .debug_info/.debug_loc are
12843      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12844      to be defined within the same shared library or executable are fine.  */
12845   if (SYMBOL_REF_EXTERNAL_P (rtl))
12846     {
12847       tree decl = SYMBOL_REF_DECL (rtl);
12848
12849       if (decl == NULL || !targetm.binds_local_p (decl))
12850         {
12851           expansion_failed (NULL_TREE, rtl,
12852                             "Symbol not defined in current TU.\n");
12853           return 1;
12854         }
12855     }
12856
12857   return 0;
12858 }
12859
12860 /* Return true if constant RTL can be emitted in DW_OP_addr or
12861    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12862    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12863
12864 static bool
12865 const_ok_for_output (rtx rtl)
12866 {
12867   if (GET_CODE (rtl) == SYMBOL_REF)
12868     return const_ok_for_output_1 (&rtl, NULL) == 0;
12869
12870   if (GET_CODE (rtl) == CONST)
12871     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
12872
12873   return true;
12874 }
12875
12876 /* The following routine converts the RTL for a variable or parameter
12877    (resident in memory) into an equivalent Dwarf representation of a
12878    mechanism for getting the address of that same variable onto the top of a
12879    hypothetical "address evaluation" stack.
12880
12881    When creating memory location descriptors, we are effectively transforming
12882    the RTL for a memory-resident object into its Dwarf postfix expression
12883    equivalent.  This routine recursively descends an RTL tree, turning
12884    it into Dwarf postfix code as it goes.
12885
12886    MODE is the mode of the memory reference, needed to handle some
12887    autoincrement addressing modes.
12888
12889    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
12890    location list for RTL.
12891
12892    Return 0 if we can't represent the location.  */
12893
12894 static dw_loc_descr_ref
12895 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12896                     enum var_init_status initialized)
12897 {
12898   dw_loc_descr_ref mem_loc_result = NULL;
12899   enum dwarf_location_atom op;
12900   dw_loc_descr_ref op0, op1;
12901
12902   /* Note that for a dynamically sized array, the location we will generate a
12903      description of here will be the lowest numbered location which is
12904      actually within the array.  That's *not* necessarily the same as the
12905      zeroth element of the array.  */
12906
12907   rtl = targetm.delegitimize_address (rtl);
12908
12909   switch (GET_CODE (rtl))
12910     {
12911     case POST_INC:
12912     case POST_DEC:
12913     case POST_MODIFY:
12914       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
12915
12916     case SUBREG:
12917       /* The case of a subreg may arise when we have a local (register)
12918          variable or a formal (register) parameter which doesn't quite fill
12919          up an entire register.  For now, just assume that it is
12920          legitimate to make the Dwarf info refer to the whole register which
12921          contains the given subreg.  */
12922       if (!subreg_lowpart_p (rtl))
12923         break;
12924       rtl = SUBREG_REG (rtl);
12925       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
12926         break;
12927       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
12928         break;
12929       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
12930       break;
12931
12932     case REG:
12933       /* Whenever a register number forms a part of the description of the
12934          method for calculating the (dynamic) address of a memory resident
12935          object, DWARF rules require the register number be referred to as
12936          a "base register".  This distinction is not based in any way upon
12937          what category of register the hardware believes the given register
12938          belongs to.  This is strictly DWARF terminology we're dealing with
12939          here. Note that in cases where the location of a memory-resident
12940          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12941          OP_CONST (0)) the actual DWARF location descriptor that we generate
12942          may just be OP_BASEREG (basereg).  This may look deceptively like
12943          the object in question was allocated to a register (rather than in
12944          memory) so DWARF consumers need to be aware of the subtle
12945          distinction between OP_REG and OP_BASEREG.  */
12946       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12947         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12948       else if (stack_realign_drap
12949                && crtl->drap_reg
12950                && crtl->args.internal_arg_pointer == rtl
12951                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12952         {
12953           /* If RTL is internal_arg_pointer, which has been optimized
12954              out, use DRAP instead.  */
12955           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12956                                             VAR_INIT_STATUS_INITIALIZED);
12957         }
12958       break;
12959
12960     case SIGN_EXTEND:
12961     case ZERO_EXTEND:
12962       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12963                                 VAR_INIT_STATUS_INITIALIZED);
12964       if (op0 == 0)
12965         break;
12966       else
12967         {
12968           int shift = DWARF2_ADDR_SIZE
12969                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12970           shift *= BITS_PER_UNIT;
12971           if (GET_CODE (rtl) == SIGN_EXTEND)
12972             op = DW_OP_shra;
12973           else
12974             op = DW_OP_shr;
12975           mem_loc_result = op0;
12976           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12977           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12978           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12979           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12980         }
12981       break;
12982
12983     case MEM:
12984       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
12985                                            VAR_INIT_STATUS_INITIALIZED);
12986       if (mem_loc_result == NULL)
12987         mem_loc_result = tls_mem_loc_descriptor (rtl);
12988       if (mem_loc_result != 0)
12989         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12990       break;
12991
12992     case LO_SUM:
12993          rtl = XEXP (rtl, 1);
12994
12995       /* ... fall through ...  */
12996
12997     case LABEL_REF:
12998       /* Some ports can transform a symbol ref into a label ref, because
12999          the symbol ref is too far away and has to be dumped into a constant
13000          pool.  */
13001     case CONST:
13002     case SYMBOL_REF:
13003       /* Alternatively, the symbol in the constant pool might be referenced
13004          by a different symbol.  */
13005       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
13006         {
13007           bool marked;
13008           rtx tmp = get_pool_constant_mark (rtl, &marked);
13009
13010           if (GET_CODE (tmp) == SYMBOL_REF)
13011             {
13012               rtl = tmp;
13013               if (CONSTANT_POOL_ADDRESS_P (tmp))
13014                 get_pool_constant_mark (tmp, &marked);
13015               else
13016                 marked = true;
13017             }
13018
13019           /* If all references to this pool constant were optimized away,
13020              it was not output and thus we can't represent it.
13021              FIXME: might try to use DW_OP_const_value here, though
13022              DW_OP_piece complicates it.  */
13023           if (!marked)
13024             {
13025               expansion_failed (NULL_TREE, rtl,
13026                                 "Constant was removed from constant pool.\n");
13027               return 0;
13028             }
13029         }
13030
13031       if (GET_CODE (rtl) == SYMBOL_REF
13032           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13033         {
13034           dw_loc_descr_ref temp;
13035
13036           /* If this is not defined, we have no way to emit the data.  */
13037           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13038             break;
13039
13040           temp = new_loc_descr (DW_OP_addr, 0, 0);
13041           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13042           temp->dw_loc_oprnd1.v.val_addr = rtl;
13043           temp->dtprel = true;
13044
13045           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13046           add_loc_descr (&mem_loc_result, temp);
13047
13048           break;
13049         }
13050
13051       if (!const_ok_for_output (rtl))
13052         break;
13053
13054     symref:
13055       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13056       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13057       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13058       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13059       break;
13060
13061     case CONCAT:
13062     case CONCATN:
13063     case VAR_LOCATION:
13064       expansion_failed (NULL_TREE, rtl,
13065                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13066       return 0;
13067
13068     case PRE_MODIFY:
13069       /* Extract the PLUS expression nested inside and fall into
13070          PLUS code below.  */
13071       rtl = XEXP (rtl, 1);
13072       goto plus;
13073
13074     case PRE_INC:
13075     case PRE_DEC:
13076       /* Turn these into a PLUS expression and fall into the PLUS code
13077          below.  */
13078       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13079                           GEN_INT (GET_CODE (rtl) == PRE_INC
13080                                    ? GET_MODE_UNIT_SIZE (mode)
13081                                    : -GET_MODE_UNIT_SIZE (mode)));
13082
13083       /* ... fall through ...  */
13084
13085     case PLUS:
13086     plus:
13087       if (is_based_loc (rtl))
13088         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13089                                           INTVAL (XEXP (rtl, 1)),
13090                                           VAR_INIT_STATUS_INITIALIZED);
13091       else
13092         {
13093           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13094                                                VAR_INIT_STATUS_INITIALIZED);
13095           if (mem_loc_result == 0)
13096             break;
13097
13098           if (CONST_INT_P (XEXP (rtl, 1)))
13099             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13100           else
13101             {
13102               dw_loc_descr_ref mem_loc_result2
13103                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13104                                       VAR_INIT_STATUS_INITIALIZED);
13105               if (mem_loc_result2 == 0)
13106                 break;
13107               add_loc_descr (&mem_loc_result, mem_loc_result2);
13108               add_loc_descr (&mem_loc_result,
13109                              new_loc_descr (DW_OP_plus, 0, 0));
13110             }
13111         }
13112       break;
13113
13114     /* If a pseudo-reg is optimized away, it is possible for it to
13115        be replaced with a MEM containing a multiply or shift.  */
13116     case MINUS:
13117       op = DW_OP_minus;
13118       goto do_binop;
13119
13120     case MULT:
13121       op = DW_OP_mul;
13122       goto do_binop;
13123
13124     case DIV:
13125       op = DW_OP_div;
13126       goto do_binop;
13127
13128     case MOD:
13129       op = DW_OP_mod;
13130       goto do_binop;
13131
13132     case ASHIFT:
13133       op = DW_OP_shl;
13134       goto do_binop;
13135
13136     case ASHIFTRT:
13137       op = DW_OP_shra;
13138       goto do_binop;
13139
13140     case LSHIFTRT:
13141       op = DW_OP_shr;
13142       goto do_binop;
13143
13144     case AND:
13145       op = DW_OP_and;
13146       goto do_binop;
13147
13148     case IOR:
13149       op = DW_OP_or;
13150       goto do_binop;
13151
13152     case XOR:
13153       op = DW_OP_xor;
13154       goto do_binop;
13155
13156     do_binop:
13157       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13158                                 VAR_INIT_STATUS_INITIALIZED);
13159       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13160                                 VAR_INIT_STATUS_INITIALIZED);
13161
13162       if (op0 == 0 || op1 == 0)
13163         break;
13164
13165       mem_loc_result = op0;
13166       add_loc_descr (&mem_loc_result, op1);
13167       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13168       break;
13169
13170     case NOT:
13171       op = DW_OP_not;
13172       goto do_unop;
13173
13174     case ABS:
13175       op = DW_OP_abs;
13176       goto do_unop;
13177
13178     case NEG:
13179       op = DW_OP_neg;
13180       goto do_unop;
13181
13182     do_unop:
13183       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13184                                 VAR_INIT_STATUS_INITIALIZED);
13185
13186       if (op0 == 0)
13187         break;
13188
13189       mem_loc_result = op0;
13190       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13191       break;
13192
13193     case CONST_INT:
13194       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13195       break;
13196
13197     case EQ:
13198       op = DW_OP_eq;
13199       goto do_scompare;
13200
13201     case GE:
13202       op = DW_OP_ge;
13203       goto do_scompare;
13204
13205     case GT:
13206       op = DW_OP_gt;
13207       goto do_scompare;
13208
13209     case LE:
13210       op = DW_OP_le;
13211       goto do_scompare;
13212
13213     case LT:
13214       op = DW_OP_lt;
13215       goto do_scompare;
13216
13217     case NE:
13218       op = DW_OP_ne;
13219       goto do_scompare;
13220
13221     do_scompare:
13222       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13223           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13224           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13225         break;
13226
13227       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13228                                 VAR_INIT_STATUS_INITIALIZED);
13229       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13230                                 VAR_INIT_STATUS_INITIALIZED);
13231
13232       if (op0 == 0 || op1 == 0)
13233         break;
13234
13235       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13236         {
13237           int shift = DWARF2_ADDR_SIZE
13238                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13239           shift *= BITS_PER_UNIT;
13240           add_loc_descr (&op0, int_loc_descriptor (shift));
13241           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13242           if (CONST_INT_P (XEXP (rtl, 1)))
13243             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13244           else
13245             {
13246               add_loc_descr (&op1, int_loc_descriptor (shift));
13247               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13248             }
13249         }
13250
13251     do_compare:
13252       mem_loc_result = op0;
13253       add_loc_descr (&mem_loc_result, op1);
13254       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13255       if (STORE_FLAG_VALUE != 1)
13256         {
13257           add_loc_descr (&mem_loc_result,
13258                          int_loc_descriptor (STORE_FLAG_VALUE));
13259           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13260         }
13261       break;
13262
13263     case GEU:
13264       op = DW_OP_ge;
13265       goto do_ucompare;
13266
13267     case GTU:
13268       op = DW_OP_gt;
13269       goto do_ucompare;
13270
13271     case LEU:
13272       op = DW_OP_le;
13273       goto do_ucompare;
13274
13275     case LTU:
13276       op = DW_OP_lt;
13277       goto do_ucompare;
13278
13279     do_ucompare:
13280       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13281           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13282           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13283         break;
13284
13285       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13286                                 VAR_INIT_STATUS_INITIALIZED);
13287       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13288                                 VAR_INIT_STATUS_INITIALIZED);
13289
13290       if (op0 == 0 || op1 == 0)
13291         break;
13292
13293       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13294         {
13295           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13296           add_loc_descr (&op0, int_loc_descriptor (mask));
13297           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13298           if (CONST_INT_P (XEXP (rtl, 1)))
13299             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13300           else
13301             {
13302               add_loc_descr (&op1, int_loc_descriptor (mask));
13303               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13304             }
13305         }
13306       else
13307         {
13308           HOST_WIDE_INT bias = 1;
13309           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13310           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13311           if (CONST_INT_P (XEXP (rtl, 1)))
13312             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13313                                       + INTVAL (XEXP (rtl, 1)));
13314           else
13315             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13316         }
13317       goto do_compare;
13318
13319     case SMIN:
13320     case SMAX:
13321     case UMIN:
13322     case UMAX:
13323       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13324           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13325           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13326         break;
13327
13328       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13329                                 VAR_INIT_STATUS_INITIALIZED);
13330       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13331                                 VAR_INIT_STATUS_INITIALIZED);
13332
13333       if (op0 == 0 || op1 == 0)
13334         break;
13335
13336       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13337       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13338       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13339       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13340         {
13341           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13342             {
13343               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13344               add_loc_descr (&op0, int_loc_descriptor (mask));
13345               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13346               add_loc_descr (&op1, int_loc_descriptor (mask));
13347               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13348             }
13349           else
13350             {
13351               HOST_WIDE_INT bias = 1;
13352               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13353               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13354               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13355             }
13356         }
13357       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13358         {
13359           int shift = DWARF2_ADDR_SIZE
13360                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13361           shift *= BITS_PER_UNIT;
13362           add_loc_descr (&op0, int_loc_descriptor (shift));
13363           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13364           add_loc_descr (&op1, int_loc_descriptor (shift));
13365           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13366         }
13367
13368       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13369         op = DW_OP_lt;
13370       else
13371         op = DW_OP_gt;
13372       mem_loc_result = op0;
13373       add_loc_descr (&mem_loc_result, op1);
13374       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13375       {
13376         dw_loc_descr_ref bra_node, drop_node;
13377
13378         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13379         add_loc_descr (&mem_loc_result, bra_node);
13380         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13381         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13382         add_loc_descr (&mem_loc_result, drop_node);
13383         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13384         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13385       }
13386       break;
13387
13388     case ZERO_EXTRACT:
13389     case SIGN_EXTRACT:
13390       if (CONST_INT_P (XEXP (rtl, 1))
13391           && CONST_INT_P (XEXP (rtl, 2))
13392           && ((unsigned) INTVAL (XEXP (rtl, 1))
13393               + (unsigned) INTVAL (XEXP (rtl, 2))
13394               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
13395           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13396           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13397         {
13398           int shift, size;
13399           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13400                                     VAR_INIT_STATUS_INITIALIZED);
13401           if (op0 == 0)
13402             break;
13403           if (GET_CODE (rtl) == SIGN_EXTRACT)
13404             op = DW_OP_shra;
13405           else
13406             op = DW_OP_shr;
13407           mem_loc_result = op0;
13408           size = INTVAL (XEXP (rtl, 1));
13409           shift = INTVAL (XEXP (rtl, 2));
13410           if (BITS_BIG_ENDIAN)
13411             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13412                     - shift - size;
13413           if (shift + size != (int) DWARF2_ADDR_SIZE)
13414             {
13415               add_loc_descr (&mem_loc_result,
13416                              int_loc_descriptor (DWARF2_ADDR_SIZE
13417                                                  - shift - size));
13418               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13419             }
13420           if (size != (int) DWARF2_ADDR_SIZE)
13421             {
13422               add_loc_descr (&mem_loc_result,
13423                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13424               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13425             }
13426         }
13427       break;
13428
13429     case COMPARE:
13430     case IF_THEN_ELSE:
13431     case ROTATE:
13432     case ROTATERT:
13433     case TRUNCATE:
13434       /* In theory, we could implement the above.  */
13435       /* DWARF cannot represent the unsigned compare operations
13436          natively.  */
13437     case SS_MULT:
13438     case US_MULT:
13439     case SS_DIV:
13440     case US_DIV:
13441     case UDIV:
13442     case UMOD:
13443     case UNORDERED:
13444     case ORDERED:
13445     case UNEQ:
13446     case UNGE:
13447     case UNGT:
13448     case UNLE:
13449     case UNLT:
13450     case LTGT:
13451     case FLOAT_EXTEND:
13452     case FLOAT_TRUNCATE:
13453     case FLOAT:
13454     case UNSIGNED_FLOAT:
13455     case FIX:
13456     case UNSIGNED_FIX:
13457     case FRACT_CONVERT:
13458     case UNSIGNED_FRACT_CONVERT:
13459     case SAT_FRACT:
13460     case UNSIGNED_SAT_FRACT:
13461     case SQRT:
13462     case BSWAP:
13463     case FFS:
13464     case CLZ:
13465     case CTZ:
13466     case POPCOUNT:
13467     case PARITY:
13468     case ASM_OPERANDS:
13469     case UNSPEC:
13470     case HIGH:
13471       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13472          can't express it in the debug info.  This can happen e.g. with some
13473          TLS UNSPECs.  */
13474       break;
13475
13476     case CONST_STRING:
13477       resolve_one_addr (&rtl, NULL);
13478       goto symref;
13479
13480     default:
13481 #ifdef ENABLE_CHECKING
13482       print_rtl (stderr, rtl);
13483       gcc_unreachable ();
13484 #else
13485       break;
13486 #endif
13487     }
13488
13489   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13490     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13491
13492   return mem_loc_result;
13493 }
13494
13495 /* Return a descriptor that describes the concatenation of two locations.
13496    This is typically a complex variable.  */
13497
13498 static dw_loc_descr_ref
13499 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13500 {
13501   dw_loc_descr_ref cc_loc_result = NULL;
13502   dw_loc_descr_ref x0_ref
13503     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13504   dw_loc_descr_ref x1_ref
13505     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13506
13507   if (x0_ref == 0 || x1_ref == 0)
13508     return 0;
13509
13510   cc_loc_result = x0_ref;
13511   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13512
13513   add_loc_descr (&cc_loc_result, x1_ref);
13514   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13515
13516   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13517     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13518
13519   return cc_loc_result;
13520 }
13521
13522 /* Return a descriptor that describes the concatenation of N
13523    locations.  */
13524
13525 static dw_loc_descr_ref
13526 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13527 {
13528   unsigned int i;
13529   dw_loc_descr_ref cc_loc_result = NULL;
13530   unsigned int n = XVECLEN (concatn, 0);
13531
13532   for (i = 0; i < n; ++i)
13533     {
13534       dw_loc_descr_ref ref;
13535       rtx x = XVECEXP (concatn, 0, i);
13536
13537       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13538       if (ref == NULL)
13539         return NULL;
13540
13541       add_loc_descr (&cc_loc_result, ref);
13542       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13543     }
13544
13545   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13546     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13547
13548   return cc_loc_result;
13549 }
13550
13551 /* Output a proper Dwarf location descriptor for a variable or parameter
13552    which is either allocated in a register or in a memory location.  For a
13553    register, we just generate an OP_REG and the register number.  For a
13554    memory location we provide a Dwarf postfix expression describing how to
13555    generate the (dynamic) address of the object onto the address stack.
13556
13557    MODE is mode of the decl if this loc_descriptor is going to be used in
13558    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13559    allowed, VOIDmode otherwise.
13560
13561    If we don't know how to describe it, return 0.  */
13562
13563 static dw_loc_descr_ref
13564 loc_descriptor (rtx rtl, enum machine_mode mode,
13565                 enum var_init_status initialized)
13566 {
13567   dw_loc_descr_ref loc_result = NULL;
13568
13569   switch (GET_CODE (rtl))
13570     {
13571     case SUBREG:
13572       /* The case of a subreg may arise when we have a local (register)
13573          variable or a formal (register) parameter which doesn't quite fill
13574          up an entire register.  For now, just assume that it is
13575          legitimate to make the Dwarf info refer to the whole register which
13576          contains the given subreg.  */
13577       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
13578       break;
13579
13580     case REG:
13581       loc_result = reg_loc_descriptor (rtl, initialized);
13582       break;
13583
13584     case SIGN_EXTEND:
13585     case ZERO_EXTEND:
13586       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13587       break;
13588
13589     case MEM:
13590       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13591                                        initialized);
13592       if (loc_result == NULL)
13593         loc_result = tls_mem_loc_descriptor (rtl);
13594       break;
13595
13596     case CONCAT:
13597       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13598                                           initialized);
13599       break;
13600
13601     case CONCATN:
13602       loc_result = concatn_loc_descriptor (rtl, initialized);
13603       break;
13604
13605     case VAR_LOCATION:
13606       /* Single part.  */
13607       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13608         {
13609           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
13610                                        initialized);
13611           break;
13612         }
13613
13614       rtl = XEXP (rtl, 1);
13615       /* FALLTHRU */
13616
13617     case PARALLEL:
13618       {
13619         rtvec par_elems = XVEC (rtl, 0);
13620         int num_elem = GET_NUM_ELEM (par_elems);
13621         enum machine_mode mode;
13622         int i;
13623
13624         /* Create the first one, so we have something to add to.  */
13625         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13626                                      VOIDmode, initialized);
13627         if (loc_result == NULL)
13628           return NULL;
13629         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13630         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13631         for (i = 1; i < num_elem; i++)
13632           {
13633             dw_loc_descr_ref temp;
13634
13635             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13636                                    VOIDmode, initialized);
13637             if (temp == NULL)
13638               return NULL;
13639             add_loc_descr (&loc_result, temp);
13640             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13641             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13642           }
13643       }
13644       break;
13645
13646     case CONST_INT:
13647       if (mode != VOIDmode && mode != BLKmode)
13648         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13649                                                     INTVAL (rtl));
13650       break;
13651
13652     case CONST_DOUBLE:
13653       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13654         {
13655           /* Note that a CONST_DOUBLE rtx could represent either an integer
13656              or a floating-point constant.  A CONST_DOUBLE is used whenever
13657              the constant requires more than one word in order to be
13658              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13659           if (GET_MODE (rtl) != VOIDmode)
13660             mode = GET_MODE (rtl);
13661
13662           loc_result = new_loc_descr (DW_OP_implicit_value,
13663                                       GET_MODE_SIZE (mode), 0);
13664           if (SCALAR_FLOAT_MODE_P (mode))
13665             {
13666               unsigned int length = GET_MODE_SIZE (mode);
13667               unsigned char *array = GGC_NEWVEC (unsigned char, length);
13668
13669               insert_float (rtl, array);
13670               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13671               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13672               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13673               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13674             }
13675           else
13676             {
13677               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13678               loc_result->dw_loc_oprnd2.v.val_double.high
13679                 = CONST_DOUBLE_HIGH (rtl);
13680               loc_result->dw_loc_oprnd2.v.val_double.low
13681                 = CONST_DOUBLE_LOW (rtl);
13682             }
13683         }
13684       break;
13685
13686     case CONST_VECTOR:
13687       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13688         {
13689           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13690           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13691           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13692           unsigned int i;
13693           unsigned char *p;
13694
13695           mode = GET_MODE (rtl);
13696           switch (GET_MODE_CLASS (mode))
13697             {
13698             case MODE_VECTOR_INT:
13699               for (i = 0, p = array; i < length; i++, p += elt_size)
13700                 {
13701                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13702                   HOST_WIDE_INT lo, hi;
13703
13704                   switch (GET_CODE (elt))
13705                     {
13706                     case CONST_INT:
13707                       lo = INTVAL (elt);
13708                       hi = -(lo < 0);
13709                       break;
13710
13711                     case CONST_DOUBLE:
13712                       lo = CONST_DOUBLE_LOW (elt);
13713                       hi = CONST_DOUBLE_HIGH (elt);
13714                       break;
13715
13716                     default:
13717                       gcc_unreachable ();
13718                     }
13719
13720                   if (elt_size <= sizeof (HOST_WIDE_INT))
13721                     insert_int (lo, elt_size, p);
13722                   else
13723                     {
13724                       unsigned char *p0 = p;
13725                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13726
13727                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13728                       if (WORDS_BIG_ENDIAN)
13729                         {
13730                           p0 = p1;
13731                           p1 = p;
13732                         }
13733                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13734                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13735                     }
13736                 }
13737               break;
13738
13739             case MODE_VECTOR_FLOAT:
13740               for (i = 0, p = array; i < length; i++, p += elt_size)
13741                 {
13742                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13743                   insert_float (elt, p);
13744                 }
13745               break;
13746
13747             default:
13748               gcc_unreachable ();
13749             }
13750
13751           loc_result = new_loc_descr (DW_OP_implicit_value,
13752                                       length * elt_size, 0);
13753           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13754           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13755           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13756           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13757         }
13758       break;
13759
13760     case CONST:
13761       if (mode == VOIDmode
13762           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
13763           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
13764           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13765         {
13766           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13767           break;
13768         }
13769       /* FALLTHROUGH */
13770     case SYMBOL_REF:
13771       if (!const_ok_for_output (rtl))
13772         break;
13773     case LABEL_REF:
13774       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13775           && (dwarf_version >= 4 || !dwarf_strict))
13776         {
13777           loc_result = new_loc_descr (DW_OP_implicit_value,
13778                                       DWARF2_ADDR_SIZE, 0);
13779           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
13780           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
13781           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13782         }
13783       break;
13784
13785     default:
13786       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13787           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13788           && (dwarf_version >= 4 || !dwarf_strict))
13789         {
13790           /* Value expression.  */
13791           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13792           if (loc_result)
13793             add_loc_descr (&loc_result,
13794                            new_loc_descr (DW_OP_stack_value, 0, 0));
13795         }
13796       break;
13797     }
13798
13799   return loc_result;
13800 }
13801
13802 /* We need to figure out what section we should use as the base for the
13803    address ranges where a given location is valid.
13804    1. If this particular DECL has a section associated with it, use that.
13805    2. If this function has a section associated with it, use that.
13806    3. Otherwise, use the text section.
13807    XXX: If you split a variable across multiple sections, we won't notice.  */
13808
13809 static const char *
13810 secname_for_decl (const_tree decl)
13811 {
13812   const char *secname;
13813
13814   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13815     {
13816       tree sectree = DECL_SECTION_NAME (decl);
13817       secname = TREE_STRING_POINTER (sectree);
13818     }
13819   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13820     {
13821       tree sectree = DECL_SECTION_NAME (current_function_decl);
13822       secname = TREE_STRING_POINTER (sectree);
13823     }
13824   else if (cfun && in_cold_section_p)
13825     secname = crtl->subsections.cold_section_label;
13826   else
13827     secname = text_section_label;
13828
13829   return secname;
13830 }
13831
13832 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13833
13834 static bool
13835 decl_by_reference_p (tree decl)
13836 {
13837   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13838            || TREE_CODE (decl) == VAR_DECL)
13839           && DECL_BY_REFERENCE (decl));
13840 }
13841
13842 /* Return single element location list containing loc descr REF.  */
13843
13844 static dw_loc_list_ref
13845 single_element_loc_list (dw_loc_descr_ref ref)
13846 {
13847   return new_loc_list (ref, NULL, NULL, NULL, 0);
13848 }
13849
13850 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13851    for VARLOC.  */
13852
13853 static dw_loc_descr_ref
13854 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13855                enum var_init_status initialized)
13856 {
13857   int have_address = 0;
13858   dw_loc_descr_ref descr;
13859   enum machine_mode mode;
13860
13861   if (want_address != 2)
13862     {
13863       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13864       /* Single part.  */
13865       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
13866         {
13867           varloc = XEXP (XEXP (varloc, 1), 0);
13868           mode = GET_MODE (varloc);
13869           if (MEM_P (varloc))
13870             {
13871               varloc = XEXP (varloc, 0);
13872               have_address = 1;
13873             }
13874           descr = mem_loc_descriptor (varloc, mode, initialized);
13875         }
13876       else
13877         return 0;
13878     }
13879   else
13880     {
13881       descr = loc_descriptor (varloc, DECL_MODE (loc), initialized);
13882       have_address = 1;
13883     }
13884
13885   if (!descr)
13886     return 0;
13887
13888   if (want_address == 2 && !have_address
13889       && (dwarf_version >= 4 || !dwarf_strict))
13890     {
13891       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13892         {
13893           expansion_failed (loc, NULL_RTX,
13894                             "DWARF address size mismatch");
13895           return 0;
13896         }
13897       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13898       have_address = 1;
13899     }
13900   /* Show if we can't fill the request for an address.  */
13901   if (want_address && !have_address)
13902     {
13903       expansion_failed (loc, NULL_RTX,
13904                         "Want address and only have value");
13905       return 0;
13906     }
13907
13908   /* If we've got an address and don't want one, dereference.  */
13909   if (!want_address && have_address)
13910     {
13911       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13912       enum dwarf_location_atom op;
13913
13914       if (size > DWARF2_ADDR_SIZE || size == -1)
13915         {
13916           expansion_failed (loc, NULL_RTX,
13917                             "DWARF address size mismatch");
13918           return 0;
13919         }
13920       else if (size == DWARF2_ADDR_SIZE)
13921         op = DW_OP_deref;
13922       else
13923         op = DW_OP_deref_size;
13924
13925       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13926     }
13927
13928   return descr;
13929 }
13930
13931 /* Return dwarf representation of location list representing for
13932    LOC_LIST of DECL.  WANT_ADDRESS has the same meaning as in
13933    loc_list_from_tree function.  */
13934
13935 static dw_loc_list_ref
13936 dw_loc_list (var_loc_list * loc_list, tree decl, int want_address)
13937 {
13938   const char *endname, *secname;
13939   dw_loc_list_ref list;
13940   rtx varloc;
13941   enum var_init_status initialized;
13942   struct var_loc_node *node;
13943   dw_loc_descr_ref descr;
13944   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13945
13946   /* Now that we know what section we are using for a base,
13947      actually construct the list of locations.
13948      The first location information is what is passed to the
13949      function that creates the location list, and the remaining
13950      locations just get added on to that list.
13951      Note that we only know the start address for a location
13952      (IE location changes), so to build the range, we use
13953      the range [current location start, next location start].
13954      This means we have to special case the last node, and generate
13955      a range of [last location start, end of function label].  */
13956
13957   node = loc_list->first;
13958   secname = secname_for_decl (decl);
13959
13960   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
13961     initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13962   else
13963     initialized = VAR_INIT_STATUS_INITIALIZED;
13964   varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13965   descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13966
13967   if (loc_list && loc_list->first != loc_list->last)
13968     list = new_loc_list (descr, node->label, node->next->label, secname, 1);
13969   else
13970     return single_element_loc_list (descr);
13971   node = node->next;
13972
13973   if (!node)
13974     return NULL;
13975
13976   for (; node->next; node = node->next)
13977     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13978       {
13979         /* The variable has a location between NODE->LABEL and
13980            NODE->NEXT->LABEL.  */
13981         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13982         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13983         descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13984         add_loc_descr_to_loc_list (&list, descr,
13985                                    node->label, node->next->label, secname);
13986       }
13987
13988   /* If the variable has a location at the last label
13989      it keeps its location until the end of function.  */
13990   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13991     {
13992       if (!current_function_decl)
13993         endname = text_end_label;
13994       else
13995         {
13996           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13997                                        current_function_funcdef_no);
13998           endname = ggc_strdup (label_id);
13999         }
14000
14001       initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
14002       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
14003       descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14004       add_loc_descr_to_loc_list (&list, descr, node->label, endname, secname);
14005     }
14006   return list;
14007 }
14008
14009 /* Return if the loc_list has only single element and thus can be represented
14010    as location description.   */
14011
14012 static bool
14013 single_element_loc_list_p (dw_loc_list_ref list)
14014 {
14015   return (!list->dw_loc_next && !list->begin && !list->end);
14016 }
14017
14018 /* To each location in list LIST add loc descr REF.  */
14019
14020 static void
14021 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14022 {
14023   dw_loc_descr_ref copy;
14024   add_loc_descr (&list->expr, ref);
14025   list = list->dw_loc_next;
14026   while (list)
14027     {
14028       copy = GGC_CNEW (dw_loc_descr_node);
14029       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14030       add_loc_descr (&list->expr, copy);
14031       while (copy->dw_loc_next)
14032         {
14033           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
14034           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14035           copy->dw_loc_next = new_copy;
14036           copy = new_copy;
14037         }
14038       list = list->dw_loc_next;
14039     }
14040 }
14041
14042 /* Given two lists RET and LIST
14043    produce location list that is result of adding expression in LIST
14044    to expression in RET on each possition in program.
14045    Might be destructive on both RET and LIST.
14046
14047    TODO: We handle only simple cases of RET or LIST having at most one
14048    element. General case would inolve sorting the lists in program order
14049    and merging them that will need some additional work.  
14050    Adding that will improve quality of debug info especially for SRA-ed
14051    structures.  */
14052
14053 static void
14054 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14055 {
14056   if (!list)
14057     return;
14058   if (!*ret)
14059     {
14060       *ret = list;
14061       return;
14062     }
14063   if (!list->dw_loc_next)
14064     {
14065       add_loc_descr_to_each (*ret, list->expr);
14066       return;
14067     }
14068   if (!(*ret)->dw_loc_next)
14069     {
14070       add_loc_descr_to_each (list, (*ret)->expr);
14071       *ret = list;
14072       return;
14073     }
14074   expansion_failed (NULL_TREE, NULL_RTX,
14075                     "Don't know how to merge two non-trivial"
14076                     " location lists.\n");
14077   *ret = NULL;
14078   return;
14079 }
14080
14081 /* LOC is constant expression.  Try a luck, look it up in constant
14082    pool and return its loc_descr of its address.  */
14083
14084 static dw_loc_descr_ref
14085 cst_pool_loc_descr (tree loc)
14086 {
14087   /* Get an RTL for this, if something has been emitted.  */
14088   rtx rtl = lookup_constant_def (loc);
14089   enum machine_mode mode;
14090
14091   if (!rtl || !MEM_P (rtl))
14092     {
14093       gcc_assert (!rtl);
14094       return 0;
14095     }
14096   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14097
14098   /* TODO: We might get more coverage if we was actually delaying expansion
14099      of all expressions till end of compilation when constant pools are fully
14100      populated.  */
14101   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14102     {
14103       expansion_failed (loc, NULL_RTX,
14104                         "CST value in contant pool but not marked.");
14105       return 0;
14106     }
14107   mode = GET_MODE (rtl);
14108   rtl = XEXP (rtl, 0);
14109   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14110 }
14111
14112 /* Return dw_loc_list representing address of addr_expr LOC
14113    by looking for innder INDIRECT_REF expression and turing it
14114    into simple arithmetics.  */
14115
14116 static dw_loc_list_ref
14117 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14118 {
14119   tree obj, offset;
14120   HOST_WIDE_INT bitsize, bitpos, bytepos;
14121   enum machine_mode mode;
14122   int volatilep;
14123   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14124   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14125
14126   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14127                              &bitsize, &bitpos, &offset, &mode,
14128                              &unsignedp, &volatilep, false);
14129   STRIP_NOPS (obj);
14130   if (bitpos % BITS_PER_UNIT)
14131     {
14132       expansion_failed (loc, NULL_RTX, "bitfield access");
14133       return 0;
14134     }
14135   if (!INDIRECT_REF_P (obj))
14136     {
14137       expansion_failed (obj,
14138                         NULL_RTX, "no indirect ref in inner refrence");
14139       return 0;
14140     }
14141   if (!offset && !bitpos)
14142     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14143   else if (toplev
14144            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14145            && (dwarf_version >= 4 || !dwarf_strict))
14146     {
14147       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14148       if (!list_ret)
14149         return 0;
14150       if (offset)
14151         {
14152           /* Variable offset.  */
14153           list_ret1 = loc_list_from_tree (offset, 0);
14154           if (list_ret1 == 0)
14155             return 0;
14156           add_loc_list (&list_ret, list_ret1);
14157           if (!list_ret)
14158             return 0;
14159           add_loc_descr_to_each (list_ret,
14160                                  new_loc_descr (DW_OP_plus, 0, 0));
14161         }
14162       bytepos = bitpos / BITS_PER_UNIT;
14163       if (bytepos > 0)
14164         add_loc_descr_to_each (list_ret,
14165                                new_loc_descr (DW_OP_plus_uconst,
14166                                               bytepos, 0));
14167       else if (bytepos < 0)
14168         loc_list_plus_const (list_ret, bytepos);
14169       add_loc_descr_to_each (list_ret,
14170                              new_loc_descr (DW_OP_stack_value, 0, 0));
14171     }
14172   return list_ret;
14173 }
14174
14175
14176 /* Generate Dwarf location list representing LOC.
14177    If WANT_ADDRESS is false, expression computing LOC will be computed
14178    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14179    if WANT_ADDRESS is 2, expression computing address useable in location
14180      will be returned (i.e. DW_OP_reg can be used
14181      to refer to register values).  */
14182
14183 static dw_loc_list_ref
14184 loc_list_from_tree (tree loc, int want_address)
14185 {
14186   dw_loc_descr_ref ret = NULL, ret1 = NULL;
14187   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14188   int have_address = 0;
14189   enum dwarf_location_atom op;
14190
14191   /* ??? Most of the time we do not take proper care for sign/zero
14192      extending the values properly.  Hopefully this won't be a real
14193      problem...  */
14194
14195   switch (TREE_CODE (loc))
14196     {
14197     case ERROR_MARK:
14198       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14199       return 0;
14200
14201     case PLACEHOLDER_EXPR:
14202       /* This case involves extracting fields from an object to determine the
14203          position of other fields.  We don't try to encode this here.  The
14204          only user of this is Ada, which encodes the needed information using
14205          the names of types.  */
14206       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14207       return 0;
14208
14209     case CALL_EXPR:
14210       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14211       /* There are no opcodes for these operations.  */
14212       return 0;
14213
14214     case PREINCREMENT_EXPR:
14215     case PREDECREMENT_EXPR:
14216     case POSTINCREMENT_EXPR:
14217     case POSTDECREMENT_EXPR:
14218       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14219       /* There are no opcodes for these operations.  */
14220       return 0;
14221
14222     case ADDR_EXPR:
14223       /* If we already want an address, see if there is INDIRECT_REF inside
14224          e.g. for &this->field.  */
14225       if (want_address)
14226         {
14227           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14228                        (loc, want_address == 2);
14229           if (list_ret)
14230             have_address = 1;
14231           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14232                    && (ret = cst_pool_loc_descr (loc)))
14233             have_address = 1;
14234         }
14235         /* Otherwise, process the argument and look for the address.  */
14236       if (!list_ret && !ret)
14237         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14238       else
14239         {
14240           if (want_address)
14241             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14242           return NULL;
14243         }
14244       break;
14245
14246     case VAR_DECL:
14247       if (DECL_THREAD_LOCAL_P (loc))
14248         {
14249           rtx rtl;
14250           enum dwarf_location_atom first_op;
14251           enum dwarf_location_atom second_op;
14252           bool dtprel = false;
14253
14254           if (targetm.have_tls)
14255             {
14256               /* If this is not defined, we have no way to emit the
14257                  data.  */
14258               if (!targetm.asm_out.output_dwarf_dtprel)
14259                 return 0;
14260
14261                /* The way DW_OP_GNU_push_tls_address is specified, we
14262                   can only look up addresses of objects in the current
14263                   module.  */
14264               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14265                 return 0;
14266               first_op = DW_OP_addr;
14267               dtprel = true;
14268               second_op = DW_OP_GNU_push_tls_address;
14269             }
14270           else
14271             {
14272               if (!targetm.emutls.debug_form_tls_address
14273                   || !(dwarf_version >= 3 || !dwarf_strict))
14274                 return 0;
14275               loc = emutls_decl (loc);
14276               first_op = DW_OP_addr;
14277               second_op = DW_OP_form_tls_address;
14278             }
14279
14280           rtl = rtl_for_decl_location (loc);
14281           if (rtl == NULL_RTX)
14282             return 0;
14283
14284           if (!MEM_P (rtl))
14285             return 0;
14286           rtl = XEXP (rtl, 0);
14287           if (! CONSTANT_P (rtl))
14288             return 0;
14289
14290           ret = new_loc_descr (first_op, 0, 0);
14291           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14292           ret->dw_loc_oprnd1.v.val_addr = rtl;
14293           ret->dtprel = dtprel;
14294
14295           ret1 = new_loc_descr (second_op, 0, 0);
14296           add_loc_descr (&ret, ret1);
14297
14298           have_address = 1;
14299           break;
14300         }
14301       /* FALLTHRU */
14302
14303     case PARM_DECL:
14304       if (DECL_HAS_VALUE_EXPR_P (loc))
14305         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14306                                    want_address);
14307       /* FALLTHRU */
14308
14309     case RESULT_DECL:
14310     case FUNCTION_DECL:
14311       {
14312         rtx rtl;
14313         var_loc_list *loc_list = lookup_decl_loc (loc);
14314
14315         if (loc_list && loc_list->first
14316             && (list_ret = dw_loc_list (loc_list, loc, want_address)))
14317           {
14318             have_address = want_address != 0;
14319             break;
14320           }
14321         rtl = rtl_for_decl_location (loc);
14322         if (rtl == NULL_RTX)
14323           {
14324             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14325             return 0;
14326           }
14327         else if (CONST_INT_P (rtl))
14328           {
14329             HOST_WIDE_INT val = INTVAL (rtl);
14330             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14331               val &= GET_MODE_MASK (DECL_MODE (loc));
14332             ret = int_loc_descriptor (val);
14333           }
14334         else if (GET_CODE (rtl) == CONST_STRING)
14335           {
14336             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14337             return 0;
14338           }
14339         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14340           {
14341             ret = new_loc_descr (DW_OP_addr, 0, 0);
14342             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14343             ret->dw_loc_oprnd1.v.val_addr = rtl;
14344           }
14345         else
14346           {
14347             enum machine_mode mode;
14348
14349             /* Certain constructs can only be represented at top-level.  */
14350             if (want_address == 2)
14351               {
14352                 ret = loc_descriptor (rtl, VOIDmode,
14353                                       VAR_INIT_STATUS_INITIALIZED);
14354                 have_address = 1;
14355               }
14356             else
14357               {
14358                 mode = GET_MODE (rtl);
14359                 if (MEM_P (rtl))
14360                   {
14361                     rtl = XEXP (rtl, 0);
14362                     have_address = 1;
14363                   }
14364                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14365               }
14366             if (!ret)
14367               expansion_failed (loc, rtl,
14368                                 "failed to produce loc descriptor for rtl");
14369           }
14370       }
14371       break;
14372
14373     case INDIRECT_REF:
14374     case ALIGN_INDIRECT_REF:
14375     case MISALIGNED_INDIRECT_REF:
14376       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14377       have_address = 1;
14378       break;
14379
14380     case COMPOUND_EXPR:
14381       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14382
14383     CASE_CONVERT:
14384     case VIEW_CONVERT_EXPR:
14385     case SAVE_EXPR:
14386     case MODIFY_EXPR:
14387       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14388
14389     case COMPONENT_REF:
14390     case BIT_FIELD_REF:
14391     case ARRAY_REF:
14392     case ARRAY_RANGE_REF:
14393     case REALPART_EXPR:
14394     case IMAGPART_EXPR:
14395       {
14396         tree obj, offset;
14397         HOST_WIDE_INT bitsize, bitpos, bytepos;
14398         enum machine_mode mode;
14399         int volatilep;
14400         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14401
14402         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14403                                    &unsignedp, &volatilep, false);
14404
14405         gcc_assert (obj != loc);
14406
14407         list_ret = loc_list_from_tree (obj,
14408                                        want_address == 2
14409                                        && !bitpos && !offset ? 2 : 1);
14410         /* TODO: We can extract value of the small expression via shifting even
14411            for nonzero bitpos.  */
14412         if (list_ret == 0)
14413           return 0;
14414         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14415           {
14416             expansion_failed (loc, NULL_RTX,
14417                               "bitfield access");
14418             return 0;
14419           }
14420
14421         if (offset != NULL_TREE)
14422           {
14423             /* Variable offset.  */
14424             list_ret1 = loc_list_from_tree (offset, 0);
14425             if (list_ret1 == 0)
14426               return 0;
14427             add_loc_list (&list_ret, list_ret1);
14428             if (!list_ret)
14429               return 0;
14430             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14431           }
14432
14433         bytepos = bitpos / BITS_PER_UNIT;
14434         if (bytepos > 0)
14435           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14436         else if (bytepos < 0)
14437           loc_list_plus_const (list_ret, bytepos); 
14438
14439         have_address = 1;
14440         break;
14441       }
14442
14443     case INTEGER_CST:
14444       if ((want_address || !host_integerp (loc, 0))
14445           && (ret = cst_pool_loc_descr (loc)))
14446         have_address = 1;
14447       else if (want_address == 2
14448                && host_integerp (loc, 0)
14449                && (ret = address_of_int_loc_descriptor
14450                            (int_size_in_bytes (TREE_TYPE (loc)),
14451                             tree_low_cst (loc, 0))))
14452         have_address = 1;
14453       else if (host_integerp (loc, 0))
14454         ret = int_loc_descriptor (tree_low_cst (loc, 0));
14455       else
14456         {
14457           expansion_failed (loc, NULL_RTX,
14458                             "Integer operand is not host integer");
14459           return 0;
14460         }
14461       break;
14462
14463     case CONSTRUCTOR:
14464     case REAL_CST:
14465     case STRING_CST:
14466     case COMPLEX_CST:
14467       if ((ret = cst_pool_loc_descr (loc)))
14468         have_address = 1;
14469       else
14470       /* We can construct small constants here using int_loc_descriptor.  */
14471         expansion_failed (loc, NULL_RTX,
14472                           "constructor or constant not in constant pool");
14473       break;
14474
14475     case TRUTH_AND_EXPR:
14476     case TRUTH_ANDIF_EXPR:
14477     case BIT_AND_EXPR:
14478       op = DW_OP_and;
14479       goto do_binop;
14480
14481     case TRUTH_XOR_EXPR:
14482     case BIT_XOR_EXPR:
14483       op = DW_OP_xor;
14484       goto do_binop;
14485
14486     case TRUTH_OR_EXPR:
14487     case TRUTH_ORIF_EXPR:
14488     case BIT_IOR_EXPR:
14489       op = DW_OP_or;
14490       goto do_binop;
14491
14492     case FLOOR_DIV_EXPR:
14493     case CEIL_DIV_EXPR:
14494     case ROUND_DIV_EXPR:
14495     case TRUNC_DIV_EXPR:
14496       op = DW_OP_div;
14497       goto do_binop;
14498
14499     case MINUS_EXPR:
14500       op = DW_OP_minus;
14501       goto do_binop;
14502
14503     case FLOOR_MOD_EXPR:
14504     case CEIL_MOD_EXPR:
14505     case ROUND_MOD_EXPR:
14506     case TRUNC_MOD_EXPR:
14507       op = DW_OP_mod;
14508       goto do_binop;
14509
14510     case MULT_EXPR:
14511       op = DW_OP_mul;
14512       goto do_binop;
14513
14514     case LSHIFT_EXPR:
14515       op = DW_OP_shl;
14516       goto do_binop;
14517
14518     case RSHIFT_EXPR:
14519       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14520       goto do_binop;
14521
14522     case POINTER_PLUS_EXPR:
14523     case PLUS_EXPR:
14524       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
14525           && host_integerp (TREE_OPERAND (loc, 1), 0))
14526         {
14527           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14528           if (list_ret == 0)
14529             return 0;
14530
14531           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14532           break;
14533         }
14534
14535       op = DW_OP_plus;
14536       goto do_binop;
14537
14538     case LE_EXPR:
14539       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14540         return 0;
14541
14542       op = DW_OP_le;
14543       goto do_binop;
14544
14545     case GE_EXPR:
14546       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14547         return 0;
14548
14549       op = DW_OP_ge;
14550       goto do_binop;
14551
14552     case LT_EXPR:
14553       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14554         return 0;
14555
14556       op = DW_OP_lt;
14557       goto do_binop;
14558
14559     case GT_EXPR:
14560       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14561         return 0;
14562
14563       op = DW_OP_gt;
14564       goto do_binop;
14565
14566     case EQ_EXPR:
14567       op = DW_OP_eq;
14568       goto do_binop;
14569
14570     case NE_EXPR:
14571       op = DW_OP_ne;
14572       goto do_binop;
14573
14574     do_binop:
14575       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14576       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14577       if (list_ret == 0 || list_ret1 == 0)
14578         return 0;
14579
14580       add_loc_list (&list_ret, list_ret1);
14581       if (list_ret == 0)
14582         return 0;
14583       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14584       break;
14585
14586     case TRUTH_NOT_EXPR:
14587     case BIT_NOT_EXPR:
14588       op = DW_OP_not;
14589       goto do_unop;
14590
14591     case ABS_EXPR:
14592       op = DW_OP_abs;
14593       goto do_unop;
14594
14595     case NEGATE_EXPR:
14596       op = DW_OP_neg;
14597       goto do_unop;
14598
14599     do_unop:
14600       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14601       if (list_ret == 0)
14602         return 0;
14603
14604       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14605       break;
14606
14607     case MIN_EXPR:
14608     case MAX_EXPR:
14609       {
14610         const enum tree_code code =
14611           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14612
14613         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14614                       build2 (code, integer_type_node,
14615                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14616                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14617       }
14618
14619       /* ... fall through ...  */
14620
14621     case COND_EXPR:
14622       {
14623         dw_loc_descr_ref lhs
14624           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14625         dw_loc_list_ref rhs
14626           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14627         dw_loc_descr_ref bra_node, jump_node, tmp;
14628
14629         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14630         if (list_ret == 0 || lhs == 0 || rhs == 0)
14631           return 0;
14632
14633         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14634         add_loc_descr_to_each (list_ret, bra_node);
14635
14636         add_loc_list (&list_ret, rhs);
14637         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14638         add_loc_descr_to_each (list_ret, jump_node);
14639
14640         add_loc_descr_to_each (list_ret, lhs);
14641         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14642         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14643
14644         /* ??? Need a node to point the skip at.  Use a nop.  */
14645         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14646         add_loc_descr_to_each (list_ret, tmp);
14647         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14648         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14649       }
14650       break;
14651
14652     case FIX_TRUNC_EXPR:
14653       return 0;
14654
14655     default:
14656       /* Leave front-end specific codes as simply unknown.  This comes
14657          up, for instance, with the C STMT_EXPR.  */
14658       if ((unsigned int) TREE_CODE (loc)
14659           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14660         {
14661           expansion_failed (loc, NULL_RTX,
14662                             "language specific tree node");
14663           return 0;
14664         }
14665
14666 #ifdef ENABLE_CHECKING
14667       /* Otherwise this is a generic code; we should just lists all of
14668          these explicitly.  We forgot one.  */
14669       gcc_unreachable ();
14670 #else
14671       /* In a release build, we want to degrade gracefully: better to
14672          generate incomplete debugging information than to crash.  */
14673       return NULL;
14674 #endif
14675     }
14676
14677   if (!ret && !list_ret)
14678     return 0;
14679
14680   if (want_address == 2 && !have_address
14681       && (dwarf_version >= 4 || !dwarf_strict))
14682     {
14683       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14684         {
14685           expansion_failed (loc, NULL_RTX,
14686                             "DWARF address size mismatch");
14687           return 0;
14688         }
14689       if (ret)
14690         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14691       else
14692         add_loc_descr_to_each (list_ret,
14693                                new_loc_descr (DW_OP_stack_value, 0, 0));
14694       have_address = 1;
14695     }
14696   /* Show if we can't fill the request for an address.  */
14697   if (want_address && !have_address)
14698     {
14699       expansion_failed (loc, NULL_RTX,
14700                         "Want address and only have value");
14701       return 0;
14702     }
14703
14704   gcc_assert (!ret || !list_ret);
14705
14706   /* If we've got an address and don't want one, dereference.  */
14707   if (!want_address && have_address)
14708     {
14709       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14710
14711       if (size > DWARF2_ADDR_SIZE || size == -1)
14712         {
14713           expansion_failed (loc, NULL_RTX,
14714                             "DWARF address size mismatch");
14715           return 0;
14716         }
14717       else if (size == DWARF2_ADDR_SIZE)
14718         op = DW_OP_deref;
14719       else
14720         op = DW_OP_deref_size;
14721
14722       if (ret)
14723         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14724       else
14725         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14726     }
14727   if (ret)
14728     list_ret = single_element_loc_list (ret);
14729
14730   return list_ret;
14731 }
14732
14733 /* Same as above but return only single location expression.  */
14734 static dw_loc_descr_ref
14735 loc_descriptor_from_tree (tree loc, int want_address)
14736 {
14737   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14738   if (!ret)
14739     return NULL;
14740   if (ret->dw_loc_next)
14741     {
14742       expansion_failed (loc, NULL_RTX,
14743                         "Location list where only loc descriptor needed");
14744       return NULL;
14745     }
14746   return ret->expr;
14747 }
14748
14749 /* Given a value, round it up to the lowest multiple of `boundary'
14750    which is not less than the value itself.  */
14751
14752 static inline HOST_WIDE_INT
14753 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14754 {
14755   return (((value + boundary - 1) / boundary) * boundary);
14756 }
14757
14758 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14759    pointer to the declared type for the relevant field variable, or return
14760    `integer_type_node' if the given node turns out to be an
14761    ERROR_MARK node.  */
14762
14763 static inline tree
14764 field_type (const_tree decl)
14765 {
14766   tree type;
14767
14768   if (TREE_CODE (decl) == ERROR_MARK)
14769     return integer_type_node;
14770
14771   type = DECL_BIT_FIELD_TYPE (decl);
14772   if (type == NULL_TREE)
14773     type = TREE_TYPE (decl);
14774
14775   return type;
14776 }
14777
14778 /* Given a pointer to a tree node, return the alignment in bits for
14779    it, or else return BITS_PER_WORD if the node actually turns out to
14780    be an ERROR_MARK node.  */
14781
14782 static inline unsigned
14783 simple_type_align_in_bits (const_tree type)
14784 {
14785   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14786 }
14787
14788 static inline unsigned
14789 simple_decl_align_in_bits (const_tree decl)
14790 {
14791   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14792 }
14793
14794 /* Return the result of rounding T up to ALIGN.  */
14795
14796 static inline HOST_WIDE_INT
14797 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
14798 {
14799   /* We must be careful if T is negative because HOST_WIDE_INT can be
14800      either "above" or "below" unsigned int as per the C promotion
14801      rules, depending on the host, thus making the signedness of the
14802      direct multiplication and division unpredictable.  */
14803   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
14804
14805   u += align - 1;
14806   u /= align;
14807   u *= align;
14808
14809   return (HOST_WIDE_INT) u;
14810 }
14811
14812 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14813    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14814    or return 0 if we are unable to determine what that offset is, either
14815    because the argument turns out to be a pointer to an ERROR_MARK node, or
14816    because the offset is actually variable.  (We can't handle the latter case
14817    just yet).  */
14818
14819 static HOST_WIDE_INT
14820 field_byte_offset (const_tree decl)
14821 {
14822   HOST_WIDE_INT object_offset_in_bits;
14823   HOST_WIDE_INT bitpos_int;
14824
14825   if (TREE_CODE (decl) == ERROR_MARK)
14826     return 0;
14827
14828   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14829
14830   /* We cannot yet cope with fields whose positions are variable, so
14831      for now, when we see such things, we simply return 0.  Someday, we may
14832      be able to handle such cases, but it will be damn difficult.  */
14833   if (! host_integerp (bit_position (decl), 0))
14834     return 0;
14835
14836   bitpos_int = int_bit_position (decl);
14837
14838 #ifdef PCC_BITFIELD_TYPE_MATTERS
14839   if (PCC_BITFIELD_TYPE_MATTERS)
14840     {
14841       tree type;
14842       tree field_size_tree;
14843       HOST_WIDE_INT deepest_bitpos;
14844       unsigned HOST_WIDE_INT field_size_in_bits;
14845       unsigned int type_align_in_bits;
14846       unsigned int decl_align_in_bits;
14847       unsigned HOST_WIDE_INT type_size_in_bits;
14848
14849       type = field_type (decl);
14850       type_size_in_bits = simple_type_size_in_bits (type);
14851       type_align_in_bits = simple_type_align_in_bits (type);
14852
14853       field_size_tree = DECL_SIZE (decl);
14854
14855       /* The size could be unspecified if there was an error, or for
14856          a flexible array member.  */
14857       if (!field_size_tree)
14858         field_size_tree = bitsize_zero_node;
14859
14860       /* If the size of the field is not constant, use the type size.  */
14861       if (host_integerp (field_size_tree, 1))
14862         field_size_in_bits = tree_low_cst (field_size_tree, 1);
14863       else
14864         field_size_in_bits = type_size_in_bits;
14865
14866       decl_align_in_bits = simple_decl_align_in_bits (decl);
14867
14868       /* The GCC front-end doesn't make any attempt to keep track of the
14869          starting bit offset (relative to the start of the containing
14870          structure type) of the hypothetical "containing object" for a
14871          bit-field.  Thus, when computing the byte offset value for the
14872          start of the "containing object" of a bit-field, we must deduce
14873          this information on our own. This can be rather tricky to do in
14874          some cases.  For example, handling the following structure type
14875          definition when compiling for an i386/i486 target (which only
14876          aligns long long's to 32-bit boundaries) can be very tricky:
14877
14878          struct S { int field1; long long field2:31; };
14879
14880          Fortunately, there is a simple rule-of-thumb which can be used
14881          in such cases.  When compiling for an i386/i486, GCC will
14882          allocate 8 bytes for the structure shown above.  It decides to
14883          do this based upon one simple rule for bit-field allocation.
14884          GCC allocates each "containing object" for each bit-field at
14885          the first (i.e. lowest addressed) legitimate alignment boundary
14886          (based upon the required minimum alignment for the declared
14887          type of the field) which it can possibly use, subject to the
14888          condition that there is still enough available space remaining
14889          in the containing object (when allocated at the selected point)
14890          to fully accommodate all of the bits of the bit-field itself.
14891
14892          This simple rule makes it obvious why GCC allocates 8 bytes for
14893          each object of the structure type shown above.  When looking
14894          for a place to allocate the "containing object" for `field2',
14895          the compiler simply tries to allocate a 64-bit "containing
14896          object" at each successive 32-bit boundary (starting at zero)
14897          until it finds a place to allocate that 64- bit field such that
14898          at least 31 contiguous (and previously unallocated) bits remain
14899          within that selected 64 bit field.  (As it turns out, for the
14900          example above, the compiler finds it is OK to allocate the
14901          "containing object" 64-bit field at bit-offset zero within the
14902          structure type.)
14903
14904          Here we attempt to work backwards from the limited set of facts
14905          we're given, and we try to deduce from those facts, where GCC
14906          must have believed that the containing object started (within
14907          the structure type). The value we deduce is then used (by the
14908          callers of this routine) to generate DW_AT_location and
14909          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14910          the case of DW_AT_location, regular fields as well).  */
14911
14912       /* Figure out the bit-distance from the start of the structure to
14913          the "deepest" bit of the bit-field.  */
14914       deepest_bitpos = bitpos_int + field_size_in_bits;
14915
14916       /* This is the tricky part.  Use some fancy footwork to deduce
14917          where the lowest addressed bit of the containing object must
14918          be.  */
14919       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14920
14921       /* Round up to type_align by default.  This works best for
14922          bitfields.  */
14923       object_offset_in_bits
14924         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14925
14926       if (object_offset_in_bits > bitpos_int)
14927         {
14928           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14929
14930           /* Round up to decl_align instead.  */
14931           object_offset_in_bits
14932             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14933         }
14934     }
14935   else
14936 #endif
14937     object_offset_in_bits = bitpos_int;
14938
14939   return object_offset_in_bits / BITS_PER_UNIT;
14940 }
14941 \f
14942 /* The following routines define various Dwarf attributes and any data
14943    associated with them.  */
14944
14945 /* Add a location description attribute value to a DIE.
14946
14947    This emits location attributes suitable for whole variables and
14948    whole parameters.  Note that the location attributes for struct fields are
14949    generated by the routine `data_member_location_attribute' below.  */
14950
14951 static inline void
14952 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14953                              dw_loc_list_ref descr)
14954 {
14955   if (descr == 0)
14956     return;
14957   if (single_element_loc_list_p (descr))
14958     add_AT_loc (die, attr_kind, descr->expr);
14959   else
14960     add_AT_loc_list (die, attr_kind, descr);
14961 }
14962
14963 /* Attach the specialized form of location attribute used for data members of
14964    struct and union types.  In the special case of a FIELD_DECL node which
14965    represents a bit-field, the "offset" part of this special location
14966    descriptor must indicate the distance in bytes from the lowest-addressed
14967    byte of the containing struct or union type to the lowest-addressed byte of
14968    the "containing object" for the bit-field.  (See the `field_byte_offset'
14969    function above).
14970
14971    For any given bit-field, the "containing object" is a hypothetical object
14972    (of some integral or enum type) within which the given bit-field lives.  The
14973    type of this hypothetical "containing object" is always the same as the
14974    declared type of the individual bit-field itself (for GCC anyway... the
14975    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14976    bytes) of the hypothetical "containing object" which will be given in the
14977    DW_AT_byte_size attribute for this bit-field.  (See the
14978    `byte_size_attribute' function below.)  It is also used when calculating the
14979    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14980    function below.)  */
14981
14982 static void
14983 add_data_member_location_attribute (dw_die_ref die, tree decl)
14984 {
14985   HOST_WIDE_INT offset;
14986   dw_loc_descr_ref loc_descr = 0;
14987
14988   if (TREE_CODE (decl) == TREE_BINFO)
14989     {
14990       /* We're working on the TAG_inheritance for a base class.  */
14991       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14992         {
14993           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14994              aren't at a fixed offset from all (sub)objects of the same
14995              type.  We need to extract the appropriate offset from our
14996              vtable.  The following dwarf expression means
14997
14998                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14999
15000              This is specific to the V3 ABI, of course.  */
15001
15002           dw_loc_descr_ref tmp;
15003
15004           /* Make a copy of the object address.  */
15005           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15006           add_loc_descr (&loc_descr, tmp);
15007
15008           /* Extract the vtable address.  */
15009           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15010           add_loc_descr (&loc_descr, tmp);
15011
15012           /* Calculate the address of the offset.  */
15013           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15014           gcc_assert (offset < 0);
15015
15016           tmp = int_loc_descriptor (-offset);
15017           add_loc_descr (&loc_descr, tmp);
15018           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15019           add_loc_descr (&loc_descr, tmp);
15020
15021           /* Extract the offset.  */
15022           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15023           add_loc_descr (&loc_descr, tmp);
15024
15025           /* Add it to the object address.  */
15026           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15027           add_loc_descr (&loc_descr, tmp);
15028         }
15029       else
15030         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15031     }
15032   else
15033     offset = field_byte_offset (decl);
15034
15035   if (! loc_descr)
15036     {
15037       if (dwarf_version > 2)
15038         {
15039           /* Don't need to output a location expression, just the constant. */
15040           add_AT_int (die, DW_AT_data_member_location, offset);
15041           return;
15042         }
15043       else
15044         {
15045           enum dwarf_location_atom op;
15046           
15047           /* The DWARF2 standard says that we should assume that the structure
15048              address is already on the stack, so we can specify a structure
15049              field address by using DW_OP_plus_uconst.  */
15050           
15051 #ifdef MIPS_DEBUGGING_INFO
15052           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15053              operator correctly.  It works only if we leave the offset on the
15054              stack.  */
15055           op = DW_OP_constu;
15056 #else
15057           op = DW_OP_plus_uconst;
15058 #endif
15059           
15060           loc_descr = new_loc_descr (op, offset, 0);
15061         }
15062     }
15063
15064   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15065 }
15066
15067 /* Writes integer values to dw_vec_const array.  */
15068
15069 static void
15070 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15071 {
15072   while (size != 0)
15073     {
15074       *dest++ = val & 0xff;
15075       val >>= 8;
15076       --size;
15077     }
15078 }
15079
15080 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15081
15082 static HOST_WIDE_INT
15083 extract_int (const unsigned char *src, unsigned int size)
15084 {
15085   HOST_WIDE_INT val = 0;
15086
15087   src += size;
15088   while (size != 0)
15089     {
15090       val <<= 8;
15091       val |= *--src & 0xff;
15092       --size;
15093     }
15094   return val;
15095 }
15096
15097 /* Writes floating point values to dw_vec_const array.  */
15098
15099 static void
15100 insert_float (const_rtx rtl, unsigned char *array)
15101 {
15102   REAL_VALUE_TYPE rv;
15103   long val[4];
15104   int i;
15105
15106   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15107   real_to_target (val, &rv, GET_MODE (rtl));
15108
15109   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
15110   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15111     {
15112       insert_int (val[i], 4, array);
15113       array += 4;
15114     }
15115 }
15116
15117 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15118    does not have a "location" either in memory or in a register.  These
15119    things can arise in GNU C when a constant is passed as an actual parameter
15120    to an inlined function.  They can also arise in C++ where declared
15121    constants do not necessarily get memory "homes".  */
15122
15123 static bool
15124 add_const_value_attribute (dw_die_ref die, rtx rtl)
15125 {
15126   switch (GET_CODE (rtl))
15127     {
15128     case CONST_INT:
15129       {
15130         HOST_WIDE_INT val = INTVAL (rtl);
15131
15132         if (val < 0)
15133           add_AT_int (die, DW_AT_const_value, val);
15134         else
15135           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15136       }
15137       return true;
15138
15139     case CONST_DOUBLE:
15140       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15141          floating-point constant.  A CONST_DOUBLE is used whenever the
15142          constant requires more than one word in order to be adequately
15143          represented.  */
15144       {
15145         enum machine_mode mode = GET_MODE (rtl);
15146
15147         if (SCALAR_FLOAT_MODE_P (mode))
15148           {
15149             unsigned int length = GET_MODE_SIZE (mode);
15150             unsigned char *array = GGC_NEWVEC (unsigned char, length);
15151
15152             insert_float (rtl, array);
15153             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15154           }
15155         else
15156           add_AT_double (die, DW_AT_const_value,
15157                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15158       }
15159       return true;
15160
15161     case CONST_VECTOR:
15162       {
15163         enum machine_mode mode = GET_MODE (rtl);
15164         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15165         unsigned int length = CONST_VECTOR_NUNITS (rtl);
15166         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
15167         unsigned int i;
15168         unsigned char *p;
15169
15170         switch (GET_MODE_CLASS (mode))
15171           {
15172           case MODE_VECTOR_INT:
15173             for (i = 0, p = array; i < length; i++, p += elt_size)
15174               {
15175                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15176                 HOST_WIDE_INT lo, hi;
15177
15178                 switch (GET_CODE (elt))
15179                   {
15180                   case CONST_INT:
15181                     lo = INTVAL (elt);
15182                     hi = -(lo < 0);
15183                     break;
15184
15185                   case CONST_DOUBLE:
15186                     lo = CONST_DOUBLE_LOW (elt);
15187                     hi = CONST_DOUBLE_HIGH (elt);
15188                     break;
15189
15190                   default:
15191                     gcc_unreachable ();
15192                   }
15193
15194                 if (elt_size <= sizeof (HOST_WIDE_INT))
15195                   insert_int (lo, elt_size, p);
15196                 else
15197                   {
15198                     unsigned char *p0 = p;
15199                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
15200
15201                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15202                     if (WORDS_BIG_ENDIAN)
15203                       {
15204                         p0 = p1;
15205                         p1 = p;
15206                       }
15207                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
15208                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
15209                   }
15210               }
15211             break;
15212
15213           case MODE_VECTOR_FLOAT:
15214             for (i = 0, p = array; i < length; i++, p += elt_size)
15215               {
15216                 rtx elt = CONST_VECTOR_ELT (rtl, i);
15217                 insert_float (elt, p);
15218               }
15219             break;
15220
15221           default:
15222             gcc_unreachable ();
15223           }
15224
15225         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15226       }
15227       return true;
15228
15229     case CONST_STRING:
15230       resolve_one_addr (&rtl, NULL);
15231       add_AT_addr (die, DW_AT_const_value, rtl);
15232       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15233       return true;
15234
15235     case CONST:
15236       if (CONSTANT_P (XEXP (rtl, 0)))
15237         return add_const_value_attribute (die, XEXP (rtl, 0));
15238       /* FALLTHROUGH */
15239     case SYMBOL_REF:
15240       if (!const_ok_for_output (rtl))
15241         return false;
15242     case LABEL_REF:
15243       add_AT_addr (die, DW_AT_const_value, rtl);
15244       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15245       return true;
15246
15247     case PLUS:
15248       /* In cases where an inlined instance of an inline function is passed
15249          the address of an `auto' variable (which is local to the caller) we
15250          can get a situation where the DECL_RTL of the artificial local
15251          variable (for the inlining) which acts as a stand-in for the
15252          corresponding formal parameter (of the inline function) will look
15253          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15254          exactly a compile-time constant expression, but it isn't the address
15255          of the (artificial) local variable either.  Rather, it represents the
15256          *value* which the artificial local variable always has during its
15257          lifetime.  We currently have no way to represent such quasi-constant
15258          values in Dwarf, so for now we just punt and generate nothing.  */
15259       return false;
15260
15261     case HIGH:
15262     case CONST_FIXED:
15263       return false;
15264
15265     case MEM:
15266       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15267           && MEM_READONLY_P (rtl)
15268           && GET_MODE (rtl) == BLKmode)
15269         {
15270           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15271           return true;
15272         }
15273       return false;
15274
15275     default:
15276       /* No other kinds of rtx should be possible here.  */
15277       gcc_unreachable ();
15278     }
15279   return false;
15280 }
15281
15282 /* Determine whether the evaluation of EXPR references any variables
15283    or functions which aren't otherwise used (and therefore may not be
15284    output).  */
15285 static tree
15286 reference_to_unused (tree * tp, int * walk_subtrees,
15287                      void * data ATTRIBUTE_UNUSED)
15288 {
15289   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15290     *walk_subtrees = 0;
15291
15292   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15293       && ! TREE_ASM_WRITTEN (*tp))
15294     return *tp;
15295   /* ???  The C++ FE emits debug information for using decls, so
15296      putting gcc_unreachable here falls over.  See PR31899.  For now
15297      be conservative.  */
15298   else if (!cgraph_global_info_ready
15299            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15300     return *tp;
15301   else if (TREE_CODE (*tp) == VAR_DECL)
15302     {
15303       struct varpool_node *node = varpool_node (*tp);
15304       if (!node->needed)
15305         return *tp;
15306     }
15307   else if (TREE_CODE (*tp) == FUNCTION_DECL
15308            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15309     {
15310       /* The call graph machinery must have finished analyzing,
15311          optimizing and gimplifying the CU by now.
15312          So if *TP has no call graph node associated
15313          to it, it means *TP will not be emitted.  */
15314       if (!cgraph_get_node (*tp))
15315         return *tp;
15316     }
15317   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15318     return *tp;
15319
15320   return NULL_TREE;
15321 }
15322
15323 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15324    for use in a later add_const_value_attribute call.  */
15325
15326 static rtx
15327 rtl_for_decl_init (tree init, tree type)
15328 {
15329   rtx rtl = NULL_RTX;
15330
15331   /* If a variable is initialized with a string constant without embedded
15332      zeros, build CONST_STRING.  */
15333   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15334     {
15335       tree enttype = TREE_TYPE (type);
15336       tree domain = TYPE_DOMAIN (type);
15337       enum machine_mode mode = TYPE_MODE (enttype);
15338
15339       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15340           && domain
15341           && integer_zerop (TYPE_MIN_VALUE (domain))
15342           && compare_tree_int (TYPE_MAX_VALUE (domain),
15343                                TREE_STRING_LENGTH (init) - 1) == 0
15344           && ((size_t) TREE_STRING_LENGTH (init)
15345               == strlen (TREE_STRING_POINTER (init)) + 1))
15346         {
15347           rtl = gen_rtx_CONST_STRING (VOIDmode,
15348                                       ggc_strdup (TREE_STRING_POINTER (init)));
15349           rtl = gen_rtx_MEM (BLKmode, rtl);
15350           MEM_READONLY_P (rtl) = 1;
15351         }
15352     }
15353   /* Other aggregates, and complex values, could be represented using
15354      CONCAT: FIXME!  */
15355   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
15356     ;
15357   /* Vectors only work if their mode is supported by the target.
15358      FIXME: generic vectors ought to work too.  */
15359   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
15360     ;
15361   /* If the initializer is something that we know will expand into an
15362      immediate RTL constant, expand it now.  We must be careful not to
15363      reference variables which won't be output.  */
15364   else if (initializer_constant_valid_p (init, type)
15365            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15366     {
15367       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15368          possible.  */
15369       if (TREE_CODE (type) == VECTOR_TYPE)
15370         switch (TREE_CODE (init))
15371           {
15372           case VECTOR_CST:
15373             break;
15374           case CONSTRUCTOR:
15375             if (TREE_CONSTANT (init))
15376               {
15377                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
15378                 bool constant_p = true;
15379                 tree value;
15380                 unsigned HOST_WIDE_INT ix;
15381
15382                 /* Even when ctor is constant, it might contain non-*_CST
15383                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15384                    belong into VECTOR_CST nodes.  */
15385                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15386                   if (!CONSTANT_CLASS_P (value))
15387                     {
15388                       constant_p = false;
15389                       break;
15390                     }
15391
15392                 if (constant_p)
15393                   {
15394                     init = build_vector_from_ctor (type, elts);
15395                     break;
15396                   }
15397               }
15398             /* FALLTHRU */
15399
15400           default:
15401             return NULL;
15402           }
15403
15404       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15405
15406       /* If expand_expr returns a MEM, it wasn't immediate.  */
15407       gcc_assert (!rtl || !MEM_P (rtl));
15408     }
15409
15410   return rtl;
15411 }
15412
15413 /* Generate RTL for the variable DECL to represent its location.  */
15414
15415 static rtx
15416 rtl_for_decl_location (tree decl)
15417 {
15418   rtx rtl;
15419
15420   /* Here we have to decide where we are going to say the parameter "lives"
15421      (as far as the debugger is concerned).  We only have a couple of
15422      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15423
15424      DECL_RTL normally indicates where the parameter lives during most of the
15425      activation of the function.  If optimization is enabled however, this
15426      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15427      that the parameter doesn't really live anywhere (as far as the code
15428      generation parts of GCC are concerned) during most of the function's
15429      activation.  That will happen (for example) if the parameter is never
15430      referenced within the function.
15431
15432      We could just generate a location descriptor here for all non-NULL
15433      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15434      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15435      where DECL_RTL is NULL or is a pseudo-reg.
15436
15437      Note however that we can only get away with using DECL_INCOMING_RTL as
15438      a backup substitute for DECL_RTL in certain limited cases.  In cases
15439      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15440      we can be sure that the parameter was passed using the same type as it is
15441      declared to have within the function, and that its DECL_INCOMING_RTL
15442      points us to a place where a value of that type is passed.
15443
15444      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15445      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15446      because in these cases DECL_INCOMING_RTL points us to a value of some
15447      type which is *different* from the type of the parameter itself.  Thus,
15448      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15449      such cases, the debugger would end up (for example) trying to fetch a
15450      `float' from a place which actually contains the first part of a
15451      `double'.  That would lead to really incorrect and confusing
15452      output at debug-time.
15453
15454      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15455      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15456      are a couple of exceptions however.  On little-endian machines we can
15457      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15458      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15459      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15460      when (on a little-endian machine) a non-prototyped function has a
15461      parameter declared to be of type `short' or `char'.  In such cases,
15462      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15463      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15464      passed `int' value.  If the debugger then uses that address to fetch
15465      a `short' or a `char' (on a little-endian machine) the result will be
15466      the correct data, so we allow for such exceptional cases below.
15467
15468      Note that our goal here is to describe the place where the given formal
15469      parameter lives during most of the function's activation (i.e. between the
15470      end of the prologue and the start of the epilogue).  We'll do that as best
15471      as we can. Note however that if the given formal parameter is modified
15472      sometime during the execution of the function, then a stack backtrace (at
15473      debug-time) will show the function as having been called with the *new*
15474      value rather than the value which was originally passed in.  This happens
15475      rarely enough that it is not a major problem, but it *is* a problem, and
15476      I'd like to fix it.
15477
15478      A future version of dwarf2out.c may generate two additional attributes for
15479      any given DW_TAG_formal_parameter DIE which will describe the "passed
15480      type" and the "passed location" for the given formal parameter in addition
15481      to the attributes we now generate to indicate the "declared type" and the
15482      "active location" for each parameter.  This additional set of attributes
15483      could be used by debuggers for stack backtraces. Separately, note that
15484      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15485      This happens (for example) for inlined-instances of inline function formal
15486      parameters which are never referenced.  This really shouldn't be
15487      happening.  All PARM_DECL nodes should get valid non-NULL
15488      DECL_INCOMING_RTL values.  FIXME.  */
15489
15490   /* Use DECL_RTL as the "location" unless we find something better.  */
15491   rtl = DECL_RTL_IF_SET (decl);
15492
15493   /* When generating abstract instances, ignore everything except
15494      constants, symbols living in memory, and symbols living in
15495      fixed registers.  */
15496   if (! reload_completed)
15497     {
15498       if (rtl
15499           && (CONSTANT_P (rtl)
15500               || (MEM_P (rtl)
15501                   && CONSTANT_P (XEXP (rtl, 0)))
15502               || (REG_P (rtl)
15503                   && TREE_CODE (decl) == VAR_DECL
15504                   && TREE_STATIC (decl))))
15505         {
15506           rtl = targetm.delegitimize_address (rtl);
15507           return rtl;
15508         }
15509       rtl = NULL_RTX;
15510     }
15511   else if (TREE_CODE (decl) == PARM_DECL)
15512     {
15513       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
15514         {
15515           tree declared_type = TREE_TYPE (decl);
15516           tree passed_type = DECL_ARG_TYPE (decl);
15517           enum machine_mode dmode = TYPE_MODE (declared_type);
15518           enum machine_mode pmode = TYPE_MODE (passed_type);
15519
15520           /* This decl represents a formal parameter which was optimized out.
15521              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15522              all cases where (rtl == NULL_RTX) just below.  */
15523           if (dmode == pmode)
15524             rtl = DECL_INCOMING_RTL (decl);
15525           else if (SCALAR_INT_MODE_P (dmode)
15526                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15527                    && DECL_INCOMING_RTL (decl))
15528             {
15529               rtx inc = DECL_INCOMING_RTL (decl);
15530               if (REG_P (inc))
15531                 rtl = inc;
15532               else if (MEM_P (inc))
15533                 {
15534                   if (BYTES_BIG_ENDIAN)
15535                     rtl = adjust_address_nv (inc, dmode,
15536                                              GET_MODE_SIZE (pmode)
15537                                              - GET_MODE_SIZE (dmode));
15538                   else
15539                     rtl = inc;
15540                 }
15541             }
15542         }
15543
15544       /* If the parm was passed in registers, but lives on the stack, then
15545          make a big endian correction if the mode of the type of the
15546          parameter is not the same as the mode of the rtl.  */
15547       /* ??? This is the same series of checks that are made in dbxout.c before
15548          we reach the big endian correction code there.  It isn't clear if all
15549          of these checks are necessary here, but keeping them all is the safe
15550          thing to do.  */
15551       else if (MEM_P (rtl)
15552                && XEXP (rtl, 0) != const0_rtx
15553                && ! CONSTANT_P (XEXP (rtl, 0))
15554                /* Not passed in memory.  */
15555                && !MEM_P (DECL_INCOMING_RTL (decl))
15556                /* Not passed by invisible reference.  */
15557                && (!REG_P (XEXP (rtl, 0))
15558                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15559                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15560 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
15561                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15562 #endif
15563                      )
15564                /* Big endian correction check.  */
15565                && BYTES_BIG_ENDIAN
15566                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15567                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15568                    < UNITS_PER_WORD))
15569         {
15570           int offset = (UNITS_PER_WORD
15571                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15572
15573           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15574                              plus_constant (XEXP (rtl, 0), offset));
15575         }
15576     }
15577   else if (TREE_CODE (decl) == VAR_DECL
15578            && rtl
15579            && MEM_P (rtl)
15580            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15581            && BYTES_BIG_ENDIAN)
15582     {
15583       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15584       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15585
15586       /* If a variable is declared "register" yet is smaller than
15587          a register, then if we store the variable to memory, it
15588          looks like we're storing a register-sized value, when in
15589          fact we are not.  We need to adjust the offset of the
15590          storage location to reflect the actual value's bytes,
15591          else gdb will not be able to display it.  */
15592       if (rsize > dsize)
15593         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15594                            plus_constant (XEXP (rtl, 0), rsize-dsize));
15595     }
15596
15597   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15598      and will have been substituted directly into all expressions that use it.
15599      C does not have such a concept, but C++ and other languages do.  */
15600   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15601     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15602
15603   if (rtl)
15604     rtl = targetm.delegitimize_address (rtl);
15605
15606   /* If we don't look past the constant pool, we risk emitting a
15607      reference to a constant pool entry that isn't referenced from
15608      code, and thus is not emitted.  */
15609   if (rtl)
15610     rtl = avoid_constant_pool_reference (rtl);
15611
15612   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15613      in the current CU, resolve_addr will remove the expression referencing
15614      it.  */
15615   if (rtl == NULL_RTX
15616       && TREE_CODE (decl) == VAR_DECL
15617       && !DECL_EXTERNAL (decl)
15618       && TREE_STATIC (decl)
15619       && DECL_NAME (decl)
15620       && !DECL_HARD_REGISTER (decl)
15621       && DECL_MODE (decl) != VOIDmode)
15622     {
15623       rtl = DECL_RTL (decl);
15624       /* Reset DECL_RTL back, as various parts of the compiler expects
15625          DECL_RTL set meaning it is actually going to be output.  */
15626       SET_DECL_RTL (decl, NULL);
15627       if (!MEM_P (rtl)
15628           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15629           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15630         rtl = NULL_RTX;
15631     }
15632
15633   return rtl;
15634 }
15635
15636 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15637    returned.  If so, the decl for the COMMON block is returned, and the
15638    value is the offset into the common block for the symbol.  */
15639
15640 static tree
15641 fortran_common (tree decl, HOST_WIDE_INT *value)
15642 {
15643   tree val_expr, cvar;
15644   enum machine_mode mode;
15645   HOST_WIDE_INT bitsize, bitpos;
15646   tree offset;
15647   int volatilep = 0, unsignedp = 0;
15648
15649   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15650      it does not have a value (the offset into the common area), or if it
15651      is thread local (as opposed to global) then it isn't common, and shouldn't
15652      be handled as such.  */
15653   if (TREE_CODE (decl) != VAR_DECL
15654       || !TREE_STATIC (decl)
15655       || !DECL_HAS_VALUE_EXPR_P (decl)
15656       || !is_fortran ())
15657     return NULL_TREE;
15658
15659   val_expr = DECL_VALUE_EXPR (decl);
15660   if (TREE_CODE (val_expr) != COMPONENT_REF)
15661     return NULL_TREE;
15662
15663   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15664                               &mode, &unsignedp, &volatilep, true);
15665
15666   if (cvar == NULL_TREE
15667       || TREE_CODE (cvar) != VAR_DECL
15668       || DECL_ARTIFICIAL (cvar)
15669       || !TREE_PUBLIC (cvar))
15670     return NULL_TREE;
15671
15672   *value = 0;
15673   if (offset != NULL)
15674     {
15675       if (!host_integerp (offset, 0))
15676         return NULL_TREE;
15677       *value = tree_low_cst (offset, 0);
15678     }
15679   if (bitpos != 0)
15680     *value += bitpos / BITS_PER_UNIT;
15681
15682   return cvar;
15683 }
15684
15685 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15686    data attribute for a variable or a parameter.  We generate the
15687    DW_AT_const_value attribute only in those cases where the given variable
15688    or parameter does not have a true "location" either in memory or in a
15689    register.  This can happen (for example) when a constant is passed as an
15690    actual argument in a call to an inline function.  (It's possible that
15691    these things can crop up in other ways also.)  Note that one type of
15692    constant value which can be passed into an inlined function is a constant
15693    pointer.  This can happen for example if an actual argument in an inlined
15694    function call evaluates to a compile-time constant address.  */
15695
15696 static bool
15697 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
15698                                        enum dwarf_attribute attr)
15699 {
15700   rtx rtl;
15701   dw_loc_list_ref list;
15702   var_loc_list *loc_list;
15703
15704   if (TREE_CODE (decl) == ERROR_MARK)
15705     return false;
15706
15707   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15708               || TREE_CODE (decl) == RESULT_DECL);
15709
15710   /* Try to get some constant RTL for this decl, and use that as the value of
15711      the location.  */
15712
15713   rtl = rtl_for_decl_location (decl);
15714   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15715       && add_const_value_attribute (die, rtl))
15716     return true;
15717
15718   /* See if we have single element location list that is equivalent to
15719      a constant value.  That way we are better to use add_const_value_attribute
15720      rather than expanding constant value equivalent.  */
15721   loc_list = lookup_decl_loc (decl);
15722   if (loc_list && loc_list->first && loc_list->first == loc_list->last)
15723     {
15724       enum var_init_status status;
15725       struct var_loc_node *node;
15726
15727       node = loc_list->first;
15728       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
15729       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
15730       if (GET_CODE (rtl) == VAR_LOCATION
15731           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
15732         rtl = XEXP (XEXP (rtl, 1), 0);
15733       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15734           && add_const_value_attribute (die, rtl))
15735          return true;
15736     }
15737   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15738   if (list)
15739     {
15740       add_AT_location_description (die, attr, list);
15741       return true;
15742     }
15743   /* None of that worked, so it must not really have a location;
15744      try adding a constant value attribute from the DECL_INITIAL.  */
15745   return tree_add_const_value_attribute_for_decl (die, decl);
15746 }
15747
15748 /* Add VARIABLE and DIE into deferred locations list.  */
15749
15750 static void
15751 defer_location (tree variable, dw_die_ref die)
15752 {
15753   deferred_locations entry;
15754   entry.variable = variable;
15755   entry.die = die;
15756   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15757 }
15758
15759 /* Helper function for tree_add_const_value_attribute.  Natively encode
15760    initializer INIT into an array.  Return true if successful.  */
15761
15762 static bool
15763 native_encode_initializer (tree init, unsigned char *array, int size)
15764 {
15765   tree type;
15766
15767   if (init == NULL_TREE)
15768     return false;
15769
15770   STRIP_NOPS (init);
15771   switch (TREE_CODE (init))
15772     {
15773     case STRING_CST:
15774       type = TREE_TYPE (init);
15775       if (TREE_CODE (type) == ARRAY_TYPE)
15776         {
15777           tree enttype = TREE_TYPE (type);
15778           enum machine_mode mode = TYPE_MODE (enttype);
15779
15780           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15781             return false;
15782           if (int_size_in_bytes (type) != size)
15783             return false;
15784           if (size > TREE_STRING_LENGTH (init))
15785             {
15786               memcpy (array, TREE_STRING_POINTER (init),
15787                       TREE_STRING_LENGTH (init));
15788               memset (array + TREE_STRING_LENGTH (init),
15789                       '\0', size - TREE_STRING_LENGTH (init));
15790             }
15791           else
15792             memcpy (array, TREE_STRING_POINTER (init), size);
15793           return true;
15794         }
15795       return false;
15796     case CONSTRUCTOR:
15797       type = TREE_TYPE (init);
15798       if (int_size_in_bytes (type) != size)
15799         return false;
15800       if (TREE_CODE (type) == ARRAY_TYPE)
15801         {
15802           HOST_WIDE_INT min_index;
15803           unsigned HOST_WIDE_INT cnt;
15804           int curpos = 0, fieldsize;
15805           constructor_elt *ce;
15806
15807           if (TYPE_DOMAIN (type) == NULL_TREE
15808               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15809             return false;
15810
15811           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15812           if (fieldsize <= 0)
15813             return false;
15814
15815           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15816           memset (array, '\0', size);
15817           for (cnt = 0;
15818                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15819                cnt++)
15820             {
15821               tree val = ce->value;
15822               tree index = ce->index;
15823               int pos = curpos;
15824               if (index && TREE_CODE (index) == RANGE_EXPR)
15825                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15826                       * fieldsize;
15827               else if (index)
15828                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15829
15830               if (val)
15831                 {
15832                   STRIP_NOPS (val);
15833                   if (!native_encode_initializer (val, array + pos, fieldsize))
15834                     return false;
15835                 }
15836               curpos = pos + fieldsize;
15837               if (index && TREE_CODE (index) == RANGE_EXPR)
15838                 {
15839                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15840                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15841                   while (count > 0)
15842                     {
15843                       if (val)
15844                         memcpy (array + curpos, array + pos, fieldsize);
15845                       curpos += fieldsize;
15846                     }
15847                 }
15848               gcc_assert (curpos <= size);
15849             }
15850           return true;
15851         }
15852       else if (TREE_CODE (type) == RECORD_TYPE
15853                || TREE_CODE (type) == UNION_TYPE)
15854         {
15855           tree field = NULL_TREE;
15856           unsigned HOST_WIDE_INT cnt;
15857           constructor_elt *ce;
15858
15859           if (int_size_in_bytes (type) != size)
15860             return false;
15861
15862           if (TREE_CODE (type) == RECORD_TYPE)
15863             field = TYPE_FIELDS (type);
15864
15865           for (cnt = 0;
15866                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15867                cnt++, field = field ? TREE_CHAIN (field) : 0)
15868             {
15869               tree val = ce->value;
15870               int pos, fieldsize;
15871
15872               if (ce->index != 0)
15873                 field = ce->index;
15874
15875               if (val)
15876                 STRIP_NOPS (val);
15877
15878               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15879                 return false;
15880
15881               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15882                   && TYPE_DOMAIN (TREE_TYPE (field))
15883                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15884                 return false;
15885               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15886                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15887                 return false;
15888               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15889               pos = int_byte_position (field);
15890               gcc_assert (pos + fieldsize <= size);
15891               if (val
15892                   && !native_encode_initializer (val, array + pos, fieldsize))
15893                 return false;
15894             }
15895           return true;
15896         }
15897       return false;
15898     case VIEW_CONVERT_EXPR:
15899     case NON_LVALUE_EXPR:
15900       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15901     default:
15902       return native_encode_expr (init, array, size) == size;
15903     }
15904 }
15905
15906 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15907    attribute is the const value T.  */
15908
15909 static bool
15910 tree_add_const_value_attribute (dw_die_ref die, tree t)
15911 {
15912   tree init;
15913   tree type = TREE_TYPE (t);
15914   rtx rtl;
15915
15916   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15917     return false;
15918
15919   init = t;
15920   gcc_assert (!DECL_P (init));
15921
15922   rtl = rtl_for_decl_init (init, type);
15923   if (rtl)
15924     return add_const_value_attribute (die, rtl);
15925   /* If the host and target are sane, try harder.  */
15926   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15927            && initializer_constant_valid_p (init, type))
15928     {
15929       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15930       if (size > 0 && (int) size == size)
15931         {
15932           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
15933
15934           if (native_encode_initializer (init, array, size))
15935             {
15936               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15937               return true;
15938             }
15939         }
15940     }
15941   return false;
15942 }
15943
15944 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15945    attribute is the const value of T, where T is an integral constant
15946    variable with static storage duration
15947    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15948
15949 static bool
15950 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15951 {
15952
15953   if (!decl
15954       || (TREE_CODE (decl) != VAR_DECL
15955           && TREE_CODE (decl) != CONST_DECL))
15956     return false;
15957
15958     if (TREE_READONLY (decl)
15959         && ! TREE_THIS_VOLATILE (decl)
15960         && DECL_INITIAL (decl))
15961       /* OK */;
15962     else
15963       return false;
15964
15965   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15966   if (get_AT (var_die, DW_AT_const_value))
15967     return false;
15968
15969   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15970 }
15971
15972 /* Convert the CFI instructions for the current function into a
15973    location list.  This is used for DW_AT_frame_base when we targeting
15974    a dwarf2 consumer that does not support the dwarf3
15975    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15976    expressions.  */
15977
15978 static dw_loc_list_ref
15979 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15980 {
15981   dw_fde_ref fde;
15982   dw_loc_list_ref list, *list_tail;
15983   dw_cfi_ref cfi;
15984   dw_cfa_location last_cfa, next_cfa;
15985   const char *start_label, *last_label, *section;
15986   dw_cfa_location remember;
15987
15988   fde = current_fde ();
15989   gcc_assert (fde != NULL);
15990
15991   section = secname_for_decl (current_function_decl);
15992   list_tail = &list;
15993   list = NULL;
15994
15995   memset (&next_cfa, 0, sizeof (next_cfa));
15996   next_cfa.reg = INVALID_REGNUM;
15997   remember = next_cfa;
15998
15999   start_label = fde->dw_fde_begin;
16000
16001   /* ??? Bald assumption that the CIE opcode list does not contain
16002      advance opcodes.  */
16003   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16004     lookup_cfa_1 (cfi, &next_cfa, &remember);
16005
16006   last_cfa = next_cfa;
16007   last_label = start_label;
16008
16009   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16010     switch (cfi->dw_cfi_opc)
16011       {
16012       case DW_CFA_set_loc:
16013       case DW_CFA_advance_loc1:
16014       case DW_CFA_advance_loc2:
16015       case DW_CFA_advance_loc4:
16016         if (!cfa_equal_p (&last_cfa, &next_cfa))
16017           {
16018             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16019                                        start_label, last_label, section,
16020                                        list == NULL);
16021
16022             list_tail = &(*list_tail)->dw_loc_next;
16023             last_cfa = next_cfa;
16024             start_label = last_label;
16025           }
16026         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16027         break;
16028
16029       case DW_CFA_advance_loc:
16030         /* The encoding is complex enough that we should never emit this.  */
16031         gcc_unreachable ();
16032
16033       default:
16034         lookup_cfa_1 (cfi, &next_cfa, &remember);
16035         break;
16036       }
16037
16038   if (!cfa_equal_p (&last_cfa, &next_cfa))
16039     {
16040       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16041                                  start_label, last_label, section,
16042                                  list == NULL);
16043       list_tail = &(*list_tail)->dw_loc_next;
16044       start_label = last_label;
16045     }
16046   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16047                              start_label, fde->dw_fde_end, section,
16048                              list == NULL);
16049
16050   return list;
16051 }
16052
16053 /* Compute a displacement from the "steady-state frame pointer" to the
16054    frame base (often the same as the CFA), and store it in
16055    frame_pointer_fb_offset.  OFFSET is added to the displacement
16056    before the latter is negated.  */
16057
16058 static void
16059 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16060 {
16061   rtx reg, elim;
16062
16063 #ifdef FRAME_POINTER_CFA_OFFSET
16064   reg = frame_pointer_rtx;
16065   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16066 #else
16067   reg = arg_pointer_rtx;
16068   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16069 #endif
16070
16071   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16072   if (GET_CODE (elim) == PLUS)
16073     {
16074       offset += INTVAL (XEXP (elim, 1));
16075       elim = XEXP (elim, 0);
16076     }
16077
16078   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16079                && (elim == hard_frame_pointer_rtx
16080                    || elim == stack_pointer_rtx))
16081               || elim == (frame_pointer_needed
16082                           ? hard_frame_pointer_rtx
16083                           : stack_pointer_rtx));
16084
16085   frame_pointer_fb_offset = -offset;
16086 }
16087
16088 /* Generate a DW_AT_name attribute given some string value to be included as
16089    the value of the attribute.  */
16090
16091 static void
16092 add_name_attribute (dw_die_ref die, const char *name_string)
16093 {
16094   if (name_string != NULL && *name_string != 0)
16095     {
16096       if (demangle_name_func)
16097         name_string = (*demangle_name_func) (name_string);
16098
16099       add_AT_string (die, DW_AT_name, name_string);
16100     }
16101 }
16102
16103 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16104
16105 static void
16106 add_comp_dir_attribute (dw_die_ref die)
16107 {
16108   const char *wd = get_src_pwd ();
16109   char *wd1;
16110
16111   if (wd == NULL)
16112     return;
16113
16114   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16115     {
16116       int wdlen;
16117
16118       wdlen = strlen (wd);
16119       wd1 = GGC_NEWVEC (char, wdlen + 2);
16120       strcpy (wd1, wd);
16121       wd1 [wdlen] = DIR_SEPARATOR;
16122       wd1 [wdlen + 1] = 0;
16123       wd = wd1;
16124     }
16125
16126     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
16127 }
16128
16129 /* Given a tree node describing an array bound (either lower or upper) output
16130    a representation for that bound.  */
16131
16132 static void
16133 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16134 {
16135   switch (TREE_CODE (bound))
16136     {
16137     case ERROR_MARK:
16138       return;
16139
16140     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
16141     case INTEGER_CST:
16142       {
16143         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16144
16145         /* Use the default if possible.  */
16146         if (bound_attr == DW_AT_lower_bound
16147             && (((is_c_family () || is_java ()) && integer_zerop (bound))
16148                 || (is_fortran () && integer_onep (bound))))
16149           ;
16150
16151         /* Otherwise represent the bound as an unsigned value with the
16152            precision of its type.  The precision and signedness of the
16153            type will be necessary to re-interpret it unambiguously.  */
16154         else if (prec < HOST_BITS_PER_WIDE_INT)
16155           {
16156             unsigned HOST_WIDE_INT mask
16157               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16158             add_AT_unsigned (subrange_die, bound_attr,
16159                              TREE_INT_CST_LOW (bound) & mask);
16160           }
16161         else if (prec == HOST_BITS_PER_WIDE_INT
16162                  || TREE_INT_CST_HIGH (bound) == 0)
16163           add_AT_unsigned (subrange_die, bound_attr,
16164                            TREE_INT_CST_LOW (bound));
16165         else
16166           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
16167                          TREE_INT_CST_LOW (bound));
16168       }
16169       break;
16170
16171     CASE_CONVERT:
16172     case VIEW_CONVERT_EXPR:
16173       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16174       break;
16175
16176     case SAVE_EXPR:
16177       break;
16178
16179     case VAR_DECL:
16180     case PARM_DECL:
16181     case RESULT_DECL:
16182       {
16183         dw_die_ref decl_die = lookup_decl_die (bound);
16184         dw_loc_list_ref loc;
16185
16186         /* ??? Can this happen, or should the variable have been bound
16187            first?  Probably it can, since I imagine that we try to create
16188            the types of parameters in the order in which they exist in
16189            the list, and won't have created a forward reference to a
16190            later parameter.  */
16191         if (decl_die != NULL)
16192           add_AT_die_ref (subrange_die, bound_attr, decl_die);
16193         else
16194           {
16195             loc = loc_list_from_tree (bound, 0);
16196             add_AT_location_description (subrange_die, bound_attr, loc);
16197           }
16198         break;
16199       }
16200
16201     default:
16202       {
16203         /* Otherwise try to create a stack operation procedure to
16204            evaluate the value of the array bound.  */
16205
16206         dw_die_ref ctx, decl_die;
16207         dw_loc_list_ref list;
16208
16209         list = loc_list_from_tree (bound, 2);
16210         if (list == NULL)
16211           break;
16212
16213         if (current_function_decl == 0)
16214           ctx = comp_unit_die;
16215         else
16216           ctx = lookup_decl_die (current_function_decl);
16217
16218         decl_die = new_die (DW_TAG_variable, ctx, bound);
16219         add_AT_flag (decl_die, DW_AT_artificial, 1);
16220         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16221         if (list->dw_loc_next)
16222           add_AT_loc_list (decl_die, DW_AT_location, list);
16223         else
16224           add_AT_loc (decl_die, DW_AT_location, list->expr);
16225
16226         add_AT_die_ref (subrange_die, bound_attr, decl_die);
16227         break;
16228       }
16229     }
16230 }
16231
16232 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16233    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16234    Note that the block of subscript information for an array type also
16235    includes information about the element type of the given array type.  */
16236
16237 static void
16238 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16239 {
16240   unsigned dimension_number;
16241   tree lower, upper;
16242   dw_die_ref subrange_die;
16243
16244   for (dimension_number = 0;
16245        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16246        type = TREE_TYPE (type), dimension_number++)
16247     {
16248       tree domain = TYPE_DOMAIN (type);
16249
16250       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16251         break;
16252
16253       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16254          and (in GNU C only) variable bounds.  Handle all three forms
16255          here.  */
16256       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16257       if (domain)
16258         {
16259           /* We have an array type with specified bounds.  */
16260           lower = TYPE_MIN_VALUE (domain);
16261           upper = TYPE_MAX_VALUE (domain);
16262
16263           /* Define the index type.  */
16264           if (TREE_TYPE (domain))
16265             {
16266               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16267                  TREE_TYPE field.  We can't emit debug info for this
16268                  because it is an unnamed integral type.  */
16269               if (TREE_CODE (domain) == INTEGER_TYPE
16270                   && TYPE_NAME (domain) == NULL_TREE
16271                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16272                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16273                 ;
16274               else
16275                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16276                                     type_die);
16277             }
16278
16279           /* ??? If upper is NULL, the array has unspecified length,
16280              but it does have a lower bound.  This happens with Fortran
16281                dimension arr(N:*)
16282              Since the debugger is definitely going to need to know N
16283              to produce useful results, go ahead and output the lower
16284              bound solo, and hope the debugger can cope.  */
16285
16286           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16287           if (upper)
16288             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16289         }
16290
16291       /* Otherwise we have an array type with an unspecified length.  The
16292          DWARF-2 spec does not say how to handle this; let's just leave out the
16293          bounds.  */
16294     }
16295 }
16296
16297 static void
16298 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16299 {
16300   unsigned size;
16301
16302   switch (TREE_CODE (tree_node))
16303     {
16304     case ERROR_MARK:
16305       size = 0;
16306       break;
16307     case ENUMERAL_TYPE:
16308     case RECORD_TYPE:
16309     case UNION_TYPE:
16310     case QUAL_UNION_TYPE:
16311       size = int_size_in_bytes (tree_node);
16312       break;
16313     case FIELD_DECL:
16314       /* For a data member of a struct or union, the DW_AT_byte_size is
16315          generally given as the number of bytes normally allocated for an
16316          object of the *declared* type of the member itself.  This is true
16317          even for bit-fields.  */
16318       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16319       break;
16320     default:
16321       gcc_unreachable ();
16322     }
16323
16324   /* Note that `size' might be -1 when we get to this point.  If it is, that
16325      indicates that the byte size of the entity in question is variable.  We
16326      have no good way of expressing this fact in Dwarf at the present time,
16327      so just let the -1 pass on through.  */
16328   add_AT_unsigned (die, DW_AT_byte_size, size);
16329 }
16330
16331 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16332    which specifies the distance in bits from the highest order bit of the
16333    "containing object" for the bit-field to the highest order bit of the
16334    bit-field itself.
16335
16336    For any given bit-field, the "containing object" is a hypothetical object
16337    (of some integral or enum type) within which the given bit-field lives.  The
16338    type of this hypothetical "containing object" is always the same as the
16339    declared type of the individual bit-field itself.  The determination of the
16340    exact location of the "containing object" for a bit-field is rather
16341    complicated.  It's handled by the `field_byte_offset' function (above).
16342
16343    Note that it is the size (in bytes) of the hypothetical "containing object"
16344    which will be given in the DW_AT_byte_size attribute for this bit-field.
16345    (See `byte_size_attribute' above).  */
16346
16347 static inline void
16348 add_bit_offset_attribute (dw_die_ref die, tree decl)
16349 {
16350   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16351   tree type = DECL_BIT_FIELD_TYPE (decl);
16352   HOST_WIDE_INT bitpos_int;
16353   HOST_WIDE_INT highest_order_object_bit_offset;
16354   HOST_WIDE_INT highest_order_field_bit_offset;
16355   HOST_WIDE_INT unsigned bit_offset;
16356
16357   /* Must be a field and a bit field.  */
16358   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16359
16360   /* We can't yet handle bit-fields whose offsets are variable, so if we
16361      encounter such things, just return without generating any attribute
16362      whatsoever.  Likewise for variable or too large size.  */
16363   if (! host_integerp (bit_position (decl), 0)
16364       || ! host_integerp (DECL_SIZE (decl), 1))
16365     return;
16366
16367   bitpos_int = int_bit_position (decl);
16368
16369   /* Note that the bit offset is always the distance (in bits) from the
16370      highest-order bit of the "containing object" to the highest-order bit of
16371      the bit-field itself.  Since the "high-order end" of any object or field
16372      is different on big-endian and little-endian machines, the computation
16373      below must take account of these differences.  */
16374   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16375   highest_order_field_bit_offset = bitpos_int;
16376
16377   if (! BYTES_BIG_ENDIAN)
16378     {
16379       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16380       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16381     }
16382
16383   bit_offset
16384     = (! BYTES_BIG_ENDIAN
16385        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16386        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16387
16388   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
16389 }
16390
16391 /* For a FIELD_DECL node which represents a bit field, output an attribute
16392    which specifies the length in bits of the given field.  */
16393
16394 static inline void
16395 add_bit_size_attribute (dw_die_ref die, tree decl)
16396 {
16397   /* Must be a field and a bit field.  */
16398   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16399               && DECL_BIT_FIELD_TYPE (decl));
16400
16401   if (host_integerp (DECL_SIZE (decl), 1))
16402     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16403 }
16404
16405 /* If the compiled language is ANSI C, then add a 'prototyped'
16406    attribute, if arg types are given for the parameters of a function.  */
16407
16408 static inline void
16409 add_prototyped_attribute (dw_die_ref die, tree func_type)
16410 {
16411   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
16412       && TYPE_ARG_TYPES (func_type) != NULL)
16413     add_AT_flag (die, DW_AT_prototyped, 1);
16414 }
16415
16416 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16417    by looking in either the type declaration or object declaration
16418    equate table.  */
16419
16420 static inline dw_die_ref
16421 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16422 {
16423   dw_die_ref origin_die = NULL;
16424
16425   if (TREE_CODE (origin) != FUNCTION_DECL)
16426     {
16427       /* We may have gotten separated from the block for the inlined
16428          function, if we're in an exception handler or some such; make
16429          sure that the abstract function has been written out.
16430
16431          Doing this for nested functions is wrong, however; functions are
16432          distinct units, and our context might not even be inline.  */
16433       tree fn = origin;
16434
16435       if (TYPE_P (fn))
16436         fn = TYPE_STUB_DECL (fn);
16437
16438       fn = decl_function_context (fn);
16439       if (fn)
16440         dwarf2out_abstract_function (fn);
16441     }
16442
16443   if (DECL_P (origin))
16444     origin_die = lookup_decl_die (origin);
16445   else if (TYPE_P (origin))
16446     origin_die = lookup_type_die (origin);
16447
16448   /* XXX: Functions that are never lowered don't always have correct block
16449      trees (in the case of java, they simply have no block tree, in some other
16450      languages).  For these functions, there is nothing we can really do to
16451      output correct debug info for inlined functions in all cases.  Rather
16452      than die, we'll just produce deficient debug info now, in that we will
16453      have variables without a proper abstract origin.  In the future, when all
16454      functions are lowered, we should re-add a gcc_assert (origin_die)
16455      here.  */
16456
16457   if (origin_die)
16458     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16459   return origin_die;
16460 }
16461
16462 /* We do not currently support the pure_virtual attribute.  */
16463
16464 static inline void
16465 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16466 {
16467   if (DECL_VINDEX (func_decl))
16468     {
16469       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16470
16471       if (host_integerp (DECL_VINDEX (func_decl), 0))
16472         add_AT_loc (die, DW_AT_vtable_elem_location,
16473                     new_loc_descr (DW_OP_constu,
16474                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16475                                    0));
16476
16477       /* GNU extension: Record what type this method came from originally.  */
16478       if (debug_info_level > DINFO_LEVEL_TERSE)
16479         add_AT_die_ref (die, DW_AT_containing_type,
16480                         lookup_type_die (DECL_CONTEXT (func_decl)));
16481     }
16482 }
16483 \f
16484 /* Add source coordinate attributes for the given decl.  */
16485
16486 static void
16487 add_src_coords_attributes (dw_die_ref die, tree decl)
16488 {
16489   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16490
16491   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16492   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16493 }
16494
16495 /* Add a DW_AT_name attribute and source coordinate attribute for the
16496    given decl, but only if it actually has a name.  */
16497
16498 static void
16499 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16500 {
16501   tree decl_name;
16502
16503   decl_name = DECL_NAME (decl);
16504   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16505     {
16506       const char *name = dwarf2_name (decl, 0);
16507       if (name)
16508         add_name_attribute (die, name);
16509       if (! DECL_ARTIFICIAL (decl))
16510         add_src_coords_attributes (die, decl);
16511
16512       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16513           && TREE_PUBLIC (decl)
16514           && !DECL_ABSTRACT (decl)
16515           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16516           && !is_fortran ())
16517         {
16518           /* Defer until we have an assembler name set.  */
16519           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16520             {
16521               limbo_die_node *asm_name;
16522
16523               asm_name = GGC_CNEW (limbo_die_node);
16524               asm_name->die = die;
16525               asm_name->created_for = decl;
16526               asm_name->next = deferred_asm_name;
16527               deferred_asm_name = asm_name;
16528             }
16529           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16530             add_AT_string (die, DW_AT_MIPS_linkage_name,
16531                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
16532         }
16533     }
16534
16535 #ifdef VMS_DEBUGGING_INFO
16536   /* Get the function's name, as described by its RTL.  This may be different
16537      from the DECL_NAME name used in the source file.  */
16538   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16539     {
16540       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16541                    XEXP (DECL_RTL (decl), 0));
16542       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16543     }
16544 #endif
16545 }
16546
16547 /* Push a new declaration scope.  */
16548
16549 static void
16550 push_decl_scope (tree scope)
16551 {
16552   VEC_safe_push (tree, gc, decl_scope_table, scope);
16553 }
16554
16555 /* Pop a declaration scope.  */
16556
16557 static inline void
16558 pop_decl_scope (void)
16559 {
16560   VEC_pop (tree, decl_scope_table);
16561 }
16562
16563 /* Return the DIE for the scope that immediately contains this type.
16564    Non-named types get global scope.  Named types nested in other
16565    types get their containing scope if it's open, or global scope
16566    otherwise.  All other types (i.e. function-local named types) get
16567    the current active scope.  */
16568
16569 static dw_die_ref
16570 scope_die_for (tree t, dw_die_ref context_die)
16571 {
16572   dw_die_ref scope_die = NULL;
16573   tree containing_scope;
16574   int i;
16575
16576   /* Non-types always go in the current scope.  */
16577   gcc_assert (TYPE_P (t));
16578
16579   containing_scope = TYPE_CONTEXT (t);
16580
16581   /* Use the containing namespace if it was passed in (for a declaration).  */
16582   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16583     {
16584       if (context_die == lookup_decl_die (containing_scope))
16585         /* OK */;
16586       else
16587         containing_scope = NULL_TREE;
16588     }
16589
16590   /* Ignore function type "scopes" from the C frontend.  They mean that
16591      a tagged type is local to a parmlist of a function declarator, but
16592      that isn't useful to DWARF.  */
16593   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16594     containing_scope = NULL_TREE;
16595
16596   if (containing_scope == NULL_TREE)
16597     scope_die = comp_unit_die;
16598   else if (TYPE_P (containing_scope))
16599     {
16600       /* For types, we can just look up the appropriate DIE.  But
16601          first we check to see if we're in the middle of emitting it
16602          so we know where the new DIE should go.  */
16603       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16604         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16605           break;
16606
16607       if (i < 0)
16608         {
16609           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16610                       || TREE_ASM_WRITTEN (containing_scope));
16611
16612           /* If none of the current dies are suitable, we get file scope.  */
16613           scope_die = comp_unit_die;
16614         }
16615       else
16616         scope_die = lookup_type_die (containing_scope);
16617     }
16618   else
16619     scope_die = context_die;
16620
16621   return scope_die;
16622 }
16623
16624 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16625
16626 static inline int
16627 local_scope_p (dw_die_ref context_die)
16628 {
16629   for (; context_die; context_die = context_die->die_parent)
16630     if (context_die->die_tag == DW_TAG_inlined_subroutine
16631         || context_die->die_tag == DW_TAG_subprogram)
16632       return 1;
16633
16634   return 0;
16635 }
16636
16637 /* Returns nonzero if CONTEXT_DIE is a class.  */
16638
16639 static inline int
16640 class_scope_p (dw_die_ref context_die)
16641 {
16642   return (context_die
16643           && (context_die->die_tag == DW_TAG_structure_type
16644               || context_die->die_tag == DW_TAG_class_type
16645               || context_die->die_tag == DW_TAG_interface_type
16646               || context_die->die_tag == DW_TAG_union_type));
16647 }
16648
16649 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16650    whether or not to treat a DIE in this context as a declaration.  */
16651
16652 static inline int
16653 class_or_namespace_scope_p (dw_die_ref context_die)
16654 {
16655   return (class_scope_p (context_die)
16656           || (context_die && context_die->die_tag == DW_TAG_namespace));
16657 }
16658
16659 /* Many forms of DIEs require a "type description" attribute.  This
16660    routine locates the proper "type descriptor" die for the type given
16661    by 'type', and adds a DW_AT_type attribute below the given die.  */
16662
16663 static void
16664 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16665                     int decl_volatile, dw_die_ref context_die)
16666 {
16667   enum tree_code code  = TREE_CODE (type);
16668   dw_die_ref type_die  = NULL;
16669
16670   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16671      or fixed-point type, use the inner type.  This is because we have no
16672      support for unnamed types in base_type_die.  This can happen if this is
16673      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16674   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16675       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16676     type = TREE_TYPE (type), code = TREE_CODE (type);
16677
16678   if (code == ERROR_MARK
16679       /* Handle a special case.  For functions whose return type is void, we
16680          generate *no* type attribute.  (Note that no object may have type
16681          `void', so this only applies to function return types).  */
16682       || code == VOID_TYPE)
16683     return;
16684
16685   type_die = modified_type_die (type,
16686                                 decl_const || TYPE_READONLY (type),
16687                                 decl_volatile || TYPE_VOLATILE (type),
16688                                 context_die);
16689
16690   if (type_die != NULL)
16691     add_AT_die_ref (object_die, DW_AT_type, type_die);
16692 }
16693
16694 /* Given an object die, add the calling convention attribute for the
16695    function call type.  */
16696 static void
16697 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16698 {
16699   enum dwarf_calling_convention value = DW_CC_normal;
16700
16701   value = ((enum dwarf_calling_convention)
16702            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16703
16704   /* DWARF doesn't provide a way to identify a program's source-level
16705      entry point.  DW_AT_calling_convention attributes are only meant
16706      to describe functions' calling conventions.  However, lacking a
16707      better way to signal the Fortran main program, we use this for the
16708      time being, following existing custom.  */
16709   if (is_fortran ()
16710       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16711     value = DW_CC_program;
16712
16713   /* Only add the attribute if the backend requests it, and
16714      is not DW_CC_normal.  */
16715   if (value && (value != DW_CC_normal))
16716     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16717 }
16718
16719 /* Given a tree pointer to a struct, class, union, or enum type node, return
16720    a pointer to the (string) tag name for the given type, or zero if the type
16721    was declared without a tag.  */
16722
16723 static const char *
16724 type_tag (const_tree type)
16725 {
16726   const char *name = 0;
16727
16728   if (TYPE_NAME (type) != 0)
16729     {
16730       tree t = 0;
16731
16732       /* Find the IDENTIFIER_NODE for the type name.  */
16733       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
16734         t = TYPE_NAME (type);
16735
16736       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16737          a TYPE_DECL node, regardless of whether or not a `typedef' was
16738          involved.  */
16739       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16740                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16741         {
16742           /* We want to be extra verbose.  Don't call dwarf_name if
16743              DECL_NAME isn't set.  The default hook for decl_printable_name
16744              doesn't like that, and in this context it's correct to return
16745              0, instead of "<anonymous>" or the like.  */
16746           if (DECL_NAME (TYPE_NAME (type)))
16747             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16748         }
16749
16750       /* Now get the name as a string, or invent one.  */
16751       if (!name && t != 0)
16752         name = IDENTIFIER_POINTER (t);
16753     }
16754
16755   return (name == 0 || *name == '\0') ? 0 : name;
16756 }
16757
16758 /* Return the type associated with a data member, make a special check
16759    for bit field types.  */
16760
16761 static inline tree
16762 member_declared_type (const_tree member)
16763 {
16764   return (DECL_BIT_FIELD_TYPE (member)
16765           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16766 }
16767
16768 /* Get the decl's label, as described by its RTL. This may be different
16769    from the DECL_NAME name used in the source file.  */
16770
16771 #if 0
16772 static const char *
16773 decl_start_label (tree decl)
16774 {
16775   rtx x;
16776   const char *fnname;
16777
16778   x = DECL_RTL (decl);
16779   gcc_assert (MEM_P (x));
16780
16781   x = XEXP (x, 0);
16782   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16783
16784   fnname = XSTR (x, 0);
16785   return fnname;
16786 }
16787 #endif
16788 \f
16789 /* These routines generate the internal representation of the DIE's for
16790    the compilation unit.  Debugging information is collected by walking
16791    the declaration trees passed in from dwarf2out_decl().  */
16792
16793 static void
16794 gen_array_type_die (tree type, dw_die_ref context_die)
16795 {
16796   dw_die_ref scope_die = scope_die_for (type, context_die);
16797   dw_die_ref array_die;
16798
16799   /* GNU compilers represent multidimensional array types as sequences of one
16800      dimensional array types whose element types are themselves array types.
16801      We sometimes squish that down to a single array_type DIE with multiple
16802      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16803      say that we are allowed to do this kind of compression in C, because
16804      there is no difference between an array of arrays and a multidimensional
16805      array.  We don't do this for Ada to remain as close as possible to the
16806      actual representation, which is especially important against the language
16807      flexibilty wrt arrays of variable size.  */
16808
16809   bool collapse_nested_arrays = !is_ada ();
16810   tree element_type;
16811
16812   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16813      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16814   if (TYPE_STRING_FLAG (type)
16815       && TREE_CODE (type) == ARRAY_TYPE
16816       && is_fortran ()
16817       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16818     {
16819       HOST_WIDE_INT size;
16820
16821       array_die = new_die (DW_TAG_string_type, scope_die, type);
16822       add_name_attribute (array_die, type_tag (type));
16823       equate_type_number_to_die (type, array_die);
16824       size = int_size_in_bytes (type);
16825       if (size >= 0)
16826         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16827       else if (TYPE_DOMAIN (type) != NULL_TREE
16828                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16829                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16830         {
16831           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16832           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16833
16834           size = int_size_in_bytes (TREE_TYPE (szdecl));
16835           if (loc && size > 0)
16836             {
16837               add_AT_location_description (array_die, DW_AT_string_length, loc);
16838               if (size != DWARF2_ADDR_SIZE)
16839                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16840             }
16841         }
16842       return;
16843     }
16844
16845   /* ??? The SGI dwarf reader fails for array of array of enum types
16846      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16847      array type comes before the outer array type.  We thus call gen_type_die
16848      before we new_die and must prevent nested array types collapsing for this
16849      target.  */
16850
16851 #ifdef MIPS_DEBUGGING_INFO
16852   gen_type_die (TREE_TYPE (type), context_die);
16853   collapse_nested_arrays = false;
16854 #endif
16855
16856   array_die = new_die (DW_TAG_array_type, scope_die, type);
16857   add_name_attribute (array_die, type_tag (type));
16858   equate_type_number_to_die (type, array_die);
16859
16860   if (TREE_CODE (type) == VECTOR_TYPE)
16861     {
16862       /* The frontend feeds us a representation for the vector as a struct
16863          containing an array.  Pull out the array type.  */
16864       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
16865       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16866     }
16867
16868   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16869   if (is_fortran ()
16870       && TREE_CODE (type) == ARRAY_TYPE
16871       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16872       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16873     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16874
16875 #if 0
16876   /* We default the array ordering.  SDB will probably do
16877      the right things even if DW_AT_ordering is not present.  It's not even
16878      an issue until we start to get into multidimensional arrays anyway.  If
16879      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16880      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16881      and when we find out that we need to put these in, we will only do so
16882      for multidimensional arrays.  */
16883   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16884 #endif
16885
16886 #ifdef MIPS_DEBUGGING_INFO
16887   /* The SGI compilers handle arrays of unknown bound by setting
16888      AT_declaration and not emitting any subrange DIEs.  */
16889   if (! TYPE_DOMAIN (type))
16890     add_AT_flag (array_die, DW_AT_declaration, 1);
16891   else
16892 #endif
16893     add_subscript_info (array_die, type, collapse_nested_arrays);
16894
16895   /* Add representation of the type of the elements of this array type and
16896      emit the corresponding DIE if we haven't done it already.  */  
16897   element_type = TREE_TYPE (type);
16898   if (collapse_nested_arrays)
16899     while (TREE_CODE (element_type) == ARRAY_TYPE)
16900       {
16901         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16902           break;
16903         element_type = TREE_TYPE (element_type);
16904       }
16905
16906 #ifndef MIPS_DEBUGGING_INFO
16907   gen_type_die (element_type, context_die);
16908 #endif
16909
16910   add_type_attribute (array_die, element_type, 0, 0, context_die);
16911
16912   if (get_AT (array_die, DW_AT_name))
16913     add_pubtype (type, array_die);
16914 }
16915
16916 static dw_loc_descr_ref
16917 descr_info_loc (tree val, tree base_decl)
16918 {
16919   HOST_WIDE_INT size;
16920   dw_loc_descr_ref loc, loc2;
16921   enum dwarf_location_atom op;
16922
16923   if (val == base_decl)
16924     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16925
16926   switch (TREE_CODE (val))
16927     {
16928     CASE_CONVERT:
16929       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16930     case VAR_DECL:
16931       return loc_descriptor_from_tree (val, 0);
16932     case INTEGER_CST:
16933       if (host_integerp (val, 0))
16934         return int_loc_descriptor (tree_low_cst (val, 0));
16935       break;
16936     case INDIRECT_REF:
16937       size = int_size_in_bytes (TREE_TYPE (val));
16938       if (size < 0)
16939         break;
16940       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16941       if (!loc)
16942         break;
16943       if (size == DWARF2_ADDR_SIZE)
16944         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16945       else
16946         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16947       return loc;
16948     case POINTER_PLUS_EXPR:
16949     case PLUS_EXPR:
16950       if (host_integerp (TREE_OPERAND (val, 1), 1)
16951           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16952              < 16384)
16953         {
16954           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16955           if (!loc)
16956             break;
16957           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16958         }
16959       else
16960         {
16961           op = DW_OP_plus;
16962         do_binop:
16963           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16964           if (!loc)
16965             break;
16966           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16967           if (!loc2)
16968             break;
16969           add_loc_descr (&loc, loc2);
16970           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16971         }
16972       return loc;
16973     case MINUS_EXPR:
16974       op = DW_OP_minus;
16975       goto do_binop;
16976     case MULT_EXPR:
16977       op = DW_OP_mul;
16978       goto do_binop;
16979     case EQ_EXPR:
16980       op = DW_OP_eq;
16981       goto do_binop;
16982     case NE_EXPR:
16983       op = DW_OP_ne;
16984       goto do_binop;
16985     default:
16986       break;
16987     }
16988   return NULL;
16989 }
16990
16991 static void
16992 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16993                       tree val, tree base_decl)
16994 {
16995   dw_loc_descr_ref loc;
16996
16997   if (host_integerp (val, 0))
16998     {
16999       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
17000       return;
17001     }
17002
17003   loc = descr_info_loc (val, base_decl);
17004   if (!loc)
17005     return;
17006
17007   add_AT_loc (die, attr, loc);
17008 }
17009
17010 /* This routine generates DIE for array with hidden descriptor, details
17011    are filled into *info by a langhook.  */
17012
17013 static void
17014 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17015                           dw_die_ref context_die)
17016 {
17017   dw_die_ref scope_die = scope_die_for (type, context_die);
17018   dw_die_ref array_die;
17019   int dim;
17020
17021   array_die = new_die (DW_TAG_array_type, scope_die, type);
17022   add_name_attribute (array_die, type_tag (type));
17023   equate_type_number_to_die (type, array_die);
17024
17025   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17026   if (is_fortran ()
17027       && info->ndimensions >= 2)
17028     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17029
17030   if (info->data_location)
17031     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17032                           info->base_decl);
17033   if (info->associated)
17034     add_descr_info_field (array_die, DW_AT_associated, info->associated,
17035                           info->base_decl);
17036   if (info->allocated)
17037     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17038                           info->base_decl);
17039
17040   for (dim = 0; dim < info->ndimensions; dim++)
17041     {
17042       dw_die_ref subrange_die
17043         = new_die (DW_TAG_subrange_type, array_die, NULL);
17044
17045       if (info->dimen[dim].lower_bound)
17046         {
17047           /* If it is the default value, omit it.  */
17048           if ((is_c_family () || is_java ())
17049               && integer_zerop (info->dimen[dim].lower_bound))
17050             ;
17051           else if (is_fortran ()
17052                    && integer_onep (info->dimen[dim].lower_bound))
17053             ;
17054           else
17055             add_descr_info_field (subrange_die, DW_AT_lower_bound,
17056                                   info->dimen[dim].lower_bound,
17057                                   info->base_decl);
17058         }
17059       if (info->dimen[dim].upper_bound)
17060         add_descr_info_field (subrange_die, DW_AT_upper_bound,
17061                               info->dimen[dim].upper_bound,
17062                               info->base_decl);
17063       if (info->dimen[dim].stride)
17064         add_descr_info_field (subrange_die, DW_AT_byte_stride,
17065                               info->dimen[dim].stride,
17066                               info->base_decl);
17067     }
17068
17069   gen_type_die (info->element_type, context_die);
17070   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17071
17072   if (get_AT (array_die, DW_AT_name))
17073     add_pubtype (type, array_die);
17074 }
17075
17076 #if 0
17077 static void
17078 gen_entry_point_die (tree decl, dw_die_ref context_die)
17079 {
17080   tree origin = decl_ultimate_origin (decl);
17081   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17082
17083   if (origin != NULL)
17084     add_abstract_origin_attribute (decl_die, origin);
17085   else
17086     {
17087       add_name_and_src_coords_attributes (decl_die, decl);
17088       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17089                           0, 0, context_die);
17090     }
17091
17092   if (DECL_ABSTRACT (decl))
17093     equate_decl_number_to_die (decl, decl_die);
17094   else
17095     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17096 }
17097 #endif
17098
17099 /* Walk through the list of incomplete types again, trying once more to
17100    emit full debugging info for them.  */
17101
17102 static void
17103 retry_incomplete_types (void)
17104 {
17105   int i;
17106
17107   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
17108     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
17109 }
17110
17111 /* Determine what tag to use for a record type.  */
17112
17113 static enum dwarf_tag
17114 record_type_tag (tree type)
17115 {
17116   if (! lang_hooks.types.classify_record)
17117     return DW_TAG_structure_type;
17118
17119   switch (lang_hooks.types.classify_record (type))
17120     {
17121     case RECORD_IS_STRUCT:
17122       return DW_TAG_structure_type;
17123
17124     case RECORD_IS_CLASS:
17125       return DW_TAG_class_type;
17126
17127     case RECORD_IS_INTERFACE:
17128       if (dwarf_version >= 3 || !dwarf_strict)
17129         return DW_TAG_interface_type;
17130       return DW_TAG_structure_type;
17131
17132     default:
17133       gcc_unreachable ();
17134     }
17135 }
17136
17137 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
17138    include all of the information about the enumeration values also. Each
17139    enumerated type name/value is listed as a child of the enumerated type
17140    DIE.  */
17141
17142 static dw_die_ref
17143 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17144 {
17145   dw_die_ref type_die = lookup_type_die (type);
17146
17147   if (type_die == NULL)
17148     {
17149       type_die = new_die (DW_TAG_enumeration_type,
17150                           scope_die_for (type, context_die), type);
17151       equate_type_number_to_die (type, type_die);
17152       add_name_attribute (type_die, type_tag (type));
17153     }
17154   else if (! TYPE_SIZE (type))
17155     return type_die;
17156   else
17157     remove_AT (type_die, DW_AT_declaration);
17158
17159   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17160      given enum type is incomplete, do not generate the DW_AT_byte_size
17161      attribute or the DW_AT_element_list attribute.  */
17162   if (TYPE_SIZE (type))
17163     {
17164       tree link;
17165
17166       TREE_ASM_WRITTEN (type) = 1;
17167       add_byte_size_attribute (type_die, type);
17168       if (TYPE_STUB_DECL (type) != NULL_TREE)
17169         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17170
17171       /* If the first reference to this type was as the return type of an
17172          inline function, then it may not have a parent.  Fix this now.  */
17173       if (type_die->die_parent == NULL)
17174         add_child_die (scope_die_for (type, context_die), type_die);
17175
17176       for (link = TYPE_VALUES (type);
17177            link != NULL; link = TREE_CHAIN (link))
17178         {
17179           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17180           tree value = TREE_VALUE (link);
17181
17182           add_name_attribute (enum_die,
17183                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17184
17185           if (TREE_CODE (value) == CONST_DECL)
17186             value = DECL_INITIAL (value);
17187
17188           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17189             /* DWARF2 does not provide a way of indicating whether or
17190                not enumeration constants are signed or unsigned.  GDB
17191                always assumes the values are signed, so we output all
17192                values as if they were signed.  That means that
17193                enumeration constants with very large unsigned values
17194                will appear to have negative values in the debugger.  */
17195             add_AT_int (enum_die, DW_AT_const_value,
17196                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17197         }
17198     }
17199   else
17200     add_AT_flag (type_die, DW_AT_declaration, 1);
17201
17202   if (get_AT (type_die, DW_AT_name))
17203     add_pubtype (type, type_die);
17204
17205   return type_die;
17206 }
17207
17208 /* Generate a DIE to represent either a real live formal parameter decl or to
17209    represent just the type of some formal parameter position in some function
17210    type.
17211
17212    Note that this routine is a bit unusual because its argument may be a
17213    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17214    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17215    node.  If it's the former then this function is being called to output a
17216    DIE to represent a formal parameter object (or some inlining thereof).  If
17217    it's the latter, then this function is only being called to output a
17218    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17219    argument type of some subprogram type.
17220    If EMIT_NAME_P is true, name and source coordinate attributes
17221    are emitted.  */
17222
17223 static dw_die_ref
17224 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17225                           dw_die_ref context_die)
17226 {
17227   tree node_or_origin = node ? node : origin;
17228   dw_die_ref parm_die
17229     = new_die (DW_TAG_formal_parameter, context_die, node);
17230
17231   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17232     {
17233     case tcc_declaration:
17234       if (!origin)
17235         origin = decl_ultimate_origin (node);
17236       if (origin != NULL)
17237         add_abstract_origin_attribute (parm_die, origin);
17238       else
17239         {
17240           tree type = TREE_TYPE (node);
17241           if (emit_name_p)
17242             add_name_and_src_coords_attributes (parm_die, node);
17243           if (decl_by_reference_p (node))
17244             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17245                                 context_die);
17246           else
17247             add_type_attribute (parm_die, type,
17248                                 TREE_READONLY (node),
17249                                 TREE_THIS_VOLATILE (node),
17250                                 context_die);
17251           if (DECL_ARTIFICIAL (node))
17252             add_AT_flag (parm_die, DW_AT_artificial, 1);
17253         }
17254
17255       if (node && node != origin)
17256         equate_decl_number_to_die (node, parm_die);
17257       if (! DECL_ABSTRACT (node_or_origin))
17258         add_location_or_const_value_attribute (parm_die, node_or_origin,
17259                                                DW_AT_location);
17260
17261       break;
17262
17263     case tcc_type:
17264       /* We were called with some kind of a ..._TYPE node.  */
17265       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17266       break;
17267
17268     default:
17269       gcc_unreachable ();
17270     }
17271
17272   return parm_die;
17273 }
17274
17275 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17276    children DW_TAG_formal_parameter DIEs representing the arguments of the
17277    parameter pack.
17278
17279    PARM_PACK must be a function parameter pack.
17280    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17281    must point to the subsequent arguments of the function PACK_ARG belongs to.
17282    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17283    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17284    following the last one for which a DIE was generated.  */
17285
17286 static dw_die_ref
17287 gen_formal_parameter_pack_die  (tree parm_pack,
17288                                 tree pack_arg,
17289                                 dw_die_ref subr_die,
17290                                 tree *next_arg)
17291 {
17292   tree arg;
17293   dw_die_ref parm_pack_die;
17294
17295   gcc_assert (parm_pack
17296               && lang_hooks.function_parameter_pack_p (parm_pack)
17297               && subr_die);
17298
17299   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17300   add_src_coords_attributes (parm_pack_die, parm_pack);
17301
17302   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
17303     {
17304       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17305                                                                  parm_pack))
17306         break;
17307       gen_formal_parameter_die (arg, NULL,
17308                                 false /* Don't emit name attribute.  */,
17309                                 parm_pack_die);
17310     }
17311   if (next_arg)
17312     *next_arg = arg;
17313   return parm_pack_die;
17314 }
17315
17316 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17317    at the end of an (ANSI prototyped) formal parameters list.  */
17318
17319 static void
17320 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17321 {
17322   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17323 }
17324
17325 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17326    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17327    parameters as specified in some function type specification (except for
17328    those which appear as part of a function *definition*).  */
17329
17330 static void
17331 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17332 {
17333   tree link;
17334   tree formal_type = NULL;
17335   tree first_parm_type;
17336   tree arg;
17337
17338   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17339     {
17340       arg = DECL_ARGUMENTS (function_or_method_type);
17341       function_or_method_type = TREE_TYPE (function_or_method_type);
17342     }
17343   else
17344     arg = NULL_TREE;
17345
17346   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17347
17348   /* Make our first pass over the list of formal parameter types and output a
17349      DW_TAG_formal_parameter DIE for each one.  */
17350   for (link = first_parm_type; link; )
17351     {
17352       dw_die_ref parm_die;
17353
17354       formal_type = TREE_VALUE (link);
17355       if (formal_type == void_type_node)
17356         break;
17357
17358       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17359       parm_die = gen_formal_parameter_die (formal_type, NULL,
17360                                            true /* Emit name attribute.  */,
17361                                            context_die);
17362       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
17363            && link == first_parm_type)
17364           || (arg && DECL_ARTIFICIAL (arg)))
17365         add_AT_flag (parm_die, DW_AT_artificial, 1);
17366
17367       link = TREE_CHAIN (link);
17368       if (arg)
17369         arg = TREE_CHAIN (arg);
17370     }
17371
17372   /* If this function type has an ellipsis, add a
17373      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17374   if (formal_type != void_type_node)
17375     gen_unspecified_parameters_die (function_or_method_type, context_die);
17376
17377   /* Make our second (and final) pass over the list of formal parameter types
17378      and output DIEs to represent those types (as necessary).  */
17379   for (link = TYPE_ARG_TYPES (function_or_method_type);
17380        link && TREE_VALUE (link);
17381        link = TREE_CHAIN (link))
17382     gen_type_die (TREE_VALUE (link), context_die);
17383 }
17384
17385 /* We want to generate the DIE for TYPE so that we can generate the
17386    die for MEMBER, which has been defined; we will need to refer back
17387    to the member declaration nested within TYPE.  If we're trying to
17388    generate minimal debug info for TYPE, processing TYPE won't do the
17389    trick; we need to attach the member declaration by hand.  */
17390
17391 static void
17392 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17393 {
17394   gen_type_die (type, context_die);
17395
17396   /* If we're trying to avoid duplicate debug info, we may not have
17397      emitted the member decl for this function.  Emit it now.  */
17398   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17399       && ! lookup_decl_die (member))
17400     {
17401       dw_die_ref type_die;
17402       gcc_assert (!decl_ultimate_origin (member));
17403
17404       push_decl_scope (type);
17405       type_die = lookup_type_die (type);
17406       if (TREE_CODE (member) == FUNCTION_DECL)
17407         gen_subprogram_die (member, type_die);
17408       else if (TREE_CODE (member) == FIELD_DECL)
17409         {
17410           /* Ignore the nameless fields that are used to skip bits but handle
17411              C++ anonymous unions and structs.  */
17412           if (DECL_NAME (member) != NULL_TREE
17413               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17414               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17415             {
17416               gen_type_die (member_declared_type (member), type_die);
17417               gen_field_die (member, type_die);
17418             }
17419         }
17420       else
17421         gen_variable_die (member, NULL_TREE, type_die);
17422
17423       pop_decl_scope ();
17424     }
17425 }
17426
17427 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17428    may later generate inlined and/or out-of-line instances of.  */
17429
17430 static void
17431 dwarf2out_abstract_function (tree decl)
17432 {
17433   dw_die_ref old_die;
17434   tree save_fn;
17435   tree context;
17436   int was_abstract = DECL_ABSTRACT (decl);
17437   htab_t old_decl_loc_table;
17438
17439   /* Make sure we have the actual abstract inline, not a clone.  */
17440   decl = DECL_ORIGIN (decl);
17441
17442   old_die = lookup_decl_die (decl);
17443   if (old_die && get_AT (old_die, DW_AT_inline))
17444     /* We've already generated the abstract instance.  */
17445     return;
17446
17447   /* We can be called while recursively when seeing block defining inlined subroutine
17448      DIE.  Be sure to not clobber the outer location table nor use it or we would
17449      get locations in abstract instantces.  */
17450   old_decl_loc_table = decl_loc_table;
17451   decl_loc_table = NULL;
17452
17453   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17454      we don't get confused by DECL_ABSTRACT.  */
17455   if (debug_info_level > DINFO_LEVEL_TERSE)
17456     {
17457       context = decl_class_context (decl);
17458       if (context)
17459         gen_type_die_for_member
17460           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
17461     }
17462
17463   /* Pretend we've just finished compiling this function.  */
17464   save_fn = current_function_decl;
17465   current_function_decl = decl;
17466   push_cfun (DECL_STRUCT_FUNCTION (decl));
17467
17468   set_decl_abstract_flags (decl, 1);
17469   dwarf2out_decl (decl);
17470   if (! was_abstract)
17471     set_decl_abstract_flags (decl, 0);
17472
17473   current_function_decl = save_fn;
17474   decl_loc_table = old_decl_loc_table;
17475   pop_cfun ();
17476 }
17477
17478 /* Helper function of premark_used_types() which gets called through
17479    htab_traverse.
17480
17481    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17482    marked as unused by prune_unused_types.  */
17483
17484 static int
17485 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17486 {
17487   tree type;
17488   dw_die_ref die;
17489
17490   type = (tree) *slot;
17491   die = lookup_type_die (type);
17492   if (die != NULL)
17493     die->die_perennial_p = 1;
17494   return 1;
17495 }
17496
17497 /* Helper function of premark_types_used_by_global_vars which gets called
17498    through htab_traverse.
17499
17500    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17501    marked as unused by prune_unused_types. The DIE of the type is marked
17502    only if the global variable using the type will actually be emitted.  */
17503
17504 static int
17505 premark_types_used_by_global_vars_helper (void **slot,
17506                                           void *data ATTRIBUTE_UNUSED)
17507 {
17508   struct types_used_by_vars_entry *entry;
17509   dw_die_ref die;
17510
17511   entry = (struct types_used_by_vars_entry *) *slot;
17512   gcc_assert (entry->type != NULL
17513               && entry->var_decl != NULL);
17514   die = lookup_type_die (entry->type);
17515   if (die)
17516     {
17517       /* Ask cgraph if the global variable really is to be emitted.
17518          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17519       struct varpool_node *node = varpool_node (entry->var_decl);
17520       if (node->needed)
17521         {
17522           die->die_perennial_p = 1;
17523           /* Keep the parent DIEs as well.  */
17524           while ((die = die->die_parent) && die->die_perennial_p == 0)
17525             die->die_perennial_p = 1;
17526         }
17527     }
17528   return 1;
17529 }
17530
17531 /* Mark all members of used_types_hash as perennial.  */
17532
17533 static void
17534 premark_used_types (void)
17535 {
17536   if (cfun && cfun->used_types_hash)
17537     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17538 }
17539
17540 /* Mark all members of types_used_by_vars_entry as perennial.  */
17541
17542 static void
17543 premark_types_used_by_global_vars (void)
17544 {
17545   if (types_used_by_vars_hash)
17546     htab_traverse (types_used_by_vars_hash,
17547                    premark_types_used_by_global_vars_helper, NULL);
17548 }
17549
17550 /* Generate a DIE to represent a declared function (either file-scope or
17551    block-local).  */
17552
17553 static void
17554 gen_subprogram_die (tree decl, dw_die_ref context_die)
17555 {
17556   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17557   tree origin = decl_ultimate_origin (decl);
17558   dw_die_ref subr_die;
17559   tree fn_arg_types;
17560   tree outer_scope;
17561   dw_die_ref old_die = lookup_decl_die (decl);
17562   int declaration = (current_function_decl != decl
17563                      || class_or_namespace_scope_p (context_die));
17564
17565   premark_used_types ();
17566
17567   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17568      started to generate the abstract instance of an inline, decided to output
17569      its containing class, and proceeded to emit the declaration of the inline
17570      from the member list for the class.  If so, DECLARATION takes priority;
17571      we'll get back to the abstract instance when done with the class.  */
17572
17573   /* The class-scope declaration DIE must be the primary DIE.  */
17574   if (origin && declaration && class_or_namespace_scope_p (context_die))
17575     {
17576       origin = NULL;
17577       gcc_assert (!old_die);
17578     }
17579
17580   /* Now that the C++ front end lazily declares artificial member fns, we
17581      might need to retrofit the declaration into its class.  */
17582   if (!declaration && !origin && !old_die
17583       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17584       && !class_or_namespace_scope_p (context_die)
17585       && debug_info_level > DINFO_LEVEL_TERSE)
17586     old_die = force_decl_die (decl);
17587
17588   if (origin != NULL)
17589     {
17590       gcc_assert (!declaration || local_scope_p (context_die));
17591
17592       /* Fixup die_parent for the abstract instance of a nested
17593          inline function.  */
17594       if (old_die && old_die->die_parent == NULL)
17595         add_child_die (context_die, old_die);
17596
17597       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17598       add_abstract_origin_attribute (subr_die, origin);
17599     }
17600   else if (old_die)
17601     {
17602       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17603       struct dwarf_file_data * file_index = lookup_filename (s.file);
17604
17605       if (!get_AT_flag (old_die, DW_AT_declaration)
17606           /* We can have a normal definition following an inline one in the
17607              case of redefinition of GNU C extern inlines.
17608              It seems reasonable to use AT_specification in this case.  */
17609           && !get_AT (old_die, DW_AT_inline))
17610         {
17611           /* Detect and ignore this case, where we are trying to output
17612              something we have already output.  */
17613           return;
17614         }
17615
17616       /* If the definition comes from the same place as the declaration,
17617          maybe use the old DIE.  We always want the DIE for this function
17618          that has the *_pc attributes to be under comp_unit_die so the
17619          debugger can find it.  We also need to do this for abstract
17620          instances of inlines, since the spec requires the out-of-line copy
17621          to have the same parent.  For local class methods, this doesn't
17622          apply; we just use the old DIE.  */
17623       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
17624           && (DECL_ARTIFICIAL (decl)
17625               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17626                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17627                       == (unsigned) s.line))))
17628         {
17629           subr_die = old_die;
17630
17631           /* Clear out the declaration attribute and the formal parameters.
17632              Do not remove all children, because it is possible that this
17633              declaration die was forced using force_decl_die(). In such
17634              cases die that forced declaration die (e.g. TAG_imported_module)
17635              is one of the children that we do not want to remove.  */
17636           remove_AT (subr_die, DW_AT_declaration);
17637           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17638         }
17639       else
17640         {
17641           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17642           add_AT_specification (subr_die, old_die);
17643           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17644             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17645           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17646             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17647         }
17648     }
17649   else
17650     {
17651       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17652
17653       if (TREE_PUBLIC (decl))
17654         add_AT_flag (subr_die, DW_AT_external, 1);
17655
17656       add_name_and_src_coords_attributes (subr_die, decl);
17657       if (debug_info_level > DINFO_LEVEL_TERSE)
17658         {
17659           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17660           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17661                               0, 0, context_die);
17662         }
17663
17664       add_pure_or_virtual_attribute (subr_die, decl);
17665       if (DECL_ARTIFICIAL (decl))
17666         add_AT_flag (subr_die, DW_AT_artificial, 1);
17667
17668       if (TREE_PROTECTED (decl))
17669         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
17670       else if (TREE_PRIVATE (decl))
17671         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
17672     }
17673
17674   if (declaration)
17675     {
17676       if (!old_die || !get_AT (old_die, DW_AT_inline))
17677         {
17678           add_AT_flag (subr_die, DW_AT_declaration, 1);
17679
17680           /* If this is an explicit function declaration then generate
17681              a DW_AT_explicit attribute.  */
17682           if (lang_hooks.decls.function_decl_explicit_p (decl)
17683               && (dwarf_version >= 3 || !dwarf_strict))
17684             add_AT_flag (subr_die, DW_AT_explicit, 1);
17685
17686           /* The first time we see a member function, it is in the context of
17687              the class to which it belongs.  We make sure of this by emitting
17688              the class first.  The next time is the definition, which is
17689              handled above.  The two may come from the same source text.
17690
17691              Note that force_decl_die() forces function declaration die. It is
17692              later reused to represent definition.  */
17693           equate_decl_number_to_die (decl, subr_die);
17694         }
17695     }
17696   else if (DECL_ABSTRACT (decl))
17697     {
17698       if (DECL_DECLARED_INLINE_P (decl))
17699         {
17700           if (cgraph_function_possibly_inlined_p (decl))
17701             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17702           else
17703             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17704         }
17705       else
17706         {
17707           if (cgraph_function_possibly_inlined_p (decl))
17708             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17709           else
17710             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17711         }
17712
17713       if (DECL_DECLARED_INLINE_P (decl)
17714           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17715         add_AT_flag (subr_die, DW_AT_artificial, 1);
17716
17717       equate_decl_number_to_die (decl, subr_die);
17718     }
17719   else if (!DECL_EXTERNAL (decl))
17720     {
17721       HOST_WIDE_INT cfa_fb_offset;
17722
17723       if (!old_die || !get_AT (old_die, DW_AT_inline))
17724         equate_decl_number_to_die (decl, subr_die);
17725
17726       if (!flag_reorder_blocks_and_partition)
17727         {
17728           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17729                                        current_function_funcdef_no);
17730           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17731           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17732                                        current_function_funcdef_no);
17733           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17734
17735           add_pubname (decl, subr_die);
17736           add_arange (decl, subr_die);
17737         }
17738       else
17739         {  /* Do nothing for now; maybe need to duplicate die, one for
17740               hot section and one for cold section, then use the hot/cold
17741               section begin/end labels to generate the aranges...  */
17742           /*
17743             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
17744             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
17745             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
17746             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
17747
17748             add_pubname (decl, subr_die);
17749             add_arange (decl, subr_die);
17750             add_arange (decl, subr_die);
17751            */
17752         }
17753
17754 #ifdef MIPS_DEBUGGING_INFO
17755       /* Add a reference to the FDE for this routine.  */
17756       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
17757 #endif
17758
17759       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17760
17761       /* We define the "frame base" as the function's CFA.  This is more
17762          convenient for several reasons: (1) It's stable across the prologue
17763          and epilogue, which makes it better than just a frame pointer,
17764          (2) With dwarf3, there exists a one-byte encoding that allows us
17765          to reference the .debug_frame data by proxy, but failing that,
17766          (3) We can at least reuse the code inspection and interpretation
17767          code that determines the CFA position at various points in the
17768          function.  */
17769       if (dwarf_version >= 3)
17770         {
17771           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17772           add_AT_loc (subr_die, DW_AT_frame_base, op);
17773         }
17774       else
17775         {
17776           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17777           if (list->dw_loc_next)
17778             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17779           else
17780             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17781         }
17782
17783       /* Compute a displacement from the "steady-state frame pointer" to
17784          the CFA.  The former is what all stack slots and argument slots
17785          will reference in the rtl; the later is what we've told the
17786          debugger about.  We'll need to adjust all frame_base references
17787          by this displacement.  */
17788       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17789
17790       if (cfun->static_chain_decl)
17791         add_AT_location_description (subr_die, DW_AT_static_link,
17792                  loc_list_from_tree (cfun->static_chain_decl, 2));
17793     }
17794
17795   /* Generate child dies for template paramaters.  */
17796   if (debug_info_level > DINFO_LEVEL_TERSE)
17797     gen_generic_params_dies (decl);
17798
17799   /* Now output descriptions of the arguments for this function. This gets
17800      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17801      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17802      `...' at the end of the formal parameter list.  In order to find out if
17803      there was a trailing ellipsis or not, we must instead look at the type
17804      associated with the FUNCTION_DECL.  This will be a node of type
17805      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17806      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17807      an ellipsis at the end.  */
17808
17809   /* In the case where we are describing a mere function declaration, all we
17810      need to do here (and all we *can* do here) is to describe the *types* of
17811      its formal parameters.  */
17812   if (debug_info_level <= DINFO_LEVEL_TERSE)
17813     ;
17814   else if (declaration)
17815     gen_formal_types_die (decl, subr_die);
17816   else
17817     {
17818       /* Generate DIEs to represent all known formal parameters.  */
17819       tree parm = DECL_ARGUMENTS (decl);
17820       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17821       tree generic_decl_parm = generic_decl
17822                                 ? DECL_ARGUMENTS (generic_decl)
17823                                 : NULL;
17824
17825       /* Now we want to walk the list of parameters of the function and
17826          emit their relevant DIEs.
17827
17828          We consider the case of DECL being an instance of a generic function
17829          as well as it being a normal function.
17830
17831          If DECL is an instance of a generic function we walk the
17832          parameters of the generic function declaration _and_ the parameters of
17833          DECL itself. This is useful because we want to emit specific DIEs for
17834          function parameter packs and those are declared as part of the
17835          generic function declaration. In that particular case,
17836          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17837          That DIE has children DIEs representing the set of arguments
17838          of the pack. Note that the set of pack arguments can be empty.
17839          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17840          children DIE.
17841         
17842          Otherwise, we just consider the parameters of DECL.  */
17843       while (generic_decl_parm || parm)
17844         {
17845           if (generic_decl_parm
17846               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17847             gen_formal_parameter_pack_die (generic_decl_parm,
17848                                            parm, subr_die,
17849                                            &parm);
17850           else if (parm)
17851             {
17852               gen_decl_die (parm, NULL, subr_die);
17853               parm = TREE_CHAIN (parm);
17854             }
17855
17856           if (generic_decl_parm)
17857             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
17858         }
17859
17860       /* Decide whether we need an unspecified_parameters DIE at the end.
17861          There are 2 more cases to do this for: 1) the ansi ... declaration -
17862          this is detectable when the end of the arg list is not a
17863          void_type_node 2) an unprototyped function declaration (not a
17864          definition).  This just means that we have no info about the
17865          parameters at all.  */
17866       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
17867       if (fn_arg_types != NULL)
17868         {
17869           /* This is the prototyped case, check for....  */
17870           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
17871             gen_unspecified_parameters_die (decl, subr_die);
17872         }
17873       else if (DECL_INITIAL (decl) == NULL_TREE)
17874         gen_unspecified_parameters_die (decl, subr_die);
17875     }
17876
17877   /* Output Dwarf info for all of the stuff within the body of the function
17878      (if it has one - it may be just a declaration).  */
17879   outer_scope = DECL_INITIAL (decl);
17880
17881   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17882      a function.  This BLOCK actually represents the outermost binding contour
17883      for the function, i.e. the contour in which the function's formal
17884      parameters and labels get declared. Curiously, it appears that the front
17885      end doesn't actually put the PARM_DECL nodes for the current function onto
17886      the BLOCK_VARS list for this outer scope, but are strung off of the
17887      DECL_ARGUMENTS list for the function instead.
17888
17889      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17890      the LABEL_DECL nodes for the function however, and we output DWARF info
17891      for those in decls_for_scope.  Just within the `outer_scope' there will be
17892      a BLOCK node representing the function's outermost pair of curly braces,
17893      and any blocks used for the base and member initializers of a C++
17894      constructor function.  */
17895   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17896     {
17897       /* Emit a DW_TAG_variable DIE for a named return value.  */
17898       if (DECL_NAME (DECL_RESULT (decl)))
17899         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17900
17901       current_function_has_inlines = 0;
17902       decls_for_scope (outer_scope, subr_die, 0);
17903
17904 #if 0 && defined (MIPS_DEBUGGING_INFO)
17905       if (current_function_has_inlines)
17906         {
17907           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
17908           if (! comp_unit_has_inlines)
17909             {
17910               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
17911               comp_unit_has_inlines = 1;
17912             }
17913         }
17914 #endif
17915     }
17916   /* Add the calling convention attribute if requested.  */
17917   add_calling_convention_attribute (subr_die, decl);
17918
17919 }
17920
17921 /* Returns a hash value for X (which really is a die_struct).  */
17922
17923 static hashval_t
17924 common_block_die_table_hash (const void *x)
17925 {
17926   const_dw_die_ref d = (const_dw_die_ref) x;
17927   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17928 }
17929
17930 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17931    as decl_id and die_parent of die_struct Y.  */
17932
17933 static int
17934 common_block_die_table_eq (const void *x, const void *y)
17935 {
17936   const_dw_die_ref d = (const_dw_die_ref) x;
17937   const_dw_die_ref e = (const_dw_die_ref) y;
17938   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17939 }
17940
17941 /* Generate a DIE to represent a declared data object.
17942    Either DECL or ORIGIN must be non-null.  */
17943
17944 static void
17945 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17946 {
17947   HOST_WIDE_INT off;
17948   tree com_decl;
17949   tree decl_or_origin = decl ? decl : origin;
17950   dw_die_ref var_die;
17951   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17952   dw_die_ref origin_die;
17953   int declaration = (DECL_EXTERNAL (decl_or_origin)
17954                      || class_or_namespace_scope_p (context_die));
17955
17956   if (!origin)
17957     origin = decl_ultimate_origin (decl);
17958
17959   com_decl = fortran_common (decl_or_origin, &off);
17960
17961   /* Symbol in common gets emitted as a child of the common block, in the form
17962      of a data member.  */
17963   if (com_decl)
17964     {
17965       tree field;
17966       dw_die_ref com_die;
17967       dw_loc_list_ref loc;
17968       die_node com_die_arg;
17969
17970       var_die = lookup_decl_die (decl_or_origin);
17971       if (var_die)
17972         {
17973           if (get_AT (var_die, DW_AT_location) == NULL)
17974             {
17975               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17976               if (loc)
17977                 {
17978                   if (off)
17979                     {
17980                       /* Optimize the common case.  */
17981                       if (single_element_loc_list_p (loc)
17982                           && loc->expr->dw_loc_opc == DW_OP_addr
17983                           && loc->expr->dw_loc_next == NULL
17984                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17985                              == SYMBOL_REF)
17986                         loc->expr->dw_loc_oprnd1.v.val_addr
17987                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17988                         else
17989                           loc_list_plus_const (loc, off);
17990                     }
17991                   add_AT_location_description (var_die, DW_AT_location, loc);
17992                   remove_AT (var_die, DW_AT_declaration);
17993                 }
17994             }
17995           return;
17996         }
17997
17998       if (common_block_die_table == NULL)
17999         common_block_die_table
18000           = htab_create_ggc (10, common_block_die_table_hash,
18001                              common_block_die_table_eq, NULL);
18002
18003       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
18004       com_die_arg.decl_id = DECL_UID (com_decl);
18005       com_die_arg.die_parent = context_die;
18006       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18007       loc = loc_list_from_tree (com_decl, 2);
18008       if (com_die == NULL)
18009         {
18010           const char *cnam
18011             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18012           void **slot;
18013
18014           com_die = new_die (DW_TAG_common_block, context_die, decl);
18015           add_name_and_src_coords_attributes (com_die, com_decl);
18016           if (loc)
18017             {
18018               add_AT_location_description (com_die, DW_AT_location, loc);
18019               /* Avoid sharing the same loc descriptor between
18020                  DW_TAG_common_block and DW_TAG_variable.  */
18021               loc = loc_list_from_tree (com_decl, 2);
18022             }
18023           else if (DECL_EXTERNAL (decl))
18024             add_AT_flag (com_die, DW_AT_declaration, 1);
18025           add_pubname_string (cnam, com_die); /* ??? needed? */
18026           com_die->decl_id = DECL_UID (com_decl);
18027           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18028           *slot = (void *) com_die;
18029         }
18030       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18031         {
18032           add_AT_location_description (com_die, DW_AT_location, loc);
18033           loc = loc_list_from_tree (com_decl, 2);
18034           remove_AT (com_die, DW_AT_declaration);
18035         }
18036       var_die = new_die (DW_TAG_variable, com_die, decl);
18037       add_name_and_src_coords_attributes (var_die, decl);
18038       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18039                           TREE_THIS_VOLATILE (decl), context_die);
18040       add_AT_flag (var_die, DW_AT_external, 1);
18041       if (loc)
18042         {
18043           if (off)
18044             {
18045               /* Optimize the common case.  */
18046               if (single_element_loc_list_p (loc)
18047                   && loc->expr->dw_loc_opc == DW_OP_addr
18048                   && loc->expr->dw_loc_next == NULL
18049                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18050                 loc->expr->dw_loc_oprnd1.v.val_addr
18051                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18052               else
18053                 loc_list_plus_const (loc, off);
18054             }
18055           add_AT_location_description (var_die, DW_AT_location, loc);
18056         }
18057       else if (DECL_EXTERNAL (decl))
18058         add_AT_flag (var_die, DW_AT_declaration, 1);
18059       equate_decl_number_to_die (decl, var_die);
18060       return;
18061     }
18062
18063   /* If the compiler emitted a definition for the DECL declaration
18064      and if we already emitted a DIE for it, don't emit a second
18065      DIE for it again.  */
18066   if (old_die
18067       && declaration)
18068     return;
18069
18070   /* For static data members, the declaration in the class is supposed
18071      to have DW_TAG_member tag; the specification should still be
18072      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18073   if (declaration && class_scope_p (context_die))
18074     var_die = new_die (DW_TAG_member, context_die, decl);
18075   else
18076     var_die = new_die (DW_TAG_variable, context_die, decl);
18077
18078   origin_die = NULL;
18079   if (origin != NULL)
18080     origin_die = add_abstract_origin_attribute (var_die, origin);
18081
18082   /* Loop unrolling can create multiple blocks that refer to the same
18083      static variable, so we must test for the DW_AT_declaration flag.
18084
18085      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18086      copy decls and set the DECL_ABSTRACT flag on them instead of
18087      sharing them.
18088
18089      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18090
18091      ??? The declare_in_namespace support causes us to get two DIEs for one
18092      variable, both of which are declarations.  We want to avoid considering
18093      one to be a specification, so we must test that this DIE is not a
18094      declaration.  */
18095   else if (old_die && TREE_STATIC (decl) && ! declaration
18096            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18097     {
18098       /* This is a definition of a C++ class level static.  */
18099       add_AT_specification (var_die, old_die);
18100       if (DECL_NAME (decl))
18101         {
18102           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18103           struct dwarf_file_data * file_index = lookup_filename (s.file);
18104
18105           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18106             add_AT_file (var_die, DW_AT_decl_file, file_index);
18107
18108           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18109             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18110         }
18111     }
18112   else
18113     {
18114       tree type = TREE_TYPE (decl);
18115
18116       add_name_and_src_coords_attributes (var_die, decl);
18117       if (decl_by_reference_p (decl))
18118         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18119       else
18120         add_type_attribute (var_die, type, TREE_READONLY (decl),
18121                             TREE_THIS_VOLATILE (decl), context_die);
18122
18123       if (TREE_PUBLIC (decl))
18124         add_AT_flag (var_die, DW_AT_external, 1);
18125
18126       if (DECL_ARTIFICIAL (decl))
18127         add_AT_flag (var_die, DW_AT_artificial, 1);
18128
18129       if (TREE_PROTECTED (decl))
18130         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
18131       else if (TREE_PRIVATE (decl))
18132         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
18133     }
18134
18135   if (declaration)
18136     add_AT_flag (var_die, DW_AT_declaration, 1);
18137
18138   if (decl && (DECL_ABSTRACT (decl) || declaration))
18139     equate_decl_number_to_die (decl, var_die);
18140
18141   if (! declaration
18142       && (! DECL_ABSTRACT (decl_or_origin)
18143           /* Local static vars are shared between all clones/inlines,
18144              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18145              already set.  */
18146           || (TREE_CODE (decl_or_origin) == VAR_DECL
18147               && TREE_STATIC (decl_or_origin)
18148               && DECL_RTL_SET_P (decl_or_origin)))
18149       /* When abstract origin already has DW_AT_location attribute, no need
18150          to add it again.  */
18151       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18152     {
18153       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18154           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18155         defer_location (decl_or_origin, var_die);
18156       else
18157         add_location_or_const_value_attribute (var_die,
18158                                                decl_or_origin,
18159                                                DW_AT_location);
18160       add_pubname (decl_or_origin, var_die);
18161     }
18162   else
18163     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18164 }
18165
18166 /* Generate a DIE to represent a named constant.  */
18167
18168 static void
18169 gen_const_die (tree decl, dw_die_ref context_die)
18170 {
18171   dw_die_ref const_die;
18172   tree type = TREE_TYPE (decl);
18173
18174   const_die = new_die (DW_TAG_constant, context_die, decl);
18175   add_name_and_src_coords_attributes (const_die, decl);
18176   add_type_attribute (const_die, type, 1, 0, context_die);
18177   if (TREE_PUBLIC (decl))
18178     add_AT_flag (const_die, DW_AT_external, 1);
18179   if (DECL_ARTIFICIAL (decl))
18180     add_AT_flag (const_die, DW_AT_artificial, 1);
18181   tree_add_const_value_attribute_for_decl (const_die, decl);
18182 }
18183
18184 /* Generate a DIE to represent a label identifier.  */
18185
18186 static void
18187 gen_label_die (tree decl, dw_die_ref context_die)
18188 {
18189   tree origin = decl_ultimate_origin (decl);
18190   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18191   rtx insn;
18192   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18193
18194   if (origin != NULL)
18195     add_abstract_origin_attribute (lbl_die, origin);
18196   else
18197     add_name_and_src_coords_attributes (lbl_die, decl);
18198
18199   if (DECL_ABSTRACT (decl))
18200     equate_decl_number_to_die (decl, lbl_die);
18201   else
18202     {
18203       insn = DECL_RTL_IF_SET (decl);
18204
18205       /* Deleted labels are programmer specified labels which have been
18206          eliminated because of various optimizations.  We still emit them
18207          here so that it is possible to put breakpoints on them.  */
18208       if (insn
18209           && (LABEL_P (insn)
18210               || ((NOTE_P (insn)
18211                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18212         {
18213           /* When optimization is enabled (via -O) some parts of the compiler
18214              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18215              represent source-level labels which were explicitly declared by
18216              the user.  This really shouldn't be happening though, so catch
18217              it if it ever does happen.  */
18218           gcc_assert (!INSN_DELETED_P (insn));
18219
18220           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18221           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18222         }
18223     }
18224 }
18225
18226 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18227    attributes to the DIE for a block STMT, to describe where the inlined
18228    function was called from.  This is similar to add_src_coords_attributes.  */
18229
18230 static inline void
18231 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18232 {
18233   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18234
18235   if (dwarf_version >= 3 || !dwarf_strict)
18236     {
18237       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18238       add_AT_unsigned (die, DW_AT_call_line, s.line);
18239     }
18240 }
18241
18242
18243 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18244    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18245
18246 static inline void
18247 add_high_low_attributes (tree stmt, dw_die_ref die)
18248 {
18249   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18250
18251   if (BLOCK_FRAGMENT_CHAIN (stmt)
18252       && (dwarf_version >= 3 || !dwarf_strict))
18253     {
18254       tree chain;
18255
18256       if (inlined_function_outer_scope_p (stmt))
18257         {
18258           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18259                                        BLOCK_NUMBER (stmt));
18260           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18261         }
18262
18263       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18264
18265       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18266       do
18267         {
18268           add_ranges (chain);
18269           chain = BLOCK_FRAGMENT_CHAIN (chain);
18270         }
18271       while (chain);
18272       add_ranges (NULL);
18273     }
18274   else
18275     {
18276       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18277                                    BLOCK_NUMBER (stmt));
18278       add_AT_lbl_id (die, DW_AT_low_pc, label);
18279       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18280                                    BLOCK_NUMBER (stmt));
18281       add_AT_lbl_id (die, DW_AT_high_pc, label);
18282     }
18283 }
18284
18285 /* Generate a DIE for a lexical block.  */
18286
18287 static void
18288 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18289 {
18290   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18291
18292   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18293     add_high_low_attributes (stmt, stmt_die);
18294
18295   decls_for_scope (stmt, stmt_die, depth);
18296 }
18297
18298 /* Generate a DIE for an inlined subprogram.  */
18299
18300 static void
18301 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18302 {
18303   tree decl;
18304
18305   /* The instance of function that is effectively being inlined shall not
18306      be abstract.  */
18307   gcc_assert (! BLOCK_ABSTRACT (stmt));
18308
18309   decl = block_ultimate_origin (stmt);
18310
18311   /* Emit info for the abstract instance first, if we haven't yet.  We
18312      must emit this even if the block is abstract, otherwise when we
18313      emit the block below (or elsewhere), we may end up trying to emit
18314      a die whose origin die hasn't been emitted, and crashing.  */
18315   dwarf2out_abstract_function (decl);
18316
18317   if (! BLOCK_ABSTRACT (stmt))
18318     {
18319       dw_die_ref subr_die
18320         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18321
18322       add_abstract_origin_attribute (subr_die, decl);
18323       if (TREE_ASM_WRITTEN (stmt))
18324         add_high_low_attributes (stmt, subr_die);
18325       add_call_src_coords_attributes (stmt, subr_die);
18326
18327       decls_for_scope (stmt, subr_die, depth);
18328       current_function_has_inlines = 1;
18329     }
18330 }
18331
18332 /* Generate a DIE for a field in a record, or structure.  */
18333
18334 static void
18335 gen_field_die (tree decl, dw_die_ref context_die)
18336 {
18337   dw_die_ref decl_die;
18338
18339   if (TREE_TYPE (decl) == error_mark_node)
18340     return;
18341
18342   decl_die = new_die (DW_TAG_member, context_die, decl);
18343   add_name_and_src_coords_attributes (decl_die, decl);
18344   add_type_attribute (decl_die, member_declared_type (decl),
18345                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18346                       context_die);
18347
18348   if (DECL_BIT_FIELD_TYPE (decl))
18349     {
18350       add_byte_size_attribute (decl_die, decl);
18351       add_bit_size_attribute (decl_die, decl);
18352       add_bit_offset_attribute (decl_die, decl);
18353     }
18354
18355   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18356     add_data_member_location_attribute (decl_die, decl);
18357
18358   if (DECL_ARTIFICIAL (decl))
18359     add_AT_flag (decl_die, DW_AT_artificial, 1);
18360
18361   if (TREE_PROTECTED (decl))
18362     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
18363   else if (TREE_PRIVATE (decl))
18364     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
18365
18366   /* Equate decl number to die, so that we can look up this decl later on.  */
18367   equate_decl_number_to_die (decl, decl_die);
18368 }
18369
18370 #if 0
18371 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18372    Use modified_type_die instead.
18373    We keep this code here just in case these types of DIEs may be needed to
18374    represent certain things in other languages (e.g. Pascal) someday.  */
18375
18376 static void
18377 gen_pointer_type_die (tree type, dw_die_ref context_die)
18378 {
18379   dw_die_ref ptr_die
18380     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18381
18382   equate_type_number_to_die (type, ptr_die);
18383   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18384   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18385 }
18386
18387 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18388    Use modified_type_die instead.
18389    We keep this code here just in case these types of DIEs may be needed to
18390    represent certain things in other languages (e.g. Pascal) someday.  */
18391
18392 static void
18393 gen_reference_type_die (tree type, dw_die_ref context_die)
18394 {
18395   dw_die_ref ref_die
18396     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
18397
18398   equate_type_number_to_die (type, ref_die);
18399   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18400   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18401 }
18402 #endif
18403
18404 /* Generate a DIE for a pointer to a member type.  */
18405
18406 static void
18407 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18408 {
18409   dw_die_ref ptr_die
18410     = new_die (DW_TAG_ptr_to_member_type,
18411                scope_die_for (type, context_die), type);
18412
18413   equate_type_number_to_die (type, ptr_die);
18414   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18415                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18416   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18417 }
18418
18419 /* Generate the DIE for the compilation unit.  */
18420
18421 static dw_die_ref
18422 gen_compile_unit_die (const char *filename)
18423 {
18424   dw_die_ref die;
18425   char producer[250];
18426   const char *language_string = lang_hooks.name;
18427   int language;
18428
18429   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18430
18431   if (filename)
18432     {
18433       add_name_attribute (die, filename);
18434       /* Don't add cwd for <built-in>.  */
18435       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18436         add_comp_dir_attribute (die);
18437     }
18438
18439   sprintf (producer, "%s %s", language_string, version_string);
18440
18441 #ifdef MIPS_DEBUGGING_INFO
18442   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18443      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18444      not appear in the producer string, the debugger reaches the conclusion
18445      that the object file is stripped and has no debugging information.
18446      To get the MIPS/SGI debugger to believe that there is debugging
18447      information in the object file, we add a -g to the producer string.  */
18448   if (debug_info_level > DINFO_LEVEL_TERSE)
18449     strcat (producer, " -g");
18450 #endif
18451
18452   add_AT_string (die, DW_AT_producer, producer);
18453
18454   language = DW_LANG_C89;
18455   if (strcmp (language_string, "GNU C++") == 0)
18456     language = DW_LANG_C_plus_plus;
18457   else if (strcmp (language_string, "GNU F77") == 0)
18458     language = DW_LANG_Fortran77;
18459   else if (strcmp (language_string, "GNU Pascal") == 0)
18460     language = DW_LANG_Pascal83;
18461   else if (dwarf_version >= 3 || !dwarf_strict)
18462     {
18463       if (strcmp (language_string, "GNU Ada") == 0)
18464         language = DW_LANG_Ada95;
18465       else if (strcmp (language_string, "GNU Fortran") == 0)
18466         language = DW_LANG_Fortran95;
18467       else if (strcmp (language_string, "GNU Java") == 0)
18468         language = DW_LANG_Java;
18469       else if (strcmp (language_string, "GNU Objective-C") == 0)
18470         language = DW_LANG_ObjC;
18471       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18472         language = DW_LANG_ObjC_plus_plus;
18473     }
18474
18475   add_AT_unsigned (die, DW_AT_language, language);
18476   return die;
18477 }
18478
18479 /* Generate the DIE for a base class.  */
18480
18481 static void
18482 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18483 {
18484   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18485
18486   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18487   add_data_member_location_attribute (die, binfo);
18488
18489   if (BINFO_VIRTUAL_P (binfo))
18490     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18491
18492   if (access == access_public_node)
18493     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18494   else if (access == access_protected_node)
18495     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18496 }
18497
18498 /* Generate a DIE for a class member.  */
18499
18500 static void
18501 gen_member_die (tree type, dw_die_ref context_die)
18502 {
18503   tree member;
18504   tree binfo = TYPE_BINFO (type);
18505   dw_die_ref child;
18506
18507   /* If this is not an incomplete type, output descriptions of each of its
18508      members. Note that as we output the DIEs necessary to represent the
18509      members of this record or union type, we will also be trying to output
18510      DIEs to represent the *types* of those members. However the `type'
18511      function (above) will specifically avoid generating type DIEs for member
18512      types *within* the list of member DIEs for this (containing) type except
18513      for those types (of members) which are explicitly marked as also being
18514      members of this (containing) type themselves.  The g++ front- end can
18515      force any given type to be treated as a member of some other (containing)
18516      type by setting the TYPE_CONTEXT of the given (member) type to point to
18517      the TREE node representing the appropriate (containing) type.  */
18518
18519   /* First output info about the base classes.  */
18520   if (binfo)
18521     {
18522       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18523       int i;
18524       tree base;
18525
18526       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18527         gen_inheritance_die (base,
18528                              (accesses ? VEC_index (tree, accesses, i)
18529                               : access_public_node), context_die);
18530     }
18531
18532   /* Now output info about the data members and type members.  */
18533   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
18534     {
18535       /* If we thought we were generating minimal debug info for TYPE
18536          and then changed our minds, some of the member declarations
18537          may have already been defined.  Don't define them again, but
18538          do put them in the right order.  */
18539
18540       child = lookup_decl_die (member);
18541       if (child)
18542         splice_child_die (context_die, child);
18543       else
18544         gen_decl_die (member, NULL, context_die);
18545     }
18546
18547   /* Now output info about the function members (if any).  */
18548   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
18549     {
18550       /* Don't include clones in the member list.  */
18551       if (DECL_ABSTRACT_ORIGIN (member))
18552         continue;
18553
18554       child = lookup_decl_die (member);
18555       if (child)
18556         splice_child_die (context_die, child);
18557       else
18558         gen_decl_die (member, NULL, context_die);
18559     }
18560 }
18561
18562 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18563    is set, we pretend that the type was never defined, so we only get the
18564    member DIEs needed by later specification DIEs.  */
18565
18566 static void
18567 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18568                                 enum debug_info_usage usage)
18569 {
18570   dw_die_ref type_die = lookup_type_die (type);
18571   dw_die_ref scope_die = 0;
18572   int nested = 0;
18573   int complete = (TYPE_SIZE (type)
18574                   && (! TYPE_STUB_DECL (type)
18575                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18576   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18577   complete = complete && should_emit_struct_debug (type, usage);
18578
18579   if (type_die && ! complete)
18580     return;
18581
18582   if (TYPE_CONTEXT (type) != NULL_TREE
18583       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18584           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18585     nested = 1;
18586
18587   scope_die = scope_die_for (type, context_die);
18588
18589   if (! type_die || (nested && scope_die == comp_unit_die))
18590     /* First occurrence of type or toplevel definition of nested class.  */
18591     {
18592       dw_die_ref old_die = type_die;
18593
18594       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18595                           ? record_type_tag (type) : DW_TAG_union_type,
18596                           scope_die, type);
18597       equate_type_number_to_die (type, type_die);
18598       if (old_die)
18599         add_AT_specification (type_die, old_die);
18600       else
18601         add_name_attribute (type_die, type_tag (type));
18602     }
18603   else
18604     remove_AT (type_die, DW_AT_declaration);
18605
18606   /* Generate child dies for template paramaters.  */
18607   if (debug_info_level > DINFO_LEVEL_TERSE
18608       && COMPLETE_TYPE_P (type))
18609     gen_generic_params_dies (type);
18610
18611   /* If this type has been completed, then give it a byte_size attribute and
18612      then give a list of members.  */
18613   if (complete && !ns_decl)
18614     {
18615       /* Prevent infinite recursion in cases where the type of some member of
18616          this type is expressed in terms of this type itself.  */
18617       TREE_ASM_WRITTEN (type) = 1;
18618       add_byte_size_attribute (type_die, type);
18619       if (TYPE_STUB_DECL (type) != NULL_TREE)
18620         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18621
18622       /* If the first reference to this type was as the return type of an
18623          inline function, then it may not have a parent.  Fix this now.  */
18624       if (type_die->die_parent == NULL)
18625         add_child_die (scope_die, type_die);
18626
18627       push_decl_scope (type);
18628       gen_member_die (type, type_die);
18629       pop_decl_scope ();
18630
18631       /* GNU extension: Record what type our vtable lives in.  */
18632       if (TYPE_VFIELD (type))
18633         {
18634           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18635
18636           gen_type_die (vtype, context_die);
18637           add_AT_die_ref (type_die, DW_AT_containing_type,
18638                           lookup_type_die (vtype));
18639         }
18640     }
18641   else
18642     {
18643       add_AT_flag (type_die, DW_AT_declaration, 1);
18644
18645       /* We don't need to do this for function-local types.  */
18646       if (TYPE_STUB_DECL (type)
18647           && ! decl_function_context (TYPE_STUB_DECL (type)))
18648         VEC_safe_push (tree, gc, incomplete_types, type);
18649     }
18650
18651   if (get_AT (type_die, DW_AT_name))
18652     add_pubtype (type, type_die);
18653 }
18654
18655 /* Generate a DIE for a subroutine _type_.  */
18656
18657 static void
18658 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18659 {
18660   tree return_type = TREE_TYPE (type);
18661   dw_die_ref subr_die
18662     = new_die (DW_TAG_subroutine_type,
18663                scope_die_for (type, context_die), type);
18664
18665   equate_type_number_to_die (type, subr_die);
18666   add_prototyped_attribute (subr_die, type);
18667   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18668   gen_formal_types_die (type, subr_die);
18669
18670   if (get_AT (subr_die, DW_AT_name))
18671     add_pubtype (type, subr_die);
18672 }
18673
18674 /* Generate a DIE for a type definition.  */
18675
18676 static void
18677 gen_typedef_die (tree decl, dw_die_ref context_die)
18678 {
18679   dw_die_ref type_die;
18680   tree origin;
18681
18682   if (TREE_ASM_WRITTEN (decl))
18683     return;
18684
18685   TREE_ASM_WRITTEN (decl) = 1;
18686   type_die = new_die (DW_TAG_typedef, context_die, decl);
18687   origin = decl_ultimate_origin (decl);
18688   if (origin != NULL)
18689     add_abstract_origin_attribute (type_die, origin);
18690   else
18691     {
18692       tree type;
18693
18694       add_name_and_src_coords_attributes (type_die, decl);
18695       if (DECL_ORIGINAL_TYPE (decl))
18696         {
18697           type = DECL_ORIGINAL_TYPE (decl);
18698
18699           gcc_assert (type != TREE_TYPE (decl));
18700           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18701         }
18702       else
18703         type = TREE_TYPE (decl);
18704
18705       add_type_attribute (type_die, type, TREE_READONLY (decl),
18706                           TREE_THIS_VOLATILE (decl), context_die);
18707     }
18708
18709   if (DECL_ABSTRACT (decl))
18710     equate_decl_number_to_die (decl, type_die);
18711
18712   if (get_AT (type_die, DW_AT_name))
18713     add_pubtype (decl, type_die);
18714 }
18715
18716 /* Generate a type description DIE.  */
18717
18718 static void
18719 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18720                                 enum debug_info_usage usage)
18721 {
18722   int need_pop;
18723   struct array_descr_info info;
18724
18725   if (type == NULL_TREE || type == error_mark_node)
18726     return;
18727
18728   /* If TYPE is a typedef type variant, let's generate debug info
18729      for the parent typedef which TYPE is a type of.  */
18730   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18731       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18732     {
18733       if (TREE_ASM_WRITTEN (type))
18734         return;
18735
18736       /* Prevent broken recursion; we can't hand off to the same type.  */
18737       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18738
18739       /* Use the DIE of the containing namespace as the parent DIE of
18740          the type description DIE we want to generate.  */
18741       if (DECL_CONTEXT (TYPE_NAME (type))
18742           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18743         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18744
18745       TREE_ASM_WRITTEN (type) = 1;
18746       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18747       return;
18748     }
18749
18750   /* If this is an array type with hidden descriptor, handle it first.  */
18751   if (!TREE_ASM_WRITTEN (type)
18752       && lang_hooks.types.get_array_descr_info
18753       && lang_hooks.types.get_array_descr_info (type, &info)
18754       && (dwarf_version >= 3 || !dwarf_strict))
18755     {
18756       gen_descr_array_type_die (type, &info, context_die);
18757       TREE_ASM_WRITTEN (type) = 1;
18758       return;
18759     }
18760
18761   /* We are going to output a DIE to represent the unqualified version
18762      of this type (i.e. without any const or volatile qualifiers) so
18763      get the main variant (i.e. the unqualified version) of this type
18764      now.  (Vectors are special because the debugging info is in the
18765      cloned type itself).  */
18766   if (TREE_CODE (type) != VECTOR_TYPE)
18767     type = type_main_variant (type);
18768
18769   if (TREE_ASM_WRITTEN (type))
18770     return;
18771
18772   switch (TREE_CODE (type))
18773     {
18774     case ERROR_MARK:
18775       break;
18776
18777     case POINTER_TYPE:
18778     case REFERENCE_TYPE:
18779       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18780          ensures that the gen_type_die recursion will terminate even if the
18781          type is recursive.  Recursive types are possible in Ada.  */
18782       /* ??? We could perhaps do this for all types before the switch
18783          statement.  */
18784       TREE_ASM_WRITTEN (type) = 1;
18785
18786       /* For these types, all that is required is that we output a DIE (or a
18787          set of DIEs) to represent the "basis" type.  */
18788       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18789                                 DINFO_USAGE_IND_USE);
18790       break;
18791
18792     case OFFSET_TYPE:
18793       /* This code is used for C++ pointer-to-data-member types.
18794          Output a description of the relevant class type.  */
18795       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18796                                         DINFO_USAGE_IND_USE);
18797
18798       /* Output a description of the type of the object pointed to.  */
18799       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18800                                         DINFO_USAGE_IND_USE);
18801
18802       /* Now output a DIE to represent this pointer-to-data-member type
18803          itself.  */
18804       gen_ptr_to_mbr_type_die (type, context_die);
18805       break;
18806
18807     case FUNCTION_TYPE:
18808       /* Force out return type (in case it wasn't forced out already).  */
18809       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18810                                         DINFO_USAGE_DIR_USE);
18811       gen_subroutine_type_die (type, context_die);
18812       break;
18813
18814     case METHOD_TYPE:
18815       /* Force out return type (in case it wasn't forced out already).  */
18816       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18817                                         DINFO_USAGE_DIR_USE);
18818       gen_subroutine_type_die (type, context_die);
18819       break;
18820
18821     case ARRAY_TYPE:
18822       gen_array_type_die (type, context_die);
18823       break;
18824
18825     case VECTOR_TYPE:
18826       gen_array_type_die (type, context_die);
18827       break;
18828
18829     case ENUMERAL_TYPE:
18830     case RECORD_TYPE:
18831     case UNION_TYPE:
18832     case QUAL_UNION_TYPE:
18833       /* If this is a nested type whose containing class hasn't been written
18834          out yet, writing it out will cover this one, too.  This does not apply
18835          to instantiations of member class templates; they need to be added to
18836          the containing class as they are generated.  FIXME: This hurts the
18837          idea of combining type decls from multiple TUs, since we can't predict
18838          what set of template instantiations we'll get.  */
18839       if (TYPE_CONTEXT (type)
18840           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18841           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18842         {
18843           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18844
18845           if (TREE_ASM_WRITTEN (type))
18846             return;
18847
18848           /* If that failed, attach ourselves to the stub.  */
18849           push_decl_scope (TYPE_CONTEXT (type));
18850           context_die = lookup_type_die (TYPE_CONTEXT (type));
18851           need_pop = 1;
18852         }
18853       else if (TYPE_CONTEXT (type) != NULL_TREE
18854                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18855         {
18856           /* If this type is local to a function that hasn't been written
18857              out yet, use a NULL context for now; it will be fixed up in
18858              decls_for_scope.  */
18859           context_die = lookup_decl_die (TYPE_CONTEXT (type));
18860           need_pop = 0;
18861         }
18862       else
18863         {
18864           context_die = declare_in_namespace (type, context_die);
18865           need_pop = 0;
18866         }
18867
18868       if (TREE_CODE (type) == ENUMERAL_TYPE)
18869         {
18870           /* This might have been written out by the call to
18871              declare_in_namespace.  */
18872           if (!TREE_ASM_WRITTEN (type))
18873             gen_enumeration_type_die (type, context_die);
18874         }
18875       else
18876         gen_struct_or_union_type_die (type, context_die, usage);
18877
18878       if (need_pop)
18879         pop_decl_scope ();
18880
18881       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18882          it up if it is ever completed.  gen_*_type_die will set it for us
18883          when appropriate.  */
18884       return;
18885
18886     case VOID_TYPE:
18887     case INTEGER_TYPE:
18888     case REAL_TYPE:
18889     case FIXED_POINT_TYPE:
18890     case COMPLEX_TYPE:
18891     case BOOLEAN_TYPE:
18892       /* No DIEs needed for fundamental types.  */
18893       break;
18894
18895     case LANG_TYPE:
18896       /* No Dwarf representation currently defined.  */
18897       break;
18898
18899     default:
18900       gcc_unreachable ();
18901     }
18902
18903   TREE_ASM_WRITTEN (type) = 1;
18904 }
18905
18906 static void
18907 gen_type_die (tree type, dw_die_ref context_die)
18908 {
18909   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18910 }
18911
18912 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18913    things which are local to the given block.  */
18914
18915 static void
18916 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18917 {
18918   int must_output_die = 0;
18919   bool inlined_func;
18920
18921   /* Ignore blocks that are NULL.  */
18922   if (stmt == NULL_TREE)
18923     return;
18924
18925   inlined_func = inlined_function_outer_scope_p (stmt);
18926
18927   /* If the block is one fragment of a non-contiguous block, do not
18928      process the variables, since they will have been done by the
18929      origin block.  Do process subblocks.  */
18930   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18931     {
18932       tree sub;
18933
18934       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18935         gen_block_die (sub, context_die, depth + 1);
18936
18937       return;
18938     }
18939
18940   /* Determine if we need to output any Dwarf DIEs at all to represent this
18941      block.  */
18942   if (inlined_func)
18943     /* The outer scopes for inlinings *must* always be represented.  We
18944        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18945     must_output_die = 1;
18946   else
18947     {
18948       /* Determine if this block directly contains any "significant"
18949          local declarations which we will need to output DIEs for.  */
18950       if (debug_info_level > DINFO_LEVEL_TERSE)
18951         /* We are not in terse mode so *any* local declaration counts
18952            as being a "significant" one.  */
18953         must_output_die = ((BLOCK_VARS (stmt) != NULL
18954                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18955                            && (TREE_USED (stmt)
18956                                || TREE_ASM_WRITTEN (stmt)
18957                                || BLOCK_ABSTRACT (stmt)));
18958       else if ((TREE_USED (stmt)
18959                 || TREE_ASM_WRITTEN (stmt)
18960                 || BLOCK_ABSTRACT (stmt))
18961                && !dwarf2out_ignore_block (stmt))
18962         must_output_die = 1;
18963     }
18964
18965   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18966      DIE for any block which contains no significant local declarations at
18967      all.  Rather, in such cases we just call `decls_for_scope' so that any
18968      needed Dwarf info for any sub-blocks will get properly generated. Note
18969      that in terse mode, our definition of what constitutes a "significant"
18970      local declaration gets restricted to include only inlined function
18971      instances and local (nested) function definitions.  */
18972   if (must_output_die)
18973     {
18974       if (inlined_func)
18975         {
18976           /* If STMT block is abstract, that means we have been called
18977              indirectly from dwarf2out_abstract_function.
18978              That function rightfully marks the descendent blocks (of
18979              the abstract function it is dealing with) as being abstract,
18980              precisely to prevent us from emitting any
18981              DW_TAG_inlined_subroutine DIE as a descendent
18982              of an abstract function instance. So in that case, we should
18983              not call gen_inlined_subroutine_die.
18984
18985              Later though, when cgraph asks dwarf2out to emit info
18986              for the concrete instance of the function decl into which
18987              the concrete instance of STMT got inlined, the later will lead
18988              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18989           if (! BLOCK_ABSTRACT (stmt))
18990             gen_inlined_subroutine_die (stmt, context_die, depth);
18991         }
18992       else
18993         gen_lexical_block_die (stmt, context_die, depth);
18994     }
18995   else
18996     decls_for_scope (stmt, context_die, depth);
18997 }
18998
18999 /* Process variable DECL (or variable with origin ORIGIN) within
19000    block STMT and add it to CONTEXT_DIE.  */
19001 static void
19002 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19003 {
19004   dw_die_ref die;
19005   tree decl_or_origin = decl ? decl : origin;
19006   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
19007
19008   if (ultimate_origin)
19009     origin = ultimate_origin;
19010
19011   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19012     die = lookup_decl_die (decl_or_origin);
19013   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19014            && TYPE_DECL_IS_STUB (decl_or_origin))
19015     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19016   else
19017     die = NULL;
19018
19019   if (die != NULL && die->die_parent == NULL)
19020     add_child_die (context_die, die);
19021   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19022     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19023                                          stmt, context_die);
19024   else
19025     gen_decl_die (decl, origin, context_die);
19026 }
19027
19028 /* Generate all of the decls declared within a given scope and (recursively)
19029    all of its sub-blocks.  */
19030
19031 static void
19032 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19033 {
19034   tree decl;
19035   unsigned int i;
19036   tree subblocks;
19037
19038   /* Ignore NULL blocks.  */
19039   if (stmt == NULL_TREE)
19040     return;
19041
19042   /* Output the DIEs to represent all of the data objects and typedefs
19043      declared directly within this block but not within any nested
19044      sub-blocks.  Also, nested function and tag DIEs have been
19045      generated with a parent of NULL; fix that up now.  */
19046   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
19047     process_scope_var (stmt, decl, NULL_TREE, context_die);
19048   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19049     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19050                        context_die);
19051
19052   /* If we're at -g1, we're not interested in subblocks.  */
19053   if (debug_info_level <= DINFO_LEVEL_TERSE)
19054     return;
19055
19056   /* Output the DIEs to represent all sub-blocks (and the items declared
19057      therein) of this block.  */
19058   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19059        subblocks != NULL;
19060        subblocks = BLOCK_CHAIN (subblocks))
19061     gen_block_die (subblocks, context_die, depth + 1);
19062 }
19063
19064 /* Is this a typedef we can avoid emitting?  */
19065
19066 static inline int
19067 is_redundant_typedef (const_tree decl)
19068 {
19069   if (TYPE_DECL_IS_STUB (decl))
19070     return 1;
19071
19072   if (DECL_ARTIFICIAL (decl)
19073       && DECL_CONTEXT (decl)
19074       && is_tagged_type (DECL_CONTEXT (decl))
19075       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19076       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19077     /* Also ignore the artificial member typedef for the class name.  */
19078     return 1;
19079
19080   return 0;
19081 }
19082
19083 /* Returns the DIE for a context.  */
19084
19085 static inline dw_die_ref
19086 get_context_die (tree context)
19087 {
19088   if (context)
19089     {
19090       /* Find die that represents this context.  */
19091       if (TYPE_P (context))
19092         return force_type_die (TYPE_MAIN_VARIANT (context));
19093       else
19094         return force_decl_die (context);
19095     }
19096   return comp_unit_die;
19097 }
19098
19099 /* Returns the DIE for decl.  A DIE will always be returned.  */
19100
19101 static dw_die_ref
19102 force_decl_die (tree decl)
19103 {
19104   dw_die_ref decl_die;
19105   unsigned saved_external_flag;
19106   tree save_fn = NULL_TREE;
19107   decl_die = lookup_decl_die (decl);
19108   if (!decl_die)
19109     {
19110       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19111
19112       decl_die = lookup_decl_die (decl);
19113       if (decl_die)
19114         return decl_die;
19115
19116       switch (TREE_CODE (decl))
19117         {
19118         case FUNCTION_DECL:
19119           /* Clear current_function_decl, so that gen_subprogram_die thinks
19120              that this is a declaration. At this point, we just want to force
19121              declaration die.  */
19122           save_fn = current_function_decl;
19123           current_function_decl = NULL_TREE;
19124           gen_subprogram_die (decl, context_die);
19125           current_function_decl = save_fn;
19126           break;
19127
19128         case VAR_DECL:
19129           /* Set external flag to force declaration die. Restore it after
19130            gen_decl_die() call.  */
19131           saved_external_flag = DECL_EXTERNAL (decl);
19132           DECL_EXTERNAL (decl) = 1;
19133           gen_decl_die (decl, NULL, context_die);
19134           DECL_EXTERNAL (decl) = saved_external_flag;
19135           break;
19136
19137         case NAMESPACE_DECL:
19138           if (dwarf_version >= 3 || !dwarf_strict)
19139             dwarf2out_decl (decl);
19140           else
19141             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19142             decl_die = comp_unit_die;
19143           break;
19144
19145         default:
19146           gcc_unreachable ();
19147         }
19148
19149       /* We should be able to find the DIE now.  */
19150       if (!decl_die)
19151         decl_die = lookup_decl_die (decl);
19152       gcc_assert (decl_die);
19153     }
19154
19155   return decl_die;
19156 }
19157
19158 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19159    always returned.  */
19160
19161 static dw_die_ref
19162 force_type_die (tree type)
19163 {
19164   dw_die_ref type_die;
19165
19166   type_die = lookup_type_die (type);
19167   if (!type_die)
19168     {
19169       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19170
19171       type_die = modified_type_die (type, TYPE_READONLY (type),
19172                                     TYPE_VOLATILE (type), context_die);
19173       gcc_assert (type_die);
19174     }
19175   return type_die;
19176 }
19177
19178 /* Force out any required namespaces to be able to output DECL,
19179    and return the new context_die for it, if it's changed.  */
19180
19181 static dw_die_ref
19182 setup_namespace_context (tree thing, dw_die_ref context_die)
19183 {
19184   tree context = (DECL_P (thing)
19185                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19186   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19187     /* Force out the namespace.  */
19188     context_die = force_decl_die (context);
19189
19190   return context_die;
19191 }
19192
19193 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19194    type) within its namespace, if appropriate.
19195
19196    For compatibility with older debuggers, namespace DIEs only contain
19197    declarations; all definitions are emitted at CU scope.  */
19198
19199 static dw_die_ref
19200 declare_in_namespace (tree thing, dw_die_ref context_die)
19201 {
19202   dw_die_ref ns_context;
19203
19204   if (debug_info_level <= DINFO_LEVEL_TERSE)
19205     return context_die;
19206
19207   /* If this decl is from an inlined function, then don't try to emit it in its
19208      namespace, as we will get confused.  It would have already been emitted
19209      when the abstract instance of the inline function was emitted anyways.  */
19210   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19211     return context_die;
19212
19213   ns_context = setup_namespace_context (thing, context_die);
19214
19215   if (ns_context != context_die)
19216     {
19217       if (is_fortran ())
19218         return ns_context;
19219       if (DECL_P (thing))
19220         gen_decl_die (thing, NULL, ns_context);
19221       else
19222         gen_type_die (thing, ns_context);
19223     }
19224   return context_die;
19225 }
19226
19227 /* Generate a DIE for a namespace or namespace alias.  */
19228
19229 static void
19230 gen_namespace_die (tree decl, dw_die_ref context_die)
19231 {
19232   dw_die_ref namespace_die;
19233
19234   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19235      they are an alias of.  */
19236   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19237     {
19238       /* Output a real namespace or module.  */
19239       context_die = setup_namespace_context (decl, comp_unit_die);
19240       namespace_die = new_die (is_fortran ()
19241                                ? DW_TAG_module : DW_TAG_namespace,
19242                                context_die, decl);
19243       /* For Fortran modules defined in different CU don't add src coords.  */
19244       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19245         {
19246           const char *name = dwarf2_name (decl, 0);
19247           if (name)
19248             add_name_attribute (namespace_die, name);
19249         }
19250       else
19251         add_name_and_src_coords_attributes (namespace_die, decl);
19252       if (DECL_EXTERNAL (decl))
19253         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19254       equate_decl_number_to_die (decl, namespace_die);
19255     }
19256   else
19257     {
19258       /* Output a namespace alias.  */
19259
19260       /* Force out the namespace we are an alias of, if necessary.  */
19261       dw_die_ref origin_die
19262         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19263
19264       if (DECL_CONTEXT (decl) == NULL_TREE
19265           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19266         context_die = setup_namespace_context (decl, comp_unit_die);
19267       /* Now create the namespace alias DIE.  */
19268       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19269       add_name_and_src_coords_attributes (namespace_die, decl);
19270       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19271       equate_decl_number_to_die (decl, namespace_die);
19272     }
19273 }
19274
19275 /* Generate Dwarf debug information for a decl described by DECL.  */
19276
19277 static void
19278 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19279 {
19280   tree decl_or_origin = decl ? decl : origin;
19281   tree class_origin = NULL;
19282
19283   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19284     return;
19285
19286   switch (TREE_CODE (decl_or_origin))
19287     {
19288     case ERROR_MARK:
19289       break;
19290
19291     case CONST_DECL:
19292       if (!is_fortran ())
19293         {
19294           /* The individual enumerators of an enum type get output when we output
19295              the Dwarf representation of the relevant enum type itself.  */
19296           break;
19297         }
19298
19299       /* Emit its type.  */
19300       gen_type_die (TREE_TYPE (decl), context_die);
19301
19302       /* And its containing namespace.  */
19303       context_die = declare_in_namespace (decl, context_die);
19304
19305       gen_const_die (decl, context_die);
19306       break;
19307
19308     case FUNCTION_DECL:
19309       /* Don't output any DIEs to represent mere function declarations,
19310          unless they are class members or explicit block externs.  */
19311       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19312           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
19313           && (current_function_decl == NULL_TREE
19314               || DECL_ARTIFICIAL (decl_or_origin)))
19315         break;
19316
19317 #if 0
19318       /* FIXME */
19319       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19320          on local redeclarations of global functions.  That seems broken.  */
19321       if (current_function_decl != decl)
19322         /* This is only a declaration.  */;
19323 #endif
19324
19325       /* If we're emitting a clone, emit info for the abstract instance.  */
19326       if (origin || DECL_ORIGIN (decl) != decl)
19327         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
19328
19329       /* If we're emitting an out-of-line copy of an inline function,
19330          emit info for the abstract instance and set up to refer to it.  */
19331       else if (cgraph_function_possibly_inlined_p (decl)
19332                && ! DECL_ABSTRACT (decl)
19333                && ! class_or_namespace_scope_p (context_die)
19334                /* dwarf2out_abstract_function won't emit a die if this is just
19335                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19336                   that case, because that works only if we have a die.  */
19337                && DECL_INITIAL (decl) != NULL_TREE)
19338         {
19339           dwarf2out_abstract_function (decl);
19340           set_decl_origin_self (decl);
19341         }
19342
19343       /* Otherwise we're emitting the primary DIE for this decl.  */
19344       else if (debug_info_level > DINFO_LEVEL_TERSE)
19345         {
19346           /* Before we describe the FUNCTION_DECL itself, make sure that we
19347              have described its return type.  */
19348           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19349
19350           /* And its virtual context.  */
19351           if (DECL_VINDEX (decl) != NULL_TREE)
19352             gen_type_die (DECL_CONTEXT (decl), context_die);
19353
19354           /* And its containing type.  */
19355           if (!origin)
19356             origin = decl_class_context (decl);
19357           if (origin != NULL_TREE)
19358             gen_type_die_for_member (origin, decl, context_die);
19359
19360           /* And its containing namespace.  */
19361           context_die = declare_in_namespace (decl, context_die);
19362         }
19363
19364       /* Now output a DIE to represent the function itself.  */
19365       if (decl)
19366         gen_subprogram_die (decl, context_die);
19367       break;
19368
19369     case TYPE_DECL:
19370       /* If we are in terse mode, don't generate any DIEs to represent any
19371          actual typedefs.  */
19372       if (debug_info_level <= DINFO_LEVEL_TERSE)
19373         break;
19374
19375       /* In the special case of a TYPE_DECL node representing the declaration
19376          of some type tag, if the given TYPE_DECL is marked as having been
19377          instantiated from some other (original) TYPE_DECL node (e.g. one which
19378          was generated within the original definition of an inline function) we
19379          used to generate a special (abbreviated) DW_TAG_structure_type,
19380          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19381          should be actually referencing those DIEs, as variable DIEs with that
19382          type would be emitted already in the abstract origin, so it was always
19383          removed during unused type prunning.  Don't add anything in this
19384          case.  */
19385       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19386         break;
19387
19388       if (is_redundant_typedef (decl))
19389         gen_type_die (TREE_TYPE (decl), context_die);
19390       else
19391         /* Output a DIE to represent the typedef itself.  */
19392         gen_typedef_die (decl, context_die);
19393       break;
19394
19395     case LABEL_DECL:
19396       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19397         gen_label_die (decl, context_die);
19398       break;
19399
19400     case VAR_DECL:
19401     case RESULT_DECL:
19402       /* If we are in terse mode, don't generate any DIEs to represent any
19403          variable declarations or definitions.  */
19404       if (debug_info_level <= DINFO_LEVEL_TERSE)
19405         break;
19406
19407       /* Output any DIEs that are needed to specify the type of this data
19408          object.  */
19409       if (decl_by_reference_p (decl_or_origin))
19410         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19411       else
19412         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19413
19414       /* And its containing type.  */
19415       class_origin = decl_class_context (decl_or_origin);
19416       if (class_origin != NULL_TREE)
19417         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19418
19419       /* And its containing namespace.  */
19420       context_die = declare_in_namespace (decl_or_origin, context_die);
19421
19422       /* Now output the DIE to represent the data object itself.  This gets
19423          complicated because of the possibility that the VAR_DECL really
19424          represents an inlined instance of a formal parameter for an inline
19425          function.  */
19426       if (!origin)
19427         origin = decl_ultimate_origin (decl);
19428       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
19429         gen_formal_parameter_die (decl, origin,
19430                                   true /* Emit name attribute.  */,
19431                                   context_die);
19432       else
19433         gen_variable_die (decl, origin, context_die);
19434       break;
19435
19436     case FIELD_DECL:
19437       /* Ignore the nameless fields that are used to skip bits but handle C++
19438          anonymous unions and structs.  */
19439       if (DECL_NAME (decl) != NULL_TREE
19440           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19441           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19442         {
19443           gen_type_die (member_declared_type (decl), context_die);
19444           gen_field_die (decl, context_die);
19445         }
19446       break;
19447
19448     case PARM_DECL:
19449       if (DECL_BY_REFERENCE (decl_or_origin))
19450         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19451       else
19452         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19453       gen_formal_parameter_die (decl, origin,
19454                                 true /* Emit name attribute.  */,
19455                                 context_die);
19456       break;
19457
19458     case NAMESPACE_DECL:
19459     case IMPORTED_DECL:
19460       if (dwarf_version >= 3 || !dwarf_strict)
19461         gen_namespace_die (decl, context_die);
19462       break;
19463
19464     default:
19465       /* Probably some frontend-internal decl.  Assume we don't care.  */
19466       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19467       break;
19468     }
19469 }
19470 \f
19471 /* Output debug information for global decl DECL.  Called from toplev.c after
19472    compilation proper has finished.  */
19473
19474 static void
19475 dwarf2out_global_decl (tree decl)
19476 {
19477   /* Output DWARF2 information for file-scope tentative data object
19478      declarations, file-scope (extern) function declarations (which
19479      had no corresponding body) and file-scope tagged type declarations
19480      and definitions which have not yet been forced out.  */
19481   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19482     dwarf2out_decl (decl);
19483 }
19484
19485 /* Output debug information for type decl DECL.  Called from toplev.c
19486    and from language front ends (to record built-in types).  */
19487 static void
19488 dwarf2out_type_decl (tree decl, int local)
19489 {
19490   if (!local)
19491     dwarf2out_decl (decl);
19492 }
19493
19494 /* Output debug information for imported module or decl DECL.
19495    NAME is non-NULL name in the lexical block if the decl has been renamed.
19496    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19497    that DECL belongs to.
19498    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19499 static void
19500 dwarf2out_imported_module_or_decl_1 (tree decl,
19501                                      tree name,
19502                                      tree lexical_block,
19503                                      dw_die_ref lexical_block_die)
19504 {
19505   expanded_location xloc;
19506   dw_die_ref imported_die = NULL;
19507   dw_die_ref at_import_die;
19508
19509   if (TREE_CODE (decl) == IMPORTED_DECL)
19510     {
19511       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19512       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19513       gcc_assert (decl);
19514     }
19515   else
19516     xloc = expand_location (input_location);
19517
19518   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19519     {
19520       if (is_base_type (TREE_TYPE (decl)))
19521         at_import_die = base_type_die (TREE_TYPE (decl));
19522       else
19523         at_import_die = force_type_die (TREE_TYPE (decl));
19524       /* For namespace N { typedef void T; } using N::T; base_type_die
19525          returns NULL, but DW_TAG_imported_declaration requires
19526          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19527       if (!at_import_die)
19528         {
19529           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19530           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19531           at_import_die = lookup_type_die (TREE_TYPE (decl));
19532           gcc_assert (at_import_die);
19533         }
19534     }
19535   else
19536     {
19537       at_import_die = lookup_decl_die (decl);
19538       if (!at_import_die)
19539         {
19540           /* If we're trying to avoid duplicate debug info, we may not have
19541              emitted the member decl for this field.  Emit it now.  */
19542           if (TREE_CODE (decl) == FIELD_DECL)
19543             {
19544               tree type = DECL_CONTEXT (decl);
19545
19546               if (TYPE_CONTEXT (type)
19547                   && TYPE_P (TYPE_CONTEXT (type))
19548                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19549                                                 DINFO_USAGE_DIR_USE))
19550                 return;
19551               gen_type_die_for_member (type, decl,
19552                                        get_context_die (TYPE_CONTEXT (type)));
19553             }
19554           at_import_die = force_decl_die (decl);
19555         }
19556     }
19557
19558   if (TREE_CODE (decl) == NAMESPACE_DECL)
19559     {
19560       if (dwarf_version >= 3 || !dwarf_strict)
19561         imported_die = new_die (DW_TAG_imported_module,
19562                                 lexical_block_die,
19563                                 lexical_block);
19564       else
19565         return;
19566     }
19567   else
19568     imported_die = new_die (DW_TAG_imported_declaration,
19569                             lexical_block_die,
19570                             lexical_block);
19571
19572   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19573   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19574   if (name)
19575     add_AT_string (imported_die, DW_AT_name,
19576                    IDENTIFIER_POINTER (name));
19577   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19578 }
19579
19580 /* Output debug information for imported module or decl DECL.
19581    NAME is non-NULL name in context if the decl has been renamed.
19582    CHILD is true if decl is one of the renamed decls as part of
19583    importing whole module.  */
19584
19585 static void
19586 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19587                                    bool child)
19588 {
19589   /* dw_die_ref at_import_die;  */
19590   dw_die_ref scope_die;
19591
19592   if (debug_info_level <= DINFO_LEVEL_TERSE)
19593     return;
19594
19595   gcc_assert (decl);
19596
19597   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19598      We need decl DIE for reference and scope die. First, get DIE for the decl
19599      itself.  */
19600
19601   /* Get the scope die for decl context. Use comp_unit_die for global module
19602      or decl. If die is not found for non globals, force new die.  */
19603   if (context
19604       && TYPE_P (context)
19605       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19606     return;
19607
19608   if (!(dwarf_version >= 3 || !dwarf_strict))
19609     return;
19610
19611   scope_die = get_context_die (context);
19612
19613   if (child)
19614     {
19615       gcc_assert (scope_die->die_child);
19616       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19617       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19618       scope_die = scope_die->die_child;
19619     }
19620
19621   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19622   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19623
19624 }
19625
19626 /* Write the debugging output for DECL.  */
19627
19628 void
19629 dwarf2out_decl (tree decl)
19630 {
19631   dw_die_ref context_die = comp_unit_die;
19632
19633   switch (TREE_CODE (decl))
19634     {
19635     case ERROR_MARK:
19636       return;
19637
19638     case FUNCTION_DECL:
19639       /* What we would really like to do here is to filter out all mere
19640          file-scope declarations of file-scope functions which are never
19641          referenced later within this translation unit (and keep all of ones
19642          that *are* referenced later on) but we aren't clairvoyant, so we have
19643          no idea which functions will be referenced in the future (i.e. later
19644          on within the current translation unit). So here we just ignore all
19645          file-scope function declarations which are not also definitions.  If
19646          and when the debugger needs to know something about these functions,
19647          it will have to hunt around and find the DWARF information associated
19648          with the definition of the function.
19649
19650          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19651          nodes represent definitions and which ones represent mere
19652          declarations.  We have to check DECL_INITIAL instead. That's because
19653          the C front-end supports some weird semantics for "extern inline"
19654          function definitions.  These can get inlined within the current
19655          translation unit (and thus, we need to generate Dwarf info for their
19656          abstract instances so that the Dwarf info for the concrete inlined
19657          instances can have something to refer to) but the compiler never
19658          generates any out-of-lines instances of such things (despite the fact
19659          that they *are* definitions).
19660
19661          The important point is that the C front-end marks these "extern
19662          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19663          them anyway. Note that the C++ front-end also plays some similar games
19664          for inline function definitions appearing within include files which
19665          also contain `#pragma interface' pragmas.  */
19666       if (DECL_INITIAL (decl) == NULL_TREE)
19667         return;
19668
19669       /* If we're a nested function, initially use a parent of NULL; if we're
19670          a plain function, this will be fixed up in decls_for_scope.  If
19671          we're a method, it will be ignored, since we already have a DIE.  */
19672       if (decl_function_context (decl)
19673           /* But if we're in terse mode, we don't care about scope.  */
19674           && debug_info_level > DINFO_LEVEL_TERSE)
19675         context_die = NULL;
19676       break;
19677
19678     case VAR_DECL:
19679       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19680          declaration and if the declaration was never even referenced from
19681          within this entire compilation unit.  We suppress these DIEs in
19682          order to save space in the .debug section (by eliminating entries
19683          which are probably useless).  Note that we must not suppress
19684          block-local extern declarations (whether used or not) because that
19685          would screw-up the debugger's name lookup mechanism and cause it to
19686          miss things which really ought to be in scope at a given point.  */
19687       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19688         return;
19689
19690       /* For local statics lookup proper context die.  */
19691       if (TREE_STATIC (decl) && decl_function_context (decl))
19692         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19693
19694       /* If we are in terse mode, don't generate any DIEs to represent any
19695          variable declarations or definitions.  */
19696       if (debug_info_level <= DINFO_LEVEL_TERSE)
19697         return;
19698       break;
19699
19700     case CONST_DECL:
19701       if (debug_info_level <= DINFO_LEVEL_TERSE)
19702         return;
19703       if (!is_fortran ())
19704         return;
19705       if (TREE_STATIC (decl) && decl_function_context (decl))
19706         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19707       break;
19708
19709     case NAMESPACE_DECL:
19710     case IMPORTED_DECL:
19711       if (debug_info_level <= DINFO_LEVEL_TERSE)
19712         return;
19713       if (lookup_decl_die (decl) != NULL)
19714         return;
19715       break;
19716
19717     case TYPE_DECL:
19718       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19719       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19720         return;
19721
19722       /* Don't bother trying to generate any DIEs to represent any of the
19723          normal built-in types for the language we are compiling.  */
19724       if (DECL_IS_BUILTIN (decl))
19725         {
19726           /* OK, we need to generate one for `bool' so GDB knows what type
19727              comparisons have.  */
19728           if (is_cxx ()
19729               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
19730               && ! DECL_IGNORED_P (decl))
19731             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
19732
19733           return;
19734         }
19735
19736       /* If we are in terse mode, don't generate any DIEs for types.  */
19737       if (debug_info_level <= DINFO_LEVEL_TERSE)
19738         return;
19739
19740       /* If we're a function-scope tag, initially use a parent of NULL;
19741          this will be fixed up in decls_for_scope.  */
19742       if (decl_function_context (decl))
19743         context_die = NULL;
19744
19745       break;
19746
19747     default:
19748       return;
19749     }
19750
19751   gen_decl_die (decl, NULL, context_die);
19752 }
19753
19754 /* Output a marker (i.e. a label) for the beginning of the generated code for
19755    a lexical block.  */
19756
19757 static void
19758 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19759                        unsigned int blocknum)
19760 {
19761   switch_to_section (current_function_section ());
19762   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19763 }
19764
19765 /* Output a marker (i.e. a label) for the end of the generated code for a
19766    lexical block.  */
19767
19768 static void
19769 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19770 {
19771   switch_to_section (current_function_section ());
19772   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19773 }
19774
19775 /* Returns nonzero if it is appropriate not to emit any debugging
19776    information for BLOCK, because it doesn't contain any instructions.
19777
19778    Don't allow this for blocks with nested functions or local classes
19779    as we would end up with orphans, and in the presence of scheduling
19780    we may end up calling them anyway.  */
19781
19782 static bool
19783 dwarf2out_ignore_block (const_tree block)
19784 {
19785   tree decl;
19786   unsigned int i;
19787
19788   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
19789     if (TREE_CODE (decl) == FUNCTION_DECL
19790         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19791       return 0;
19792   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19793     {
19794       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19795       if (TREE_CODE (decl) == FUNCTION_DECL
19796           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19797       return 0;
19798     }
19799
19800   return 1;
19801 }
19802
19803 /* Hash table routines for file_hash.  */
19804
19805 static int
19806 file_table_eq (const void *p1_p, const void *p2_p)
19807 {
19808   const struct dwarf_file_data *const p1 =
19809     (const struct dwarf_file_data *) p1_p;
19810   const char *const p2 = (const char *) p2_p;
19811   return strcmp (p1->filename, p2) == 0;
19812 }
19813
19814 static hashval_t
19815 file_table_hash (const void *p_p)
19816 {
19817   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19818   return htab_hash_string (p->filename);
19819 }
19820
19821 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19822    dwarf2out.c) and return its "index".  The index of each (known) filename is
19823    just a unique number which is associated with only that one filename.  We
19824    need such numbers for the sake of generating labels (in the .debug_sfnames
19825    section) and references to those files numbers (in the .debug_srcinfo
19826    and.debug_macinfo sections).  If the filename given as an argument is not
19827    found in our current list, add it to the list and assign it the next
19828    available unique index number.  In order to speed up searches, we remember
19829    the index of the filename was looked up last.  This handles the majority of
19830    all searches.  */
19831
19832 static struct dwarf_file_data *
19833 lookup_filename (const char *file_name)
19834 {
19835   void ** slot;
19836   struct dwarf_file_data * created;
19837
19838   /* Check to see if the file name that was searched on the previous
19839      call matches this file name.  If so, return the index.  */
19840   if (file_table_last_lookup
19841       && (file_name == file_table_last_lookup->filename
19842           || strcmp (file_table_last_lookup->filename, file_name) == 0))
19843     return file_table_last_lookup;
19844
19845   /* Didn't match the previous lookup, search the table.  */
19846   slot = htab_find_slot_with_hash (file_table, file_name,
19847                                    htab_hash_string (file_name), INSERT);
19848   if (*slot)
19849     return (struct dwarf_file_data *) *slot;
19850
19851   created = GGC_NEW (struct dwarf_file_data);
19852   created->filename = file_name;
19853   created->emitted_number = 0;
19854   *slot = created;
19855   return created;
19856 }
19857
19858 /* If the assembler will construct the file table, then translate the compiler
19859    internal file table number into the assembler file table number, and emit
19860    a .file directive if we haven't already emitted one yet.  The file table
19861    numbers are different because we prune debug info for unused variables and
19862    types, which may include filenames.  */
19863
19864 static int
19865 maybe_emit_file (struct dwarf_file_data * fd)
19866 {
19867   if (! fd->emitted_number)
19868     {
19869       if (last_emitted_file)
19870         fd->emitted_number = last_emitted_file->emitted_number + 1;
19871       else
19872         fd->emitted_number = 1;
19873       last_emitted_file = fd;
19874
19875       if (DWARF2_ASM_LINE_DEBUG_INFO)
19876         {
19877           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19878           output_quoted_string (asm_out_file,
19879                                 remap_debug_filename (fd->filename));
19880           fputc ('\n', asm_out_file);
19881         }
19882     }
19883
19884   return fd->emitted_number;
19885 }
19886
19887 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19888    That generation should happen after function debug info has been
19889    generated. The value of the attribute is the constant value of ARG.  */
19890
19891 static void
19892 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19893 {
19894   die_arg_entry entry;
19895
19896   if (!die || !arg)
19897     return;
19898
19899   if (!tmpl_value_parm_die_table)
19900     tmpl_value_parm_die_table
19901       = VEC_alloc (die_arg_entry, gc, 32);
19902
19903   entry.die = die;
19904   entry.arg = arg;
19905   VEC_safe_push (die_arg_entry, gc,
19906                  tmpl_value_parm_die_table,
19907                  &entry);
19908 }
19909
19910 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19911    by append_entry_to_tmpl_value_parm_die_table. This function must
19912    be called after function DIEs have been generated.  */
19913
19914 static void
19915 gen_remaining_tmpl_value_param_die_attribute (void)
19916 {
19917   if (tmpl_value_parm_die_table)
19918     {
19919       unsigned i;
19920       die_arg_entry *e;
19921
19922       for (i = 0;
19923            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
19924            i++)
19925         tree_add_const_value_attribute (e->die, e->arg);
19926     }
19927 }
19928
19929
19930 /* Replace DW_AT_name for the decl with name.  */
19931  
19932 static void
19933 dwarf2out_set_name (tree decl, tree name)
19934 {
19935   dw_die_ref die;
19936   dw_attr_ref attr;
19937   const char *dname;
19938
19939   die = TYPE_SYMTAB_DIE (decl);
19940   if (!die)
19941     return;
19942
19943   dname = dwarf2_name (name, 0);
19944   if (!dname)
19945     return;
19946
19947   attr = get_AT (die, DW_AT_name);
19948   if (attr)
19949     {
19950       struct indirect_string_node *node;
19951
19952       node = find_AT_string (dname);
19953       /* replace the string.  */
19954       attr->dw_attr_val.v.val_str = node;
19955     }
19956
19957   else
19958     add_name_attribute (die, dname);
19959 }
19960
19961 /* Called by the final INSN scan whenever we see a direct function call.
19962    Make an entry into the direct call table, recording the point of call
19963    and a reference to the target function's debug entry.  */
19964
19965 static void
19966 dwarf2out_direct_call (tree targ)
19967 {
19968   dcall_entry e;
19969   tree origin = decl_ultimate_origin (targ);
19970
19971   /* If this is a clone, use the abstract origin as the target.  */
19972   if (origin)
19973     targ = origin;
19974
19975   e.poc_label_num = poc_label_num++;
19976   e.poc_decl = current_function_decl;
19977   e.targ_die = force_decl_die (targ);
19978   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
19979
19980   /* Drop a label at the return point to mark the point of call.  */
19981   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
19982 }
19983
19984 /* Returns a hash value for X (which really is a struct vcall_insn).  */
19985
19986 static hashval_t
19987 vcall_insn_table_hash (const void *x)
19988 {
19989   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
19990 }
19991
19992 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
19993    insnd_uid of *Y.  */
19994
19995 static int
19996 vcall_insn_table_eq (const void *x, const void *y)
19997 {
19998   return (((const struct vcall_insn *) x)->insn_uid
19999           == ((const struct vcall_insn *) y)->insn_uid);
20000 }
20001
20002 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
20003
20004 static void
20005 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
20006 {
20007   struct vcall_insn *item = GGC_NEW (struct vcall_insn);
20008   struct vcall_insn **slot;
20009
20010   gcc_assert (item);
20011   item->insn_uid = insn_uid;
20012   item->vtable_slot = vtable_slot;
20013   slot = (struct vcall_insn **)
20014       htab_find_slot_with_hash (vcall_insn_table, &item,
20015                                 (hashval_t) insn_uid, INSERT);
20016   *slot = item;
20017 }
20018
20019 /* Return the VTABLE_SLOT associated with INSN_UID.  */
20020
20021 static unsigned int
20022 lookup_vcall_insn (unsigned int insn_uid)
20023 {
20024   struct vcall_insn item;
20025   struct vcall_insn *p;
20026
20027   item.insn_uid = insn_uid;
20028   item.vtable_slot = 0;
20029   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
20030                                                  (void *) &item,
20031                                                  (hashval_t) insn_uid);
20032   if (p == NULL)
20033     return (unsigned int) -1;
20034   return p->vtable_slot;
20035 }
20036
20037
20038 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
20039    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
20040    is the vtable slot index that we will need to put in the virtual call
20041    table later.  */
20042
20043 static void
20044 dwarf2out_virtual_call_token (tree addr, int insn_uid)
20045 {
20046   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
20047     {
20048       tree token = OBJ_TYPE_REF_TOKEN (addr);
20049       if (TREE_CODE (token) == INTEGER_CST)
20050         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
20051     }
20052 }
20053
20054 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
20055    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
20056    with NEW_INSN.  */
20057
20058 static void
20059 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
20060 {
20061   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
20062
20063   if (vtable_slot != (unsigned int) -1)
20064     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
20065 }
20066
20067 /* Called by the final INSN scan whenever we see a virtual function call.
20068    Make an entry into the virtual call table, recording the point of call
20069    and the slot index of the vtable entry used to call the virtual member
20070    function.  The slot index was associated with the INSN_UID during the
20071    lowering to RTL.  */
20072
20073 static void
20074 dwarf2out_virtual_call (int insn_uid)
20075 {
20076   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
20077   vcall_entry e;
20078
20079   if (vtable_slot == (unsigned int) -1)
20080     return;
20081
20082   e.poc_label_num = poc_label_num++;
20083   e.vtable_slot = vtable_slot;
20084   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
20085
20086   /* Drop a label at the return point to mark the point of call.  */
20087   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
20088 }
20089
20090 /* Called by the final INSN scan whenever we see a var location.  We
20091    use it to drop labels in the right places, and throw the location in
20092    our lookup table.  */
20093
20094 static void
20095 dwarf2out_var_location (rtx loc_note)
20096 {
20097   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20098   struct var_loc_node *newloc;
20099   rtx next_real;
20100   static const char *last_label;
20101   static const char *last_postcall_label;
20102   static bool last_in_cold_section_p;
20103   tree decl;
20104
20105   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20106     return;
20107
20108   next_real = next_real_insn (loc_note);
20109   /* If there are no instructions which would be affected by this note,
20110      don't do anything.  */
20111   if (next_real == NULL_RTX)
20112     return;
20113
20114   newloc = GGC_CNEW (struct var_loc_node);
20115   /* If there were no real insns between note we processed last time
20116      and this note, use the label we emitted last time.  */
20117   if (last_var_location_insn == NULL_RTX
20118       || last_var_location_insn != next_real
20119       || last_in_cold_section_p != in_cold_section_p)
20120     {
20121       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20122       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20123       loclabel_num++;
20124       last_label = ggc_strdup (loclabel);
20125       last_postcall_label = NULL;
20126     }
20127   newloc->var_loc_note = loc_note;
20128   newloc->next = NULL;
20129
20130   if (!NOTE_DURING_CALL_P (loc_note))
20131     newloc->label = last_label;
20132   else
20133     {
20134       if (!last_postcall_label)
20135         {
20136           sprintf (loclabel, "%s-1", last_label);
20137           last_postcall_label = ggc_strdup (loclabel);
20138         }
20139       newloc->label = last_postcall_label;
20140     }
20141
20142   if (cfun && in_cold_section_p)
20143     newloc->section_label = crtl->subsections.cold_section_label;
20144   else
20145     newloc->section_label = text_section_label;
20146
20147   last_var_location_insn = next_real;
20148   last_in_cold_section_p = in_cold_section_p;
20149   decl = NOTE_VAR_LOCATION_DECL (loc_note);
20150   add_var_loc_to_decl (decl, newloc);
20151 }
20152
20153 /* We need to reset the locations at the beginning of each
20154    function. We can't do this in the end_function hook, because the
20155    declarations that use the locations won't have been output when
20156    that hook is called.  Also compute have_multiple_function_sections here.  */
20157
20158 static void
20159 dwarf2out_begin_function (tree fun)
20160 {
20161   htab_empty (decl_loc_table);
20162
20163   if (function_section (fun) != text_section)
20164     have_multiple_function_sections = true;
20165
20166   dwarf2out_note_section_used ();
20167 }
20168
20169 /* Output a label to mark the beginning of a source code line entry
20170    and record information relating to this source line, in
20171    'line_info_table' for later output of the .debug_line section.  */
20172
20173 static void
20174 dwarf2out_source_line (unsigned int line, const char *filename,
20175                        int discriminator, bool is_stmt)
20176 {
20177   static bool last_is_stmt = true;
20178
20179   if (debug_info_level >= DINFO_LEVEL_NORMAL
20180       && line != 0)
20181     {
20182       int file_num = maybe_emit_file (lookup_filename (filename));
20183
20184       switch_to_section (current_function_section ());
20185
20186       /* If requested, emit something human-readable.  */
20187       if (flag_debug_asm)
20188         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
20189                  filename, line);
20190
20191       if (DWARF2_ASM_LINE_DEBUG_INFO)
20192         {
20193           /* Emit the .loc directive understood by GNU as.  */
20194           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20195           if (is_stmt != last_is_stmt)
20196             {
20197               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20198               last_is_stmt = is_stmt;
20199             }
20200           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20201             fprintf (asm_out_file, " discriminator %d", discriminator);
20202           fputc ('\n', asm_out_file);
20203
20204           /* Indicate that line number info exists.  */
20205           line_info_table_in_use++;
20206         }
20207       else if (function_section (current_function_decl) != text_section)
20208         {
20209           dw_separate_line_info_ref line_info;
20210           targetm.asm_out.internal_label (asm_out_file,
20211                                           SEPARATE_LINE_CODE_LABEL,
20212                                           separate_line_info_table_in_use);
20213
20214           /* Expand the line info table if necessary.  */
20215           if (separate_line_info_table_in_use
20216               == separate_line_info_table_allocated)
20217             {
20218               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
20219               separate_line_info_table
20220                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
20221                                  separate_line_info_table,
20222                                  separate_line_info_table_allocated);
20223               memset (separate_line_info_table
20224                        + separate_line_info_table_in_use,
20225                       0,
20226                       (LINE_INFO_TABLE_INCREMENT
20227                        * sizeof (dw_separate_line_info_entry)));
20228             }
20229
20230           /* Add the new entry at the end of the line_info_table.  */
20231           line_info
20232             = &separate_line_info_table[separate_line_info_table_in_use++];
20233           line_info->dw_file_num = file_num;
20234           line_info->dw_line_num = line;
20235           line_info->function = current_function_funcdef_no;
20236         }
20237       else
20238         {
20239           dw_line_info_ref line_info;
20240
20241           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
20242                                      line_info_table_in_use);
20243
20244           /* Expand the line info table if necessary.  */
20245           if (line_info_table_in_use == line_info_table_allocated)
20246             {
20247               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
20248               line_info_table
20249                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
20250                                  line_info_table_allocated);
20251               memset (line_info_table + line_info_table_in_use, 0,
20252                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
20253             }
20254
20255           /* Add the new entry at the end of the line_info_table.  */
20256           line_info = &line_info_table[line_info_table_in_use++];
20257           line_info->dw_file_num = file_num;
20258           line_info->dw_line_num = line;
20259         }
20260     }
20261 }
20262
20263 /* Record the beginning of a new source file.  */
20264
20265 static void
20266 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20267 {
20268   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20269     {
20270       /* Record the beginning of the file for break_out_includes.  */
20271       dw_die_ref bincl_die;
20272
20273       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
20274       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20275     }
20276
20277   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20278     {
20279       int file_num = maybe_emit_file (lookup_filename (filename));
20280
20281       switch_to_section (debug_macinfo_section);
20282       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20283       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
20284                                    lineno);
20285
20286       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
20287     }
20288 }
20289
20290 /* Record the end of a source file.  */
20291
20292 static void
20293 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20294 {
20295   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20296     /* Record the end of the file for break_out_includes.  */
20297     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
20298
20299   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20300     {
20301       switch_to_section (debug_macinfo_section);
20302       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20303     }
20304 }
20305
20306 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20307    the tail part of the directive line, i.e. the part which is past the
20308    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20309
20310 static void
20311 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20312                   const char *buffer ATTRIBUTE_UNUSED)
20313 {
20314   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20315     {
20316       switch_to_section (debug_macinfo_section);
20317       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20318       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
20319       dw2_asm_output_nstring (buffer, -1, "The macro");
20320     }
20321 }
20322
20323 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20324    the tail part of the directive line, i.e. the part which is past the
20325    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20326
20327 static void
20328 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20329                  const char *buffer ATTRIBUTE_UNUSED)
20330 {
20331   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20332     {
20333       switch_to_section (debug_macinfo_section);
20334       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20335       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
20336       dw2_asm_output_nstring (buffer, -1, "The macro");
20337     }
20338 }
20339
20340 /* Set up for Dwarf output at the start of compilation.  */
20341
20342 static void
20343 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20344 {
20345   /* Allocate the file_table.  */
20346   file_table = htab_create_ggc (50, file_table_hash,
20347                                 file_table_eq, NULL);
20348
20349   /* Allocate the decl_die_table.  */
20350   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20351                                     decl_die_table_eq, NULL);
20352
20353   /* Allocate the decl_loc_table.  */
20354   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20355                                     decl_loc_table_eq, NULL);
20356
20357   /* Allocate the initial hunk of the decl_scope_table.  */
20358   decl_scope_table = VEC_alloc (tree, gc, 256);
20359
20360   /* Allocate the initial hunk of the abbrev_die_table.  */
20361   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
20362   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20363   /* Zero-th entry is allocated, but unused.  */
20364   abbrev_die_table_in_use = 1;
20365
20366   /* Allocate the initial hunk of the line_info_table.  */
20367   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
20368   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
20369
20370   /* Zero-th entry is allocated, but unused.  */
20371   line_info_table_in_use = 1;
20372
20373   /* Allocate the pubtypes and pubnames vectors.  */
20374   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20375   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20376
20377   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
20378   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
20379                                       vcall_insn_table_eq, NULL);
20380
20381   /* Generate the initial DIE for the .debug section.  Note that the (string)
20382      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
20383      will (typically) be a relative pathname and that this pathname should be
20384      taken as being relative to the directory from which the compiler was
20385      invoked when the given (base) source file was compiled.  We will fill
20386      in this value in dwarf2out_finish.  */
20387   comp_unit_die = gen_compile_unit_die (NULL);
20388
20389   incomplete_types = VEC_alloc (tree, gc, 64);
20390
20391   used_rtx_array = VEC_alloc (rtx, gc, 32);
20392
20393   debug_info_section = get_section (DEBUG_INFO_SECTION,
20394                                     SECTION_DEBUG, NULL);
20395   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20396                                       SECTION_DEBUG, NULL);
20397   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20398                                        SECTION_DEBUG, NULL);
20399   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20400                                        SECTION_DEBUG, NULL);
20401   debug_line_section = get_section (DEBUG_LINE_SECTION,
20402                                     SECTION_DEBUG, NULL);
20403   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20404                                    SECTION_DEBUG, NULL);
20405   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20406                                         SECTION_DEBUG, NULL);
20407   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20408                                         SECTION_DEBUG, NULL);
20409   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
20410                                      SECTION_DEBUG, NULL);
20411   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
20412                                      SECTION_DEBUG, NULL);
20413   debug_str_section = get_section (DEBUG_STR_SECTION,
20414                                    DEBUG_STR_SECTION_FLAGS, NULL);
20415   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20416                                       SECTION_DEBUG, NULL);
20417   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20418                                      SECTION_DEBUG, NULL);
20419
20420   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20421   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20422                                DEBUG_ABBREV_SECTION_LABEL, 0);
20423   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20424   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20425                                COLD_TEXT_SECTION_LABEL, 0);
20426   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20427
20428   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20429                                DEBUG_INFO_SECTION_LABEL, 0);
20430   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20431                                DEBUG_LINE_SECTION_LABEL, 0);
20432   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20433                                DEBUG_RANGES_SECTION_LABEL, 0);
20434   switch_to_section (debug_abbrev_section);
20435   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
20436   switch_to_section (debug_info_section);
20437   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
20438   switch_to_section (debug_line_section);
20439   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
20440
20441   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20442     {
20443       switch_to_section (debug_macinfo_section);
20444       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20445                                    DEBUG_MACINFO_SECTION_LABEL, 0);
20446       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
20447     }
20448
20449   switch_to_section (text_section);
20450   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20451   if (flag_reorder_blocks_and_partition)
20452     {
20453       cold_text_section = unlikely_text_section ();
20454       switch_to_section (cold_text_section);
20455       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20456     }
20457
20458 }
20459
20460 /* Called before cgraph_optimize starts outputtting functions, variables
20461    and toplevel asms into assembly.  */
20462
20463 static void
20464 dwarf2out_assembly_start (void)
20465 {
20466   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
20467     {
20468 #ifndef TARGET_UNWIND_INFO
20469       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
20470 #endif
20471         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20472     }
20473 }
20474
20475 /* A helper function for dwarf2out_finish called through
20476    htab_traverse.  Emit one queued .debug_str string.  */
20477
20478 static int
20479 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20480 {
20481   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20482
20483   if (node->label && node->refcount)
20484     {
20485       switch_to_section (debug_str_section);
20486       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20487       assemble_string (node->str, strlen (node->str) + 1);
20488     }
20489
20490   return 1;
20491 }
20492
20493 #if ENABLE_ASSERT_CHECKING
20494 /* Verify that all marks are clear.  */
20495
20496 static void
20497 verify_marks_clear (dw_die_ref die)
20498 {
20499   dw_die_ref c;
20500
20501   gcc_assert (! die->die_mark);
20502   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20503 }
20504 #endif /* ENABLE_ASSERT_CHECKING */
20505
20506 /* Clear the marks for a die and its children.
20507    Be cool if the mark isn't set.  */
20508
20509 static void
20510 prune_unmark_dies (dw_die_ref die)
20511 {
20512   dw_die_ref c;
20513
20514   if (die->die_mark)
20515     die->die_mark = 0;
20516   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20517 }
20518
20519 /* Given DIE that we're marking as used, find any other dies
20520    it references as attributes and mark them as used.  */
20521
20522 static void
20523 prune_unused_types_walk_attribs (dw_die_ref die)
20524 {
20525   dw_attr_ref a;
20526   unsigned ix;
20527
20528   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20529     {
20530       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20531         {
20532           /* A reference to another DIE.
20533              Make sure that it will get emitted.
20534              If it was broken out into a comdat group, don't follow it.  */
20535           if (dwarf_version < 4
20536               || a->dw_attr == DW_AT_specification
20537               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20538             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20539         }
20540       /* Set the string's refcount to 0 so that prune_unused_types_mark
20541          accounts properly for it.  */
20542       if (AT_class (a) == dw_val_class_str)
20543         a->dw_attr_val.v.val_str->refcount = 0;
20544     }
20545 }
20546
20547
20548 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20549    to DIE's children.  */
20550
20551 static void
20552 prune_unused_types_mark (dw_die_ref die, int dokids)
20553 {
20554   dw_die_ref c;
20555
20556   if (die->die_mark == 0)
20557     {
20558       /* We haven't done this node yet.  Mark it as used.  */
20559       die->die_mark = 1;
20560
20561       /* We also have to mark its parents as used.
20562          (But we don't want to mark our parents' kids due to this.)  */
20563       if (die->die_parent)
20564         prune_unused_types_mark (die->die_parent, 0);
20565
20566       /* Mark any referenced nodes.  */
20567       prune_unused_types_walk_attribs (die);
20568
20569       /* If this node is a specification,
20570          also mark the definition, if it exists.  */
20571       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20572         prune_unused_types_mark (die->die_definition, 1);
20573     }
20574
20575   if (dokids && die->die_mark != 2)
20576     {
20577       /* We need to walk the children, but haven't done so yet.
20578          Remember that we've walked the kids.  */
20579       die->die_mark = 2;
20580
20581       /* If this is an array type, we need to make sure our
20582          kids get marked, even if they're types.  If we're
20583          breaking out types into comdat sections, do this
20584          for all type definitions.  */
20585       if (die->die_tag == DW_TAG_array_type
20586           || (dwarf_version >= 4 
20587               && is_type_die (die) && ! is_declaration_die (die)))
20588         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20589       else
20590         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20591     }
20592 }
20593
20594 /* For local classes, look if any static member functions were emitted
20595    and if so, mark them.  */
20596
20597 static void
20598 prune_unused_types_walk_local_classes (dw_die_ref die)
20599 {
20600   dw_die_ref c;
20601
20602   if (die->die_mark == 2)
20603     return;
20604
20605   switch (die->die_tag)
20606     {
20607     case DW_TAG_structure_type:
20608     case DW_TAG_union_type:
20609     case DW_TAG_class_type:
20610       break;
20611
20612     case DW_TAG_subprogram:
20613       if (!get_AT_flag (die, DW_AT_declaration)
20614           || die->die_definition != NULL)
20615         prune_unused_types_mark (die, 1);
20616       return;
20617
20618     default:
20619       return;
20620     }
20621
20622   /* Mark children.  */
20623   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20624 }
20625
20626 /* Walk the tree DIE and mark types that we actually use.  */
20627
20628 static void
20629 prune_unused_types_walk (dw_die_ref die)
20630 {
20631   dw_die_ref c;
20632
20633   /* Don't do anything if this node is already marked and
20634      children have been marked as well.  */
20635   if (die->die_mark == 2)
20636     return;
20637
20638   switch (die->die_tag)
20639     {
20640     case DW_TAG_structure_type:
20641     case DW_TAG_union_type:
20642     case DW_TAG_class_type:
20643       if (die->die_perennial_p)
20644         break;
20645
20646       for (c = die->die_parent; c; c = c->die_parent)
20647         if (c->die_tag == DW_TAG_subprogram)
20648           break;
20649
20650       /* Finding used static member functions inside of classes
20651          is needed just for local classes, because for other classes
20652          static member function DIEs with DW_AT_specification
20653          are emitted outside of the DW_TAG_*_type.  If we ever change
20654          it, we'd need to call this even for non-local classes.  */
20655       if (c)
20656         prune_unused_types_walk_local_classes (die);
20657
20658       /* It's a type node --- don't mark it.  */
20659       return;
20660
20661     case DW_TAG_const_type:
20662     case DW_TAG_packed_type:
20663     case DW_TAG_pointer_type:
20664     case DW_TAG_reference_type:
20665     case DW_TAG_volatile_type:
20666     case DW_TAG_typedef:
20667     case DW_TAG_array_type:
20668     case DW_TAG_interface_type:
20669     case DW_TAG_friend:
20670     case DW_TAG_variant_part:
20671     case DW_TAG_enumeration_type:
20672     case DW_TAG_subroutine_type:
20673     case DW_TAG_string_type:
20674     case DW_TAG_set_type:
20675     case DW_TAG_subrange_type:
20676     case DW_TAG_ptr_to_member_type:
20677     case DW_TAG_file_type:
20678       if (die->die_perennial_p)
20679         break;
20680
20681       /* It's a type node --- don't mark it.  */
20682       return;
20683
20684     default:
20685       /* Mark everything else.  */
20686       break;
20687   }
20688
20689   if (die->die_mark == 0)
20690     {
20691       die->die_mark = 1;
20692
20693       /* Now, mark any dies referenced from here.  */
20694       prune_unused_types_walk_attribs (die);
20695     }
20696
20697   die->die_mark = 2;
20698
20699   /* Mark children.  */
20700   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20701 }
20702
20703 /* Increment the string counts on strings referred to from DIE's
20704    attributes.  */
20705
20706 static void
20707 prune_unused_types_update_strings (dw_die_ref die)
20708 {
20709   dw_attr_ref a;
20710   unsigned ix;
20711
20712   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20713     if (AT_class (a) == dw_val_class_str)
20714       {
20715         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20716         s->refcount++;
20717         /* Avoid unnecessarily putting strings that are used less than
20718            twice in the hash table.  */
20719         if (s->refcount
20720             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20721           {
20722             void ** slot;
20723             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20724                                              htab_hash_string (s->str),
20725                                              INSERT);
20726             gcc_assert (*slot == NULL);
20727             *slot = s;
20728           }
20729       }
20730 }
20731
20732 /* Remove from the tree DIE any dies that aren't marked.  */
20733
20734 static void
20735 prune_unused_types_prune (dw_die_ref die)
20736 {
20737   dw_die_ref c;
20738
20739   gcc_assert (die->die_mark);
20740   prune_unused_types_update_strings (die);
20741
20742   if (! die->die_child)
20743     return;
20744
20745   c = die->die_child;
20746   do {
20747     dw_die_ref prev = c;
20748     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20749       if (c == die->die_child)
20750         {
20751           /* No marked children between 'prev' and the end of the list.  */
20752           if (prev == c)
20753             /* No marked children at all.  */
20754             die->die_child = NULL;
20755           else
20756             {
20757               prev->die_sib = c->die_sib;
20758               die->die_child = prev;
20759             }
20760           return;
20761         }
20762
20763     if (c != prev->die_sib)
20764       prev->die_sib = c;
20765     prune_unused_types_prune (c);
20766   } while (c != die->die_child);
20767 }
20768
20769 /* A helper function for dwarf2out_finish called through
20770    htab_traverse.  Clear .debug_str strings that we haven't already
20771    decided to emit.  */
20772
20773 static int
20774 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20775 {
20776   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20777
20778   if (!node->label || !node->refcount)
20779     htab_clear_slot (debug_str_hash, h);
20780
20781   return 1;
20782 }
20783
20784 /* Remove dies representing declarations that we never use.  */
20785
20786 static void
20787 prune_unused_types (void)
20788 {
20789   unsigned int i;
20790   limbo_die_node *node;
20791   comdat_type_node *ctnode;
20792   pubname_ref pub;
20793   dcall_entry *dcall;
20794
20795 #if ENABLE_ASSERT_CHECKING
20796   /* All the marks should already be clear.  */
20797   verify_marks_clear (comp_unit_die);
20798   for (node = limbo_die_list; node; node = node->next)
20799     verify_marks_clear (node->die);
20800   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20801     verify_marks_clear (ctnode->root_die);
20802 #endif /* ENABLE_ASSERT_CHECKING */
20803
20804   /* Mark types that are used in global variables.  */
20805   premark_types_used_by_global_vars ();
20806
20807   /* Set the mark on nodes that are actually used.  */
20808   prune_unused_types_walk (comp_unit_die);
20809   for (node = limbo_die_list; node; node = node->next)
20810     prune_unused_types_walk (node->die);
20811   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20812     {
20813       prune_unused_types_walk (ctnode->root_die);
20814       prune_unused_types_mark (ctnode->type_die, 1);
20815     }
20816
20817   /* Also set the mark on nodes referenced from the
20818      pubname_table or arange_table.  */
20819   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
20820     prune_unused_types_mark (pub->die, 1);
20821   for (i = 0; i < arange_table_in_use; i++)
20822     prune_unused_types_mark (arange_table[i], 1);
20823
20824   /* Mark nodes referenced from the direct call table.  */
20825   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
20826     prune_unused_types_mark (dcall->targ_die, 1);
20827
20828   /* Get rid of nodes that aren't marked; and update the string counts.  */
20829   if (debug_str_hash && debug_str_hash_forced)
20830     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
20831   else if (debug_str_hash)
20832     htab_empty (debug_str_hash);
20833   prune_unused_types_prune (comp_unit_die);
20834   for (node = limbo_die_list; node; node = node->next)
20835     prune_unused_types_prune (node->die);
20836   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20837     prune_unused_types_prune (ctnode->root_die);
20838
20839   /* Leave the marks clear.  */
20840   prune_unmark_dies (comp_unit_die);
20841   for (node = limbo_die_list; node; node = node->next)
20842     prune_unmark_dies (node->die);
20843   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20844     prune_unmark_dies (ctnode->root_die);
20845 }
20846
20847 /* Set the parameter to true if there are any relative pathnames in
20848    the file table.  */
20849 static int
20850 file_table_relative_p (void ** slot, void *param)
20851 {
20852   bool *p = (bool *) param;
20853   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
20854   if (!IS_ABSOLUTE_PATH (d->filename))
20855     {
20856       *p = true;
20857       return 0;
20858     }
20859   return 1;
20860 }
20861
20862 /* Routines to manipulate hash table of comdat type units.  */
20863
20864 static hashval_t
20865 htab_ct_hash (const void *of)
20866 {
20867   hashval_t h;
20868   const comdat_type_node *const type_node = (const comdat_type_node *) of;
20869
20870   memcpy (&h, type_node->signature, sizeof (h));
20871   return h;
20872 }
20873
20874 static int
20875 htab_ct_eq (const void *of1, const void *of2)
20876 {
20877   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
20878   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
20879
20880   return (! memcmp (type_node_1->signature, type_node_2->signature,
20881                     DWARF_TYPE_SIGNATURE_SIZE));
20882 }
20883
20884 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
20885    to the location it would have been added, should we know its
20886    DECL_ASSEMBLER_NAME when we added other attributes.  This will
20887    probably improve compactness of debug info, removing equivalent
20888    abbrevs, and hide any differences caused by deferring the
20889    computation of the assembler name, triggered by e.g. PCH.  */
20890
20891 static inline void
20892 move_linkage_attr (dw_die_ref die)
20893 {
20894   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
20895   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
20896
20897   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
20898
20899   while (--ix > 0)
20900     {
20901       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
20902
20903       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
20904         break;
20905     }
20906
20907   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
20908     {
20909       VEC_pop (dw_attr_node, die->die_attr);
20910       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
20911     }
20912 }
20913
20914 /* Helper function for resolve_addr, attempt to resolve
20915    one CONST_STRING, return non-zero if not successful.  Similarly verify that
20916    SYMBOL_REFs refer to variables emitted in the current CU.  */
20917
20918 static int
20919 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
20920 {
20921   rtx rtl = *addr;
20922
20923   if (GET_CODE (rtl) == CONST_STRING)
20924     {
20925       size_t len = strlen (XSTR (rtl, 0)) + 1;
20926       tree t = build_string (len, XSTR (rtl, 0));
20927       tree tlen = build_int_cst (NULL_TREE, len - 1);
20928       TREE_TYPE (t)
20929         = build_array_type (char_type_node, build_index_type (tlen));
20930       rtl = lookup_constant_def (t);
20931       if (!rtl || !MEM_P (rtl))
20932         return 1;
20933       rtl = XEXP (rtl, 0);
20934       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
20935       *addr = rtl;
20936       return 0;
20937     }
20938
20939   if (GET_CODE (rtl) == SYMBOL_REF
20940       && SYMBOL_REF_DECL (rtl)
20941       && TREE_CODE (SYMBOL_REF_DECL (rtl)) == VAR_DECL
20942       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
20943     return 1;
20944
20945   if (GET_CODE (rtl) == CONST
20946       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
20947     return 1;
20948
20949   return 0;
20950 }
20951
20952 /* Helper function for resolve_addr, handle one location
20953    expression, return false if at least one CONST_STRING or SYMBOL_REF in
20954    the location list couldn't be resolved.  */
20955
20956 static bool
20957 resolve_addr_in_expr (dw_loc_descr_ref loc)
20958 {
20959   for (; loc; loc = loc->dw_loc_next)
20960     if ((loc->dw_loc_opc == DW_OP_addr
20961          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
20962         || (loc->dw_loc_opc == DW_OP_implicit_value
20963             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
20964             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
20965       return false;
20966   return true;
20967 }
20968
20969 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
20970    an address in .rodata section if the string literal is emitted there,
20971    or remove the containing location list or replace DW_AT_const_value
20972    with DW_AT_location and empty location expression, if it isn't found
20973    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
20974    to something that has been emitted in the current CU.  */
20975
20976 static void
20977 resolve_addr (dw_die_ref die)
20978 {
20979   dw_die_ref c;
20980   dw_attr_ref a;
20981   dw_loc_list_ref curr;
20982   unsigned ix;
20983
20984   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20985     switch (AT_class (a))
20986       {
20987       case dw_val_class_loc_list:
20988         for (curr = AT_loc_list (a); curr != NULL; curr = curr->dw_loc_next)
20989           if (!resolve_addr_in_expr (curr->expr))
20990             curr->expr = NULL;
20991         break;
20992       case dw_val_class_loc:
20993         if (!resolve_addr_in_expr (AT_loc (a)))
20994           a->dw_attr_val.v.val_loc = NULL;
20995         break;
20996       case dw_val_class_addr:
20997         if (a->dw_attr == DW_AT_const_value
20998             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
20999           {
21000             a->dw_attr = DW_AT_location;
21001             a->dw_attr_val.val_class = dw_val_class_loc;
21002             a->dw_attr_val.v.val_loc = NULL;
21003           }
21004         break;
21005       default:
21006         break;
21007       }
21008
21009   FOR_EACH_CHILD (die, c, resolve_addr (c));
21010 }
21011
21012 /* Output stuff that dwarf requires at the end of every file,
21013    and generate the DWARF-2 debugging info.  */
21014
21015 static void
21016 dwarf2out_finish (const char *filename)
21017 {
21018   limbo_die_node *node, *next_node;
21019   comdat_type_node *ctnode;
21020   htab_t comdat_type_table;
21021   dw_die_ref die = 0;
21022   unsigned int i;
21023
21024   gen_remaining_tmpl_value_param_die_attribute ();
21025
21026   /* Add the name for the main input file now.  We delayed this from
21027      dwarf2out_init to avoid complications with PCH.  */
21028   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
21029   if (!IS_ABSOLUTE_PATH (filename))
21030     add_comp_dir_attribute (comp_unit_die);
21031   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
21032     {
21033       bool p = false;
21034       htab_traverse (file_table, file_table_relative_p, &p);
21035       if (p)
21036         add_comp_dir_attribute (comp_unit_die);
21037     }
21038
21039   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
21040     {
21041       add_location_or_const_value_attribute (
21042         VEC_index (deferred_locations, deferred_locations_list, i)->die,
21043         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
21044         DW_AT_location);
21045     }
21046
21047   /* Traverse the limbo die list, and add parent/child links.  The only
21048      dies without parents that should be here are concrete instances of
21049      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
21050      For concrete instances, we can get the parent die from the abstract
21051      instance.  */
21052   for (node = limbo_die_list; node; node = next_node)
21053     {
21054       next_node = node->next;
21055       die = node->die;
21056
21057       if (die->die_parent == NULL)
21058         {
21059           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
21060
21061           if (origin)
21062             add_child_die (origin->die_parent, die);
21063           else if (die == comp_unit_die)
21064             ;
21065           else if (errorcount > 0 || sorrycount > 0)
21066             /* It's OK to be confused by errors in the input.  */
21067             add_child_die (comp_unit_die, die);
21068           else
21069             {
21070               /* In certain situations, the lexical block containing a
21071                  nested function can be optimized away, which results
21072                  in the nested function die being orphaned.  Likewise
21073                  with the return type of that nested function.  Force
21074                  this to be a child of the containing function.
21075
21076                  It may happen that even the containing function got fully
21077                  inlined and optimized out.  In that case we are lost and
21078                  assign the empty child.  This should not be big issue as
21079                  the function is likely unreachable too.  */
21080               tree context = NULL_TREE;
21081
21082               gcc_assert (node->created_for);
21083
21084               if (DECL_P (node->created_for))
21085                 context = DECL_CONTEXT (node->created_for);
21086               else if (TYPE_P (node->created_for))
21087                 context = TYPE_CONTEXT (node->created_for);
21088
21089               gcc_assert (context
21090                           && (TREE_CODE (context) == FUNCTION_DECL
21091                               || TREE_CODE (context) == NAMESPACE_DECL));
21092
21093               origin = lookup_decl_die (context);
21094               if (origin)
21095                 add_child_die (origin, die);
21096               else
21097                 add_child_die (comp_unit_die, die);
21098             }
21099         }
21100     }
21101
21102   limbo_die_list = NULL;
21103
21104   resolve_addr (comp_unit_die);
21105
21106   for (node = deferred_asm_name; node; node = node->next)
21107     {
21108       tree decl = node->created_for;
21109       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21110         {
21111           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
21112                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
21113           move_linkage_attr (node->die);
21114         }
21115     }
21116
21117   deferred_asm_name = NULL;
21118
21119   /* Walk through the list of incomplete types again, trying once more to
21120      emit full debugging info for them.  */
21121   retry_incomplete_types ();
21122
21123   if (flag_eliminate_unused_debug_types)
21124     prune_unused_types ();
21125
21126   /* Generate separate CUs for each of the include files we've seen.
21127      They will go into limbo_die_list.  */
21128   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21129     break_out_includes (comp_unit_die);
21130
21131   /* Generate separate COMDAT sections for type DIEs. */
21132   if (dwarf_version >= 4)
21133     {
21134       break_out_comdat_types (comp_unit_die);
21135
21136       /* Each new type_unit DIE was added to the limbo die list when created.
21137          Since these have all been added to comdat_type_list, clear the
21138          limbo die list.  */
21139       limbo_die_list = NULL;
21140
21141       /* For each new comdat type unit, copy declarations for incomplete
21142          types to make the new unit self-contained (i.e., no direct
21143          references to the main compile unit).  */
21144       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21145         copy_decls_for_unworthy_types (ctnode->root_die);
21146       copy_decls_for_unworthy_types (comp_unit_die);
21147
21148       /* In the process of copying declarations from one unit to another,
21149          we may have left some declarations behind that are no longer
21150          referenced.  Prune them.  */
21151       prune_unused_types ();
21152     }
21153
21154   /* Traverse the DIE's and add add sibling attributes to those DIE's
21155      that have children.  */
21156   add_sibling_attributes (comp_unit_die);
21157   for (node = limbo_die_list; node; node = node->next)
21158     add_sibling_attributes (node->die);
21159   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21160     add_sibling_attributes (ctnode->root_die);
21161
21162   /* Output a terminator label for the .text section.  */
21163   switch_to_section (text_section);
21164   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
21165   if (flag_reorder_blocks_and_partition)
21166     {
21167       switch_to_section (unlikely_text_section ());
21168       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
21169     }
21170
21171   /* We can only use the low/high_pc attributes if all of the code was
21172      in .text.  */
21173   if (!have_multiple_function_sections
21174       || !(dwarf_version >= 3 || !dwarf_strict))
21175     {
21176       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
21177       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
21178     }
21179
21180   else
21181     {
21182       unsigned fde_idx = 0;
21183
21184       /* We need to give .debug_loc and .debug_ranges an appropriate
21185          "base address".  Use zero so that these addresses become
21186          absolute.  Historically, we've emitted the unexpected
21187          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
21188          Emit both to give time for other tools to adapt.  */
21189       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
21190       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
21191
21192       add_AT_range_list (comp_unit_die, DW_AT_ranges,
21193                          add_ranges_by_labels (text_section_label,
21194                                                text_end_label));
21195       if (flag_reorder_blocks_and_partition)
21196         add_ranges_by_labels (cold_text_section_label,
21197                               cold_end_label);
21198
21199       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
21200         {
21201           dw_fde_ref fde = &fde_table[fde_idx];
21202
21203           if (fde->dw_fde_switched_sections)
21204             {
21205               if (!fde->in_std_section)
21206                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
21207                                       fde->dw_fde_hot_section_end_label);
21208               if (!fde->cold_in_std_section)
21209                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
21210                                       fde->dw_fde_unlikely_section_end_label);
21211             }
21212           else if (!fde->in_std_section)
21213             add_ranges_by_labels (fde->dw_fde_begin,
21214                                   fde->dw_fde_end);
21215         }
21216
21217       add_ranges (NULL);
21218     }
21219
21220   /* Output location list section if necessary.  */
21221   if (have_location_lists)
21222     {
21223       /* Output the location lists info.  */
21224       switch_to_section (debug_loc_section);
21225       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
21226                                    DEBUG_LOC_SECTION_LABEL, 0);
21227       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
21228       output_location_lists (die);
21229     }
21230
21231   if (debug_info_level >= DINFO_LEVEL_NORMAL)
21232     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
21233                     debug_line_section_label);
21234
21235   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21236     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
21237
21238   /* Output all of the compilation units.  We put the main one last so that
21239      the offsets are available to output_pubnames.  */
21240   for (node = limbo_die_list; node; node = node->next)
21241     output_comp_unit (node->die, 0);
21242
21243   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
21244   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
21245     {
21246       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
21247
21248       /* Don't output duplicate types.  */
21249       if (*slot != HTAB_EMPTY_ENTRY)
21250         continue;
21251
21252       /* Add a pointer to the line table for the main compilation unit
21253          so that the debugger can make sense of DW_AT_decl_file
21254          attributes.  */
21255       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21256         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
21257                         debug_line_section_label);
21258
21259       output_comdat_type_unit (ctnode);
21260       *slot = ctnode;
21261     }
21262   htab_delete (comdat_type_table);
21263
21264   /* Output the main compilation unit if non-empty or if .debug_macinfo
21265      has been emitted.  */
21266   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
21267
21268   /* Output the abbreviation table.  */
21269   switch_to_section (debug_abbrev_section);
21270   output_abbrev_section ();
21271
21272   /* Output public names table if necessary.  */
21273   if (!VEC_empty (pubname_entry, pubname_table))
21274     {
21275       switch_to_section (debug_pubnames_section);
21276       output_pubnames (pubname_table);
21277     }
21278
21279   /* Output public types table if necessary.  */
21280   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
21281      It shouldn't hurt to emit it always, since pure DWARF2 consumers
21282      simply won't look for the section.  */
21283   if (!VEC_empty (pubname_entry, pubtype_table))
21284     {
21285       switch_to_section (debug_pubtypes_section);
21286       output_pubnames (pubtype_table);
21287     }
21288
21289   /* Output direct and virtual call tables if necessary.  */
21290   if (!VEC_empty (dcall_entry, dcall_table))
21291     {
21292       switch_to_section (debug_dcall_section);
21293       output_dcall_table ();
21294     }
21295   if (!VEC_empty (vcall_entry, vcall_table))
21296     {
21297       switch_to_section (debug_vcall_section);
21298       output_vcall_table ();
21299     }
21300
21301   /* Output the address range information.  We only put functions in the arange
21302      table, so don't write it out if we don't have any.  */
21303   if (fde_table_in_use)
21304     {
21305       switch_to_section (debug_aranges_section);
21306       output_aranges ();
21307     }
21308
21309   /* Output ranges section if necessary.  */
21310   if (ranges_table_in_use)
21311     {
21312       switch_to_section (debug_ranges_section);
21313       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
21314       output_ranges ();
21315     }
21316
21317   /* Output the source line correspondence table.  We must do this
21318      even if there is no line information.  Otherwise, on an empty
21319      translation unit, we will generate a present, but empty,
21320      .debug_info section.  IRIX 6.5 `nm' will then complain when
21321      examining the file.  This is done late so that any filenames
21322      used by the debug_info section are marked as 'used'.  */
21323   if (! DWARF2_ASM_LINE_DEBUG_INFO)
21324     {
21325       switch_to_section (debug_line_section);
21326       output_line_info ();
21327     }
21328
21329   /* Have to end the macro section.  */
21330   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21331     {
21332       switch_to_section (debug_macinfo_section);
21333       dw2_asm_output_data (1, 0, "End compilation unit");
21334     }
21335
21336   /* If we emitted any DW_FORM_strp form attribute, output the string
21337      table too.  */
21338   if (debug_str_hash)
21339     htab_traverse (debug_str_hash, output_indirect_string, NULL);
21340 }
21341 #else
21342
21343 /* This should never be used, but its address is needed for comparisons.  */
21344 const struct gcc_debug_hooks dwarf2_debug_hooks =
21345 {
21346   0,            /* init */
21347   0,            /* finish */
21348   0,            /* assembly_start */
21349   0,            /* define */
21350   0,            /* undef */
21351   0,            /* start_source_file */
21352   0,            /* end_source_file */
21353   0,            /* begin_block */
21354   0,            /* end_block */
21355   0,            /* ignore_block */
21356   0,            /* source_line */
21357   0,            /* begin_prologue */
21358   0,            /* end_prologue */
21359   0,            /* end_epilogue */
21360   0,            /* begin_function */
21361   0,            /* end_function */
21362   0,            /* function_decl */
21363   0,            /* global_decl */
21364   0,            /* type_decl */
21365   0,            /* imported_module_or_decl */
21366   0,            /* deferred_inline_function */
21367   0,            /* outlining_inline_function */
21368   0,            /* label */
21369   0,            /* handle_pch */
21370   0,            /* var_location */
21371   0,            /* switch_text_section */
21372   0,            /* direct_call */
21373   0,            /* virtual_call_token */
21374   0,            /* copy_call_info */
21375   0,            /* virtual_call */
21376   0,            /* set_name */
21377   0             /* start_end_main_source_file */
21378 };
21379
21380 #endif /* DWARF2_DEBUGGING_INFO */
21381
21382 #include "gt-dwarf2out.h"