OSDN Git Service

0f3bea860be6885c86a273e15e3878322e297020
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 #ifndef DWARF2_UNWIND_INFO
116 #define DWARF2_UNWIND_INFO 0
117 #endif
118
119 #ifndef INCOMING_RETURN_ADDR_RTX
120 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
121 #endif
122
123 #ifndef DWARF2_FRAME_INFO
124 # ifdef DWARF2_DEBUGGING_INFO
125 #  define DWARF2_FRAME_INFO \
126   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
127 # else
128 #  define DWARF2_FRAME_INFO 0
129 # endif
130 #endif
131
132 /* Map register numbers held in the call frame info that gcc has
133    collected using DWARF_FRAME_REGNUM to those that should be output in
134    .debug_frame and .eh_frame.  */
135 #ifndef DWARF2_FRAME_REG_OUT
136 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
137 #endif
138
139 /* Save the result of dwarf2out_do_frame across PCH.  */
140 static GTY(()) bool saved_do_cfi_asm = 0;
141
142 /* Decide whether we want to emit frame unwind information for the current
143    translation unit.  */
144
145 int
146 dwarf2out_do_frame (void)
147 {
148   /* We want to emit correct CFA location expressions or lists, so we
149      have to return true if we're going to output debug info, even if
150      we're not going to output frame or unwind info.  */
151   return (write_symbols == DWARF2_DEBUG
152           || write_symbols == VMS_AND_DWARF2_DEBUG
153           || DWARF2_FRAME_INFO || saved_do_cfi_asm
154           || (DWARF2_UNWIND_INFO
155               && (flag_unwind_tables
156                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
157           );
158 }
159
160 /* Decide whether to emit frame unwind via assembler directives.  */
161
162 int
163 dwarf2out_do_cfi_asm (void)
164 {
165   int enc;
166
167 #ifdef MIPS_DEBUGGING_INFO
168   return false;
169 #endif
170   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
171     return false;
172   if (saved_do_cfi_asm)
173     return true;
174   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
175     return false;
176
177   /* Make sure the personality encoding is one the assembler can support.
178      In particular, aligned addresses can't be handled.  */
179   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
180   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
181     return false;
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185
186   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
187     {
188 #ifdef TARGET_UNWIND_INFO
189       return false;
190 #else
191       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
192         return false;
193 #endif
194     }
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_dcall_section;
232 static GTY(()) section *debug_vcall_section;
233 static GTY(()) section *debug_str_section;
234 static GTY(()) section *debug_ranges_section;
235 static GTY(()) section *debug_frame_section;
236
237 /* Personality decl of current unit.  Used only when assembler does not support
238    personality CFI.  */
239 static GTY(()) rtx current_unit_personality;
240
241 /* How to start an assembler comment.  */
242 #ifndef ASM_COMMENT_START
243 #define ASM_COMMENT_START ";#"
244 #endif
245
246 typedef struct dw_cfi_struct *dw_cfi_ref;
247 typedef struct dw_fde_struct *dw_fde_ref;
248 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
249
250 /* Call frames are described using a sequence of Call Frame
251    Information instructions.  The register number, offset
252    and address fields are provided as possible operands;
253    their use is selected by the opcode field.  */
254
255 enum dw_cfi_oprnd_type {
256   dw_cfi_oprnd_unused,
257   dw_cfi_oprnd_reg_num,
258   dw_cfi_oprnd_offset,
259   dw_cfi_oprnd_addr,
260   dw_cfi_oprnd_loc
261 };
262
263 typedef union GTY(()) dw_cfi_oprnd_struct {
264   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
265   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
266   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
267   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
268 }
269 dw_cfi_oprnd;
270
271 typedef struct GTY(()) dw_cfi_struct {
272   dw_cfi_ref dw_cfi_next;
273   enum dwarf_call_frame_info dw_cfi_opc;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd1;
276   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
277     dw_cfi_oprnd2;
278 }
279 dw_cfi_node;
280
281 /* This is how we define the location of the CFA. We use to handle it
282    as REG + OFFSET all the time,  but now it can be more complex.
283    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
284    Instead of passing around REG and OFFSET, we pass a copy
285    of this structure.  */
286 typedef struct GTY(()) cfa_loc {
287   HOST_WIDE_INT offset;
288   HOST_WIDE_INT base_offset;
289   unsigned int reg;
290   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
291   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
292 } dw_cfa_location;
293
294 /* All call frame descriptions (FDE's) in the GCC generated DWARF
295    refer to a single Common Information Entry (CIE), defined at
296    the beginning of the .debug_frame section.  This use of a single
297    CIE obviates the need to keep track of multiple CIE's
298    in the DWARF generation routines below.  */
299
300 typedef struct GTY(()) dw_fde_struct {
301   tree decl;
302   const char *dw_fde_begin;
303   const char *dw_fde_current_label;
304   const char *dw_fde_end;
305   const char *dw_fde_vms_end_prologue;
306   const char *dw_fde_vms_begin_epilogue;
307   const char *dw_fde_hot_section_label;
308   const char *dw_fde_hot_section_end_label;
309   const char *dw_fde_unlikely_section_label;
310   const char *dw_fde_unlikely_section_end_label;
311   dw_cfi_ref dw_fde_cfi;
312   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
313   HOST_WIDE_INT stack_realignment;
314   unsigned funcdef_number;
315   /* Dynamic realign argument pointer register.  */
316   unsigned int drap_reg;
317   /* Virtual dynamic realign argument pointer register.  */
318   unsigned int vdrap_reg;
319   /* These 3 flags are copied from rtl_data in function.h.  */
320   unsigned all_throwers_are_sibcalls : 1;
321   unsigned uses_eh_lsda : 1;
322   unsigned nothrow : 1;
323   /* Whether we did stack realign in this call frame.  */
324   unsigned stack_realign : 1;
325   /* Whether dynamic realign argument pointer register has been saved.  */
326   unsigned drap_reg_saved: 1;
327   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
328   unsigned in_std_section : 1;
329   /* True iff dw_fde_unlikely_section_label is in text_section or
330      cold_text_section.  */
331   unsigned cold_in_std_section : 1;
332   /* True iff switched sections.  */
333   unsigned dw_fde_switched_sections : 1;
334   /* True iff switching from cold to hot section.  */
335   unsigned dw_fde_switched_cold_to_hot : 1;
336 }
337 dw_fde_node;
338
339 /* Maximum size (in bytes) of an artificially generated label.  */
340 #define MAX_ARTIFICIAL_LABEL_BYTES      30
341
342 /* The size of addresses as they appear in the Dwarf 2 data.
343    Some architectures use word addresses to refer to code locations,
344    but Dwarf 2 info always uses byte addresses.  On such machines,
345    Dwarf 2 addresses need to be larger than the architecture's
346    pointers.  */
347 #ifndef DWARF2_ADDR_SIZE
348 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
349 #endif
350
351 /* The size in bytes of a DWARF field indicating an offset or length
352    relative to a debug info section, specified to be 4 bytes in the
353    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
354    as PTR_SIZE.  */
355
356 #ifndef DWARF_OFFSET_SIZE
357 #define DWARF_OFFSET_SIZE 4
358 #endif
359
360 /* The size in bytes of a DWARF 4 type signature.  */
361
362 #ifndef DWARF_TYPE_SIGNATURE_SIZE
363 #define DWARF_TYPE_SIGNATURE_SIZE 8
364 #endif
365
366 /* According to the (draft) DWARF 3 specification, the initial length
367    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
368    bytes are 0xffffffff, followed by the length stored in the next 8
369    bytes.
370
371    However, the SGI/MIPS ABI uses an initial length which is equal to
372    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
373
374 #ifndef DWARF_INITIAL_LENGTH_SIZE
375 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
376 #endif
377
378 /* Round SIZE up to the nearest BOUNDARY.  */
379 #define DWARF_ROUND(SIZE,BOUNDARY) \
380   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
381
382 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
383 #ifndef DWARF_CIE_DATA_ALIGNMENT
384 #ifdef STACK_GROWS_DOWNWARD
385 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
386 #else
387 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
388 #endif
389 #endif
390
391 /* CIE identifier.  */
392 #if HOST_BITS_PER_WIDE_INT >= 64
393 #define DWARF_CIE_ID \
394   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
395 #else
396 #define DWARF_CIE_ID DW_CIE_ID
397 #endif
398
399 /* A pointer to the base of a table that contains frame description
400    information for each routine.  */
401 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
402
403 /* Number of elements currently allocated for fde_table.  */
404 static GTY(()) unsigned fde_table_allocated;
405
406 /* Number of elements in fde_table currently in use.  */
407 static GTY(()) unsigned fde_table_in_use;
408
409 /* Size (in elements) of increments by which we may expand the
410    fde_table.  */
411 #define FDE_TABLE_INCREMENT 256
412
413 /* Get the current fde_table entry we should use.  */
414
415 static inline dw_fde_ref
416 current_fde (void)
417 {
418   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
419 }
420
421 /* A list of call frame insns for the CIE.  */
422 static GTY(()) dw_cfi_ref cie_cfi_head;
423
424 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
425    attribute that accelerates the lookup of the FDE associated
426    with the subprogram.  This variable holds the table index of the FDE
427    associated with the current function (body) definition.  */
428 static unsigned current_funcdef_fde;
429
430 struct GTY(()) indirect_string_node {
431   const char *str;
432   unsigned int refcount;
433   enum dwarf_form form;
434   char *label;
435 };
436
437 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
438
439 /* True if the compilation unit has location entries that reference
440    debug strings.  */
441 static GTY(()) bool debug_str_hash_forced = false;
442
443 static GTY(()) int dw2_string_counter;
444 static GTY(()) unsigned long dwarf2out_cfi_label_num;
445
446 /* True if the compilation unit places functions in more than one section.  */
447 static GTY(()) bool have_multiple_function_sections = false;
448
449 /* Whether the default text and cold text sections have been used at all.  */
450
451 static GTY(()) bool text_section_used = false;
452 static GTY(()) bool cold_text_section_used = false;
453
454 /* The default cold text section.  */
455 static GTY(()) section *cold_text_section;
456
457 /* Forward declarations for functions defined in this file.  */
458
459 static char *stripattributes (const char *);
460 static const char *dwarf_cfi_name (unsigned);
461 static dw_cfi_ref new_cfi (void);
462 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
463 static void add_fde_cfi (const char *, dw_cfi_ref);
464 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
465 static void lookup_cfa (dw_cfa_location *);
466 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
467 static void initial_return_save (rtx);
468 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
469                                           HOST_WIDE_INT);
470 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
471 static void output_cfi_directive (dw_cfi_ref);
472 static void output_call_frame_info (int);
473 static void dwarf2out_note_section_used (void);
474 static void flush_queued_reg_saves (void);
475 static bool clobbers_queued_reg_save (const_rtx);
476 static void dwarf2out_frame_debug_expr (rtx, const char *);
477
478 /* Support for complex CFA locations.  */
479 static void output_cfa_loc (dw_cfi_ref);
480 static void output_cfa_loc_raw (dw_cfi_ref);
481 static void get_cfa_from_loc_descr (dw_cfa_location *,
482                                     struct dw_loc_descr_struct *);
483 static struct dw_loc_descr_struct *build_cfa_loc
484   (dw_cfa_location *, HOST_WIDE_INT);
485 static struct dw_loc_descr_struct *build_cfa_aligned_loc
486   (HOST_WIDE_INT, HOST_WIDE_INT);
487 static void def_cfa_1 (const char *, dw_cfa_location *);
488 static struct dw_loc_descr_struct *mem_loc_descriptor
489   (rtx, enum machine_mode mode, enum var_init_status);
490
491 /* How to start an assembler comment.  */
492 #ifndef ASM_COMMENT_START
493 #define ASM_COMMENT_START ";#"
494 #endif
495
496 /* Data and reference forms for relocatable data.  */
497 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
498 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
499
500 #ifndef DEBUG_FRAME_SECTION
501 #define DEBUG_FRAME_SECTION     ".debug_frame"
502 #endif
503
504 #ifndef FUNC_BEGIN_LABEL
505 #define FUNC_BEGIN_LABEL        "LFB"
506 #endif
507
508 #ifndef FUNC_END_LABEL
509 #define FUNC_END_LABEL          "LFE"
510 #endif
511
512 #ifndef PROLOGUE_END_LABEL
513 #define PROLOGUE_END_LABEL      "LPE"
514 #endif
515
516 #ifndef EPILOGUE_BEGIN_LABEL
517 #define EPILOGUE_BEGIN_LABEL    "LEB"
518 #endif
519
520 #ifndef FRAME_BEGIN_LABEL
521 #define FRAME_BEGIN_LABEL       "Lframe"
522 #endif
523 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
524 #define CIE_END_LABEL           "LECIE"
525 #define FDE_LABEL               "LSFDE"
526 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
527 #define FDE_END_LABEL           "LEFDE"
528 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
529 #define LINE_NUMBER_END_LABEL   "LELT"
530 #define LN_PROLOG_AS_LABEL      "LASLTP"
531 #define LN_PROLOG_END_LABEL     "LELTP"
532 #define DIE_LABEL_PREFIX        "DW"
533
534 /* The DWARF 2 CFA column which tracks the return address.  Normally this
535    is the column for PC, or the first column after all of the hard
536    registers.  */
537 #ifndef DWARF_FRAME_RETURN_COLUMN
538 #ifdef PC_REGNUM
539 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
540 #else
541 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
542 #endif
543 #endif
544
545 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
546    default, we just provide columns for all registers.  */
547 #ifndef DWARF_FRAME_REGNUM
548 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
549 #endif
550 \f
551 /* Hook used by __throw.  */
552
553 rtx
554 expand_builtin_dwarf_sp_column (void)
555 {
556   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
557   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
558 }
559
560 /* Return a pointer to a copy of the section string name S with all
561    attributes stripped off, and an asterisk prepended (for assemble_name).  */
562
563 static inline char *
564 stripattributes (const char *s)
565 {
566   char *stripped = XNEWVEC (char, strlen (s) + 2);
567   char *p = stripped;
568
569   *p++ = '*';
570
571   while (*s && *s != ',')
572     *p++ = *s++;
573
574   *p = '\0';
575   return stripped;
576 }
577
578 /* MEM is a memory reference for the register size table, each element of
579    which has mode MODE.  Initialize column C as a return address column.  */
580
581 static void
582 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
583 {
584   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
585   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
586   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
587 }
588
589 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
590
591 static inline HOST_WIDE_INT
592 div_data_align (HOST_WIDE_INT off)
593 {
594   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
595   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
596   return r;
597 }
598
599 /* Return true if we need a signed version of a given opcode
600    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
601
602 static inline bool
603 need_data_align_sf_opcode (HOST_WIDE_INT off)
604 {
605   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
606 }
607
608 /* Generate code to initialize the register size table.  */
609
610 void
611 expand_builtin_init_dwarf_reg_sizes (tree address)
612 {
613   unsigned int i;
614   enum machine_mode mode = TYPE_MODE (char_type_node);
615   rtx addr = expand_normal (address);
616   rtx mem = gen_rtx_MEM (BLKmode, addr);
617   bool wrote_return_column = false;
618
619   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
620     {
621       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
622
623       if (rnum < DWARF_FRAME_REGISTERS)
624         {
625           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
626           enum machine_mode save_mode = reg_raw_mode[i];
627           HOST_WIDE_INT size;
628
629           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
630             save_mode = choose_hard_reg_mode (i, 1, true);
631           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
632             {
633               if (save_mode == VOIDmode)
634                 continue;
635               wrote_return_column = true;
636             }
637           size = GET_MODE_SIZE (save_mode);
638           if (offset < 0)
639             continue;
640
641           emit_move_insn (adjust_address (mem, mode, offset),
642                           gen_int_mode (size, mode));
643         }
644     }
645
646   if (!wrote_return_column)
647     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
648
649 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
650   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
651 #endif
652
653   targetm.init_dwarf_reg_sizes_extra (address);
654 }
655
656 /* Convert a DWARF call frame info. operation to its string name */
657
658 static const char *
659 dwarf_cfi_name (unsigned int cfi_opc)
660 {
661   switch (cfi_opc)
662     {
663     case DW_CFA_advance_loc:
664       return "DW_CFA_advance_loc";
665     case DW_CFA_offset:
666       return "DW_CFA_offset";
667     case DW_CFA_restore:
668       return "DW_CFA_restore";
669     case DW_CFA_nop:
670       return "DW_CFA_nop";
671     case DW_CFA_set_loc:
672       return "DW_CFA_set_loc";
673     case DW_CFA_advance_loc1:
674       return "DW_CFA_advance_loc1";
675     case DW_CFA_advance_loc2:
676       return "DW_CFA_advance_loc2";
677     case DW_CFA_advance_loc4:
678       return "DW_CFA_advance_loc4";
679     case DW_CFA_offset_extended:
680       return "DW_CFA_offset_extended";
681     case DW_CFA_restore_extended:
682       return "DW_CFA_restore_extended";
683     case DW_CFA_undefined:
684       return "DW_CFA_undefined";
685     case DW_CFA_same_value:
686       return "DW_CFA_same_value";
687     case DW_CFA_register:
688       return "DW_CFA_register";
689     case DW_CFA_remember_state:
690       return "DW_CFA_remember_state";
691     case DW_CFA_restore_state:
692       return "DW_CFA_restore_state";
693     case DW_CFA_def_cfa:
694       return "DW_CFA_def_cfa";
695     case DW_CFA_def_cfa_register:
696       return "DW_CFA_def_cfa_register";
697     case DW_CFA_def_cfa_offset:
698       return "DW_CFA_def_cfa_offset";
699
700     /* DWARF 3 */
701     case DW_CFA_def_cfa_expression:
702       return "DW_CFA_def_cfa_expression";
703     case DW_CFA_expression:
704       return "DW_CFA_expression";
705     case DW_CFA_offset_extended_sf:
706       return "DW_CFA_offset_extended_sf";
707     case DW_CFA_def_cfa_sf:
708       return "DW_CFA_def_cfa_sf";
709     case DW_CFA_def_cfa_offset_sf:
710       return "DW_CFA_def_cfa_offset_sf";
711
712     /* SGI/MIPS specific */
713     case DW_CFA_MIPS_advance_loc8:
714       return "DW_CFA_MIPS_advance_loc8";
715
716     /* GNU extensions */
717     case DW_CFA_GNU_window_save:
718       return "DW_CFA_GNU_window_save";
719     case DW_CFA_GNU_args_size:
720       return "DW_CFA_GNU_args_size";
721     case DW_CFA_GNU_negative_offset_extended:
722       return "DW_CFA_GNU_negative_offset_extended";
723
724     default:
725       return "DW_CFA_<unknown>";
726     }
727 }
728
729 /* Return a pointer to a newly allocated Call Frame Instruction.  */
730
731 static inline dw_cfi_ref
732 new_cfi (void)
733 {
734   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
735
736   cfi->dw_cfi_next = NULL;
737   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
738   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
739
740   return cfi;
741 }
742
743 /* Add a Call Frame Instruction to list of instructions.  */
744
745 static inline void
746 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
747 {
748   dw_cfi_ref *p;
749   dw_fde_ref fde = current_fde ();
750
751   /* When DRAP is used, CFA is defined with an expression.  Redefine
752      CFA may lead to a different CFA value.   */
753   /* ??? Of course, this heuristic fails when we're annotating epilogues,
754      because of course we'll always want to redefine the CFA back to the
755      stack pointer on the way out.  Where should we move this check?  */
756   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
757     switch (cfi->dw_cfi_opc)
758       {
759         case DW_CFA_def_cfa_register:
760         case DW_CFA_def_cfa_offset:
761         case DW_CFA_def_cfa_offset_sf:
762         case DW_CFA_def_cfa:
763         case DW_CFA_def_cfa_sf:
764           gcc_unreachable ();
765
766         default:
767           break;
768       }
769
770   /* Find the end of the chain.  */
771   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
772     ;
773
774   *p = cfi;
775 }
776
777 /* Generate a new label for the CFI info to refer to.  FORCE is true
778    if a label needs to be output even when using .cfi_* directives.  */
779
780 char *
781 dwarf2out_cfi_label (bool force)
782 {
783   static char label[20];
784
785   if (!force && dwarf2out_do_cfi_asm ())
786     {
787       /* In this case, we will be emitting the asm directive instead of
788          the label, so just return a placeholder to keep the rest of the
789          interfaces happy.  */
790       strcpy (label, "<do not output>");
791     }
792   else
793     {
794       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
795       ASM_OUTPUT_LABEL (asm_out_file, label);
796     }
797
798   return label;
799 }
800
801 /* True if remember_state should be emitted before following CFI directive.  */
802 static bool emit_cfa_remember;
803
804 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
805    or to the CIE if LABEL is NULL.  */
806
807 static void
808 add_fde_cfi (const char *label, dw_cfi_ref cfi)
809 {
810   dw_cfi_ref *list_head;
811
812   if (emit_cfa_remember)
813     {
814       dw_cfi_ref cfi_remember;
815
816       /* Emit the state save.  */
817       emit_cfa_remember = false;
818       cfi_remember = new_cfi ();
819       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
820       add_fde_cfi (label, cfi_remember);
821     }
822
823   list_head = &cie_cfi_head;
824
825   if (dwarf2out_do_cfi_asm ())
826     {
827       if (label)
828         {
829           dw_fde_ref fde = current_fde ();
830
831           gcc_assert (fde != NULL);
832
833           /* We still have to add the cfi to the list so that lookup_cfa
834              works later on.  When -g2 and above we even need to force
835              emitting of CFI labels and add to list a DW_CFA_set_loc for
836              convert_cfa_to_fb_loc_list purposes.  If we're generating
837              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
838              convert_cfa_to_fb_loc_list.  */
839           if (dwarf_version == 2
840               && debug_info_level > DINFO_LEVEL_TERSE
841               && (write_symbols == DWARF2_DEBUG
842                   || write_symbols == VMS_AND_DWARF2_DEBUG))
843             {
844               switch (cfi->dw_cfi_opc)
845                 {
846                 case DW_CFA_def_cfa_offset:
847                 case DW_CFA_def_cfa_offset_sf:
848                 case DW_CFA_def_cfa_register:
849                 case DW_CFA_def_cfa:
850                 case DW_CFA_def_cfa_sf:
851                 case DW_CFA_def_cfa_expression:
852                 case DW_CFA_restore_state:
853                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
854                     label = dwarf2out_cfi_label (true);
855
856                   if (fde->dw_fde_current_label == NULL
857                       || strcmp (label, fde->dw_fde_current_label) != 0)
858                     {
859                       dw_cfi_ref xcfi;
860
861                       label = xstrdup (label);
862
863                       /* Set the location counter to the new label.  */
864                       xcfi = new_cfi ();
865                       /* It doesn't metter whether DW_CFA_set_loc
866                          or DW_CFA_advance_loc4 is added here, those aren't
867                          emitted into assembly, only looked up by
868                          convert_cfa_to_fb_loc_list.  */
869                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
870                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
871                       add_cfi (&fde->dw_fde_cfi, xcfi);
872                       fde->dw_fde_current_label = label;
873                     }
874                   break;
875                 default:
876                   break;
877                 }
878             }
879
880           output_cfi_directive (cfi);
881
882           list_head = &fde->dw_fde_cfi;
883         }
884       /* ??? If this is a CFI for the CIE, we don't emit.  This
885          assumes that the standard CIE contents that the assembler
886          uses matches the standard CIE contents that the compiler
887          uses.  This is probably a bad assumption.  I'm not quite
888          sure how to address this for now.  */
889     }
890   else if (label)
891     {
892       dw_fde_ref fde = current_fde ();
893
894       gcc_assert (fde != NULL);
895
896       if (*label == 0)
897         label = dwarf2out_cfi_label (false);
898
899       if (fde->dw_fde_current_label == NULL
900           || strcmp (label, fde->dw_fde_current_label) != 0)
901         {
902           dw_cfi_ref xcfi;
903
904           label = xstrdup (label);
905
906           /* Set the location counter to the new label.  */
907           xcfi = new_cfi ();
908           /* If we have a current label, advance from there, otherwise
909              set the location directly using set_loc.  */
910           xcfi->dw_cfi_opc = fde->dw_fde_current_label
911                              ? DW_CFA_advance_loc4
912                              : DW_CFA_set_loc;
913           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
914           add_cfi (&fde->dw_fde_cfi, xcfi);
915
916           fde->dw_fde_current_label = label;
917         }
918
919       list_head = &fde->dw_fde_cfi;
920     }
921
922   add_cfi (list_head, cfi);
923 }
924
925 /* Subroutine of lookup_cfa.  */
926
927 static void
928 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
929 {
930   switch (cfi->dw_cfi_opc)
931     {
932     case DW_CFA_def_cfa_offset:
933     case DW_CFA_def_cfa_offset_sf:
934       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
935       break;
936     case DW_CFA_def_cfa_register:
937       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
938       break;
939     case DW_CFA_def_cfa:
940     case DW_CFA_def_cfa_sf:
941       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
942       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
943       break;
944     case DW_CFA_def_cfa_expression:
945       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
946       break;
947
948     case DW_CFA_remember_state:
949       gcc_assert (!remember->in_use);
950       *remember = *loc;
951       remember->in_use = 1;
952       break;
953     case DW_CFA_restore_state:
954       gcc_assert (remember->in_use);
955       *loc = *remember;
956       remember->in_use = 0;
957       break;
958
959     default:
960       break;
961     }
962 }
963
964 /* Find the previous value for the CFA.  */
965
966 static void
967 lookup_cfa (dw_cfa_location *loc)
968 {
969   dw_cfi_ref cfi;
970   dw_fde_ref fde;
971   dw_cfa_location remember;
972
973   memset (loc, 0, sizeof (*loc));
974   loc->reg = INVALID_REGNUM;
975   remember = *loc;
976
977   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
978     lookup_cfa_1 (cfi, loc, &remember);
979
980   fde = current_fde ();
981   if (fde)
982     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
983       lookup_cfa_1 (cfi, loc, &remember);
984 }
985
986 /* The current rule for calculating the DWARF2 canonical frame address.  */
987 static dw_cfa_location cfa;
988
989 /* The register used for saving registers to the stack, and its offset
990    from the CFA.  */
991 static dw_cfa_location cfa_store;
992
993 /* The current save location around an epilogue.  */
994 static dw_cfa_location cfa_remember;
995
996 /* The running total of the size of arguments pushed onto the stack.  */
997 static HOST_WIDE_INT args_size;
998
999 /* The last args_size we actually output.  */
1000 static HOST_WIDE_INT old_args_size;
1001
1002 /* Entry point to update the canonical frame address (CFA).
1003    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1004    calculated from REG+OFFSET.  */
1005
1006 void
1007 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1008 {
1009   dw_cfa_location loc;
1010   loc.indirect = 0;
1011   loc.base_offset = 0;
1012   loc.reg = reg;
1013   loc.offset = offset;
1014   def_cfa_1 (label, &loc);
1015 }
1016
1017 /* Determine if two dw_cfa_location structures define the same data.  */
1018
1019 static bool
1020 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1021 {
1022   return (loc1->reg == loc2->reg
1023           && loc1->offset == loc2->offset
1024           && loc1->indirect == loc2->indirect
1025           && (loc1->indirect == 0
1026               || loc1->base_offset == loc2->base_offset));
1027 }
1028
1029 /* This routine does the actual work.  The CFA is now calculated from
1030    the dw_cfa_location structure.  */
1031
1032 static void
1033 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1034 {
1035   dw_cfi_ref cfi;
1036   dw_cfa_location old_cfa, loc;
1037
1038   cfa = *loc_p;
1039   loc = *loc_p;
1040
1041   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1042     cfa_store.offset = loc.offset;
1043
1044   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1045   lookup_cfa (&old_cfa);
1046
1047   /* If nothing changed, no need to issue any call frame instructions.  */
1048   if (cfa_equal_p (&loc, &old_cfa))
1049     return;
1050
1051   cfi = new_cfi ();
1052
1053   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1054     {
1055       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1056          the CFA register did not change but the offset did.  The data
1057          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1058          in the assembler via the .cfi_def_cfa_offset directive.  */
1059       if (loc.offset < 0)
1060         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1061       else
1062         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1063       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1064     }
1065
1066 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1067   else if (loc.offset == old_cfa.offset
1068            && old_cfa.reg != INVALID_REGNUM
1069            && !loc.indirect
1070            && !old_cfa.indirect)
1071     {
1072       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1073          indicating the CFA register has changed to <register> but the
1074          offset has not changed.  */
1075       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1076       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1077     }
1078 #endif
1079
1080   else if (loc.indirect == 0)
1081     {
1082       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1083          indicating the CFA register has changed to <register> with
1084          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1085          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1086          directive.  */
1087       if (loc.offset < 0)
1088         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1089       else
1090         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1091       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1092       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1093     }
1094   else
1095     {
1096       /* Construct a DW_CFA_def_cfa_expression instruction to
1097          calculate the CFA using a full location expression since no
1098          register-offset pair is available.  */
1099       struct dw_loc_descr_struct *loc_list;
1100
1101       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1102       loc_list = build_cfa_loc (&loc, 0);
1103       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1104     }
1105
1106   add_fde_cfi (label, cfi);
1107 }
1108
1109 /* Add the CFI for saving a register.  REG is the CFA column number.
1110    LABEL is passed to add_fde_cfi.
1111    If SREG is -1, the register is saved at OFFSET from the CFA;
1112    otherwise it is saved in SREG.  */
1113
1114 static void
1115 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1116 {
1117   dw_cfi_ref cfi = new_cfi ();
1118   dw_fde_ref fde = current_fde ();
1119
1120   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1121
1122   /* When stack is aligned, store REG using DW_CFA_expression with
1123      FP.  */
1124   if (fde
1125       && fde->stack_realign
1126       && sreg == INVALID_REGNUM)
1127     {
1128       cfi->dw_cfi_opc = DW_CFA_expression;
1129       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1130       cfi->dw_cfi_oprnd2.dw_cfi_loc
1131         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1132     }
1133   else if (sreg == INVALID_REGNUM)
1134     {
1135       if (need_data_align_sf_opcode (offset))
1136         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1137       else if (reg & ~0x3f)
1138         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1139       else
1140         cfi->dw_cfi_opc = DW_CFA_offset;
1141       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1142     }
1143   else if (sreg == reg)
1144     cfi->dw_cfi_opc = DW_CFA_same_value;
1145   else
1146     {
1147       cfi->dw_cfi_opc = DW_CFA_register;
1148       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1149     }
1150
1151   add_fde_cfi (label, cfi);
1152 }
1153
1154 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1155    This CFI tells the unwinder that it needs to restore the window registers
1156    from the previous frame's window save area.
1157
1158    ??? Perhaps we should note in the CIE where windows are saved (instead of
1159    assuming 0(cfa)) and what registers are in the window.  */
1160
1161 void
1162 dwarf2out_window_save (const char *label)
1163 {
1164   dw_cfi_ref cfi = new_cfi ();
1165
1166   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1167   add_fde_cfi (label, cfi);
1168 }
1169
1170 /* Entry point for saving a register to the stack.  REG is the GCC register
1171    number.  LABEL and OFFSET are passed to reg_save.  */
1172
1173 void
1174 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1175 {
1176   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1177 }
1178
1179 /* Entry point for saving the return address in the stack.
1180    LABEL and OFFSET are passed to reg_save.  */
1181
1182 void
1183 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1184 {
1185   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1186 }
1187
1188 /* Entry point for saving the return address in a register.
1189    LABEL and SREG are passed to reg_save.  */
1190
1191 void
1192 dwarf2out_return_reg (const char *label, unsigned int sreg)
1193 {
1194   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1195 }
1196
1197 /* Record the initial position of the return address.  RTL is
1198    INCOMING_RETURN_ADDR_RTX.  */
1199
1200 static void
1201 initial_return_save (rtx rtl)
1202 {
1203   unsigned int reg = INVALID_REGNUM;
1204   HOST_WIDE_INT offset = 0;
1205
1206   switch (GET_CODE (rtl))
1207     {
1208     case REG:
1209       /* RA is in a register.  */
1210       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1211       break;
1212
1213     case MEM:
1214       /* RA is on the stack.  */
1215       rtl = XEXP (rtl, 0);
1216       switch (GET_CODE (rtl))
1217         {
1218         case REG:
1219           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1220           offset = 0;
1221           break;
1222
1223         case PLUS:
1224           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1225           offset = INTVAL (XEXP (rtl, 1));
1226           break;
1227
1228         case MINUS:
1229           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1230           offset = -INTVAL (XEXP (rtl, 1));
1231           break;
1232
1233         default:
1234           gcc_unreachable ();
1235         }
1236
1237       break;
1238
1239     case PLUS:
1240       /* The return address is at some offset from any value we can
1241          actually load.  For instance, on the SPARC it is in %i7+8. Just
1242          ignore the offset for now; it doesn't matter for unwinding frames.  */
1243       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1244       initial_return_save (XEXP (rtl, 0));
1245       return;
1246
1247     default:
1248       gcc_unreachable ();
1249     }
1250
1251   if (reg != DWARF_FRAME_RETURN_COLUMN)
1252     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1253 }
1254
1255 /* Given a SET, calculate the amount of stack adjustment it
1256    contains.  */
1257
1258 static HOST_WIDE_INT
1259 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1260                      HOST_WIDE_INT cur_offset)
1261 {
1262   const_rtx src = SET_SRC (pattern);
1263   const_rtx dest = SET_DEST (pattern);
1264   HOST_WIDE_INT offset = 0;
1265   enum rtx_code code;
1266
1267   if (dest == stack_pointer_rtx)
1268     {
1269       code = GET_CODE (src);
1270
1271       /* Assume (set (reg sp) (reg whatever)) sets args_size
1272          level to 0.  */
1273       if (code == REG && src != stack_pointer_rtx)
1274         {
1275           offset = -cur_args_size;
1276 #ifndef STACK_GROWS_DOWNWARD
1277           offset = -offset;
1278 #endif
1279           return offset - cur_offset;
1280         }
1281
1282       if (! (code == PLUS || code == MINUS)
1283           || XEXP (src, 0) != stack_pointer_rtx
1284           || !CONST_INT_P (XEXP (src, 1)))
1285         return 0;
1286
1287       /* (set (reg sp) (plus (reg sp) (const_int))) */
1288       offset = INTVAL (XEXP (src, 1));
1289       if (code == PLUS)
1290         offset = -offset;
1291       return offset;
1292     }
1293
1294   if (MEM_P (src) && !MEM_P (dest))
1295     dest = src;
1296   if (MEM_P (dest))
1297     {
1298       /* (set (mem (pre_dec (reg sp))) (foo)) */
1299       src = XEXP (dest, 0);
1300       code = GET_CODE (src);
1301
1302       switch (code)
1303         {
1304         case PRE_MODIFY:
1305         case POST_MODIFY:
1306           if (XEXP (src, 0) == stack_pointer_rtx)
1307             {
1308               rtx val = XEXP (XEXP (src, 1), 1);
1309               /* We handle only adjustments by constant amount.  */
1310               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1311                           && CONST_INT_P (val));
1312               offset = -INTVAL (val);
1313               break;
1314             }
1315           return 0;
1316
1317         case PRE_DEC:
1318         case POST_DEC:
1319           if (XEXP (src, 0) == stack_pointer_rtx)
1320             {
1321               offset = GET_MODE_SIZE (GET_MODE (dest));
1322               break;
1323             }
1324           return 0;
1325
1326         case PRE_INC:
1327         case POST_INC:
1328           if (XEXP (src, 0) == stack_pointer_rtx)
1329             {
1330               offset = -GET_MODE_SIZE (GET_MODE (dest));
1331               break;
1332             }
1333           return 0;
1334
1335         default:
1336           return 0;
1337         }
1338     }
1339   else
1340     return 0;
1341
1342   return offset;
1343 }
1344
1345 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1346    indexed by INSN_UID.  */
1347
1348 static HOST_WIDE_INT *barrier_args_size;
1349
1350 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1351
1352 static HOST_WIDE_INT
1353 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1354                              VEC (rtx, heap) **next)
1355 {
1356   HOST_WIDE_INT offset = 0;
1357   int i;
1358
1359   if (! RTX_FRAME_RELATED_P (insn))
1360     {
1361       if (prologue_epilogue_contains (insn))
1362         /* Nothing */;
1363       else if (GET_CODE (PATTERN (insn)) == SET)
1364         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1365       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1366                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1367         {
1368           /* There may be stack adjustments inside compound insns.  Search
1369              for them.  */
1370           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1371             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1372               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1373                                              cur_args_size, offset);
1374         }
1375     }
1376   else
1377     {
1378       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1379
1380       if (expr)
1381         {
1382           expr = XEXP (expr, 0);
1383           if (GET_CODE (expr) == PARALLEL
1384               || GET_CODE (expr) == SEQUENCE)
1385             for (i = 1; i < XVECLEN (expr, 0); i++)
1386               {
1387                 rtx elem = XVECEXP (expr, 0, i);
1388
1389                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1390                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1391               }
1392         }
1393     }
1394
1395 #ifndef STACK_GROWS_DOWNWARD
1396   offset = -offset;
1397 #endif
1398
1399   cur_args_size += offset;
1400   if (cur_args_size < 0)
1401     cur_args_size = 0;
1402
1403   if (JUMP_P (insn))
1404     {
1405       rtx dest = JUMP_LABEL (insn);
1406
1407       if (dest)
1408         {
1409           if (barrier_args_size [INSN_UID (dest)] < 0)
1410             {
1411               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1412               VEC_safe_push (rtx, heap, *next, dest);
1413             }
1414         }
1415     }
1416
1417   return cur_args_size;
1418 }
1419
1420 /* Walk the whole function and compute args_size on BARRIERs.  */
1421
1422 static void
1423 compute_barrier_args_size (void)
1424 {
1425   int max_uid = get_max_uid (), i;
1426   rtx insn;
1427   VEC (rtx, heap) *worklist, *next, *tmp;
1428
1429   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1430   for (i = 0; i < max_uid; i++)
1431     barrier_args_size[i] = -1;
1432
1433   worklist = VEC_alloc (rtx, heap, 20);
1434   next = VEC_alloc (rtx, heap, 20);
1435   insn = get_insns ();
1436   barrier_args_size[INSN_UID (insn)] = 0;
1437   VEC_quick_push (rtx, worklist, insn);
1438   for (;;)
1439     {
1440       while (!VEC_empty (rtx, worklist))
1441         {
1442           rtx prev, body, first_insn;
1443           HOST_WIDE_INT cur_args_size;
1444
1445           first_insn = insn = VEC_pop (rtx, worklist);
1446           cur_args_size = barrier_args_size[INSN_UID (insn)];
1447           prev = prev_nonnote_insn (insn);
1448           if (prev && BARRIER_P (prev))
1449             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1450
1451           for (; insn; insn = NEXT_INSN (insn))
1452             {
1453               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1454                 continue;
1455               if (BARRIER_P (insn))
1456                 break;
1457
1458               if (LABEL_P (insn))
1459                 {
1460                   if (insn == first_insn)
1461                     continue;
1462                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1463                     {
1464                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1465                       continue;
1466                     }
1467                   else
1468                     {
1469                       /* The insns starting with this label have been
1470                          already scanned or are in the worklist.  */
1471                       break;
1472                     }
1473                 }
1474
1475               body = PATTERN (insn);
1476               if (GET_CODE (body) == SEQUENCE)
1477                 {
1478                   HOST_WIDE_INT dest_args_size = cur_args_size;
1479                   for (i = 1; i < XVECLEN (body, 0); i++)
1480                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1481                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1482                       dest_args_size
1483                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1484                                                        dest_args_size, &next);
1485                     else
1486                       cur_args_size
1487                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1488                                                        cur_args_size, &next);
1489
1490                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1491                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1492                                                  dest_args_size, &next);
1493                   else
1494                     cur_args_size
1495                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1496                                                      cur_args_size, &next);
1497                 }
1498               else
1499                 cur_args_size
1500                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1501             }
1502         }
1503
1504       if (VEC_empty (rtx, next))
1505         break;
1506
1507       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1508       tmp = next;
1509       next = worklist;
1510       worklist = tmp;
1511       VEC_truncate (rtx, next, 0);
1512     }
1513
1514   VEC_free (rtx, heap, worklist);
1515   VEC_free (rtx, heap, next);
1516 }
1517
1518 /* Add a CFI to update the running total of the size of arguments
1519    pushed onto the stack.  */
1520
1521 static void
1522 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1523 {
1524   dw_cfi_ref cfi;
1525
1526   if (size == old_args_size)
1527     return;
1528
1529   old_args_size = size;
1530
1531   cfi = new_cfi ();
1532   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1533   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1534   add_fde_cfi (label, cfi);
1535 }
1536
1537 /* Record a stack adjustment of OFFSET bytes.  */
1538
1539 static void
1540 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1541 {
1542   if (cfa.reg == STACK_POINTER_REGNUM)
1543     cfa.offset += offset;
1544
1545   if (cfa_store.reg == STACK_POINTER_REGNUM)
1546     cfa_store.offset += offset;
1547
1548   if (ACCUMULATE_OUTGOING_ARGS)
1549     return;
1550
1551 #ifndef STACK_GROWS_DOWNWARD
1552   offset = -offset;
1553 #endif
1554
1555   args_size += offset;
1556   if (args_size < 0)
1557     args_size = 0;
1558
1559   def_cfa_1 (label, &cfa);
1560   if (flag_asynchronous_unwind_tables)
1561     dwarf2out_args_size (label, args_size);
1562 }
1563
1564 /* Check INSN to see if it looks like a push or a stack adjustment, and
1565    make a note of it if it does.  EH uses this information to find out
1566    how much extra space it needs to pop off the stack.  */
1567
1568 static void
1569 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1570 {
1571   HOST_WIDE_INT offset;
1572   const char *label;
1573   int i;
1574
1575   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1576      with this function.  Proper support would require all frame-related
1577      insns to be marked, and to be able to handle saving state around
1578      epilogues textually in the middle of the function.  */
1579   if (prologue_epilogue_contains (insn))
1580     return;
1581
1582   /* If INSN is an instruction from target of an annulled branch, the
1583      effects are for the target only and so current argument size
1584      shouldn't change at all.  */
1585   if (final_sequence
1586       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1587       && INSN_FROM_TARGET_P (insn))
1588     return;
1589
1590   /* If only calls can throw, and we have a frame pointer,
1591      save up adjustments until we see the CALL_INSN.  */
1592   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1593     {
1594       if (CALL_P (insn) && !after_p)
1595         {
1596           /* Extract the size of the args from the CALL rtx itself.  */
1597           insn = PATTERN (insn);
1598           if (GET_CODE (insn) == PARALLEL)
1599             insn = XVECEXP (insn, 0, 0);
1600           if (GET_CODE (insn) == SET)
1601             insn = SET_SRC (insn);
1602           gcc_assert (GET_CODE (insn) == CALL);
1603           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1604         }
1605       return;
1606     }
1607
1608   if (CALL_P (insn) && !after_p)
1609     {
1610       if (!flag_asynchronous_unwind_tables)
1611         dwarf2out_args_size ("", args_size);
1612       return;
1613     }
1614   else if (BARRIER_P (insn))
1615     {
1616       /* Don't call compute_barrier_args_size () if the only
1617          BARRIER is at the end of function.  */
1618       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1619         compute_barrier_args_size ();
1620       if (barrier_args_size == NULL)
1621         offset = 0;
1622       else
1623         {
1624           offset = barrier_args_size[INSN_UID (insn)];
1625           if (offset < 0)
1626             offset = 0;
1627         }
1628
1629       offset -= args_size;
1630 #ifndef STACK_GROWS_DOWNWARD
1631       offset = -offset;
1632 #endif
1633     }
1634   else if (GET_CODE (PATTERN (insn)) == SET)
1635     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1636   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1637            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1638     {
1639       /* There may be stack adjustments inside compound insns.  Search
1640          for them.  */
1641       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1642         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1643           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1644                                          args_size, offset);
1645     }
1646   else
1647     return;
1648
1649   if (offset == 0)
1650     return;
1651
1652   label = dwarf2out_cfi_label (false);
1653   dwarf2out_stack_adjust (offset, label);
1654 }
1655
1656 /* We delay emitting a register save until either (a) we reach the end
1657    of the prologue or (b) the register is clobbered.  This clusters
1658    register saves so that there are fewer pc advances.  */
1659
1660 struct GTY(()) queued_reg_save {
1661   struct queued_reg_save *next;
1662   rtx reg;
1663   HOST_WIDE_INT cfa_offset;
1664   rtx saved_reg;
1665 };
1666
1667 static GTY(()) struct queued_reg_save *queued_reg_saves;
1668
1669 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1670 struct GTY(()) reg_saved_in_data {
1671   rtx orig_reg;
1672   rtx saved_in_reg;
1673 };
1674
1675 /* A list of registers saved in other registers.
1676    The list intentionally has a small maximum capacity of 4; if your
1677    port needs more than that, you might consider implementing a
1678    more efficient data structure.  */
1679 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1680 static GTY(()) size_t num_regs_saved_in_regs;
1681
1682 static const char *last_reg_save_label;
1683
1684 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1685    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1686
1687 static void
1688 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1689 {
1690   struct queued_reg_save *q;
1691
1692   /* Duplicates waste space, but it's also necessary to remove them
1693      for correctness, since the queue gets output in reverse
1694      order.  */
1695   for (q = queued_reg_saves; q != NULL; q = q->next)
1696     if (REGNO (q->reg) == REGNO (reg))
1697       break;
1698
1699   if (q == NULL)
1700     {
1701       q = ggc_alloc_queued_reg_save ();
1702       q->next = queued_reg_saves;
1703       queued_reg_saves = q;
1704     }
1705
1706   q->reg = reg;
1707   q->cfa_offset = offset;
1708   q->saved_reg = sreg;
1709
1710   last_reg_save_label = label;
1711 }
1712
1713 /* Output all the entries in QUEUED_REG_SAVES.  */
1714
1715 static void
1716 flush_queued_reg_saves (void)
1717 {
1718   struct queued_reg_save *q;
1719
1720   for (q = queued_reg_saves; q; q = q->next)
1721     {
1722       size_t i;
1723       unsigned int reg, sreg;
1724
1725       for (i = 0; i < num_regs_saved_in_regs; i++)
1726         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1727           break;
1728       if (q->saved_reg && i == num_regs_saved_in_regs)
1729         {
1730           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1731           num_regs_saved_in_regs++;
1732         }
1733       if (i != num_regs_saved_in_regs)
1734         {
1735           regs_saved_in_regs[i].orig_reg = q->reg;
1736           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1737         }
1738
1739       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1740       if (q->saved_reg)
1741         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1742       else
1743         sreg = INVALID_REGNUM;
1744       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1745     }
1746
1747   queued_reg_saves = NULL;
1748   last_reg_save_label = NULL;
1749 }
1750
1751 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1752    location for?  Or, does it clobber a register which we've previously
1753    said that some other register is saved in, and for which we now
1754    have a new location for?  */
1755
1756 static bool
1757 clobbers_queued_reg_save (const_rtx insn)
1758 {
1759   struct queued_reg_save *q;
1760
1761   for (q = queued_reg_saves; q; q = q->next)
1762     {
1763       size_t i;
1764       if (modified_in_p (q->reg, insn))
1765         return true;
1766       for (i = 0; i < num_regs_saved_in_regs; i++)
1767         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1768             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1769           return true;
1770     }
1771
1772   return false;
1773 }
1774
1775 /* Entry point for saving the first register into the second.  */
1776
1777 void
1778 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1779 {
1780   size_t i;
1781   unsigned int regno, sregno;
1782
1783   for (i = 0; i < num_regs_saved_in_regs; i++)
1784     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1785       break;
1786   if (i == num_regs_saved_in_regs)
1787     {
1788       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1789       num_regs_saved_in_regs++;
1790     }
1791   regs_saved_in_regs[i].orig_reg = reg;
1792   regs_saved_in_regs[i].saved_in_reg = sreg;
1793
1794   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1795   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1796   reg_save (label, regno, sregno, 0);
1797 }
1798
1799 /* What register, if any, is currently saved in REG?  */
1800
1801 static rtx
1802 reg_saved_in (rtx reg)
1803 {
1804   unsigned int regn = REGNO (reg);
1805   size_t i;
1806   struct queued_reg_save *q;
1807
1808   for (q = queued_reg_saves; q; q = q->next)
1809     if (q->saved_reg && regn == REGNO (q->saved_reg))
1810       return q->reg;
1811
1812   for (i = 0; i < num_regs_saved_in_regs; i++)
1813     if (regs_saved_in_regs[i].saved_in_reg
1814         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1815       return regs_saved_in_regs[i].orig_reg;
1816
1817   return NULL_RTX;
1818 }
1819
1820
1821 /* A temporary register holding an integral value used in adjusting SP
1822    or setting up the store_reg.  The "offset" field holds the integer
1823    value, not an offset.  */
1824 static dw_cfa_location cfa_temp;
1825
1826 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1827
1828 static void
1829 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1830 {
1831   memset (&cfa, 0, sizeof (cfa));
1832
1833   switch (GET_CODE (pat))
1834     {
1835     case PLUS:
1836       cfa.reg = REGNO (XEXP (pat, 0));
1837       cfa.offset = INTVAL (XEXP (pat, 1));
1838       break;
1839
1840     case REG:
1841       cfa.reg = REGNO (pat);
1842       break;
1843
1844     case MEM:
1845       cfa.indirect = 1;
1846       pat = XEXP (pat, 0);
1847       if (GET_CODE (pat) == PLUS)
1848         {
1849           cfa.base_offset = INTVAL (XEXP (pat, 1));
1850           pat = XEXP (pat, 0);
1851         }
1852       cfa.reg = REGNO (pat);
1853       break;
1854
1855     default:
1856       /* Recurse and define an expression.  */
1857       gcc_unreachable ();
1858     }
1859
1860   def_cfa_1 (label, &cfa);
1861 }
1862
1863 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1864
1865 static void
1866 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1867 {
1868   rtx src, dest;
1869
1870   gcc_assert (GET_CODE (pat) == SET);
1871   dest = XEXP (pat, 0);
1872   src = XEXP (pat, 1);
1873
1874   switch (GET_CODE (src))
1875     {
1876     case PLUS:
1877       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1878       cfa.offset -= INTVAL (XEXP (src, 1));
1879       break;
1880
1881     case REG:
1882         break;
1883
1884     default:
1885         gcc_unreachable ();
1886     }
1887
1888   cfa.reg = REGNO (dest);
1889   gcc_assert (cfa.indirect == 0);
1890
1891   def_cfa_1 (label, &cfa);
1892 }
1893
1894 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1895
1896 static void
1897 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1898 {
1899   HOST_WIDE_INT offset;
1900   rtx src, addr, span;
1901
1902   src = XEXP (set, 1);
1903   addr = XEXP (set, 0);
1904   gcc_assert (MEM_P (addr));
1905   addr = XEXP (addr, 0);
1906
1907   /* As documented, only consider extremely simple addresses.  */
1908   switch (GET_CODE (addr))
1909     {
1910     case REG:
1911       gcc_assert (REGNO (addr) == cfa.reg);
1912       offset = -cfa.offset;
1913       break;
1914     case PLUS:
1915       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1916       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1917       break;
1918     default:
1919       gcc_unreachable ();
1920     }
1921
1922   span = targetm.dwarf_register_span (src);
1923
1924   /* ??? We'd like to use queue_reg_save, but we need to come up with
1925      a different flushing heuristic for epilogues.  */
1926   if (!span)
1927     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1928   else
1929     {
1930       /* We have a PARALLEL describing where the contents of SRC live.
1931          Queue register saves for each piece of the PARALLEL.  */
1932       int par_index;
1933       int limit;
1934       HOST_WIDE_INT span_offset = offset;
1935
1936       gcc_assert (GET_CODE (span) == PARALLEL);
1937
1938       limit = XVECLEN (span, 0);
1939       for (par_index = 0; par_index < limit; par_index++)
1940         {
1941           rtx elem = XVECEXP (span, 0, par_index);
1942
1943           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1944                     INVALID_REGNUM, span_offset);
1945           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1946         }
1947     }
1948 }
1949
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1951
1952 static void
1953 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1954 {
1955   rtx src, dest;
1956   unsigned sregno, dregno;
1957
1958   src = XEXP (set, 1);
1959   dest = XEXP (set, 0);
1960
1961   if (src == pc_rtx)
1962     sregno = DWARF_FRAME_RETURN_COLUMN;
1963   else
1964     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1965
1966   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1967
1968   /* ??? We'd like to use queue_reg_save, but we need to come up with
1969      a different flushing heuristic for epilogues.  */
1970   reg_save (label, sregno, dregno, 0);
1971 }
1972
1973 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1974
1975 static void
1976 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1977 {
1978   rtx src, dest, span;
1979   dw_cfi_ref cfi = new_cfi ();
1980
1981   dest = SET_DEST (set);
1982   src = SET_SRC (set);
1983
1984   gcc_assert (REG_P (src));
1985   gcc_assert (MEM_P (dest));
1986
1987   span = targetm.dwarf_register_span (src);
1988   gcc_assert (!span);
1989
1990   cfi->dw_cfi_opc = DW_CFA_expression;
1991   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1992   cfi->dw_cfi_oprnd2.dw_cfi_loc
1993     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1994                           VAR_INIT_STATUS_INITIALIZED);
1995
1996   /* ??? We'd like to use queue_reg_save, were the interface different,
1997      and, as above, we could manage flushing for epilogues.  */
1998   add_fde_cfi (label, cfi);
1999 }
2000
2001 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2002
2003 static void
2004 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2005 {
2006   dw_cfi_ref cfi = new_cfi ();
2007   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2008
2009   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2010   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2011
2012   add_fde_cfi (label, cfi);
2013 }
2014
2015 /* Record call frame debugging information for an expression EXPR,
2016    which either sets SP or FP (adjusting how we calculate the frame
2017    address) or saves a register to the stack or another register.
2018    LABEL indicates the address of EXPR.
2019
2020    This function encodes a state machine mapping rtxes to actions on
2021    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2022    users need not read the source code.
2023
2024   The High-Level Picture
2025
2026   Changes in the register we use to calculate the CFA: Currently we
2027   assume that if you copy the CFA register into another register, we
2028   should take the other one as the new CFA register; this seems to
2029   work pretty well.  If it's wrong for some target, it's simple
2030   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2031
2032   Changes in the register we use for saving registers to the stack:
2033   This is usually SP, but not always.  Again, we deduce that if you
2034   copy SP into another register (and SP is not the CFA register),
2035   then the new register is the one we will be using for register
2036   saves.  This also seems to work.
2037
2038   Register saves: There's not much guesswork about this one; if
2039   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2040   register save, and the register used to calculate the destination
2041   had better be the one we think we're using for this purpose.
2042   It's also assumed that a copy from a call-saved register to another
2043   register is saving that register if RTX_FRAME_RELATED_P is set on
2044   that instruction.  If the copy is from a call-saved register to
2045   the *same* register, that means that the register is now the same
2046   value as in the caller.
2047
2048   Except: If the register being saved is the CFA register, and the
2049   offset is nonzero, we are saving the CFA, so we assume we have to
2050   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2051   the intent is to save the value of SP from the previous frame.
2052
2053   In addition, if a register has previously been saved to a different
2054   register,
2055
2056   Invariants / Summaries of Rules
2057
2058   cfa          current rule for calculating the CFA.  It usually
2059                consists of a register and an offset.
2060   cfa_store    register used by prologue code to save things to the stack
2061                cfa_store.offset is the offset from the value of
2062                cfa_store.reg to the actual CFA
2063   cfa_temp     register holding an integral value.  cfa_temp.offset
2064                stores the value, which will be used to adjust the
2065                stack pointer.  cfa_temp is also used like cfa_store,
2066                to track stores to the stack via fp or a temp reg.
2067
2068   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2069                with cfa.reg as the first operand changes the cfa.reg and its
2070                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2071                cfa_temp.offset.
2072
2073   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2074                expression yielding a constant.  This sets cfa_temp.reg
2075                and cfa_temp.offset.
2076
2077   Rule 5:      Create a new register cfa_store used to save items to the
2078                stack.
2079
2080   Rules 10-14: Save a register to the stack.  Define offset as the
2081                difference of the original location and cfa_store's
2082                location (or cfa_temp's location if cfa_temp is used).
2083
2084   Rules 16-20: If AND operation happens on sp in prologue, we assume
2085                stack is realigned.  We will use a group of DW_OP_XXX
2086                expressions to represent the location of the stored
2087                register instead of CFA+offset.
2088
2089   The Rules
2090
2091   "{a,b}" indicates a choice of a xor b.
2092   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2093
2094   Rule 1:
2095   (set <reg1> <reg2>:cfa.reg)
2096   effects: cfa.reg = <reg1>
2097            cfa.offset unchanged
2098            cfa_temp.reg = <reg1>
2099            cfa_temp.offset = cfa.offset
2100
2101   Rule 2:
2102   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2103                               {<const_int>,<reg>:cfa_temp.reg}))
2104   effects: cfa.reg = sp if fp used
2105            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2106            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2107              if cfa_store.reg==sp
2108
2109   Rule 3:
2110   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2111   effects: cfa.reg = fp
2112            cfa_offset += +/- <const_int>
2113
2114   Rule 4:
2115   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2116   constraints: <reg1> != fp
2117                <reg1> != sp
2118   effects: cfa.reg = <reg1>
2119            cfa_temp.reg = <reg1>
2120            cfa_temp.offset = cfa.offset
2121
2122   Rule 5:
2123   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2124   constraints: <reg1> != fp
2125                <reg1> != sp
2126   effects: cfa_store.reg = <reg1>
2127            cfa_store.offset = cfa.offset - cfa_temp.offset
2128
2129   Rule 6:
2130   (set <reg> <const_int>)
2131   effects: cfa_temp.reg = <reg>
2132            cfa_temp.offset = <const_int>
2133
2134   Rule 7:
2135   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2136   effects: cfa_temp.reg = <reg1>
2137            cfa_temp.offset |= <const_int>
2138
2139   Rule 8:
2140   (set <reg> (high <exp>))
2141   effects: none
2142
2143   Rule 9:
2144   (set <reg> (lo_sum <exp> <const_int>))
2145   effects: cfa_temp.reg = <reg>
2146            cfa_temp.offset = <const_int>
2147
2148   Rule 10:
2149   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2150   effects: cfa_store.offset -= <const_int>
2151            cfa.offset = cfa_store.offset if cfa.reg == sp
2152            cfa.reg = sp
2153            cfa.base_offset = -cfa_store.offset
2154
2155   Rule 11:
2156   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2157   effects: cfa_store.offset += -/+ mode_size(mem)
2158            cfa.offset = cfa_store.offset if cfa.reg == sp
2159            cfa.reg = sp
2160            cfa.base_offset = -cfa_store.offset
2161
2162   Rule 12:
2163   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2164
2165        <reg2>)
2166   effects: cfa.reg = <reg1>
2167            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2168
2169   Rule 13:
2170   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2171   effects: cfa.reg = <reg1>
2172            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2173
2174   Rule 14:
2175   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2176   effects: cfa.reg = <reg1>
2177            cfa.base_offset = -cfa_temp.offset
2178            cfa_temp.offset -= mode_size(mem)
2179
2180   Rule 15:
2181   (set <reg> {unspec, unspec_volatile})
2182   effects: target-dependent
2183
2184   Rule 16:
2185   (set sp (and: sp <const_int>))
2186   constraints: cfa_store.reg == sp
2187   effects: current_fde.stack_realign = 1
2188            cfa_store.offset = 0
2189            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2190
2191   Rule 17:
2192   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2193   effects: cfa_store.offset += -/+ mode_size(mem)
2194
2195   Rule 18:
2196   (set (mem ({pre_inc, pre_dec} sp)) fp)
2197   constraints: fde->stack_realign == 1
2198   effects: cfa_store.offset = 0
2199            cfa.reg != HARD_FRAME_POINTER_REGNUM
2200
2201   Rule 19:
2202   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2203   constraints: fde->stack_realign == 1
2204                && cfa.offset == 0
2205                && cfa.indirect == 0
2206                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2207   effects: Use DW_CFA_def_cfa_expression to define cfa
2208            cfa.reg == fde->drap_reg  */
2209
2210 static void
2211 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2212 {
2213   rtx src, dest, span;
2214   HOST_WIDE_INT offset;
2215   dw_fde_ref fde;
2216
2217   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2218      the PARALLEL independently. The first element is always processed if
2219      it is a SET. This is for backward compatibility.   Other elements
2220      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2221      flag is set in them.  */
2222   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2223     {
2224       int par_index;
2225       int limit = XVECLEN (expr, 0);
2226       rtx elem;
2227
2228       /* PARALLELs have strict read-modify-write semantics, so we
2229          ought to evaluate every rvalue before changing any lvalue.
2230          It's cumbersome to do that in general, but there's an
2231          easy approximation that is enough for all current users:
2232          handle register saves before register assignments.  */
2233       if (GET_CODE (expr) == PARALLEL)
2234         for (par_index = 0; par_index < limit; par_index++)
2235           {
2236             elem = XVECEXP (expr, 0, par_index);
2237             if (GET_CODE (elem) == SET
2238                 && MEM_P (SET_DEST (elem))
2239                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2240               dwarf2out_frame_debug_expr (elem, label);
2241           }
2242
2243       for (par_index = 0; par_index < limit; par_index++)
2244         {
2245           elem = XVECEXP (expr, 0, par_index);
2246           if (GET_CODE (elem) == SET
2247               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2248               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2249             dwarf2out_frame_debug_expr (elem, label);
2250           else if (GET_CODE (elem) == SET
2251                    && par_index != 0
2252                    && !RTX_FRAME_RELATED_P (elem))
2253             {
2254               /* Stack adjustment combining might combine some post-prologue
2255                  stack adjustment into a prologue stack adjustment.  */
2256               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2257
2258               if (offset != 0)
2259                 dwarf2out_stack_adjust (offset, label);
2260             }
2261         }
2262       return;
2263     }
2264
2265   gcc_assert (GET_CODE (expr) == SET);
2266
2267   src = SET_SRC (expr);
2268   dest = SET_DEST (expr);
2269
2270   if (REG_P (src))
2271     {
2272       rtx rsi = reg_saved_in (src);
2273       if (rsi)
2274         src = rsi;
2275     }
2276
2277   fde = current_fde ();
2278
2279   switch (GET_CODE (dest))
2280     {
2281     case REG:
2282       switch (GET_CODE (src))
2283         {
2284           /* Setting FP from SP.  */
2285         case REG:
2286           if (cfa.reg == (unsigned) REGNO (src))
2287             {
2288               /* Rule 1 */
2289               /* Update the CFA rule wrt SP or FP.  Make sure src is
2290                  relative to the current CFA register.
2291
2292                  We used to require that dest be either SP or FP, but the
2293                  ARM copies SP to a temporary register, and from there to
2294                  FP.  So we just rely on the backends to only set
2295                  RTX_FRAME_RELATED_P on appropriate insns.  */
2296               cfa.reg = REGNO (dest);
2297               cfa_temp.reg = cfa.reg;
2298               cfa_temp.offset = cfa.offset;
2299             }
2300           else
2301             {
2302               /* Saving a register in a register.  */
2303               gcc_assert (!fixed_regs [REGNO (dest)]
2304                           /* For the SPARC and its register window.  */
2305                           || (DWARF_FRAME_REGNUM (REGNO (src))
2306                               == DWARF_FRAME_RETURN_COLUMN));
2307
2308               /* After stack is aligned, we can only save SP in FP
2309                  if drap register is used.  In this case, we have
2310                  to restore stack pointer with the CFA value and we
2311                  don't generate this DWARF information.  */
2312               if (fde
2313                   && fde->stack_realign
2314                   && REGNO (src) == STACK_POINTER_REGNUM)
2315                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2316                             && fde->drap_reg != INVALID_REGNUM
2317                             && cfa.reg != REGNO (src));
2318               else
2319                 queue_reg_save (label, src, dest, 0);
2320             }
2321           break;
2322
2323         case PLUS:
2324         case MINUS:
2325         case LO_SUM:
2326           if (dest == stack_pointer_rtx)
2327             {
2328               /* Rule 2 */
2329               /* Adjusting SP.  */
2330               switch (GET_CODE (XEXP (src, 1)))
2331                 {
2332                 case CONST_INT:
2333                   offset = INTVAL (XEXP (src, 1));
2334                   break;
2335                 case REG:
2336                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2337                               == cfa_temp.reg);
2338                   offset = cfa_temp.offset;
2339                   break;
2340                 default:
2341                   gcc_unreachable ();
2342                 }
2343
2344               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2345                 {
2346                   /* Restoring SP from FP in the epilogue.  */
2347                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2348                   cfa.reg = STACK_POINTER_REGNUM;
2349                 }
2350               else if (GET_CODE (src) == LO_SUM)
2351                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2352                 ;
2353               else
2354                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2355
2356               if (GET_CODE (src) != MINUS)
2357                 offset = -offset;
2358               if (cfa.reg == STACK_POINTER_REGNUM)
2359                 cfa.offset += offset;
2360               if (cfa_store.reg == STACK_POINTER_REGNUM)
2361                 cfa_store.offset += offset;
2362             }
2363           else if (dest == hard_frame_pointer_rtx)
2364             {
2365               /* Rule 3 */
2366               /* Either setting the FP from an offset of the SP,
2367                  or adjusting the FP */
2368               gcc_assert (frame_pointer_needed);
2369
2370               gcc_assert (REG_P (XEXP (src, 0))
2371                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2372                           && CONST_INT_P (XEXP (src, 1)));
2373               offset = INTVAL (XEXP (src, 1));
2374               if (GET_CODE (src) != MINUS)
2375                 offset = -offset;
2376               cfa.offset += offset;
2377               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2378             }
2379           else
2380             {
2381               gcc_assert (GET_CODE (src) != MINUS);
2382
2383               /* Rule 4 */
2384               if (REG_P (XEXP (src, 0))
2385                   && REGNO (XEXP (src, 0)) == cfa.reg
2386                   && CONST_INT_P (XEXP (src, 1)))
2387                 {
2388                   /* Setting a temporary CFA register that will be copied
2389                      into the FP later on.  */
2390                   offset = - INTVAL (XEXP (src, 1));
2391                   cfa.offset += offset;
2392                   cfa.reg = REGNO (dest);
2393                   /* Or used to save regs to the stack.  */
2394                   cfa_temp.reg = cfa.reg;
2395                   cfa_temp.offset = cfa.offset;
2396                 }
2397
2398               /* Rule 5 */
2399               else if (REG_P (XEXP (src, 0))
2400                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2401                        && XEXP (src, 1) == stack_pointer_rtx)
2402                 {
2403                   /* Setting a scratch register that we will use instead
2404                      of SP for saving registers to the stack.  */
2405                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2406                   cfa_store.reg = REGNO (dest);
2407                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2408                 }
2409
2410               /* Rule 9 */
2411               else if (GET_CODE (src) == LO_SUM
2412                        && CONST_INT_P (XEXP (src, 1)))
2413                 {
2414                   cfa_temp.reg = REGNO (dest);
2415                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2416                 }
2417               else
2418                 gcc_unreachable ();
2419             }
2420           break;
2421
2422           /* Rule 6 */
2423         case CONST_INT:
2424           cfa_temp.reg = REGNO (dest);
2425           cfa_temp.offset = INTVAL (src);
2426           break;
2427
2428           /* Rule 7 */
2429         case IOR:
2430           gcc_assert (REG_P (XEXP (src, 0))
2431                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2432                       && CONST_INT_P (XEXP (src, 1)));
2433
2434           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2435             cfa_temp.reg = REGNO (dest);
2436           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2437           break;
2438
2439           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2440              which will fill in all of the bits.  */
2441           /* Rule 8 */
2442         case HIGH:
2443           break;
2444
2445           /* Rule 15 */
2446         case UNSPEC:
2447         case UNSPEC_VOLATILE:
2448           gcc_assert (targetm.dwarf_handle_frame_unspec);
2449           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2450           return;
2451
2452           /* Rule 16 */
2453         case AND:
2454           /* If this AND operation happens on stack pointer in prologue,
2455              we assume the stack is realigned and we extract the
2456              alignment.  */
2457           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2458             {
2459               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2460               fde->stack_realign = 1;
2461               fde->stack_realignment = INTVAL (XEXP (src, 1));
2462               cfa_store.offset = 0;
2463
2464               if (cfa.reg != STACK_POINTER_REGNUM
2465                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2466                 fde->drap_reg = cfa.reg;
2467             }
2468           return;
2469
2470         default:
2471           gcc_unreachable ();
2472         }
2473
2474       def_cfa_1 (label, &cfa);
2475       break;
2476
2477     case MEM:
2478
2479       /* Saving a register to the stack.  Make sure dest is relative to the
2480          CFA register.  */
2481       switch (GET_CODE (XEXP (dest, 0)))
2482         {
2483           /* Rule 10 */
2484           /* With a push.  */
2485         case PRE_MODIFY:
2486           /* We can't handle variable size modifications.  */
2487           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2488                       == CONST_INT);
2489           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2490
2491           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2492                       && cfa_store.reg == STACK_POINTER_REGNUM);
2493
2494           cfa_store.offset += offset;
2495           if (cfa.reg == STACK_POINTER_REGNUM)
2496             cfa.offset = cfa_store.offset;
2497
2498           offset = -cfa_store.offset;
2499           break;
2500
2501           /* Rule 11 */
2502         case PRE_INC:
2503         case PRE_DEC:
2504           offset = GET_MODE_SIZE (GET_MODE (dest));
2505           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2506             offset = -offset;
2507
2508           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2509                        == STACK_POINTER_REGNUM)
2510                       && cfa_store.reg == STACK_POINTER_REGNUM);
2511
2512           cfa_store.offset += offset;
2513
2514           /* Rule 18: If stack is aligned, we will use FP as a
2515              reference to represent the address of the stored
2516              regiser.  */
2517           if (fde
2518               && fde->stack_realign
2519               && src == hard_frame_pointer_rtx)
2520             {
2521               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2522               cfa_store.offset = 0;
2523             }
2524
2525           if (cfa.reg == STACK_POINTER_REGNUM)
2526             cfa.offset = cfa_store.offset;
2527
2528           offset = -cfa_store.offset;
2529           break;
2530
2531           /* Rule 12 */
2532           /* With an offset.  */
2533         case PLUS:
2534         case MINUS:
2535         case LO_SUM:
2536           {
2537             int regno;
2538
2539             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2540                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2541             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2542             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2543               offset = -offset;
2544
2545             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2546
2547             if (cfa_store.reg == (unsigned) regno)
2548               offset -= cfa_store.offset;
2549             else
2550               {
2551                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2552                 offset -= cfa_temp.offset;
2553               }
2554           }
2555           break;
2556
2557           /* Rule 13 */
2558           /* Without an offset.  */
2559         case REG:
2560           {
2561             int regno = REGNO (XEXP (dest, 0));
2562
2563             if (cfa_store.reg == (unsigned) regno)
2564               offset = -cfa_store.offset;
2565             else
2566               {
2567                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2568                 offset = -cfa_temp.offset;
2569               }
2570           }
2571           break;
2572
2573           /* Rule 14 */
2574         case POST_INC:
2575           gcc_assert (cfa_temp.reg
2576                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2577           offset = -cfa_temp.offset;
2578           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2579           break;
2580
2581         default:
2582           gcc_unreachable ();
2583         }
2584
2585         /* Rule 17 */
2586         /* If the source operand of this MEM operation is not a
2587            register, basically the source is return address.  Here
2588            we only care how much stack grew and we don't save it.  */
2589       if (!REG_P (src))
2590         break;
2591
2592       if (REGNO (src) != STACK_POINTER_REGNUM
2593           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2594           && (unsigned) REGNO (src) == cfa.reg)
2595         {
2596           /* We're storing the current CFA reg into the stack.  */
2597
2598           if (cfa.offset == 0)
2599             {
2600               /* Rule 19 */
2601               /* If stack is aligned, putting CFA reg into stack means
2602                  we can no longer use reg + offset to represent CFA.
2603                  Here we use DW_CFA_def_cfa_expression instead.  The
2604                  result of this expression equals to the original CFA
2605                  value.  */
2606               if (fde
2607                   && fde->stack_realign
2608                   && cfa.indirect == 0
2609                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2610                 {
2611                   dw_cfa_location cfa_exp;
2612
2613                   gcc_assert (fde->drap_reg == cfa.reg);
2614
2615                   cfa_exp.indirect = 1;
2616                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2617                   cfa_exp.base_offset = offset;
2618                   cfa_exp.offset = 0;
2619
2620                   fde->drap_reg_saved = 1;
2621
2622                   def_cfa_1 (label, &cfa_exp);
2623                   break;
2624                 }
2625
2626               /* If the source register is exactly the CFA, assume
2627                  we're saving SP like any other register; this happens
2628                  on the ARM.  */
2629               def_cfa_1 (label, &cfa);
2630               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2631               break;
2632             }
2633           else
2634             {
2635               /* Otherwise, we'll need to look in the stack to
2636                  calculate the CFA.  */
2637               rtx x = XEXP (dest, 0);
2638
2639               if (!REG_P (x))
2640                 x = XEXP (x, 0);
2641               gcc_assert (REG_P (x));
2642
2643               cfa.reg = REGNO (x);
2644               cfa.base_offset = offset;
2645               cfa.indirect = 1;
2646               def_cfa_1 (label, &cfa);
2647               break;
2648             }
2649         }
2650
2651       def_cfa_1 (label, &cfa);
2652       {
2653         span = targetm.dwarf_register_span (src);
2654
2655         if (!span)
2656           queue_reg_save (label, src, NULL_RTX, offset);
2657         else
2658           {
2659             /* We have a PARALLEL describing where the contents of SRC
2660                live.  Queue register saves for each piece of the
2661                PARALLEL.  */
2662             int par_index;
2663             int limit;
2664             HOST_WIDE_INT span_offset = offset;
2665
2666             gcc_assert (GET_CODE (span) == PARALLEL);
2667
2668             limit = XVECLEN (span, 0);
2669             for (par_index = 0; par_index < limit; par_index++)
2670               {
2671                 rtx elem = XVECEXP (span, 0, par_index);
2672
2673                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2674                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2675               }
2676           }
2677       }
2678       break;
2679
2680     default:
2681       gcc_unreachable ();
2682     }
2683 }
2684
2685 /* Record call frame debugging information for INSN, which either
2686    sets SP or FP (adjusting how we calculate the frame address) or saves a
2687    register to the stack.  If INSN is NULL_RTX, initialize our state.
2688
2689    If AFTER_P is false, we're being called before the insn is emitted,
2690    otherwise after.  Call instructions get invoked twice.  */
2691
2692 void
2693 dwarf2out_frame_debug (rtx insn, bool after_p)
2694 {
2695   const char *label;
2696   rtx note, n;
2697   bool handled_one = false;
2698
2699   if (insn == NULL_RTX)
2700     {
2701       size_t i;
2702
2703       /* Flush any queued register saves.  */
2704       flush_queued_reg_saves ();
2705
2706       /* Set up state for generating call frame debug info.  */
2707       lookup_cfa (&cfa);
2708       gcc_assert (cfa.reg
2709                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2710
2711       cfa.reg = STACK_POINTER_REGNUM;
2712       cfa_store = cfa;
2713       cfa_temp.reg = -1;
2714       cfa_temp.offset = 0;
2715
2716       for (i = 0; i < num_regs_saved_in_regs; i++)
2717         {
2718           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2719           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2720         }
2721       num_regs_saved_in_regs = 0;
2722
2723       if (barrier_args_size)
2724         {
2725           XDELETEVEC (barrier_args_size);
2726           barrier_args_size = NULL;
2727         }
2728       return;
2729     }
2730
2731   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2732     flush_queued_reg_saves ();
2733
2734   if (!RTX_FRAME_RELATED_P (insn))
2735     {
2736       /* ??? This should be done unconditionally since stack adjustments
2737          matter if the stack pointer is not the CFA register anymore but
2738          is still used to save registers.  */
2739       if (!ACCUMULATE_OUTGOING_ARGS)
2740         dwarf2out_notice_stack_adjust (insn, after_p);
2741       return;
2742     }
2743
2744   label = dwarf2out_cfi_label (false);
2745
2746   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2747     switch (REG_NOTE_KIND (note))
2748       {
2749       case REG_FRAME_RELATED_EXPR:
2750         insn = XEXP (note, 0);
2751         goto found;
2752
2753       case REG_CFA_DEF_CFA:
2754         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2755         handled_one = true;
2756         break;
2757
2758       case REG_CFA_ADJUST_CFA:
2759         n = XEXP (note, 0);
2760         if (n == NULL)
2761           {
2762             n = PATTERN (insn);
2763             if (GET_CODE (n) == PARALLEL)
2764               n = XVECEXP (n, 0, 0);
2765           }
2766         dwarf2out_frame_debug_adjust_cfa (n, label);
2767         handled_one = true;
2768         break;
2769
2770       case REG_CFA_OFFSET:
2771         n = XEXP (note, 0);
2772         if (n == NULL)
2773           n = single_set (insn);
2774         dwarf2out_frame_debug_cfa_offset (n, label);
2775         handled_one = true;
2776         break;
2777
2778       case REG_CFA_REGISTER:
2779         n = XEXP (note, 0);
2780         if (n == NULL)
2781           {
2782             n = PATTERN (insn);
2783             if (GET_CODE (n) == PARALLEL)
2784               n = XVECEXP (n, 0, 0);
2785           }
2786         dwarf2out_frame_debug_cfa_register (n, label);
2787         handled_one = true;
2788         break;
2789
2790       case REG_CFA_EXPRESSION:
2791         n = XEXP (note, 0);
2792         if (n == NULL)
2793           n = single_set (insn);
2794         dwarf2out_frame_debug_cfa_expression (n, label);
2795         handled_one = true;
2796         break;
2797
2798       case REG_CFA_RESTORE:
2799         n = XEXP (note, 0);
2800         if (n == NULL)
2801           {
2802             n = PATTERN (insn);
2803             if (GET_CODE (n) == PARALLEL)
2804               n = XVECEXP (n, 0, 0);
2805             n = XEXP (n, 0);
2806           }
2807         dwarf2out_frame_debug_cfa_restore (n, label);
2808         handled_one = true;
2809         break;
2810
2811       case REG_CFA_SET_VDRAP:
2812         n = XEXP (note, 0);
2813         if (REG_P (n))
2814           {
2815             dw_fde_ref fde = current_fde ();
2816             if (fde)
2817               {
2818                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2819                 if (REG_P (n))
2820                   fde->vdrap_reg = REGNO (n);
2821               }
2822           }
2823         handled_one = true;
2824         break;
2825
2826       default:
2827         break;
2828       }
2829   if (handled_one)
2830     return;
2831
2832   insn = PATTERN (insn);
2833  found:
2834   dwarf2out_frame_debug_expr (insn, label);
2835
2836   /* Check again.  A parallel can save and update the same register.
2837      We could probably check just once, here, but this is safer than
2838      removing the check above.  */
2839   if (clobbers_queued_reg_save (insn))
2840     flush_queued_reg_saves ();
2841 }
2842
2843 /* Determine if we need to save and restore CFI information around this
2844    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2845    we do need to save/restore, then emit the save now, and insert a
2846    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2847
2848 void
2849 dwarf2out_cfi_begin_epilogue (rtx insn)
2850 {
2851   bool saw_frp = false;
2852   rtx i;
2853
2854   /* Scan forward to the return insn, noticing if there are possible
2855      frame related insns.  */
2856   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2857     {
2858       if (!INSN_P (i))
2859         continue;
2860
2861       /* Look for both regular and sibcalls to end the block.  */
2862       if (returnjump_p (i))
2863         break;
2864       if (CALL_P (i) && SIBLING_CALL_P (i))
2865         break;
2866
2867       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2868         {
2869           int idx;
2870           rtx seq = PATTERN (i);
2871
2872           if (returnjump_p (XVECEXP (seq, 0, 0)))
2873             break;
2874           if (CALL_P (XVECEXP (seq, 0, 0))
2875               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2876             break;
2877
2878           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2879             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2880               saw_frp = true;
2881         }
2882
2883       if (RTX_FRAME_RELATED_P (i))
2884         saw_frp = true;
2885     }
2886
2887   /* If the port doesn't emit epilogue unwind info, we don't need a
2888      save/restore pair.  */
2889   if (!saw_frp)
2890     return;
2891
2892   /* Otherwise, search forward to see if the return insn was the last
2893      basic block of the function.  If so, we don't need save/restore.  */
2894   gcc_assert (i != NULL);
2895   i = next_real_insn (i);
2896   if (i == NULL)
2897     return;
2898
2899   /* Insert the restore before that next real insn in the stream, and before
2900      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2901      properly nested.  This should be after any label or alignment.  This
2902      will be pushed into the CFI stream by the function below.  */
2903   while (1)
2904     {
2905       rtx p = PREV_INSN (i);
2906       if (!NOTE_P (p))
2907         break;
2908       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2909         break;
2910       i = p;
2911     }
2912   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2913
2914   emit_cfa_remember = true;
2915
2916   /* And emulate the state save.  */
2917   gcc_assert (!cfa_remember.in_use);
2918   cfa_remember = cfa;
2919   cfa_remember.in_use = 1;
2920 }
2921
2922 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2923    required.  */
2924
2925 void
2926 dwarf2out_frame_debug_restore_state (void)
2927 {
2928   dw_cfi_ref cfi = new_cfi ();
2929   const char *label = dwarf2out_cfi_label (false);
2930
2931   cfi->dw_cfi_opc = DW_CFA_restore_state;
2932   add_fde_cfi (label, cfi);
2933
2934   gcc_assert (cfa_remember.in_use);
2935   cfa = cfa_remember;
2936   cfa_remember.in_use = 0;
2937 }
2938
2939 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2940 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2941  (enum dwarf_call_frame_info cfi);
2942
2943 static enum dw_cfi_oprnd_type
2944 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2945 {
2946   switch (cfi)
2947     {
2948     case DW_CFA_nop:
2949     case DW_CFA_GNU_window_save:
2950     case DW_CFA_remember_state:
2951     case DW_CFA_restore_state:
2952       return dw_cfi_oprnd_unused;
2953
2954     case DW_CFA_set_loc:
2955     case DW_CFA_advance_loc1:
2956     case DW_CFA_advance_loc2:
2957     case DW_CFA_advance_loc4:
2958     case DW_CFA_MIPS_advance_loc8:
2959       return dw_cfi_oprnd_addr;
2960
2961     case DW_CFA_offset:
2962     case DW_CFA_offset_extended:
2963     case DW_CFA_def_cfa:
2964     case DW_CFA_offset_extended_sf:
2965     case DW_CFA_def_cfa_sf:
2966     case DW_CFA_restore:
2967     case DW_CFA_restore_extended:
2968     case DW_CFA_undefined:
2969     case DW_CFA_same_value:
2970     case DW_CFA_def_cfa_register:
2971     case DW_CFA_register:
2972     case DW_CFA_expression:
2973       return dw_cfi_oprnd_reg_num;
2974
2975     case DW_CFA_def_cfa_offset:
2976     case DW_CFA_GNU_args_size:
2977     case DW_CFA_def_cfa_offset_sf:
2978       return dw_cfi_oprnd_offset;
2979
2980     case DW_CFA_def_cfa_expression:
2981       return dw_cfi_oprnd_loc;
2982
2983     default:
2984       gcc_unreachable ();
2985     }
2986 }
2987
2988 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2989 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2990  (enum dwarf_call_frame_info cfi);
2991
2992 static enum dw_cfi_oprnd_type
2993 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2994 {
2995   switch (cfi)
2996     {
2997     case DW_CFA_def_cfa:
2998     case DW_CFA_def_cfa_sf:
2999     case DW_CFA_offset:
3000     case DW_CFA_offset_extended_sf:
3001     case DW_CFA_offset_extended:
3002       return dw_cfi_oprnd_offset;
3003
3004     case DW_CFA_register:
3005       return dw_cfi_oprnd_reg_num;
3006
3007     case DW_CFA_expression:
3008       return dw_cfi_oprnd_loc;
3009
3010     default:
3011       return dw_cfi_oprnd_unused;
3012     }
3013 }
3014
3015 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3016    switch to the data section instead, and write out a synthetic start label
3017    for collect2 the first time around.  */
3018
3019 static void
3020 switch_to_eh_frame_section (bool back)
3021 {
3022   tree label;
3023
3024 #ifdef EH_FRAME_SECTION_NAME
3025   if (eh_frame_section == 0)
3026     {
3027       int flags;
3028
3029       if (EH_TABLES_CAN_BE_READ_ONLY)
3030         {
3031           int fde_encoding;
3032           int per_encoding;
3033           int lsda_encoding;
3034
3035           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3036                                                        /*global=*/0);
3037           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3038                                                        /*global=*/1);
3039           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3040                                                         /*global=*/0);
3041           flags = ((! flag_pic
3042                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3043                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3044                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3045                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3046                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3047                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3048                    ? 0 : SECTION_WRITE);
3049         }
3050       else
3051         flags = SECTION_WRITE;
3052       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3053     }
3054 #endif /* EH_FRAME_SECTION_NAME */
3055
3056   if (eh_frame_section)
3057     switch_to_section (eh_frame_section);
3058   else
3059     {
3060       /* We have no special eh_frame section.  Put the information in
3061          the data section and emit special labels to guide collect2.  */
3062       switch_to_section (data_section);
3063
3064       if (!back)
3065         {
3066           label = get_file_function_name ("F");
3067           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3068           targetm.asm_out.globalize_label (asm_out_file,
3069                                            IDENTIFIER_POINTER (label));
3070           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3071         }
3072     }
3073 }
3074
3075 /* Switch [BACK] to the eh or debug frame table section, depending on
3076    FOR_EH.  */
3077
3078 static void
3079 switch_to_frame_table_section (int for_eh, bool back)
3080 {
3081   if (for_eh)
3082     switch_to_eh_frame_section (back);
3083   else
3084     {
3085       if (!debug_frame_section)
3086         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3087                                            SECTION_DEBUG, NULL);
3088       switch_to_section (debug_frame_section);
3089     }
3090 }
3091
3092 /* Output a Call Frame Information opcode and its operand(s).  */
3093
3094 static void
3095 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3096 {
3097   unsigned long r;
3098   HOST_WIDE_INT off;
3099
3100   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3101     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3102                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3103                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3104                          ((unsigned HOST_WIDE_INT)
3105                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3106   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3107     {
3108       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3109       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3110                            "DW_CFA_offset, column %#lx", r);
3111       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3112       dw2_asm_output_data_uleb128 (off, NULL);
3113     }
3114   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3115     {
3116       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3117       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3118                            "DW_CFA_restore, column %#lx", r);
3119     }
3120   else
3121     {
3122       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3123                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3124
3125       switch (cfi->dw_cfi_opc)
3126         {
3127         case DW_CFA_set_loc:
3128           if (for_eh)
3129             dw2_asm_output_encoded_addr_rtx (
3130                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3131                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3132                 false, NULL);
3133           else
3134             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3135                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3136           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3137           break;
3138
3139         case DW_CFA_advance_loc1:
3140           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3141                                 fde->dw_fde_current_label, NULL);
3142           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3143           break;
3144
3145         case DW_CFA_advance_loc2:
3146           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3147                                 fde->dw_fde_current_label, NULL);
3148           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3149           break;
3150
3151         case DW_CFA_advance_loc4:
3152           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3153                                 fde->dw_fde_current_label, NULL);
3154           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3155           break;
3156
3157         case DW_CFA_MIPS_advance_loc8:
3158           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3159                                 fde->dw_fde_current_label, NULL);
3160           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3161           break;
3162
3163         case DW_CFA_offset_extended:
3164           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3165           dw2_asm_output_data_uleb128 (r, NULL);
3166           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3167           dw2_asm_output_data_uleb128 (off, NULL);
3168           break;
3169
3170         case DW_CFA_def_cfa:
3171           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3172           dw2_asm_output_data_uleb128 (r, NULL);
3173           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3174           break;
3175
3176         case DW_CFA_offset_extended_sf:
3177           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3178           dw2_asm_output_data_uleb128 (r, NULL);
3179           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3180           dw2_asm_output_data_sleb128 (off, NULL);
3181           break;
3182
3183         case DW_CFA_def_cfa_sf:
3184           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3185           dw2_asm_output_data_uleb128 (r, NULL);
3186           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3187           dw2_asm_output_data_sleb128 (off, NULL);
3188           break;
3189
3190         case DW_CFA_restore_extended:
3191         case DW_CFA_undefined:
3192         case DW_CFA_same_value:
3193         case DW_CFA_def_cfa_register:
3194           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3195           dw2_asm_output_data_uleb128 (r, NULL);
3196           break;
3197
3198         case DW_CFA_register:
3199           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3200           dw2_asm_output_data_uleb128 (r, NULL);
3201           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3202           dw2_asm_output_data_uleb128 (r, NULL);
3203           break;
3204
3205         case DW_CFA_def_cfa_offset:
3206         case DW_CFA_GNU_args_size:
3207           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3208           break;
3209
3210         case DW_CFA_def_cfa_offset_sf:
3211           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3212           dw2_asm_output_data_sleb128 (off, NULL);
3213           break;
3214
3215         case DW_CFA_GNU_window_save:
3216           break;
3217
3218         case DW_CFA_def_cfa_expression:
3219         case DW_CFA_expression:
3220           output_cfa_loc (cfi);
3221           break;
3222
3223         case DW_CFA_GNU_negative_offset_extended:
3224           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3225           gcc_unreachable ();
3226
3227         default:
3228           break;
3229         }
3230     }
3231 }
3232
3233 /* Similar, but do it via assembler directives instead.  */
3234
3235 static void
3236 output_cfi_directive (dw_cfi_ref cfi)
3237 {
3238   unsigned long r, r2;
3239
3240   switch (cfi->dw_cfi_opc)
3241     {
3242     case DW_CFA_advance_loc:
3243     case DW_CFA_advance_loc1:
3244     case DW_CFA_advance_loc2:
3245     case DW_CFA_advance_loc4:
3246     case DW_CFA_MIPS_advance_loc8:
3247     case DW_CFA_set_loc:
3248       /* Should only be created by add_fde_cfi in a code path not
3249          followed when emitting via directives.  The assembler is
3250          going to take care of this for us.  */
3251       gcc_unreachable ();
3252
3253     case DW_CFA_offset:
3254     case DW_CFA_offset_extended:
3255     case DW_CFA_offset_extended_sf:
3256       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3257       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3258                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3259       break;
3260
3261     case DW_CFA_restore:
3262     case DW_CFA_restore_extended:
3263       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3264       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3265       break;
3266
3267     case DW_CFA_undefined:
3268       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3269       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3270       break;
3271
3272     case DW_CFA_same_value:
3273       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3274       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3275       break;
3276
3277     case DW_CFA_def_cfa:
3278     case DW_CFA_def_cfa_sf:
3279       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3280       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3281                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3282       break;
3283
3284     case DW_CFA_def_cfa_register:
3285       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3286       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3287       break;
3288
3289     case DW_CFA_register:
3290       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3291       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3292       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3293       break;
3294
3295     case DW_CFA_def_cfa_offset:
3296     case DW_CFA_def_cfa_offset_sf:
3297       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3298                HOST_WIDE_INT_PRINT_DEC"\n",
3299                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3300       break;
3301
3302     case DW_CFA_remember_state:
3303       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3304       break;
3305     case DW_CFA_restore_state:
3306       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3307       break;
3308
3309     case DW_CFA_GNU_args_size:
3310       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3311       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3312       if (flag_debug_asm)
3313         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3314                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3315       fputc ('\n', asm_out_file);
3316       break;
3317
3318     case DW_CFA_GNU_window_save:
3319       fprintf (asm_out_file, "\t.cfi_window_save\n");
3320       break;
3321
3322     case DW_CFA_def_cfa_expression:
3323     case DW_CFA_expression:
3324       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3325       output_cfa_loc_raw (cfi);
3326       fputc ('\n', asm_out_file);
3327       break;
3328
3329     default:
3330       gcc_unreachable ();
3331     }
3332 }
3333
3334 DEF_VEC_P (dw_cfi_ref);
3335 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3336
3337 /* Output CFIs to bring current FDE to the same state as after executing
3338    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3339    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3340    other arguments to pass to output_cfi.  */
3341
3342 static void
3343 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3344 {
3345   struct dw_cfi_struct cfi_buf;
3346   dw_cfi_ref cfi2;
3347   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3348   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3349   unsigned int len, idx;
3350
3351   for (;; cfi = cfi->dw_cfi_next)
3352     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3353       {
3354       case DW_CFA_advance_loc:
3355       case DW_CFA_advance_loc1:
3356       case DW_CFA_advance_loc2:
3357       case DW_CFA_advance_loc4:
3358       case DW_CFA_MIPS_advance_loc8:
3359       case DW_CFA_set_loc:
3360         /* All advances should be ignored.  */
3361         break;
3362       case DW_CFA_remember_state:
3363         {
3364           dw_cfi_ref args_size = cfi_args_size;
3365
3366           /* Skip everything between .cfi_remember_state and
3367              .cfi_restore_state.  */
3368           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3369             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3370               break;
3371             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3372               args_size = cfi2;
3373             else
3374               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3375
3376           if (cfi2 == NULL)
3377             goto flush_all;
3378           else
3379             {
3380               cfi = cfi2;
3381               cfi_args_size = args_size;
3382             }
3383           break;
3384         }
3385       case DW_CFA_GNU_args_size:
3386         cfi_args_size = cfi;
3387         break;
3388       case DW_CFA_GNU_window_save:
3389         goto flush_all;
3390       case DW_CFA_offset:
3391       case DW_CFA_offset_extended:
3392       case DW_CFA_offset_extended_sf:
3393       case DW_CFA_restore:
3394       case DW_CFA_restore_extended:
3395       case DW_CFA_undefined:
3396       case DW_CFA_same_value:
3397       case DW_CFA_register:
3398       case DW_CFA_val_offset:
3399       case DW_CFA_val_offset_sf:
3400       case DW_CFA_expression:
3401       case DW_CFA_val_expression:
3402       case DW_CFA_GNU_negative_offset_extended:
3403         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3404           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3405                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3406         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3407         break;
3408       case DW_CFA_def_cfa:
3409       case DW_CFA_def_cfa_sf:
3410       case DW_CFA_def_cfa_expression:
3411         cfi_cfa = cfi;
3412         cfi_cfa_offset = cfi;
3413         break;
3414       case DW_CFA_def_cfa_register:
3415         cfi_cfa = cfi;
3416         break;
3417       case DW_CFA_def_cfa_offset:
3418       case DW_CFA_def_cfa_offset_sf:
3419         cfi_cfa_offset = cfi;
3420         break;
3421       case DW_CFA_nop:
3422         gcc_assert (cfi == NULL);
3423       flush_all:
3424         len = VEC_length (dw_cfi_ref, regs);
3425         for (idx = 0; idx < len; idx++)
3426           {
3427             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3428             if (cfi2 != NULL
3429                 && cfi2->dw_cfi_opc != DW_CFA_restore
3430                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3431               {
3432                 if (do_cfi_asm)
3433                   output_cfi_directive (cfi2);
3434                 else
3435                   output_cfi (cfi2, fde, for_eh);
3436               }
3437           }
3438         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3439           {
3440             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3441             cfi_buf = *cfi_cfa;
3442             switch (cfi_cfa_offset->dw_cfi_opc)
3443               {
3444               case DW_CFA_def_cfa_offset:
3445                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3446                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3447                 break;
3448               case DW_CFA_def_cfa_offset_sf:
3449                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3450                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3451                 break;
3452               case DW_CFA_def_cfa:
3453               case DW_CFA_def_cfa_sf:
3454                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3455                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3456                 break;
3457               default:
3458                 gcc_unreachable ();
3459               }
3460             cfi_cfa = &cfi_buf;
3461           }
3462         else if (cfi_cfa_offset)
3463           cfi_cfa = cfi_cfa_offset;
3464         if (cfi_cfa)
3465           {
3466             if (do_cfi_asm)
3467               output_cfi_directive (cfi_cfa);
3468             else
3469               output_cfi (cfi_cfa, fde, for_eh);
3470           }
3471         cfi_cfa = NULL;
3472         cfi_cfa_offset = NULL;
3473         if (cfi_args_size
3474             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3475           {
3476             if (do_cfi_asm)
3477               output_cfi_directive (cfi_args_size);
3478             else
3479               output_cfi (cfi_args_size, fde, for_eh);
3480           }
3481         cfi_args_size = NULL;
3482         if (cfi == NULL)
3483           {
3484             VEC_free (dw_cfi_ref, heap, regs);
3485             return;
3486           }
3487         else if (do_cfi_asm)
3488           output_cfi_directive (cfi);
3489         else
3490           output_cfi (cfi, fde, for_eh);
3491         break;
3492       default:
3493         gcc_unreachable ();
3494     }
3495 }
3496
3497 /* Output one FDE.  */
3498
3499 static void
3500 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3501             char *section_start_label, int fde_encoding, char *augmentation,
3502             bool any_lsda_needed, int lsda_encoding)
3503 {
3504   const char *begin, *end;
3505   static unsigned int j;
3506   char l1[20], l2[20];
3507   dw_cfi_ref cfi;
3508
3509   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3510                                      /* empty */ 0);
3511   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3512                                   for_eh + j);
3513   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3514   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3515   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3516     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3517                          " indicating 64-bit DWARF extension");
3518   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3519                         "FDE Length");
3520   ASM_OUTPUT_LABEL (asm_out_file, l1);
3521
3522   if (for_eh)
3523     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3524   else
3525     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3526                            debug_frame_section, "FDE CIE offset");
3527
3528   if (!fde->dw_fde_switched_sections)
3529     {
3530       begin = fde->dw_fde_begin;
3531       end = fde->dw_fde_end;
3532     }
3533   else
3534     {
3535       /* For the first section, prefer dw_fde_begin over
3536          dw_fde_{hot,cold}_section_label, as the latter
3537          might be separated from the real start of the
3538          function by alignment padding.  */
3539       if (!second)
3540         begin = fde->dw_fde_begin;
3541       else if (fde->dw_fde_switched_cold_to_hot)
3542         begin = fde->dw_fde_hot_section_label;
3543       else
3544         begin = fde->dw_fde_unlikely_section_label;
3545       if (second ^ fde->dw_fde_switched_cold_to_hot)
3546         end = fde->dw_fde_unlikely_section_end_label;
3547       else
3548         end = fde->dw_fde_hot_section_end_label;
3549     }
3550
3551   if (for_eh)
3552     {
3553       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3554       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3555       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3556                                        "FDE initial location");
3557       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3558                             end, begin, "FDE address range");
3559     }
3560   else
3561     {
3562       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3563       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3564     }
3565
3566   if (augmentation[0])
3567     {
3568       if (any_lsda_needed)
3569         {
3570           int size = size_of_encoded_value (lsda_encoding);
3571
3572           if (lsda_encoding == DW_EH_PE_aligned)
3573             {
3574               int offset = (  4         /* Length */
3575                             + 4         /* CIE offset */
3576                             + 2 * size_of_encoded_value (fde_encoding)
3577                             + 1         /* Augmentation size */ );
3578               int pad = -offset & (PTR_SIZE - 1);
3579
3580               size += pad;
3581               gcc_assert (size_of_uleb128 (size) == 1);
3582             }
3583
3584           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3585
3586           if (fde->uses_eh_lsda)
3587             {
3588               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3589                                            fde->funcdef_number);
3590               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3591                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3592                                                false,
3593                                                "Language Specific Data Area");
3594             }
3595           else
3596             {
3597               if (lsda_encoding == DW_EH_PE_aligned)
3598                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3599               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3600                                    "Language Specific Data Area (none)");
3601             }
3602         }
3603       else
3604         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3605     }
3606
3607   /* Loop through the Call Frame Instructions associated with
3608      this FDE.  */
3609   fde->dw_fde_current_label = begin;
3610   if (!fde->dw_fde_switched_sections)
3611     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3612       output_cfi (cfi, fde, for_eh);
3613   else if (!second)
3614     {
3615       if (fde->dw_fde_switch_cfi)
3616         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3617           {
3618             output_cfi (cfi, fde, for_eh);
3619             if (cfi == fde->dw_fde_switch_cfi)
3620               break;
3621           }
3622     }
3623   else
3624     {
3625       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3626
3627       if (fde->dw_fde_switch_cfi)
3628         {
3629           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3630           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3631           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3632           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3633         }
3634       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3635         output_cfi (cfi, fde, for_eh);
3636     }
3637
3638   /* If we are to emit a ref/link from function bodies to their frame tables,
3639      do it now.  This is typically performed to make sure that tables
3640      associated with functions are dragged with them and not discarded in
3641      garbage collecting links. We need to do this on a per function basis to
3642      cope with -ffunction-sections.  */
3643
3644 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3645   /* Switch to the function section, emit the ref to the tables, and
3646      switch *back* into the table section.  */
3647   switch_to_section (function_section (fde->decl));
3648   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3649   switch_to_frame_table_section (for_eh, true);
3650 #endif
3651
3652   /* Pad the FDE out to an address sized boundary.  */
3653   ASM_OUTPUT_ALIGN (asm_out_file,
3654                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3655   ASM_OUTPUT_LABEL (asm_out_file, l2);
3656
3657   j += 2;
3658 }
3659
3660 /* Return true if frame description entry FDE is needed for EH.  */
3661
3662 static bool
3663 fde_needed_for_eh_p (dw_fde_ref fde)
3664 {
3665   if (flag_asynchronous_unwind_tables)
3666     return true;
3667
3668   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3669     return true;
3670
3671   if (fde->uses_eh_lsda)
3672     return true;
3673
3674   /* If exceptions are enabled, we have collected nothrow info.  */
3675   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3676     return false;
3677
3678   return true;
3679 }
3680
3681 /* Output the call frame information used to record information
3682    that relates to calculating the frame pointer, and records the
3683    location of saved registers.  */
3684
3685 static void
3686 output_call_frame_info (int for_eh)
3687 {
3688   unsigned int i;
3689   dw_fde_ref fde;
3690   dw_cfi_ref cfi;
3691   char l1[20], l2[20], section_start_label[20];
3692   bool any_lsda_needed = false;
3693   char augmentation[6];
3694   int augmentation_size;
3695   int fde_encoding = DW_EH_PE_absptr;
3696   int per_encoding = DW_EH_PE_absptr;
3697   int lsda_encoding = DW_EH_PE_absptr;
3698   int return_reg;
3699   rtx personality = NULL;
3700   int dw_cie_version;
3701
3702   /* Don't emit a CIE if there won't be any FDEs.  */
3703   if (fde_table_in_use == 0)
3704     return;
3705
3706   /* Nothing to do if the assembler's doing it all.  */
3707   if (dwarf2out_do_cfi_asm ())
3708     return;
3709
3710   /* If we don't have any functions we'll want to unwind out of, don't emit
3711      any EH unwind information.  If we make FDEs linkonce, we may have to
3712      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3713      want to avoid having an FDE kept around when the function it refers to
3714      is discarded.  Example where this matters: a primary function template
3715      in C++ requires EH information, an explicit specialization doesn't.  */
3716   if (for_eh)
3717     {
3718       bool any_eh_needed = false;
3719
3720       for (i = 0; i < fde_table_in_use; i++)
3721         if (fde_table[i].uses_eh_lsda)
3722           any_eh_needed = any_lsda_needed = true;
3723         else if (fde_needed_for_eh_p (&fde_table[i]))
3724           any_eh_needed = true;
3725         else if (TARGET_USES_WEAK_UNWIND_INFO)
3726           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3727                                              1, 1);
3728
3729       if (!any_eh_needed)
3730         return;
3731     }
3732
3733   /* We're going to be generating comments, so turn on app.  */
3734   if (flag_debug_asm)
3735     app_enable ();
3736
3737   /* Switch to the proper frame section, first time.  */
3738   switch_to_frame_table_section (for_eh, false);
3739
3740   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3741   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3742
3743   /* Output the CIE.  */
3744   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3745   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3746   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3747     dw2_asm_output_data (4, 0xffffffff,
3748       "Initial length escape value indicating 64-bit DWARF extension");
3749   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3750                         "Length of Common Information Entry");
3751   ASM_OUTPUT_LABEL (asm_out_file, l1);
3752
3753   /* Now that the CIE pointer is PC-relative for EH,
3754      use 0 to identify the CIE.  */
3755   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3756                        (for_eh ? 0 : DWARF_CIE_ID),
3757                        "CIE Identifier Tag");
3758
3759   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3760      use CIE version 1, unless that would produce incorrect results
3761      due to overflowing the return register column.  */
3762   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3763   dw_cie_version = 1;
3764   if (return_reg >= 256 || dwarf_version > 2)
3765     dw_cie_version = 3;
3766   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3767
3768   augmentation[0] = 0;
3769   augmentation_size = 0;
3770
3771   personality = current_unit_personality;
3772   if (for_eh)
3773     {
3774       char *p;
3775
3776       /* Augmentation:
3777          z      Indicates that a uleb128 is present to size the
3778                 augmentation section.
3779          L      Indicates the encoding (and thus presence) of
3780                 an LSDA pointer in the FDE augmentation.
3781          R      Indicates a non-default pointer encoding for
3782                 FDE code pointers.
3783          P      Indicates the presence of an encoding + language
3784                 personality routine in the CIE augmentation.  */
3785
3786       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3787       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3788       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3789
3790       p = augmentation + 1;
3791       if (personality)
3792         {
3793           *p++ = 'P';
3794           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3795           assemble_external_libcall (personality);
3796         }
3797       if (any_lsda_needed)
3798         {
3799           *p++ = 'L';
3800           augmentation_size += 1;
3801         }
3802       if (fde_encoding != DW_EH_PE_absptr)
3803         {
3804           *p++ = 'R';
3805           augmentation_size += 1;
3806         }
3807       if (p > augmentation + 1)
3808         {
3809           augmentation[0] = 'z';
3810           *p = '\0';
3811         }
3812
3813       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3814       if (personality && per_encoding == DW_EH_PE_aligned)
3815         {
3816           int offset = (  4             /* Length */
3817                         + 4             /* CIE Id */
3818                         + 1             /* CIE version */
3819                         + strlen (augmentation) + 1     /* Augmentation */
3820                         + size_of_uleb128 (1)           /* Code alignment */
3821                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3822                         + 1             /* RA column */
3823                         + 1             /* Augmentation size */
3824                         + 1             /* Personality encoding */ );
3825           int pad = -offset & (PTR_SIZE - 1);
3826
3827           augmentation_size += pad;
3828
3829           /* Augmentations should be small, so there's scarce need to
3830              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3831           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3832         }
3833     }
3834
3835   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3836   if (dw_cie_version >= 4)
3837     {
3838       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3839       dw2_asm_output_data (1, 0, "CIE Segment Size");
3840     }
3841   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3842   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3843                                "CIE Data Alignment Factor");
3844
3845   if (dw_cie_version == 1)
3846     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3847   else
3848     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3849
3850   if (augmentation[0])
3851     {
3852       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3853       if (personality)
3854         {
3855           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3856                                eh_data_format_name (per_encoding));
3857           dw2_asm_output_encoded_addr_rtx (per_encoding,
3858                                            personality,
3859                                            true, NULL);
3860         }
3861
3862       if (any_lsda_needed)
3863         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3864                              eh_data_format_name (lsda_encoding));
3865
3866       if (fde_encoding != DW_EH_PE_absptr)
3867         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3868                              eh_data_format_name (fde_encoding));
3869     }
3870
3871   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3872     output_cfi (cfi, NULL, for_eh);
3873
3874   /* Pad the CIE out to an address sized boundary.  */
3875   ASM_OUTPUT_ALIGN (asm_out_file,
3876                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3877   ASM_OUTPUT_LABEL (asm_out_file, l2);
3878
3879   /* Loop through all of the FDE's.  */
3880   for (i = 0; i < fde_table_in_use; i++)
3881     {
3882       unsigned int k;
3883       fde = &fde_table[i];
3884
3885       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3886       if (for_eh && !fde_needed_for_eh_p (fde))
3887         continue;
3888
3889       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3890         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3891                     augmentation, any_lsda_needed, lsda_encoding);
3892     }
3893
3894   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3895     dw2_asm_output_data (4, 0, "End of Table");
3896 #ifdef MIPS_DEBUGGING_INFO
3897   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3898      get a value of 0.  Putting .align 0 after the label fixes it.  */
3899   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3900 #endif
3901
3902   /* Turn off app to make assembly quicker.  */
3903   if (flag_debug_asm)
3904     app_disable ();
3905 }
3906
3907 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3908
3909 static void
3910 dwarf2out_do_cfi_startproc (bool second)
3911 {
3912   int enc;
3913   rtx ref;
3914   rtx personality = get_personality_function (current_function_decl);
3915
3916   fprintf (asm_out_file, "\t.cfi_startproc\n");
3917
3918   if (personality)
3919     {
3920       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3921       ref = personality;
3922
3923       /* ??? The GAS support isn't entirely consistent.  We have to
3924          handle indirect support ourselves, but PC-relative is done
3925          in the assembler.  Further, the assembler can't handle any
3926          of the weirder relocation types.  */
3927       if (enc & DW_EH_PE_indirect)
3928         ref = dw2_force_const_mem (ref, true);
3929
3930       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3931       output_addr_const (asm_out_file, ref);
3932       fputc ('\n', asm_out_file);
3933     }
3934
3935   if (crtl->uses_eh_lsda)
3936     {
3937       char lab[20];
3938
3939       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3940       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3941                                    current_function_funcdef_no);
3942       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3943       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3944
3945       if (enc & DW_EH_PE_indirect)
3946         ref = dw2_force_const_mem (ref, true);
3947
3948       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3949       output_addr_const (asm_out_file, ref);
3950       fputc ('\n', asm_out_file);
3951     }
3952 }
3953
3954 /* Output a marker (i.e. a label) for the beginning of a function, before
3955    the prologue.  */
3956
3957 void
3958 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3959                           const char *file ATTRIBUTE_UNUSED)
3960 {
3961   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3962   char * dup_label;
3963   dw_fde_ref fde;
3964   section *fnsec;
3965
3966   current_function_func_begin_label = NULL;
3967
3968 #ifdef TARGET_UNWIND_INFO
3969   /* ??? current_function_func_begin_label is also used by except.c
3970      for call-site information.  We must emit this label if it might
3971      be used.  */
3972   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3973       && ! dwarf2out_do_frame ())
3974     return;
3975 #else
3976   if (! dwarf2out_do_frame ())
3977     return;
3978 #endif
3979
3980   fnsec = function_section (current_function_decl);
3981   switch_to_section (fnsec);
3982   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3983                                current_function_funcdef_no);
3984   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3985                           current_function_funcdef_no);
3986   dup_label = xstrdup (label);
3987   current_function_func_begin_label = dup_label;
3988
3989 #ifdef TARGET_UNWIND_INFO
3990   /* We can elide the fde allocation if we're not emitting debug info.  */
3991   if (! dwarf2out_do_frame ())
3992     return;
3993 #endif
3994
3995   /* Expand the fde table if necessary.  */
3996   if (fde_table_in_use == fde_table_allocated)
3997     {
3998       fde_table_allocated += FDE_TABLE_INCREMENT;
3999       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4000       memset (fde_table + fde_table_in_use, 0,
4001               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4002     }
4003
4004   /* Record the FDE associated with this function.  */
4005   current_funcdef_fde = fde_table_in_use;
4006
4007   /* Add the new FDE at the end of the fde_table.  */
4008   fde = &fde_table[fde_table_in_use++];
4009   fde->decl = current_function_decl;
4010   fde->dw_fde_begin = dup_label;
4011   fde->dw_fde_current_label = dup_label;
4012   fde->dw_fde_hot_section_label = NULL;
4013   fde->dw_fde_hot_section_end_label = NULL;
4014   fde->dw_fde_unlikely_section_label = NULL;
4015   fde->dw_fde_unlikely_section_end_label = NULL;
4016   fde->dw_fde_switched_sections = 0;
4017   fde->dw_fde_switched_cold_to_hot = 0;
4018   fde->dw_fde_end = NULL;
4019   fde->dw_fde_vms_end_prologue = NULL;
4020   fde->dw_fde_vms_begin_epilogue = NULL;
4021   fde->dw_fde_cfi = NULL;
4022   fde->dw_fde_switch_cfi = NULL;
4023   fde->funcdef_number = current_function_funcdef_no;
4024   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4025   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4026   fde->nothrow = crtl->nothrow;
4027   fde->drap_reg = INVALID_REGNUM;
4028   fde->vdrap_reg = INVALID_REGNUM;
4029   if (flag_reorder_blocks_and_partition)
4030     {
4031       section *unlikelysec;
4032       if (first_function_block_is_cold)
4033         fde->in_std_section = 1;
4034       else
4035         fde->in_std_section
4036           = (fnsec == text_section
4037              || (cold_text_section && fnsec == cold_text_section));
4038       unlikelysec = unlikely_text_section ();
4039       fde->cold_in_std_section
4040         = (unlikelysec == text_section
4041            || (cold_text_section && unlikelysec == cold_text_section));
4042     }
4043   else
4044     {
4045       fde->in_std_section
4046         = (fnsec == text_section
4047            || (cold_text_section && fnsec == cold_text_section));
4048       fde->cold_in_std_section = 0;
4049     }
4050
4051   args_size = old_args_size = 0;
4052
4053   /* We only want to output line number information for the genuine dwarf2
4054      prologue case, not the eh frame case.  */
4055 #ifdef DWARF2_DEBUGGING_INFO
4056   if (file)
4057     dwarf2out_source_line (line, file, 0, true);
4058 #endif
4059
4060   if (dwarf2out_do_cfi_asm ())
4061     dwarf2out_do_cfi_startproc (false);
4062   else
4063     {
4064       rtx personality = get_personality_function (current_function_decl);
4065       if (!current_unit_personality)
4066         current_unit_personality = personality;
4067
4068       /* We cannot keep a current personality per function as without CFI
4069          asm, at the point where we emit the CFI data, there is no current
4070          function anymore.  */
4071       if (personality && current_unit_personality != personality)
4072         sorry ("multiple EH personalities are supported only with assemblers "
4073                "supporting .cfi_personality directive");
4074     }
4075 }
4076
4077 /* Output a marker (i.e. a label) for the end of the generated code
4078    for a function prologue.  This gets called *after* the prologue code has
4079    been generated.  */
4080
4081 void
4082 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4083                         const char *file ATTRIBUTE_UNUSED)
4084 {
4085   dw_fde_ref fde;
4086   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4087
4088   /* Output a label to mark the endpoint of the code generated for this
4089      function.  */
4090   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4091                                current_function_funcdef_no);
4092   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4093                           current_function_funcdef_no);
4094   fde = &fde_table[fde_table_in_use - 1];
4095   fde->dw_fde_vms_end_prologue = xstrdup (label);
4096 }
4097
4098 /* Output a marker (i.e. a label) for the beginning of the generated code
4099    for a function epilogue.  This gets called *before* the prologue code has
4100    been generated.  */
4101
4102 void
4103 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4104                           const char *file ATTRIBUTE_UNUSED)
4105 {
4106   dw_fde_ref fde;
4107   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4108
4109   fde = &fde_table[fde_table_in_use - 1];
4110   if (fde->dw_fde_vms_begin_epilogue)
4111     return;
4112
4113   /* Output a label to mark the endpoint of the code generated for this
4114      function.  */
4115   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4116                                current_function_funcdef_no);
4117   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4118                           current_function_funcdef_no);
4119   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4120 }
4121
4122 /* Output a marker (i.e. a label) for the absolute end of the generated code
4123    for a function definition.  This gets called *after* the epilogue code has
4124    been generated.  */
4125
4126 void
4127 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4128                         const char *file ATTRIBUTE_UNUSED)
4129 {
4130   dw_fde_ref fde;
4131   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4132
4133   last_var_location_insn = NULL_RTX;
4134
4135   if (dwarf2out_do_cfi_asm ())
4136     fprintf (asm_out_file, "\t.cfi_endproc\n");
4137
4138   /* Output a label to mark the endpoint of the code generated for this
4139      function.  */
4140   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4141                                current_function_funcdef_no);
4142   ASM_OUTPUT_LABEL (asm_out_file, label);
4143   fde = current_fde ();
4144   gcc_assert (fde != NULL);
4145   fde->dw_fde_end = xstrdup (label);
4146 }
4147
4148 void
4149 dwarf2out_frame_init (void)
4150 {
4151   /* Allocate the initial hunk of the fde_table.  */
4152   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4153   fde_table_allocated = FDE_TABLE_INCREMENT;
4154   fde_table_in_use = 0;
4155
4156   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4157      sake of lookup_cfa.  */
4158
4159   /* On entry, the Canonical Frame Address is at SP.  */
4160   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4161
4162   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4163     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4164 }
4165
4166 void
4167 dwarf2out_frame_finish (void)
4168 {
4169   /* Output call frame information.  */
4170   if (DWARF2_FRAME_INFO)
4171     output_call_frame_info (0);
4172
4173 #ifndef TARGET_UNWIND_INFO
4174   /* Output another copy for the unwinder.  */
4175   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4176     output_call_frame_info (1);
4177 #endif
4178 }
4179
4180 /* Note that the current function section is being used for code.  */
4181
4182 static void
4183 dwarf2out_note_section_used (void)
4184 {
4185   section *sec = current_function_section ();
4186   if (sec == text_section)
4187     text_section_used = true;
4188   else if (sec == cold_text_section)
4189     cold_text_section_used = true;
4190 }
4191
4192 void
4193 dwarf2out_switch_text_section (void)
4194 {
4195   dw_fde_ref fde = current_fde ();
4196
4197   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4198
4199   fde->dw_fde_switched_sections = 1;
4200   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4201
4202   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4203   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4204   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4205   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4206   have_multiple_function_sections = true;
4207
4208   /* Reset the current label on switching text sections, so that we
4209      don't attempt to advance_loc4 between labels in different sections.  */
4210   fde->dw_fde_current_label = NULL;
4211
4212   /* There is no need to mark used sections when not debugging.  */
4213   if (cold_text_section != NULL)
4214     dwarf2out_note_section_used ();
4215
4216   if (dwarf2out_do_cfi_asm ())
4217     fprintf (asm_out_file, "\t.cfi_endproc\n");
4218
4219   /* Now do the real section switch.  */
4220   switch_to_section (current_function_section ());
4221
4222   if (dwarf2out_do_cfi_asm ())
4223     {
4224       dwarf2out_do_cfi_startproc (true);
4225       /* As this is a different FDE, insert all current CFI instructions
4226          again.  */
4227       output_cfis (fde->dw_fde_cfi, true, fde, true);
4228     }
4229   else
4230     {
4231       dw_cfi_ref cfi = fde->dw_fde_cfi;
4232
4233       cfi = fde->dw_fde_cfi;
4234       if (cfi)
4235         while (cfi->dw_cfi_next != NULL)
4236           cfi = cfi->dw_cfi_next;
4237       fde->dw_fde_switch_cfi = cfi;
4238     }
4239 }
4240 \f
4241 /* And now, the subset of the debugging information support code necessary
4242    for emitting location expressions.  */
4243
4244 /* Data about a single source file.  */
4245 struct GTY(()) dwarf_file_data {
4246   const char * filename;
4247   int emitted_number;
4248 };
4249
4250 typedef struct dw_val_struct *dw_val_ref;
4251 typedef struct die_struct *dw_die_ref;
4252 typedef const struct die_struct *const_dw_die_ref;
4253 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4254 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4255
4256 typedef struct GTY(()) deferred_locations_struct
4257 {
4258   tree variable;
4259   dw_die_ref die;
4260 } deferred_locations;
4261
4262 DEF_VEC_O(deferred_locations);
4263 DEF_VEC_ALLOC_O(deferred_locations,gc);
4264
4265 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4266
4267 DEF_VEC_P(dw_die_ref);
4268 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4269
4270 /* Each DIE may have a series of attribute/value pairs.  Values
4271    can take on several forms.  The forms that are used in this
4272    implementation are listed below.  */
4273
4274 enum dw_val_class
4275 {
4276   dw_val_class_addr,
4277   dw_val_class_offset,
4278   dw_val_class_loc,
4279   dw_val_class_loc_list,
4280   dw_val_class_range_list,
4281   dw_val_class_const,
4282   dw_val_class_unsigned_const,
4283   dw_val_class_const_double,
4284   dw_val_class_vec,
4285   dw_val_class_flag,
4286   dw_val_class_die_ref,
4287   dw_val_class_fde_ref,
4288   dw_val_class_lbl_id,
4289   dw_val_class_lineptr,
4290   dw_val_class_str,
4291   dw_val_class_macptr,
4292   dw_val_class_file,
4293   dw_val_class_data8,
4294   dw_val_class_vms_delta
4295 };
4296
4297 /* Describe a floating point constant value, or a vector constant value.  */
4298
4299 typedef struct GTY(()) dw_vec_struct {
4300   unsigned char * GTY((length ("%h.length"))) array;
4301   unsigned length;
4302   unsigned elt_size;
4303 }
4304 dw_vec_const;
4305
4306 /* The dw_val_node describes an attribute's value, as it is
4307    represented internally.  */
4308
4309 typedef struct GTY(()) dw_val_struct {
4310   enum dw_val_class val_class;
4311   union dw_val_struct_union
4312     {
4313       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4314       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4315       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4316       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4317       HOST_WIDE_INT GTY ((default)) val_int;
4318       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4319       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4320       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4321       struct dw_val_die_union
4322         {
4323           dw_die_ref die;
4324           int external;
4325         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4326       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4327       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4328       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4329       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4330       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4331       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4332       struct dw_val_vms_delta_union
4333         {
4334           char * lbl1;
4335           char * lbl2;
4336         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4337     }
4338   GTY ((desc ("%1.val_class"))) v;
4339 }
4340 dw_val_node;
4341
4342 /* Locations in memory are described using a sequence of stack machine
4343    operations.  */
4344
4345 typedef struct GTY(()) dw_loc_descr_struct {
4346   dw_loc_descr_ref dw_loc_next;
4347   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4348   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4349      from DW_OP_addr with a dtp-relative symbol relocation.  */
4350   unsigned int dtprel : 1;
4351   int dw_loc_addr;
4352   dw_val_node dw_loc_oprnd1;
4353   dw_val_node dw_loc_oprnd2;
4354 }
4355 dw_loc_descr_node;
4356
4357 /* Location lists are ranges + location descriptions for that range,
4358    so you can track variables that are in different places over
4359    their entire life.  */
4360 typedef struct GTY(()) dw_loc_list_struct {
4361   dw_loc_list_ref dw_loc_next;
4362   const char *begin; /* Label for begin address of range */
4363   const char *end;  /* Label for end address of range */
4364   char *ll_symbol; /* Label for beginning of location list.
4365                       Only on head of list */
4366   const char *section; /* Section this loclist is relative to */
4367   dw_loc_descr_ref expr;
4368 } dw_loc_list_node;
4369
4370 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4371
4372 /* Convert a DWARF stack opcode into its string name.  */
4373
4374 static const char *
4375 dwarf_stack_op_name (unsigned int op)
4376 {
4377   switch (op)
4378     {
4379     case DW_OP_addr:
4380       return "DW_OP_addr";
4381     case DW_OP_deref:
4382       return "DW_OP_deref";
4383     case DW_OP_const1u:
4384       return "DW_OP_const1u";
4385     case DW_OP_const1s:
4386       return "DW_OP_const1s";
4387     case DW_OP_const2u:
4388       return "DW_OP_const2u";
4389     case DW_OP_const2s:
4390       return "DW_OP_const2s";
4391     case DW_OP_const4u:
4392       return "DW_OP_const4u";
4393     case DW_OP_const4s:
4394       return "DW_OP_const4s";
4395     case DW_OP_const8u:
4396       return "DW_OP_const8u";
4397     case DW_OP_const8s:
4398       return "DW_OP_const8s";
4399     case DW_OP_constu:
4400       return "DW_OP_constu";
4401     case DW_OP_consts:
4402       return "DW_OP_consts";
4403     case DW_OP_dup:
4404       return "DW_OP_dup";
4405     case DW_OP_drop:
4406       return "DW_OP_drop";
4407     case DW_OP_over:
4408       return "DW_OP_over";
4409     case DW_OP_pick:
4410       return "DW_OP_pick";
4411     case DW_OP_swap:
4412       return "DW_OP_swap";
4413     case DW_OP_rot:
4414       return "DW_OP_rot";
4415     case DW_OP_xderef:
4416       return "DW_OP_xderef";
4417     case DW_OP_abs:
4418       return "DW_OP_abs";
4419     case DW_OP_and:
4420       return "DW_OP_and";
4421     case DW_OP_div:
4422       return "DW_OP_div";
4423     case DW_OP_minus:
4424       return "DW_OP_minus";
4425     case DW_OP_mod:
4426       return "DW_OP_mod";
4427     case DW_OP_mul:
4428       return "DW_OP_mul";
4429     case DW_OP_neg:
4430       return "DW_OP_neg";
4431     case DW_OP_not:
4432       return "DW_OP_not";
4433     case DW_OP_or:
4434       return "DW_OP_or";
4435     case DW_OP_plus:
4436       return "DW_OP_plus";
4437     case DW_OP_plus_uconst:
4438       return "DW_OP_plus_uconst";
4439     case DW_OP_shl:
4440       return "DW_OP_shl";
4441     case DW_OP_shr:
4442       return "DW_OP_shr";
4443     case DW_OP_shra:
4444       return "DW_OP_shra";
4445     case DW_OP_xor:
4446       return "DW_OP_xor";
4447     case DW_OP_bra:
4448       return "DW_OP_bra";
4449     case DW_OP_eq:
4450       return "DW_OP_eq";
4451     case DW_OP_ge:
4452       return "DW_OP_ge";
4453     case DW_OP_gt:
4454       return "DW_OP_gt";
4455     case DW_OP_le:
4456       return "DW_OP_le";
4457     case DW_OP_lt:
4458       return "DW_OP_lt";
4459     case DW_OP_ne:
4460       return "DW_OP_ne";
4461     case DW_OP_skip:
4462       return "DW_OP_skip";
4463     case DW_OP_lit0:
4464       return "DW_OP_lit0";
4465     case DW_OP_lit1:
4466       return "DW_OP_lit1";
4467     case DW_OP_lit2:
4468       return "DW_OP_lit2";
4469     case DW_OP_lit3:
4470       return "DW_OP_lit3";
4471     case DW_OP_lit4:
4472       return "DW_OP_lit4";
4473     case DW_OP_lit5:
4474       return "DW_OP_lit5";
4475     case DW_OP_lit6:
4476       return "DW_OP_lit6";
4477     case DW_OP_lit7:
4478       return "DW_OP_lit7";
4479     case DW_OP_lit8:
4480       return "DW_OP_lit8";
4481     case DW_OP_lit9:
4482       return "DW_OP_lit9";
4483     case DW_OP_lit10:
4484       return "DW_OP_lit10";
4485     case DW_OP_lit11:
4486       return "DW_OP_lit11";
4487     case DW_OP_lit12:
4488       return "DW_OP_lit12";
4489     case DW_OP_lit13:
4490       return "DW_OP_lit13";
4491     case DW_OP_lit14:
4492       return "DW_OP_lit14";
4493     case DW_OP_lit15:
4494       return "DW_OP_lit15";
4495     case DW_OP_lit16:
4496       return "DW_OP_lit16";
4497     case DW_OP_lit17:
4498       return "DW_OP_lit17";
4499     case DW_OP_lit18:
4500       return "DW_OP_lit18";
4501     case DW_OP_lit19:
4502       return "DW_OP_lit19";
4503     case DW_OP_lit20:
4504       return "DW_OP_lit20";
4505     case DW_OP_lit21:
4506       return "DW_OP_lit21";
4507     case DW_OP_lit22:
4508       return "DW_OP_lit22";
4509     case DW_OP_lit23:
4510       return "DW_OP_lit23";
4511     case DW_OP_lit24:
4512       return "DW_OP_lit24";
4513     case DW_OP_lit25:
4514       return "DW_OP_lit25";
4515     case DW_OP_lit26:
4516       return "DW_OP_lit26";
4517     case DW_OP_lit27:
4518       return "DW_OP_lit27";
4519     case DW_OP_lit28:
4520       return "DW_OP_lit28";
4521     case DW_OP_lit29:
4522       return "DW_OP_lit29";
4523     case DW_OP_lit30:
4524       return "DW_OP_lit30";
4525     case DW_OP_lit31:
4526       return "DW_OP_lit31";
4527     case DW_OP_reg0:
4528       return "DW_OP_reg0";
4529     case DW_OP_reg1:
4530       return "DW_OP_reg1";
4531     case DW_OP_reg2:
4532       return "DW_OP_reg2";
4533     case DW_OP_reg3:
4534       return "DW_OP_reg3";
4535     case DW_OP_reg4:
4536       return "DW_OP_reg4";
4537     case DW_OP_reg5:
4538       return "DW_OP_reg5";
4539     case DW_OP_reg6:
4540       return "DW_OP_reg6";
4541     case DW_OP_reg7:
4542       return "DW_OP_reg7";
4543     case DW_OP_reg8:
4544       return "DW_OP_reg8";
4545     case DW_OP_reg9:
4546       return "DW_OP_reg9";
4547     case DW_OP_reg10:
4548       return "DW_OP_reg10";
4549     case DW_OP_reg11:
4550       return "DW_OP_reg11";
4551     case DW_OP_reg12:
4552       return "DW_OP_reg12";
4553     case DW_OP_reg13:
4554       return "DW_OP_reg13";
4555     case DW_OP_reg14:
4556       return "DW_OP_reg14";
4557     case DW_OP_reg15:
4558       return "DW_OP_reg15";
4559     case DW_OP_reg16:
4560       return "DW_OP_reg16";
4561     case DW_OP_reg17:
4562       return "DW_OP_reg17";
4563     case DW_OP_reg18:
4564       return "DW_OP_reg18";
4565     case DW_OP_reg19:
4566       return "DW_OP_reg19";
4567     case DW_OP_reg20:
4568       return "DW_OP_reg20";
4569     case DW_OP_reg21:
4570       return "DW_OP_reg21";
4571     case DW_OP_reg22:
4572       return "DW_OP_reg22";
4573     case DW_OP_reg23:
4574       return "DW_OP_reg23";
4575     case DW_OP_reg24:
4576       return "DW_OP_reg24";
4577     case DW_OP_reg25:
4578       return "DW_OP_reg25";
4579     case DW_OP_reg26:
4580       return "DW_OP_reg26";
4581     case DW_OP_reg27:
4582       return "DW_OP_reg27";
4583     case DW_OP_reg28:
4584       return "DW_OP_reg28";
4585     case DW_OP_reg29:
4586       return "DW_OP_reg29";
4587     case DW_OP_reg30:
4588       return "DW_OP_reg30";
4589     case DW_OP_reg31:
4590       return "DW_OP_reg31";
4591     case DW_OP_breg0:
4592       return "DW_OP_breg0";
4593     case DW_OP_breg1:
4594       return "DW_OP_breg1";
4595     case DW_OP_breg2:
4596       return "DW_OP_breg2";
4597     case DW_OP_breg3:
4598       return "DW_OP_breg3";
4599     case DW_OP_breg4:
4600       return "DW_OP_breg4";
4601     case DW_OP_breg5:
4602       return "DW_OP_breg5";
4603     case DW_OP_breg6:
4604       return "DW_OP_breg6";
4605     case DW_OP_breg7:
4606       return "DW_OP_breg7";
4607     case DW_OP_breg8:
4608       return "DW_OP_breg8";
4609     case DW_OP_breg9:
4610       return "DW_OP_breg9";
4611     case DW_OP_breg10:
4612       return "DW_OP_breg10";
4613     case DW_OP_breg11:
4614       return "DW_OP_breg11";
4615     case DW_OP_breg12:
4616       return "DW_OP_breg12";
4617     case DW_OP_breg13:
4618       return "DW_OP_breg13";
4619     case DW_OP_breg14:
4620       return "DW_OP_breg14";
4621     case DW_OP_breg15:
4622       return "DW_OP_breg15";
4623     case DW_OP_breg16:
4624       return "DW_OP_breg16";
4625     case DW_OP_breg17:
4626       return "DW_OP_breg17";
4627     case DW_OP_breg18:
4628       return "DW_OP_breg18";
4629     case DW_OP_breg19:
4630       return "DW_OP_breg19";
4631     case DW_OP_breg20:
4632       return "DW_OP_breg20";
4633     case DW_OP_breg21:
4634       return "DW_OP_breg21";
4635     case DW_OP_breg22:
4636       return "DW_OP_breg22";
4637     case DW_OP_breg23:
4638       return "DW_OP_breg23";
4639     case DW_OP_breg24:
4640       return "DW_OP_breg24";
4641     case DW_OP_breg25:
4642       return "DW_OP_breg25";
4643     case DW_OP_breg26:
4644       return "DW_OP_breg26";
4645     case DW_OP_breg27:
4646       return "DW_OP_breg27";
4647     case DW_OP_breg28:
4648       return "DW_OP_breg28";
4649     case DW_OP_breg29:
4650       return "DW_OP_breg29";
4651     case DW_OP_breg30:
4652       return "DW_OP_breg30";
4653     case DW_OP_breg31:
4654       return "DW_OP_breg31";
4655     case DW_OP_regx:
4656       return "DW_OP_regx";
4657     case DW_OP_fbreg:
4658       return "DW_OP_fbreg";
4659     case DW_OP_bregx:
4660       return "DW_OP_bregx";
4661     case DW_OP_piece:
4662       return "DW_OP_piece";
4663     case DW_OP_deref_size:
4664       return "DW_OP_deref_size";
4665     case DW_OP_xderef_size:
4666       return "DW_OP_xderef_size";
4667     case DW_OP_nop:
4668       return "DW_OP_nop";
4669
4670     case DW_OP_push_object_address:
4671       return "DW_OP_push_object_address";
4672     case DW_OP_call2:
4673       return "DW_OP_call2";
4674     case DW_OP_call4:
4675       return "DW_OP_call4";
4676     case DW_OP_call_ref:
4677       return "DW_OP_call_ref";
4678     case DW_OP_implicit_value:
4679       return "DW_OP_implicit_value";
4680     case DW_OP_stack_value:
4681       return "DW_OP_stack_value";
4682     case DW_OP_form_tls_address:
4683       return "DW_OP_form_tls_address";
4684     case DW_OP_call_frame_cfa:
4685       return "DW_OP_call_frame_cfa";
4686     case DW_OP_bit_piece:
4687       return "DW_OP_bit_piece";
4688
4689     case DW_OP_GNU_push_tls_address:
4690       return "DW_OP_GNU_push_tls_address";
4691     case DW_OP_GNU_uninit:
4692       return "DW_OP_GNU_uninit";
4693     case DW_OP_GNU_encoded_addr:
4694       return "DW_OP_GNU_encoded_addr";
4695
4696     default:
4697       return "OP_<unknown>";
4698     }
4699 }
4700
4701 /* Return a pointer to a newly allocated location description.  Location
4702    descriptions are simple expression terms that can be strung
4703    together to form more complicated location (address) descriptions.  */
4704
4705 static inline dw_loc_descr_ref
4706 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4707                unsigned HOST_WIDE_INT oprnd2)
4708 {
4709   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4710
4711   descr->dw_loc_opc = op;
4712   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4713   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4714   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4715   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4716
4717   return descr;
4718 }
4719
4720 /* Return a pointer to a newly allocated location description for
4721    REG and OFFSET.  */
4722
4723 static inline dw_loc_descr_ref
4724 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4725 {
4726   if (reg <= 31)
4727     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4728                           offset, 0);
4729   else
4730     return new_loc_descr (DW_OP_bregx, reg, offset);
4731 }
4732
4733 /* Add a location description term to a location description expression.  */
4734
4735 static inline void
4736 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4737 {
4738   dw_loc_descr_ref *d;
4739
4740   /* Find the end of the chain.  */
4741   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4742     ;
4743
4744   *d = descr;
4745 }
4746
4747 /* Add a constant OFFSET to a location expression.  */
4748
4749 static void
4750 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4751 {
4752   dw_loc_descr_ref loc;
4753   HOST_WIDE_INT *p;
4754
4755   gcc_assert (*list_head != NULL);
4756
4757   if (!offset)
4758     return;
4759
4760   /* Find the end of the chain.  */
4761   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4762     ;
4763
4764   p = NULL;
4765   if (loc->dw_loc_opc == DW_OP_fbreg
4766       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4767     p = &loc->dw_loc_oprnd1.v.val_int;
4768   else if (loc->dw_loc_opc == DW_OP_bregx)
4769     p = &loc->dw_loc_oprnd2.v.val_int;
4770
4771   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4772      offset.  Don't optimize if an signed integer overflow would happen.  */
4773   if (p != NULL
4774       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4775           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4776     *p += offset;
4777
4778   else if (offset > 0)
4779     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4780
4781   else
4782     {
4783       loc->dw_loc_next = int_loc_descriptor (-offset);
4784       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4785     }
4786 }
4787
4788 /* Add a constant OFFSET to a location list.  */
4789
4790 static void
4791 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4792 {
4793   dw_loc_list_ref d;
4794   for (d = list_head; d != NULL; d = d->dw_loc_next)
4795     loc_descr_plus_const (&d->expr, offset);
4796 }
4797
4798 /* Return the size of a location descriptor.  */
4799
4800 static unsigned long
4801 size_of_loc_descr (dw_loc_descr_ref loc)
4802 {
4803   unsigned long size = 1;
4804
4805   switch (loc->dw_loc_opc)
4806     {
4807     case DW_OP_addr:
4808       size += DWARF2_ADDR_SIZE;
4809       break;
4810     case DW_OP_const1u:
4811     case DW_OP_const1s:
4812       size += 1;
4813       break;
4814     case DW_OP_const2u:
4815     case DW_OP_const2s:
4816       size += 2;
4817       break;
4818     case DW_OP_const4u:
4819     case DW_OP_const4s:
4820       size += 4;
4821       break;
4822     case DW_OP_const8u:
4823     case DW_OP_const8s:
4824       size += 8;
4825       break;
4826     case DW_OP_constu:
4827       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4828       break;
4829     case DW_OP_consts:
4830       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4831       break;
4832     case DW_OP_pick:
4833       size += 1;
4834       break;
4835     case DW_OP_plus_uconst:
4836       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4837       break;
4838     case DW_OP_skip:
4839     case DW_OP_bra:
4840       size += 2;
4841       break;
4842     case DW_OP_breg0:
4843     case DW_OP_breg1:
4844     case DW_OP_breg2:
4845     case DW_OP_breg3:
4846     case DW_OP_breg4:
4847     case DW_OP_breg5:
4848     case DW_OP_breg6:
4849     case DW_OP_breg7:
4850     case DW_OP_breg8:
4851     case DW_OP_breg9:
4852     case DW_OP_breg10:
4853     case DW_OP_breg11:
4854     case DW_OP_breg12:
4855     case DW_OP_breg13:
4856     case DW_OP_breg14:
4857     case DW_OP_breg15:
4858     case DW_OP_breg16:
4859     case DW_OP_breg17:
4860     case DW_OP_breg18:
4861     case DW_OP_breg19:
4862     case DW_OP_breg20:
4863     case DW_OP_breg21:
4864     case DW_OP_breg22:
4865     case DW_OP_breg23:
4866     case DW_OP_breg24:
4867     case DW_OP_breg25:
4868     case DW_OP_breg26:
4869     case DW_OP_breg27:
4870     case DW_OP_breg28:
4871     case DW_OP_breg29:
4872     case DW_OP_breg30:
4873     case DW_OP_breg31:
4874       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4875       break;
4876     case DW_OP_regx:
4877       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4878       break;
4879     case DW_OP_fbreg:
4880       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4881       break;
4882     case DW_OP_bregx:
4883       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4884       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4885       break;
4886     case DW_OP_piece:
4887       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4888       break;
4889     case DW_OP_bit_piece:
4890       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4891       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4892       break;
4893     case DW_OP_deref_size:
4894     case DW_OP_xderef_size:
4895       size += 1;
4896       break;
4897     case DW_OP_call2:
4898       size += 2;
4899       break;
4900     case DW_OP_call4:
4901       size += 4;
4902       break;
4903     case DW_OP_call_ref:
4904       size += DWARF2_ADDR_SIZE;
4905       break;
4906     case DW_OP_implicit_value:
4907       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4908               + loc->dw_loc_oprnd1.v.val_unsigned;
4909       break;
4910     default:
4911       break;
4912     }
4913
4914   return size;
4915 }
4916
4917 /* Return the size of a series of location descriptors.  */
4918
4919 static unsigned long
4920 size_of_locs (dw_loc_descr_ref loc)
4921 {
4922   dw_loc_descr_ref l;
4923   unsigned long size;
4924
4925   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4926      field, to avoid writing to a PCH file.  */
4927   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4928     {
4929       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4930         break;
4931       size += size_of_loc_descr (l);
4932     }
4933   if (! l)
4934     return size;
4935
4936   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4937     {
4938       l->dw_loc_addr = size;
4939       size += size_of_loc_descr (l);
4940     }
4941
4942   return size;
4943 }
4944
4945 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4946
4947 /* Output location description stack opcode's operands (if any).  */
4948
4949 static void
4950 output_loc_operands (dw_loc_descr_ref loc)
4951 {
4952   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4953   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4954
4955   switch (loc->dw_loc_opc)
4956     {
4957 #ifdef DWARF2_DEBUGGING_INFO
4958     case DW_OP_const2u:
4959     case DW_OP_const2s:
4960       dw2_asm_output_data (2, val1->v.val_int, NULL);
4961       break;
4962     case DW_OP_const4u:
4963       if (loc->dtprel)
4964         {
4965           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4966           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4967                                                val1->v.val_addr);
4968           fputc ('\n', asm_out_file);
4969           break;
4970         }
4971       /* FALLTHRU */
4972     case DW_OP_const4s:
4973       dw2_asm_output_data (4, val1->v.val_int, NULL);
4974       break;
4975     case DW_OP_const8u:
4976       if (loc->dtprel)
4977         {
4978           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4979           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
4980                                                val1->v.val_addr);
4981           fputc ('\n', asm_out_file);
4982           break;
4983         }
4984       /* FALLTHRU */
4985     case DW_OP_const8s:
4986       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4987       dw2_asm_output_data (8, val1->v.val_int, NULL);
4988       break;
4989     case DW_OP_skip:
4990     case DW_OP_bra:
4991       {
4992         int offset;
4993
4994         gcc_assert (val1->val_class == dw_val_class_loc);
4995         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4996
4997         dw2_asm_output_data (2, offset, NULL);
4998       }
4999       break;
5000     case DW_OP_implicit_value:
5001       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5002       switch (val2->val_class)
5003         {
5004         case dw_val_class_const:
5005           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5006           break;
5007         case dw_val_class_vec:
5008           {
5009             unsigned int elt_size = val2->v.val_vec.elt_size;
5010             unsigned int len = val2->v.val_vec.length;
5011             unsigned int i;
5012             unsigned char *p;
5013
5014             if (elt_size > sizeof (HOST_WIDE_INT))
5015               {
5016                 elt_size /= 2;
5017                 len *= 2;
5018               }
5019             for (i = 0, p = val2->v.val_vec.array;
5020                  i < len;
5021                  i++, p += elt_size)
5022               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5023                                    "fp or vector constant word %u", i);
5024           }
5025           break;
5026         case dw_val_class_const_double:
5027           {
5028             unsigned HOST_WIDE_INT first, second;
5029
5030             if (WORDS_BIG_ENDIAN)
5031               {
5032                 first = val2->v.val_double.high;
5033                 second = val2->v.val_double.low;
5034               }
5035             else
5036               {
5037                 first = val2->v.val_double.low;
5038                 second = val2->v.val_double.high;
5039               }
5040             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5041                                  first, NULL);
5042             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5043                                  second, NULL);
5044           }
5045           break;
5046         case dw_val_class_addr:
5047           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5048           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5049           break;
5050         default:
5051           gcc_unreachable ();
5052         }
5053       break;
5054 #else
5055     case DW_OP_const2u:
5056     case DW_OP_const2s:
5057     case DW_OP_const4u:
5058     case DW_OP_const4s:
5059     case DW_OP_const8u:
5060     case DW_OP_const8s:
5061     case DW_OP_skip:
5062     case DW_OP_bra:
5063     case DW_OP_implicit_value:
5064       /* We currently don't make any attempt to make sure these are
5065          aligned properly like we do for the main unwind info, so
5066          don't support emitting things larger than a byte if we're
5067          only doing unwinding.  */
5068       gcc_unreachable ();
5069 #endif
5070     case DW_OP_const1u:
5071     case DW_OP_const1s:
5072       dw2_asm_output_data (1, val1->v.val_int, NULL);
5073       break;
5074     case DW_OP_constu:
5075       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5076       break;
5077     case DW_OP_consts:
5078       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5079       break;
5080     case DW_OP_pick:
5081       dw2_asm_output_data (1, val1->v.val_int, NULL);
5082       break;
5083     case DW_OP_plus_uconst:
5084       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5085       break;
5086     case DW_OP_breg0:
5087     case DW_OP_breg1:
5088     case DW_OP_breg2:
5089     case DW_OP_breg3:
5090     case DW_OP_breg4:
5091     case DW_OP_breg5:
5092     case DW_OP_breg6:
5093     case DW_OP_breg7:
5094     case DW_OP_breg8:
5095     case DW_OP_breg9:
5096     case DW_OP_breg10:
5097     case DW_OP_breg11:
5098     case DW_OP_breg12:
5099     case DW_OP_breg13:
5100     case DW_OP_breg14:
5101     case DW_OP_breg15:
5102     case DW_OP_breg16:
5103     case DW_OP_breg17:
5104     case DW_OP_breg18:
5105     case DW_OP_breg19:
5106     case DW_OP_breg20:
5107     case DW_OP_breg21:
5108     case DW_OP_breg22:
5109     case DW_OP_breg23:
5110     case DW_OP_breg24:
5111     case DW_OP_breg25:
5112     case DW_OP_breg26:
5113     case DW_OP_breg27:
5114     case DW_OP_breg28:
5115     case DW_OP_breg29:
5116     case DW_OP_breg30:
5117     case DW_OP_breg31:
5118       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5119       break;
5120     case DW_OP_regx:
5121       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5122       break;
5123     case DW_OP_fbreg:
5124       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5125       break;
5126     case DW_OP_bregx:
5127       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5128       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5129       break;
5130     case DW_OP_piece:
5131       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5132       break;
5133     case DW_OP_bit_piece:
5134       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5135       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5136       break;
5137     case DW_OP_deref_size:
5138     case DW_OP_xderef_size:
5139       dw2_asm_output_data (1, val1->v.val_int, NULL);
5140       break;
5141
5142     case DW_OP_addr:
5143       if (loc->dtprel)
5144         {
5145           if (targetm.asm_out.output_dwarf_dtprel)
5146             {
5147               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5148                                                    DWARF2_ADDR_SIZE,
5149                                                    val1->v.val_addr);
5150               fputc ('\n', asm_out_file);
5151             }
5152           else
5153             gcc_unreachable ();
5154         }
5155       else
5156         {
5157 #ifdef DWARF2_DEBUGGING_INFO
5158           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5159 #else
5160           gcc_unreachable ();
5161 #endif
5162         }
5163       break;
5164
5165     default:
5166       /* Other codes have no operands.  */
5167       break;
5168     }
5169 }
5170
5171 /* Output a sequence of location operations.  */
5172
5173 static void
5174 output_loc_sequence (dw_loc_descr_ref loc)
5175 {
5176   for (; loc != NULL; loc = loc->dw_loc_next)
5177     {
5178       /* Output the opcode.  */
5179       dw2_asm_output_data (1, loc->dw_loc_opc,
5180                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5181
5182       /* Output the operand(s) (if any).  */
5183       output_loc_operands (loc);
5184     }
5185 }
5186
5187 /* Output location description stack opcode's operands (if any).
5188    The output is single bytes on a line, suitable for .cfi_escape.  */
5189
5190 static void
5191 output_loc_operands_raw (dw_loc_descr_ref loc)
5192 {
5193   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5194   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5195
5196   switch (loc->dw_loc_opc)
5197     {
5198     case DW_OP_addr:
5199     case DW_OP_implicit_value:
5200       /* We cannot output addresses in .cfi_escape, only bytes.  */
5201       gcc_unreachable ();
5202
5203     case DW_OP_const1u:
5204     case DW_OP_const1s:
5205     case DW_OP_pick:
5206     case DW_OP_deref_size:
5207     case DW_OP_xderef_size:
5208       fputc (',', asm_out_file);
5209       dw2_asm_output_data_raw (1, val1->v.val_int);
5210       break;
5211
5212     case DW_OP_const2u:
5213     case DW_OP_const2s:
5214       fputc (',', asm_out_file);
5215       dw2_asm_output_data_raw (2, val1->v.val_int);
5216       break;
5217
5218     case DW_OP_const4u:
5219     case DW_OP_const4s:
5220       fputc (',', asm_out_file);
5221       dw2_asm_output_data_raw (4, val1->v.val_int);
5222       break;
5223
5224     case DW_OP_const8u:
5225     case DW_OP_const8s:
5226       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5227       fputc (',', asm_out_file);
5228       dw2_asm_output_data_raw (8, val1->v.val_int);
5229       break;
5230
5231     case DW_OP_skip:
5232     case DW_OP_bra:
5233       {
5234         int offset;
5235
5236         gcc_assert (val1->val_class == dw_val_class_loc);
5237         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5238
5239         fputc (',', asm_out_file);
5240         dw2_asm_output_data_raw (2, offset);
5241       }
5242       break;
5243
5244     case DW_OP_constu:
5245     case DW_OP_plus_uconst:
5246     case DW_OP_regx:
5247     case DW_OP_piece:
5248       fputc (',', asm_out_file);
5249       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5250       break;
5251
5252     case DW_OP_bit_piece:
5253       fputc (',', asm_out_file);
5254       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5255       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5256       break;
5257
5258     case DW_OP_consts:
5259     case DW_OP_breg0:
5260     case DW_OP_breg1:
5261     case DW_OP_breg2:
5262     case DW_OP_breg3:
5263     case DW_OP_breg4:
5264     case DW_OP_breg5:
5265     case DW_OP_breg6:
5266     case DW_OP_breg7:
5267     case DW_OP_breg8:
5268     case DW_OP_breg9:
5269     case DW_OP_breg10:
5270     case DW_OP_breg11:
5271     case DW_OP_breg12:
5272     case DW_OP_breg13:
5273     case DW_OP_breg14:
5274     case DW_OP_breg15:
5275     case DW_OP_breg16:
5276     case DW_OP_breg17:
5277     case DW_OP_breg18:
5278     case DW_OP_breg19:
5279     case DW_OP_breg20:
5280     case DW_OP_breg21:
5281     case DW_OP_breg22:
5282     case DW_OP_breg23:
5283     case DW_OP_breg24:
5284     case DW_OP_breg25:
5285     case DW_OP_breg26:
5286     case DW_OP_breg27:
5287     case DW_OP_breg28:
5288     case DW_OP_breg29:
5289     case DW_OP_breg30:
5290     case DW_OP_breg31:
5291     case DW_OP_fbreg:
5292       fputc (',', asm_out_file);
5293       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5294       break;
5295
5296     case DW_OP_bregx:
5297       fputc (',', asm_out_file);
5298       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5299       fputc (',', asm_out_file);
5300       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5301       break;
5302
5303     default:
5304       /* Other codes have no operands.  */
5305       break;
5306     }
5307 }
5308
5309 static void
5310 output_loc_sequence_raw (dw_loc_descr_ref loc)
5311 {
5312   while (1)
5313     {
5314       /* Output the opcode.  */
5315       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5316       output_loc_operands_raw (loc);
5317
5318       if (!loc->dw_loc_next)
5319         break;
5320       loc = loc->dw_loc_next;
5321
5322       fputc (',', asm_out_file);
5323     }
5324 }
5325
5326 /* This routine will generate the correct assembly data for a location
5327    description based on a cfi entry with a complex address.  */
5328
5329 static void
5330 output_cfa_loc (dw_cfi_ref cfi)
5331 {
5332   dw_loc_descr_ref loc;
5333   unsigned long size;
5334
5335   if (cfi->dw_cfi_opc == DW_CFA_expression)
5336     {
5337       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5338       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5339     }
5340   else
5341     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5342
5343   /* Output the size of the block.  */
5344   size = size_of_locs (loc);
5345   dw2_asm_output_data_uleb128 (size, NULL);
5346
5347   /* Now output the operations themselves.  */
5348   output_loc_sequence (loc);
5349 }
5350
5351 /* Similar, but used for .cfi_escape.  */
5352
5353 static void
5354 output_cfa_loc_raw (dw_cfi_ref cfi)
5355 {
5356   dw_loc_descr_ref loc;
5357   unsigned long size;
5358
5359   if (cfi->dw_cfi_opc == DW_CFA_expression)
5360     {
5361       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5362       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5363     }
5364   else
5365     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5366
5367   /* Output the size of the block.  */
5368   size = size_of_locs (loc);
5369   dw2_asm_output_data_uleb128_raw (size);
5370   fputc (',', asm_out_file);
5371
5372   /* Now output the operations themselves.  */
5373   output_loc_sequence_raw (loc);
5374 }
5375
5376 /* This function builds a dwarf location descriptor sequence from a
5377    dw_cfa_location, adding the given OFFSET to the result of the
5378    expression.  */
5379
5380 static struct dw_loc_descr_struct *
5381 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5382 {
5383   struct dw_loc_descr_struct *head, *tmp;
5384
5385   offset += cfa->offset;
5386
5387   if (cfa->indirect)
5388     {
5389       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5390       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5391       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5392       add_loc_descr (&head, tmp);
5393       if (offset != 0)
5394         {
5395           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5396           add_loc_descr (&head, tmp);
5397         }
5398     }
5399   else
5400     head = new_reg_loc_descr (cfa->reg, offset);
5401
5402   return head;
5403 }
5404
5405 /* This function builds a dwarf location descriptor sequence for
5406    the address at OFFSET from the CFA when stack is aligned to
5407    ALIGNMENT byte.  */
5408
5409 static struct dw_loc_descr_struct *
5410 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5411 {
5412   struct dw_loc_descr_struct *head;
5413   unsigned int dwarf_fp
5414     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5415
5416  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5417   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5418     {
5419       head = new_reg_loc_descr (dwarf_fp, 0);
5420       add_loc_descr (&head, int_loc_descriptor (alignment));
5421       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5422       loc_descr_plus_const (&head, offset);
5423     }
5424   else
5425     head = new_reg_loc_descr (dwarf_fp, offset);
5426   return head;
5427 }
5428
5429 /* This function fills in aa dw_cfa_location structure from a dwarf location
5430    descriptor sequence.  */
5431
5432 static void
5433 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5434 {
5435   struct dw_loc_descr_struct *ptr;
5436   cfa->offset = 0;
5437   cfa->base_offset = 0;
5438   cfa->indirect = 0;
5439   cfa->reg = -1;
5440
5441   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5442     {
5443       enum dwarf_location_atom op = ptr->dw_loc_opc;
5444
5445       switch (op)
5446         {
5447         case DW_OP_reg0:
5448         case DW_OP_reg1:
5449         case DW_OP_reg2:
5450         case DW_OP_reg3:
5451         case DW_OP_reg4:
5452         case DW_OP_reg5:
5453         case DW_OP_reg6:
5454         case DW_OP_reg7:
5455         case DW_OP_reg8:
5456         case DW_OP_reg9:
5457         case DW_OP_reg10:
5458         case DW_OP_reg11:
5459         case DW_OP_reg12:
5460         case DW_OP_reg13:
5461         case DW_OP_reg14:
5462         case DW_OP_reg15:
5463         case DW_OP_reg16:
5464         case DW_OP_reg17:
5465         case DW_OP_reg18:
5466         case DW_OP_reg19:
5467         case DW_OP_reg20:
5468         case DW_OP_reg21:
5469         case DW_OP_reg22:
5470         case DW_OP_reg23:
5471         case DW_OP_reg24:
5472         case DW_OP_reg25:
5473         case DW_OP_reg26:
5474         case DW_OP_reg27:
5475         case DW_OP_reg28:
5476         case DW_OP_reg29:
5477         case DW_OP_reg30:
5478         case DW_OP_reg31:
5479           cfa->reg = op - DW_OP_reg0;
5480           break;
5481         case DW_OP_regx:
5482           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5483           break;
5484         case DW_OP_breg0:
5485         case DW_OP_breg1:
5486         case DW_OP_breg2:
5487         case DW_OP_breg3:
5488         case DW_OP_breg4:
5489         case DW_OP_breg5:
5490         case DW_OP_breg6:
5491         case DW_OP_breg7:
5492         case DW_OP_breg8:
5493         case DW_OP_breg9:
5494         case DW_OP_breg10:
5495         case DW_OP_breg11:
5496         case DW_OP_breg12:
5497         case DW_OP_breg13:
5498         case DW_OP_breg14:
5499         case DW_OP_breg15:
5500         case DW_OP_breg16:
5501         case DW_OP_breg17:
5502         case DW_OP_breg18:
5503         case DW_OP_breg19:
5504         case DW_OP_breg20:
5505         case DW_OP_breg21:
5506         case DW_OP_breg22:
5507         case DW_OP_breg23:
5508         case DW_OP_breg24:
5509         case DW_OP_breg25:
5510         case DW_OP_breg26:
5511         case DW_OP_breg27:
5512         case DW_OP_breg28:
5513         case DW_OP_breg29:
5514         case DW_OP_breg30:
5515         case DW_OP_breg31:
5516           cfa->reg = op - DW_OP_breg0;
5517           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5518           break;
5519         case DW_OP_bregx:
5520           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5521           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5522           break;
5523         case DW_OP_deref:
5524           cfa->indirect = 1;
5525           break;
5526         case DW_OP_plus_uconst:
5527           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5528           break;
5529         default:
5530           internal_error ("DW_LOC_OP %s not implemented",
5531                           dwarf_stack_op_name (ptr->dw_loc_opc));
5532         }
5533     }
5534 }
5535 \f
5536 /* And now, the support for symbolic debugging information.  */
5537
5538 /* .debug_str support.  */
5539 static int output_indirect_string (void **, void *);
5540
5541 static void dwarf2out_init (const char *);
5542 static void dwarf2out_finish (const char *);
5543 static void dwarf2out_assembly_start (void);
5544 static void dwarf2out_define (unsigned int, const char *);
5545 static void dwarf2out_undef (unsigned int, const char *);
5546 static void dwarf2out_start_source_file (unsigned, const char *);
5547 static void dwarf2out_end_source_file (unsigned);
5548 static void dwarf2out_function_decl (tree);
5549 static void dwarf2out_begin_block (unsigned, unsigned);
5550 static void dwarf2out_end_block (unsigned, unsigned);
5551 static bool dwarf2out_ignore_block (const_tree);
5552 static void dwarf2out_global_decl (tree);
5553 static void dwarf2out_type_decl (tree, int);
5554 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5555 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5556                                                  dw_die_ref);
5557 static void dwarf2out_abstract_function (tree);
5558 static void dwarf2out_var_location (rtx);
5559 static void dwarf2out_direct_call (tree);
5560 static void dwarf2out_virtual_call_token (tree, int);
5561 static void dwarf2out_copy_call_info (rtx, rtx);
5562 static void dwarf2out_virtual_call (int);
5563 static void dwarf2out_begin_function (tree);
5564 static void dwarf2out_set_name (tree, tree);
5565
5566 /* The debug hooks structure.  */
5567
5568 const struct gcc_debug_hooks dwarf2_debug_hooks =
5569 {
5570   dwarf2out_init,
5571   dwarf2out_finish,
5572   dwarf2out_assembly_start,
5573   dwarf2out_define,
5574   dwarf2out_undef,
5575   dwarf2out_start_source_file,
5576   dwarf2out_end_source_file,
5577   dwarf2out_begin_block,
5578   dwarf2out_end_block,
5579   dwarf2out_ignore_block,
5580   dwarf2out_source_line,
5581   dwarf2out_begin_prologue,
5582 #if VMS_DEBUGGING_INFO
5583   dwarf2out_vms_end_prologue,
5584   dwarf2out_vms_begin_epilogue,
5585 #else
5586   debug_nothing_int_charstar,
5587   debug_nothing_int_charstar,
5588 #endif
5589   dwarf2out_end_epilogue,
5590   dwarf2out_begin_function,
5591   debug_nothing_int,            /* end_function */
5592   dwarf2out_function_decl,      /* function_decl */
5593   dwarf2out_global_decl,
5594   dwarf2out_type_decl,          /* type_decl */
5595   dwarf2out_imported_module_or_decl,
5596   debug_nothing_tree,           /* deferred_inline_function */
5597   /* The DWARF 2 backend tries to reduce debugging bloat by not
5598      emitting the abstract description of inline functions until
5599      something tries to reference them.  */
5600   dwarf2out_abstract_function,  /* outlining_inline_function */
5601   debug_nothing_rtx,            /* label */
5602   debug_nothing_int,            /* handle_pch */
5603   dwarf2out_var_location,
5604   dwarf2out_switch_text_section,
5605   dwarf2out_direct_call,
5606   dwarf2out_virtual_call_token,
5607   dwarf2out_copy_call_info,
5608   dwarf2out_virtual_call,
5609   dwarf2out_set_name,
5610   1                             /* start_end_main_source_file */
5611 };
5612 \f
5613 /* NOTE: In the comments in this file, many references are made to
5614    "Debugging Information Entries".  This term is abbreviated as `DIE'
5615    throughout the remainder of this file.  */
5616
5617 /* An internal representation of the DWARF output is built, and then
5618    walked to generate the DWARF debugging info.  The walk of the internal
5619    representation is done after the entire program has been compiled.
5620    The types below are used to describe the internal representation.  */
5621
5622 /* Various DIE's use offsets relative to the beginning of the
5623    .debug_info section to refer to each other.  */
5624
5625 typedef long int dw_offset;
5626
5627 /* Define typedefs here to avoid circular dependencies.  */
5628
5629 typedef struct dw_attr_struct *dw_attr_ref;
5630 typedef struct dw_line_info_struct *dw_line_info_ref;
5631 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5632 typedef struct pubname_struct *pubname_ref;
5633 typedef struct dw_ranges_struct *dw_ranges_ref;
5634 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5635 typedef struct comdat_type_struct *comdat_type_node_ref;
5636
5637 /* Each entry in the line_info_table maintains the file and
5638    line number associated with the label generated for that
5639    entry.  The label gives the PC value associated with
5640    the line number entry.  */
5641
5642 typedef struct GTY(()) dw_line_info_struct {
5643   unsigned long dw_file_num;
5644   unsigned long dw_line_num;
5645 }
5646 dw_line_info_entry;
5647
5648 /* Line information for functions in separate sections; each one gets its
5649    own sequence.  */
5650 typedef struct GTY(()) dw_separate_line_info_struct {
5651   unsigned long dw_file_num;
5652   unsigned long dw_line_num;
5653   unsigned long function;
5654 }
5655 dw_separate_line_info_entry;
5656
5657 /* Each DIE attribute has a field specifying the attribute kind,
5658    a link to the next attribute in the chain, and an attribute value.
5659    Attributes are typically linked below the DIE they modify.  */
5660
5661 typedef struct GTY(()) dw_attr_struct {
5662   enum dwarf_attribute dw_attr;
5663   dw_val_node dw_attr_val;
5664 }
5665 dw_attr_node;
5666
5667 DEF_VEC_O(dw_attr_node);
5668 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5669
5670 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5671    The children of each node form a circular list linked by
5672    die_sib.  die_child points to the node *before* the "first" child node.  */
5673
5674 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5675   enum dwarf_tag die_tag;
5676   union die_symbol_or_type_node
5677     {
5678       char * GTY ((tag ("0"))) die_symbol;
5679       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5680     }
5681   GTY ((desc ("dwarf_version >= 4"))) die_id;
5682   VEC(dw_attr_node,gc) * die_attr;
5683   dw_die_ref die_parent;
5684   dw_die_ref die_child;
5685   dw_die_ref die_sib;
5686   dw_die_ref die_definition; /* ref from a specification to its definition */
5687   dw_offset die_offset;
5688   unsigned long die_abbrev;
5689   int die_mark;
5690   /* Die is used and must not be pruned as unused.  */
5691   int die_perennial_p;
5692   unsigned int decl_id;
5693 }
5694 die_node;
5695
5696 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5697 #define FOR_EACH_CHILD(die, c, expr) do {       \
5698   c = die->die_child;                           \
5699   if (c) do {                                   \
5700     c = c->die_sib;                             \
5701     expr;                                       \
5702   } while (c != die->die_child);                \
5703 } while (0)
5704
5705 /* The pubname structure */
5706
5707 typedef struct GTY(()) pubname_struct {
5708   dw_die_ref die;
5709   const char *name;
5710 }
5711 pubname_entry;
5712
5713 DEF_VEC_O(pubname_entry);
5714 DEF_VEC_ALLOC_O(pubname_entry, gc);
5715
5716 struct GTY(()) dw_ranges_struct {
5717   /* If this is positive, it's a block number, otherwise it's a
5718      bitwise-negated index into dw_ranges_by_label.  */
5719   int num;
5720 };
5721
5722 struct GTY(()) dw_ranges_by_label_struct {
5723   const char *begin;
5724   const char *end;
5725 };
5726
5727 /* The comdat type node structure.  */
5728 typedef struct GTY(()) comdat_type_struct
5729 {
5730   dw_die_ref root_die;
5731   dw_die_ref type_die;
5732   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5733   struct comdat_type_struct *next;
5734 }
5735 comdat_type_node;
5736
5737 /* The limbo die list structure.  */
5738 typedef struct GTY(()) limbo_die_struct {
5739   dw_die_ref die;
5740   tree created_for;
5741   struct limbo_die_struct *next;
5742 }
5743 limbo_die_node;
5744
5745 typedef struct GTY(()) skeleton_chain_struct
5746 {
5747   dw_die_ref old_die;
5748   dw_die_ref new_die;
5749   struct skeleton_chain_struct *parent;
5750 }
5751 skeleton_chain_node;
5752
5753 /* How to start an assembler comment.  */
5754 #ifndef ASM_COMMENT_START
5755 #define ASM_COMMENT_START ";#"
5756 #endif
5757
5758 /* Define a macro which returns nonzero for a TYPE_DECL which was
5759    implicitly generated for a tagged type.
5760
5761    Note that unlike the gcc front end (which generates a NULL named
5762    TYPE_DECL node for each complete tagged type, each array type, and
5763    each function type node created) the g++ front end generates a
5764    _named_ TYPE_DECL node for each tagged type node created.
5765    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5766    generate a DW_TAG_typedef DIE for them.  */
5767
5768 #define TYPE_DECL_IS_STUB(decl)                         \
5769   (DECL_NAME (decl) == NULL_TREE                        \
5770    || (DECL_ARTIFICIAL (decl)                           \
5771        && is_tagged_type (TREE_TYPE (decl))             \
5772        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5773            /* This is necessary for stub decls that     \
5774               appear in nested inline functions.  */    \
5775            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5776                && (decl_ultimate_origin (decl)          \
5777                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5778
5779 /* Information concerning the compilation unit's programming
5780    language, and compiler version.  */
5781
5782 /* Fixed size portion of the DWARF compilation unit header.  */
5783 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5784   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5785
5786 /* Fixed size portion of the DWARF comdat type unit header.  */
5787 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5788   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5789    + DWARF_OFFSET_SIZE)
5790
5791 /* Fixed size portion of public names info.  */
5792 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5793
5794 /* Fixed size portion of the address range info.  */
5795 #define DWARF_ARANGES_HEADER_SIZE                                       \
5796   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5797                 DWARF2_ADDR_SIZE * 2)                                   \
5798    - DWARF_INITIAL_LENGTH_SIZE)
5799
5800 /* Size of padding portion in the address range info.  It must be
5801    aligned to twice the pointer size.  */
5802 #define DWARF_ARANGES_PAD_SIZE \
5803   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5804                 DWARF2_ADDR_SIZE * 2)                              \
5805    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5806
5807 /* Use assembler line directives if available.  */
5808 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5809 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5810 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5811 #else
5812 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5813 #endif
5814 #endif
5815
5816 /* Minimum line offset in a special line info. opcode.
5817    This value was chosen to give a reasonable range of values.  */
5818 #define DWARF_LINE_BASE  -10
5819
5820 /* First special line opcode - leave room for the standard opcodes.  */
5821 #define DWARF_LINE_OPCODE_BASE  10
5822
5823 /* Range of line offsets in a special line info. opcode.  */
5824 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5825
5826 /* Flag that indicates the initial value of the is_stmt_start flag.
5827    In the present implementation, we do not mark any lines as
5828    the beginning of a source statement, because that information
5829    is not made available by the GCC front-end.  */
5830 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5831
5832 /* Maximum number of operations per instruction bundle.  */
5833 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5834 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5835 #endif
5836
5837 /* This location is used by calc_die_sizes() to keep track
5838    the offset of each DIE within the .debug_info section.  */
5839 static unsigned long next_die_offset;
5840
5841 /* Record the root of the DIE's built for the current compilation unit.  */
5842 static GTY(()) dw_die_ref comp_unit_die;
5843
5844 /* A list of type DIEs that have been separated into comdat sections.  */
5845 static GTY(()) comdat_type_node *comdat_type_list;
5846
5847 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5848 static GTY(()) limbo_die_node *limbo_die_list;
5849
5850 /* A list of DIEs for which we may have to generate
5851    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5852 static GTY(()) limbo_die_node *deferred_asm_name;
5853
5854 /* Filenames referenced by this compilation unit.  */
5855 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5856
5857 /* A hash table of references to DIE's that describe declarations.
5858    The key is a DECL_UID() which is a unique number identifying each decl.  */
5859 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5860
5861 /* A hash table of references to DIE's that describe COMMON blocks.
5862    The key is DECL_UID() ^ die_parent.  */
5863 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5864
5865 typedef struct GTY(()) die_arg_entry_struct {
5866     dw_die_ref die;
5867     tree arg;
5868 } die_arg_entry;
5869
5870 DEF_VEC_O(die_arg_entry);
5871 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5872
5873 /* Node of the variable location list.  */
5874 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5875   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5876      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5877      in mode of the EXPR_LIST node and first EXPR_LIST operand
5878      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5879      location or NULL for padding.  For larger bitsizes,
5880      mode is 0 and first operand is a CONCAT with bitsize
5881      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5882      NULL as second operand.  */
5883   rtx GTY (()) loc;
5884   const char * GTY (()) label;
5885   struct var_loc_node * GTY (()) next;
5886 };
5887
5888 /* Variable location list.  */
5889 struct GTY (()) var_loc_list_def {
5890   struct var_loc_node * GTY (()) first;
5891
5892   /* Pointer to the last but one or last element of the
5893      chained list.  If the list is empty, both first and
5894      last are NULL, if the list contains just one node
5895      or the last node certainly is not redundant, it points
5896      to the last node, otherwise points to the last but one.
5897      Do not mark it for GC because it is marked through the chain.  */
5898   struct var_loc_node * GTY ((skip ("%h"))) last;
5899
5900   /* DECL_UID of the variable decl.  */
5901   unsigned int decl_id;
5902 };
5903 typedef struct var_loc_list_def var_loc_list;
5904
5905
5906 /* Table of decl location linked lists.  */
5907 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5908
5909 /* A pointer to the base of a list of references to DIE's that
5910    are uniquely identified by their tag, presence/absence of
5911    children DIE's, and list of attribute/value pairs.  */
5912 static GTY((length ("abbrev_die_table_allocated")))
5913   dw_die_ref *abbrev_die_table;
5914
5915 /* Number of elements currently allocated for abbrev_die_table.  */
5916 static GTY(()) unsigned abbrev_die_table_allocated;
5917
5918 /* Number of elements in type_die_table currently in use.  */
5919 static GTY(()) unsigned abbrev_die_table_in_use;
5920
5921 /* Size (in elements) of increments by which we may expand the
5922    abbrev_die_table.  */
5923 #define ABBREV_DIE_TABLE_INCREMENT 256
5924
5925 /* A pointer to the base of a table that contains line information
5926    for each source code line in .text in the compilation unit.  */
5927 static GTY((length ("line_info_table_allocated")))
5928      dw_line_info_ref line_info_table;
5929
5930 /* Number of elements currently allocated for line_info_table.  */
5931 static GTY(()) unsigned line_info_table_allocated;
5932
5933 /* Number of elements in line_info_table currently in use.  */
5934 static GTY(()) unsigned line_info_table_in_use;
5935
5936 /* A pointer to the base of a table that contains line information
5937    for each source code line outside of .text in the compilation unit.  */
5938 static GTY ((length ("separate_line_info_table_allocated")))
5939      dw_separate_line_info_ref separate_line_info_table;
5940
5941 /* Number of elements currently allocated for separate_line_info_table.  */
5942 static GTY(()) unsigned separate_line_info_table_allocated;
5943
5944 /* Number of elements in separate_line_info_table currently in use.  */
5945 static GTY(()) unsigned separate_line_info_table_in_use;
5946
5947 /* Size (in elements) of increments by which we may expand the
5948    line_info_table.  */
5949 #define LINE_INFO_TABLE_INCREMENT 1024
5950
5951 /* A pointer to the base of a table that contains a list of publicly
5952    accessible names.  */
5953 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5954
5955 /* A pointer to the base of a table that contains a list of publicly
5956    accessible types.  */
5957 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5958
5959 /* Array of dies for which we should generate .debug_arange info.  */
5960 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5961
5962 /* Number of elements currently allocated for arange_table.  */
5963 static GTY(()) unsigned arange_table_allocated;
5964
5965 /* Number of elements in arange_table currently in use.  */
5966 static GTY(()) unsigned arange_table_in_use;
5967
5968 /* Size (in elements) of increments by which we may expand the
5969    arange_table.  */
5970 #define ARANGE_TABLE_INCREMENT 64
5971
5972 /* Array of dies for which we should generate .debug_ranges info.  */
5973 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5974
5975 /* Number of elements currently allocated for ranges_table.  */
5976 static GTY(()) unsigned ranges_table_allocated;
5977
5978 /* Number of elements in ranges_table currently in use.  */
5979 static GTY(()) unsigned ranges_table_in_use;
5980
5981 /* Array of pairs of labels referenced in ranges_table.  */
5982 static GTY ((length ("ranges_by_label_allocated")))
5983      dw_ranges_by_label_ref ranges_by_label;
5984
5985 /* Number of elements currently allocated for ranges_by_label.  */
5986 static GTY(()) unsigned ranges_by_label_allocated;
5987
5988 /* Number of elements in ranges_by_label currently in use.  */
5989 static GTY(()) unsigned ranges_by_label_in_use;
5990
5991 /* Size (in elements) of increments by which we may expand the
5992    ranges_table.  */
5993 #define RANGES_TABLE_INCREMENT 64
5994
5995 /* Whether we have location lists that need outputting */
5996 static GTY(()) bool have_location_lists;
5997
5998 /* Unique label counter.  */
5999 static GTY(()) unsigned int loclabel_num;
6000
6001 /* Unique label counter for point-of-call tables.  */
6002 static GTY(()) unsigned int poc_label_num;
6003
6004 /* The direct call table structure.  */
6005
6006 typedef struct GTY(()) dcall_struct {
6007   unsigned int poc_label_num;
6008   tree poc_decl;
6009   dw_die_ref targ_die;
6010 }
6011 dcall_entry;
6012
6013 DEF_VEC_O(dcall_entry);
6014 DEF_VEC_ALLOC_O(dcall_entry, gc);
6015
6016 /* The virtual call table structure.  */
6017
6018 typedef struct GTY(()) vcall_struct {
6019   unsigned int poc_label_num;
6020   unsigned int vtable_slot;
6021 }
6022 vcall_entry;
6023
6024 DEF_VEC_O(vcall_entry);
6025 DEF_VEC_ALLOC_O(vcall_entry, gc);
6026
6027 /* Pointers to the direct and virtual call tables.  */
6028 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6029 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6030
6031 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6032
6033 struct GTY (()) vcall_insn {
6034   int insn_uid;
6035   unsigned int vtable_slot;
6036 };
6037
6038 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6039
6040 /* Record whether the function being analyzed contains inlined functions.  */
6041 static int current_function_has_inlines;
6042 #if 0 && defined (MIPS_DEBUGGING_INFO)
6043 static int comp_unit_has_inlines;
6044 #endif
6045
6046 /* The last file entry emitted by maybe_emit_file().  */
6047 static GTY(()) struct dwarf_file_data * last_emitted_file;
6048
6049 /* Number of internal labels generated by gen_internal_sym().  */
6050 static GTY(()) int label_num;
6051
6052 /* Cached result of previous call to lookup_filename.  */
6053 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6054
6055 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6056
6057 /* Offset from the "steady-state frame pointer" to the frame base,
6058    within the current function.  */
6059 static HOST_WIDE_INT frame_pointer_fb_offset;
6060
6061 /* Forward declarations for functions defined in this file.  */
6062
6063 static int is_pseudo_reg (const_rtx);
6064 static tree type_main_variant (tree);
6065 static int is_tagged_type (const_tree);
6066 static const char *dwarf_tag_name (unsigned);
6067 static const char *dwarf_attr_name (unsigned);
6068 static const char *dwarf_form_name (unsigned);
6069 static tree decl_ultimate_origin (const_tree);
6070 static tree decl_class_context (tree);
6071 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6072 static inline enum dw_val_class AT_class (dw_attr_ref);
6073 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6074 static inline unsigned AT_flag (dw_attr_ref);
6075 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6076 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6077 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6078 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6079 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6080                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6081 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6082                                unsigned int, unsigned char *);
6083 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6084 static hashval_t debug_str_do_hash (const void *);
6085 static int debug_str_eq (const void *, const void *);
6086 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6087 static inline const char *AT_string (dw_attr_ref);
6088 static enum dwarf_form AT_string_form (dw_attr_ref);
6089 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6090 static void add_AT_specification (dw_die_ref, dw_die_ref);
6091 static inline dw_die_ref AT_ref (dw_attr_ref);
6092 static inline int AT_ref_external (dw_attr_ref);
6093 static inline void set_AT_ref_external (dw_attr_ref, int);
6094 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6095 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6096 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6097 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6098                              dw_loc_list_ref);
6099 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6100 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6101 static inline rtx AT_addr (dw_attr_ref);
6102 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6103 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6104 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6105 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6106                            unsigned HOST_WIDE_INT);
6107 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6108                                unsigned long);
6109 static inline const char *AT_lbl (dw_attr_ref);
6110 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6111 static const char *get_AT_low_pc (dw_die_ref);
6112 static const char *get_AT_hi_pc (dw_die_ref);
6113 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6114 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6115 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6116 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6117 static bool is_cxx (void);
6118 static bool is_fortran (void);
6119 static bool is_ada (void);
6120 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6121 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6122 static void add_child_die (dw_die_ref, dw_die_ref);
6123 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6124 static dw_die_ref lookup_type_die (tree);
6125 static void equate_type_number_to_die (tree, dw_die_ref);
6126 static hashval_t decl_die_table_hash (const void *);
6127 static int decl_die_table_eq (const void *, const void *);
6128 static dw_die_ref lookup_decl_die (tree);
6129 static hashval_t common_block_die_table_hash (const void *);
6130 static int common_block_die_table_eq (const void *, const void *);
6131 static hashval_t decl_loc_table_hash (const void *);
6132 static int decl_loc_table_eq (const void *, const void *);
6133 static var_loc_list *lookup_decl_loc (const_tree);
6134 static void equate_decl_number_to_die (tree, dw_die_ref);
6135 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6136 static void print_spaces (FILE *);
6137 static void print_die (dw_die_ref, FILE *);
6138 static void print_dwarf_line_table (FILE *);
6139 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6140 static dw_die_ref pop_compile_unit (dw_die_ref);
6141 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6142 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6143 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6144 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6145 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6146 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6147 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6148                                    struct md5_ctx *, int *);
6149 struct checksum_attributes;
6150 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6151 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6152 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6153 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6154 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6155 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6156 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6157 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6158 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6159 static void compute_section_prefix (dw_die_ref);
6160 static int is_type_die (dw_die_ref);
6161 static int is_comdat_die (dw_die_ref);
6162 static int is_symbol_die (dw_die_ref);
6163 static void assign_symbol_names (dw_die_ref);
6164 static void break_out_includes (dw_die_ref);
6165 static int is_declaration_die (dw_die_ref);
6166 static int should_move_die_to_comdat (dw_die_ref);
6167 static dw_die_ref clone_as_declaration (dw_die_ref);
6168 static dw_die_ref clone_die (dw_die_ref);
6169 static dw_die_ref clone_tree (dw_die_ref);
6170 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6171 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6172 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6173 static dw_die_ref generate_skeleton (dw_die_ref);
6174 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6175                                                          dw_die_ref);
6176 static void break_out_comdat_types (dw_die_ref);
6177 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6178 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6179 static void copy_decls_for_unworthy_types (dw_die_ref);
6180
6181 static hashval_t htab_cu_hash (const void *);
6182 static int htab_cu_eq (const void *, const void *);
6183 static void htab_cu_del (void *);
6184 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6185 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6186 static void add_sibling_attributes (dw_die_ref);
6187 static void build_abbrev_table (dw_die_ref);
6188 static void output_location_lists (dw_die_ref);
6189 static int constant_size (unsigned HOST_WIDE_INT);
6190 static unsigned long size_of_die (dw_die_ref);
6191 static void calc_die_sizes (dw_die_ref);
6192 static void mark_dies (dw_die_ref);
6193 static void unmark_dies (dw_die_ref);
6194 static void unmark_all_dies (dw_die_ref);
6195 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6196 static unsigned long size_of_aranges (void);
6197 static enum dwarf_form value_format (dw_attr_ref);
6198 static void output_value_format (dw_attr_ref);
6199 static void output_abbrev_section (void);
6200 static void output_die_symbol (dw_die_ref);
6201 static void output_die (dw_die_ref);
6202 static void output_compilation_unit_header (void);
6203 static void output_comp_unit (dw_die_ref, int);
6204 static void output_comdat_type_unit (comdat_type_node *);
6205 static const char *dwarf2_name (tree, int);
6206 static void add_pubname (tree, dw_die_ref);
6207 static void add_pubname_string (const char *, dw_die_ref);
6208 static void add_pubtype (tree, dw_die_ref);
6209 static void output_pubnames (VEC (pubname_entry,gc) *);
6210 static void add_arange (tree, dw_die_ref);
6211 static void output_aranges (void);
6212 static unsigned int add_ranges_num (int);
6213 static unsigned int add_ranges (const_tree);
6214 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6215                                   bool *);
6216 static void output_ranges (void);
6217 static void output_line_info (void);
6218 static void output_file_names (void);
6219 static dw_die_ref base_type_die (tree);
6220 static int is_base_type (tree);
6221 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6222 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6223 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6224 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6225 static int type_is_enum (const_tree);
6226 static unsigned int dbx_reg_number (const_rtx);
6227 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6228 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6229 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6230                                                 enum var_init_status);
6231 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6232                                                      enum var_init_status);
6233 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6234                                          enum var_init_status);
6235 static int is_based_loc (const_rtx);
6236 static int resolve_one_addr (rtx *, void *);
6237 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6238                                                enum var_init_status);
6239 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6240                                         enum var_init_status);
6241 static dw_loc_list_ref loc_list_from_tree (tree, int);
6242 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6243 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6244 static tree field_type (const_tree);
6245 static unsigned int simple_type_align_in_bits (const_tree);
6246 static unsigned int simple_decl_align_in_bits (const_tree);
6247 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6248 static HOST_WIDE_INT field_byte_offset (const_tree);
6249 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6250                                          dw_loc_list_ref);
6251 static void add_data_member_location_attribute (dw_die_ref, tree);
6252 static bool add_const_value_attribute (dw_die_ref, rtx);
6253 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6254 static void insert_double (double_int, unsigned char *);
6255 static void insert_float (const_rtx, unsigned char *);
6256 static rtx rtl_for_decl_location (tree);
6257 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6258                                                    enum dwarf_attribute);
6259 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6260 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6261 static void add_name_attribute (dw_die_ref, const char *);
6262 static void add_comp_dir_attribute (dw_die_ref);
6263 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6264 static void add_subscript_info (dw_die_ref, tree, bool);
6265 static void add_byte_size_attribute (dw_die_ref, tree);
6266 static void add_bit_offset_attribute (dw_die_ref, tree);
6267 static void add_bit_size_attribute (dw_die_ref, tree);
6268 static void add_prototyped_attribute (dw_die_ref, tree);
6269 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6270 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6271 static void add_src_coords_attributes (dw_die_ref, tree);
6272 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6273 static void push_decl_scope (tree);
6274 static void pop_decl_scope (void);
6275 static dw_die_ref scope_die_for (tree, dw_die_ref);
6276 static inline int local_scope_p (dw_die_ref);
6277 static inline int class_scope_p (dw_die_ref);
6278 static inline int class_or_namespace_scope_p (dw_die_ref);
6279 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6280 static void add_calling_convention_attribute (dw_die_ref, tree);
6281 static const char *type_tag (const_tree);
6282 static tree member_declared_type (const_tree);
6283 #if 0
6284 static const char *decl_start_label (tree);
6285 #endif
6286 static void gen_array_type_die (tree, dw_die_ref);
6287 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6288 #if 0
6289 static void gen_entry_point_die (tree, dw_die_ref);
6290 #endif
6291 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6292 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6293 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6294 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6295 static void gen_formal_types_die (tree, dw_die_ref);
6296 static void gen_subprogram_die (tree, dw_die_ref);
6297 static void gen_variable_die (tree, tree, dw_die_ref);
6298 static void gen_const_die (tree, dw_die_ref);
6299 static void gen_label_die (tree, dw_die_ref);
6300 static void gen_lexical_block_die (tree, dw_die_ref, int);
6301 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6302 static void gen_field_die (tree, dw_die_ref);
6303 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6304 static dw_die_ref gen_compile_unit_die (const char *);
6305 static void gen_inheritance_die (tree, tree, dw_die_ref);
6306 static void gen_member_die (tree, dw_die_ref);
6307 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6308                                                 enum debug_info_usage);
6309 static void gen_subroutine_type_die (tree, dw_die_ref);
6310 static void gen_typedef_die (tree, dw_die_ref);
6311 static void gen_type_die (tree, dw_die_ref);
6312 static void gen_block_die (tree, dw_die_ref, int);
6313 static void decls_for_scope (tree, dw_die_ref, int);
6314 static int is_redundant_typedef (const_tree);
6315 static bool is_naming_typedef_decl (const_tree);
6316 static inline dw_die_ref get_context_die (tree);
6317 static void gen_namespace_die (tree, dw_die_ref);
6318 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6319 static dw_die_ref force_decl_die (tree);
6320 static dw_die_ref force_type_die (tree);
6321 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6322 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6323 static struct dwarf_file_data * lookup_filename (const char *);
6324 static void retry_incomplete_types (void);
6325 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6326 static void gen_generic_params_dies (tree);
6327 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6328 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6329 static void splice_child_die (dw_die_ref, dw_die_ref);
6330 static int file_info_cmp (const void *, const void *);
6331 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6332                                      const char *, const char *);
6333 static void output_loc_list (dw_loc_list_ref);
6334 static char *gen_internal_sym (const char *);
6335
6336 static void prune_unmark_dies (dw_die_ref);
6337 static void prune_unused_types_mark (dw_die_ref, int);
6338 static void prune_unused_types_walk (dw_die_ref);
6339 static void prune_unused_types_walk_attribs (dw_die_ref);
6340 static void prune_unused_types_prune (dw_die_ref);
6341 static void prune_unused_types (void);
6342 static int maybe_emit_file (struct dwarf_file_data *fd);
6343 static inline const char *AT_vms_delta1 (dw_attr_ref);
6344 static inline const char *AT_vms_delta2 (dw_attr_ref);
6345 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6346                                      const char *, const char *);
6347 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6348 static void gen_remaining_tmpl_value_param_die_attribute (void);
6349
6350 /* Section names used to hold DWARF debugging information.  */
6351 #ifndef DEBUG_INFO_SECTION
6352 #define DEBUG_INFO_SECTION      ".debug_info"
6353 #endif
6354 #ifndef DEBUG_ABBREV_SECTION
6355 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6356 #endif
6357 #ifndef DEBUG_ARANGES_SECTION
6358 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6359 #endif
6360 #ifndef DEBUG_MACINFO_SECTION
6361 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6362 #endif
6363 #ifndef DEBUG_LINE_SECTION
6364 #define DEBUG_LINE_SECTION      ".debug_line"
6365 #endif
6366 #ifndef DEBUG_LOC_SECTION
6367 #define DEBUG_LOC_SECTION       ".debug_loc"
6368 #endif
6369 #ifndef DEBUG_PUBNAMES_SECTION
6370 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6371 #endif
6372 #ifndef DEBUG_PUBTYPES_SECTION
6373 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6374 #endif
6375 #ifndef DEBUG_DCALL_SECTION
6376 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6377 #endif
6378 #ifndef DEBUG_VCALL_SECTION
6379 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6380 #endif
6381 #ifndef DEBUG_STR_SECTION
6382 #define DEBUG_STR_SECTION       ".debug_str"
6383 #endif
6384 #ifndef DEBUG_RANGES_SECTION
6385 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6386 #endif
6387
6388 /* Standard ELF section names for compiled code and data.  */
6389 #ifndef TEXT_SECTION_NAME
6390 #define TEXT_SECTION_NAME       ".text"
6391 #endif
6392
6393 /* Section flags for .debug_str section.  */
6394 #define DEBUG_STR_SECTION_FLAGS \
6395   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6396    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6397    : SECTION_DEBUG)
6398
6399 /* Labels we insert at beginning sections we can reference instead of
6400    the section names themselves.  */
6401
6402 #ifndef TEXT_SECTION_LABEL
6403 #define TEXT_SECTION_LABEL              "Ltext"
6404 #endif
6405 #ifndef COLD_TEXT_SECTION_LABEL
6406 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6407 #endif
6408 #ifndef DEBUG_LINE_SECTION_LABEL
6409 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6410 #endif
6411 #ifndef DEBUG_INFO_SECTION_LABEL
6412 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6413 #endif
6414 #ifndef DEBUG_ABBREV_SECTION_LABEL
6415 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6416 #endif
6417 #ifndef DEBUG_LOC_SECTION_LABEL
6418 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6419 #endif
6420 #ifndef DEBUG_RANGES_SECTION_LABEL
6421 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6422 #endif
6423 #ifndef DEBUG_MACINFO_SECTION_LABEL
6424 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6425 #endif
6426
6427
6428 /* Definitions of defaults for formats and names of various special
6429    (artificial) labels which may be generated within this file (when the -g
6430    options is used and DWARF2_DEBUGGING_INFO is in effect.
6431    If necessary, these may be overridden from within the tm.h file, but
6432    typically, overriding these defaults is unnecessary.  */
6433
6434 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6435 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6436 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6437 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6438 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6439 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6440 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6441 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6442 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6443 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6444
6445 #ifndef TEXT_END_LABEL
6446 #define TEXT_END_LABEL          "Letext"
6447 #endif
6448 #ifndef COLD_END_LABEL
6449 #define COLD_END_LABEL          "Letext_cold"
6450 #endif
6451 #ifndef BLOCK_BEGIN_LABEL
6452 #define BLOCK_BEGIN_LABEL       "LBB"
6453 #endif
6454 #ifndef BLOCK_END_LABEL
6455 #define BLOCK_END_LABEL         "LBE"
6456 #endif
6457 #ifndef LINE_CODE_LABEL
6458 #define LINE_CODE_LABEL         "LM"
6459 #endif
6460 #ifndef SEPARATE_LINE_CODE_LABEL
6461 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6462 #endif
6463
6464 \f
6465 /* We allow a language front-end to designate a function that is to be
6466    called to "demangle" any name before it is put into a DIE.  */
6467
6468 static const char *(*demangle_name_func) (const char *);
6469
6470 void
6471 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6472 {
6473   demangle_name_func = func;
6474 }
6475
6476 /* Test if rtl node points to a pseudo register.  */
6477
6478 static inline int
6479 is_pseudo_reg (const_rtx rtl)
6480 {
6481   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6482           || (GET_CODE (rtl) == SUBREG
6483               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6484 }
6485
6486 /* Return a reference to a type, with its const and volatile qualifiers
6487    removed.  */
6488
6489 static inline tree
6490 type_main_variant (tree type)
6491 {
6492   type = TYPE_MAIN_VARIANT (type);
6493
6494   /* ??? There really should be only one main variant among any group of
6495      variants of a given type (and all of the MAIN_VARIANT values for all
6496      members of the group should point to that one type) but sometimes the C
6497      front-end messes this up for array types, so we work around that bug
6498      here.  */
6499   if (TREE_CODE (type) == ARRAY_TYPE)
6500     while (type != TYPE_MAIN_VARIANT (type))
6501       type = TYPE_MAIN_VARIANT (type);
6502
6503   return type;
6504 }
6505
6506 /* Return nonzero if the given type node represents a tagged type.  */
6507
6508 static inline int
6509 is_tagged_type (const_tree type)
6510 {
6511   enum tree_code code = TREE_CODE (type);
6512
6513   return (code == RECORD_TYPE || code == UNION_TYPE
6514           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6515 }
6516
6517 /* Convert a DIE tag into its string name.  */
6518
6519 static const char *
6520 dwarf_tag_name (unsigned int tag)
6521 {
6522   switch (tag)
6523     {
6524     case DW_TAG_padding:
6525       return "DW_TAG_padding";
6526     case DW_TAG_array_type:
6527       return "DW_TAG_array_type";
6528     case DW_TAG_class_type:
6529       return "DW_TAG_class_type";
6530     case DW_TAG_entry_point:
6531       return "DW_TAG_entry_point";
6532     case DW_TAG_enumeration_type:
6533       return "DW_TAG_enumeration_type";
6534     case DW_TAG_formal_parameter:
6535       return "DW_TAG_formal_parameter";
6536     case DW_TAG_imported_declaration:
6537       return "DW_TAG_imported_declaration";
6538     case DW_TAG_label:
6539       return "DW_TAG_label";
6540     case DW_TAG_lexical_block:
6541       return "DW_TAG_lexical_block";
6542     case DW_TAG_member:
6543       return "DW_TAG_member";
6544     case DW_TAG_pointer_type:
6545       return "DW_TAG_pointer_type";
6546     case DW_TAG_reference_type:
6547       return "DW_TAG_reference_type";
6548     case DW_TAG_compile_unit:
6549       return "DW_TAG_compile_unit";
6550     case DW_TAG_string_type:
6551       return "DW_TAG_string_type";
6552     case DW_TAG_structure_type:
6553       return "DW_TAG_structure_type";
6554     case DW_TAG_subroutine_type:
6555       return "DW_TAG_subroutine_type";
6556     case DW_TAG_typedef:
6557       return "DW_TAG_typedef";
6558     case DW_TAG_union_type:
6559       return "DW_TAG_union_type";
6560     case DW_TAG_unspecified_parameters:
6561       return "DW_TAG_unspecified_parameters";
6562     case DW_TAG_variant:
6563       return "DW_TAG_variant";
6564     case DW_TAG_common_block:
6565       return "DW_TAG_common_block";
6566     case DW_TAG_common_inclusion:
6567       return "DW_TAG_common_inclusion";
6568     case DW_TAG_inheritance:
6569       return "DW_TAG_inheritance";
6570     case DW_TAG_inlined_subroutine:
6571       return "DW_TAG_inlined_subroutine";
6572     case DW_TAG_module:
6573       return "DW_TAG_module";
6574     case DW_TAG_ptr_to_member_type:
6575       return "DW_TAG_ptr_to_member_type";
6576     case DW_TAG_set_type:
6577       return "DW_TAG_set_type";
6578     case DW_TAG_subrange_type:
6579       return "DW_TAG_subrange_type";
6580     case DW_TAG_with_stmt:
6581       return "DW_TAG_with_stmt";
6582     case DW_TAG_access_declaration:
6583       return "DW_TAG_access_declaration";
6584     case DW_TAG_base_type:
6585       return "DW_TAG_base_type";
6586     case DW_TAG_catch_block:
6587       return "DW_TAG_catch_block";
6588     case DW_TAG_const_type:
6589       return "DW_TAG_const_type";
6590     case DW_TAG_constant:
6591       return "DW_TAG_constant";
6592     case DW_TAG_enumerator:
6593       return "DW_TAG_enumerator";
6594     case DW_TAG_file_type:
6595       return "DW_TAG_file_type";
6596     case DW_TAG_friend:
6597       return "DW_TAG_friend";
6598     case DW_TAG_namelist:
6599       return "DW_TAG_namelist";
6600     case DW_TAG_namelist_item:
6601       return "DW_TAG_namelist_item";
6602     case DW_TAG_packed_type:
6603       return "DW_TAG_packed_type";
6604     case DW_TAG_subprogram:
6605       return "DW_TAG_subprogram";
6606     case DW_TAG_template_type_param:
6607       return "DW_TAG_template_type_param";
6608     case DW_TAG_template_value_param:
6609       return "DW_TAG_template_value_param";
6610     case DW_TAG_thrown_type:
6611       return "DW_TAG_thrown_type";
6612     case DW_TAG_try_block:
6613       return "DW_TAG_try_block";
6614     case DW_TAG_variant_part:
6615       return "DW_TAG_variant_part";
6616     case DW_TAG_variable:
6617       return "DW_TAG_variable";
6618     case DW_TAG_volatile_type:
6619       return "DW_TAG_volatile_type";
6620     case DW_TAG_dwarf_procedure:
6621       return "DW_TAG_dwarf_procedure";
6622     case DW_TAG_restrict_type:
6623       return "DW_TAG_restrict_type";
6624     case DW_TAG_interface_type:
6625       return "DW_TAG_interface_type";
6626     case DW_TAG_namespace:
6627       return "DW_TAG_namespace";
6628     case DW_TAG_imported_module:
6629       return "DW_TAG_imported_module";
6630     case DW_TAG_unspecified_type:
6631       return "DW_TAG_unspecified_type";
6632     case DW_TAG_partial_unit:
6633       return "DW_TAG_partial_unit";
6634     case DW_TAG_imported_unit:
6635       return "DW_TAG_imported_unit";
6636     case DW_TAG_condition:
6637       return "DW_TAG_condition";
6638     case DW_TAG_shared_type:
6639       return "DW_TAG_shared_type";
6640     case DW_TAG_type_unit:
6641       return "DW_TAG_type_unit";
6642     case DW_TAG_rvalue_reference_type:
6643       return "DW_TAG_rvalue_reference_type";
6644     case DW_TAG_template_alias:
6645       return "DW_TAG_template_alias";
6646     case DW_TAG_GNU_template_parameter_pack:
6647       return "DW_TAG_GNU_template_parameter_pack";
6648     case DW_TAG_GNU_formal_parameter_pack:
6649       return "DW_TAG_GNU_formal_parameter_pack";
6650     case DW_TAG_MIPS_loop:
6651       return "DW_TAG_MIPS_loop";
6652     case DW_TAG_format_label:
6653       return "DW_TAG_format_label";
6654     case DW_TAG_function_template:
6655       return "DW_TAG_function_template";
6656     case DW_TAG_class_template:
6657       return "DW_TAG_class_template";
6658     case DW_TAG_GNU_BINCL:
6659       return "DW_TAG_GNU_BINCL";
6660     case DW_TAG_GNU_EINCL:
6661       return "DW_TAG_GNU_EINCL";
6662     case DW_TAG_GNU_template_template_param:
6663       return "DW_TAG_GNU_template_template_param";
6664     default:
6665       return "DW_TAG_<unknown>";
6666     }
6667 }
6668
6669 /* Convert a DWARF attribute code into its string name.  */
6670
6671 static const char *
6672 dwarf_attr_name (unsigned int attr)
6673 {
6674   switch (attr)
6675     {
6676     case DW_AT_sibling:
6677       return "DW_AT_sibling";
6678     case DW_AT_location:
6679       return "DW_AT_location";
6680     case DW_AT_name:
6681       return "DW_AT_name";
6682     case DW_AT_ordering:
6683       return "DW_AT_ordering";
6684     case DW_AT_subscr_data:
6685       return "DW_AT_subscr_data";
6686     case DW_AT_byte_size:
6687       return "DW_AT_byte_size";
6688     case DW_AT_bit_offset:
6689       return "DW_AT_bit_offset";
6690     case DW_AT_bit_size:
6691       return "DW_AT_bit_size";
6692     case DW_AT_element_list:
6693       return "DW_AT_element_list";
6694     case DW_AT_stmt_list:
6695       return "DW_AT_stmt_list";
6696     case DW_AT_low_pc:
6697       return "DW_AT_low_pc";
6698     case DW_AT_high_pc:
6699       return "DW_AT_high_pc";
6700     case DW_AT_language:
6701       return "DW_AT_language";
6702     case DW_AT_member:
6703       return "DW_AT_member";
6704     case DW_AT_discr:
6705       return "DW_AT_discr";
6706     case DW_AT_discr_value:
6707       return "DW_AT_discr_value";
6708     case DW_AT_visibility:
6709       return "DW_AT_visibility";
6710     case DW_AT_import:
6711       return "DW_AT_import";
6712     case DW_AT_string_length:
6713       return "DW_AT_string_length";
6714     case DW_AT_common_reference:
6715       return "DW_AT_common_reference";
6716     case DW_AT_comp_dir:
6717       return "DW_AT_comp_dir";
6718     case DW_AT_const_value:
6719       return "DW_AT_const_value";
6720     case DW_AT_containing_type:
6721       return "DW_AT_containing_type";
6722     case DW_AT_default_value:
6723       return "DW_AT_default_value";
6724     case DW_AT_inline:
6725       return "DW_AT_inline";
6726     case DW_AT_is_optional:
6727       return "DW_AT_is_optional";
6728     case DW_AT_lower_bound:
6729       return "DW_AT_lower_bound";
6730     case DW_AT_producer:
6731       return "DW_AT_producer";
6732     case DW_AT_prototyped:
6733       return "DW_AT_prototyped";
6734     case DW_AT_return_addr:
6735       return "DW_AT_return_addr";
6736     case DW_AT_start_scope:
6737       return "DW_AT_start_scope";
6738     case DW_AT_bit_stride:
6739       return "DW_AT_bit_stride";
6740     case DW_AT_upper_bound:
6741       return "DW_AT_upper_bound";
6742     case DW_AT_abstract_origin:
6743       return "DW_AT_abstract_origin";
6744     case DW_AT_accessibility:
6745       return "DW_AT_accessibility";
6746     case DW_AT_address_class:
6747       return "DW_AT_address_class";
6748     case DW_AT_artificial:
6749       return "DW_AT_artificial";
6750     case DW_AT_base_types:
6751       return "DW_AT_base_types";
6752     case DW_AT_calling_convention:
6753       return "DW_AT_calling_convention";
6754     case DW_AT_count:
6755       return "DW_AT_count";
6756     case DW_AT_data_member_location:
6757       return "DW_AT_data_member_location";
6758     case DW_AT_decl_column:
6759       return "DW_AT_decl_column";
6760     case DW_AT_decl_file:
6761       return "DW_AT_decl_file";
6762     case DW_AT_decl_line:
6763       return "DW_AT_decl_line";
6764     case DW_AT_declaration:
6765       return "DW_AT_declaration";
6766     case DW_AT_discr_list:
6767       return "DW_AT_discr_list";
6768     case DW_AT_encoding:
6769       return "DW_AT_encoding";
6770     case DW_AT_external:
6771       return "DW_AT_external";
6772     case DW_AT_explicit:
6773       return "DW_AT_explicit";
6774     case DW_AT_frame_base:
6775       return "DW_AT_frame_base";
6776     case DW_AT_friend:
6777       return "DW_AT_friend";
6778     case DW_AT_identifier_case:
6779       return "DW_AT_identifier_case";
6780     case DW_AT_macro_info:
6781       return "DW_AT_macro_info";
6782     case DW_AT_namelist_items:
6783       return "DW_AT_namelist_items";
6784     case DW_AT_priority:
6785       return "DW_AT_priority";
6786     case DW_AT_segment:
6787       return "DW_AT_segment";
6788     case DW_AT_specification:
6789       return "DW_AT_specification";
6790     case DW_AT_static_link:
6791       return "DW_AT_static_link";
6792     case DW_AT_type:
6793       return "DW_AT_type";
6794     case DW_AT_use_location:
6795       return "DW_AT_use_location";
6796     case DW_AT_variable_parameter:
6797       return "DW_AT_variable_parameter";
6798     case DW_AT_virtuality:
6799       return "DW_AT_virtuality";
6800     case DW_AT_vtable_elem_location:
6801       return "DW_AT_vtable_elem_location";
6802
6803     case DW_AT_allocated:
6804       return "DW_AT_allocated";
6805     case DW_AT_associated:
6806       return "DW_AT_associated";
6807     case DW_AT_data_location:
6808       return "DW_AT_data_location";
6809     case DW_AT_byte_stride:
6810       return "DW_AT_byte_stride";
6811     case DW_AT_entry_pc:
6812       return "DW_AT_entry_pc";
6813     case DW_AT_use_UTF8:
6814       return "DW_AT_use_UTF8";
6815     case DW_AT_extension:
6816       return "DW_AT_extension";
6817     case DW_AT_ranges:
6818       return "DW_AT_ranges";
6819     case DW_AT_trampoline:
6820       return "DW_AT_trampoline";
6821     case DW_AT_call_column:
6822       return "DW_AT_call_column";
6823     case DW_AT_call_file:
6824       return "DW_AT_call_file";
6825     case DW_AT_call_line:
6826       return "DW_AT_call_line";
6827     case DW_AT_object_pointer:
6828       return "DW_AT_object_pointer";
6829
6830     case DW_AT_signature:
6831       return "DW_AT_signature";
6832     case DW_AT_main_subprogram:
6833       return "DW_AT_main_subprogram";
6834     case DW_AT_data_bit_offset:
6835       return "DW_AT_data_bit_offset";
6836     case DW_AT_const_expr:
6837       return "DW_AT_const_expr";
6838     case DW_AT_enum_class:
6839       return "DW_AT_enum_class";
6840     case DW_AT_linkage_name:
6841       return "DW_AT_linkage_name";
6842
6843     case DW_AT_MIPS_fde:
6844       return "DW_AT_MIPS_fde";
6845     case DW_AT_MIPS_loop_begin:
6846       return "DW_AT_MIPS_loop_begin";
6847     case DW_AT_MIPS_tail_loop_begin:
6848       return "DW_AT_MIPS_tail_loop_begin";
6849     case DW_AT_MIPS_epilog_begin:
6850       return "DW_AT_MIPS_epilog_begin";
6851 #if VMS_DEBUGGING_INFO
6852     case DW_AT_HP_prologue:
6853       return "DW_AT_HP_prologue";
6854 #else
6855     case DW_AT_MIPS_loop_unroll_factor:
6856       return "DW_AT_MIPS_loop_unroll_factor";
6857 #endif
6858     case DW_AT_MIPS_software_pipeline_depth:
6859       return "DW_AT_MIPS_software_pipeline_depth";
6860     case DW_AT_MIPS_linkage_name:
6861       return "DW_AT_MIPS_linkage_name";
6862 #if VMS_DEBUGGING_INFO
6863     case DW_AT_HP_epilogue:
6864       return "DW_AT_HP_epilogue";
6865 #else
6866     case DW_AT_MIPS_stride:
6867       return "DW_AT_MIPS_stride";
6868 #endif
6869     case DW_AT_MIPS_abstract_name:
6870       return "DW_AT_MIPS_abstract_name";
6871     case DW_AT_MIPS_clone_origin:
6872       return "DW_AT_MIPS_clone_origin";
6873     case DW_AT_MIPS_has_inlines:
6874       return "DW_AT_MIPS_has_inlines";
6875
6876     case DW_AT_sf_names:
6877       return "DW_AT_sf_names";
6878     case DW_AT_src_info:
6879       return "DW_AT_src_info";
6880     case DW_AT_mac_info:
6881       return "DW_AT_mac_info";
6882     case DW_AT_src_coords:
6883       return "DW_AT_src_coords";
6884     case DW_AT_body_begin:
6885       return "DW_AT_body_begin";
6886     case DW_AT_body_end:
6887       return "DW_AT_body_end";
6888     case DW_AT_GNU_vector:
6889       return "DW_AT_GNU_vector";
6890     case DW_AT_GNU_guarded_by:
6891       return "DW_AT_GNU_guarded_by";
6892     case DW_AT_GNU_pt_guarded_by:
6893       return "DW_AT_GNU_pt_guarded_by";
6894     case DW_AT_GNU_guarded:
6895       return "DW_AT_GNU_guarded";
6896     case DW_AT_GNU_pt_guarded:
6897       return "DW_AT_GNU_pt_guarded";
6898     case DW_AT_GNU_locks_excluded:
6899       return "DW_AT_GNU_locks_excluded";
6900     case DW_AT_GNU_exclusive_locks_required:
6901       return "DW_AT_GNU_exclusive_locks_required";
6902     case DW_AT_GNU_shared_locks_required:
6903       return "DW_AT_GNU_shared_locks_required";
6904     case DW_AT_GNU_odr_signature:
6905       return "DW_AT_GNU_odr_signature";
6906     case DW_AT_GNU_template_name:
6907       return "DW_AT_GNU_template_name";
6908
6909     case DW_AT_VMS_rtnbeg_pd_address:
6910       return "DW_AT_VMS_rtnbeg_pd_address";
6911
6912     default:
6913       return "DW_AT_<unknown>";
6914     }
6915 }
6916
6917 /* Convert a DWARF value form code into its string name.  */
6918
6919 static const char *
6920 dwarf_form_name (unsigned int form)
6921 {
6922   switch (form)
6923     {
6924     case DW_FORM_addr:
6925       return "DW_FORM_addr";
6926     case DW_FORM_block2:
6927       return "DW_FORM_block2";
6928     case DW_FORM_block4:
6929       return "DW_FORM_block4";
6930     case DW_FORM_data2:
6931       return "DW_FORM_data2";
6932     case DW_FORM_data4:
6933       return "DW_FORM_data4";
6934     case DW_FORM_data8:
6935       return "DW_FORM_data8";
6936     case DW_FORM_string:
6937       return "DW_FORM_string";
6938     case DW_FORM_block:
6939       return "DW_FORM_block";
6940     case DW_FORM_block1:
6941       return "DW_FORM_block1";
6942     case DW_FORM_data1:
6943       return "DW_FORM_data1";
6944     case DW_FORM_flag:
6945       return "DW_FORM_flag";
6946     case DW_FORM_sdata:
6947       return "DW_FORM_sdata";
6948     case DW_FORM_strp:
6949       return "DW_FORM_strp";
6950     case DW_FORM_udata:
6951       return "DW_FORM_udata";
6952     case DW_FORM_ref_addr:
6953       return "DW_FORM_ref_addr";
6954     case DW_FORM_ref1:
6955       return "DW_FORM_ref1";
6956     case DW_FORM_ref2:
6957       return "DW_FORM_ref2";
6958     case DW_FORM_ref4:
6959       return "DW_FORM_ref4";
6960     case DW_FORM_ref8:
6961       return "DW_FORM_ref8";
6962     case DW_FORM_ref_udata:
6963       return "DW_FORM_ref_udata";
6964     case DW_FORM_indirect:
6965       return "DW_FORM_indirect";
6966     case DW_FORM_sec_offset:
6967       return "DW_FORM_sec_offset";
6968     case DW_FORM_exprloc:
6969       return "DW_FORM_exprloc";
6970     case DW_FORM_flag_present:
6971       return "DW_FORM_flag_present";
6972     case DW_FORM_ref_sig8:
6973       return "DW_FORM_ref_sig8";
6974     default:
6975       return "DW_FORM_<unknown>";
6976     }
6977 }
6978 \f
6979 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6980    instance of an inlined instance of a decl which is local to an inline
6981    function, so we have to trace all of the way back through the origin chain
6982    to find out what sort of node actually served as the original seed for the
6983    given block.  */
6984
6985 static tree
6986 decl_ultimate_origin (const_tree decl)
6987 {
6988   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6989     return NULL_TREE;
6990
6991   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6992      nodes in the function to point to themselves; ignore that if
6993      we're trying to output the abstract instance of this function.  */
6994   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6995     return NULL_TREE;
6996
6997   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6998      most distant ancestor, this should never happen.  */
6999   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7000
7001   return DECL_ABSTRACT_ORIGIN (decl);
7002 }
7003
7004 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7005    of a virtual function may refer to a base class, so we check the 'this'
7006    parameter.  */
7007
7008 static tree
7009 decl_class_context (tree decl)
7010 {
7011   tree context = NULL_TREE;
7012
7013   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7014     context = DECL_CONTEXT (decl);
7015   else
7016     context = TYPE_MAIN_VARIANT
7017       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7018
7019   if (context && !TYPE_P (context))
7020     context = NULL_TREE;
7021
7022   return context;
7023 }
7024 \f
7025 /* Add an attribute/value pair to a DIE.  */
7026
7027 static inline void
7028 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7029 {
7030   /* Maybe this should be an assert?  */
7031   if (die == NULL)
7032     return;
7033
7034   if (die->die_attr == NULL)
7035     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7036   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7037 }
7038
7039 static inline enum dw_val_class
7040 AT_class (dw_attr_ref a)
7041 {
7042   return a->dw_attr_val.val_class;
7043 }
7044
7045 /* Add a flag value attribute to a DIE.  */
7046
7047 static inline void
7048 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7049 {
7050   dw_attr_node attr;
7051
7052   attr.dw_attr = attr_kind;
7053   attr.dw_attr_val.val_class = dw_val_class_flag;
7054   attr.dw_attr_val.v.val_flag = flag;
7055   add_dwarf_attr (die, &attr);
7056 }
7057
7058 static inline unsigned
7059 AT_flag (dw_attr_ref a)
7060 {
7061   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7062   return a->dw_attr_val.v.val_flag;
7063 }
7064
7065 /* Add a signed integer attribute value to a DIE.  */
7066
7067 static inline void
7068 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7069 {
7070   dw_attr_node attr;
7071
7072   attr.dw_attr = attr_kind;
7073   attr.dw_attr_val.val_class = dw_val_class_const;
7074   attr.dw_attr_val.v.val_int = int_val;
7075   add_dwarf_attr (die, &attr);
7076 }
7077
7078 static inline HOST_WIDE_INT
7079 AT_int (dw_attr_ref a)
7080 {
7081   gcc_assert (a && AT_class (a) == dw_val_class_const);
7082   return a->dw_attr_val.v.val_int;
7083 }
7084
7085 /* Add an unsigned integer attribute value to a DIE.  */
7086
7087 static inline void
7088 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7089                  unsigned HOST_WIDE_INT unsigned_val)
7090 {
7091   dw_attr_node attr;
7092
7093   attr.dw_attr = attr_kind;
7094   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7095   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7096   add_dwarf_attr (die, &attr);
7097 }
7098
7099 static inline unsigned HOST_WIDE_INT
7100 AT_unsigned (dw_attr_ref a)
7101 {
7102   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7103   return a->dw_attr_val.v.val_unsigned;
7104 }
7105
7106 /* Add an unsigned double integer attribute value to a DIE.  */
7107
7108 static inline void
7109 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7110                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7111 {
7112   dw_attr_node attr;
7113
7114   attr.dw_attr = attr_kind;
7115   attr.dw_attr_val.val_class = dw_val_class_const_double;
7116   attr.dw_attr_val.v.val_double.high = high;
7117   attr.dw_attr_val.v.val_double.low = low;
7118   add_dwarf_attr (die, &attr);
7119 }
7120
7121 /* Add a floating point attribute value to a DIE and return it.  */
7122
7123 static inline void
7124 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7125             unsigned int length, unsigned int elt_size, unsigned char *array)
7126 {
7127   dw_attr_node attr;
7128
7129   attr.dw_attr = attr_kind;
7130   attr.dw_attr_val.val_class = dw_val_class_vec;
7131   attr.dw_attr_val.v.val_vec.length = length;
7132   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7133   attr.dw_attr_val.v.val_vec.array = array;
7134   add_dwarf_attr (die, &attr);
7135 }
7136
7137 /* Add an 8-byte data attribute value to a DIE.  */
7138
7139 static inline void
7140 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7141               unsigned char data8[8])
7142 {
7143   dw_attr_node attr;
7144
7145   attr.dw_attr = attr_kind;
7146   attr.dw_attr_val.val_class = dw_val_class_data8;
7147   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7148   add_dwarf_attr (die, &attr);
7149 }
7150
7151 /* Hash and equality functions for debug_str_hash.  */
7152
7153 static hashval_t
7154 debug_str_do_hash (const void *x)
7155 {
7156   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7157 }
7158
7159 static int
7160 debug_str_eq (const void *x1, const void *x2)
7161 {
7162   return strcmp ((((const struct indirect_string_node *)x1)->str),
7163                  (const char *)x2) == 0;
7164 }
7165
7166 /* Add STR to the indirect string hash table.  */
7167
7168 static struct indirect_string_node *
7169 find_AT_string (const char *str)
7170 {
7171   struct indirect_string_node *node;
7172   void **slot;
7173
7174   if (! debug_str_hash)
7175     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7176                                       debug_str_eq, NULL);
7177
7178   slot = htab_find_slot_with_hash (debug_str_hash, str,
7179                                    htab_hash_string (str), INSERT);
7180   if (*slot == NULL)
7181     {
7182       node = ggc_alloc_cleared_indirect_string_node ();
7183       node->str = ggc_strdup (str);
7184       *slot = node;
7185     }
7186   else
7187     node = (struct indirect_string_node *) *slot;
7188
7189   node->refcount++;
7190   return node;
7191 }
7192
7193 /* Add a string attribute value to a DIE.  */
7194
7195 static inline void
7196 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7197 {
7198   dw_attr_node attr;
7199   struct indirect_string_node *node;
7200
7201   node = find_AT_string (str);
7202
7203   attr.dw_attr = attr_kind;
7204   attr.dw_attr_val.val_class = dw_val_class_str;
7205   attr.dw_attr_val.v.val_str = node;
7206   add_dwarf_attr (die, &attr);
7207 }
7208
7209 /* Create a label for an indirect string node, ensuring it is going to
7210    be output, unless its reference count goes down to zero.  */
7211
7212 static inline void
7213 gen_label_for_indirect_string (struct indirect_string_node *node)
7214 {
7215   char label[32];
7216
7217   if (node->label)
7218     return;
7219
7220   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7221   ++dw2_string_counter;
7222   node->label = xstrdup (label);
7223 }
7224
7225 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7226    debug string STR.  */
7227
7228 static inline rtx
7229 get_debug_string_label (const char *str)
7230 {
7231   struct indirect_string_node *node = find_AT_string (str);
7232
7233   debug_str_hash_forced = true;
7234
7235   gen_label_for_indirect_string (node);
7236
7237   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7238 }
7239
7240 static inline const char *
7241 AT_string (dw_attr_ref a)
7242 {
7243   gcc_assert (a && AT_class (a) == dw_val_class_str);
7244   return a->dw_attr_val.v.val_str->str;
7245 }
7246
7247 /* Find out whether a string should be output inline in DIE
7248    or out-of-line in .debug_str section.  */
7249
7250 static enum dwarf_form
7251 AT_string_form (dw_attr_ref a)
7252 {
7253   struct indirect_string_node *node;
7254   unsigned int len;
7255
7256   gcc_assert (a && AT_class (a) == dw_val_class_str);
7257
7258   node = a->dw_attr_val.v.val_str;
7259   if (node->form)
7260     return node->form;
7261
7262   len = strlen (node->str) + 1;
7263
7264   /* If the string is shorter or equal to the size of the reference, it is
7265      always better to put it inline.  */
7266   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7267     return node->form = DW_FORM_string;
7268
7269   /* If we cannot expect the linker to merge strings in .debug_str
7270      section, only put it into .debug_str if it is worth even in this
7271      single module.  */
7272   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7273       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7274       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7275     return node->form = DW_FORM_string;
7276
7277   gen_label_for_indirect_string (node);
7278
7279   return node->form = DW_FORM_strp;
7280 }
7281
7282 /* Add a DIE reference attribute value to a DIE.  */
7283
7284 static inline void
7285 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7286 {
7287   dw_attr_node attr;
7288
7289   attr.dw_attr = attr_kind;
7290   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7291   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7292   attr.dw_attr_val.v.val_die_ref.external = 0;
7293   add_dwarf_attr (die, &attr);
7294 }
7295
7296 /* Add an AT_specification attribute to a DIE, and also make the back
7297    pointer from the specification to the definition.  */
7298
7299 static inline void
7300 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7301 {
7302   add_AT_die_ref (die, DW_AT_specification, targ_die);
7303   gcc_assert (!targ_die->die_definition);
7304   targ_die->die_definition = die;
7305 }
7306
7307 static inline dw_die_ref
7308 AT_ref (dw_attr_ref a)
7309 {
7310   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7311   return a->dw_attr_val.v.val_die_ref.die;
7312 }
7313
7314 static inline int
7315 AT_ref_external (dw_attr_ref a)
7316 {
7317   if (a && AT_class (a) == dw_val_class_die_ref)
7318     return a->dw_attr_val.v.val_die_ref.external;
7319
7320   return 0;
7321 }
7322
7323 static inline void
7324 set_AT_ref_external (dw_attr_ref a, int i)
7325 {
7326   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7327   a->dw_attr_val.v.val_die_ref.external = i;
7328 }
7329
7330 /* Add an FDE reference attribute value to a DIE.  */
7331
7332 static inline void
7333 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7334 {
7335   dw_attr_node attr;
7336
7337   attr.dw_attr = attr_kind;
7338   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7339   attr.dw_attr_val.v.val_fde_index = targ_fde;
7340   add_dwarf_attr (die, &attr);
7341 }
7342
7343 /* Add a location description attribute value to a DIE.  */
7344
7345 static inline void
7346 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7347 {
7348   dw_attr_node attr;
7349
7350   attr.dw_attr = attr_kind;
7351   attr.dw_attr_val.val_class = dw_val_class_loc;
7352   attr.dw_attr_val.v.val_loc = loc;
7353   add_dwarf_attr (die, &attr);
7354 }
7355
7356 static inline dw_loc_descr_ref
7357 AT_loc (dw_attr_ref a)
7358 {
7359   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7360   return a->dw_attr_val.v.val_loc;
7361 }
7362
7363 static inline void
7364 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7365 {
7366   dw_attr_node attr;
7367
7368   attr.dw_attr = attr_kind;
7369   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7370   attr.dw_attr_val.v.val_loc_list = loc_list;
7371   add_dwarf_attr (die, &attr);
7372   have_location_lists = true;
7373 }
7374
7375 static inline dw_loc_list_ref
7376 AT_loc_list (dw_attr_ref a)
7377 {
7378   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7379   return a->dw_attr_val.v.val_loc_list;
7380 }
7381
7382 static inline dw_loc_list_ref *
7383 AT_loc_list_ptr (dw_attr_ref a)
7384 {
7385   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7386   return &a->dw_attr_val.v.val_loc_list;
7387 }
7388
7389 /* Add an address constant attribute value to a DIE.  */
7390
7391 static inline void
7392 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7393 {
7394   dw_attr_node attr;
7395
7396   attr.dw_attr = attr_kind;
7397   attr.dw_attr_val.val_class = dw_val_class_addr;
7398   attr.dw_attr_val.v.val_addr = addr;
7399   add_dwarf_attr (die, &attr);
7400 }
7401
7402 /* Get the RTX from to an address DIE attribute.  */
7403
7404 static inline rtx
7405 AT_addr (dw_attr_ref a)
7406 {
7407   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7408   return a->dw_attr_val.v.val_addr;
7409 }
7410
7411 /* Add a file attribute value to a DIE.  */
7412
7413 static inline void
7414 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7415              struct dwarf_file_data *fd)
7416 {
7417   dw_attr_node attr;
7418
7419   attr.dw_attr = attr_kind;
7420   attr.dw_attr_val.val_class = dw_val_class_file;
7421   attr.dw_attr_val.v.val_file = fd;
7422   add_dwarf_attr (die, &attr);
7423 }
7424
7425 /* Get the dwarf_file_data from a file DIE attribute.  */
7426
7427 static inline struct dwarf_file_data *
7428 AT_file (dw_attr_ref a)
7429 {
7430   gcc_assert (a && AT_class (a) == dw_val_class_file);
7431   return a->dw_attr_val.v.val_file;
7432 }
7433
7434 /* Add a vms delta attribute value to a DIE.  */
7435
7436 static inline void
7437 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7438                   const char *lbl1, const char *lbl2)
7439 {
7440   dw_attr_node attr;
7441
7442   attr.dw_attr = attr_kind;
7443   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7444   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7445   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7446   add_dwarf_attr (die, &attr);
7447 }
7448
7449 /* Add a label identifier attribute value to a DIE.  */
7450
7451 static inline void
7452 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7453 {
7454   dw_attr_node attr;
7455
7456   attr.dw_attr = attr_kind;
7457   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7458   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7459   add_dwarf_attr (die, &attr);
7460 }
7461
7462 /* Add a section offset attribute value to a DIE, an offset into the
7463    debug_line section.  */
7464
7465 static inline void
7466 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7467                 const char *label)
7468 {
7469   dw_attr_node attr;
7470
7471   attr.dw_attr = attr_kind;
7472   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7473   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7474   add_dwarf_attr (die, &attr);
7475 }
7476
7477 /* Add a section offset attribute value to a DIE, an offset into the
7478    debug_macinfo section.  */
7479
7480 static inline void
7481 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7482                const char *label)
7483 {
7484   dw_attr_node attr;
7485
7486   attr.dw_attr = attr_kind;
7487   attr.dw_attr_val.val_class = dw_val_class_macptr;
7488   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7489   add_dwarf_attr (die, &attr);
7490 }
7491
7492 /* Add an offset attribute value to a DIE.  */
7493
7494 static inline void
7495 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7496                unsigned HOST_WIDE_INT offset)
7497 {
7498   dw_attr_node attr;
7499
7500   attr.dw_attr = attr_kind;
7501   attr.dw_attr_val.val_class = dw_val_class_offset;
7502   attr.dw_attr_val.v.val_offset = offset;
7503   add_dwarf_attr (die, &attr);
7504 }
7505
7506 /* Add an range_list attribute value to a DIE.  */
7507
7508 static void
7509 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7510                    long unsigned int offset)
7511 {
7512   dw_attr_node attr;
7513
7514   attr.dw_attr = attr_kind;
7515   attr.dw_attr_val.val_class = dw_val_class_range_list;
7516   attr.dw_attr_val.v.val_offset = offset;
7517   add_dwarf_attr (die, &attr);
7518 }
7519
7520 /* Return the start label of a delta attribute.  */
7521
7522 static inline const char *
7523 AT_vms_delta1 (dw_attr_ref a)
7524 {
7525   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7526   return a->dw_attr_val.v.val_vms_delta.lbl1;
7527 }
7528
7529 /* Return the end label of a delta attribute.  */
7530
7531 static inline const char *
7532 AT_vms_delta2 (dw_attr_ref a)
7533 {
7534   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7535   return a->dw_attr_val.v.val_vms_delta.lbl2;
7536 }
7537
7538 static inline const char *
7539 AT_lbl (dw_attr_ref a)
7540 {
7541   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7542                     || AT_class (a) == dw_val_class_lineptr
7543                     || AT_class (a) == dw_val_class_macptr));
7544   return a->dw_attr_val.v.val_lbl_id;
7545 }
7546
7547 /* Get the attribute of type attr_kind.  */
7548
7549 static dw_attr_ref
7550 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7551 {
7552   dw_attr_ref a;
7553   unsigned ix;
7554   dw_die_ref spec = NULL;
7555
7556   if (! die)
7557     return NULL;
7558
7559   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7560     if (a->dw_attr == attr_kind)
7561       return a;
7562     else if (a->dw_attr == DW_AT_specification
7563              || a->dw_attr == DW_AT_abstract_origin)
7564       spec = AT_ref (a);
7565
7566   if (spec)
7567     return get_AT (spec, attr_kind);
7568
7569   return NULL;
7570 }
7571
7572 /* Return the "low pc" attribute value, typically associated with a subprogram
7573    DIE.  Return null if the "low pc" attribute is either not present, or if it
7574    cannot be represented as an assembler label identifier.  */
7575
7576 static inline const char *
7577 get_AT_low_pc (dw_die_ref die)
7578 {
7579   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7580
7581   return a ? AT_lbl (a) : NULL;
7582 }
7583
7584 /* Return the "high pc" attribute value, typically associated with a subprogram
7585    DIE.  Return null if the "high pc" attribute is either not present, or if it
7586    cannot be represented as an assembler label identifier.  */
7587
7588 static inline const char *
7589 get_AT_hi_pc (dw_die_ref die)
7590 {
7591   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7592
7593   return a ? AT_lbl (a) : NULL;
7594 }
7595
7596 /* Return the value of the string attribute designated by ATTR_KIND, or
7597    NULL if it is not present.  */
7598
7599 static inline const char *
7600 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7601 {
7602   dw_attr_ref a = get_AT (die, attr_kind);
7603
7604   return a ? AT_string (a) : NULL;
7605 }
7606
7607 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7608    if it is not present.  */
7609
7610 static inline int
7611 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7612 {
7613   dw_attr_ref a = get_AT (die, attr_kind);
7614
7615   return a ? AT_flag (a) : 0;
7616 }
7617
7618 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7619    if it is not present.  */
7620
7621 static inline unsigned
7622 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7623 {
7624   dw_attr_ref a = get_AT (die, attr_kind);
7625
7626   return a ? AT_unsigned (a) : 0;
7627 }
7628
7629 static inline dw_die_ref
7630 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7631 {
7632   dw_attr_ref a = get_AT (die, attr_kind);
7633
7634   return a ? AT_ref (a) : NULL;
7635 }
7636
7637 static inline struct dwarf_file_data *
7638 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7639 {
7640   dw_attr_ref a = get_AT (die, attr_kind);
7641
7642   return a ? AT_file (a) : NULL;
7643 }
7644
7645 /* Return TRUE if the language is C++.  */
7646
7647 static inline bool
7648 is_cxx (void)
7649 {
7650   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7651
7652   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7653 }
7654
7655 /* Return TRUE if the language is Fortran.  */
7656
7657 static inline bool
7658 is_fortran (void)
7659 {
7660   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7661
7662   return (lang == DW_LANG_Fortran77
7663           || lang == DW_LANG_Fortran90
7664           || lang == DW_LANG_Fortran95);
7665 }
7666
7667 /* Return TRUE if the language is Ada.  */
7668
7669 static inline bool
7670 is_ada (void)
7671 {
7672   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7673
7674   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7675 }
7676
7677 /* Remove the specified attribute if present.  */
7678
7679 static void
7680 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7681 {
7682   dw_attr_ref a;
7683   unsigned ix;
7684
7685   if (! die)
7686     return;
7687
7688   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7689     if (a->dw_attr == attr_kind)
7690       {
7691         if (AT_class (a) == dw_val_class_str)
7692           if (a->dw_attr_val.v.val_str->refcount)
7693             a->dw_attr_val.v.val_str->refcount--;
7694
7695         /* VEC_ordered_remove should help reduce the number of abbrevs
7696            that are needed.  */
7697         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7698         return;
7699       }
7700 }
7701
7702 /* Remove CHILD from its parent.  PREV must have the property that
7703    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7704
7705 static void
7706 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7707 {
7708   gcc_assert (child->die_parent == prev->die_parent);
7709   gcc_assert (prev->die_sib == child);
7710   if (prev == child)
7711     {
7712       gcc_assert (child->die_parent->die_child == child);
7713       prev = NULL;
7714     }
7715   else
7716     prev->die_sib = child->die_sib;
7717   if (child->die_parent->die_child == child)
7718     child->die_parent->die_child = prev;
7719 }
7720
7721 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7722    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7723
7724 static void
7725 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7726 {
7727   dw_die_ref parent = old_child->die_parent;
7728
7729   gcc_assert (parent == prev->die_parent);
7730   gcc_assert (prev->die_sib == old_child);
7731
7732   new_child->die_parent = parent;
7733   if (prev == old_child)
7734     {
7735       gcc_assert (parent->die_child == old_child);
7736       new_child->die_sib = new_child;
7737     }
7738   else
7739     {
7740       prev->die_sib = new_child;
7741       new_child->die_sib = old_child->die_sib;
7742     }
7743   if (old_child->die_parent->die_child == old_child)
7744     old_child->die_parent->die_child = new_child;
7745 }
7746
7747 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7748
7749 static void
7750 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7751 {
7752   dw_die_ref c;
7753   new_parent->die_child = old_parent->die_child;
7754   old_parent->die_child = NULL;
7755   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7756 }
7757
7758 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7759    matches TAG.  */
7760
7761 static void
7762 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7763 {
7764   dw_die_ref c;
7765
7766   c = die->die_child;
7767   if (c) do {
7768     dw_die_ref prev = c;
7769     c = c->die_sib;
7770     while (c->die_tag == tag)
7771       {
7772         remove_child_with_prev (c, prev);
7773         /* Might have removed every child.  */
7774         if (c == c->die_sib)
7775           return;
7776         c = c->die_sib;
7777       }
7778   } while (c != die->die_child);
7779 }
7780
7781 /* Add a CHILD_DIE as the last child of DIE.  */
7782
7783 static void
7784 add_child_die (dw_die_ref die, dw_die_ref child_die)
7785 {
7786   /* FIXME this should probably be an assert.  */
7787   if (! die || ! child_die)
7788     return;
7789   gcc_assert (die != child_die);
7790
7791   child_die->die_parent = die;
7792   if (die->die_child)
7793     {
7794       child_die->die_sib = die->die_child->die_sib;
7795       die->die_child->die_sib = child_die;
7796     }
7797   else
7798     child_die->die_sib = child_die;
7799   die->die_child = child_die;
7800 }
7801
7802 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7803    is the specification, to the end of PARENT's list of children.
7804    This is done by removing and re-adding it.  */
7805
7806 static void
7807 splice_child_die (dw_die_ref parent, dw_die_ref child)
7808 {
7809   dw_die_ref p;
7810
7811   /* We want the declaration DIE from inside the class, not the
7812      specification DIE at toplevel.  */
7813   if (child->die_parent != parent)
7814     {
7815       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7816
7817       if (tmp)
7818         child = tmp;
7819     }
7820
7821   gcc_assert (child->die_parent == parent
7822               || (child->die_parent
7823                   == get_AT_ref (parent, DW_AT_specification)));
7824
7825   for (p = child->die_parent->die_child; ; p = p->die_sib)
7826     if (p->die_sib == child)
7827       {
7828         remove_child_with_prev (child, p);
7829         break;
7830       }
7831
7832   add_child_die (parent, child);
7833 }
7834
7835 /* Return a pointer to a newly created DIE node.  */
7836
7837 static inline dw_die_ref
7838 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7839 {
7840   dw_die_ref die = ggc_alloc_cleared_die_node ();
7841
7842   die->die_tag = tag_value;
7843
7844   if (parent_die != NULL)
7845     add_child_die (parent_die, die);
7846   else
7847     {
7848       limbo_die_node *limbo_node;
7849
7850       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7851       limbo_node->die = die;
7852       limbo_node->created_for = t;
7853       limbo_node->next = limbo_die_list;
7854       limbo_die_list = limbo_node;
7855     }
7856
7857   return die;
7858 }
7859
7860 /* Return the DIE associated with the given type specifier.  */
7861
7862 static inline dw_die_ref
7863 lookup_type_die (tree type)
7864 {
7865   return TYPE_SYMTAB_DIE (type);
7866 }
7867
7868 /* Equate a DIE to a given type specifier.  */
7869
7870 static inline void
7871 equate_type_number_to_die (tree type, dw_die_ref type_die)
7872 {
7873   TYPE_SYMTAB_DIE (type) = type_die;
7874 }
7875
7876 /* Returns a hash value for X (which really is a die_struct).  */
7877
7878 static hashval_t
7879 decl_die_table_hash (const void *x)
7880 {
7881   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7882 }
7883
7884 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7885
7886 static int
7887 decl_die_table_eq (const void *x, const void *y)
7888 {
7889   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7890 }
7891
7892 /* Return the DIE associated with a given declaration.  */
7893
7894 static inline dw_die_ref
7895 lookup_decl_die (tree decl)
7896 {
7897   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7898 }
7899
7900 /* Returns a hash value for X (which really is a var_loc_list).  */
7901
7902 static hashval_t
7903 decl_loc_table_hash (const void *x)
7904 {
7905   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7906 }
7907
7908 /* Return nonzero if decl_id of var_loc_list X is the same as
7909    UID of decl *Y.  */
7910
7911 static int
7912 decl_loc_table_eq (const void *x, const void *y)
7913 {
7914   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7915 }
7916
7917 /* Return the var_loc list associated with a given declaration.  */
7918
7919 static inline var_loc_list *
7920 lookup_decl_loc (const_tree decl)
7921 {
7922   if (!decl_loc_table)
7923     return NULL;
7924   return (var_loc_list *)
7925     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7926 }
7927
7928 /* Equate a DIE to a particular declaration.  */
7929
7930 static void
7931 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7932 {
7933   unsigned int decl_id = DECL_UID (decl);
7934   void **slot;
7935
7936   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7937   *slot = decl_die;
7938   decl_die->decl_id = decl_id;
7939 }
7940
7941 /* Return how many bits covers PIECE EXPR_LIST.  */
7942
7943 static int
7944 decl_piece_bitsize (rtx piece)
7945 {
7946   int ret = (int) GET_MODE (piece);
7947   if (ret)
7948     return ret;
7949   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7950               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7951   return INTVAL (XEXP (XEXP (piece, 0), 0));
7952 }
7953
7954 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7955
7956 static rtx *
7957 decl_piece_varloc_ptr (rtx piece)
7958 {
7959   if ((int) GET_MODE (piece))
7960     return &XEXP (piece, 0);
7961   else
7962     return &XEXP (XEXP (piece, 0), 1);
7963 }
7964
7965 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7966    Next is the chain of following piece nodes.  */
7967
7968 static rtx
7969 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7970 {
7971   if (bitsize <= (int) MAX_MACHINE_MODE)
7972     return alloc_EXPR_LIST (bitsize, loc_note, next);
7973   else
7974     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7975                                                GEN_INT (bitsize),
7976                                                loc_note), next);
7977 }
7978
7979 /* Return rtx that should be stored into loc field for
7980    LOC_NOTE and BITPOS/BITSIZE.  */
7981
7982 static rtx
7983 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7984                       HOST_WIDE_INT bitsize)
7985 {
7986   if (bitsize != -1)
7987     {
7988       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7989       if (bitpos != 0)
7990         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7991     }
7992   return loc_note;
7993 }
7994
7995 /* This function either modifies location piece list *DEST in
7996    place (if SRC and INNER is NULL), or copies location piece list
7997    *SRC to *DEST while modifying it.  Location BITPOS is modified
7998    to contain LOC_NOTE, any pieces overlapping it are removed resp.
7999    not copied and if needed some padding around it is added.
8000    When modifying in place, DEST should point to EXPR_LIST where
8001    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8002    to the start of the whole list and INNER points to the EXPR_LIST
8003    where earlier pieces cover PIECE_BITPOS bits.  */
8004
8005 static void
8006 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8007                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8008                    HOST_WIDE_INT bitsize, rtx loc_note)
8009 {
8010   int diff;
8011   bool copy = inner != NULL;
8012
8013   if (copy)
8014     {
8015       /* First copy all nodes preceeding the current bitpos.  */
8016       while (src != inner)
8017         {
8018           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8019                                    decl_piece_bitsize (*src), NULL_RTX);
8020           dest = &XEXP (*dest, 1);
8021           src = &XEXP (*src, 1);
8022         }
8023     }
8024   /* Add padding if needed.  */
8025   if (bitpos != piece_bitpos)
8026     {
8027       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8028                                copy ? NULL_RTX : *dest);
8029       dest = &XEXP (*dest, 1);
8030     }
8031   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8032     {
8033       gcc_assert (!copy);
8034       /* A piece with correct bitpos and bitsize already exist,
8035          just update the location for it and return.  */
8036       *decl_piece_varloc_ptr (*dest) = loc_note;
8037       return;
8038     }
8039   /* Add the piece that changed.  */
8040   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8041   dest = &XEXP (*dest, 1);
8042   /* Skip over pieces that overlap it.  */
8043   diff = bitpos - piece_bitpos + bitsize;
8044   if (!copy)
8045     src = dest;
8046   while (diff > 0 && *src)
8047     {
8048       rtx piece = *src;
8049       diff -= decl_piece_bitsize (piece);
8050       if (copy)
8051         src = &XEXP (piece, 1);
8052       else
8053         {
8054           *src = XEXP (piece, 1);
8055           free_EXPR_LIST_node (piece);
8056         }
8057     }
8058   /* Add padding if needed.  */
8059   if (diff < 0 && *src)
8060     {
8061       if (!copy)
8062         dest = src;
8063       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8064       dest = &XEXP (*dest, 1);
8065     }
8066   if (!copy)
8067     return;
8068   /* Finally copy all nodes following it.  */
8069   while (*src)
8070     {
8071       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8072                                decl_piece_bitsize (*src), NULL_RTX);
8073       dest = &XEXP (*dest, 1);
8074       src = &XEXP (*src, 1);
8075     }
8076 }
8077
8078 /* Add a variable location node to the linked list for DECL.  */
8079
8080 static struct var_loc_node *
8081 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8082 {
8083   unsigned int decl_id;
8084   var_loc_list *temp;
8085   void **slot;
8086   struct var_loc_node *loc = NULL;
8087   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8088
8089   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8090     {
8091       tree realdecl = DECL_DEBUG_EXPR (decl);
8092       if (realdecl && handled_component_p (realdecl))
8093         {
8094           HOST_WIDE_INT maxsize;
8095           tree innerdecl;
8096           innerdecl
8097             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8098           if (!DECL_P (innerdecl)
8099               || DECL_IGNORED_P (innerdecl)
8100               || TREE_STATIC (innerdecl)
8101               || bitsize <= 0
8102               || bitpos + bitsize > 256
8103               || bitsize != maxsize)
8104             return NULL;
8105           decl = innerdecl;
8106         }
8107     }
8108
8109   decl_id = DECL_UID (decl);
8110   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8111   if (*slot == NULL)
8112     {
8113       temp = ggc_alloc_cleared_var_loc_list ();
8114       temp->decl_id = decl_id;
8115       *slot = temp;
8116     }
8117   else
8118     temp = (var_loc_list *) *slot;
8119
8120   if (temp->last)
8121     {
8122       struct var_loc_node *last = temp->last, *unused = NULL;
8123       rtx *piece_loc = NULL, last_loc_note;
8124       int piece_bitpos = 0;
8125       if (last->next)
8126         {
8127           last = last->next;
8128           gcc_assert (last->next == NULL);
8129         }
8130       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8131         {
8132           piece_loc = &last->loc;
8133           do
8134             {
8135               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8136               if (piece_bitpos + cur_bitsize > bitpos)
8137                 break;
8138               piece_bitpos += cur_bitsize;
8139               piece_loc = &XEXP (*piece_loc, 1);
8140             }
8141           while (*piece_loc);
8142         }
8143       /* TEMP->LAST here is either pointer to the last but one or
8144          last element in the chained list, LAST is pointer to the
8145          last element.  */
8146       if (label && strcmp (last->label, label) == 0)
8147         {
8148           /* For SRA optimized variables if there weren't any real
8149              insns since last note, just modify the last node.  */
8150           if (piece_loc != NULL)
8151             {
8152               adjust_piece_list (piece_loc, NULL, NULL,
8153                                  bitpos, piece_bitpos, bitsize, loc_note);
8154               return NULL;
8155             }
8156           /* If the last note doesn't cover any instructions, remove it.  */
8157           if (temp->last != last)
8158             {
8159               temp->last->next = NULL;
8160               unused = last;
8161               last = temp->last;
8162               gcc_assert (strcmp (last->label, label) != 0);
8163             }
8164           else
8165             {
8166               gcc_assert (temp->first == temp->last);
8167               memset (temp->last, '\0', sizeof (*temp->last));
8168               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8169               return temp->last;
8170             }
8171         }
8172       if (bitsize == -1 && NOTE_P (last->loc))
8173         last_loc_note = last->loc;
8174       else if (piece_loc != NULL
8175                && *piece_loc != NULL_RTX
8176                && piece_bitpos == bitpos
8177                && decl_piece_bitsize (*piece_loc) == bitsize)
8178         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8179       else
8180         last_loc_note = NULL_RTX;
8181       /* If the current location is the same as the end of the list,
8182          and either both or neither of the locations is uninitialized,
8183          we have nothing to do.  */
8184       if (last_loc_note == NULL_RTX
8185           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8186                             NOTE_VAR_LOCATION_LOC (loc_note)))
8187           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8188                != NOTE_VAR_LOCATION_STATUS (loc_note))
8189               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8190                    == VAR_INIT_STATUS_UNINITIALIZED)
8191                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8192                       == VAR_INIT_STATUS_UNINITIALIZED))))
8193         {
8194           /* Add LOC to the end of list and update LAST.  If the last
8195              element of the list has been removed above, reuse its
8196              memory for the new node, otherwise allocate a new one.  */
8197           if (unused)
8198             {
8199               loc = unused;
8200               memset (loc, '\0', sizeof (*loc));
8201             }
8202           else
8203             loc = ggc_alloc_cleared_var_loc_node ();
8204           if (bitsize == -1 || piece_loc == NULL)
8205             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8206           else
8207             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8208                                bitpos, piece_bitpos, bitsize, loc_note);
8209           last->next = loc;
8210           /* Ensure TEMP->LAST will point either to the new last but one
8211              element of the chain, or to the last element in it.  */
8212           if (last != temp->last)
8213             temp->last = last;
8214         }
8215       else if (unused)
8216         ggc_free (unused);
8217     }
8218   else
8219     {
8220       loc = ggc_alloc_cleared_var_loc_node ();
8221       temp->first = loc;
8222       temp->last = loc;
8223       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8224     }
8225   return loc;
8226 }
8227 \f
8228 /* Keep track of the number of spaces used to indent the
8229    output of the debugging routines that print the structure of
8230    the DIE internal representation.  */
8231 static int print_indent;
8232
8233 /* Indent the line the number of spaces given by print_indent.  */
8234
8235 static inline void
8236 print_spaces (FILE *outfile)
8237 {
8238   fprintf (outfile, "%*s", print_indent, "");
8239 }
8240
8241 /* Print a type signature in hex.  */
8242
8243 static inline void
8244 print_signature (FILE *outfile, char *sig)
8245 {
8246   int i;
8247
8248   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8249     fprintf (outfile, "%02x", sig[i] & 0xff);
8250 }
8251
8252 /* Print the information associated with a given DIE, and its children.
8253    This routine is a debugging aid only.  */
8254
8255 static void
8256 print_die (dw_die_ref die, FILE *outfile)
8257 {
8258   dw_attr_ref a;
8259   dw_die_ref c;
8260   unsigned ix;
8261
8262   print_spaces (outfile);
8263   fprintf (outfile, "DIE %4ld: %s\n",
8264            die->die_offset, dwarf_tag_name (die->die_tag));
8265   print_spaces (outfile);
8266   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8267   fprintf (outfile, " offset: %ld\n", die->die_offset);
8268   if (dwarf_version >= 4 && die->die_id.die_type_node)
8269     {
8270       print_spaces (outfile);
8271       fprintf (outfile, "  signature: ");
8272       print_signature (outfile, die->die_id.die_type_node->signature);
8273       fprintf (outfile, "\n");
8274     }
8275
8276   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8277     {
8278       print_spaces (outfile);
8279       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8280
8281       switch (AT_class (a))
8282         {
8283         case dw_val_class_addr:
8284           fprintf (outfile, "address");
8285           break;
8286         case dw_val_class_offset:
8287           fprintf (outfile, "offset");
8288           break;
8289         case dw_val_class_loc:
8290           fprintf (outfile, "location descriptor");
8291           break;
8292         case dw_val_class_loc_list:
8293           fprintf (outfile, "location list -> label:%s",
8294                    AT_loc_list (a)->ll_symbol);
8295           break;
8296         case dw_val_class_range_list:
8297           fprintf (outfile, "range list");
8298           break;
8299         case dw_val_class_const:
8300           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8301           break;
8302         case dw_val_class_unsigned_const:
8303           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8304           break;
8305         case dw_val_class_const_double:
8306           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8307                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8308                    a->dw_attr_val.v.val_double.high,
8309                    a->dw_attr_val.v.val_double.low);
8310           break;
8311         case dw_val_class_vec:
8312           fprintf (outfile, "floating-point or vector constant");
8313           break;
8314         case dw_val_class_flag:
8315           fprintf (outfile, "%u", AT_flag (a));
8316           break;
8317         case dw_val_class_die_ref:
8318           if (AT_ref (a) != NULL)
8319             {
8320               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8321                 {
8322                   fprintf (outfile, "die -> signature: ");
8323                   print_signature (outfile,
8324                                    AT_ref (a)->die_id.die_type_node->signature);
8325                 }
8326               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8327                 fprintf (outfile, "die -> label: %s",
8328                          AT_ref (a)->die_id.die_symbol);
8329               else
8330                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8331             }
8332           else
8333             fprintf (outfile, "die -> <null>");
8334           break;
8335         case dw_val_class_vms_delta:
8336           fprintf (outfile, "delta: @slotcount(%s-%s)",
8337                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8338           break;
8339         case dw_val_class_lbl_id:
8340         case dw_val_class_lineptr:
8341         case dw_val_class_macptr:
8342           fprintf (outfile, "label: %s", AT_lbl (a));
8343           break;
8344         case dw_val_class_str:
8345           if (AT_string (a) != NULL)
8346             fprintf (outfile, "\"%s\"", AT_string (a));
8347           else
8348             fprintf (outfile, "<null>");
8349           break;
8350         case dw_val_class_file:
8351           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8352                    AT_file (a)->emitted_number);
8353           break;
8354         case dw_val_class_data8:
8355           {
8356             int i;
8357
8358             for (i = 0; i < 8; i++)
8359               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8360             break;
8361           }
8362         default:
8363           break;
8364         }
8365
8366       fprintf (outfile, "\n");
8367     }
8368
8369   if (die->die_child != NULL)
8370     {
8371       print_indent += 4;
8372       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8373       print_indent -= 4;
8374     }
8375   if (print_indent == 0)
8376     fprintf (outfile, "\n");
8377 }
8378
8379 /* Print the contents of the source code line number correspondence table.
8380    This routine is a debugging aid only.  */
8381
8382 static void
8383 print_dwarf_line_table (FILE *outfile)
8384 {
8385   unsigned i;
8386   dw_line_info_ref line_info;
8387
8388   fprintf (outfile, "\n\nDWARF source line information\n");
8389   for (i = 1; i < line_info_table_in_use; i++)
8390     {
8391       line_info = &line_info_table[i];
8392       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8393                line_info->dw_file_num,
8394                line_info->dw_line_num);
8395     }
8396
8397   fprintf (outfile, "\n\n");
8398 }
8399
8400 /* Print the information collected for a given DIE.  */
8401
8402 DEBUG_FUNCTION void
8403 debug_dwarf_die (dw_die_ref die)
8404 {
8405   print_die (die, stderr);
8406 }
8407
8408 /* Print all DWARF information collected for the compilation unit.
8409    This routine is a debugging aid only.  */
8410
8411 DEBUG_FUNCTION void
8412 debug_dwarf (void)
8413 {
8414   print_indent = 0;
8415   print_die (comp_unit_die, stderr);
8416   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8417     print_dwarf_line_table (stderr);
8418 }
8419 \f
8420 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8421    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8422    DIE that marks the start of the DIEs for this include file.  */
8423
8424 static dw_die_ref
8425 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8426 {
8427   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8428   dw_die_ref new_unit = gen_compile_unit_die (filename);
8429
8430   new_unit->die_sib = old_unit;
8431   return new_unit;
8432 }
8433
8434 /* Close an include-file CU and reopen the enclosing one.  */
8435
8436 static dw_die_ref
8437 pop_compile_unit (dw_die_ref old_unit)
8438 {
8439   dw_die_ref new_unit = old_unit->die_sib;
8440
8441   old_unit->die_sib = NULL;
8442   return new_unit;
8443 }
8444
8445 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8446 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8447
8448 /* Calculate the checksum of a location expression.  */
8449
8450 static inline void
8451 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8452 {
8453   int tem;
8454
8455   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8456   CHECKSUM (tem);
8457   CHECKSUM (loc->dw_loc_oprnd1);
8458   CHECKSUM (loc->dw_loc_oprnd2);
8459 }
8460
8461 /* Calculate the checksum of an attribute.  */
8462
8463 static void
8464 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8465 {
8466   dw_loc_descr_ref loc;
8467   rtx r;
8468
8469   CHECKSUM (at->dw_attr);
8470
8471   /* We don't care that this was compiled with a different compiler
8472      snapshot; if the output is the same, that's what matters.  */
8473   if (at->dw_attr == DW_AT_producer)
8474     return;
8475
8476   switch (AT_class (at))
8477     {
8478     case dw_val_class_const:
8479       CHECKSUM (at->dw_attr_val.v.val_int);
8480       break;
8481     case dw_val_class_unsigned_const:
8482       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8483       break;
8484     case dw_val_class_const_double:
8485       CHECKSUM (at->dw_attr_val.v.val_double);
8486       break;
8487     case dw_val_class_vec:
8488       CHECKSUM (at->dw_attr_val.v.val_vec);
8489       break;
8490     case dw_val_class_flag:
8491       CHECKSUM (at->dw_attr_val.v.val_flag);
8492       break;
8493     case dw_val_class_str:
8494       CHECKSUM_STRING (AT_string (at));
8495       break;
8496
8497     case dw_val_class_addr:
8498       r = AT_addr (at);
8499       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8500       CHECKSUM_STRING (XSTR (r, 0));
8501       break;
8502
8503     case dw_val_class_offset:
8504       CHECKSUM (at->dw_attr_val.v.val_offset);
8505       break;
8506
8507     case dw_val_class_loc:
8508       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8509         loc_checksum (loc, ctx);
8510       break;
8511
8512     case dw_val_class_die_ref:
8513       die_checksum (AT_ref (at), ctx, mark);
8514       break;
8515
8516     case dw_val_class_fde_ref:
8517     case dw_val_class_vms_delta:
8518     case dw_val_class_lbl_id:
8519     case dw_val_class_lineptr:
8520     case dw_val_class_macptr:
8521       break;
8522
8523     case dw_val_class_file:
8524       CHECKSUM_STRING (AT_file (at)->filename);
8525       break;
8526
8527     case dw_val_class_data8:
8528       CHECKSUM (at->dw_attr_val.v.val_data8);
8529       break;
8530
8531     default:
8532       break;
8533     }
8534 }
8535
8536 /* Calculate the checksum of a DIE.  */
8537
8538 static void
8539 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8540 {
8541   dw_die_ref c;
8542   dw_attr_ref a;
8543   unsigned ix;
8544
8545   /* To avoid infinite recursion.  */
8546   if (die->die_mark)
8547     {
8548       CHECKSUM (die->die_mark);
8549       return;
8550     }
8551   die->die_mark = ++(*mark);
8552
8553   CHECKSUM (die->die_tag);
8554
8555   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8556     attr_checksum (a, ctx, mark);
8557
8558   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8559 }
8560
8561 #undef CHECKSUM
8562 #undef CHECKSUM_STRING
8563
8564 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8565 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8566 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8567 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8568 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8569 #define CHECKSUM_ATTR(FOO) \
8570   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8571
8572 /* Calculate the checksum of a number in signed LEB128 format.  */
8573
8574 static void
8575 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8576 {
8577   unsigned char byte;
8578   bool more;
8579
8580   while (1)
8581     {
8582       byte = (value & 0x7f);
8583       value >>= 7;
8584       more = !((value == 0 && (byte & 0x40) == 0)
8585                 || (value == -1 && (byte & 0x40) != 0));
8586       if (more)
8587         byte |= 0x80;
8588       CHECKSUM (byte);
8589       if (!more)
8590         break;
8591     }
8592 }
8593
8594 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8595
8596 static void
8597 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8598 {
8599   while (1)
8600     {
8601       unsigned char byte = (value & 0x7f);
8602       value >>= 7;
8603       if (value != 0)
8604         /* More bytes to follow.  */
8605         byte |= 0x80;
8606       CHECKSUM (byte);
8607       if (value == 0)
8608         break;
8609     }
8610 }
8611
8612 /* Checksum the context of the DIE.  This adds the names of any
8613    surrounding namespaces or structures to the checksum.  */
8614
8615 static void
8616 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8617 {
8618   const char *name;
8619   dw_die_ref spec;
8620   int tag = die->die_tag;
8621
8622   if (tag != DW_TAG_namespace
8623       && tag != DW_TAG_structure_type
8624       && tag != DW_TAG_class_type)
8625     return;
8626
8627   name = get_AT_string (die, DW_AT_name);
8628
8629   spec = get_AT_ref (die, DW_AT_specification);
8630   if (spec != NULL)
8631     die = spec;
8632
8633   if (die->die_parent != NULL)
8634     checksum_die_context (die->die_parent, ctx);
8635
8636   CHECKSUM_ULEB128 ('C');
8637   CHECKSUM_ULEB128 (tag);
8638   if (name != NULL)
8639     CHECKSUM_STRING (name);
8640 }
8641
8642 /* Calculate the checksum of a location expression.  */
8643
8644 static inline void
8645 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8646 {
8647   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8648      were emitted as a DW_FORM_sdata instead of a location expression.  */
8649   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8650     {
8651       CHECKSUM_ULEB128 (DW_FORM_sdata);
8652       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8653       return;
8654     }
8655
8656   /* Otherwise, just checksum the raw location expression.  */
8657   while (loc != NULL)
8658     {
8659       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8660       CHECKSUM (loc->dw_loc_oprnd1);
8661       CHECKSUM (loc->dw_loc_oprnd2);
8662       loc = loc->dw_loc_next;
8663     }
8664 }
8665
8666 /* Calculate the checksum of an attribute.  */
8667
8668 static void
8669 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8670                        struct md5_ctx *ctx, int *mark)
8671 {
8672   dw_loc_descr_ref loc;
8673   rtx r;
8674
8675   if (AT_class (at) == dw_val_class_die_ref)
8676     {
8677       dw_die_ref target_die = AT_ref (at);
8678
8679       /* For pointer and reference types, we checksum only the (qualified)
8680          name of the target type (if there is a name).  For friend entries,
8681          we checksum only the (qualified) name of the target type or function.
8682          This allows the checksum to remain the same whether the target type
8683          is complete or not.  */
8684       if ((at->dw_attr == DW_AT_type
8685            && (tag == DW_TAG_pointer_type
8686                || tag == DW_TAG_reference_type
8687                || tag == DW_TAG_rvalue_reference_type
8688                || tag == DW_TAG_ptr_to_member_type))
8689           || (at->dw_attr == DW_AT_friend
8690               && tag == DW_TAG_friend))
8691         {
8692           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8693
8694           if (name_attr != NULL)
8695             {
8696               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8697
8698               if (decl == NULL)
8699                 decl = target_die;
8700               CHECKSUM_ULEB128 ('N');
8701               CHECKSUM_ULEB128 (at->dw_attr);
8702               if (decl->die_parent != NULL)
8703                 checksum_die_context (decl->die_parent, ctx);
8704               CHECKSUM_ULEB128 ('E');
8705               CHECKSUM_STRING (AT_string (name_attr));
8706               return;
8707             }
8708         }
8709
8710       /* For all other references to another DIE, we check to see if the
8711          target DIE has already been visited.  If it has, we emit a
8712          backward reference; if not, we descend recursively.  */
8713       if (target_die->die_mark > 0)
8714         {
8715           CHECKSUM_ULEB128 ('R');
8716           CHECKSUM_ULEB128 (at->dw_attr);
8717           CHECKSUM_ULEB128 (target_die->die_mark);
8718         }
8719       else
8720         {
8721           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8722
8723           if (decl == NULL)
8724             decl = target_die;
8725           target_die->die_mark = ++(*mark);
8726           CHECKSUM_ULEB128 ('T');
8727           CHECKSUM_ULEB128 (at->dw_attr);
8728           if (decl->die_parent != NULL)
8729             checksum_die_context (decl->die_parent, ctx);
8730           die_checksum_ordered (target_die, ctx, mark);
8731         }
8732       return;
8733     }
8734
8735   CHECKSUM_ULEB128 ('A');
8736   CHECKSUM_ULEB128 (at->dw_attr);
8737
8738   switch (AT_class (at))
8739     {
8740     case dw_val_class_const:
8741       CHECKSUM_ULEB128 (DW_FORM_sdata);
8742       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8743       break;
8744
8745     case dw_val_class_unsigned_const:
8746       CHECKSUM_ULEB128 (DW_FORM_sdata);
8747       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8748       break;
8749
8750     case dw_val_class_const_double:
8751       CHECKSUM_ULEB128 (DW_FORM_block);
8752       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8753       CHECKSUM (at->dw_attr_val.v.val_double);
8754       break;
8755
8756     case dw_val_class_vec:
8757       CHECKSUM_ULEB128 (DW_FORM_block);
8758       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8759       CHECKSUM (at->dw_attr_val.v.val_vec);
8760       break;
8761
8762     case dw_val_class_flag:
8763       CHECKSUM_ULEB128 (DW_FORM_flag);
8764       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8765       break;
8766
8767     case dw_val_class_str:
8768       CHECKSUM_ULEB128 (DW_FORM_string);
8769       CHECKSUM_STRING (AT_string (at));
8770       break;
8771
8772     case dw_val_class_addr:
8773       r = AT_addr (at);
8774       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8775       CHECKSUM_ULEB128 (DW_FORM_string);
8776       CHECKSUM_STRING (XSTR (r, 0));
8777       break;
8778
8779     case dw_val_class_offset:
8780       CHECKSUM_ULEB128 (DW_FORM_sdata);
8781       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8782       break;
8783
8784     case dw_val_class_loc:
8785       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8786         loc_checksum_ordered (loc, ctx);
8787       break;
8788
8789     case dw_val_class_fde_ref:
8790     case dw_val_class_lbl_id:
8791     case dw_val_class_lineptr:
8792     case dw_val_class_macptr:
8793       break;
8794
8795     case dw_val_class_file:
8796       CHECKSUM_ULEB128 (DW_FORM_string);
8797       CHECKSUM_STRING (AT_file (at)->filename);
8798       break;
8799
8800     case dw_val_class_data8:
8801       CHECKSUM (at->dw_attr_val.v.val_data8);
8802       break;
8803
8804     default:
8805       break;
8806     }
8807 }
8808
8809 struct checksum_attributes
8810 {
8811   dw_attr_ref at_name;
8812   dw_attr_ref at_type;
8813   dw_attr_ref at_friend;
8814   dw_attr_ref at_accessibility;
8815   dw_attr_ref at_address_class;
8816   dw_attr_ref at_allocated;
8817   dw_attr_ref at_artificial;
8818   dw_attr_ref at_associated;
8819   dw_attr_ref at_binary_scale;
8820   dw_attr_ref at_bit_offset;
8821   dw_attr_ref at_bit_size;
8822   dw_attr_ref at_bit_stride;
8823   dw_attr_ref at_byte_size;
8824   dw_attr_ref at_byte_stride;
8825   dw_attr_ref at_const_value;
8826   dw_attr_ref at_containing_type;
8827   dw_attr_ref at_count;
8828   dw_attr_ref at_data_location;
8829   dw_attr_ref at_data_member_location;
8830   dw_attr_ref at_decimal_scale;
8831   dw_attr_ref at_decimal_sign;
8832   dw_attr_ref at_default_value;
8833   dw_attr_ref at_digit_count;
8834   dw_attr_ref at_discr;
8835   dw_attr_ref at_discr_list;
8836   dw_attr_ref at_discr_value;
8837   dw_attr_ref at_encoding;
8838   dw_attr_ref at_endianity;
8839   dw_attr_ref at_explicit;
8840   dw_attr_ref at_is_optional;
8841   dw_attr_ref at_location;
8842   dw_attr_ref at_lower_bound;
8843   dw_attr_ref at_mutable;
8844   dw_attr_ref at_ordering;
8845   dw_attr_ref at_picture_string;
8846   dw_attr_ref at_prototyped;
8847   dw_attr_ref at_small;
8848   dw_attr_ref at_segment;
8849   dw_attr_ref at_string_length;
8850   dw_attr_ref at_threads_scaled;
8851   dw_attr_ref at_upper_bound;
8852   dw_attr_ref at_use_location;
8853   dw_attr_ref at_use_UTF8;
8854   dw_attr_ref at_variable_parameter;
8855   dw_attr_ref at_virtuality;
8856   dw_attr_ref at_visibility;
8857   dw_attr_ref at_vtable_elem_location;
8858 };
8859
8860 /* Collect the attributes that we will want to use for the checksum.  */
8861
8862 static void
8863 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8864 {
8865   dw_attr_ref a;
8866   unsigned ix;
8867
8868   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8869     {
8870       switch (a->dw_attr)
8871         {
8872         case DW_AT_name:
8873           attrs->at_name = a;
8874           break;
8875         case DW_AT_type:
8876           attrs->at_type = a;
8877           break;
8878         case DW_AT_friend:
8879           attrs->at_friend = a;
8880           break;
8881         case DW_AT_accessibility:
8882           attrs->at_accessibility = a;
8883           break;
8884         case DW_AT_address_class:
8885           attrs->at_address_class = a;
8886           break;
8887         case DW_AT_allocated:
8888           attrs->at_allocated = a;
8889           break;
8890         case DW_AT_artificial:
8891           attrs->at_artificial = a;
8892           break;
8893         case DW_AT_associated:
8894           attrs->at_associated = a;
8895           break;
8896         case DW_AT_binary_scale:
8897           attrs->at_binary_scale = a;
8898           break;
8899         case DW_AT_bit_offset:
8900           attrs->at_bit_offset = a;
8901           break;
8902         case DW_AT_bit_size:
8903           attrs->at_bit_size = a;
8904           break;
8905         case DW_AT_bit_stride:
8906           attrs->at_bit_stride = a;
8907           break;
8908         case DW_AT_byte_size:
8909           attrs->at_byte_size = a;
8910           break;
8911         case DW_AT_byte_stride:
8912           attrs->at_byte_stride = a;
8913           break;
8914         case DW_AT_const_value:
8915           attrs->at_const_value = a;
8916           break;
8917         case DW_AT_containing_type:
8918           attrs->at_containing_type = a;
8919           break;
8920         case DW_AT_count:
8921           attrs->at_count = a;
8922           break;
8923         case DW_AT_data_location:
8924           attrs->at_data_location = a;
8925           break;
8926         case DW_AT_data_member_location:
8927           attrs->at_data_member_location = a;
8928           break;
8929         case DW_AT_decimal_scale:
8930           attrs->at_decimal_scale = a;
8931           break;
8932         case DW_AT_decimal_sign:
8933           attrs->at_decimal_sign = a;
8934           break;
8935         case DW_AT_default_value:
8936           attrs->at_default_value = a;
8937           break;
8938         case DW_AT_digit_count:
8939           attrs->at_digit_count = a;
8940           break;
8941         case DW_AT_discr:
8942           attrs->at_discr = a;
8943           break;
8944         case DW_AT_discr_list:
8945           attrs->at_discr_list = a;
8946           break;
8947         case DW_AT_discr_value:
8948           attrs->at_discr_value = a;
8949           break;
8950         case DW_AT_encoding:
8951           attrs->at_encoding = a;
8952           break;
8953         case DW_AT_endianity:
8954           attrs->at_endianity = a;
8955           break;
8956         case DW_AT_explicit:
8957           attrs->at_explicit = a;
8958           break;
8959         case DW_AT_is_optional:
8960           attrs->at_is_optional = a;
8961           break;
8962         case DW_AT_location:
8963           attrs->at_location = a;
8964           break;
8965         case DW_AT_lower_bound:
8966           attrs->at_lower_bound = a;
8967           break;
8968         case DW_AT_mutable:
8969           attrs->at_mutable = a;
8970           break;
8971         case DW_AT_ordering:
8972           attrs->at_ordering = a;
8973           break;
8974         case DW_AT_picture_string:
8975           attrs->at_picture_string = a;
8976           break;
8977         case DW_AT_prototyped:
8978           attrs->at_prototyped = a;
8979           break;
8980         case DW_AT_small:
8981           attrs->at_small = a;
8982           break;
8983         case DW_AT_segment:
8984           attrs->at_segment = a;
8985           break;
8986         case DW_AT_string_length:
8987           attrs->at_string_length = a;
8988           break;
8989         case DW_AT_threads_scaled:
8990           attrs->at_threads_scaled = a;
8991           break;
8992         case DW_AT_upper_bound:
8993           attrs->at_upper_bound = a;
8994           break;
8995         case DW_AT_use_location:
8996           attrs->at_use_location = a;
8997           break;
8998         case DW_AT_use_UTF8:
8999           attrs->at_use_UTF8 = a;
9000           break;
9001         case DW_AT_variable_parameter:
9002           attrs->at_variable_parameter = a;
9003           break;
9004         case DW_AT_virtuality:
9005           attrs->at_virtuality = a;
9006           break;
9007         case DW_AT_visibility:
9008           attrs->at_visibility = a;
9009           break;
9010         case DW_AT_vtable_elem_location:
9011           attrs->at_vtable_elem_location = a;
9012           break;
9013         default:
9014           break;
9015         }
9016     }
9017 }
9018
9019 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9020
9021 static void
9022 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9023 {
9024   dw_die_ref c;
9025   dw_die_ref decl;
9026   struct checksum_attributes attrs;
9027
9028   CHECKSUM_ULEB128 ('D');
9029   CHECKSUM_ULEB128 (die->die_tag);
9030
9031   memset (&attrs, 0, sizeof (attrs));
9032
9033   decl = get_AT_ref (die, DW_AT_specification);
9034   if (decl != NULL)
9035     collect_checksum_attributes (&attrs, decl);
9036   collect_checksum_attributes (&attrs, die);
9037
9038   CHECKSUM_ATTR (attrs.at_name);
9039   CHECKSUM_ATTR (attrs.at_accessibility);
9040   CHECKSUM_ATTR (attrs.at_address_class);
9041   CHECKSUM_ATTR (attrs.at_allocated);
9042   CHECKSUM_ATTR (attrs.at_artificial);
9043   CHECKSUM_ATTR (attrs.at_associated);
9044   CHECKSUM_ATTR (attrs.at_binary_scale);
9045   CHECKSUM_ATTR (attrs.at_bit_offset);
9046   CHECKSUM_ATTR (attrs.at_bit_size);
9047   CHECKSUM_ATTR (attrs.at_bit_stride);
9048   CHECKSUM_ATTR (attrs.at_byte_size);
9049   CHECKSUM_ATTR (attrs.at_byte_stride);
9050   CHECKSUM_ATTR (attrs.at_const_value);
9051   CHECKSUM_ATTR (attrs.at_containing_type);
9052   CHECKSUM_ATTR (attrs.at_count);
9053   CHECKSUM_ATTR (attrs.at_data_location);
9054   CHECKSUM_ATTR (attrs.at_data_member_location);
9055   CHECKSUM_ATTR (attrs.at_decimal_scale);
9056   CHECKSUM_ATTR (attrs.at_decimal_sign);
9057   CHECKSUM_ATTR (attrs.at_default_value);
9058   CHECKSUM_ATTR (attrs.at_digit_count);
9059   CHECKSUM_ATTR (attrs.at_discr);
9060   CHECKSUM_ATTR (attrs.at_discr_list);
9061   CHECKSUM_ATTR (attrs.at_discr_value);
9062   CHECKSUM_ATTR (attrs.at_encoding);
9063   CHECKSUM_ATTR (attrs.at_endianity);
9064   CHECKSUM_ATTR (attrs.at_explicit);
9065   CHECKSUM_ATTR (attrs.at_is_optional);
9066   CHECKSUM_ATTR (attrs.at_location);
9067   CHECKSUM_ATTR (attrs.at_lower_bound);
9068   CHECKSUM_ATTR (attrs.at_mutable);
9069   CHECKSUM_ATTR (attrs.at_ordering);
9070   CHECKSUM_ATTR (attrs.at_picture_string);
9071   CHECKSUM_ATTR (attrs.at_prototyped);
9072   CHECKSUM_ATTR (attrs.at_small);
9073   CHECKSUM_ATTR (attrs.at_segment);
9074   CHECKSUM_ATTR (attrs.at_string_length);
9075   CHECKSUM_ATTR (attrs.at_threads_scaled);
9076   CHECKSUM_ATTR (attrs.at_upper_bound);
9077   CHECKSUM_ATTR (attrs.at_use_location);
9078   CHECKSUM_ATTR (attrs.at_use_UTF8);
9079   CHECKSUM_ATTR (attrs.at_variable_parameter);
9080   CHECKSUM_ATTR (attrs.at_virtuality);
9081   CHECKSUM_ATTR (attrs.at_visibility);
9082   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9083   CHECKSUM_ATTR (attrs.at_type);
9084   CHECKSUM_ATTR (attrs.at_friend);
9085
9086   /* Checksum the child DIEs, except for nested types and member functions.  */
9087   c = die->die_child;
9088   if (c) do {
9089     dw_attr_ref name_attr;
9090
9091     c = c->die_sib;
9092     name_attr = get_AT (c, DW_AT_name);
9093     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9094         && name_attr != NULL)
9095       {
9096         CHECKSUM_ULEB128 ('S');
9097         CHECKSUM_ULEB128 (c->die_tag);
9098         CHECKSUM_STRING (AT_string (name_attr));
9099       }
9100     else
9101       {
9102         /* Mark this DIE so it gets processed when unmarking.  */
9103         if (c->die_mark == 0)
9104           c->die_mark = -1;
9105         die_checksum_ordered (c, ctx, mark);
9106       }
9107   } while (c != die->die_child);
9108
9109   CHECKSUM_ULEB128 (0);
9110 }
9111
9112 #undef CHECKSUM
9113 #undef CHECKSUM_STRING
9114 #undef CHECKSUM_ATTR
9115 #undef CHECKSUM_LEB128
9116 #undef CHECKSUM_ULEB128
9117
9118 /* Generate the type signature for DIE.  This is computed by generating an
9119    MD5 checksum over the DIE's tag, its relevant attributes, and its
9120    children.  Attributes that are references to other DIEs are processed
9121    by recursion, using the MARK field to prevent infinite recursion.
9122    If the DIE is nested inside a namespace or another type, we also
9123    need to include that context in the signature.  The lower 64 bits
9124    of the resulting MD5 checksum comprise the signature.  */
9125
9126 static void
9127 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9128 {
9129   int mark;
9130   const char *name;
9131   unsigned char checksum[16];
9132   struct md5_ctx ctx;
9133   dw_die_ref decl;
9134
9135   name = get_AT_string (die, DW_AT_name);
9136   decl = get_AT_ref (die, DW_AT_specification);
9137
9138   /* First, compute a signature for just the type name (and its surrounding
9139      context, if any.  This is stored in the type unit DIE for link-time
9140      ODR (one-definition rule) checking.  */
9141
9142   if (is_cxx() && name != NULL)
9143     {
9144       md5_init_ctx (&ctx);
9145
9146       /* Checksum the names of surrounding namespaces and structures.  */
9147       if (decl != NULL && decl->die_parent != NULL)
9148         checksum_die_context (decl->die_parent, &ctx);
9149
9150       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9151       md5_process_bytes (name, strlen (name) + 1, &ctx);
9152       md5_finish_ctx (&ctx, checksum);
9153
9154       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9155     }
9156
9157   /* Next, compute the complete type signature.  */
9158
9159   md5_init_ctx (&ctx);
9160   mark = 1;
9161   die->die_mark = mark;
9162
9163   /* Checksum the names of surrounding namespaces and structures.  */
9164   if (decl != NULL && decl->die_parent != NULL)
9165     checksum_die_context (decl->die_parent, &ctx);
9166
9167   /* Checksum the DIE and its children.  */
9168   die_checksum_ordered (die, &ctx, &mark);
9169   unmark_all_dies (die);
9170   md5_finish_ctx (&ctx, checksum);
9171
9172   /* Store the signature in the type node and link the type DIE and the
9173      type node together.  */
9174   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9175           DWARF_TYPE_SIGNATURE_SIZE);
9176   die->die_id.die_type_node = type_node;
9177   type_node->type_die = die;
9178
9179   /* If the DIE is a specification, link its declaration to the type node
9180      as well.  */
9181   if (decl != NULL)
9182     decl->die_id.die_type_node = type_node;
9183 }
9184
9185 /* Do the location expressions look same?  */
9186 static inline int
9187 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9188 {
9189   return loc1->dw_loc_opc == loc2->dw_loc_opc
9190          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9191          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9192 }
9193
9194 /* Do the values look the same?  */
9195 static int
9196 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9197 {
9198   dw_loc_descr_ref loc1, loc2;
9199   rtx r1, r2;
9200
9201   if (v1->val_class != v2->val_class)
9202     return 0;
9203
9204   switch (v1->val_class)
9205     {
9206     case dw_val_class_const:
9207       return v1->v.val_int == v2->v.val_int;
9208     case dw_val_class_unsigned_const:
9209       return v1->v.val_unsigned == v2->v.val_unsigned;
9210     case dw_val_class_const_double:
9211       return v1->v.val_double.high == v2->v.val_double.high
9212              && v1->v.val_double.low == v2->v.val_double.low;
9213     case dw_val_class_vec:
9214       if (v1->v.val_vec.length != v2->v.val_vec.length
9215           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9216         return 0;
9217       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9218                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9219         return 0;
9220       return 1;
9221     case dw_val_class_flag:
9222       return v1->v.val_flag == v2->v.val_flag;
9223     case dw_val_class_str:
9224       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9225
9226     case dw_val_class_addr:
9227       r1 = v1->v.val_addr;
9228       r2 = v2->v.val_addr;
9229       if (GET_CODE (r1) != GET_CODE (r2))
9230         return 0;
9231       return !rtx_equal_p (r1, r2);
9232
9233     case dw_val_class_offset:
9234       return v1->v.val_offset == v2->v.val_offset;
9235
9236     case dw_val_class_loc:
9237       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9238            loc1 && loc2;
9239            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9240         if (!same_loc_p (loc1, loc2, mark))
9241           return 0;
9242       return !loc1 && !loc2;
9243
9244     case dw_val_class_die_ref:
9245       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9246
9247     case dw_val_class_fde_ref:
9248     case dw_val_class_vms_delta:
9249     case dw_val_class_lbl_id:
9250     case dw_val_class_lineptr:
9251     case dw_val_class_macptr:
9252       return 1;
9253
9254     case dw_val_class_file:
9255       return v1->v.val_file == v2->v.val_file;
9256
9257     case dw_val_class_data8:
9258       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9259
9260     default:
9261       return 1;
9262     }
9263 }
9264
9265 /* Do the attributes look the same?  */
9266
9267 static int
9268 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9269 {
9270   if (at1->dw_attr != at2->dw_attr)
9271     return 0;
9272
9273   /* We don't care that this was compiled with a different compiler
9274      snapshot; if the output is the same, that's what matters. */
9275   if (at1->dw_attr == DW_AT_producer)
9276     return 1;
9277
9278   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9279 }
9280
9281 /* Do the dies look the same?  */
9282
9283 static int
9284 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9285 {
9286   dw_die_ref c1, c2;
9287   dw_attr_ref a1;
9288   unsigned ix;
9289
9290   /* To avoid infinite recursion.  */
9291   if (die1->die_mark)
9292     return die1->die_mark == die2->die_mark;
9293   die1->die_mark = die2->die_mark = ++(*mark);
9294
9295   if (die1->die_tag != die2->die_tag)
9296     return 0;
9297
9298   if (VEC_length (dw_attr_node, die1->die_attr)
9299       != VEC_length (dw_attr_node, die2->die_attr))
9300     return 0;
9301
9302   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
9303     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9304       return 0;
9305
9306   c1 = die1->die_child;
9307   c2 = die2->die_child;
9308   if (! c1)
9309     {
9310       if (c2)
9311         return 0;
9312     }
9313   else
9314     for (;;)
9315       {
9316         if (!same_die_p (c1, c2, mark))
9317           return 0;
9318         c1 = c1->die_sib;
9319         c2 = c2->die_sib;
9320         if (c1 == die1->die_child)
9321           {
9322             if (c2 == die2->die_child)
9323               break;
9324             else
9325               return 0;
9326           }
9327     }
9328
9329   return 1;
9330 }
9331
9332 /* Do the dies look the same?  Wrapper around same_die_p.  */
9333
9334 static int
9335 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9336 {
9337   int mark = 0;
9338   int ret = same_die_p (die1, die2, &mark);
9339
9340   unmark_all_dies (die1);
9341   unmark_all_dies (die2);
9342
9343   return ret;
9344 }
9345
9346 /* The prefix to attach to symbols on DIEs in the current comdat debug
9347    info section.  */
9348 static char *comdat_symbol_id;
9349
9350 /* The index of the current symbol within the current comdat CU.  */
9351 static unsigned int comdat_symbol_number;
9352
9353 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9354    children, and set comdat_symbol_id accordingly.  */
9355
9356 static void
9357 compute_section_prefix (dw_die_ref unit_die)
9358 {
9359   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9360   const char *base = die_name ? lbasename (die_name) : "anonymous";
9361   char *name = XALLOCAVEC (char, strlen (base) + 64);
9362   char *p;
9363   int i, mark;
9364   unsigned char checksum[16];
9365   struct md5_ctx ctx;
9366
9367   /* Compute the checksum of the DIE, then append part of it as hex digits to
9368      the name filename of the unit.  */
9369
9370   md5_init_ctx (&ctx);
9371   mark = 0;
9372   die_checksum (unit_die, &ctx, &mark);
9373   unmark_all_dies (unit_die);
9374   md5_finish_ctx (&ctx, checksum);
9375
9376   sprintf (name, "%s.", base);
9377   clean_symbol_name (name);
9378
9379   p = name + strlen (name);
9380   for (i = 0; i < 4; i++)
9381     {
9382       sprintf (p, "%.2x", checksum[i]);
9383       p += 2;
9384     }
9385
9386   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9387   comdat_symbol_number = 0;
9388 }
9389
9390 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9391
9392 static int
9393 is_type_die (dw_die_ref die)
9394 {
9395   switch (die->die_tag)
9396     {
9397     case DW_TAG_array_type:
9398     case DW_TAG_class_type:
9399     case DW_TAG_interface_type:
9400     case DW_TAG_enumeration_type:
9401     case DW_TAG_pointer_type:
9402     case DW_TAG_reference_type:
9403     case DW_TAG_rvalue_reference_type:
9404     case DW_TAG_string_type:
9405     case DW_TAG_structure_type:
9406     case DW_TAG_subroutine_type:
9407     case DW_TAG_union_type:
9408     case DW_TAG_ptr_to_member_type:
9409     case DW_TAG_set_type:
9410     case DW_TAG_subrange_type:
9411     case DW_TAG_base_type:
9412     case DW_TAG_const_type:
9413     case DW_TAG_file_type:
9414     case DW_TAG_packed_type:
9415     case DW_TAG_volatile_type:
9416     case DW_TAG_typedef:
9417       return 1;
9418     default:
9419       return 0;
9420     }
9421 }
9422
9423 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9424    Basically, we want to choose the bits that are likely to be shared between
9425    compilations (types) and leave out the bits that are specific to individual
9426    compilations (functions).  */
9427
9428 static int
9429 is_comdat_die (dw_die_ref c)
9430 {
9431   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9432      we do for stabs.  The advantage is a greater likelihood of sharing between
9433      objects that don't include headers in the same order (and therefore would
9434      put the base types in a different comdat).  jason 8/28/00 */
9435
9436   if (c->die_tag == DW_TAG_base_type)
9437     return 0;
9438
9439   if (c->die_tag == DW_TAG_pointer_type
9440       || c->die_tag == DW_TAG_reference_type
9441       || c->die_tag == DW_TAG_rvalue_reference_type
9442       || c->die_tag == DW_TAG_const_type
9443       || c->die_tag == DW_TAG_volatile_type)
9444     {
9445       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9446
9447       return t ? is_comdat_die (t) : 0;
9448     }
9449
9450   return is_type_die (c);
9451 }
9452
9453 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9454    compilation unit.  */
9455
9456 static int
9457 is_symbol_die (dw_die_ref c)
9458 {
9459   return (is_type_die (c)
9460           || is_declaration_die (c)
9461           || c->die_tag == DW_TAG_namespace
9462           || c->die_tag == DW_TAG_module);
9463 }
9464
9465 static char *
9466 gen_internal_sym (const char *prefix)
9467 {
9468   char buf[256];
9469
9470   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9471   return xstrdup (buf);
9472 }
9473
9474 /* Assign symbols to all worthy DIEs under DIE.  */
9475
9476 static void
9477 assign_symbol_names (dw_die_ref die)
9478 {
9479   dw_die_ref c;
9480
9481   if (is_symbol_die (die))
9482     {
9483       if (comdat_symbol_id)
9484         {
9485           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9486
9487           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9488                    comdat_symbol_id, comdat_symbol_number++);
9489           die->die_id.die_symbol = xstrdup (p);
9490         }
9491       else
9492         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9493     }
9494
9495   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9496 }
9497
9498 struct cu_hash_table_entry
9499 {
9500   dw_die_ref cu;
9501   unsigned min_comdat_num, max_comdat_num;
9502   struct cu_hash_table_entry *next;
9503 };
9504
9505 /* Routines to manipulate hash table of CUs.  */
9506 static hashval_t
9507 htab_cu_hash (const void *of)
9508 {
9509   const struct cu_hash_table_entry *const entry =
9510     (const struct cu_hash_table_entry *) of;
9511
9512   return htab_hash_string (entry->cu->die_id.die_symbol);
9513 }
9514
9515 static int
9516 htab_cu_eq (const void *of1, const void *of2)
9517 {
9518   const struct cu_hash_table_entry *const entry1 =
9519     (const struct cu_hash_table_entry *) of1;
9520   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9521
9522   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9523 }
9524
9525 static void
9526 htab_cu_del (void *what)
9527 {
9528   struct cu_hash_table_entry *next,
9529     *entry = (struct cu_hash_table_entry *) what;
9530
9531   while (entry)
9532     {
9533       next = entry->next;
9534       free (entry);
9535       entry = next;
9536     }
9537 }
9538
9539 /* Check whether we have already seen this CU and set up SYM_NUM
9540    accordingly.  */
9541 static int
9542 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9543 {
9544   struct cu_hash_table_entry dummy;
9545   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9546
9547   dummy.max_comdat_num = 0;
9548
9549   slot = (struct cu_hash_table_entry **)
9550     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9551         INSERT);
9552   entry = *slot;
9553
9554   for (; entry; last = entry, entry = entry->next)
9555     {
9556       if (same_die_p_wrap (cu, entry->cu))
9557         break;
9558     }
9559
9560   if (entry)
9561     {
9562       *sym_num = entry->min_comdat_num;
9563       return 1;
9564     }
9565
9566   entry = XCNEW (struct cu_hash_table_entry);
9567   entry->cu = cu;
9568   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9569   entry->next = *slot;
9570   *slot = entry;
9571
9572   return 0;
9573 }
9574
9575 /* Record SYM_NUM to record of CU in HTABLE.  */
9576 static void
9577 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9578 {
9579   struct cu_hash_table_entry **slot, *entry;
9580
9581   slot = (struct cu_hash_table_entry **)
9582     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9583         NO_INSERT);
9584   entry = *slot;
9585
9586   entry->max_comdat_num = sym_num;
9587 }
9588
9589 /* Traverse the DIE (which is always comp_unit_die), and set up
9590    additional compilation units for each of the include files we see
9591    bracketed by BINCL/EINCL.  */
9592
9593 static void
9594 break_out_includes (dw_die_ref die)
9595 {
9596   dw_die_ref c;
9597   dw_die_ref unit = NULL;
9598   limbo_die_node *node, **pnode;
9599   htab_t cu_hash_table;
9600
9601   c = die->die_child;
9602   if (c) do {
9603     dw_die_ref prev = c;
9604     c = c->die_sib;
9605     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9606            || (unit && is_comdat_die (c)))
9607       {
9608         dw_die_ref next = c->die_sib;
9609
9610         /* This DIE is for a secondary CU; remove it from the main one.  */
9611         remove_child_with_prev (c, prev);
9612
9613         if (c->die_tag == DW_TAG_GNU_BINCL)
9614           unit = push_new_compile_unit (unit, c);
9615         else if (c->die_tag == DW_TAG_GNU_EINCL)
9616           unit = pop_compile_unit (unit);
9617         else
9618           add_child_die (unit, c);
9619         c = next;
9620         if (c == die->die_child)
9621           break;
9622       }
9623   } while (c != die->die_child);
9624
9625 #if 0
9626   /* We can only use this in debugging, since the frontend doesn't check
9627      to make sure that we leave every include file we enter.  */
9628   gcc_assert (!unit);
9629 #endif
9630
9631   assign_symbol_names (die);
9632   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9633   for (node = limbo_die_list, pnode = &limbo_die_list;
9634        node;
9635        node = node->next)
9636     {
9637       int is_dupl;
9638
9639       compute_section_prefix (node->die);
9640       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9641                         &comdat_symbol_number);
9642       assign_symbol_names (node->die);
9643       if (is_dupl)
9644         *pnode = node->next;
9645       else
9646         {
9647           pnode = &node->next;
9648           record_comdat_symbol_number (node->die, cu_hash_table,
9649                 comdat_symbol_number);
9650         }
9651     }
9652   htab_delete (cu_hash_table);
9653 }
9654
9655 /* Return non-zero if this DIE is a declaration.  */
9656
9657 static int
9658 is_declaration_die (dw_die_ref die)
9659 {
9660   dw_attr_ref a;
9661   unsigned ix;
9662
9663   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9664     if (a->dw_attr == DW_AT_declaration)
9665       return 1;
9666
9667   return 0;
9668 }
9669
9670 /* Return non-zero if this DIE is nested inside a subprogram.  */
9671
9672 static int
9673 is_nested_in_subprogram (dw_die_ref die)
9674 {
9675   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9676
9677   if (decl == NULL)
9678     decl = die;
9679   return local_scope_p (decl);
9680 }
9681
9682 /* Return non-zero if this is a type DIE that should be moved to a
9683    COMDAT .debug_types section.  */
9684
9685 static int
9686 should_move_die_to_comdat (dw_die_ref die)
9687 {
9688   switch (die->die_tag)
9689     {
9690     case DW_TAG_class_type:
9691     case DW_TAG_structure_type:
9692     case DW_TAG_enumeration_type:
9693     case DW_TAG_union_type:
9694       /* Don't move declarations, inlined instances, or types nested in a
9695          subprogram.  */
9696       if (is_declaration_die (die)
9697           || get_AT (die, DW_AT_abstract_origin)
9698           || is_nested_in_subprogram (die))
9699         return 0;
9700       return 1;
9701     case DW_TAG_array_type:
9702     case DW_TAG_interface_type:
9703     case DW_TAG_pointer_type:
9704     case DW_TAG_reference_type:
9705     case DW_TAG_rvalue_reference_type:
9706     case DW_TAG_string_type:
9707     case DW_TAG_subroutine_type:
9708     case DW_TAG_ptr_to_member_type:
9709     case DW_TAG_set_type:
9710     case DW_TAG_subrange_type:
9711     case DW_TAG_base_type:
9712     case DW_TAG_const_type:
9713     case DW_TAG_file_type:
9714     case DW_TAG_packed_type:
9715     case DW_TAG_volatile_type:
9716     case DW_TAG_typedef:
9717     default:
9718       return 0;
9719     }
9720 }
9721
9722 /* Make a clone of DIE.  */
9723
9724 static dw_die_ref
9725 clone_die (dw_die_ref die)
9726 {
9727   dw_die_ref clone;
9728   dw_attr_ref a;
9729   unsigned ix;
9730
9731   clone = ggc_alloc_cleared_die_node ();
9732   clone->die_tag = die->die_tag;
9733
9734   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9735     add_dwarf_attr (clone, a);
9736
9737   return clone;
9738 }
9739
9740 /* Make a clone of the tree rooted at DIE.  */
9741
9742 static dw_die_ref
9743 clone_tree (dw_die_ref die)
9744 {
9745   dw_die_ref c;
9746   dw_die_ref clone = clone_die (die);
9747
9748   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9749
9750   return clone;
9751 }
9752
9753 /* Make a clone of DIE as a declaration.  */
9754
9755 static dw_die_ref
9756 clone_as_declaration (dw_die_ref die)
9757 {
9758   dw_die_ref clone;
9759   dw_die_ref decl;
9760   dw_attr_ref a;
9761   unsigned ix;
9762
9763   /* If the DIE is already a declaration, just clone it.  */
9764   if (is_declaration_die (die))
9765     return clone_die (die);
9766
9767   /* If the DIE is a specification, just clone its declaration DIE.  */
9768   decl = get_AT_ref (die, DW_AT_specification);
9769   if (decl != NULL)
9770     return clone_die (decl);
9771
9772   clone = ggc_alloc_cleared_die_node ();
9773   clone->die_tag = die->die_tag;
9774
9775   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9776     {
9777       /* We don't want to copy over all attributes.
9778          For example we don't want DW_AT_byte_size because otherwise we will no
9779          longer have a declaration and GDB will treat it as a definition.  */
9780
9781       switch (a->dw_attr)
9782         {
9783         case DW_AT_artificial:
9784         case DW_AT_containing_type:
9785         case DW_AT_external:
9786         case DW_AT_name:
9787         case DW_AT_type:
9788         case DW_AT_virtuality:
9789         case DW_AT_linkage_name:
9790         case DW_AT_MIPS_linkage_name:
9791           add_dwarf_attr (clone, a);
9792           break;
9793         case DW_AT_byte_size:
9794         default:
9795           break;
9796         }
9797     }
9798
9799   if (die->die_id.die_type_node)
9800     add_AT_die_ref (clone, DW_AT_signature, die);
9801
9802   add_AT_flag (clone, DW_AT_declaration, 1);
9803   return clone;
9804 }
9805
9806 /* Copy the declaration context to the new compile unit DIE.  This includes
9807    any surrounding namespace or type declarations.  If the DIE has an
9808    AT_specification attribute, it also includes attributes and children
9809    attached to the specification.  */
9810
9811 static void
9812 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9813 {
9814   dw_die_ref decl;
9815   dw_die_ref new_decl;
9816
9817   decl = get_AT_ref (die, DW_AT_specification);
9818   if (decl == NULL)
9819     decl = die;
9820   else
9821     {
9822       unsigned ix;
9823       dw_die_ref c;
9824       dw_attr_ref a;
9825
9826       /* Copy the type node pointer from the new DIE to the original
9827          declaration DIE so we can forward references later.  */
9828       decl->die_id.die_type_node = die->die_id.die_type_node;
9829
9830       remove_AT (die, DW_AT_specification);
9831
9832       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9833         {
9834           if (a->dw_attr != DW_AT_name
9835               && a->dw_attr != DW_AT_declaration
9836               && a->dw_attr != DW_AT_external)
9837             add_dwarf_attr (die, a);
9838         }
9839
9840       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9841     }
9842
9843   if (decl->die_parent != NULL
9844       && decl->die_parent->die_tag != DW_TAG_compile_unit
9845       && decl->die_parent->die_tag != DW_TAG_type_unit)
9846     {
9847       new_decl = copy_ancestor_tree (unit, decl, NULL);
9848       if (new_decl != NULL)
9849         {
9850           remove_AT (new_decl, DW_AT_signature);
9851           add_AT_specification (die, new_decl);
9852         }
9853     }
9854 }
9855
9856 /* Generate the skeleton ancestor tree for the given NODE, then clone
9857    the DIE and add the clone into the tree.  */
9858
9859 static void
9860 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9861 {
9862   if (node->new_die != NULL)
9863     return;
9864
9865   node->new_die = clone_as_declaration (node->old_die);
9866
9867   if (node->parent != NULL)
9868     {
9869       generate_skeleton_ancestor_tree (node->parent);
9870       add_child_die (node->parent->new_die, node->new_die);
9871     }
9872 }
9873
9874 /* Generate a skeleton tree of DIEs containing any declarations that are
9875    found in the original tree.  We traverse the tree looking for declaration
9876    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9877
9878 static void
9879 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9880 {
9881   skeleton_chain_node node;
9882   dw_die_ref c;
9883   dw_die_ref first;
9884   dw_die_ref prev = NULL;
9885   dw_die_ref next = NULL;
9886
9887   node.parent = parent;
9888
9889   first = c = parent->old_die->die_child;
9890   if (c)
9891     next = c->die_sib;
9892   if (c) do {
9893     if (prev == NULL || prev->die_sib == c)
9894       prev = c;
9895     c = next;
9896     next = (c == first ? NULL : c->die_sib);
9897     node.old_die = c;
9898     node.new_die = NULL;
9899     if (is_declaration_die (c))
9900       {
9901         /* Clone the existing DIE, move the original to the skeleton
9902            tree (which is in the main CU), and put the clone, with
9903            all the original's children, where the original came from.  */
9904         dw_die_ref clone = clone_die (c);
9905         move_all_children (c, clone);
9906
9907         replace_child (c, clone, prev);
9908         generate_skeleton_ancestor_tree (parent);
9909         add_child_die (parent->new_die, c);
9910         node.new_die = c;
9911         c = clone;
9912       }
9913     generate_skeleton_bottom_up (&node);
9914   } while (next != NULL);
9915 }
9916
9917 /* Wrapper function for generate_skeleton_bottom_up.  */
9918
9919 static dw_die_ref
9920 generate_skeleton (dw_die_ref die)
9921 {
9922   skeleton_chain_node node;
9923
9924   node.old_die = die;
9925   node.new_die = NULL;
9926   node.parent = NULL;
9927
9928   /* If this type definition is nested inside another type,
9929      always leave at least a declaration in its place.  */
9930   if (die->die_parent != NULL && is_type_die (die->die_parent))
9931     node.new_die = clone_as_declaration (die);
9932
9933   generate_skeleton_bottom_up (&node);
9934   return node.new_die;
9935 }
9936
9937 /* Remove the DIE from its parent, possibly replacing it with a cloned
9938    declaration.  The original DIE will be moved to a new compile unit
9939    so that existing references to it follow it to the new location.  If
9940    any of the original DIE's descendants is a declaration, we need to
9941    replace the original DIE with a skeleton tree and move the
9942    declarations back into the skeleton tree.  */
9943
9944 static dw_die_ref
9945 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9946 {
9947   dw_die_ref skeleton;
9948
9949   skeleton = generate_skeleton (child);
9950   if (skeleton == NULL)
9951     remove_child_with_prev (child, prev);
9952   else
9953     {
9954       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9955       replace_child (child, skeleton, prev);
9956     }
9957
9958   return skeleton;
9959 }
9960
9961 /* Traverse the DIE and set up additional .debug_types sections for each
9962    type worthy of being placed in a COMDAT section.  */
9963
9964 static void
9965 break_out_comdat_types (dw_die_ref die)
9966 {
9967   dw_die_ref c;
9968   dw_die_ref first;
9969   dw_die_ref prev = NULL;
9970   dw_die_ref next = NULL;
9971   dw_die_ref unit = NULL;
9972
9973   first = c = die->die_child;
9974   if (c)
9975     next = c->die_sib;
9976   if (c) do {
9977     if (prev == NULL || prev->die_sib == c)
9978       prev = c;
9979     c = next;
9980     next = (c == first ? NULL : c->die_sib);
9981     if (should_move_die_to_comdat (c))
9982       {
9983         dw_die_ref replacement;
9984         comdat_type_node_ref type_node;
9985
9986         /* Create a new type unit DIE as the root for the new tree, and
9987            add it to the list of comdat types.  */
9988         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9989         add_AT_unsigned (unit, DW_AT_language,
9990                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9991         type_node = ggc_alloc_cleared_comdat_type_node ();
9992         type_node->root_die = unit;
9993         type_node->next = comdat_type_list;
9994         comdat_type_list = type_node;
9995
9996         /* Generate the type signature.  */
9997         generate_type_signature (c, type_node);
9998
9999         /* Copy the declaration context, attributes, and children of the
10000            declaration into the new compile unit DIE.  */
10001         copy_declaration_context (unit, c);
10002
10003         /* Remove this DIE from the main CU.  */
10004         replacement = remove_child_or_replace_with_skeleton (c, prev);
10005
10006         /* Break out nested types into their own type units.  */
10007         break_out_comdat_types (c);
10008
10009         /* Add the DIE to the new compunit.  */
10010         add_child_die (unit, c);
10011
10012         if (replacement != NULL)
10013           c = replacement;
10014       }
10015     else if (c->die_tag == DW_TAG_namespace
10016              || c->die_tag == DW_TAG_class_type
10017              || c->die_tag == DW_TAG_structure_type
10018              || c->die_tag == DW_TAG_union_type)
10019       {
10020         /* Look for nested types that can be broken out.  */
10021         break_out_comdat_types (c);
10022       }
10023   } while (next != NULL);
10024 }
10025
10026 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10027
10028 struct decl_table_entry
10029 {
10030   dw_die_ref orig;
10031   dw_die_ref copy;
10032 };
10033
10034 /* Routines to manipulate hash table of copied declarations.  */
10035
10036 static hashval_t
10037 htab_decl_hash (const void *of)
10038 {
10039   const struct decl_table_entry *const entry =
10040     (const struct decl_table_entry *) of;
10041
10042   return htab_hash_pointer (entry->orig);
10043 }
10044
10045 static int
10046 htab_decl_eq (const void *of1, const void *of2)
10047 {
10048   const struct decl_table_entry *const entry1 =
10049     (const struct decl_table_entry *) of1;
10050   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10051
10052   return entry1->orig == entry2;
10053 }
10054
10055 static void
10056 htab_decl_del (void *what)
10057 {
10058   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10059
10060   free (entry);
10061 }
10062
10063 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10064    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10065    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10066    to check if the ancestor has already been copied into UNIT.  */
10067
10068 static dw_die_ref
10069 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10070 {
10071   dw_die_ref parent = die->die_parent;
10072   dw_die_ref new_parent = unit;
10073   dw_die_ref copy;
10074   void **slot = NULL;
10075   struct decl_table_entry *entry = NULL;
10076
10077   if (decl_table)
10078     {
10079       /* Check if the entry has already been copied to UNIT.  */
10080       slot = htab_find_slot_with_hash (decl_table, die,
10081                                        htab_hash_pointer (die), INSERT);
10082       if (*slot != HTAB_EMPTY_ENTRY)
10083         {
10084           entry = (struct decl_table_entry *) *slot;
10085           return entry->copy;
10086         }
10087
10088       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10089       entry = XCNEW (struct decl_table_entry);
10090       entry->orig = die;
10091       entry->copy = NULL;
10092       *slot = entry;
10093     }
10094
10095   if (parent != NULL)
10096     {
10097       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10098       if (spec != NULL)
10099         parent = spec;
10100       if (parent->die_tag != DW_TAG_compile_unit
10101           && parent->die_tag != DW_TAG_type_unit)
10102         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10103     }
10104
10105   copy = clone_as_declaration (die);
10106   add_child_die (new_parent, copy);
10107
10108   if (decl_table != NULL)
10109     {
10110       /* Record the pointer to the copy.  */
10111       entry->copy = copy;
10112     }
10113
10114   return copy;
10115 }
10116
10117 /* Walk the DIE and its children, looking for references to incomplete
10118    or trivial types that are unmarked (i.e., that are not in the current
10119    type_unit).  */
10120
10121 static void
10122 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10123 {
10124   dw_die_ref c;
10125   dw_attr_ref a;
10126   unsigned ix;
10127
10128   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10129     {
10130       if (AT_class (a) == dw_val_class_die_ref)
10131         {
10132           dw_die_ref targ = AT_ref (a);
10133           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10134           void **slot;
10135           struct decl_table_entry *entry;
10136
10137           if (targ->die_mark != 0 || type_node != NULL)
10138             continue;
10139
10140           slot = htab_find_slot_with_hash (decl_table, targ,
10141                                            htab_hash_pointer (targ), INSERT);
10142
10143           if (*slot != HTAB_EMPTY_ENTRY)
10144             {
10145               /* TARG has already been copied, so we just need to
10146                  modify the reference to point to the copy.  */
10147               entry = (struct decl_table_entry *) *slot;
10148               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10149             }
10150           else
10151             {
10152               dw_die_ref parent = unit;
10153               dw_die_ref copy = clone_tree (targ);
10154
10155               /* Make sure the cloned tree is marked as part of the
10156                  type unit.  */
10157               mark_dies (copy);
10158
10159               /* Record in DECL_TABLE that TARG has been copied.
10160                  Need to do this now, before the recursive call,
10161                  because DECL_TABLE may be expanded and SLOT
10162                  would no longer be a valid pointer.  */
10163               entry = XCNEW (struct decl_table_entry);
10164               entry->orig = targ;
10165               entry->copy = copy;
10166               *slot = entry;
10167
10168               /* If TARG has surrounding context, copy its ancestor tree
10169                  into the new type unit.  */
10170               if (targ->die_parent != NULL
10171                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10172                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10173                 parent = copy_ancestor_tree (unit, targ->die_parent,
10174                                              decl_table);
10175
10176               add_child_die (parent, copy);
10177               a->dw_attr_val.v.val_die_ref.die = copy;
10178
10179               /* Make sure the newly-copied DIE is walked.  If it was
10180                  installed in a previously-added context, it won't
10181                  get visited otherwise.  */
10182               if (parent != unit)
10183                 {
10184                   /* Find the highest point of the newly-added tree,
10185                      mark each node along the way, and walk from there.  */
10186                   parent->die_mark = 1;
10187                   while (parent->die_parent
10188                          && parent->die_parent->die_mark == 0)
10189                     {
10190                       parent = parent->die_parent;
10191                       parent->die_mark = 1;
10192                     }
10193                   copy_decls_walk (unit, parent, decl_table);
10194                 }
10195             }
10196         }
10197     }
10198
10199   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10200 }
10201
10202 /* Copy declarations for "unworthy" types into the new comdat section.
10203    Incomplete types, modified types, and certain other types aren't broken
10204    out into comdat sections of their own, so they don't have a signature,
10205    and we need to copy the declaration into the same section so that we
10206    don't have an external reference.  */
10207
10208 static void
10209 copy_decls_for_unworthy_types (dw_die_ref unit)
10210 {
10211   htab_t decl_table;
10212
10213   mark_dies (unit);
10214   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10215   copy_decls_walk (unit, unit, decl_table);
10216   htab_delete (decl_table);
10217   unmark_dies (unit);
10218 }
10219
10220 /* Traverse the DIE and add a sibling attribute if it may have the
10221    effect of speeding up access to siblings.  To save some space,
10222    avoid generating sibling attributes for DIE's without children.  */
10223
10224 static void
10225 add_sibling_attributes (dw_die_ref die)
10226 {
10227   dw_die_ref c;
10228
10229   if (! die->die_child)
10230     return;
10231
10232   if (die->die_parent && die != die->die_parent->die_child)
10233     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10234
10235   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10236 }
10237
10238 /* Output all location lists for the DIE and its children.  */
10239
10240 static void
10241 output_location_lists (dw_die_ref die)
10242 {
10243   dw_die_ref c;
10244   dw_attr_ref a;
10245   unsigned ix;
10246
10247   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10248     if (AT_class (a) == dw_val_class_loc_list)
10249       output_loc_list (AT_loc_list (a));
10250
10251   FOR_EACH_CHILD (die, c, output_location_lists (c));
10252 }
10253
10254 /* The format of each DIE (and its attribute value pairs) is encoded in an
10255    abbreviation table.  This routine builds the abbreviation table and assigns
10256    a unique abbreviation id for each abbreviation entry.  The children of each
10257    die are visited recursively.  */
10258
10259 static void
10260 build_abbrev_table (dw_die_ref die)
10261 {
10262   unsigned long abbrev_id;
10263   unsigned int n_alloc;
10264   dw_die_ref c;
10265   dw_attr_ref a;
10266   unsigned ix;
10267
10268   /* Scan the DIE references, and mark as external any that refer to
10269      DIEs from other CUs (i.e. those which are not marked).  */
10270   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10271     if (AT_class (a) == dw_val_class_die_ref
10272         && AT_ref (a)->die_mark == 0)
10273       {
10274         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10275         set_AT_ref_external (a, 1);
10276       }
10277
10278   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10279     {
10280       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10281       dw_attr_ref die_a, abbrev_a;
10282       unsigned ix;
10283       bool ok = true;
10284
10285       if (abbrev->die_tag != die->die_tag)
10286         continue;
10287       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10288         continue;
10289
10290       if (VEC_length (dw_attr_node, abbrev->die_attr)
10291           != VEC_length (dw_attr_node, die->die_attr))
10292         continue;
10293
10294       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
10295         {
10296           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10297           if ((abbrev_a->dw_attr != die_a->dw_attr)
10298               || (value_format (abbrev_a) != value_format (die_a)))
10299             {
10300               ok = false;
10301               break;
10302             }
10303         }
10304       if (ok)
10305         break;
10306     }
10307
10308   if (abbrev_id >= abbrev_die_table_in_use)
10309     {
10310       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10311         {
10312           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10313           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10314                                             n_alloc);
10315
10316           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10317                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10318           abbrev_die_table_allocated = n_alloc;
10319         }
10320
10321       ++abbrev_die_table_in_use;
10322       abbrev_die_table[abbrev_id] = die;
10323     }
10324
10325   die->die_abbrev = abbrev_id;
10326   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10327 }
10328 \f
10329 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10330
10331 static int
10332 constant_size (unsigned HOST_WIDE_INT value)
10333 {
10334   int log;
10335
10336   if (value == 0)
10337     log = 0;
10338   else
10339     log = floor_log2 (value);
10340
10341   log = log / 8;
10342   log = 1 << (floor_log2 (log) + 1);
10343
10344   return log;
10345 }
10346
10347 /* Return the size of a DIE as it is represented in the
10348    .debug_info section.  */
10349
10350 static unsigned long
10351 size_of_die (dw_die_ref die)
10352 {
10353   unsigned long size = 0;
10354   dw_attr_ref a;
10355   unsigned ix;
10356
10357   size += size_of_uleb128 (die->die_abbrev);
10358   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10359     {
10360       switch (AT_class (a))
10361         {
10362         case dw_val_class_addr:
10363           size += DWARF2_ADDR_SIZE;
10364           break;
10365         case dw_val_class_offset:
10366           size += DWARF_OFFSET_SIZE;
10367           break;
10368         case dw_val_class_loc:
10369           {
10370             unsigned long lsize = size_of_locs (AT_loc (a));
10371
10372             /* Block length.  */
10373             if (dwarf_version >= 4)
10374               size += size_of_uleb128 (lsize);
10375             else
10376               size += constant_size (lsize);
10377             size += lsize;
10378           }
10379           break;
10380         case dw_val_class_loc_list:
10381           size += DWARF_OFFSET_SIZE;
10382           break;
10383         case dw_val_class_range_list:
10384           size += DWARF_OFFSET_SIZE;
10385           break;
10386         case dw_val_class_const:
10387           size += size_of_sleb128 (AT_int (a));
10388           break;
10389         case dw_val_class_unsigned_const:
10390           size += constant_size (AT_unsigned (a));
10391           break;
10392         case dw_val_class_const_double:
10393           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10394           if (HOST_BITS_PER_WIDE_INT >= 64)
10395             size++; /* block */
10396           break;
10397         case dw_val_class_vec:
10398           size += constant_size (a->dw_attr_val.v.val_vec.length
10399                                  * a->dw_attr_val.v.val_vec.elt_size)
10400                   + a->dw_attr_val.v.val_vec.length
10401                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10402           break;
10403         case dw_val_class_flag:
10404           if (dwarf_version >= 4)
10405             /* Currently all add_AT_flag calls pass in 1 as last argument,
10406                so DW_FORM_flag_present can be used.  If that ever changes,
10407                we'll need to use DW_FORM_flag and have some optimization
10408                in build_abbrev_table that will change those to
10409                DW_FORM_flag_present if it is set to 1 in all DIEs using
10410                the same abbrev entry.  */
10411             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10412           else
10413             size += 1;
10414           break;
10415         case dw_val_class_die_ref:
10416           if (AT_ref_external (a))
10417             {
10418               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10419                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10420                  is sized by target address length, whereas in DWARF3
10421                  it's always sized as an offset.  */
10422               if (dwarf_version >= 4)
10423                 size += DWARF_TYPE_SIGNATURE_SIZE;
10424               else if (dwarf_version == 2)
10425                 size += DWARF2_ADDR_SIZE;
10426               else
10427                 size += DWARF_OFFSET_SIZE;
10428             }
10429           else
10430             size += DWARF_OFFSET_SIZE;
10431           break;
10432         case dw_val_class_fde_ref:
10433           size += DWARF_OFFSET_SIZE;
10434           break;
10435         case dw_val_class_lbl_id:
10436           size += DWARF2_ADDR_SIZE;
10437           break;
10438         case dw_val_class_lineptr:
10439         case dw_val_class_macptr:
10440           size += DWARF_OFFSET_SIZE;
10441           break;
10442         case dw_val_class_str:
10443           if (AT_string_form (a) == DW_FORM_strp)
10444             size += DWARF_OFFSET_SIZE;
10445           else
10446             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10447           break;
10448         case dw_val_class_file:
10449           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10450           break;
10451         case dw_val_class_data8:
10452           size += 8;
10453           break;
10454         case dw_val_class_vms_delta:
10455           size += DWARF_OFFSET_SIZE;
10456           break;
10457         default:
10458           gcc_unreachable ();
10459         }
10460     }
10461
10462   return size;
10463 }
10464
10465 /* Size the debugging information associated with a given DIE.  Visits the
10466    DIE's children recursively.  Updates the global variable next_die_offset, on
10467    each time through.  Uses the current value of next_die_offset to update the
10468    die_offset field in each DIE.  */
10469
10470 static void
10471 calc_die_sizes (dw_die_ref die)
10472 {
10473   dw_die_ref c;
10474
10475   die->die_offset = next_die_offset;
10476   next_die_offset += size_of_die (die);
10477
10478   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10479
10480   if (die->die_child != NULL)
10481     /* Count the null byte used to terminate sibling lists.  */
10482     next_die_offset += 1;
10483 }
10484
10485 /* Set the marks for a die and its children.  We do this so
10486    that we know whether or not a reference needs to use FORM_ref_addr; only
10487    DIEs in the same CU will be marked.  We used to clear out the offset
10488    and use that as the flag, but ran into ordering problems.  */
10489
10490 static void
10491 mark_dies (dw_die_ref die)
10492 {
10493   dw_die_ref c;
10494
10495   gcc_assert (!die->die_mark);
10496
10497   die->die_mark = 1;
10498   FOR_EACH_CHILD (die, c, mark_dies (c));
10499 }
10500
10501 /* Clear the marks for a die and its children.  */
10502
10503 static void
10504 unmark_dies (dw_die_ref die)
10505 {
10506   dw_die_ref c;
10507
10508   if (dwarf_version < 4)
10509     gcc_assert (die->die_mark);
10510
10511   die->die_mark = 0;
10512   FOR_EACH_CHILD (die, c, unmark_dies (c));
10513 }
10514
10515 /* Clear the marks for a die, its children and referred dies.  */
10516
10517 static void
10518 unmark_all_dies (dw_die_ref die)
10519 {
10520   dw_die_ref c;
10521   dw_attr_ref a;
10522   unsigned ix;
10523
10524   if (!die->die_mark)
10525     return;
10526   die->die_mark = 0;
10527
10528   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10529
10530   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10531     if (AT_class (a) == dw_val_class_die_ref)
10532       unmark_all_dies (AT_ref (a));
10533 }
10534
10535 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10536    generated for the compilation unit.  */
10537
10538 static unsigned long
10539 size_of_pubnames (VEC (pubname_entry, gc) * names)
10540 {
10541   unsigned long size;
10542   unsigned i;
10543   pubname_ref p;
10544
10545   size = DWARF_PUBNAMES_HEADER_SIZE;
10546   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10547     if (names != pubtype_table
10548         || p->die->die_offset != 0
10549         || !flag_eliminate_unused_debug_types)
10550       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10551
10552   size += DWARF_OFFSET_SIZE;
10553   return size;
10554 }
10555
10556 /* Return the size of the information in the .debug_aranges section.  */
10557
10558 static unsigned long
10559 size_of_aranges (void)
10560 {
10561   unsigned long size;
10562
10563   size = DWARF_ARANGES_HEADER_SIZE;
10564
10565   /* Count the address/length pair for this compilation unit.  */
10566   if (text_section_used)
10567     size += 2 * DWARF2_ADDR_SIZE;
10568   if (cold_text_section_used)
10569     size += 2 * DWARF2_ADDR_SIZE;
10570   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10571
10572   /* Count the two zero words used to terminated the address range table.  */
10573   size += 2 * DWARF2_ADDR_SIZE;
10574   return size;
10575 }
10576 \f
10577 /* Select the encoding of an attribute value.  */
10578
10579 static enum dwarf_form
10580 value_format (dw_attr_ref a)
10581 {
10582   switch (a->dw_attr_val.val_class)
10583     {
10584     case dw_val_class_addr:
10585       /* Only very few attributes allow DW_FORM_addr.  */
10586       switch (a->dw_attr)
10587         {
10588         case DW_AT_low_pc:
10589         case DW_AT_high_pc:
10590         case DW_AT_entry_pc:
10591         case DW_AT_trampoline:
10592           return DW_FORM_addr;
10593         default:
10594           break;
10595         }
10596       switch (DWARF2_ADDR_SIZE)
10597         {
10598         case 1:
10599           return DW_FORM_data1;
10600         case 2:
10601           return DW_FORM_data2;
10602         case 4:
10603           return DW_FORM_data4;
10604         case 8:
10605           return DW_FORM_data8;
10606         default:
10607           gcc_unreachable ();
10608         }
10609     case dw_val_class_range_list:
10610     case dw_val_class_loc_list:
10611       if (dwarf_version >= 4)
10612         return DW_FORM_sec_offset;
10613       /* FALLTHRU */
10614     case dw_val_class_vms_delta:
10615     case dw_val_class_offset:
10616       switch (DWARF_OFFSET_SIZE)
10617         {
10618         case 4:
10619           return DW_FORM_data4;
10620         case 8:
10621           return DW_FORM_data8;
10622         default:
10623           gcc_unreachable ();
10624         }
10625     case dw_val_class_loc:
10626       if (dwarf_version >= 4)
10627         return DW_FORM_exprloc;
10628       switch (constant_size (size_of_locs (AT_loc (a))))
10629         {
10630         case 1:
10631           return DW_FORM_block1;
10632         case 2:
10633           return DW_FORM_block2;
10634         default:
10635           gcc_unreachable ();
10636         }
10637     case dw_val_class_const:
10638       return DW_FORM_sdata;
10639     case dw_val_class_unsigned_const:
10640       switch (constant_size (AT_unsigned (a)))
10641         {
10642         case 1:
10643           return DW_FORM_data1;
10644         case 2:
10645           return DW_FORM_data2;
10646         case 4:
10647           return DW_FORM_data4;
10648         case 8:
10649           return DW_FORM_data8;
10650         default:
10651           gcc_unreachable ();
10652         }
10653     case dw_val_class_const_double:
10654       switch (HOST_BITS_PER_WIDE_INT)
10655         {
10656         case 8:
10657           return DW_FORM_data2;
10658         case 16:
10659           return DW_FORM_data4;
10660         case 32:
10661           return DW_FORM_data8;
10662         case 64:
10663         default:
10664           return DW_FORM_block1;
10665         }
10666     case dw_val_class_vec:
10667       switch (constant_size (a->dw_attr_val.v.val_vec.length
10668                              * a->dw_attr_val.v.val_vec.elt_size))
10669         {
10670         case 1:
10671           return DW_FORM_block1;
10672         case 2:
10673           return DW_FORM_block2;
10674         case 4:
10675           return DW_FORM_block4;
10676         default:
10677           gcc_unreachable ();
10678         }
10679     case dw_val_class_flag:
10680       if (dwarf_version >= 4)
10681         {
10682           /* Currently all add_AT_flag calls pass in 1 as last argument,
10683              so DW_FORM_flag_present can be used.  If that ever changes,
10684              we'll need to use DW_FORM_flag and have some optimization
10685              in build_abbrev_table that will change those to
10686              DW_FORM_flag_present if it is set to 1 in all DIEs using
10687              the same abbrev entry.  */
10688           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10689           return DW_FORM_flag_present;
10690         }
10691       return DW_FORM_flag;
10692     case dw_val_class_die_ref:
10693       if (AT_ref_external (a))
10694         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10695       else
10696         return DW_FORM_ref;
10697     case dw_val_class_fde_ref:
10698       return DW_FORM_data;
10699     case dw_val_class_lbl_id:
10700       return DW_FORM_addr;
10701     case dw_val_class_lineptr:
10702     case dw_val_class_macptr:
10703       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10704     case dw_val_class_str:
10705       return AT_string_form (a);
10706     case dw_val_class_file:
10707       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10708         {
10709         case 1:
10710           return DW_FORM_data1;
10711         case 2:
10712           return DW_FORM_data2;
10713         case 4:
10714           return DW_FORM_data4;
10715         default:
10716           gcc_unreachable ();
10717         }
10718
10719     case dw_val_class_data8:
10720       return DW_FORM_data8;
10721
10722     default:
10723       gcc_unreachable ();
10724     }
10725 }
10726
10727 /* Output the encoding of an attribute value.  */
10728
10729 static void
10730 output_value_format (dw_attr_ref a)
10731 {
10732   enum dwarf_form form = value_format (a);
10733
10734   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10735 }
10736
10737 /* Output the .debug_abbrev section which defines the DIE abbreviation
10738    table.  */
10739
10740 static void
10741 output_abbrev_section (void)
10742 {
10743   unsigned long abbrev_id;
10744
10745   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10746     {
10747       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10748       unsigned ix;
10749       dw_attr_ref a_attr;
10750
10751       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10752       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10753                                    dwarf_tag_name (abbrev->die_tag));
10754
10755       if (abbrev->die_child != NULL)
10756         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10757       else
10758         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10759
10760       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10761            ix++)
10762         {
10763           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10764                                        dwarf_attr_name (a_attr->dw_attr));
10765           output_value_format (a_attr);
10766         }
10767
10768       dw2_asm_output_data (1, 0, NULL);
10769       dw2_asm_output_data (1, 0, NULL);
10770     }
10771
10772   /* Terminate the table.  */
10773   dw2_asm_output_data (1, 0, NULL);
10774 }
10775
10776 /* Output a symbol we can use to refer to this DIE from another CU.  */
10777
10778 static inline void
10779 output_die_symbol (dw_die_ref die)
10780 {
10781   char *sym = die->die_id.die_symbol;
10782
10783   if (sym == 0)
10784     return;
10785
10786   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10787     /* We make these global, not weak; if the target doesn't support
10788        .linkonce, it doesn't support combining the sections, so debugging
10789        will break.  */
10790     targetm.asm_out.globalize_label (asm_out_file, sym);
10791
10792   ASM_OUTPUT_LABEL (asm_out_file, sym);
10793 }
10794
10795 /* Return a new location list, given the begin and end range, and the
10796    expression.  */
10797
10798 static inline dw_loc_list_ref
10799 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10800               const char *section)
10801 {
10802   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10803
10804   retlist->begin = begin;
10805   retlist->end = end;
10806   retlist->expr = expr;
10807   retlist->section = section;
10808
10809   return retlist;
10810 }
10811
10812 /* Generate a new internal symbol for this location list node, if it
10813    hasn't got one yet.  */
10814
10815 static inline void
10816 gen_llsym (dw_loc_list_ref list)
10817 {
10818   gcc_assert (!list->ll_symbol);
10819   list->ll_symbol = gen_internal_sym ("LLST");
10820 }
10821
10822 /* Output the location list given to us.  */
10823
10824 static void
10825 output_loc_list (dw_loc_list_ref list_head)
10826 {
10827   dw_loc_list_ref curr = list_head;
10828
10829   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10830
10831   /* Walk the location list, and output each range + expression.  */
10832   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10833     {
10834       unsigned long size;
10835       /* Don't output an entry that starts and ends at the same address.  */
10836       if (strcmp (curr->begin, curr->end) == 0)
10837         continue;
10838       if (!have_multiple_function_sections)
10839         {
10840           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10841                                 "Location list begin address (%s)",
10842                                 list_head->ll_symbol);
10843           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10844                                 "Location list end address (%s)",
10845                                 list_head->ll_symbol);
10846         }
10847       else
10848         {
10849           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10850                                "Location list begin address (%s)",
10851                                list_head->ll_symbol);
10852           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10853                                "Location list end address (%s)",
10854                                list_head->ll_symbol);
10855         }
10856       size = size_of_locs (curr->expr);
10857
10858       /* Output the block length for this list of location operations.  */
10859       gcc_assert (size <= 0xffff);
10860       dw2_asm_output_data (2, size, "%s", "Location expression size");
10861
10862       output_loc_sequence (curr->expr);
10863     }
10864
10865   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10866                        "Location list terminator begin (%s)",
10867                        list_head->ll_symbol);
10868   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10869                        "Location list terminator end (%s)",
10870                        list_head->ll_symbol);
10871 }
10872
10873 /* Output a type signature.  */
10874
10875 static inline void
10876 output_signature (const char *sig, const char *name)
10877 {
10878   int i;
10879
10880   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10881     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10882 }
10883
10884 /* Output the DIE and its attributes.  Called recursively to generate
10885    the definitions of each child DIE.  */
10886
10887 static void
10888 output_die (dw_die_ref die)
10889 {
10890   dw_attr_ref a;
10891   dw_die_ref c;
10892   unsigned long size;
10893   unsigned ix;
10894
10895   /* If someone in another CU might refer to us, set up a symbol for
10896      them to point to.  */
10897   if (dwarf_version < 4 && die->die_id.die_symbol)
10898     output_die_symbol (die);
10899
10900   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10901                                (unsigned long)die->die_offset,
10902                                dwarf_tag_name (die->die_tag));
10903
10904   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10905     {
10906       const char *name = dwarf_attr_name (a->dw_attr);
10907
10908       switch (AT_class (a))
10909         {
10910         case dw_val_class_addr:
10911           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10912           break;
10913
10914         case dw_val_class_offset:
10915           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10916                                "%s", name);
10917           break;
10918
10919         case dw_val_class_range_list:
10920           {
10921             char *p = strchr (ranges_section_label, '\0');
10922
10923             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10924                      a->dw_attr_val.v.val_offset);
10925             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10926                                    debug_ranges_section, "%s", name);
10927             *p = '\0';
10928           }
10929           break;
10930
10931         case dw_val_class_loc:
10932           size = size_of_locs (AT_loc (a));
10933
10934           /* Output the block length for this list of location operations.  */
10935           if (dwarf_version >= 4)
10936             dw2_asm_output_data_uleb128 (size, "%s", name);
10937           else
10938             dw2_asm_output_data (constant_size (size), size, "%s", name);
10939
10940           output_loc_sequence (AT_loc (a));
10941           break;
10942
10943         case dw_val_class_const:
10944           /* ??? It would be slightly more efficient to use a scheme like is
10945              used for unsigned constants below, but gdb 4.x does not sign
10946              extend.  Gdb 5.x does sign extend.  */
10947           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10948           break;
10949
10950         case dw_val_class_unsigned_const:
10951           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10952                                AT_unsigned (a), "%s", name);
10953           break;
10954
10955         case dw_val_class_const_double:
10956           {
10957             unsigned HOST_WIDE_INT first, second;
10958
10959             if (HOST_BITS_PER_WIDE_INT >= 64)
10960               dw2_asm_output_data (1,
10961                                    2 * HOST_BITS_PER_WIDE_INT
10962                                    / HOST_BITS_PER_CHAR,
10963                                    NULL);
10964
10965             if (WORDS_BIG_ENDIAN)
10966               {
10967                 first = a->dw_attr_val.v.val_double.high;
10968                 second = a->dw_attr_val.v.val_double.low;
10969               }
10970             else
10971               {
10972                 first = a->dw_attr_val.v.val_double.low;
10973                 second = a->dw_attr_val.v.val_double.high;
10974               }
10975
10976             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10977                                  first, name);
10978             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10979                                  second, NULL);
10980           }
10981           break;
10982
10983         case dw_val_class_vec:
10984           {
10985             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10986             unsigned int len = a->dw_attr_val.v.val_vec.length;
10987             unsigned int i;
10988             unsigned char *p;
10989
10990             dw2_asm_output_data (constant_size (len * elt_size),
10991                                  len * elt_size, "%s", name);
10992             if (elt_size > sizeof (HOST_WIDE_INT))
10993               {
10994                 elt_size /= 2;
10995                 len *= 2;
10996               }
10997             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10998                  i < len;
10999                  i++, p += elt_size)
11000               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11001                                    "fp or vector constant word %u", i);
11002             break;
11003           }
11004
11005         case dw_val_class_flag:
11006           if (dwarf_version >= 4)
11007             {
11008               /* Currently all add_AT_flag calls pass in 1 as last argument,
11009                  so DW_FORM_flag_present can be used.  If that ever changes,
11010                  we'll need to use DW_FORM_flag and have some optimization
11011                  in build_abbrev_table that will change those to
11012                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11013                  the same abbrev entry.  */
11014               gcc_assert (AT_flag (a) == 1);
11015               if (flag_debug_asm)
11016                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11017                          ASM_COMMENT_START, name);
11018               break;
11019             }
11020           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11021           break;
11022
11023         case dw_val_class_loc_list:
11024           {
11025             char *sym = AT_loc_list (a)->ll_symbol;
11026
11027             gcc_assert (sym);
11028             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11029                                    "%s", name);
11030           }
11031           break;
11032
11033         case dw_val_class_die_ref:
11034           if (AT_ref_external (a))
11035             {
11036               if (dwarf_version >= 4)
11037                 {
11038                   comdat_type_node_ref type_node =
11039                     AT_ref (a)->die_id.die_type_node;
11040
11041                   gcc_assert (type_node);
11042                   output_signature (type_node->signature, name);
11043                 }
11044               else
11045                 {
11046                   char *sym = AT_ref (a)->die_id.die_symbol;
11047                   int size;
11048
11049                   gcc_assert (sym);
11050                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11051                      length, whereas in DWARF3 it's always sized as an
11052                      offset.  */
11053                   if (dwarf_version == 2)
11054                     size = DWARF2_ADDR_SIZE;
11055                   else
11056                     size = DWARF_OFFSET_SIZE;
11057                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11058                                          name);
11059                 }
11060             }
11061           else
11062             {
11063               gcc_assert (AT_ref (a)->die_offset);
11064               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11065                                    "%s", name);
11066             }
11067           break;
11068
11069         case dw_val_class_fde_ref:
11070           {
11071             char l1[20];
11072
11073             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11074                                          a->dw_attr_val.v.val_fde_index * 2);
11075             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11076                                    "%s", name);
11077           }
11078           break;
11079
11080         case dw_val_class_vms_delta:
11081           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11082                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11083                                     "%s", name);
11084           break;
11085
11086         case dw_val_class_lbl_id:
11087           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11088           break;
11089
11090         case dw_val_class_lineptr:
11091           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11092                                  debug_line_section, "%s", name);
11093           break;
11094
11095         case dw_val_class_macptr:
11096           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11097                                  debug_macinfo_section, "%s", name);
11098           break;
11099
11100         case dw_val_class_str:
11101           if (AT_string_form (a) == DW_FORM_strp)
11102             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11103                                    a->dw_attr_val.v.val_str->label,
11104                                    debug_str_section,
11105                                    "%s: \"%s\"", name, AT_string (a));
11106           else
11107             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11108           break;
11109
11110         case dw_val_class_file:
11111           {
11112             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11113
11114             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11115                                  a->dw_attr_val.v.val_file->filename);
11116             break;
11117           }
11118
11119         case dw_val_class_data8:
11120           {
11121             int i;
11122
11123             for (i = 0; i < 8; i++)
11124               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11125                                    i == 0 ? "%s" : NULL, name);
11126             break;
11127           }
11128
11129         default:
11130           gcc_unreachable ();
11131         }
11132     }
11133
11134   FOR_EACH_CHILD (die, c, output_die (c));
11135
11136   /* Add null byte to terminate sibling list.  */
11137   if (die->die_child != NULL)
11138     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11139                          (unsigned long) die->die_offset);
11140 }
11141
11142 /* Output the compilation unit that appears at the beginning of the
11143    .debug_info section, and precedes the DIE descriptions.  */
11144
11145 static void
11146 output_compilation_unit_header (void)
11147 {
11148   int ver = dwarf_version;
11149
11150   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11151     dw2_asm_output_data (4, 0xffffffff,
11152       "Initial length escape value indicating 64-bit DWARF extension");
11153   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11154                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11155                        "Length of Compilation Unit Info");
11156   dw2_asm_output_data (2, ver, "DWARF version number");
11157   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11158                          debug_abbrev_section,
11159                          "Offset Into Abbrev. Section");
11160   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11161 }
11162
11163 /* Output the compilation unit DIE and its children.  */
11164
11165 static void
11166 output_comp_unit (dw_die_ref die, int output_if_empty)
11167 {
11168   const char *secname;
11169   char *oldsym, *tmp;
11170
11171   /* Unless we are outputting main CU, we may throw away empty ones.  */
11172   if (!output_if_empty && die->die_child == NULL)
11173     return;
11174
11175   /* Even if there are no children of this DIE, we must output the information
11176      about the compilation unit.  Otherwise, on an empty translation unit, we
11177      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11178      will then complain when examining the file.  First mark all the DIEs in
11179      this CU so we know which get local refs.  */
11180   mark_dies (die);
11181
11182   build_abbrev_table (die);
11183
11184   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11185   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11186   calc_die_sizes (die);
11187
11188   oldsym = die->die_id.die_symbol;
11189   if (oldsym)
11190     {
11191       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11192
11193       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11194       secname = tmp;
11195       die->die_id.die_symbol = NULL;
11196       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11197     }
11198   else
11199     switch_to_section (debug_info_section);
11200
11201   /* Output debugging information.  */
11202   output_compilation_unit_header ();
11203   output_die (die);
11204
11205   /* Leave the marks on the main CU, so we can check them in
11206      output_pubnames.  */
11207   if (oldsym)
11208     {
11209       unmark_dies (die);
11210       die->die_id.die_symbol = oldsym;
11211     }
11212 }
11213
11214 /* Output a comdat type unit DIE and its children.  */
11215
11216 static void
11217 output_comdat_type_unit (comdat_type_node *node)
11218 {
11219   const char *secname;
11220   char *tmp;
11221   int i;
11222 #if defined (OBJECT_FORMAT_ELF)
11223   tree comdat_key;
11224 #endif
11225
11226   /* First mark all the DIEs in this CU so we know which get local refs.  */
11227   mark_dies (node->root_die);
11228
11229   build_abbrev_table (node->root_die);
11230
11231   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11232   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11233   calc_die_sizes (node->root_die);
11234
11235 #if defined (OBJECT_FORMAT_ELF)
11236   secname = ".debug_types";
11237   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11238   sprintf (tmp, "wt.");
11239   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11240     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11241   comdat_key = get_identifier (tmp);
11242   targetm.asm_out.named_section (secname,
11243                                  SECTION_DEBUG | SECTION_LINKONCE,
11244                                  comdat_key);
11245 #else
11246   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11247   sprintf (tmp, ".gnu.linkonce.wt.");
11248   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11249     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11250   secname = tmp;
11251   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11252 #endif
11253
11254   /* Output debugging information.  */
11255   output_compilation_unit_header ();
11256   output_signature (node->signature, "Type Signature");
11257   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11258                        "Offset to Type DIE");
11259   output_die (node->root_die);
11260
11261   unmark_dies (node->root_die);
11262 }
11263
11264 /* Return the DWARF2/3 pubname associated with a decl.  */
11265
11266 static const char *
11267 dwarf2_name (tree decl, int scope)
11268 {
11269   if (DECL_NAMELESS (decl))
11270     return NULL;
11271   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11272 }
11273
11274 /* Add a new entry to .debug_pubnames if appropriate.  */
11275
11276 static void
11277 add_pubname_string (const char *str, dw_die_ref die)
11278 {
11279   if (targetm.want_debug_pub_sections)
11280     {
11281       pubname_entry e;
11282
11283       e.die = die;
11284       e.name = xstrdup (str);
11285       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11286     }
11287 }
11288
11289 static void
11290 add_pubname (tree decl, dw_die_ref die)
11291 {
11292   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11293     {
11294       const char *name = dwarf2_name (decl, 1);
11295       if (name)
11296         add_pubname_string (name, die);
11297     }
11298 }
11299
11300 /* Add a new entry to .debug_pubtypes if appropriate.  */
11301
11302 static void
11303 add_pubtype (tree decl, dw_die_ref die)
11304 {
11305   pubname_entry e;
11306
11307   if (!targetm.want_debug_pub_sections)
11308     return;
11309
11310   e.name = NULL;
11311   if ((TREE_PUBLIC (decl)
11312        || die->die_parent == comp_unit_die)
11313       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11314     {
11315       e.die = die;
11316       if (TYPE_P (decl))
11317         {
11318           if (TYPE_NAME (decl))
11319             {
11320               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11321                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11322               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11323                        && DECL_NAME (TYPE_NAME (decl)))
11324                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11325               else
11326                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11327             }
11328         }
11329       else
11330         {
11331           e.name = dwarf2_name (decl, 1);
11332           if (e.name)
11333             e.name = xstrdup (e.name);
11334         }
11335
11336       /* If we don't have a name for the type, there's no point in adding
11337          it to the table.  */
11338       if (e.name && e.name[0] != '\0')
11339         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11340     }
11341 }
11342
11343 /* Output the public names table used to speed up access to externally
11344    visible names; or the public types table used to find type definitions.  */
11345
11346 static void
11347 output_pubnames (VEC (pubname_entry, gc) * names)
11348 {
11349   unsigned i;
11350   unsigned long pubnames_length = size_of_pubnames (names);
11351   pubname_ref pub;
11352
11353   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11354     dw2_asm_output_data (4, 0xffffffff,
11355       "Initial length escape value indicating 64-bit DWARF extension");
11356   if (names == pubname_table)
11357     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11358                          "Length of Public Names Info");
11359   else
11360     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11361                          "Length of Public Type Names Info");
11362   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11363   dw2_asm_output_data (2, 2, "DWARF Version");
11364   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11365                          debug_info_section,
11366                          "Offset of Compilation Unit Info");
11367   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11368                        "Compilation Unit Length");
11369
11370   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
11371     {
11372       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11373       if (names == pubname_table)
11374         gcc_assert (pub->die->die_mark);
11375
11376       if (names != pubtype_table
11377           || pub->die->die_offset != 0
11378           || !flag_eliminate_unused_debug_types)
11379         {
11380           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11381                                "DIE offset");
11382
11383           dw2_asm_output_nstring (pub->name, -1, "external name");
11384         }
11385     }
11386
11387   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11388 }
11389
11390 /* Add a new entry to .debug_aranges if appropriate.  */
11391
11392 static void
11393 add_arange (tree decl, dw_die_ref die)
11394 {
11395   if (! DECL_SECTION_NAME (decl))
11396     return;
11397
11398   if (arange_table_in_use == arange_table_allocated)
11399     {
11400       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11401       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11402                                     arange_table_allocated);
11403       memset (arange_table + arange_table_in_use, 0,
11404               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11405     }
11406
11407   arange_table[arange_table_in_use++] = die;
11408 }
11409
11410 /* Output the information that goes into the .debug_aranges table.
11411    Namely, define the beginning and ending address range of the
11412    text section generated for this compilation unit.  */
11413
11414 static void
11415 output_aranges (void)
11416 {
11417   unsigned i;
11418   unsigned long aranges_length = size_of_aranges ();
11419
11420   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11421     dw2_asm_output_data (4, 0xffffffff,
11422       "Initial length escape value indicating 64-bit DWARF extension");
11423   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11424                        "Length of Address Ranges Info");
11425   /* Version number for aranges is still 2, even in DWARF3.  */
11426   dw2_asm_output_data (2, 2, "DWARF Version");
11427   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11428                          debug_info_section,
11429                          "Offset of Compilation Unit Info");
11430   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11431   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11432
11433   /* We need to align to twice the pointer size here.  */
11434   if (DWARF_ARANGES_PAD_SIZE)
11435     {
11436       /* Pad using a 2 byte words so that padding is correct for any
11437          pointer size.  */
11438       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11439                            2 * DWARF2_ADDR_SIZE);
11440       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11441         dw2_asm_output_data (2, 0, NULL);
11442     }
11443
11444   /* It is necessary not to output these entries if the sections were
11445      not used; if the sections were not used, the length will be 0 and
11446      the address may end up as 0 if the section is discarded by ld
11447      --gc-sections, leaving an invalid (0, 0) entry that can be
11448      confused with the terminator.  */
11449   if (text_section_used)
11450     {
11451       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11452       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11453                             text_section_label, "Length");
11454     }
11455   if (cold_text_section_used)
11456     {
11457       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11458                            "Address");
11459       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11460                             cold_text_section_label, "Length");
11461     }
11462
11463   for (i = 0; i < arange_table_in_use; i++)
11464     {
11465       dw_die_ref die = arange_table[i];
11466
11467       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11468       gcc_assert (die->die_mark);
11469
11470       if (die->die_tag == DW_TAG_subprogram)
11471         {
11472           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11473                                "Address");
11474           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11475                                 get_AT_low_pc (die), "Length");
11476         }
11477       else
11478         {
11479           /* A static variable; extract the symbol from DW_AT_location.
11480              Note that this code isn't currently hit, as we only emit
11481              aranges for functions (jason 9/23/99).  */
11482           dw_attr_ref a = get_AT (die, DW_AT_location);
11483           dw_loc_descr_ref loc;
11484
11485           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11486
11487           loc = AT_loc (a);
11488           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11489
11490           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11491                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11492           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11493                                get_AT_unsigned (die, DW_AT_byte_size),
11494                                "Length");
11495         }
11496     }
11497
11498   /* Output the terminator words.  */
11499   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11500   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11501 }
11502
11503 /* Add a new entry to .debug_ranges.  Return the offset at which it
11504    was placed.  */
11505
11506 static unsigned int
11507 add_ranges_num (int num)
11508 {
11509   unsigned int in_use = ranges_table_in_use;
11510
11511   if (in_use == ranges_table_allocated)
11512     {
11513       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11514       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11515                                     ranges_table_allocated);
11516       memset (ranges_table + ranges_table_in_use, 0,
11517               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11518     }
11519
11520   ranges_table[in_use].num = num;
11521   ranges_table_in_use = in_use + 1;
11522
11523   return in_use * 2 * DWARF2_ADDR_SIZE;
11524 }
11525
11526 /* Add a new entry to .debug_ranges corresponding to a block, or a
11527    range terminator if BLOCK is NULL.  */
11528
11529 static unsigned int
11530 add_ranges (const_tree block)
11531 {
11532   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11533 }
11534
11535 /* Add a new entry to .debug_ranges corresponding to a pair of
11536    labels.  */
11537
11538 static void
11539 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11540                       bool *added)
11541 {
11542   unsigned int in_use = ranges_by_label_in_use;
11543   unsigned int offset;
11544
11545   if (in_use == ranges_by_label_allocated)
11546     {
11547       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11548       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11549                                        ranges_by_label,
11550                                        ranges_by_label_allocated);
11551       memset (ranges_by_label + ranges_by_label_in_use, 0,
11552               RANGES_TABLE_INCREMENT
11553               * sizeof (struct dw_ranges_by_label_struct));
11554     }
11555
11556   ranges_by_label[in_use].begin = begin;
11557   ranges_by_label[in_use].end = end;
11558   ranges_by_label_in_use = in_use + 1;
11559
11560   offset = add_ranges_num (-(int)in_use - 1);
11561   if (!*added)
11562     {
11563       add_AT_range_list (die, DW_AT_ranges, offset);
11564       *added = true;
11565     }
11566 }
11567
11568 static void
11569 output_ranges (void)
11570 {
11571   unsigned i;
11572   static const char *const start_fmt = "Offset %#x";
11573   const char *fmt = start_fmt;
11574
11575   for (i = 0; i < ranges_table_in_use; i++)
11576     {
11577       int block_num = ranges_table[i].num;
11578
11579       if (block_num > 0)
11580         {
11581           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11582           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11583
11584           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11585           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11586
11587           /* If all code is in the text section, then the compilation
11588              unit base address defaults to DW_AT_low_pc, which is the
11589              base of the text section.  */
11590           if (!have_multiple_function_sections)
11591             {
11592               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11593                                     text_section_label,
11594                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11595               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11596                                     text_section_label, NULL);
11597             }
11598
11599           /* Otherwise, the compilation unit base address is zero,
11600              which allows us to use absolute addresses, and not worry
11601              about whether the target supports cross-section
11602              arithmetic.  */
11603           else
11604             {
11605               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11606                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11607               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11608             }
11609
11610           fmt = NULL;
11611         }
11612
11613       /* Negative block_num stands for an index into ranges_by_label.  */
11614       else if (block_num < 0)
11615         {
11616           int lab_idx = - block_num - 1;
11617
11618           if (!have_multiple_function_sections)
11619             {
11620               gcc_unreachable ();
11621 #if 0
11622               /* If we ever use add_ranges_by_labels () for a single
11623                  function section, all we have to do is to take out
11624                  the #if 0 above.  */
11625               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11626                                     ranges_by_label[lab_idx].begin,
11627                                     text_section_label,
11628                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11629               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11630                                     ranges_by_label[lab_idx].end,
11631                                     text_section_label, NULL);
11632 #endif
11633             }
11634           else
11635             {
11636               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11637                                    ranges_by_label[lab_idx].begin,
11638                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11639               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11640                                    ranges_by_label[lab_idx].end,
11641                                    NULL);
11642             }
11643         }
11644       else
11645         {
11646           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11647           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11648           fmt = start_fmt;
11649         }
11650     }
11651 }
11652
11653 /* Data structure containing information about input files.  */
11654 struct file_info
11655 {
11656   const char *path;     /* Complete file name.  */
11657   const char *fname;    /* File name part.  */
11658   int length;           /* Length of entire string.  */
11659   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11660   int dir_idx;          /* Index in directory table.  */
11661 };
11662
11663 /* Data structure containing information about directories with source
11664    files.  */
11665 struct dir_info
11666 {
11667   const char *path;     /* Path including directory name.  */
11668   int length;           /* Path length.  */
11669   int prefix;           /* Index of directory entry which is a prefix.  */
11670   int count;            /* Number of files in this directory.  */
11671   int dir_idx;          /* Index of directory used as base.  */
11672 };
11673
11674 /* Callback function for file_info comparison.  We sort by looking at
11675    the directories in the path.  */
11676
11677 static int
11678 file_info_cmp (const void *p1, const void *p2)
11679 {
11680   const struct file_info *const s1 = (const struct file_info *) p1;
11681   const struct file_info *const s2 = (const struct file_info *) p2;
11682   const unsigned char *cp1;
11683   const unsigned char *cp2;
11684
11685   /* Take care of file names without directories.  We need to make sure that
11686      we return consistent values to qsort since some will get confused if
11687      we return the same value when identical operands are passed in opposite
11688      orders.  So if neither has a directory, return 0 and otherwise return
11689      1 or -1 depending on which one has the directory.  */
11690   if ((s1->path == s1->fname || s2->path == s2->fname))
11691     return (s2->path == s2->fname) - (s1->path == s1->fname);
11692
11693   cp1 = (const unsigned char *) s1->path;
11694   cp2 = (const unsigned char *) s2->path;
11695
11696   while (1)
11697     {
11698       ++cp1;
11699       ++cp2;
11700       /* Reached the end of the first path?  If so, handle like above.  */
11701       if ((cp1 == (const unsigned char *) s1->fname)
11702           || (cp2 == (const unsigned char *) s2->fname))
11703         return ((cp2 == (const unsigned char *) s2->fname)
11704                 - (cp1 == (const unsigned char *) s1->fname));
11705
11706       /* Character of current path component the same?  */
11707       else if (*cp1 != *cp2)
11708         return *cp1 - *cp2;
11709     }
11710 }
11711
11712 struct file_name_acquire_data
11713 {
11714   struct file_info *files;
11715   int used_files;
11716   int max_files;
11717 };
11718
11719 /* Traversal function for the hash table.  */
11720
11721 static int
11722 file_name_acquire (void ** slot, void *data)
11723 {
11724   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11725   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11726   struct file_info *fi;
11727   const char *f;
11728
11729   gcc_assert (fnad->max_files >= d->emitted_number);
11730
11731   if (! d->emitted_number)
11732     return 1;
11733
11734   gcc_assert (fnad->max_files != fnad->used_files);
11735
11736   fi = fnad->files + fnad->used_files++;
11737
11738   /* Skip all leading "./".  */
11739   f = d->filename;
11740   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11741     f += 2;
11742
11743   /* Create a new array entry.  */
11744   fi->path = f;
11745   fi->length = strlen (f);
11746   fi->file_idx = d;
11747
11748   /* Search for the file name part.  */
11749   f = strrchr (f, DIR_SEPARATOR);
11750 #if defined (DIR_SEPARATOR_2)
11751   {
11752     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11753
11754     if (g != NULL)
11755       {
11756         if (f == NULL || f < g)
11757           f = g;
11758       }
11759   }
11760 #endif
11761
11762   fi->fname = f == NULL ? fi->path : f + 1;
11763   return 1;
11764 }
11765
11766 /* Output the directory table and the file name table.  We try to minimize
11767    the total amount of memory needed.  A heuristic is used to avoid large
11768    slowdowns with many input files.  */
11769
11770 static void
11771 output_file_names (void)
11772 {
11773   struct file_name_acquire_data fnad;
11774   int numfiles;
11775   struct file_info *files;
11776   struct dir_info *dirs;
11777   int *saved;
11778   int *savehere;
11779   int *backmap;
11780   int ndirs;
11781   int idx_offset;
11782   int i;
11783
11784   if (!last_emitted_file)
11785     {
11786       dw2_asm_output_data (1, 0, "End directory table");
11787       dw2_asm_output_data (1, 0, "End file name table");
11788       return;
11789     }
11790
11791   numfiles = last_emitted_file->emitted_number;
11792
11793   /* Allocate the various arrays we need.  */
11794   files = XALLOCAVEC (struct file_info, numfiles);
11795   dirs = XALLOCAVEC (struct dir_info, numfiles);
11796
11797   fnad.files = files;
11798   fnad.used_files = 0;
11799   fnad.max_files = numfiles;
11800   htab_traverse (file_table, file_name_acquire, &fnad);
11801   gcc_assert (fnad.used_files == fnad.max_files);
11802
11803   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11804
11805   /* Find all the different directories used.  */
11806   dirs[0].path = files[0].path;
11807   dirs[0].length = files[0].fname - files[0].path;
11808   dirs[0].prefix = -1;
11809   dirs[0].count = 1;
11810   dirs[0].dir_idx = 0;
11811   files[0].dir_idx = 0;
11812   ndirs = 1;
11813
11814   for (i = 1; i < numfiles; i++)
11815     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11816         && memcmp (dirs[ndirs - 1].path, files[i].path,
11817                    dirs[ndirs - 1].length) == 0)
11818       {
11819         /* Same directory as last entry.  */
11820         files[i].dir_idx = ndirs - 1;
11821         ++dirs[ndirs - 1].count;
11822       }
11823     else
11824       {
11825         int j;
11826
11827         /* This is a new directory.  */
11828         dirs[ndirs].path = files[i].path;
11829         dirs[ndirs].length = files[i].fname - files[i].path;
11830         dirs[ndirs].count = 1;
11831         dirs[ndirs].dir_idx = ndirs;
11832         files[i].dir_idx = ndirs;
11833
11834         /* Search for a prefix.  */
11835         dirs[ndirs].prefix = -1;
11836         for (j = 0; j < ndirs; j++)
11837           if (dirs[j].length < dirs[ndirs].length
11838               && dirs[j].length > 1
11839               && (dirs[ndirs].prefix == -1
11840                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11841               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11842             dirs[ndirs].prefix = j;
11843
11844         ++ndirs;
11845       }
11846
11847   /* Now to the actual work.  We have to find a subset of the directories which
11848      allow expressing the file name using references to the directory table
11849      with the least amount of characters.  We do not do an exhaustive search
11850      where we would have to check out every combination of every single
11851      possible prefix.  Instead we use a heuristic which provides nearly optimal
11852      results in most cases and never is much off.  */
11853   saved = XALLOCAVEC (int, ndirs);
11854   savehere = XALLOCAVEC (int, ndirs);
11855
11856   memset (saved, '\0', ndirs * sizeof (saved[0]));
11857   for (i = 0; i < ndirs; i++)
11858     {
11859       int j;
11860       int total;
11861
11862       /* We can always save some space for the current directory.  But this
11863          does not mean it will be enough to justify adding the directory.  */
11864       savehere[i] = dirs[i].length;
11865       total = (savehere[i] - saved[i]) * dirs[i].count;
11866
11867       for (j = i + 1; j < ndirs; j++)
11868         {
11869           savehere[j] = 0;
11870           if (saved[j] < dirs[i].length)
11871             {
11872               /* Determine whether the dirs[i] path is a prefix of the
11873                  dirs[j] path.  */
11874               int k;
11875
11876               k = dirs[j].prefix;
11877               while (k != -1 && k != (int) i)
11878                 k = dirs[k].prefix;
11879
11880               if (k == (int) i)
11881                 {
11882                   /* Yes it is.  We can possibly save some memory by
11883                      writing the filenames in dirs[j] relative to
11884                      dirs[i].  */
11885                   savehere[j] = dirs[i].length;
11886                   total += (savehere[j] - saved[j]) * dirs[j].count;
11887                 }
11888             }
11889         }
11890
11891       /* Check whether we can save enough to justify adding the dirs[i]
11892          directory.  */
11893       if (total > dirs[i].length + 1)
11894         {
11895           /* It's worthwhile adding.  */
11896           for (j = i; j < ndirs; j++)
11897             if (savehere[j] > 0)
11898               {
11899                 /* Remember how much we saved for this directory so far.  */
11900                 saved[j] = savehere[j];
11901
11902                 /* Remember the prefix directory.  */
11903                 dirs[j].dir_idx = i;
11904               }
11905         }
11906     }
11907
11908   /* Emit the directory name table.  */
11909   idx_offset = dirs[0].length > 0 ? 1 : 0;
11910   for (i = 1 - idx_offset; i < ndirs; i++)
11911     dw2_asm_output_nstring (dirs[i].path,
11912                             dirs[i].length
11913                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11914                             "Directory Entry: %#x", i + idx_offset);
11915
11916   dw2_asm_output_data (1, 0, "End directory table");
11917
11918   /* We have to emit them in the order of emitted_number since that's
11919      used in the debug info generation.  To do this efficiently we
11920      generate a back-mapping of the indices first.  */
11921   backmap = XALLOCAVEC (int, numfiles);
11922   for (i = 0; i < numfiles; i++)
11923     backmap[files[i].file_idx->emitted_number - 1] = i;
11924
11925   /* Now write all the file names.  */
11926   for (i = 0; i < numfiles; i++)
11927     {
11928       int file_idx = backmap[i];
11929       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11930
11931 #ifdef VMS_DEBUGGING_INFO
11932 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11933
11934       /* Setting these fields can lead to debugger miscomparisons,
11935          but VMS Debug requires them to be set correctly.  */
11936
11937       int ver;
11938       long long cdt;
11939       long siz;
11940       int maxfilelen = strlen (files[file_idx].path)
11941                                + dirs[dir_idx].length
11942                                + MAX_VMS_VERSION_LEN + 1;
11943       char *filebuf = XALLOCAVEC (char, maxfilelen);
11944
11945       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11946       snprintf (filebuf, maxfilelen, "%s;%d",
11947                 files[file_idx].path + dirs[dir_idx].length, ver);
11948
11949       dw2_asm_output_nstring
11950         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11951
11952       /* Include directory index.  */
11953       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11954
11955       /* Modification time.  */
11956       dw2_asm_output_data_uleb128
11957         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11958           ? cdt : 0,
11959          NULL);
11960
11961       /* File length in bytes.  */
11962       dw2_asm_output_data_uleb128
11963         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11964           ? siz : 0,
11965          NULL);
11966 #else
11967       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11968                               "File Entry: %#x", (unsigned) i + 1);
11969
11970       /* Include directory index.  */
11971       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11972
11973       /* Modification time.  */
11974       dw2_asm_output_data_uleb128 (0, NULL);
11975
11976       /* File length in bytes.  */
11977       dw2_asm_output_data_uleb128 (0, NULL);
11978 #endif /* VMS_DEBUGGING_INFO */
11979     }
11980
11981   dw2_asm_output_data (1, 0, "End file name table");
11982 }
11983
11984
11985 /* Output the source line number correspondence information.  This
11986    information goes into the .debug_line section.  */
11987
11988 static void
11989 output_line_info (void)
11990 {
11991   char l1[20], l2[20], p1[20], p2[20];
11992   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11993   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11994   unsigned opc;
11995   unsigned n_op_args;
11996   unsigned long lt_index;
11997   unsigned long current_line;
11998   long line_offset;
11999   long line_delta;
12000   unsigned long current_file;
12001   unsigned long function;
12002   int ver = dwarf_version;
12003
12004   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12005   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12006   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12007   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12008
12009   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12010     dw2_asm_output_data (4, 0xffffffff,
12011       "Initial length escape value indicating 64-bit DWARF extension");
12012   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12013                         "Length of Source Line Info");
12014   ASM_OUTPUT_LABEL (asm_out_file, l1);
12015
12016   dw2_asm_output_data (2, ver, "DWARF Version");
12017   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12018   ASM_OUTPUT_LABEL (asm_out_file, p1);
12019
12020   /* Define the architecture-dependent minimum instruction length (in
12021    bytes).  In this implementation of DWARF, this field is used for
12022    information purposes only.  Since GCC generates assembly language,
12023    we have no a priori knowledge of how many instruction bytes are
12024    generated for each source line, and therefore can use only the
12025    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12026    commands.  Accordingly, we fix this as `1', which is "correct
12027    enough" for all architectures, and don't let the target override.  */
12028   dw2_asm_output_data (1, 1,
12029                        "Minimum Instruction Length");
12030
12031   if (ver >= 4)
12032     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12033                          "Maximum Operations Per Instruction");
12034   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12035                        "Default is_stmt_start flag");
12036   dw2_asm_output_data (1, DWARF_LINE_BASE,
12037                        "Line Base Value (Special Opcodes)");
12038   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12039                        "Line Range Value (Special Opcodes)");
12040   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12041                        "Special Opcode Base");
12042
12043   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12044     {
12045       switch (opc)
12046         {
12047         case DW_LNS_advance_pc:
12048         case DW_LNS_advance_line:
12049         case DW_LNS_set_file:
12050         case DW_LNS_set_column:
12051         case DW_LNS_fixed_advance_pc:
12052           n_op_args = 1;
12053           break;
12054         default:
12055           n_op_args = 0;
12056           break;
12057         }
12058
12059       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12060                            opc, n_op_args);
12061     }
12062
12063   /* Write out the information about the files we use.  */
12064   output_file_names ();
12065   ASM_OUTPUT_LABEL (asm_out_file, p2);
12066
12067   /* We used to set the address register to the first location in the text
12068      section here, but that didn't accomplish anything since we already
12069      have a line note for the opening brace of the first function.  */
12070
12071   /* Generate the line number to PC correspondence table, encoded as
12072      a series of state machine operations.  */
12073   current_file = 1;
12074   current_line = 1;
12075
12076   if (cfun && in_cold_section_p)
12077     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12078   else
12079     strcpy (prev_line_label, text_section_label);
12080   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12081     {
12082       dw_line_info_ref line_info = &line_info_table[lt_index];
12083
12084 #if 0
12085       /* Disable this optimization for now; GDB wants to see two line notes
12086          at the beginning of a function so it can find the end of the
12087          prologue.  */
12088
12089       /* Don't emit anything for redundant notes.  Just updating the
12090          address doesn't accomplish anything, because we already assume
12091          that anything after the last address is this line.  */
12092       if (line_info->dw_line_num == current_line
12093           && line_info->dw_file_num == current_file)
12094         continue;
12095 #endif
12096
12097       /* Emit debug info for the address of the current line.
12098
12099          Unfortunately, we have little choice here currently, and must always
12100          use the most general form.  GCC does not know the address delta
12101          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12102          attributes which will give an upper bound on the address range.  We
12103          could perhaps use length attributes to determine when it is safe to
12104          use DW_LNS_fixed_advance_pc.  */
12105
12106       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12107       if (0)
12108         {
12109           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12110           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12111                                "DW_LNS_fixed_advance_pc");
12112           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12113         }
12114       else
12115         {
12116           /* This can handle any delta.  This takes
12117              4+DWARF2_ADDR_SIZE bytes.  */
12118           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12119           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12120           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12121           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12122         }
12123
12124       strcpy (prev_line_label, line_label);
12125
12126       /* Emit debug info for the source file of the current line, if
12127          different from the previous line.  */
12128       if (line_info->dw_file_num != current_file)
12129         {
12130           current_file = line_info->dw_file_num;
12131           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12132           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12133         }
12134
12135       /* Emit debug info for the current line number, choosing the encoding
12136          that uses the least amount of space.  */
12137       if (line_info->dw_line_num != current_line)
12138         {
12139           line_offset = line_info->dw_line_num - current_line;
12140           line_delta = line_offset - DWARF_LINE_BASE;
12141           current_line = line_info->dw_line_num;
12142           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12143             /* This can handle deltas from -10 to 234, using the current
12144                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12145                takes 1 byte.  */
12146             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12147                                  "line %lu", current_line);
12148           else
12149             {
12150               /* This can handle any delta.  This takes at least 4 bytes,
12151                  depending on the value being encoded.  */
12152               dw2_asm_output_data (1, DW_LNS_advance_line,
12153                                    "advance to line %lu", current_line);
12154               dw2_asm_output_data_sleb128 (line_offset, NULL);
12155               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12156             }
12157         }
12158       else
12159         /* We still need to start a new row, so output a copy insn.  */
12160         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12161     }
12162
12163   /* Emit debug info for the address of the end of the function.  */
12164   if (0)
12165     {
12166       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12167                            "DW_LNS_fixed_advance_pc");
12168       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12169     }
12170   else
12171     {
12172       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12173       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12174       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12175       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12176     }
12177
12178   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12179   dw2_asm_output_data_uleb128 (1, NULL);
12180   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12181
12182   function = 0;
12183   current_file = 1;
12184   current_line = 1;
12185   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12186     {
12187       dw_separate_line_info_ref line_info
12188         = &separate_line_info_table[lt_index];
12189
12190 #if 0
12191       /* Don't emit anything for redundant notes.  */
12192       if (line_info->dw_line_num == current_line
12193           && line_info->dw_file_num == current_file
12194           && line_info->function == function)
12195         goto cont;
12196 #endif
12197
12198       /* Emit debug info for the address of the current line.  If this is
12199          a new function, or the first line of a function, then we need
12200          to handle it differently.  */
12201       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12202                                    lt_index);
12203       if (function != line_info->function)
12204         {
12205           function = line_info->function;
12206
12207           /* Set the address register to the first line in the function.  */
12208           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12209           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12210           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12211           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12212         }
12213       else
12214         {
12215           /* ??? See the DW_LNS_advance_pc comment above.  */
12216           if (0)
12217             {
12218               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12219                                    "DW_LNS_fixed_advance_pc");
12220               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12221             }
12222           else
12223             {
12224               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12225               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12226               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12227               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12228             }
12229         }
12230
12231       strcpy (prev_line_label, line_label);
12232
12233       /* Emit debug info for the source file of the current line, if
12234          different from the previous line.  */
12235       if (line_info->dw_file_num != current_file)
12236         {
12237           current_file = line_info->dw_file_num;
12238           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12239           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12240         }
12241
12242       /* Emit debug info for the current line number, choosing the encoding
12243          that uses the least amount of space.  */
12244       if (line_info->dw_line_num != current_line)
12245         {
12246           line_offset = line_info->dw_line_num - current_line;
12247           line_delta = line_offset - DWARF_LINE_BASE;
12248           current_line = line_info->dw_line_num;
12249           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12250             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12251                                  "line %lu", current_line);
12252           else
12253             {
12254               dw2_asm_output_data (1, DW_LNS_advance_line,
12255                                    "advance to line %lu", current_line);
12256               dw2_asm_output_data_sleb128 (line_offset, NULL);
12257               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12258             }
12259         }
12260       else
12261         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12262
12263 #if 0
12264     cont:
12265 #endif
12266
12267       lt_index++;
12268
12269       /* If we're done with a function, end its sequence.  */
12270       if (lt_index == separate_line_info_table_in_use
12271           || separate_line_info_table[lt_index].function != function)
12272         {
12273           current_file = 1;
12274           current_line = 1;
12275
12276           /* Emit debug info for the address of the end of the function.  */
12277           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12278           if (0)
12279             {
12280               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12281                                    "DW_LNS_fixed_advance_pc");
12282               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12283             }
12284           else
12285             {
12286               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12287               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12288               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12289               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12290             }
12291
12292           /* Output the marker for the end of this sequence.  */
12293           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12294           dw2_asm_output_data_uleb128 (1, NULL);
12295           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12296         }
12297     }
12298
12299   /* Output the marker for the end of the line number info.  */
12300   ASM_OUTPUT_LABEL (asm_out_file, l2);
12301 }
12302
12303 /* Return the size of the .debug_dcall table for the compilation unit.  */
12304
12305 static unsigned long
12306 size_of_dcall_table (void)
12307 {
12308   unsigned long size;
12309   unsigned int i;
12310   dcall_entry *p;
12311   tree last_poc_decl = NULL;
12312
12313   /* Header:  version + debug info section pointer + pointer size.  */
12314   size = 2 + DWARF_OFFSET_SIZE + 1;
12315
12316   /* Each entry:  code label + DIE offset.  */
12317   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12318     {
12319       gcc_assert (p->targ_die != NULL);
12320       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12321       if (p->poc_decl != last_poc_decl)
12322         {
12323           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12324           gcc_assert (poc_die);
12325           last_poc_decl = p->poc_decl;
12326           if (poc_die)
12327             size += (DWARF_OFFSET_SIZE
12328                      + size_of_uleb128 (poc_die->die_offset));
12329         }
12330       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12331     }
12332
12333   return size;
12334 }
12335
12336 /* Output the direct call table used to disambiguate PC values when
12337    identical function have been merged.  */
12338
12339 static void
12340 output_dcall_table (void)
12341 {
12342   unsigned i;
12343   unsigned long dcall_length = size_of_dcall_table ();
12344   dcall_entry *p;
12345   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12346   tree last_poc_decl = NULL;
12347
12348   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12349     dw2_asm_output_data (4, 0xffffffff,
12350       "Initial length escape value indicating 64-bit DWARF extension");
12351   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12352                        "Length of Direct Call Table");
12353   dw2_asm_output_data (2, 4, "Version number");
12354   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12355                          debug_info_section,
12356                          "Offset of Compilation Unit Info");
12357   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12358
12359   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12360     {
12361       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12362       if (p->poc_decl != last_poc_decl)
12363         {
12364           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12365           last_poc_decl = p->poc_decl;
12366           if (poc_die)
12367             {
12368               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12369               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12370                                            "Caller DIE offset");
12371             }
12372         }
12373       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12374       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12375       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12376                                    "Callee DIE offset");
12377     }
12378 }
12379 \f
12380 /* Return the size of the .debug_vcall table for the compilation unit.  */
12381
12382 static unsigned long
12383 size_of_vcall_table (void)
12384 {
12385   unsigned long size;
12386   unsigned int i;
12387   vcall_entry *p;
12388
12389   /* Header:  version + pointer size.  */
12390   size = 2 + 1;
12391
12392   /* Each entry:  code label + vtable slot index.  */
12393   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12394     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12395
12396   return size;
12397 }
12398
12399 /* Output the virtual call table used to disambiguate PC values when
12400    identical function have been merged.  */
12401
12402 static void
12403 output_vcall_table (void)
12404 {
12405   unsigned i;
12406   unsigned long vcall_length = size_of_vcall_table ();
12407   vcall_entry *p;
12408   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12409
12410   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12411     dw2_asm_output_data (4, 0xffffffff,
12412       "Initial length escape value indicating 64-bit DWARF extension");
12413   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12414                        "Length of Virtual Call Table");
12415   dw2_asm_output_data (2, 4, "Version number");
12416   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12417
12418   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12419     {
12420       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12421       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12422       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12423     }
12424 }
12425 \f
12426 /* Given a pointer to a tree node for some base type, return a pointer to
12427    a DIE that describes the given type.
12428
12429    This routine must only be called for GCC type nodes that correspond to
12430    Dwarf base (fundamental) types.  */
12431
12432 static dw_die_ref
12433 base_type_die (tree type)
12434 {
12435   dw_die_ref base_type_result;
12436   enum dwarf_type encoding;
12437
12438   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12439     return 0;
12440
12441   /* If this is a subtype that should not be emitted as a subrange type,
12442      use the base type.  See subrange_type_for_debug_p.  */
12443   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12444     type = TREE_TYPE (type);
12445
12446   switch (TREE_CODE (type))
12447     {
12448     case INTEGER_TYPE:
12449       if ((dwarf_version >= 4 || !dwarf_strict)
12450           && TYPE_NAME (type)
12451           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12452           && DECL_IS_BUILTIN (TYPE_NAME (type))
12453           && DECL_NAME (TYPE_NAME (type)))
12454         {
12455           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12456           if (strcmp (name, "char16_t") == 0
12457               || strcmp (name, "char32_t") == 0)
12458             {
12459               encoding = DW_ATE_UTF;
12460               break;
12461             }
12462         }
12463       if (TYPE_STRING_FLAG (type))
12464         {
12465           if (TYPE_UNSIGNED (type))
12466             encoding = DW_ATE_unsigned_char;
12467           else
12468             encoding = DW_ATE_signed_char;
12469         }
12470       else if (TYPE_UNSIGNED (type))
12471         encoding = DW_ATE_unsigned;
12472       else
12473         encoding = DW_ATE_signed;
12474       break;
12475
12476     case REAL_TYPE:
12477       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12478         {
12479           if (dwarf_version >= 3 || !dwarf_strict)
12480             encoding = DW_ATE_decimal_float;
12481           else
12482             encoding = DW_ATE_lo_user;
12483         }
12484       else
12485         encoding = DW_ATE_float;
12486       break;
12487
12488     case FIXED_POINT_TYPE:
12489       if (!(dwarf_version >= 3 || !dwarf_strict))
12490         encoding = DW_ATE_lo_user;
12491       else if (TYPE_UNSIGNED (type))
12492         encoding = DW_ATE_unsigned_fixed;
12493       else
12494         encoding = DW_ATE_signed_fixed;
12495       break;
12496
12497       /* Dwarf2 doesn't know anything about complex ints, so use
12498          a user defined type for it.  */
12499     case COMPLEX_TYPE:
12500       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12501         encoding = DW_ATE_complex_float;
12502       else
12503         encoding = DW_ATE_lo_user;
12504       break;
12505
12506     case BOOLEAN_TYPE:
12507       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12508       encoding = DW_ATE_boolean;
12509       break;
12510
12511     default:
12512       /* No other TREE_CODEs are Dwarf fundamental types.  */
12513       gcc_unreachable ();
12514     }
12515
12516   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12517
12518   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12519                    int_size_in_bytes (type));
12520   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12521
12522   return base_type_result;
12523 }
12524
12525 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12526    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12527
12528 static inline int
12529 is_base_type (tree type)
12530 {
12531   switch (TREE_CODE (type))
12532     {
12533     case ERROR_MARK:
12534     case VOID_TYPE:
12535     case INTEGER_TYPE:
12536     case REAL_TYPE:
12537     case FIXED_POINT_TYPE:
12538     case COMPLEX_TYPE:
12539     case BOOLEAN_TYPE:
12540       return 1;
12541
12542     case ARRAY_TYPE:
12543     case RECORD_TYPE:
12544     case UNION_TYPE:
12545     case QUAL_UNION_TYPE:
12546     case ENUMERAL_TYPE:
12547     case FUNCTION_TYPE:
12548     case METHOD_TYPE:
12549     case POINTER_TYPE:
12550     case REFERENCE_TYPE:
12551     case OFFSET_TYPE:
12552     case LANG_TYPE:
12553     case VECTOR_TYPE:
12554       return 0;
12555
12556     default:
12557       gcc_unreachable ();
12558     }
12559
12560   return 0;
12561 }
12562
12563 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12564    node, return the size in bits for the type if it is a constant, or else
12565    return the alignment for the type if the type's size is not constant, or
12566    else return BITS_PER_WORD if the type actually turns out to be an
12567    ERROR_MARK node.  */
12568
12569 static inline unsigned HOST_WIDE_INT
12570 simple_type_size_in_bits (const_tree type)
12571 {
12572   if (TREE_CODE (type) == ERROR_MARK)
12573     return BITS_PER_WORD;
12574   else if (TYPE_SIZE (type) == NULL_TREE)
12575     return 0;
12576   else if (host_integerp (TYPE_SIZE (type), 1))
12577     return tree_low_cst (TYPE_SIZE (type), 1);
12578   else
12579     return TYPE_ALIGN (type);
12580 }
12581
12582 /* Similarly, but return a double_int instead of UHWI.  */
12583
12584 static inline double_int
12585 double_int_type_size_in_bits (const_tree type)
12586 {
12587   if (TREE_CODE (type) == ERROR_MARK)
12588     return uhwi_to_double_int (BITS_PER_WORD);
12589   else if (TYPE_SIZE (type) == NULL_TREE)
12590     return double_int_zero;
12591   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12592     return tree_to_double_int (TYPE_SIZE (type));
12593   else
12594     return uhwi_to_double_int (TYPE_ALIGN (type));
12595 }
12596
12597 /*  Given a pointer to a tree node for a subrange type, return a pointer
12598     to a DIE that describes the given type.  */
12599
12600 static dw_die_ref
12601 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12602 {
12603   dw_die_ref subrange_die;
12604   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12605
12606   if (context_die == NULL)
12607     context_die = comp_unit_die;
12608
12609   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12610
12611   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12612     {
12613       /* The size of the subrange type and its base type do not match,
12614          so we need to generate a size attribute for the subrange type.  */
12615       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12616     }
12617
12618   if (low)
12619     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12620   if (high)
12621     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12622
12623   return subrange_die;
12624 }
12625
12626 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12627    entry that chains various modifiers in front of the given type.  */
12628
12629 static dw_die_ref
12630 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12631                    dw_die_ref context_die)
12632 {
12633   enum tree_code code = TREE_CODE (type);
12634   dw_die_ref mod_type_die;
12635   dw_die_ref sub_die = NULL;
12636   tree item_type = NULL;
12637   tree qualified_type;
12638   tree name, low, high;
12639
12640   if (code == ERROR_MARK)
12641     return NULL;
12642
12643   /* See if we already have the appropriately qualified variant of
12644      this type.  */
12645   qualified_type
12646     = get_qualified_type (type,
12647                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12648                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12649
12650   if (qualified_type == sizetype
12651       && TYPE_NAME (qualified_type)
12652       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12653     {
12654 #ifdef ENABLE_CHECKING
12655       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12656                   == INTEGER_TYPE
12657                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12658                      == TYPE_PRECISION (qualified_type)
12659                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12660                      == TYPE_UNSIGNED (qualified_type));
12661 #endif
12662       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12663     }
12664
12665   /* If we do, then we can just use its DIE, if it exists.  */
12666   if (qualified_type)
12667     {
12668       mod_type_die = lookup_type_die (qualified_type);
12669       if (mod_type_die)
12670         return mod_type_die;
12671     }
12672
12673   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12674
12675   /* Handle C typedef types.  */
12676   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12677       && !DECL_ARTIFICIAL (name))
12678     {
12679       tree dtype = TREE_TYPE (name);
12680
12681       if (qualified_type == dtype)
12682         {
12683           /* For a named type, use the typedef.  */
12684           gen_type_die (qualified_type, context_die);
12685           return lookup_type_die (qualified_type);
12686         }
12687       else if (is_const_type < TYPE_READONLY (dtype)
12688                || is_volatile_type < TYPE_VOLATILE (dtype)
12689                || (is_const_type <= TYPE_READONLY (dtype)
12690                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12691                    && DECL_ORIGINAL_TYPE (name) != type))
12692         /* cv-unqualified version of named type.  Just use the unnamed
12693            type to which it refers.  */
12694         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12695                                   is_const_type, is_volatile_type,
12696                                   context_die);
12697       /* Else cv-qualified version of named type; fall through.  */
12698     }
12699
12700   if (is_const_type)
12701     {
12702       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12703       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12704     }
12705   else if (is_volatile_type)
12706     {
12707       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12708       sub_die = modified_type_die (type, 0, 0, context_die);
12709     }
12710   else if (code == POINTER_TYPE)
12711     {
12712       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12713       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12714                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12715       item_type = TREE_TYPE (type);
12716       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12717         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12718                          TYPE_ADDR_SPACE (item_type));
12719     }
12720   else if (code == REFERENCE_TYPE)
12721     {
12722       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12723         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12724                                 type);
12725       else
12726         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12727       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12728                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12729       item_type = TREE_TYPE (type);
12730       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12731         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12732                          TYPE_ADDR_SPACE (item_type));
12733     }
12734   else if (code == INTEGER_TYPE
12735            && TREE_TYPE (type) != NULL_TREE
12736            && subrange_type_for_debug_p (type, &low, &high))
12737     {
12738       mod_type_die = subrange_type_die (type, low, high, context_die);
12739       item_type = TREE_TYPE (type);
12740     }
12741   else if (is_base_type (type))
12742     mod_type_die = base_type_die (type);
12743   else
12744     {
12745       gen_type_die (type, context_die);
12746
12747       /* We have to get the type_main_variant here (and pass that to the
12748          `lookup_type_die' routine) because the ..._TYPE node we have
12749          might simply be a *copy* of some original type node (where the
12750          copy was created to help us keep track of typedef names) and
12751          that copy might have a different TYPE_UID from the original
12752          ..._TYPE node.  */
12753       if (TREE_CODE (type) != VECTOR_TYPE)
12754         return lookup_type_die (type_main_variant (type));
12755       else
12756         /* Vectors have the debugging information in the type,
12757            not the main variant.  */
12758         return lookup_type_die (type);
12759     }
12760
12761   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12762      don't output a DW_TAG_typedef, since there isn't one in the
12763      user's program; just attach a DW_AT_name to the type.
12764      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12765      if the base type already has the same name.  */
12766   if (name
12767       && ((TREE_CODE (name) != TYPE_DECL
12768            && (qualified_type == TYPE_MAIN_VARIANT (type)
12769                || (!is_const_type && !is_volatile_type)))
12770           || (TREE_CODE (name) == TYPE_DECL
12771               && TREE_TYPE (name) == qualified_type
12772               && DECL_NAME (name))))
12773     {
12774       if (TREE_CODE (name) == TYPE_DECL)
12775         /* Could just call add_name_and_src_coords_attributes here,
12776            but since this is a builtin type it doesn't have any
12777            useful source coordinates anyway.  */
12778         name = DECL_NAME (name);
12779       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12780     }
12781   /* This probably indicates a bug.  */
12782   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12783     add_name_attribute (mod_type_die, "__unknown__");
12784
12785   if (qualified_type)
12786     equate_type_number_to_die (qualified_type, mod_type_die);
12787
12788   if (item_type)
12789     /* We must do this after the equate_type_number_to_die call, in case
12790        this is a recursive type.  This ensures that the modified_type_die
12791        recursion will terminate even if the type is recursive.  Recursive
12792        types are possible in Ada.  */
12793     sub_die = modified_type_die (item_type,
12794                                  TYPE_READONLY (item_type),
12795                                  TYPE_VOLATILE (item_type),
12796                                  context_die);
12797
12798   if (sub_die != NULL)
12799     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12800
12801   return mod_type_die;
12802 }
12803
12804 /* Generate DIEs for the generic parameters of T.
12805    T must be either a generic type or a generic function.
12806    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12807
12808 static void
12809 gen_generic_params_dies (tree t)
12810 {
12811   tree parms, args;
12812   int parms_num, i;
12813   dw_die_ref die = NULL;
12814
12815   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12816     return;
12817
12818   if (TYPE_P (t))
12819     die = lookup_type_die (t);
12820   else if (DECL_P (t))
12821     die = lookup_decl_die (t);
12822
12823   gcc_assert (die);
12824
12825   parms = lang_hooks.get_innermost_generic_parms (t);
12826   if (!parms)
12827     /* T has no generic parameter. It means T is neither a generic type
12828        or function. End of story.  */
12829     return;
12830
12831   parms_num = TREE_VEC_LENGTH (parms);
12832   args = lang_hooks.get_innermost_generic_args (t);
12833   for (i = 0; i < parms_num; i++)
12834     {
12835       tree parm, arg, arg_pack_elems;
12836
12837       parm = TREE_VEC_ELT (parms, i);
12838       arg = TREE_VEC_ELT (args, i);
12839       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12840       gcc_assert (parm && TREE_VALUE (parm) && arg);
12841
12842       if (parm && TREE_VALUE (parm) && arg)
12843         {
12844           /* If PARM represents a template parameter pack,
12845              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12846              by DW_TAG_template_*_parameter DIEs for the argument
12847              pack elements of ARG. Note that ARG would then be
12848              an argument pack.  */
12849           if (arg_pack_elems)
12850             template_parameter_pack_die (TREE_VALUE (parm),
12851                                          arg_pack_elems,
12852                                          die);
12853           else
12854             generic_parameter_die (TREE_VALUE (parm), arg,
12855                                    true /* Emit DW_AT_name */, die);
12856         }
12857     }
12858 }
12859
12860 /* Create and return a DIE for PARM which should be
12861    the representation of a generic type parameter.
12862    For instance, in the C++ front end, PARM would be a template parameter.
12863    ARG is the argument to PARM.
12864    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12865    name of the PARM.
12866    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12867    as a child node.  */
12868
12869 static dw_die_ref
12870 generic_parameter_die (tree parm, tree arg,
12871                        bool emit_name_p,
12872                        dw_die_ref parent_die)
12873 {
12874   dw_die_ref tmpl_die = NULL;
12875   const char *name = NULL;
12876
12877   if (!parm || !DECL_NAME (parm) || !arg)
12878     return NULL;
12879
12880   /* We support non-type generic parameters and arguments,
12881      type generic parameters and arguments, as well as
12882      generic generic parameters (a.k.a. template template parameters in C++)
12883      and arguments.  */
12884   if (TREE_CODE (parm) == PARM_DECL)
12885     /* PARM is a nontype generic parameter  */
12886     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12887   else if (TREE_CODE (parm) == TYPE_DECL)
12888     /* PARM is a type generic parameter.  */
12889     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12890   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12891     /* PARM is a generic generic parameter.
12892        Its DIE is a GNU extension. It shall have a
12893        DW_AT_name attribute to represent the name of the template template
12894        parameter, and a DW_AT_GNU_template_name attribute to represent the
12895        name of the template template argument.  */
12896     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12897                         parent_die, parm);
12898   else
12899     gcc_unreachable ();
12900
12901   if (tmpl_die)
12902     {
12903       tree tmpl_type;
12904
12905       /* If PARM is a generic parameter pack, it means we are
12906          emitting debug info for a template argument pack element.
12907          In other terms, ARG is a template argument pack element.
12908          In that case, we don't emit any DW_AT_name attribute for
12909          the die.  */
12910       if (emit_name_p)
12911         {
12912           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12913           gcc_assert (name);
12914           add_AT_string (tmpl_die, DW_AT_name, name);
12915         }
12916
12917       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12918         {
12919           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12920              TMPL_DIE should have a child DW_AT_type attribute that is set
12921              to the type of the argument to PARM, which is ARG.
12922              If PARM is a type generic parameter, TMPL_DIE should have a
12923              child DW_AT_type that is set to ARG.  */
12924           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12925           add_type_attribute (tmpl_die, tmpl_type, 0,
12926                               TREE_THIS_VOLATILE (tmpl_type),
12927                               parent_die);
12928         }
12929       else
12930         {
12931           /* So TMPL_DIE is a DIE representing a
12932              a generic generic template parameter, a.k.a template template
12933              parameter in C++ and arg is a template.  */
12934
12935           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12936              to the name of the argument.  */
12937           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12938           if (name)
12939             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12940         }
12941
12942       if (TREE_CODE (parm) == PARM_DECL)
12943         /* So PARM is a non-type generic parameter.
12944            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12945            attribute of TMPL_DIE which value represents the value
12946            of ARG.
12947            We must be careful here:
12948            The value of ARG might reference some function decls.
12949            We might currently be emitting debug info for a generic
12950            type and types are emitted before function decls, we don't
12951            know if the function decls referenced by ARG will actually be
12952            emitted after cgraph computations.
12953            So must defer the generation of the DW_AT_const_value to
12954            after cgraph is ready.  */
12955         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12956     }
12957
12958   return tmpl_die;
12959 }
12960
12961 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12962    PARM_PACK must be a template parameter pack. The returned DIE
12963    will be child DIE of PARENT_DIE.  */
12964
12965 static dw_die_ref
12966 template_parameter_pack_die (tree parm_pack,
12967                              tree parm_pack_args,
12968                              dw_die_ref parent_die)
12969 {
12970   dw_die_ref die;
12971   int j;
12972
12973   gcc_assert (parent_die && parm_pack);
12974
12975   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12976   add_name_and_src_coords_attributes (die, parm_pack);
12977   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12978     generic_parameter_die (parm_pack,
12979                            TREE_VEC_ELT (parm_pack_args, j),
12980                            false /* Don't emit DW_AT_name */,
12981                            die);
12982   return die;
12983 }
12984
12985 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12986    an enumerated type.  */
12987
12988 static inline int
12989 type_is_enum (const_tree type)
12990 {
12991   return TREE_CODE (type) == ENUMERAL_TYPE;
12992 }
12993
12994 /* Return the DBX register number described by a given RTL node.  */
12995
12996 static unsigned int
12997 dbx_reg_number (const_rtx rtl)
12998 {
12999   unsigned regno = REGNO (rtl);
13000
13001   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13002
13003 #ifdef LEAF_REG_REMAP
13004   if (current_function_uses_only_leaf_regs)
13005     {
13006       int leaf_reg = LEAF_REG_REMAP (regno);
13007       if (leaf_reg != -1)
13008         regno = (unsigned) leaf_reg;
13009     }
13010 #endif
13011
13012   return DBX_REGISTER_NUMBER (regno);
13013 }
13014
13015 /* Optionally add a DW_OP_piece term to a location description expression.
13016    DW_OP_piece is only added if the location description expression already
13017    doesn't end with DW_OP_piece.  */
13018
13019 static void
13020 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13021 {
13022   dw_loc_descr_ref loc;
13023
13024   if (*list_head != NULL)
13025     {
13026       /* Find the end of the chain.  */
13027       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13028         ;
13029
13030       if (loc->dw_loc_opc != DW_OP_piece)
13031         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13032     }
13033 }
13034
13035 /* Return a location descriptor that designates a machine register or
13036    zero if there is none.  */
13037
13038 static dw_loc_descr_ref
13039 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13040 {
13041   rtx regs;
13042
13043   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13044     return 0;
13045
13046   /* We only use "frame base" when we're sure we're talking about the
13047      post-prologue local stack frame.  We do this by *not* running
13048      register elimination until this point, and recognizing the special
13049      argument pointer and soft frame pointer rtx's.
13050      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13051   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13052       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13053     {
13054       dw_loc_descr_ref result = NULL;
13055
13056       if (dwarf_version >= 4 || !dwarf_strict)
13057         {
13058           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13059           if (result)
13060             add_loc_descr (&result,
13061                            new_loc_descr (DW_OP_stack_value, 0, 0));
13062         }
13063       return result;
13064     }
13065
13066   regs = targetm.dwarf_register_span (rtl);
13067
13068   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13069     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13070   else
13071     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13072 }
13073
13074 /* Return a location descriptor that designates a machine register for
13075    a given hard register number.  */
13076
13077 static dw_loc_descr_ref
13078 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13079 {
13080   dw_loc_descr_ref reg_loc_descr;
13081
13082   if (regno <= 31)
13083     reg_loc_descr
13084       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13085   else
13086     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13087
13088   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13089     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13090
13091   return reg_loc_descr;
13092 }
13093
13094 /* Given an RTL of a register, return a location descriptor that
13095    designates a value that spans more than one register.  */
13096
13097 static dw_loc_descr_ref
13098 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13099                              enum var_init_status initialized)
13100 {
13101   int nregs, size, i;
13102   unsigned reg;
13103   dw_loc_descr_ref loc_result = NULL;
13104
13105   reg = REGNO (rtl);
13106 #ifdef LEAF_REG_REMAP
13107   if (current_function_uses_only_leaf_regs)
13108     {
13109       int leaf_reg = LEAF_REG_REMAP (reg);
13110       if (leaf_reg != -1)
13111         reg = (unsigned) leaf_reg;
13112     }
13113 #endif
13114   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13115   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13116
13117   /* Simple, contiguous registers.  */
13118   if (regs == NULL_RTX)
13119     {
13120       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13121
13122       loc_result = NULL;
13123       while (nregs--)
13124         {
13125           dw_loc_descr_ref t;
13126
13127           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13128                                       VAR_INIT_STATUS_INITIALIZED);
13129           add_loc_descr (&loc_result, t);
13130           add_loc_descr_op_piece (&loc_result, size);
13131           ++reg;
13132         }
13133       return loc_result;
13134     }
13135
13136   /* Now onto stupid register sets in non contiguous locations.  */
13137
13138   gcc_assert (GET_CODE (regs) == PARALLEL);
13139
13140   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13141   loc_result = NULL;
13142
13143   for (i = 0; i < XVECLEN (regs, 0); ++i)
13144     {
13145       dw_loc_descr_ref t;
13146
13147       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13148                                   VAR_INIT_STATUS_INITIALIZED);
13149       add_loc_descr (&loc_result, t);
13150       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13151       add_loc_descr_op_piece (&loc_result, size);
13152     }
13153
13154   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13155     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13156   return loc_result;
13157 }
13158
13159 /* Return a location descriptor that designates a constant.  */
13160
13161 static dw_loc_descr_ref
13162 int_loc_descriptor (HOST_WIDE_INT i)
13163 {
13164   enum dwarf_location_atom op;
13165
13166   /* Pick the smallest representation of a constant, rather than just
13167      defaulting to the LEB encoding.  */
13168   if (i >= 0)
13169     {
13170       if (i <= 31)
13171         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13172       else if (i <= 0xff)
13173         op = DW_OP_const1u;
13174       else if (i <= 0xffff)
13175         op = DW_OP_const2u;
13176       else if (HOST_BITS_PER_WIDE_INT == 32
13177                || i <= 0xffffffff)
13178         op = DW_OP_const4u;
13179       else
13180         op = DW_OP_constu;
13181     }
13182   else
13183     {
13184       if (i >= -0x80)
13185         op = DW_OP_const1s;
13186       else if (i >= -0x8000)
13187         op = DW_OP_const2s;
13188       else if (HOST_BITS_PER_WIDE_INT == 32
13189                || i >= -0x80000000)
13190         op = DW_OP_const4s;
13191       else
13192         op = DW_OP_consts;
13193     }
13194
13195   return new_loc_descr (op, i, 0);
13196 }
13197
13198 /* Return loc description representing "address" of integer value.
13199    This can appear only as toplevel expression.  */
13200
13201 static dw_loc_descr_ref
13202 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13203 {
13204   int litsize;
13205   dw_loc_descr_ref loc_result = NULL;
13206
13207   if (!(dwarf_version >= 4 || !dwarf_strict))
13208     return NULL;
13209
13210   if (i >= 0)
13211     {
13212       if (i <= 31)
13213         litsize = 1;
13214       else if (i <= 0xff)
13215         litsize = 2;
13216       else if (i <= 0xffff)
13217         litsize = 3;
13218       else if (HOST_BITS_PER_WIDE_INT == 32
13219                || i <= 0xffffffff)
13220         litsize = 5;
13221       else
13222         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13223     }
13224   else
13225     {
13226       if (i >= -0x80)
13227         litsize = 2;
13228       else if (i >= -0x8000)
13229         litsize = 3;
13230       else if (HOST_BITS_PER_WIDE_INT == 32
13231                || i >= -0x80000000)
13232         litsize = 5;
13233       else
13234         litsize = 1 + size_of_sleb128 (i);
13235     }
13236   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13237      is more compact.  For DW_OP_stack_value we need:
13238      litsize + 1 (DW_OP_stack_value)
13239      and for DW_OP_implicit_value:
13240      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13241   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13242     {
13243       loc_result = int_loc_descriptor (i);
13244       add_loc_descr (&loc_result,
13245                      new_loc_descr (DW_OP_stack_value, 0, 0));
13246       return loc_result;
13247     }
13248
13249   loc_result = new_loc_descr (DW_OP_implicit_value,
13250                               size, 0);
13251   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13252   loc_result->dw_loc_oprnd2.v.val_int = i;
13253   return loc_result;
13254 }
13255
13256 /* Return a location descriptor that designates a base+offset location.  */
13257
13258 static dw_loc_descr_ref
13259 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13260                  enum var_init_status initialized)
13261 {
13262   unsigned int regno;
13263   dw_loc_descr_ref result;
13264   dw_fde_ref fde = current_fde ();
13265
13266   /* We only use "frame base" when we're sure we're talking about the
13267      post-prologue local stack frame.  We do this by *not* running
13268      register elimination until this point, and recognizing the special
13269      argument pointer and soft frame pointer rtx's.  */
13270   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13271     {
13272       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13273
13274       if (elim != reg)
13275         {
13276           if (GET_CODE (elim) == PLUS)
13277             {
13278               offset += INTVAL (XEXP (elim, 1));
13279               elim = XEXP (elim, 0);
13280             }
13281           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13282                        && (elim == hard_frame_pointer_rtx
13283                            || elim == stack_pointer_rtx))
13284                       || elim == (frame_pointer_needed
13285                                   ? hard_frame_pointer_rtx
13286                                   : stack_pointer_rtx));
13287
13288           /* If drap register is used to align stack, use frame
13289              pointer + offset to access stack variables.  If stack
13290              is aligned without drap, use stack pointer + offset to
13291              access stack variables.  */
13292           if (crtl->stack_realign_tried
13293               && reg == frame_pointer_rtx)
13294             {
13295               int base_reg
13296                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13297                                       ? HARD_FRAME_POINTER_REGNUM
13298                                       : STACK_POINTER_REGNUM);
13299               return new_reg_loc_descr (base_reg, offset);
13300             }
13301
13302           offset += frame_pointer_fb_offset;
13303           return new_loc_descr (DW_OP_fbreg, offset, 0);
13304         }
13305     }
13306   else if (!optimize
13307            && fde
13308            && (fde->drap_reg == REGNO (reg)
13309                || fde->vdrap_reg == REGNO (reg)))
13310     {
13311       /* Use cfa+offset to represent the location of arguments passed
13312          on the stack when drap is used to align stack.
13313          Only do this when not optimizing, for optimized code var-tracking
13314          is supposed to track where the arguments live and the register
13315          used as vdrap or drap in some spot might be used for something
13316          else in other part of the routine.  */
13317       return new_loc_descr (DW_OP_fbreg, offset, 0);
13318     }
13319
13320   regno = dbx_reg_number (reg);
13321   if (regno <= 31)
13322     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13323                             offset, 0);
13324   else
13325     result = new_loc_descr (DW_OP_bregx, regno, offset);
13326
13327   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13328     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13329
13330   return result;
13331 }
13332
13333 /* Return true if this RTL expression describes a base+offset calculation.  */
13334
13335 static inline int
13336 is_based_loc (const_rtx rtl)
13337 {
13338   return (GET_CODE (rtl) == PLUS
13339           && ((REG_P (XEXP (rtl, 0))
13340                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13341                && CONST_INT_P (XEXP (rtl, 1)))));
13342 }
13343
13344 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13345    failed.  */
13346
13347 static dw_loc_descr_ref
13348 tls_mem_loc_descriptor (rtx mem)
13349 {
13350   tree base;
13351   dw_loc_descr_ref loc_result;
13352
13353   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13354     return NULL;
13355
13356   base = get_base_address (MEM_EXPR (mem));
13357   if (base == NULL
13358       || TREE_CODE (base) != VAR_DECL
13359       || !DECL_THREAD_LOCAL_P (base))
13360     return NULL;
13361
13362   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13363   if (loc_result == NULL)
13364     return NULL;
13365
13366   if (INTVAL (MEM_OFFSET (mem)))
13367     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13368
13369   return loc_result;
13370 }
13371
13372 /* Output debug info about reason why we failed to expand expression as dwarf
13373    expression.  */
13374
13375 static void
13376 expansion_failed (tree expr, rtx rtl, char const *reason)
13377 {
13378   if (dump_file && (dump_flags & TDF_DETAILS))
13379     {
13380       fprintf (dump_file, "Failed to expand as dwarf: ");
13381       if (expr)
13382         print_generic_expr (dump_file, expr, dump_flags);
13383       if (rtl)
13384         {
13385           fprintf (dump_file, "\n");
13386           print_rtl (dump_file, rtl);
13387         }
13388       fprintf (dump_file, "\nReason: %s\n", reason);
13389     }
13390 }
13391
13392 /* Helper function for const_ok_for_output, called either directly
13393    or via for_each_rtx.  */
13394
13395 static int
13396 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13397 {
13398   rtx rtl = *rtlp;
13399
13400   if (GET_CODE (rtl) == UNSPEC)
13401     {
13402       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13403          we can't express it in the debug info.  */
13404 #ifdef ENABLE_CHECKING
13405       inform (current_function_decl
13406               ? DECL_SOURCE_LOCATION (current_function_decl)
13407               : UNKNOWN_LOCATION,
13408               "non-delegitimized UNSPEC %d found in variable location",
13409               XINT (rtl, 1));
13410 #endif
13411       expansion_failed (NULL_TREE, rtl,
13412                         "UNSPEC hasn't been delegitimized.\n");
13413       return 1;
13414     }
13415
13416   if (GET_CODE (rtl) != SYMBOL_REF)
13417     return 0;
13418
13419   if (CONSTANT_POOL_ADDRESS_P (rtl))
13420     {
13421       bool marked;
13422       get_pool_constant_mark (rtl, &marked);
13423       /* If all references to this pool constant were optimized away,
13424          it was not output and thus we can't represent it.  */
13425       if (!marked)
13426         {
13427           expansion_failed (NULL_TREE, rtl,
13428                             "Constant was removed from constant pool.\n");
13429           return 1;
13430         }
13431     }
13432
13433   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13434     return 1;
13435
13436   /* Avoid references to external symbols in debug info, on several targets
13437      the linker might even refuse to link when linking a shared library,
13438      and in many other cases the relocations for .debug_info/.debug_loc are
13439      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13440      to be defined within the same shared library or executable are fine.  */
13441   if (SYMBOL_REF_EXTERNAL_P (rtl))
13442     {
13443       tree decl = SYMBOL_REF_DECL (rtl);
13444
13445       if (decl == NULL || !targetm.binds_local_p (decl))
13446         {
13447           expansion_failed (NULL_TREE, rtl,
13448                             "Symbol not defined in current TU.\n");
13449           return 1;
13450         }
13451     }
13452
13453   return 0;
13454 }
13455
13456 /* Return true if constant RTL can be emitted in DW_OP_addr or
13457    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13458    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13459
13460 static bool
13461 const_ok_for_output (rtx rtl)
13462 {
13463   if (GET_CODE (rtl) == SYMBOL_REF)
13464     return const_ok_for_output_1 (&rtl, NULL) == 0;
13465
13466   if (GET_CODE (rtl) == CONST)
13467     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13468
13469   return true;
13470 }
13471
13472 /* The following routine converts the RTL for a variable or parameter
13473    (resident in memory) into an equivalent Dwarf representation of a
13474    mechanism for getting the address of that same variable onto the top of a
13475    hypothetical "address evaluation" stack.
13476
13477    When creating memory location descriptors, we are effectively transforming
13478    the RTL for a memory-resident object into its Dwarf postfix expression
13479    equivalent.  This routine recursively descends an RTL tree, turning
13480    it into Dwarf postfix code as it goes.
13481
13482    MODE is the mode of the memory reference, needed to handle some
13483    autoincrement addressing modes.
13484
13485    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13486    location list for RTL.
13487
13488    Return 0 if we can't represent the location.  */
13489
13490 static dw_loc_descr_ref
13491 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13492                     enum var_init_status initialized)
13493 {
13494   dw_loc_descr_ref mem_loc_result = NULL;
13495   enum dwarf_location_atom op;
13496   dw_loc_descr_ref op0, op1;
13497
13498   /* Note that for a dynamically sized array, the location we will generate a
13499      description of here will be the lowest numbered location which is
13500      actually within the array.  That's *not* necessarily the same as the
13501      zeroth element of the array.  */
13502
13503   rtl = targetm.delegitimize_address (rtl);
13504
13505   switch (GET_CODE (rtl))
13506     {
13507     case POST_INC:
13508     case POST_DEC:
13509     case POST_MODIFY:
13510       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13511
13512     case SUBREG:
13513       /* The case of a subreg may arise when we have a local (register)
13514          variable or a formal (register) parameter which doesn't quite fill
13515          up an entire register.  For now, just assume that it is
13516          legitimate to make the Dwarf info refer to the whole register which
13517          contains the given subreg.  */
13518       if (!subreg_lowpart_p (rtl))
13519         break;
13520       rtl = SUBREG_REG (rtl);
13521       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13522         break;
13523       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13524         break;
13525       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13526       break;
13527
13528     case REG:
13529       /* Whenever a register number forms a part of the description of the
13530          method for calculating the (dynamic) address of a memory resident
13531          object, DWARF rules require the register number be referred to as
13532          a "base register".  This distinction is not based in any way upon
13533          what category of register the hardware believes the given register
13534          belongs to.  This is strictly DWARF terminology we're dealing with
13535          here. Note that in cases where the location of a memory-resident
13536          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13537          OP_CONST (0)) the actual DWARF location descriptor that we generate
13538          may just be OP_BASEREG (basereg).  This may look deceptively like
13539          the object in question was allocated to a register (rather than in
13540          memory) so DWARF consumers need to be aware of the subtle
13541          distinction between OP_REG and OP_BASEREG.  */
13542       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13543         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13544       else if (stack_realign_drap
13545                && crtl->drap_reg
13546                && crtl->args.internal_arg_pointer == rtl
13547                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13548         {
13549           /* If RTL is internal_arg_pointer, which has been optimized
13550              out, use DRAP instead.  */
13551           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13552                                             VAR_INIT_STATUS_INITIALIZED);
13553         }
13554       break;
13555
13556     case SIGN_EXTEND:
13557     case ZERO_EXTEND:
13558       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13559                                 VAR_INIT_STATUS_INITIALIZED);
13560       if (op0 == 0)
13561         break;
13562       else
13563         {
13564           int shift = DWARF2_ADDR_SIZE
13565                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13566           shift *= BITS_PER_UNIT;
13567           if (GET_CODE (rtl) == SIGN_EXTEND)
13568             op = DW_OP_shra;
13569           else
13570             op = DW_OP_shr;
13571           mem_loc_result = op0;
13572           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13573           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13574           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13575           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13576         }
13577       break;
13578
13579     case MEM:
13580       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13581                                            VAR_INIT_STATUS_INITIALIZED);
13582       if (mem_loc_result == NULL)
13583         mem_loc_result = tls_mem_loc_descriptor (rtl);
13584       if (mem_loc_result != 0)
13585         {
13586           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13587             {
13588               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13589               return 0;
13590             }
13591           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13592             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13593           else
13594             add_loc_descr (&mem_loc_result,
13595                            new_loc_descr (DW_OP_deref_size,
13596                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13597         }
13598       else
13599         {
13600           rtx new_rtl = avoid_constant_pool_reference (rtl);
13601           if (new_rtl != rtl)
13602             return mem_loc_descriptor (new_rtl, mode, initialized);
13603         }
13604       break;
13605
13606     case LO_SUM:
13607          rtl = XEXP (rtl, 1);
13608
13609       /* ... fall through ...  */
13610
13611     case LABEL_REF:
13612       /* Some ports can transform a symbol ref into a label ref, because
13613          the symbol ref is too far away and has to be dumped into a constant
13614          pool.  */
13615     case CONST:
13616     case SYMBOL_REF:
13617       if (GET_CODE (rtl) == SYMBOL_REF
13618           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13619         {
13620           dw_loc_descr_ref temp;
13621
13622           /* If this is not defined, we have no way to emit the data.  */
13623           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13624             break;
13625
13626           /* We used to emit DW_OP_addr here, but that's wrong, since
13627              DW_OP_addr should be relocated by the debug info consumer,
13628              while DW_OP_GNU_push_tls_address operand should not.  */
13629           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13630                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13631           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13632           temp->dw_loc_oprnd1.v.val_addr = rtl;
13633           temp->dtprel = true;
13634
13635           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13636           add_loc_descr (&mem_loc_result, temp);
13637
13638           break;
13639         }
13640
13641       if (!const_ok_for_output (rtl))
13642         break;
13643
13644     symref:
13645       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13646       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13647       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13648       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13649       break;
13650
13651     case CONCAT:
13652     case CONCATN:
13653     case VAR_LOCATION:
13654       expansion_failed (NULL_TREE, rtl,
13655                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13656       return 0;
13657
13658     case PRE_MODIFY:
13659       /* Extract the PLUS expression nested inside and fall into
13660          PLUS code below.  */
13661       rtl = XEXP (rtl, 1);
13662       goto plus;
13663
13664     case PRE_INC:
13665     case PRE_DEC:
13666       /* Turn these into a PLUS expression and fall into the PLUS code
13667          below.  */
13668       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13669                           GEN_INT (GET_CODE (rtl) == PRE_INC
13670                                    ? GET_MODE_UNIT_SIZE (mode)
13671                                    : -GET_MODE_UNIT_SIZE (mode)));
13672
13673       /* ... fall through ...  */
13674
13675     case PLUS:
13676     plus:
13677       if (is_based_loc (rtl))
13678         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13679                                           INTVAL (XEXP (rtl, 1)),
13680                                           VAR_INIT_STATUS_INITIALIZED);
13681       else
13682         {
13683           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13684                                                VAR_INIT_STATUS_INITIALIZED);
13685           if (mem_loc_result == 0)
13686             break;
13687
13688           if (CONST_INT_P (XEXP (rtl, 1)))
13689             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13690           else
13691             {
13692               dw_loc_descr_ref mem_loc_result2
13693                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13694                                       VAR_INIT_STATUS_INITIALIZED);
13695               if (mem_loc_result2 == 0)
13696                 break;
13697               add_loc_descr (&mem_loc_result, mem_loc_result2);
13698               add_loc_descr (&mem_loc_result,
13699                              new_loc_descr (DW_OP_plus, 0, 0));
13700             }
13701         }
13702       break;
13703
13704     /* If a pseudo-reg is optimized away, it is possible for it to
13705        be replaced with a MEM containing a multiply or shift.  */
13706     case MINUS:
13707       op = DW_OP_minus;
13708       goto do_binop;
13709
13710     case MULT:
13711       op = DW_OP_mul;
13712       goto do_binop;
13713
13714     case DIV:
13715       op = DW_OP_div;
13716       goto do_binop;
13717
13718     case UMOD:
13719       op = DW_OP_mod;
13720       goto do_binop;
13721
13722     case ASHIFT:
13723       op = DW_OP_shl;
13724       goto do_binop;
13725
13726     case ASHIFTRT:
13727       op = DW_OP_shra;
13728       goto do_binop;
13729
13730     case LSHIFTRT:
13731       op = DW_OP_shr;
13732       goto do_binop;
13733
13734     case AND:
13735       op = DW_OP_and;
13736       goto do_binop;
13737
13738     case IOR:
13739       op = DW_OP_or;
13740       goto do_binop;
13741
13742     case XOR:
13743       op = DW_OP_xor;
13744       goto do_binop;
13745
13746     do_binop:
13747       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13748                                 VAR_INIT_STATUS_INITIALIZED);
13749       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13750                                 VAR_INIT_STATUS_INITIALIZED);
13751
13752       if (op0 == 0 || op1 == 0)
13753         break;
13754
13755       mem_loc_result = op0;
13756       add_loc_descr (&mem_loc_result, op1);
13757       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13758       break;
13759
13760     case MOD:
13761       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13762                                 VAR_INIT_STATUS_INITIALIZED);
13763       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13764                                 VAR_INIT_STATUS_INITIALIZED);
13765
13766       if (op0 == 0 || op1 == 0)
13767         break;
13768
13769       mem_loc_result = op0;
13770       add_loc_descr (&mem_loc_result, op1);
13771       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13772       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13773       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13774       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13775       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13776       break;
13777
13778     case NOT:
13779       op = DW_OP_not;
13780       goto do_unop;
13781
13782     case ABS:
13783       op = DW_OP_abs;
13784       goto do_unop;
13785
13786     case NEG:
13787       op = DW_OP_neg;
13788       goto do_unop;
13789
13790     do_unop:
13791       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13792                                 VAR_INIT_STATUS_INITIALIZED);
13793
13794       if (op0 == 0)
13795         break;
13796
13797       mem_loc_result = op0;
13798       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13799       break;
13800
13801     case CONST_INT:
13802       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13803       break;
13804
13805     case EQ:
13806       op = DW_OP_eq;
13807       goto do_scompare;
13808
13809     case GE:
13810       op = DW_OP_ge;
13811       goto do_scompare;
13812
13813     case GT:
13814       op = DW_OP_gt;
13815       goto do_scompare;
13816
13817     case LE:
13818       op = DW_OP_le;
13819       goto do_scompare;
13820
13821     case LT:
13822       op = DW_OP_lt;
13823       goto do_scompare;
13824
13825     case NE:
13826       op = DW_OP_ne;
13827       goto do_scompare;
13828
13829     do_scompare:
13830       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13831           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13832         break;
13833       else
13834         {
13835           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13836
13837           if (op_mode == VOIDmode)
13838             op_mode = GET_MODE (XEXP (rtl, 1));
13839           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13840             break;
13841
13842           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13843                                     VAR_INIT_STATUS_INITIALIZED);
13844           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13845                                     VAR_INIT_STATUS_INITIALIZED);
13846
13847           if (op0 == 0 || op1 == 0)
13848             break;
13849
13850           if (op_mode != VOIDmode
13851               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13852             {
13853               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13854               shift *= BITS_PER_UNIT;
13855               /* For eq/ne, if the operands are known to be zero-extended,
13856                  there is no need to do the fancy shifting up.  */
13857               if (op == DW_OP_eq || op == DW_OP_ne)
13858                 {
13859                   dw_loc_descr_ref last0, last1;
13860                   for (last0 = op0;
13861                        last0->dw_loc_next != NULL;
13862                        last0 = last0->dw_loc_next)
13863                     ;
13864                   for (last1 = op1;
13865                        last1->dw_loc_next != NULL;
13866                        last1 = last1->dw_loc_next)
13867                     ;
13868                   /* deref_size zero extends, and for constants we can check
13869                      whether they are zero extended or not.  */
13870                   if (((last0->dw_loc_opc == DW_OP_deref_size
13871                         && last0->dw_loc_oprnd1.v.val_int
13872                            <= GET_MODE_SIZE (op_mode))
13873                        || (CONST_INT_P (XEXP (rtl, 0))
13874                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13875                                == (INTVAL (XEXP (rtl, 0))
13876                                    & GET_MODE_MASK (op_mode))))
13877                       && ((last1->dw_loc_opc == DW_OP_deref_size
13878                            && last1->dw_loc_oprnd1.v.val_int
13879                               <= GET_MODE_SIZE (op_mode))
13880                           || (CONST_INT_P (XEXP (rtl, 1))
13881                               && (unsigned HOST_WIDE_INT)
13882                                  INTVAL (XEXP (rtl, 1))
13883                                  == (INTVAL (XEXP (rtl, 1))
13884                                      & GET_MODE_MASK (op_mode)))))
13885                     goto do_compare;
13886                 }
13887               add_loc_descr (&op0, int_loc_descriptor (shift));
13888               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13889               if (CONST_INT_P (XEXP (rtl, 1)))
13890                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13891               else
13892                 {
13893                   add_loc_descr (&op1, int_loc_descriptor (shift));
13894                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13895                 }
13896             }
13897         }
13898
13899     do_compare:
13900       mem_loc_result = op0;
13901       add_loc_descr (&mem_loc_result, op1);
13902       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13903       if (STORE_FLAG_VALUE != 1)
13904         {
13905           add_loc_descr (&mem_loc_result,
13906                          int_loc_descriptor (STORE_FLAG_VALUE));
13907           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13908         }
13909       break;
13910
13911     case GEU:
13912       op = DW_OP_ge;
13913       goto do_ucompare;
13914
13915     case GTU:
13916       op = DW_OP_gt;
13917       goto do_ucompare;
13918
13919     case LEU:
13920       op = DW_OP_le;
13921       goto do_ucompare;
13922
13923     case LTU:
13924       op = DW_OP_lt;
13925       goto do_ucompare;
13926
13927     do_ucompare:
13928       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13929           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13930         break;
13931       else
13932         {
13933           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13934
13935           if (op_mode == VOIDmode)
13936             op_mode = GET_MODE (XEXP (rtl, 1));
13937           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13938             break;
13939
13940           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13941                                     VAR_INIT_STATUS_INITIALIZED);
13942           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13943                                     VAR_INIT_STATUS_INITIALIZED);
13944
13945           if (op0 == 0 || op1 == 0)
13946             break;
13947
13948           if (op_mode != VOIDmode
13949               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13950             {
13951               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13952               dw_loc_descr_ref last0, last1;
13953               for (last0 = op0;
13954                    last0->dw_loc_next != NULL;
13955                    last0 = last0->dw_loc_next)
13956                 ;
13957               for (last1 = op1;
13958                    last1->dw_loc_next != NULL;
13959                    last1 = last1->dw_loc_next)
13960                 ;
13961               if (CONST_INT_P (XEXP (rtl, 0)))
13962                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13963               /* deref_size zero extends, so no need to mask it again.  */
13964               else if (last0->dw_loc_opc != DW_OP_deref_size
13965                        || last0->dw_loc_oprnd1.v.val_int
13966                           > GET_MODE_SIZE (op_mode))
13967                 {
13968                   add_loc_descr (&op0, int_loc_descriptor (mask));
13969                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13970                 }
13971               if (CONST_INT_P (XEXP (rtl, 1)))
13972                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13973               /* deref_size zero extends, so no need to mask it again.  */
13974               else if (last1->dw_loc_opc != DW_OP_deref_size
13975                        || last1->dw_loc_oprnd1.v.val_int
13976                           > GET_MODE_SIZE (op_mode))
13977                 {
13978                   add_loc_descr (&op1, int_loc_descriptor (mask));
13979                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13980                 }
13981             }
13982           else
13983             {
13984               HOST_WIDE_INT bias = 1;
13985               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13986               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13987               if (CONST_INT_P (XEXP (rtl, 1)))
13988                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13989                                           + INTVAL (XEXP (rtl, 1)));
13990               else
13991                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13992                                                     bias, 0));
13993             }
13994         }
13995       goto do_compare;
13996
13997     case SMIN:
13998     case SMAX:
13999     case UMIN:
14000     case UMAX:
14001       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14002           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14003           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14004         break;
14005
14006       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14007                                 VAR_INIT_STATUS_INITIALIZED);
14008       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14009                                 VAR_INIT_STATUS_INITIALIZED);
14010
14011       if (op0 == 0 || op1 == 0)
14012         break;
14013
14014       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14015       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14016       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14017       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14018         {
14019           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14020             {
14021               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14022               add_loc_descr (&op0, int_loc_descriptor (mask));
14023               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14024               add_loc_descr (&op1, int_loc_descriptor (mask));
14025               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14026             }
14027           else
14028             {
14029               HOST_WIDE_INT bias = 1;
14030               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14031               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14032               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14033             }
14034         }
14035       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14036         {
14037           int shift = DWARF2_ADDR_SIZE
14038                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14039           shift *= BITS_PER_UNIT;
14040           add_loc_descr (&op0, int_loc_descriptor (shift));
14041           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14042           add_loc_descr (&op1, int_loc_descriptor (shift));
14043           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14044         }
14045
14046       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14047         op = DW_OP_lt;
14048       else
14049         op = DW_OP_gt;
14050       mem_loc_result = op0;
14051       add_loc_descr (&mem_loc_result, op1);
14052       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14053       {
14054         dw_loc_descr_ref bra_node, drop_node;
14055
14056         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14057         add_loc_descr (&mem_loc_result, bra_node);
14058         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14059         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14060         add_loc_descr (&mem_loc_result, drop_node);
14061         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14062         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14063       }
14064       break;
14065
14066     case ZERO_EXTRACT:
14067     case SIGN_EXTRACT:
14068       if (CONST_INT_P (XEXP (rtl, 1))
14069           && CONST_INT_P (XEXP (rtl, 2))
14070           && ((unsigned) INTVAL (XEXP (rtl, 1))
14071               + (unsigned) INTVAL (XEXP (rtl, 2))
14072               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14073           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14074           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14075         {
14076           int shift, size;
14077           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14078                                     VAR_INIT_STATUS_INITIALIZED);
14079           if (op0 == 0)
14080             break;
14081           if (GET_CODE (rtl) == SIGN_EXTRACT)
14082             op = DW_OP_shra;
14083           else
14084             op = DW_OP_shr;
14085           mem_loc_result = op0;
14086           size = INTVAL (XEXP (rtl, 1));
14087           shift = INTVAL (XEXP (rtl, 2));
14088           if (BITS_BIG_ENDIAN)
14089             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14090                     - shift - size;
14091           if (shift + size != (int) DWARF2_ADDR_SIZE)
14092             {
14093               add_loc_descr (&mem_loc_result,
14094                              int_loc_descriptor (DWARF2_ADDR_SIZE
14095                                                  - shift - size));
14096               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14097             }
14098           if (size != (int) DWARF2_ADDR_SIZE)
14099             {
14100               add_loc_descr (&mem_loc_result,
14101                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14102               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14103             }
14104         }
14105       break;
14106
14107     case COMPARE:
14108     case IF_THEN_ELSE:
14109     case ROTATE:
14110     case ROTATERT:
14111     case TRUNCATE:
14112       /* In theory, we could implement the above.  */
14113       /* DWARF cannot represent the unsigned compare operations
14114          natively.  */
14115     case SS_MULT:
14116     case US_MULT:
14117     case SS_DIV:
14118     case US_DIV:
14119     case SS_PLUS:
14120     case US_PLUS:
14121     case SS_MINUS:
14122     case US_MINUS:
14123     case SS_NEG:
14124     case US_NEG:
14125     case SS_ABS:
14126     case SS_ASHIFT:
14127     case US_ASHIFT:
14128     case SS_TRUNCATE:
14129     case US_TRUNCATE:
14130     case UDIV:
14131     case UNORDERED:
14132     case ORDERED:
14133     case UNEQ:
14134     case UNGE:
14135     case UNGT:
14136     case UNLE:
14137     case UNLT:
14138     case LTGT:
14139     case FLOAT_EXTEND:
14140     case FLOAT_TRUNCATE:
14141     case FLOAT:
14142     case UNSIGNED_FLOAT:
14143     case FIX:
14144     case UNSIGNED_FIX:
14145     case FRACT_CONVERT:
14146     case UNSIGNED_FRACT_CONVERT:
14147     case SAT_FRACT:
14148     case UNSIGNED_SAT_FRACT:
14149     case SQRT:
14150     case BSWAP:
14151     case FFS:
14152     case CLZ:
14153     case CTZ:
14154     case POPCOUNT:
14155     case PARITY:
14156     case ASM_OPERANDS:
14157     case VEC_MERGE:
14158     case VEC_SELECT:
14159     case VEC_CONCAT:
14160     case VEC_DUPLICATE:
14161     case UNSPEC:
14162     case HIGH:
14163       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14164          can't express it in the debug info.  This can happen e.g. with some
14165          TLS UNSPECs.  */
14166       break;
14167
14168     case CONST_STRING:
14169       resolve_one_addr (&rtl, NULL);
14170       goto symref;
14171
14172     default:
14173 #ifdef ENABLE_CHECKING
14174       print_rtl (stderr, rtl);
14175       gcc_unreachable ();
14176 #else
14177       break;
14178 #endif
14179     }
14180
14181   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14182     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14183
14184   return mem_loc_result;
14185 }
14186
14187 /* Return a descriptor that describes the concatenation of two locations.
14188    This is typically a complex variable.  */
14189
14190 static dw_loc_descr_ref
14191 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14192 {
14193   dw_loc_descr_ref cc_loc_result = NULL;
14194   dw_loc_descr_ref x0_ref
14195     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14196   dw_loc_descr_ref x1_ref
14197     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14198
14199   if (x0_ref == 0 || x1_ref == 0)
14200     return 0;
14201
14202   cc_loc_result = x0_ref;
14203   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14204
14205   add_loc_descr (&cc_loc_result, x1_ref);
14206   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14207
14208   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14209     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14210
14211   return cc_loc_result;
14212 }
14213
14214 /* Return a descriptor that describes the concatenation of N
14215    locations.  */
14216
14217 static dw_loc_descr_ref
14218 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14219 {
14220   unsigned int i;
14221   dw_loc_descr_ref cc_loc_result = NULL;
14222   unsigned int n = XVECLEN (concatn, 0);
14223
14224   for (i = 0; i < n; ++i)
14225     {
14226       dw_loc_descr_ref ref;
14227       rtx x = XVECEXP (concatn, 0, i);
14228
14229       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14230       if (ref == NULL)
14231         return NULL;
14232
14233       add_loc_descr (&cc_loc_result, ref);
14234       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14235     }
14236
14237   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14238     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14239
14240   return cc_loc_result;
14241 }
14242
14243 /* Output a proper Dwarf location descriptor for a variable or parameter
14244    which is either allocated in a register or in a memory location.  For a
14245    register, we just generate an OP_REG and the register number.  For a
14246    memory location we provide a Dwarf postfix expression describing how to
14247    generate the (dynamic) address of the object onto the address stack.
14248
14249    MODE is mode of the decl if this loc_descriptor is going to be used in
14250    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14251    allowed, VOIDmode otherwise.
14252
14253    If we don't know how to describe it, return 0.  */
14254
14255 static dw_loc_descr_ref
14256 loc_descriptor (rtx rtl, enum machine_mode mode,
14257                 enum var_init_status initialized)
14258 {
14259   dw_loc_descr_ref loc_result = NULL;
14260
14261   switch (GET_CODE (rtl))
14262     {
14263     case SUBREG:
14264       /* The case of a subreg may arise when we have a local (register)
14265          variable or a formal (register) parameter which doesn't quite fill
14266          up an entire register.  For now, just assume that it is
14267          legitimate to make the Dwarf info refer to the whole register which
14268          contains the given subreg.  */
14269       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14270       break;
14271
14272     case REG:
14273       loc_result = reg_loc_descriptor (rtl, initialized);
14274       break;
14275
14276     case MEM:
14277       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14278                                        initialized);
14279       if (loc_result == NULL)
14280         loc_result = tls_mem_loc_descriptor (rtl);
14281       if (loc_result == NULL)
14282         {
14283           rtx new_rtl = avoid_constant_pool_reference (rtl);
14284           if (new_rtl != rtl)
14285             loc_result = loc_descriptor (new_rtl, mode, initialized);
14286         }
14287       break;
14288
14289     case CONCAT:
14290       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14291                                           initialized);
14292       break;
14293
14294     case CONCATN:
14295       loc_result = concatn_loc_descriptor (rtl, initialized);
14296       break;
14297
14298     case VAR_LOCATION:
14299       /* Single part.  */
14300       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14301         {
14302           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14303           if (GET_CODE (loc) == EXPR_LIST)
14304             loc = XEXP (loc, 0);
14305           loc_result = loc_descriptor (loc, mode, initialized);
14306           break;
14307         }
14308
14309       rtl = XEXP (rtl, 1);
14310       /* FALLTHRU */
14311
14312     case PARALLEL:
14313       {
14314         rtvec par_elems = XVEC (rtl, 0);
14315         int num_elem = GET_NUM_ELEM (par_elems);
14316         enum machine_mode mode;
14317         int i;
14318
14319         /* Create the first one, so we have something to add to.  */
14320         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14321                                      VOIDmode, initialized);
14322         if (loc_result == NULL)
14323           return NULL;
14324         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14325         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14326         for (i = 1; i < num_elem; i++)
14327           {
14328             dw_loc_descr_ref temp;
14329
14330             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14331                                    VOIDmode, initialized);
14332             if (temp == NULL)
14333               return NULL;
14334             add_loc_descr (&loc_result, temp);
14335             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14336             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14337           }
14338       }
14339       break;
14340
14341     case CONST_INT:
14342       if (mode != VOIDmode && mode != BLKmode)
14343         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14344                                                     INTVAL (rtl));
14345       break;
14346
14347     case CONST_DOUBLE:
14348       if (mode == VOIDmode)
14349         mode = GET_MODE (rtl);
14350
14351       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14352         {
14353           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14354
14355           /* Note that a CONST_DOUBLE rtx could represent either an integer
14356              or a floating-point constant.  A CONST_DOUBLE is used whenever
14357              the constant requires more than one word in order to be
14358              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14359           loc_result = new_loc_descr (DW_OP_implicit_value,
14360                                       GET_MODE_SIZE (mode), 0);
14361           if (SCALAR_FLOAT_MODE_P (mode))
14362             {
14363               unsigned int length = GET_MODE_SIZE (mode);
14364               unsigned char *array
14365                   = (unsigned char*) ggc_alloc_atomic (length);
14366
14367               insert_float (rtl, array);
14368               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14369               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14370               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14371               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14372             }
14373           else
14374             {
14375               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14376               loc_result->dw_loc_oprnd2.v.val_double
14377                 = rtx_to_double_int (rtl);
14378             }
14379         }
14380       break;
14381
14382     case CONST_VECTOR:
14383       if (mode == VOIDmode)
14384         mode = GET_MODE (rtl);
14385
14386       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14387         {
14388           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14389           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14390           unsigned char *array = (unsigned char *)
14391             ggc_alloc_atomic (length * elt_size);
14392           unsigned int i;
14393           unsigned char *p;
14394
14395           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14396           switch (GET_MODE_CLASS (mode))
14397             {
14398             case MODE_VECTOR_INT:
14399               for (i = 0, p = array; i < length; i++, p += elt_size)
14400                 {
14401                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14402                   double_int val = rtx_to_double_int (elt);
14403
14404                   if (elt_size <= sizeof (HOST_WIDE_INT))
14405                     insert_int (double_int_to_shwi (val), elt_size, p);
14406                   else
14407                     {
14408                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14409                       insert_double (val, p);
14410                     }
14411                 }
14412               break;
14413
14414             case MODE_VECTOR_FLOAT:
14415               for (i = 0, p = array; i < length; i++, p += elt_size)
14416                 {
14417                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14418                   insert_float (elt, p);
14419                 }
14420               break;
14421
14422             default:
14423               gcc_unreachable ();
14424             }
14425
14426           loc_result = new_loc_descr (DW_OP_implicit_value,
14427                                       length * elt_size, 0);
14428           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14429           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14430           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14431           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14432         }
14433       break;
14434
14435     case CONST:
14436       if (mode == VOIDmode
14437           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14438           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14439           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14440         {
14441           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14442           break;
14443         }
14444       /* FALLTHROUGH */
14445     case SYMBOL_REF:
14446       if (!const_ok_for_output (rtl))
14447         break;
14448     case LABEL_REF:
14449       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14450           && (dwarf_version >= 4 || !dwarf_strict))
14451         {
14452           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14453           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14454           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14455           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14456           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14457         }
14458       break;
14459
14460     default:
14461       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14462           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14463           && (dwarf_version >= 4 || !dwarf_strict))
14464         {
14465           /* Value expression.  */
14466           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14467           if (loc_result)
14468             add_loc_descr (&loc_result,
14469                            new_loc_descr (DW_OP_stack_value, 0, 0));
14470         }
14471       break;
14472     }
14473
14474   return loc_result;
14475 }
14476
14477 /* We need to figure out what section we should use as the base for the
14478    address ranges where a given location is valid.
14479    1. If this particular DECL has a section associated with it, use that.
14480    2. If this function has a section associated with it, use that.
14481    3. Otherwise, use the text section.
14482    XXX: If you split a variable across multiple sections, we won't notice.  */
14483
14484 static const char *
14485 secname_for_decl (const_tree decl)
14486 {
14487   const char *secname;
14488
14489   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14490     {
14491       tree sectree = DECL_SECTION_NAME (decl);
14492       secname = TREE_STRING_POINTER (sectree);
14493     }
14494   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14495     {
14496       tree sectree = DECL_SECTION_NAME (current_function_decl);
14497       secname = TREE_STRING_POINTER (sectree);
14498     }
14499   else if (cfun && in_cold_section_p)
14500     secname = crtl->subsections.cold_section_label;
14501   else
14502     secname = text_section_label;
14503
14504   return secname;
14505 }
14506
14507 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14508
14509 static bool
14510 decl_by_reference_p (tree decl)
14511 {
14512   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14513            || TREE_CODE (decl) == VAR_DECL)
14514           && DECL_BY_REFERENCE (decl));
14515 }
14516
14517 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14518    for VARLOC.  */
14519
14520 static dw_loc_descr_ref
14521 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14522                enum var_init_status initialized)
14523 {
14524   int have_address = 0;
14525   dw_loc_descr_ref descr;
14526   enum machine_mode mode;
14527
14528   if (want_address != 2)
14529     {
14530       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14531       /* Single part.  */
14532       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14533         {
14534           varloc = PAT_VAR_LOCATION_LOC (varloc);
14535           if (GET_CODE (varloc) == EXPR_LIST)
14536             varloc = XEXP (varloc, 0);
14537           mode = GET_MODE (varloc);
14538           if (MEM_P (varloc))
14539             {
14540               rtx addr = XEXP (varloc, 0);
14541               descr = mem_loc_descriptor (addr, mode, initialized);
14542               if (descr)
14543                 have_address = 1;
14544               else
14545                 {
14546                   rtx x = avoid_constant_pool_reference (varloc);
14547                   if (x != varloc)
14548                     descr = mem_loc_descriptor (x, mode, initialized);
14549                 }
14550             }
14551           else
14552             descr = mem_loc_descriptor (varloc, mode, initialized);
14553         }
14554       else
14555         return 0;
14556     }
14557   else
14558     {
14559       if (GET_CODE (varloc) == VAR_LOCATION)
14560         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14561       else
14562         mode = DECL_MODE (loc);
14563       descr = loc_descriptor (varloc, mode, initialized);
14564       have_address = 1;
14565     }
14566
14567   if (!descr)
14568     return 0;
14569
14570   if (want_address == 2 && !have_address
14571       && (dwarf_version >= 4 || !dwarf_strict))
14572     {
14573       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14574         {
14575           expansion_failed (loc, NULL_RTX,
14576                             "DWARF address size mismatch");
14577           return 0;
14578         }
14579       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14580       have_address = 1;
14581     }
14582   /* Show if we can't fill the request for an address.  */
14583   if (want_address && !have_address)
14584     {
14585       expansion_failed (loc, NULL_RTX,
14586                         "Want address and only have value");
14587       return 0;
14588     }
14589
14590   /* If we've got an address and don't want one, dereference.  */
14591   if (!want_address && have_address)
14592     {
14593       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14594       enum dwarf_location_atom op;
14595
14596       if (size > DWARF2_ADDR_SIZE || size == -1)
14597         {
14598           expansion_failed (loc, NULL_RTX,
14599                             "DWARF address size mismatch");
14600           return 0;
14601         }
14602       else if (size == DWARF2_ADDR_SIZE)
14603         op = DW_OP_deref;
14604       else
14605         op = DW_OP_deref_size;
14606
14607       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14608     }
14609
14610   return descr;
14611 }
14612
14613 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14614    if it is not possible.  */
14615
14616 static dw_loc_descr_ref
14617 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14618 {
14619   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14620     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14621   else if (dwarf_version >= 3 || !dwarf_strict)
14622     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14623   else
14624     return NULL;
14625 }
14626
14627 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14628    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14629
14630 static dw_loc_descr_ref
14631 dw_sra_loc_expr (tree decl, rtx loc)
14632 {
14633   rtx p;
14634   unsigned int padsize = 0;
14635   dw_loc_descr_ref descr, *descr_tail;
14636   unsigned HOST_WIDE_INT decl_size;
14637   rtx varloc;
14638   enum var_init_status initialized;
14639
14640   if (DECL_SIZE (decl) == NULL
14641       || !host_integerp (DECL_SIZE (decl), 1))
14642     return NULL;
14643
14644   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14645   descr = NULL;
14646   descr_tail = &descr;
14647
14648   for (p = loc; p; p = XEXP (p, 1))
14649     {
14650       unsigned int bitsize = decl_piece_bitsize (p);
14651       rtx loc_note = *decl_piece_varloc_ptr (p);
14652       dw_loc_descr_ref cur_descr;
14653       dw_loc_descr_ref *tail, last = NULL;
14654       unsigned int opsize = 0;
14655
14656       if (loc_note == NULL_RTX
14657           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14658         {
14659           padsize += bitsize;
14660           continue;
14661         }
14662       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14663       varloc = NOTE_VAR_LOCATION (loc_note);
14664       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14665       if (cur_descr == NULL)
14666         {
14667           padsize += bitsize;
14668           continue;
14669         }
14670
14671       /* Check that cur_descr either doesn't use
14672          DW_OP_*piece operations, or their sum is equal
14673          to bitsize.  Otherwise we can't embed it.  */
14674       for (tail = &cur_descr; *tail != NULL;
14675            tail = &(*tail)->dw_loc_next)
14676         if ((*tail)->dw_loc_opc == DW_OP_piece)
14677           {
14678             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14679                       * BITS_PER_UNIT;
14680             last = *tail;
14681           }
14682         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14683           {
14684             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14685             last = *tail;
14686           }
14687
14688       if (last != NULL && opsize != bitsize)
14689         {
14690           padsize += bitsize;
14691           continue;
14692         }
14693
14694       /* If there is a hole, add DW_OP_*piece after empty DWARF
14695          expression, which means that those bits are optimized out.  */
14696       if (padsize)
14697         {
14698           if (padsize > decl_size)
14699             return NULL;
14700           decl_size -= padsize;
14701           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14702           if (*descr_tail == NULL)
14703             return NULL;
14704           descr_tail = &(*descr_tail)->dw_loc_next;
14705           padsize = 0;
14706         }
14707       *descr_tail = cur_descr;
14708       descr_tail = tail;
14709       if (bitsize > decl_size)
14710         return NULL;
14711       decl_size -= bitsize;
14712       if (last == NULL)
14713         {
14714           HOST_WIDE_INT offset = 0;
14715           if (GET_CODE (varloc) == VAR_LOCATION
14716               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14717             {
14718               varloc = PAT_VAR_LOCATION_LOC (varloc);
14719               if (GET_CODE (varloc) == EXPR_LIST)
14720                 varloc = XEXP (varloc, 0);
14721             }
14722           do 
14723             {
14724               if (GET_CODE (varloc) == CONST
14725                   || GET_CODE (varloc) == SIGN_EXTEND
14726                   || GET_CODE (varloc) == ZERO_EXTEND)
14727                 varloc = XEXP (varloc, 0);
14728               else if (GET_CODE (varloc) == SUBREG)
14729                 varloc = SUBREG_REG (varloc);
14730               else
14731                 break;
14732             }
14733           while (1);
14734           /* DW_OP_bit_size offset should be zero for register
14735              or implicit location descriptions and empty location
14736              descriptions, but for memory addresses needs big endian
14737              adjustment.  */
14738           if (MEM_P (varloc))
14739             {
14740               unsigned HOST_WIDE_INT memsize
14741                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14742               if (memsize != bitsize)
14743                 {
14744                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14745                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14746                     return NULL;
14747                   if (memsize < bitsize)
14748                     return NULL;
14749                   if (BITS_BIG_ENDIAN)
14750                     offset = memsize - bitsize;
14751                 }
14752             }
14753
14754           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14755           if (*descr_tail == NULL)
14756             return NULL;
14757           descr_tail = &(*descr_tail)->dw_loc_next;
14758         }
14759     }
14760
14761   /* If there were any non-empty expressions, add padding till the end of
14762      the decl.  */
14763   if (descr != NULL && decl_size != 0)
14764     {
14765       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14766       if (*descr_tail == NULL)
14767         return NULL;
14768     }
14769   return descr;
14770 }
14771
14772 /* Return the dwarf representation of the location list LOC_LIST of
14773    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14774    function.  */
14775
14776 static dw_loc_list_ref
14777 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14778 {
14779   const char *endname, *secname;
14780   rtx varloc;
14781   enum var_init_status initialized;
14782   struct var_loc_node *node;
14783   dw_loc_descr_ref descr;
14784   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14785   dw_loc_list_ref list = NULL;
14786   dw_loc_list_ref *listp = &list;
14787
14788   /* Now that we know what section we are using for a base,
14789      actually construct the list of locations.
14790      The first location information is what is passed to the
14791      function that creates the location list, and the remaining
14792      locations just get added on to that list.
14793      Note that we only know the start address for a location
14794      (IE location changes), so to build the range, we use
14795      the range [current location start, next location start].
14796      This means we have to special case the last node, and generate
14797      a range of [last location start, end of function label].  */
14798
14799   secname = secname_for_decl (decl);
14800
14801   for (node = loc_list->first; node; node = node->next)
14802     if (GET_CODE (node->loc) == EXPR_LIST
14803         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14804       {
14805         if (GET_CODE (node->loc) == EXPR_LIST)
14806           {
14807             /* This requires DW_OP_{,bit_}piece, which is not usable
14808                inside DWARF expressions.  */
14809             if (want_address != 2)
14810               continue;
14811             descr = dw_sra_loc_expr (decl, node->loc);
14812             if (descr == NULL)
14813               continue;
14814           }
14815         else
14816           {
14817             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14818             varloc = NOTE_VAR_LOCATION (node->loc);
14819             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14820           }
14821         if (descr)
14822           {
14823             /* The variable has a location between NODE->LABEL and
14824                NODE->NEXT->LABEL.  */
14825             if (node->next)
14826               endname = node->next->label;
14827             /* If the variable has a location at the last label
14828                it keeps its location until the end of function.  */
14829             else if (!current_function_decl)
14830               endname = text_end_label;
14831             else
14832               {
14833                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14834                                              current_function_funcdef_no);
14835                 endname = ggc_strdup (label_id);
14836               }
14837
14838             *listp = new_loc_list (descr, node->label, endname, secname);
14839             listp = &(*listp)->dw_loc_next;
14840           }
14841       }
14842
14843   /* Try to avoid the overhead of a location list emitting a location
14844      expression instead, but only if we didn't have more than one
14845      location entry in the first place.  If some entries were not
14846      representable, we don't want to pretend a single entry that was
14847      applies to the entire scope in which the variable is
14848      available.  */
14849   if (list && loc_list->first->next)
14850     gen_llsym (list);
14851
14852   return list;
14853 }
14854
14855 /* Return if the loc_list has only single element and thus can be represented
14856    as location description.   */
14857
14858 static bool
14859 single_element_loc_list_p (dw_loc_list_ref list)
14860 {
14861   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14862   return !list->ll_symbol;
14863 }
14864
14865 /* To each location in list LIST add loc descr REF.  */
14866
14867 static void
14868 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14869 {
14870   dw_loc_descr_ref copy;
14871   add_loc_descr (&list->expr, ref);
14872   list = list->dw_loc_next;
14873   while (list)
14874     {
14875       copy = ggc_alloc_dw_loc_descr_node ();
14876       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14877       add_loc_descr (&list->expr, copy);
14878       while (copy->dw_loc_next)
14879         {
14880           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14881           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14882           copy->dw_loc_next = new_copy;
14883           copy = new_copy;
14884         }
14885       list = list->dw_loc_next;
14886     }
14887 }
14888
14889 /* Given two lists RET and LIST
14890    produce location list that is result of adding expression in LIST
14891    to expression in RET on each possition in program.
14892    Might be destructive on both RET and LIST.
14893
14894    TODO: We handle only simple cases of RET or LIST having at most one
14895    element. General case would inolve sorting the lists in program order
14896    and merging them that will need some additional work.
14897    Adding that will improve quality of debug info especially for SRA-ed
14898    structures.  */
14899
14900 static void
14901 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14902 {
14903   if (!list)
14904     return;
14905   if (!*ret)
14906     {
14907       *ret = list;
14908       return;
14909     }
14910   if (!list->dw_loc_next)
14911     {
14912       add_loc_descr_to_each (*ret, list->expr);
14913       return;
14914     }
14915   if (!(*ret)->dw_loc_next)
14916     {
14917       add_loc_descr_to_each (list, (*ret)->expr);
14918       *ret = list;
14919       return;
14920     }
14921   expansion_failed (NULL_TREE, NULL_RTX,
14922                     "Don't know how to merge two non-trivial"
14923                     " location lists.\n");
14924   *ret = NULL;
14925   return;
14926 }
14927
14928 /* LOC is constant expression.  Try a luck, look it up in constant
14929    pool and return its loc_descr of its address.  */
14930
14931 static dw_loc_descr_ref
14932 cst_pool_loc_descr (tree loc)
14933 {
14934   /* Get an RTL for this, if something has been emitted.  */
14935   rtx rtl = lookup_constant_def (loc);
14936   enum machine_mode mode;
14937
14938   if (!rtl || !MEM_P (rtl))
14939     {
14940       gcc_assert (!rtl);
14941       return 0;
14942     }
14943   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14944
14945   /* TODO: We might get more coverage if we was actually delaying expansion
14946      of all expressions till end of compilation when constant pools are fully
14947      populated.  */
14948   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14949     {
14950       expansion_failed (loc, NULL_RTX,
14951                         "CST value in contant pool but not marked.");
14952       return 0;
14953     }
14954   mode = GET_MODE (rtl);
14955   rtl = XEXP (rtl, 0);
14956   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14957 }
14958
14959 /* Return dw_loc_list representing address of addr_expr LOC
14960    by looking for innder INDIRECT_REF expression and turing it
14961    into simple arithmetics.  */
14962
14963 static dw_loc_list_ref
14964 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14965 {
14966   tree obj, offset;
14967   HOST_WIDE_INT bitsize, bitpos, bytepos;
14968   enum machine_mode mode;
14969   int volatilep;
14970   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14971   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14972
14973   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14974                              &bitsize, &bitpos, &offset, &mode,
14975                              &unsignedp, &volatilep, false);
14976   STRIP_NOPS (obj);
14977   if (bitpos % BITS_PER_UNIT)
14978     {
14979       expansion_failed (loc, NULL_RTX, "bitfield access");
14980       return 0;
14981     }
14982   if (!INDIRECT_REF_P (obj))
14983     {
14984       expansion_failed (obj,
14985                         NULL_RTX, "no indirect ref in inner refrence");
14986       return 0;
14987     }
14988   if (!offset && !bitpos)
14989     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14990   else if (toplev
14991            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14992            && (dwarf_version >= 4 || !dwarf_strict))
14993     {
14994       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14995       if (!list_ret)
14996         return 0;
14997       if (offset)
14998         {
14999           /* Variable offset.  */
15000           list_ret1 = loc_list_from_tree (offset, 0);
15001           if (list_ret1 == 0)
15002             return 0;
15003           add_loc_list (&list_ret, list_ret1);
15004           if (!list_ret)
15005             return 0;
15006           add_loc_descr_to_each (list_ret,
15007                                  new_loc_descr (DW_OP_plus, 0, 0));
15008         }
15009       bytepos = bitpos / BITS_PER_UNIT;
15010       if (bytepos > 0)
15011         add_loc_descr_to_each (list_ret,
15012                                new_loc_descr (DW_OP_plus_uconst,
15013                                               bytepos, 0));
15014       else if (bytepos < 0)
15015         loc_list_plus_const (list_ret, bytepos);
15016       add_loc_descr_to_each (list_ret,
15017                              new_loc_descr (DW_OP_stack_value, 0, 0));
15018     }
15019   return list_ret;
15020 }
15021
15022
15023 /* Generate Dwarf location list representing LOC.
15024    If WANT_ADDRESS is false, expression computing LOC will be computed
15025    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15026    if WANT_ADDRESS is 2, expression computing address useable in location
15027      will be returned (i.e. DW_OP_reg can be used
15028      to refer to register values).  */
15029
15030 static dw_loc_list_ref
15031 loc_list_from_tree (tree loc, int want_address)
15032 {
15033   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15034   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15035   int have_address = 0;
15036   enum dwarf_location_atom op;
15037
15038   /* ??? Most of the time we do not take proper care for sign/zero
15039      extending the values properly.  Hopefully this won't be a real
15040      problem...  */
15041
15042   switch (TREE_CODE (loc))
15043     {
15044     case ERROR_MARK:
15045       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15046       return 0;
15047
15048     case PLACEHOLDER_EXPR:
15049       /* This case involves extracting fields from an object to determine the
15050          position of other fields.  We don't try to encode this here.  The
15051          only user of this is Ada, which encodes the needed information using
15052          the names of types.  */
15053       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15054       return 0;
15055
15056     case CALL_EXPR:
15057       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15058       /* There are no opcodes for these operations.  */
15059       return 0;
15060
15061     case PREINCREMENT_EXPR:
15062     case PREDECREMENT_EXPR:
15063     case POSTINCREMENT_EXPR:
15064     case POSTDECREMENT_EXPR:
15065       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15066       /* There are no opcodes for these operations.  */
15067       return 0;
15068
15069     case ADDR_EXPR:
15070       /* If we already want an address, see if there is INDIRECT_REF inside
15071          e.g. for &this->field.  */
15072       if (want_address)
15073         {
15074           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15075                        (loc, want_address == 2);
15076           if (list_ret)
15077             have_address = 1;
15078           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15079                    && (ret = cst_pool_loc_descr (loc)))
15080             have_address = 1;
15081         }
15082         /* Otherwise, process the argument and look for the address.  */
15083       if (!list_ret && !ret)
15084         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15085       else
15086         {
15087           if (want_address)
15088             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15089           return NULL;
15090         }
15091       break;
15092
15093     case VAR_DECL:
15094       if (DECL_THREAD_LOCAL_P (loc))
15095         {
15096           rtx rtl;
15097           enum dwarf_location_atom first_op;
15098           enum dwarf_location_atom second_op;
15099           bool dtprel = false;
15100
15101           if (targetm.have_tls)
15102             {
15103               /* If this is not defined, we have no way to emit the
15104                  data.  */
15105               if (!targetm.asm_out.output_dwarf_dtprel)
15106                 return 0;
15107
15108                /* The way DW_OP_GNU_push_tls_address is specified, we
15109                   can only look up addresses of objects in the current
15110                   module.  We used DW_OP_addr as first op, but that's
15111                   wrong, because DW_OP_addr is relocated by the debug
15112                   info consumer, while DW_OP_GNU_push_tls_address
15113                   operand shouldn't be.  */
15114               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15115                 return 0;
15116               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15117               dtprel = true;
15118               second_op = DW_OP_GNU_push_tls_address;
15119             }
15120           else
15121             {
15122               if (!targetm.emutls.debug_form_tls_address
15123                   || !(dwarf_version >= 3 || !dwarf_strict))
15124                 return 0;
15125               /* We stuffed the control variable into the DECL_VALUE_EXPR
15126                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15127                  no longer appear in gimple code.  We used the control
15128                  variable in specific so that we could pick it up here.  */
15129               loc = DECL_VALUE_EXPR (loc);
15130               first_op = DW_OP_addr;
15131               second_op = DW_OP_form_tls_address;
15132             }
15133
15134           rtl = rtl_for_decl_location (loc);
15135           if (rtl == NULL_RTX)
15136             return 0;
15137
15138           if (!MEM_P (rtl))
15139             return 0;
15140           rtl = XEXP (rtl, 0);
15141           if (! CONSTANT_P (rtl))
15142             return 0;
15143
15144           ret = new_loc_descr (first_op, 0, 0);
15145           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15146           ret->dw_loc_oprnd1.v.val_addr = rtl;
15147           ret->dtprel = dtprel;
15148
15149           ret1 = new_loc_descr (second_op, 0, 0);
15150           add_loc_descr (&ret, ret1);
15151
15152           have_address = 1;
15153           break;
15154         }
15155       /* FALLTHRU */
15156
15157     case PARM_DECL:
15158       if (DECL_HAS_VALUE_EXPR_P (loc))
15159         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15160                                    want_address);
15161       /* FALLTHRU */
15162
15163     case RESULT_DECL:
15164     case FUNCTION_DECL:
15165       {
15166         rtx rtl;
15167         var_loc_list *loc_list = lookup_decl_loc (loc);
15168
15169         if (loc_list && loc_list->first)
15170           {
15171             list_ret = dw_loc_list (loc_list, loc, want_address);
15172             have_address = want_address != 0;
15173             break;
15174           }
15175         rtl = rtl_for_decl_location (loc);
15176         if (rtl == NULL_RTX)
15177           {
15178             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15179             return 0;
15180           }
15181         else if (CONST_INT_P (rtl))
15182           {
15183             HOST_WIDE_INT val = INTVAL (rtl);
15184             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15185               val &= GET_MODE_MASK (DECL_MODE (loc));
15186             ret = int_loc_descriptor (val);
15187           }
15188         else if (GET_CODE (rtl) == CONST_STRING)
15189           {
15190             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15191             return 0;
15192           }
15193         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15194           {
15195             ret = new_loc_descr (DW_OP_addr, 0, 0);
15196             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15197             ret->dw_loc_oprnd1.v.val_addr = rtl;
15198           }
15199         else
15200           {
15201             enum machine_mode mode;
15202
15203             /* Certain constructs can only be represented at top-level.  */
15204             if (want_address == 2)
15205               {
15206                 ret = loc_descriptor (rtl, VOIDmode,
15207                                       VAR_INIT_STATUS_INITIALIZED);
15208                 have_address = 1;
15209               }
15210             else
15211               {
15212                 mode = GET_MODE (rtl);
15213                 if (MEM_P (rtl))
15214                   {
15215                     rtl = XEXP (rtl, 0);
15216                     have_address = 1;
15217                   }
15218                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15219               }
15220             if (!ret)
15221               expansion_failed (loc, rtl,
15222                                 "failed to produce loc descriptor for rtl");
15223           }
15224       }
15225       break;
15226
15227     case MEM_REF:
15228       /* ??? FIXME.  */
15229       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15230         return 0;
15231       /* Fallthru.  */
15232     case INDIRECT_REF:
15233     case MISALIGNED_INDIRECT_REF:
15234       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15235       have_address = 1;
15236       break;
15237
15238     case COMPOUND_EXPR:
15239       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15240
15241     CASE_CONVERT:
15242     case VIEW_CONVERT_EXPR:
15243     case SAVE_EXPR:
15244     case MODIFY_EXPR:
15245       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15246
15247     case COMPONENT_REF:
15248     case BIT_FIELD_REF:
15249     case ARRAY_REF:
15250     case ARRAY_RANGE_REF:
15251     case REALPART_EXPR:
15252     case IMAGPART_EXPR:
15253       {
15254         tree obj, offset;
15255         HOST_WIDE_INT bitsize, bitpos, bytepos;
15256         enum machine_mode mode;
15257         int volatilep;
15258         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15259
15260         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15261                                    &unsignedp, &volatilep, false);
15262
15263         gcc_assert (obj != loc);
15264
15265         list_ret = loc_list_from_tree (obj,
15266                                        want_address == 2
15267                                        && !bitpos && !offset ? 2 : 1);
15268         /* TODO: We can extract value of the small expression via shifting even
15269            for nonzero bitpos.  */
15270         if (list_ret == 0)
15271           return 0;
15272         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15273           {
15274             expansion_failed (loc, NULL_RTX,
15275                               "bitfield access");
15276             return 0;
15277           }
15278
15279         if (offset != NULL_TREE)
15280           {
15281             /* Variable offset.  */
15282             list_ret1 = loc_list_from_tree (offset, 0);
15283             if (list_ret1 == 0)
15284               return 0;
15285             add_loc_list (&list_ret, list_ret1);
15286             if (!list_ret)
15287               return 0;
15288             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15289           }
15290
15291         bytepos = bitpos / BITS_PER_UNIT;
15292         if (bytepos > 0)
15293           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15294         else if (bytepos < 0)
15295           loc_list_plus_const (list_ret, bytepos);
15296
15297         have_address = 1;
15298         break;
15299       }
15300
15301     case INTEGER_CST:
15302       if ((want_address || !host_integerp (loc, 0))
15303           && (ret = cst_pool_loc_descr (loc)))
15304         have_address = 1;
15305       else if (want_address == 2
15306                && host_integerp (loc, 0)
15307                && (ret = address_of_int_loc_descriptor
15308                            (int_size_in_bytes (TREE_TYPE (loc)),
15309                             tree_low_cst (loc, 0))))
15310         have_address = 1;
15311       else if (host_integerp (loc, 0))
15312         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15313       else
15314         {
15315           expansion_failed (loc, NULL_RTX,
15316                             "Integer operand is not host integer");
15317           return 0;
15318         }
15319       break;
15320
15321     case CONSTRUCTOR:
15322     case REAL_CST:
15323     case STRING_CST:
15324     case COMPLEX_CST:
15325       if ((ret = cst_pool_loc_descr (loc)))
15326         have_address = 1;
15327       else
15328       /* We can construct small constants here using int_loc_descriptor.  */
15329         expansion_failed (loc, NULL_RTX,
15330                           "constructor or constant not in constant pool");
15331       break;
15332
15333     case TRUTH_AND_EXPR:
15334     case TRUTH_ANDIF_EXPR:
15335     case BIT_AND_EXPR:
15336       op = DW_OP_and;
15337       goto do_binop;
15338
15339     case TRUTH_XOR_EXPR:
15340     case BIT_XOR_EXPR:
15341       op = DW_OP_xor;
15342       goto do_binop;
15343
15344     case TRUTH_OR_EXPR:
15345     case TRUTH_ORIF_EXPR:
15346     case BIT_IOR_EXPR:
15347       op = DW_OP_or;
15348       goto do_binop;
15349
15350     case FLOOR_DIV_EXPR:
15351     case CEIL_DIV_EXPR:
15352     case ROUND_DIV_EXPR:
15353     case TRUNC_DIV_EXPR:
15354       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15355         return 0;
15356       op = DW_OP_div;
15357       goto do_binop;
15358
15359     case MINUS_EXPR:
15360       op = DW_OP_minus;
15361       goto do_binop;
15362
15363     case FLOOR_MOD_EXPR:
15364     case CEIL_MOD_EXPR:
15365     case ROUND_MOD_EXPR:
15366     case TRUNC_MOD_EXPR:
15367       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15368         {
15369           op = DW_OP_mod;
15370           goto do_binop;
15371         }
15372       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15373       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15374       if (list_ret == 0 || list_ret1 == 0)
15375         return 0;
15376
15377       add_loc_list (&list_ret, list_ret1);
15378       if (list_ret == 0)
15379         return 0;
15380       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15381       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15382       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15383       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15384       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15385       break;
15386
15387     case MULT_EXPR:
15388       op = DW_OP_mul;
15389       goto do_binop;
15390
15391     case LSHIFT_EXPR:
15392       op = DW_OP_shl;
15393       goto do_binop;
15394
15395     case RSHIFT_EXPR:
15396       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15397       goto do_binop;
15398
15399     case POINTER_PLUS_EXPR:
15400     case PLUS_EXPR:
15401       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15402         {
15403           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15404           if (list_ret == 0)
15405             return 0;
15406
15407           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15408           break;
15409         }
15410
15411       op = DW_OP_plus;
15412       goto do_binop;
15413
15414     case LE_EXPR:
15415       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15416         return 0;
15417
15418       op = DW_OP_le;
15419       goto do_binop;
15420
15421     case GE_EXPR:
15422       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15423         return 0;
15424
15425       op = DW_OP_ge;
15426       goto do_binop;
15427
15428     case LT_EXPR:
15429       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15430         return 0;
15431
15432       op = DW_OP_lt;
15433       goto do_binop;
15434
15435     case GT_EXPR:
15436       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15437         return 0;
15438
15439       op = DW_OP_gt;
15440       goto do_binop;
15441
15442     case EQ_EXPR:
15443       op = DW_OP_eq;
15444       goto do_binop;
15445
15446     case NE_EXPR:
15447       op = DW_OP_ne;
15448       goto do_binop;
15449
15450     do_binop:
15451       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15452       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15453       if (list_ret == 0 || list_ret1 == 0)
15454         return 0;
15455
15456       add_loc_list (&list_ret, list_ret1);
15457       if (list_ret == 0)
15458         return 0;
15459       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15460       break;
15461
15462     case TRUTH_NOT_EXPR:
15463     case BIT_NOT_EXPR:
15464       op = DW_OP_not;
15465       goto do_unop;
15466
15467     case ABS_EXPR:
15468       op = DW_OP_abs;
15469       goto do_unop;
15470
15471     case NEGATE_EXPR:
15472       op = DW_OP_neg;
15473       goto do_unop;
15474
15475     do_unop:
15476       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15477       if (list_ret == 0)
15478         return 0;
15479
15480       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15481       break;
15482
15483     case MIN_EXPR:
15484     case MAX_EXPR:
15485       {
15486         const enum tree_code code =
15487           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15488
15489         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15490                       build2 (code, integer_type_node,
15491                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15492                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15493       }
15494
15495       /* ... fall through ...  */
15496
15497     case COND_EXPR:
15498       {
15499         dw_loc_descr_ref lhs
15500           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15501         dw_loc_list_ref rhs
15502           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15503         dw_loc_descr_ref bra_node, jump_node, tmp;
15504
15505         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15506         if (list_ret == 0 || lhs == 0 || rhs == 0)
15507           return 0;
15508
15509         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15510         add_loc_descr_to_each (list_ret, bra_node);
15511
15512         add_loc_list (&list_ret, rhs);
15513         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15514         add_loc_descr_to_each (list_ret, jump_node);
15515
15516         add_loc_descr_to_each (list_ret, lhs);
15517         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15518         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15519
15520         /* ??? Need a node to point the skip at.  Use a nop.  */
15521         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15522         add_loc_descr_to_each (list_ret, tmp);
15523         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15524         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15525       }
15526       break;
15527
15528     case FIX_TRUNC_EXPR:
15529       return 0;
15530
15531     default:
15532       /* Leave front-end specific codes as simply unknown.  This comes
15533          up, for instance, with the C STMT_EXPR.  */
15534       if ((unsigned int) TREE_CODE (loc)
15535           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15536         {
15537           expansion_failed (loc, NULL_RTX,
15538                             "language specific tree node");
15539           return 0;
15540         }
15541
15542 #ifdef ENABLE_CHECKING
15543       /* Otherwise this is a generic code; we should just lists all of
15544          these explicitly.  We forgot one.  */
15545       gcc_unreachable ();
15546 #else
15547       /* In a release build, we want to degrade gracefully: better to
15548          generate incomplete debugging information than to crash.  */
15549       return NULL;
15550 #endif
15551     }
15552
15553   if (!ret && !list_ret)
15554     return 0;
15555
15556   if (want_address == 2 && !have_address
15557       && (dwarf_version >= 4 || !dwarf_strict))
15558     {
15559       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15560         {
15561           expansion_failed (loc, NULL_RTX,
15562                             "DWARF address size mismatch");
15563           return 0;
15564         }
15565       if (ret)
15566         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15567       else
15568         add_loc_descr_to_each (list_ret,
15569                                new_loc_descr (DW_OP_stack_value, 0, 0));
15570       have_address = 1;
15571     }
15572   /* Show if we can't fill the request for an address.  */
15573   if (want_address && !have_address)
15574     {
15575       expansion_failed (loc, NULL_RTX,
15576                         "Want address and only have value");
15577       return 0;
15578     }
15579
15580   gcc_assert (!ret || !list_ret);
15581
15582   /* If we've got an address and don't want one, dereference.  */
15583   if (!want_address && have_address)
15584     {
15585       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15586
15587       if (size > DWARF2_ADDR_SIZE || size == -1)
15588         {
15589           expansion_failed (loc, NULL_RTX,
15590                             "DWARF address size mismatch");
15591           return 0;
15592         }
15593       else if (size == DWARF2_ADDR_SIZE)
15594         op = DW_OP_deref;
15595       else
15596         op = DW_OP_deref_size;
15597
15598       if (ret)
15599         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15600       else
15601         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15602     }
15603   if (ret)
15604     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15605
15606   return list_ret;
15607 }
15608
15609 /* Same as above but return only single location expression.  */
15610 static dw_loc_descr_ref
15611 loc_descriptor_from_tree (tree loc, int want_address)
15612 {
15613   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15614   if (!ret)
15615     return NULL;
15616   if (ret->dw_loc_next)
15617     {
15618       expansion_failed (loc, NULL_RTX,
15619                         "Location list where only loc descriptor needed");
15620       return NULL;
15621     }
15622   return ret->expr;
15623 }
15624
15625 /* Given a value, round it up to the lowest multiple of `boundary'
15626    which is not less than the value itself.  */
15627
15628 static inline HOST_WIDE_INT
15629 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15630 {
15631   return (((value + boundary - 1) / boundary) * boundary);
15632 }
15633
15634 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15635    pointer to the declared type for the relevant field variable, or return
15636    `integer_type_node' if the given node turns out to be an
15637    ERROR_MARK node.  */
15638
15639 static inline tree
15640 field_type (const_tree decl)
15641 {
15642   tree type;
15643
15644   if (TREE_CODE (decl) == ERROR_MARK)
15645     return integer_type_node;
15646
15647   type = DECL_BIT_FIELD_TYPE (decl);
15648   if (type == NULL_TREE)
15649     type = TREE_TYPE (decl);
15650
15651   return type;
15652 }
15653
15654 /* Given a pointer to a tree node, return the alignment in bits for
15655    it, or else return BITS_PER_WORD if the node actually turns out to
15656    be an ERROR_MARK node.  */
15657
15658 static inline unsigned
15659 simple_type_align_in_bits (const_tree type)
15660 {
15661   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15662 }
15663
15664 static inline unsigned
15665 simple_decl_align_in_bits (const_tree decl)
15666 {
15667   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15668 }
15669
15670 /* Return the result of rounding T up to ALIGN.  */
15671
15672 static inline double_int
15673 round_up_to_align (double_int t, unsigned int align)
15674 {
15675   double_int alignd = uhwi_to_double_int (align);
15676   t = double_int_add (t, alignd);
15677   t = double_int_add (t, double_int_minus_one);
15678   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15679   t = double_int_mul (t, alignd);
15680   return t;
15681 }
15682
15683 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15684    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15685    or return 0 if we are unable to determine what that offset is, either
15686    because the argument turns out to be a pointer to an ERROR_MARK node, or
15687    because the offset is actually variable.  (We can't handle the latter case
15688    just yet).  */
15689
15690 static HOST_WIDE_INT
15691 field_byte_offset (const_tree decl)
15692 {
15693   double_int object_offset_in_bits;
15694   double_int object_offset_in_bytes;
15695   double_int bitpos_int;
15696
15697   if (TREE_CODE (decl) == ERROR_MARK)
15698     return 0;
15699
15700   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15701
15702   /* We cannot yet cope with fields whose positions are variable, so
15703      for now, when we see such things, we simply return 0.  Someday, we may
15704      be able to handle such cases, but it will be damn difficult.  */
15705   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15706     return 0;
15707
15708   bitpos_int = tree_to_double_int (bit_position (decl));
15709
15710 #ifdef PCC_BITFIELD_TYPE_MATTERS
15711   if (PCC_BITFIELD_TYPE_MATTERS)
15712     {
15713       tree type;
15714       tree field_size_tree;
15715       double_int deepest_bitpos;
15716       double_int field_size_in_bits;
15717       unsigned int type_align_in_bits;
15718       unsigned int decl_align_in_bits;
15719       double_int type_size_in_bits;
15720
15721       type = field_type (decl);
15722       type_size_in_bits = double_int_type_size_in_bits (type);
15723       type_align_in_bits = simple_type_align_in_bits (type);
15724
15725       field_size_tree = DECL_SIZE (decl);
15726
15727       /* The size could be unspecified if there was an error, or for
15728          a flexible array member.  */
15729       if (!field_size_tree)
15730         field_size_tree = bitsize_zero_node;
15731
15732       /* If the size of the field is not constant, use the type size.  */
15733       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15734         field_size_in_bits = tree_to_double_int (field_size_tree);
15735       else
15736         field_size_in_bits = type_size_in_bits;
15737
15738       decl_align_in_bits = simple_decl_align_in_bits (decl);
15739
15740       /* The GCC front-end doesn't make any attempt to keep track of the
15741          starting bit offset (relative to the start of the containing
15742          structure type) of the hypothetical "containing object" for a
15743          bit-field.  Thus, when computing the byte offset value for the
15744          start of the "containing object" of a bit-field, we must deduce
15745          this information on our own. This can be rather tricky to do in
15746          some cases.  For example, handling the following structure type
15747          definition when compiling for an i386/i486 target (which only
15748          aligns long long's to 32-bit boundaries) can be very tricky:
15749
15750          struct S { int field1; long long field2:31; };
15751
15752          Fortunately, there is a simple rule-of-thumb which can be used
15753          in such cases.  When compiling for an i386/i486, GCC will
15754          allocate 8 bytes for the structure shown above.  It decides to
15755          do this based upon one simple rule for bit-field allocation.
15756          GCC allocates each "containing object" for each bit-field at
15757          the first (i.e. lowest addressed) legitimate alignment boundary
15758          (based upon the required minimum alignment for the declared
15759          type of the field) which it can possibly use, subject to the
15760          condition that there is still enough available space remaining
15761          in the containing object (when allocated at the selected point)
15762          to fully accommodate all of the bits of the bit-field itself.
15763
15764          This simple rule makes it obvious why GCC allocates 8 bytes for
15765          each object of the structure type shown above.  When looking
15766          for a place to allocate the "containing object" for `field2',
15767          the compiler simply tries to allocate a 64-bit "containing
15768          object" at each successive 32-bit boundary (starting at zero)
15769          until it finds a place to allocate that 64- bit field such that
15770          at least 31 contiguous (and previously unallocated) bits remain
15771          within that selected 64 bit field.  (As it turns out, for the
15772          example above, the compiler finds it is OK to allocate the
15773          "containing object" 64-bit field at bit-offset zero within the
15774          structure type.)
15775
15776          Here we attempt to work backwards from the limited set of facts
15777          we're given, and we try to deduce from those facts, where GCC
15778          must have believed that the containing object started (within
15779          the structure type). The value we deduce is then used (by the
15780          callers of this routine) to generate DW_AT_location and
15781          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15782          the case of DW_AT_location, regular fields as well).  */
15783
15784       /* Figure out the bit-distance from the start of the structure to
15785          the "deepest" bit of the bit-field.  */
15786       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15787
15788       /* This is the tricky part.  Use some fancy footwork to deduce
15789          where the lowest addressed bit of the containing object must
15790          be.  */
15791       object_offset_in_bits
15792         = double_int_sub (deepest_bitpos, type_size_in_bits);
15793
15794       /* Round up to type_align by default.  This works best for
15795          bitfields.  */
15796       object_offset_in_bits
15797         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15798
15799       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15800         {
15801           object_offset_in_bits
15802             = double_int_sub (deepest_bitpos, type_size_in_bits);
15803
15804           /* Round up to decl_align instead.  */
15805           object_offset_in_bits
15806             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15807         }
15808     }
15809   else
15810 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15811     object_offset_in_bits = bitpos_int;
15812
15813   object_offset_in_bytes
15814     = double_int_div (object_offset_in_bits,
15815                       uhwi_to_double_int (BITS_PER_UNIT), true,
15816                       TRUNC_DIV_EXPR);
15817   return double_int_to_shwi (object_offset_in_bytes);
15818 }
15819 \f
15820 /* The following routines define various Dwarf attributes and any data
15821    associated with them.  */
15822
15823 /* Add a location description attribute value to a DIE.
15824
15825    This emits location attributes suitable for whole variables and
15826    whole parameters.  Note that the location attributes for struct fields are
15827    generated by the routine `data_member_location_attribute' below.  */
15828
15829 static inline void
15830 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15831                              dw_loc_list_ref descr)
15832 {
15833   if (descr == 0)
15834     return;
15835   if (single_element_loc_list_p (descr))
15836     add_AT_loc (die, attr_kind, descr->expr);
15837   else
15838     add_AT_loc_list (die, attr_kind, descr);
15839 }
15840
15841 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15842
15843 static void
15844 add_accessibility_attribute (dw_die_ref die, tree decl)
15845 {
15846   if (TREE_PROTECTED (decl))
15847     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15848   else if (TREE_PRIVATE (decl))
15849     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15850 }
15851
15852 /* Attach the specialized form of location attribute used for data members of
15853    struct and union types.  In the special case of a FIELD_DECL node which
15854    represents a bit-field, the "offset" part of this special location
15855    descriptor must indicate the distance in bytes from the lowest-addressed
15856    byte of the containing struct or union type to the lowest-addressed byte of
15857    the "containing object" for the bit-field.  (See the `field_byte_offset'
15858    function above).
15859
15860    For any given bit-field, the "containing object" is a hypothetical object
15861    (of some integral or enum type) within which the given bit-field lives.  The
15862    type of this hypothetical "containing object" is always the same as the
15863    declared type of the individual bit-field itself (for GCC anyway... the
15864    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15865    bytes) of the hypothetical "containing object" which will be given in the
15866    DW_AT_byte_size attribute for this bit-field.  (See the
15867    `byte_size_attribute' function below.)  It is also used when calculating the
15868    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15869    function below.)  */
15870
15871 static void
15872 add_data_member_location_attribute (dw_die_ref die, tree decl)
15873 {
15874   HOST_WIDE_INT offset;
15875   dw_loc_descr_ref loc_descr = 0;
15876
15877   if (TREE_CODE (decl) == TREE_BINFO)
15878     {
15879       /* We're working on the TAG_inheritance for a base class.  */
15880       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15881         {
15882           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15883              aren't at a fixed offset from all (sub)objects of the same
15884              type.  We need to extract the appropriate offset from our
15885              vtable.  The following dwarf expression means
15886
15887                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15888
15889              This is specific to the V3 ABI, of course.  */
15890
15891           dw_loc_descr_ref tmp;
15892
15893           /* Make a copy of the object address.  */
15894           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15895           add_loc_descr (&loc_descr, tmp);
15896
15897           /* Extract the vtable address.  */
15898           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15899           add_loc_descr (&loc_descr, tmp);
15900
15901           /* Calculate the address of the offset.  */
15902           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15903           gcc_assert (offset < 0);
15904
15905           tmp = int_loc_descriptor (-offset);
15906           add_loc_descr (&loc_descr, tmp);
15907           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15908           add_loc_descr (&loc_descr, tmp);
15909
15910           /* Extract the offset.  */
15911           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15912           add_loc_descr (&loc_descr, tmp);
15913
15914           /* Add it to the object address.  */
15915           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15916           add_loc_descr (&loc_descr, tmp);
15917         }
15918       else
15919         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15920     }
15921   else
15922     offset = field_byte_offset (decl);
15923
15924   if (! loc_descr)
15925     {
15926       if (dwarf_version > 2)
15927         {
15928           /* Don't need to output a location expression, just the constant. */
15929           if (offset < 0)
15930             add_AT_int (die, DW_AT_data_member_location, offset);
15931           else
15932             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15933           return;
15934         }
15935       else
15936         {
15937           enum dwarf_location_atom op;
15938
15939           /* The DWARF2 standard says that we should assume that the structure
15940              address is already on the stack, so we can specify a structure
15941              field address by using DW_OP_plus_uconst.  */
15942
15943 #ifdef MIPS_DEBUGGING_INFO
15944           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15945              operator correctly.  It works only if we leave the offset on the
15946              stack.  */
15947           op = DW_OP_constu;
15948 #else
15949           op = DW_OP_plus_uconst;
15950 #endif
15951
15952           loc_descr = new_loc_descr (op, offset, 0);
15953         }
15954     }
15955
15956   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15957 }
15958
15959 /* Writes integer values to dw_vec_const array.  */
15960
15961 static void
15962 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15963 {
15964   while (size != 0)
15965     {
15966       *dest++ = val & 0xff;
15967       val >>= 8;
15968       --size;
15969     }
15970 }
15971
15972 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15973
15974 static HOST_WIDE_INT
15975 extract_int (const unsigned char *src, unsigned int size)
15976 {
15977   HOST_WIDE_INT val = 0;
15978
15979   src += size;
15980   while (size != 0)
15981     {
15982       val <<= 8;
15983       val |= *--src & 0xff;
15984       --size;
15985     }
15986   return val;
15987 }
15988
15989 /* Writes double_int values to dw_vec_const array.  */
15990
15991 static void
15992 insert_double (double_int val, unsigned char *dest)
15993 {
15994   unsigned char *p0 = dest;
15995   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15996
15997   if (WORDS_BIG_ENDIAN)
15998     {
15999       p0 = p1;
16000       p1 = dest;
16001     }
16002
16003   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16004   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16005 }
16006
16007 /* Writes floating point values to dw_vec_const array.  */
16008
16009 static void
16010 insert_float (const_rtx rtl, unsigned char *array)
16011 {
16012   REAL_VALUE_TYPE rv;
16013   long val[4];
16014   int i;
16015
16016   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16017   real_to_target (val, &rv, GET_MODE (rtl));
16018
16019   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16020   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16021     {
16022       insert_int (val[i], 4, array);
16023       array += 4;
16024     }
16025 }
16026
16027 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16028    does not have a "location" either in memory or in a register.  These
16029    things can arise in GNU C when a constant is passed as an actual parameter
16030    to an inlined function.  They can also arise in C++ where declared
16031    constants do not necessarily get memory "homes".  */
16032
16033 static bool
16034 add_const_value_attribute (dw_die_ref die, rtx rtl)
16035 {
16036   switch (GET_CODE (rtl))
16037     {
16038     case CONST_INT:
16039       {
16040         HOST_WIDE_INT val = INTVAL (rtl);
16041
16042         if (val < 0)
16043           add_AT_int (die, DW_AT_const_value, val);
16044         else
16045           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16046       }
16047       return true;
16048
16049     case CONST_DOUBLE:
16050       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16051          floating-point constant.  A CONST_DOUBLE is used whenever the
16052          constant requires more than one word in order to be adequately
16053          represented.  */
16054       {
16055         enum machine_mode mode = GET_MODE (rtl);
16056
16057         if (SCALAR_FLOAT_MODE_P (mode))
16058           {
16059             unsigned int length = GET_MODE_SIZE (mode);
16060             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16061
16062             insert_float (rtl, array);
16063             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16064           }
16065         else
16066           add_AT_double (die, DW_AT_const_value,
16067                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16068       }
16069       return true;
16070
16071     case CONST_VECTOR:
16072       {
16073         enum machine_mode mode = GET_MODE (rtl);
16074         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16075         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16076         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16077           (length * elt_size);
16078         unsigned int i;
16079         unsigned char *p;
16080
16081         switch (GET_MODE_CLASS (mode))
16082           {
16083           case MODE_VECTOR_INT:
16084             for (i = 0, p = array; i < length; i++, p += elt_size)
16085               {
16086                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16087                 double_int val = rtx_to_double_int (elt);
16088
16089                 if (elt_size <= sizeof (HOST_WIDE_INT))
16090                   insert_int (double_int_to_shwi (val), elt_size, p);
16091                 else
16092                   {
16093                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16094                     insert_double (val, p);
16095                   }
16096               }
16097             break;
16098
16099           case MODE_VECTOR_FLOAT:
16100             for (i = 0, p = array; i < length; i++, p += elt_size)
16101               {
16102                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16103                 insert_float (elt, p);
16104               }
16105             break;
16106
16107           default:
16108             gcc_unreachable ();
16109           }
16110
16111         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16112       }
16113       return true;
16114
16115     case CONST_STRING:
16116       if (dwarf_version >= 4 || !dwarf_strict)
16117         {
16118           dw_loc_descr_ref loc_result;
16119           resolve_one_addr (&rtl, NULL);
16120         rtl_addr:
16121           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16122           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16123           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16124           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16125           add_AT_loc (die, DW_AT_location, loc_result);
16126           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16127           return true;
16128         }
16129       return false;
16130
16131     case CONST:
16132       if (CONSTANT_P (XEXP (rtl, 0)))
16133         return add_const_value_attribute (die, XEXP (rtl, 0));
16134       /* FALLTHROUGH */
16135     case SYMBOL_REF:
16136       if (!const_ok_for_output (rtl))
16137         return false;
16138     case LABEL_REF:
16139       if (dwarf_version >= 4 || !dwarf_strict)
16140         goto rtl_addr;
16141       return false;
16142
16143     case PLUS:
16144       /* In cases where an inlined instance of an inline function is passed
16145          the address of an `auto' variable (which is local to the caller) we
16146          can get a situation where the DECL_RTL of the artificial local
16147          variable (for the inlining) which acts as a stand-in for the
16148          corresponding formal parameter (of the inline function) will look
16149          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16150          exactly a compile-time constant expression, but it isn't the address
16151          of the (artificial) local variable either.  Rather, it represents the
16152          *value* which the artificial local variable always has during its
16153          lifetime.  We currently have no way to represent such quasi-constant
16154          values in Dwarf, so for now we just punt and generate nothing.  */
16155       return false;
16156
16157     case HIGH:
16158     case CONST_FIXED:
16159       return false;
16160
16161     case MEM:
16162       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16163           && MEM_READONLY_P (rtl)
16164           && GET_MODE (rtl) == BLKmode)
16165         {
16166           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16167           return true;
16168         }
16169       return false;
16170
16171     default:
16172       /* No other kinds of rtx should be possible here.  */
16173       gcc_unreachable ();
16174     }
16175   return false;
16176 }
16177
16178 /* Determine whether the evaluation of EXPR references any variables
16179    or functions which aren't otherwise used (and therefore may not be
16180    output).  */
16181 static tree
16182 reference_to_unused (tree * tp, int * walk_subtrees,
16183                      void * data ATTRIBUTE_UNUSED)
16184 {
16185   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16186     *walk_subtrees = 0;
16187
16188   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16189       && ! TREE_ASM_WRITTEN (*tp))
16190     return *tp;
16191   /* ???  The C++ FE emits debug information for using decls, so
16192      putting gcc_unreachable here falls over.  See PR31899.  For now
16193      be conservative.  */
16194   else if (!cgraph_global_info_ready
16195            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16196     return *tp;
16197   else if (TREE_CODE (*tp) == VAR_DECL)
16198     {
16199       struct varpool_node *node = varpool_get_node (*tp);
16200       if (!node || !node->needed)
16201         return *tp;
16202     }
16203   else if (TREE_CODE (*tp) == FUNCTION_DECL
16204            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16205     {
16206       /* The call graph machinery must have finished analyzing,
16207          optimizing and gimplifying the CU by now.
16208          So if *TP has no call graph node associated
16209          to it, it means *TP will not be emitted.  */
16210       if (!cgraph_get_node (*tp))
16211         return *tp;
16212     }
16213   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16214     return *tp;
16215
16216   return NULL_TREE;
16217 }
16218
16219 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16220    for use in a later add_const_value_attribute call.  */
16221
16222 static rtx
16223 rtl_for_decl_init (tree init, tree type)
16224 {
16225   rtx rtl = NULL_RTX;
16226
16227   /* If a variable is initialized with a string constant without embedded
16228      zeros, build CONST_STRING.  */
16229   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16230     {
16231       tree enttype = TREE_TYPE (type);
16232       tree domain = TYPE_DOMAIN (type);
16233       enum machine_mode mode = TYPE_MODE (enttype);
16234
16235       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16236           && domain
16237           && integer_zerop (TYPE_MIN_VALUE (domain))
16238           && compare_tree_int (TYPE_MAX_VALUE (domain),
16239                                TREE_STRING_LENGTH (init) - 1) == 0
16240           && ((size_t) TREE_STRING_LENGTH (init)
16241               == strlen (TREE_STRING_POINTER (init)) + 1))
16242         {
16243           rtl = gen_rtx_CONST_STRING (VOIDmode,
16244                                       ggc_strdup (TREE_STRING_POINTER (init)));
16245           rtl = gen_rtx_MEM (BLKmode, rtl);
16246           MEM_READONLY_P (rtl) = 1;
16247         }
16248     }
16249   /* Other aggregates, and complex values, could be represented using
16250      CONCAT: FIXME!  */
16251   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16252     ;
16253   /* Vectors only work if their mode is supported by the target.
16254      FIXME: generic vectors ought to work too.  */
16255   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16256     ;
16257   /* If the initializer is something that we know will expand into an
16258      immediate RTL constant, expand it now.  We must be careful not to
16259      reference variables which won't be output.  */
16260   else if (initializer_constant_valid_p (init, type)
16261            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16262     {
16263       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16264          possible.  */
16265       if (TREE_CODE (type) == VECTOR_TYPE)
16266         switch (TREE_CODE (init))
16267           {
16268           case VECTOR_CST:
16269             break;
16270           case CONSTRUCTOR:
16271             if (TREE_CONSTANT (init))
16272               {
16273                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16274                 bool constant_p = true;
16275                 tree value;
16276                 unsigned HOST_WIDE_INT ix;
16277
16278                 /* Even when ctor is constant, it might contain non-*_CST
16279                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16280                    belong into VECTOR_CST nodes.  */
16281                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16282                   if (!CONSTANT_CLASS_P (value))
16283                     {
16284                       constant_p = false;
16285                       break;
16286                     }
16287
16288                 if (constant_p)
16289                   {
16290                     init = build_vector_from_ctor (type, elts);
16291                     break;
16292                   }
16293               }
16294             /* FALLTHRU */
16295
16296           default:
16297             return NULL;
16298           }
16299
16300       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16301
16302       /* If expand_expr returns a MEM, it wasn't immediate.  */
16303       gcc_assert (!rtl || !MEM_P (rtl));
16304     }
16305
16306   return rtl;
16307 }
16308
16309 /* Generate RTL for the variable DECL to represent its location.  */
16310
16311 static rtx
16312 rtl_for_decl_location (tree decl)
16313 {
16314   rtx rtl;
16315
16316   /* Here we have to decide where we are going to say the parameter "lives"
16317      (as far as the debugger is concerned).  We only have a couple of
16318      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16319
16320      DECL_RTL normally indicates where the parameter lives during most of the
16321      activation of the function.  If optimization is enabled however, this
16322      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16323      that the parameter doesn't really live anywhere (as far as the code
16324      generation parts of GCC are concerned) during most of the function's
16325      activation.  That will happen (for example) if the parameter is never
16326      referenced within the function.
16327
16328      We could just generate a location descriptor here for all non-NULL
16329      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16330      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16331      where DECL_RTL is NULL or is a pseudo-reg.
16332
16333      Note however that we can only get away with using DECL_INCOMING_RTL as
16334      a backup substitute for DECL_RTL in certain limited cases.  In cases
16335      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16336      we can be sure that the parameter was passed using the same type as it is
16337      declared to have within the function, and that its DECL_INCOMING_RTL
16338      points us to a place where a value of that type is passed.
16339
16340      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16341      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16342      because in these cases DECL_INCOMING_RTL points us to a value of some
16343      type which is *different* from the type of the parameter itself.  Thus,
16344      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16345      such cases, the debugger would end up (for example) trying to fetch a
16346      `float' from a place which actually contains the first part of a
16347      `double'.  That would lead to really incorrect and confusing
16348      output at debug-time.
16349
16350      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16351      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16352      are a couple of exceptions however.  On little-endian machines we can
16353      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16354      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16355      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16356      when (on a little-endian machine) a non-prototyped function has a
16357      parameter declared to be of type `short' or `char'.  In such cases,
16358      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16359      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16360      passed `int' value.  If the debugger then uses that address to fetch
16361      a `short' or a `char' (on a little-endian machine) the result will be
16362      the correct data, so we allow for such exceptional cases below.
16363
16364      Note that our goal here is to describe the place where the given formal
16365      parameter lives during most of the function's activation (i.e. between the
16366      end of the prologue and the start of the epilogue).  We'll do that as best
16367      as we can. Note however that if the given formal parameter is modified
16368      sometime during the execution of the function, then a stack backtrace (at
16369      debug-time) will show the function as having been called with the *new*
16370      value rather than the value which was originally passed in.  This happens
16371      rarely enough that it is not a major problem, but it *is* a problem, and
16372      I'd like to fix it.
16373
16374      A future version of dwarf2out.c may generate two additional attributes for
16375      any given DW_TAG_formal_parameter DIE which will describe the "passed
16376      type" and the "passed location" for the given formal parameter in addition
16377      to the attributes we now generate to indicate the "declared type" and the
16378      "active location" for each parameter.  This additional set of attributes
16379      could be used by debuggers for stack backtraces. Separately, note that
16380      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16381      This happens (for example) for inlined-instances of inline function formal
16382      parameters which are never referenced.  This really shouldn't be
16383      happening.  All PARM_DECL nodes should get valid non-NULL
16384      DECL_INCOMING_RTL values.  FIXME.  */
16385
16386   /* Use DECL_RTL as the "location" unless we find something better.  */
16387   rtl = DECL_RTL_IF_SET (decl);
16388
16389   /* When generating abstract instances, ignore everything except
16390      constants, symbols living in memory, and symbols living in
16391      fixed registers.  */
16392   if (! reload_completed)
16393     {
16394       if (rtl
16395           && (CONSTANT_P (rtl)
16396               || (MEM_P (rtl)
16397                   && CONSTANT_P (XEXP (rtl, 0)))
16398               || (REG_P (rtl)
16399                   && TREE_CODE (decl) == VAR_DECL
16400                   && TREE_STATIC (decl))))
16401         {
16402           rtl = targetm.delegitimize_address (rtl);
16403           return rtl;
16404         }
16405       rtl = NULL_RTX;
16406     }
16407   else if (TREE_CODE (decl) == PARM_DECL)
16408     {
16409       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16410         {
16411           tree declared_type = TREE_TYPE (decl);
16412           tree passed_type = DECL_ARG_TYPE (decl);
16413           enum machine_mode dmode = TYPE_MODE (declared_type);
16414           enum machine_mode pmode = TYPE_MODE (passed_type);
16415
16416           /* This decl represents a formal parameter which was optimized out.
16417              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16418              all cases where (rtl == NULL_RTX) just below.  */
16419           if (dmode == pmode)
16420             rtl = DECL_INCOMING_RTL (decl);
16421           else if (SCALAR_INT_MODE_P (dmode)
16422                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16423                    && DECL_INCOMING_RTL (decl))
16424             {
16425               rtx inc = DECL_INCOMING_RTL (decl);
16426               if (REG_P (inc))
16427                 rtl = inc;
16428               else if (MEM_P (inc))
16429                 {
16430                   if (BYTES_BIG_ENDIAN)
16431                     rtl = adjust_address_nv (inc, dmode,
16432                                              GET_MODE_SIZE (pmode)
16433                                              - GET_MODE_SIZE (dmode));
16434                   else
16435                     rtl = inc;
16436                 }
16437             }
16438         }
16439
16440       /* If the parm was passed in registers, but lives on the stack, then
16441          make a big endian correction if the mode of the type of the
16442          parameter is not the same as the mode of the rtl.  */
16443       /* ??? This is the same series of checks that are made in dbxout.c before
16444          we reach the big endian correction code there.  It isn't clear if all
16445          of these checks are necessary here, but keeping them all is the safe
16446          thing to do.  */
16447       else if (MEM_P (rtl)
16448                && XEXP (rtl, 0) != const0_rtx
16449                && ! CONSTANT_P (XEXP (rtl, 0))
16450                /* Not passed in memory.  */
16451                && !MEM_P (DECL_INCOMING_RTL (decl))
16452                /* Not passed by invisible reference.  */
16453                && (!REG_P (XEXP (rtl, 0))
16454                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16455                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16456 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16457                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16458 #endif
16459                      )
16460                /* Big endian correction check.  */
16461                && BYTES_BIG_ENDIAN
16462                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16463                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16464                    < UNITS_PER_WORD))
16465         {
16466           int offset = (UNITS_PER_WORD
16467                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16468
16469           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16470                              plus_constant (XEXP (rtl, 0), offset));
16471         }
16472     }
16473   else if (TREE_CODE (decl) == VAR_DECL
16474            && rtl
16475            && MEM_P (rtl)
16476            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16477            && BYTES_BIG_ENDIAN)
16478     {
16479       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16480       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16481
16482       /* If a variable is declared "register" yet is smaller than
16483          a register, then if we store the variable to memory, it
16484          looks like we're storing a register-sized value, when in
16485          fact we are not.  We need to adjust the offset of the
16486          storage location to reflect the actual value's bytes,
16487          else gdb will not be able to display it.  */
16488       if (rsize > dsize)
16489         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16490                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16491     }
16492
16493   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16494      and will have been substituted directly into all expressions that use it.
16495      C does not have such a concept, but C++ and other languages do.  */
16496   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16497     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16498
16499   if (rtl)
16500     rtl = targetm.delegitimize_address (rtl);
16501
16502   /* If we don't look past the constant pool, we risk emitting a
16503      reference to a constant pool entry that isn't referenced from
16504      code, and thus is not emitted.  */
16505   if (rtl)
16506     rtl = avoid_constant_pool_reference (rtl);
16507
16508   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16509      in the current CU, resolve_addr will remove the expression referencing
16510      it.  */
16511   if (rtl == NULL_RTX
16512       && TREE_CODE (decl) == VAR_DECL
16513       && !DECL_EXTERNAL (decl)
16514       && TREE_STATIC (decl)
16515       && DECL_NAME (decl)
16516       && !DECL_HARD_REGISTER (decl)
16517       && DECL_MODE (decl) != VOIDmode)
16518     {
16519       rtl = make_decl_rtl_for_debug (decl);
16520       if (!MEM_P (rtl)
16521           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16522           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16523         rtl = NULL_RTX;
16524     }
16525
16526   return rtl;
16527 }
16528
16529 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16530    returned.  If so, the decl for the COMMON block is returned, and the
16531    value is the offset into the common block for the symbol.  */
16532
16533 static tree
16534 fortran_common (tree decl, HOST_WIDE_INT *value)
16535 {
16536   tree val_expr, cvar;
16537   enum machine_mode mode;
16538   HOST_WIDE_INT bitsize, bitpos;
16539   tree offset;
16540   int volatilep = 0, unsignedp = 0;
16541
16542   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16543      it does not have a value (the offset into the common area), or if it
16544      is thread local (as opposed to global) then it isn't common, and shouldn't
16545      be handled as such.  */
16546   if (TREE_CODE (decl) != VAR_DECL
16547       || !TREE_STATIC (decl)
16548       || !DECL_HAS_VALUE_EXPR_P (decl)
16549       || !is_fortran ())
16550     return NULL_TREE;
16551
16552   val_expr = DECL_VALUE_EXPR (decl);
16553   if (TREE_CODE (val_expr) != COMPONENT_REF)
16554     return NULL_TREE;
16555
16556   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16557                               &mode, &unsignedp, &volatilep, true);
16558
16559   if (cvar == NULL_TREE
16560       || TREE_CODE (cvar) != VAR_DECL
16561       || DECL_ARTIFICIAL (cvar)
16562       || !TREE_PUBLIC (cvar))
16563     return NULL_TREE;
16564
16565   *value = 0;
16566   if (offset != NULL)
16567     {
16568       if (!host_integerp (offset, 0))
16569         return NULL_TREE;
16570       *value = tree_low_cst (offset, 0);
16571     }
16572   if (bitpos != 0)
16573     *value += bitpos / BITS_PER_UNIT;
16574
16575   return cvar;
16576 }
16577
16578 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16579    data attribute for a variable or a parameter.  We generate the
16580    DW_AT_const_value attribute only in those cases where the given variable
16581    or parameter does not have a true "location" either in memory or in a
16582    register.  This can happen (for example) when a constant is passed as an
16583    actual argument in a call to an inline function.  (It's possible that
16584    these things can crop up in other ways also.)  Note that one type of
16585    constant value which can be passed into an inlined function is a constant
16586    pointer.  This can happen for example if an actual argument in an inlined
16587    function call evaluates to a compile-time constant address.  */
16588
16589 static bool
16590 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16591                                        enum dwarf_attribute attr)
16592 {
16593   rtx rtl;
16594   dw_loc_list_ref list;
16595   var_loc_list *loc_list;
16596
16597   if (TREE_CODE (decl) == ERROR_MARK)
16598     return false;
16599
16600   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16601               || TREE_CODE (decl) == RESULT_DECL);
16602
16603   /* Try to get some constant RTL for this decl, and use that as the value of
16604      the location.  */
16605
16606   rtl = rtl_for_decl_location (decl);
16607   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16608       && add_const_value_attribute (die, rtl))
16609     return true;
16610
16611   /* See if we have single element location list that is equivalent to
16612      a constant value.  That way we are better to use add_const_value_attribute
16613      rather than expanding constant value equivalent.  */
16614   loc_list = lookup_decl_loc (decl);
16615   if (loc_list
16616       && loc_list->first
16617       && loc_list->first->next == NULL
16618       && NOTE_P (loc_list->first->loc)
16619       && NOTE_VAR_LOCATION (loc_list->first->loc)
16620       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16621     {
16622       struct var_loc_node *node;
16623
16624       node = loc_list->first;
16625       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16626       if (GET_CODE (rtl) == EXPR_LIST)
16627         rtl = XEXP (rtl, 0);
16628       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16629           && add_const_value_attribute (die, rtl))
16630          return true;
16631     }
16632   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16633   if (list)
16634     {
16635       add_AT_location_description (die, attr, list);
16636       return true;
16637     }
16638   /* None of that worked, so it must not really have a location;
16639      try adding a constant value attribute from the DECL_INITIAL.  */
16640   return tree_add_const_value_attribute_for_decl (die, decl);
16641 }
16642
16643 /* Add VARIABLE and DIE into deferred locations list.  */
16644
16645 static void
16646 defer_location (tree variable, dw_die_ref die)
16647 {
16648   deferred_locations entry;
16649   entry.variable = variable;
16650   entry.die = die;
16651   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16652 }
16653
16654 /* Helper function for tree_add_const_value_attribute.  Natively encode
16655    initializer INIT into an array.  Return true if successful.  */
16656
16657 static bool
16658 native_encode_initializer (tree init, unsigned char *array, int size)
16659 {
16660   tree type;
16661
16662   if (init == NULL_TREE)
16663     return false;
16664
16665   STRIP_NOPS (init);
16666   switch (TREE_CODE (init))
16667     {
16668     case STRING_CST:
16669       type = TREE_TYPE (init);
16670       if (TREE_CODE (type) == ARRAY_TYPE)
16671         {
16672           tree enttype = TREE_TYPE (type);
16673           enum machine_mode mode = TYPE_MODE (enttype);
16674
16675           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16676             return false;
16677           if (int_size_in_bytes (type) != size)
16678             return false;
16679           if (size > TREE_STRING_LENGTH (init))
16680             {
16681               memcpy (array, TREE_STRING_POINTER (init),
16682                       TREE_STRING_LENGTH (init));
16683               memset (array + TREE_STRING_LENGTH (init),
16684                       '\0', size - TREE_STRING_LENGTH (init));
16685             }
16686           else
16687             memcpy (array, TREE_STRING_POINTER (init), size);
16688           return true;
16689         }
16690       return false;
16691     case CONSTRUCTOR:
16692       type = TREE_TYPE (init);
16693       if (int_size_in_bytes (type) != size)
16694         return false;
16695       if (TREE_CODE (type) == ARRAY_TYPE)
16696         {
16697           HOST_WIDE_INT min_index;
16698           unsigned HOST_WIDE_INT cnt;
16699           int curpos = 0, fieldsize;
16700           constructor_elt *ce;
16701
16702           if (TYPE_DOMAIN (type) == NULL_TREE
16703               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16704             return false;
16705
16706           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16707           if (fieldsize <= 0)
16708             return false;
16709
16710           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16711           memset (array, '\0', size);
16712           for (cnt = 0;
16713                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16714                cnt++)
16715             {
16716               tree val = ce->value;
16717               tree index = ce->index;
16718               int pos = curpos;
16719               if (index && TREE_CODE (index) == RANGE_EXPR)
16720                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16721                       * fieldsize;
16722               else if (index)
16723                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16724
16725               if (val)
16726                 {
16727                   STRIP_NOPS (val);
16728                   if (!native_encode_initializer (val, array + pos, fieldsize))
16729                     return false;
16730                 }
16731               curpos = pos + fieldsize;
16732               if (index && TREE_CODE (index) == RANGE_EXPR)
16733                 {
16734                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16735                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16736                   while (count > 0)
16737                     {
16738                       if (val)
16739                         memcpy (array + curpos, array + pos, fieldsize);
16740                       curpos += fieldsize;
16741                     }
16742                 }
16743               gcc_assert (curpos <= size);
16744             }
16745           return true;
16746         }
16747       else if (TREE_CODE (type) == RECORD_TYPE
16748                || TREE_CODE (type) == UNION_TYPE)
16749         {
16750           tree field = NULL_TREE;
16751           unsigned HOST_WIDE_INT cnt;
16752           constructor_elt *ce;
16753
16754           if (int_size_in_bytes (type) != size)
16755             return false;
16756
16757           if (TREE_CODE (type) == RECORD_TYPE)
16758             field = TYPE_FIELDS (type);
16759
16760           for (cnt = 0;
16761                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16762                cnt++, field = field ? DECL_CHAIN (field) : 0)
16763             {
16764               tree val = ce->value;
16765               int pos, fieldsize;
16766
16767               if (ce->index != 0)
16768                 field = ce->index;
16769
16770               if (val)
16771                 STRIP_NOPS (val);
16772
16773               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16774                 return false;
16775
16776               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16777                   && TYPE_DOMAIN (TREE_TYPE (field))
16778                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16779                 return false;
16780               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16781                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16782                 return false;
16783               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16784               pos = int_byte_position (field);
16785               gcc_assert (pos + fieldsize <= size);
16786               if (val
16787                   && !native_encode_initializer (val, array + pos, fieldsize))
16788                 return false;
16789             }
16790           return true;
16791         }
16792       return false;
16793     case VIEW_CONVERT_EXPR:
16794     case NON_LVALUE_EXPR:
16795       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16796     default:
16797       return native_encode_expr (init, array, size) == size;
16798     }
16799 }
16800
16801 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16802    attribute is the const value T.  */
16803
16804 static bool
16805 tree_add_const_value_attribute (dw_die_ref die, tree t)
16806 {
16807   tree init;
16808   tree type = TREE_TYPE (t);
16809   rtx rtl;
16810
16811   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16812     return false;
16813
16814   init = t;
16815   gcc_assert (!DECL_P (init));
16816
16817   rtl = rtl_for_decl_init (init, type);
16818   if (rtl)
16819     return add_const_value_attribute (die, rtl);
16820   /* If the host and target are sane, try harder.  */
16821   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16822            && initializer_constant_valid_p (init, type))
16823     {
16824       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16825       if (size > 0 && (int) size == size)
16826         {
16827           unsigned char *array = (unsigned char *)
16828             ggc_alloc_cleared_atomic (size);
16829
16830           if (native_encode_initializer (init, array, size))
16831             {
16832               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16833               return true;
16834             }
16835         }
16836     }
16837   return false;
16838 }
16839
16840 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16841    attribute is the const value of T, where T is an integral constant
16842    variable with static storage duration
16843    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16844
16845 static bool
16846 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16847 {
16848
16849   if (!decl
16850       || (TREE_CODE (decl) != VAR_DECL
16851           && TREE_CODE (decl) != CONST_DECL))
16852     return false;
16853
16854     if (TREE_READONLY (decl)
16855         && ! TREE_THIS_VOLATILE (decl)
16856         && DECL_INITIAL (decl))
16857       /* OK */;
16858     else
16859       return false;
16860
16861   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16862   if (get_AT (var_die, DW_AT_const_value))
16863     return false;
16864
16865   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16866 }
16867
16868 /* Convert the CFI instructions for the current function into a
16869    location list.  This is used for DW_AT_frame_base when we targeting
16870    a dwarf2 consumer that does not support the dwarf3
16871    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16872    expressions.  */
16873
16874 static dw_loc_list_ref
16875 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16876 {
16877   dw_fde_ref fde;
16878   dw_loc_list_ref list, *list_tail;
16879   dw_cfi_ref cfi;
16880   dw_cfa_location last_cfa, next_cfa;
16881   const char *start_label, *last_label, *section;
16882   dw_cfa_location remember;
16883
16884   fde = current_fde ();
16885   gcc_assert (fde != NULL);
16886
16887   section = secname_for_decl (current_function_decl);
16888   list_tail = &list;
16889   list = NULL;
16890
16891   memset (&next_cfa, 0, sizeof (next_cfa));
16892   next_cfa.reg = INVALID_REGNUM;
16893   remember = next_cfa;
16894
16895   start_label = fde->dw_fde_begin;
16896
16897   /* ??? Bald assumption that the CIE opcode list does not contain
16898      advance opcodes.  */
16899   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16900     lookup_cfa_1 (cfi, &next_cfa, &remember);
16901
16902   last_cfa = next_cfa;
16903   last_label = start_label;
16904
16905   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16906     switch (cfi->dw_cfi_opc)
16907       {
16908       case DW_CFA_set_loc:
16909       case DW_CFA_advance_loc1:
16910       case DW_CFA_advance_loc2:
16911       case DW_CFA_advance_loc4:
16912         if (!cfa_equal_p (&last_cfa, &next_cfa))
16913           {
16914             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16915                                        start_label, last_label, section);
16916
16917             list_tail = &(*list_tail)->dw_loc_next;
16918             last_cfa = next_cfa;
16919             start_label = last_label;
16920           }
16921         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16922         break;
16923
16924       case DW_CFA_advance_loc:
16925         /* The encoding is complex enough that we should never emit this.  */
16926         gcc_unreachable ();
16927
16928       default:
16929         lookup_cfa_1 (cfi, &next_cfa, &remember);
16930         break;
16931       }
16932
16933   if (!cfa_equal_p (&last_cfa, &next_cfa))
16934     {
16935       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16936                                  start_label, last_label, section);
16937       list_tail = &(*list_tail)->dw_loc_next;
16938       start_label = last_label;
16939     }
16940
16941   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16942                              start_label, fde->dw_fde_end, section);
16943
16944   if (list && list->dw_loc_next)
16945     gen_llsym (list);
16946
16947   return list;
16948 }
16949
16950 /* Compute a displacement from the "steady-state frame pointer" to the
16951    frame base (often the same as the CFA), and store it in
16952    frame_pointer_fb_offset.  OFFSET is added to the displacement
16953    before the latter is negated.  */
16954
16955 static void
16956 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16957 {
16958   rtx reg, elim;
16959
16960 #ifdef FRAME_POINTER_CFA_OFFSET
16961   reg = frame_pointer_rtx;
16962   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16963 #else
16964   reg = arg_pointer_rtx;
16965   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16966 #endif
16967
16968   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16969   if (GET_CODE (elim) == PLUS)
16970     {
16971       offset += INTVAL (XEXP (elim, 1));
16972       elim = XEXP (elim, 0);
16973     }
16974
16975   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16976                && (elim == hard_frame_pointer_rtx
16977                    || elim == stack_pointer_rtx))
16978               || elim == (frame_pointer_needed
16979                           ? hard_frame_pointer_rtx
16980                           : stack_pointer_rtx));
16981
16982   frame_pointer_fb_offset = -offset;
16983 }
16984
16985 /* Generate a DW_AT_name attribute given some string value to be included as
16986    the value of the attribute.  */
16987
16988 static void
16989 add_name_attribute (dw_die_ref die, const char *name_string)
16990 {
16991   if (name_string != NULL && *name_string != 0)
16992     {
16993       if (demangle_name_func)
16994         name_string = (*demangle_name_func) (name_string);
16995
16996       add_AT_string (die, DW_AT_name, name_string);
16997     }
16998 }
16999
17000 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17001
17002 static void
17003 add_comp_dir_attribute (dw_die_ref die)
17004 {
17005   const char *wd = get_src_pwd ();
17006   char *wd1;
17007
17008   if (wd == NULL)
17009     return;
17010
17011   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17012     {
17013       int wdlen;
17014
17015       wdlen = strlen (wd);
17016       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17017       strcpy (wd1, wd);
17018       wd1 [wdlen] = DIR_SEPARATOR;
17019       wd1 [wdlen + 1] = 0;
17020       wd = wd1;
17021     }
17022
17023     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17024 }
17025
17026 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17027    default.  */
17028
17029 static int
17030 lower_bound_default (void)
17031 {
17032   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17033     {
17034     case DW_LANG_C:
17035     case DW_LANG_C89:
17036     case DW_LANG_C99:
17037     case DW_LANG_C_plus_plus:
17038     case DW_LANG_ObjC:
17039     case DW_LANG_ObjC_plus_plus:
17040     case DW_LANG_Java:
17041       return 0;
17042     case DW_LANG_Fortran77:
17043     case DW_LANG_Fortran90:
17044     case DW_LANG_Fortran95:
17045       return 1;
17046     case DW_LANG_UPC:
17047     case DW_LANG_D:
17048     case DW_LANG_Python:
17049       return dwarf_version >= 4 ? 0 : -1;
17050     case DW_LANG_Ada95:
17051     case DW_LANG_Ada83:
17052     case DW_LANG_Cobol74:
17053     case DW_LANG_Cobol85:
17054     case DW_LANG_Pascal83:
17055     case DW_LANG_Modula2:
17056     case DW_LANG_PLI:
17057       return dwarf_version >= 4 ? 1 : -1;
17058     default:
17059       return -1;
17060     }
17061 }
17062
17063 /* Given a tree node describing an array bound (either lower or upper) output
17064    a representation for that bound.  */
17065
17066 static void
17067 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17068 {
17069   switch (TREE_CODE (bound))
17070     {
17071     case ERROR_MARK:
17072       return;
17073
17074     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17075     case INTEGER_CST:
17076       {
17077         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17078         int dflt;
17079
17080         /* Use the default if possible.  */
17081         if (bound_attr == DW_AT_lower_bound
17082             && host_integerp (bound, 0)
17083             && (dflt = lower_bound_default ()) != -1
17084             && tree_low_cst (bound, 0) == dflt)
17085           ;
17086
17087         /* Otherwise represent the bound as an unsigned value with the
17088            precision of its type.  The precision and signedness of the
17089            type will be necessary to re-interpret it unambiguously.  */
17090         else if (prec < HOST_BITS_PER_WIDE_INT)
17091           {
17092             unsigned HOST_WIDE_INT mask
17093               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17094             add_AT_unsigned (subrange_die, bound_attr,
17095                              TREE_INT_CST_LOW (bound) & mask);
17096           }
17097         else if (prec == HOST_BITS_PER_WIDE_INT
17098                  || TREE_INT_CST_HIGH (bound) == 0)
17099           add_AT_unsigned (subrange_die, bound_attr,
17100                            TREE_INT_CST_LOW (bound));
17101         else
17102           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17103                          TREE_INT_CST_LOW (bound));
17104       }
17105       break;
17106
17107     CASE_CONVERT:
17108     case VIEW_CONVERT_EXPR:
17109       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17110       break;
17111
17112     case SAVE_EXPR:
17113       break;
17114
17115     case VAR_DECL:
17116     case PARM_DECL:
17117     case RESULT_DECL:
17118       {
17119         dw_die_ref decl_die = lookup_decl_die (bound);
17120
17121         /* ??? Can this happen, or should the variable have been bound
17122            first?  Probably it can, since I imagine that we try to create
17123            the types of parameters in the order in which they exist in
17124            the list, and won't have created a forward reference to a
17125            later parameter.  */
17126         if (decl_die != NULL)
17127           {
17128             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17129             break;
17130           }
17131       }
17132       /* FALLTHRU */
17133
17134     default:
17135       {
17136         /* Otherwise try to create a stack operation procedure to
17137            evaluate the value of the array bound.  */
17138
17139         dw_die_ref ctx, decl_die;
17140         dw_loc_list_ref list;
17141
17142         list = loc_list_from_tree (bound, 2);
17143         if (list == NULL || single_element_loc_list_p (list))
17144           {
17145             /* If DW_AT_*bound is not a reference nor constant, it is
17146                a DWARF expression rather than location description.
17147                For that loc_list_from_tree (bound, 0) is needed.
17148                If that fails to give a single element list,
17149                fall back to outputting this as a reference anyway.  */
17150             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17151             if (list2 && single_element_loc_list_p (list2))
17152               {
17153                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17154                 break;
17155               }
17156           }
17157         if (list == NULL)
17158           break;
17159
17160         if (current_function_decl == 0)
17161           ctx = comp_unit_die;
17162         else
17163           ctx = lookup_decl_die (current_function_decl);
17164
17165         decl_die = new_die (DW_TAG_variable, ctx, bound);
17166         add_AT_flag (decl_die, DW_AT_artificial, 1);
17167         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17168         add_AT_location_description (decl_die, DW_AT_location, list);
17169         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17170         break;
17171       }
17172     }
17173 }
17174
17175 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17176    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17177    Note that the block of subscript information for an array type also
17178    includes information about the element type of the given array type.  */
17179
17180 static void
17181 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17182 {
17183   unsigned dimension_number;
17184   tree lower, upper;
17185   dw_die_ref subrange_die;
17186
17187   for (dimension_number = 0;
17188        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17189        type = TREE_TYPE (type), dimension_number++)
17190     {
17191       tree domain = TYPE_DOMAIN (type);
17192
17193       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17194         break;
17195
17196       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17197          and (in GNU C only) variable bounds.  Handle all three forms
17198          here.  */
17199       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17200       if (domain)
17201         {
17202           /* We have an array type with specified bounds.  */
17203           lower = TYPE_MIN_VALUE (domain);
17204           upper = TYPE_MAX_VALUE (domain);
17205
17206           /* Define the index type.  */
17207           if (TREE_TYPE (domain))
17208             {
17209               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17210                  TREE_TYPE field.  We can't emit debug info for this
17211                  because it is an unnamed integral type.  */
17212               if (TREE_CODE (domain) == INTEGER_TYPE
17213                   && TYPE_NAME (domain) == NULL_TREE
17214                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17215                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17216                 ;
17217               else
17218                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17219                                     type_die);
17220             }
17221
17222           /* ??? If upper is NULL, the array has unspecified length,
17223              but it does have a lower bound.  This happens with Fortran
17224                dimension arr(N:*)
17225              Since the debugger is definitely going to need to know N
17226              to produce useful results, go ahead and output the lower
17227              bound solo, and hope the debugger can cope.  */
17228
17229           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17230           if (upper)
17231             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17232         }
17233
17234       /* Otherwise we have an array type with an unspecified length.  The
17235          DWARF-2 spec does not say how to handle this; let's just leave out the
17236          bounds.  */
17237     }
17238 }
17239
17240 static void
17241 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17242 {
17243   unsigned size;
17244
17245   switch (TREE_CODE (tree_node))
17246     {
17247     case ERROR_MARK:
17248       size = 0;
17249       break;
17250     case ENUMERAL_TYPE:
17251     case RECORD_TYPE:
17252     case UNION_TYPE:
17253     case QUAL_UNION_TYPE:
17254       size = int_size_in_bytes (tree_node);
17255       break;
17256     case FIELD_DECL:
17257       /* For a data member of a struct or union, the DW_AT_byte_size is
17258          generally given as the number of bytes normally allocated for an
17259          object of the *declared* type of the member itself.  This is true
17260          even for bit-fields.  */
17261       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17262       break;
17263     default:
17264       gcc_unreachable ();
17265     }
17266
17267   /* Note that `size' might be -1 when we get to this point.  If it is, that
17268      indicates that the byte size of the entity in question is variable.  We
17269      have no good way of expressing this fact in Dwarf at the present time,
17270      so just let the -1 pass on through.  */
17271   add_AT_unsigned (die, DW_AT_byte_size, size);
17272 }
17273
17274 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17275    which specifies the distance in bits from the highest order bit of the
17276    "containing object" for the bit-field to the highest order bit of the
17277    bit-field itself.
17278
17279    For any given bit-field, the "containing object" is a hypothetical object
17280    (of some integral or enum type) within which the given bit-field lives.  The
17281    type of this hypothetical "containing object" is always the same as the
17282    declared type of the individual bit-field itself.  The determination of the
17283    exact location of the "containing object" for a bit-field is rather
17284    complicated.  It's handled by the `field_byte_offset' function (above).
17285
17286    Note that it is the size (in bytes) of the hypothetical "containing object"
17287    which will be given in the DW_AT_byte_size attribute for this bit-field.
17288    (See `byte_size_attribute' above).  */
17289
17290 static inline void
17291 add_bit_offset_attribute (dw_die_ref die, tree decl)
17292 {
17293   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17294   tree type = DECL_BIT_FIELD_TYPE (decl);
17295   HOST_WIDE_INT bitpos_int;
17296   HOST_WIDE_INT highest_order_object_bit_offset;
17297   HOST_WIDE_INT highest_order_field_bit_offset;
17298   HOST_WIDE_INT unsigned bit_offset;
17299
17300   /* Must be a field and a bit field.  */
17301   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17302
17303   /* We can't yet handle bit-fields whose offsets are variable, so if we
17304      encounter such things, just return without generating any attribute
17305      whatsoever.  Likewise for variable or too large size.  */
17306   if (! host_integerp (bit_position (decl), 0)
17307       || ! host_integerp (DECL_SIZE (decl), 1))
17308     return;
17309
17310   bitpos_int = int_bit_position (decl);
17311
17312   /* Note that the bit offset is always the distance (in bits) from the
17313      highest-order bit of the "containing object" to the highest-order bit of
17314      the bit-field itself.  Since the "high-order end" of any object or field
17315      is different on big-endian and little-endian machines, the computation
17316      below must take account of these differences.  */
17317   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17318   highest_order_field_bit_offset = bitpos_int;
17319
17320   if (! BYTES_BIG_ENDIAN)
17321     {
17322       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17323       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17324     }
17325
17326   bit_offset
17327     = (! BYTES_BIG_ENDIAN
17328        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17329        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17330
17331   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17332 }
17333
17334 /* For a FIELD_DECL node which represents a bit field, output an attribute
17335    which specifies the length in bits of the given field.  */
17336
17337 static inline void
17338 add_bit_size_attribute (dw_die_ref die, tree decl)
17339 {
17340   /* Must be a field and a bit field.  */
17341   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17342               && DECL_BIT_FIELD_TYPE (decl));
17343
17344   if (host_integerp (DECL_SIZE (decl), 1))
17345     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17346 }
17347
17348 /* If the compiled language is ANSI C, then add a 'prototyped'
17349    attribute, if arg types are given for the parameters of a function.  */
17350
17351 static inline void
17352 add_prototyped_attribute (dw_die_ref die, tree func_type)
17353 {
17354   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17355       && TYPE_ARG_TYPES (func_type) != NULL)
17356     add_AT_flag (die, DW_AT_prototyped, 1);
17357 }
17358
17359 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17360    by looking in either the type declaration or object declaration
17361    equate table.  */
17362
17363 static inline dw_die_ref
17364 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17365 {
17366   dw_die_ref origin_die = NULL;
17367
17368   if (TREE_CODE (origin) != FUNCTION_DECL)
17369     {
17370       /* We may have gotten separated from the block for the inlined
17371          function, if we're in an exception handler or some such; make
17372          sure that the abstract function has been written out.
17373
17374          Doing this for nested functions is wrong, however; functions are
17375          distinct units, and our context might not even be inline.  */
17376       tree fn = origin;
17377
17378       if (TYPE_P (fn))
17379         fn = TYPE_STUB_DECL (fn);
17380
17381       fn = decl_function_context (fn);
17382       if (fn)
17383         dwarf2out_abstract_function (fn);
17384     }
17385
17386   if (DECL_P (origin))
17387     origin_die = lookup_decl_die (origin);
17388   else if (TYPE_P (origin))
17389     origin_die = lookup_type_die (origin);
17390
17391   /* XXX: Functions that are never lowered don't always have correct block
17392      trees (in the case of java, they simply have no block tree, in some other
17393      languages).  For these functions, there is nothing we can really do to
17394      output correct debug info for inlined functions in all cases.  Rather
17395      than die, we'll just produce deficient debug info now, in that we will
17396      have variables without a proper abstract origin.  In the future, when all
17397      functions are lowered, we should re-add a gcc_assert (origin_die)
17398      here.  */
17399
17400   if (origin_die)
17401     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17402   return origin_die;
17403 }
17404
17405 /* We do not currently support the pure_virtual attribute.  */
17406
17407 static inline void
17408 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17409 {
17410   if (DECL_VINDEX (func_decl))
17411     {
17412       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17413
17414       if (host_integerp (DECL_VINDEX (func_decl), 0))
17415         add_AT_loc (die, DW_AT_vtable_elem_location,
17416                     new_loc_descr (DW_OP_constu,
17417                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17418                                    0));
17419
17420       /* GNU extension: Record what type this method came from originally.  */
17421       if (debug_info_level > DINFO_LEVEL_TERSE
17422           && DECL_CONTEXT (func_decl))
17423         add_AT_die_ref (die, DW_AT_containing_type,
17424                         lookup_type_die (DECL_CONTEXT (func_decl)));
17425     }
17426 }
17427 \f
17428 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17429    given decl.  This used to be a vendor extension until after DWARF 4
17430    standardized it.  */
17431
17432 static void
17433 add_linkage_attr (dw_die_ref die, tree decl)
17434 {
17435   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17436
17437   /* Mimic what assemble_name_raw does with a leading '*'.  */
17438   if (name[0] == '*')
17439     name = &name[1];
17440
17441   if (dwarf_version >= 4)
17442     add_AT_string (die, DW_AT_linkage_name, name);
17443   else
17444     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17445 }
17446
17447 /* Add source coordinate attributes for the given decl.  */
17448
17449 static void
17450 add_src_coords_attributes (dw_die_ref die, tree decl)
17451 {
17452   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17453
17454   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17455   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17456 }
17457
17458 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17459
17460 static void
17461 add_linkage_name (dw_die_ref die, tree decl)
17462 {
17463   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17464        && TREE_PUBLIC (decl)
17465        && !DECL_ABSTRACT (decl)
17466        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17467        && die->die_tag != DW_TAG_member)
17468     {
17469       /* Defer until we have an assembler name set.  */
17470       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17471         {
17472           limbo_die_node *asm_name;
17473
17474           asm_name = ggc_alloc_cleared_limbo_die_node ();
17475           asm_name->die = die;
17476           asm_name->created_for = decl;
17477           asm_name->next = deferred_asm_name;
17478           deferred_asm_name = asm_name;
17479         }
17480       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17481         add_linkage_attr (die, decl);
17482     }
17483 }
17484
17485 /* Add a DW_AT_name attribute and source coordinate attribute for the
17486    given decl, but only if it actually has a name.  */
17487
17488 static void
17489 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17490 {
17491   tree decl_name;
17492
17493   decl_name = DECL_NAME (decl);
17494   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17495     {
17496       const char *name = dwarf2_name (decl, 0);
17497       if (name)
17498         add_name_attribute (die, name);
17499       if (! DECL_ARTIFICIAL (decl))
17500         add_src_coords_attributes (die, decl);
17501
17502       add_linkage_name (die, decl);
17503     }
17504
17505 #ifdef VMS_DEBUGGING_INFO
17506   /* Get the function's name, as described by its RTL.  This may be different
17507      from the DECL_NAME name used in the source file.  */
17508   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17509     {
17510       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17511                    XEXP (DECL_RTL (decl), 0));
17512       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17513     }
17514 #endif /* VMS_DEBUGGING_INFO */
17515 }
17516
17517 #ifdef VMS_DEBUGGING_INFO
17518 /* Output the debug main pointer die for VMS */
17519
17520 void
17521 dwarf2out_vms_debug_main_pointer (void)
17522 {
17523   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17524   dw_die_ref die;
17525
17526   /* Allocate the VMS debug main subprogram die.  */
17527   die = ggc_alloc_cleared_die_node ();
17528   die->die_tag = DW_TAG_subprogram;
17529   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17530   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17531                                current_function_funcdef_no);
17532   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17533
17534   /* Make it the first child of comp_unit_die.  */
17535   die->die_parent = comp_unit_die;
17536   if (comp_unit_die->die_child)
17537     {
17538       die->die_sib = comp_unit_die->die_child->die_sib;
17539       comp_unit_die->die_child->die_sib = die;
17540     }
17541   else
17542     {
17543       die->die_sib = die;
17544       comp_unit_die->die_child = die;
17545     }
17546 }
17547 #endif /* VMS_DEBUGGING_INFO */
17548
17549 /* Push a new declaration scope.  */
17550
17551 static void
17552 push_decl_scope (tree scope)
17553 {
17554   VEC_safe_push (tree, gc, decl_scope_table, scope);
17555 }
17556
17557 /* Pop a declaration scope.  */
17558
17559 static inline void
17560 pop_decl_scope (void)
17561 {
17562   VEC_pop (tree, decl_scope_table);
17563 }
17564
17565 /* Return the DIE for the scope that immediately contains this type.
17566    Non-named types get global scope.  Named types nested in other
17567    types get their containing scope if it's open, or global scope
17568    otherwise.  All other types (i.e. function-local named types) get
17569    the current active scope.  */
17570
17571 static dw_die_ref
17572 scope_die_for (tree t, dw_die_ref context_die)
17573 {
17574   dw_die_ref scope_die = NULL;
17575   tree containing_scope;
17576   int i;
17577
17578   /* Non-types always go in the current scope.  */
17579   gcc_assert (TYPE_P (t));
17580
17581   containing_scope = TYPE_CONTEXT (t);
17582
17583   /* Use the containing namespace if it was passed in (for a declaration).  */
17584   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17585     {
17586       if (context_die == lookup_decl_die (containing_scope))
17587         /* OK */;
17588       else
17589         containing_scope = NULL_TREE;
17590     }
17591
17592   /* Ignore function type "scopes" from the C frontend.  They mean that
17593      a tagged type is local to a parmlist of a function declarator, but
17594      that isn't useful to DWARF.  */
17595   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17596     containing_scope = NULL_TREE;
17597
17598   if (containing_scope == NULL_TREE)
17599     scope_die = comp_unit_die;
17600   else if (TYPE_P (containing_scope))
17601     {
17602       /* For types, we can just look up the appropriate DIE.  But
17603          first we check to see if we're in the middle of emitting it
17604          so we know where the new DIE should go.  */
17605       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17606         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17607           break;
17608
17609       if (i < 0)
17610         {
17611           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17612                       || TREE_ASM_WRITTEN (containing_scope));
17613           /*We are not in the middle of emitting the type
17614             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17615           scope_die = lookup_type_die (containing_scope);
17616
17617           /* If none of the current dies are suitable, we get file scope.  */
17618           if (scope_die == NULL)
17619             scope_die = comp_unit_die;
17620         }
17621       else
17622         scope_die = lookup_type_die (containing_scope);
17623     }
17624   else
17625     scope_die = context_die;
17626
17627   return scope_die;
17628 }
17629
17630 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17631
17632 static inline int
17633 local_scope_p (dw_die_ref context_die)
17634 {
17635   for (; context_die; context_die = context_die->die_parent)
17636     if (context_die->die_tag == DW_TAG_inlined_subroutine
17637         || context_die->die_tag == DW_TAG_subprogram)
17638       return 1;
17639
17640   return 0;
17641 }
17642
17643 /* Returns nonzero if CONTEXT_DIE is a class.  */
17644
17645 static inline int
17646 class_scope_p (dw_die_ref context_die)
17647 {
17648   return (context_die
17649           && (context_die->die_tag == DW_TAG_structure_type
17650               || context_die->die_tag == DW_TAG_class_type
17651               || context_die->die_tag == DW_TAG_interface_type
17652               || context_die->die_tag == DW_TAG_union_type));
17653 }
17654
17655 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17656    whether or not to treat a DIE in this context as a declaration.  */
17657
17658 static inline int
17659 class_or_namespace_scope_p (dw_die_ref context_die)
17660 {
17661   return (class_scope_p (context_die)
17662           || (context_die && context_die->die_tag == DW_TAG_namespace));
17663 }
17664
17665 /* Many forms of DIEs require a "type description" attribute.  This
17666    routine locates the proper "type descriptor" die for the type given
17667    by 'type', and adds a DW_AT_type attribute below the given die.  */
17668
17669 static void
17670 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17671                     int decl_volatile, dw_die_ref context_die)
17672 {
17673   enum tree_code code  = TREE_CODE (type);
17674   dw_die_ref type_die  = NULL;
17675
17676   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17677      or fixed-point type, use the inner type.  This is because we have no
17678      support for unnamed types in base_type_die.  This can happen if this is
17679      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17680   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17681       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17682     type = TREE_TYPE (type), code = TREE_CODE (type);
17683
17684   if (code == ERROR_MARK
17685       /* Handle a special case.  For functions whose return type is void, we
17686          generate *no* type attribute.  (Note that no object may have type
17687          `void', so this only applies to function return types).  */
17688       || code == VOID_TYPE)
17689     return;
17690
17691   type_die = modified_type_die (type,
17692                                 decl_const || TYPE_READONLY (type),
17693                                 decl_volatile || TYPE_VOLATILE (type),
17694                                 context_die);
17695
17696   if (type_die != NULL)
17697     add_AT_die_ref (object_die, DW_AT_type, type_die);
17698 }
17699
17700 /* Given an object die, add the calling convention attribute for the
17701    function call type.  */
17702 static void
17703 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17704 {
17705   enum dwarf_calling_convention value = DW_CC_normal;
17706
17707   value = ((enum dwarf_calling_convention)
17708            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17709
17710   /* DWARF doesn't provide a way to identify a program's source-level
17711      entry point.  DW_AT_calling_convention attributes are only meant
17712      to describe functions' calling conventions.  However, lacking a
17713      better way to signal the Fortran main program, we use this for the
17714      time being, following existing custom.  */
17715   if (is_fortran ()
17716       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17717     value = DW_CC_program;
17718
17719   /* Only add the attribute if the backend requests it, and
17720      is not DW_CC_normal.  */
17721   if (value && (value != DW_CC_normal))
17722     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17723 }
17724
17725 /* Given a tree pointer to a struct, class, union, or enum type node, return
17726    a pointer to the (string) tag name for the given type, or zero if the type
17727    was declared without a tag.  */
17728
17729 static const char *
17730 type_tag (const_tree type)
17731 {
17732   const char *name = 0;
17733
17734   if (TYPE_NAME (type) != 0)
17735     {
17736       tree t = 0;
17737
17738       /* Find the IDENTIFIER_NODE for the type name.  */
17739       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17740           && !TYPE_NAMELESS (type))
17741         t = TYPE_NAME (type);
17742
17743       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17744          a TYPE_DECL node, regardless of whether or not a `typedef' was
17745          involved.  */
17746       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17747                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17748         {
17749           /* We want to be extra verbose.  Don't call dwarf_name if
17750              DECL_NAME isn't set.  The default hook for decl_printable_name
17751              doesn't like that, and in this context it's correct to return
17752              0, instead of "<anonymous>" or the like.  */
17753           if (DECL_NAME (TYPE_NAME (type))
17754               && !DECL_NAMELESS (TYPE_NAME (type)))
17755             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17756         }
17757
17758       /* Now get the name as a string, or invent one.  */
17759       if (!name && t != 0)
17760         name = IDENTIFIER_POINTER (t);
17761     }
17762
17763   return (name == 0 || *name == '\0') ? 0 : name;
17764 }
17765
17766 /* Return the type associated with a data member, make a special check
17767    for bit field types.  */
17768
17769 static inline tree
17770 member_declared_type (const_tree member)
17771 {
17772   return (DECL_BIT_FIELD_TYPE (member)
17773           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17774 }
17775
17776 /* Get the decl's label, as described by its RTL. This may be different
17777    from the DECL_NAME name used in the source file.  */
17778
17779 #if 0
17780 static const char *
17781 decl_start_label (tree decl)
17782 {
17783   rtx x;
17784   const char *fnname;
17785
17786   x = DECL_RTL (decl);
17787   gcc_assert (MEM_P (x));
17788
17789   x = XEXP (x, 0);
17790   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17791
17792   fnname = XSTR (x, 0);
17793   return fnname;
17794 }
17795 #endif
17796 \f
17797 /* These routines generate the internal representation of the DIE's for
17798    the compilation unit.  Debugging information is collected by walking
17799    the declaration trees passed in from dwarf2out_decl().  */
17800
17801 static void
17802 gen_array_type_die (tree type, dw_die_ref context_die)
17803 {
17804   dw_die_ref scope_die = scope_die_for (type, context_die);
17805   dw_die_ref array_die;
17806
17807   /* GNU compilers represent multidimensional array types as sequences of one
17808      dimensional array types whose element types are themselves array types.
17809      We sometimes squish that down to a single array_type DIE with multiple
17810      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17811      say that we are allowed to do this kind of compression in C, because
17812      there is no difference between an array of arrays and a multidimensional
17813      array.  We don't do this for Ada to remain as close as possible to the
17814      actual representation, which is especially important against the language
17815      flexibilty wrt arrays of variable size.  */
17816
17817   bool collapse_nested_arrays = !is_ada ();
17818   tree element_type;
17819
17820   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17821      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17822   if (TYPE_STRING_FLAG (type)
17823       && TREE_CODE (type) == ARRAY_TYPE
17824       && is_fortran ()
17825       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17826     {
17827       HOST_WIDE_INT size;
17828
17829       array_die = new_die (DW_TAG_string_type, scope_die, type);
17830       add_name_attribute (array_die, type_tag (type));
17831       equate_type_number_to_die (type, array_die);
17832       size = int_size_in_bytes (type);
17833       if (size >= 0)
17834         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17835       else if (TYPE_DOMAIN (type) != NULL_TREE
17836                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17837                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17838         {
17839           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17840           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17841
17842           size = int_size_in_bytes (TREE_TYPE (szdecl));
17843           if (loc && size > 0)
17844             {
17845               add_AT_location_description (array_die, DW_AT_string_length, loc);
17846               if (size != DWARF2_ADDR_SIZE)
17847                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17848             }
17849         }
17850       return;
17851     }
17852
17853   /* ??? The SGI dwarf reader fails for array of array of enum types
17854      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17855      array type comes before the outer array type.  We thus call gen_type_die
17856      before we new_die and must prevent nested array types collapsing for this
17857      target.  */
17858
17859 #ifdef MIPS_DEBUGGING_INFO
17860   gen_type_die (TREE_TYPE (type), context_die);
17861   collapse_nested_arrays = false;
17862 #endif
17863
17864   array_die = new_die (DW_TAG_array_type, scope_die, type);
17865   add_name_attribute (array_die, type_tag (type));
17866   equate_type_number_to_die (type, array_die);
17867
17868   if (TREE_CODE (type) == VECTOR_TYPE)
17869     {
17870       /* The frontend feeds us a representation for the vector as a struct
17871          containing an array.  Pull out the array type.  */
17872       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17873       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17874     }
17875
17876   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17877   if (is_fortran ()
17878       && TREE_CODE (type) == ARRAY_TYPE
17879       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17880       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17881     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17882
17883 #if 0
17884   /* We default the array ordering.  SDB will probably do
17885      the right things even if DW_AT_ordering is not present.  It's not even
17886      an issue until we start to get into multidimensional arrays anyway.  If
17887      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17888      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17889      and when we find out that we need to put these in, we will only do so
17890      for multidimensional arrays.  */
17891   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17892 #endif
17893
17894 #ifdef MIPS_DEBUGGING_INFO
17895   /* The SGI compilers handle arrays of unknown bound by setting
17896      AT_declaration and not emitting any subrange DIEs.  */
17897   if (! TYPE_DOMAIN (type))
17898     add_AT_flag (array_die, DW_AT_declaration, 1);
17899   else
17900 #endif
17901     add_subscript_info (array_die, type, collapse_nested_arrays);
17902
17903   /* Add representation of the type of the elements of this array type and
17904      emit the corresponding DIE if we haven't done it already.  */
17905   element_type = TREE_TYPE (type);
17906   if (collapse_nested_arrays)
17907     while (TREE_CODE (element_type) == ARRAY_TYPE)
17908       {
17909         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17910           break;
17911         element_type = TREE_TYPE (element_type);
17912       }
17913
17914 #ifndef MIPS_DEBUGGING_INFO
17915   gen_type_die (element_type, context_die);
17916 #endif
17917
17918   add_type_attribute (array_die, element_type, 0, 0, context_die);
17919
17920   if (get_AT (array_die, DW_AT_name))
17921     add_pubtype (type, array_die);
17922 }
17923
17924 static dw_loc_descr_ref
17925 descr_info_loc (tree val, tree base_decl)
17926 {
17927   HOST_WIDE_INT size;
17928   dw_loc_descr_ref loc, loc2;
17929   enum dwarf_location_atom op;
17930
17931   if (val == base_decl)
17932     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17933
17934   switch (TREE_CODE (val))
17935     {
17936     CASE_CONVERT:
17937       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17938     case VAR_DECL:
17939       return loc_descriptor_from_tree (val, 0);
17940     case INTEGER_CST:
17941       if (host_integerp (val, 0))
17942         return int_loc_descriptor (tree_low_cst (val, 0));
17943       break;
17944     case INDIRECT_REF:
17945       size = int_size_in_bytes (TREE_TYPE (val));
17946       if (size < 0)
17947         break;
17948       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17949       if (!loc)
17950         break;
17951       if (size == DWARF2_ADDR_SIZE)
17952         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17953       else
17954         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17955       return loc;
17956     case POINTER_PLUS_EXPR:
17957     case PLUS_EXPR:
17958       if (host_integerp (TREE_OPERAND (val, 1), 1)
17959           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17960              < 16384)
17961         {
17962           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17963           if (!loc)
17964             break;
17965           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17966         }
17967       else
17968         {
17969           op = DW_OP_plus;
17970         do_binop:
17971           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17972           if (!loc)
17973             break;
17974           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17975           if (!loc2)
17976             break;
17977           add_loc_descr (&loc, loc2);
17978           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17979         }
17980       return loc;
17981     case MINUS_EXPR:
17982       op = DW_OP_minus;
17983       goto do_binop;
17984     case MULT_EXPR:
17985       op = DW_OP_mul;
17986       goto do_binop;
17987     case EQ_EXPR:
17988       op = DW_OP_eq;
17989       goto do_binop;
17990     case NE_EXPR:
17991       op = DW_OP_ne;
17992       goto do_binop;
17993     default:
17994       break;
17995     }
17996   return NULL;
17997 }
17998
17999 static void
18000 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18001                       tree val, tree base_decl)
18002 {
18003   dw_loc_descr_ref loc;
18004
18005   if (host_integerp (val, 0))
18006     {
18007       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18008       return;
18009     }
18010
18011   loc = descr_info_loc (val, base_decl);
18012   if (!loc)
18013     return;
18014
18015   add_AT_loc (die, attr, loc);
18016 }
18017
18018 /* This routine generates DIE for array with hidden descriptor, details
18019    are filled into *info by a langhook.  */
18020
18021 static void
18022 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18023                           dw_die_ref context_die)
18024 {
18025   dw_die_ref scope_die = scope_die_for (type, context_die);
18026   dw_die_ref array_die;
18027   int dim;
18028
18029   array_die = new_die (DW_TAG_array_type, scope_die, type);
18030   add_name_attribute (array_die, type_tag (type));
18031   equate_type_number_to_die (type, array_die);
18032
18033   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18034   if (is_fortran ()
18035       && info->ndimensions >= 2)
18036     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18037
18038   if (info->data_location)
18039     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18040                           info->base_decl);
18041   if (info->associated)
18042     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18043                           info->base_decl);
18044   if (info->allocated)
18045     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18046                           info->base_decl);
18047
18048   for (dim = 0; dim < info->ndimensions; dim++)
18049     {
18050       dw_die_ref subrange_die
18051         = new_die (DW_TAG_subrange_type, array_die, NULL);
18052
18053       if (info->dimen[dim].lower_bound)
18054         {
18055           /* If it is the default value, omit it.  */
18056           int dflt;
18057
18058           if (host_integerp (info->dimen[dim].lower_bound, 0)
18059               && (dflt = lower_bound_default ()) != -1
18060               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18061             ;
18062           else
18063             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18064                                   info->dimen[dim].lower_bound,
18065                                   info->base_decl);
18066         }
18067       if (info->dimen[dim].upper_bound)
18068         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18069                               info->dimen[dim].upper_bound,
18070                               info->base_decl);
18071       if (info->dimen[dim].stride)
18072         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18073                               info->dimen[dim].stride,
18074                               info->base_decl);
18075     }
18076
18077   gen_type_die (info->element_type, context_die);
18078   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18079
18080   if (get_AT (array_die, DW_AT_name))
18081     add_pubtype (type, array_die);
18082 }
18083
18084 #if 0
18085 static void
18086 gen_entry_point_die (tree decl, dw_die_ref context_die)
18087 {
18088   tree origin = decl_ultimate_origin (decl);
18089   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18090
18091   if (origin != NULL)
18092     add_abstract_origin_attribute (decl_die, origin);
18093   else
18094     {
18095       add_name_and_src_coords_attributes (decl_die, decl);
18096       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18097                           0, 0, context_die);
18098     }
18099
18100   if (DECL_ABSTRACT (decl))
18101     equate_decl_number_to_die (decl, decl_die);
18102   else
18103     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18104 }
18105 #endif
18106
18107 /* Walk through the list of incomplete types again, trying once more to
18108    emit full debugging info for them.  */
18109
18110 static void
18111 retry_incomplete_types (void)
18112 {
18113   int i;
18114
18115   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18116     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18117                                   DINFO_USAGE_DIR_USE))
18118       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18119 }
18120
18121 /* Determine what tag to use for a record type.  */
18122
18123 static enum dwarf_tag
18124 record_type_tag (tree type)
18125 {
18126   if (! lang_hooks.types.classify_record)
18127     return DW_TAG_structure_type;
18128
18129   switch (lang_hooks.types.classify_record (type))
18130     {
18131     case RECORD_IS_STRUCT:
18132       return DW_TAG_structure_type;
18133
18134     case RECORD_IS_CLASS:
18135       return DW_TAG_class_type;
18136
18137     case RECORD_IS_INTERFACE:
18138       if (dwarf_version >= 3 || !dwarf_strict)
18139         return DW_TAG_interface_type;
18140       return DW_TAG_structure_type;
18141
18142     default:
18143       gcc_unreachable ();
18144     }
18145 }
18146
18147 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18148    include all of the information about the enumeration values also. Each
18149    enumerated type name/value is listed as a child of the enumerated type
18150    DIE.  */
18151
18152 static dw_die_ref
18153 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18154 {
18155   dw_die_ref type_die = lookup_type_die (type);
18156
18157   if (type_die == NULL)
18158     {
18159       type_die = new_die (DW_TAG_enumeration_type,
18160                           scope_die_for (type, context_die), type);
18161       equate_type_number_to_die (type, type_die);
18162       add_name_attribute (type_die, type_tag (type));
18163       if ((dwarf_version >= 4 || !dwarf_strict)
18164           && ENUM_IS_SCOPED (type))
18165         add_AT_flag (type_die, DW_AT_enum_class, 1);
18166     }
18167   else if (! TYPE_SIZE (type))
18168     return type_die;
18169   else
18170     remove_AT (type_die, DW_AT_declaration);
18171
18172   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18173      given enum type is incomplete, do not generate the DW_AT_byte_size
18174      attribute or the DW_AT_element_list attribute.  */
18175   if (TYPE_SIZE (type))
18176     {
18177       tree link;
18178
18179       TREE_ASM_WRITTEN (type) = 1;
18180       add_byte_size_attribute (type_die, type);
18181       if (TYPE_STUB_DECL (type) != NULL_TREE)
18182         {
18183           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18184           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18185         }
18186
18187       /* If the first reference to this type was as the return type of an
18188          inline function, then it may not have a parent.  Fix this now.  */
18189       if (type_die->die_parent == NULL)
18190         add_child_die (scope_die_for (type, context_die), type_die);
18191
18192       for (link = TYPE_VALUES (type);
18193            link != NULL; link = TREE_CHAIN (link))
18194         {
18195           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18196           tree value = TREE_VALUE (link);
18197
18198           add_name_attribute (enum_die,
18199                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18200
18201           if (TREE_CODE (value) == CONST_DECL)
18202             value = DECL_INITIAL (value);
18203
18204           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18205             /* DWARF2 does not provide a way of indicating whether or
18206                not enumeration constants are signed or unsigned.  GDB
18207                always assumes the values are signed, so we output all
18208                values as if they were signed.  That means that
18209                enumeration constants with very large unsigned values
18210                will appear to have negative values in the debugger.  */
18211             add_AT_int (enum_die, DW_AT_const_value,
18212                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18213         }
18214     }
18215   else
18216     add_AT_flag (type_die, DW_AT_declaration, 1);
18217
18218   if (get_AT (type_die, DW_AT_name))
18219     add_pubtype (type, type_die);
18220
18221   return type_die;
18222 }
18223
18224 /* Generate a DIE to represent either a real live formal parameter decl or to
18225    represent just the type of some formal parameter position in some function
18226    type.
18227
18228    Note that this routine is a bit unusual because its argument may be a
18229    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18230    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18231    node.  If it's the former then this function is being called to output a
18232    DIE to represent a formal parameter object (or some inlining thereof).  If
18233    it's the latter, then this function is only being called to output a
18234    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18235    argument type of some subprogram type.
18236    If EMIT_NAME_P is true, name and source coordinate attributes
18237    are emitted.  */
18238
18239 static dw_die_ref
18240 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18241                           dw_die_ref context_die)
18242 {
18243   tree node_or_origin = node ? node : origin;
18244   tree ultimate_origin;
18245   dw_die_ref parm_die
18246     = new_die (DW_TAG_formal_parameter, context_die, node);
18247
18248   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18249     {
18250     case tcc_declaration:
18251       ultimate_origin = decl_ultimate_origin (node_or_origin);
18252       if (node || ultimate_origin)
18253         origin = ultimate_origin;
18254       if (origin != NULL)
18255         add_abstract_origin_attribute (parm_die, origin);
18256       else if (emit_name_p)
18257         add_name_and_src_coords_attributes (parm_die, node);
18258       if (origin == NULL
18259           || (! DECL_ABSTRACT (node_or_origin)
18260               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18261                                            decl_function_context
18262                                                             (node_or_origin))))
18263         {
18264           tree type = TREE_TYPE (node_or_origin);
18265           if (decl_by_reference_p (node_or_origin))
18266             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18267                                 context_die);
18268           else
18269             add_type_attribute (parm_die, type,
18270                                 TREE_READONLY (node_or_origin),
18271                                 TREE_THIS_VOLATILE (node_or_origin),
18272                                 context_die);
18273         }
18274       if (origin == NULL && DECL_ARTIFICIAL (node))
18275         add_AT_flag (parm_die, DW_AT_artificial, 1);
18276
18277       if (node && node != origin)
18278         equate_decl_number_to_die (node, parm_die);
18279       if (! DECL_ABSTRACT (node_or_origin))
18280         add_location_or_const_value_attribute (parm_die, node_or_origin,
18281                                                DW_AT_location);
18282
18283       break;
18284
18285     case tcc_type:
18286       /* We were called with some kind of a ..._TYPE node.  */
18287       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18288       break;
18289
18290     default:
18291       gcc_unreachable ();
18292     }
18293
18294   return parm_die;
18295 }
18296
18297 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18298    children DW_TAG_formal_parameter DIEs representing the arguments of the
18299    parameter pack.
18300
18301    PARM_PACK must be a function parameter pack.
18302    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18303    must point to the subsequent arguments of the function PACK_ARG belongs to.
18304    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18305    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18306    following the last one for which a DIE was generated.  */
18307
18308 static dw_die_ref
18309 gen_formal_parameter_pack_die  (tree parm_pack,
18310                                 tree pack_arg,
18311                                 dw_die_ref subr_die,
18312                                 tree *next_arg)
18313 {
18314   tree arg;
18315   dw_die_ref parm_pack_die;
18316
18317   gcc_assert (parm_pack
18318               && lang_hooks.function_parameter_pack_p (parm_pack)
18319               && subr_die);
18320
18321   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18322   add_src_coords_attributes (parm_pack_die, parm_pack);
18323
18324   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18325     {
18326       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18327                                                                  parm_pack))
18328         break;
18329       gen_formal_parameter_die (arg, NULL,
18330                                 false /* Don't emit name attribute.  */,
18331                                 parm_pack_die);
18332     }
18333   if (next_arg)
18334     *next_arg = arg;
18335   return parm_pack_die;
18336 }
18337
18338 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18339    at the end of an (ANSI prototyped) formal parameters list.  */
18340
18341 static void
18342 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18343 {
18344   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18345 }
18346
18347 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18348    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18349    parameters as specified in some function type specification (except for
18350    those which appear as part of a function *definition*).  */
18351
18352 static void
18353 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18354 {
18355   tree link;
18356   tree formal_type = NULL;
18357   tree first_parm_type;
18358   tree arg;
18359
18360   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18361     {
18362       arg = DECL_ARGUMENTS (function_or_method_type);
18363       function_or_method_type = TREE_TYPE (function_or_method_type);
18364     }
18365   else
18366     arg = NULL_TREE;
18367
18368   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18369
18370   /* Make our first pass over the list of formal parameter types and output a
18371      DW_TAG_formal_parameter DIE for each one.  */
18372   for (link = first_parm_type; link; )
18373     {
18374       dw_die_ref parm_die;
18375
18376       formal_type = TREE_VALUE (link);
18377       if (formal_type == void_type_node)
18378         break;
18379
18380       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18381       parm_die = gen_formal_parameter_die (formal_type, NULL,
18382                                            true /* Emit name attribute.  */,
18383                                            context_die);
18384       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18385           && link == first_parm_type)
18386         {
18387           add_AT_flag (parm_die, DW_AT_artificial, 1);
18388           if (dwarf_version >= 3 || !dwarf_strict)
18389             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18390         }
18391       else if (arg && DECL_ARTIFICIAL (arg))
18392         add_AT_flag (parm_die, DW_AT_artificial, 1);
18393
18394       link = TREE_CHAIN (link);
18395       if (arg)
18396         arg = DECL_CHAIN (arg);
18397     }
18398
18399   /* If this function type has an ellipsis, add a
18400      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18401   if (formal_type != void_type_node)
18402     gen_unspecified_parameters_die (function_or_method_type, context_die);
18403
18404   /* Make our second (and final) pass over the list of formal parameter types
18405      and output DIEs to represent those types (as necessary).  */
18406   for (link = TYPE_ARG_TYPES (function_or_method_type);
18407        link && TREE_VALUE (link);
18408        link = TREE_CHAIN (link))
18409     gen_type_die (TREE_VALUE (link), context_die);
18410 }
18411
18412 /* We want to generate the DIE for TYPE so that we can generate the
18413    die for MEMBER, which has been defined; we will need to refer back
18414    to the member declaration nested within TYPE.  If we're trying to
18415    generate minimal debug info for TYPE, processing TYPE won't do the
18416    trick; we need to attach the member declaration by hand.  */
18417
18418 static void
18419 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18420 {
18421   gen_type_die (type, context_die);
18422
18423   /* If we're trying to avoid duplicate debug info, we may not have
18424      emitted the member decl for this function.  Emit it now.  */
18425   if (TYPE_STUB_DECL (type)
18426       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18427       && ! lookup_decl_die (member))
18428     {
18429       dw_die_ref type_die;
18430       gcc_assert (!decl_ultimate_origin (member));
18431
18432       push_decl_scope (type);
18433       type_die = lookup_type_die (type);
18434       if (TREE_CODE (member) == FUNCTION_DECL)
18435         gen_subprogram_die (member, type_die);
18436       else if (TREE_CODE (member) == FIELD_DECL)
18437         {
18438           /* Ignore the nameless fields that are used to skip bits but handle
18439              C++ anonymous unions and structs.  */
18440           if (DECL_NAME (member) != NULL_TREE
18441               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18442               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18443             {
18444               gen_type_die (member_declared_type (member), type_die);
18445               gen_field_die (member, type_die);
18446             }
18447         }
18448       else
18449         gen_variable_die (member, NULL_TREE, type_die);
18450
18451       pop_decl_scope ();
18452     }
18453 }
18454
18455 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18456    may later generate inlined and/or out-of-line instances of.  */
18457
18458 static void
18459 dwarf2out_abstract_function (tree decl)
18460 {
18461   dw_die_ref old_die;
18462   tree save_fn;
18463   tree context;
18464   int was_abstract;
18465   htab_t old_decl_loc_table;
18466
18467   /* Make sure we have the actual abstract inline, not a clone.  */
18468   decl = DECL_ORIGIN (decl);
18469
18470   old_die = lookup_decl_die (decl);
18471   if (old_die && get_AT (old_die, DW_AT_inline))
18472     /* We've already generated the abstract instance.  */
18473     return;
18474
18475   /* We can be called while recursively when seeing block defining inlined subroutine
18476      DIE.  Be sure to not clobber the outer location table nor use it or we would
18477      get locations in abstract instantces.  */
18478   old_decl_loc_table = decl_loc_table;
18479   decl_loc_table = NULL;
18480
18481   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18482      we don't get confused by DECL_ABSTRACT.  */
18483   if (debug_info_level > DINFO_LEVEL_TERSE)
18484     {
18485       context = decl_class_context (decl);
18486       if (context)
18487         gen_type_die_for_member
18488           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18489     }
18490
18491   /* Pretend we've just finished compiling this function.  */
18492   save_fn = current_function_decl;
18493   current_function_decl = decl;
18494   push_cfun (DECL_STRUCT_FUNCTION (decl));
18495
18496   was_abstract = DECL_ABSTRACT (decl);
18497   set_decl_abstract_flags (decl, 1);
18498   dwarf2out_decl (decl);
18499   if (! was_abstract)
18500     set_decl_abstract_flags (decl, 0);
18501
18502   current_function_decl = save_fn;
18503   decl_loc_table = old_decl_loc_table;
18504   pop_cfun ();
18505 }
18506
18507 /* Helper function of premark_used_types() which gets called through
18508    htab_traverse.
18509
18510    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18511    marked as unused by prune_unused_types.  */
18512
18513 static int
18514 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18515 {
18516   tree type;
18517   dw_die_ref die;
18518
18519   type = (tree) *slot;
18520   die = lookup_type_die (type);
18521   if (die != NULL)
18522     die->die_perennial_p = 1;
18523   return 1;
18524 }
18525
18526 /* Helper function of premark_types_used_by_global_vars which gets called
18527    through htab_traverse.
18528
18529    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18530    marked as unused by prune_unused_types. The DIE of the type is marked
18531    only if the global variable using the type will actually be emitted.  */
18532
18533 static int
18534 premark_types_used_by_global_vars_helper (void **slot,
18535                                           void *data ATTRIBUTE_UNUSED)
18536 {
18537   struct types_used_by_vars_entry *entry;
18538   dw_die_ref die;
18539
18540   entry = (struct types_used_by_vars_entry *) *slot;
18541   gcc_assert (entry->type != NULL
18542               && entry->var_decl != NULL);
18543   die = lookup_type_die (entry->type);
18544   if (die)
18545     {
18546       /* Ask cgraph if the global variable really is to be emitted.
18547          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18548       struct varpool_node *node = varpool_get_node (entry->var_decl);
18549       if (node && node->needed)
18550         {
18551           die->die_perennial_p = 1;
18552           /* Keep the parent DIEs as well.  */
18553           while ((die = die->die_parent) && die->die_perennial_p == 0)
18554             die->die_perennial_p = 1;
18555         }
18556     }
18557   return 1;
18558 }
18559
18560 /* Mark all members of used_types_hash as perennial.  */
18561
18562 static void
18563 premark_used_types (void)
18564 {
18565   if (cfun && cfun->used_types_hash)
18566     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18567 }
18568
18569 /* Mark all members of types_used_by_vars_entry as perennial.  */
18570
18571 static void
18572 premark_types_used_by_global_vars (void)
18573 {
18574   if (types_used_by_vars_hash)
18575     htab_traverse (types_used_by_vars_hash,
18576                    premark_types_used_by_global_vars_helper, NULL);
18577 }
18578
18579 /* Generate a DIE to represent a declared function (either file-scope or
18580    block-local).  */
18581
18582 static void
18583 gen_subprogram_die (tree decl, dw_die_ref context_die)
18584 {
18585   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18586   tree origin = decl_ultimate_origin (decl);
18587   dw_die_ref subr_die;
18588   tree fn_arg_types;
18589   tree outer_scope;
18590   dw_die_ref old_die = lookup_decl_die (decl);
18591   int declaration = (current_function_decl != decl
18592                      || class_or_namespace_scope_p (context_die));
18593
18594   premark_used_types ();
18595
18596   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18597      started to generate the abstract instance of an inline, decided to output
18598      its containing class, and proceeded to emit the declaration of the inline
18599      from the member list for the class.  If so, DECLARATION takes priority;
18600      we'll get back to the abstract instance when done with the class.  */
18601
18602   /* The class-scope declaration DIE must be the primary DIE.  */
18603   if (origin && declaration && class_or_namespace_scope_p (context_die))
18604     {
18605       origin = NULL;
18606       gcc_assert (!old_die);
18607     }
18608
18609   /* Now that the C++ front end lazily declares artificial member fns, we
18610      might need to retrofit the declaration into its class.  */
18611   if (!declaration && !origin && !old_die
18612       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18613       && !class_or_namespace_scope_p (context_die)
18614       && debug_info_level > DINFO_LEVEL_TERSE)
18615     old_die = force_decl_die (decl);
18616
18617   if (origin != NULL)
18618     {
18619       gcc_assert (!declaration || local_scope_p (context_die));
18620
18621       /* Fixup die_parent for the abstract instance of a nested
18622          inline function.  */
18623       if (old_die && old_die->die_parent == NULL)
18624         add_child_die (context_die, old_die);
18625
18626       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18627       add_abstract_origin_attribute (subr_die, origin);
18628     }
18629   else if (old_die)
18630     {
18631       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18632       struct dwarf_file_data * file_index = lookup_filename (s.file);
18633
18634       if (!get_AT_flag (old_die, DW_AT_declaration)
18635           /* We can have a normal definition following an inline one in the
18636              case of redefinition of GNU C extern inlines.
18637              It seems reasonable to use AT_specification in this case.  */
18638           && !get_AT (old_die, DW_AT_inline))
18639         {
18640           /* Detect and ignore this case, where we are trying to output
18641              something we have already output.  */
18642           return;
18643         }
18644
18645       /* If the definition comes from the same place as the declaration,
18646          maybe use the old DIE.  We always want the DIE for this function
18647          that has the *_pc attributes to be under comp_unit_die so the
18648          debugger can find it.  We also need to do this for abstract
18649          instances of inlines, since the spec requires the out-of-line copy
18650          to have the same parent.  For local class methods, this doesn't
18651          apply; we just use the old DIE.  */
18652       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18653           && (DECL_ARTIFICIAL (decl)
18654               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18655                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18656                       == (unsigned) s.line))))
18657         {
18658           subr_die = old_die;
18659
18660           /* Clear out the declaration attribute and the formal parameters.
18661              Do not remove all children, because it is possible that this
18662              declaration die was forced using force_decl_die(). In such
18663              cases die that forced declaration die (e.g. TAG_imported_module)
18664              is one of the children that we do not want to remove.  */
18665           remove_AT (subr_die, DW_AT_declaration);
18666           remove_AT (subr_die, DW_AT_object_pointer);
18667           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18668         }
18669       else
18670         {
18671           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18672           add_AT_specification (subr_die, old_die);
18673           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18674             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18675           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18676             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18677         }
18678     }
18679   else
18680     {
18681       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18682
18683       if (TREE_PUBLIC (decl))
18684         add_AT_flag (subr_die, DW_AT_external, 1);
18685
18686       add_name_and_src_coords_attributes (subr_die, decl);
18687       if (debug_info_level > DINFO_LEVEL_TERSE)
18688         {
18689           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18690           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18691                               0, 0, context_die);
18692         }
18693
18694       add_pure_or_virtual_attribute (subr_die, decl);
18695       if (DECL_ARTIFICIAL (decl))
18696         add_AT_flag (subr_die, DW_AT_artificial, 1);
18697
18698       add_accessibility_attribute (subr_die, decl);
18699     }
18700
18701   if (declaration)
18702     {
18703       if (!old_die || !get_AT (old_die, DW_AT_inline))
18704         {
18705           add_AT_flag (subr_die, DW_AT_declaration, 1);
18706
18707           /* If this is an explicit function declaration then generate
18708              a DW_AT_explicit attribute.  */
18709           if (lang_hooks.decls.function_decl_explicit_p (decl)
18710               && (dwarf_version >= 3 || !dwarf_strict))
18711             add_AT_flag (subr_die, DW_AT_explicit, 1);
18712
18713           /* The first time we see a member function, it is in the context of
18714              the class to which it belongs.  We make sure of this by emitting
18715              the class first.  The next time is the definition, which is
18716              handled above.  The two may come from the same source text.
18717
18718              Note that force_decl_die() forces function declaration die. It is
18719              later reused to represent definition.  */
18720           equate_decl_number_to_die (decl, subr_die);
18721         }
18722     }
18723   else if (DECL_ABSTRACT (decl))
18724     {
18725       if (DECL_DECLARED_INLINE_P (decl))
18726         {
18727           if (cgraph_function_possibly_inlined_p (decl))
18728             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18729           else
18730             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18731         }
18732       else
18733         {
18734           if (cgraph_function_possibly_inlined_p (decl))
18735             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18736           else
18737             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18738         }
18739
18740       if (DECL_DECLARED_INLINE_P (decl)
18741           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18742         add_AT_flag (subr_die, DW_AT_artificial, 1);
18743
18744       equate_decl_number_to_die (decl, subr_die);
18745     }
18746   else if (!DECL_EXTERNAL (decl))
18747     {
18748       HOST_WIDE_INT cfa_fb_offset;
18749
18750       if (!old_die || !get_AT (old_die, DW_AT_inline))
18751         equate_decl_number_to_die (decl, subr_die);
18752
18753       if (!flag_reorder_blocks_and_partition)
18754         {
18755           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18756                                        current_function_funcdef_no);
18757           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18758           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18759                                        current_function_funcdef_no);
18760           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18761
18762 #if VMS_DEBUGGING_INFO
18763       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18764          Section 2.3 Prologue and Epilogue Attributes:
18765          When a breakpoint is set on entry to a function, it is generally
18766          desirable for execution to be suspended, not on the very first
18767          instruction of the function, but rather at a point after the
18768          function's frame has been set up, after any language defined local
18769          declaration processing has been completed, and before execution of
18770          the first statement of the function begins. Debuggers generally
18771          cannot properly determine where this point is.  Similarly for a
18772          breakpoint set on exit from a function. The prologue and epilogue
18773          attributes allow a compiler to communicate the location(s) to use.  */
18774
18775       {
18776         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18777
18778         if (fde->dw_fde_vms_end_prologue)
18779           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18780             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18781
18782         if (fde->dw_fde_vms_begin_epilogue)
18783           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18784             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18785       }
18786 #endif
18787
18788           add_pubname (decl, subr_die);
18789           add_arange (decl, subr_die);
18790         }
18791       else
18792         {  /* Do nothing for now; maybe need to duplicate die, one for
18793               hot section and one for cold section, then use the hot/cold
18794               section begin/end labels to generate the aranges...  */
18795           /*
18796             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18797             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18798             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18799             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18800
18801             add_pubname (decl, subr_die);
18802             add_arange (decl, subr_die);
18803             add_arange (decl, subr_die);
18804            */
18805         }
18806
18807 #ifdef MIPS_DEBUGGING_INFO
18808       /* Add a reference to the FDE for this routine.  */
18809       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18810 #endif
18811
18812       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18813
18814       /* We define the "frame base" as the function's CFA.  This is more
18815          convenient for several reasons: (1) It's stable across the prologue
18816          and epilogue, which makes it better than just a frame pointer,
18817          (2) With dwarf3, there exists a one-byte encoding that allows us
18818          to reference the .debug_frame data by proxy, but failing that,
18819          (3) We can at least reuse the code inspection and interpretation
18820          code that determines the CFA position at various points in the
18821          function.  */
18822       if (dwarf_version >= 3)
18823         {
18824           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18825           add_AT_loc (subr_die, DW_AT_frame_base, op);
18826         }
18827       else
18828         {
18829           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18830           if (list->dw_loc_next)
18831             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18832           else
18833             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18834         }
18835
18836       /* Compute a displacement from the "steady-state frame pointer" to
18837          the CFA.  The former is what all stack slots and argument slots
18838          will reference in the rtl; the later is what we've told the
18839          debugger about.  We'll need to adjust all frame_base references
18840          by this displacement.  */
18841       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18842
18843       if (cfun->static_chain_decl)
18844         add_AT_location_description (subr_die, DW_AT_static_link,
18845                  loc_list_from_tree (cfun->static_chain_decl, 2));
18846     }
18847
18848   /* Generate child dies for template paramaters.  */
18849   if (debug_info_level > DINFO_LEVEL_TERSE)
18850     gen_generic_params_dies (decl);
18851
18852   /* Now output descriptions of the arguments for this function. This gets
18853      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18854      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18855      `...' at the end of the formal parameter list.  In order to find out if
18856      there was a trailing ellipsis or not, we must instead look at the type
18857      associated with the FUNCTION_DECL.  This will be a node of type
18858      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18859      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18860      an ellipsis at the end.  */
18861
18862   /* In the case where we are describing a mere function declaration, all we
18863      need to do here (and all we *can* do here) is to describe the *types* of
18864      its formal parameters.  */
18865   if (debug_info_level <= DINFO_LEVEL_TERSE)
18866     ;
18867   else if (declaration)
18868     gen_formal_types_die (decl, subr_die);
18869   else
18870     {
18871       /* Generate DIEs to represent all known formal parameters.  */
18872       tree parm = DECL_ARGUMENTS (decl);
18873       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18874       tree generic_decl_parm = generic_decl
18875                                 ? DECL_ARGUMENTS (generic_decl)
18876                                 : NULL;
18877
18878       /* Now we want to walk the list of parameters of the function and
18879          emit their relevant DIEs.
18880
18881          We consider the case of DECL being an instance of a generic function
18882          as well as it being a normal function.
18883
18884          If DECL is an instance of a generic function we walk the
18885          parameters of the generic function declaration _and_ the parameters of
18886          DECL itself. This is useful because we want to emit specific DIEs for
18887          function parameter packs and those are declared as part of the
18888          generic function declaration. In that particular case,
18889          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18890          That DIE has children DIEs representing the set of arguments
18891          of the pack. Note that the set of pack arguments can be empty.
18892          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18893          children DIE.
18894
18895          Otherwise, we just consider the parameters of DECL.  */
18896       while (generic_decl_parm || parm)
18897         {
18898           if (generic_decl_parm
18899               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18900             gen_formal_parameter_pack_die (generic_decl_parm,
18901                                            parm, subr_die,
18902                                            &parm);
18903           else if (parm)
18904             {
18905               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18906
18907               if (parm == DECL_ARGUMENTS (decl)
18908                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18909                   && parm_die
18910                   && (dwarf_version >= 3 || !dwarf_strict))
18911                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18912
18913               parm = DECL_CHAIN (parm);
18914             }
18915
18916           if (generic_decl_parm)
18917             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18918         }
18919
18920       /* Decide whether we need an unspecified_parameters DIE at the end.
18921          There are 2 more cases to do this for: 1) the ansi ... declaration -
18922          this is detectable when the end of the arg list is not a
18923          void_type_node 2) an unprototyped function declaration (not a
18924          definition).  This just means that we have no info about the
18925          parameters at all.  */
18926       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18927       if (fn_arg_types != NULL)
18928         {
18929           /* This is the prototyped case, check for....  */
18930           if (stdarg_p (TREE_TYPE (decl)))
18931             gen_unspecified_parameters_die (decl, subr_die);
18932         }
18933       else if (DECL_INITIAL (decl) == NULL_TREE)
18934         gen_unspecified_parameters_die (decl, subr_die);
18935     }
18936
18937   /* Output Dwarf info for all of the stuff within the body of the function
18938      (if it has one - it may be just a declaration).  */
18939   outer_scope = DECL_INITIAL (decl);
18940
18941   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18942      a function.  This BLOCK actually represents the outermost binding contour
18943      for the function, i.e. the contour in which the function's formal
18944      parameters and labels get declared. Curiously, it appears that the front
18945      end doesn't actually put the PARM_DECL nodes for the current function onto
18946      the BLOCK_VARS list for this outer scope, but are strung off of the
18947      DECL_ARGUMENTS list for the function instead.
18948
18949      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18950      the LABEL_DECL nodes for the function however, and we output DWARF info
18951      for those in decls_for_scope.  Just within the `outer_scope' there will be
18952      a BLOCK node representing the function's outermost pair of curly braces,
18953      and any blocks used for the base and member initializers of a C++
18954      constructor function.  */
18955   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18956     {
18957       /* Emit a DW_TAG_variable DIE for a named return value.  */
18958       if (DECL_NAME (DECL_RESULT (decl)))
18959         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18960
18961       current_function_has_inlines = 0;
18962       decls_for_scope (outer_scope, subr_die, 0);
18963
18964 #if 0 && defined (MIPS_DEBUGGING_INFO)
18965       if (current_function_has_inlines)
18966         {
18967           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18968           if (! comp_unit_has_inlines)
18969             {
18970               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18971               comp_unit_has_inlines = 1;
18972             }
18973         }
18974 #endif
18975     }
18976   /* Add the calling convention attribute if requested.  */
18977   add_calling_convention_attribute (subr_die, decl);
18978
18979 }
18980
18981 /* Returns a hash value for X (which really is a die_struct).  */
18982
18983 static hashval_t
18984 common_block_die_table_hash (const void *x)
18985 {
18986   const_dw_die_ref d = (const_dw_die_ref) x;
18987   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18988 }
18989
18990 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18991    as decl_id and die_parent of die_struct Y.  */
18992
18993 static int
18994 common_block_die_table_eq (const void *x, const void *y)
18995 {
18996   const_dw_die_ref d = (const_dw_die_ref) x;
18997   const_dw_die_ref e = (const_dw_die_ref) y;
18998   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18999 }
19000
19001 /* Generate a DIE to represent a declared data object.
19002    Either DECL or ORIGIN must be non-null.  */
19003
19004 static void
19005 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19006 {
19007   HOST_WIDE_INT off;
19008   tree com_decl;
19009   tree decl_or_origin = decl ? decl : origin;
19010   tree ultimate_origin;
19011   dw_die_ref var_die;
19012   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19013   dw_die_ref origin_die;
19014   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19015                       || class_or_namespace_scope_p (context_die));
19016   bool specialization_p = false;
19017
19018   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19019   if (decl || ultimate_origin)
19020     origin = ultimate_origin;
19021   com_decl = fortran_common (decl_or_origin, &off);
19022
19023   /* Symbol in common gets emitted as a child of the common block, in the form
19024      of a data member.  */
19025   if (com_decl)
19026     {
19027       dw_die_ref com_die;
19028       dw_loc_list_ref loc;
19029       die_node com_die_arg;
19030
19031       var_die = lookup_decl_die (decl_or_origin);
19032       if (var_die)
19033         {
19034           if (get_AT (var_die, DW_AT_location) == NULL)
19035             {
19036               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19037               if (loc)
19038                 {
19039                   if (off)
19040                     {
19041                       /* Optimize the common case.  */
19042                       if (single_element_loc_list_p (loc)
19043                           && loc->expr->dw_loc_opc == DW_OP_addr
19044                           && loc->expr->dw_loc_next == NULL
19045                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19046                              == SYMBOL_REF)
19047                         loc->expr->dw_loc_oprnd1.v.val_addr
19048                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19049                         else
19050                           loc_list_plus_const (loc, off);
19051                     }
19052                   add_AT_location_description (var_die, DW_AT_location, loc);
19053                   remove_AT (var_die, DW_AT_declaration);
19054                 }
19055             }
19056           return;
19057         }
19058
19059       if (common_block_die_table == NULL)
19060         common_block_die_table
19061           = htab_create_ggc (10, common_block_die_table_hash,
19062                              common_block_die_table_eq, NULL);
19063
19064       com_die_arg.decl_id = DECL_UID (com_decl);
19065       com_die_arg.die_parent = context_die;
19066       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19067       loc = loc_list_from_tree (com_decl, 2);
19068       if (com_die == NULL)
19069         {
19070           const char *cnam
19071             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19072           void **slot;
19073
19074           com_die = new_die (DW_TAG_common_block, context_die, decl);
19075           add_name_and_src_coords_attributes (com_die, com_decl);
19076           if (loc)
19077             {
19078               add_AT_location_description (com_die, DW_AT_location, loc);
19079               /* Avoid sharing the same loc descriptor between
19080                  DW_TAG_common_block and DW_TAG_variable.  */
19081               loc = loc_list_from_tree (com_decl, 2);
19082             }
19083           else if (DECL_EXTERNAL (decl))
19084             add_AT_flag (com_die, DW_AT_declaration, 1);
19085           add_pubname_string (cnam, com_die); /* ??? needed? */
19086           com_die->decl_id = DECL_UID (com_decl);
19087           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19088           *slot = (void *) com_die;
19089         }
19090       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19091         {
19092           add_AT_location_description (com_die, DW_AT_location, loc);
19093           loc = loc_list_from_tree (com_decl, 2);
19094           remove_AT (com_die, DW_AT_declaration);
19095         }
19096       var_die = new_die (DW_TAG_variable, com_die, decl);
19097       add_name_and_src_coords_attributes (var_die, decl);
19098       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19099                           TREE_THIS_VOLATILE (decl), context_die);
19100       add_AT_flag (var_die, DW_AT_external, 1);
19101       if (loc)
19102         {
19103           if (off)
19104             {
19105               /* Optimize the common case.  */
19106               if (single_element_loc_list_p (loc)
19107                   && loc->expr->dw_loc_opc == DW_OP_addr
19108                   && loc->expr->dw_loc_next == NULL
19109                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19110                 loc->expr->dw_loc_oprnd1.v.val_addr
19111                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19112               else
19113                 loc_list_plus_const (loc, off);
19114             }
19115           add_AT_location_description (var_die, DW_AT_location, loc);
19116         }
19117       else if (DECL_EXTERNAL (decl))
19118         add_AT_flag (var_die, DW_AT_declaration, 1);
19119       equate_decl_number_to_die (decl, var_die);
19120       return;
19121     }
19122
19123   /* If the compiler emitted a definition for the DECL declaration
19124      and if we already emitted a DIE for it, don't emit a second
19125      DIE for it again. Allow re-declarations of DECLs that are
19126      inside functions, though.  */
19127   if (old_die && declaration && !local_scope_p (context_die))
19128     return;
19129
19130   /* For static data members, the declaration in the class is supposed
19131      to have DW_TAG_member tag; the specification should still be
19132      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19133   if (declaration && class_scope_p (context_die))
19134     var_die = new_die (DW_TAG_member, context_die, decl);
19135   else
19136     var_die = new_die (DW_TAG_variable, context_die, decl);
19137
19138   origin_die = NULL;
19139   if (origin != NULL)
19140     origin_die = add_abstract_origin_attribute (var_die, origin);
19141
19142   /* Loop unrolling can create multiple blocks that refer to the same
19143      static variable, so we must test for the DW_AT_declaration flag.
19144
19145      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19146      copy decls and set the DECL_ABSTRACT flag on them instead of
19147      sharing them.
19148
19149      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19150
19151      ??? The declare_in_namespace support causes us to get two DIEs for one
19152      variable, both of which are declarations.  We want to avoid considering
19153      one to be a specification, so we must test that this DIE is not a
19154      declaration.  */
19155   else if (old_die && TREE_STATIC (decl) && ! declaration
19156            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19157     {
19158       /* This is a definition of a C++ class level static.  */
19159       add_AT_specification (var_die, old_die);
19160       specialization_p = true;
19161       if (DECL_NAME (decl))
19162         {
19163           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19164           struct dwarf_file_data * file_index = lookup_filename (s.file);
19165
19166           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19167             add_AT_file (var_die, DW_AT_decl_file, file_index);
19168
19169           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19170             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19171
19172           if (old_die->die_tag == DW_TAG_member)
19173             add_linkage_name (var_die, decl);
19174         }
19175     }
19176   else
19177     add_name_and_src_coords_attributes (var_die, decl);
19178
19179   if ((origin == NULL && !specialization_p)
19180       || (origin != NULL
19181           && !DECL_ABSTRACT (decl_or_origin)
19182           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19183                                        decl_function_context
19184                                                         (decl_or_origin))))
19185     {
19186       tree type = TREE_TYPE (decl_or_origin);
19187
19188       if (decl_by_reference_p (decl_or_origin))
19189         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19190       else
19191         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19192                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19193     }
19194
19195   if (origin == NULL && !specialization_p)
19196     {
19197       if (TREE_PUBLIC (decl))
19198         add_AT_flag (var_die, DW_AT_external, 1);
19199
19200       if (DECL_ARTIFICIAL (decl))
19201         add_AT_flag (var_die, DW_AT_artificial, 1);
19202
19203       add_accessibility_attribute (var_die, decl);
19204     }
19205
19206   if (declaration)
19207     add_AT_flag (var_die, DW_AT_declaration, 1);
19208
19209   if (decl && (DECL_ABSTRACT (decl) || declaration))
19210     equate_decl_number_to_die (decl, var_die);
19211
19212   if (! declaration
19213       && (! DECL_ABSTRACT (decl_or_origin)
19214           /* Local static vars are shared between all clones/inlines,
19215              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19216              already set.  */
19217           || (TREE_CODE (decl_or_origin) == VAR_DECL
19218               && TREE_STATIC (decl_or_origin)
19219               && DECL_RTL_SET_P (decl_or_origin)))
19220       /* When abstract origin already has DW_AT_location attribute, no need
19221          to add it again.  */
19222       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19223     {
19224       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19225           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19226         defer_location (decl_or_origin, var_die);
19227       else
19228         add_location_or_const_value_attribute (var_die,
19229                                                decl_or_origin,
19230                                                DW_AT_location);
19231       add_pubname (decl_or_origin, var_die);
19232     }
19233   else
19234     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19235 }
19236
19237 /* Generate a DIE to represent a named constant.  */
19238
19239 static void
19240 gen_const_die (tree decl, dw_die_ref context_die)
19241 {
19242   dw_die_ref const_die;
19243   tree type = TREE_TYPE (decl);
19244
19245   const_die = new_die (DW_TAG_constant, context_die, decl);
19246   add_name_and_src_coords_attributes (const_die, decl);
19247   add_type_attribute (const_die, type, 1, 0, context_die);
19248   if (TREE_PUBLIC (decl))
19249     add_AT_flag (const_die, DW_AT_external, 1);
19250   if (DECL_ARTIFICIAL (decl))
19251     add_AT_flag (const_die, DW_AT_artificial, 1);
19252   tree_add_const_value_attribute_for_decl (const_die, decl);
19253 }
19254
19255 /* Generate a DIE to represent a label identifier.  */
19256
19257 static void
19258 gen_label_die (tree decl, dw_die_ref context_die)
19259 {
19260   tree origin = decl_ultimate_origin (decl);
19261   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19262   rtx insn;
19263   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19264
19265   if (origin != NULL)
19266     add_abstract_origin_attribute (lbl_die, origin);
19267   else
19268     add_name_and_src_coords_attributes (lbl_die, decl);
19269
19270   if (DECL_ABSTRACT (decl))
19271     equate_decl_number_to_die (decl, lbl_die);
19272   else
19273     {
19274       insn = DECL_RTL_IF_SET (decl);
19275
19276       /* Deleted labels are programmer specified labels which have been
19277          eliminated because of various optimizations.  We still emit them
19278          here so that it is possible to put breakpoints on them.  */
19279       if (insn
19280           && (LABEL_P (insn)
19281               || ((NOTE_P (insn)
19282                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19283         {
19284           /* When optimization is enabled (via -O) some parts of the compiler
19285              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19286              represent source-level labels which were explicitly declared by
19287              the user.  This really shouldn't be happening though, so catch
19288              it if it ever does happen.  */
19289           gcc_assert (!INSN_DELETED_P (insn));
19290
19291           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19292           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19293         }
19294     }
19295 }
19296
19297 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19298    attributes to the DIE for a block STMT, to describe where the inlined
19299    function was called from.  This is similar to add_src_coords_attributes.  */
19300
19301 static inline void
19302 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19303 {
19304   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19305
19306   if (dwarf_version >= 3 || !dwarf_strict)
19307     {
19308       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19309       add_AT_unsigned (die, DW_AT_call_line, s.line);
19310     }
19311 }
19312
19313
19314 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19315    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19316
19317 static inline void
19318 add_high_low_attributes (tree stmt, dw_die_ref die)
19319 {
19320   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19321
19322   if (BLOCK_FRAGMENT_CHAIN (stmt)
19323       && (dwarf_version >= 3 || !dwarf_strict))
19324     {
19325       tree chain;
19326
19327       if (inlined_function_outer_scope_p (stmt))
19328         {
19329           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19330                                        BLOCK_NUMBER (stmt));
19331           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19332         }
19333
19334       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19335
19336       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19337       do
19338         {
19339           add_ranges (chain);
19340           chain = BLOCK_FRAGMENT_CHAIN (chain);
19341         }
19342       while (chain);
19343       add_ranges (NULL);
19344     }
19345   else
19346     {
19347       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19348                                    BLOCK_NUMBER (stmt));
19349       add_AT_lbl_id (die, DW_AT_low_pc, label);
19350       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19351                                    BLOCK_NUMBER (stmt));
19352       add_AT_lbl_id (die, DW_AT_high_pc, label);
19353     }
19354 }
19355
19356 /* Generate a DIE for a lexical block.  */
19357
19358 static void
19359 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19360 {
19361   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19362
19363   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19364     add_high_low_attributes (stmt, stmt_die);
19365
19366   decls_for_scope (stmt, stmt_die, depth);
19367 }
19368
19369 /* Generate a DIE for an inlined subprogram.  */
19370
19371 static void
19372 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19373 {
19374   tree decl;
19375
19376   /* The instance of function that is effectively being inlined shall not
19377      be abstract.  */
19378   gcc_assert (! BLOCK_ABSTRACT (stmt));
19379
19380   decl = block_ultimate_origin (stmt);
19381
19382   /* Emit info for the abstract instance first, if we haven't yet.  We
19383      must emit this even if the block is abstract, otherwise when we
19384      emit the block below (or elsewhere), we may end up trying to emit
19385      a die whose origin die hasn't been emitted, and crashing.  */
19386   dwarf2out_abstract_function (decl);
19387
19388   if (! BLOCK_ABSTRACT (stmt))
19389     {
19390       dw_die_ref subr_die
19391         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19392
19393       add_abstract_origin_attribute (subr_die, decl);
19394       if (TREE_ASM_WRITTEN (stmt))
19395         add_high_low_attributes (stmt, subr_die);
19396       add_call_src_coords_attributes (stmt, subr_die);
19397
19398       decls_for_scope (stmt, subr_die, depth);
19399       current_function_has_inlines = 1;
19400     }
19401 }
19402
19403 /* Generate a DIE for a field in a record, or structure.  */
19404
19405 static void
19406 gen_field_die (tree decl, dw_die_ref context_die)
19407 {
19408   dw_die_ref decl_die;
19409
19410   if (TREE_TYPE (decl) == error_mark_node)
19411     return;
19412
19413   decl_die = new_die (DW_TAG_member, context_die, decl);
19414   add_name_and_src_coords_attributes (decl_die, decl);
19415   add_type_attribute (decl_die, member_declared_type (decl),
19416                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19417                       context_die);
19418
19419   if (DECL_BIT_FIELD_TYPE (decl))
19420     {
19421       add_byte_size_attribute (decl_die, decl);
19422       add_bit_size_attribute (decl_die, decl);
19423       add_bit_offset_attribute (decl_die, decl);
19424     }
19425
19426   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19427     add_data_member_location_attribute (decl_die, decl);
19428
19429   if (DECL_ARTIFICIAL (decl))
19430     add_AT_flag (decl_die, DW_AT_artificial, 1);
19431
19432   add_accessibility_attribute (decl_die, decl);
19433
19434   /* Equate decl number to die, so that we can look up this decl later on.  */
19435   equate_decl_number_to_die (decl, decl_die);
19436 }
19437
19438 #if 0
19439 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19440    Use modified_type_die instead.
19441    We keep this code here just in case these types of DIEs may be needed to
19442    represent certain things in other languages (e.g. Pascal) someday.  */
19443
19444 static void
19445 gen_pointer_type_die (tree type, dw_die_ref context_die)
19446 {
19447   dw_die_ref ptr_die
19448     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19449
19450   equate_type_number_to_die (type, ptr_die);
19451   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19452   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19453 }
19454
19455 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19456    Use modified_type_die instead.
19457    We keep this code here just in case these types of DIEs may be needed to
19458    represent certain things in other languages (e.g. Pascal) someday.  */
19459
19460 static void
19461 gen_reference_type_die (tree type, dw_die_ref context_die)
19462 {
19463   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19464
19465   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19466     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19467   else
19468     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19469
19470   equate_type_number_to_die (type, ref_die);
19471   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19472   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19473 }
19474 #endif
19475
19476 /* Generate a DIE for a pointer to a member type.  */
19477
19478 static void
19479 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19480 {
19481   dw_die_ref ptr_die
19482     = new_die (DW_TAG_ptr_to_member_type,
19483                scope_die_for (type, context_die), type);
19484
19485   equate_type_number_to_die (type, ptr_die);
19486   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19487                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19488   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19489 }
19490
19491 /* Generate the DIE for the compilation unit.  */
19492
19493 static dw_die_ref
19494 gen_compile_unit_die (const char *filename)
19495 {
19496   dw_die_ref die;
19497   char producer[250];
19498   const char *language_string = lang_hooks.name;
19499   int language;
19500
19501   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19502
19503   if (filename)
19504     {
19505       add_name_attribute (die, filename);
19506       /* Don't add cwd for <built-in>.  */
19507       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19508         add_comp_dir_attribute (die);
19509     }
19510
19511   sprintf (producer, "%s %s", language_string, version_string);
19512
19513 #ifdef MIPS_DEBUGGING_INFO
19514   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19515      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19516      not appear in the producer string, the debugger reaches the conclusion
19517      that the object file is stripped and has no debugging information.
19518      To get the MIPS/SGI debugger to believe that there is debugging
19519      information in the object file, we add a -g to the producer string.  */
19520   if (debug_info_level > DINFO_LEVEL_TERSE)
19521     strcat (producer, " -g");
19522 #endif
19523
19524   add_AT_string (die, DW_AT_producer, producer);
19525
19526   language = DW_LANG_C89;
19527   if (strcmp (language_string, "GNU C++") == 0)
19528     language = DW_LANG_C_plus_plus;
19529   else if (strcmp (language_string, "GNU F77") == 0)
19530     language = DW_LANG_Fortran77;
19531   else if (strcmp (language_string, "GNU Pascal") == 0)
19532     language = DW_LANG_Pascal83;
19533   else if (dwarf_version >= 3 || !dwarf_strict)
19534     {
19535       if (strcmp (language_string, "GNU Ada") == 0)
19536         language = DW_LANG_Ada95;
19537       else if (strcmp (language_string, "GNU Fortran") == 0)
19538         language = DW_LANG_Fortran95;
19539       else if (strcmp (language_string, "GNU Java") == 0)
19540         language = DW_LANG_Java;
19541       else if (strcmp (language_string, "GNU Objective-C") == 0)
19542         language = DW_LANG_ObjC;
19543       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19544         language = DW_LANG_ObjC_plus_plus;
19545     }
19546
19547   add_AT_unsigned (die, DW_AT_language, language);
19548
19549   switch (language)
19550     {
19551     case DW_LANG_Fortran77:
19552     case DW_LANG_Fortran90:
19553     case DW_LANG_Fortran95:
19554       /* Fortran has case insensitive identifiers and the front-end
19555          lowercases everything.  */
19556       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19557       break;
19558     default:
19559       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19560       break;
19561     }
19562   return die;
19563 }
19564
19565 /* Generate the DIE for a base class.  */
19566
19567 static void
19568 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19569 {
19570   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19571
19572   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19573   add_data_member_location_attribute (die, binfo);
19574
19575   if (BINFO_VIRTUAL_P (binfo))
19576     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19577
19578   if (access == access_public_node)
19579     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19580   else if (access == access_protected_node)
19581     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19582 }
19583
19584 /* Generate a DIE for a class member.  */
19585
19586 static void
19587 gen_member_die (tree type, dw_die_ref context_die)
19588 {
19589   tree member;
19590   tree binfo = TYPE_BINFO (type);
19591   dw_die_ref child;
19592
19593   /* If this is not an incomplete type, output descriptions of each of its
19594      members. Note that as we output the DIEs necessary to represent the
19595      members of this record or union type, we will also be trying to output
19596      DIEs to represent the *types* of those members. However the `type'
19597      function (above) will specifically avoid generating type DIEs for member
19598      types *within* the list of member DIEs for this (containing) type except
19599      for those types (of members) which are explicitly marked as also being
19600      members of this (containing) type themselves.  The g++ front- end can
19601      force any given type to be treated as a member of some other (containing)
19602      type by setting the TYPE_CONTEXT of the given (member) type to point to
19603      the TREE node representing the appropriate (containing) type.  */
19604
19605   /* First output info about the base classes.  */
19606   if (binfo)
19607     {
19608       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19609       int i;
19610       tree base;
19611
19612       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19613         gen_inheritance_die (base,
19614                              (accesses ? VEC_index (tree, accesses, i)
19615                               : access_public_node), context_die);
19616     }
19617
19618   /* Now output info about the data members and type members.  */
19619   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19620     {
19621       /* If we thought we were generating minimal debug info for TYPE
19622          and then changed our minds, some of the member declarations
19623          may have already been defined.  Don't define them again, but
19624          do put them in the right order.  */
19625
19626       child = lookup_decl_die (member);
19627       if (child)
19628         splice_child_die (context_die, child);
19629       else
19630         gen_decl_die (member, NULL, context_die);
19631     }
19632
19633   /* Now output info about the function members (if any).  */
19634   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19635     {
19636       /* Don't include clones in the member list.  */
19637       if (DECL_ABSTRACT_ORIGIN (member))
19638         continue;
19639
19640       child = lookup_decl_die (member);
19641       if (child)
19642         splice_child_die (context_die, child);
19643       else
19644         gen_decl_die (member, NULL, context_die);
19645     }
19646 }
19647
19648 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19649    is set, we pretend that the type was never defined, so we only get the
19650    member DIEs needed by later specification DIEs.  */
19651
19652 static void
19653 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19654                                 enum debug_info_usage usage)
19655 {
19656   dw_die_ref type_die = lookup_type_die (type);
19657   dw_die_ref scope_die = 0;
19658   int nested = 0;
19659   int complete = (TYPE_SIZE (type)
19660                   && (! TYPE_STUB_DECL (type)
19661                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19662   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19663   complete = complete && should_emit_struct_debug (type, usage);
19664
19665   if (type_die && ! complete)
19666     return;
19667
19668   if (TYPE_CONTEXT (type) != NULL_TREE
19669       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19670           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19671     nested = 1;
19672
19673   scope_die = scope_die_for (type, context_die);
19674
19675   if (! type_die || (nested && scope_die == comp_unit_die))
19676     /* First occurrence of type or toplevel definition of nested class.  */
19677     {
19678       dw_die_ref old_die = type_die;
19679
19680       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19681                           ? record_type_tag (type) : DW_TAG_union_type,
19682                           scope_die, type);
19683       equate_type_number_to_die (type, type_die);
19684       if (old_die)
19685         add_AT_specification (type_die, old_die);
19686       else
19687         add_name_attribute (type_die, type_tag (type));
19688     }
19689   else
19690     remove_AT (type_die, DW_AT_declaration);
19691
19692   /* Generate child dies for template paramaters.  */
19693   if (debug_info_level > DINFO_LEVEL_TERSE
19694       && COMPLETE_TYPE_P (type))
19695     gen_generic_params_dies (type);
19696
19697   /* If this type has been completed, then give it a byte_size attribute and
19698      then give a list of members.  */
19699   if (complete && !ns_decl)
19700     {
19701       /* Prevent infinite recursion in cases where the type of some member of
19702          this type is expressed in terms of this type itself.  */
19703       TREE_ASM_WRITTEN (type) = 1;
19704       add_byte_size_attribute (type_die, type);
19705       if (TYPE_STUB_DECL (type) != NULL_TREE)
19706         {
19707           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19708           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19709         }
19710
19711       /* If the first reference to this type was as the return type of an
19712          inline function, then it may not have a parent.  Fix this now.  */
19713       if (type_die->die_parent == NULL)
19714         add_child_die (scope_die, type_die);
19715
19716       push_decl_scope (type);
19717       gen_member_die (type, type_die);
19718       pop_decl_scope ();
19719
19720       /* GNU extension: Record what type our vtable lives in.  */
19721       if (TYPE_VFIELD (type))
19722         {
19723           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19724
19725           gen_type_die (vtype, context_die);
19726           add_AT_die_ref (type_die, DW_AT_containing_type,
19727                           lookup_type_die (vtype));
19728         }
19729     }
19730   else
19731     {
19732       add_AT_flag (type_die, DW_AT_declaration, 1);
19733
19734       /* We don't need to do this for function-local types.  */
19735       if (TYPE_STUB_DECL (type)
19736           && ! decl_function_context (TYPE_STUB_DECL (type)))
19737         VEC_safe_push (tree, gc, incomplete_types, type);
19738     }
19739
19740   if (get_AT (type_die, DW_AT_name))
19741     add_pubtype (type, type_die);
19742 }
19743
19744 /* Generate a DIE for a subroutine _type_.  */
19745
19746 static void
19747 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19748 {
19749   tree return_type = TREE_TYPE (type);
19750   dw_die_ref subr_die
19751     = new_die (DW_TAG_subroutine_type,
19752                scope_die_for (type, context_die), type);
19753
19754   equate_type_number_to_die (type, subr_die);
19755   add_prototyped_attribute (subr_die, type);
19756   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19757   gen_formal_types_die (type, subr_die);
19758
19759   if (get_AT (subr_die, DW_AT_name))
19760     add_pubtype (type, subr_die);
19761 }
19762
19763 /* Generate a DIE for a type definition.  */
19764
19765 static void
19766 gen_typedef_die (tree decl, dw_die_ref context_die)
19767 {
19768   dw_die_ref type_die;
19769   tree origin;
19770
19771   if (TREE_ASM_WRITTEN (decl))
19772     return;
19773
19774   TREE_ASM_WRITTEN (decl) = 1;
19775   type_die = new_die (DW_TAG_typedef, context_die, decl);
19776   origin = decl_ultimate_origin (decl);
19777   if (origin != NULL)
19778     add_abstract_origin_attribute (type_die, origin);
19779   else
19780     {
19781       tree type;
19782
19783       add_name_and_src_coords_attributes (type_die, decl);
19784       if (DECL_ORIGINAL_TYPE (decl))
19785         {
19786           type = DECL_ORIGINAL_TYPE (decl);
19787
19788           gcc_assert (type != TREE_TYPE (decl));
19789           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19790         }
19791       else
19792         {
19793           type = TREE_TYPE (decl);
19794
19795           if (is_naming_typedef_decl (TYPE_NAME (type)))
19796             {
19797               /* Here, we are in the case of decl being a typedef naming
19798                  an anonymous type, e.g:
19799                      typedef struct {...} foo;
19800                  In that case TREE_TYPE (decl) is not a typedef variant
19801                  type and TYPE_NAME of the anonymous type is set to the
19802                  TYPE_DECL of the typedef. This construct is emitted by
19803                  the C++ FE.
19804
19805                  TYPE is the anonymous struct named by the typedef
19806                  DECL. As we need the DW_AT_type attribute of the
19807                  DW_TAG_typedef to point to the DIE of TYPE, let's
19808                  generate that DIE right away. add_type_attribute
19809                  called below will then pick (via lookup_type_die) that
19810                  anonymous struct DIE.  */
19811               if (!TREE_ASM_WRITTEN (type))
19812                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19813             }
19814         }
19815
19816       add_type_attribute (type_die, type, TREE_READONLY (decl),
19817                           TREE_THIS_VOLATILE (decl), context_die);
19818
19819       if (is_naming_typedef_decl (decl))
19820         /* We want that all subsequent calls to lookup_type_die with
19821            TYPE in argument yield the DW_TAG_typedef we have just
19822            created.  */
19823         equate_type_number_to_die (type, type_die);
19824
19825       add_accessibility_attribute (type_die, decl);
19826     }
19827
19828   if (DECL_ABSTRACT (decl))
19829     equate_decl_number_to_die (decl, type_die);
19830
19831   if (get_AT (type_die, DW_AT_name))
19832     add_pubtype (decl, type_die);
19833 }
19834
19835 /* Generate a DIE for a struct, class, enum or union type.  */
19836
19837 static void
19838 gen_tagged_type_die (tree type,
19839                      dw_die_ref context_die,
19840                      enum debug_info_usage usage)
19841 {
19842   int need_pop;
19843
19844   if (type == NULL_TREE
19845       || !is_tagged_type (type))
19846     return;
19847
19848   /* If this is a nested type whose containing class hasn't been written
19849      out yet, writing it out will cover this one, too.  This does not apply
19850      to instantiations of member class templates; they need to be added to
19851      the containing class as they are generated.  FIXME: This hurts the
19852      idea of combining type decls from multiple TUs, since we can't predict
19853      what set of template instantiations we'll get.  */
19854   if (TYPE_CONTEXT (type)
19855       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19856       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19857     {
19858       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19859
19860       if (TREE_ASM_WRITTEN (type))
19861         return;
19862
19863       /* If that failed, attach ourselves to the stub.  */
19864       push_decl_scope (TYPE_CONTEXT (type));
19865       context_die = lookup_type_die (TYPE_CONTEXT (type));
19866       need_pop = 1;
19867     }
19868   else if (TYPE_CONTEXT (type) != NULL_TREE
19869            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19870     {
19871       /* If this type is local to a function that hasn't been written
19872          out yet, use a NULL context for now; it will be fixed up in
19873          decls_for_scope.  */
19874       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19875       need_pop = 0;
19876     }
19877   else
19878     {
19879       context_die = declare_in_namespace (type, context_die);
19880       need_pop = 0;
19881     }
19882
19883   if (TREE_CODE (type) == ENUMERAL_TYPE)
19884     {
19885       /* This might have been written out by the call to
19886          declare_in_namespace.  */
19887       if (!TREE_ASM_WRITTEN (type))
19888         gen_enumeration_type_die (type, context_die);
19889     }
19890   else
19891     gen_struct_or_union_type_die (type, context_die, usage);
19892
19893   if (need_pop)
19894     pop_decl_scope ();
19895
19896   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19897      it up if it is ever completed.  gen_*_type_die will set it for us
19898      when appropriate.  */
19899 }
19900
19901 /* Generate a type description DIE.  */
19902
19903 static void
19904 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19905                                 enum debug_info_usage usage)
19906 {
19907   struct array_descr_info info;
19908
19909   if (type == NULL_TREE || type == error_mark_node)
19910     return;
19911
19912   /* If TYPE is a typedef type variant, let's generate debug info
19913      for the parent typedef which TYPE is a type of.  */
19914   if (typedef_variant_p (type))
19915     {
19916       if (TREE_ASM_WRITTEN (type))
19917         return;
19918
19919       /* Prevent broken recursion; we can't hand off to the same type.  */
19920       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19921
19922       /* Use the DIE of the containing namespace as the parent DIE of
19923          the type description DIE we want to generate.  */
19924       if (DECL_CONTEXT (TYPE_NAME (type))
19925           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19926         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19927
19928       TREE_ASM_WRITTEN (type) = 1;
19929
19930       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19931       return;
19932     }
19933
19934   /* If type is an anonymous tagged type named by a typedef, let's
19935      generate debug info for the typedef.  */
19936   if (is_naming_typedef_decl (TYPE_NAME (type)))
19937     {
19938       /* Use the DIE of the containing namespace as the parent DIE of
19939          the type description DIE we want to generate.  */
19940       if (DECL_CONTEXT (TYPE_NAME (type))
19941           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19942         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19943       
19944       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19945       return;
19946     }
19947
19948   /* If this is an array type with hidden descriptor, handle it first.  */
19949   if (!TREE_ASM_WRITTEN (type)
19950       && lang_hooks.types.get_array_descr_info
19951       && lang_hooks.types.get_array_descr_info (type, &info)
19952       && (dwarf_version >= 3 || !dwarf_strict))
19953     {
19954       gen_descr_array_type_die (type, &info, context_die);
19955       TREE_ASM_WRITTEN (type) = 1;
19956       return;
19957     }
19958
19959   /* We are going to output a DIE to represent the unqualified version
19960      of this type (i.e. without any const or volatile qualifiers) so
19961      get the main variant (i.e. the unqualified version) of this type
19962      now.  (Vectors are special because the debugging info is in the
19963      cloned type itself).  */
19964   if (TREE_CODE (type) != VECTOR_TYPE)
19965     type = type_main_variant (type);
19966
19967   if (TREE_ASM_WRITTEN (type))
19968     return;
19969
19970   switch (TREE_CODE (type))
19971     {
19972     case ERROR_MARK:
19973       break;
19974
19975     case POINTER_TYPE:
19976     case REFERENCE_TYPE:
19977       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19978          ensures that the gen_type_die recursion will terminate even if the
19979          type is recursive.  Recursive types are possible in Ada.  */
19980       /* ??? We could perhaps do this for all types before the switch
19981          statement.  */
19982       TREE_ASM_WRITTEN (type) = 1;
19983
19984       /* For these types, all that is required is that we output a DIE (or a
19985          set of DIEs) to represent the "basis" type.  */
19986       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19987                                 DINFO_USAGE_IND_USE);
19988       break;
19989
19990     case OFFSET_TYPE:
19991       /* This code is used for C++ pointer-to-data-member types.
19992          Output a description of the relevant class type.  */
19993       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19994                                         DINFO_USAGE_IND_USE);
19995
19996       /* Output a description of the type of the object pointed to.  */
19997       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19998                                         DINFO_USAGE_IND_USE);
19999
20000       /* Now output a DIE to represent this pointer-to-data-member type
20001          itself.  */
20002       gen_ptr_to_mbr_type_die (type, context_die);
20003       break;
20004
20005     case FUNCTION_TYPE:
20006       /* Force out return type (in case it wasn't forced out already).  */
20007       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20008                                         DINFO_USAGE_DIR_USE);
20009       gen_subroutine_type_die (type, context_die);
20010       break;
20011
20012     case METHOD_TYPE:
20013       /* Force out return type (in case it wasn't forced out already).  */
20014       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20015                                         DINFO_USAGE_DIR_USE);
20016       gen_subroutine_type_die (type, context_die);
20017       break;
20018
20019     case ARRAY_TYPE:
20020       gen_array_type_die (type, context_die);
20021       break;
20022
20023     case VECTOR_TYPE:
20024       gen_array_type_die (type, context_die);
20025       break;
20026
20027     case ENUMERAL_TYPE:
20028     case RECORD_TYPE:
20029     case UNION_TYPE:
20030     case QUAL_UNION_TYPE:
20031       gen_tagged_type_die (type, context_die, usage);
20032       return;
20033
20034     case VOID_TYPE:
20035     case INTEGER_TYPE:
20036     case REAL_TYPE:
20037     case FIXED_POINT_TYPE:
20038     case COMPLEX_TYPE:
20039     case BOOLEAN_TYPE:
20040       /* No DIEs needed for fundamental types.  */
20041       break;
20042
20043     case LANG_TYPE:
20044       /* Just use DW_TAG_unspecified_type.  */
20045       {
20046         dw_die_ref type_die = lookup_type_die (type);
20047         if (type_die == NULL)
20048           {
20049             tree name = TYPE_NAME (type);
20050             if (TREE_CODE (name) == TYPE_DECL)
20051               name = DECL_NAME (name);
20052             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20053             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20054             equate_type_number_to_die (type, type_die);
20055           }
20056       }
20057       break;
20058
20059     default:
20060       gcc_unreachable ();
20061     }
20062
20063   TREE_ASM_WRITTEN (type) = 1;
20064 }
20065
20066 static void
20067 gen_type_die (tree type, dw_die_ref context_die)
20068 {
20069   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20070 }
20071
20072 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20073    things which are local to the given block.  */
20074
20075 static void
20076 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20077 {
20078   int must_output_die = 0;
20079   bool inlined_func;
20080
20081   /* Ignore blocks that are NULL.  */
20082   if (stmt == NULL_TREE)
20083     return;
20084
20085   inlined_func = inlined_function_outer_scope_p (stmt);
20086
20087   /* If the block is one fragment of a non-contiguous block, do not
20088      process the variables, since they will have been done by the
20089      origin block.  Do process subblocks.  */
20090   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20091     {
20092       tree sub;
20093
20094       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20095         gen_block_die (sub, context_die, depth + 1);
20096
20097       return;
20098     }
20099
20100   /* Determine if we need to output any Dwarf DIEs at all to represent this
20101      block.  */
20102   if (inlined_func)
20103     /* The outer scopes for inlinings *must* always be represented.  We
20104        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20105     must_output_die = 1;
20106   else
20107     {
20108       /* Determine if this block directly contains any "significant"
20109          local declarations which we will need to output DIEs for.  */
20110       if (debug_info_level > DINFO_LEVEL_TERSE)
20111         /* We are not in terse mode so *any* local declaration counts
20112            as being a "significant" one.  */
20113         must_output_die = ((BLOCK_VARS (stmt) != NULL
20114                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20115                            && (TREE_USED (stmt)
20116                                || TREE_ASM_WRITTEN (stmt)
20117                                || BLOCK_ABSTRACT (stmt)));
20118       else if ((TREE_USED (stmt)
20119                 || TREE_ASM_WRITTEN (stmt)
20120                 || BLOCK_ABSTRACT (stmt))
20121                && !dwarf2out_ignore_block (stmt))
20122         must_output_die = 1;
20123     }
20124
20125   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20126      DIE for any block which contains no significant local declarations at
20127      all.  Rather, in such cases we just call `decls_for_scope' so that any
20128      needed Dwarf info for any sub-blocks will get properly generated. Note
20129      that in terse mode, our definition of what constitutes a "significant"
20130      local declaration gets restricted to include only inlined function
20131      instances and local (nested) function definitions.  */
20132   if (must_output_die)
20133     {
20134       if (inlined_func)
20135         {
20136           /* If STMT block is abstract, that means we have been called
20137              indirectly from dwarf2out_abstract_function.
20138              That function rightfully marks the descendent blocks (of
20139              the abstract function it is dealing with) as being abstract,
20140              precisely to prevent us from emitting any
20141              DW_TAG_inlined_subroutine DIE as a descendent
20142              of an abstract function instance. So in that case, we should
20143              not call gen_inlined_subroutine_die.
20144
20145              Later though, when cgraph asks dwarf2out to emit info
20146              for the concrete instance of the function decl into which
20147              the concrete instance of STMT got inlined, the later will lead
20148              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20149           if (! BLOCK_ABSTRACT (stmt))
20150             gen_inlined_subroutine_die (stmt, context_die, depth);
20151         }
20152       else
20153         gen_lexical_block_die (stmt, context_die, depth);
20154     }
20155   else
20156     decls_for_scope (stmt, context_die, depth);
20157 }
20158
20159 /* Process variable DECL (or variable with origin ORIGIN) within
20160    block STMT and add it to CONTEXT_DIE.  */
20161 static void
20162 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20163 {
20164   dw_die_ref die;
20165   tree decl_or_origin = decl ? decl : origin;
20166
20167   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20168     die = lookup_decl_die (decl_or_origin);
20169   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20170            && TYPE_DECL_IS_STUB (decl_or_origin))
20171     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20172   else
20173     die = NULL;
20174
20175   if (die != NULL && die->die_parent == NULL)
20176     add_child_die (context_die, die);
20177   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20178     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20179                                          stmt, context_die);
20180   else
20181     gen_decl_die (decl, origin, context_die);
20182 }
20183
20184 /* Generate all of the decls declared within a given scope and (recursively)
20185    all of its sub-blocks.  */
20186
20187 static void
20188 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20189 {
20190   tree decl;
20191   unsigned int i;
20192   tree subblocks;
20193
20194   /* Ignore NULL blocks.  */
20195   if (stmt == NULL_TREE)
20196     return;
20197
20198   /* Output the DIEs to represent all of the data objects and typedefs
20199      declared directly within this block but not within any nested
20200      sub-blocks.  Also, nested function and tag DIEs have been
20201      generated with a parent of NULL; fix that up now.  */
20202   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20203     process_scope_var (stmt, decl, NULL_TREE, context_die);
20204   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20205     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20206                        context_die);
20207
20208   /* If we're at -g1, we're not interested in subblocks.  */
20209   if (debug_info_level <= DINFO_LEVEL_TERSE)
20210     return;
20211
20212   /* Output the DIEs to represent all sub-blocks (and the items declared
20213      therein) of this block.  */
20214   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20215        subblocks != NULL;
20216        subblocks = BLOCK_CHAIN (subblocks))
20217     gen_block_die (subblocks, context_die, depth + 1);
20218 }
20219
20220 /* Is this a typedef we can avoid emitting?  */
20221
20222 static inline int
20223 is_redundant_typedef (const_tree decl)
20224 {
20225   if (TYPE_DECL_IS_STUB (decl))
20226     return 1;
20227
20228   if (DECL_ARTIFICIAL (decl)
20229       && DECL_CONTEXT (decl)
20230       && is_tagged_type (DECL_CONTEXT (decl))
20231       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20232       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20233     /* Also ignore the artificial member typedef for the class name.  */
20234     return 1;
20235
20236   return 0;
20237 }
20238
20239 /* Return TRUE if TYPE is a typedef that names a type for linkage
20240    purposes. This kind of typedefs is produced by the C++ FE for
20241    constructs like:
20242
20243    typedef struct {...} foo;
20244
20245    In that case, there is no typedef variant type produced for foo.
20246    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20247    struct type.  */
20248
20249 static bool
20250 is_naming_typedef_decl (const_tree decl)
20251 {
20252   if (decl == NULL_TREE
20253       || TREE_CODE (decl) != TYPE_DECL
20254       || !is_tagged_type (TREE_TYPE (decl))
20255       || DECL_IS_BUILTIN (decl)
20256       || is_redundant_typedef (decl)
20257       /* It looks like Ada produces TYPE_DECLs that are very similar
20258          to C++ naming typedefs but that have different
20259          semantics. Let's be specific to c++ for now.  */
20260       || !is_cxx ())
20261     return FALSE;
20262
20263   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20264           && TYPE_NAME (TREE_TYPE (decl)) == decl
20265           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20266               != TYPE_NAME (TREE_TYPE (decl))));
20267 }
20268
20269 /* Returns the DIE for a context.  */
20270
20271 static inline dw_die_ref
20272 get_context_die (tree context)
20273 {
20274   if (context)
20275     {
20276       /* Find die that represents this context.  */
20277       if (TYPE_P (context))
20278         return force_type_die (TYPE_MAIN_VARIANT (context));
20279       else
20280         return force_decl_die (context);
20281     }
20282   return comp_unit_die;
20283 }
20284
20285 /* Returns the DIE for decl.  A DIE will always be returned.  */
20286
20287 static dw_die_ref
20288 force_decl_die (tree decl)
20289 {
20290   dw_die_ref decl_die;
20291   unsigned saved_external_flag;
20292   tree save_fn = NULL_TREE;
20293   decl_die = lookup_decl_die (decl);
20294   if (!decl_die)
20295     {
20296       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20297
20298       decl_die = lookup_decl_die (decl);
20299       if (decl_die)
20300         return decl_die;
20301
20302       switch (TREE_CODE (decl))
20303         {
20304         case FUNCTION_DECL:
20305           /* Clear current_function_decl, so that gen_subprogram_die thinks
20306              that this is a declaration. At this point, we just want to force
20307              declaration die.  */
20308           save_fn = current_function_decl;
20309           current_function_decl = NULL_TREE;
20310           gen_subprogram_die (decl, context_die);
20311           current_function_decl = save_fn;
20312           break;
20313
20314         case VAR_DECL:
20315           /* Set external flag to force declaration die. Restore it after
20316            gen_decl_die() call.  */
20317           saved_external_flag = DECL_EXTERNAL (decl);
20318           DECL_EXTERNAL (decl) = 1;
20319           gen_decl_die (decl, NULL, context_die);
20320           DECL_EXTERNAL (decl) = saved_external_flag;
20321           break;
20322
20323         case NAMESPACE_DECL:
20324           if (dwarf_version >= 3 || !dwarf_strict)
20325             dwarf2out_decl (decl);
20326           else
20327             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20328             decl_die = comp_unit_die;
20329           break;
20330
20331         default:
20332           gcc_unreachable ();
20333         }
20334
20335       /* We should be able to find the DIE now.  */
20336       if (!decl_die)
20337         decl_die = lookup_decl_die (decl);
20338       gcc_assert (decl_die);
20339     }
20340
20341   return decl_die;
20342 }
20343
20344 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20345    always returned.  */
20346
20347 static dw_die_ref
20348 force_type_die (tree type)
20349 {
20350   dw_die_ref type_die;
20351
20352   type_die = lookup_type_die (type);
20353   if (!type_die)
20354     {
20355       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20356
20357       type_die = modified_type_die (type, TYPE_READONLY (type),
20358                                     TYPE_VOLATILE (type), context_die);
20359       gcc_assert (type_die);
20360     }
20361   return type_die;
20362 }
20363
20364 /* Force out any required namespaces to be able to output DECL,
20365    and return the new context_die for it, if it's changed.  */
20366
20367 static dw_die_ref
20368 setup_namespace_context (tree thing, dw_die_ref context_die)
20369 {
20370   tree context = (DECL_P (thing)
20371                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20372   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20373     /* Force out the namespace.  */
20374     context_die = force_decl_die (context);
20375
20376   return context_die;
20377 }
20378
20379 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20380    type) within its namespace, if appropriate.
20381
20382    For compatibility with older debuggers, namespace DIEs only contain
20383    declarations; all definitions are emitted at CU scope.  */
20384
20385 static dw_die_ref
20386 declare_in_namespace (tree thing, dw_die_ref context_die)
20387 {
20388   dw_die_ref ns_context;
20389
20390   if (debug_info_level <= DINFO_LEVEL_TERSE)
20391     return context_die;
20392
20393   /* If this decl is from an inlined function, then don't try to emit it in its
20394      namespace, as we will get confused.  It would have already been emitted
20395      when the abstract instance of the inline function was emitted anyways.  */
20396   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20397     return context_die;
20398
20399   ns_context = setup_namespace_context (thing, context_die);
20400
20401   if (ns_context != context_die)
20402     {
20403       if (is_fortran ())
20404         return ns_context;
20405       if (DECL_P (thing))
20406         gen_decl_die (thing, NULL, ns_context);
20407       else
20408         gen_type_die (thing, ns_context);
20409     }
20410   return context_die;
20411 }
20412
20413 /* Generate a DIE for a namespace or namespace alias.  */
20414
20415 static void
20416 gen_namespace_die (tree decl, dw_die_ref context_die)
20417 {
20418   dw_die_ref namespace_die;
20419
20420   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20421      they are an alias of.  */
20422   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20423     {
20424       /* Output a real namespace or module.  */
20425       context_die = setup_namespace_context (decl, comp_unit_die);
20426       namespace_die = new_die (is_fortran ()
20427                                ? DW_TAG_module : DW_TAG_namespace,
20428                                context_die, decl);
20429       /* For Fortran modules defined in different CU don't add src coords.  */
20430       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20431         {
20432           const char *name = dwarf2_name (decl, 0);
20433           if (name)
20434             add_name_attribute (namespace_die, name);
20435         }
20436       else
20437         add_name_and_src_coords_attributes (namespace_die, decl);
20438       if (DECL_EXTERNAL (decl))
20439         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20440       equate_decl_number_to_die (decl, namespace_die);
20441     }
20442   else
20443     {
20444       /* Output a namespace alias.  */
20445
20446       /* Force out the namespace we are an alias of, if necessary.  */
20447       dw_die_ref origin_die
20448         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20449
20450       if (DECL_CONTEXT (decl) == NULL_TREE
20451           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20452         context_die = setup_namespace_context (decl, comp_unit_die);
20453       /* Now create the namespace alias DIE.  */
20454       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20455       add_name_and_src_coords_attributes (namespace_die, decl);
20456       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20457       equate_decl_number_to_die (decl, namespace_die);
20458     }
20459 }
20460
20461 /* Generate Dwarf debug information for a decl described by DECL.
20462    The return value is currently only meaningful for PARM_DECLs,
20463    for all other decls it returns NULL.  */
20464
20465 static dw_die_ref
20466 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20467 {
20468   tree decl_or_origin = decl ? decl : origin;
20469   tree class_origin = NULL, ultimate_origin;
20470
20471   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20472     return NULL;
20473
20474   switch (TREE_CODE (decl_or_origin))
20475     {
20476     case ERROR_MARK:
20477       break;
20478
20479     case CONST_DECL:
20480       if (!is_fortran ())
20481         {
20482           /* The individual enumerators of an enum type get output when we output
20483              the Dwarf representation of the relevant enum type itself.  */
20484           break;
20485         }
20486
20487       /* Emit its type.  */
20488       gen_type_die (TREE_TYPE (decl), context_die);
20489
20490       /* And its containing namespace.  */
20491       context_die = declare_in_namespace (decl, context_die);
20492
20493       gen_const_die (decl, context_die);
20494       break;
20495
20496     case FUNCTION_DECL:
20497       /* Don't output any DIEs to represent mere function declarations,
20498          unless they are class members or explicit block externs.  */
20499       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20500           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20501           && (current_function_decl == NULL_TREE
20502               || DECL_ARTIFICIAL (decl_or_origin)))
20503         break;
20504
20505 #if 0
20506       /* FIXME */
20507       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20508          on local redeclarations of global functions.  That seems broken.  */
20509       if (current_function_decl != decl)
20510         /* This is only a declaration.  */;
20511 #endif
20512
20513       /* If we're emitting a clone, emit info for the abstract instance.  */
20514       if (origin || DECL_ORIGIN (decl) != decl)
20515         dwarf2out_abstract_function (origin
20516                                      ? DECL_ORIGIN (origin)
20517                                      : DECL_ABSTRACT_ORIGIN (decl));
20518
20519       /* If we're emitting an out-of-line copy of an inline function,
20520          emit info for the abstract instance and set up to refer to it.  */
20521       else if (cgraph_function_possibly_inlined_p (decl)
20522                && ! DECL_ABSTRACT (decl)
20523                && ! class_or_namespace_scope_p (context_die)
20524                /* dwarf2out_abstract_function won't emit a die if this is just
20525                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20526                   that case, because that works only if we have a die.  */
20527                && DECL_INITIAL (decl) != NULL_TREE)
20528         {
20529           dwarf2out_abstract_function (decl);
20530           set_decl_origin_self (decl);
20531         }
20532
20533       /* Otherwise we're emitting the primary DIE for this decl.  */
20534       else if (debug_info_level > DINFO_LEVEL_TERSE)
20535         {
20536           /* Before we describe the FUNCTION_DECL itself, make sure that we
20537              have described its return type.  */
20538           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20539
20540           /* And its virtual context.  */
20541           if (DECL_VINDEX (decl) != NULL_TREE)
20542             gen_type_die (DECL_CONTEXT (decl), context_die);
20543
20544           /* And its containing type.  */
20545           if (!origin)
20546             origin = decl_class_context (decl);
20547           if (origin != NULL_TREE)
20548             gen_type_die_for_member (origin, decl, context_die);
20549
20550           /* And its containing namespace.  */
20551           context_die = declare_in_namespace (decl, context_die);
20552         }
20553
20554       /* Now output a DIE to represent the function itself.  */
20555       if (decl)
20556         gen_subprogram_die (decl, context_die);
20557       break;
20558
20559     case TYPE_DECL:
20560       /* If we are in terse mode, don't generate any DIEs to represent any
20561          actual typedefs.  */
20562       if (debug_info_level <= DINFO_LEVEL_TERSE)
20563         break;
20564
20565       /* In the special case of a TYPE_DECL node representing the declaration
20566          of some type tag, if the given TYPE_DECL is marked as having been
20567          instantiated from some other (original) TYPE_DECL node (e.g. one which
20568          was generated within the original definition of an inline function) we
20569          used to generate a special (abbreviated) DW_TAG_structure_type,
20570          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20571          should be actually referencing those DIEs, as variable DIEs with that
20572          type would be emitted already in the abstract origin, so it was always
20573          removed during unused type prunning.  Don't add anything in this
20574          case.  */
20575       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20576         break;
20577
20578       if (is_redundant_typedef (decl))
20579         gen_type_die (TREE_TYPE (decl), context_die);
20580       else
20581         /* Output a DIE to represent the typedef itself.  */
20582         gen_typedef_die (decl, context_die);
20583       break;
20584
20585     case LABEL_DECL:
20586       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20587         gen_label_die (decl, context_die);
20588       break;
20589
20590     case VAR_DECL:
20591     case RESULT_DECL:
20592       /* If we are in terse mode, don't generate any DIEs to represent any
20593          variable declarations or definitions.  */
20594       if (debug_info_level <= DINFO_LEVEL_TERSE)
20595         break;
20596
20597       /* Output any DIEs that are needed to specify the type of this data
20598          object.  */
20599       if (decl_by_reference_p (decl_or_origin))
20600         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20601       else
20602         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20603
20604       /* And its containing type.  */
20605       class_origin = decl_class_context (decl_or_origin);
20606       if (class_origin != NULL_TREE)
20607         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20608
20609       /* And its containing namespace.  */
20610       context_die = declare_in_namespace (decl_or_origin, context_die);
20611
20612       /* Now output the DIE to represent the data object itself.  This gets
20613          complicated because of the possibility that the VAR_DECL really
20614          represents an inlined instance of a formal parameter for an inline
20615          function.  */
20616       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20617       if (ultimate_origin != NULL_TREE
20618           && TREE_CODE (ultimate_origin) == PARM_DECL)
20619         gen_formal_parameter_die (decl, origin,
20620                                   true /* Emit name attribute.  */,
20621                                   context_die);
20622       else
20623         gen_variable_die (decl, origin, context_die);
20624       break;
20625
20626     case FIELD_DECL:
20627       /* Ignore the nameless fields that are used to skip bits but handle C++
20628          anonymous unions and structs.  */
20629       if (DECL_NAME (decl) != NULL_TREE
20630           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20631           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20632         {
20633           gen_type_die (member_declared_type (decl), context_die);
20634           gen_field_die (decl, context_die);
20635         }
20636       break;
20637
20638     case PARM_DECL:
20639       if (DECL_BY_REFERENCE (decl_or_origin))
20640         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20641       else
20642         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20643       return gen_formal_parameter_die (decl, origin,
20644                                        true /* Emit name attribute.  */,
20645                                        context_die);
20646
20647     case NAMESPACE_DECL:
20648     case IMPORTED_DECL:
20649       if (dwarf_version >= 3 || !dwarf_strict)
20650         gen_namespace_die (decl, context_die);
20651       break;
20652
20653     default:
20654       /* Probably some frontend-internal decl.  Assume we don't care.  */
20655       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20656       break;
20657     }
20658
20659   return NULL;
20660 }
20661 \f
20662 /* Output debug information for global decl DECL.  Called from toplev.c after
20663    compilation proper has finished.  */
20664
20665 static void
20666 dwarf2out_global_decl (tree decl)
20667 {
20668   /* Output DWARF2 information for file-scope tentative data object
20669      declarations, file-scope (extern) function declarations (which
20670      had no corresponding body) and file-scope tagged type declarations
20671      and definitions which have not yet been forced out.  */
20672   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20673     dwarf2out_decl (decl);
20674 }
20675
20676 /* Output debug information for type decl DECL.  Called from toplev.c
20677    and from language front ends (to record built-in types).  */
20678 static void
20679 dwarf2out_type_decl (tree decl, int local)
20680 {
20681   if (!local)
20682     dwarf2out_decl (decl);
20683 }
20684
20685 /* Output debug information for imported module or decl DECL.
20686    NAME is non-NULL name in the lexical block if the decl has been renamed.
20687    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20688    that DECL belongs to.
20689    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20690 static void
20691 dwarf2out_imported_module_or_decl_1 (tree decl,
20692                                      tree name,
20693                                      tree lexical_block,
20694                                      dw_die_ref lexical_block_die)
20695 {
20696   expanded_location xloc;
20697   dw_die_ref imported_die = NULL;
20698   dw_die_ref at_import_die;
20699
20700   if (TREE_CODE (decl) == IMPORTED_DECL)
20701     {
20702       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20703       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20704       gcc_assert (decl);
20705     }
20706   else
20707     xloc = expand_location (input_location);
20708
20709   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20710     {
20711       at_import_die = force_type_die (TREE_TYPE (decl));
20712       /* For namespace N { typedef void T; } using N::T; base_type_die
20713          returns NULL, but DW_TAG_imported_declaration requires
20714          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20715       if (!at_import_die)
20716         {
20717           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20718           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20719           at_import_die = lookup_type_die (TREE_TYPE (decl));
20720           gcc_assert (at_import_die);
20721         }
20722     }
20723   else
20724     {
20725       at_import_die = lookup_decl_die (decl);
20726       if (!at_import_die)
20727         {
20728           /* If we're trying to avoid duplicate debug info, we may not have
20729              emitted the member decl for this field.  Emit it now.  */
20730           if (TREE_CODE (decl) == FIELD_DECL)
20731             {
20732               tree type = DECL_CONTEXT (decl);
20733
20734               if (TYPE_CONTEXT (type)
20735                   && TYPE_P (TYPE_CONTEXT (type))
20736                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20737                                                 DINFO_USAGE_DIR_USE))
20738                 return;
20739               gen_type_die_for_member (type, decl,
20740                                        get_context_die (TYPE_CONTEXT (type)));
20741             }
20742           at_import_die = force_decl_die (decl);
20743         }
20744     }
20745
20746   if (TREE_CODE (decl) == NAMESPACE_DECL)
20747     {
20748       if (dwarf_version >= 3 || !dwarf_strict)
20749         imported_die = new_die (DW_TAG_imported_module,
20750                                 lexical_block_die,
20751                                 lexical_block);
20752       else
20753         return;
20754     }
20755   else
20756     imported_die = new_die (DW_TAG_imported_declaration,
20757                             lexical_block_die,
20758                             lexical_block);
20759
20760   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20761   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20762   if (name)
20763     add_AT_string (imported_die, DW_AT_name,
20764                    IDENTIFIER_POINTER (name));
20765   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20766 }
20767
20768 /* Output debug information for imported module or decl DECL.
20769    NAME is non-NULL name in context if the decl has been renamed.
20770    CHILD is true if decl is one of the renamed decls as part of
20771    importing whole module.  */
20772
20773 static void
20774 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20775                                    bool child)
20776 {
20777   /* dw_die_ref at_import_die;  */
20778   dw_die_ref scope_die;
20779
20780   if (debug_info_level <= DINFO_LEVEL_TERSE)
20781     return;
20782
20783   gcc_assert (decl);
20784
20785   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20786      We need decl DIE for reference and scope die. First, get DIE for the decl
20787      itself.  */
20788
20789   /* Get the scope die for decl context. Use comp_unit_die for global module
20790      or decl. If die is not found for non globals, force new die.  */
20791   if (context
20792       && TYPE_P (context)
20793       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20794     return;
20795
20796   if (!(dwarf_version >= 3 || !dwarf_strict))
20797     return;
20798
20799   scope_die = get_context_die (context);
20800
20801   if (child)
20802     {
20803       gcc_assert (scope_die->die_child);
20804       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20805       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20806       scope_die = scope_die->die_child;
20807     }
20808
20809   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20810   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20811
20812 }
20813
20814 /* Write the debugging output for DECL.  */
20815
20816 void
20817 dwarf2out_decl (tree decl)
20818 {
20819   dw_die_ref context_die = comp_unit_die;
20820
20821   switch (TREE_CODE (decl))
20822     {
20823     case ERROR_MARK:
20824       return;
20825
20826     case FUNCTION_DECL:
20827       /* What we would really like to do here is to filter out all mere
20828          file-scope declarations of file-scope functions which are never
20829          referenced later within this translation unit (and keep all of ones
20830          that *are* referenced later on) but we aren't clairvoyant, so we have
20831          no idea which functions will be referenced in the future (i.e. later
20832          on within the current translation unit). So here we just ignore all
20833          file-scope function declarations which are not also definitions.  If
20834          and when the debugger needs to know something about these functions,
20835          it will have to hunt around and find the DWARF information associated
20836          with the definition of the function.
20837
20838          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20839          nodes represent definitions and which ones represent mere
20840          declarations.  We have to check DECL_INITIAL instead. That's because
20841          the C front-end supports some weird semantics for "extern inline"
20842          function definitions.  These can get inlined within the current
20843          translation unit (and thus, we need to generate Dwarf info for their
20844          abstract instances so that the Dwarf info for the concrete inlined
20845          instances can have something to refer to) but the compiler never
20846          generates any out-of-lines instances of such things (despite the fact
20847          that they *are* definitions).
20848
20849          The important point is that the C front-end marks these "extern
20850          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20851          them anyway. Note that the C++ front-end also plays some similar games
20852          for inline function definitions appearing within include files which
20853          also contain `#pragma interface' pragmas.  */
20854       if (DECL_INITIAL (decl) == NULL_TREE)
20855         return;
20856
20857       /* If we're a nested function, initially use a parent of NULL; if we're
20858          a plain function, this will be fixed up in decls_for_scope.  If
20859          we're a method, it will be ignored, since we already have a DIE.  */
20860       if (decl_function_context (decl)
20861           /* But if we're in terse mode, we don't care about scope.  */
20862           && debug_info_level > DINFO_LEVEL_TERSE)
20863         context_die = NULL;
20864       break;
20865
20866     case VAR_DECL:
20867       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20868          declaration and if the declaration was never even referenced from
20869          within this entire compilation unit.  We suppress these DIEs in
20870          order to save space in the .debug section (by eliminating entries
20871          which are probably useless).  Note that we must not suppress
20872          block-local extern declarations (whether used or not) because that
20873          would screw-up the debugger's name lookup mechanism and cause it to
20874          miss things which really ought to be in scope at a given point.  */
20875       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20876         return;
20877
20878       /* For local statics lookup proper context die.  */
20879       if (TREE_STATIC (decl) && decl_function_context (decl))
20880         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20881
20882       /* If we are in terse mode, don't generate any DIEs to represent any
20883          variable declarations or definitions.  */
20884       if (debug_info_level <= DINFO_LEVEL_TERSE)
20885         return;
20886       break;
20887
20888     case CONST_DECL:
20889       if (debug_info_level <= DINFO_LEVEL_TERSE)
20890         return;
20891       if (!is_fortran ())
20892         return;
20893       if (TREE_STATIC (decl) && decl_function_context (decl))
20894         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20895       break;
20896
20897     case NAMESPACE_DECL:
20898     case IMPORTED_DECL:
20899       if (debug_info_level <= DINFO_LEVEL_TERSE)
20900         return;
20901       if (lookup_decl_die (decl) != NULL)
20902         return;
20903       break;
20904
20905     case TYPE_DECL:
20906       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20907       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20908         return;
20909
20910       /* Don't bother trying to generate any DIEs to represent any of the
20911          normal built-in types for the language we are compiling.  */
20912       if (DECL_IS_BUILTIN (decl))
20913         {
20914           /* OK, we need to generate one for `bool' so GDB knows what type
20915              comparisons have.  */
20916           if (is_cxx ()
20917               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20918               && ! DECL_IGNORED_P (decl))
20919             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20920
20921           return;
20922         }
20923
20924       /* If we are in terse mode, don't generate any DIEs for types.  */
20925       if (debug_info_level <= DINFO_LEVEL_TERSE)
20926         return;
20927
20928       /* If we're a function-scope tag, initially use a parent of NULL;
20929          this will be fixed up in decls_for_scope.  */
20930       if (decl_function_context (decl))
20931         context_die = NULL;
20932
20933       break;
20934
20935     default:
20936       return;
20937     }
20938
20939   gen_decl_die (decl, NULL, context_die);
20940 }
20941
20942 /* Write the debugging output for DECL.  */
20943
20944 static void
20945 dwarf2out_function_decl (tree decl)
20946 {
20947   dwarf2out_decl (decl);
20948
20949   htab_empty (decl_loc_table);
20950 }
20951
20952 /* Output a marker (i.e. a label) for the beginning of the generated code for
20953    a lexical block.  */
20954
20955 static void
20956 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20957                        unsigned int blocknum)
20958 {
20959   switch_to_section (current_function_section ());
20960   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20961 }
20962
20963 /* Output a marker (i.e. a label) for the end of the generated code for a
20964    lexical block.  */
20965
20966 static void
20967 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20968 {
20969   switch_to_section (current_function_section ());
20970   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20971 }
20972
20973 /* Returns nonzero if it is appropriate not to emit any debugging
20974    information for BLOCK, because it doesn't contain any instructions.
20975
20976    Don't allow this for blocks with nested functions or local classes
20977    as we would end up with orphans, and in the presence of scheduling
20978    we may end up calling them anyway.  */
20979
20980 static bool
20981 dwarf2out_ignore_block (const_tree block)
20982 {
20983   tree decl;
20984   unsigned int i;
20985
20986   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20987     if (TREE_CODE (decl) == FUNCTION_DECL
20988         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20989       return 0;
20990   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20991     {
20992       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20993       if (TREE_CODE (decl) == FUNCTION_DECL
20994           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20995       return 0;
20996     }
20997
20998   return 1;
20999 }
21000
21001 /* Hash table routines for file_hash.  */
21002
21003 static int
21004 file_table_eq (const void *p1_p, const void *p2_p)
21005 {
21006   const struct dwarf_file_data *const p1 =
21007     (const struct dwarf_file_data *) p1_p;
21008   const char *const p2 = (const char *) p2_p;
21009   return strcmp (p1->filename, p2) == 0;
21010 }
21011
21012 static hashval_t
21013 file_table_hash (const void *p_p)
21014 {
21015   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21016   return htab_hash_string (p->filename);
21017 }
21018
21019 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21020    dwarf2out.c) and return its "index".  The index of each (known) filename is
21021    just a unique number which is associated with only that one filename.  We
21022    need such numbers for the sake of generating labels (in the .debug_sfnames
21023    section) and references to those files numbers (in the .debug_srcinfo
21024    and.debug_macinfo sections).  If the filename given as an argument is not
21025    found in our current list, add it to the list and assign it the next
21026    available unique index number.  In order to speed up searches, we remember
21027    the index of the filename was looked up last.  This handles the majority of
21028    all searches.  */
21029
21030 static struct dwarf_file_data *
21031 lookup_filename (const char *file_name)
21032 {
21033   void ** slot;
21034   struct dwarf_file_data * created;
21035
21036   /* Check to see if the file name that was searched on the previous
21037      call matches this file name.  If so, return the index.  */
21038   if (file_table_last_lookup
21039       && (file_name == file_table_last_lookup->filename
21040           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21041     return file_table_last_lookup;
21042
21043   /* Didn't match the previous lookup, search the table.  */
21044   slot = htab_find_slot_with_hash (file_table, file_name,
21045                                    htab_hash_string (file_name), INSERT);
21046   if (*slot)
21047     return (struct dwarf_file_data *) *slot;
21048
21049   created = ggc_alloc_dwarf_file_data ();
21050   created->filename = file_name;
21051   created->emitted_number = 0;
21052   *slot = created;
21053   return created;
21054 }
21055
21056 /* If the assembler will construct the file table, then translate the compiler
21057    internal file table number into the assembler file table number, and emit
21058    a .file directive if we haven't already emitted one yet.  The file table
21059    numbers are different because we prune debug info for unused variables and
21060    types, which may include filenames.  */
21061
21062 static int
21063 maybe_emit_file (struct dwarf_file_data * fd)
21064 {
21065   if (! fd->emitted_number)
21066     {
21067       if (last_emitted_file)
21068         fd->emitted_number = last_emitted_file->emitted_number + 1;
21069       else
21070         fd->emitted_number = 1;
21071       last_emitted_file = fd;
21072
21073       if (DWARF2_ASM_LINE_DEBUG_INFO)
21074         {
21075           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21076           output_quoted_string (asm_out_file,
21077                                 remap_debug_filename (fd->filename));
21078           fputc ('\n', asm_out_file);
21079         }
21080     }
21081
21082   return fd->emitted_number;
21083 }
21084
21085 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21086    That generation should happen after function debug info has been
21087    generated. The value of the attribute is the constant value of ARG.  */
21088
21089 static void
21090 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21091 {
21092   die_arg_entry entry;
21093
21094   if (!die || !arg)
21095     return;
21096
21097   if (!tmpl_value_parm_die_table)
21098     tmpl_value_parm_die_table
21099       = VEC_alloc (die_arg_entry, gc, 32);
21100
21101   entry.die = die;
21102   entry.arg = arg;
21103   VEC_safe_push (die_arg_entry, gc,
21104                  tmpl_value_parm_die_table,
21105                  &entry);
21106 }
21107
21108 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21109    by append_entry_to_tmpl_value_parm_die_table. This function must
21110    be called after function DIEs have been generated.  */
21111
21112 static void
21113 gen_remaining_tmpl_value_param_die_attribute (void)
21114 {
21115   if (tmpl_value_parm_die_table)
21116     {
21117       unsigned i;
21118       die_arg_entry *e;
21119
21120       for (i = 0;
21121            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
21122            i++)
21123         tree_add_const_value_attribute (e->die, e->arg);
21124     }
21125 }
21126
21127
21128 /* Replace DW_AT_name for the decl with name.  */
21129
21130 static void
21131 dwarf2out_set_name (tree decl, tree name)
21132 {
21133   dw_die_ref die;
21134   dw_attr_ref attr;
21135   const char *dname;
21136
21137   die = TYPE_SYMTAB_DIE (decl);
21138   if (!die)
21139     return;
21140
21141   dname = dwarf2_name (name, 0);
21142   if (!dname)
21143     return;
21144
21145   attr = get_AT (die, DW_AT_name);
21146   if (attr)
21147     {
21148       struct indirect_string_node *node;
21149
21150       node = find_AT_string (dname);
21151       /* replace the string.  */
21152       attr->dw_attr_val.v.val_str = node;
21153     }
21154
21155   else
21156     add_name_attribute (die, dname);
21157 }
21158
21159 /* Called by the final INSN scan whenever we see a direct function call.
21160    Make an entry into the direct call table, recording the point of call
21161    and a reference to the target function's debug entry.  */
21162
21163 static void
21164 dwarf2out_direct_call (tree targ)
21165 {
21166   dcall_entry e;
21167   tree origin = decl_ultimate_origin (targ);
21168
21169   /* If this is a clone, use the abstract origin as the target.  */
21170   if (origin)
21171     targ = origin;
21172
21173   e.poc_label_num = poc_label_num++;
21174   e.poc_decl = current_function_decl;
21175   e.targ_die = force_decl_die (targ);
21176   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21177
21178   /* Drop a label at the return point to mark the point of call.  */
21179   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21180 }
21181
21182 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21183
21184 static hashval_t
21185 vcall_insn_table_hash (const void *x)
21186 {
21187   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21188 }
21189
21190 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21191    insnd_uid of *Y.  */
21192
21193 static int
21194 vcall_insn_table_eq (const void *x, const void *y)
21195 {
21196   return (((const struct vcall_insn *) x)->insn_uid
21197           == ((const struct vcall_insn *) y)->insn_uid);
21198 }
21199
21200 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21201
21202 static void
21203 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21204 {
21205   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21206   struct vcall_insn **slot;
21207
21208   gcc_assert (item);
21209   item->insn_uid = insn_uid;
21210   item->vtable_slot = vtable_slot;
21211   slot = (struct vcall_insn **)
21212       htab_find_slot_with_hash (vcall_insn_table, &item,
21213                                 (hashval_t) insn_uid, INSERT);
21214   *slot = item;
21215 }
21216
21217 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21218
21219 static unsigned int
21220 lookup_vcall_insn (unsigned int insn_uid)
21221 {
21222   struct vcall_insn item;
21223   struct vcall_insn *p;
21224
21225   item.insn_uid = insn_uid;
21226   item.vtable_slot = 0;
21227   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21228                                                  (void *) &item,
21229                                                  (hashval_t) insn_uid);
21230   if (p == NULL)
21231     return (unsigned int) -1;
21232   return p->vtable_slot;
21233 }
21234
21235
21236 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21237    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21238    is the vtable slot index that we will need to put in the virtual call
21239    table later.  */
21240
21241 static void
21242 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21243 {
21244   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21245     {
21246       tree token = OBJ_TYPE_REF_TOKEN (addr);
21247       if (TREE_CODE (token) == INTEGER_CST)
21248         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21249     }
21250 }
21251
21252 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21253    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21254    with NEW_INSN.  */
21255
21256 static void
21257 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21258 {
21259   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21260
21261   if (vtable_slot != (unsigned int) -1)
21262     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21263 }
21264
21265 /* Called by the final INSN scan whenever we see a virtual function call.
21266    Make an entry into the virtual call table, recording the point of call
21267    and the slot index of the vtable entry used to call the virtual member
21268    function.  The slot index was associated with the INSN_UID during the
21269    lowering to RTL.  */
21270
21271 static void
21272 dwarf2out_virtual_call (int insn_uid)
21273 {
21274   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21275   vcall_entry e;
21276
21277   if (vtable_slot == (unsigned int) -1)
21278     return;
21279
21280   e.poc_label_num = poc_label_num++;
21281   e.vtable_slot = vtable_slot;
21282   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21283
21284   /* Drop a label at the return point to mark the point of call.  */
21285   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21286 }
21287
21288 /* Called by the final INSN scan whenever we see a var location.  We
21289    use it to drop labels in the right places, and throw the location in
21290    our lookup table.  */
21291
21292 static void
21293 dwarf2out_var_location (rtx loc_note)
21294 {
21295   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21296   struct var_loc_node *newloc;
21297   rtx next_real;
21298   static const char *last_label;
21299   static const char *last_postcall_label;
21300   static bool last_in_cold_section_p;
21301   tree decl;
21302
21303   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21304     return;
21305
21306   next_real = next_real_insn (loc_note);
21307   /* If there are no instructions which would be affected by this note,
21308      don't do anything.  */
21309   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21310     return;
21311
21312   /* If there were any real insns between note we processed last time
21313      and this note (or if it is the first note), clear
21314      last_{,postcall_}label so that they are not reused this time.  */
21315   if (last_var_location_insn == NULL_RTX
21316       || last_var_location_insn != next_real
21317       || last_in_cold_section_p != in_cold_section_p)
21318     {
21319       last_label = NULL;
21320       last_postcall_label = NULL;
21321     }
21322
21323   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21324   newloc = add_var_loc_to_decl (decl, loc_note,
21325                                 NOTE_DURING_CALL_P (loc_note)
21326                                 ? last_postcall_label : last_label);
21327   if (newloc == NULL)
21328     return;
21329
21330   /* If there were no real insns between note we processed last time
21331      and this note, use the label we emitted last time.  Otherwise
21332      create a new label and emit it.  */
21333   if (last_label == NULL)
21334     {
21335       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21336       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21337       loclabel_num++;
21338       last_label = ggc_strdup (loclabel);
21339     }
21340
21341   if (!NOTE_DURING_CALL_P (loc_note))
21342     newloc->label = last_label;
21343   else
21344     {
21345       if (!last_postcall_label)
21346         {
21347           sprintf (loclabel, "%s-1", last_label);
21348           last_postcall_label = ggc_strdup (loclabel);
21349         }
21350       newloc->label = last_postcall_label;
21351     }
21352
21353   last_var_location_insn = next_real;
21354   last_in_cold_section_p = in_cold_section_p;
21355 }
21356
21357 /* We need to reset the locations at the beginning of each
21358    function. We can't do this in the end_function hook, because the
21359    declarations that use the locations won't have been output when
21360    that hook is called.  Also compute have_multiple_function_sections here.  */
21361
21362 static void
21363 dwarf2out_begin_function (tree fun)
21364 {
21365   if (function_section (fun) != text_section)
21366     have_multiple_function_sections = true;
21367
21368   dwarf2out_note_section_used ();
21369 }
21370
21371 /* Output a label to mark the beginning of a source code line entry
21372    and record information relating to this source line, in
21373    'line_info_table' for later output of the .debug_line section.  */
21374
21375 static void
21376 dwarf2out_source_line (unsigned int line, const char *filename,
21377                        int discriminator, bool is_stmt)
21378 {
21379   static bool last_is_stmt = true;
21380
21381   if (debug_info_level >= DINFO_LEVEL_NORMAL
21382       && line != 0)
21383     {
21384       int file_num = maybe_emit_file (lookup_filename (filename));
21385
21386       switch_to_section (current_function_section ());
21387
21388       /* If requested, emit something human-readable.  */
21389       if (flag_debug_asm)
21390         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21391                  filename, line);
21392
21393       if (DWARF2_ASM_LINE_DEBUG_INFO)
21394         {
21395           /* Emit the .loc directive understood by GNU as.  */
21396           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21397           if (is_stmt != last_is_stmt)
21398             {
21399               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21400               last_is_stmt = is_stmt;
21401             }
21402           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21403             fprintf (asm_out_file, " discriminator %d", discriminator);
21404           fputc ('\n', asm_out_file);
21405
21406           /* Indicate that line number info exists.  */
21407           line_info_table_in_use++;
21408         }
21409       else if (function_section (current_function_decl) != text_section)
21410         {
21411           dw_separate_line_info_ref line_info;
21412           targetm.asm_out.internal_label (asm_out_file,
21413                                           SEPARATE_LINE_CODE_LABEL,
21414                                           separate_line_info_table_in_use);
21415
21416           /* Expand the line info table if necessary.  */
21417           if (separate_line_info_table_in_use
21418               == separate_line_info_table_allocated)
21419             {
21420               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21421               separate_line_info_table
21422                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21423                                  separate_line_info_table,
21424                                  separate_line_info_table_allocated);
21425               memset (separate_line_info_table
21426                        + separate_line_info_table_in_use,
21427                       0,
21428                       (LINE_INFO_TABLE_INCREMENT
21429                        * sizeof (dw_separate_line_info_entry)));
21430             }
21431
21432           /* Add the new entry at the end of the line_info_table.  */
21433           line_info
21434             = &separate_line_info_table[separate_line_info_table_in_use++];
21435           line_info->dw_file_num = file_num;
21436           line_info->dw_line_num = line;
21437           line_info->function = current_function_funcdef_no;
21438         }
21439       else
21440         {
21441           dw_line_info_ref line_info;
21442
21443           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21444                                      line_info_table_in_use);
21445
21446           /* Expand the line info table if necessary.  */
21447           if (line_info_table_in_use == line_info_table_allocated)
21448             {
21449               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21450               line_info_table
21451                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21452                                  line_info_table_allocated);
21453               memset (line_info_table + line_info_table_in_use, 0,
21454                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21455             }
21456
21457           /* Add the new entry at the end of the line_info_table.  */
21458           line_info = &line_info_table[line_info_table_in_use++];
21459           line_info->dw_file_num = file_num;
21460           line_info->dw_line_num = line;
21461         }
21462     }
21463 }
21464
21465 /* Record the beginning of a new source file.  */
21466
21467 static void
21468 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21469 {
21470   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21471     {
21472       /* Record the beginning of the file for break_out_includes.  */
21473       dw_die_ref bincl_die;
21474
21475       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21476       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21477     }
21478
21479   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21480     {
21481       int file_num = maybe_emit_file (lookup_filename (filename));
21482
21483       switch_to_section (debug_macinfo_section);
21484       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21485       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21486                                    lineno);
21487
21488       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21489     }
21490 }
21491
21492 /* Record the end of a source file.  */
21493
21494 static void
21495 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21496 {
21497   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21498     /* Record the end of the file for break_out_includes.  */
21499     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21500
21501   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21502     {
21503       switch_to_section (debug_macinfo_section);
21504       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21505     }
21506 }
21507
21508 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21509    the tail part of the directive line, i.e. the part which is past the
21510    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21511
21512 static void
21513 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21514                   const char *buffer ATTRIBUTE_UNUSED)
21515 {
21516   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21517     {
21518       switch_to_section (debug_macinfo_section);
21519       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21520       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21521       dw2_asm_output_nstring (buffer, -1, "The macro");
21522     }
21523 }
21524
21525 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21526    the tail part of the directive line, i.e. the part which is past the
21527    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21528
21529 static void
21530 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21531                  const char *buffer ATTRIBUTE_UNUSED)
21532 {
21533   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21534     {
21535       switch_to_section (debug_macinfo_section);
21536       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21537       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21538       dw2_asm_output_nstring (buffer, -1, "The macro");
21539     }
21540 }
21541
21542 /* Set up for Dwarf output at the start of compilation.  */
21543
21544 static void
21545 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21546 {
21547   /* Allocate the file_table.  */
21548   file_table = htab_create_ggc (50, file_table_hash,
21549                                 file_table_eq, NULL);
21550
21551   /* Allocate the decl_die_table.  */
21552   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21553                                     decl_die_table_eq, NULL);
21554
21555   /* Allocate the decl_loc_table.  */
21556   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21557                                     decl_loc_table_eq, NULL);
21558
21559   /* Allocate the initial hunk of the decl_scope_table.  */
21560   decl_scope_table = VEC_alloc (tree, gc, 256);
21561
21562   /* Allocate the initial hunk of the abbrev_die_table.  */
21563   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21564     (ABBREV_DIE_TABLE_INCREMENT);
21565   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21566   /* Zero-th entry is allocated, but unused.  */
21567   abbrev_die_table_in_use = 1;
21568
21569   /* Allocate the initial hunk of the line_info_table.  */
21570   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21571     (LINE_INFO_TABLE_INCREMENT);
21572   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21573
21574   /* Zero-th entry is allocated, but unused.  */
21575   line_info_table_in_use = 1;
21576
21577   /* Allocate the pubtypes and pubnames vectors.  */
21578   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21579   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21580
21581   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21582   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21583                                       vcall_insn_table_eq, NULL);
21584
21585   /* Generate the initial DIE for the .debug section.  Note that the (string)
21586      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21587      will (typically) be a relative pathname and that this pathname should be
21588      taken as being relative to the directory from which the compiler was
21589      invoked when the given (base) source file was compiled.  We will fill
21590      in this value in dwarf2out_finish.  */
21591   comp_unit_die = gen_compile_unit_die (NULL);
21592
21593   incomplete_types = VEC_alloc (tree, gc, 64);
21594
21595   used_rtx_array = VEC_alloc (rtx, gc, 32);
21596
21597   debug_info_section = get_section (DEBUG_INFO_SECTION,
21598                                     SECTION_DEBUG, NULL);
21599   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21600                                       SECTION_DEBUG, NULL);
21601   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21602                                        SECTION_DEBUG, NULL);
21603   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21604                                        SECTION_DEBUG, NULL);
21605   debug_line_section = get_section (DEBUG_LINE_SECTION,
21606                                     SECTION_DEBUG, NULL);
21607   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21608                                    SECTION_DEBUG, NULL);
21609   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21610                                         SECTION_DEBUG, NULL);
21611   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21612                                         SECTION_DEBUG, NULL);
21613   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21614                                      SECTION_DEBUG, NULL);
21615   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21616                                      SECTION_DEBUG, NULL);
21617   debug_str_section = get_section (DEBUG_STR_SECTION,
21618                                    DEBUG_STR_SECTION_FLAGS, NULL);
21619   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21620                                       SECTION_DEBUG, NULL);
21621   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21622                                      SECTION_DEBUG, NULL);
21623
21624   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21625   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21626                                DEBUG_ABBREV_SECTION_LABEL, 0);
21627   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21628   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21629                                COLD_TEXT_SECTION_LABEL, 0);
21630   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21631
21632   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21633                                DEBUG_INFO_SECTION_LABEL, 0);
21634   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21635                                DEBUG_LINE_SECTION_LABEL, 0);
21636   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21637                                DEBUG_RANGES_SECTION_LABEL, 0);
21638   switch_to_section (debug_abbrev_section);
21639   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21640   switch_to_section (debug_info_section);
21641   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21642   switch_to_section (debug_line_section);
21643   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21644
21645   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21646     {
21647       switch_to_section (debug_macinfo_section);
21648       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21649                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21650       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21651     }
21652
21653   switch_to_section (text_section);
21654   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21655   if (flag_reorder_blocks_and_partition)
21656     {
21657       cold_text_section = unlikely_text_section ();
21658       switch_to_section (cold_text_section);
21659       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21660     }
21661
21662 }
21663
21664 /* Called before cgraph_optimize starts outputtting functions, variables
21665    and toplevel asms into assembly.  */
21666
21667 static void
21668 dwarf2out_assembly_start (void)
21669 {
21670   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21671     {
21672 #ifndef TARGET_UNWIND_INFO
21673       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21674 #endif
21675         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21676     }
21677 }
21678
21679 /* A helper function for dwarf2out_finish called through
21680    htab_traverse.  Emit one queued .debug_str string.  */
21681
21682 static int
21683 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21684 {
21685   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21686
21687   if (node->label && node->refcount)
21688     {
21689       switch_to_section (debug_str_section);
21690       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21691       assemble_string (node->str, strlen (node->str) + 1);
21692     }
21693
21694   return 1;
21695 }
21696
21697 #if ENABLE_ASSERT_CHECKING
21698 /* Verify that all marks are clear.  */
21699
21700 static void
21701 verify_marks_clear (dw_die_ref die)
21702 {
21703   dw_die_ref c;
21704
21705   gcc_assert (! die->die_mark);
21706   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21707 }
21708 #endif /* ENABLE_ASSERT_CHECKING */
21709
21710 /* Clear the marks for a die and its children.
21711    Be cool if the mark isn't set.  */
21712
21713 static void
21714 prune_unmark_dies (dw_die_ref die)
21715 {
21716   dw_die_ref c;
21717
21718   if (die->die_mark)
21719     die->die_mark = 0;
21720   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21721 }
21722
21723 /* Given DIE that we're marking as used, find any other dies
21724    it references as attributes and mark them as used.  */
21725
21726 static void
21727 prune_unused_types_walk_attribs (dw_die_ref die)
21728 {
21729   dw_attr_ref a;
21730   unsigned ix;
21731
21732   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21733     {
21734       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21735         {
21736           /* A reference to another DIE.
21737              Make sure that it will get emitted.
21738              If it was broken out into a comdat group, don't follow it.  */
21739           if (dwarf_version < 4
21740               || a->dw_attr == DW_AT_specification
21741               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21742             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21743         }
21744       /* Set the string's refcount to 0 so that prune_unused_types_mark
21745          accounts properly for it.  */
21746       if (AT_class (a) == dw_val_class_str)
21747         a->dw_attr_val.v.val_str->refcount = 0;
21748     }
21749 }
21750
21751
21752 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21753    to DIE's children.  */
21754
21755 static void
21756 prune_unused_types_mark (dw_die_ref die, int dokids)
21757 {
21758   dw_die_ref c;
21759
21760   if (die->die_mark == 0)
21761     {
21762       /* We haven't done this node yet.  Mark it as used.  */
21763       die->die_mark = 1;
21764
21765       /* We also have to mark its parents as used.
21766          (But we don't want to mark our parents' kids due to this.)  */
21767       if (die->die_parent)
21768         prune_unused_types_mark (die->die_parent, 0);
21769
21770       /* Mark any referenced nodes.  */
21771       prune_unused_types_walk_attribs (die);
21772
21773       /* If this node is a specification,
21774          also mark the definition, if it exists.  */
21775       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21776         prune_unused_types_mark (die->die_definition, 1);
21777     }
21778
21779   if (dokids && die->die_mark != 2)
21780     {
21781       /* We need to walk the children, but haven't done so yet.
21782          Remember that we've walked the kids.  */
21783       die->die_mark = 2;
21784
21785       /* If this is an array type, we need to make sure our
21786          kids get marked, even if they're types.  If we're
21787          breaking out types into comdat sections, do this
21788          for all type definitions.  */
21789       if (die->die_tag == DW_TAG_array_type
21790           || (dwarf_version >= 4
21791               && is_type_die (die) && ! is_declaration_die (die)))
21792         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21793       else
21794         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21795     }
21796 }
21797
21798 /* For local classes, look if any static member functions were emitted
21799    and if so, mark them.  */
21800
21801 static void
21802 prune_unused_types_walk_local_classes (dw_die_ref die)
21803 {
21804   dw_die_ref c;
21805
21806   if (die->die_mark == 2)
21807     return;
21808
21809   switch (die->die_tag)
21810     {
21811     case DW_TAG_structure_type:
21812     case DW_TAG_union_type:
21813     case DW_TAG_class_type:
21814       break;
21815
21816     case DW_TAG_subprogram:
21817       if (!get_AT_flag (die, DW_AT_declaration)
21818           || die->die_definition != NULL)
21819         prune_unused_types_mark (die, 1);
21820       return;
21821
21822     default:
21823       return;
21824     }
21825
21826   /* Mark children.  */
21827   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21828 }
21829
21830 /* Walk the tree DIE and mark types that we actually use.  */
21831
21832 static void
21833 prune_unused_types_walk (dw_die_ref die)
21834 {
21835   dw_die_ref c;
21836
21837   /* Don't do anything if this node is already marked and
21838      children have been marked as well.  */
21839   if (die->die_mark == 2)
21840     return;
21841
21842   switch (die->die_tag)
21843     {
21844     case DW_TAG_structure_type:
21845     case DW_TAG_union_type:
21846     case DW_TAG_class_type:
21847       if (die->die_perennial_p)
21848         break;
21849
21850       for (c = die->die_parent; c; c = c->die_parent)
21851         if (c->die_tag == DW_TAG_subprogram)
21852           break;
21853
21854       /* Finding used static member functions inside of classes
21855          is needed just for local classes, because for other classes
21856          static member function DIEs with DW_AT_specification
21857          are emitted outside of the DW_TAG_*_type.  If we ever change
21858          it, we'd need to call this even for non-local classes.  */
21859       if (c)
21860         prune_unused_types_walk_local_classes (die);
21861
21862       /* It's a type node --- don't mark it.  */
21863       return;
21864
21865     case DW_TAG_const_type:
21866     case DW_TAG_packed_type:
21867     case DW_TAG_pointer_type:
21868     case DW_TAG_reference_type:
21869     case DW_TAG_rvalue_reference_type:
21870     case DW_TAG_volatile_type:
21871     case DW_TAG_typedef:
21872     case DW_TAG_array_type:
21873     case DW_TAG_interface_type:
21874     case DW_TAG_friend:
21875     case DW_TAG_variant_part:
21876     case DW_TAG_enumeration_type:
21877     case DW_TAG_subroutine_type:
21878     case DW_TAG_string_type:
21879     case DW_TAG_set_type:
21880     case DW_TAG_subrange_type:
21881     case DW_TAG_ptr_to_member_type:
21882     case DW_TAG_file_type:
21883       if (die->die_perennial_p)
21884         break;
21885
21886       /* It's a type node --- don't mark it.  */
21887       return;
21888
21889     default:
21890       /* Mark everything else.  */
21891       break;
21892   }
21893
21894   if (die->die_mark == 0)
21895     {
21896       die->die_mark = 1;
21897
21898       /* Now, mark any dies referenced from here.  */
21899       prune_unused_types_walk_attribs (die);
21900     }
21901
21902   die->die_mark = 2;
21903
21904   /* Mark children.  */
21905   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21906 }
21907
21908 /* Increment the string counts on strings referred to from DIE's
21909    attributes.  */
21910
21911 static void
21912 prune_unused_types_update_strings (dw_die_ref die)
21913 {
21914   dw_attr_ref a;
21915   unsigned ix;
21916
21917   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21918     if (AT_class (a) == dw_val_class_str)
21919       {
21920         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21921         s->refcount++;
21922         /* Avoid unnecessarily putting strings that are used less than
21923            twice in the hash table.  */
21924         if (s->refcount
21925             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21926           {
21927             void ** slot;
21928             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21929                                              htab_hash_string (s->str),
21930                                              INSERT);
21931             gcc_assert (*slot == NULL);
21932             *slot = s;
21933           }
21934       }
21935 }
21936
21937 /* Remove from the tree DIE any dies that aren't marked.  */
21938
21939 static void
21940 prune_unused_types_prune (dw_die_ref die)
21941 {
21942   dw_die_ref c;
21943
21944   gcc_assert (die->die_mark);
21945   prune_unused_types_update_strings (die);
21946
21947   if (! die->die_child)
21948     return;
21949
21950   c = die->die_child;
21951   do {
21952     dw_die_ref prev = c;
21953     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21954       if (c == die->die_child)
21955         {
21956           /* No marked children between 'prev' and the end of the list.  */
21957           if (prev == c)
21958             /* No marked children at all.  */
21959             die->die_child = NULL;
21960           else
21961             {
21962               prev->die_sib = c->die_sib;
21963               die->die_child = prev;
21964             }
21965           return;
21966         }
21967
21968     if (c != prev->die_sib)
21969       prev->die_sib = c;
21970     prune_unused_types_prune (c);
21971   } while (c != die->die_child);
21972 }
21973
21974 /* A helper function for dwarf2out_finish called through
21975    htab_traverse.  Clear .debug_str strings that we haven't already
21976    decided to emit.  */
21977
21978 static int
21979 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21980 {
21981   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21982
21983   if (!node->label || !node->refcount)
21984     htab_clear_slot (debug_str_hash, h);
21985
21986   return 1;
21987 }
21988
21989 /* Remove dies representing declarations that we never use.  */
21990
21991 static void
21992 prune_unused_types (void)
21993 {
21994   unsigned int i;
21995   limbo_die_node *node;
21996   comdat_type_node *ctnode;
21997   pubname_ref pub;
21998   dcall_entry *dcall;
21999
22000 #if ENABLE_ASSERT_CHECKING
22001   /* All the marks should already be clear.  */
22002   verify_marks_clear (comp_unit_die);
22003   for (node = limbo_die_list; node; node = node->next)
22004     verify_marks_clear (node->die);
22005   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22006     verify_marks_clear (ctnode->root_die);
22007 #endif /* ENABLE_ASSERT_CHECKING */
22008
22009   /* Mark types that are used in global variables.  */
22010   premark_types_used_by_global_vars ();
22011
22012   /* Set the mark on nodes that are actually used.  */
22013   prune_unused_types_walk (comp_unit_die);
22014   for (node = limbo_die_list; node; node = node->next)
22015     prune_unused_types_walk (node->die);
22016   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22017     {
22018       prune_unused_types_walk (ctnode->root_die);
22019       prune_unused_types_mark (ctnode->type_die, 1);
22020     }
22021
22022   /* Also set the mark on nodes referenced from the
22023      pubname_table or arange_table.  */
22024   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
22025     prune_unused_types_mark (pub->die, 1);
22026   for (i = 0; i < arange_table_in_use; i++)
22027     prune_unused_types_mark (arange_table[i], 1);
22028
22029   /* Mark nodes referenced from the direct call table.  */
22030   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
22031     prune_unused_types_mark (dcall->targ_die, 1);
22032
22033   /* Get rid of nodes that aren't marked; and update the string counts.  */
22034   if (debug_str_hash && debug_str_hash_forced)
22035     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22036   else if (debug_str_hash)
22037     htab_empty (debug_str_hash);
22038   prune_unused_types_prune (comp_unit_die);
22039   for (node = limbo_die_list; node; node = node->next)
22040     prune_unused_types_prune (node->die);
22041   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22042     prune_unused_types_prune (ctnode->root_die);
22043
22044   /* Leave the marks clear.  */
22045   prune_unmark_dies (comp_unit_die);
22046   for (node = limbo_die_list; node; node = node->next)
22047     prune_unmark_dies (node->die);
22048   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22049     prune_unmark_dies (ctnode->root_die);
22050 }
22051
22052 /* Set the parameter to true if there are any relative pathnames in
22053    the file table.  */
22054 static int
22055 file_table_relative_p (void ** slot, void *param)
22056 {
22057   bool *p = (bool *) param;
22058   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22059   if (!IS_ABSOLUTE_PATH (d->filename))
22060     {
22061       *p = true;
22062       return 0;
22063     }
22064   return 1;
22065 }
22066
22067 /* Routines to manipulate hash table of comdat type units.  */
22068
22069 static hashval_t
22070 htab_ct_hash (const void *of)
22071 {
22072   hashval_t h;
22073   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22074
22075   memcpy (&h, type_node->signature, sizeof (h));
22076   return h;
22077 }
22078
22079 static int
22080 htab_ct_eq (const void *of1, const void *of2)
22081 {
22082   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22083   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22084
22085   return (! memcmp (type_node_1->signature, type_node_2->signature,
22086                     DWARF_TYPE_SIGNATURE_SIZE));
22087 }
22088
22089 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22090    to the location it would have been added, should we know its
22091    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22092    probably improve compactness of debug info, removing equivalent
22093    abbrevs, and hide any differences caused by deferring the
22094    computation of the assembler name, triggered by e.g. PCH.  */
22095
22096 static inline void
22097 move_linkage_attr (dw_die_ref die)
22098 {
22099   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22100   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22101
22102   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22103               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22104
22105   while (--ix > 0)
22106     {
22107       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22108
22109       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22110         break;
22111     }
22112
22113   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22114     {
22115       VEC_pop (dw_attr_node, die->die_attr);
22116       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22117     }
22118 }
22119
22120 /* Helper function for resolve_addr, attempt to resolve
22121    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22122    SYMBOL_REFs refer to variables emitted in the current CU.  */
22123
22124 static int
22125 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22126 {
22127   rtx rtl = *addr;
22128
22129   if (GET_CODE (rtl) == CONST_STRING)
22130     {
22131       size_t len = strlen (XSTR (rtl, 0)) + 1;
22132       tree t = build_string (len, XSTR (rtl, 0));
22133       tree tlen = build_int_cst (NULL_TREE, len - 1);
22134       TREE_TYPE (t)
22135         = build_array_type (char_type_node, build_index_type (tlen));
22136       rtl = lookup_constant_def (t);
22137       if (!rtl || !MEM_P (rtl))
22138         return 1;
22139       rtl = XEXP (rtl, 0);
22140       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22141       *addr = rtl;
22142       return 0;
22143     }
22144
22145   if (GET_CODE (rtl) == SYMBOL_REF
22146       && SYMBOL_REF_DECL (rtl)
22147       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22148     return 1;
22149
22150   if (GET_CODE (rtl) == CONST
22151       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22152     return 1;
22153
22154   return 0;
22155 }
22156
22157 /* Helper function for resolve_addr, handle one location
22158    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22159    the location list couldn't be resolved.  */
22160
22161 static bool
22162 resolve_addr_in_expr (dw_loc_descr_ref loc)
22163 {
22164   for (; loc; loc = loc->dw_loc_next)
22165     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22166          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22167         || (loc->dw_loc_opc == DW_OP_implicit_value
22168             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22169             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22170       return false;
22171   return true;
22172 }
22173
22174 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22175    an address in .rodata section if the string literal is emitted there,
22176    or remove the containing location list or replace DW_AT_const_value
22177    with DW_AT_location and empty location expression, if it isn't found
22178    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22179    to something that has been emitted in the current CU.  */
22180
22181 static void
22182 resolve_addr (dw_die_ref die)
22183 {
22184   dw_die_ref c;
22185   dw_attr_ref a;
22186   dw_loc_list_ref *curr;
22187   unsigned ix;
22188
22189   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
22190     switch (AT_class (a))
22191       {
22192       case dw_val_class_loc_list:
22193         curr = AT_loc_list_ptr (a);
22194         while (*curr)
22195           {
22196             if (!resolve_addr_in_expr ((*curr)->expr))
22197               {
22198                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22199                 if (next && (*curr)->ll_symbol)
22200                   {
22201                     gcc_assert (!next->ll_symbol);
22202                     next->ll_symbol = (*curr)->ll_symbol;
22203                   }
22204                 *curr = next;
22205               }
22206             else
22207               curr = &(*curr)->dw_loc_next;
22208           }
22209         if (!AT_loc_list (a))
22210           {
22211             remove_AT (die, a->dw_attr);
22212             ix--;
22213           }
22214         break;
22215       case dw_val_class_loc:
22216         if (!resolve_addr_in_expr (AT_loc (a)))
22217           {
22218             remove_AT (die, a->dw_attr);
22219             ix--;
22220           }
22221         break;
22222       case dw_val_class_addr:
22223         if (a->dw_attr == DW_AT_const_value
22224             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22225           {
22226             remove_AT (die, a->dw_attr);
22227             ix--;
22228           }
22229         break;
22230       default:
22231         break;
22232       }
22233
22234   FOR_EACH_CHILD (die, c, resolve_addr (c));
22235 }
22236
22237 /* Output stuff that dwarf requires at the end of every file,
22238    and generate the DWARF-2 debugging info.  */
22239
22240 static void
22241 dwarf2out_finish (const char *filename)
22242 {
22243   limbo_die_node *node, *next_node;
22244   comdat_type_node *ctnode;
22245   htab_t comdat_type_table;
22246   dw_die_ref die = 0;
22247   unsigned int i;
22248
22249   gen_remaining_tmpl_value_param_die_attribute ();
22250
22251   /* Add the name for the main input file now.  We delayed this from
22252      dwarf2out_init to avoid complications with PCH.  */
22253   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22254   if (!IS_ABSOLUTE_PATH (filename))
22255     add_comp_dir_attribute (comp_unit_die);
22256   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22257     {
22258       bool p = false;
22259       htab_traverse (file_table, file_table_relative_p, &p);
22260       if (p)
22261         add_comp_dir_attribute (comp_unit_die);
22262     }
22263
22264   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22265     {
22266       add_location_or_const_value_attribute (
22267         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22268         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22269         DW_AT_location);
22270     }
22271
22272   /* Traverse the limbo die list, and add parent/child links.  The only
22273      dies without parents that should be here are concrete instances of
22274      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22275      For concrete instances, we can get the parent die from the abstract
22276      instance.  */
22277   for (node = limbo_die_list; node; node = next_node)
22278     {
22279       next_node = node->next;
22280       die = node->die;
22281
22282       if (die->die_parent == NULL)
22283         {
22284           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22285
22286           if (origin)
22287             add_child_die (origin->die_parent, die);
22288           else if (die == comp_unit_die)
22289             ;
22290           else if (seen_error ())
22291             /* It's OK to be confused by errors in the input.  */
22292             add_child_die (comp_unit_die, die);
22293           else
22294             {
22295               /* In certain situations, the lexical block containing a
22296                  nested function can be optimized away, which results
22297                  in the nested function die being orphaned.  Likewise
22298                  with the return type of that nested function.  Force
22299                  this to be a child of the containing function.
22300
22301                  It may happen that even the containing function got fully
22302                  inlined and optimized out.  In that case we are lost and
22303                  assign the empty child.  This should not be big issue as
22304                  the function is likely unreachable too.  */
22305               tree context = NULL_TREE;
22306
22307               gcc_assert (node->created_for);
22308
22309               if (DECL_P (node->created_for))
22310                 context = DECL_CONTEXT (node->created_for);
22311               else if (TYPE_P (node->created_for))
22312                 context = TYPE_CONTEXT (node->created_for);
22313
22314               gcc_assert (context
22315                           && (TREE_CODE (context) == FUNCTION_DECL
22316                               || TREE_CODE (context) == NAMESPACE_DECL));
22317
22318               origin = lookup_decl_die (context);
22319               if (origin)
22320                 add_child_die (origin, die);
22321               else
22322                 add_child_die (comp_unit_die, die);
22323             }
22324         }
22325     }
22326
22327   limbo_die_list = NULL;
22328
22329   resolve_addr (comp_unit_die);
22330
22331   for (node = deferred_asm_name; node; node = node->next)
22332     {
22333       tree decl = node->created_for;
22334       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22335         {
22336           add_linkage_attr (node->die, decl);
22337           move_linkage_attr (node->die);
22338         }
22339     }
22340
22341   deferred_asm_name = NULL;
22342
22343   /* Walk through the list of incomplete types again, trying once more to
22344      emit full debugging info for them.  */
22345   retry_incomplete_types ();
22346
22347   if (flag_eliminate_unused_debug_types)
22348     prune_unused_types ();
22349
22350   /* Generate separate CUs for each of the include files we've seen.
22351      They will go into limbo_die_list.  */
22352   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22353     break_out_includes (comp_unit_die);
22354
22355   /* Generate separate COMDAT sections for type DIEs. */
22356   if (dwarf_version >= 4)
22357     {
22358       break_out_comdat_types (comp_unit_die);
22359
22360       /* Each new type_unit DIE was added to the limbo die list when created.
22361          Since these have all been added to comdat_type_list, clear the
22362          limbo die list.  */
22363       limbo_die_list = NULL;
22364
22365       /* For each new comdat type unit, copy declarations for incomplete
22366          types to make the new unit self-contained (i.e., no direct
22367          references to the main compile unit).  */
22368       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22369         copy_decls_for_unworthy_types (ctnode->root_die);
22370       copy_decls_for_unworthy_types (comp_unit_die);
22371
22372       /* In the process of copying declarations from one unit to another,
22373          we may have left some declarations behind that are no longer
22374          referenced.  Prune them.  */
22375       prune_unused_types ();
22376     }
22377
22378   /* Traverse the DIE's and add add sibling attributes to those DIE's
22379      that have children.  */
22380   add_sibling_attributes (comp_unit_die);
22381   for (node = limbo_die_list; node; node = node->next)
22382     add_sibling_attributes (node->die);
22383   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22384     add_sibling_attributes (ctnode->root_die);
22385
22386   /* Output a terminator label for the .text section.  */
22387   switch_to_section (text_section);
22388   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22389   if (flag_reorder_blocks_and_partition)
22390     {
22391       switch_to_section (unlikely_text_section ());
22392       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22393     }
22394
22395   /* We can only use the low/high_pc attributes if all of the code was
22396      in .text.  */
22397   if (!have_multiple_function_sections
22398       || !(dwarf_version >= 3 || !dwarf_strict))
22399     {
22400       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22401       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22402     }
22403
22404   else
22405     {
22406       unsigned fde_idx = 0;
22407       bool range_list_added = false;
22408
22409       /* We need to give .debug_loc and .debug_ranges an appropriate
22410          "base address".  Use zero so that these addresses become
22411          absolute.  Historically, we've emitted the unexpected
22412          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22413          Emit both to give time for other tools to adapt.  */
22414       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22415       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22416
22417       if (text_section_used)
22418         add_ranges_by_labels (comp_unit_die, text_section_label,
22419                               text_end_label, &range_list_added);
22420       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22421         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22422                               cold_end_label, &range_list_added);
22423
22424       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22425         {
22426           dw_fde_ref fde = &fde_table[fde_idx];
22427
22428           if (fde->dw_fde_switched_sections)
22429             {
22430               if (!fde->in_std_section)
22431                 add_ranges_by_labels (comp_unit_die,
22432                                       fde->dw_fde_hot_section_label,
22433                                       fde->dw_fde_hot_section_end_label,
22434                                       &range_list_added);
22435               if (!fde->cold_in_std_section)
22436                 add_ranges_by_labels (comp_unit_die,
22437                                       fde->dw_fde_unlikely_section_label,
22438                                       fde->dw_fde_unlikely_section_end_label,
22439                                       &range_list_added);
22440             }
22441           else if (!fde->in_std_section)
22442             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22443                                   fde->dw_fde_end, &range_list_added);
22444         }
22445
22446       if (range_list_added)
22447         add_ranges (NULL);
22448     }
22449
22450   /* Output location list section if necessary.  */
22451   if (have_location_lists)
22452     {
22453       /* Output the location lists info.  */
22454       switch_to_section (debug_loc_section);
22455       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22456                                    DEBUG_LOC_SECTION_LABEL, 0);
22457       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22458       output_location_lists (die);
22459     }
22460
22461   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22462     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22463                     debug_line_section_label);
22464
22465   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22466     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22467
22468   /* Output all of the compilation units.  We put the main one last so that
22469      the offsets are available to output_pubnames.  */
22470   for (node = limbo_die_list; node; node = node->next)
22471     output_comp_unit (node->die, 0);
22472
22473   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22474   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22475     {
22476       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22477
22478       /* Don't output duplicate types.  */
22479       if (*slot != HTAB_EMPTY_ENTRY)
22480         continue;
22481
22482       /* Add a pointer to the line table for the main compilation unit
22483          so that the debugger can make sense of DW_AT_decl_file
22484          attributes.  */
22485       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22486         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22487                         debug_line_section_label);
22488
22489       output_comdat_type_unit (ctnode);
22490       *slot = ctnode;
22491     }
22492   htab_delete (comdat_type_table);
22493
22494   /* Output the main compilation unit if non-empty or if .debug_macinfo
22495      has been emitted.  */
22496   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22497
22498   /* Output the abbreviation table.  */
22499   switch_to_section (debug_abbrev_section);
22500   output_abbrev_section ();
22501
22502   /* Output public names table if necessary.  */
22503   if (!VEC_empty (pubname_entry, pubname_table))
22504     {
22505       switch_to_section (debug_pubnames_section);
22506       output_pubnames (pubname_table);
22507     }
22508
22509   /* Output public types table if necessary.  */
22510   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22511      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22512      simply won't look for the section.  */
22513   if (!VEC_empty (pubname_entry, pubtype_table))
22514     {
22515       switch_to_section (debug_pubtypes_section);
22516       output_pubnames (pubtype_table);
22517     }
22518
22519   /* Output direct and virtual call tables if necessary.  */
22520   if (!VEC_empty (dcall_entry, dcall_table))
22521     {
22522       switch_to_section (debug_dcall_section);
22523       output_dcall_table ();
22524     }
22525   if (!VEC_empty (vcall_entry, vcall_table))
22526     {
22527       switch_to_section (debug_vcall_section);
22528       output_vcall_table ();
22529     }
22530
22531   /* Output the address range information.  We only put functions in the arange
22532      table, so don't write it out if we don't have any.  */
22533   if (fde_table_in_use)
22534     {
22535       switch_to_section (debug_aranges_section);
22536       output_aranges ();
22537     }
22538
22539   /* Output ranges section if necessary.  */
22540   if (ranges_table_in_use)
22541     {
22542       switch_to_section (debug_ranges_section);
22543       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22544       output_ranges ();
22545     }
22546
22547   /* Output the source line correspondence table.  We must do this
22548      even if there is no line information.  Otherwise, on an empty
22549      translation unit, we will generate a present, but empty,
22550      .debug_info section.  IRIX 6.5 `nm' will then complain when
22551      examining the file.  This is done late so that any filenames
22552      used by the debug_info section are marked as 'used'.  */
22553   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22554     {
22555       switch_to_section (debug_line_section);
22556       output_line_info ();
22557     }
22558
22559   /* Have to end the macro section.  */
22560   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22561     {
22562       switch_to_section (debug_macinfo_section);
22563       dw2_asm_output_data (1, 0, "End compilation unit");
22564     }
22565
22566   /* If we emitted any DW_FORM_strp form attribute, output the string
22567      table too.  */
22568   if (debug_str_hash)
22569     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22570 }
22571
22572 #include "gt-dwarf2out.h"