OSDN Git Service

* c-common.c (parse_optimize_options): Do not capitalize warning
[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 bool clobbers_queued_reg_save (const_rtx);
475 static void dwarf2out_frame_debug_expr (rtx, const char *);
476
477 /* Support for complex CFA locations.  */
478 static void output_cfa_loc (dw_cfi_ref);
479 static void output_cfa_loc_raw (dw_cfi_ref);
480 static void get_cfa_from_loc_descr (dw_cfa_location *,
481                                     struct dw_loc_descr_struct *);
482 static struct dw_loc_descr_struct *build_cfa_loc
483   (dw_cfa_location *, HOST_WIDE_INT);
484 static struct dw_loc_descr_struct *build_cfa_aligned_loc
485   (HOST_WIDE_INT, HOST_WIDE_INT);
486 static void def_cfa_1 (const char *, dw_cfa_location *);
487 static struct dw_loc_descr_struct *mem_loc_descriptor
488   (rtx, enum machine_mode mode, enum var_init_status);
489
490 /* How to start an assembler comment.  */
491 #ifndef ASM_COMMENT_START
492 #define ASM_COMMENT_START ";#"
493 #endif
494
495 /* Data and reference forms for relocatable data.  */
496 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
497 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
498
499 #ifndef DEBUG_FRAME_SECTION
500 #define DEBUG_FRAME_SECTION     ".debug_frame"
501 #endif
502
503 #ifndef FUNC_BEGIN_LABEL
504 #define FUNC_BEGIN_LABEL        "LFB"
505 #endif
506
507 #ifndef FUNC_END_LABEL
508 #define FUNC_END_LABEL          "LFE"
509 #endif
510
511 #ifndef PROLOGUE_END_LABEL
512 #define PROLOGUE_END_LABEL      "LPE"
513 #endif
514
515 #ifndef EPILOGUE_BEGIN_LABEL
516 #define EPILOGUE_BEGIN_LABEL    "LEB"
517 #endif
518
519 #ifndef FRAME_BEGIN_LABEL
520 #define FRAME_BEGIN_LABEL       "Lframe"
521 #endif
522 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
523 #define CIE_END_LABEL           "LECIE"
524 #define FDE_LABEL               "LSFDE"
525 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
526 #define FDE_END_LABEL           "LEFDE"
527 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
528 #define LINE_NUMBER_END_LABEL   "LELT"
529 #define LN_PROLOG_AS_LABEL      "LASLTP"
530 #define LN_PROLOG_END_LABEL     "LELTP"
531 #define DIE_LABEL_PREFIX        "DW"
532
533 /* The DWARF 2 CFA column which tracks the return address.  Normally this
534    is the column for PC, or the first column after all of the hard
535    registers.  */
536 #ifndef DWARF_FRAME_RETURN_COLUMN
537 #ifdef PC_REGNUM
538 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
539 #else
540 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
541 #endif
542 #endif
543
544 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
545    default, we just provide columns for all registers.  */
546 #ifndef DWARF_FRAME_REGNUM
547 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
548 #endif
549 \f
550 /* Hook used by __throw.  */
551
552 rtx
553 expand_builtin_dwarf_sp_column (void)
554 {
555   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
556   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
557 }
558
559 /* Return a pointer to a copy of the section string name S with all
560    attributes stripped off, and an asterisk prepended (for assemble_name).  */
561
562 static inline char *
563 stripattributes (const char *s)
564 {
565   char *stripped = XNEWVEC (char, strlen (s) + 2);
566   char *p = stripped;
567
568   *p++ = '*';
569
570   while (*s && *s != ',')
571     *p++ = *s++;
572
573   *p = '\0';
574   return stripped;
575 }
576
577 /* MEM is a memory reference for the register size table, each element of
578    which has mode MODE.  Initialize column C as a return address column.  */
579
580 static void
581 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
582 {
583   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
584   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
585   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
586 }
587
588 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
589
590 static inline HOST_WIDE_INT
591 div_data_align (HOST_WIDE_INT off)
592 {
593   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
594   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
595   return r;
596 }
597
598 /* Return true if we need a signed version of a given opcode
599    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
600
601 static inline bool
602 need_data_align_sf_opcode (HOST_WIDE_INT off)
603 {
604   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
605 }
606
607 /* Generate code to initialize the register size table.  */
608
609 void
610 expand_builtin_init_dwarf_reg_sizes (tree address)
611 {
612   unsigned int i;
613   enum machine_mode mode = TYPE_MODE (char_type_node);
614   rtx addr = expand_normal (address);
615   rtx mem = gen_rtx_MEM (BLKmode, addr);
616   bool wrote_return_column = false;
617
618   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
619     {
620       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
621
622       if (rnum < DWARF_FRAME_REGISTERS)
623         {
624           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
625           enum machine_mode save_mode = reg_raw_mode[i];
626           HOST_WIDE_INT size;
627
628           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
629             save_mode = choose_hard_reg_mode (i, 1, true);
630           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
631             {
632               if (save_mode == VOIDmode)
633                 continue;
634               wrote_return_column = true;
635             }
636           size = GET_MODE_SIZE (save_mode);
637           if (offset < 0)
638             continue;
639
640           emit_move_insn (adjust_address (mem, mode, offset),
641                           gen_int_mode (size, mode));
642         }
643     }
644
645   if (!wrote_return_column)
646     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
647
648 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
649   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
650 #endif
651
652   targetm.init_dwarf_reg_sizes_extra (address);
653 }
654
655 /* Convert a DWARF call frame info. operation to its string name */
656
657 static const char *
658 dwarf_cfi_name (unsigned int cfi_opc)
659 {
660   switch (cfi_opc)
661     {
662     case DW_CFA_advance_loc:
663       return "DW_CFA_advance_loc";
664     case DW_CFA_offset:
665       return "DW_CFA_offset";
666     case DW_CFA_restore:
667       return "DW_CFA_restore";
668     case DW_CFA_nop:
669       return "DW_CFA_nop";
670     case DW_CFA_set_loc:
671       return "DW_CFA_set_loc";
672     case DW_CFA_advance_loc1:
673       return "DW_CFA_advance_loc1";
674     case DW_CFA_advance_loc2:
675       return "DW_CFA_advance_loc2";
676     case DW_CFA_advance_loc4:
677       return "DW_CFA_advance_loc4";
678     case DW_CFA_offset_extended:
679       return "DW_CFA_offset_extended";
680     case DW_CFA_restore_extended:
681       return "DW_CFA_restore_extended";
682     case DW_CFA_undefined:
683       return "DW_CFA_undefined";
684     case DW_CFA_same_value:
685       return "DW_CFA_same_value";
686     case DW_CFA_register:
687       return "DW_CFA_register";
688     case DW_CFA_remember_state:
689       return "DW_CFA_remember_state";
690     case DW_CFA_restore_state:
691       return "DW_CFA_restore_state";
692     case DW_CFA_def_cfa:
693       return "DW_CFA_def_cfa";
694     case DW_CFA_def_cfa_register:
695       return "DW_CFA_def_cfa_register";
696     case DW_CFA_def_cfa_offset:
697       return "DW_CFA_def_cfa_offset";
698
699     /* DWARF 3 */
700     case DW_CFA_def_cfa_expression:
701       return "DW_CFA_def_cfa_expression";
702     case DW_CFA_expression:
703       return "DW_CFA_expression";
704     case DW_CFA_offset_extended_sf:
705       return "DW_CFA_offset_extended_sf";
706     case DW_CFA_def_cfa_sf:
707       return "DW_CFA_def_cfa_sf";
708     case DW_CFA_def_cfa_offset_sf:
709       return "DW_CFA_def_cfa_offset_sf";
710
711     /* SGI/MIPS specific */
712     case DW_CFA_MIPS_advance_loc8:
713       return "DW_CFA_MIPS_advance_loc8";
714
715     /* GNU extensions */
716     case DW_CFA_GNU_window_save:
717       return "DW_CFA_GNU_window_save";
718     case DW_CFA_GNU_args_size:
719       return "DW_CFA_GNU_args_size";
720     case DW_CFA_GNU_negative_offset_extended:
721       return "DW_CFA_GNU_negative_offset_extended";
722
723     default:
724       return "DW_CFA_<unknown>";
725     }
726 }
727
728 /* Return a pointer to a newly allocated Call Frame Instruction.  */
729
730 static inline dw_cfi_ref
731 new_cfi (void)
732 {
733   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
734
735   cfi->dw_cfi_next = NULL;
736   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
737   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
738
739   return cfi;
740 }
741
742 /* Add a Call Frame Instruction to list of instructions.  */
743
744 static inline void
745 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
746 {
747   dw_cfi_ref *p;
748   dw_fde_ref fde = current_fde ();
749
750   /* When DRAP is used, CFA is defined with an expression.  Redefine
751      CFA may lead to a different CFA value.   */
752   /* ??? Of course, this heuristic fails when we're annotating epilogues,
753      because of course we'll always want to redefine the CFA back to the
754      stack pointer on the way out.  Where should we move this check?  */
755   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
756     switch (cfi->dw_cfi_opc)
757       {
758         case DW_CFA_def_cfa_register:
759         case DW_CFA_def_cfa_offset:
760         case DW_CFA_def_cfa_offset_sf:
761         case DW_CFA_def_cfa:
762         case DW_CFA_def_cfa_sf:
763           gcc_unreachable ();
764
765         default:
766           break;
767       }
768
769   /* Find the end of the chain.  */
770   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
771     ;
772
773   *p = cfi;
774 }
775
776 /* Generate a new label for the CFI info to refer to.  FORCE is true
777    if a label needs to be output even when using .cfi_* directives.  */
778
779 char *
780 dwarf2out_cfi_label (bool force)
781 {
782   static char label[20];
783
784   if (!force && dwarf2out_do_cfi_asm ())
785     {
786       /* In this case, we will be emitting the asm directive instead of
787          the label, so just return a placeholder to keep the rest of the
788          interfaces happy.  */
789       strcpy (label, "<do not output>");
790     }
791   else
792     {
793       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
794       ASM_OUTPUT_LABEL (asm_out_file, label);
795     }
796
797   return label;
798 }
799
800 /* True if remember_state should be emitted before following CFI directive.  */
801 static bool emit_cfa_remember;
802
803 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
804    or to the CIE if LABEL is NULL.  */
805
806 static void
807 add_fde_cfi (const char *label, dw_cfi_ref cfi)
808 {
809   dw_cfi_ref *list_head;
810
811   if (emit_cfa_remember)
812     {
813       dw_cfi_ref cfi_remember;
814
815       /* Emit the state save.  */
816       emit_cfa_remember = false;
817       cfi_remember = new_cfi ();
818       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
819       add_fde_cfi (label, cfi_remember);
820     }
821
822   list_head = &cie_cfi_head;
823
824   if (dwarf2out_do_cfi_asm ())
825     {
826       if (label)
827         {
828           dw_fde_ref fde = current_fde ();
829
830           gcc_assert (fde != NULL);
831
832           /* We still have to add the cfi to the list so that lookup_cfa
833              works later on.  When -g2 and above we even need to force
834              emitting of CFI labels and add to list a DW_CFA_set_loc for
835              convert_cfa_to_fb_loc_list purposes.  If we're generating
836              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
837              convert_cfa_to_fb_loc_list.  */
838           if (dwarf_version == 2
839               && debug_info_level > DINFO_LEVEL_TERSE
840               && (write_symbols == DWARF2_DEBUG
841                   || write_symbols == VMS_AND_DWARF2_DEBUG))
842             {
843               switch (cfi->dw_cfi_opc)
844                 {
845                 case DW_CFA_def_cfa_offset:
846                 case DW_CFA_def_cfa_offset_sf:
847                 case DW_CFA_def_cfa_register:
848                 case DW_CFA_def_cfa:
849                 case DW_CFA_def_cfa_sf:
850                 case DW_CFA_def_cfa_expression:
851                 case DW_CFA_restore_state:
852                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
853                     label = dwarf2out_cfi_label (true);
854
855                   if (fde->dw_fde_current_label == NULL
856                       || strcmp (label, fde->dw_fde_current_label) != 0)
857                     {
858                       dw_cfi_ref xcfi;
859
860                       label = xstrdup (label);
861
862                       /* Set the location counter to the new label.  */
863                       xcfi = new_cfi ();
864                       /* It doesn't metter whether DW_CFA_set_loc
865                          or DW_CFA_advance_loc4 is added here, those aren't
866                          emitted into assembly, only looked up by
867                          convert_cfa_to_fb_loc_list.  */
868                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
869                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
870                       add_cfi (&fde->dw_fde_cfi, xcfi);
871                       fde->dw_fde_current_label = label;
872                     }
873                   break;
874                 default:
875                   break;
876                 }
877             }
878
879           output_cfi_directive (cfi);
880
881           list_head = &fde->dw_fde_cfi;
882         }
883       /* ??? If this is a CFI for the CIE, we don't emit.  This
884          assumes that the standard CIE contents that the assembler
885          uses matches the standard CIE contents that the compiler
886          uses.  This is probably a bad assumption.  I'm not quite
887          sure how to address this for now.  */
888     }
889   else if (label)
890     {
891       dw_fde_ref fde = current_fde ();
892
893       gcc_assert (fde != NULL);
894
895       if (*label == 0)
896         label = dwarf2out_cfi_label (false);
897
898       if (fde->dw_fde_current_label == NULL
899           || strcmp (label, fde->dw_fde_current_label) != 0)
900         {
901           dw_cfi_ref xcfi;
902
903           label = xstrdup (label);
904
905           /* Set the location counter to the new label.  */
906           xcfi = new_cfi ();
907           /* If we have a current label, advance from there, otherwise
908              set the location directly using set_loc.  */
909           xcfi->dw_cfi_opc = fde->dw_fde_current_label
910                              ? DW_CFA_advance_loc4
911                              : DW_CFA_set_loc;
912           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
913           add_cfi (&fde->dw_fde_cfi, xcfi);
914
915           fde->dw_fde_current_label = label;
916         }
917
918       list_head = &fde->dw_fde_cfi;
919     }
920
921   add_cfi (list_head, cfi);
922 }
923
924 /* Subroutine of lookup_cfa.  */
925
926 static void
927 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
928 {
929   switch (cfi->dw_cfi_opc)
930     {
931     case DW_CFA_def_cfa_offset:
932     case DW_CFA_def_cfa_offset_sf:
933       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
934       break;
935     case DW_CFA_def_cfa_register:
936       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
937       break;
938     case DW_CFA_def_cfa:
939     case DW_CFA_def_cfa_sf:
940       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
941       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
942       break;
943     case DW_CFA_def_cfa_expression:
944       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
945       break;
946
947     case DW_CFA_remember_state:
948       gcc_assert (!remember->in_use);
949       *remember = *loc;
950       remember->in_use = 1;
951       break;
952     case DW_CFA_restore_state:
953       gcc_assert (remember->in_use);
954       *loc = *remember;
955       remember->in_use = 0;
956       break;
957
958     default:
959       break;
960     }
961 }
962
963 /* Find the previous value for the CFA.  */
964
965 static void
966 lookup_cfa (dw_cfa_location *loc)
967 {
968   dw_cfi_ref cfi;
969   dw_fde_ref fde;
970   dw_cfa_location remember;
971
972   memset (loc, 0, sizeof (*loc));
973   loc->reg = INVALID_REGNUM;
974   remember = *loc;
975
976   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
977     lookup_cfa_1 (cfi, loc, &remember);
978
979   fde = current_fde ();
980   if (fde)
981     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
982       lookup_cfa_1 (cfi, loc, &remember);
983 }
984
985 /* The current rule for calculating the DWARF2 canonical frame address.  */
986 static dw_cfa_location cfa;
987
988 /* The register used for saving registers to the stack, and its offset
989    from the CFA.  */
990 static dw_cfa_location cfa_store;
991
992 /* The current save location around an epilogue.  */
993 static dw_cfa_location cfa_remember;
994
995 /* The running total of the size of arguments pushed onto the stack.  */
996 static HOST_WIDE_INT args_size;
997
998 /* The last args_size we actually output.  */
999 static HOST_WIDE_INT old_args_size;
1000
1001 /* Entry point to update the canonical frame address (CFA).
1002    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1003    calculated from REG+OFFSET.  */
1004
1005 void
1006 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1007 {
1008   dw_cfa_location loc;
1009   loc.indirect = 0;
1010   loc.base_offset = 0;
1011   loc.reg = reg;
1012   loc.offset = offset;
1013   def_cfa_1 (label, &loc);
1014 }
1015
1016 /* Determine if two dw_cfa_location structures define the same data.  */
1017
1018 static bool
1019 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1020 {
1021   return (loc1->reg == loc2->reg
1022           && loc1->offset == loc2->offset
1023           && loc1->indirect == loc2->indirect
1024           && (loc1->indirect == 0
1025               || loc1->base_offset == loc2->base_offset));
1026 }
1027
1028 /* This routine does the actual work.  The CFA is now calculated from
1029    the dw_cfa_location structure.  */
1030
1031 static void
1032 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1033 {
1034   dw_cfi_ref cfi;
1035   dw_cfa_location old_cfa, loc;
1036
1037   cfa = *loc_p;
1038   loc = *loc_p;
1039
1040   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1041     cfa_store.offset = loc.offset;
1042
1043   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1044   lookup_cfa (&old_cfa);
1045
1046   /* If nothing changed, no need to issue any call frame instructions.  */
1047   if (cfa_equal_p (&loc, &old_cfa))
1048     return;
1049
1050   cfi = new_cfi ();
1051
1052   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1053     {
1054       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1055          the CFA register did not change but the offset did.  The data
1056          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1057          in the assembler via the .cfi_def_cfa_offset directive.  */
1058       if (loc.offset < 0)
1059         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1060       else
1061         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1062       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1063     }
1064
1065 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1066   else if (loc.offset == old_cfa.offset
1067            && old_cfa.reg != INVALID_REGNUM
1068            && !loc.indirect
1069            && !old_cfa.indirect)
1070     {
1071       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1072          indicating the CFA register has changed to <register> but the
1073          offset has not changed.  */
1074       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1075       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1076     }
1077 #endif
1078
1079   else if (loc.indirect == 0)
1080     {
1081       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1082          indicating the CFA register has changed to <register> with
1083          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1084          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1085          directive.  */
1086       if (loc.offset < 0)
1087         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1088       else
1089         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1090       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1091       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1092     }
1093   else
1094     {
1095       /* Construct a DW_CFA_def_cfa_expression instruction to
1096          calculate the CFA using a full location expression since no
1097          register-offset pair is available.  */
1098       struct dw_loc_descr_struct *loc_list;
1099
1100       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1101       loc_list = build_cfa_loc (&loc, 0);
1102       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1103     }
1104
1105   add_fde_cfi (label, cfi);
1106 }
1107
1108 /* Add the CFI for saving a register.  REG is the CFA column number.
1109    LABEL is passed to add_fde_cfi.
1110    If SREG is -1, the register is saved at OFFSET from the CFA;
1111    otherwise it is saved in SREG.  */
1112
1113 static void
1114 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1115 {
1116   dw_cfi_ref cfi = new_cfi ();
1117   dw_fde_ref fde = current_fde ();
1118
1119   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1120
1121   /* When stack is aligned, store REG using DW_CFA_expression with
1122      FP.  */
1123   if (fde
1124       && fde->stack_realign
1125       && sreg == INVALID_REGNUM)
1126     {
1127       cfi->dw_cfi_opc = DW_CFA_expression;
1128       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1129       cfi->dw_cfi_oprnd2.dw_cfi_loc
1130         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1131     }
1132   else if (sreg == INVALID_REGNUM)
1133     {
1134       if (need_data_align_sf_opcode (offset))
1135         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1136       else if (reg & ~0x3f)
1137         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1138       else
1139         cfi->dw_cfi_opc = DW_CFA_offset;
1140       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1141     }
1142   else if (sreg == reg)
1143     cfi->dw_cfi_opc = DW_CFA_same_value;
1144   else
1145     {
1146       cfi->dw_cfi_opc = DW_CFA_register;
1147       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1148     }
1149
1150   add_fde_cfi (label, cfi);
1151 }
1152
1153 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1154    This CFI tells the unwinder that it needs to restore the window registers
1155    from the previous frame's window save area.
1156
1157    ??? Perhaps we should note in the CIE where windows are saved (instead of
1158    assuming 0(cfa)) and what registers are in the window.  */
1159
1160 void
1161 dwarf2out_window_save (const char *label)
1162 {
1163   dw_cfi_ref cfi = new_cfi ();
1164
1165   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1166   add_fde_cfi (label, cfi);
1167 }
1168
1169 /* Entry point for saving a register to the stack.  REG is the GCC register
1170    number.  LABEL and OFFSET are passed to reg_save.  */
1171
1172 void
1173 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1174 {
1175   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1176 }
1177
1178 /* Entry point for saving the return address in the stack.
1179    LABEL and OFFSET are passed to reg_save.  */
1180
1181 void
1182 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1183 {
1184   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1185 }
1186
1187 /* Entry point for saving the return address in a register.
1188    LABEL and SREG are passed to reg_save.  */
1189
1190 void
1191 dwarf2out_return_reg (const char *label, unsigned int sreg)
1192 {
1193   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1194 }
1195
1196 /* Record the initial position of the return address.  RTL is
1197    INCOMING_RETURN_ADDR_RTX.  */
1198
1199 static void
1200 initial_return_save (rtx rtl)
1201 {
1202   unsigned int reg = INVALID_REGNUM;
1203   HOST_WIDE_INT offset = 0;
1204
1205   switch (GET_CODE (rtl))
1206     {
1207     case REG:
1208       /* RA is in a register.  */
1209       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1210       break;
1211
1212     case MEM:
1213       /* RA is on the stack.  */
1214       rtl = XEXP (rtl, 0);
1215       switch (GET_CODE (rtl))
1216         {
1217         case REG:
1218           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1219           offset = 0;
1220           break;
1221
1222         case PLUS:
1223           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1224           offset = INTVAL (XEXP (rtl, 1));
1225           break;
1226
1227         case MINUS:
1228           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1229           offset = -INTVAL (XEXP (rtl, 1));
1230           break;
1231
1232         default:
1233           gcc_unreachable ();
1234         }
1235
1236       break;
1237
1238     case PLUS:
1239       /* The return address is at some offset from any value we can
1240          actually load.  For instance, on the SPARC it is in %i7+8. Just
1241          ignore the offset for now; it doesn't matter for unwinding frames.  */
1242       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1243       initial_return_save (XEXP (rtl, 0));
1244       return;
1245
1246     default:
1247       gcc_unreachable ();
1248     }
1249
1250   if (reg != DWARF_FRAME_RETURN_COLUMN)
1251     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1252 }
1253
1254 /* Given a SET, calculate the amount of stack adjustment it
1255    contains.  */
1256
1257 static HOST_WIDE_INT
1258 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1259                      HOST_WIDE_INT cur_offset)
1260 {
1261   const_rtx src = SET_SRC (pattern);
1262   const_rtx dest = SET_DEST (pattern);
1263   HOST_WIDE_INT offset = 0;
1264   enum rtx_code code;
1265
1266   if (dest == stack_pointer_rtx)
1267     {
1268       code = GET_CODE (src);
1269
1270       /* Assume (set (reg sp) (reg whatever)) sets args_size
1271          level to 0.  */
1272       if (code == REG && src != stack_pointer_rtx)
1273         {
1274           offset = -cur_args_size;
1275 #ifndef STACK_GROWS_DOWNWARD
1276           offset = -offset;
1277 #endif
1278           return offset - cur_offset;
1279         }
1280
1281       if (! (code == PLUS || code == MINUS)
1282           || XEXP (src, 0) != stack_pointer_rtx
1283           || !CONST_INT_P (XEXP (src, 1)))
1284         return 0;
1285
1286       /* (set (reg sp) (plus (reg sp) (const_int))) */
1287       offset = INTVAL (XEXP (src, 1));
1288       if (code == PLUS)
1289         offset = -offset;
1290       return offset;
1291     }
1292
1293   if (MEM_P (src) && !MEM_P (dest))
1294     dest = src;
1295   if (MEM_P (dest))
1296     {
1297       /* (set (mem (pre_dec (reg sp))) (foo)) */
1298       src = XEXP (dest, 0);
1299       code = GET_CODE (src);
1300
1301       switch (code)
1302         {
1303         case PRE_MODIFY:
1304         case POST_MODIFY:
1305           if (XEXP (src, 0) == stack_pointer_rtx)
1306             {
1307               rtx val = XEXP (XEXP (src, 1), 1);
1308               /* We handle only adjustments by constant amount.  */
1309               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1310                           && CONST_INT_P (val));
1311               offset = -INTVAL (val);
1312               break;
1313             }
1314           return 0;
1315
1316         case PRE_DEC:
1317         case POST_DEC:
1318           if (XEXP (src, 0) == stack_pointer_rtx)
1319             {
1320               offset = GET_MODE_SIZE (GET_MODE (dest));
1321               break;
1322             }
1323           return 0;
1324
1325         case PRE_INC:
1326         case POST_INC:
1327           if (XEXP (src, 0) == stack_pointer_rtx)
1328             {
1329               offset = -GET_MODE_SIZE (GET_MODE (dest));
1330               break;
1331             }
1332           return 0;
1333
1334         default:
1335           return 0;
1336         }
1337     }
1338   else
1339     return 0;
1340
1341   return offset;
1342 }
1343
1344 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1345    indexed by INSN_UID.  */
1346
1347 static HOST_WIDE_INT *barrier_args_size;
1348
1349 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1350
1351 static HOST_WIDE_INT
1352 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1353                              VEC (rtx, heap) **next)
1354 {
1355   HOST_WIDE_INT offset = 0;
1356   int i;
1357
1358   if (! RTX_FRAME_RELATED_P (insn))
1359     {
1360       if (prologue_epilogue_contains (insn))
1361         /* Nothing */;
1362       else if (GET_CODE (PATTERN (insn)) == SET)
1363         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1364       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1365                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1366         {
1367           /* There may be stack adjustments inside compound insns.  Search
1368              for them.  */
1369           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1370             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1371               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1372                                              cur_args_size, offset);
1373         }
1374     }
1375   else
1376     {
1377       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1378
1379       if (expr)
1380         {
1381           expr = XEXP (expr, 0);
1382           if (GET_CODE (expr) == PARALLEL
1383               || GET_CODE (expr) == SEQUENCE)
1384             for (i = 1; i < XVECLEN (expr, 0); i++)
1385               {
1386                 rtx elem = XVECEXP (expr, 0, i);
1387
1388                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1389                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1390               }
1391         }
1392     }
1393
1394 #ifndef STACK_GROWS_DOWNWARD
1395   offset = -offset;
1396 #endif
1397
1398   cur_args_size += offset;
1399   if (cur_args_size < 0)
1400     cur_args_size = 0;
1401
1402   if (JUMP_P (insn))
1403     {
1404       rtx dest = JUMP_LABEL (insn);
1405
1406       if (dest)
1407         {
1408           if (barrier_args_size [INSN_UID (dest)] < 0)
1409             {
1410               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1411               VEC_safe_push (rtx, heap, *next, dest);
1412             }
1413         }
1414     }
1415
1416   return cur_args_size;
1417 }
1418
1419 /* Walk the whole function and compute args_size on BARRIERs.  */
1420
1421 static void
1422 compute_barrier_args_size (void)
1423 {
1424   int max_uid = get_max_uid (), i;
1425   rtx insn;
1426   VEC (rtx, heap) *worklist, *next, *tmp;
1427
1428   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1429   for (i = 0; i < max_uid; i++)
1430     barrier_args_size[i] = -1;
1431
1432   worklist = VEC_alloc (rtx, heap, 20);
1433   next = VEC_alloc (rtx, heap, 20);
1434   insn = get_insns ();
1435   barrier_args_size[INSN_UID (insn)] = 0;
1436   VEC_quick_push (rtx, worklist, insn);
1437   for (;;)
1438     {
1439       while (!VEC_empty (rtx, worklist))
1440         {
1441           rtx prev, body, first_insn;
1442           HOST_WIDE_INT cur_args_size;
1443
1444           first_insn = insn = VEC_pop (rtx, worklist);
1445           cur_args_size = barrier_args_size[INSN_UID (insn)];
1446           prev = prev_nonnote_insn (insn);
1447           if (prev && BARRIER_P (prev))
1448             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1449
1450           for (; insn; insn = NEXT_INSN (insn))
1451             {
1452               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1453                 continue;
1454               if (BARRIER_P (insn))
1455                 break;
1456
1457               if (LABEL_P (insn))
1458                 {
1459                   if (insn == first_insn)
1460                     continue;
1461                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1462                     {
1463                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1464                       continue;
1465                     }
1466                   else
1467                     {
1468                       /* The insns starting with this label have been
1469                          already scanned or are in the worklist.  */
1470                       break;
1471                     }
1472                 }
1473
1474               body = PATTERN (insn);
1475               if (GET_CODE (body) == SEQUENCE)
1476                 {
1477                   HOST_WIDE_INT dest_args_size = cur_args_size;
1478                   for (i = 1; i < XVECLEN (body, 0); i++)
1479                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1480                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1481                       dest_args_size
1482                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1483                                                        dest_args_size, &next);
1484                     else
1485                       cur_args_size
1486                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1487                                                        cur_args_size, &next);
1488
1489                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1490                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1491                                                  dest_args_size, &next);
1492                   else
1493                     cur_args_size
1494                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1495                                                      cur_args_size, &next);
1496                 }
1497               else
1498                 cur_args_size
1499                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1500             }
1501         }
1502
1503       if (VEC_empty (rtx, next))
1504         break;
1505
1506       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1507       tmp = next;
1508       next = worklist;
1509       worklist = tmp;
1510       VEC_truncate (rtx, next, 0);
1511     }
1512
1513   VEC_free (rtx, heap, worklist);
1514   VEC_free (rtx, heap, next);
1515 }
1516
1517 /* Add a CFI to update the running total of the size of arguments
1518    pushed onto the stack.  */
1519
1520 static void
1521 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1522 {
1523   dw_cfi_ref cfi;
1524
1525   if (size == old_args_size)
1526     return;
1527
1528   old_args_size = size;
1529
1530   cfi = new_cfi ();
1531   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1532   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1533   add_fde_cfi (label, cfi);
1534 }
1535
1536 /* Record a stack adjustment of OFFSET bytes.  */
1537
1538 static void
1539 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1540 {
1541   if (cfa.reg == STACK_POINTER_REGNUM)
1542     cfa.offset += offset;
1543
1544   if (cfa_store.reg == STACK_POINTER_REGNUM)
1545     cfa_store.offset += offset;
1546
1547   if (ACCUMULATE_OUTGOING_ARGS)
1548     return;
1549
1550 #ifndef STACK_GROWS_DOWNWARD
1551   offset = -offset;
1552 #endif
1553
1554   args_size += offset;
1555   if (args_size < 0)
1556     args_size = 0;
1557
1558   def_cfa_1 (label, &cfa);
1559   if (flag_asynchronous_unwind_tables)
1560     dwarf2out_args_size (label, args_size);
1561 }
1562
1563 /* Check INSN to see if it looks like a push or a stack adjustment, and
1564    make a note of it if it does.  EH uses this information to find out
1565    how much extra space it needs to pop off the stack.  */
1566
1567 static void
1568 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1569 {
1570   HOST_WIDE_INT offset;
1571   const char *label;
1572   int i;
1573
1574   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1575      with this function.  Proper support would require all frame-related
1576      insns to be marked, and to be able to handle saving state around
1577      epilogues textually in the middle of the function.  */
1578   if (prologue_epilogue_contains (insn))
1579     return;
1580
1581   /* If INSN is an instruction from target of an annulled branch, the
1582      effects are for the target only and so current argument size
1583      shouldn't change at all.  */
1584   if (final_sequence
1585       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1586       && INSN_FROM_TARGET_P (insn))
1587     return;
1588
1589   /* If only calls can throw, and we have a frame pointer,
1590      save up adjustments until we see the CALL_INSN.  */
1591   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1592     {
1593       if (CALL_P (insn) && !after_p)
1594         {
1595           /* Extract the size of the args from the CALL rtx itself.  */
1596           insn = PATTERN (insn);
1597           if (GET_CODE (insn) == PARALLEL)
1598             insn = XVECEXP (insn, 0, 0);
1599           if (GET_CODE (insn) == SET)
1600             insn = SET_SRC (insn);
1601           gcc_assert (GET_CODE (insn) == CALL);
1602           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1603         }
1604       return;
1605     }
1606
1607   if (CALL_P (insn) && !after_p)
1608     {
1609       if (!flag_asynchronous_unwind_tables)
1610         dwarf2out_args_size ("", args_size);
1611       return;
1612     }
1613   else if (BARRIER_P (insn))
1614     {
1615       /* Don't call compute_barrier_args_size () if the only
1616          BARRIER is at the end of function.  */
1617       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1618         compute_barrier_args_size ();
1619       if (barrier_args_size == NULL)
1620         offset = 0;
1621       else
1622         {
1623           offset = barrier_args_size[INSN_UID (insn)];
1624           if (offset < 0)
1625             offset = 0;
1626         }
1627
1628       offset -= args_size;
1629 #ifndef STACK_GROWS_DOWNWARD
1630       offset = -offset;
1631 #endif
1632     }
1633   else if (GET_CODE (PATTERN (insn)) == SET)
1634     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1635   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1636            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1637     {
1638       /* There may be stack adjustments inside compound insns.  Search
1639          for them.  */
1640       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1641         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1642           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1643                                          args_size, offset);
1644     }
1645   else
1646     return;
1647
1648   if (offset == 0)
1649     return;
1650
1651   label = dwarf2out_cfi_label (false);
1652   dwarf2out_stack_adjust (offset, label);
1653 }
1654
1655 /* We delay emitting a register save until either (a) we reach the end
1656    of the prologue or (b) the register is clobbered.  This clusters
1657    register saves so that there are fewer pc advances.  */
1658
1659 struct GTY(()) queued_reg_save {
1660   struct queued_reg_save *next;
1661   rtx reg;
1662   HOST_WIDE_INT cfa_offset;
1663   rtx saved_reg;
1664 };
1665
1666 static GTY(()) struct queued_reg_save *queued_reg_saves;
1667
1668 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1669 struct GTY(()) reg_saved_in_data {
1670   rtx orig_reg;
1671   rtx saved_in_reg;
1672 };
1673
1674 /* A list of registers saved in other registers.
1675    The list intentionally has a small maximum capacity of 4; if your
1676    port needs more than that, you might consider implementing a
1677    more efficient data structure.  */
1678 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1679 static GTY(()) size_t num_regs_saved_in_regs;
1680
1681 static const char *last_reg_save_label;
1682
1683 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1684    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1685
1686 static void
1687 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1688 {
1689   struct queued_reg_save *q;
1690
1691   /* Duplicates waste space, but it's also necessary to remove them
1692      for correctness, since the queue gets output in reverse
1693      order.  */
1694   for (q = queued_reg_saves; q != NULL; q = q->next)
1695     if (REGNO (q->reg) == REGNO (reg))
1696       break;
1697
1698   if (q == NULL)
1699     {
1700       q = ggc_alloc_queued_reg_save ();
1701       q->next = queued_reg_saves;
1702       queued_reg_saves = q;
1703     }
1704
1705   q->reg = reg;
1706   q->cfa_offset = offset;
1707   q->saved_reg = sreg;
1708
1709   last_reg_save_label = label;
1710 }
1711
1712 /* Output all the entries in QUEUED_REG_SAVES.  */
1713
1714 void
1715 dwarf2out_flush_queued_reg_saves (void)
1716 {
1717   struct queued_reg_save *q;
1718
1719   for (q = queued_reg_saves; q; q = q->next)
1720     {
1721       size_t i;
1722       unsigned int reg, sreg;
1723
1724       for (i = 0; i < num_regs_saved_in_regs; i++)
1725         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1726           break;
1727       if (q->saved_reg && i == num_regs_saved_in_regs)
1728         {
1729           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1730           num_regs_saved_in_regs++;
1731         }
1732       if (i != num_regs_saved_in_regs)
1733         {
1734           regs_saved_in_regs[i].orig_reg = q->reg;
1735           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1736         }
1737
1738       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1739       if (q->saved_reg)
1740         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1741       else
1742         sreg = INVALID_REGNUM;
1743       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1744     }
1745
1746   queued_reg_saves = NULL;
1747   last_reg_save_label = NULL;
1748 }
1749
1750 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1751    location for?  Or, does it clobber a register which we've previously
1752    said that some other register is saved in, and for which we now
1753    have a new location for?  */
1754
1755 static bool
1756 clobbers_queued_reg_save (const_rtx insn)
1757 {
1758   struct queued_reg_save *q;
1759
1760   for (q = queued_reg_saves; q; q = q->next)
1761     {
1762       size_t i;
1763       if (modified_in_p (q->reg, insn))
1764         return true;
1765       for (i = 0; i < num_regs_saved_in_regs; i++)
1766         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1767             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1768           return true;
1769     }
1770
1771   return false;
1772 }
1773
1774 /* Entry point for saving the first register into the second.  */
1775
1776 void
1777 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1778 {
1779   size_t i;
1780   unsigned int regno, sregno;
1781
1782   for (i = 0; i < num_regs_saved_in_regs; i++)
1783     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1784       break;
1785   if (i == num_regs_saved_in_regs)
1786     {
1787       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1788       num_regs_saved_in_regs++;
1789     }
1790   regs_saved_in_regs[i].orig_reg = reg;
1791   regs_saved_in_regs[i].saved_in_reg = sreg;
1792
1793   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1794   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1795   reg_save (label, regno, sregno, 0);
1796 }
1797
1798 /* What register, if any, is currently saved in REG?  */
1799
1800 static rtx
1801 reg_saved_in (rtx reg)
1802 {
1803   unsigned int regn = REGNO (reg);
1804   size_t i;
1805   struct queued_reg_save *q;
1806
1807   for (q = queued_reg_saves; q; q = q->next)
1808     if (q->saved_reg && regn == REGNO (q->saved_reg))
1809       return q->reg;
1810
1811   for (i = 0; i < num_regs_saved_in_regs; i++)
1812     if (regs_saved_in_regs[i].saved_in_reg
1813         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1814       return regs_saved_in_regs[i].orig_reg;
1815
1816   return NULL_RTX;
1817 }
1818
1819
1820 /* A temporary register holding an integral value used in adjusting SP
1821    or setting up the store_reg.  The "offset" field holds the integer
1822    value, not an offset.  */
1823 static dw_cfa_location cfa_temp;
1824
1825 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1826
1827 static void
1828 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1829 {
1830   memset (&cfa, 0, sizeof (cfa));
1831
1832   switch (GET_CODE (pat))
1833     {
1834     case PLUS:
1835       cfa.reg = REGNO (XEXP (pat, 0));
1836       cfa.offset = INTVAL (XEXP (pat, 1));
1837       break;
1838
1839     case REG:
1840       cfa.reg = REGNO (pat);
1841       break;
1842
1843     case MEM:
1844       cfa.indirect = 1;
1845       pat = XEXP (pat, 0);
1846       if (GET_CODE (pat) == PLUS)
1847         {
1848           cfa.base_offset = INTVAL (XEXP (pat, 1));
1849           pat = XEXP (pat, 0);
1850         }
1851       cfa.reg = REGNO (pat);
1852       break;
1853
1854     default:
1855       /* Recurse and define an expression.  */
1856       gcc_unreachable ();
1857     }
1858
1859   def_cfa_1 (label, &cfa);
1860 }
1861
1862 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1863
1864 static void
1865 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1866 {
1867   rtx src, dest;
1868
1869   gcc_assert (GET_CODE (pat) == SET);
1870   dest = XEXP (pat, 0);
1871   src = XEXP (pat, 1);
1872
1873   switch (GET_CODE (src))
1874     {
1875     case PLUS:
1876       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1877       cfa.offset -= INTVAL (XEXP (src, 1));
1878       break;
1879
1880     case REG:
1881         break;
1882
1883     default:
1884         gcc_unreachable ();
1885     }
1886
1887   cfa.reg = REGNO (dest);
1888   gcc_assert (cfa.indirect == 0);
1889
1890   def_cfa_1 (label, &cfa);
1891 }
1892
1893 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1894
1895 static void
1896 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1897 {
1898   HOST_WIDE_INT offset;
1899   rtx src, addr, span;
1900
1901   src = XEXP (set, 1);
1902   addr = XEXP (set, 0);
1903   gcc_assert (MEM_P (addr));
1904   addr = XEXP (addr, 0);
1905
1906   /* As documented, only consider extremely simple addresses.  */
1907   switch (GET_CODE (addr))
1908     {
1909     case REG:
1910       gcc_assert (REGNO (addr) == cfa.reg);
1911       offset = -cfa.offset;
1912       break;
1913     case PLUS:
1914       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1915       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1916       break;
1917     default:
1918       gcc_unreachable ();
1919     }
1920
1921   span = targetm.dwarf_register_span (src);
1922
1923   /* ??? We'd like to use queue_reg_save, but we need to come up with
1924      a different flushing heuristic for epilogues.  */
1925   if (!span)
1926     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1927   else
1928     {
1929       /* We have a PARALLEL describing where the contents of SRC live.
1930          Queue register saves for each piece of the PARALLEL.  */
1931       int par_index;
1932       int limit;
1933       HOST_WIDE_INT span_offset = offset;
1934
1935       gcc_assert (GET_CODE (span) == PARALLEL);
1936
1937       limit = XVECLEN (span, 0);
1938       for (par_index = 0; par_index < limit; par_index++)
1939         {
1940           rtx elem = XVECEXP (span, 0, par_index);
1941
1942           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1943                     INVALID_REGNUM, span_offset);
1944           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1945         }
1946     }
1947 }
1948
1949 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1950
1951 static void
1952 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1953 {
1954   rtx src, dest;
1955   unsigned sregno, dregno;
1956
1957   src = XEXP (set, 1);
1958   dest = XEXP (set, 0);
1959
1960   if (src == pc_rtx)
1961     sregno = DWARF_FRAME_RETURN_COLUMN;
1962   else
1963     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1964
1965   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1966
1967   /* ??? We'd like to use queue_reg_save, but we need to come up with
1968      a different flushing heuristic for epilogues.  */
1969   reg_save (label, sregno, dregno, 0);
1970 }
1971
1972 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
1973
1974 static void
1975 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
1976 {
1977   rtx src, dest, span;
1978   dw_cfi_ref cfi = new_cfi ();
1979
1980   dest = SET_DEST (set);
1981   src = SET_SRC (set);
1982
1983   gcc_assert (REG_P (src));
1984   gcc_assert (MEM_P (dest));
1985
1986   span = targetm.dwarf_register_span (src);
1987   gcc_assert (!span);
1988
1989   cfi->dw_cfi_opc = DW_CFA_expression;
1990   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
1991   cfi->dw_cfi_oprnd2.dw_cfi_loc
1992     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
1993                           VAR_INIT_STATUS_INITIALIZED);
1994
1995   /* ??? We'd like to use queue_reg_save, were the interface different,
1996      and, as above, we could manage flushing for epilogues.  */
1997   add_fde_cfi (label, cfi);
1998 }
1999
2000 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2001
2002 static void
2003 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2004 {
2005   dw_cfi_ref cfi = new_cfi ();
2006   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2007
2008   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2009   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2010
2011   add_fde_cfi (label, cfi);
2012 }
2013
2014 /* Record call frame debugging information for an expression EXPR,
2015    which either sets SP or FP (adjusting how we calculate the frame
2016    address) or saves a register to the stack or another register.
2017    LABEL indicates the address of EXPR.
2018
2019    This function encodes a state machine mapping rtxes to actions on
2020    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2021    users need not read the source code.
2022
2023   The High-Level Picture
2024
2025   Changes in the register we use to calculate the CFA: Currently we
2026   assume that if you copy the CFA register into another register, we
2027   should take the other one as the new CFA register; this seems to
2028   work pretty well.  If it's wrong for some target, it's simple
2029   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2030
2031   Changes in the register we use for saving registers to the stack:
2032   This is usually SP, but not always.  Again, we deduce that if you
2033   copy SP into another register (and SP is not the CFA register),
2034   then the new register is the one we will be using for register
2035   saves.  This also seems to work.
2036
2037   Register saves: There's not much guesswork about this one; if
2038   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2039   register save, and the register used to calculate the destination
2040   had better be the one we think we're using for this purpose.
2041   It's also assumed that a copy from a call-saved register to another
2042   register is saving that register if RTX_FRAME_RELATED_P is set on
2043   that instruction.  If the copy is from a call-saved register to
2044   the *same* register, that means that the register is now the same
2045   value as in the caller.
2046
2047   Except: If the register being saved is the CFA register, and the
2048   offset is nonzero, we are saving the CFA, so we assume we have to
2049   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2050   the intent is to save the value of SP from the previous frame.
2051
2052   In addition, if a register has previously been saved to a different
2053   register,
2054
2055   Invariants / Summaries of Rules
2056
2057   cfa          current rule for calculating the CFA.  It usually
2058                consists of a register and an offset.
2059   cfa_store    register used by prologue code to save things to the stack
2060                cfa_store.offset is the offset from the value of
2061                cfa_store.reg to the actual CFA
2062   cfa_temp     register holding an integral value.  cfa_temp.offset
2063                stores the value, which will be used to adjust the
2064                stack pointer.  cfa_temp is also used like cfa_store,
2065                to track stores to the stack via fp or a temp reg.
2066
2067   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2068                with cfa.reg as the first operand changes the cfa.reg and its
2069                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2070                cfa_temp.offset.
2071
2072   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2073                expression yielding a constant.  This sets cfa_temp.reg
2074                and cfa_temp.offset.
2075
2076   Rule 5:      Create a new register cfa_store used to save items to the
2077                stack.
2078
2079   Rules 10-14: Save a register to the stack.  Define offset as the
2080                difference of the original location and cfa_store's
2081                location (or cfa_temp's location if cfa_temp is used).
2082
2083   Rules 16-20: If AND operation happens on sp in prologue, we assume
2084                stack is realigned.  We will use a group of DW_OP_XXX
2085                expressions to represent the location of the stored
2086                register instead of CFA+offset.
2087
2088   The Rules
2089
2090   "{a,b}" indicates a choice of a xor b.
2091   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2092
2093   Rule 1:
2094   (set <reg1> <reg2>:cfa.reg)
2095   effects: cfa.reg = <reg1>
2096            cfa.offset unchanged
2097            cfa_temp.reg = <reg1>
2098            cfa_temp.offset = cfa.offset
2099
2100   Rule 2:
2101   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2102                               {<const_int>,<reg>:cfa_temp.reg}))
2103   effects: cfa.reg = sp if fp used
2104            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2105            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2106              if cfa_store.reg==sp
2107
2108   Rule 3:
2109   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2110   effects: cfa.reg = fp
2111            cfa_offset += +/- <const_int>
2112
2113   Rule 4:
2114   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2115   constraints: <reg1> != fp
2116                <reg1> != sp
2117   effects: cfa.reg = <reg1>
2118            cfa_temp.reg = <reg1>
2119            cfa_temp.offset = cfa.offset
2120
2121   Rule 5:
2122   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2123   constraints: <reg1> != fp
2124                <reg1> != sp
2125   effects: cfa_store.reg = <reg1>
2126            cfa_store.offset = cfa.offset - cfa_temp.offset
2127
2128   Rule 6:
2129   (set <reg> <const_int>)
2130   effects: cfa_temp.reg = <reg>
2131            cfa_temp.offset = <const_int>
2132
2133   Rule 7:
2134   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2135   effects: cfa_temp.reg = <reg1>
2136            cfa_temp.offset |= <const_int>
2137
2138   Rule 8:
2139   (set <reg> (high <exp>))
2140   effects: none
2141
2142   Rule 9:
2143   (set <reg> (lo_sum <exp> <const_int>))
2144   effects: cfa_temp.reg = <reg>
2145            cfa_temp.offset = <const_int>
2146
2147   Rule 10:
2148   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2149   effects: cfa_store.offset -= <const_int>
2150            cfa.offset = cfa_store.offset if cfa.reg == sp
2151            cfa.reg = sp
2152            cfa.base_offset = -cfa_store.offset
2153
2154   Rule 11:
2155   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2156   effects: cfa_store.offset += -/+ mode_size(mem)
2157            cfa.offset = cfa_store.offset if cfa.reg == sp
2158            cfa.reg = sp
2159            cfa.base_offset = -cfa_store.offset
2160
2161   Rule 12:
2162   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2163
2164        <reg2>)
2165   effects: cfa.reg = <reg1>
2166            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2167
2168   Rule 13:
2169   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2170   effects: cfa.reg = <reg1>
2171            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2172
2173   Rule 14:
2174   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2175   effects: cfa.reg = <reg1>
2176            cfa.base_offset = -cfa_temp.offset
2177            cfa_temp.offset -= mode_size(mem)
2178
2179   Rule 15:
2180   (set <reg> {unspec, unspec_volatile})
2181   effects: target-dependent
2182
2183   Rule 16:
2184   (set sp (and: sp <const_int>))
2185   constraints: cfa_store.reg == sp
2186   effects: current_fde.stack_realign = 1
2187            cfa_store.offset = 0
2188            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2189
2190   Rule 17:
2191   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2192   effects: cfa_store.offset += -/+ mode_size(mem)
2193
2194   Rule 18:
2195   (set (mem ({pre_inc, pre_dec} sp)) fp)
2196   constraints: fde->stack_realign == 1
2197   effects: cfa_store.offset = 0
2198            cfa.reg != HARD_FRAME_POINTER_REGNUM
2199
2200   Rule 19:
2201   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2202   constraints: fde->stack_realign == 1
2203                && cfa.offset == 0
2204                && cfa.indirect == 0
2205                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2206   effects: Use DW_CFA_def_cfa_expression to define cfa
2207            cfa.reg == fde->drap_reg  */
2208
2209 static void
2210 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2211 {
2212   rtx src, dest, span;
2213   HOST_WIDE_INT offset;
2214   dw_fde_ref fde;
2215
2216   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2217      the PARALLEL independently. The first element is always processed if
2218      it is a SET. This is for backward compatibility.   Other elements
2219      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2220      flag is set in them.  */
2221   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2222     {
2223       int par_index;
2224       int limit = XVECLEN (expr, 0);
2225       rtx elem;
2226
2227       /* PARALLELs have strict read-modify-write semantics, so we
2228          ought to evaluate every rvalue before changing any lvalue.
2229          It's cumbersome to do that in general, but there's an
2230          easy approximation that is enough for all current users:
2231          handle register saves before register assignments.  */
2232       if (GET_CODE (expr) == PARALLEL)
2233         for (par_index = 0; par_index < limit; par_index++)
2234           {
2235             elem = XVECEXP (expr, 0, par_index);
2236             if (GET_CODE (elem) == SET
2237                 && MEM_P (SET_DEST (elem))
2238                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2239               dwarf2out_frame_debug_expr (elem, label);
2240           }
2241
2242       for (par_index = 0; par_index < limit; par_index++)
2243         {
2244           elem = XVECEXP (expr, 0, par_index);
2245           if (GET_CODE (elem) == SET
2246               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2247               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2248             dwarf2out_frame_debug_expr (elem, label);
2249           else if (GET_CODE (elem) == SET
2250                    && par_index != 0
2251                    && !RTX_FRAME_RELATED_P (elem))
2252             {
2253               /* Stack adjustment combining might combine some post-prologue
2254                  stack adjustment into a prologue stack adjustment.  */
2255               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2256
2257               if (offset != 0)
2258                 dwarf2out_stack_adjust (offset, label);
2259             }
2260         }
2261       return;
2262     }
2263
2264   gcc_assert (GET_CODE (expr) == SET);
2265
2266   src = SET_SRC (expr);
2267   dest = SET_DEST (expr);
2268
2269   if (REG_P (src))
2270     {
2271       rtx rsi = reg_saved_in (src);
2272       if (rsi)
2273         src = rsi;
2274     }
2275
2276   fde = current_fde ();
2277
2278   switch (GET_CODE (dest))
2279     {
2280     case REG:
2281       switch (GET_CODE (src))
2282         {
2283           /* Setting FP from SP.  */
2284         case REG:
2285           if (cfa.reg == (unsigned) REGNO (src))
2286             {
2287               /* Rule 1 */
2288               /* Update the CFA rule wrt SP or FP.  Make sure src is
2289                  relative to the current CFA register.
2290
2291                  We used to require that dest be either SP or FP, but the
2292                  ARM copies SP to a temporary register, and from there to
2293                  FP.  So we just rely on the backends to only set
2294                  RTX_FRAME_RELATED_P on appropriate insns.  */
2295               cfa.reg = REGNO (dest);
2296               cfa_temp.reg = cfa.reg;
2297               cfa_temp.offset = cfa.offset;
2298             }
2299           else
2300             {
2301               /* Saving a register in a register.  */
2302               gcc_assert (!fixed_regs [REGNO (dest)]
2303                           /* For the SPARC and its register window.  */
2304                           || (DWARF_FRAME_REGNUM (REGNO (src))
2305                               == DWARF_FRAME_RETURN_COLUMN));
2306
2307               /* After stack is aligned, we can only save SP in FP
2308                  if drap register is used.  In this case, we have
2309                  to restore stack pointer with the CFA value and we
2310                  don't generate this DWARF information.  */
2311               if (fde
2312                   && fde->stack_realign
2313                   && REGNO (src) == STACK_POINTER_REGNUM)
2314                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2315                             && fde->drap_reg != INVALID_REGNUM
2316                             && cfa.reg != REGNO (src));
2317               else
2318                 queue_reg_save (label, src, dest, 0);
2319             }
2320           break;
2321
2322         case PLUS:
2323         case MINUS:
2324         case LO_SUM:
2325           if (dest == stack_pointer_rtx)
2326             {
2327               /* Rule 2 */
2328               /* Adjusting SP.  */
2329               switch (GET_CODE (XEXP (src, 1)))
2330                 {
2331                 case CONST_INT:
2332                   offset = INTVAL (XEXP (src, 1));
2333                   break;
2334                 case REG:
2335                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2336                               == cfa_temp.reg);
2337                   offset = cfa_temp.offset;
2338                   break;
2339                 default:
2340                   gcc_unreachable ();
2341                 }
2342
2343               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2344                 {
2345                   /* Restoring SP from FP in the epilogue.  */
2346                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2347                   cfa.reg = STACK_POINTER_REGNUM;
2348                 }
2349               else if (GET_CODE (src) == LO_SUM)
2350                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2351                 ;
2352               else
2353                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2354
2355               if (GET_CODE (src) != MINUS)
2356                 offset = -offset;
2357               if (cfa.reg == STACK_POINTER_REGNUM)
2358                 cfa.offset += offset;
2359               if (cfa_store.reg == STACK_POINTER_REGNUM)
2360                 cfa_store.offset += offset;
2361             }
2362           else if (dest == hard_frame_pointer_rtx)
2363             {
2364               /* Rule 3 */
2365               /* Either setting the FP from an offset of the SP,
2366                  or adjusting the FP */
2367               gcc_assert (frame_pointer_needed);
2368
2369               gcc_assert (REG_P (XEXP (src, 0))
2370                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2371                           && CONST_INT_P (XEXP (src, 1)));
2372               offset = INTVAL (XEXP (src, 1));
2373               if (GET_CODE (src) != MINUS)
2374                 offset = -offset;
2375               cfa.offset += offset;
2376               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2377             }
2378           else
2379             {
2380               gcc_assert (GET_CODE (src) != MINUS);
2381
2382               /* Rule 4 */
2383               if (REG_P (XEXP (src, 0))
2384                   && REGNO (XEXP (src, 0)) == cfa.reg
2385                   && CONST_INT_P (XEXP (src, 1)))
2386                 {
2387                   /* Setting a temporary CFA register that will be copied
2388                      into the FP later on.  */
2389                   offset = - INTVAL (XEXP (src, 1));
2390                   cfa.offset += offset;
2391                   cfa.reg = REGNO (dest);
2392                   /* Or used to save regs to the stack.  */
2393                   cfa_temp.reg = cfa.reg;
2394                   cfa_temp.offset = cfa.offset;
2395                 }
2396
2397               /* Rule 5 */
2398               else if (REG_P (XEXP (src, 0))
2399                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2400                        && XEXP (src, 1) == stack_pointer_rtx)
2401                 {
2402                   /* Setting a scratch register that we will use instead
2403                      of SP for saving registers to the stack.  */
2404                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2405                   cfa_store.reg = REGNO (dest);
2406                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2407                 }
2408
2409               /* Rule 9 */
2410               else if (GET_CODE (src) == LO_SUM
2411                        && CONST_INT_P (XEXP (src, 1)))
2412                 {
2413                   cfa_temp.reg = REGNO (dest);
2414                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2415                 }
2416               else
2417                 gcc_unreachable ();
2418             }
2419           break;
2420
2421           /* Rule 6 */
2422         case CONST_INT:
2423           cfa_temp.reg = REGNO (dest);
2424           cfa_temp.offset = INTVAL (src);
2425           break;
2426
2427           /* Rule 7 */
2428         case IOR:
2429           gcc_assert (REG_P (XEXP (src, 0))
2430                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2431                       && CONST_INT_P (XEXP (src, 1)));
2432
2433           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2434             cfa_temp.reg = REGNO (dest);
2435           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2436           break;
2437
2438           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2439              which will fill in all of the bits.  */
2440           /* Rule 8 */
2441         case HIGH:
2442           break;
2443
2444           /* Rule 15 */
2445         case UNSPEC:
2446         case UNSPEC_VOLATILE:
2447           gcc_assert (targetm.dwarf_handle_frame_unspec);
2448           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2449           return;
2450
2451           /* Rule 16 */
2452         case AND:
2453           /* If this AND operation happens on stack pointer in prologue,
2454              we assume the stack is realigned and we extract the
2455              alignment.  */
2456           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2457             {
2458               /* We interpret reg_save differently with stack_realign set.
2459                  Thus we must flush whatever we have queued first.  */
2460               dwarf2out_flush_queued_reg_saves ();
2461
2462               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2463               fde->stack_realign = 1;
2464               fde->stack_realignment = INTVAL (XEXP (src, 1));
2465               cfa_store.offset = 0;
2466
2467               if (cfa.reg != STACK_POINTER_REGNUM
2468                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2469                 fde->drap_reg = cfa.reg;
2470             }
2471           return;
2472
2473         default:
2474           gcc_unreachable ();
2475         }
2476
2477       def_cfa_1 (label, &cfa);
2478       break;
2479
2480     case MEM:
2481
2482       /* Saving a register to the stack.  Make sure dest is relative to the
2483          CFA register.  */
2484       switch (GET_CODE (XEXP (dest, 0)))
2485         {
2486           /* Rule 10 */
2487           /* With a push.  */
2488         case PRE_MODIFY:
2489           /* We can't handle variable size modifications.  */
2490           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2491                       == CONST_INT);
2492           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2493
2494           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2495                       && cfa_store.reg == STACK_POINTER_REGNUM);
2496
2497           cfa_store.offset += offset;
2498           if (cfa.reg == STACK_POINTER_REGNUM)
2499             cfa.offset = cfa_store.offset;
2500
2501           offset = -cfa_store.offset;
2502           break;
2503
2504           /* Rule 11 */
2505         case PRE_INC:
2506         case PRE_DEC:
2507           offset = GET_MODE_SIZE (GET_MODE (dest));
2508           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2509             offset = -offset;
2510
2511           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2512                        == STACK_POINTER_REGNUM)
2513                       && cfa_store.reg == STACK_POINTER_REGNUM);
2514
2515           cfa_store.offset += offset;
2516
2517           /* Rule 18: If stack is aligned, we will use FP as a
2518              reference to represent the address of the stored
2519              regiser.  */
2520           if (fde
2521               && fde->stack_realign
2522               && src == hard_frame_pointer_rtx)
2523             {
2524               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2525               cfa_store.offset = 0;
2526             }
2527
2528           if (cfa.reg == STACK_POINTER_REGNUM)
2529             cfa.offset = cfa_store.offset;
2530
2531           offset = -cfa_store.offset;
2532           break;
2533
2534           /* Rule 12 */
2535           /* With an offset.  */
2536         case PLUS:
2537         case MINUS:
2538         case LO_SUM:
2539           {
2540             int regno;
2541
2542             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2543                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2544             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2545             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2546               offset = -offset;
2547
2548             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2549
2550             if (cfa_store.reg == (unsigned) regno)
2551               offset -= cfa_store.offset;
2552             else
2553               {
2554                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2555                 offset -= cfa_temp.offset;
2556               }
2557           }
2558           break;
2559
2560           /* Rule 13 */
2561           /* Without an offset.  */
2562         case REG:
2563           {
2564             int regno = REGNO (XEXP (dest, 0));
2565
2566             if (cfa_store.reg == (unsigned) regno)
2567               offset = -cfa_store.offset;
2568             else
2569               {
2570                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2571                 offset = -cfa_temp.offset;
2572               }
2573           }
2574           break;
2575
2576           /* Rule 14 */
2577         case POST_INC:
2578           gcc_assert (cfa_temp.reg
2579                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2580           offset = -cfa_temp.offset;
2581           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2582           break;
2583
2584         default:
2585           gcc_unreachable ();
2586         }
2587
2588         /* Rule 17 */
2589         /* If the source operand of this MEM operation is not a
2590            register, basically the source is return address.  Here
2591            we only care how much stack grew and we don't save it.  */
2592       if (!REG_P (src))
2593         break;
2594
2595       if (REGNO (src) != STACK_POINTER_REGNUM
2596           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2597           && (unsigned) REGNO (src) == cfa.reg)
2598         {
2599           /* We're storing the current CFA reg into the stack.  */
2600
2601           if (cfa.offset == 0)
2602             {
2603               /* Rule 19 */
2604               /* If stack is aligned, putting CFA reg into stack means
2605                  we can no longer use reg + offset to represent CFA.
2606                  Here we use DW_CFA_def_cfa_expression instead.  The
2607                  result of this expression equals to the original CFA
2608                  value.  */
2609               if (fde
2610                   && fde->stack_realign
2611                   && cfa.indirect == 0
2612                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2613                 {
2614                   dw_cfa_location cfa_exp;
2615
2616                   gcc_assert (fde->drap_reg == cfa.reg);
2617
2618                   cfa_exp.indirect = 1;
2619                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2620                   cfa_exp.base_offset = offset;
2621                   cfa_exp.offset = 0;
2622
2623                   fde->drap_reg_saved = 1;
2624
2625                   def_cfa_1 (label, &cfa_exp);
2626                   break;
2627                 }
2628
2629               /* If the source register is exactly the CFA, assume
2630                  we're saving SP like any other register; this happens
2631                  on the ARM.  */
2632               def_cfa_1 (label, &cfa);
2633               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2634               break;
2635             }
2636           else
2637             {
2638               /* Otherwise, we'll need to look in the stack to
2639                  calculate the CFA.  */
2640               rtx x = XEXP (dest, 0);
2641
2642               if (!REG_P (x))
2643                 x = XEXP (x, 0);
2644               gcc_assert (REG_P (x));
2645
2646               cfa.reg = REGNO (x);
2647               cfa.base_offset = offset;
2648               cfa.indirect = 1;
2649               def_cfa_1 (label, &cfa);
2650               break;
2651             }
2652         }
2653
2654       def_cfa_1 (label, &cfa);
2655       {
2656         span = targetm.dwarf_register_span (src);
2657
2658         if (!span)
2659           queue_reg_save (label, src, NULL_RTX, offset);
2660         else
2661           {
2662             /* We have a PARALLEL describing where the contents of SRC
2663                live.  Queue register saves for each piece of the
2664                PARALLEL.  */
2665             int par_index;
2666             int limit;
2667             HOST_WIDE_INT span_offset = offset;
2668
2669             gcc_assert (GET_CODE (span) == PARALLEL);
2670
2671             limit = XVECLEN (span, 0);
2672             for (par_index = 0; par_index < limit; par_index++)
2673               {
2674                 rtx elem = XVECEXP (span, 0, par_index);
2675
2676                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2677                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2678               }
2679           }
2680       }
2681       break;
2682
2683     default:
2684       gcc_unreachable ();
2685     }
2686 }
2687
2688 /* Record call frame debugging information for INSN, which either
2689    sets SP or FP (adjusting how we calculate the frame address) or saves a
2690    register to the stack.  If INSN is NULL_RTX, initialize our state.
2691
2692    If AFTER_P is false, we're being called before the insn is emitted,
2693    otherwise after.  Call instructions get invoked twice.  */
2694
2695 void
2696 dwarf2out_frame_debug (rtx insn, bool after_p)
2697 {
2698   const char *label;
2699   rtx note, n;
2700   bool handled_one = false;
2701
2702   if (insn == NULL_RTX)
2703     {
2704       size_t i;
2705
2706       /* Flush any queued register saves.  */
2707       dwarf2out_flush_queued_reg_saves ();
2708
2709       /* Set up state for generating call frame debug info.  */
2710       lookup_cfa (&cfa);
2711       gcc_assert (cfa.reg
2712                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2713
2714       cfa.reg = STACK_POINTER_REGNUM;
2715       cfa_store = cfa;
2716       cfa_temp.reg = -1;
2717       cfa_temp.offset = 0;
2718
2719       for (i = 0; i < num_regs_saved_in_regs; i++)
2720         {
2721           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2722           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2723         }
2724       num_regs_saved_in_regs = 0;
2725
2726       if (barrier_args_size)
2727         {
2728           XDELETEVEC (barrier_args_size);
2729           barrier_args_size = NULL;
2730         }
2731       return;
2732     }
2733
2734   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2735     dwarf2out_flush_queued_reg_saves ();
2736
2737   if (!RTX_FRAME_RELATED_P (insn))
2738     {
2739       /* ??? This should be done unconditionally since stack adjustments
2740          matter if the stack pointer is not the CFA register anymore but
2741          is still used to save registers.  */
2742       if (!ACCUMULATE_OUTGOING_ARGS)
2743         dwarf2out_notice_stack_adjust (insn, after_p);
2744       return;
2745     }
2746
2747   label = dwarf2out_cfi_label (false);
2748
2749   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2750     switch (REG_NOTE_KIND (note))
2751       {
2752       case REG_FRAME_RELATED_EXPR:
2753         insn = XEXP (note, 0);
2754         goto found;
2755
2756       case REG_CFA_DEF_CFA:
2757         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2758         handled_one = true;
2759         break;
2760
2761       case REG_CFA_ADJUST_CFA:
2762         n = XEXP (note, 0);
2763         if (n == NULL)
2764           {
2765             n = PATTERN (insn);
2766             if (GET_CODE (n) == PARALLEL)
2767               n = XVECEXP (n, 0, 0);
2768           }
2769         dwarf2out_frame_debug_adjust_cfa (n, label);
2770         handled_one = true;
2771         break;
2772
2773       case REG_CFA_OFFSET:
2774         n = XEXP (note, 0);
2775         if (n == NULL)
2776           n = single_set (insn);
2777         dwarf2out_frame_debug_cfa_offset (n, label);
2778         handled_one = true;
2779         break;
2780
2781       case REG_CFA_REGISTER:
2782         n = XEXP (note, 0);
2783         if (n == NULL)
2784           {
2785             n = PATTERN (insn);
2786             if (GET_CODE (n) == PARALLEL)
2787               n = XVECEXP (n, 0, 0);
2788           }
2789         dwarf2out_frame_debug_cfa_register (n, label);
2790         handled_one = true;
2791         break;
2792
2793       case REG_CFA_EXPRESSION:
2794         n = XEXP (note, 0);
2795         if (n == NULL)
2796           n = single_set (insn);
2797         dwarf2out_frame_debug_cfa_expression (n, label);
2798         handled_one = true;
2799         break;
2800
2801       case REG_CFA_RESTORE:
2802         n = XEXP (note, 0);
2803         if (n == NULL)
2804           {
2805             n = PATTERN (insn);
2806             if (GET_CODE (n) == PARALLEL)
2807               n = XVECEXP (n, 0, 0);
2808             n = XEXP (n, 0);
2809           }
2810         dwarf2out_frame_debug_cfa_restore (n, label);
2811         handled_one = true;
2812         break;
2813
2814       case REG_CFA_SET_VDRAP:
2815         n = XEXP (note, 0);
2816         if (REG_P (n))
2817           {
2818             dw_fde_ref fde = current_fde ();
2819             if (fde)
2820               {
2821                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2822                 if (REG_P (n))
2823                   fde->vdrap_reg = REGNO (n);
2824               }
2825           }
2826         handled_one = true;
2827         break;
2828
2829       default:
2830         break;
2831       }
2832   if (handled_one)
2833     return;
2834
2835   insn = PATTERN (insn);
2836  found:
2837   dwarf2out_frame_debug_expr (insn, label);
2838
2839   /* Check again.  A parallel can save and update the same register.
2840      We could probably check just once, here, but this is safer than
2841      removing the check above.  */
2842   if (clobbers_queued_reg_save (insn))
2843     dwarf2out_flush_queued_reg_saves ();
2844 }
2845
2846 /* Determine if we need to save and restore CFI information around this
2847    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2848    we do need to save/restore, then emit the save now, and insert a
2849    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2850
2851 void
2852 dwarf2out_cfi_begin_epilogue (rtx insn)
2853 {
2854   bool saw_frp = false;
2855   rtx i;
2856
2857   /* Scan forward to the return insn, noticing if there are possible
2858      frame related insns.  */
2859   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2860     {
2861       if (!INSN_P (i))
2862         continue;
2863
2864       /* Look for both regular and sibcalls to end the block.  */
2865       if (returnjump_p (i))
2866         break;
2867       if (CALL_P (i) && SIBLING_CALL_P (i))
2868         break;
2869
2870       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2871         {
2872           int idx;
2873           rtx seq = PATTERN (i);
2874
2875           if (returnjump_p (XVECEXP (seq, 0, 0)))
2876             break;
2877           if (CALL_P (XVECEXP (seq, 0, 0))
2878               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2879             break;
2880
2881           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2882             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2883               saw_frp = true;
2884         }
2885
2886       if (RTX_FRAME_RELATED_P (i))
2887         saw_frp = true;
2888     }
2889
2890   /* If the port doesn't emit epilogue unwind info, we don't need a
2891      save/restore pair.  */
2892   if (!saw_frp)
2893     return;
2894
2895   /* Otherwise, search forward to see if the return insn was the last
2896      basic block of the function.  If so, we don't need save/restore.  */
2897   gcc_assert (i != NULL);
2898   i = next_real_insn (i);
2899   if (i == NULL)
2900     return;
2901
2902   /* Insert the restore before that next real insn in the stream, and before
2903      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2904      properly nested.  This should be after any label or alignment.  This
2905      will be pushed into the CFI stream by the function below.  */
2906   while (1)
2907     {
2908       rtx p = PREV_INSN (i);
2909       if (!NOTE_P (p))
2910         break;
2911       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2912         break;
2913       i = p;
2914     }
2915   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2916
2917   emit_cfa_remember = true;
2918
2919   /* And emulate the state save.  */
2920   gcc_assert (!cfa_remember.in_use);
2921   cfa_remember = cfa;
2922   cfa_remember.in_use = 1;
2923 }
2924
2925 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2926    required.  */
2927
2928 void
2929 dwarf2out_frame_debug_restore_state (void)
2930 {
2931   dw_cfi_ref cfi = new_cfi ();
2932   const char *label = dwarf2out_cfi_label (false);
2933
2934   cfi->dw_cfi_opc = DW_CFA_restore_state;
2935   add_fde_cfi (label, cfi);
2936
2937   gcc_assert (cfa_remember.in_use);
2938   cfa = cfa_remember;
2939   cfa_remember.in_use = 0;
2940 }
2941
2942 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2943 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2944  (enum dwarf_call_frame_info cfi);
2945
2946 static enum dw_cfi_oprnd_type
2947 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2948 {
2949   switch (cfi)
2950     {
2951     case DW_CFA_nop:
2952     case DW_CFA_GNU_window_save:
2953     case DW_CFA_remember_state:
2954     case DW_CFA_restore_state:
2955       return dw_cfi_oprnd_unused;
2956
2957     case DW_CFA_set_loc:
2958     case DW_CFA_advance_loc1:
2959     case DW_CFA_advance_loc2:
2960     case DW_CFA_advance_loc4:
2961     case DW_CFA_MIPS_advance_loc8:
2962       return dw_cfi_oprnd_addr;
2963
2964     case DW_CFA_offset:
2965     case DW_CFA_offset_extended:
2966     case DW_CFA_def_cfa:
2967     case DW_CFA_offset_extended_sf:
2968     case DW_CFA_def_cfa_sf:
2969     case DW_CFA_restore:
2970     case DW_CFA_restore_extended:
2971     case DW_CFA_undefined:
2972     case DW_CFA_same_value:
2973     case DW_CFA_def_cfa_register:
2974     case DW_CFA_register:
2975     case DW_CFA_expression:
2976       return dw_cfi_oprnd_reg_num;
2977
2978     case DW_CFA_def_cfa_offset:
2979     case DW_CFA_GNU_args_size:
2980     case DW_CFA_def_cfa_offset_sf:
2981       return dw_cfi_oprnd_offset;
2982
2983     case DW_CFA_def_cfa_expression:
2984       return dw_cfi_oprnd_loc;
2985
2986     default:
2987       gcc_unreachable ();
2988     }
2989 }
2990
2991 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2992 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2993  (enum dwarf_call_frame_info cfi);
2994
2995 static enum dw_cfi_oprnd_type
2996 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2997 {
2998   switch (cfi)
2999     {
3000     case DW_CFA_def_cfa:
3001     case DW_CFA_def_cfa_sf:
3002     case DW_CFA_offset:
3003     case DW_CFA_offset_extended_sf:
3004     case DW_CFA_offset_extended:
3005       return dw_cfi_oprnd_offset;
3006
3007     case DW_CFA_register:
3008       return dw_cfi_oprnd_reg_num;
3009
3010     case DW_CFA_expression:
3011       return dw_cfi_oprnd_loc;
3012
3013     default:
3014       return dw_cfi_oprnd_unused;
3015     }
3016 }
3017
3018 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3019    switch to the data section instead, and write out a synthetic start label
3020    for collect2 the first time around.  */
3021
3022 static void
3023 switch_to_eh_frame_section (bool back)
3024 {
3025   tree label;
3026
3027 #ifdef EH_FRAME_SECTION_NAME
3028   if (eh_frame_section == 0)
3029     {
3030       int flags;
3031
3032       if (EH_TABLES_CAN_BE_READ_ONLY)
3033         {
3034           int fde_encoding;
3035           int per_encoding;
3036           int lsda_encoding;
3037
3038           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3039                                                        /*global=*/0);
3040           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3041                                                        /*global=*/1);
3042           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3043                                                         /*global=*/0);
3044           flags = ((! flag_pic
3045                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3046                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3047                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3048                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3049                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3050                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3051                    ? 0 : SECTION_WRITE);
3052         }
3053       else
3054         flags = SECTION_WRITE;
3055       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3056     }
3057 #endif /* EH_FRAME_SECTION_NAME */
3058
3059   if (eh_frame_section)
3060     switch_to_section (eh_frame_section);
3061   else
3062     {
3063       /* We have no special eh_frame section.  Put the information in
3064          the data section and emit special labels to guide collect2.  */
3065       switch_to_section (data_section);
3066
3067       if (!back)
3068         {
3069           label = get_file_function_name ("F");
3070           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3071           targetm.asm_out.globalize_label (asm_out_file,
3072                                            IDENTIFIER_POINTER (label));
3073           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3074         }
3075     }
3076 }
3077
3078 /* Switch [BACK] to the eh or debug frame table section, depending on
3079    FOR_EH.  */
3080
3081 static void
3082 switch_to_frame_table_section (int for_eh, bool back)
3083 {
3084   if (for_eh)
3085     switch_to_eh_frame_section (back);
3086   else
3087     {
3088       if (!debug_frame_section)
3089         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3090                                            SECTION_DEBUG, NULL);
3091       switch_to_section (debug_frame_section);
3092     }
3093 }
3094
3095 /* Output a Call Frame Information opcode and its operand(s).  */
3096
3097 static void
3098 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3099 {
3100   unsigned long r;
3101   HOST_WIDE_INT off;
3102
3103   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3104     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3105                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3106                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3107                          ((unsigned HOST_WIDE_INT)
3108                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3109   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3110     {
3111       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3112       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3113                            "DW_CFA_offset, column %#lx", r);
3114       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3115       dw2_asm_output_data_uleb128 (off, NULL);
3116     }
3117   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3118     {
3119       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3120       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3121                            "DW_CFA_restore, column %#lx", r);
3122     }
3123   else
3124     {
3125       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3126                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3127
3128       switch (cfi->dw_cfi_opc)
3129         {
3130         case DW_CFA_set_loc:
3131           if (for_eh)
3132             dw2_asm_output_encoded_addr_rtx (
3133                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3134                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3135                 false, NULL);
3136           else
3137             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3138                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3139           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3140           break;
3141
3142         case DW_CFA_advance_loc1:
3143           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3144                                 fde->dw_fde_current_label, NULL);
3145           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3146           break;
3147
3148         case DW_CFA_advance_loc2:
3149           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3150                                 fde->dw_fde_current_label, NULL);
3151           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3152           break;
3153
3154         case DW_CFA_advance_loc4:
3155           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3156                                 fde->dw_fde_current_label, NULL);
3157           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3158           break;
3159
3160         case DW_CFA_MIPS_advance_loc8:
3161           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3162                                 fde->dw_fde_current_label, NULL);
3163           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3164           break;
3165
3166         case DW_CFA_offset_extended:
3167           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3168           dw2_asm_output_data_uleb128 (r, NULL);
3169           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3170           dw2_asm_output_data_uleb128 (off, NULL);
3171           break;
3172
3173         case DW_CFA_def_cfa:
3174           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3175           dw2_asm_output_data_uleb128 (r, NULL);
3176           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3177           break;
3178
3179         case DW_CFA_offset_extended_sf:
3180           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3181           dw2_asm_output_data_uleb128 (r, NULL);
3182           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3183           dw2_asm_output_data_sleb128 (off, NULL);
3184           break;
3185
3186         case DW_CFA_def_cfa_sf:
3187           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3188           dw2_asm_output_data_uleb128 (r, NULL);
3189           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3190           dw2_asm_output_data_sleb128 (off, NULL);
3191           break;
3192
3193         case DW_CFA_restore_extended:
3194         case DW_CFA_undefined:
3195         case DW_CFA_same_value:
3196         case DW_CFA_def_cfa_register:
3197           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3198           dw2_asm_output_data_uleb128 (r, NULL);
3199           break;
3200
3201         case DW_CFA_register:
3202           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3203           dw2_asm_output_data_uleb128 (r, NULL);
3204           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3205           dw2_asm_output_data_uleb128 (r, NULL);
3206           break;
3207
3208         case DW_CFA_def_cfa_offset:
3209         case DW_CFA_GNU_args_size:
3210           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3211           break;
3212
3213         case DW_CFA_def_cfa_offset_sf:
3214           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3215           dw2_asm_output_data_sleb128 (off, NULL);
3216           break;
3217
3218         case DW_CFA_GNU_window_save:
3219           break;
3220
3221         case DW_CFA_def_cfa_expression:
3222         case DW_CFA_expression:
3223           output_cfa_loc (cfi);
3224           break;
3225
3226         case DW_CFA_GNU_negative_offset_extended:
3227           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3228           gcc_unreachable ();
3229
3230         default:
3231           break;
3232         }
3233     }
3234 }
3235
3236 /* Similar, but do it via assembler directives instead.  */
3237
3238 static void
3239 output_cfi_directive (dw_cfi_ref cfi)
3240 {
3241   unsigned long r, r2;
3242
3243   switch (cfi->dw_cfi_opc)
3244     {
3245     case DW_CFA_advance_loc:
3246     case DW_CFA_advance_loc1:
3247     case DW_CFA_advance_loc2:
3248     case DW_CFA_advance_loc4:
3249     case DW_CFA_MIPS_advance_loc8:
3250     case DW_CFA_set_loc:
3251       /* Should only be created by add_fde_cfi in a code path not
3252          followed when emitting via directives.  The assembler is
3253          going to take care of this for us.  */
3254       gcc_unreachable ();
3255
3256     case DW_CFA_offset:
3257     case DW_CFA_offset_extended:
3258     case DW_CFA_offset_extended_sf:
3259       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3260       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3261                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3262       break;
3263
3264     case DW_CFA_restore:
3265     case DW_CFA_restore_extended:
3266       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3267       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3268       break;
3269
3270     case DW_CFA_undefined:
3271       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3272       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3273       break;
3274
3275     case DW_CFA_same_value:
3276       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3277       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3278       break;
3279
3280     case DW_CFA_def_cfa:
3281     case DW_CFA_def_cfa_sf:
3282       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3283       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3284                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3285       break;
3286
3287     case DW_CFA_def_cfa_register:
3288       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3289       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3290       break;
3291
3292     case DW_CFA_register:
3293       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3294       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3295       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3296       break;
3297
3298     case DW_CFA_def_cfa_offset:
3299     case DW_CFA_def_cfa_offset_sf:
3300       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3301                HOST_WIDE_INT_PRINT_DEC"\n",
3302                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3303       break;
3304
3305     case DW_CFA_remember_state:
3306       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3307       break;
3308     case DW_CFA_restore_state:
3309       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3310       break;
3311
3312     case DW_CFA_GNU_args_size:
3313       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3314       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3315       if (flag_debug_asm)
3316         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3317                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3318       fputc ('\n', asm_out_file);
3319       break;
3320
3321     case DW_CFA_GNU_window_save:
3322       fprintf (asm_out_file, "\t.cfi_window_save\n");
3323       break;
3324
3325     case DW_CFA_def_cfa_expression:
3326     case DW_CFA_expression:
3327       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3328       output_cfa_loc_raw (cfi);
3329       fputc ('\n', asm_out_file);
3330       break;
3331
3332     default:
3333       gcc_unreachable ();
3334     }
3335 }
3336
3337 DEF_VEC_P (dw_cfi_ref);
3338 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3339
3340 /* Output CFIs to bring current FDE to the same state as after executing
3341    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3342    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3343    other arguments to pass to output_cfi.  */
3344
3345 static void
3346 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3347 {
3348   struct dw_cfi_struct cfi_buf;
3349   dw_cfi_ref cfi2;
3350   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3351   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3352   unsigned int len, idx;
3353
3354   for (;; cfi = cfi->dw_cfi_next)
3355     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3356       {
3357       case DW_CFA_advance_loc:
3358       case DW_CFA_advance_loc1:
3359       case DW_CFA_advance_loc2:
3360       case DW_CFA_advance_loc4:
3361       case DW_CFA_MIPS_advance_loc8:
3362       case DW_CFA_set_loc:
3363         /* All advances should be ignored.  */
3364         break;
3365       case DW_CFA_remember_state:
3366         {
3367           dw_cfi_ref args_size = cfi_args_size;
3368
3369           /* Skip everything between .cfi_remember_state and
3370              .cfi_restore_state.  */
3371           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3372             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3373               break;
3374             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3375               args_size = cfi2;
3376             else
3377               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3378
3379           if (cfi2 == NULL)
3380             goto flush_all;
3381           else
3382             {
3383               cfi = cfi2;
3384               cfi_args_size = args_size;
3385             }
3386           break;
3387         }
3388       case DW_CFA_GNU_args_size:
3389         cfi_args_size = cfi;
3390         break;
3391       case DW_CFA_GNU_window_save:
3392         goto flush_all;
3393       case DW_CFA_offset:
3394       case DW_CFA_offset_extended:
3395       case DW_CFA_offset_extended_sf:
3396       case DW_CFA_restore:
3397       case DW_CFA_restore_extended:
3398       case DW_CFA_undefined:
3399       case DW_CFA_same_value:
3400       case DW_CFA_register:
3401       case DW_CFA_val_offset:
3402       case DW_CFA_val_offset_sf:
3403       case DW_CFA_expression:
3404       case DW_CFA_val_expression:
3405       case DW_CFA_GNU_negative_offset_extended:
3406         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3407           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3408                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3409         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3410         break;
3411       case DW_CFA_def_cfa:
3412       case DW_CFA_def_cfa_sf:
3413       case DW_CFA_def_cfa_expression:
3414         cfi_cfa = cfi;
3415         cfi_cfa_offset = cfi;
3416         break;
3417       case DW_CFA_def_cfa_register:
3418         cfi_cfa = cfi;
3419         break;
3420       case DW_CFA_def_cfa_offset:
3421       case DW_CFA_def_cfa_offset_sf:
3422         cfi_cfa_offset = cfi;
3423         break;
3424       case DW_CFA_nop:
3425         gcc_assert (cfi == NULL);
3426       flush_all:
3427         len = VEC_length (dw_cfi_ref, regs);
3428         for (idx = 0; idx < len; idx++)
3429           {
3430             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3431             if (cfi2 != NULL
3432                 && cfi2->dw_cfi_opc != DW_CFA_restore
3433                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3434               {
3435                 if (do_cfi_asm)
3436                   output_cfi_directive (cfi2);
3437                 else
3438                   output_cfi (cfi2, fde, for_eh);
3439               }
3440           }
3441         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3442           {
3443             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3444             cfi_buf = *cfi_cfa;
3445             switch (cfi_cfa_offset->dw_cfi_opc)
3446               {
3447               case DW_CFA_def_cfa_offset:
3448                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3449                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3450                 break;
3451               case DW_CFA_def_cfa_offset_sf:
3452                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3453                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3454                 break;
3455               case DW_CFA_def_cfa:
3456               case DW_CFA_def_cfa_sf:
3457                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3458                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3459                 break;
3460               default:
3461                 gcc_unreachable ();
3462               }
3463             cfi_cfa = &cfi_buf;
3464           }
3465         else if (cfi_cfa_offset)
3466           cfi_cfa = cfi_cfa_offset;
3467         if (cfi_cfa)
3468           {
3469             if (do_cfi_asm)
3470               output_cfi_directive (cfi_cfa);
3471             else
3472               output_cfi (cfi_cfa, fde, for_eh);
3473           }
3474         cfi_cfa = NULL;
3475         cfi_cfa_offset = NULL;
3476         if (cfi_args_size
3477             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3478           {
3479             if (do_cfi_asm)
3480               output_cfi_directive (cfi_args_size);
3481             else
3482               output_cfi (cfi_args_size, fde, for_eh);
3483           }
3484         cfi_args_size = NULL;
3485         if (cfi == NULL)
3486           {
3487             VEC_free (dw_cfi_ref, heap, regs);
3488             return;
3489           }
3490         else if (do_cfi_asm)
3491           output_cfi_directive (cfi);
3492         else
3493           output_cfi (cfi, fde, for_eh);
3494         break;
3495       default:
3496         gcc_unreachable ();
3497     }
3498 }
3499
3500 /* Output one FDE.  */
3501
3502 static void
3503 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3504             char *section_start_label, int fde_encoding, char *augmentation,
3505             bool any_lsda_needed, int lsda_encoding)
3506 {
3507   const char *begin, *end;
3508   static unsigned int j;
3509   char l1[20], l2[20];
3510   dw_cfi_ref cfi;
3511
3512   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3513                                      /* empty */ 0);
3514   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3515                                   for_eh + j);
3516   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3517   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3518   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3519     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3520                          " indicating 64-bit DWARF extension");
3521   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3522                         "FDE Length");
3523   ASM_OUTPUT_LABEL (asm_out_file, l1);
3524
3525   if (for_eh)
3526     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3527   else
3528     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3529                            debug_frame_section, "FDE CIE offset");
3530
3531   if (!fde->dw_fde_switched_sections)
3532     {
3533       begin = fde->dw_fde_begin;
3534       end = fde->dw_fde_end;
3535     }
3536   else
3537     {
3538       /* For the first section, prefer dw_fde_begin over
3539          dw_fde_{hot,cold}_section_label, as the latter
3540          might be separated from the real start of the
3541          function by alignment padding.  */
3542       if (!second)
3543         begin = fde->dw_fde_begin;
3544       else if (fde->dw_fde_switched_cold_to_hot)
3545         begin = fde->dw_fde_hot_section_label;
3546       else
3547         begin = fde->dw_fde_unlikely_section_label;
3548       if (second ^ fde->dw_fde_switched_cold_to_hot)
3549         end = fde->dw_fde_unlikely_section_end_label;
3550       else
3551         end = fde->dw_fde_hot_section_end_label;
3552     }
3553
3554   if (for_eh)
3555     {
3556       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3557       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3558       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3559                                        "FDE initial location");
3560       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3561                             end, begin, "FDE address range");
3562     }
3563   else
3564     {
3565       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3566       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3567     }
3568
3569   if (augmentation[0])
3570     {
3571       if (any_lsda_needed)
3572         {
3573           int size = size_of_encoded_value (lsda_encoding);
3574
3575           if (lsda_encoding == DW_EH_PE_aligned)
3576             {
3577               int offset = (  4         /* Length */
3578                             + 4         /* CIE offset */
3579                             + 2 * size_of_encoded_value (fde_encoding)
3580                             + 1         /* Augmentation size */ );
3581               int pad = -offset & (PTR_SIZE - 1);
3582
3583               size += pad;
3584               gcc_assert (size_of_uleb128 (size) == 1);
3585             }
3586
3587           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3588
3589           if (fde->uses_eh_lsda)
3590             {
3591               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3592                                            fde->funcdef_number);
3593               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3594                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3595                                                false,
3596                                                "Language Specific Data Area");
3597             }
3598           else
3599             {
3600               if (lsda_encoding == DW_EH_PE_aligned)
3601                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3602               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3603                                    "Language Specific Data Area (none)");
3604             }
3605         }
3606       else
3607         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3608     }
3609
3610   /* Loop through the Call Frame Instructions associated with
3611      this FDE.  */
3612   fde->dw_fde_current_label = begin;
3613   if (!fde->dw_fde_switched_sections)
3614     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3615       output_cfi (cfi, fde, for_eh);
3616   else if (!second)
3617     {
3618       if (fde->dw_fde_switch_cfi)
3619         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3620           {
3621             output_cfi (cfi, fde, for_eh);
3622             if (cfi == fde->dw_fde_switch_cfi)
3623               break;
3624           }
3625     }
3626   else
3627     {
3628       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3629
3630       if (fde->dw_fde_switch_cfi)
3631         {
3632           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3633           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3634           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3635           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3636         }
3637       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3638         output_cfi (cfi, fde, for_eh);
3639     }
3640
3641   /* If we are to emit a ref/link from function bodies to their frame tables,
3642      do it now.  This is typically performed to make sure that tables
3643      associated with functions are dragged with them and not discarded in
3644      garbage collecting links. We need to do this on a per function basis to
3645      cope with -ffunction-sections.  */
3646
3647 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3648   /* Switch to the function section, emit the ref to the tables, and
3649      switch *back* into the table section.  */
3650   switch_to_section (function_section (fde->decl));
3651   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3652   switch_to_frame_table_section (for_eh, true);
3653 #endif
3654
3655   /* Pad the FDE out to an address sized boundary.  */
3656   ASM_OUTPUT_ALIGN (asm_out_file,
3657                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3658   ASM_OUTPUT_LABEL (asm_out_file, l2);
3659
3660   j += 2;
3661 }
3662
3663 /* Return true if frame description entry FDE is needed for EH.  */
3664
3665 static bool
3666 fde_needed_for_eh_p (dw_fde_ref fde)
3667 {
3668   if (flag_asynchronous_unwind_tables)
3669     return true;
3670
3671   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3672     return true;
3673
3674   if (fde->uses_eh_lsda)
3675     return true;
3676
3677   /* If exceptions are enabled, we have collected nothrow info.  */
3678   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3679     return false;
3680
3681   return true;
3682 }
3683
3684 /* Output the call frame information used to record information
3685    that relates to calculating the frame pointer, and records the
3686    location of saved registers.  */
3687
3688 static void
3689 output_call_frame_info (int for_eh)
3690 {
3691   unsigned int i;
3692   dw_fde_ref fde;
3693   dw_cfi_ref cfi;
3694   char l1[20], l2[20], section_start_label[20];
3695   bool any_lsda_needed = false;
3696   char augmentation[6];
3697   int augmentation_size;
3698   int fde_encoding = DW_EH_PE_absptr;
3699   int per_encoding = DW_EH_PE_absptr;
3700   int lsda_encoding = DW_EH_PE_absptr;
3701   int return_reg;
3702   rtx personality = NULL;
3703   int dw_cie_version;
3704
3705   /* Don't emit a CIE if there won't be any FDEs.  */
3706   if (fde_table_in_use == 0)
3707     return;
3708
3709   /* Nothing to do if the assembler's doing it all.  */
3710   if (dwarf2out_do_cfi_asm ())
3711     return;
3712
3713   /* If we don't have any functions we'll want to unwind out of, don't emit
3714      any EH unwind information.  If we make FDEs linkonce, we may have to
3715      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3716      want to avoid having an FDE kept around when the function it refers to
3717      is discarded.  Example where this matters: a primary function template
3718      in C++ requires EH information, an explicit specialization doesn't.  */
3719   if (for_eh)
3720     {
3721       bool any_eh_needed = false;
3722
3723       for (i = 0; i < fde_table_in_use; i++)
3724         if (fde_table[i].uses_eh_lsda)
3725           any_eh_needed = any_lsda_needed = true;
3726         else if (fde_needed_for_eh_p (&fde_table[i]))
3727           any_eh_needed = true;
3728         else if (TARGET_USES_WEAK_UNWIND_INFO)
3729           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3730                                              1, 1);
3731
3732       if (!any_eh_needed)
3733         return;
3734     }
3735
3736   /* We're going to be generating comments, so turn on app.  */
3737   if (flag_debug_asm)
3738     app_enable ();
3739
3740   /* Switch to the proper frame section, first time.  */
3741   switch_to_frame_table_section (for_eh, false);
3742
3743   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3744   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3745
3746   /* Output the CIE.  */
3747   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3748   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3749   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3750     dw2_asm_output_data (4, 0xffffffff,
3751       "Initial length escape value indicating 64-bit DWARF extension");
3752   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3753                         "Length of Common Information Entry");
3754   ASM_OUTPUT_LABEL (asm_out_file, l1);
3755
3756   /* Now that the CIE pointer is PC-relative for EH,
3757      use 0 to identify the CIE.  */
3758   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3759                        (for_eh ? 0 : DWARF_CIE_ID),
3760                        "CIE Identifier Tag");
3761
3762   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3763      use CIE version 1, unless that would produce incorrect results
3764      due to overflowing the return register column.  */
3765   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3766   dw_cie_version = 1;
3767   if (return_reg >= 256 || dwarf_version > 2)
3768     dw_cie_version = 3;
3769   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3770
3771   augmentation[0] = 0;
3772   augmentation_size = 0;
3773
3774   personality = current_unit_personality;
3775   if (for_eh)
3776     {
3777       char *p;
3778
3779       /* Augmentation:
3780          z      Indicates that a uleb128 is present to size the
3781                 augmentation section.
3782          L      Indicates the encoding (and thus presence) of
3783                 an LSDA pointer in the FDE augmentation.
3784          R      Indicates a non-default pointer encoding for
3785                 FDE code pointers.
3786          P      Indicates the presence of an encoding + language
3787                 personality routine in the CIE augmentation.  */
3788
3789       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3790       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3791       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3792
3793       p = augmentation + 1;
3794       if (personality)
3795         {
3796           *p++ = 'P';
3797           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3798           assemble_external_libcall (personality);
3799         }
3800       if (any_lsda_needed)
3801         {
3802           *p++ = 'L';
3803           augmentation_size += 1;
3804         }
3805       if (fde_encoding != DW_EH_PE_absptr)
3806         {
3807           *p++ = 'R';
3808           augmentation_size += 1;
3809         }
3810       if (p > augmentation + 1)
3811         {
3812           augmentation[0] = 'z';
3813           *p = '\0';
3814         }
3815
3816       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3817       if (personality && per_encoding == DW_EH_PE_aligned)
3818         {
3819           int offset = (  4             /* Length */
3820                         + 4             /* CIE Id */
3821                         + 1             /* CIE version */
3822                         + strlen (augmentation) + 1     /* Augmentation */
3823                         + size_of_uleb128 (1)           /* Code alignment */
3824                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3825                         + 1             /* RA column */
3826                         + 1             /* Augmentation size */
3827                         + 1             /* Personality encoding */ );
3828           int pad = -offset & (PTR_SIZE - 1);
3829
3830           augmentation_size += pad;
3831
3832           /* Augmentations should be small, so there's scarce need to
3833              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3834           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3835         }
3836     }
3837
3838   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3839   if (dw_cie_version >= 4)
3840     {
3841       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3842       dw2_asm_output_data (1, 0, "CIE Segment Size");
3843     }
3844   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3845   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3846                                "CIE Data Alignment Factor");
3847
3848   if (dw_cie_version == 1)
3849     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3850   else
3851     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3852
3853   if (augmentation[0])
3854     {
3855       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3856       if (personality)
3857         {
3858           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3859                                eh_data_format_name (per_encoding));
3860           dw2_asm_output_encoded_addr_rtx (per_encoding,
3861                                            personality,
3862                                            true, NULL);
3863         }
3864
3865       if (any_lsda_needed)
3866         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3867                              eh_data_format_name (lsda_encoding));
3868
3869       if (fde_encoding != DW_EH_PE_absptr)
3870         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3871                              eh_data_format_name (fde_encoding));
3872     }
3873
3874   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3875     output_cfi (cfi, NULL, for_eh);
3876
3877   /* Pad the CIE out to an address sized boundary.  */
3878   ASM_OUTPUT_ALIGN (asm_out_file,
3879                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3880   ASM_OUTPUT_LABEL (asm_out_file, l2);
3881
3882   /* Loop through all of the FDE's.  */
3883   for (i = 0; i < fde_table_in_use; i++)
3884     {
3885       unsigned int k;
3886       fde = &fde_table[i];
3887
3888       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3889       if (for_eh && !fde_needed_for_eh_p (fde))
3890         continue;
3891
3892       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3893         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3894                     augmentation, any_lsda_needed, lsda_encoding);
3895     }
3896
3897   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3898     dw2_asm_output_data (4, 0, "End of Table");
3899 #ifdef MIPS_DEBUGGING_INFO
3900   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3901      get a value of 0.  Putting .align 0 after the label fixes it.  */
3902   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3903 #endif
3904
3905   /* Turn off app to make assembly quicker.  */
3906   if (flag_debug_asm)
3907     app_disable ();
3908 }
3909
3910 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3911
3912 static void
3913 dwarf2out_do_cfi_startproc (bool second)
3914 {
3915   int enc;
3916   rtx ref;
3917   rtx personality = get_personality_function (current_function_decl);
3918
3919   fprintf (asm_out_file, "\t.cfi_startproc\n");
3920
3921   if (personality)
3922     {
3923       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3924       ref = personality;
3925
3926       /* ??? The GAS support isn't entirely consistent.  We have to
3927          handle indirect support ourselves, but PC-relative is done
3928          in the assembler.  Further, the assembler can't handle any
3929          of the weirder relocation types.  */
3930       if (enc & DW_EH_PE_indirect)
3931         ref = dw2_force_const_mem (ref, true);
3932
3933       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3934       output_addr_const (asm_out_file, ref);
3935       fputc ('\n', asm_out_file);
3936     }
3937
3938   if (crtl->uses_eh_lsda)
3939     {
3940       char lab[20];
3941
3942       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3943       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3944                                    current_function_funcdef_no);
3945       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3946       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3947
3948       if (enc & DW_EH_PE_indirect)
3949         ref = dw2_force_const_mem (ref, true);
3950
3951       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3952       output_addr_const (asm_out_file, ref);
3953       fputc ('\n', asm_out_file);
3954     }
3955 }
3956
3957 /* Output a marker (i.e. a label) for the beginning of a function, before
3958    the prologue.  */
3959
3960 void
3961 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3962                           const char *file ATTRIBUTE_UNUSED)
3963 {
3964   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3965   char * dup_label;
3966   dw_fde_ref fde;
3967   section *fnsec;
3968
3969   current_function_func_begin_label = NULL;
3970
3971 #ifdef TARGET_UNWIND_INFO
3972   /* ??? current_function_func_begin_label is also used by except.c
3973      for call-site information.  We must emit this label if it might
3974      be used.  */
3975   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3976       && ! dwarf2out_do_frame ())
3977     return;
3978 #else
3979   if (! dwarf2out_do_frame ())
3980     return;
3981 #endif
3982
3983   fnsec = function_section (current_function_decl);
3984   switch_to_section (fnsec);
3985   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3986                                current_function_funcdef_no);
3987   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3988                           current_function_funcdef_no);
3989   dup_label = xstrdup (label);
3990   current_function_func_begin_label = dup_label;
3991
3992 #ifdef TARGET_UNWIND_INFO
3993   /* We can elide the fde allocation if we're not emitting debug info.  */
3994   if (! dwarf2out_do_frame ())
3995     return;
3996 #endif
3997
3998   /* Expand the fde table if necessary.  */
3999   if (fde_table_in_use == fde_table_allocated)
4000     {
4001       fde_table_allocated += FDE_TABLE_INCREMENT;
4002       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4003       memset (fde_table + fde_table_in_use, 0,
4004               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4005     }
4006
4007   /* Record the FDE associated with this function.  */
4008   current_funcdef_fde = fde_table_in_use;
4009
4010   /* Add the new FDE at the end of the fde_table.  */
4011   fde = &fde_table[fde_table_in_use++];
4012   fde->decl = current_function_decl;
4013   fde->dw_fde_begin = dup_label;
4014   fde->dw_fde_current_label = dup_label;
4015   fde->dw_fde_hot_section_label = NULL;
4016   fde->dw_fde_hot_section_end_label = NULL;
4017   fde->dw_fde_unlikely_section_label = NULL;
4018   fde->dw_fde_unlikely_section_end_label = NULL;
4019   fde->dw_fde_switched_sections = 0;
4020   fde->dw_fde_switched_cold_to_hot = 0;
4021   fde->dw_fde_end = NULL;
4022   fde->dw_fde_vms_end_prologue = NULL;
4023   fde->dw_fde_vms_begin_epilogue = NULL;
4024   fde->dw_fde_cfi = NULL;
4025   fde->dw_fde_switch_cfi = NULL;
4026   fde->funcdef_number = current_function_funcdef_no;
4027   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4028   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4029   fde->nothrow = crtl->nothrow;
4030   fde->drap_reg = INVALID_REGNUM;
4031   fde->vdrap_reg = INVALID_REGNUM;
4032   if (flag_reorder_blocks_and_partition)
4033     {
4034       section *unlikelysec;
4035       if (first_function_block_is_cold)
4036         fde->in_std_section = 1;
4037       else
4038         fde->in_std_section
4039           = (fnsec == text_section
4040              || (cold_text_section && fnsec == cold_text_section));
4041       unlikelysec = unlikely_text_section ();
4042       fde->cold_in_std_section
4043         = (unlikelysec == text_section
4044            || (cold_text_section && unlikelysec == cold_text_section));
4045     }
4046   else
4047     {
4048       fde->in_std_section
4049         = (fnsec == text_section
4050            || (cold_text_section && fnsec == cold_text_section));
4051       fde->cold_in_std_section = 0;
4052     }
4053
4054   args_size = old_args_size = 0;
4055
4056   /* We only want to output line number information for the genuine dwarf2
4057      prologue case, not the eh frame case.  */
4058 #ifdef DWARF2_DEBUGGING_INFO
4059   if (file)
4060     dwarf2out_source_line (line, file, 0, true);
4061 #endif
4062
4063   if (dwarf2out_do_cfi_asm ())
4064     dwarf2out_do_cfi_startproc (false);
4065   else
4066     {
4067       rtx personality = get_personality_function (current_function_decl);
4068       if (!current_unit_personality)
4069         current_unit_personality = personality;
4070
4071       /* We cannot keep a current personality per function as without CFI
4072          asm, at the point where we emit the CFI data, there is no current
4073          function anymore.  */
4074       if (personality && current_unit_personality != personality)
4075         sorry ("multiple EH personalities are supported only with assemblers "
4076                "supporting .cfi_personality directive");
4077     }
4078 }
4079
4080 /* Output a marker (i.e. a label) for the end of the generated code
4081    for a function prologue.  This gets called *after* the prologue code has
4082    been generated.  */
4083
4084 void
4085 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4086                         const char *file ATTRIBUTE_UNUSED)
4087 {
4088   dw_fde_ref fde;
4089   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4090
4091   /* Output a label to mark the endpoint of the code generated for this
4092      function.  */
4093   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4094                                current_function_funcdef_no);
4095   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4096                           current_function_funcdef_no);
4097   fde = &fde_table[fde_table_in_use - 1];
4098   fde->dw_fde_vms_end_prologue = xstrdup (label);
4099 }
4100
4101 /* Output a marker (i.e. a label) for the beginning of the generated code
4102    for a function epilogue.  This gets called *before* the prologue code has
4103    been generated.  */
4104
4105 void
4106 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4107                           const char *file ATTRIBUTE_UNUSED)
4108 {
4109   dw_fde_ref fde;
4110   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4111
4112   fde = &fde_table[fde_table_in_use - 1];
4113   if (fde->dw_fde_vms_begin_epilogue)
4114     return;
4115
4116   /* Output a label to mark the endpoint of the code generated for this
4117      function.  */
4118   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4119                                current_function_funcdef_no);
4120   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4121                           current_function_funcdef_no);
4122   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4123 }
4124
4125 /* Output a marker (i.e. a label) for the absolute end of the generated code
4126    for a function definition.  This gets called *after* the epilogue code has
4127    been generated.  */
4128
4129 void
4130 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4131                         const char *file ATTRIBUTE_UNUSED)
4132 {
4133   dw_fde_ref fde;
4134   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4135
4136   last_var_location_insn = NULL_RTX;
4137
4138   if (dwarf2out_do_cfi_asm ())
4139     fprintf (asm_out_file, "\t.cfi_endproc\n");
4140
4141   /* Output a label to mark the endpoint of the code generated for this
4142      function.  */
4143   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4144                                current_function_funcdef_no);
4145   ASM_OUTPUT_LABEL (asm_out_file, label);
4146   fde = current_fde ();
4147   gcc_assert (fde != NULL);
4148   fde->dw_fde_end = xstrdup (label);
4149 }
4150
4151 void
4152 dwarf2out_frame_init (void)
4153 {
4154   /* Allocate the initial hunk of the fde_table.  */
4155   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4156   fde_table_allocated = FDE_TABLE_INCREMENT;
4157   fde_table_in_use = 0;
4158
4159   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4160      sake of lookup_cfa.  */
4161
4162   /* On entry, the Canonical Frame Address is at SP.  */
4163   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4164
4165   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4166     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4167 }
4168
4169 void
4170 dwarf2out_frame_finish (void)
4171 {
4172   /* Output call frame information.  */
4173   if (DWARF2_FRAME_INFO)
4174     output_call_frame_info (0);
4175
4176 #ifndef TARGET_UNWIND_INFO
4177   /* Output another copy for the unwinder.  */
4178   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4179     output_call_frame_info (1);
4180 #endif
4181 }
4182
4183 /* Note that the current function section is being used for code.  */
4184
4185 static void
4186 dwarf2out_note_section_used (void)
4187 {
4188   section *sec = current_function_section ();
4189   if (sec == text_section)
4190     text_section_used = true;
4191   else if (sec == cold_text_section)
4192     cold_text_section_used = true;
4193 }
4194
4195 void
4196 dwarf2out_switch_text_section (void)
4197 {
4198   dw_fde_ref fde = current_fde ();
4199
4200   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4201
4202   fde->dw_fde_switched_sections = 1;
4203   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4204
4205   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4206   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4207   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4208   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4209   have_multiple_function_sections = true;
4210
4211   /* Reset the current label on switching text sections, so that we
4212      don't attempt to advance_loc4 between labels in different sections.  */
4213   fde->dw_fde_current_label = NULL;
4214
4215   /* There is no need to mark used sections when not debugging.  */
4216   if (cold_text_section != NULL)
4217     dwarf2out_note_section_used ();
4218
4219   if (dwarf2out_do_cfi_asm ())
4220     fprintf (asm_out_file, "\t.cfi_endproc\n");
4221
4222   /* Now do the real section switch.  */
4223   switch_to_section (current_function_section ());
4224
4225   if (dwarf2out_do_cfi_asm ())
4226     {
4227       dwarf2out_do_cfi_startproc (true);
4228       /* As this is a different FDE, insert all current CFI instructions
4229          again.  */
4230       output_cfis (fde->dw_fde_cfi, true, fde, true);
4231     }
4232   else
4233     {
4234       dw_cfi_ref cfi = fde->dw_fde_cfi;
4235
4236       cfi = fde->dw_fde_cfi;
4237       if (cfi)
4238         while (cfi->dw_cfi_next != NULL)
4239           cfi = cfi->dw_cfi_next;
4240       fde->dw_fde_switch_cfi = cfi;
4241     }
4242 }
4243 \f
4244 /* And now, the subset of the debugging information support code necessary
4245    for emitting location expressions.  */
4246
4247 /* Data about a single source file.  */
4248 struct GTY(()) dwarf_file_data {
4249   const char * filename;
4250   int emitted_number;
4251 };
4252
4253 typedef struct dw_val_struct *dw_val_ref;
4254 typedef struct die_struct *dw_die_ref;
4255 typedef const struct die_struct *const_dw_die_ref;
4256 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4257 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4258
4259 typedef struct GTY(()) deferred_locations_struct
4260 {
4261   tree variable;
4262   dw_die_ref die;
4263 } deferred_locations;
4264
4265 DEF_VEC_O(deferred_locations);
4266 DEF_VEC_ALLOC_O(deferred_locations,gc);
4267
4268 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4269
4270 DEF_VEC_P(dw_die_ref);
4271 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4272
4273 /* Each DIE may have a series of attribute/value pairs.  Values
4274    can take on several forms.  The forms that are used in this
4275    implementation are listed below.  */
4276
4277 enum dw_val_class
4278 {
4279   dw_val_class_addr,
4280   dw_val_class_offset,
4281   dw_val_class_loc,
4282   dw_val_class_loc_list,
4283   dw_val_class_range_list,
4284   dw_val_class_const,
4285   dw_val_class_unsigned_const,
4286   dw_val_class_const_double,
4287   dw_val_class_vec,
4288   dw_val_class_flag,
4289   dw_val_class_die_ref,
4290   dw_val_class_fde_ref,
4291   dw_val_class_lbl_id,
4292   dw_val_class_lineptr,
4293   dw_val_class_str,
4294   dw_val_class_macptr,
4295   dw_val_class_file,
4296   dw_val_class_data8,
4297   dw_val_class_decl_ref,
4298   dw_val_class_vms_delta
4299 };
4300
4301 /* Describe a floating point constant value, or a vector constant value.  */
4302
4303 typedef struct GTY(()) dw_vec_struct {
4304   unsigned char * GTY((length ("%h.length"))) array;
4305   unsigned length;
4306   unsigned elt_size;
4307 }
4308 dw_vec_const;
4309
4310 /* The dw_val_node describes an attribute's value, as it is
4311    represented internally.  */
4312
4313 typedef struct GTY(()) dw_val_struct {
4314   enum dw_val_class val_class;
4315   union dw_val_struct_union
4316     {
4317       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4318       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4319       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4320       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4321       HOST_WIDE_INT GTY ((default)) val_int;
4322       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4323       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4324       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4325       struct dw_val_die_union
4326         {
4327           dw_die_ref die;
4328           int external;
4329         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4330       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4331       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4332       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4333       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4334       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4335       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4336       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4337       struct dw_val_vms_delta_union
4338         {
4339           char * lbl1;
4340           char * lbl2;
4341         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4342     }
4343   GTY ((desc ("%1.val_class"))) v;
4344 }
4345 dw_val_node;
4346
4347 /* Locations in memory are described using a sequence of stack machine
4348    operations.  */
4349
4350 typedef struct GTY(()) dw_loc_descr_struct {
4351   dw_loc_descr_ref dw_loc_next;
4352   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4353   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4354      from DW_OP_addr with a dtp-relative symbol relocation.  */
4355   unsigned int dtprel : 1;
4356   int dw_loc_addr;
4357   dw_val_node dw_loc_oprnd1;
4358   dw_val_node dw_loc_oprnd2;
4359 }
4360 dw_loc_descr_node;
4361
4362 /* Location lists are ranges + location descriptions for that range,
4363    so you can track variables that are in different places over
4364    their entire life.  */
4365 typedef struct GTY(()) dw_loc_list_struct {
4366   dw_loc_list_ref dw_loc_next;
4367   const char *begin; /* Label for begin address of range */
4368   const char *end;  /* Label for end address of range */
4369   char *ll_symbol; /* Label for beginning of location list.
4370                       Only on head of list */
4371   const char *section; /* Section this loclist is relative to */
4372   dw_loc_descr_ref expr;
4373 } dw_loc_list_node;
4374
4375 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4376
4377 /* Convert a DWARF stack opcode into its string name.  */
4378
4379 static const char *
4380 dwarf_stack_op_name (unsigned int op)
4381 {
4382   switch (op)
4383     {
4384     case DW_OP_addr:
4385       return "DW_OP_addr";
4386     case DW_OP_deref:
4387       return "DW_OP_deref";
4388     case DW_OP_const1u:
4389       return "DW_OP_const1u";
4390     case DW_OP_const1s:
4391       return "DW_OP_const1s";
4392     case DW_OP_const2u:
4393       return "DW_OP_const2u";
4394     case DW_OP_const2s:
4395       return "DW_OP_const2s";
4396     case DW_OP_const4u:
4397       return "DW_OP_const4u";
4398     case DW_OP_const4s:
4399       return "DW_OP_const4s";
4400     case DW_OP_const8u:
4401       return "DW_OP_const8u";
4402     case DW_OP_const8s:
4403       return "DW_OP_const8s";
4404     case DW_OP_constu:
4405       return "DW_OP_constu";
4406     case DW_OP_consts:
4407       return "DW_OP_consts";
4408     case DW_OP_dup:
4409       return "DW_OP_dup";
4410     case DW_OP_drop:
4411       return "DW_OP_drop";
4412     case DW_OP_over:
4413       return "DW_OP_over";
4414     case DW_OP_pick:
4415       return "DW_OP_pick";
4416     case DW_OP_swap:
4417       return "DW_OP_swap";
4418     case DW_OP_rot:
4419       return "DW_OP_rot";
4420     case DW_OP_xderef:
4421       return "DW_OP_xderef";
4422     case DW_OP_abs:
4423       return "DW_OP_abs";
4424     case DW_OP_and:
4425       return "DW_OP_and";
4426     case DW_OP_div:
4427       return "DW_OP_div";
4428     case DW_OP_minus:
4429       return "DW_OP_minus";
4430     case DW_OP_mod:
4431       return "DW_OP_mod";
4432     case DW_OP_mul:
4433       return "DW_OP_mul";
4434     case DW_OP_neg:
4435       return "DW_OP_neg";
4436     case DW_OP_not:
4437       return "DW_OP_not";
4438     case DW_OP_or:
4439       return "DW_OP_or";
4440     case DW_OP_plus:
4441       return "DW_OP_plus";
4442     case DW_OP_plus_uconst:
4443       return "DW_OP_plus_uconst";
4444     case DW_OP_shl:
4445       return "DW_OP_shl";
4446     case DW_OP_shr:
4447       return "DW_OP_shr";
4448     case DW_OP_shra:
4449       return "DW_OP_shra";
4450     case DW_OP_xor:
4451       return "DW_OP_xor";
4452     case DW_OP_bra:
4453       return "DW_OP_bra";
4454     case DW_OP_eq:
4455       return "DW_OP_eq";
4456     case DW_OP_ge:
4457       return "DW_OP_ge";
4458     case DW_OP_gt:
4459       return "DW_OP_gt";
4460     case DW_OP_le:
4461       return "DW_OP_le";
4462     case DW_OP_lt:
4463       return "DW_OP_lt";
4464     case DW_OP_ne:
4465       return "DW_OP_ne";
4466     case DW_OP_skip:
4467       return "DW_OP_skip";
4468     case DW_OP_lit0:
4469       return "DW_OP_lit0";
4470     case DW_OP_lit1:
4471       return "DW_OP_lit1";
4472     case DW_OP_lit2:
4473       return "DW_OP_lit2";
4474     case DW_OP_lit3:
4475       return "DW_OP_lit3";
4476     case DW_OP_lit4:
4477       return "DW_OP_lit4";
4478     case DW_OP_lit5:
4479       return "DW_OP_lit5";
4480     case DW_OP_lit6:
4481       return "DW_OP_lit6";
4482     case DW_OP_lit7:
4483       return "DW_OP_lit7";
4484     case DW_OP_lit8:
4485       return "DW_OP_lit8";
4486     case DW_OP_lit9:
4487       return "DW_OP_lit9";
4488     case DW_OP_lit10:
4489       return "DW_OP_lit10";
4490     case DW_OP_lit11:
4491       return "DW_OP_lit11";
4492     case DW_OP_lit12:
4493       return "DW_OP_lit12";
4494     case DW_OP_lit13:
4495       return "DW_OP_lit13";
4496     case DW_OP_lit14:
4497       return "DW_OP_lit14";
4498     case DW_OP_lit15:
4499       return "DW_OP_lit15";
4500     case DW_OP_lit16:
4501       return "DW_OP_lit16";
4502     case DW_OP_lit17:
4503       return "DW_OP_lit17";
4504     case DW_OP_lit18:
4505       return "DW_OP_lit18";
4506     case DW_OP_lit19:
4507       return "DW_OP_lit19";
4508     case DW_OP_lit20:
4509       return "DW_OP_lit20";
4510     case DW_OP_lit21:
4511       return "DW_OP_lit21";
4512     case DW_OP_lit22:
4513       return "DW_OP_lit22";
4514     case DW_OP_lit23:
4515       return "DW_OP_lit23";
4516     case DW_OP_lit24:
4517       return "DW_OP_lit24";
4518     case DW_OP_lit25:
4519       return "DW_OP_lit25";
4520     case DW_OP_lit26:
4521       return "DW_OP_lit26";
4522     case DW_OP_lit27:
4523       return "DW_OP_lit27";
4524     case DW_OP_lit28:
4525       return "DW_OP_lit28";
4526     case DW_OP_lit29:
4527       return "DW_OP_lit29";
4528     case DW_OP_lit30:
4529       return "DW_OP_lit30";
4530     case DW_OP_lit31:
4531       return "DW_OP_lit31";
4532     case DW_OP_reg0:
4533       return "DW_OP_reg0";
4534     case DW_OP_reg1:
4535       return "DW_OP_reg1";
4536     case DW_OP_reg2:
4537       return "DW_OP_reg2";
4538     case DW_OP_reg3:
4539       return "DW_OP_reg3";
4540     case DW_OP_reg4:
4541       return "DW_OP_reg4";
4542     case DW_OP_reg5:
4543       return "DW_OP_reg5";
4544     case DW_OP_reg6:
4545       return "DW_OP_reg6";
4546     case DW_OP_reg7:
4547       return "DW_OP_reg7";
4548     case DW_OP_reg8:
4549       return "DW_OP_reg8";
4550     case DW_OP_reg9:
4551       return "DW_OP_reg9";
4552     case DW_OP_reg10:
4553       return "DW_OP_reg10";
4554     case DW_OP_reg11:
4555       return "DW_OP_reg11";
4556     case DW_OP_reg12:
4557       return "DW_OP_reg12";
4558     case DW_OP_reg13:
4559       return "DW_OP_reg13";
4560     case DW_OP_reg14:
4561       return "DW_OP_reg14";
4562     case DW_OP_reg15:
4563       return "DW_OP_reg15";
4564     case DW_OP_reg16:
4565       return "DW_OP_reg16";
4566     case DW_OP_reg17:
4567       return "DW_OP_reg17";
4568     case DW_OP_reg18:
4569       return "DW_OP_reg18";
4570     case DW_OP_reg19:
4571       return "DW_OP_reg19";
4572     case DW_OP_reg20:
4573       return "DW_OP_reg20";
4574     case DW_OP_reg21:
4575       return "DW_OP_reg21";
4576     case DW_OP_reg22:
4577       return "DW_OP_reg22";
4578     case DW_OP_reg23:
4579       return "DW_OP_reg23";
4580     case DW_OP_reg24:
4581       return "DW_OP_reg24";
4582     case DW_OP_reg25:
4583       return "DW_OP_reg25";
4584     case DW_OP_reg26:
4585       return "DW_OP_reg26";
4586     case DW_OP_reg27:
4587       return "DW_OP_reg27";
4588     case DW_OP_reg28:
4589       return "DW_OP_reg28";
4590     case DW_OP_reg29:
4591       return "DW_OP_reg29";
4592     case DW_OP_reg30:
4593       return "DW_OP_reg30";
4594     case DW_OP_reg31:
4595       return "DW_OP_reg31";
4596     case DW_OP_breg0:
4597       return "DW_OP_breg0";
4598     case DW_OP_breg1:
4599       return "DW_OP_breg1";
4600     case DW_OP_breg2:
4601       return "DW_OP_breg2";
4602     case DW_OP_breg3:
4603       return "DW_OP_breg3";
4604     case DW_OP_breg4:
4605       return "DW_OP_breg4";
4606     case DW_OP_breg5:
4607       return "DW_OP_breg5";
4608     case DW_OP_breg6:
4609       return "DW_OP_breg6";
4610     case DW_OP_breg7:
4611       return "DW_OP_breg7";
4612     case DW_OP_breg8:
4613       return "DW_OP_breg8";
4614     case DW_OP_breg9:
4615       return "DW_OP_breg9";
4616     case DW_OP_breg10:
4617       return "DW_OP_breg10";
4618     case DW_OP_breg11:
4619       return "DW_OP_breg11";
4620     case DW_OP_breg12:
4621       return "DW_OP_breg12";
4622     case DW_OP_breg13:
4623       return "DW_OP_breg13";
4624     case DW_OP_breg14:
4625       return "DW_OP_breg14";
4626     case DW_OP_breg15:
4627       return "DW_OP_breg15";
4628     case DW_OP_breg16:
4629       return "DW_OP_breg16";
4630     case DW_OP_breg17:
4631       return "DW_OP_breg17";
4632     case DW_OP_breg18:
4633       return "DW_OP_breg18";
4634     case DW_OP_breg19:
4635       return "DW_OP_breg19";
4636     case DW_OP_breg20:
4637       return "DW_OP_breg20";
4638     case DW_OP_breg21:
4639       return "DW_OP_breg21";
4640     case DW_OP_breg22:
4641       return "DW_OP_breg22";
4642     case DW_OP_breg23:
4643       return "DW_OP_breg23";
4644     case DW_OP_breg24:
4645       return "DW_OP_breg24";
4646     case DW_OP_breg25:
4647       return "DW_OP_breg25";
4648     case DW_OP_breg26:
4649       return "DW_OP_breg26";
4650     case DW_OP_breg27:
4651       return "DW_OP_breg27";
4652     case DW_OP_breg28:
4653       return "DW_OP_breg28";
4654     case DW_OP_breg29:
4655       return "DW_OP_breg29";
4656     case DW_OP_breg30:
4657       return "DW_OP_breg30";
4658     case DW_OP_breg31:
4659       return "DW_OP_breg31";
4660     case DW_OP_regx:
4661       return "DW_OP_regx";
4662     case DW_OP_fbreg:
4663       return "DW_OP_fbreg";
4664     case DW_OP_bregx:
4665       return "DW_OP_bregx";
4666     case DW_OP_piece:
4667       return "DW_OP_piece";
4668     case DW_OP_deref_size:
4669       return "DW_OP_deref_size";
4670     case DW_OP_xderef_size:
4671       return "DW_OP_xderef_size";
4672     case DW_OP_nop:
4673       return "DW_OP_nop";
4674
4675     case DW_OP_push_object_address:
4676       return "DW_OP_push_object_address";
4677     case DW_OP_call2:
4678       return "DW_OP_call2";
4679     case DW_OP_call4:
4680       return "DW_OP_call4";
4681     case DW_OP_call_ref:
4682       return "DW_OP_call_ref";
4683     case DW_OP_implicit_value:
4684       return "DW_OP_implicit_value";
4685     case DW_OP_stack_value:
4686       return "DW_OP_stack_value";
4687     case DW_OP_form_tls_address:
4688       return "DW_OP_form_tls_address";
4689     case DW_OP_call_frame_cfa:
4690       return "DW_OP_call_frame_cfa";
4691     case DW_OP_bit_piece:
4692       return "DW_OP_bit_piece";
4693
4694     case DW_OP_GNU_push_tls_address:
4695       return "DW_OP_GNU_push_tls_address";
4696     case DW_OP_GNU_uninit:
4697       return "DW_OP_GNU_uninit";
4698     case DW_OP_GNU_encoded_addr:
4699       return "DW_OP_GNU_encoded_addr";
4700     case DW_OP_GNU_implicit_pointer:
4701       return "DW_OP_GNU_implicit_pointer";
4702
4703     default:
4704       return "OP_<unknown>";
4705     }
4706 }
4707
4708 /* Return a pointer to a newly allocated location description.  Location
4709    descriptions are simple expression terms that can be strung
4710    together to form more complicated location (address) descriptions.  */
4711
4712 static inline dw_loc_descr_ref
4713 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4714                unsigned HOST_WIDE_INT oprnd2)
4715 {
4716   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4717
4718   descr->dw_loc_opc = op;
4719   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4720   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4721   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4722   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4723
4724   return descr;
4725 }
4726
4727 /* Return a pointer to a newly allocated location description for
4728    REG and OFFSET.  */
4729
4730 static inline dw_loc_descr_ref
4731 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4732 {
4733   if (reg <= 31)
4734     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4735                           offset, 0);
4736   else
4737     return new_loc_descr (DW_OP_bregx, reg, offset);
4738 }
4739
4740 /* Add a location description term to a location description expression.  */
4741
4742 static inline void
4743 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4744 {
4745   dw_loc_descr_ref *d;
4746
4747   /* Find the end of the chain.  */
4748   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4749     ;
4750
4751   *d = descr;
4752 }
4753
4754 /* Add a constant OFFSET to a location expression.  */
4755
4756 static void
4757 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4758 {
4759   dw_loc_descr_ref loc;
4760   HOST_WIDE_INT *p;
4761
4762   gcc_assert (*list_head != NULL);
4763
4764   if (!offset)
4765     return;
4766
4767   /* Find the end of the chain.  */
4768   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4769     ;
4770
4771   p = NULL;
4772   if (loc->dw_loc_opc == DW_OP_fbreg
4773       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4774     p = &loc->dw_loc_oprnd1.v.val_int;
4775   else if (loc->dw_loc_opc == DW_OP_bregx)
4776     p = &loc->dw_loc_oprnd2.v.val_int;
4777
4778   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4779      offset.  Don't optimize if an signed integer overflow would happen.  */
4780   if (p != NULL
4781       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4782           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4783     *p += offset;
4784
4785   else if (offset > 0)
4786     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4787
4788   else
4789     {
4790       loc->dw_loc_next = int_loc_descriptor (-offset);
4791       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4792     }
4793 }
4794
4795 /* Add a constant OFFSET to a location list.  */
4796
4797 static void
4798 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4799 {
4800   dw_loc_list_ref d;
4801   for (d = list_head; d != NULL; d = d->dw_loc_next)
4802     loc_descr_plus_const (&d->expr, offset);
4803 }
4804
4805 #define DWARF_REF_SIZE  \
4806   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4807
4808 /* Return the size of a location descriptor.  */
4809
4810 static unsigned long
4811 size_of_loc_descr (dw_loc_descr_ref loc)
4812 {
4813   unsigned long size = 1;
4814
4815   switch (loc->dw_loc_opc)
4816     {
4817     case DW_OP_addr:
4818       size += DWARF2_ADDR_SIZE;
4819       break;
4820     case DW_OP_const1u:
4821     case DW_OP_const1s:
4822       size += 1;
4823       break;
4824     case DW_OP_const2u:
4825     case DW_OP_const2s:
4826       size += 2;
4827       break;
4828     case DW_OP_const4u:
4829     case DW_OP_const4s:
4830       size += 4;
4831       break;
4832     case DW_OP_const8u:
4833     case DW_OP_const8s:
4834       size += 8;
4835       break;
4836     case DW_OP_constu:
4837       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4838       break;
4839     case DW_OP_consts:
4840       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4841       break;
4842     case DW_OP_pick:
4843       size += 1;
4844       break;
4845     case DW_OP_plus_uconst:
4846       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4847       break;
4848     case DW_OP_skip:
4849     case DW_OP_bra:
4850       size += 2;
4851       break;
4852     case DW_OP_breg0:
4853     case DW_OP_breg1:
4854     case DW_OP_breg2:
4855     case DW_OP_breg3:
4856     case DW_OP_breg4:
4857     case DW_OP_breg5:
4858     case DW_OP_breg6:
4859     case DW_OP_breg7:
4860     case DW_OP_breg8:
4861     case DW_OP_breg9:
4862     case DW_OP_breg10:
4863     case DW_OP_breg11:
4864     case DW_OP_breg12:
4865     case DW_OP_breg13:
4866     case DW_OP_breg14:
4867     case DW_OP_breg15:
4868     case DW_OP_breg16:
4869     case DW_OP_breg17:
4870     case DW_OP_breg18:
4871     case DW_OP_breg19:
4872     case DW_OP_breg20:
4873     case DW_OP_breg21:
4874     case DW_OP_breg22:
4875     case DW_OP_breg23:
4876     case DW_OP_breg24:
4877     case DW_OP_breg25:
4878     case DW_OP_breg26:
4879     case DW_OP_breg27:
4880     case DW_OP_breg28:
4881     case DW_OP_breg29:
4882     case DW_OP_breg30:
4883     case DW_OP_breg31:
4884       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4885       break;
4886     case DW_OP_regx:
4887       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4888       break;
4889     case DW_OP_fbreg:
4890       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4891       break;
4892     case DW_OP_bregx:
4893       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4894       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4895       break;
4896     case DW_OP_piece:
4897       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4898       break;
4899     case DW_OP_bit_piece:
4900       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4901       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4902       break;
4903     case DW_OP_deref_size:
4904     case DW_OP_xderef_size:
4905       size += 1;
4906       break;
4907     case DW_OP_call2:
4908       size += 2;
4909       break;
4910     case DW_OP_call4:
4911       size += 4;
4912       break;
4913     case DW_OP_call_ref:
4914       size += DWARF_REF_SIZE;
4915       break;
4916     case DW_OP_implicit_value:
4917       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4918               + loc->dw_loc_oprnd1.v.val_unsigned;
4919       break;
4920     case DW_OP_GNU_implicit_pointer:
4921       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4922       break;
4923     default:
4924       break;
4925     }
4926
4927   return size;
4928 }
4929
4930 /* Return the size of a series of location descriptors.  */
4931
4932 static unsigned long
4933 size_of_locs (dw_loc_descr_ref loc)
4934 {
4935   dw_loc_descr_ref l;
4936   unsigned long size;
4937
4938   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4939      field, to avoid writing to a PCH file.  */
4940   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4941     {
4942       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4943         break;
4944       size += size_of_loc_descr (l);
4945     }
4946   if (! l)
4947     return size;
4948
4949   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4950     {
4951       l->dw_loc_addr = size;
4952       size += size_of_loc_descr (l);
4953     }
4954
4955   return size;
4956 }
4957
4958 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4959 static void get_ref_die_offset_label (char *, dw_die_ref);
4960
4961 /* Output location description stack opcode's operands (if any).  */
4962
4963 static void
4964 output_loc_operands (dw_loc_descr_ref loc)
4965 {
4966   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4967   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4968
4969   switch (loc->dw_loc_opc)
4970     {
4971 #ifdef DWARF2_DEBUGGING_INFO
4972     case DW_OP_const2u:
4973     case DW_OP_const2s:
4974       dw2_asm_output_data (2, val1->v.val_int, NULL);
4975       break;
4976     case DW_OP_const4u:
4977       if (loc->dtprel)
4978         {
4979           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4980           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4981                                                val1->v.val_addr);
4982           fputc ('\n', asm_out_file);
4983           break;
4984         }
4985       /* FALLTHRU */
4986     case DW_OP_const4s:
4987       dw2_asm_output_data (4, val1->v.val_int, NULL);
4988       break;
4989     case DW_OP_const8u:
4990       if (loc->dtprel)
4991         {
4992           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4993           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
4994                                                val1->v.val_addr);
4995           fputc ('\n', asm_out_file);
4996           break;
4997         }
4998       /* FALLTHRU */
4999     case DW_OP_const8s:
5000       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5001       dw2_asm_output_data (8, val1->v.val_int, NULL);
5002       break;
5003     case DW_OP_skip:
5004     case DW_OP_bra:
5005       {
5006         int offset;
5007
5008         gcc_assert (val1->val_class == dw_val_class_loc);
5009         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5010
5011         dw2_asm_output_data (2, offset, NULL);
5012       }
5013       break;
5014     case DW_OP_implicit_value:
5015       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5016       switch (val2->val_class)
5017         {
5018         case dw_val_class_const:
5019           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5020           break;
5021         case dw_val_class_vec:
5022           {
5023             unsigned int elt_size = val2->v.val_vec.elt_size;
5024             unsigned int len = val2->v.val_vec.length;
5025             unsigned int i;
5026             unsigned char *p;
5027
5028             if (elt_size > sizeof (HOST_WIDE_INT))
5029               {
5030                 elt_size /= 2;
5031                 len *= 2;
5032               }
5033             for (i = 0, p = val2->v.val_vec.array;
5034                  i < len;
5035                  i++, p += elt_size)
5036               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5037                                    "fp or vector constant word %u", i);
5038           }
5039           break;
5040         case dw_val_class_const_double:
5041           {
5042             unsigned HOST_WIDE_INT first, second;
5043
5044             if (WORDS_BIG_ENDIAN)
5045               {
5046                 first = val2->v.val_double.high;
5047                 second = val2->v.val_double.low;
5048               }
5049             else
5050               {
5051                 first = val2->v.val_double.low;
5052                 second = val2->v.val_double.high;
5053               }
5054             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5055                                  first, NULL);
5056             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5057                                  second, NULL);
5058           }
5059           break;
5060         case dw_val_class_addr:
5061           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5062           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5063           break;
5064         default:
5065           gcc_unreachable ();
5066         }
5067       break;
5068 #else
5069     case DW_OP_const2u:
5070     case DW_OP_const2s:
5071     case DW_OP_const4u:
5072     case DW_OP_const4s:
5073     case DW_OP_const8u:
5074     case DW_OP_const8s:
5075     case DW_OP_skip:
5076     case DW_OP_bra:
5077     case DW_OP_implicit_value:
5078       /* We currently don't make any attempt to make sure these are
5079          aligned properly like we do for the main unwind info, so
5080          don't support emitting things larger than a byte if we're
5081          only doing unwinding.  */
5082       gcc_unreachable ();
5083 #endif
5084     case DW_OP_const1u:
5085     case DW_OP_const1s:
5086       dw2_asm_output_data (1, val1->v.val_int, NULL);
5087       break;
5088     case DW_OP_constu:
5089       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5090       break;
5091     case DW_OP_consts:
5092       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5093       break;
5094     case DW_OP_pick:
5095       dw2_asm_output_data (1, val1->v.val_int, NULL);
5096       break;
5097     case DW_OP_plus_uconst:
5098       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5099       break;
5100     case DW_OP_breg0:
5101     case DW_OP_breg1:
5102     case DW_OP_breg2:
5103     case DW_OP_breg3:
5104     case DW_OP_breg4:
5105     case DW_OP_breg5:
5106     case DW_OP_breg6:
5107     case DW_OP_breg7:
5108     case DW_OP_breg8:
5109     case DW_OP_breg9:
5110     case DW_OP_breg10:
5111     case DW_OP_breg11:
5112     case DW_OP_breg12:
5113     case DW_OP_breg13:
5114     case DW_OP_breg14:
5115     case DW_OP_breg15:
5116     case DW_OP_breg16:
5117     case DW_OP_breg17:
5118     case DW_OP_breg18:
5119     case DW_OP_breg19:
5120     case DW_OP_breg20:
5121     case DW_OP_breg21:
5122     case DW_OP_breg22:
5123     case DW_OP_breg23:
5124     case DW_OP_breg24:
5125     case DW_OP_breg25:
5126     case DW_OP_breg26:
5127     case DW_OP_breg27:
5128     case DW_OP_breg28:
5129     case DW_OP_breg29:
5130     case DW_OP_breg30:
5131     case DW_OP_breg31:
5132       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5133       break;
5134     case DW_OP_regx:
5135       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5136       break;
5137     case DW_OP_fbreg:
5138       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5139       break;
5140     case DW_OP_bregx:
5141       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5142       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5143       break;
5144     case DW_OP_piece:
5145       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5146       break;
5147     case DW_OP_bit_piece:
5148       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5149       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5150       break;
5151     case DW_OP_deref_size:
5152     case DW_OP_xderef_size:
5153       dw2_asm_output_data (1, val1->v.val_int, NULL);
5154       break;
5155
5156     case DW_OP_addr:
5157       if (loc->dtprel)
5158         {
5159           if (targetm.asm_out.output_dwarf_dtprel)
5160             {
5161               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5162                                                    DWARF2_ADDR_SIZE,
5163                                                    val1->v.val_addr);
5164               fputc ('\n', asm_out_file);
5165             }
5166           else
5167             gcc_unreachable ();
5168         }
5169       else
5170         {
5171 #ifdef DWARF2_DEBUGGING_INFO
5172           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5173 #else
5174           gcc_unreachable ();
5175 #endif
5176         }
5177       break;
5178
5179     case DW_OP_GNU_implicit_pointer:
5180       {
5181         char label[MAX_ARTIFICIAL_LABEL_BYTES
5182                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5183         gcc_assert (val1->val_class == dw_val_class_die_ref);
5184         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5185         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5186         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5187       }
5188       break;
5189
5190     default:
5191       /* Other codes have no operands.  */
5192       break;
5193     }
5194 }
5195
5196 /* Output a sequence of location operations.  */
5197
5198 static void
5199 output_loc_sequence (dw_loc_descr_ref loc)
5200 {
5201   for (; loc != NULL; loc = loc->dw_loc_next)
5202     {
5203       /* Output the opcode.  */
5204       dw2_asm_output_data (1, loc->dw_loc_opc,
5205                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5206
5207       /* Output the operand(s) (if any).  */
5208       output_loc_operands (loc);
5209     }
5210 }
5211
5212 /* Output location description stack opcode's operands (if any).
5213    The output is single bytes on a line, suitable for .cfi_escape.  */
5214
5215 static void
5216 output_loc_operands_raw (dw_loc_descr_ref loc)
5217 {
5218   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5219   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5220
5221   switch (loc->dw_loc_opc)
5222     {
5223     case DW_OP_addr:
5224     case DW_OP_implicit_value:
5225       /* We cannot output addresses in .cfi_escape, only bytes.  */
5226       gcc_unreachable ();
5227
5228     case DW_OP_const1u:
5229     case DW_OP_const1s:
5230     case DW_OP_pick:
5231     case DW_OP_deref_size:
5232     case DW_OP_xderef_size:
5233       fputc (',', asm_out_file);
5234       dw2_asm_output_data_raw (1, val1->v.val_int);
5235       break;
5236
5237     case DW_OP_const2u:
5238     case DW_OP_const2s:
5239       fputc (',', asm_out_file);
5240       dw2_asm_output_data_raw (2, val1->v.val_int);
5241       break;
5242
5243     case DW_OP_const4u:
5244     case DW_OP_const4s:
5245       fputc (',', asm_out_file);
5246       dw2_asm_output_data_raw (4, val1->v.val_int);
5247       break;
5248
5249     case DW_OP_const8u:
5250     case DW_OP_const8s:
5251       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5252       fputc (',', asm_out_file);
5253       dw2_asm_output_data_raw (8, val1->v.val_int);
5254       break;
5255
5256     case DW_OP_skip:
5257     case DW_OP_bra:
5258       {
5259         int offset;
5260
5261         gcc_assert (val1->val_class == dw_val_class_loc);
5262         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5263
5264         fputc (',', asm_out_file);
5265         dw2_asm_output_data_raw (2, offset);
5266       }
5267       break;
5268
5269     case DW_OP_constu:
5270     case DW_OP_plus_uconst:
5271     case DW_OP_regx:
5272     case DW_OP_piece:
5273       fputc (',', asm_out_file);
5274       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5275       break;
5276
5277     case DW_OP_bit_piece:
5278       fputc (',', asm_out_file);
5279       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5280       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5281       break;
5282
5283     case DW_OP_consts:
5284     case DW_OP_breg0:
5285     case DW_OP_breg1:
5286     case DW_OP_breg2:
5287     case DW_OP_breg3:
5288     case DW_OP_breg4:
5289     case DW_OP_breg5:
5290     case DW_OP_breg6:
5291     case DW_OP_breg7:
5292     case DW_OP_breg8:
5293     case DW_OP_breg9:
5294     case DW_OP_breg10:
5295     case DW_OP_breg11:
5296     case DW_OP_breg12:
5297     case DW_OP_breg13:
5298     case DW_OP_breg14:
5299     case DW_OP_breg15:
5300     case DW_OP_breg16:
5301     case DW_OP_breg17:
5302     case DW_OP_breg18:
5303     case DW_OP_breg19:
5304     case DW_OP_breg20:
5305     case DW_OP_breg21:
5306     case DW_OP_breg22:
5307     case DW_OP_breg23:
5308     case DW_OP_breg24:
5309     case DW_OP_breg25:
5310     case DW_OP_breg26:
5311     case DW_OP_breg27:
5312     case DW_OP_breg28:
5313     case DW_OP_breg29:
5314     case DW_OP_breg30:
5315     case DW_OP_breg31:
5316     case DW_OP_fbreg:
5317       fputc (',', asm_out_file);
5318       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5319       break;
5320
5321     case DW_OP_bregx:
5322       fputc (',', asm_out_file);
5323       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5324       fputc (',', asm_out_file);
5325       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5326       break;
5327
5328     case DW_OP_GNU_implicit_pointer:
5329       gcc_unreachable ();
5330       break;
5331
5332     default:
5333       /* Other codes have no operands.  */
5334       break;
5335     }
5336 }
5337
5338 static void
5339 output_loc_sequence_raw (dw_loc_descr_ref loc)
5340 {
5341   while (1)
5342     {
5343       /* Output the opcode.  */
5344       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5345       output_loc_operands_raw (loc);
5346
5347       if (!loc->dw_loc_next)
5348         break;
5349       loc = loc->dw_loc_next;
5350
5351       fputc (',', asm_out_file);
5352     }
5353 }
5354
5355 /* This routine will generate the correct assembly data for a location
5356    description based on a cfi entry with a complex address.  */
5357
5358 static void
5359 output_cfa_loc (dw_cfi_ref cfi)
5360 {
5361   dw_loc_descr_ref loc;
5362   unsigned long size;
5363
5364   if (cfi->dw_cfi_opc == DW_CFA_expression)
5365     {
5366       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5367       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5368     }
5369   else
5370     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5371
5372   /* Output the size of the block.  */
5373   size = size_of_locs (loc);
5374   dw2_asm_output_data_uleb128 (size, NULL);
5375
5376   /* Now output the operations themselves.  */
5377   output_loc_sequence (loc);
5378 }
5379
5380 /* Similar, but used for .cfi_escape.  */
5381
5382 static void
5383 output_cfa_loc_raw (dw_cfi_ref cfi)
5384 {
5385   dw_loc_descr_ref loc;
5386   unsigned long size;
5387
5388   if (cfi->dw_cfi_opc == DW_CFA_expression)
5389     {
5390       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5391       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5392     }
5393   else
5394     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5395
5396   /* Output the size of the block.  */
5397   size = size_of_locs (loc);
5398   dw2_asm_output_data_uleb128_raw (size);
5399   fputc (',', asm_out_file);
5400
5401   /* Now output the operations themselves.  */
5402   output_loc_sequence_raw (loc);
5403 }
5404
5405 /* This function builds a dwarf location descriptor sequence from a
5406    dw_cfa_location, adding the given OFFSET to the result of the
5407    expression.  */
5408
5409 static struct dw_loc_descr_struct *
5410 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5411 {
5412   struct dw_loc_descr_struct *head, *tmp;
5413
5414   offset += cfa->offset;
5415
5416   if (cfa->indirect)
5417     {
5418       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5419       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5420       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5421       add_loc_descr (&head, tmp);
5422       if (offset != 0)
5423         {
5424           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5425           add_loc_descr (&head, tmp);
5426         }
5427     }
5428   else
5429     head = new_reg_loc_descr (cfa->reg, offset);
5430
5431   return head;
5432 }
5433
5434 /* This function builds a dwarf location descriptor sequence for
5435    the address at OFFSET from the CFA when stack is aligned to
5436    ALIGNMENT byte.  */
5437
5438 static struct dw_loc_descr_struct *
5439 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5440 {
5441   struct dw_loc_descr_struct *head;
5442   unsigned int dwarf_fp
5443     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5444
5445  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5446   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5447     {
5448       head = new_reg_loc_descr (dwarf_fp, 0);
5449       add_loc_descr (&head, int_loc_descriptor (alignment));
5450       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5451       loc_descr_plus_const (&head, offset);
5452     }
5453   else
5454     head = new_reg_loc_descr (dwarf_fp, offset);
5455   return head;
5456 }
5457
5458 /* This function fills in aa dw_cfa_location structure from a dwarf location
5459    descriptor sequence.  */
5460
5461 static void
5462 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5463 {
5464   struct dw_loc_descr_struct *ptr;
5465   cfa->offset = 0;
5466   cfa->base_offset = 0;
5467   cfa->indirect = 0;
5468   cfa->reg = -1;
5469
5470   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5471     {
5472       enum dwarf_location_atom op = ptr->dw_loc_opc;
5473
5474       switch (op)
5475         {
5476         case DW_OP_reg0:
5477         case DW_OP_reg1:
5478         case DW_OP_reg2:
5479         case DW_OP_reg3:
5480         case DW_OP_reg4:
5481         case DW_OP_reg5:
5482         case DW_OP_reg6:
5483         case DW_OP_reg7:
5484         case DW_OP_reg8:
5485         case DW_OP_reg9:
5486         case DW_OP_reg10:
5487         case DW_OP_reg11:
5488         case DW_OP_reg12:
5489         case DW_OP_reg13:
5490         case DW_OP_reg14:
5491         case DW_OP_reg15:
5492         case DW_OP_reg16:
5493         case DW_OP_reg17:
5494         case DW_OP_reg18:
5495         case DW_OP_reg19:
5496         case DW_OP_reg20:
5497         case DW_OP_reg21:
5498         case DW_OP_reg22:
5499         case DW_OP_reg23:
5500         case DW_OP_reg24:
5501         case DW_OP_reg25:
5502         case DW_OP_reg26:
5503         case DW_OP_reg27:
5504         case DW_OP_reg28:
5505         case DW_OP_reg29:
5506         case DW_OP_reg30:
5507         case DW_OP_reg31:
5508           cfa->reg = op - DW_OP_reg0;
5509           break;
5510         case DW_OP_regx:
5511           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5512           break;
5513         case DW_OP_breg0:
5514         case DW_OP_breg1:
5515         case DW_OP_breg2:
5516         case DW_OP_breg3:
5517         case DW_OP_breg4:
5518         case DW_OP_breg5:
5519         case DW_OP_breg6:
5520         case DW_OP_breg7:
5521         case DW_OP_breg8:
5522         case DW_OP_breg9:
5523         case DW_OP_breg10:
5524         case DW_OP_breg11:
5525         case DW_OP_breg12:
5526         case DW_OP_breg13:
5527         case DW_OP_breg14:
5528         case DW_OP_breg15:
5529         case DW_OP_breg16:
5530         case DW_OP_breg17:
5531         case DW_OP_breg18:
5532         case DW_OP_breg19:
5533         case DW_OP_breg20:
5534         case DW_OP_breg21:
5535         case DW_OP_breg22:
5536         case DW_OP_breg23:
5537         case DW_OP_breg24:
5538         case DW_OP_breg25:
5539         case DW_OP_breg26:
5540         case DW_OP_breg27:
5541         case DW_OP_breg28:
5542         case DW_OP_breg29:
5543         case DW_OP_breg30:
5544         case DW_OP_breg31:
5545           cfa->reg = op - DW_OP_breg0;
5546           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5547           break;
5548         case DW_OP_bregx:
5549           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5550           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5551           break;
5552         case DW_OP_deref:
5553           cfa->indirect = 1;
5554           break;
5555         case DW_OP_plus_uconst:
5556           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5557           break;
5558         default:
5559           internal_error ("DW_LOC_OP %s not implemented",
5560                           dwarf_stack_op_name (ptr->dw_loc_opc));
5561         }
5562     }
5563 }
5564 \f
5565 /* And now, the support for symbolic debugging information.  */
5566
5567 /* .debug_str support.  */
5568 static int output_indirect_string (void **, void *);
5569
5570 static void dwarf2out_init (const char *);
5571 static void dwarf2out_finish (const char *);
5572 static void dwarf2out_assembly_start (void);
5573 static void dwarf2out_define (unsigned int, const char *);
5574 static void dwarf2out_undef (unsigned int, const char *);
5575 static void dwarf2out_start_source_file (unsigned, const char *);
5576 static void dwarf2out_end_source_file (unsigned);
5577 static void dwarf2out_function_decl (tree);
5578 static void dwarf2out_begin_block (unsigned, unsigned);
5579 static void dwarf2out_end_block (unsigned, unsigned);
5580 static bool dwarf2out_ignore_block (const_tree);
5581 static void dwarf2out_global_decl (tree);
5582 static void dwarf2out_type_decl (tree, int);
5583 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5584 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5585                                                  dw_die_ref);
5586 static void dwarf2out_abstract_function (tree);
5587 static void dwarf2out_var_location (rtx);
5588 static void dwarf2out_direct_call (tree);
5589 static void dwarf2out_virtual_call_token (tree, int);
5590 static void dwarf2out_copy_call_info (rtx, rtx);
5591 static void dwarf2out_virtual_call (int);
5592 static void dwarf2out_begin_function (tree);
5593 static void dwarf2out_set_name (tree, tree);
5594
5595 /* The debug hooks structure.  */
5596
5597 const struct gcc_debug_hooks dwarf2_debug_hooks =
5598 {
5599   dwarf2out_init,
5600   dwarf2out_finish,
5601   dwarf2out_assembly_start,
5602   dwarf2out_define,
5603   dwarf2out_undef,
5604   dwarf2out_start_source_file,
5605   dwarf2out_end_source_file,
5606   dwarf2out_begin_block,
5607   dwarf2out_end_block,
5608   dwarf2out_ignore_block,
5609   dwarf2out_source_line,
5610   dwarf2out_begin_prologue,
5611 #if VMS_DEBUGGING_INFO
5612   dwarf2out_vms_end_prologue,
5613   dwarf2out_vms_begin_epilogue,
5614 #else
5615   debug_nothing_int_charstar,
5616   debug_nothing_int_charstar,
5617 #endif
5618   dwarf2out_end_epilogue,
5619   dwarf2out_begin_function,
5620   debug_nothing_int,            /* end_function */
5621   dwarf2out_function_decl,      /* function_decl */
5622   dwarf2out_global_decl,
5623   dwarf2out_type_decl,          /* type_decl */
5624   dwarf2out_imported_module_or_decl,
5625   debug_nothing_tree,           /* deferred_inline_function */
5626   /* The DWARF 2 backend tries to reduce debugging bloat by not
5627      emitting the abstract description of inline functions until
5628      something tries to reference them.  */
5629   dwarf2out_abstract_function,  /* outlining_inline_function */
5630   debug_nothing_rtx,            /* label */
5631   debug_nothing_int,            /* handle_pch */
5632   dwarf2out_var_location,
5633   dwarf2out_switch_text_section,
5634   dwarf2out_direct_call,
5635   dwarf2out_virtual_call_token,
5636   dwarf2out_copy_call_info,
5637   dwarf2out_virtual_call,
5638   dwarf2out_set_name,
5639   1                             /* start_end_main_source_file */
5640 };
5641 \f
5642 /* NOTE: In the comments in this file, many references are made to
5643    "Debugging Information Entries".  This term is abbreviated as `DIE'
5644    throughout the remainder of this file.  */
5645
5646 /* An internal representation of the DWARF output is built, and then
5647    walked to generate the DWARF debugging info.  The walk of the internal
5648    representation is done after the entire program has been compiled.
5649    The types below are used to describe the internal representation.  */
5650
5651 /* Various DIE's use offsets relative to the beginning of the
5652    .debug_info section to refer to each other.  */
5653
5654 typedef long int dw_offset;
5655
5656 /* Define typedefs here to avoid circular dependencies.  */
5657
5658 typedef struct dw_attr_struct *dw_attr_ref;
5659 typedef struct dw_line_info_struct *dw_line_info_ref;
5660 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5661 typedef struct pubname_struct *pubname_ref;
5662 typedef struct dw_ranges_struct *dw_ranges_ref;
5663 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5664 typedef struct comdat_type_struct *comdat_type_node_ref;
5665
5666 /* Each entry in the line_info_table maintains the file and
5667    line number associated with the label generated for that
5668    entry.  The label gives the PC value associated with
5669    the line number entry.  */
5670
5671 typedef struct GTY(()) dw_line_info_struct {
5672   unsigned long dw_file_num;
5673   unsigned long dw_line_num;
5674 }
5675 dw_line_info_entry;
5676
5677 /* Line information for functions in separate sections; each one gets its
5678    own sequence.  */
5679 typedef struct GTY(()) dw_separate_line_info_struct {
5680   unsigned long dw_file_num;
5681   unsigned long dw_line_num;
5682   unsigned long function;
5683 }
5684 dw_separate_line_info_entry;
5685
5686 /* Each DIE attribute has a field specifying the attribute kind,
5687    a link to the next attribute in the chain, and an attribute value.
5688    Attributes are typically linked below the DIE they modify.  */
5689
5690 typedef struct GTY(()) dw_attr_struct {
5691   enum dwarf_attribute dw_attr;
5692   dw_val_node dw_attr_val;
5693 }
5694 dw_attr_node;
5695
5696 DEF_VEC_O(dw_attr_node);
5697 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5698
5699 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5700    The children of each node form a circular list linked by
5701    die_sib.  die_child points to the node *before* the "first" child node.  */
5702
5703 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5704   union die_symbol_or_type_node
5705     {
5706       char * GTY ((tag ("0"))) die_symbol;
5707       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5708     }
5709   GTY ((desc ("dwarf_version >= 4"))) die_id;
5710   VEC(dw_attr_node,gc) * die_attr;
5711   dw_die_ref die_parent;
5712   dw_die_ref die_child;
5713   dw_die_ref die_sib;
5714   dw_die_ref die_definition; /* ref from a specification to its definition */
5715   dw_offset die_offset;
5716   unsigned long die_abbrev;
5717   int die_mark;
5718   /* Die is used and must not be pruned as unused.  */
5719   int die_perennial_p;
5720   unsigned int decl_id;
5721   enum dwarf_tag die_tag;
5722 }
5723 die_node;
5724
5725 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5726 #define FOR_EACH_CHILD(die, c, expr) do {       \
5727   c = die->die_child;                           \
5728   if (c) do {                                   \
5729     c = c->die_sib;                             \
5730     expr;                                       \
5731   } while (c != die->die_child);                \
5732 } while (0)
5733
5734 /* The pubname structure */
5735
5736 typedef struct GTY(()) pubname_struct {
5737   dw_die_ref die;
5738   const char *name;
5739 }
5740 pubname_entry;
5741
5742 DEF_VEC_O(pubname_entry);
5743 DEF_VEC_ALLOC_O(pubname_entry, gc);
5744
5745 struct GTY(()) dw_ranges_struct {
5746   /* If this is positive, it's a block number, otherwise it's a
5747      bitwise-negated index into dw_ranges_by_label.  */
5748   int num;
5749 };
5750
5751 struct GTY(()) dw_ranges_by_label_struct {
5752   const char *begin;
5753   const char *end;
5754 };
5755
5756 /* The comdat type node structure.  */
5757 typedef struct GTY(()) comdat_type_struct
5758 {
5759   dw_die_ref root_die;
5760   dw_die_ref type_die;
5761   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5762   struct comdat_type_struct *next;
5763 }
5764 comdat_type_node;
5765
5766 /* The limbo die list structure.  */
5767 typedef struct GTY(()) limbo_die_struct {
5768   dw_die_ref die;
5769   tree created_for;
5770   struct limbo_die_struct *next;
5771 }
5772 limbo_die_node;
5773
5774 typedef struct GTY(()) skeleton_chain_struct
5775 {
5776   dw_die_ref old_die;
5777   dw_die_ref new_die;
5778   struct skeleton_chain_struct *parent;
5779 }
5780 skeleton_chain_node;
5781
5782 /* How to start an assembler comment.  */
5783 #ifndef ASM_COMMENT_START
5784 #define ASM_COMMENT_START ";#"
5785 #endif
5786
5787 /* Define a macro which returns nonzero for a TYPE_DECL which was
5788    implicitly generated for a tagged type.
5789
5790    Note that unlike the gcc front end (which generates a NULL named
5791    TYPE_DECL node for each complete tagged type, each array type, and
5792    each function type node created) the g++ front end generates a
5793    _named_ TYPE_DECL node for each tagged type node created.
5794    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5795    generate a DW_TAG_typedef DIE for them.  */
5796
5797 #define TYPE_DECL_IS_STUB(decl)                         \
5798   (DECL_NAME (decl) == NULL_TREE                        \
5799    || (DECL_ARTIFICIAL (decl)                           \
5800        && is_tagged_type (TREE_TYPE (decl))             \
5801        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5802            /* This is necessary for stub decls that     \
5803               appear in nested inline functions.  */    \
5804            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5805                && (decl_ultimate_origin (decl)          \
5806                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5807
5808 /* Information concerning the compilation unit's programming
5809    language, and compiler version.  */
5810
5811 /* Fixed size portion of the DWARF compilation unit header.  */
5812 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5813   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5814
5815 /* Fixed size portion of the DWARF comdat type unit header.  */
5816 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5817   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5818    + DWARF_OFFSET_SIZE)
5819
5820 /* Fixed size portion of public names info.  */
5821 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5822
5823 /* Fixed size portion of the address range info.  */
5824 #define DWARF_ARANGES_HEADER_SIZE                                       \
5825   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5826                 DWARF2_ADDR_SIZE * 2)                                   \
5827    - DWARF_INITIAL_LENGTH_SIZE)
5828
5829 /* Size of padding portion in the address range info.  It must be
5830    aligned to twice the pointer size.  */
5831 #define DWARF_ARANGES_PAD_SIZE \
5832   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5833                 DWARF2_ADDR_SIZE * 2)                              \
5834    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5835
5836 /* Use assembler line directives if available.  */
5837 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5838 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5839 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5840 #else
5841 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5842 #endif
5843 #endif
5844
5845 /* Minimum line offset in a special line info. opcode.
5846    This value was chosen to give a reasonable range of values.  */
5847 #define DWARF_LINE_BASE  -10
5848
5849 /* First special line opcode - leave room for the standard opcodes.  */
5850 #define DWARF_LINE_OPCODE_BASE  10
5851
5852 /* Range of line offsets in a special line info. opcode.  */
5853 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5854
5855 /* Flag that indicates the initial value of the is_stmt_start flag.
5856    In the present implementation, we do not mark any lines as
5857    the beginning of a source statement, because that information
5858    is not made available by the GCC front-end.  */
5859 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5860
5861 /* Maximum number of operations per instruction bundle.  */
5862 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5863 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5864 #endif
5865
5866 /* This location is used by calc_die_sizes() to keep track
5867    the offset of each DIE within the .debug_info section.  */
5868 static unsigned long next_die_offset;
5869
5870 /* Record the root of the DIE's built for the current compilation unit.  */
5871 static GTY(()) dw_die_ref comp_unit_die;
5872
5873 /* A list of type DIEs that have been separated into comdat sections.  */
5874 static GTY(()) comdat_type_node *comdat_type_list;
5875
5876 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5877 static GTY(()) limbo_die_node *limbo_die_list;
5878
5879 /* A list of DIEs for which we may have to generate
5880    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5881 static GTY(()) limbo_die_node *deferred_asm_name;
5882
5883 /* Filenames referenced by this compilation unit.  */
5884 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5885
5886 /* A hash table of references to DIE's that describe declarations.
5887    The key is a DECL_UID() which is a unique number identifying each decl.  */
5888 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5889
5890 /* A hash table of references to DIE's that describe COMMON blocks.
5891    The key is DECL_UID() ^ die_parent.  */
5892 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5893
5894 typedef struct GTY(()) die_arg_entry_struct {
5895     dw_die_ref die;
5896     tree arg;
5897 } die_arg_entry;
5898
5899 DEF_VEC_O(die_arg_entry);
5900 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5901
5902 /* Node of the variable location list.  */
5903 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5904   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5905      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5906      in mode of the EXPR_LIST node and first EXPR_LIST operand
5907      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5908      location or NULL for padding.  For larger bitsizes,
5909      mode is 0 and first operand is a CONCAT with bitsize
5910      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5911      NULL as second operand.  */
5912   rtx GTY (()) loc;
5913   const char * GTY (()) label;
5914   struct var_loc_node * GTY (()) next;
5915 };
5916
5917 /* Variable location list.  */
5918 struct GTY (()) var_loc_list_def {
5919   struct var_loc_node * GTY (()) first;
5920
5921   /* Pointer to the last but one or last element of the
5922      chained list.  If the list is empty, both first and
5923      last are NULL, if the list contains just one node
5924      or the last node certainly is not redundant, it points
5925      to the last node, otherwise points to the last but one.
5926      Do not mark it for GC because it is marked through the chain.  */
5927   struct var_loc_node * GTY ((skip ("%h"))) last;
5928
5929   /* DECL_UID of the variable decl.  */
5930   unsigned int decl_id;
5931 };
5932 typedef struct var_loc_list_def var_loc_list;
5933
5934
5935 /* Table of decl location linked lists.  */
5936 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5937
5938 /* A pointer to the base of a list of references to DIE's that
5939    are uniquely identified by their tag, presence/absence of
5940    children DIE's, and list of attribute/value pairs.  */
5941 static GTY((length ("abbrev_die_table_allocated")))
5942   dw_die_ref *abbrev_die_table;
5943
5944 /* Number of elements currently allocated for abbrev_die_table.  */
5945 static GTY(()) unsigned abbrev_die_table_allocated;
5946
5947 /* Number of elements in type_die_table currently in use.  */
5948 static GTY(()) unsigned abbrev_die_table_in_use;
5949
5950 /* Size (in elements) of increments by which we may expand the
5951    abbrev_die_table.  */
5952 #define ABBREV_DIE_TABLE_INCREMENT 256
5953
5954 /* A pointer to the base of a table that contains line information
5955    for each source code line in .text in the compilation unit.  */
5956 static GTY((length ("line_info_table_allocated")))
5957      dw_line_info_ref line_info_table;
5958
5959 /* Number of elements currently allocated for line_info_table.  */
5960 static GTY(()) unsigned line_info_table_allocated;
5961
5962 /* Number of elements in line_info_table currently in use.  */
5963 static GTY(()) unsigned line_info_table_in_use;
5964
5965 /* A pointer to the base of a table that contains line information
5966    for each source code line outside of .text in the compilation unit.  */
5967 static GTY ((length ("separate_line_info_table_allocated")))
5968      dw_separate_line_info_ref separate_line_info_table;
5969
5970 /* Number of elements currently allocated for separate_line_info_table.  */
5971 static GTY(()) unsigned separate_line_info_table_allocated;
5972
5973 /* Number of elements in separate_line_info_table currently in use.  */
5974 static GTY(()) unsigned separate_line_info_table_in_use;
5975
5976 /* Size (in elements) of increments by which we may expand the
5977    line_info_table.  */
5978 #define LINE_INFO_TABLE_INCREMENT 1024
5979
5980 /* A pointer to the base of a table that contains a list of publicly
5981    accessible names.  */
5982 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5983
5984 /* A pointer to the base of a table that contains a list of publicly
5985    accessible types.  */
5986 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5987
5988 /* Array of dies for which we should generate .debug_arange info.  */
5989 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5990
5991 /* Number of elements currently allocated for arange_table.  */
5992 static GTY(()) unsigned arange_table_allocated;
5993
5994 /* Number of elements in arange_table currently in use.  */
5995 static GTY(()) unsigned arange_table_in_use;
5996
5997 /* Size (in elements) of increments by which we may expand the
5998    arange_table.  */
5999 #define ARANGE_TABLE_INCREMENT 64
6000
6001 /* Array of dies for which we should generate .debug_ranges info.  */
6002 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6003
6004 /* Number of elements currently allocated for ranges_table.  */
6005 static GTY(()) unsigned ranges_table_allocated;
6006
6007 /* Number of elements in ranges_table currently in use.  */
6008 static GTY(()) unsigned ranges_table_in_use;
6009
6010 /* Array of pairs of labels referenced in ranges_table.  */
6011 static GTY ((length ("ranges_by_label_allocated")))
6012      dw_ranges_by_label_ref ranges_by_label;
6013
6014 /* Number of elements currently allocated for ranges_by_label.  */
6015 static GTY(()) unsigned ranges_by_label_allocated;
6016
6017 /* Number of elements in ranges_by_label currently in use.  */
6018 static GTY(()) unsigned ranges_by_label_in_use;
6019
6020 /* Size (in elements) of increments by which we may expand the
6021    ranges_table.  */
6022 #define RANGES_TABLE_INCREMENT 64
6023
6024 /* Whether we have location lists that need outputting */
6025 static GTY(()) bool have_location_lists;
6026
6027 /* Unique label counter.  */
6028 static GTY(()) unsigned int loclabel_num;
6029
6030 /* Unique label counter for point-of-call tables.  */
6031 static GTY(()) unsigned int poc_label_num;
6032
6033 /* The direct call table structure.  */
6034
6035 typedef struct GTY(()) dcall_struct {
6036   unsigned int poc_label_num;
6037   tree poc_decl;
6038   dw_die_ref targ_die;
6039 }
6040 dcall_entry;
6041
6042 DEF_VEC_O(dcall_entry);
6043 DEF_VEC_ALLOC_O(dcall_entry, gc);
6044
6045 /* The virtual call table structure.  */
6046
6047 typedef struct GTY(()) vcall_struct {
6048   unsigned int poc_label_num;
6049   unsigned int vtable_slot;
6050 }
6051 vcall_entry;
6052
6053 DEF_VEC_O(vcall_entry);
6054 DEF_VEC_ALLOC_O(vcall_entry, gc);
6055
6056 /* Pointers to the direct and virtual call tables.  */
6057 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6058 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6059
6060 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6061
6062 struct GTY (()) vcall_insn {
6063   int insn_uid;
6064   unsigned int vtable_slot;
6065 };
6066
6067 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6068
6069 /* Record whether the function being analyzed contains inlined functions.  */
6070 static int current_function_has_inlines;
6071
6072 /* The last file entry emitted by maybe_emit_file().  */
6073 static GTY(()) struct dwarf_file_data * last_emitted_file;
6074
6075 /* Number of internal labels generated by gen_internal_sym().  */
6076 static GTY(()) int label_num;
6077
6078 /* Cached result of previous call to lookup_filename.  */
6079 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6080
6081 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6082
6083 /* Offset from the "steady-state frame pointer" to the frame base,
6084    within the current function.  */
6085 static HOST_WIDE_INT frame_pointer_fb_offset;
6086
6087 /* Forward declarations for functions defined in this file.  */
6088
6089 static int is_pseudo_reg (const_rtx);
6090 static tree type_main_variant (tree);
6091 static int is_tagged_type (const_tree);
6092 static const char *dwarf_tag_name (unsigned);
6093 static const char *dwarf_attr_name (unsigned);
6094 static const char *dwarf_form_name (unsigned);
6095 static tree decl_ultimate_origin (const_tree);
6096 static tree decl_class_context (tree);
6097 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6098 static inline enum dw_val_class AT_class (dw_attr_ref);
6099 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6100 static inline unsigned AT_flag (dw_attr_ref);
6101 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6102 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6103 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6104 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6105 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6106                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6107 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6108                                unsigned int, unsigned char *);
6109 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6110 static hashval_t debug_str_do_hash (const void *);
6111 static int debug_str_eq (const void *, const void *);
6112 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6113 static inline const char *AT_string (dw_attr_ref);
6114 static enum dwarf_form AT_string_form (dw_attr_ref);
6115 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6116 static void add_AT_specification (dw_die_ref, dw_die_ref);
6117 static inline dw_die_ref AT_ref (dw_attr_ref);
6118 static inline int AT_ref_external (dw_attr_ref);
6119 static inline void set_AT_ref_external (dw_attr_ref, int);
6120 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6121 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6122 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6123 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6124                              dw_loc_list_ref);
6125 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6126 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6127 static inline rtx AT_addr (dw_attr_ref);
6128 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6129 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6130 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6131 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6132                            unsigned HOST_WIDE_INT);
6133 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6134                                unsigned long);
6135 static inline const char *AT_lbl (dw_attr_ref);
6136 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6137 static const char *get_AT_low_pc (dw_die_ref);
6138 static const char *get_AT_hi_pc (dw_die_ref);
6139 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6140 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6141 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6142 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6143 static bool is_cxx (void);
6144 static bool is_fortran (void);
6145 static bool is_ada (void);
6146 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6147 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6148 static void add_child_die (dw_die_ref, dw_die_ref);
6149 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6150 static dw_die_ref lookup_type_die (tree);
6151 static void equate_type_number_to_die (tree, dw_die_ref);
6152 static hashval_t decl_die_table_hash (const void *);
6153 static int decl_die_table_eq (const void *, const void *);
6154 static dw_die_ref lookup_decl_die (tree);
6155 static hashval_t common_block_die_table_hash (const void *);
6156 static int common_block_die_table_eq (const void *, const void *);
6157 static hashval_t decl_loc_table_hash (const void *);
6158 static int decl_loc_table_eq (const void *, const void *);
6159 static var_loc_list *lookup_decl_loc (const_tree);
6160 static void equate_decl_number_to_die (tree, dw_die_ref);
6161 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6162 static void print_spaces (FILE *);
6163 static void print_die (dw_die_ref, FILE *);
6164 static void print_dwarf_line_table (FILE *);
6165 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6166 static dw_die_ref pop_compile_unit (dw_die_ref);
6167 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6168 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6169 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6170 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6171 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6172 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6173 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6174                                    struct md5_ctx *, int *);
6175 struct checksum_attributes;
6176 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6177 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6178 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6179 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6180 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6181 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6182 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6183 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6184 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6185 static void compute_section_prefix (dw_die_ref);
6186 static int is_type_die (dw_die_ref);
6187 static int is_comdat_die (dw_die_ref);
6188 static int is_symbol_die (dw_die_ref);
6189 static void assign_symbol_names (dw_die_ref);
6190 static void break_out_includes (dw_die_ref);
6191 static int is_declaration_die (dw_die_ref);
6192 static int should_move_die_to_comdat (dw_die_ref);
6193 static dw_die_ref clone_as_declaration (dw_die_ref);
6194 static dw_die_ref clone_die (dw_die_ref);
6195 static dw_die_ref clone_tree (dw_die_ref);
6196 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6197 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6198 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6199 static dw_die_ref generate_skeleton (dw_die_ref);
6200 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6201                                                          dw_die_ref);
6202 static void break_out_comdat_types (dw_die_ref);
6203 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6204 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6205 static void copy_decls_for_unworthy_types (dw_die_ref);
6206
6207 static hashval_t htab_cu_hash (const void *);
6208 static int htab_cu_eq (const void *, const void *);
6209 static void htab_cu_del (void *);
6210 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6211 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6212 static void add_sibling_attributes (dw_die_ref);
6213 static void build_abbrev_table (dw_die_ref);
6214 static void output_location_lists (dw_die_ref);
6215 static int constant_size (unsigned HOST_WIDE_INT);
6216 static unsigned long size_of_die (dw_die_ref);
6217 static void calc_die_sizes (dw_die_ref);
6218 static void mark_dies (dw_die_ref);
6219 static void unmark_dies (dw_die_ref);
6220 static void unmark_all_dies (dw_die_ref);
6221 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6222 static unsigned long size_of_aranges (void);
6223 static enum dwarf_form value_format (dw_attr_ref);
6224 static void output_value_format (dw_attr_ref);
6225 static void output_abbrev_section (void);
6226 static void output_die_symbol (dw_die_ref);
6227 static void output_die (dw_die_ref);
6228 static void output_compilation_unit_header (void);
6229 static void output_comp_unit (dw_die_ref, int);
6230 static void output_comdat_type_unit (comdat_type_node *);
6231 static const char *dwarf2_name (tree, int);
6232 static void add_pubname (tree, dw_die_ref);
6233 static void add_pubname_string (const char *, dw_die_ref);
6234 static void add_pubtype (tree, dw_die_ref);
6235 static void output_pubnames (VEC (pubname_entry,gc) *);
6236 static void add_arange (tree, dw_die_ref);
6237 static void output_aranges (void);
6238 static unsigned int add_ranges_num (int);
6239 static unsigned int add_ranges (const_tree);
6240 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6241                                   bool *);
6242 static void output_ranges (void);
6243 static void output_line_info (void);
6244 static void output_file_names (void);
6245 static dw_die_ref base_type_die (tree);
6246 static int is_base_type (tree);
6247 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6248 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6249 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6250 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6251 static int type_is_enum (const_tree);
6252 static unsigned int dbx_reg_number (const_rtx);
6253 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6254 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6255 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6256                                                 enum var_init_status);
6257 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6258                                                      enum var_init_status);
6259 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6260                                          enum var_init_status);
6261 static int is_based_loc (const_rtx);
6262 static int resolve_one_addr (rtx *, void *);
6263 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6264                                                enum var_init_status);
6265 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6266                                         enum var_init_status);
6267 static dw_loc_list_ref loc_list_from_tree (tree, int);
6268 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6269 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6270 static tree field_type (const_tree);
6271 static unsigned int simple_type_align_in_bits (const_tree);
6272 static unsigned int simple_decl_align_in_bits (const_tree);
6273 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6274 static HOST_WIDE_INT field_byte_offset (const_tree);
6275 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6276                                          dw_loc_list_ref);
6277 static void add_data_member_location_attribute (dw_die_ref, tree);
6278 static bool add_const_value_attribute (dw_die_ref, rtx);
6279 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6280 static void insert_double (double_int, unsigned char *);
6281 static void insert_float (const_rtx, unsigned char *);
6282 static rtx rtl_for_decl_location (tree);
6283 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6284                                                    enum dwarf_attribute);
6285 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6286 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6287 static void add_name_attribute (dw_die_ref, const char *);
6288 static void add_comp_dir_attribute (dw_die_ref);
6289 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6290 static void add_subscript_info (dw_die_ref, tree, bool);
6291 static void add_byte_size_attribute (dw_die_ref, tree);
6292 static void add_bit_offset_attribute (dw_die_ref, tree);
6293 static void add_bit_size_attribute (dw_die_ref, tree);
6294 static void add_prototyped_attribute (dw_die_ref, tree);
6295 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6296 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6297 static void add_src_coords_attributes (dw_die_ref, tree);
6298 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6299 static void push_decl_scope (tree);
6300 static void pop_decl_scope (void);
6301 static dw_die_ref scope_die_for (tree, dw_die_ref);
6302 static inline int local_scope_p (dw_die_ref);
6303 static inline int class_scope_p (dw_die_ref);
6304 static inline int class_or_namespace_scope_p (dw_die_ref);
6305 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6306 static void add_calling_convention_attribute (dw_die_ref, tree);
6307 static const char *type_tag (const_tree);
6308 static tree member_declared_type (const_tree);
6309 #if 0
6310 static const char *decl_start_label (tree);
6311 #endif
6312 static void gen_array_type_die (tree, dw_die_ref);
6313 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6314 #if 0
6315 static void gen_entry_point_die (tree, dw_die_ref);
6316 #endif
6317 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6318 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6319 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6320 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6321 static void gen_formal_types_die (tree, dw_die_ref);
6322 static void gen_subprogram_die (tree, dw_die_ref);
6323 static void gen_variable_die (tree, tree, dw_die_ref);
6324 static void gen_const_die (tree, dw_die_ref);
6325 static void gen_label_die (tree, dw_die_ref);
6326 static void gen_lexical_block_die (tree, dw_die_ref, int);
6327 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6328 static void gen_field_die (tree, dw_die_ref);
6329 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6330 static dw_die_ref gen_compile_unit_die (const char *);
6331 static void gen_inheritance_die (tree, tree, dw_die_ref);
6332 static void gen_member_die (tree, dw_die_ref);
6333 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6334                                                 enum debug_info_usage);
6335 static void gen_subroutine_type_die (tree, dw_die_ref);
6336 static void gen_typedef_die (tree, dw_die_ref);
6337 static void gen_type_die (tree, dw_die_ref);
6338 static void gen_block_die (tree, dw_die_ref, int);
6339 static void decls_for_scope (tree, dw_die_ref, int);
6340 static int is_redundant_typedef (const_tree);
6341 static bool is_naming_typedef_decl (const_tree);
6342 static inline dw_die_ref get_context_die (tree);
6343 static void gen_namespace_die (tree, dw_die_ref);
6344 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6345 static dw_die_ref force_decl_die (tree);
6346 static dw_die_ref force_type_die (tree);
6347 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6348 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6349 static struct dwarf_file_data * lookup_filename (const char *);
6350 static void retry_incomplete_types (void);
6351 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6352 static void gen_generic_params_dies (tree);
6353 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6354 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6355 static void splice_child_die (dw_die_ref, dw_die_ref);
6356 static int file_info_cmp (const void *, const void *);
6357 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6358                                      const char *, const char *);
6359 static void output_loc_list (dw_loc_list_ref);
6360 static char *gen_internal_sym (const char *);
6361
6362 static void prune_unmark_dies (dw_die_ref);
6363 static void prune_unused_types_mark (dw_die_ref, int);
6364 static void prune_unused_types_walk (dw_die_ref);
6365 static void prune_unused_types_walk_attribs (dw_die_ref);
6366 static void prune_unused_types_prune (dw_die_ref);
6367 static void prune_unused_types (void);
6368 static int maybe_emit_file (struct dwarf_file_data *fd);
6369 static inline const char *AT_vms_delta1 (dw_attr_ref);
6370 static inline const char *AT_vms_delta2 (dw_attr_ref);
6371 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6372                                      const char *, const char *);
6373 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6374 static void gen_remaining_tmpl_value_param_die_attribute (void);
6375
6376 /* Section names used to hold DWARF debugging information.  */
6377 #ifndef DEBUG_INFO_SECTION
6378 #define DEBUG_INFO_SECTION      ".debug_info"
6379 #endif
6380 #ifndef DEBUG_ABBREV_SECTION
6381 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6382 #endif
6383 #ifndef DEBUG_ARANGES_SECTION
6384 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6385 #endif
6386 #ifndef DEBUG_MACINFO_SECTION
6387 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6388 #endif
6389 #ifndef DEBUG_LINE_SECTION
6390 #define DEBUG_LINE_SECTION      ".debug_line"
6391 #endif
6392 #ifndef DEBUG_LOC_SECTION
6393 #define DEBUG_LOC_SECTION       ".debug_loc"
6394 #endif
6395 #ifndef DEBUG_PUBNAMES_SECTION
6396 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6397 #endif
6398 #ifndef DEBUG_PUBTYPES_SECTION
6399 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6400 #endif
6401 #ifndef DEBUG_DCALL_SECTION
6402 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6403 #endif
6404 #ifndef DEBUG_VCALL_SECTION
6405 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6406 #endif
6407 #ifndef DEBUG_STR_SECTION
6408 #define DEBUG_STR_SECTION       ".debug_str"
6409 #endif
6410 #ifndef DEBUG_RANGES_SECTION
6411 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6412 #endif
6413
6414 /* Standard ELF section names for compiled code and data.  */
6415 #ifndef TEXT_SECTION_NAME
6416 #define TEXT_SECTION_NAME       ".text"
6417 #endif
6418
6419 /* Section flags for .debug_str section.  */
6420 #define DEBUG_STR_SECTION_FLAGS \
6421   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6422    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6423    : SECTION_DEBUG)
6424
6425 /* Labels we insert at beginning sections we can reference instead of
6426    the section names themselves.  */
6427
6428 #ifndef TEXT_SECTION_LABEL
6429 #define TEXT_SECTION_LABEL              "Ltext"
6430 #endif
6431 #ifndef COLD_TEXT_SECTION_LABEL
6432 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6433 #endif
6434 #ifndef DEBUG_LINE_SECTION_LABEL
6435 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6436 #endif
6437 #ifndef DEBUG_INFO_SECTION_LABEL
6438 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6439 #endif
6440 #ifndef DEBUG_ABBREV_SECTION_LABEL
6441 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6442 #endif
6443 #ifndef DEBUG_LOC_SECTION_LABEL
6444 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6445 #endif
6446 #ifndef DEBUG_RANGES_SECTION_LABEL
6447 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6448 #endif
6449 #ifndef DEBUG_MACINFO_SECTION_LABEL
6450 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6451 #endif
6452
6453
6454 /* Definitions of defaults for formats and names of various special
6455    (artificial) labels which may be generated within this file (when the -g
6456    options is used and DWARF2_DEBUGGING_INFO is in effect.
6457    If necessary, these may be overridden from within the tm.h file, but
6458    typically, overriding these defaults is unnecessary.  */
6459
6460 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6461 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6462 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6463 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6464 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6465 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6466 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6467 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6468 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6469 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6470
6471 #ifndef TEXT_END_LABEL
6472 #define TEXT_END_LABEL          "Letext"
6473 #endif
6474 #ifndef COLD_END_LABEL
6475 #define COLD_END_LABEL          "Letext_cold"
6476 #endif
6477 #ifndef BLOCK_BEGIN_LABEL
6478 #define BLOCK_BEGIN_LABEL       "LBB"
6479 #endif
6480 #ifndef BLOCK_END_LABEL
6481 #define BLOCK_END_LABEL         "LBE"
6482 #endif
6483 #ifndef LINE_CODE_LABEL
6484 #define LINE_CODE_LABEL         "LM"
6485 #endif
6486 #ifndef SEPARATE_LINE_CODE_LABEL
6487 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6488 #endif
6489
6490 \f
6491 /* We allow a language front-end to designate a function that is to be
6492    called to "demangle" any name before it is put into a DIE.  */
6493
6494 static const char *(*demangle_name_func) (const char *);
6495
6496 void
6497 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6498 {
6499   demangle_name_func = func;
6500 }
6501
6502 /* Test if rtl node points to a pseudo register.  */
6503
6504 static inline int
6505 is_pseudo_reg (const_rtx rtl)
6506 {
6507   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6508           || (GET_CODE (rtl) == SUBREG
6509               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6510 }
6511
6512 /* Return a reference to a type, with its const and volatile qualifiers
6513    removed.  */
6514
6515 static inline tree
6516 type_main_variant (tree type)
6517 {
6518   type = TYPE_MAIN_VARIANT (type);
6519
6520   /* ??? There really should be only one main variant among any group of
6521      variants of a given type (and all of the MAIN_VARIANT values for all
6522      members of the group should point to that one type) but sometimes the C
6523      front-end messes this up for array types, so we work around that bug
6524      here.  */
6525   if (TREE_CODE (type) == ARRAY_TYPE)
6526     while (type != TYPE_MAIN_VARIANT (type))
6527       type = TYPE_MAIN_VARIANT (type);
6528
6529   return type;
6530 }
6531
6532 /* Return nonzero if the given type node represents a tagged type.  */
6533
6534 static inline int
6535 is_tagged_type (const_tree type)
6536 {
6537   enum tree_code code = TREE_CODE (type);
6538
6539   return (code == RECORD_TYPE || code == UNION_TYPE
6540           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6541 }
6542
6543 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6544
6545 static void
6546 get_ref_die_offset_label (char *label, dw_die_ref ref)
6547 {
6548   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6549 }
6550
6551 /* Convert a DIE tag into its string name.  */
6552
6553 static const char *
6554 dwarf_tag_name (unsigned int tag)
6555 {
6556   switch (tag)
6557     {
6558     case DW_TAG_padding:
6559       return "DW_TAG_padding";
6560     case DW_TAG_array_type:
6561       return "DW_TAG_array_type";
6562     case DW_TAG_class_type:
6563       return "DW_TAG_class_type";
6564     case DW_TAG_entry_point:
6565       return "DW_TAG_entry_point";
6566     case DW_TAG_enumeration_type:
6567       return "DW_TAG_enumeration_type";
6568     case DW_TAG_formal_parameter:
6569       return "DW_TAG_formal_parameter";
6570     case DW_TAG_imported_declaration:
6571       return "DW_TAG_imported_declaration";
6572     case DW_TAG_label:
6573       return "DW_TAG_label";
6574     case DW_TAG_lexical_block:
6575       return "DW_TAG_lexical_block";
6576     case DW_TAG_member:
6577       return "DW_TAG_member";
6578     case DW_TAG_pointer_type:
6579       return "DW_TAG_pointer_type";
6580     case DW_TAG_reference_type:
6581       return "DW_TAG_reference_type";
6582     case DW_TAG_compile_unit:
6583       return "DW_TAG_compile_unit";
6584     case DW_TAG_string_type:
6585       return "DW_TAG_string_type";
6586     case DW_TAG_structure_type:
6587       return "DW_TAG_structure_type";
6588     case DW_TAG_subroutine_type:
6589       return "DW_TAG_subroutine_type";
6590     case DW_TAG_typedef:
6591       return "DW_TAG_typedef";
6592     case DW_TAG_union_type:
6593       return "DW_TAG_union_type";
6594     case DW_TAG_unspecified_parameters:
6595       return "DW_TAG_unspecified_parameters";
6596     case DW_TAG_variant:
6597       return "DW_TAG_variant";
6598     case DW_TAG_common_block:
6599       return "DW_TAG_common_block";
6600     case DW_TAG_common_inclusion:
6601       return "DW_TAG_common_inclusion";
6602     case DW_TAG_inheritance:
6603       return "DW_TAG_inheritance";
6604     case DW_TAG_inlined_subroutine:
6605       return "DW_TAG_inlined_subroutine";
6606     case DW_TAG_module:
6607       return "DW_TAG_module";
6608     case DW_TAG_ptr_to_member_type:
6609       return "DW_TAG_ptr_to_member_type";
6610     case DW_TAG_set_type:
6611       return "DW_TAG_set_type";
6612     case DW_TAG_subrange_type:
6613       return "DW_TAG_subrange_type";
6614     case DW_TAG_with_stmt:
6615       return "DW_TAG_with_stmt";
6616     case DW_TAG_access_declaration:
6617       return "DW_TAG_access_declaration";
6618     case DW_TAG_base_type:
6619       return "DW_TAG_base_type";
6620     case DW_TAG_catch_block:
6621       return "DW_TAG_catch_block";
6622     case DW_TAG_const_type:
6623       return "DW_TAG_const_type";
6624     case DW_TAG_constant:
6625       return "DW_TAG_constant";
6626     case DW_TAG_enumerator:
6627       return "DW_TAG_enumerator";
6628     case DW_TAG_file_type:
6629       return "DW_TAG_file_type";
6630     case DW_TAG_friend:
6631       return "DW_TAG_friend";
6632     case DW_TAG_namelist:
6633       return "DW_TAG_namelist";
6634     case DW_TAG_namelist_item:
6635       return "DW_TAG_namelist_item";
6636     case DW_TAG_packed_type:
6637       return "DW_TAG_packed_type";
6638     case DW_TAG_subprogram:
6639       return "DW_TAG_subprogram";
6640     case DW_TAG_template_type_param:
6641       return "DW_TAG_template_type_param";
6642     case DW_TAG_template_value_param:
6643       return "DW_TAG_template_value_param";
6644     case DW_TAG_thrown_type:
6645       return "DW_TAG_thrown_type";
6646     case DW_TAG_try_block:
6647       return "DW_TAG_try_block";
6648     case DW_TAG_variant_part:
6649       return "DW_TAG_variant_part";
6650     case DW_TAG_variable:
6651       return "DW_TAG_variable";
6652     case DW_TAG_volatile_type:
6653       return "DW_TAG_volatile_type";
6654     case DW_TAG_dwarf_procedure:
6655       return "DW_TAG_dwarf_procedure";
6656     case DW_TAG_restrict_type:
6657       return "DW_TAG_restrict_type";
6658     case DW_TAG_interface_type:
6659       return "DW_TAG_interface_type";
6660     case DW_TAG_namespace:
6661       return "DW_TAG_namespace";
6662     case DW_TAG_imported_module:
6663       return "DW_TAG_imported_module";
6664     case DW_TAG_unspecified_type:
6665       return "DW_TAG_unspecified_type";
6666     case DW_TAG_partial_unit:
6667       return "DW_TAG_partial_unit";
6668     case DW_TAG_imported_unit:
6669       return "DW_TAG_imported_unit";
6670     case DW_TAG_condition:
6671       return "DW_TAG_condition";
6672     case DW_TAG_shared_type:
6673       return "DW_TAG_shared_type";
6674     case DW_TAG_type_unit:
6675       return "DW_TAG_type_unit";
6676     case DW_TAG_rvalue_reference_type:
6677       return "DW_TAG_rvalue_reference_type";
6678     case DW_TAG_template_alias:
6679       return "DW_TAG_template_alias";
6680     case DW_TAG_GNU_template_parameter_pack:
6681       return "DW_TAG_GNU_template_parameter_pack";
6682     case DW_TAG_GNU_formal_parameter_pack:
6683       return "DW_TAG_GNU_formal_parameter_pack";
6684     case DW_TAG_MIPS_loop:
6685       return "DW_TAG_MIPS_loop";
6686     case DW_TAG_format_label:
6687       return "DW_TAG_format_label";
6688     case DW_TAG_function_template:
6689       return "DW_TAG_function_template";
6690     case DW_TAG_class_template:
6691       return "DW_TAG_class_template";
6692     case DW_TAG_GNU_BINCL:
6693       return "DW_TAG_GNU_BINCL";
6694     case DW_TAG_GNU_EINCL:
6695       return "DW_TAG_GNU_EINCL";
6696     case DW_TAG_GNU_template_template_param:
6697       return "DW_TAG_GNU_template_template_param";
6698     default:
6699       return "DW_TAG_<unknown>";
6700     }
6701 }
6702
6703 /* Convert a DWARF attribute code into its string name.  */
6704
6705 static const char *
6706 dwarf_attr_name (unsigned int attr)
6707 {
6708   switch (attr)
6709     {
6710     case DW_AT_sibling:
6711       return "DW_AT_sibling";
6712     case DW_AT_location:
6713       return "DW_AT_location";
6714     case DW_AT_name:
6715       return "DW_AT_name";
6716     case DW_AT_ordering:
6717       return "DW_AT_ordering";
6718     case DW_AT_subscr_data:
6719       return "DW_AT_subscr_data";
6720     case DW_AT_byte_size:
6721       return "DW_AT_byte_size";
6722     case DW_AT_bit_offset:
6723       return "DW_AT_bit_offset";
6724     case DW_AT_bit_size:
6725       return "DW_AT_bit_size";
6726     case DW_AT_element_list:
6727       return "DW_AT_element_list";
6728     case DW_AT_stmt_list:
6729       return "DW_AT_stmt_list";
6730     case DW_AT_low_pc:
6731       return "DW_AT_low_pc";
6732     case DW_AT_high_pc:
6733       return "DW_AT_high_pc";
6734     case DW_AT_language:
6735       return "DW_AT_language";
6736     case DW_AT_member:
6737       return "DW_AT_member";
6738     case DW_AT_discr:
6739       return "DW_AT_discr";
6740     case DW_AT_discr_value:
6741       return "DW_AT_discr_value";
6742     case DW_AT_visibility:
6743       return "DW_AT_visibility";
6744     case DW_AT_import:
6745       return "DW_AT_import";
6746     case DW_AT_string_length:
6747       return "DW_AT_string_length";
6748     case DW_AT_common_reference:
6749       return "DW_AT_common_reference";
6750     case DW_AT_comp_dir:
6751       return "DW_AT_comp_dir";
6752     case DW_AT_const_value:
6753       return "DW_AT_const_value";
6754     case DW_AT_containing_type:
6755       return "DW_AT_containing_type";
6756     case DW_AT_default_value:
6757       return "DW_AT_default_value";
6758     case DW_AT_inline:
6759       return "DW_AT_inline";
6760     case DW_AT_is_optional:
6761       return "DW_AT_is_optional";
6762     case DW_AT_lower_bound:
6763       return "DW_AT_lower_bound";
6764     case DW_AT_producer:
6765       return "DW_AT_producer";
6766     case DW_AT_prototyped:
6767       return "DW_AT_prototyped";
6768     case DW_AT_return_addr:
6769       return "DW_AT_return_addr";
6770     case DW_AT_start_scope:
6771       return "DW_AT_start_scope";
6772     case DW_AT_bit_stride:
6773       return "DW_AT_bit_stride";
6774     case DW_AT_upper_bound:
6775       return "DW_AT_upper_bound";
6776     case DW_AT_abstract_origin:
6777       return "DW_AT_abstract_origin";
6778     case DW_AT_accessibility:
6779       return "DW_AT_accessibility";
6780     case DW_AT_address_class:
6781       return "DW_AT_address_class";
6782     case DW_AT_artificial:
6783       return "DW_AT_artificial";
6784     case DW_AT_base_types:
6785       return "DW_AT_base_types";
6786     case DW_AT_calling_convention:
6787       return "DW_AT_calling_convention";
6788     case DW_AT_count:
6789       return "DW_AT_count";
6790     case DW_AT_data_member_location:
6791       return "DW_AT_data_member_location";
6792     case DW_AT_decl_column:
6793       return "DW_AT_decl_column";
6794     case DW_AT_decl_file:
6795       return "DW_AT_decl_file";
6796     case DW_AT_decl_line:
6797       return "DW_AT_decl_line";
6798     case DW_AT_declaration:
6799       return "DW_AT_declaration";
6800     case DW_AT_discr_list:
6801       return "DW_AT_discr_list";
6802     case DW_AT_encoding:
6803       return "DW_AT_encoding";
6804     case DW_AT_external:
6805       return "DW_AT_external";
6806     case DW_AT_explicit:
6807       return "DW_AT_explicit";
6808     case DW_AT_frame_base:
6809       return "DW_AT_frame_base";
6810     case DW_AT_friend:
6811       return "DW_AT_friend";
6812     case DW_AT_identifier_case:
6813       return "DW_AT_identifier_case";
6814     case DW_AT_macro_info:
6815       return "DW_AT_macro_info";
6816     case DW_AT_namelist_items:
6817       return "DW_AT_namelist_items";
6818     case DW_AT_priority:
6819       return "DW_AT_priority";
6820     case DW_AT_segment:
6821       return "DW_AT_segment";
6822     case DW_AT_specification:
6823       return "DW_AT_specification";
6824     case DW_AT_static_link:
6825       return "DW_AT_static_link";
6826     case DW_AT_type:
6827       return "DW_AT_type";
6828     case DW_AT_use_location:
6829       return "DW_AT_use_location";
6830     case DW_AT_variable_parameter:
6831       return "DW_AT_variable_parameter";
6832     case DW_AT_virtuality:
6833       return "DW_AT_virtuality";
6834     case DW_AT_vtable_elem_location:
6835       return "DW_AT_vtable_elem_location";
6836
6837     case DW_AT_allocated:
6838       return "DW_AT_allocated";
6839     case DW_AT_associated:
6840       return "DW_AT_associated";
6841     case DW_AT_data_location:
6842       return "DW_AT_data_location";
6843     case DW_AT_byte_stride:
6844       return "DW_AT_byte_stride";
6845     case DW_AT_entry_pc:
6846       return "DW_AT_entry_pc";
6847     case DW_AT_use_UTF8:
6848       return "DW_AT_use_UTF8";
6849     case DW_AT_extension:
6850       return "DW_AT_extension";
6851     case DW_AT_ranges:
6852       return "DW_AT_ranges";
6853     case DW_AT_trampoline:
6854       return "DW_AT_trampoline";
6855     case DW_AT_call_column:
6856       return "DW_AT_call_column";
6857     case DW_AT_call_file:
6858       return "DW_AT_call_file";
6859     case DW_AT_call_line:
6860       return "DW_AT_call_line";
6861     case DW_AT_object_pointer:
6862       return "DW_AT_object_pointer";
6863
6864     case DW_AT_signature:
6865       return "DW_AT_signature";
6866     case DW_AT_main_subprogram:
6867       return "DW_AT_main_subprogram";
6868     case DW_AT_data_bit_offset:
6869       return "DW_AT_data_bit_offset";
6870     case DW_AT_const_expr:
6871       return "DW_AT_const_expr";
6872     case DW_AT_enum_class:
6873       return "DW_AT_enum_class";
6874     case DW_AT_linkage_name:
6875       return "DW_AT_linkage_name";
6876
6877     case DW_AT_MIPS_fde:
6878       return "DW_AT_MIPS_fde";
6879     case DW_AT_MIPS_loop_begin:
6880       return "DW_AT_MIPS_loop_begin";
6881     case DW_AT_MIPS_tail_loop_begin:
6882       return "DW_AT_MIPS_tail_loop_begin";
6883     case DW_AT_MIPS_epilog_begin:
6884       return "DW_AT_MIPS_epilog_begin";
6885 #if VMS_DEBUGGING_INFO
6886     case DW_AT_HP_prologue:
6887       return "DW_AT_HP_prologue";
6888 #else
6889     case DW_AT_MIPS_loop_unroll_factor:
6890       return "DW_AT_MIPS_loop_unroll_factor";
6891 #endif
6892     case DW_AT_MIPS_software_pipeline_depth:
6893       return "DW_AT_MIPS_software_pipeline_depth";
6894     case DW_AT_MIPS_linkage_name:
6895       return "DW_AT_MIPS_linkage_name";
6896 #if VMS_DEBUGGING_INFO
6897     case DW_AT_HP_epilogue:
6898       return "DW_AT_HP_epilogue";
6899 #else
6900     case DW_AT_MIPS_stride:
6901       return "DW_AT_MIPS_stride";
6902 #endif
6903     case DW_AT_MIPS_abstract_name:
6904       return "DW_AT_MIPS_abstract_name";
6905     case DW_AT_MIPS_clone_origin:
6906       return "DW_AT_MIPS_clone_origin";
6907     case DW_AT_MIPS_has_inlines:
6908       return "DW_AT_MIPS_has_inlines";
6909
6910     case DW_AT_sf_names:
6911       return "DW_AT_sf_names";
6912     case DW_AT_src_info:
6913       return "DW_AT_src_info";
6914     case DW_AT_mac_info:
6915       return "DW_AT_mac_info";
6916     case DW_AT_src_coords:
6917       return "DW_AT_src_coords";
6918     case DW_AT_body_begin:
6919       return "DW_AT_body_begin";
6920     case DW_AT_body_end:
6921       return "DW_AT_body_end";
6922     case DW_AT_GNU_vector:
6923       return "DW_AT_GNU_vector";
6924     case DW_AT_GNU_guarded_by:
6925       return "DW_AT_GNU_guarded_by";
6926     case DW_AT_GNU_pt_guarded_by:
6927       return "DW_AT_GNU_pt_guarded_by";
6928     case DW_AT_GNU_guarded:
6929       return "DW_AT_GNU_guarded";
6930     case DW_AT_GNU_pt_guarded:
6931       return "DW_AT_GNU_pt_guarded";
6932     case DW_AT_GNU_locks_excluded:
6933       return "DW_AT_GNU_locks_excluded";
6934     case DW_AT_GNU_exclusive_locks_required:
6935       return "DW_AT_GNU_exclusive_locks_required";
6936     case DW_AT_GNU_shared_locks_required:
6937       return "DW_AT_GNU_shared_locks_required";
6938     case DW_AT_GNU_odr_signature:
6939       return "DW_AT_GNU_odr_signature";
6940     case DW_AT_GNU_template_name:
6941       return "DW_AT_GNU_template_name";
6942
6943     case DW_AT_VMS_rtnbeg_pd_address:
6944       return "DW_AT_VMS_rtnbeg_pd_address";
6945
6946     default:
6947       return "DW_AT_<unknown>";
6948     }
6949 }
6950
6951 /* Convert a DWARF value form code into its string name.  */
6952
6953 static const char *
6954 dwarf_form_name (unsigned int form)
6955 {
6956   switch (form)
6957     {
6958     case DW_FORM_addr:
6959       return "DW_FORM_addr";
6960     case DW_FORM_block2:
6961       return "DW_FORM_block2";
6962     case DW_FORM_block4:
6963       return "DW_FORM_block4";
6964     case DW_FORM_data2:
6965       return "DW_FORM_data2";
6966     case DW_FORM_data4:
6967       return "DW_FORM_data4";
6968     case DW_FORM_data8:
6969       return "DW_FORM_data8";
6970     case DW_FORM_string:
6971       return "DW_FORM_string";
6972     case DW_FORM_block:
6973       return "DW_FORM_block";
6974     case DW_FORM_block1:
6975       return "DW_FORM_block1";
6976     case DW_FORM_data1:
6977       return "DW_FORM_data1";
6978     case DW_FORM_flag:
6979       return "DW_FORM_flag";
6980     case DW_FORM_sdata:
6981       return "DW_FORM_sdata";
6982     case DW_FORM_strp:
6983       return "DW_FORM_strp";
6984     case DW_FORM_udata:
6985       return "DW_FORM_udata";
6986     case DW_FORM_ref_addr:
6987       return "DW_FORM_ref_addr";
6988     case DW_FORM_ref1:
6989       return "DW_FORM_ref1";
6990     case DW_FORM_ref2:
6991       return "DW_FORM_ref2";
6992     case DW_FORM_ref4:
6993       return "DW_FORM_ref4";
6994     case DW_FORM_ref8:
6995       return "DW_FORM_ref8";
6996     case DW_FORM_ref_udata:
6997       return "DW_FORM_ref_udata";
6998     case DW_FORM_indirect:
6999       return "DW_FORM_indirect";
7000     case DW_FORM_sec_offset:
7001       return "DW_FORM_sec_offset";
7002     case DW_FORM_exprloc:
7003       return "DW_FORM_exprloc";
7004     case DW_FORM_flag_present:
7005       return "DW_FORM_flag_present";
7006     case DW_FORM_ref_sig8:
7007       return "DW_FORM_ref_sig8";
7008     default:
7009       return "DW_FORM_<unknown>";
7010     }
7011 }
7012 \f
7013 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7014    instance of an inlined instance of a decl which is local to an inline
7015    function, so we have to trace all of the way back through the origin chain
7016    to find out what sort of node actually served as the original seed for the
7017    given block.  */
7018
7019 static tree
7020 decl_ultimate_origin (const_tree decl)
7021 {
7022   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7023     return NULL_TREE;
7024
7025   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7026      nodes in the function to point to themselves; ignore that if
7027      we're trying to output the abstract instance of this function.  */
7028   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7029     return NULL_TREE;
7030
7031   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7032      most distant ancestor, this should never happen.  */
7033   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7034
7035   return DECL_ABSTRACT_ORIGIN (decl);
7036 }
7037
7038 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7039    of a virtual function may refer to a base class, so we check the 'this'
7040    parameter.  */
7041
7042 static tree
7043 decl_class_context (tree decl)
7044 {
7045   tree context = NULL_TREE;
7046
7047   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7048     context = DECL_CONTEXT (decl);
7049   else
7050     context = TYPE_MAIN_VARIANT
7051       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7052
7053   if (context && !TYPE_P (context))
7054     context = NULL_TREE;
7055
7056   return context;
7057 }
7058 \f
7059 /* Add an attribute/value pair to a DIE.  */
7060
7061 static inline void
7062 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7063 {
7064   /* Maybe this should be an assert?  */
7065   if (die == NULL)
7066     return;
7067
7068   if (die->die_attr == NULL)
7069     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7070   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7071 }
7072
7073 static inline enum dw_val_class
7074 AT_class (dw_attr_ref a)
7075 {
7076   return a->dw_attr_val.val_class;
7077 }
7078
7079 /* Add a flag value attribute to a DIE.  */
7080
7081 static inline void
7082 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7083 {
7084   dw_attr_node attr;
7085
7086   attr.dw_attr = attr_kind;
7087   attr.dw_attr_val.val_class = dw_val_class_flag;
7088   attr.dw_attr_val.v.val_flag = flag;
7089   add_dwarf_attr (die, &attr);
7090 }
7091
7092 static inline unsigned
7093 AT_flag (dw_attr_ref a)
7094 {
7095   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7096   return a->dw_attr_val.v.val_flag;
7097 }
7098
7099 /* Add a signed integer attribute value to a DIE.  */
7100
7101 static inline void
7102 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7103 {
7104   dw_attr_node attr;
7105
7106   attr.dw_attr = attr_kind;
7107   attr.dw_attr_val.val_class = dw_val_class_const;
7108   attr.dw_attr_val.v.val_int = int_val;
7109   add_dwarf_attr (die, &attr);
7110 }
7111
7112 static inline HOST_WIDE_INT
7113 AT_int (dw_attr_ref a)
7114 {
7115   gcc_assert (a && AT_class (a) == dw_val_class_const);
7116   return a->dw_attr_val.v.val_int;
7117 }
7118
7119 /* Add an unsigned integer attribute value to a DIE.  */
7120
7121 static inline void
7122 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7123                  unsigned HOST_WIDE_INT unsigned_val)
7124 {
7125   dw_attr_node attr;
7126
7127   attr.dw_attr = attr_kind;
7128   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7129   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7130   add_dwarf_attr (die, &attr);
7131 }
7132
7133 static inline unsigned HOST_WIDE_INT
7134 AT_unsigned (dw_attr_ref a)
7135 {
7136   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7137   return a->dw_attr_val.v.val_unsigned;
7138 }
7139
7140 /* Add an unsigned double integer attribute value to a DIE.  */
7141
7142 static inline void
7143 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7144                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7145 {
7146   dw_attr_node attr;
7147
7148   attr.dw_attr = attr_kind;
7149   attr.dw_attr_val.val_class = dw_val_class_const_double;
7150   attr.dw_attr_val.v.val_double.high = high;
7151   attr.dw_attr_val.v.val_double.low = low;
7152   add_dwarf_attr (die, &attr);
7153 }
7154
7155 /* Add a floating point attribute value to a DIE and return it.  */
7156
7157 static inline void
7158 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7159             unsigned int length, unsigned int elt_size, unsigned char *array)
7160 {
7161   dw_attr_node attr;
7162
7163   attr.dw_attr = attr_kind;
7164   attr.dw_attr_val.val_class = dw_val_class_vec;
7165   attr.dw_attr_val.v.val_vec.length = length;
7166   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7167   attr.dw_attr_val.v.val_vec.array = array;
7168   add_dwarf_attr (die, &attr);
7169 }
7170
7171 /* Add an 8-byte data attribute value to a DIE.  */
7172
7173 static inline void
7174 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7175               unsigned char data8[8])
7176 {
7177   dw_attr_node attr;
7178
7179   attr.dw_attr = attr_kind;
7180   attr.dw_attr_val.val_class = dw_val_class_data8;
7181   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7182   add_dwarf_attr (die, &attr);
7183 }
7184
7185 /* Hash and equality functions for debug_str_hash.  */
7186
7187 static hashval_t
7188 debug_str_do_hash (const void *x)
7189 {
7190   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7191 }
7192
7193 static int
7194 debug_str_eq (const void *x1, const void *x2)
7195 {
7196   return strcmp ((((const struct indirect_string_node *)x1)->str),
7197                  (const char *)x2) == 0;
7198 }
7199
7200 /* Add STR to the indirect string hash table.  */
7201
7202 static struct indirect_string_node *
7203 find_AT_string (const char *str)
7204 {
7205   struct indirect_string_node *node;
7206   void **slot;
7207
7208   if (! debug_str_hash)
7209     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7210                                       debug_str_eq, NULL);
7211
7212   slot = htab_find_slot_with_hash (debug_str_hash, str,
7213                                    htab_hash_string (str), INSERT);
7214   if (*slot == NULL)
7215     {
7216       node = ggc_alloc_cleared_indirect_string_node ();
7217       node->str = ggc_strdup (str);
7218       *slot = node;
7219     }
7220   else
7221     node = (struct indirect_string_node *) *slot;
7222
7223   node->refcount++;
7224   return node;
7225 }
7226
7227 /* Add a string attribute value to a DIE.  */
7228
7229 static inline void
7230 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7231 {
7232   dw_attr_node attr;
7233   struct indirect_string_node *node;
7234
7235   node = find_AT_string (str);
7236
7237   attr.dw_attr = attr_kind;
7238   attr.dw_attr_val.val_class = dw_val_class_str;
7239   attr.dw_attr_val.v.val_str = node;
7240   add_dwarf_attr (die, &attr);
7241 }
7242
7243 /* Create a label for an indirect string node, ensuring it is going to
7244    be output, unless its reference count goes down to zero.  */
7245
7246 static inline void
7247 gen_label_for_indirect_string (struct indirect_string_node *node)
7248 {
7249   char label[32];
7250
7251   if (node->label)
7252     return;
7253
7254   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7255   ++dw2_string_counter;
7256   node->label = xstrdup (label);
7257 }
7258
7259 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7260    debug string STR.  */
7261
7262 static inline rtx
7263 get_debug_string_label (const char *str)
7264 {
7265   struct indirect_string_node *node = find_AT_string (str);
7266
7267   debug_str_hash_forced = true;
7268
7269   gen_label_for_indirect_string (node);
7270
7271   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7272 }
7273
7274 static inline const char *
7275 AT_string (dw_attr_ref a)
7276 {
7277   gcc_assert (a && AT_class (a) == dw_val_class_str);
7278   return a->dw_attr_val.v.val_str->str;
7279 }
7280
7281 /* Find out whether a string should be output inline in DIE
7282    or out-of-line in .debug_str section.  */
7283
7284 static enum dwarf_form
7285 AT_string_form (dw_attr_ref a)
7286 {
7287   struct indirect_string_node *node;
7288   unsigned int len;
7289
7290   gcc_assert (a && AT_class (a) == dw_val_class_str);
7291
7292   node = a->dw_attr_val.v.val_str;
7293   if (node->form)
7294     return node->form;
7295
7296   len = strlen (node->str) + 1;
7297
7298   /* If the string is shorter or equal to the size of the reference, it is
7299      always better to put it inline.  */
7300   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7301     return node->form = DW_FORM_string;
7302
7303   /* If we cannot expect the linker to merge strings in .debug_str
7304      section, only put it into .debug_str if it is worth even in this
7305      single module.  */
7306   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7307       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7308       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7309     return node->form = DW_FORM_string;
7310
7311   gen_label_for_indirect_string (node);
7312
7313   return node->form = DW_FORM_strp;
7314 }
7315
7316 /* Add a DIE reference attribute value to a DIE.  */
7317
7318 static inline void
7319 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7320 {
7321   dw_attr_node attr;
7322
7323   attr.dw_attr = attr_kind;
7324   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7325   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7326   attr.dw_attr_val.v.val_die_ref.external = 0;
7327   add_dwarf_attr (die, &attr);
7328 }
7329
7330 /* Add an AT_specification attribute to a DIE, and also make the back
7331    pointer from the specification to the definition.  */
7332
7333 static inline void
7334 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7335 {
7336   add_AT_die_ref (die, DW_AT_specification, targ_die);
7337   gcc_assert (!targ_die->die_definition);
7338   targ_die->die_definition = die;
7339 }
7340
7341 static inline dw_die_ref
7342 AT_ref (dw_attr_ref a)
7343 {
7344   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7345   return a->dw_attr_val.v.val_die_ref.die;
7346 }
7347
7348 static inline int
7349 AT_ref_external (dw_attr_ref a)
7350 {
7351   if (a && AT_class (a) == dw_val_class_die_ref)
7352     return a->dw_attr_val.v.val_die_ref.external;
7353
7354   return 0;
7355 }
7356
7357 static inline void
7358 set_AT_ref_external (dw_attr_ref a, int i)
7359 {
7360   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7361   a->dw_attr_val.v.val_die_ref.external = i;
7362 }
7363
7364 /* Add an FDE reference attribute value to a DIE.  */
7365
7366 static inline void
7367 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7368 {
7369   dw_attr_node attr;
7370
7371   attr.dw_attr = attr_kind;
7372   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7373   attr.dw_attr_val.v.val_fde_index = targ_fde;
7374   add_dwarf_attr (die, &attr);
7375 }
7376
7377 /* Add a location description attribute value to a DIE.  */
7378
7379 static inline void
7380 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7381 {
7382   dw_attr_node attr;
7383
7384   attr.dw_attr = attr_kind;
7385   attr.dw_attr_val.val_class = dw_val_class_loc;
7386   attr.dw_attr_val.v.val_loc = loc;
7387   add_dwarf_attr (die, &attr);
7388 }
7389
7390 static inline dw_loc_descr_ref
7391 AT_loc (dw_attr_ref a)
7392 {
7393   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7394   return a->dw_attr_val.v.val_loc;
7395 }
7396
7397 static inline void
7398 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7399 {
7400   dw_attr_node attr;
7401
7402   attr.dw_attr = attr_kind;
7403   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7404   attr.dw_attr_val.v.val_loc_list = loc_list;
7405   add_dwarf_attr (die, &attr);
7406   have_location_lists = true;
7407 }
7408
7409 static inline dw_loc_list_ref
7410 AT_loc_list (dw_attr_ref a)
7411 {
7412   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7413   return a->dw_attr_val.v.val_loc_list;
7414 }
7415
7416 static inline dw_loc_list_ref *
7417 AT_loc_list_ptr (dw_attr_ref a)
7418 {
7419   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7420   return &a->dw_attr_val.v.val_loc_list;
7421 }
7422
7423 /* Add an address constant attribute value to a DIE.  */
7424
7425 static inline void
7426 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7427 {
7428   dw_attr_node attr;
7429
7430   attr.dw_attr = attr_kind;
7431   attr.dw_attr_val.val_class = dw_val_class_addr;
7432   attr.dw_attr_val.v.val_addr = addr;
7433   add_dwarf_attr (die, &attr);
7434 }
7435
7436 /* Get the RTX from to an address DIE attribute.  */
7437
7438 static inline rtx
7439 AT_addr (dw_attr_ref a)
7440 {
7441   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7442   return a->dw_attr_val.v.val_addr;
7443 }
7444
7445 /* Add a file attribute value to a DIE.  */
7446
7447 static inline void
7448 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7449              struct dwarf_file_data *fd)
7450 {
7451   dw_attr_node attr;
7452
7453   attr.dw_attr = attr_kind;
7454   attr.dw_attr_val.val_class = dw_val_class_file;
7455   attr.dw_attr_val.v.val_file = fd;
7456   add_dwarf_attr (die, &attr);
7457 }
7458
7459 /* Get the dwarf_file_data from a file DIE attribute.  */
7460
7461 static inline struct dwarf_file_data *
7462 AT_file (dw_attr_ref a)
7463 {
7464   gcc_assert (a && AT_class (a) == dw_val_class_file);
7465   return a->dw_attr_val.v.val_file;
7466 }
7467
7468 /* Add a vms delta attribute value to a DIE.  */
7469
7470 static inline void
7471 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7472                   const char *lbl1, const char *lbl2)
7473 {
7474   dw_attr_node attr;
7475
7476   attr.dw_attr = attr_kind;
7477   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7478   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7479   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7480   add_dwarf_attr (die, &attr);
7481 }
7482
7483 /* Add a label identifier attribute value to a DIE.  */
7484
7485 static inline void
7486 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7487 {
7488   dw_attr_node attr;
7489
7490   attr.dw_attr = attr_kind;
7491   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7492   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7493   add_dwarf_attr (die, &attr);
7494 }
7495
7496 /* Add a section offset attribute value to a DIE, an offset into the
7497    debug_line section.  */
7498
7499 static inline void
7500 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7501                 const char *label)
7502 {
7503   dw_attr_node attr;
7504
7505   attr.dw_attr = attr_kind;
7506   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7507   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7508   add_dwarf_attr (die, &attr);
7509 }
7510
7511 /* Add a section offset attribute value to a DIE, an offset into the
7512    debug_macinfo section.  */
7513
7514 static inline void
7515 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7516                const char *label)
7517 {
7518   dw_attr_node attr;
7519
7520   attr.dw_attr = attr_kind;
7521   attr.dw_attr_val.val_class = dw_val_class_macptr;
7522   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7523   add_dwarf_attr (die, &attr);
7524 }
7525
7526 /* Add an offset attribute value to a DIE.  */
7527
7528 static inline void
7529 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7530                unsigned HOST_WIDE_INT offset)
7531 {
7532   dw_attr_node attr;
7533
7534   attr.dw_attr = attr_kind;
7535   attr.dw_attr_val.val_class = dw_val_class_offset;
7536   attr.dw_attr_val.v.val_offset = offset;
7537   add_dwarf_attr (die, &attr);
7538 }
7539
7540 /* Add an range_list attribute value to a DIE.  */
7541
7542 static void
7543 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7544                    long unsigned int offset)
7545 {
7546   dw_attr_node attr;
7547
7548   attr.dw_attr = attr_kind;
7549   attr.dw_attr_val.val_class = dw_val_class_range_list;
7550   attr.dw_attr_val.v.val_offset = offset;
7551   add_dwarf_attr (die, &attr);
7552 }
7553
7554 /* Return the start label of a delta attribute.  */
7555
7556 static inline const char *
7557 AT_vms_delta1 (dw_attr_ref a)
7558 {
7559   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7560   return a->dw_attr_val.v.val_vms_delta.lbl1;
7561 }
7562
7563 /* Return the end label of a delta attribute.  */
7564
7565 static inline const char *
7566 AT_vms_delta2 (dw_attr_ref a)
7567 {
7568   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7569   return a->dw_attr_val.v.val_vms_delta.lbl2;
7570 }
7571
7572 static inline const char *
7573 AT_lbl (dw_attr_ref a)
7574 {
7575   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7576                     || AT_class (a) == dw_val_class_lineptr
7577                     || AT_class (a) == dw_val_class_macptr));
7578   return a->dw_attr_val.v.val_lbl_id;
7579 }
7580
7581 /* Get the attribute of type attr_kind.  */
7582
7583 static dw_attr_ref
7584 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7585 {
7586   dw_attr_ref a;
7587   unsigned ix;
7588   dw_die_ref spec = NULL;
7589
7590   if (! die)
7591     return NULL;
7592
7593   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7594     if (a->dw_attr == attr_kind)
7595       return a;
7596     else if (a->dw_attr == DW_AT_specification
7597              || a->dw_attr == DW_AT_abstract_origin)
7598       spec = AT_ref (a);
7599
7600   if (spec)
7601     return get_AT (spec, attr_kind);
7602
7603   return NULL;
7604 }
7605
7606 /* Return the "low pc" attribute value, typically associated with a subprogram
7607    DIE.  Return null if the "low pc" attribute is either not present, or if it
7608    cannot be represented as an assembler label identifier.  */
7609
7610 static inline const char *
7611 get_AT_low_pc (dw_die_ref die)
7612 {
7613   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7614
7615   return a ? AT_lbl (a) : NULL;
7616 }
7617
7618 /* Return the "high pc" attribute value, typically associated with a subprogram
7619    DIE.  Return null if the "high pc" attribute is either not present, or if it
7620    cannot be represented as an assembler label identifier.  */
7621
7622 static inline const char *
7623 get_AT_hi_pc (dw_die_ref die)
7624 {
7625   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7626
7627   return a ? AT_lbl (a) : NULL;
7628 }
7629
7630 /* Return the value of the string attribute designated by ATTR_KIND, or
7631    NULL if it is not present.  */
7632
7633 static inline const char *
7634 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7635 {
7636   dw_attr_ref a = get_AT (die, attr_kind);
7637
7638   return a ? AT_string (a) : NULL;
7639 }
7640
7641 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7642    if it is not present.  */
7643
7644 static inline int
7645 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7646 {
7647   dw_attr_ref a = get_AT (die, attr_kind);
7648
7649   return a ? AT_flag (a) : 0;
7650 }
7651
7652 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7653    if it is not present.  */
7654
7655 static inline unsigned
7656 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7657 {
7658   dw_attr_ref a = get_AT (die, attr_kind);
7659
7660   return a ? AT_unsigned (a) : 0;
7661 }
7662
7663 static inline dw_die_ref
7664 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7665 {
7666   dw_attr_ref a = get_AT (die, attr_kind);
7667
7668   return a ? AT_ref (a) : NULL;
7669 }
7670
7671 static inline struct dwarf_file_data *
7672 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7673 {
7674   dw_attr_ref a = get_AT (die, attr_kind);
7675
7676   return a ? AT_file (a) : NULL;
7677 }
7678
7679 /* Return TRUE if the language is C++.  */
7680
7681 static inline bool
7682 is_cxx (void)
7683 {
7684   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7685
7686   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7687 }
7688
7689 /* Return TRUE if the language is Fortran.  */
7690
7691 static inline bool
7692 is_fortran (void)
7693 {
7694   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7695
7696   return (lang == DW_LANG_Fortran77
7697           || lang == DW_LANG_Fortran90
7698           || lang == DW_LANG_Fortran95);
7699 }
7700
7701 /* Return TRUE if the language is Ada.  */
7702
7703 static inline bool
7704 is_ada (void)
7705 {
7706   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7707
7708   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7709 }
7710
7711 /* Remove the specified attribute if present.  */
7712
7713 static void
7714 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7715 {
7716   dw_attr_ref a;
7717   unsigned ix;
7718
7719   if (! die)
7720     return;
7721
7722   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7723     if (a->dw_attr == attr_kind)
7724       {
7725         if (AT_class (a) == dw_val_class_str)
7726           if (a->dw_attr_val.v.val_str->refcount)
7727             a->dw_attr_val.v.val_str->refcount--;
7728
7729         /* VEC_ordered_remove should help reduce the number of abbrevs
7730            that are needed.  */
7731         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7732         return;
7733       }
7734 }
7735
7736 /* Remove CHILD from its parent.  PREV must have the property that
7737    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7738
7739 static void
7740 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7741 {
7742   gcc_assert (child->die_parent == prev->die_parent);
7743   gcc_assert (prev->die_sib == child);
7744   if (prev == child)
7745     {
7746       gcc_assert (child->die_parent->die_child == child);
7747       prev = NULL;
7748     }
7749   else
7750     prev->die_sib = child->die_sib;
7751   if (child->die_parent->die_child == child)
7752     child->die_parent->die_child = prev;
7753 }
7754
7755 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7756    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7757
7758 static void
7759 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7760 {
7761   dw_die_ref parent = old_child->die_parent;
7762
7763   gcc_assert (parent == prev->die_parent);
7764   gcc_assert (prev->die_sib == old_child);
7765
7766   new_child->die_parent = parent;
7767   if (prev == old_child)
7768     {
7769       gcc_assert (parent->die_child == old_child);
7770       new_child->die_sib = new_child;
7771     }
7772   else
7773     {
7774       prev->die_sib = new_child;
7775       new_child->die_sib = old_child->die_sib;
7776     }
7777   if (old_child->die_parent->die_child == old_child)
7778     old_child->die_parent->die_child = new_child;
7779 }
7780
7781 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7782
7783 static void
7784 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7785 {
7786   dw_die_ref c;
7787   new_parent->die_child = old_parent->die_child;
7788   old_parent->die_child = NULL;
7789   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7790 }
7791
7792 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7793    matches TAG.  */
7794
7795 static void
7796 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7797 {
7798   dw_die_ref c;
7799
7800   c = die->die_child;
7801   if (c) do {
7802     dw_die_ref prev = c;
7803     c = c->die_sib;
7804     while (c->die_tag == tag)
7805       {
7806         remove_child_with_prev (c, prev);
7807         /* Might have removed every child.  */
7808         if (c == c->die_sib)
7809           return;
7810         c = c->die_sib;
7811       }
7812   } while (c != die->die_child);
7813 }
7814
7815 /* Add a CHILD_DIE as the last child of DIE.  */
7816
7817 static void
7818 add_child_die (dw_die_ref die, dw_die_ref child_die)
7819 {
7820   /* FIXME this should probably be an assert.  */
7821   if (! die || ! child_die)
7822     return;
7823   gcc_assert (die != child_die);
7824
7825   child_die->die_parent = die;
7826   if (die->die_child)
7827     {
7828       child_die->die_sib = die->die_child->die_sib;
7829       die->die_child->die_sib = child_die;
7830     }
7831   else
7832     child_die->die_sib = child_die;
7833   die->die_child = child_die;
7834 }
7835
7836 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7837    is the specification, to the end of PARENT's list of children.
7838    This is done by removing and re-adding it.  */
7839
7840 static void
7841 splice_child_die (dw_die_ref parent, dw_die_ref child)
7842 {
7843   dw_die_ref p;
7844
7845   /* We want the declaration DIE from inside the class, not the
7846      specification DIE at toplevel.  */
7847   if (child->die_parent != parent)
7848     {
7849       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7850
7851       if (tmp)
7852         child = tmp;
7853     }
7854
7855   gcc_assert (child->die_parent == parent
7856               || (child->die_parent
7857                   == get_AT_ref (parent, DW_AT_specification)));
7858
7859   for (p = child->die_parent->die_child; ; p = p->die_sib)
7860     if (p->die_sib == child)
7861       {
7862         remove_child_with_prev (child, p);
7863         break;
7864       }
7865
7866   add_child_die (parent, child);
7867 }
7868
7869 /* Return a pointer to a newly created DIE node.  */
7870
7871 static inline dw_die_ref
7872 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7873 {
7874   dw_die_ref die = ggc_alloc_cleared_die_node ();
7875
7876   die->die_tag = tag_value;
7877
7878   if (parent_die != NULL)
7879     add_child_die (parent_die, die);
7880   else
7881     {
7882       limbo_die_node *limbo_node;
7883
7884       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7885       limbo_node->die = die;
7886       limbo_node->created_for = t;
7887       limbo_node->next = limbo_die_list;
7888       limbo_die_list = limbo_node;
7889     }
7890
7891   return die;
7892 }
7893
7894 /* Return the DIE associated with the given type specifier.  */
7895
7896 static inline dw_die_ref
7897 lookup_type_die (tree type)
7898 {
7899   return TYPE_SYMTAB_DIE (type);
7900 }
7901
7902 /* Equate a DIE to a given type specifier.  */
7903
7904 static inline void
7905 equate_type_number_to_die (tree type, dw_die_ref type_die)
7906 {
7907   TYPE_SYMTAB_DIE (type) = type_die;
7908 }
7909
7910 /* Returns a hash value for X (which really is a die_struct).  */
7911
7912 static hashval_t
7913 decl_die_table_hash (const void *x)
7914 {
7915   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7916 }
7917
7918 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7919
7920 static int
7921 decl_die_table_eq (const void *x, const void *y)
7922 {
7923   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7924 }
7925
7926 /* Return the DIE associated with a given declaration.  */
7927
7928 static inline dw_die_ref
7929 lookup_decl_die (tree decl)
7930 {
7931   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7932 }
7933
7934 /* Returns a hash value for X (which really is a var_loc_list).  */
7935
7936 static hashval_t
7937 decl_loc_table_hash (const void *x)
7938 {
7939   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7940 }
7941
7942 /* Return nonzero if decl_id of var_loc_list X is the same as
7943    UID of decl *Y.  */
7944
7945 static int
7946 decl_loc_table_eq (const void *x, const void *y)
7947 {
7948   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7949 }
7950
7951 /* Return the var_loc list associated with a given declaration.  */
7952
7953 static inline var_loc_list *
7954 lookup_decl_loc (const_tree decl)
7955 {
7956   if (!decl_loc_table)
7957     return NULL;
7958   return (var_loc_list *)
7959     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7960 }
7961
7962 /* Equate a DIE to a particular declaration.  */
7963
7964 static void
7965 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7966 {
7967   unsigned int decl_id = DECL_UID (decl);
7968   void **slot;
7969
7970   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7971   *slot = decl_die;
7972   decl_die->decl_id = decl_id;
7973 }
7974
7975 /* Return how many bits covers PIECE EXPR_LIST.  */
7976
7977 static int
7978 decl_piece_bitsize (rtx piece)
7979 {
7980   int ret = (int) GET_MODE (piece);
7981   if (ret)
7982     return ret;
7983   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7984               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7985   return INTVAL (XEXP (XEXP (piece, 0), 0));
7986 }
7987
7988 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7989
7990 static rtx *
7991 decl_piece_varloc_ptr (rtx piece)
7992 {
7993   if ((int) GET_MODE (piece))
7994     return &XEXP (piece, 0);
7995   else
7996     return &XEXP (XEXP (piece, 0), 1);
7997 }
7998
7999 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8000    Next is the chain of following piece nodes.  */
8001
8002 static rtx
8003 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8004 {
8005   if (bitsize <= (int) MAX_MACHINE_MODE)
8006     return alloc_EXPR_LIST (bitsize, loc_note, next);
8007   else
8008     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8009                                                GEN_INT (bitsize),
8010                                                loc_note), next);
8011 }
8012
8013 /* Return rtx that should be stored into loc field for
8014    LOC_NOTE and BITPOS/BITSIZE.  */
8015
8016 static rtx
8017 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8018                       HOST_WIDE_INT bitsize)
8019 {
8020   if (bitsize != -1)
8021     {
8022       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8023       if (bitpos != 0)
8024         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8025     }
8026   return loc_note;
8027 }
8028
8029 /* This function either modifies location piece list *DEST in
8030    place (if SRC and INNER is NULL), or copies location piece list
8031    *SRC to *DEST while modifying it.  Location BITPOS is modified
8032    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8033    not copied and if needed some padding around it is added.
8034    When modifying in place, DEST should point to EXPR_LIST where
8035    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8036    to the start of the whole list and INNER points to the EXPR_LIST
8037    where earlier pieces cover PIECE_BITPOS bits.  */
8038
8039 static void
8040 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8041                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8042                    HOST_WIDE_INT bitsize, rtx loc_note)
8043 {
8044   int diff;
8045   bool copy = inner != NULL;
8046
8047   if (copy)
8048     {
8049       /* First copy all nodes preceeding the current bitpos.  */
8050       while (src != inner)
8051         {
8052           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8053                                    decl_piece_bitsize (*src), NULL_RTX);
8054           dest = &XEXP (*dest, 1);
8055           src = &XEXP (*src, 1);
8056         }
8057     }
8058   /* Add padding if needed.  */
8059   if (bitpos != piece_bitpos)
8060     {
8061       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8062                                copy ? NULL_RTX : *dest);
8063       dest = &XEXP (*dest, 1);
8064     }
8065   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8066     {
8067       gcc_assert (!copy);
8068       /* A piece with correct bitpos and bitsize already exist,
8069          just update the location for it and return.  */
8070       *decl_piece_varloc_ptr (*dest) = loc_note;
8071       return;
8072     }
8073   /* Add the piece that changed.  */
8074   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8075   dest = &XEXP (*dest, 1);
8076   /* Skip over pieces that overlap it.  */
8077   diff = bitpos - piece_bitpos + bitsize;
8078   if (!copy)
8079     src = dest;
8080   while (diff > 0 && *src)
8081     {
8082       rtx piece = *src;
8083       diff -= decl_piece_bitsize (piece);
8084       if (copy)
8085         src = &XEXP (piece, 1);
8086       else
8087         {
8088           *src = XEXP (piece, 1);
8089           free_EXPR_LIST_node (piece);
8090         }
8091     }
8092   /* Add padding if needed.  */
8093   if (diff < 0 && *src)
8094     {
8095       if (!copy)
8096         dest = src;
8097       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8098       dest = &XEXP (*dest, 1);
8099     }
8100   if (!copy)
8101     return;
8102   /* Finally copy all nodes following it.  */
8103   while (*src)
8104     {
8105       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8106                                decl_piece_bitsize (*src), NULL_RTX);
8107       dest = &XEXP (*dest, 1);
8108       src = &XEXP (*src, 1);
8109     }
8110 }
8111
8112 /* Add a variable location node to the linked list for DECL.  */
8113
8114 static struct var_loc_node *
8115 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8116 {
8117   unsigned int decl_id;
8118   var_loc_list *temp;
8119   void **slot;
8120   struct var_loc_node *loc = NULL;
8121   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8122
8123   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8124     {
8125       tree realdecl = DECL_DEBUG_EXPR (decl);
8126       if (realdecl && handled_component_p (realdecl))
8127         {
8128           HOST_WIDE_INT maxsize;
8129           tree innerdecl;
8130           innerdecl
8131             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8132           if (!DECL_P (innerdecl)
8133               || DECL_IGNORED_P (innerdecl)
8134               || TREE_STATIC (innerdecl)
8135               || bitsize <= 0
8136               || bitpos + bitsize > 256
8137               || bitsize != maxsize)
8138             return NULL;
8139           decl = innerdecl;
8140         }
8141     }
8142
8143   decl_id = DECL_UID (decl);
8144   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8145   if (*slot == NULL)
8146     {
8147       temp = ggc_alloc_cleared_var_loc_list ();
8148       temp->decl_id = decl_id;
8149       *slot = temp;
8150     }
8151   else
8152     temp = (var_loc_list *) *slot;
8153
8154   if (temp->last)
8155     {
8156       struct var_loc_node *last = temp->last, *unused = NULL;
8157       rtx *piece_loc = NULL, last_loc_note;
8158       int piece_bitpos = 0;
8159       if (last->next)
8160         {
8161           last = last->next;
8162           gcc_assert (last->next == NULL);
8163         }
8164       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8165         {
8166           piece_loc = &last->loc;
8167           do
8168             {
8169               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8170               if (piece_bitpos + cur_bitsize > bitpos)
8171                 break;
8172               piece_bitpos += cur_bitsize;
8173               piece_loc = &XEXP (*piece_loc, 1);
8174             }
8175           while (*piece_loc);
8176         }
8177       /* TEMP->LAST here is either pointer to the last but one or
8178          last element in the chained list, LAST is pointer to the
8179          last element.  */
8180       if (label && strcmp (last->label, label) == 0)
8181         {
8182           /* For SRA optimized variables if there weren't any real
8183              insns since last note, just modify the last node.  */
8184           if (piece_loc != NULL)
8185             {
8186               adjust_piece_list (piece_loc, NULL, NULL,
8187                                  bitpos, piece_bitpos, bitsize, loc_note);
8188               return NULL;
8189             }
8190           /* If the last note doesn't cover any instructions, remove it.  */
8191           if (temp->last != last)
8192             {
8193               temp->last->next = NULL;
8194               unused = last;
8195               last = temp->last;
8196               gcc_assert (strcmp (last->label, label) != 0);
8197             }
8198           else
8199             {
8200               gcc_assert (temp->first == temp->last);
8201               memset (temp->last, '\0', sizeof (*temp->last));
8202               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8203               return temp->last;
8204             }
8205         }
8206       if (bitsize == -1 && NOTE_P (last->loc))
8207         last_loc_note = last->loc;
8208       else if (piece_loc != NULL
8209                && *piece_loc != NULL_RTX
8210                && piece_bitpos == bitpos
8211                && decl_piece_bitsize (*piece_loc) == bitsize)
8212         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8213       else
8214         last_loc_note = NULL_RTX;
8215       /* If the current location is the same as the end of the list,
8216          and either both or neither of the locations is uninitialized,
8217          we have nothing to do.  */
8218       if (last_loc_note == NULL_RTX
8219           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8220                             NOTE_VAR_LOCATION_LOC (loc_note)))
8221           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8222                != NOTE_VAR_LOCATION_STATUS (loc_note))
8223               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8224                    == VAR_INIT_STATUS_UNINITIALIZED)
8225                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8226                       == VAR_INIT_STATUS_UNINITIALIZED))))
8227         {
8228           /* Add LOC to the end of list and update LAST.  If the last
8229              element of the list has been removed above, reuse its
8230              memory for the new node, otherwise allocate a new one.  */
8231           if (unused)
8232             {
8233               loc = unused;
8234               memset (loc, '\0', sizeof (*loc));
8235             }
8236           else
8237             loc = ggc_alloc_cleared_var_loc_node ();
8238           if (bitsize == -1 || piece_loc == NULL)
8239             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8240           else
8241             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8242                                bitpos, piece_bitpos, bitsize, loc_note);
8243           last->next = loc;
8244           /* Ensure TEMP->LAST will point either to the new last but one
8245              element of the chain, or to the last element in it.  */
8246           if (last != temp->last)
8247             temp->last = last;
8248         }
8249       else if (unused)
8250         ggc_free (unused);
8251     }
8252   else
8253     {
8254       loc = ggc_alloc_cleared_var_loc_node ();
8255       temp->first = loc;
8256       temp->last = loc;
8257       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8258     }
8259   return loc;
8260 }
8261 \f
8262 /* Keep track of the number of spaces used to indent the
8263    output of the debugging routines that print the structure of
8264    the DIE internal representation.  */
8265 static int print_indent;
8266
8267 /* Indent the line the number of spaces given by print_indent.  */
8268
8269 static inline void
8270 print_spaces (FILE *outfile)
8271 {
8272   fprintf (outfile, "%*s", print_indent, "");
8273 }
8274
8275 /* Print a type signature in hex.  */
8276
8277 static inline void
8278 print_signature (FILE *outfile, char *sig)
8279 {
8280   int i;
8281
8282   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8283     fprintf (outfile, "%02x", sig[i] & 0xff);
8284 }
8285
8286 /* Print the information associated with a given DIE, and its children.
8287    This routine is a debugging aid only.  */
8288
8289 static void
8290 print_die (dw_die_ref die, FILE *outfile)
8291 {
8292   dw_attr_ref a;
8293   dw_die_ref c;
8294   unsigned ix;
8295
8296   print_spaces (outfile);
8297   fprintf (outfile, "DIE %4ld: %s\n",
8298            die->die_offset, dwarf_tag_name (die->die_tag));
8299   print_spaces (outfile);
8300   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8301   fprintf (outfile, " offset: %ld\n", die->die_offset);
8302   if (dwarf_version >= 4 && die->die_id.die_type_node)
8303     {
8304       print_spaces (outfile);
8305       fprintf (outfile, "  signature: ");
8306       print_signature (outfile, die->die_id.die_type_node->signature);
8307       fprintf (outfile, "\n");
8308     }
8309
8310   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8311     {
8312       print_spaces (outfile);
8313       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8314
8315       switch (AT_class (a))
8316         {
8317         case dw_val_class_addr:
8318           fprintf (outfile, "address");
8319           break;
8320         case dw_val_class_offset:
8321           fprintf (outfile, "offset");
8322           break;
8323         case dw_val_class_loc:
8324           fprintf (outfile, "location descriptor");
8325           break;
8326         case dw_val_class_loc_list:
8327           fprintf (outfile, "location list -> label:%s",
8328                    AT_loc_list (a)->ll_symbol);
8329           break;
8330         case dw_val_class_range_list:
8331           fprintf (outfile, "range list");
8332           break;
8333         case dw_val_class_const:
8334           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8335           break;
8336         case dw_val_class_unsigned_const:
8337           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8338           break;
8339         case dw_val_class_const_double:
8340           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8341                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8342                    a->dw_attr_val.v.val_double.high,
8343                    a->dw_attr_val.v.val_double.low);
8344           break;
8345         case dw_val_class_vec:
8346           fprintf (outfile, "floating-point or vector constant");
8347           break;
8348         case dw_val_class_flag:
8349           fprintf (outfile, "%u", AT_flag (a));
8350           break;
8351         case dw_val_class_die_ref:
8352           if (AT_ref (a) != NULL)
8353             {
8354               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8355                 {
8356                   fprintf (outfile, "die -> signature: ");
8357                   print_signature (outfile,
8358                                    AT_ref (a)->die_id.die_type_node->signature);
8359                 }
8360               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8361                 fprintf (outfile, "die -> label: %s",
8362                          AT_ref (a)->die_id.die_symbol);
8363               else
8364                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8365             }
8366           else
8367             fprintf (outfile, "die -> <null>");
8368           break;
8369         case dw_val_class_vms_delta:
8370           fprintf (outfile, "delta: @slotcount(%s-%s)",
8371                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8372           break;
8373         case dw_val_class_lbl_id:
8374         case dw_val_class_lineptr:
8375         case dw_val_class_macptr:
8376           fprintf (outfile, "label: %s", AT_lbl (a));
8377           break;
8378         case dw_val_class_str:
8379           if (AT_string (a) != NULL)
8380             fprintf (outfile, "\"%s\"", AT_string (a));
8381           else
8382             fprintf (outfile, "<null>");
8383           break;
8384         case dw_val_class_file:
8385           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8386                    AT_file (a)->emitted_number);
8387           break;
8388         case dw_val_class_data8:
8389           {
8390             int i;
8391
8392             for (i = 0; i < 8; i++)
8393               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8394             break;
8395           }
8396         default:
8397           break;
8398         }
8399
8400       fprintf (outfile, "\n");
8401     }
8402
8403   if (die->die_child != NULL)
8404     {
8405       print_indent += 4;
8406       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8407       print_indent -= 4;
8408     }
8409   if (print_indent == 0)
8410     fprintf (outfile, "\n");
8411 }
8412
8413 /* Print the contents of the source code line number correspondence table.
8414    This routine is a debugging aid only.  */
8415
8416 static void
8417 print_dwarf_line_table (FILE *outfile)
8418 {
8419   unsigned i;
8420   dw_line_info_ref line_info;
8421
8422   fprintf (outfile, "\n\nDWARF source line information\n");
8423   for (i = 1; i < line_info_table_in_use; i++)
8424     {
8425       line_info = &line_info_table[i];
8426       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8427                line_info->dw_file_num,
8428                line_info->dw_line_num);
8429     }
8430
8431   fprintf (outfile, "\n\n");
8432 }
8433
8434 /* Print the information collected for a given DIE.  */
8435
8436 DEBUG_FUNCTION void
8437 debug_dwarf_die (dw_die_ref die)
8438 {
8439   print_die (die, stderr);
8440 }
8441
8442 /* Print all DWARF information collected for the compilation unit.
8443    This routine is a debugging aid only.  */
8444
8445 DEBUG_FUNCTION void
8446 debug_dwarf (void)
8447 {
8448   print_indent = 0;
8449   print_die (comp_unit_die, stderr);
8450   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8451     print_dwarf_line_table (stderr);
8452 }
8453 \f
8454 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8455    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8456    DIE that marks the start of the DIEs for this include file.  */
8457
8458 static dw_die_ref
8459 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8460 {
8461   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8462   dw_die_ref new_unit = gen_compile_unit_die (filename);
8463
8464   new_unit->die_sib = old_unit;
8465   return new_unit;
8466 }
8467
8468 /* Close an include-file CU and reopen the enclosing one.  */
8469
8470 static dw_die_ref
8471 pop_compile_unit (dw_die_ref old_unit)
8472 {
8473   dw_die_ref new_unit = old_unit->die_sib;
8474
8475   old_unit->die_sib = NULL;
8476   return new_unit;
8477 }
8478
8479 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8480 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8481
8482 /* Calculate the checksum of a location expression.  */
8483
8484 static inline void
8485 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8486 {
8487   int tem;
8488
8489   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8490   CHECKSUM (tem);
8491   CHECKSUM (loc->dw_loc_oprnd1);
8492   CHECKSUM (loc->dw_loc_oprnd2);
8493 }
8494
8495 /* Calculate the checksum of an attribute.  */
8496
8497 static void
8498 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8499 {
8500   dw_loc_descr_ref loc;
8501   rtx r;
8502
8503   CHECKSUM (at->dw_attr);
8504
8505   /* We don't care that this was compiled with a different compiler
8506      snapshot; if the output is the same, that's what matters.  */
8507   if (at->dw_attr == DW_AT_producer)
8508     return;
8509
8510   switch (AT_class (at))
8511     {
8512     case dw_val_class_const:
8513       CHECKSUM (at->dw_attr_val.v.val_int);
8514       break;
8515     case dw_val_class_unsigned_const:
8516       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8517       break;
8518     case dw_val_class_const_double:
8519       CHECKSUM (at->dw_attr_val.v.val_double);
8520       break;
8521     case dw_val_class_vec:
8522       CHECKSUM (at->dw_attr_val.v.val_vec);
8523       break;
8524     case dw_val_class_flag:
8525       CHECKSUM (at->dw_attr_val.v.val_flag);
8526       break;
8527     case dw_val_class_str:
8528       CHECKSUM_STRING (AT_string (at));
8529       break;
8530
8531     case dw_val_class_addr:
8532       r = AT_addr (at);
8533       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8534       CHECKSUM_STRING (XSTR (r, 0));
8535       break;
8536
8537     case dw_val_class_offset:
8538       CHECKSUM (at->dw_attr_val.v.val_offset);
8539       break;
8540
8541     case dw_val_class_loc:
8542       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8543         loc_checksum (loc, ctx);
8544       break;
8545
8546     case dw_val_class_die_ref:
8547       die_checksum (AT_ref (at), ctx, mark);
8548       break;
8549
8550     case dw_val_class_fde_ref:
8551     case dw_val_class_vms_delta:
8552     case dw_val_class_lbl_id:
8553     case dw_val_class_lineptr:
8554     case dw_val_class_macptr:
8555       break;
8556
8557     case dw_val_class_file:
8558       CHECKSUM_STRING (AT_file (at)->filename);
8559       break;
8560
8561     case dw_val_class_data8:
8562       CHECKSUM (at->dw_attr_val.v.val_data8);
8563       break;
8564
8565     default:
8566       break;
8567     }
8568 }
8569
8570 /* Calculate the checksum of a DIE.  */
8571
8572 static void
8573 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8574 {
8575   dw_die_ref c;
8576   dw_attr_ref a;
8577   unsigned ix;
8578
8579   /* To avoid infinite recursion.  */
8580   if (die->die_mark)
8581     {
8582       CHECKSUM (die->die_mark);
8583       return;
8584     }
8585   die->die_mark = ++(*mark);
8586
8587   CHECKSUM (die->die_tag);
8588
8589   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8590     attr_checksum (a, ctx, mark);
8591
8592   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8593 }
8594
8595 #undef CHECKSUM
8596 #undef CHECKSUM_STRING
8597
8598 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8599 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8600 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8601 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8602 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8603 #define CHECKSUM_ATTR(FOO) \
8604   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8605
8606 /* Calculate the checksum of a number in signed LEB128 format.  */
8607
8608 static void
8609 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8610 {
8611   unsigned char byte;
8612   bool more;
8613
8614   while (1)
8615     {
8616       byte = (value & 0x7f);
8617       value >>= 7;
8618       more = !((value == 0 && (byte & 0x40) == 0)
8619                 || (value == -1 && (byte & 0x40) != 0));
8620       if (more)
8621         byte |= 0x80;
8622       CHECKSUM (byte);
8623       if (!more)
8624         break;
8625     }
8626 }
8627
8628 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8629
8630 static void
8631 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8632 {
8633   while (1)
8634     {
8635       unsigned char byte = (value & 0x7f);
8636       value >>= 7;
8637       if (value != 0)
8638         /* More bytes to follow.  */
8639         byte |= 0x80;
8640       CHECKSUM (byte);
8641       if (value == 0)
8642         break;
8643     }
8644 }
8645
8646 /* Checksum the context of the DIE.  This adds the names of any
8647    surrounding namespaces or structures to the checksum.  */
8648
8649 static void
8650 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8651 {
8652   const char *name;
8653   dw_die_ref spec;
8654   int tag = die->die_tag;
8655
8656   if (tag != DW_TAG_namespace
8657       && tag != DW_TAG_structure_type
8658       && tag != DW_TAG_class_type)
8659     return;
8660
8661   name = get_AT_string (die, DW_AT_name);
8662
8663   spec = get_AT_ref (die, DW_AT_specification);
8664   if (spec != NULL)
8665     die = spec;
8666
8667   if (die->die_parent != NULL)
8668     checksum_die_context (die->die_parent, ctx);
8669
8670   CHECKSUM_ULEB128 ('C');
8671   CHECKSUM_ULEB128 (tag);
8672   if (name != NULL)
8673     CHECKSUM_STRING (name);
8674 }
8675
8676 /* Calculate the checksum of a location expression.  */
8677
8678 static inline void
8679 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8680 {
8681   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8682      were emitted as a DW_FORM_sdata instead of a location expression.  */
8683   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8684     {
8685       CHECKSUM_ULEB128 (DW_FORM_sdata);
8686       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8687       return;
8688     }
8689
8690   /* Otherwise, just checksum the raw location expression.  */
8691   while (loc != NULL)
8692     {
8693       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8694       CHECKSUM (loc->dw_loc_oprnd1);
8695       CHECKSUM (loc->dw_loc_oprnd2);
8696       loc = loc->dw_loc_next;
8697     }
8698 }
8699
8700 /* Calculate the checksum of an attribute.  */
8701
8702 static void
8703 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8704                        struct md5_ctx *ctx, int *mark)
8705 {
8706   dw_loc_descr_ref loc;
8707   rtx r;
8708
8709   if (AT_class (at) == dw_val_class_die_ref)
8710     {
8711       dw_die_ref target_die = AT_ref (at);
8712
8713       /* For pointer and reference types, we checksum only the (qualified)
8714          name of the target type (if there is a name).  For friend entries,
8715          we checksum only the (qualified) name of the target type or function.
8716          This allows the checksum to remain the same whether the target type
8717          is complete or not.  */
8718       if ((at->dw_attr == DW_AT_type
8719            && (tag == DW_TAG_pointer_type
8720                || tag == DW_TAG_reference_type
8721                || tag == DW_TAG_rvalue_reference_type
8722                || tag == DW_TAG_ptr_to_member_type))
8723           || (at->dw_attr == DW_AT_friend
8724               && tag == DW_TAG_friend))
8725         {
8726           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8727
8728           if (name_attr != NULL)
8729             {
8730               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8731
8732               if (decl == NULL)
8733                 decl = target_die;
8734               CHECKSUM_ULEB128 ('N');
8735               CHECKSUM_ULEB128 (at->dw_attr);
8736               if (decl->die_parent != NULL)
8737                 checksum_die_context (decl->die_parent, ctx);
8738               CHECKSUM_ULEB128 ('E');
8739               CHECKSUM_STRING (AT_string (name_attr));
8740               return;
8741             }
8742         }
8743
8744       /* For all other references to another DIE, we check to see if the
8745          target DIE has already been visited.  If it has, we emit a
8746          backward reference; if not, we descend recursively.  */
8747       if (target_die->die_mark > 0)
8748         {
8749           CHECKSUM_ULEB128 ('R');
8750           CHECKSUM_ULEB128 (at->dw_attr);
8751           CHECKSUM_ULEB128 (target_die->die_mark);
8752         }
8753       else
8754         {
8755           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8756
8757           if (decl == NULL)
8758             decl = target_die;
8759           target_die->die_mark = ++(*mark);
8760           CHECKSUM_ULEB128 ('T');
8761           CHECKSUM_ULEB128 (at->dw_attr);
8762           if (decl->die_parent != NULL)
8763             checksum_die_context (decl->die_parent, ctx);
8764           die_checksum_ordered (target_die, ctx, mark);
8765         }
8766       return;
8767     }
8768
8769   CHECKSUM_ULEB128 ('A');
8770   CHECKSUM_ULEB128 (at->dw_attr);
8771
8772   switch (AT_class (at))
8773     {
8774     case dw_val_class_const:
8775       CHECKSUM_ULEB128 (DW_FORM_sdata);
8776       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8777       break;
8778
8779     case dw_val_class_unsigned_const:
8780       CHECKSUM_ULEB128 (DW_FORM_sdata);
8781       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8782       break;
8783
8784     case dw_val_class_const_double:
8785       CHECKSUM_ULEB128 (DW_FORM_block);
8786       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8787       CHECKSUM (at->dw_attr_val.v.val_double);
8788       break;
8789
8790     case dw_val_class_vec:
8791       CHECKSUM_ULEB128 (DW_FORM_block);
8792       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8793       CHECKSUM (at->dw_attr_val.v.val_vec);
8794       break;
8795
8796     case dw_val_class_flag:
8797       CHECKSUM_ULEB128 (DW_FORM_flag);
8798       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8799       break;
8800
8801     case dw_val_class_str:
8802       CHECKSUM_ULEB128 (DW_FORM_string);
8803       CHECKSUM_STRING (AT_string (at));
8804       break;
8805
8806     case dw_val_class_addr:
8807       r = AT_addr (at);
8808       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8809       CHECKSUM_ULEB128 (DW_FORM_string);
8810       CHECKSUM_STRING (XSTR (r, 0));
8811       break;
8812
8813     case dw_val_class_offset:
8814       CHECKSUM_ULEB128 (DW_FORM_sdata);
8815       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8816       break;
8817
8818     case dw_val_class_loc:
8819       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8820         loc_checksum_ordered (loc, ctx);
8821       break;
8822
8823     case dw_val_class_fde_ref:
8824     case dw_val_class_lbl_id:
8825     case dw_val_class_lineptr:
8826     case dw_val_class_macptr:
8827       break;
8828
8829     case dw_val_class_file:
8830       CHECKSUM_ULEB128 (DW_FORM_string);
8831       CHECKSUM_STRING (AT_file (at)->filename);
8832       break;
8833
8834     case dw_val_class_data8:
8835       CHECKSUM (at->dw_attr_val.v.val_data8);
8836       break;
8837
8838     default:
8839       break;
8840     }
8841 }
8842
8843 struct checksum_attributes
8844 {
8845   dw_attr_ref at_name;
8846   dw_attr_ref at_type;
8847   dw_attr_ref at_friend;
8848   dw_attr_ref at_accessibility;
8849   dw_attr_ref at_address_class;
8850   dw_attr_ref at_allocated;
8851   dw_attr_ref at_artificial;
8852   dw_attr_ref at_associated;
8853   dw_attr_ref at_binary_scale;
8854   dw_attr_ref at_bit_offset;
8855   dw_attr_ref at_bit_size;
8856   dw_attr_ref at_bit_stride;
8857   dw_attr_ref at_byte_size;
8858   dw_attr_ref at_byte_stride;
8859   dw_attr_ref at_const_value;
8860   dw_attr_ref at_containing_type;
8861   dw_attr_ref at_count;
8862   dw_attr_ref at_data_location;
8863   dw_attr_ref at_data_member_location;
8864   dw_attr_ref at_decimal_scale;
8865   dw_attr_ref at_decimal_sign;
8866   dw_attr_ref at_default_value;
8867   dw_attr_ref at_digit_count;
8868   dw_attr_ref at_discr;
8869   dw_attr_ref at_discr_list;
8870   dw_attr_ref at_discr_value;
8871   dw_attr_ref at_encoding;
8872   dw_attr_ref at_endianity;
8873   dw_attr_ref at_explicit;
8874   dw_attr_ref at_is_optional;
8875   dw_attr_ref at_location;
8876   dw_attr_ref at_lower_bound;
8877   dw_attr_ref at_mutable;
8878   dw_attr_ref at_ordering;
8879   dw_attr_ref at_picture_string;
8880   dw_attr_ref at_prototyped;
8881   dw_attr_ref at_small;
8882   dw_attr_ref at_segment;
8883   dw_attr_ref at_string_length;
8884   dw_attr_ref at_threads_scaled;
8885   dw_attr_ref at_upper_bound;
8886   dw_attr_ref at_use_location;
8887   dw_attr_ref at_use_UTF8;
8888   dw_attr_ref at_variable_parameter;
8889   dw_attr_ref at_virtuality;
8890   dw_attr_ref at_visibility;
8891   dw_attr_ref at_vtable_elem_location;
8892 };
8893
8894 /* Collect the attributes that we will want to use for the checksum.  */
8895
8896 static void
8897 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8898 {
8899   dw_attr_ref a;
8900   unsigned ix;
8901
8902   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8903     {
8904       switch (a->dw_attr)
8905         {
8906         case DW_AT_name:
8907           attrs->at_name = a;
8908           break;
8909         case DW_AT_type:
8910           attrs->at_type = a;
8911           break;
8912         case DW_AT_friend:
8913           attrs->at_friend = a;
8914           break;
8915         case DW_AT_accessibility:
8916           attrs->at_accessibility = a;
8917           break;
8918         case DW_AT_address_class:
8919           attrs->at_address_class = a;
8920           break;
8921         case DW_AT_allocated:
8922           attrs->at_allocated = a;
8923           break;
8924         case DW_AT_artificial:
8925           attrs->at_artificial = a;
8926           break;
8927         case DW_AT_associated:
8928           attrs->at_associated = a;
8929           break;
8930         case DW_AT_binary_scale:
8931           attrs->at_binary_scale = a;
8932           break;
8933         case DW_AT_bit_offset:
8934           attrs->at_bit_offset = a;
8935           break;
8936         case DW_AT_bit_size:
8937           attrs->at_bit_size = a;
8938           break;
8939         case DW_AT_bit_stride:
8940           attrs->at_bit_stride = a;
8941           break;
8942         case DW_AT_byte_size:
8943           attrs->at_byte_size = a;
8944           break;
8945         case DW_AT_byte_stride:
8946           attrs->at_byte_stride = a;
8947           break;
8948         case DW_AT_const_value:
8949           attrs->at_const_value = a;
8950           break;
8951         case DW_AT_containing_type:
8952           attrs->at_containing_type = a;
8953           break;
8954         case DW_AT_count:
8955           attrs->at_count = a;
8956           break;
8957         case DW_AT_data_location:
8958           attrs->at_data_location = a;
8959           break;
8960         case DW_AT_data_member_location:
8961           attrs->at_data_member_location = a;
8962           break;
8963         case DW_AT_decimal_scale:
8964           attrs->at_decimal_scale = a;
8965           break;
8966         case DW_AT_decimal_sign:
8967           attrs->at_decimal_sign = a;
8968           break;
8969         case DW_AT_default_value:
8970           attrs->at_default_value = a;
8971           break;
8972         case DW_AT_digit_count:
8973           attrs->at_digit_count = a;
8974           break;
8975         case DW_AT_discr:
8976           attrs->at_discr = a;
8977           break;
8978         case DW_AT_discr_list:
8979           attrs->at_discr_list = a;
8980           break;
8981         case DW_AT_discr_value:
8982           attrs->at_discr_value = a;
8983           break;
8984         case DW_AT_encoding:
8985           attrs->at_encoding = a;
8986           break;
8987         case DW_AT_endianity:
8988           attrs->at_endianity = a;
8989           break;
8990         case DW_AT_explicit:
8991           attrs->at_explicit = a;
8992           break;
8993         case DW_AT_is_optional:
8994           attrs->at_is_optional = a;
8995           break;
8996         case DW_AT_location:
8997           attrs->at_location = a;
8998           break;
8999         case DW_AT_lower_bound:
9000           attrs->at_lower_bound = a;
9001           break;
9002         case DW_AT_mutable:
9003           attrs->at_mutable = a;
9004           break;
9005         case DW_AT_ordering:
9006           attrs->at_ordering = a;
9007           break;
9008         case DW_AT_picture_string:
9009           attrs->at_picture_string = a;
9010           break;
9011         case DW_AT_prototyped:
9012           attrs->at_prototyped = a;
9013           break;
9014         case DW_AT_small:
9015           attrs->at_small = a;
9016           break;
9017         case DW_AT_segment:
9018           attrs->at_segment = a;
9019           break;
9020         case DW_AT_string_length:
9021           attrs->at_string_length = a;
9022           break;
9023         case DW_AT_threads_scaled:
9024           attrs->at_threads_scaled = a;
9025           break;
9026         case DW_AT_upper_bound:
9027           attrs->at_upper_bound = a;
9028           break;
9029         case DW_AT_use_location:
9030           attrs->at_use_location = a;
9031           break;
9032         case DW_AT_use_UTF8:
9033           attrs->at_use_UTF8 = a;
9034           break;
9035         case DW_AT_variable_parameter:
9036           attrs->at_variable_parameter = a;
9037           break;
9038         case DW_AT_virtuality:
9039           attrs->at_virtuality = a;
9040           break;
9041         case DW_AT_visibility:
9042           attrs->at_visibility = a;
9043           break;
9044         case DW_AT_vtable_elem_location:
9045           attrs->at_vtable_elem_location = a;
9046           break;
9047         default:
9048           break;
9049         }
9050     }
9051 }
9052
9053 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9054
9055 static void
9056 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9057 {
9058   dw_die_ref c;
9059   dw_die_ref decl;
9060   struct checksum_attributes attrs;
9061
9062   CHECKSUM_ULEB128 ('D');
9063   CHECKSUM_ULEB128 (die->die_tag);
9064
9065   memset (&attrs, 0, sizeof (attrs));
9066
9067   decl = get_AT_ref (die, DW_AT_specification);
9068   if (decl != NULL)
9069     collect_checksum_attributes (&attrs, decl);
9070   collect_checksum_attributes (&attrs, die);
9071
9072   CHECKSUM_ATTR (attrs.at_name);
9073   CHECKSUM_ATTR (attrs.at_accessibility);
9074   CHECKSUM_ATTR (attrs.at_address_class);
9075   CHECKSUM_ATTR (attrs.at_allocated);
9076   CHECKSUM_ATTR (attrs.at_artificial);
9077   CHECKSUM_ATTR (attrs.at_associated);
9078   CHECKSUM_ATTR (attrs.at_binary_scale);
9079   CHECKSUM_ATTR (attrs.at_bit_offset);
9080   CHECKSUM_ATTR (attrs.at_bit_size);
9081   CHECKSUM_ATTR (attrs.at_bit_stride);
9082   CHECKSUM_ATTR (attrs.at_byte_size);
9083   CHECKSUM_ATTR (attrs.at_byte_stride);
9084   CHECKSUM_ATTR (attrs.at_const_value);
9085   CHECKSUM_ATTR (attrs.at_containing_type);
9086   CHECKSUM_ATTR (attrs.at_count);
9087   CHECKSUM_ATTR (attrs.at_data_location);
9088   CHECKSUM_ATTR (attrs.at_data_member_location);
9089   CHECKSUM_ATTR (attrs.at_decimal_scale);
9090   CHECKSUM_ATTR (attrs.at_decimal_sign);
9091   CHECKSUM_ATTR (attrs.at_default_value);
9092   CHECKSUM_ATTR (attrs.at_digit_count);
9093   CHECKSUM_ATTR (attrs.at_discr);
9094   CHECKSUM_ATTR (attrs.at_discr_list);
9095   CHECKSUM_ATTR (attrs.at_discr_value);
9096   CHECKSUM_ATTR (attrs.at_encoding);
9097   CHECKSUM_ATTR (attrs.at_endianity);
9098   CHECKSUM_ATTR (attrs.at_explicit);
9099   CHECKSUM_ATTR (attrs.at_is_optional);
9100   CHECKSUM_ATTR (attrs.at_location);
9101   CHECKSUM_ATTR (attrs.at_lower_bound);
9102   CHECKSUM_ATTR (attrs.at_mutable);
9103   CHECKSUM_ATTR (attrs.at_ordering);
9104   CHECKSUM_ATTR (attrs.at_picture_string);
9105   CHECKSUM_ATTR (attrs.at_prototyped);
9106   CHECKSUM_ATTR (attrs.at_small);
9107   CHECKSUM_ATTR (attrs.at_segment);
9108   CHECKSUM_ATTR (attrs.at_string_length);
9109   CHECKSUM_ATTR (attrs.at_threads_scaled);
9110   CHECKSUM_ATTR (attrs.at_upper_bound);
9111   CHECKSUM_ATTR (attrs.at_use_location);
9112   CHECKSUM_ATTR (attrs.at_use_UTF8);
9113   CHECKSUM_ATTR (attrs.at_variable_parameter);
9114   CHECKSUM_ATTR (attrs.at_virtuality);
9115   CHECKSUM_ATTR (attrs.at_visibility);
9116   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9117   CHECKSUM_ATTR (attrs.at_type);
9118   CHECKSUM_ATTR (attrs.at_friend);
9119
9120   /* Checksum the child DIEs, except for nested types and member functions.  */
9121   c = die->die_child;
9122   if (c) do {
9123     dw_attr_ref name_attr;
9124
9125     c = c->die_sib;
9126     name_attr = get_AT (c, DW_AT_name);
9127     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9128         && name_attr != NULL)
9129       {
9130         CHECKSUM_ULEB128 ('S');
9131         CHECKSUM_ULEB128 (c->die_tag);
9132         CHECKSUM_STRING (AT_string (name_attr));
9133       }
9134     else
9135       {
9136         /* Mark this DIE so it gets processed when unmarking.  */
9137         if (c->die_mark == 0)
9138           c->die_mark = -1;
9139         die_checksum_ordered (c, ctx, mark);
9140       }
9141   } while (c != die->die_child);
9142
9143   CHECKSUM_ULEB128 (0);
9144 }
9145
9146 #undef CHECKSUM
9147 #undef CHECKSUM_STRING
9148 #undef CHECKSUM_ATTR
9149 #undef CHECKSUM_LEB128
9150 #undef CHECKSUM_ULEB128
9151
9152 /* Generate the type signature for DIE.  This is computed by generating an
9153    MD5 checksum over the DIE's tag, its relevant attributes, and its
9154    children.  Attributes that are references to other DIEs are processed
9155    by recursion, using the MARK field to prevent infinite recursion.
9156    If the DIE is nested inside a namespace or another type, we also
9157    need to include that context in the signature.  The lower 64 bits
9158    of the resulting MD5 checksum comprise the signature.  */
9159
9160 static void
9161 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9162 {
9163   int mark;
9164   const char *name;
9165   unsigned char checksum[16];
9166   struct md5_ctx ctx;
9167   dw_die_ref decl;
9168
9169   name = get_AT_string (die, DW_AT_name);
9170   decl = get_AT_ref (die, DW_AT_specification);
9171
9172   /* First, compute a signature for just the type name (and its surrounding
9173      context, if any.  This is stored in the type unit DIE for link-time
9174      ODR (one-definition rule) checking.  */
9175
9176   if (is_cxx() && name != NULL)
9177     {
9178       md5_init_ctx (&ctx);
9179
9180       /* Checksum the names of surrounding namespaces and structures.  */
9181       if (decl != NULL && decl->die_parent != NULL)
9182         checksum_die_context (decl->die_parent, &ctx);
9183
9184       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9185       md5_process_bytes (name, strlen (name) + 1, &ctx);
9186       md5_finish_ctx (&ctx, checksum);
9187
9188       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9189     }
9190
9191   /* Next, compute the complete type signature.  */
9192
9193   md5_init_ctx (&ctx);
9194   mark = 1;
9195   die->die_mark = mark;
9196
9197   /* Checksum the names of surrounding namespaces and structures.  */
9198   if (decl != NULL && decl->die_parent != NULL)
9199     checksum_die_context (decl->die_parent, &ctx);
9200
9201   /* Checksum the DIE and its children.  */
9202   die_checksum_ordered (die, &ctx, &mark);
9203   unmark_all_dies (die);
9204   md5_finish_ctx (&ctx, checksum);
9205
9206   /* Store the signature in the type node and link the type DIE and the
9207      type node together.  */
9208   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9209           DWARF_TYPE_SIGNATURE_SIZE);
9210   die->die_id.die_type_node = type_node;
9211   type_node->type_die = die;
9212
9213   /* If the DIE is a specification, link its declaration to the type node
9214      as well.  */
9215   if (decl != NULL)
9216     decl->die_id.die_type_node = type_node;
9217 }
9218
9219 /* Do the location expressions look same?  */
9220 static inline int
9221 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9222 {
9223   return loc1->dw_loc_opc == loc2->dw_loc_opc
9224          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9225          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9226 }
9227
9228 /* Do the values look the same?  */
9229 static int
9230 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9231 {
9232   dw_loc_descr_ref loc1, loc2;
9233   rtx r1, r2;
9234
9235   if (v1->val_class != v2->val_class)
9236     return 0;
9237
9238   switch (v1->val_class)
9239     {
9240     case dw_val_class_const:
9241       return v1->v.val_int == v2->v.val_int;
9242     case dw_val_class_unsigned_const:
9243       return v1->v.val_unsigned == v2->v.val_unsigned;
9244     case dw_val_class_const_double:
9245       return v1->v.val_double.high == v2->v.val_double.high
9246              && v1->v.val_double.low == v2->v.val_double.low;
9247     case dw_val_class_vec:
9248       if (v1->v.val_vec.length != v2->v.val_vec.length
9249           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9250         return 0;
9251       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9252                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9253         return 0;
9254       return 1;
9255     case dw_val_class_flag:
9256       return v1->v.val_flag == v2->v.val_flag;
9257     case dw_val_class_str:
9258       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9259
9260     case dw_val_class_addr:
9261       r1 = v1->v.val_addr;
9262       r2 = v2->v.val_addr;
9263       if (GET_CODE (r1) != GET_CODE (r2))
9264         return 0;
9265       return !rtx_equal_p (r1, r2);
9266
9267     case dw_val_class_offset:
9268       return v1->v.val_offset == v2->v.val_offset;
9269
9270     case dw_val_class_loc:
9271       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9272            loc1 && loc2;
9273            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9274         if (!same_loc_p (loc1, loc2, mark))
9275           return 0;
9276       return !loc1 && !loc2;
9277
9278     case dw_val_class_die_ref:
9279       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9280
9281     case dw_val_class_fde_ref:
9282     case dw_val_class_vms_delta:
9283     case dw_val_class_lbl_id:
9284     case dw_val_class_lineptr:
9285     case dw_val_class_macptr:
9286       return 1;
9287
9288     case dw_val_class_file:
9289       return v1->v.val_file == v2->v.val_file;
9290
9291     case dw_val_class_data8:
9292       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9293
9294     default:
9295       return 1;
9296     }
9297 }
9298
9299 /* Do the attributes look the same?  */
9300
9301 static int
9302 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9303 {
9304   if (at1->dw_attr != at2->dw_attr)
9305     return 0;
9306
9307   /* We don't care that this was compiled with a different compiler
9308      snapshot; if the output is the same, that's what matters. */
9309   if (at1->dw_attr == DW_AT_producer)
9310     return 1;
9311
9312   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9313 }
9314
9315 /* Do the dies look the same?  */
9316
9317 static int
9318 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9319 {
9320   dw_die_ref c1, c2;
9321   dw_attr_ref a1;
9322   unsigned ix;
9323
9324   /* To avoid infinite recursion.  */
9325   if (die1->die_mark)
9326     return die1->die_mark == die2->die_mark;
9327   die1->die_mark = die2->die_mark = ++(*mark);
9328
9329   if (die1->die_tag != die2->die_tag)
9330     return 0;
9331
9332   if (VEC_length (dw_attr_node, die1->die_attr)
9333       != VEC_length (dw_attr_node, die2->die_attr))
9334     return 0;
9335
9336   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9337     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9338       return 0;
9339
9340   c1 = die1->die_child;
9341   c2 = die2->die_child;
9342   if (! c1)
9343     {
9344       if (c2)
9345         return 0;
9346     }
9347   else
9348     for (;;)
9349       {
9350         if (!same_die_p (c1, c2, mark))
9351           return 0;
9352         c1 = c1->die_sib;
9353         c2 = c2->die_sib;
9354         if (c1 == die1->die_child)
9355           {
9356             if (c2 == die2->die_child)
9357               break;
9358             else
9359               return 0;
9360           }
9361     }
9362
9363   return 1;
9364 }
9365
9366 /* Do the dies look the same?  Wrapper around same_die_p.  */
9367
9368 static int
9369 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9370 {
9371   int mark = 0;
9372   int ret = same_die_p (die1, die2, &mark);
9373
9374   unmark_all_dies (die1);
9375   unmark_all_dies (die2);
9376
9377   return ret;
9378 }
9379
9380 /* The prefix to attach to symbols on DIEs in the current comdat debug
9381    info section.  */
9382 static char *comdat_symbol_id;
9383
9384 /* The index of the current symbol within the current comdat CU.  */
9385 static unsigned int comdat_symbol_number;
9386
9387 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9388    children, and set comdat_symbol_id accordingly.  */
9389
9390 static void
9391 compute_section_prefix (dw_die_ref unit_die)
9392 {
9393   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9394   const char *base = die_name ? lbasename (die_name) : "anonymous";
9395   char *name = XALLOCAVEC (char, strlen (base) + 64);
9396   char *p;
9397   int i, mark;
9398   unsigned char checksum[16];
9399   struct md5_ctx ctx;
9400
9401   /* Compute the checksum of the DIE, then append part of it as hex digits to
9402      the name filename of the unit.  */
9403
9404   md5_init_ctx (&ctx);
9405   mark = 0;
9406   die_checksum (unit_die, &ctx, &mark);
9407   unmark_all_dies (unit_die);
9408   md5_finish_ctx (&ctx, checksum);
9409
9410   sprintf (name, "%s.", base);
9411   clean_symbol_name (name);
9412
9413   p = name + strlen (name);
9414   for (i = 0; i < 4; i++)
9415     {
9416       sprintf (p, "%.2x", checksum[i]);
9417       p += 2;
9418     }
9419
9420   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9421   comdat_symbol_number = 0;
9422 }
9423
9424 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9425
9426 static int
9427 is_type_die (dw_die_ref die)
9428 {
9429   switch (die->die_tag)
9430     {
9431     case DW_TAG_array_type:
9432     case DW_TAG_class_type:
9433     case DW_TAG_interface_type:
9434     case DW_TAG_enumeration_type:
9435     case DW_TAG_pointer_type:
9436     case DW_TAG_reference_type:
9437     case DW_TAG_rvalue_reference_type:
9438     case DW_TAG_string_type:
9439     case DW_TAG_structure_type:
9440     case DW_TAG_subroutine_type:
9441     case DW_TAG_union_type:
9442     case DW_TAG_ptr_to_member_type:
9443     case DW_TAG_set_type:
9444     case DW_TAG_subrange_type:
9445     case DW_TAG_base_type:
9446     case DW_TAG_const_type:
9447     case DW_TAG_file_type:
9448     case DW_TAG_packed_type:
9449     case DW_TAG_volatile_type:
9450     case DW_TAG_typedef:
9451       return 1;
9452     default:
9453       return 0;
9454     }
9455 }
9456
9457 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9458    Basically, we want to choose the bits that are likely to be shared between
9459    compilations (types) and leave out the bits that are specific to individual
9460    compilations (functions).  */
9461
9462 static int
9463 is_comdat_die (dw_die_ref c)
9464 {
9465   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9466      we do for stabs.  The advantage is a greater likelihood of sharing between
9467      objects that don't include headers in the same order (and therefore would
9468      put the base types in a different comdat).  jason 8/28/00 */
9469
9470   if (c->die_tag == DW_TAG_base_type)
9471     return 0;
9472
9473   if (c->die_tag == DW_TAG_pointer_type
9474       || c->die_tag == DW_TAG_reference_type
9475       || c->die_tag == DW_TAG_rvalue_reference_type
9476       || c->die_tag == DW_TAG_const_type
9477       || c->die_tag == DW_TAG_volatile_type)
9478     {
9479       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9480
9481       return t ? is_comdat_die (t) : 0;
9482     }
9483
9484   return is_type_die (c);
9485 }
9486
9487 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9488    compilation unit.  */
9489
9490 static int
9491 is_symbol_die (dw_die_ref c)
9492 {
9493   return (is_type_die (c)
9494           || is_declaration_die (c)
9495           || c->die_tag == DW_TAG_namespace
9496           || c->die_tag == DW_TAG_module);
9497 }
9498
9499 static char *
9500 gen_internal_sym (const char *prefix)
9501 {
9502   char buf[256];
9503
9504   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9505   return xstrdup (buf);
9506 }
9507
9508 /* Assign symbols to all worthy DIEs under DIE.  */
9509
9510 static void
9511 assign_symbol_names (dw_die_ref die)
9512 {
9513   dw_die_ref c;
9514
9515   if (is_symbol_die (die))
9516     {
9517       if (comdat_symbol_id)
9518         {
9519           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9520
9521           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9522                    comdat_symbol_id, comdat_symbol_number++);
9523           die->die_id.die_symbol = xstrdup (p);
9524         }
9525       else
9526         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9527     }
9528
9529   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9530 }
9531
9532 struct cu_hash_table_entry
9533 {
9534   dw_die_ref cu;
9535   unsigned min_comdat_num, max_comdat_num;
9536   struct cu_hash_table_entry *next;
9537 };
9538
9539 /* Routines to manipulate hash table of CUs.  */
9540 static hashval_t
9541 htab_cu_hash (const void *of)
9542 {
9543   const struct cu_hash_table_entry *const entry =
9544     (const struct cu_hash_table_entry *) of;
9545
9546   return htab_hash_string (entry->cu->die_id.die_symbol);
9547 }
9548
9549 static int
9550 htab_cu_eq (const void *of1, const void *of2)
9551 {
9552   const struct cu_hash_table_entry *const entry1 =
9553     (const struct cu_hash_table_entry *) of1;
9554   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9555
9556   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9557 }
9558
9559 static void
9560 htab_cu_del (void *what)
9561 {
9562   struct cu_hash_table_entry *next,
9563     *entry = (struct cu_hash_table_entry *) what;
9564
9565   while (entry)
9566     {
9567       next = entry->next;
9568       free (entry);
9569       entry = next;
9570     }
9571 }
9572
9573 /* Check whether we have already seen this CU and set up SYM_NUM
9574    accordingly.  */
9575 static int
9576 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9577 {
9578   struct cu_hash_table_entry dummy;
9579   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9580
9581   dummy.max_comdat_num = 0;
9582
9583   slot = (struct cu_hash_table_entry **)
9584     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9585         INSERT);
9586   entry = *slot;
9587
9588   for (; entry; last = entry, entry = entry->next)
9589     {
9590       if (same_die_p_wrap (cu, entry->cu))
9591         break;
9592     }
9593
9594   if (entry)
9595     {
9596       *sym_num = entry->min_comdat_num;
9597       return 1;
9598     }
9599
9600   entry = XCNEW (struct cu_hash_table_entry);
9601   entry->cu = cu;
9602   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9603   entry->next = *slot;
9604   *slot = entry;
9605
9606   return 0;
9607 }
9608
9609 /* Record SYM_NUM to record of CU in HTABLE.  */
9610 static void
9611 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9612 {
9613   struct cu_hash_table_entry **slot, *entry;
9614
9615   slot = (struct cu_hash_table_entry **)
9616     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9617         NO_INSERT);
9618   entry = *slot;
9619
9620   entry->max_comdat_num = sym_num;
9621 }
9622
9623 /* Traverse the DIE (which is always comp_unit_die), and set up
9624    additional compilation units for each of the include files we see
9625    bracketed by BINCL/EINCL.  */
9626
9627 static void
9628 break_out_includes (dw_die_ref die)
9629 {
9630   dw_die_ref c;
9631   dw_die_ref unit = NULL;
9632   limbo_die_node *node, **pnode;
9633   htab_t cu_hash_table;
9634
9635   c = die->die_child;
9636   if (c) do {
9637     dw_die_ref prev = c;
9638     c = c->die_sib;
9639     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9640            || (unit && is_comdat_die (c)))
9641       {
9642         dw_die_ref next = c->die_sib;
9643
9644         /* This DIE is for a secondary CU; remove it from the main one.  */
9645         remove_child_with_prev (c, prev);
9646
9647         if (c->die_tag == DW_TAG_GNU_BINCL)
9648           unit = push_new_compile_unit (unit, c);
9649         else if (c->die_tag == DW_TAG_GNU_EINCL)
9650           unit = pop_compile_unit (unit);
9651         else
9652           add_child_die (unit, c);
9653         c = next;
9654         if (c == die->die_child)
9655           break;
9656       }
9657   } while (c != die->die_child);
9658
9659 #if 0
9660   /* We can only use this in debugging, since the frontend doesn't check
9661      to make sure that we leave every include file we enter.  */
9662   gcc_assert (!unit);
9663 #endif
9664
9665   assign_symbol_names (die);
9666   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9667   for (node = limbo_die_list, pnode = &limbo_die_list;
9668        node;
9669        node = node->next)
9670     {
9671       int is_dupl;
9672
9673       compute_section_prefix (node->die);
9674       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9675                         &comdat_symbol_number);
9676       assign_symbol_names (node->die);
9677       if (is_dupl)
9678         *pnode = node->next;
9679       else
9680         {
9681           pnode = &node->next;
9682           record_comdat_symbol_number (node->die, cu_hash_table,
9683                 comdat_symbol_number);
9684         }
9685     }
9686   htab_delete (cu_hash_table);
9687 }
9688
9689 /* Return non-zero if this DIE is a declaration.  */
9690
9691 static int
9692 is_declaration_die (dw_die_ref die)
9693 {
9694   dw_attr_ref a;
9695   unsigned ix;
9696
9697   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9698     if (a->dw_attr == DW_AT_declaration)
9699       return 1;
9700
9701   return 0;
9702 }
9703
9704 /* Return non-zero if this DIE is nested inside a subprogram.  */
9705
9706 static int
9707 is_nested_in_subprogram (dw_die_ref die)
9708 {
9709   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9710
9711   if (decl == NULL)
9712     decl = die;
9713   return local_scope_p (decl);
9714 }
9715
9716 /* Return non-zero if this is a type DIE that should be moved to a
9717    COMDAT .debug_types section.  */
9718
9719 static int
9720 should_move_die_to_comdat (dw_die_ref die)
9721 {
9722   switch (die->die_tag)
9723     {
9724     case DW_TAG_class_type:
9725     case DW_TAG_structure_type:
9726     case DW_TAG_enumeration_type:
9727     case DW_TAG_union_type:
9728       /* Don't move declarations, inlined instances, or types nested in a
9729          subprogram.  */
9730       if (is_declaration_die (die)
9731           || get_AT (die, DW_AT_abstract_origin)
9732           || is_nested_in_subprogram (die))
9733         return 0;
9734       return 1;
9735     case DW_TAG_array_type:
9736     case DW_TAG_interface_type:
9737     case DW_TAG_pointer_type:
9738     case DW_TAG_reference_type:
9739     case DW_TAG_rvalue_reference_type:
9740     case DW_TAG_string_type:
9741     case DW_TAG_subroutine_type:
9742     case DW_TAG_ptr_to_member_type:
9743     case DW_TAG_set_type:
9744     case DW_TAG_subrange_type:
9745     case DW_TAG_base_type:
9746     case DW_TAG_const_type:
9747     case DW_TAG_file_type:
9748     case DW_TAG_packed_type:
9749     case DW_TAG_volatile_type:
9750     case DW_TAG_typedef:
9751     default:
9752       return 0;
9753     }
9754 }
9755
9756 /* Make a clone of DIE.  */
9757
9758 static dw_die_ref
9759 clone_die (dw_die_ref die)
9760 {
9761   dw_die_ref clone;
9762   dw_attr_ref a;
9763   unsigned ix;
9764
9765   clone = ggc_alloc_cleared_die_node ();
9766   clone->die_tag = die->die_tag;
9767
9768   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9769     add_dwarf_attr (clone, a);
9770
9771   return clone;
9772 }
9773
9774 /* Make a clone of the tree rooted at DIE.  */
9775
9776 static dw_die_ref
9777 clone_tree (dw_die_ref die)
9778 {
9779   dw_die_ref c;
9780   dw_die_ref clone = clone_die (die);
9781
9782   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9783
9784   return clone;
9785 }
9786
9787 /* Make a clone of DIE as a declaration.  */
9788
9789 static dw_die_ref
9790 clone_as_declaration (dw_die_ref die)
9791 {
9792   dw_die_ref clone;
9793   dw_die_ref decl;
9794   dw_attr_ref a;
9795   unsigned ix;
9796
9797   /* If the DIE is already a declaration, just clone it.  */
9798   if (is_declaration_die (die))
9799     return clone_die (die);
9800
9801   /* If the DIE is a specification, just clone its declaration DIE.  */
9802   decl = get_AT_ref (die, DW_AT_specification);
9803   if (decl != NULL)
9804     return clone_die (decl);
9805
9806   clone = ggc_alloc_cleared_die_node ();
9807   clone->die_tag = die->die_tag;
9808
9809   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9810     {
9811       /* We don't want to copy over all attributes.
9812          For example we don't want DW_AT_byte_size because otherwise we will no
9813          longer have a declaration and GDB will treat it as a definition.  */
9814
9815       switch (a->dw_attr)
9816         {
9817         case DW_AT_artificial:
9818         case DW_AT_containing_type:
9819         case DW_AT_external:
9820         case DW_AT_name:
9821         case DW_AT_type:
9822         case DW_AT_virtuality:
9823         case DW_AT_linkage_name:
9824         case DW_AT_MIPS_linkage_name:
9825           add_dwarf_attr (clone, a);
9826           break;
9827         case DW_AT_byte_size:
9828         default:
9829           break;
9830         }
9831     }
9832
9833   if (die->die_id.die_type_node)
9834     add_AT_die_ref (clone, DW_AT_signature, die);
9835
9836   add_AT_flag (clone, DW_AT_declaration, 1);
9837   return clone;
9838 }
9839
9840 /* Copy the declaration context to the new compile unit DIE.  This includes
9841    any surrounding namespace or type declarations.  If the DIE has an
9842    AT_specification attribute, it also includes attributes and children
9843    attached to the specification.  */
9844
9845 static void
9846 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9847 {
9848   dw_die_ref decl;
9849   dw_die_ref new_decl;
9850
9851   decl = get_AT_ref (die, DW_AT_specification);
9852   if (decl == NULL)
9853     decl = die;
9854   else
9855     {
9856       unsigned ix;
9857       dw_die_ref c;
9858       dw_attr_ref a;
9859
9860       /* Copy the type node pointer from the new DIE to the original
9861          declaration DIE so we can forward references later.  */
9862       decl->die_id.die_type_node = die->die_id.die_type_node;
9863
9864       remove_AT (die, DW_AT_specification);
9865
9866       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
9867         {
9868           if (a->dw_attr != DW_AT_name
9869               && a->dw_attr != DW_AT_declaration
9870               && a->dw_attr != DW_AT_external)
9871             add_dwarf_attr (die, a);
9872         }
9873
9874       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9875     }
9876
9877   if (decl->die_parent != NULL
9878       && decl->die_parent->die_tag != DW_TAG_compile_unit
9879       && decl->die_parent->die_tag != DW_TAG_type_unit)
9880     {
9881       new_decl = copy_ancestor_tree (unit, decl, NULL);
9882       if (new_decl != NULL)
9883         {
9884           remove_AT (new_decl, DW_AT_signature);
9885           add_AT_specification (die, new_decl);
9886         }
9887     }
9888 }
9889
9890 /* Generate the skeleton ancestor tree for the given NODE, then clone
9891    the DIE and add the clone into the tree.  */
9892
9893 static void
9894 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9895 {
9896   if (node->new_die != NULL)
9897     return;
9898
9899   node->new_die = clone_as_declaration (node->old_die);
9900
9901   if (node->parent != NULL)
9902     {
9903       generate_skeleton_ancestor_tree (node->parent);
9904       add_child_die (node->parent->new_die, node->new_die);
9905     }
9906 }
9907
9908 /* Generate a skeleton tree of DIEs containing any declarations that are
9909    found in the original tree.  We traverse the tree looking for declaration
9910    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9911
9912 static void
9913 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9914 {
9915   skeleton_chain_node node;
9916   dw_die_ref c;
9917   dw_die_ref first;
9918   dw_die_ref prev = NULL;
9919   dw_die_ref next = NULL;
9920
9921   node.parent = parent;
9922
9923   first = c = parent->old_die->die_child;
9924   if (c)
9925     next = c->die_sib;
9926   if (c) do {
9927     if (prev == NULL || prev->die_sib == c)
9928       prev = c;
9929     c = next;
9930     next = (c == first ? NULL : c->die_sib);
9931     node.old_die = c;
9932     node.new_die = NULL;
9933     if (is_declaration_die (c))
9934       {
9935         /* Clone the existing DIE, move the original to the skeleton
9936            tree (which is in the main CU), and put the clone, with
9937            all the original's children, where the original came from.  */
9938         dw_die_ref clone = clone_die (c);
9939         move_all_children (c, clone);
9940
9941         replace_child (c, clone, prev);
9942         generate_skeleton_ancestor_tree (parent);
9943         add_child_die (parent->new_die, c);
9944         node.new_die = c;
9945         c = clone;
9946       }
9947     generate_skeleton_bottom_up (&node);
9948   } while (next != NULL);
9949 }
9950
9951 /* Wrapper function for generate_skeleton_bottom_up.  */
9952
9953 static dw_die_ref
9954 generate_skeleton (dw_die_ref die)
9955 {
9956   skeleton_chain_node node;
9957
9958   node.old_die = die;
9959   node.new_die = NULL;
9960   node.parent = NULL;
9961
9962   /* If this type definition is nested inside another type,
9963      always leave at least a declaration in its place.  */
9964   if (die->die_parent != NULL && is_type_die (die->die_parent))
9965     node.new_die = clone_as_declaration (die);
9966
9967   generate_skeleton_bottom_up (&node);
9968   return node.new_die;
9969 }
9970
9971 /* Remove the DIE from its parent, possibly replacing it with a cloned
9972    declaration.  The original DIE will be moved to a new compile unit
9973    so that existing references to it follow it to the new location.  If
9974    any of the original DIE's descendants is a declaration, we need to
9975    replace the original DIE with a skeleton tree and move the
9976    declarations back into the skeleton tree.  */
9977
9978 static dw_die_ref
9979 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9980 {
9981   dw_die_ref skeleton;
9982
9983   skeleton = generate_skeleton (child);
9984   if (skeleton == NULL)
9985     remove_child_with_prev (child, prev);
9986   else
9987     {
9988       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9989       replace_child (child, skeleton, prev);
9990     }
9991
9992   return skeleton;
9993 }
9994
9995 /* Traverse the DIE and set up additional .debug_types sections for each
9996    type worthy of being placed in a COMDAT section.  */
9997
9998 static void
9999 break_out_comdat_types (dw_die_ref die)
10000 {
10001   dw_die_ref c;
10002   dw_die_ref first;
10003   dw_die_ref prev = NULL;
10004   dw_die_ref next = NULL;
10005   dw_die_ref unit = NULL;
10006
10007   first = c = die->die_child;
10008   if (c)
10009     next = c->die_sib;
10010   if (c) do {
10011     if (prev == NULL || prev->die_sib == c)
10012       prev = c;
10013     c = next;
10014     next = (c == first ? NULL : c->die_sib);
10015     if (should_move_die_to_comdat (c))
10016       {
10017         dw_die_ref replacement;
10018         comdat_type_node_ref type_node;
10019
10020         /* Create a new type unit DIE as the root for the new tree, and
10021            add it to the list of comdat types.  */
10022         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10023         add_AT_unsigned (unit, DW_AT_language,
10024                          get_AT_unsigned (comp_unit_die, DW_AT_language));
10025         type_node = ggc_alloc_cleared_comdat_type_node ();
10026         type_node->root_die = unit;
10027         type_node->next = comdat_type_list;
10028         comdat_type_list = type_node;
10029
10030         /* Generate the type signature.  */
10031         generate_type_signature (c, type_node);
10032
10033         /* Copy the declaration context, attributes, and children of the
10034            declaration into the new compile unit DIE.  */
10035         copy_declaration_context (unit, c);
10036
10037         /* Remove this DIE from the main CU.  */
10038         replacement = remove_child_or_replace_with_skeleton (c, prev);
10039
10040         /* Break out nested types into their own type units.  */
10041         break_out_comdat_types (c);
10042
10043         /* Add the DIE to the new compunit.  */
10044         add_child_die (unit, c);
10045
10046         if (replacement != NULL)
10047           c = replacement;
10048       }
10049     else if (c->die_tag == DW_TAG_namespace
10050              || c->die_tag == DW_TAG_class_type
10051              || c->die_tag == DW_TAG_structure_type
10052              || c->die_tag == DW_TAG_union_type)
10053       {
10054         /* Look for nested types that can be broken out.  */
10055         break_out_comdat_types (c);
10056       }
10057   } while (next != NULL);
10058 }
10059
10060 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10061
10062 struct decl_table_entry
10063 {
10064   dw_die_ref orig;
10065   dw_die_ref copy;
10066 };
10067
10068 /* Routines to manipulate hash table of copied declarations.  */
10069
10070 static hashval_t
10071 htab_decl_hash (const void *of)
10072 {
10073   const struct decl_table_entry *const entry =
10074     (const struct decl_table_entry *) of;
10075
10076   return htab_hash_pointer (entry->orig);
10077 }
10078
10079 static int
10080 htab_decl_eq (const void *of1, const void *of2)
10081 {
10082   const struct decl_table_entry *const entry1 =
10083     (const struct decl_table_entry *) of1;
10084   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10085
10086   return entry1->orig == entry2;
10087 }
10088
10089 static void
10090 htab_decl_del (void *what)
10091 {
10092   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10093
10094   free (entry);
10095 }
10096
10097 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10098    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10099    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10100    to check if the ancestor has already been copied into UNIT.  */
10101
10102 static dw_die_ref
10103 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10104 {
10105   dw_die_ref parent = die->die_parent;
10106   dw_die_ref new_parent = unit;
10107   dw_die_ref copy;
10108   void **slot = NULL;
10109   struct decl_table_entry *entry = NULL;
10110
10111   if (decl_table)
10112     {
10113       /* Check if the entry has already been copied to UNIT.  */
10114       slot = htab_find_slot_with_hash (decl_table, die,
10115                                        htab_hash_pointer (die), INSERT);
10116       if (*slot != HTAB_EMPTY_ENTRY)
10117         {
10118           entry = (struct decl_table_entry *) *slot;
10119           return entry->copy;
10120         }
10121
10122       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10123       entry = XCNEW (struct decl_table_entry);
10124       entry->orig = die;
10125       entry->copy = NULL;
10126       *slot = entry;
10127     }
10128
10129   if (parent != NULL)
10130     {
10131       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10132       if (spec != NULL)
10133         parent = spec;
10134       if (parent->die_tag != DW_TAG_compile_unit
10135           && parent->die_tag != DW_TAG_type_unit)
10136         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10137     }
10138
10139   copy = clone_as_declaration (die);
10140   add_child_die (new_parent, copy);
10141
10142   if (decl_table != NULL)
10143     {
10144       /* Record the pointer to the copy.  */
10145       entry->copy = copy;
10146     }
10147
10148   return copy;
10149 }
10150
10151 /* Walk the DIE and its children, looking for references to incomplete
10152    or trivial types that are unmarked (i.e., that are not in the current
10153    type_unit).  */
10154
10155 static void
10156 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10157 {
10158   dw_die_ref c;
10159   dw_attr_ref a;
10160   unsigned ix;
10161
10162   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10163     {
10164       if (AT_class (a) == dw_val_class_die_ref)
10165         {
10166           dw_die_ref targ = AT_ref (a);
10167           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10168           void **slot;
10169           struct decl_table_entry *entry;
10170
10171           if (targ->die_mark != 0 || type_node != NULL)
10172             continue;
10173
10174           slot = htab_find_slot_with_hash (decl_table, targ,
10175                                            htab_hash_pointer (targ), INSERT);
10176
10177           if (*slot != HTAB_EMPTY_ENTRY)
10178             {
10179               /* TARG has already been copied, so we just need to
10180                  modify the reference to point to the copy.  */
10181               entry = (struct decl_table_entry *) *slot;
10182               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10183             }
10184           else
10185             {
10186               dw_die_ref parent = unit;
10187               dw_die_ref copy = clone_tree (targ);
10188
10189               /* Make sure the cloned tree is marked as part of the
10190                  type unit.  */
10191               mark_dies (copy);
10192
10193               /* Record in DECL_TABLE that TARG has been copied.
10194                  Need to do this now, before the recursive call,
10195                  because DECL_TABLE may be expanded and SLOT
10196                  would no longer be a valid pointer.  */
10197               entry = XCNEW (struct decl_table_entry);
10198               entry->orig = targ;
10199               entry->copy = copy;
10200               *slot = entry;
10201
10202               /* If TARG has surrounding context, copy its ancestor tree
10203                  into the new type unit.  */
10204               if (targ->die_parent != NULL
10205                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10206                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10207                 parent = copy_ancestor_tree (unit, targ->die_parent,
10208                                              decl_table);
10209
10210               add_child_die (parent, copy);
10211               a->dw_attr_val.v.val_die_ref.die = copy;
10212
10213               /* Make sure the newly-copied DIE is walked.  If it was
10214                  installed in a previously-added context, it won't
10215                  get visited otherwise.  */
10216               if (parent != unit)
10217                 {
10218                   /* Find the highest point of the newly-added tree,
10219                      mark each node along the way, and walk from there.  */
10220                   parent->die_mark = 1;
10221                   while (parent->die_parent
10222                          && parent->die_parent->die_mark == 0)
10223                     {
10224                       parent = parent->die_parent;
10225                       parent->die_mark = 1;
10226                     }
10227                   copy_decls_walk (unit, parent, decl_table);
10228                 }
10229             }
10230         }
10231     }
10232
10233   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10234 }
10235
10236 /* Copy declarations for "unworthy" types into the new comdat section.
10237    Incomplete types, modified types, and certain other types aren't broken
10238    out into comdat sections of their own, so they don't have a signature,
10239    and we need to copy the declaration into the same section so that we
10240    don't have an external reference.  */
10241
10242 static void
10243 copy_decls_for_unworthy_types (dw_die_ref unit)
10244 {
10245   htab_t decl_table;
10246
10247   mark_dies (unit);
10248   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10249   copy_decls_walk (unit, unit, decl_table);
10250   htab_delete (decl_table);
10251   unmark_dies (unit);
10252 }
10253
10254 /* Traverse the DIE and add a sibling attribute if it may have the
10255    effect of speeding up access to siblings.  To save some space,
10256    avoid generating sibling attributes for DIE's without children.  */
10257
10258 static void
10259 add_sibling_attributes (dw_die_ref die)
10260 {
10261   dw_die_ref c;
10262
10263   if (! die->die_child)
10264     return;
10265
10266   if (die->die_parent && die != die->die_parent->die_child)
10267     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10268
10269   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10270 }
10271
10272 /* Output all location lists for the DIE and its children.  */
10273
10274 static void
10275 output_location_lists (dw_die_ref die)
10276 {
10277   dw_die_ref c;
10278   dw_attr_ref a;
10279   unsigned ix;
10280
10281   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10282     if (AT_class (a) == dw_val_class_loc_list)
10283       output_loc_list (AT_loc_list (a));
10284
10285   FOR_EACH_CHILD (die, c, output_location_lists (c));
10286 }
10287
10288 /* The format of each DIE (and its attribute value pairs) is encoded in an
10289    abbreviation table.  This routine builds the abbreviation table and assigns
10290    a unique abbreviation id for each abbreviation entry.  The children of each
10291    die are visited recursively.  */
10292
10293 static void
10294 build_abbrev_table (dw_die_ref die)
10295 {
10296   unsigned long abbrev_id;
10297   unsigned int n_alloc;
10298   dw_die_ref c;
10299   dw_attr_ref a;
10300   unsigned ix;
10301
10302   /* Scan the DIE references, and mark as external any that refer to
10303      DIEs from other CUs (i.e. those which are not marked).  */
10304   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10305     if (AT_class (a) == dw_val_class_die_ref
10306         && AT_ref (a)->die_mark == 0)
10307       {
10308         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10309         set_AT_ref_external (a, 1);
10310       }
10311
10312   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10313     {
10314       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10315       dw_attr_ref die_a, abbrev_a;
10316       unsigned ix;
10317       bool ok = true;
10318
10319       if (abbrev->die_tag != die->die_tag)
10320         continue;
10321       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10322         continue;
10323
10324       if (VEC_length (dw_attr_node, abbrev->die_attr)
10325           != VEC_length (dw_attr_node, die->die_attr))
10326         continue;
10327
10328       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10329         {
10330           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10331           if ((abbrev_a->dw_attr != die_a->dw_attr)
10332               || (value_format (abbrev_a) != value_format (die_a)))
10333             {
10334               ok = false;
10335               break;
10336             }
10337         }
10338       if (ok)
10339         break;
10340     }
10341
10342   if (abbrev_id >= abbrev_die_table_in_use)
10343     {
10344       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10345         {
10346           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10347           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10348                                             n_alloc);
10349
10350           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10351                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10352           abbrev_die_table_allocated = n_alloc;
10353         }
10354
10355       ++abbrev_die_table_in_use;
10356       abbrev_die_table[abbrev_id] = die;
10357     }
10358
10359   die->die_abbrev = abbrev_id;
10360   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10361 }
10362 \f
10363 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10364
10365 static int
10366 constant_size (unsigned HOST_WIDE_INT value)
10367 {
10368   int log;
10369
10370   if (value == 0)
10371     log = 0;
10372   else
10373     log = floor_log2 (value);
10374
10375   log = log / 8;
10376   log = 1 << (floor_log2 (log) + 1);
10377
10378   return log;
10379 }
10380
10381 /* Return the size of a DIE as it is represented in the
10382    .debug_info section.  */
10383
10384 static unsigned long
10385 size_of_die (dw_die_ref die)
10386 {
10387   unsigned long size = 0;
10388   dw_attr_ref a;
10389   unsigned ix;
10390
10391   size += size_of_uleb128 (die->die_abbrev);
10392   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10393     {
10394       switch (AT_class (a))
10395         {
10396         case dw_val_class_addr:
10397           size += DWARF2_ADDR_SIZE;
10398           break;
10399         case dw_val_class_offset:
10400           size += DWARF_OFFSET_SIZE;
10401           break;
10402         case dw_val_class_loc:
10403           {
10404             unsigned long lsize = size_of_locs (AT_loc (a));
10405
10406             /* Block length.  */
10407             if (dwarf_version >= 4)
10408               size += size_of_uleb128 (lsize);
10409             else
10410               size += constant_size (lsize);
10411             size += lsize;
10412           }
10413           break;
10414         case dw_val_class_loc_list:
10415           size += DWARF_OFFSET_SIZE;
10416           break;
10417         case dw_val_class_range_list:
10418           size += DWARF_OFFSET_SIZE;
10419           break;
10420         case dw_val_class_const:
10421           size += size_of_sleb128 (AT_int (a));
10422           break;
10423         case dw_val_class_unsigned_const:
10424           size += constant_size (AT_unsigned (a));
10425           break;
10426         case dw_val_class_const_double:
10427           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10428           if (HOST_BITS_PER_WIDE_INT >= 64)
10429             size++; /* block */
10430           break;
10431         case dw_val_class_vec:
10432           size += constant_size (a->dw_attr_val.v.val_vec.length
10433                                  * a->dw_attr_val.v.val_vec.elt_size)
10434                   + a->dw_attr_val.v.val_vec.length
10435                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10436           break;
10437         case dw_val_class_flag:
10438           if (dwarf_version >= 4)
10439             /* Currently all add_AT_flag calls pass in 1 as last argument,
10440                so DW_FORM_flag_present can be used.  If that ever changes,
10441                we'll need to use DW_FORM_flag and have some optimization
10442                in build_abbrev_table that will change those to
10443                DW_FORM_flag_present if it is set to 1 in all DIEs using
10444                the same abbrev entry.  */
10445             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10446           else
10447             size += 1;
10448           break;
10449         case dw_val_class_die_ref:
10450           if (AT_ref_external (a))
10451             {
10452               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10453                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10454                  is sized by target address length, whereas in DWARF3
10455                  it's always sized as an offset.  */
10456               if (dwarf_version >= 4)
10457                 size += DWARF_TYPE_SIGNATURE_SIZE;
10458               else if (dwarf_version == 2)
10459                 size += DWARF2_ADDR_SIZE;
10460               else
10461                 size += DWARF_OFFSET_SIZE;
10462             }
10463           else
10464             size += DWARF_OFFSET_SIZE;
10465           break;
10466         case dw_val_class_fde_ref:
10467           size += DWARF_OFFSET_SIZE;
10468           break;
10469         case dw_val_class_lbl_id:
10470           size += DWARF2_ADDR_SIZE;
10471           break;
10472         case dw_val_class_lineptr:
10473         case dw_val_class_macptr:
10474           size += DWARF_OFFSET_SIZE;
10475           break;
10476         case dw_val_class_str:
10477           if (AT_string_form (a) == DW_FORM_strp)
10478             size += DWARF_OFFSET_SIZE;
10479           else
10480             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10481           break;
10482         case dw_val_class_file:
10483           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10484           break;
10485         case dw_val_class_data8:
10486           size += 8;
10487           break;
10488         case dw_val_class_vms_delta:
10489           size += DWARF_OFFSET_SIZE;
10490           break;
10491         default:
10492           gcc_unreachable ();
10493         }
10494     }
10495
10496   return size;
10497 }
10498
10499 /* Size the debugging information associated with a given DIE.  Visits the
10500    DIE's children recursively.  Updates the global variable next_die_offset, on
10501    each time through.  Uses the current value of next_die_offset to update the
10502    die_offset field in each DIE.  */
10503
10504 static void
10505 calc_die_sizes (dw_die_ref die)
10506 {
10507   dw_die_ref c;
10508
10509   die->die_offset = next_die_offset;
10510   next_die_offset += size_of_die (die);
10511
10512   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10513
10514   if (die->die_child != NULL)
10515     /* Count the null byte used to terminate sibling lists.  */
10516     next_die_offset += 1;
10517 }
10518
10519 /* Set the marks for a die and its children.  We do this so
10520    that we know whether or not a reference needs to use FORM_ref_addr; only
10521    DIEs in the same CU will be marked.  We used to clear out the offset
10522    and use that as the flag, but ran into ordering problems.  */
10523
10524 static void
10525 mark_dies (dw_die_ref die)
10526 {
10527   dw_die_ref c;
10528
10529   gcc_assert (!die->die_mark);
10530
10531   die->die_mark = 1;
10532   FOR_EACH_CHILD (die, c, mark_dies (c));
10533 }
10534
10535 /* Clear the marks for a die and its children.  */
10536
10537 static void
10538 unmark_dies (dw_die_ref die)
10539 {
10540   dw_die_ref c;
10541
10542   if (dwarf_version < 4)
10543     gcc_assert (die->die_mark);
10544
10545   die->die_mark = 0;
10546   FOR_EACH_CHILD (die, c, unmark_dies (c));
10547 }
10548
10549 /* Clear the marks for a die, its children and referred dies.  */
10550
10551 static void
10552 unmark_all_dies (dw_die_ref die)
10553 {
10554   dw_die_ref c;
10555   dw_attr_ref a;
10556   unsigned ix;
10557
10558   if (!die->die_mark)
10559     return;
10560   die->die_mark = 0;
10561
10562   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10563
10564   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10565     if (AT_class (a) == dw_val_class_die_ref)
10566       unmark_all_dies (AT_ref (a));
10567 }
10568
10569 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10570    generated for the compilation unit.  */
10571
10572 static unsigned long
10573 size_of_pubnames (VEC (pubname_entry, gc) * names)
10574 {
10575   unsigned long size;
10576   unsigned i;
10577   pubname_ref p;
10578
10579   size = DWARF_PUBNAMES_HEADER_SIZE;
10580   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10581     if (names != pubtype_table
10582         || p->die->die_offset != 0
10583         || !flag_eliminate_unused_debug_types)
10584       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10585
10586   size += DWARF_OFFSET_SIZE;
10587   return size;
10588 }
10589
10590 /* Return the size of the information in the .debug_aranges section.  */
10591
10592 static unsigned long
10593 size_of_aranges (void)
10594 {
10595   unsigned long size;
10596
10597   size = DWARF_ARANGES_HEADER_SIZE;
10598
10599   /* Count the address/length pair for this compilation unit.  */
10600   if (text_section_used)
10601     size += 2 * DWARF2_ADDR_SIZE;
10602   if (cold_text_section_used)
10603     size += 2 * DWARF2_ADDR_SIZE;
10604   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10605
10606   /* Count the two zero words used to terminated the address range table.  */
10607   size += 2 * DWARF2_ADDR_SIZE;
10608   return size;
10609 }
10610 \f
10611 /* Select the encoding of an attribute value.  */
10612
10613 static enum dwarf_form
10614 value_format (dw_attr_ref a)
10615 {
10616   switch (a->dw_attr_val.val_class)
10617     {
10618     case dw_val_class_addr:
10619       /* Only very few attributes allow DW_FORM_addr.  */
10620       switch (a->dw_attr)
10621         {
10622         case DW_AT_low_pc:
10623         case DW_AT_high_pc:
10624         case DW_AT_entry_pc:
10625         case DW_AT_trampoline:
10626           return DW_FORM_addr;
10627         default:
10628           break;
10629         }
10630       switch (DWARF2_ADDR_SIZE)
10631         {
10632         case 1:
10633           return DW_FORM_data1;
10634         case 2:
10635           return DW_FORM_data2;
10636         case 4:
10637           return DW_FORM_data4;
10638         case 8:
10639           return DW_FORM_data8;
10640         default:
10641           gcc_unreachable ();
10642         }
10643     case dw_val_class_range_list:
10644     case dw_val_class_loc_list:
10645       if (dwarf_version >= 4)
10646         return DW_FORM_sec_offset;
10647       /* FALLTHRU */
10648     case dw_val_class_vms_delta:
10649     case dw_val_class_offset:
10650       switch (DWARF_OFFSET_SIZE)
10651         {
10652         case 4:
10653           return DW_FORM_data4;
10654         case 8:
10655           return DW_FORM_data8;
10656         default:
10657           gcc_unreachable ();
10658         }
10659     case dw_val_class_loc:
10660       if (dwarf_version >= 4)
10661         return DW_FORM_exprloc;
10662       switch (constant_size (size_of_locs (AT_loc (a))))
10663         {
10664         case 1:
10665           return DW_FORM_block1;
10666         case 2:
10667           return DW_FORM_block2;
10668         default:
10669           gcc_unreachable ();
10670         }
10671     case dw_val_class_const:
10672       return DW_FORM_sdata;
10673     case dw_val_class_unsigned_const:
10674       switch (constant_size (AT_unsigned (a)))
10675         {
10676         case 1:
10677           return DW_FORM_data1;
10678         case 2:
10679           return DW_FORM_data2;
10680         case 4:
10681           return DW_FORM_data4;
10682         case 8:
10683           return DW_FORM_data8;
10684         default:
10685           gcc_unreachable ();
10686         }
10687     case dw_val_class_const_double:
10688       switch (HOST_BITS_PER_WIDE_INT)
10689         {
10690         case 8:
10691           return DW_FORM_data2;
10692         case 16:
10693           return DW_FORM_data4;
10694         case 32:
10695           return DW_FORM_data8;
10696         case 64:
10697         default:
10698           return DW_FORM_block1;
10699         }
10700     case dw_val_class_vec:
10701       switch (constant_size (a->dw_attr_val.v.val_vec.length
10702                              * a->dw_attr_val.v.val_vec.elt_size))
10703         {
10704         case 1:
10705           return DW_FORM_block1;
10706         case 2:
10707           return DW_FORM_block2;
10708         case 4:
10709           return DW_FORM_block4;
10710         default:
10711           gcc_unreachable ();
10712         }
10713     case dw_val_class_flag:
10714       if (dwarf_version >= 4)
10715         {
10716           /* Currently all add_AT_flag calls pass in 1 as last argument,
10717              so DW_FORM_flag_present can be used.  If that ever changes,
10718              we'll need to use DW_FORM_flag and have some optimization
10719              in build_abbrev_table that will change those to
10720              DW_FORM_flag_present if it is set to 1 in all DIEs using
10721              the same abbrev entry.  */
10722           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10723           return DW_FORM_flag_present;
10724         }
10725       return DW_FORM_flag;
10726     case dw_val_class_die_ref:
10727       if (AT_ref_external (a))
10728         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10729       else
10730         return DW_FORM_ref;
10731     case dw_val_class_fde_ref:
10732       return DW_FORM_data;
10733     case dw_val_class_lbl_id:
10734       return DW_FORM_addr;
10735     case dw_val_class_lineptr:
10736     case dw_val_class_macptr:
10737       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10738     case dw_val_class_str:
10739       return AT_string_form (a);
10740     case dw_val_class_file:
10741       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10742         {
10743         case 1:
10744           return DW_FORM_data1;
10745         case 2:
10746           return DW_FORM_data2;
10747         case 4:
10748           return DW_FORM_data4;
10749         default:
10750           gcc_unreachable ();
10751         }
10752
10753     case dw_val_class_data8:
10754       return DW_FORM_data8;
10755
10756     default:
10757       gcc_unreachable ();
10758     }
10759 }
10760
10761 /* Output the encoding of an attribute value.  */
10762
10763 static void
10764 output_value_format (dw_attr_ref a)
10765 {
10766   enum dwarf_form form = value_format (a);
10767
10768   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10769 }
10770
10771 /* Output the .debug_abbrev section which defines the DIE abbreviation
10772    table.  */
10773
10774 static void
10775 output_abbrev_section (void)
10776 {
10777   unsigned long abbrev_id;
10778
10779   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10780     {
10781       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10782       unsigned ix;
10783       dw_attr_ref a_attr;
10784
10785       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10786       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10787                                    dwarf_tag_name (abbrev->die_tag));
10788
10789       if (abbrev->die_child != NULL)
10790         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10791       else
10792         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10793
10794       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10795            ix++)
10796         {
10797           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10798                                        dwarf_attr_name (a_attr->dw_attr));
10799           output_value_format (a_attr);
10800         }
10801
10802       dw2_asm_output_data (1, 0, NULL);
10803       dw2_asm_output_data (1, 0, NULL);
10804     }
10805
10806   /* Terminate the table.  */
10807   dw2_asm_output_data (1, 0, NULL);
10808 }
10809
10810 /* Output a symbol we can use to refer to this DIE from another CU.  */
10811
10812 static inline void
10813 output_die_symbol (dw_die_ref die)
10814 {
10815   char *sym = die->die_id.die_symbol;
10816
10817   if (sym == 0)
10818     return;
10819
10820   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10821     /* We make these global, not weak; if the target doesn't support
10822        .linkonce, it doesn't support combining the sections, so debugging
10823        will break.  */
10824     targetm.asm_out.globalize_label (asm_out_file, sym);
10825
10826   ASM_OUTPUT_LABEL (asm_out_file, sym);
10827 }
10828
10829 /* Return a new location list, given the begin and end range, and the
10830    expression.  */
10831
10832 static inline dw_loc_list_ref
10833 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10834               const char *section)
10835 {
10836   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10837
10838   retlist->begin = begin;
10839   retlist->end = end;
10840   retlist->expr = expr;
10841   retlist->section = section;
10842
10843   return retlist;
10844 }
10845
10846 /* Generate a new internal symbol for this location list node, if it
10847    hasn't got one yet.  */
10848
10849 static inline void
10850 gen_llsym (dw_loc_list_ref list)
10851 {
10852   gcc_assert (!list->ll_symbol);
10853   list->ll_symbol = gen_internal_sym ("LLST");
10854 }
10855
10856 /* Output the location list given to us.  */
10857
10858 static void
10859 output_loc_list (dw_loc_list_ref list_head)
10860 {
10861   dw_loc_list_ref curr = list_head;
10862
10863   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10864
10865   /* Walk the location list, and output each range + expression.  */
10866   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10867     {
10868       unsigned long size;
10869       /* Don't output an entry that starts and ends at the same address.  */
10870       if (strcmp (curr->begin, curr->end) == 0)
10871         continue;
10872       if (!have_multiple_function_sections)
10873         {
10874           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10875                                 "Location list begin address (%s)",
10876                                 list_head->ll_symbol);
10877           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10878                                 "Location list end address (%s)",
10879                                 list_head->ll_symbol);
10880         }
10881       else
10882         {
10883           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10884                                "Location list begin address (%s)",
10885                                list_head->ll_symbol);
10886           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10887                                "Location list end address (%s)",
10888                                list_head->ll_symbol);
10889         }
10890       size = size_of_locs (curr->expr);
10891
10892       /* Output the block length for this list of location operations.  */
10893       gcc_assert (size <= 0xffff);
10894       dw2_asm_output_data (2, size, "%s", "Location expression size");
10895
10896       output_loc_sequence (curr->expr);
10897     }
10898
10899   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10900                        "Location list terminator begin (%s)",
10901                        list_head->ll_symbol);
10902   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10903                        "Location list terminator end (%s)",
10904                        list_head->ll_symbol);
10905 }
10906
10907 /* Output a type signature.  */
10908
10909 static inline void
10910 output_signature (const char *sig, const char *name)
10911 {
10912   int i;
10913
10914   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10915     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10916 }
10917
10918 /* Output the DIE and its attributes.  Called recursively to generate
10919    the definitions of each child DIE.  */
10920
10921 static void
10922 output_die (dw_die_ref die)
10923 {
10924   dw_attr_ref a;
10925   dw_die_ref c;
10926   unsigned long size;
10927   unsigned ix;
10928
10929   /* If someone in another CU might refer to us, set up a symbol for
10930      them to point to.  */
10931   if (dwarf_version < 4 && die->die_id.die_symbol)
10932     output_die_symbol (die);
10933
10934   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10935                                (unsigned long)die->die_offset,
10936                                dwarf_tag_name (die->die_tag));
10937
10938   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10939     {
10940       const char *name = dwarf_attr_name (a->dw_attr);
10941
10942       switch (AT_class (a))
10943         {
10944         case dw_val_class_addr:
10945           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10946           break;
10947
10948         case dw_val_class_offset:
10949           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10950                                "%s", name);
10951           break;
10952
10953         case dw_val_class_range_list:
10954           {
10955             char *p = strchr (ranges_section_label, '\0');
10956
10957             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10958                      a->dw_attr_val.v.val_offset);
10959             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10960                                    debug_ranges_section, "%s", name);
10961             *p = '\0';
10962           }
10963           break;
10964
10965         case dw_val_class_loc:
10966           size = size_of_locs (AT_loc (a));
10967
10968           /* Output the block length for this list of location operations.  */
10969           if (dwarf_version >= 4)
10970             dw2_asm_output_data_uleb128 (size, "%s", name);
10971           else
10972             dw2_asm_output_data (constant_size (size), size, "%s", name);
10973
10974           output_loc_sequence (AT_loc (a));
10975           break;
10976
10977         case dw_val_class_const:
10978           /* ??? It would be slightly more efficient to use a scheme like is
10979              used for unsigned constants below, but gdb 4.x does not sign
10980              extend.  Gdb 5.x does sign extend.  */
10981           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10982           break;
10983
10984         case dw_val_class_unsigned_const:
10985           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10986                                AT_unsigned (a), "%s", name);
10987           break;
10988
10989         case dw_val_class_const_double:
10990           {
10991             unsigned HOST_WIDE_INT first, second;
10992
10993             if (HOST_BITS_PER_WIDE_INT >= 64)
10994               dw2_asm_output_data (1,
10995                                    2 * HOST_BITS_PER_WIDE_INT
10996                                    / HOST_BITS_PER_CHAR,
10997                                    NULL);
10998
10999             if (WORDS_BIG_ENDIAN)
11000               {
11001                 first = a->dw_attr_val.v.val_double.high;
11002                 second = a->dw_attr_val.v.val_double.low;
11003               }
11004             else
11005               {
11006                 first = a->dw_attr_val.v.val_double.low;
11007                 second = a->dw_attr_val.v.val_double.high;
11008               }
11009
11010             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11011                                  first, name);
11012             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11013                                  second, NULL);
11014           }
11015           break;
11016
11017         case dw_val_class_vec:
11018           {
11019             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11020             unsigned int len = a->dw_attr_val.v.val_vec.length;
11021             unsigned int i;
11022             unsigned char *p;
11023
11024             dw2_asm_output_data (constant_size (len * elt_size),
11025                                  len * elt_size, "%s", name);
11026             if (elt_size > sizeof (HOST_WIDE_INT))
11027               {
11028                 elt_size /= 2;
11029                 len *= 2;
11030               }
11031             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11032                  i < len;
11033                  i++, p += elt_size)
11034               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11035                                    "fp or vector constant word %u", i);
11036             break;
11037           }
11038
11039         case dw_val_class_flag:
11040           if (dwarf_version >= 4)
11041             {
11042               /* Currently all add_AT_flag calls pass in 1 as last argument,
11043                  so DW_FORM_flag_present can be used.  If that ever changes,
11044                  we'll need to use DW_FORM_flag and have some optimization
11045                  in build_abbrev_table that will change those to
11046                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11047                  the same abbrev entry.  */
11048               gcc_assert (AT_flag (a) == 1);
11049               if (flag_debug_asm)
11050                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11051                          ASM_COMMENT_START, name);
11052               break;
11053             }
11054           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11055           break;
11056
11057         case dw_val_class_loc_list:
11058           {
11059             char *sym = AT_loc_list (a)->ll_symbol;
11060
11061             gcc_assert (sym);
11062             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11063                                    "%s", name);
11064           }
11065           break;
11066
11067         case dw_val_class_die_ref:
11068           if (AT_ref_external (a))
11069             {
11070               if (dwarf_version >= 4)
11071                 {
11072                   comdat_type_node_ref type_node =
11073                     AT_ref (a)->die_id.die_type_node;
11074
11075                   gcc_assert (type_node);
11076                   output_signature (type_node->signature, name);
11077                 }
11078               else
11079                 {
11080                   char *sym = AT_ref (a)->die_id.die_symbol;
11081                   int size;
11082
11083                   gcc_assert (sym);
11084                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11085                      length, whereas in DWARF3 it's always sized as an
11086                      offset.  */
11087                   if (dwarf_version == 2)
11088                     size = DWARF2_ADDR_SIZE;
11089                   else
11090                     size = DWARF_OFFSET_SIZE;
11091                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11092                                          name);
11093                 }
11094             }
11095           else
11096             {
11097               gcc_assert (AT_ref (a)->die_offset);
11098               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11099                                    "%s", name);
11100             }
11101           break;
11102
11103         case dw_val_class_fde_ref:
11104           {
11105             char l1[20];
11106
11107             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11108                                          a->dw_attr_val.v.val_fde_index * 2);
11109             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11110                                    "%s", name);
11111           }
11112           break;
11113
11114         case dw_val_class_vms_delta:
11115           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11116                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11117                                     "%s", name);
11118           break;
11119
11120         case dw_val_class_lbl_id:
11121           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11122           break;
11123
11124         case dw_val_class_lineptr:
11125           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11126                                  debug_line_section, "%s", name);
11127           break;
11128
11129         case dw_val_class_macptr:
11130           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11131                                  debug_macinfo_section, "%s", name);
11132           break;
11133
11134         case dw_val_class_str:
11135           if (AT_string_form (a) == DW_FORM_strp)
11136             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11137                                    a->dw_attr_val.v.val_str->label,
11138                                    debug_str_section,
11139                                    "%s: \"%s\"", name, AT_string (a));
11140           else
11141             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11142           break;
11143
11144         case dw_val_class_file:
11145           {
11146             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11147
11148             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11149                                  a->dw_attr_val.v.val_file->filename);
11150             break;
11151           }
11152
11153         case dw_val_class_data8:
11154           {
11155             int i;
11156
11157             for (i = 0; i < 8; i++)
11158               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11159                                    i == 0 ? "%s" : NULL, name);
11160             break;
11161           }
11162
11163         default:
11164           gcc_unreachable ();
11165         }
11166     }
11167
11168   FOR_EACH_CHILD (die, c, output_die (c));
11169
11170   /* Add null byte to terminate sibling list.  */
11171   if (die->die_child != NULL)
11172     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11173                          (unsigned long) die->die_offset);
11174 }
11175
11176 /* Output the compilation unit that appears at the beginning of the
11177    .debug_info section, and precedes the DIE descriptions.  */
11178
11179 static void
11180 output_compilation_unit_header (void)
11181 {
11182   int ver = dwarf_version;
11183
11184   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11185     dw2_asm_output_data (4, 0xffffffff,
11186       "Initial length escape value indicating 64-bit DWARF extension");
11187   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11188                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11189                        "Length of Compilation Unit Info");
11190   dw2_asm_output_data (2, ver, "DWARF version number");
11191   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11192                          debug_abbrev_section,
11193                          "Offset Into Abbrev. Section");
11194   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11195 }
11196
11197 /* Output the compilation unit DIE and its children.  */
11198
11199 static void
11200 output_comp_unit (dw_die_ref die, int output_if_empty)
11201 {
11202   const char *secname;
11203   char *oldsym, *tmp;
11204
11205   /* Unless we are outputting main CU, we may throw away empty ones.  */
11206   if (!output_if_empty && die->die_child == NULL)
11207     return;
11208
11209   /* Even if there are no children of this DIE, we must output the information
11210      about the compilation unit.  Otherwise, on an empty translation unit, we
11211      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11212      will then complain when examining the file.  First mark all the DIEs in
11213      this CU so we know which get local refs.  */
11214   mark_dies (die);
11215
11216   build_abbrev_table (die);
11217
11218   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11219   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11220   calc_die_sizes (die);
11221
11222   oldsym = die->die_id.die_symbol;
11223   if (oldsym)
11224     {
11225       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11226
11227       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11228       secname = tmp;
11229       die->die_id.die_symbol = NULL;
11230       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11231     }
11232   else
11233     switch_to_section (debug_info_section);
11234
11235   /* Output debugging information.  */
11236   output_compilation_unit_header ();
11237   output_die (die);
11238
11239   /* Leave the marks on the main CU, so we can check them in
11240      output_pubnames.  */
11241   if (oldsym)
11242     {
11243       unmark_dies (die);
11244       die->die_id.die_symbol = oldsym;
11245     }
11246 }
11247
11248 /* Output a comdat type unit DIE and its children.  */
11249
11250 static void
11251 output_comdat_type_unit (comdat_type_node *node)
11252 {
11253   const char *secname;
11254   char *tmp;
11255   int i;
11256 #if defined (OBJECT_FORMAT_ELF)
11257   tree comdat_key;
11258 #endif
11259
11260   /* First mark all the DIEs in this CU so we know which get local refs.  */
11261   mark_dies (node->root_die);
11262
11263   build_abbrev_table (node->root_die);
11264
11265   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11266   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11267   calc_die_sizes (node->root_die);
11268
11269 #if defined (OBJECT_FORMAT_ELF)
11270   secname = ".debug_types";
11271   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11272   sprintf (tmp, "wt.");
11273   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11274     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11275   comdat_key = get_identifier (tmp);
11276   targetm.asm_out.named_section (secname,
11277                                  SECTION_DEBUG | SECTION_LINKONCE,
11278                                  comdat_key);
11279 #else
11280   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11281   sprintf (tmp, ".gnu.linkonce.wt.");
11282   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11283     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11284   secname = tmp;
11285   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11286 #endif
11287
11288   /* Output debugging information.  */
11289   output_compilation_unit_header ();
11290   output_signature (node->signature, "Type Signature");
11291   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11292                        "Offset to Type DIE");
11293   output_die (node->root_die);
11294
11295   unmark_dies (node->root_die);
11296 }
11297
11298 /* Return the DWARF2/3 pubname associated with a decl.  */
11299
11300 static const char *
11301 dwarf2_name (tree decl, int scope)
11302 {
11303   if (DECL_NAMELESS (decl))
11304     return NULL;
11305   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11306 }
11307
11308 /* Add a new entry to .debug_pubnames if appropriate.  */
11309
11310 static void
11311 add_pubname_string (const char *str, dw_die_ref die)
11312 {
11313   if (targetm.want_debug_pub_sections)
11314     {
11315       pubname_entry e;
11316
11317       e.die = die;
11318       e.name = xstrdup (str);
11319       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11320     }
11321 }
11322
11323 static void
11324 add_pubname (tree decl, dw_die_ref die)
11325 {
11326   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11327     {
11328       const char *name = dwarf2_name (decl, 1);
11329       if (name)
11330         add_pubname_string (name, die);
11331     }
11332 }
11333
11334 /* Add a new entry to .debug_pubtypes if appropriate.  */
11335
11336 static void
11337 add_pubtype (tree decl, dw_die_ref die)
11338 {
11339   pubname_entry e;
11340
11341   if (!targetm.want_debug_pub_sections)
11342     return;
11343
11344   e.name = NULL;
11345   if ((TREE_PUBLIC (decl)
11346        || die->die_parent == comp_unit_die)
11347       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11348     {
11349       e.die = die;
11350       if (TYPE_P (decl))
11351         {
11352           if (TYPE_NAME (decl))
11353             {
11354               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11355                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11356               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11357                        && DECL_NAME (TYPE_NAME (decl)))
11358                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11359               else
11360                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11361             }
11362         }
11363       else
11364         {
11365           e.name = dwarf2_name (decl, 1);
11366           if (e.name)
11367             e.name = xstrdup (e.name);
11368         }
11369
11370       /* If we don't have a name for the type, there's no point in adding
11371          it to the table.  */
11372       if (e.name && e.name[0] != '\0')
11373         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11374     }
11375 }
11376
11377 /* Output the public names table used to speed up access to externally
11378    visible names; or the public types table used to find type definitions.  */
11379
11380 static void
11381 output_pubnames (VEC (pubname_entry, gc) * names)
11382 {
11383   unsigned i;
11384   unsigned long pubnames_length = size_of_pubnames (names);
11385   pubname_ref pub;
11386
11387   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11388     dw2_asm_output_data (4, 0xffffffff,
11389       "Initial length escape value indicating 64-bit DWARF extension");
11390   if (names == pubname_table)
11391     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11392                          "Length of Public Names Info");
11393   else
11394     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11395                          "Length of Public Type Names Info");
11396   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11397   dw2_asm_output_data (2, 2, "DWARF Version");
11398   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11399                          debug_info_section,
11400                          "Offset of Compilation Unit Info");
11401   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11402                        "Compilation Unit Length");
11403
11404   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11405     {
11406       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11407       if (names == pubname_table)
11408         gcc_assert (pub->die->die_mark);
11409
11410       if (names != pubtype_table
11411           || pub->die->die_offset != 0
11412           || !flag_eliminate_unused_debug_types)
11413         {
11414           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11415                                "DIE offset");
11416
11417           dw2_asm_output_nstring (pub->name, -1, "external name");
11418         }
11419     }
11420
11421   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11422 }
11423
11424 /* Add a new entry to .debug_aranges if appropriate.  */
11425
11426 static void
11427 add_arange (tree decl, dw_die_ref die)
11428 {
11429   if (! DECL_SECTION_NAME (decl))
11430     return;
11431
11432   if (arange_table_in_use == arange_table_allocated)
11433     {
11434       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11435       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11436                                     arange_table_allocated);
11437       memset (arange_table + arange_table_in_use, 0,
11438               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11439     }
11440
11441   arange_table[arange_table_in_use++] = die;
11442 }
11443
11444 /* Output the information that goes into the .debug_aranges table.
11445    Namely, define the beginning and ending address range of the
11446    text section generated for this compilation unit.  */
11447
11448 static void
11449 output_aranges (void)
11450 {
11451   unsigned i;
11452   unsigned long aranges_length = size_of_aranges ();
11453
11454   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11455     dw2_asm_output_data (4, 0xffffffff,
11456       "Initial length escape value indicating 64-bit DWARF extension");
11457   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11458                        "Length of Address Ranges Info");
11459   /* Version number for aranges is still 2, even in DWARF3.  */
11460   dw2_asm_output_data (2, 2, "DWARF Version");
11461   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11462                          debug_info_section,
11463                          "Offset of Compilation Unit Info");
11464   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11465   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11466
11467   /* We need to align to twice the pointer size here.  */
11468   if (DWARF_ARANGES_PAD_SIZE)
11469     {
11470       /* Pad using a 2 byte words so that padding is correct for any
11471          pointer size.  */
11472       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11473                            2 * DWARF2_ADDR_SIZE);
11474       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11475         dw2_asm_output_data (2, 0, NULL);
11476     }
11477
11478   /* It is necessary not to output these entries if the sections were
11479      not used; if the sections were not used, the length will be 0 and
11480      the address may end up as 0 if the section is discarded by ld
11481      --gc-sections, leaving an invalid (0, 0) entry that can be
11482      confused with the terminator.  */
11483   if (text_section_used)
11484     {
11485       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11486       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11487                             text_section_label, "Length");
11488     }
11489   if (cold_text_section_used)
11490     {
11491       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11492                            "Address");
11493       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11494                             cold_text_section_label, "Length");
11495     }
11496
11497   for (i = 0; i < arange_table_in_use; i++)
11498     {
11499       dw_die_ref die = arange_table[i];
11500
11501       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11502       gcc_assert (die->die_mark);
11503
11504       if (die->die_tag == DW_TAG_subprogram)
11505         {
11506           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11507                                "Address");
11508           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11509                                 get_AT_low_pc (die), "Length");
11510         }
11511       else
11512         {
11513           /* A static variable; extract the symbol from DW_AT_location.
11514              Note that this code isn't currently hit, as we only emit
11515              aranges for functions (jason 9/23/99).  */
11516           dw_attr_ref a = get_AT (die, DW_AT_location);
11517           dw_loc_descr_ref loc;
11518
11519           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11520
11521           loc = AT_loc (a);
11522           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11523
11524           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11525                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11526           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11527                                get_AT_unsigned (die, DW_AT_byte_size),
11528                                "Length");
11529         }
11530     }
11531
11532   /* Output the terminator words.  */
11533   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11534   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11535 }
11536
11537 /* Add a new entry to .debug_ranges.  Return the offset at which it
11538    was placed.  */
11539
11540 static unsigned int
11541 add_ranges_num (int num)
11542 {
11543   unsigned int in_use = ranges_table_in_use;
11544
11545   if (in_use == ranges_table_allocated)
11546     {
11547       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11548       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11549                                     ranges_table_allocated);
11550       memset (ranges_table + ranges_table_in_use, 0,
11551               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11552     }
11553
11554   ranges_table[in_use].num = num;
11555   ranges_table_in_use = in_use + 1;
11556
11557   return in_use * 2 * DWARF2_ADDR_SIZE;
11558 }
11559
11560 /* Add a new entry to .debug_ranges corresponding to a block, or a
11561    range terminator if BLOCK is NULL.  */
11562
11563 static unsigned int
11564 add_ranges (const_tree block)
11565 {
11566   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11567 }
11568
11569 /* Add a new entry to .debug_ranges corresponding to a pair of
11570    labels.  */
11571
11572 static void
11573 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11574                       bool *added)
11575 {
11576   unsigned int in_use = ranges_by_label_in_use;
11577   unsigned int offset;
11578
11579   if (in_use == ranges_by_label_allocated)
11580     {
11581       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11582       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11583                                        ranges_by_label,
11584                                        ranges_by_label_allocated);
11585       memset (ranges_by_label + ranges_by_label_in_use, 0,
11586               RANGES_TABLE_INCREMENT
11587               * sizeof (struct dw_ranges_by_label_struct));
11588     }
11589
11590   ranges_by_label[in_use].begin = begin;
11591   ranges_by_label[in_use].end = end;
11592   ranges_by_label_in_use = in_use + 1;
11593
11594   offset = add_ranges_num (-(int)in_use - 1);
11595   if (!*added)
11596     {
11597       add_AT_range_list (die, DW_AT_ranges, offset);
11598       *added = true;
11599     }
11600 }
11601
11602 static void
11603 output_ranges (void)
11604 {
11605   unsigned i;
11606   static const char *const start_fmt = "Offset %#x";
11607   const char *fmt = start_fmt;
11608
11609   for (i = 0; i < ranges_table_in_use; i++)
11610     {
11611       int block_num = ranges_table[i].num;
11612
11613       if (block_num > 0)
11614         {
11615           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11616           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11617
11618           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11619           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11620
11621           /* If all code is in the text section, then the compilation
11622              unit base address defaults to DW_AT_low_pc, which is the
11623              base of the text section.  */
11624           if (!have_multiple_function_sections)
11625             {
11626               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11627                                     text_section_label,
11628                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11629               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11630                                     text_section_label, NULL);
11631             }
11632
11633           /* Otherwise, the compilation unit base address is zero,
11634              which allows us to use absolute addresses, and not worry
11635              about whether the target supports cross-section
11636              arithmetic.  */
11637           else
11638             {
11639               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11640                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11641               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11642             }
11643
11644           fmt = NULL;
11645         }
11646
11647       /* Negative block_num stands for an index into ranges_by_label.  */
11648       else if (block_num < 0)
11649         {
11650           int lab_idx = - block_num - 1;
11651
11652           if (!have_multiple_function_sections)
11653             {
11654               gcc_unreachable ();
11655 #if 0
11656               /* If we ever use add_ranges_by_labels () for a single
11657                  function section, all we have to do is to take out
11658                  the #if 0 above.  */
11659               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11660                                     ranges_by_label[lab_idx].begin,
11661                                     text_section_label,
11662                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11663               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11664                                     ranges_by_label[lab_idx].end,
11665                                     text_section_label, NULL);
11666 #endif
11667             }
11668           else
11669             {
11670               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11671                                    ranges_by_label[lab_idx].begin,
11672                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11673               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11674                                    ranges_by_label[lab_idx].end,
11675                                    NULL);
11676             }
11677         }
11678       else
11679         {
11680           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11681           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11682           fmt = start_fmt;
11683         }
11684     }
11685 }
11686
11687 /* Data structure containing information about input files.  */
11688 struct file_info
11689 {
11690   const char *path;     /* Complete file name.  */
11691   const char *fname;    /* File name part.  */
11692   int length;           /* Length of entire string.  */
11693   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11694   int dir_idx;          /* Index in directory table.  */
11695 };
11696
11697 /* Data structure containing information about directories with source
11698    files.  */
11699 struct dir_info
11700 {
11701   const char *path;     /* Path including directory name.  */
11702   int length;           /* Path length.  */
11703   int prefix;           /* Index of directory entry which is a prefix.  */
11704   int count;            /* Number of files in this directory.  */
11705   int dir_idx;          /* Index of directory used as base.  */
11706 };
11707
11708 /* Callback function for file_info comparison.  We sort by looking at
11709    the directories in the path.  */
11710
11711 static int
11712 file_info_cmp (const void *p1, const void *p2)
11713 {
11714   const struct file_info *const s1 = (const struct file_info *) p1;
11715   const struct file_info *const s2 = (const struct file_info *) p2;
11716   const unsigned char *cp1;
11717   const unsigned char *cp2;
11718
11719   /* Take care of file names without directories.  We need to make sure that
11720      we return consistent values to qsort since some will get confused if
11721      we return the same value when identical operands are passed in opposite
11722      orders.  So if neither has a directory, return 0 and otherwise return
11723      1 or -1 depending on which one has the directory.  */
11724   if ((s1->path == s1->fname || s2->path == s2->fname))
11725     return (s2->path == s2->fname) - (s1->path == s1->fname);
11726
11727   cp1 = (const unsigned char *) s1->path;
11728   cp2 = (const unsigned char *) s2->path;
11729
11730   while (1)
11731     {
11732       ++cp1;
11733       ++cp2;
11734       /* Reached the end of the first path?  If so, handle like above.  */
11735       if ((cp1 == (const unsigned char *) s1->fname)
11736           || (cp2 == (const unsigned char *) s2->fname))
11737         return ((cp2 == (const unsigned char *) s2->fname)
11738                 - (cp1 == (const unsigned char *) s1->fname));
11739
11740       /* Character of current path component the same?  */
11741       else if (*cp1 != *cp2)
11742         return *cp1 - *cp2;
11743     }
11744 }
11745
11746 struct file_name_acquire_data
11747 {
11748   struct file_info *files;
11749   int used_files;
11750   int max_files;
11751 };
11752
11753 /* Traversal function for the hash table.  */
11754
11755 static int
11756 file_name_acquire (void ** slot, void *data)
11757 {
11758   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11759   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11760   struct file_info *fi;
11761   const char *f;
11762
11763   gcc_assert (fnad->max_files >= d->emitted_number);
11764
11765   if (! d->emitted_number)
11766     return 1;
11767
11768   gcc_assert (fnad->max_files != fnad->used_files);
11769
11770   fi = fnad->files + fnad->used_files++;
11771
11772   /* Skip all leading "./".  */
11773   f = d->filename;
11774   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11775     f += 2;
11776
11777   /* Create a new array entry.  */
11778   fi->path = f;
11779   fi->length = strlen (f);
11780   fi->file_idx = d;
11781
11782   /* Search for the file name part.  */
11783   f = strrchr (f, DIR_SEPARATOR);
11784 #if defined (DIR_SEPARATOR_2)
11785   {
11786     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11787
11788     if (g != NULL)
11789       {
11790         if (f == NULL || f < g)
11791           f = g;
11792       }
11793   }
11794 #endif
11795
11796   fi->fname = f == NULL ? fi->path : f + 1;
11797   return 1;
11798 }
11799
11800 /* Output the directory table and the file name table.  We try to minimize
11801    the total amount of memory needed.  A heuristic is used to avoid large
11802    slowdowns with many input files.  */
11803
11804 static void
11805 output_file_names (void)
11806 {
11807   struct file_name_acquire_data fnad;
11808   int numfiles;
11809   struct file_info *files;
11810   struct dir_info *dirs;
11811   int *saved;
11812   int *savehere;
11813   int *backmap;
11814   int ndirs;
11815   int idx_offset;
11816   int i;
11817
11818   if (!last_emitted_file)
11819     {
11820       dw2_asm_output_data (1, 0, "End directory table");
11821       dw2_asm_output_data (1, 0, "End file name table");
11822       return;
11823     }
11824
11825   numfiles = last_emitted_file->emitted_number;
11826
11827   /* Allocate the various arrays we need.  */
11828   files = XALLOCAVEC (struct file_info, numfiles);
11829   dirs = XALLOCAVEC (struct dir_info, numfiles);
11830
11831   fnad.files = files;
11832   fnad.used_files = 0;
11833   fnad.max_files = numfiles;
11834   htab_traverse (file_table, file_name_acquire, &fnad);
11835   gcc_assert (fnad.used_files == fnad.max_files);
11836
11837   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11838
11839   /* Find all the different directories used.  */
11840   dirs[0].path = files[0].path;
11841   dirs[0].length = files[0].fname - files[0].path;
11842   dirs[0].prefix = -1;
11843   dirs[0].count = 1;
11844   dirs[0].dir_idx = 0;
11845   files[0].dir_idx = 0;
11846   ndirs = 1;
11847
11848   for (i = 1; i < numfiles; i++)
11849     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11850         && memcmp (dirs[ndirs - 1].path, files[i].path,
11851                    dirs[ndirs - 1].length) == 0)
11852       {
11853         /* Same directory as last entry.  */
11854         files[i].dir_idx = ndirs - 1;
11855         ++dirs[ndirs - 1].count;
11856       }
11857     else
11858       {
11859         int j;
11860
11861         /* This is a new directory.  */
11862         dirs[ndirs].path = files[i].path;
11863         dirs[ndirs].length = files[i].fname - files[i].path;
11864         dirs[ndirs].count = 1;
11865         dirs[ndirs].dir_idx = ndirs;
11866         files[i].dir_idx = ndirs;
11867
11868         /* Search for a prefix.  */
11869         dirs[ndirs].prefix = -1;
11870         for (j = 0; j < ndirs; j++)
11871           if (dirs[j].length < dirs[ndirs].length
11872               && dirs[j].length > 1
11873               && (dirs[ndirs].prefix == -1
11874                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11875               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11876             dirs[ndirs].prefix = j;
11877
11878         ++ndirs;
11879       }
11880
11881   /* Now to the actual work.  We have to find a subset of the directories which
11882      allow expressing the file name using references to the directory table
11883      with the least amount of characters.  We do not do an exhaustive search
11884      where we would have to check out every combination of every single
11885      possible prefix.  Instead we use a heuristic which provides nearly optimal
11886      results in most cases and never is much off.  */
11887   saved = XALLOCAVEC (int, ndirs);
11888   savehere = XALLOCAVEC (int, ndirs);
11889
11890   memset (saved, '\0', ndirs * sizeof (saved[0]));
11891   for (i = 0; i < ndirs; i++)
11892     {
11893       int j;
11894       int total;
11895
11896       /* We can always save some space for the current directory.  But this
11897          does not mean it will be enough to justify adding the directory.  */
11898       savehere[i] = dirs[i].length;
11899       total = (savehere[i] - saved[i]) * dirs[i].count;
11900
11901       for (j = i + 1; j < ndirs; j++)
11902         {
11903           savehere[j] = 0;
11904           if (saved[j] < dirs[i].length)
11905             {
11906               /* Determine whether the dirs[i] path is a prefix of the
11907                  dirs[j] path.  */
11908               int k;
11909
11910               k = dirs[j].prefix;
11911               while (k != -1 && k != (int) i)
11912                 k = dirs[k].prefix;
11913
11914               if (k == (int) i)
11915                 {
11916                   /* Yes it is.  We can possibly save some memory by
11917                      writing the filenames in dirs[j] relative to
11918                      dirs[i].  */
11919                   savehere[j] = dirs[i].length;
11920                   total += (savehere[j] - saved[j]) * dirs[j].count;
11921                 }
11922             }
11923         }
11924
11925       /* Check whether we can save enough to justify adding the dirs[i]
11926          directory.  */
11927       if (total > dirs[i].length + 1)
11928         {
11929           /* It's worthwhile adding.  */
11930           for (j = i; j < ndirs; j++)
11931             if (savehere[j] > 0)
11932               {
11933                 /* Remember how much we saved for this directory so far.  */
11934                 saved[j] = savehere[j];
11935
11936                 /* Remember the prefix directory.  */
11937                 dirs[j].dir_idx = i;
11938               }
11939         }
11940     }
11941
11942   /* Emit the directory name table.  */
11943   idx_offset = dirs[0].length > 0 ? 1 : 0;
11944   for (i = 1 - idx_offset; i < ndirs; i++)
11945     dw2_asm_output_nstring (dirs[i].path,
11946                             dirs[i].length
11947                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11948                             "Directory Entry: %#x", i + idx_offset);
11949
11950   dw2_asm_output_data (1, 0, "End directory table");
11951
11952   /* We have to emit them in the order of emitted_number since that's
11953      used in the debug info generation.  To do this efficiently we
11954      generate a back-mapping of the indices first.  */
11955   backmap = XALLOCAVEC (int, numfiles);
11956   for (i = 0; i < numfiles; i++)
11957     backmap[files[i].file_idx->emitted_number - 1] = i;
11958
11959   /* Now write all the file names.  */
11960   for (i = 0; i < numfiles; i++)
11961     {
11962       int file_idx = backmap[i];
11963       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11964
11965 #ifdef VMS_DEBUGGING_INFO
11966 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11967
11968       /* Setting these fields can lead to debugger miscomparisons,
11969          but VMS Debug requires them to be set correctly.  */
11970
11971       int ver;
11972       long long cdt;
11973       long siz;
11974       int maxfilelen = strlen (files[file_idx].path)
11975                                + dirs[dir_idx].length
11976                                + MAX_VMS_VERSION_LEN + 1;
11977       char *filebuf = XALLOCAVEC (char, maxfilelen);
11978
11979       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11980       snprintf (filebuf, maxfilelen, "%s;%d",
11981                 files[file_idx].path + dirs[dir_idx].length, ver);
11982
11983       dw2_asm_output_nstring
11984         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11985
11986       /* Include directory index.  */
11987       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11988
11989       /* Modification time.  */
11990       dw2_asm_output_data_uleb128
11991         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11992           ? cdt : 0,
11993          NULL);
11994
11995       /* File length in bytes.  */
11996       dw2_asm_output_data_uleb128
11997         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11998           ? siz : 0,
11999          NULL);
12000 #else
12001       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12002                               "File Entry: %#x", (unsigned) i + 1);
12003
12004       /* Include directory index.  */
12005       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12006
12007       /* Modification time.  */
12008       dw2_asm_output_data_uleb128 (0, NULL);
12009
12010       /* File length in bytes.  */
12011       dw2_asm_output_data_uleb128 (0, NULL);
12012 #endif /* VMS_DEBUGGING_INFO */
12013     }
12014
12015   dw2_asm_output_data (1, 0, "End file name table");
12016 }
12017
12018
12019 /* Output the source line number correspondence information.  This
12020    information goes into the .debug_line section.  */
12021
12022 static void
12023 output_line_info (void)
12024 {
12025   char l1[20], l2[20], p1[20], p2[20];
12026   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12027   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12028   unsigned opc;
12029   unsigned n_op_args;
12030   unsigned long lt_index;
12031   unsigned long current_line;
12032   long line_offset;
12033   long line_delta;
12034   unsigned long current_file;
12035   unsigned long function;
12036   int ver = dwarf_version;
12037
12038   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12039   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12040   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12041   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12042
12043   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12044     dw2_asm_output_data (4, 0xffffffff,
12045       "Initial length escape value indicating 64-bit DWARF extension");
12046   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12047                         "Length of Source Line Info");
12048   ASM_OUTPUT_LABEL (asm_out_file, l1);
12049
12050   dw2_asm_output_data (2, ver, "DWARF Version");
12051   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12052   ASM_OUTPUT_LABEL (asm_out_file, p1);
12053
12054   /* Define the architecture-dependent minimum instruction length (in
12055    bytes).  In this implementation of DWARF, this field is used for
12056    information purposes only.  Since GCC generates assembly language,
12057    we have no a priori knowledge of how many instruction bytes are
12058    generated for each source line, and therefore can use only the
12059    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12060    commands.  Accordingly, we fix this as `1', which is "correct
12061    enough" for all architectures, and don't let the target override.  */
12062   dw2_asm_output_data (1, 1,
12063                        "Minimum Instruction Length");
12064
12065   if (ver >= 4)
12066     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12067                          "Maximum Operations Per Instruction");
12068   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12069                        "Default is_stmt_start flag");
12070   dw2_asm_output_data (1, DWARF_LINE_BASE,
12071                        "Line Base Value (Special Opcodes)");
12072   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12073                        "Line Range Value (Special Opcodes)");
12074   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12075                        "Special Opcode Base");
12076
12077   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12078     {
12079       switch (opc)
12080         {
12081         case DW_LNS_advance_pc:
12082         case DW_LNS_advance_line:
12083         case DW_LNS_set_file:
12084         case DW_LNS_set_column:
12085         case DW_LNS_fixed_advance_pc:
12086           n_op_args = 1;
12087           break;
12088         default:
12089           n_op_args = 0;
12090           break;
12091         }
12092
12093       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12094                            opc, n_op_args);
12095     }
12096
12097   /* Write out the information about the files we use.  */
12098   output_file_names ();
12099   ASM_OUTPUT_LABEL (asm_out_file, p2);
12100
12101   /* We used to set the address register to the first location in the text
12102      section here, but that didn't accomplish anything since we already
12103      have a line note for the opening brace of the first function.  */
12104
12105   /* Generate the line number to PC correspondence table, encoded as
12106      a series of state machine operations.  */
12107   current_file = 1;
12108   current_line = 1;
12109
12110   if (cfun && in_cold_section_p)
12111     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12112   else
12113     strcpy (prev_line_label, text_section_label);
12114   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12115     {
12116       dw_line_info_ref line_info = &line_info_table[lt_index];
12117
12118 #if 0
12119       /* Disable this optimization for now; GDB wants to see two line notes
12120          at the beginning of a function so it can find the end of the
12121          prologue.  */
12122
12123       /* Don't emit anything for redundant notes.  Just updating the
12124          address doesn't accomplish anything, because we already assume
12125          that anything after the last address is this line.  */
12126       if (line_info->dw_line_num == current_line
12127           && line_info->dw_file_num == current_file)
12128         continue;
12129 #endif
12130
12131       /* Emit debug info for the address of the current line.
12132
12133          Unfortunately, we have little choice here currently, and must always
12134          use the most general form.  GCC does not know the address delta
12135          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12136          attributes which will give an upper bound on the address range.  We
12137          could perhaps use length attributes to determine when it is safe to
12138          use DW_LNS_fixed_advance_pc.  */
12139
12140       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12141       if (0)
12142         {
12143           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12144           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12145                                "DW_LNS_fixed_advance_pc");
12146           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12147         }
12148       else
12149         {
12150           /* This can handle any delta.  This takes
12151              4+DWARF2_ADDR_SIZE bytes.  */
12152           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12153           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12154           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12155           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12156         }
12157
12158       strcpy (prev_line_label, line_label);
12159
12160       /* Emit debug info for the source file of the current line, if
12161          different from the previous line.  */
12162       if (line_info->dw_file_num != current_file)
12163         {
12164           current_file = line_info->dw_file_num;
12165           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12166           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12167         }
12168
12169       /* Emit debug info for the current line number, choosing the encoding
12170          that uses the least amount of space.  */
12171       if (line_info->dw_line_num != current_line)
12172         {
12173           line_offset = line_info->dw_line_num - current_line;
12174           line_delta = line_offset - DWARF_LINE_BASE;
12175           current_line = line_info->dw_line_num;
12176           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12177             /* This can handle deltas from -10 to 234, using the current
12178                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12179                takes 1 byte.  */
12180             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12181                                  "line %lu", current_line);
12182           else
12183             {
12184               /* This can handle any delta.  This takes at least 4 bytes,
12185                  depending on the value being encoded.  */
12186               dw2_asm_output_data (1, DW_LNS_advance_line,
12187                                    "advance to line %lu", current_line);
12188               dw2_asm_output_data_sleb128 (line_offset, NULL);
12189               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12190             }
12191         }
12192       else
12193         /* We still need to start a new row, so output a copy insn.  */
12194         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12195     }
12196
12197   /* Emit debug info for the address of the end of the function.  */
12198   if (0)
12199     {
12200       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12201                            "DW_LNS_fixed_advance_pc");
12202       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12203     }
12204   else
12205     {
12206       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12207       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12208       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12209       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12210     }
12211
12212   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12213   dw2_asm_output_data_uleb128 (1, NULL);
12214   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12215
12216   function = 0;
12217   current_file = 1;
12218   current_line = 1;
12219   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12220     {
12221       dw_separate_line_info_ref line_info
12222         = &separate_line_info_table[lt_index];
12223
12224 #if 0
12225       /* Don't emit anything for redundant notes.  */
12226       if (line_info->dw_line_num == current_line
12227           && line_info->dw_file_num == current_file
12228           && line_info->function == function)
12229         goto cont;
12230 #endif
12231
12232       /* Emit debug info for the address of the current line.  If this is
12233          a new function, or the first line of a function, then we need
12234          to handle it differently.  */
12235       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12236                                    lt_index);
12237       if (function != line_info->function)
12238         {
12239           function = line_info->function;
12240
12241           /* Set the address register to the first line in the function.  */
12242           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12243           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12244           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12245           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12246         }
12247       else
12248         {
12249           /* ??? See the DW_LNS_advance_pc comment above.  */
12250           if (0)
12251             {
12252               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12253                                    "DW_LNS_fixed_advance_pc");
12254               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12255             }
12256           else
12257             {
12258               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12259               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12260               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12261               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12262             }
12263         }
12264
12265       strcpy (prev_line_label, line_label);
12266
12267       /* Emit debug info for the source file of the current line, if
12268          different from the previous line.  */
12269       if (line_info->dw_file_num != current_file)
12270         {
12271           current_file = line_info->dw_file_num;
12272           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12273           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12274         }
12275
12276       /* Emit debug info for the current line number, choosing the encoding
12277          that uses the least amount of space.  */
12278       if (line_info->dw_line_num != current_line)
12279         {
12280           line_offset = line_info->dw_line_num - current_line;
12281           line_delta = line_offset - DWARF_LINE_BASE;
12282           current_line = line_info->dw_line_num;
12283           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12284             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12285                                  "line %lu", current_line);
12286           else
12287             {
12288               dw2_asm_output_data (1, DW_LNS_advance_line,
12289                                    "advance to line %lu", current_line);
12290               dw2_asm_output_data_sleb128 (line_offset, NULL);
12291               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12292             }
12293         }
12294       else
12295         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12296
12297 #if 0
12298     cont:
12299 #endif
12300
12301       lt_index++;
12302
12303       /* If we're done with a function, end its sequence.  */
12304       if (lt_index == separate_line_info_table_in_use
12305           || separate_line_info_table[lt_index].function != function)
12306         {
12307           current_file = 1;
12308           current_line = 1;
12309
12310           /* Emit debug info for the address of the end of the function.  */
12311           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12312           if (0)
12313             {
12314               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12315                                    "DW_LNS_fixed_advance_pc");
12316               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12317             }
12318           else
12319             {
12320               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12321               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12322               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12323               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12324             }
12325
12326           /* Output the marker for the end of this sequence.  */
12327           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12328           dw2_asm_output_data_uleb128 (1, NULL);
12329           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12330         }
12331     }
12332
12333   /* Output the marker for the end of the line number info.  */
12334   ASM_OUTPUT_LABEL (asm_out_file, l2);
12335 }
12336
12337 /* Return the size of the .debug_dcall table for the compilation unit.  */
12338
12339 static unsigned long
12340 size_of_dcall_table (void)
12341 {
12342   unsigned long size;
12343   unsigned int i;
12344   dcall_entry *p;
12345   tree last_poc_decl = NULL;
12346
12347   /* Header:  version + debug info section pointer + pointer size.  */
12348   size = 2 + DWARF_OFFSET_SIZE + 1;
12349
12350   /* Each entry:  code label + DIE offset.  */
12351   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12352     {
12353       gcc_assert (p->targ_die != NULL);
12354       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12355       if (p->poc_decl != last_poc_decl)
12356         {
12357           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12358           gcc_assert (poc_die);
12359           last_poc_decl = p->poc_decl;
12360           if (poc_die)
12361             size += (DWARF_OFFSET_SIZE
12362                      + size_of_uleb128 (poc_die->die_offset));
12363         }
12364       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12365     }
12366
12367   return size;
12368 }
12369
12370 /* Output the direct call table used to disambiguate PC values when
12371    identical function have been merged.  */
12372
12373 static void
12374 output_dcall_table (void)
12375 {
12376   unsigned i;
12377   unsigned long dcall_length = size_of_dcall_table ();
12378   dcall_entry *p;
12379   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12380   tree last_poc_decl = NULL;
12381
12382   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12383     dw2_asm_output_data (4, 0xffffffff,
12384       "Initial length escape value indicating 64-bit DWARF extension");
12385   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12386                        "Length of Direct Call Table");
12387   dw2_asm_output_data (2, 4, "Version number");
12388   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12389                          debug_info_section,
12390                          "Offset of Compilation Unit Info");
12391   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12392
12393   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12394     {
12395       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12396       if (p->poc_decl != last_poc_decl)
12397         {
12398           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12399           last_poc_decl = p->poc_decl;
12400           if (poc_die)
12401             {
12402               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12403               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12404                                            "Caller DIE offset");
12405             }
12406         }
12407       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12408       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12409       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12410                                    "Callee DIE offset");
12411     }
12412 }
12413 \f
12414 /* Return the size of the .debug_vcall table for the compilation unit.  */
12415
12416 static unsigned long
12417 size_of_vcall_table (void)
12418 {
12419   unsigned long size;
12420   unsigned int i;
12421   vcall_entry *p;
12422
12423   /* Header:  version + pointer size.  */
12424   size = 2 + 1;
12425
12426   /* Each entry:  code label + vtable slot index.  */
12427   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12428     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12429
12430   return size;
12431 }
12432
12433 /* Output the virtual call table used to disambiguate PC values when
12434    identical function have been merged.  */
12435
12436 static void
12437 output_vcall_table (void)
12438 {
12439   unsigned i;
12440   unsigned long vcall_length = size_of_vcall_table ();
12441   vcall_entry *p;
12442   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12443
12444   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12445     dw2_asm_output_data (4, 0xffffffff,
12446       "Initial length escape value indicating 64-bit DWARF extension");
12447   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12448                        "Length of Virtual Call Table");
12449   dw2_asm_output_data (2, 4, "Version number");
12450   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12451
12452   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12453     {
12454       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12455       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12456       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12457     }
12458 }
12459 \f
12460 /* Given a pointer to a tree node for some base type, return a pointer to
12461    a DIE that describes the given type.
12462
12463    This routine must only be called for GCC type nodes that correspond to
12464    Dwarf base (fundamental) types.  */
12465
12466 static dw_die_ref
12467 base_type_die (tree type)
12468 {
12469   dw_die_ref base_type_result;
12470   enum dwarf_type encoding;
12471
12472   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12473     return 0;
12474
12475   /* If this is a subtype that should not be emitted as a subrange type,
12476      use the base type.  See subrange_type_for_debug_p.  */
12477   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12478     type = TREE_TYPE (type);
12479
12480   switch (TREE_CODE (type))
12481     {
12482     case INTEGER_TYPE:
12483       if ((dwarf_version >= 4 || !dwarf_strict)
12484           && TYPE_NAME (type)
12485           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12486           && DECL_IS_BUILTIN (TYPE_NAME (type))
12487           && DECL_NAME (TYPE_NAME (type)))
12488         {
12489           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12490           if (strcmp (name, "char16_t") == 0
12491               || strcmp (name, "char32_t") == 0)
12492             {
12493               encoding = DW_ATE_UTF;
12494               break;
12495             }
12496         }
12497       if (TYPE_STRING_FLAG (type))
12498         {
12499           if (TYPE_UNSIGNED (type))
12500             encoding = DW_ATE_unsigned_char;
12501           else
12502             encoding = DW_ATE_signed_char;
12503         }
12504       else if (TYPE_UNSIGNED (type))
12505         encoding = DW_ATE_unsigned;
12506       else
12507         encoding = DW_ATE_signed;
12508       break;
12509
12510     case REAL_TYPE:
12511       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12512         {
12513           if (dwarf_version >= 3 || !dwarf_strict)
12514             encoding = DW_ATE_decimal_float;
12515           else
12516             encoding = DW_ATE_lo_user;
12517         }
12518       else
12519         encoding = DW_ATE_float;
12520       break;
12521
12522     case FIXED_POINT_TYPE:
12523       if (!(dwarf_version >= 3 || !dwarf_strict))
12524         encoding = DW_ATE_lo_user;
12525       else if (TYPE_UNSIGNED (type))
12526         encoding = DW_ATE_unsigned_fixed;
12527       else
12528         encoding = DW_ATE_signed_fixed;
12529       break;
12530
12531       /* Dwarf2 doesn't know anything about complex ints, so use
12532          a user defined type for it.  */
12533     case COMPLEX_TYPE:
12534       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12535         encoding = DW_ATE_complex_float;
12536       else
12537         encoding = DW_ATE_lo_user;
12538       break;
12539
12540     case BOOLEAN_TYPE:
12541       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12542       encoding = DW_ATE_boolean;
12543       break;
12544
12545     default:
12546       /* No other TREE_CODEs are Dwarf fundamental types.  */
12547       gcc_unreachable ();
12548     }
12549
12550   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12551
12552   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12553                    int_size_in_bytes (type));
12554   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12555
12556   return base_type_result;
12557 }
12558
12559 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12560    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12561
12562 static inline int
12563 is_base_type (tree type)
12564 {
12565   switch (TREE_CODE (type))
12566     {
12567     case ERROR_MARK:
12568     case VOID_TYPE:
12569     case INTEGER_TYPE:
12570     case REAL_TYPE:
12571     case FIXED_POINT_TYPE:
12572     case COMPLEX_TYPE:
12573     case BOOLEAN_TYPE:
12574       return 1;
12575
12576     case ARRAY_TYPE:
12577     case RECORD_TYPE:
12578     case UNION_TYPE:
12579     case QUAL_UNION_TYPE:
12580     case ENUMERAL_TYPE:
12581     case FUNCTION_TYPE:
12582     case METHOD_TYPE:
12583     case POINTER_TYPE:
12584     case REFERENCE_TYPE:
12585     case OFFSET_TYPE:
12586     case LANG_TYPE:
12587     case VECTOR_TYPE:
12588       return 0;
12589
12590     default:
12591       gcc_unreachable ();
12592     }
12593
12594   return 0;
12595 }
12596
12597 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12598    node, return the size in bits for the type if it is a constant, or else
12599    return the alignment for the type if the type's size is not constant, or
12600    else return BITS_PER_WORD if the type actually turns out to be an
12601    ERROR_MARK node.  */
12602
12603 static inline unsigned HOST_WIDE_INT
12604 simple_type_size_in_bits (const_tree type)
12605 {
12606   if (TREE_CODE (type) == ERROR_MARK)
12607     return BITS_PER_WORD;
12608   else if (TYPE_SIZE (type) == NULL_TREE)
12609     return 0;
12610   else if (host_integerp (TYPE_SIZE (type), 1))
12611     return tree_low_cst (TYPE_SIZE (type), 1);
12612   else
12613     return TYPE_ALIGN (type);
12614 }
12615
12616 /* Similarly, but return a double_int instead of UHWI.  */
12617
12618 static inline double_int
12619 double_int_type_size_in_bits (const_tree type)
12620 {
12621   if (TREE_CODE (type) == ERROR_MARK)
12622     return uhwi_to_double_int (BITS_PER_WORD);
12623   else if (TYPE_SIZE (type) == NULL_TREE)
12624     return double_int_zero;
12625   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12626     return tree_to_double_int (TYPE_SIZE (type));
12627   else
12628     return uhwi_to_double_int (TYPE_ALIGN (type));
12629 }
12630
12631 /*  Given a pointer to a tree node for a subrange type, return a pointer
12632     to a DIE that describes the given type.  */
12633
12634 static dw_die_ref
12635 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12636 {
12637   dw_die_ref subrange_die;
12638   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12639
12640   if (context_die == NULL)
12641     context_die = comp_unit_die;
12642
12643   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12644
12645   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12646     {
12647       /* The size of the subrange type and its base type do not match,
12648          so we need to generate a size attribute for the subrange type.  */
12649       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12650     }
12651
12652   if (low)
12653     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12654   if (high)
12655     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12656
12657   return subrange_die;
12658 }
12659
12660 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12661    entry that chains various modifiers in front of the given type.  */
12662
12663 static dw_die_ref
12664 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12665                    dw_die_ref context_die)
12666 {
12667   enum tree_code code = TREE_CODE (type);
12668   dw_die_ref mod_type_die;
12669   dw_die_ref sub_die = NULL;
12670   tree item_type = NULL;
12671   tree qualified_type;
12672   tree name, low, high;
12673
12674   if (code == ERROR_MARK)
12675     return NULL;
12676
12677   /* See if we already have the appropriately qualified variant of
12678      this type.  */
12679   qualified_type
12680     = get_qualified_type (type,
12681                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12682                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12683
12684   if (qualified_type == sizetype
12685       && TYPE_NAME (qualified_type)
12686       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12687     {
12688 #ifdef ENABLE_CHECKING
12689       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12690                   == INTEGER_TYPE
12691                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12692                      == TYPE_PRECISION (qualified_type)
12693                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12694                      == TYPE_UNSIGNED (qualified_type));
12695 #endif
12696       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12697     }
12698
12699   /* If we do, then we can just use its DIE, if it exists.  */
12700   if (qualified_type)
12701     {
12702       mod_type_die = lookup_type_die (qualified_type);
12703       if (mod_type_die)
12704         return mod_type_die;
12705     }
12706
12707   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12708
12709   /* Handle C typedef types.  */
12710   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12711       && !DECL_ARTIFICIAL (name))
12712     {
12713       tree dtype = TREE_TYPE (name);
12714
12715       if (qualified_type == dtype)
12716         {
12717           /* For a named type, use the typedef.  */
12718           gen_type_die (qualified_type, context_die);
12719           return lookup_type_die (qualified_type);
12720         }
12721       else if (is_const_type < TYPE_READONLY (dtype)
12722                || is_volatile_type < TYPE_VOLATILE (dtype)
12723                || (is_const_type <= TYPE_READONLY (dtype)
12724                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12725                    && DECL_ORIGINAL_TYPE (name) != type))
12726         /* cv-unqualified version of named type.  Just use the unnamed
12727            type to which it refers.  */
12728         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12729                                   is_const_type, is_volatile_type,
12730                                   context_die);
12731       /* Else cv-qualified version of named type; fall through.  */
12732     }
12733
12734   if (is_const_type)
12735     {
12736       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12737       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12738     }
12739   else if (is_volatile_type)
12740     {
12741       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12742       sub_die = modified_type_die (type, 0, 0, context_die);
12743     }
12744   else if (code == POINTER_TYPE)
12745     {
12746       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12747       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12748                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12749       item_type = TREE_TYPE (type);
12750       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12751         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12752                          TYPE_ADDR_SPACE (item_type));
12753     }
12754   else if (code == REFERENCE_TYPE)
12755     {
12756       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12757         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12758                                 type);
12759       else
12760         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12761       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12762                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12763       item_type = TREE_TYPE (type);
12764       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12765         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12766                          TYPE_ADDR_SPACE (item_type));
12767     }
12768   else if (code == INTEGER_TYPE
12769            && TREE_TYPE (type) != NULL_TREE
12770            && subrange_type_for_debug_p (type, &low, &high))
12771     {
12772       mod_type_die = subrange_type_die (type, low, high, context_die);
12773       item_type = TREE_TYPE (type);
12774     }
12775   else if (is_base_type (type))
12776     mod_type_die = base_type_die (type);
12777   else
12778     {
12779       gen_type_die (type, context_die);
12780
12781       /* We have to get the type_main_variant here (and pass that to the
12782          `lookup_type_die' routine) because the ..._TYPE node we have
12783          might simply be a *copy* of some original type node (where the
12784          copy was created to help us keep track of typedef names) and
12785          that copy might have a different TYPE_UID from the original
12786          ..._TYPE node.  */
12787       if (TREE_CODE (type) != VECTOR_TYPE)
12788         return lookup_type_die (type_main_variant (type));
12789       else
12790         /* Vectors have the debugging information in the type,
12791            not the main variant.  */
12792         return lookup_type_die (type);
12793     }
12794
12795   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12796      don't output a DW_TAG_typedef, since there isn't one in the
12797      user's program; just attach a DW_AT_name to the type.
12798      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12799      if the base type already has the same name.  */
12800   if (name
12801       && ((TREE_CODE (name) != TYPE_DECL
12802            && (qualified_type == TYPE_MAIN_VARIANT (type)
12803                || (!is_const_type && !is_volatile_type)))
12804           || (TREE_CODE (name) == TYPE_DECL
12805               && TREE_TYPE (name) == qualified_type
12806               && DECL_NAME (name))))
12807     {
12808       if (TREE_CODE (name) == TYPE_DECL)
12809         /* Could just call add_name_and_src_coords_attributes here,
12810            but since this is a builtin type it doesn't have any
12811            useful source coordinates anyway.  */
12812         name = DECL_NAME (name);
12813       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12814     }
12815   /* This probably indicates a bug.  */
12816   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12817     add_name_attribute (mod_type_die, "__unknown__");
12818
12819   if (qualified_type)
12820     equate_type_number_to_die (qualified_type, mod_type_die);
12821
12822   if (item_type)
12823     /* We must do this after the equate_type_number_to_die call, in case
12824        this is a recursive type.  This ensures that the modified_type_die
12825        recursion will terminate even if the type is recursive.  Recursive
12826        types are possible in Ada.  */
12827     sub_die = modified_type_die (item_type,
12828                                  TYPE_READONLY (item_type),
12829                                  TYPE_VOLATILE (item_type),
12830                                  context_die);
12831
12832   if (sub_die != NULL)
12833     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12834
12835   return mod_type_die;
12836 }
12837
12838 /* Generate DIEs for the generic parameters of T.
12839    T must be either a generic type or a generic function.
12840    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12841
12842 static void
12843 gen_generic_params_dies (tree t)
12844 {
12845   tree parms, args;
12846   int parms_num, i;
12847   dw_die_ref die = NULL;
12848
12849   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12850     return;
12851
12852   if (TYPE_P (t))
12853     die = lookup_type_die (t);
12854   else if (DECL_P (t))
12855     die = lookup_decl_die (t);
12856
12857   gcc_assert (die);
12858
12859   parms = lang_hooks.get_innermost_generic_parms (t);
12860   if (!parms)
12861     /* T has no generic parameter. It means T is neither a generic type
12862        or function. End of story.  */
12863     return;
12864
12865   parms_num = TREE_VEC_LENGTH (parms);
12866   args = lang_hooks.get_innermost_generic_args (t);
12867   for (i = 0; i < parms_num; i++)
12868     {
12869       tree parm, arg, arg_pack_elems;
12870
12871       parm = TREE_VEC_ELT (parms, i);
12872       arg = TREE_VEC_ELT (args, i);
12873       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12874       gcc_assert (parm && TREE_VALUE (parm) && arg);
12875
12876       if (parm && TREE_VALUE (parm) && arg)
12877         {
12878           /* If PARM represents a template parameter pack,
12879              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12880              by DW_TAG_template_*_parameter DIEs for the argument
12881              pack elements of ARG. Note that ARG would then be
12882              an argument pack.  */
12883           if (arg_pack_elems)
12884             template_parameter_pack_die (TREE_VALUE (parm),
12885                                          arg_pack_elems,
12886                                          die);
12887           else
12888             generic_parameter_die (TREE_VALUE (parm), arg,
12889                                    true /* Emit DW_AT_name */, die);
12890         }
12891     }
12892 }
12893
12894 /* Create and return a DIE for PARM which should be
12895    the representation of a generic type parameter.
12896    For instance, in the C++ front end, PARM would be a template parameter.
12897    ARG is the argument to PARM.
12898    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12899    name of the PARM.
12900    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12901    as a child node.  */
12902
12903 static dw_die_ref
12904 generic_parameter_die (tree parm, tree arg,
12905                        bool emit_name_p,
12906                        dw_die_ref parent_die)
12907 {
12908   dw_die_ref tmpl_die = NULL;
12909   const char *name = NULL;
12910
12911   if (!parm || !DECL_NAME (parm) || !arg)
12912     return NULL;
12913
12914   /* We support non-type generic parameters and arguments,
12915      type generic parameters and arguments, as well as
12916      generic generic parameters (a.k.a. template template parameters in C++)
12917      and arguments.  */
12918   if (TREE_CODE (parm) == PARM_DECL)
12919     /* PARM is a nontype generic parameter  */
12920     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12921   else if (TREE_CODE (parm) == TYPE_DECL)
12922     /* PARM is a type generic parameter.  */
12923     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12924   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12925     /* PARM is a generic generic parameter.
12926        Its DIE is a GNU extension. It shall have a
12927        DW_AT_name attribute to represent the name of the template template
12928        parameter, and a DW_AT_GNU_template_name attribute to represent the
12929        name of the template template argument.  */
12930     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12931                         parent_die, parm);
12932   else
12933     gcc_unreachable ();
12934
12935   if (tmpl_die)
12936     {
12937       tree tmpl_type;
12938
12939       /* If PARM is a generic parameter pack, it means we are
12940          emitting debug info for a template argument pack element.
12941          In other terms, ARG is a template argument pack element.
12942          In that case, we don't emit any DW_AT_name attribute for
12943          the die.  */
12944       if (emit_name_p)
12945         {
12946           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12947           gcc_assert (name);
12948           add_AT_string (tmpl_die, DW_AT_name, name);
12949         }
12950
12951       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12952         {
12953           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12954              TMPL_DIE should have a child DW_AT_type attribute that is set
12955              to the type of the argument to PARM, which is ARG.
12956              If PARM is a type generic parameter, TMPL_DIE should have a
12957              child DW_AT_type that is set to ARG.  */
12958           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12959           add_type_attribute (tmpl_die, tmpl_type, 0,
12960                               TREE_THIS_VOLATILE (tmpl_type),
12961                               parent_die);
12962         }
12963       else
12964         {
12965           /* So TMPL_DIE is a DIE representing a
12966              a generic generic template parameter, a.k.a template template
12967              parameter in C++ and arg is a template.  */
12968
12969           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12970              to the name of the argument.  */
12971           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12972           if (name)
12973             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12974         }
12975
12976       if (TREE_CODE (parm) == PARM_DECL)
12977         /* So PARM is a non-type generic parameter.
12978            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12979            attribute of TMPL_DIE which value represents the value
12980            of ARG.
12981            We must be careful here:
12982            The value of ARG might reference some function decls.
12983            We might currently be emitting debug info for a generic
12984            type and types are emitted before function decls, we don't
12985            know if the function decls referenced by ARG will actually be
12986            emitted after cgraph computations.
12987            So must defer the generation of the DW_AT_const_value to
12988            after cgraph is ready.  */
12989         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12990     }
12991
12992   return tmpl_die;
12993 }
12994
12995 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12996    PARM_PACK must be a template parameter pack. The returned DIE
12997    will be child DIE of PARENT_DIE.  */
12998
12999 static dw_die_ref
13000 template_parameter_pack_die (tree parm_pack,
13001                              tree parm_pack_args,
13002                              dw_die_ref parent_die)
13003 {
13004   dw_die_ref die;
13005   int j;
13006
13007   gcc_assert (parent_die && parm_pack);
13008
13009   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13010   add_name_and_src_coords_attributes (die, parm_pack);
13011   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13012     generic_parameter_die (parm_pack,
13013                            TREE_VEC_ELT (parm_pack_args, j),
13014                            false /* Don't emit DW_AT_name */,
13015                            die);
13016   return die;
13017 }
13018
13019 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13020    an enumerated type.  */
13021
13022 static inline int
13023 type_is_enum (const_tree type)
13024 {
13025   return TREE_CODE (type) == ENUMERAL_TYPE;
13026 }
13027
13028 /* Return the DBX register number described by a given RTL node.  */
13029
13030 static unsigned int
13031 dbx_reg_number (const_rtx rtl)
13032 {
13033   unsigned regno = REGNO (rtl);
13034
13035   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13036
13037 #ifdef LEAF_REG_REMAP
13038   if (current_function_uses_only_leaf_regs)
13039     {
13040       int leaf_reg = LEAF_REG_REMAP (regno);
13041       if (leaf_reg != -1)
13042         regno = (unsigned) leaf_reg;
13043     }
13044 #endif
13045
13046   return DBX_REGISTER_NUMBER (regno);
13047 }
13048
13049 /* Optionally add a DW_OP_piece term to a location description expression.
13050    DW_OP_piece is only added if the location description expression already
13051    doesn't end with DW_OP_piece.  */
13052
13053 static void
13054 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13055 {
13056   dw_loc_descr_ref loc;
13057
13058   if (*list_head != NULL)
13059     {
13060       /* Find the end of the chain.  */
13061       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13062         ;
13063
13064       if (loc->dw_loc_opc != DW_OP_piece)
13065         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13066     }
13067 }
13068
13069 /* Return a location descriptor that designates a machine register or
13070    zero if there is none.  */
13071
13072 static dw_loc_descr_ref
13073 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13074 {
13075   rtx regs;
13076
13077   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13078     return 0;
13079
13080   /* We only use "frame base" when we're sure we're talking about the
13081      post-prologue local stack frame.  We do this by *not* running
13082      register elimination until this point, and recognizing the special
13083      argument pointer and soft frame pointer rtx's.
13084      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13085   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13086       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13087     {
13088       dw_loc_descr_ref result = NULL;
13089
13090       if (dwarf_version >= 4 || !dwarf_strict)
13091         {
13092           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13093           if (result)
13094             add_loc_descr (&result,
13095                            new_loc_descr (DW_OP_stack_value, 0, 0));
13096         }
13097       return result;
13098     }
13099
13100   regs = targetm.dwarf_register_span (rtl);
13101
13102   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13103     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13104   else
13105     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13106 }
13107
13108 /* Return a location descriptor that designates a machine register for
13109    a given hard register number.  */
13110
13111 static dw_loc_descr_ref
13112 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13113 {
13114   dw_loc_descr_ref reg_loc_descr;
13115
13116   if (regno <= 31)
13117     reg_loc_descr
13118       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13119   else
13120     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13121
13122   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13123     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13124
13125   return reg_loc_descr;
13126 }
13127
13128 /* Given an RTL of a register, return a location descriptor that
13129    designates a value that spans more than one register.  */
13130
13131 static dw_loc_descr_ref
13132 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13133                              enum var_init_status initialized)
13134 {
13135   int nregs, size, i;
13136   unsigned reg;
13137   dw_loc_descr_ref loc_result = NULL;
13138
13139   reg = REGNO (rtl);
13140 #ifdef LEAF_REG_REMAP
13141   if (current_function_uses_only_leaf_regs)
13142     {
13143       int leaf_reg = LEAF_REG_REMAP (reg);
13144       if (leaf_reg != -1)
13145         reg = (unsigned) leaf_reg;
13146     }
13147 #endif
13148   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13149   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13150
13151   /* Simple, contiguous registers.  */
13152   if (regs == NULL_RTX)
13153     {
13154       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13155
13156       loc_result = NULL;
13157       while (nregs--)
13158         {
13159           dw_loc_descr_ref t;
13160
13161           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13162                                       VAR_INIT_STATUS_INITIALIZED);
13163           add_loc_descr (&loc_result, t);
13164           add_loc_descr_op_piece (&loc_result, size);
13165           ++reg;
13166         }
13167       return loc_result;
13168     }
13169
13170   /* Now onto stupid register sets in non contiguous locations.  */
13171
13172   gcc_assert (GET_CODE (regs) == PARALLEL);
13173
13174   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13175   loc_result = NULL;
13176
13177   for (i = 0; i < XVECLEN (regs, 0); ++i)
13178     {
13179       dw_loc_descr_ref t;
13180
13181       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13182                                   VAR_INIT_STATUS_INITIALIZED);
13183       add_loc_descr (&loc_result, t);
13184       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13185       add_loc_descr_op_piece (&loc_result, size);
13186     }
13187
13188   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13189     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13190   return loc_result;
13191 }
13192
13193 /* Return a location descriptor that designates a constant.  */
13194
13195 static dw_loc_descr_ref
13196 int_loc_descriptor (HOST_WIDE_INT i)
13197 {
13198   enum dwarf_location_atom op;
13199
13200   /* Pick the smallest representation of a constant, rather than just
13201      defaulting to the LEB encoding.  */
13202   if (i >= 0)
13203     {
13204       if (i <= 31)
13205         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13206       else if (i <= 0xff)
13207         op = DW_OP_const1u;
13208       else if (i <= 0xffff)
13209         op = DW_OP_const2u;
13210       else if (HOST_BITS_PER_WIDE_INT == 32
13211                || i <= 0xffffffff)
13212         op = DW_OP_const4u;
13213       else
13214         op = DW_OP_constu;
13215     }
13216   else
13217     {
13218       if (i >= -0x80)
13219         op = DW_OP_const1s;
13220       else if (i >= -0x8000)
13221         op = DW_OP_const2s;
13222       else if (HOST_BITS_PER_WIDE_INT == 32
13223                || i >= -0x80000000)
13224         op = DW_OP_const4s;
13225       else
13226         op = DW_OP_consts;
13227     }
13228
13229   return new_loc_descr (op, i, 0);
13230 }
13231
13232 /* Return loc description representing "address" of integer value.
13233    This can appear only as toplevel expression.  */
13234
13235 static dw_loc_descr_ref
13236 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13237 {
13238   int litsize;
13239   dw_loc_descr_ref loc_result = NULL;
13240
13241   if (!(dwarf_version >= 4 || !dwarf_strict))
13242     return NULL;
13243
13244   if (i >= 0)
13245     {
13246       if (i <= 31)
13247         litsize = 1;
13248       else if (i <= 0xff)
13249         litsize = 2;
13250       else if (i <= 0xffff)
13251         litsize = 3;
13252       else if (HOST_BITS_PER_WIDE_INT == 32
13253                || i <= 0xffffffff)
13254         litsize = 5;
13255       else
13256         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13257     }
13258   else
13259     {
13260       if (i >= -0x80)
13261         litsize = 2;
13262       else if (i >= -0x8000)
13263         litsize = 3;
13264       else if (HOST_BITS_PER_WIDE_INT == 32
13265                || i >= -0x80000000)
13266         litsize = 5;
13267       else
13268         litsize = 1 + size_of_sleb128 (i);
13269     }
13270   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13271      is more compact.  For DW_OP_stack_value we need:
13272      litsize + 1 (DW_OP_stack_value)
13273      and for DW_OP_implicit_value:
13274      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13275   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13276     {
13277       loc_result = int_loc_descriptor (i);
13278       add_loc_descr (&loc_result,
13279                      new_loc_descr (DW_OP_stack_value, 0, 0));
13280       return loc_result;
13281     }
13282
13283   loc_result = new_loc_descr (DW_OP_implicit_value,
13284                               size, 0);
13285   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13286   loc_result->dw_loc_oprnd2.v.val_int = i;
13287   return loc_result;
13288 }
13289
13290 /* Return a location descriptor that designates a base+offset location.  */
13291
13292 static dw_loc_descr_ref
13293 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13294                  enum var_init_status initialized)
13295 {
13296   unsigned int regno;
13297   dw_loc_descr_ref result;
13298   dw_fde_ref fde = current_fde ();
13299
13300   /* We only use "frame base" when we're sure we're talking about the
13301      post-prologue local stack frame.  We do this by *not* running
13302      register elimination until this point, and recognizing the special
13303      argument pointer and soft frame pointer rtx's.  */
13304   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13305     {
13306       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13307
13308       if (elim != reg)
13309         {
13310           if (GET_CODE (elim) == PLUS)
13311             {
13312               offset += INTVAL (XEXP (elim, 1));
13313               elim = XEXP (elim, 0);
13314             }
13315           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13316                        && (elim == hard_frame_pointer_rtx
13317                            || elim == stack_pointer_rtx))
13318                       || elim == (frame_pointer_needed
13319                                   ? hard_frame_pointer_rtx
13320                                   : stack_pointer_rtx));
13321
13322           /* If drap register is used to align stack, use frame
13323              pointer + offset to access stack variables.  If stack
13324              is aligned without drap, use stack pointer + offset to
13325              access stack variables.  */
13326           if (crtl->stack_realign_tried
13327               && reg == frame_pointer_rtx)
13328             {
13329               int base_reg
13330                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13331                                       ? HARD_FRAME_POINTER_REGNUM
13332                                       : STACK_POINTER_REGNUM);
13333               return new_reg_loc_descr (base_reg, offset);
13334             }
13335
13336           offset += frame_pointer_fb_offset;
13337           return new_loc_descr (DW_OP_fbreg, offset, 0);
13338         }
13339     }
13340   else if (!optimize
13341            && fde
13342            && (fde->drap_reg == REGNO (reg)
13343                || fde->vdrap_reg == REGNO (reg)))
13344     {
13345       /* Use cfa+offset to represent the location of arguments passed
13346          on the stack when drap is used to align stack.
13347          Only do this when not optimizing, for optimized code var-tracking
13348          is supposed to track where the arguments live and the register
13349          used as vdrap or drap in some spot might be used for something
13350          else in other part of the routine.  */
13351       return new_loc_descr (DW_OP_fbreg, offset, 0);
13352     }
13353
13354   regno = dbx_reg_number (reg);
13355   if (regno <= 31)
13356     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13357                             offset, 0);
13358   else
13359     result = new_loc_descr (DW_OP_bregx, regno, offset);
13360
13361   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13362     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13363
13364   return result;
13365 }
13366
13367 /* Return true if this RTL expression describes a base+offset calculation.  */
13368
13369 static inline int
13370 is_based_loc (const_rtx rtl)
13371 {
13372   return (GET_CODE (rtl) == PLUS
13373           && ((REG_P (XEXP (rtl, 0))
13374                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13375                && CONST_INT_P (XEXP (rtl, 1)))));
13376 }
13377
13378 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13379    failed.  */
13380
13381 static dw_loc_descr_ref
13382 tls_mem_loc_descriptor (rtx mem)
13383 {
13384   tree base;
13385   dw_loc_descr_ref loc_result;
13386
13387   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13388     return NULL;
13389
13390   base = get_base_address (MEM_EXPR (mem));
13391   if (base == NULL
13392       || TREE_CODE (base) != VAR_DECL
13393       || !DECL_THREAD_LOCAL_P (base))
13394     return NULL;
13395
13396   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13397   if (loc_result == NULL)
13398     return NULL;
13399
13400   if (INTVAL (MEM_OFFSET (mem)))
13401     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13402
13403   return loc_result;
13404 }
13405
13406 /* Output debug info about reason why we failed to expand expression as dwarf
13407    expression.  */
13408
13409 static void
13410 expansion_failed (tree expr, rtx rtl, char const *reason)
13411 {
13412   if (dump_file && (dump_flags & TDF_DETAILS))
13413     {
13414       fprintf (dump_file, "Failed to expand as dwarf: ");
13415       if (expr)
13416         print_generic_expr (dump_file, expr, dump_flags);
13417       if (rtl)
13418         {
13419           fprintf (dump_file, "\n");
13420           print_rtl (dump_file, rtl);
13421         }
13422       fprintf (dump_file, "\nReason: %s\n", reason);
13423     }
13424 }
13425
13426 /* Helper function for const_ok_for_output, called either directly
13427    or via for_each_rtx.  */
13428
13429 static int
13430 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13431 {
13432   rtx rtl = *rtlp;
13433
13434   if (GET_CODE (rtl) == UNSPEC)
13435     {
13436       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13437          we can't express it in the debug info.  */
13438 #ifdef ENABLE_CHECKING
13439       inform (current_function_decl
13440               ? DECL_SOURCE_LOCATION (current_function_decl)
13441               : UNKNOWN_LOCATION,
13442               "non-delegitimized UNSPEC %d found in variable location",
13443               XINT (rtl, 1));
13444 #endif
13445       expansion_failed (NULL_TREE, rtl,
13446                         "UNSPEC hasn't been delegitimized.\n");
13447       return 1;
13448     }
13449
13450   if (GET_CODE (rtl) != SYMBOL_REF)
13451     return 0;
13452
13453   if (CONSTANT_POOL_ADDRESS_P (rtl))
13454     {
13455       bool marked;
13456       get_pool_constant_mark (rtl, &marked);
13457       /* If all references to this pool constant were optimized away,
13458          it was not output and thus we can't represent it.  */
13459       if (!marked)
13460         {
13461           expansion_failed (NULL_TREE, rtl,
13462                             "Constant was removed from constant pool.\n");
13463           return 1;
13464         }
13465     }
13466
13467   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13468     return 1;
13469
13470   /* Avoid references to external symbols in debug info, on several targets
13471      the linker might even refuse to link when linking a shared library,
13472      and in many other cases the relocations for .debug_info/.debug_loc are
13473      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13474      to be defined within the same shared library or executable are fine.  */
13475   if (SYMBOL_REF_EXTERNAL_P (rtl))
13476     {
13477       tree decl = SYMBOL_REF_DECL (rtl);
13478
13479       if (decl == NULL || !targetm.binds_local_p (decl))
13480         {
13481           expansion_failed (NULL_TREE, rtl,
13482                             "Symbol not defined in current TU.\n");
13483           return 1;
13484         }
13485     }
13486
13487   return 0;
13488 }
13489
13490 /* Return true if constant RTL can be emitted in DW_OP_addr or
13491    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13492    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13493
13494 static bool
13495 const_ok_for_output (rtx rtl)
13496 {
13497   if (GET_CODE (rtl) == SYMBOL_REF)
13498     return const_ok_for_output_1 (&rtl, NULL) == 0;
13499
13500   if (GET_CODE (rtl) == CONST)
13501     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13502
13503   return true;
13504 }
13505
13506 /* The following routine converts the RTL for a variable or parameter
13507    (resident in memory) into an equivalent Dwarf representation of a
13508    mechanism for getting the address of that same variable onto the top of a
13509    hypothetical "address evaluation" stack.
13510
13511    When creating memory location descriptors, we are effectively transforming
13512    the RTL for a memory-resident object into its Dwarf postfix expression
13513    equivalent.  This routine recursively descends an RTL tree, turning
13514    it into Dwarf postfix code as it goes.
13515
13516    MODE is the mode of the memory reference, needed to handle some
13517    autoincrement addressing modes.
13518
13519    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13520    location list for RTL.
13521
13522    Return 0 if we can't represent the location.  */
13523
13524 static dw_loc_descr_ref
13525 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13526                     enum var_init_status initialized)
13527 {
13528   dw_loc_descr_ref mem_loc_result = NULL;
13529   enum dwarf_location_atom op;
13530   dw_loc_descr_ref op0, op1;
13531
13532   /* Note that for a dynamically sized array, the location we will generate a
13533      description of here will be the lowest numbered location which is
13534      actually within the array.  That's *not* necessarily the same as the
13535      zeroth element of the array.  */
13536
13537   rtl = targetm.delegitimize_address (rtl);
13538
13539   switch (GET_CODE (rtl))
13540     {
13541     case POST_INC:
13542     case POST_DEC:
13543     case POST_MODIFY:
13544       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13545
13546     case SUBREG:
13547       /* The case of a subreg may arise when we have a local (register)
13548          variable or a formal (register) parameter which doesn't quite fill
13549          up an entire register.  For now, just assume that it is
13550          legitimate to make the Dwarf info refer to the whole register which
13551          contains the given subreg.  */
13552       if (!subreg_lowpart_p (rtl))
13553         break;
13554       rtl = SUBREG_REG (rtl);
13555       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13556         break;
13557       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13558         break;
13559       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13560       break;
13561
13562     case REG:
13563       /* Whenever a register number forms a part of the description of the
13564          method for calculating the (dynamic) address of a memory resident
13565          object, DWARF rules require the register number be referred to as
13566          a "base register".  This distinction is not based in any way upon
13567          what category of register the hardware believes the given register
13568          belongs to.  This is strictly DWARF terminology we're dealing with
13569          here. Note that in cases where the location of a memory-resident
13570          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13571          OP_CONST (0)) the actual DWARF location descriptor that we generate
13572          may just be OP_BASEREG (basereg).  This may look deceptively like
13573          the object in question was allocated to a register (rather than in
13574          memory) so DWARF consumers need to be aware of the subtle
13575          distinction between OP_REG and OP_BASEREG.  */
13576       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13577         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13578       else if (stack_realign_drap
13579                && crtl->drap_reg
13580                && crtl->args.internal_arg_pointer == rtl
13581                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13582         {
13583           /* If RTL is internal_arg_pointer, which has been optimized
13584              out, use DRAP instead.  */
13585           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13586                                             VAR_INIT_STATUS_INITIALIZED);
13587         }
13588       break;
13589
13590     case SIGN_EXTEND:
13591     case ZERO_EXTEND:
13592       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13593                                 VAR_INIT_STATUS_INITIALIZED);
13594       if (op0 == 0)
13595         break;
13596       else
13597         {
13598           int shift = DWARF2_ADDR_SIZE
13599                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13600           shift *= BITS_PER_UNIT;
13601           if (GET_CODE (rtl) == SIGN_EXTEND)
13602             op = DW_OP_shra;
13603           else
13604             op = DW_OP_shr;
13605           mem_loc_result = op0;
13606           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13607           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13608           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13609           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13610         }
13611       break;
13612
13613     case MEM:
13614       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13615                                            VAR_INIT_STATUS_INITIALIZED);
13616       if (mem_loc_result == NULL)
13617         mem_loc_result = tls_mem_loc_descriptor (rtl);
13618       if (mem_loc_result != 0)
13619         {
13620           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13621             {
13622               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13623               return 0;
13624             }
13625           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13626             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13627           else
13628             add_loc_descr (&mem_loc_result,
13629                            new_loc_descr (DW_OP_deref_size,
13630                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13631         }
13632       else
13633         {
13634           rtx new_rtl = avoid_constant_pool_reference (rtl);
13635           if (new_rtl != rtl)
13636             return mem_loc_descriptor (new_rtl, mode, initialized);
13637         }
13638       break;
13639
13640     case LO_SUM:
13641          rtl = XEXP (rtl, 1);
13642
13643       /* ... fall through ...  */
13644
13645     case LABEL_REF:
13646       /* Some ports can transform a symbol ref into a label ref, because
13647          the symbol ref is too far away and has to be dumped into a constant
13648          pool.  */
13649     case CONST:
13650     case SYMBOL_REF:
13651       if (GET_CODE (rtl) == SYMBOL_REF
13652           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13653         {
13654           dw_loc_descr_ref temp;
13655
13656           /* If this is not defined, we have no way to emit the data.  */
13657           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13658             break;
13659
13660           /* We used to emit DW_OP_addr here, but that's wrong, since
13661              DW_OP_addr should be relocated by the debug info consumer,
13662              while DW_OP_GNU_push_tls_address operand should not.  */
13663           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13664                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13665           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13666           temp->dw_loc_oprnd1.v.val_addr = rtl;
13667           temp->dtprel = true;
13668
13669           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13670           add_loc_descr (&mem_loc_result, temp);
13671
13672           break;
13673         }
13674
13675       if (!const_ok_for_output (rtl))
13676         break;
13677
13678     symref:
13679       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13680       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13681       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13682       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13683       break;
13684
13685     case CONCAT:
13686     case CONCATN:
13687     case VAR_LOCATION:
13688     case DEBUG_IMPLICIT_PTR:
13689       expansion_failed (NULL_TREE, rtl,
13690                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13691       return 0;
13692
13693     case PRE_MODIFY:
13694       /* Extract the PLUS expression nested inside and fall into
13695          PLUS code below.  */
13696       rtl = XEXP (rtl, 1);
13697       goto plus;
13698
13699     case PRE_INC:
13700     case PRE_DEC:
13701       /* Turn these into a PLUS expression and fall into the PLUS code
13702          below.  */
13703       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13704                           GEN_INT (GET_CODE (rtl) == PRE_INC
13705                                    ? GET_MODE_UNIT_SIZE (mode)
13706                                    : -GET_MODE_UNIT_SIZE (mode)));
13707
13708       /* ... fall through ...  */
13709
13710     case PLUS:
13711     plus:
13712       if (is_based_loc (rtl))
13713         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13714                                           INTVAL (XEXP (rtl, 1)),
13715                                           VAR_INIT_STATUS_INITIALIZED);
13716       else
13717         {
13718           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13719                                                VAR_INIT_STATUS_INITIALIZED);
13720           if (mem_loc_result == 0)
13721             break;
13722
13723           if (CONST_INT_P (XEXP (rtl, 1)))
13724             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13725           else
13726             {
13727               dw_loc_descr_ref mem_loc_result2
13728                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13729                                       VAR_INIT_STATUS_INITIALIZED);
13730               if (mem_loc_result2 == 0)
13731                 break;
13732               add_loc_descr (&mem_loc_result, mem_loc_result2);
13733               add_loc_descr (&mem_loc_result,
13734                              new_loc_descr (DW_OP_plus, 0, 0));
13735             }
13736         }
13737       break;
13738
13739     /* If a pseudo-reg is optimized away, it is possible for it to
13740        be replaced with a MEM containing a multiply or shift.  */
13741     case MINUS:
13742       op = DW_OP_minus;
13743       goto do_binop;
13744
13745     case MULT:
13746       op = DW_OP_mul;
13747       goto do_binop;
13748
13749     case DIV:
13750       op = DW_OP_div;
13751       goto do_binop;
13752
13753     case UMOD:
13754       op = DW_OP_mod;
13755       goto do_binop;
13756
13757     case ASHIFT:
13758       op = DW_OP_shl;
13759       goto do_binop;
13760
13761     case ASHIFTRT:
13762       op = DW_OP_shra;
13763       goto do_binop;
13764
13765     case LSHIFTRT:
13766       op = DW_OP_shr;
13767       goto do_binop;
13768
13769     case AND:
13770       op = DW_OP_and;
13771       goto do_binop;
13772
13773     case IOR:
13774       op = DW_OP_or;
13775       goto do_binop;
13776
13777     case XOR:
13778       op = DW_OP_xor;
13779       goto do_binop;
13780
13781     do_binop:
13782       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13783                                 VAR_INIT_STATUS_INITIALIZED);
13784       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13785                                 VAR_INIT_STATUS_INITIALIZED);
13786
13787       if (op0 == 0 || op1 == 0)
13788         break;
13789
13790       mem_loc_result = op0;
13791       add_loc_descr (&mem_loc_result, op1);
13792       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13793       break;
13794
13795     case MOD:
13796       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13797                                 VAR_INIT_STATUS_INITIALIZED);
13798       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13799                                 VAR_INIT_STATUS_INITIALIZED);
13800
13801       if (op0 == 0 || op1 == 0)
13802         break;
13803
13804       mem_loc_result = op0;
13805       add_loc_descr (&mem_loc_result, op1);
13806       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13807       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13808       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13809       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13810       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13811       break;
13812
13813     case NOT:
13814       op = DW_OP_not;
13815       goto do_unop;
13816
13817     case ABS:
13818       op = DW_OP_abs;
13819       goto do_unop;
13820
13821     case NEG:
13822       op = DW_OP_neg;
13823       goto do_unop;
13824
13825     do_unop:
13826       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13827                                 VAR_INIT_STATUS_INITIALIZED);
13828
13829       if (op0 == 0)
13830         break;
13831
13832       mem_loc_result = op0;
13833       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13834       break;
13835
13836     case CONST_INT:
13837       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13838       break;
13839
13840     case EQ:
13841       op = DW_OP_eq;
13842       goto do_scompare;
13843
13844     case GE:
13845       op = DW_OP_ge;
13846       goto do_scompare;
13847
13848     case GT:
13849       op = DW_OP_gt;
13850       goto do_scompare;
13851
13852     case LE:
13853       op = DW_OP_le;
13854       goto do_scompare;
13855
13856     case LT:
13857       op = DW_OP_lt;
13858       goto do_scompare;
13859
13860     case NE:
13861       op = DW_OP_ne;
13862       goto do_scompare;
13863
13864     do_scompare:
13865       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13866           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13867         break;
13868       else
13869         {
13870           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13871
13872           if (op_mode == VOIDmode)
13873             op_mode = GET_MODE (XEXP (rtl, 1));
13874           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13875             break;
13876
13877           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13878                                     VAR_INIT_STATUS_INITIALIZED);
13879           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13880                                     VAR_INIT_STATUS_INITIALIZED);
13881
13882           if (op0 == 0 || op1 == 0)
13883             break;
13884
13885           if (op_mode != VOIDmode
13886               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13887             {
13888               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13889               shift *= BITS_PER_UNIT;
13890               /* For eq/ne, if the operands are known to be zero-extended,
13891                  there is no need to do the fancy shifting up.  */
13892               if (op == DW_OP_eq || op == DW_OP_ne)
13893                 {
13894                   dw_loc_descr_ref last0, last1;
13895                   for (last0 = op0;
13896                        last0->dw_loc_next != NULL;
13897                        last0 = last0->dw_loc_next)
13898                     ;
13899                   for (last1 = op1;
13900                        last1->dw_loc_next != NULL;
13901                        last1 = last1->dw_loc_next)
13902                     ;
13903                   /* deref_size zero extends, and for constants we can check
13904                      whether they are zero extended or not.  */
13905                   if (((last0->dw_loc_opc == DW_OP_deref_size
13906                         && last0->dw_loc_oprnd1.v.val_int
13907                            <= GET_MODE_SIZE (op_mode))
13908                        || (CONST_INT_P (XEXP (rtl, 0))
13909                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13910                                == (INTVAL (XEXP (rtl, 0))
13911                                    & GET_MODE_MASK (op_mode))))
13912                       && ((last1->dw_loc_opc == DW_OP_deref_size
13913                            && last1->dw_loc_oprnd1.v.val_int
13914                               <= GET_MODE_SIZE (op_mode))
13915                           || (CONST_INT_P (XEXP (rtl, 1))
13916                               && (unsigned HOST_WIDE_INT)
13917                                  INTVAL (XEXP (rtl, 1))
13918                                  == (INTVAL (XEXP (rtl, 1))
13919                                      & GET_MODE_MASK (op_mode)))))
13920                     goto do_compare;
13921                 }
13922               add_loc_descr (&op0, int_loc_descriptor (shift));
13923               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13924               if (CONST_INT_P (XEXP (rtl, 1)))
13925                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13926               else
13927                 {
13928                   add_loc_descr (&op1, int_loc_descriptor (shift));
13929                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13930                 }
13931             }
13932         }
13933
13934     do_compare:
13935       mem_loc_result = op0;
13936       add_loc_descr (&mem_loc_result, op1);
13937       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13938       if (STORE_FLAG_VALUE != 1)
13939         {
13940           add_loc_descr (&mem_loc_result,
13941                          int_loc_descriptor (STORE_FLAG_VALUE));
13942           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13943         }
13944       break;
13945
13946     case GEU:
13947       op = DW_OP_ge;
13948       goto do_ucompare;
13949
13950     case GTU:
13951       op = DW_OP_gt;
13952       goto do_ucompare;
13953
13954     case LEU:
13955       op = DW_OP_le;
13956       goto do_ucompare;
13957
13958     case LTU:
13959       op = DW_OP_lt;
13960       goto do_ucompare;
13961
13962     do_ucompare:
13963       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13964           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13965         break;
13966       else
13967         {
13968           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13969
13970           if (op_mode == VOIDmode)
13971             op_mode = GET_MODE (XEXP (rtl, 1));
13972           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13973             break;
13974
13975           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13976                                     VAR_INIT_STATUS_INITIALIZED);
13977           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13978                                     VAR_INIT_STATUS_INITIALIZED);
13979
13980           if (op0 == 0 || op1 == 0)
13981             break;
13982
13983           if (op_mode != VOIDmode
13984               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13985             {
13986               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13987               dw_loc_descr_ref last0, last1;
13988               for (last0 = op0;
13989                    last0->dw_loc_next != NULL;
13990                    last0 = last0->dw_loc_next)
13991                 ;
13992               for (last1 = op1;
13993                    last1->dw_loc_next != NULL;
13994                    last1 = last1->dw_loc_next)
13995                 ;
13996               if (CONST_INT_P (XEXP (rtl, 0)))
13997                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13998               /* deref_size zero extends, so no need to mask it again.  */
13999               else if (last0->dw_loc_opc != DW_OP_deref_size
14000                        || last0->dw_loc_oprnd1.v.val_int
14001                           > GET_MODE_SIZE (op_mode))
14002                 {
14003                   add_loc_descr (&op0, int_loc_descriptor (mask));
14004                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14005                 }
14006               if (CONST_INT_P (XEXP (rtl, 1)))
14007                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14008               /* deref_size zero extends, so no need to mask it again.  */
14009               else if (last1->dw_loc_opc != DW_OP_deref_size
14010                        || last1->dw_loc_oprnd1.v.val_int
14011                           > GET_MODE_SIZE (op_mode))
14012                 {
14013                   add_loc_descr (&op1, int_loc_descriptor (mask));
14014                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14015                 }
14016             }
14017           else
14018             {
14019               HOST_WIDE_INT bias = 1;
14020               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14021               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14022               if (CONST_INT_P (XEXP (rtl, 1)))
14023                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14024                                           + INTVAL (XEXP (rtl, 1)));
14025               else
14026                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14027                                                     bias, 0));
14028             }
14029         }
14030       goto do_compare;
14031
14032     case SMIN:
14033     case SMAX:
14034     case UMIN:
14035     case UMAX:
14036       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14037           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14038           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14039         break;
14040
14041       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14042                                 VAR_INIT_STATUS_INITIALIZED);
14043       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14044                                 VAR_INIT_STATUS_INITIALIZED);
14045
14046       if (op0 == 0 || op1 == 0)
14047         break;
14048
14049       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14050       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14051       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14052       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14053         {
14054           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14055             {
14056               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14057               add_loc_descr (&op0, int_loc_descriptor (mask));
14058               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14059               add_loc_descr (&op1, int_loc_descriptor (mask));
14060               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14061             }
14062           else
14063             {
14064               HOST_WIDE_INT bias = 1;
14065               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14066               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14067               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14068             }
14069         }
14070       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14071         {
14072           int shift = DWARF2_ADDR_SIZE
14073                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14074           shift *= BITS_PER_UNIT;
14075           add_loc_descr (&op0, int_loc_descriptor (shift));
14076           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14077           add_loc_descr (&op1, int_loc_descriptor (shift));
14078           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14079         }
14080
14081       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14082         op = DW_OP_lt;
14083       else
14084         op = DW_OP_gt;
14085       mem_loc_result = op0;
14086       add_loc_descr (&mem_loc_result, op1);
14087       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14088       {
14089         dw_loc_descr_ref bra_node, drop_node;
14090
14091         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14092         add_loc_descr (&mem_loc_result, bra_node);
14093         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14094         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14095         add_loc_descr (&mem_loc_result, drop_node);
14096         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14097         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14098       }
14099       break;
14100
14101     case ZERO_EXTRACT:
14102     case SIGN_EXTRACT:
14103       if (CONST_INT_P (XEXP (rtl, 1))
14104           && CONST_INT_P (XEXP (rtl, 2))
14105           && ((unsigned) INTVAL (XEXP (rtl, 1))
14106               + (unsigned) INTVAL (XEXP (rtl, 2))
14107               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14108           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14109           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14110         {
14111           int shift, size;
14112           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14113                                     VAR_INIT_STATUS_INITIALIZED);
14114           if (op0 == 0)
14115             break;
14116           if (GET_CODE (rtl) == SIGN_EXTRACT)
14117             op = DW_OP_shra;
14118           else
14119             op = DW_OP_shr;
14120           mem_loc_result = op0;
14121           size = INTVAL (XEXP (rtl, 1));
14122           shift = INTVAL (XEXP (rtl, 2));
14123           if (BITS_BIG_ENDIAN)
14124             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14125                     - shift - size;
14126           if (shift + size != (int) DWARF2_ADDR_SIZE)
14127             {
14128               add_loc_descr (&mem_loc_result,
14129                              int_loc_descriptor (DWARF2_ADDR_SIZE
14130                                                  - shift - size));
14131               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14132             }
14133           if (size != (int) DWARF2_ADDR_SIZE)
14134             {
14135               add_loc_descr (&mem_loc_result,
14136                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14137               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14138             }
14139         }
14140       break;
14141
14142     case COMPARE:
14143     case IF_THEN_ELSE:
14144     case ROTATE:
14145     case ROTATERT:
14146     case TRUNCATE:
14147       /* In theory, we could implement the above.  */
14148       /* DWARF cannot represent the unsigned compare operations
14149          natively.  */
14150     case SS_MULT:
14151     case US_MULT:
14152     case SS_DIV:
14153     case US_DIV:
14154     case SS_PLUS:
14155     case US_PLUS:
14156     case SS_MINUS:
14157     case US_MINUS:
14158     case SS_NEG:
14159     case US_NEG:
14160     case SS_ABS:
14161     case SS_ASHIFT:
14162     case US_ASHIFT:
14163     case SS_TRUNCATE:
14164     case US_TRUNCATE:
14165     case UDIV:
14166     case UNORDERED:
14167     case ORDERED:
14168     case UNEQ:
14169     case UNGE:
14170     case UNGT:
14171     case UNLE:
14172     case UNLT:
14173     case LTGT:
14174     case FLOAT_EXTEND:
14175     case FLOAT_TRUNCATE:
14176     case FLOAT:
14177     case UNSIGNED_FLOAT:
14178     case FIX:
14179     case UNSIGNED_FIX:
14180     case FRACT_CONVERT:
14181     case UNSIGNED_FRACT_CONVERT:
14182     case SAT_FRACT:
14183     case UNSIGNED_SAT_FRACT:
14184     case SQRT:
14185     case BSWAP:
14186     case FFS:
14187     case CLZ:
14188     case CTZ:
14189     case POPCOUNT:
14190     case PARITY:
14191     case ASM_OPERANDS:
14192     case VEC_MERGE:
14193     case VEC_SELECT:
14194     case VEC_CONCAT:
14195     case VEC_DUPLICATE:
14196     case UNSPEC:
14197     case HIGH:
14198       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14199          can't express it in the debug info.  This can happen e.g. with some
14200          TLS UNSPECs.  */
14201       break;
14202
14203     case CONST_STRING:
14204       resolve_one_addr (&rtl, NULL);
14205       goto symref;
14206
14207     default:
14208 #ifdef ENABLE_CHECKING
14209       print_rtl (stderr, rtl);
14210       gcc_unreachable ();
14211 #else
14212       break;
14213 #endif
14214     }
14215
14216   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14217     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14218
14219   return mem_loc_result;
14220 }
14221
14222 /* Return a descriptor that describes the concatenation of two locations.
14223    This is typically a complex variable.  */
14224
14225 static dw_loc_descr_ref
14226 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14227 {
14228   dw_loc_descr_ref cc_loc_result = NULL;
14229   dw_loc_descr_ref x0_ref
14230     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14231   dw_loc_descr_ref x1_ref
14232     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14233
14234   if (x0_ref == 0 || x1_ref == 0)
14235     return 0;
14236
14237   cc_loc_result = x0_ref;
14238   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14239
14240   add_loc_descr (&cc_loc_result, x1_ref);
14241   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14242
14243   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14244     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14245
14246   return cc_loc_result;
14247 }
14248
14249 /* Return a descriptor that describes the concatenation of N
14250    locations.  */
14251
14252 static dw_loc_descr_ref
14253 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14254 {
14255   unsigned int i;
14256   dw_loc_descr_ref cc_loc_result = NULL;
14257   unsigned int n = XVECLEN (concatn, 0);
14258
14259   for (i = 0; i < n; ++i)
14260     {
14261       dw_loc_descr_ref ref;
14262       rtx x = XVECEXP (concatn, 0, i);
14263
14264       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14265       if (ref == NULL)
14266         return NULL;
14267
14268       add_loc_descr (&cc_loc_result, ref);
14269       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14270     }
14271
14272   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14273     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14274
14275   return cc_loc_result;
14276 }
14277
14278 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14279    for DEBUG_IMPLICIT_PTR RTL.  */
14280
14281 static dw_loc_descr_ref
14282 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14283 {
14284   dw_loc_descr_ref ret;
14285   dw_die_ref ref;
14286
14287   if (dwarf_strict)
14288     return NULL;
14289   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14290               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14291               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14292   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14293   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14294   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14295   if (ref)
14296     {
14297       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14298       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14299       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14300     }
14301   else
14302     {
14303       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14304       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14305     }
14306   return ret;
14307 }
14308
14309 /* Output a proper Dwarf location descriptor for a variable or parameter
14310    which is either allocated in a register or in a memory location.  For a
14311    register, we just generate an OP_REG and the register number.  For a
14312    memory location we provide a Dwarf postfix expression describing how to
14313    generate the (dynamic) address of the object onto the address stack.
14314
14315    MODE is mode of the decl if this loc_descriptor is going to be used in
14316    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14317    allowed, VOIDmode otherwise.
14318
14319    If we don't know how to describe it, return 0.  */
14320
14321 static dw_loc_descr_ref
14322 loc_descriptor (rtx rtl, enum machine_mode mode,
14323                 enum var_init_status initialized)
14324 {
14325   dw_loc_descr_ref loc_result = NULL;
14326
14327   switch (GET_CODE (rtl))
14328     {
14329     case SUBREG:
14330       /* The case of a subreg may arise when we have a local (register)
14331          variable or a formal (register) parameter which doesn't quite fill
14332          up an entire register.  For now, just assume that it is
14333          legitimate to make the Dwarf info refer to the whole register which
14334          contains the given subreg.  */
14335       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14336       break;
14337
14338     case REG:
14339       loc_result = reg_loc_descriptor (rtl, initialized);
14340       break;
14341
14342     case MEM:
14343       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14344                                        initialized);
14345       if (loc_result == NULL)
14346         loc_result = tls_mem_loc_descriptor (rtl);
14347       if (loc_result == NULL)
14348         {
14349           rtx new_rtl = avoid_constant_pool_reference (rtl);
14350           if (new_rtl != rtl)
14351             loc_result = loc_descriptor (new_rtl, mode, initialized);
14352         }
14353       break;
14354
14355     case CONCAT:
14356       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14357                                           initialized);
14358       break;
14359
14360     case CONCATN:
14361       loc_result = concatn_loc_descriptor (rtl, initialized);
14362       break;
14363
14364     case VAR_LOCATION:
14365       /* Single part.  */
14366       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14367         {
14368           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14369           if (GET_CODE (loc) == EXPR_LIST)
14370             loc = XEXP (loc, 0);
14371           loc_result = loc_descriptor (loc, mode, initialized);
14372           break;
14373         }
14374
14375       rtl = XEXP (rtl, 1);
14376       /* FALLTHRU */
14377
14378     case PARALLEL:
14379       {
14380         rtvec par_elems = XVEC (rtl, 0);
14381         int num_elem = GET_NUM_ELEM (par_elems);
14382         enum machine_mode mode;
14383         int i;
14384
14385         /* Create the first one, so we have something to add to.  */
14386         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14387                                      VOIDmode, initialized);
14388         if (loc_result == NULL)
14389           return NULL;
14390         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14391         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14392         for (i = 1; i < num_elem; i++)
14393           {
14394             dw_loc_descr_ref temp;
14395
14396             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14397                                    VOIDmode, initialized);
14398             if (temp == NULL)
14399               return NULL;
14400             add_loc_descr (&loc_result, temp);
14401             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14402             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14403           }
14404       }
14405       break;
14406
14407     case CONST_INT:
14408       if (mode != VOIDmode && mode != BLKmode)
14409         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14410                                                     INTVAL (rtl));
14411       break;
14412
14413     case CONST_DOUBLE:
14414       if (mode == VOIDmode)
14415         mode = GET_MODE (rtl);
14416
14417       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14418         {
14419           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14420
14421           /* Note that a CONST_DOUBLE rtx could represent either an integer
14422              or a floating-point constant.  A CONST_DOUBLE is used whenever
14423              the constant requires more than one word in order to be
14424              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14425           loc_result = new_loc_descr (DW_OP_implicit_value,
14426                                       GET_MODE_SIZE (mode), 0);
14427           if (SCALAR_FLOAT_MODE_P (mode))
14428             {
14429               unsigned int length = GET_MODE_SIZE (mode);
14430               unsigned char *array
14431                   = (unsigned char*) ggc_alloc_atomic (length);
14432
14433               insert_float (rtl, array);
14434               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14435               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14436               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14437               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14438             }
14439           else
14440             {
14441               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14442               loc_result->dw_loc_oprnd2.v.val_double
14443                 = rtx_to_double_int (rtl);
14444             }
14445         }
14446       break;
14447
14448     case CONST_VECTOR:
14449       if (mode == VOIDmode)
14450         mode = GET_MODE (rtl);
14451
14452       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14453         {
14454           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14455           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14456           unsigned char *array = (unsigned char *)
14457             ggc_alloc_atomic (length * elt_size);
14458           unsigned int i;
14459           unsigned char *p;
14460
14461           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14462           switch (GET_MODE_CLASS (mode))
14463             {
14464             case MODE_VECTOR_INT:
14465               for (i = 0, p = array; i < length; i++, p += elt_size)
14466                 {
14467                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14468                   double_int val = rtx_to_double_int (elt);
14469
14470                   if (elt_size <= sizeof (HOST_WIDE_INT))
14471                     insert_int (double_int_to_shwi (val), elt_size, p);
14472                   else
14473                     {
14474                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14475                       insert_double (val, p);
14476                     }
14477                 }
14478               break;
14479
14480             case MODE_VECTOR_FLOAT:
14481               for (i = 0, p = array; i < length; i++, p += elt_size)
14482                 {
14483                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14484                   insert_float (elt, p);
14485                 }
14486               break;
14487
14488             default:
14489               gcc_unreachable ();
14490             }
14491
14492           loc_result = new_loc_descr (DW_OP_implicit_value,
14493                                       length * elt_size, 0);
14494           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14495           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14496           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14497           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14498         }
14499       break;
14500
14501     case CONST:
14502       if (mode == VOIDmode
14503           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14504           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14505           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14506         {
14507           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14508           break;
14509         }
14510       /* FALLTHROUGH */
14511     case SYMBOL_REF:
14512       if (!const_ok_for_output (rtl))
14513         break;
14514     case LABEL_REF:
14515       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14516           && (dwarf_version >= 4 || !dwarf_strict))
14517         {
14518           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14519           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14520           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14521           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14522           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14523         }
14524       break;
14525
14526     case DEBUG_IMPLICIT_PTR:
14527       loc_result = implicit_ptr_descriptor (rtl, 0);
14528       break;
14529
14530     case PLUS:
14531       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14532           && CONST_INT_P (XEXP (rtl, 1)))
14533         {
14534           loc_result
14535             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14536           break;
14537         }
14538       /* FALLTHRU */
14539     default:
14540       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14541           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14542           && (dwarf_version >= 4 || !dwarf_strict))
14543         {
14544           /* Value expression.  */
14545           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14546           if (loc_result)
14547             add_loc_descr (&loc_result,
14548                            new_loc_descr (DW_OP_stack_value, 0, 0));
14549         }
14550       break;
14551     }
14552
14553   return loc_result;
14554 }
14555
14556 /* We need to figure out what section we should use as the base for the
14557    address ranges where a given location is valid.
14558    1. If this particular DECL has a section associated with it, use that.
14559    2. If this function has a section associated with it, use that.
14560    3. Otherwise, use the text section.
14561    XXX: If you split a variable across multiple sections, we won't notice.  */
14562
14563 static const char *
14564 secname_for_decl (const_tree decl)
14565 {
14566   const char *secname;
14567
14568   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14569     {
14570       tree sectree = DECL_SECTION_NAME (decl);
14571       secname = TREE_STRING_POINTER (sectree);
14572     }
14573   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14574     {
14575       tree sectree = DECL_SECTION_NAME (current_function_decl);
14576       secname = TREE_STRING_POINTER (sectree);
14577     }
14578   else if (cfun && in_cold_section_p)
14579     secname = crtl->subsections.cold_section_label;
14580   else
14581     secname = text_section_label;
14582
14583   return secname;
14584 }
14585
14586 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14587
14588 static bool
14589 decl_by_reference_p (tree decl)
14590 {
14591   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14592            || TREE_CODE (decl) == VAR_DECL)
14593           && DECL_BY_REFERENCE (decl));
14594 }
14595
14596 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14597    for VARLOC.  */
14598
14599 static dw_loc_descr_ref
14600 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14601                enum var_init_status initialized)
14602 {
14603   int have_address = 0;
14604   dw_loc_descr_ref descr;
14605   enum machine_mode mode;
14606
14607   if (want_address != 2)
14608     {
14609       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14610       /* Single part.  */
14611       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14612         {
14613           varloc = PAT_VAR_LOCATION_LOC (varloc);
14614           if (GET_CODE (varloc) == EXPR_LIST)
14615             varloc = XEXP (varloc, 0);
14616           mode = GET_MODE (varloc);
14617           if (MEM_P (varloc))
14618             {
14619               rtx addr = XEXP (varloc, 0);
14620               descr = mem_loc_descriptor (addr, mode, initialized);
14621               if (descr)
14622                 have_address = 1;
14623               else
14624                 {
14625                   rtx x = avoid_constant_pool_reference (varloc);
14626                   if (x != varloc)
14627                     descr = mem_loc_descriptor (x, mode, initialized);
14628                 }
14629             }
14630           else
14631             descr = mem_loc_descriptor (varloc, mode, initialized);
14632         }
14633       else
14634         return 0;
14635     }
14636   else
14637     {
14638       if (GET_CODE (varloc) == VAR_LOCATION)
14639         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14640       else
14641         mode = DECL_MODE (loc);
14642       descr = loc_descriptor (varloc, mode, initialized);
14643       have_address = 1;
14644     }
14645
14646   if (!descr)
14647     return 0;
14648
14649   if (want_address == 2 && !have_address
14650       && (dwarf_version >= 4 || !dwarf_strict))
14651     {
14652       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14653         {
14654           expansion_failed (loc, NULL_RTX,
14655                             "DWARF address size mismatch");
14656           return 0;
14657         }
14658       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14659       have_address = 1;
14660     }
14661   /* Show if we can't fill the request for an address.  */
14662   if (want_address && !have_address)
14663     {
14664       expansion_failed (loc, NULL_RTX,
14665                         "Want address and only have value");
14666       return 0;
14667     }
14668
14669   /* If we've got an address and don't want one, dereference.  */
14670   if (!want_address && have_address)
14671     {
14672       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14673       enum dwarf_location_atom op;
14674
14675       if (size > DWARF2_ADDR_SIZE || size == -1)
14676         {
14677           expansion_failed (loc, NULL_RTX,
14678                             "DWARF address size mismatch");
14679           return 0;
14680         }
14681       else if (size == DWARF2_ADDR_SIZE)
14682         op = DW_OP_deref;
14683       else
14684         op = DW_OP_deref_size;
14685
14686       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14687     }
14688
14689   return descr;
14690 }
14691
14692 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14693    if it is not possible.  */
14694
14695 static dw_loc_descr_ref
14696 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14697 {
14698   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14699     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14700   else if (dwarf_version >= 3 || !dwarf_strict)
14701     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14702   else
14703     return NULL;
14704 }
14705
14706 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14707    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14708
14709 static dw_loc_descr_ref
14710 dw_sra_loc_expr (tree decl, rtx loc)
14711 {
14712   rtx p;
14713   unsigned int padsize = 0;
14714   dw_loc_descr_ref descr, *descr_tail;
14715   unsigned HOST_WIDE_INT decl_size;
14716   rtx varloc;
14717   enum var_init_status initialized;
14718
14719   if (DECL_SIZE (decl) == NULL
14720       || !host_integerp (DECL_SIZE (decl), 1))
14721     return NULL;
14722
14723   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14724   descr = NULL;
14725   descr_tail = &descr;
14726
14727   for (p = loc; p; p = XEXP (p, 1))
14728     {
14729       unsigned int bitsize = decl_piece_bitsize (p);
14730       rtx loc_note = *decl_piece_varloc_ptr (p);
14731       dw_loc_descr_ref cur_descr;
14732       dw_loc_descr_ref *tail, last = NULL;
14733       unsigned int opsize = 0;
14734
14735       if (loc_note == NULL_RTX
14736           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14737         {
14738           padsize += bitsize;
14739           continue;
14740         }
14741       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14742       varloc = NOTE_VAR_LOCATION (loc_note);
14743       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14744       if (cur_descr == NULL)
14745         {
14746           padsize += bitsize;
14747           continue;
14748         }
14749
14750       /* Check that cur_descr either doesn't use
14751          DW_OP_*piece operations, or their sum is equal
14752          to bitsize.  Otherwise we can't embed it.  */
14753       for (tail = &cur_descr; *tail != NULL;
14754            tail = &(*tail)->dw_loc_next)
14755         if ((*tail)->dw_loc_opc == DW_OP_piece)
14756           {
14757             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14758                       * BITS_PER_UNIT;
14759             last = *tail;
14760           }
14761         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14762           {
14763             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14764             last = *tail;
14765           }
14766
14767       if (last != NULL && opsize != bitsize)
14768         {
14769           padsize += bitsize;
14770           continue;
14771         }
14772
14773       /* If there is a hole, add DW_OP_*piece after empty DWARF
14774          expression, which means that those bits are optimized out.  */
14775       if (padsize)
14776         {
14777           if (padsize > decl_size)
14778             return NULL;
14779           decl_size -= padsize;
14780           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14781           if (*descr_tail == NULL)
14782             return NULL;
14783           descr_tail = &(*descr_tail)->dw_loc_next;
14784           padsize = 0;
14785         }
14786       *descr_tail = cur_descr;
14787       descr_tail = tail;
14788       if (bitsize > decl_size)
14789         return NULL;
14790       decl_size -= bitsize;
14791       if (last == NULL)
14792         {
14793           HOST_WIDE_INT offset = 0;
14794           if (GET_CODE (varloc) == VAR_LOCATION
14795               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14796             {
14797               varloc = PAT_VAR_LOCATION_LOC (varloc);
14798               if (GET_CODE (varloc) == EXPR_LIST)
14799                 varloc = XEXP (varloc, 0);
14800             }
14801           do 
14802             {
14803               if (GET_CODE (varloc) == CONST
14804                   || GET_CODE (varloc) == SIGN_EXTEND
14805                   || GET_CODE (varloc) == ZERO_EXTEND)
14806                 varloc = XEXP (varloc, 0);
14807               else if (GET_CODE (varloc) == SUBREG)
14808                 varloc = SUBREG_REG (varloc);
14809               else
14810                 break;
14811             }
14812           while (1);
14813           /* DW_OP_bit_size offset should be zero for register
14814              or implicit location descriptions and empty location
14815              descriptions, but for memory addresses needs big endian
14816              adjustment.  */
14817           if (MEM_P (varloc))
14818             {
14819               unsigned HOST_WIDE_INT memsize
14820                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14821               if (memsize != bitsize)
14822                 {
14823                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14824                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14825                     return NULL;
14826                   if (memsize < bitsize)
14827                     return NULL;
14828                   if (BITS_BIG_ENDIAN)
14829                     offset = memsize - bitsize;
14830                 }
14831             }
14832
14833           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14834           if (*descr_tail == NULL)
14835             return NULL;
14836           descr_tail = &(*descr_tail)->dw_loc_next;
14837         }
14838     }
14839
14840   /* If there were any non-empty expressions, add padding till the end of
14841      the decl.  */
14842   if (descr != NULL && decl_size != 0)
14843     {
14844       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14845       if (*descr_tail == NULL)
14846         return NULL;
14847     }
14848   return descr;
14849 }
14850
14851 /* Return the dwarf representation of the location list LOC_LIST of
14852    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14853    function.  */
14854
14855 static dw_loc_list_ref
14856 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14857 {
14858   const char *endname, *secname;
14859   rtx varloc;
14860   enum var_init_status initialized;
14861   struct var_loc_node *node;
14862   dw_loc_descr_ref descr;
14863   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14864   dw_loc_list_ref list = NULL;
14865   dw_loc_list_ref *listp = &list;
14866
14867   /* Now that we know what section we are using for a base,
14868      actually construct the list of locations.
14869      The first location information is what is passed to the
14870      function that creates the location list, and the remaining
14871      locations just get added on to that list.
14872      Note that we only know the start address for a location
14873      (IE location changes), so to build the range, we use
14874      the range [current location start, next location start].
14875      This means we have to special case the last node, and generate
14876      a range of [last location start, end of function label].  */
14877
14878   secname = secname_for_decl (decl);
14879
14880   for (node = loc_list->first; node; node = node->next)
14881     if (GET_CODE (node->loc) == EXPR_LIST
14882         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14883       {
14884         if (GET_CODE (node->loc) == EXPR_LIST)
14885           {
14886             /* This requires DW_OP_{,bit_}piece, which is not usable
14887                inside DWARF expressions.  */
14888             if (want_address != 2)
14889               continue;
14890             descr = dw_sra_loc_expr (decl, node->loc);
14891             if (descr == NULL)
14892               continue;
14893           }
14894         else
14895           {
14896             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14897             varloc = NOTE_VAR_LOCATION (node->loc);
14898             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14899           }
14900         if (descr)
14901           {
14902             /* The variable has a location between NODE->LABEL and
14903                NODE->NEXT->LABEL.  */
14904             if (node->next)
14905               endname = node->next->label;
14906             /* If the variable has a location at the last label
14907                it keeps its location until the end of function.  */
14908             else if (!current_function_decl)
14909               endname = text_end_label;
14910             else
14911               {
14912                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14913                                              current_function_funcdef_no);
14914                 endname = ggc_strdup (label_id);
14915               }
14916
14917             *listp = new_loc_list (descr, node->label, endname, secname);
14918             listp = &(*listp)->dw_loc_next;
14919           }
14920       }
14921
14922   /* Try to avoid the overhead of a location list emitting a location
14923      expression instead, but only if we didn't have more than one
14924      location entry in the first place.  If some entries were not
14925      representable, we don't want to pretend a single entry that was
14926      applies to the entire scope in which the variable is
14927      available.  */
14928   if (list && loc_list->first->next)
14929     gen_llsym (list);
14930
14931   return list;
14932 }
14933
14934 /* Return if the loc_list has only single element and thus can be represented
14935    as location description.   */
14936
14937 static bool
14938 single_element_loc_list_p (dw_loc_list_ref list)
14939 {
14940   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14941   return !list->ll_symbol;
14942 }
14943
14944 /* To each location in list LIST add loc descr REF.  */
14945
14946 static void
14947 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14948 {
14949   dw_loc_descr_ref copy;
14950   add_loc_descr (&list->expr, ref);
14951   list = list->dw_loc_next;
14952   while (list)
14953     {
14954       copy = ggc_alloc_dw_loc_descr_node ();
14955       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14956       add_loc_descr (&list->expr, copy);
14957       while (copy->dw_loc_next)
14958         {
14959           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14960           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14961           copy->dw_loc_next = new_copy;
14962           copy = new_copy;
14963         }
14964       list = list->dw_loc_next;
14965     }
14966 }
14967
14968 /* Given two lists RET and LIST
14969    produce location list that is result of adding expression in LIST
14970    to expression in RET on each possition in program.
14971    Might be destructive on both RET and LIST.
14972
14973    TODO: We handle only simple cases of RET or LIST having at most one
14974    element. General case would inolve sorting the lists in program order
14975    and merging them that will need some additional work.
14976    Adding that will improve quality of debug info especially for SRA-ed
14977    structures.  */
14978
14979 static void
14980 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14981 {
14982   if (!list)
14983     return;
14984   if (!*ret)
14985     {
14986       *ret = list;
14987       return;
14988     }
14989   if (!list->dw_loc_next)
14990     {
14991       add_loc_descr_to_each (*ret, list->expr);
14992       return;
14993     }
14994   if (!(*ret)->dw_loc_next)
14995     {
14996       add_loc_descr_to_each (list, (*ret)->expr);
14997       *ret = list;
14998       return;
14999     }
15000   expansion_failed (NULL_TREE, NULL_RTX,
15001                     "Don't know how to merge two non-trivial"
15002                     " location lists.\n");
15003   *ret = NULL;
15004   return;
15005 }
15006
15007 /* LOC is constant expression.  Try a luck, look it up in constant
15008    pool and return its loc_descr of its address.  */
15009
15010 static dw_loc_descr_ref
15011 cst_pool_loc_descr (tree loc)
15012 {
15013   /* Get an RTL for this, if something has been emitted.  */
15014   rtx rtl = lookup_constant_def (loc);
15015   enum machine_mode mode;
15016
15017   if (!rtl || !MEM_P (rtl))
15018     {
15019       gcc_assert (!rtl);
15020       return 0;
15021     }
15022   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15023
15024   /* TODO: We might get more coverage if we was actually delaying expansion
15025      of all expressions till end of compilation when constant pools are fully
15026      populated.  */
15027   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15028     {
15029       expansion_failed (loc, NULL_RTX,
15030                         "CST value in contant pool but not marked.");
15031       return 0;
15032     }
15033   mode = GET_MODE (rtl);
15034   rtl = XEXP (rtl, 0);
15035   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15036 }
15037
15038 /* Return dw_loc_list representing address of addr_expr LOC
15039    by looking for innder INDIRECT_REF expression and turing it
15040    into simple arithmetics.  */
15041
15042 static dw_loc_list_ref
15043 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15044 {
15045   tree obj, offset;
15046   HOST_WIDE_INT bitsize, bitpos, bytepos;
15047   enum machine_mode mode;
15048   int volatilep;
15049   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15050   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15051
15052   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15053                              &bitsize, &bitpos, &offset, &mode,
15054                              &unsignedp, &volatilep, false);
15055   STRIP_NOPS (obj);
15056   if (bitpos % BITS_PER_UNIT)
15057     {
15058       expansion_failed (loc, NULL_RTX, "bitfield access");
15059       return 0;
15060     }
15061   if (!INDIRECT_REF_P (obj))
15062     {
15063       expansion_failed (obj,
15064                         NULL_RTX, "no indirect ref in inner refrence");
15065       return 0;
15066     }
15067   if (!offset && !bitpos)
15068     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15069   else if (toplev
15070            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15071            && (dwarf_version >= 4 || !dwarf_strict))
15072     {
15073       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15074       if (!list_ret)
15075         return 0;
15076       if (offset)
15077         {
15078           /* Variable offset.  */
15079           list_ret1 = loc_list_from_tree (offset, 0);
15080           if (list_ret1 == 0)
15081             return 0;
15082           add_loc_list (&list_ret, list_ret1);
15083           if (!list_ret)
15084             return 0;
15085           add_loc_descr_to_each (list_ret,
15086                                  new_loc_descr (DW_OP_plus, 0, 0));
15087         }
15088       bytepos = bitpos / BITS_PER_UNIT;
15089       if (bytepos > 0)
15090         add_loc_descr_to_each (list_ret,
15091                                new_loc_descr (DW_OP_plus_uconst,
15092                                               bytepos, 0));
15093       else if (bytepos < 0)
15094         loc_list_plus_const (list_ret, bytepos);
15095       add_loc_descr_to_each (list_ret,
15096                              new_loc_descr (DW_OP_stack_value, 0, 0));
15097     }
15098   return list_ret;
15099 }
15100
15101
15102 /* Generate Dwarf location list representing LOC.
15103    If WANT_ADDRESS is false, expression computing LOC will be computed
15104    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15105    if WANT_ADDRESS is 2, expression computing address useable in location
15106      will be returned (i.e. DW_OP_reg can be used
15107      to refer to register values).  */
15108
15109 static dw_loc_list_ref
15110 loc_list_from_tree (tree loc, int want_address)
15111 {
15112   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15113   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15114   int have_address = 0;
15115   enum dwarf_location_atom op;
15116
15117   /* ??? Most of the time we do not take proper care for sign/zero
15118      extending the values properly.  Hopefully this won't be a real
15119      problem...  */
15120
15121   switch (TREE_CODE (loc))
15122     {
15123     case ERROR_MARK:
15124       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15125       return 0;
15126
15127     case PLACEHOLDER_EXPR:
15128       /* This case involves extracting fields from an object to determine the
15129          position of other fields.  We don't try to encode this here.  The
15130          only user of this is Ada, which encodes the needed information using
15131          the names of types.  */
15132       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15133       return 0;
15134
15135     case CALL_EXPR:
15136       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15137       /* There are no opcodes for these operations.  */
15138       return 0;
15139
15140     case PREINCREMENT_EXPR:
15141     case PREDECREMENT_EXPR:
15142     case POSTINCREMENT_EXPR:
15143     case POSTDECREMENT_EXPR:
15144       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15145       /* There are no opcodes for these operations.  */
15146       return 0;
15147
15148     case ADDR_EXPR:
15149       /* If we already want an address, see if there is INDIRECT_REF inside
15150          e.g. for &this->field.  */
15151       if (want_address)
15152         {
15153           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15154                        (loc, want_address == 2);
15155           if (list_ret)
15156             have_address = 1;
15157           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15158                    && (ret = cst_pool_loc_descr (loc)))
15159             have_address = 1;
15160         }
15161         /* Otherwise, process the argument and look for the address.  */
15162       if (!list_ret && !ret)
15163         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15164       else
15165         {
15166           if (want_address)
15167             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15168           return NULL;
15169         }
15170       break;
15171
15172     case VAR_DECL:
15173       if (DECL_THREAD_LOCAL_P (loc))
15174         {
15175           rtx rtl;
15176           enum dwarf_location_atom first_op;
15177           enum dwarf_location_atom second_op;
15178           bool dtprel = false;
15179
15180           if (targetm.have_tls)
15181             {
15182               /* If this is not defined, we have no way to emit the
15183                  data.  */
15184               if (!targetm.asm_out.output_dwarf_dtprel)
15185                 return 0;
15186
15187                /* The way DW_OP_GNU_push_tls_address is specified, we
15188                   can only look up addresses of objects in the current
15189                   module.  We used DW_OP_addr as first op, but that's
15190                   wrong, because DW_OP_addr is relocated by the debug
15191                   info consumer, while DW_OP_GNU_push_tls_address
15192                   operand shouldn't be.  */
15193               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15194                 return 0;
15195               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15196               dtprel = true;
15197               second_op = DW_OP_GNU_push_tls_address;
15198             }
15199           else
15200             {
15201               if (!targetm.emutls.debug_form_tls_address
15202                   || !(dwarf_version >= 3 || !dwarf_strict))
15203                 return 0;
15204               /* We stuffed the control variable into the DECL_VALUE_EXPR
15205                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15206                  no longer appear in gimple code.  We used the control
15207                  variable in specific so that we could pick it up here.  */
15208               loc = DECL_VALUE_EXPR (loc);
15209               first_op = DW_OP_addr;
15210               second_op = DW_OP_form_tls_address;
15211             }
15212
15213           rtl = rtl_for_decl_location (loc);
15214           if (rtl == NULL_RTX)
15215             return 0;
15216
15217           if (!MEM_P (rtl))
15218             return 0;
15219           rtl = XEXP (rtl, 0);
15220           if (! CONSTANT_P (rtl))
15221             return 0;
15222
15223           ret = new_loc_descr (first_op, 0, 0);
15224           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15225           ret->dw_loc_oprnd1.v.val_addr = rtl;
15226           ret->dtprel = dtprel;
15227
15228           ret1 = new_loc_descr (second_op, 0, 0);
15229           add_loc_descr (&ret, ret1);
15230
15231           have_address = 1;
15232           break;
15233         }
15234       /* FALLTHRU */
15235
15236     case PARM_DECL:
15237       if (DECL_HAS_VALUE_EXPR_P (loc))
15238         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15239                                    want_address);
15240       /* FALLTHRU */
15241
15242     case RESULT_DECL:
15243     case FUNCTION_DECL:
15244       {
15245         rtx rtl;
15246         var_loc_list *loc_list = lookup_decl_loc (loc);
15247
15248         if (loc_list && loc_list->first)
15249           {
15250             list_ret = dw_loc_list (loc_list, loc, want_address);
15251             have_address = want_address != 0;
15252             break;
15253           }
15254         rtl = rtl_for_decl_location (loc);
15255         if (rtl == NULL_RTX)
15256           {
15257             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15258             return 0;
15259           }
15260         else if (CONST_INT_P (rtl))
15261           {
15262             HOST_WIDE_INT val = INTVAL (rtl);
15263             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15264               val &= GET_MODE_MASK (DECL_MODE (loc));
15265             ret = int_loc_descriptor (val);
15266           }
15267         else if (GET_CODE (rtl) == CONST_STRING)
15268           {
15269             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15270             return 0;
15271           }
15272         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15273           {
15274             ret = new_loc_descr (DW_OP_addr, 0, 0);
15275             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15276             ret->dw_loc_oprnd1.v.val_addr = rtl;
15277           }
15278         else
15279           {
15280             enum machine_mode mode;
15281
15282             /* Certain constructs can only be represented at top-level.  */
15283             if (want_address == 2)
15284               {
15285                 ret = loc_descriptor (rtl, VOIDmode,
15286                                       VAR_INIT_STATUS_INITIALIZED);
15287                 have_address = 1;
15288               }
15289             else
15290               {
15291                 mode = GET_MODE (rtl);
15292                 if (MEM_P (rtl))
15293                   {
15294                     rtl = XEXP (rtl, 0);
15295                     have_address = 1;
15296                   }
15297                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15298               }
15299             if (!ret)
15300               expansion_failed (loc, rtl,
15301                                 "failed to produce loc descriptor for rtl");
15302           }
15303       }
15304       break;
15305
15306     case MEM_REF:
15307       /* ??? FIXME.  */
15308       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15309         return 0;
15310       /* Fallthru.  */
15311     case INDIRECT_REF:
15312       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15313       have_address = 1;
15314       break;
15315
15316     case COMPOUND_EXPR:
15317       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15318
15319     CASE_CONVERT:
15320     case VIEW_CONVERT_EXPR:
15321     case SAVE_EXPR:
15322     case MODIFY_EXPR:
15323       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15324
15325     case COMPONENT_REF:
15326     case BIT_FIELD_REF:
15327     case ARRAY_REF:
15328     case ARRAY_RANGE_REF:
15329     case REALPART_EXPR:
15330     case IMAGPART_EXPR:
15331       {
15332         tree obj, offset;
15333         HOST_WIDE_INT bitsize, bitpos, bytepos;
15334         enum machine_mode mode;
15335         int volatilep;
15336         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15337
15338         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15339                                    &unsignedp, &volatilep, false);
15340
15341         gcc_assert (obj != loc);
15342
15343         list_ret = loc_list_from_tree (obj,
15344                                        want_address == 2
15345                                        && !bitpos && !offset ? 2 : 1);
15346         /* TODO: We can extract value of the small expression via shifting even
15347            for nonzero bitpos.  */
15348         if (list_ret == 0)
15349           return 0;
15350         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15351           {
15352             expansion_failed (loc, NULL_RTX,
15353                               "bitfield access");
15354             return 0;
15355           }
15356
15357         if (offset != NULL_TREE)
15358           {
15359             /* Variable offset.  */
15360             list_ret1 = loc_list_from_tree (offset, 0);
15361             if (list_ret1 == 0)
15362               return 0;
15363             add_loc_list (&list_ret, list_ret1);
15364             if (!list_ret)
15365               return 0;
15366             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15367           }
15368
15369         bytepos = bitpos / BITS_PER_UNIT;
15370         if (bytepos > 0)
15371           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15372         else if (bytepos < 0)
15373           loc_list_plus_const (list_ret, bytepos);
15374
15375         have_address = 1;
15376         break;
15377       }
15378
15379     case INTEGER_CST:
15380       if ((want_address || !host_integerp (loc, 0))
15381           && (ret = cst_pool_loc_descr (loc)))
15382         have_address = 1;
15383       else if (want_address == 2
15384                && host_integerp (loc, 0)
15385                && (ret = address_of_int_loc_descriptor
15386                            (int_size_in_bytes (TREE_TYPE (loc)),
15387                             tree_low_cst (loc, 0))))
15388         have_address = 1;
15389       else if (host_integerp (loc, 0))
15390         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15391       else
15392         {
15393           expansion_failed (loc, NULL_RTX,
15394                             "Integer operand is not host integer");
15395           return 0;
15396         }
15397       break;
15398
15399     case CONSTRUCTOR:
15400     case REAL_CST:
15401     case STRING_CST:
15402     case COMPLEX_CST:
15403       if ((ret = cst_pool_loc_descr (loc)))
15404         have_address = 1;
15405       else
15406       /* We can construct small constants here using int_loc_descriptor.  */
15407         expansion_failed (loc, NULL_RTX,
15408                           "constructor or constant not in constant pool");
15409       break;
15410
15411     case TRUTH_AND_EXPR:
15412     case TRUTH_ANDIF_EXPR:
15413     case BIT_AND_EXPR:
15414       op = DW_OP_and;
15415       goto do_binop;
15416
15417     case TRUTH_XOR_EXPR:
15418     case BIT_XOR_EXPR:
15419       op = DW_OP_xor;
15420       goto do_binop;
15421
15422     case TRUTH_OR_EXPR:
15423     case TRUTH_ORIF_EXPR:
15424     case BIT_IOR_EXPR:
15425       op = DW_OP_or;
15426       goto do_binop;
15427
15428     case FLOOR_DIV_EXPR:
15429     case CEIL_DIV_EXPR:
15430     case ROUND_DIV_EXPR:
15431     case TRUNC_DIV_EXPR:
15432       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15433         return 0;
15434       op = DW_OP_div;
15435       goto do_binop;
15436
15437     case MINUS_EXPR:
15438       op = DW_OP_minus;
15439       goto do_binop;
15440
15441     case FLOOR_MOD_EXPR:
15442     case CEIL_MOD_EXPR:
15443     case ROUND_MOD_EXPR:
15444     case TRUNC_MOD_EXPR:
15445       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15446         {
15447           op = DW_OP_mod;
15448           goto do_binop;
15449         }
15450       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15451       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15452       if (list_ret == 0 || list_ret1 == 0)
15453         return 0;
15454
15455       add_loc_list (&list_ret, list_ret1);
15456       if (list_ret == 0)
15457         return 0;
15458       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15459       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15460       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15461       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15462       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15463       break;
15464
15465     case MULT_EXPR:
15466       op = DW_OP_mul;
15467       goto do_binop;
15468
15469     case LSHIFT_EXPR:
15470       op = DW_OP_shl;
15471       goto do_binop;
15472
15473     case RSHIFT_EXPR:
15474       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15475       goto do_binop;
15476
15477     case POINTER_PLUS_EXPR:
15478     case PLUS_EXPR:
15479       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15480         {
15481           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15482           if (list_ret == 0)
15483             return 0;
15484
15485           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15486           break;
15487         }
15488
15489       op = DW_OP_plus;
15490       goto do_binop;
15491
15492     case LE_EXPR:
15493       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15494         return 0;
15495
15496       op = DW_OP_le;
15497       goto do_binop;
15498
15499     case GE_EXPR:
15500       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15501         return 0;
15502
15503       op = DW_OP_ge;
15504       goto do_binop;
15505
15506     case LT_EXPR:
15507       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15508         return 0;
15509
15510       op = DW_OP_lt;
15511       goto do_binop;
15512
15513     case GT_EXPR:
15514       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15515         return 0;
15516
15517       op = DW_OP_gt;
15518       goto do_binop;
15519
15520     case EQ_EXPR:
15521       op = DW_OP_eq;
15522       goto do_binop;
15523
15524     case NE_EXPR:
15525       op = DW_OP_ne;
15526       goto do_binop;
15527
15528     do_binop:
15529       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15530       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15531       if (list_ret == 0 || list_ret1 == 0)
15532         return 0;
15533
15534       add_loc_list (&list_ret, list_ret1);
15535       if (list_ret == 0)
15536         return 0;
15537       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15538       break;
15539
15540     case TRUTH_NOT_EXPR:
15541     case BIT_NOT_EXPR:
15542       op = DW_OP_not;
15543       goto do_unop;
15544
15545     case ABS_EXPR:
15546       op = DW_OP_abs;
15547       goto do_unop;
15548
15549     case NEGATE_EXPR:
15550       op = DW_OP_neg;
15551       goto do_unop;
15552
15553     do_unop:
15554       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15555       if (list_ret == 0)
15556         return 0;
15557
15558       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15559       break;
15560
15561     case MIN_EXPR:
15562     case MAX_EXPR:
15563       {
15564         const enum tree_code code =
15565           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15566
15567         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15568                       build2 (code, integer_type_node,
15569                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15570                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15571       }
15572
15573       /* ... fall through ...  */
15574
15575     case COND_EXPR:
15576       {
15577         dw_loc_descr_ref lhs
15578           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15579         dw_loc_list_ref rhs
15580           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15581         dw_loc_descr_ref bra_node, jump_node, tmp;
15582
15583         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15584         if (list_ret == 0 || lhs == 0 || rhs == 0)
15585           return 0;
15586
15587         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15588         add_loc_descr_to_each (list_ret, bra_node);
15589
15590         add_loc_list (&list_ret, rhs);
15591         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15592         add_loc_descr_to_each (list_ret, jump_node);
15593
15594         add_loc_descr_to_each (list_ret, lhs);
15595         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15596         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15597
15598         /* ??? Need a node to point the skip at.  Use a nop.  */
15599         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15600         add_loc_descr_to_each (list_ret, tmp);
15601         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15602         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15603       }
15604       break;
15605
15606     case FIX_TRUNC_EXPR:
15607       return 0;
15608
15609     default:
15610       /* Leave front-end specific codes as simply unknown.  This comes
15611          up, for instance, with the C STMT_EXPR.  */
15612       if ((unsigned int) TREE_CODE (loc)
15613           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15614         {
15615           expansion_failed (loc, NULL_RTX,
15616                             "language specific tree node");
15617           return 0;
15618         }
15619
15620 #ifdef ENABLE_CHECKING
15621       /* Otherwise this is a generic code; we should just lists all of
15622          these explicitly.  We forgot one.  */
15623       gcc_unreachable ();
15624 #else
15625       /* In a release build, we want to degrade gracefully: better to
15626          generate incomplete debugging information than to crash.  */
15627       return NULL;
15628 #endif
15629     }
15630
15631   if (!ret && !list_ret)
15632     return 0;
15633
15634   if (want_address == 2 && !have_address
15635       && (dwarf_version >= 4 || !dwarf_strict))
15636     {
15637       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15638         {
15639           expansion_failed (loc, NULL_RTX,
15640                             "DWARF address size mismatch");
15641           return 0;
15642         }
15643       if (ret)
15644         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15645       else
15646         add_loc_descr_to_each (list_ret,
15647                                new_loc_descr (DW_OP_stack_value, 0, 0));
15648       have_address = 1;
15649     }
15650   /* Show if we can't fill the request for an address.  */
15651   if (want_address && !have_address)
15652     {
15653       expansion_failed (loc, NULL_RTX,
15654                         "Want address and only have value");
15655       return 0;
15656     }
15657
15658   gcc_assert (!ret || !list_ret);
15659
15660   /* If we've got an address and don't want one, dereference.  */
15661   if (!want_address && have_address)
15662     {
15663       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15664
15665       if (size > DWARF2_ADDR_SIZE || size == -1)
15666         {
15667           expansion_failed (loc, NULL_RTX,
15668                             "DWARF address size mismatch");
15669           return 0;
15670         }
15671       else if (size == DWARF2_ADDR_SIZE)
15672         op = DW_OP_deref;
15673       else
15674         op = DW_OP_deref_size;
15675
15676       if (ret)
15677         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15678       else
15679         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15680     }
15681   if (ret)
15682     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15683
15684   return list_ret;
15685 }
15686
15687 /* Same as above but return only single location expression.  */
15688 static dw_loc_descr_ref
15689 loc_descriptor_from_tree (tree loc, int want_address)
15690 {
15691   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15692   if (!ret)
15693     return NULL;
15694   if (ret->dw_loc_next)
15695     {
15696       expansion_failed (loc, NULL_RTX,
15697                         "Location list where only loc descriptor needed");
15698       return NULL;
15699     }
15700   return ret->expr;
15701 }
15702
15703 /* Given a value, round it up to the lowest multiple of `boundary'
15704    which is not less than the value itself.  */
15705
15706 static inline HOST_WIDE_INT
15707 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15708 {
15709   return (((value + boundary - 1) / boundary) * boundary);
15710 }
15711
15712 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15713    pointer to the declared type for the relevant field variable, or return
15714    `integer_type_node' if the given node turns out to be an
15715    ERROR_MARK node.  */
15716
15717 static inline tree
15718 field_type (const_tree decl)
15719 {
15720   tree type;
15721
15722   if (TREE_CODE (decl) == ERROR_MARK)
15723     return integer_type_node;
15724
15725   type = DECL_BIT_FIELD_TYPE (decl);
15726   if (type == NULL_TREE)
15727     type = TREE_TYPE (decl);
15728
15729   return type;
15730 }
15731
15732 /* Given a pointer to a tree node, return the alignment in bits for
15733    it, or else return BITS_PER_WORD if the node actually turns out to
15734    be an ERROR_MARK node.  */
15735
15736 static inline unsigned
15737 simple_type_align_in_bits (const_tree type)
15738 {
15739   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15740 }
15741
15742 static inline unsigned
15743 simple_decl_align_in_bits (const_tree decl)
15744 {
15745   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15746 }
15747
15748 /* Return the result of rounding T up to ALIGN.  */
15749
15750 static inline double_int
15751 round_up_to_align (double_int t, unsigned int align)
15752 {
15753   double_int alignd = uhwi_to_double_int (align);
15754   t = double_int_add (t, alignd);
15755   t = double_int_add (t, double_int_minus_one);
15756   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15757   t = double_int_mul (t, alignd);
15758   return t;
15759 }
15760
15761 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15762    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15763    or return 0 if we are unable to determine what that offset is, either
15764    because the argument turns out to be a pointer to an ERROR_MARK node, or
15765    because the offset is actually variable.  (We can't handle the latter case
15766    just yet).  */
15767
15768 static HOST_WIDE_INT
15769 field_byte_offset (const_tree decl)
15770 {
15771   double_int object_offset_in_bits;
15772   double_int object_offset_in_bytes;
15773   double_int bitpos_int;
15774
15775   if (TREE_CODE (decl) == ERROR_MARK)
15776     return 0;
15777
15778   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15779
15780   /* We cannot yet cope with fields whose positions are variable, so
15781      for now, when we see such things, we simply return 0.  Someday, we may
15782      be able to handle such cases, but it will be damn difficult.  */
15783   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15784     return 0;
15785
15786   bitpos_int = tree_to_double_int (bit_position (decl));
15787
15788 #ifdef PCC_BITFIELD_TYPE_MATTERS
15789   if (PCC_BITFIELD_TYPE_MATTERS)
15790     {
15791       tree type;
15792       tree field_size_tree;
15793       double_int deepest_bitpos;
15794       double_int field_size_in_bits;
15795       unsigned int type_align_in_bits;
15796       unsigned int decl_align_in_bits;
15797       double_int type_size_in_bits;
15798
15799       type = field_type (decl);
15800       type_size_in_bits = double_int_type_size_in_bits (type);
15801       type_align_in_bits = simple_type_align_in_bits (type);
15802
15803       field_size_tree = DECL_SIZE (decl);
15804
15805       /* The size could be unspecified if there was an error, or for
15806          a flexible array member.  */
15807       if (!field_size_tree)
15808         field_size_tree = bitsize_zero_node;
15809
15810       /* If the size of the field is not constant, use the type size.  */
15811       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15812         field_size_in_bits = tree_to_double_int (field_size_tree);
15813       else
15814         field_size_in_bits = type_size_in_bits;
15815
15816       decl_align_in_bits = simple_decl_align_in_bits (decl);
15817
15818       /* The GCC front-end doesn't make any attempt to keep track of the
15819          starting bit offset (relative to the start of the containing
15820          structure type) of the hypothetical "containing object" for a
15821          bit-field.  Thus, when computing the byte offset value for the
15822          start of the "containing object" of a bit-field, we must deduce
15823          this information on our own. This can be rather tricky to do in
15824          some cases.  For example, handling the following structure type
15825          definition when compiling for an i386/i486 target (which only
15826          aligns long long's to 32-bit boundaries) can be very tricky:
15827
15828          struct S { int field1; long long field2:31; };
15829
15830          Fortunately, there is a simple rule-of-thumb which can be used
15831          in such cases.  When compiling for an i386/i486, GCC will
15832          allocate 8 bytes for the structure shown above.  It decides to
15833          do this based upon one simple rule for bit-field allocation.
15834          GCC allocates each "containing object" for each bit-field at
15835          the first (i.e. lowest addressed) legitimate alignment boundary
15836          (based upon the required minimum alignment for the declared
15837          type of the field) which it can possibly use, subject to the
15838          condition that there is still enough available space remaining
15839          in the containing object (when allocated at the selected point)
15840          to fully accommodate all of the bits of the bit-field itself.
15841
15842          This simple rule makes it obvious why GCC allocates 8 bytes for
15843          each object of the structure type shown above.  When looking
15844          for a place to allocate the "containing object" for `field2',
15845          the compiler simply tries to allocate a 64-bit "containing
15846          object" at each successive 32-bit boundary (starting at zero)
15847          until it finds a place to allocate that 64- bit field such that
15848          at least 31 contiguous (and previously unallocated) bits remain
15849          within that selected 64 bit field.  (As it turns out, for the
15850          example above, the compiler finds it is OK to allocate the
15851          "containing object" 64-bit field at bit-offset zero within the
15852          structure type.)
15853
15854          Here we attempt to work backwards from the limited set of facts
15855          we're given, and we try to deduce from those facts, where GCC
15856          must have believed that the containing object started (within
15857          the structure type). The value we deduce is then used (by the
15858          callers of this routine) to generate DW_AT_location and
15859          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15860          the case of DW_AT_location, regular fields as well).  */
15861
15862       /* Figure out the bit-distance from the start of the structure to
15863          the "deepest" bit of the bit-field.  */
15864       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15865
15866       /* This is the tricky part.  Use some fancy footwork to deduce
15867          where the lowest addressed bit of the containing object must
15868          be.  */
15869       object_offset_in_bits
15870         = double_int_sub (deepest_bitpos, type_size_in_bits);
15871
15872       /* Round up to type_align by default.  This works best for
15873          bitfields.  */
15874       object_offset_in_bits
15875         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15876
15877       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15878         {
15879           object_offset_in_bits
15880             = double_int_sub (deepest_bitpos, type_size_in_bits);
15881
15882           /* Round up to decl_align instead.  */
15883           object_offset_in_bits
15884             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15885         }
15886     }
15887   else
15888 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15889     object_offset_in_bits = bitpos_int;
15890
15891   object_offset_in_bytes
15892     = double_int_div (object_offset_in_bits,
15893                       uhwi_to_double_int (BITS_PER_UNIT), true,
15894                       TRUNC_DIV_EXPR);
15895   return double_int_to_shwi (object_offset_in_bytes);
15896 }
15897 \f
15898 /* The following routines define various Dwarf attributes and any data
15899    associated with them.  */
15900
15901 /* Add a location description attribute value to a DIE.
15902
15903    This emits location attributes suitable for whole variables and
15904    whole parameters.  Note that the location attributes for struct fields are
15905    generated by the routine `data_member_location_attribute' below.  */
15906
15907 static inline void
15908 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15909                              dw_loc_list_ref descr)
15910 {
15911   if (descr == 0)
15912     return;
15913   if (single_element_loc_list_p (descr))
15914     add_AT_loc (die, attr_kind, descr->expr);
15915   else
15916     add_AT_loc_list (die, attr_kind, descr);
15917 }
15918
15919 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15920
15921 static void
15922 add_accessibility_attribute (dw_die_ref die, tree decl)
15923 {
15924   if (TREE_PROTECTED (decl))
15925     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15926   else if (TREE_PRIVATE (decl))
15927     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15928 }
15929
15930 /* Attach the specialized form of location attribute used for data members of
15931    struct and union types.  In the special case of a FIELD_DECL node which
15932    represents a bit-field, the "offset" part of this special location
15933    descriptor must indicate the distance in bytes from the lowest-addressed
15934    byte of the containing struct or union type to the lowest-addressed byte of
15935    the "containing object" for the bit-field.  (See the `field_byte_offset'
15936    function above).
15937
15938    For any given bit-field, the "containing object" is a hypothetical object
15939    (of some integral or enum type) within which the given bit-field lives.  The
15940    type of this hypothetical "containing object" is always the same as the
15941    declared type of the individual bit-field itself (for GCC anyway... the
15942    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15943    bytes) of the hypothetical "containing object" which will be given in the
15944    DW_AT_byte_size attribute for this bit-field.  (See the
15945    `byte_size_attribute' function below.)  It is also used when calculating the
15946    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15947    function below.)  */
15948
15949 static void
15950 add_data_member_location_attribute (dw_die_ref die, tree decl)
15951 {
15952   HOST_WIDE_INT offset;
15953   dw_loc_descr_ref loc_descr = 0;
15954
15955   if (TREE_CODE (decl) == TREE_BINFO)
15956     {
15957       /* We're working on the TAG_inheritance for a base class.  */
15958       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15959         {
15960           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15961              aren't at a fixed offset from all (sub)objects of the same
15962              type.  We need to extract the appropriate offset from our
15963              vtable.  The following dwarf expression means
15964
15965                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15966
15967              This is specific to the V3 ABI, of course.  */
15968
15969           dw_loc_descr_ref tmp;
15970
15971           /* Make a copy of the object address.  */
15972           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15973           add_loc_descr (&loc_descr, tmp);
15974
15975           /* Extract the vtable address.  */
15976           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15977           add_loc_descr (&loc_descr, tmp);
15978
15979           /* Calculate the address of the offset.  */
15980           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15981           gcc_assert (offset < 0);
15982
15983           tmp = int_loc_descriptor (-offset);
15984           add_loc_descr (&loc_descr, tmp);
15985           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15986           add_loc_descr (&loc_descr, tmp);
15987
15988           /* Extract the offset.  */
15989           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15990           add_loc_descr (&loc_descr, tmp);
15991
15992           /* Add it to the object address.  */
15993           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15994           add_loc_descr (&loc_descr, tmp);
15995         }
15996       else
15997         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15998     }
15999   else
16000     offset = field_byte_offset (decl);
16001
16002   if (! loc_descr)
16003     {
16004       if (dwarf_version > 2)
16005         {
16006           /* Don't need to output a location expression, just the constant. */
16007           if (offset < 0)
16008             add_AT_int (die, DW_AT_data_member_location, offset);
16009           else
16010             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16011           return;
16012         }
16013       else
16014         {
16015           enum dwarf_location_atom op;
16016
16017           /* The DWARF2 standard says that we should assume that the structure
16018              address is already on the stack, so we can specify a structure
16019              field address by using DW_OP_plus_uconst.  */
16020
16021 #ifdef MIPS_DEBUGGING_INFO
16022           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16023              operator correctly.  It works only if we leave the offset on the
16024              stack.  */
16025           op = DW_OP_constu;
16026 #else
16027           op = DW_OP_plus_uconst;
16028 #endif
16029
16030           loc_descr = new_loc_descr (op, offset, 0);
16031         }
16032     }
16033
16034   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16035 }
16036
16037 /* Writes integer values to dw_vec_const array.  */
16038
16039 static void
16040 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16041 {
16042   while (size != 0)
16043     {
16044       *dest++ = val & 0xff;
16045       val >>= 8;
16046       --size;
16047     }
16048 }
16049
16050 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16051
16052 static HOST_WIDE_INT
16053 extract_int (const unsigned char *src, unsigned int size)
16054 {
16055   HOST_WIDE_INT val = 0;
16056
16057   src += size;
16058   while (size != 0)
16059     {
16060       val <<= 8;
16061       val |= *--src & 0xff;
16062       --size;
16063     }
16064   return val;
16065 }
16066
16067 /* Writes double_int values to dw_vec_const array.  */
16068
16069 static void
16070 insert_double (double_int val, unsigned char *dest)
16071 {
16072   unsigned char *p0 = dest;
16073   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16074
16075   if (WORDS_BIG_ENDIAN)
16076     {
16077       p0 = p1;
16078       p1 = dest;
16079     }
16080
16081   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16082   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16083 }
16084
16085 /* Writes floating point values to dw_vec_const array.  */
16086
16087 static void
16088 insert_float (const_rtx rtl, unsigned char *array)
16089 {
16090   REAL_VALUE_TYPE rv;
16091   long val[4];
16092   int i;
16093
16094   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16095   real_to_target (val, &rv, GET_MODE (rtl));
16096
16097   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16098   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16099     {
16100       insert_int (val[i], 4, array);
16101       array += 4;
16102     }
16103 }
16104
16105 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16106    does not have a "location" either in memory or in a register.  These
16107    things can arise in GNU C when a constant is passed as an actual parameter
16108    to an inlined function.  They can also arise in C++ where declared
16109    constants do not necessarily get memory "homes".  */
16110
16111 static bool
16112 add_const_value_attribute (dw_die_ref die, rtx rtl)
16113 {
16114   switch (GET_CODE (rtl))
16115     {
16116     case CONST_INT:
16117       {
16118         HOST_WIDE_INT val = INTVAL (rtl);
16119
16120         if (val < 0)
16121           add_AT_int (die, DW_AT_const_value, val);
16122         else
16123           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16124       }
16125       return true;
16126
16127     case CONST_DOUBLE:
16128       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16129          floating-point constant.  A CONST_DOUBLE is used whenever the
16130          constant requires more than one word in order to be adequately
16131          represented.  */
16132       {
16133         enum machine_mode mode = GET_MODE (rtl);
16134
16135         if (SCALAR_FLOAT_MODE_P (mode))
16136           {
16137             unsigned int length = GET_MODE_SIZE (mode);
16138             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16139
16140             insert_float (rtl, array);
16141             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16142           }
16143         else
16144           add_AT_double (die, DW_AT_const_value,
16145                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16146       }
16147       return true;
16148
16149     case CONST_VECTOR:
16150       {
16151         enum machine_mode mode = GET_MODE (rtl);
16152         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16153         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16154         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16155           (length * elt_size);
16156         unsigned int i;
16157         unsigned char *p;
16158
16159         switch (GET_MODE_CLASS (mode))
16160           {
16161           case MODE_VECTOR_INT:
16162             for (i = 0, p = array; i < length; i++, p += elt_size)
16163               {
16164                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16165                 double_int val = rtx_to_double_int (elt);
16166
16167                 if (elt_size <= sizeof (HOST_WIDE_INT))
16168                   insert_int (double_int_to_shwi (val), elt_size, p);
16169                 else
16170                   {
16171                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16172                     insert_double (val, p);
16173                   }
16174               }
16175             break;
16176
16177           case MODE_VECTOR_FLOAT:
16178             for (i = 0, p = array; i < length; i++, p += elt_size)
16179               {
16180                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16181                 insert_float (elt, p);
16182               }
16183             break;
16184
16185           default:
16186             gcc_unreachable ();
16187           }
16188
16189         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16190       }
16191       return true;
16192
16193     case CONST_STRING:
16194       if (dwarf_version >= 4 || !dwarf_strict)
16195         {
16196           dw_loc_descr_ref loc_result;
16197           resolve_one_addr (&rtl, NULL);
16198         rtl_addr:
16199           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16200           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16201           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16202           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16203           add_AT_loc (die, DW_AT_location, loc_result);
16204           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16205           return true;
16206         }
16207       return false;
16208
16209     case CONST:
16210       if (CONSTANT_P (XEXP (rtl, 0)))
16211         return add_const_value_attribute (die, XEXP (rtl, 0));
16212       /* FALLTHROUGH */
16213     case SYMBOL_REF:
16214       if (!const_ok_for_output (rtl))
16215         return false;
16216     case LABEL_REF:
16217       if (dwarf_version >= 4 || !dwarf_strict)
16218         goto rtl_addr;
16219       return false;
16220
16221     case PLUS:
16222       /* In cases where an inlined instance of an inline function is passed
16223          the address of an `auto' variable (which is local to the caller) we
16224          can get a situation where the DECL_RTL of the artificial local
16225          variable (for the inlining) which acts as a stand-in for the
16226          corresponding formal parameter (of the inline function) will look
16227          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16228          exactly a compile-time constant expression, but it isn't the address
16229          of the (artificial) local variable either.  Rather, it represents the
16230          *value* which the artificial local variable always has during its
16231          lifetime.  We currently have no way to represent such quasi-constant
16232          values in Dwarf, so for now we just punt and generate nothing.  */
16233       return false;
16234
16235     case HIGH:
16236     case CONST_FIXED:
16237       return false;
16238
16239     case MEM:
16240       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16241           && MEM_READONLY_P (rtl)
16242           && GET_MODE (rtl) == BLKmode)
16243         {
16244           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16245           return true;
16246         }
16247       return false;
16248
16249     default:
16250       /* No other kinds of rtx should be possible here.  */
16251       gcc_unreachable ();
16252     }
16253   return false;
16254 }
16255
16256 /* Determine whether the evaluation of EXPR references any variables
16257    or functions which aren't otherwise used (and therefore may not be
16258    output).  */
16259 static tree
16260 reference_to_unused (tree * tp, int * walk_subtrees,
16261                      void * data ATTRIBUTE_UNUSED)
16262 {
16263   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16264     *walk_subtrees = 0;
16265
16266   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16267       && ! TREE_ASM_WRITTEN (*tp))
16268     return *tp;
16269   /* ???  The C++ FE emits debug information for using decls, so
16270      putting gcc_unreachable here falls over.  See PR31899.  For now
16271      be conservative.  */
16272   else if (!cgraph_global_info_ready
16273            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16274     return *tp;
16275   else if (TREE_CODE (*tp) == VAR_DECL)
16276     {
16277       struct varpool_node *node = varpool_get_node (*tp);
16278       if (!node || !node->needed)
16279         return *tp;
16280     }
16281   else if (TREE_CODE (*tp) == FUNCTION_DECL
16282            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16283     {
16284       /* The call graph machinery must have finished analyzing,
16285          optimizing and gimplifying the CU by now.
16286          So if *TP has no call graph node associated
16287          to it, it means *TP will not be emitted.  */
16288       if (!cgraph_get_node (*tp))
16289         return *tp;
16290     }
16291   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16292     return *tp;
16293
16294   return NULL_TREE;
16295 }
16296
16297 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16298    for use in a later add_const_value_attribute call.  */
16299
16300 static rtx
16301 rtl_for_decl_init (tree init, tree type)
16302 {
16303   rtx rtl = NULL_RTX;
16304
16305   /* If a variable is initialized with a string constant without embedded
16306      zeros, build CONST_STRING.  */
16307   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16308     {
16309       tree enttype = TREE_TYPE (type);
16310       tree domain = TYPE_DOMAIN (type);
16311       enum machine_mode mode = TYPE_MODE (enttype);
16312
16313       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16314           && domain
16315           && integer_zerop (TYPE_MIN_VALUE (domain))
16316           && compare_tree_int (TYPE_MAX_VALUE (domain),
16317                                TREE_STRING_LENGTH (init) - 1) == 0
16318           && ((size_t) TREE_STRING_LENGTH (init)
16319               == strlen (TREE_STRING_POINTER (init)) + 1))
16320         {
16321           rtl = gen_rtx_CONST_STRING (VOIDmode,
16322                                       ggc_strdup (TREE_STRING_POINTER (init)));
16323           rtl = gen_rtx_MEM (BLKmode, rtl);
16324           MEM_READONLY_P (rtl) = 1;
16325         }
16326     }
16327   /* Other aggregates, and complex values, could be represented using
16328      CONCAT: FIXME!  */
16329   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16330     ;
16331   /* Vectors only work if their mode is supported by the target.
16332      FIXME: generic vectors ought to work too.  */
16333   else if (TREE_CODE (type) == VECTOR_TYPE
16334            && !VECTOR_MODE_P (TYPE_MODE (type)))
16335     ;
16336   /* If the initializer is something that we know will expand into an
16337      immediate RTL constant, expand it now.  We must be careful not to
16338      reference variables which won't be output.  */
16339   else if (initializer_constant_valid_p (init, type)
16340            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16341     {
16342       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16343          possible.  */
16344       if (TREE_CODE (type) == VECTOR_TYPE)
16345         switch (TREE_CODE (init))
16346           {
16347           case VECTOR_CST:
16348             break;
16349           case CONSTRUCTOR:
16350             if (TREE_CONSTANT (init))
16351               {
16352                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16353                 bool constant_p = true;
16354                 tree value;
16355                 unsigned HOST_WIDE_INT ix;
16356
16357                 /* Even when ctor is constant, it might contain non-*_CST
16358                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16359                    belong into VECTOR_CST nodes.  */
16360                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16361                   if (!CONSTANT_CLASS_P (value))
16362                     {
16363                       constant_p = false;
16364                       break;
16365                     }
16366
16367                 if (constant_p)
16368                   {
16369                     init = build_vector_from_ctor (type, elts);
16370                     break;
16371                   }
16372               }
16373             /* FALLTHRU */
16374
16375           default:
16376             return NULL;
16377           }
16378
16379       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16380
16381       /* If expand_expr returns a MEM, it wasn't immediate.  */
16382       gcc_assert (!rtl || !MEM_P (rtl));
16383     }
16384
16385   return rtl;
16386 }
16387
16388 /* Generate RTL for the variable DECL to represent its location.  */
16389
16390 static rtx
16391 rtl_for_decl_location (tree decl)
16392 {
16393   rtx rtl;
16394
16395   /* Here we have to decide where we are going to say the parameter "lives"
16396      (as far as the debugger is concerned).  We only have a couple of
16397      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16398
16399      DECL_RTL normally indicates where the parameter lives during most of the
16400      activation of the function.  If optimization is enabled however, this
16401      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16402      that the parameter doesn't really live anywhere (as far as the code
16403      generation parts of GCC are concerned) during most of the function's
16404      activation.  That will happen (for example) if the parameter is never
16405      referenced within the function.
16406
16407      We could just generate a location descriptor here for all non-NULL
16408      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16409      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16410      where DECL_RTL is NULL or is a pseudo-reg.
16411
16412      Note however that we can only get away with using DECL_INCOMING_RTL as
16413      a backup substitute for DECL_RTL in certain limited cases.  In cases
16414      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16415      we can be sure that the parameter was passed using the same type as it is
16416      declared to have within the function, and that its DECL_INCOMING_RTL
16417      points us to a place where a value of that type is passed.
16418
16419      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16420      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16421      because in these cases DECL_INCOMING_RTL points us to a value of some
16422      type which is *different* from the type of the parameter itself.  Thus,
16423      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16424      such cases, the debugger would end up (for example) trying to fetch a
16425      `float' from a place which actually contains the first part of a
16426      `double'.  That would lead to really incorrect and confusing
16427      output at debug-time.
16428
16429      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16430      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16431      are a couple of exceptions however.  On little-endian machines we can
16432      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16433      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16434      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16435      when (on a little-endian machine) a non-prototyped function has a
16436      parameter declared to be of type `short' or `char'.  In such cases,
16437      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16438      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16439      passed `int' value.  If the debugger then uses that address to fetch
16440      a `short' or a `char' (on a little-endian machine) the result will be
16441      the correct data, so we allow for such exceptional cases below.
16442
16443      Note that our goal here is to describe the place where the given formal
16444      parameter lives during most of the function's activation (i.e. between the
16445      end of the prologue and the start of the epilogue).  We'll do that as best
16446      as we can. Note however that if the given formal parameter is modified
16447      sometime during the execution of the function, then a stack backtrace (at
16448      debug-time) will show the function as having been called with the *new*
16449      value rather than the value which was originally passed in.  This happens
16450      rarely enough that it is not a major problem, but it *is* a problem, and
16451      I'd like to fix it.
16452
16453      A future version of dwarf2out.c may generate two additional attributes for
16454      any given DW_TAG_formal_parameter DIE which will describe the "passed
16455      type" and the "passed location" for the given formal parameter in addition
16456      to the attributes we now generate to indicate the "declared type" and the
16457      "active location" for each parameter.  This additional set of attributes
16458      could be used by debuggers for stack backtraces. Separately, note that
16459      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16460      This happens (for example) for inlined-instances of inline function formal
16461      parameters which are never referenced.  This really shouldn't be
16462      happening.  All PARM_DECL nodes should get valid non-NULL
16463      DECL_INCOMING_RTL values.  FIXME.  */
16464
16465   /* Use DECL_RTL as the "location" unless we find something better.  */
16466   rtl = DECL_RTL_IF_SET (decl);
16467
16468   /* When generating abstract instances, ignore everything except
16469      constants, symbols living in memory, and symbols living in
16470      fixed registers.  */
16471   if (! reload_completed)
16472     {
16473       if (rtl
16474           && (CONSTANT_P (rtl)
16475               || (MEM_P (rtl)
16476                   && CONSTANT_P (XEXP (rtl, 0)))
16477               || (REG_P (rtl)
16478                   && TREE_CODE (decl) == VAR_DECL
16479                   && TREE_STATIC (decl))))
16480         {
16481           rtl = targetm.delegitimize_address (rtl);
16482           return rtl;
16483         }
16484       rtl = NULL_RTX;
16485     }
16486   else if (TREE_CODE (decl) == PARM_DECL)
16487     {
16488       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16489         {
16490           tree declared_type = TREE_TYPE (decl);
16491           tree passed_type = DECL_ARG_TYPE (decl);
16492           enum machine_mode dmode = TYPE_MODE (declared_type);
16493           enum machine_mode pmode = TYPE_MODE (passed_type);
16494
16495           /* This decl represents a formal parameter which was optimized out.
16496              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16497              all cases where (rtl == NULL_RTX) just below.  */
16498           if (dmode == pmode)
16499             rtl = DECL_INCOMING_RTL (decl);
16500           else if (SCALAR_INT_MODE_P (dmode)
16501                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16502                    && DECL_INCOMING_RTL (decl))
16503             {
16504               rtx inc = DECL_INCOMING_RTL (decl);
16505               if (REG_P (inc))
16506                 rtl = inc;
16507               else if (MEM_P (inc))
16508                 {
16509                   if (BYTES_BIG_ENDIAN)
16510                     rtl = adjust_address_nv (inc, dmode,
16511                                              GET_MODE_SIZE (pmode)
16512                                              - GET_MODE_SIZE (dmode));
16513                   else
16514                     rtl = inc;
16515                 }
16516             }
16517         }
16518
16519       /* If the parm was passed in registers, but lives on the stack, then
16520          make a big endian correction if the mode of the type of the
16521          parameter is not the same as the mode of the rtl.  */
16522       /* ??? This is the same series of checks that are made in dbxout.c before
16523          we reach the big endian correction code there.  It isn't clear if all
16524          of these checks are necessary here, but keeping them all is the safe
16525          thing to do.  */
16526       else if (MEM_P (rtl)
16527                && XEXP (rtl, 0) != const0_rtx
16528                && ! CONSTANT_P (XEXP (rtl, 0))
16529                /* Not passed in memory.  */
16530                && !MEM_P (DECL_INCOMING_RTL (decl))
16531                /* Not passed by invisible reference.  */
16532                && (!REG_P (XEXP (rtl, 0))
16533                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16534                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16535 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16536                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16537 #endif
16538                      )
16539                /* Big endian correction check.  */
16540                && BYTES_BIG_ENDIAN
16541                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16542                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16543                    < UNITS_PER_WORD))
16544         {
16545           int offset = (UNITS_PER_WORD
16546                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16547
16548           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16549                              plus_constant (XEXP (rtl, 0), offset));
16550         }
16551     }
16552   else if (TREE_CODE (decl) == VAR_DECL
16553            && rtl
16554            && MEM_P (rtl)
16555            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16556            && BYTES_BIG_ENDIAN)
16557     {
16558       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16559       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16560
16561       /* If a variable is declared "register" yet is smaller than
16562          a register, then if we store the variable to memory, it
16563          looks like we're storing a register-sized value, when in
16564          fact we are not.  We need to adjust the offset of the
16565          storage location to reflect the actual value's bytes,
16566          else gdb will not be able to display it.  */
16567       if (rsize > dsize)
16568         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16569                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16570     }
16571
16572   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16573      and will have been substituted directly into all expressions that use it.
16574      C does not have such a concept, but C++ and other languages do.  */
16575   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16576     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16577
16578   if (rtl)
16579     rtl = targetm.delegitimize_address (rtl);
16580
16581   /* If we don't look past the constant pool, we risk emitting a
16582      reference to a constant pool entry that isn't referenced from
16583      code, and thus is not emitted.  */
16584   if (rtl)
16585     rtl = avoid_constant_pool_reference (rtl);
16586
16587   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16588      in the current CU, resolve_addr will remove the expression referencing
16589      it.  */
16590   if (rtl == NULL_RTX
16591       && TREE_CODE (decl) == VAR_DECL
16592       && !DECL_EXTERNAL (decl)
16593       && TREE_STATIC (decl)
16594       && DECL_NAME (decl)
16595       && !DECL_HARD_REGISTER (decl)
16596       && DECL_MODE (decl) != VOIDmode)
16597     {
16598       rtl = make_decl_rtl_for_debug (decl);
16599       if (!MEM_P (rtl)
16600           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16601           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16602         rtl = NULL_RTX;
16603     }
16604
16605   return rtl;
16606 }
16607
16608 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16609    returned.  If so, the decl for the COMMON block is returned, and the
16610    value is the offset into the common block for the symbol.  */
16611
16612 static tree
16613 fortran_common (tree decl, HOST_WIDE_INT *value)
16614 {
16615   tree val_expr, cvar;
16616   enum machine_mode mode;
16617   HOST_WIDE_INT bitsize, bitpos;
16618   tree offset;
16619   int volatilep = 0, unsignedp = 0;
16620
16621   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16622      it does not have a value (the offset into the common area), or if it
16623      is thread local (as opposed to global) then it isn't common, and shouldn't
16624      be handled as such.  */
16625   if (TREE_CODE (decl) != VAR_DECL
16626       || !TREE_STATIC (decl)
16627       || !DECL_HAS_VALUE_EXPR_P (decl)
16628       || !is_fortran ())
16629     return NULL_TREE;
16630
16631   val_expr = DECL_VALUE_EXPR (decl);
16632   if (TREE_CODE (val_expr) != COMPONENT_REF)
16633     return NULL_TREE;
16634
16635   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16636                               &mode, &unsignedp, &volatilep, true);
16637
16638   if (cvar == NULL_TREE
16639       || TREE_CODE (cvar) != VAR_DECL
16640       || DECL_ARTIFICIAL (cvar)
16641       || !TREE_PUBLIC (cvar))
16642     return NULL_TREE;
16643
16644   *value = 0;
16645   if (offset != NULL)
16646     {
16647       if (!host_integerp (offset, 0))
16648         return NULL_TREE;
16649       *value = tree_low_cst (offset, 0);
16650     }
16651   if (bitpos != 0)
16652     *value += bitpos / BITS_PER_UNIT;
16653
16654   return cvar;
16655 }
16656
16657 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16658    data attribute for a variable or a parameter.  We generate the
16659    DW_AT_const_value attribute only in those cases where the given variable
16660    or parameter does not have a true "location" either in memory or in a
16661    register.  This can happen (for example) when a constant is passed as an
16662    actual argument in a call to an inline function.  (It's possible that
16663    these things can crop up in other ways also.)  Note that one type of
16664    constant value which can be passed into an inlined function is a constant
16665    pointer.  This can happen for example if an actual argument in an inlined
16666    function call evaluates to a compile-time constant address.  */
16667
16668 static bool
16669 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16670                                        enum dwarf_attribute attr)
16671 {
16672   rtx rtl;
16673   dw_loc_list_ref list;
16674   var_loc_list *loc_list;
16675
16676   if (TREE_CODE (decl) == ERROR_MARK)
16677     return false;
16678
16679   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16680               || TREE_CODE (decl) == RESULT_DECL);
16681
16682   /* Try to get some constant RTL for this decl, and use that as the value of
16683      the location.  */
16684
16685   rtl = rtl_for_decl_location (decl);
16686   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16687       && add_const_value_attribute (die, rtl))
16688     return true;
16689
16690   /* See if we have single element location list that is equivalent to
16691      a constant value.  That way we are better to use add_const_value_attribute
16692      rather than expanding constant value equivalent.  */
16693   loc_list = lookup_decl_loc (decl);
16694   if (loc_list
16695       && loc_list->first
16696       && loc_list->first->next == NULL
16697       && NOTE_P (loc_list->first->loc)
16698       && NOTE_VAR_LOCATION (loc_list->first->loc)
16699       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16700     {
16701       struct var_loc_node *node;
16702
16703       node = loc_list->first;
16704       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16705       if (GET_CODE (rtl) == EXPR_LIST)
16706         rtl = XEXP (rtl, 0);
16707       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16708           && add_const_value_attribute (die, rtl))
16709          return true;
16710     }
16711   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16712   if (list)
16713     {
16714       add_AT_location_description (die, attr, list);
16715       return true;
16716     }
16717   /* None of that worked, so it must not really have a location;
16718      try adding a constant value attribute from the DECL_INITIAL.  */
16719   return tree_add_const_value_attribute_for_decl (die, decl);
16720 }
16721
16722 /* Add VARIABLE and DIE into deferred locations list.  */
16723
16724 static void
16725 defer_location (tree variable, dw_die_ref die)
16726 {
16727   deferred_locations entry;
16728   entry.variable = variable;
16729   entry.die = die;
16730   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16731 }
16732
16733 /* Helper function for tree_add_const_value_attribute.  Natively encode
16734    initializer INIT into an array.  Return true if successful.  */
16735
16736 static bool
16737 native_encode_initializer (tree init, unsigned char *array, int size)
16738 {
16739   tree type;
16740
16741   if (init == NULL_TREE)
16742     return false;
16743
16744   STRIP_NOPS (init);
16745   switch (TREE_CODE (init))
16746     {
16747     case STRING_CST:
16748       type = TREE_TYPE (init);
16749       if (TREE_CODE (type) == ARRAY_TYPE)
16750         {
16751           tree enttype = TREE_TYPE (type);
16752           enum machine_mode mode = TYPE_MODE (enttype);
16753
16754           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16755             return false;
16756           if (int_size_in_bytes (type) != size)
16757             return false;
16758           if (size > TREE_STRING_LENGTH (init))
16759             {
16760               memcpy (array, TREE_STRING_POINTER (init),
16761                       TREE_STRING_LENGTH (init));
16762               memset (array + TREE_STRING_LENGTH (init),
16763                       '\0', size - TREE_STRING_LENGTH (init));
16764             }
16765           else
16766             memcpy (array, TREE_STRING_POINTER (init), size);
16767           return true;
16768         }
16769       return false;
16770     case CONSTRUCTOR:
16771       type = TREE_TYPE (init);
16772       if (int_size_in_bytes (type) != size)
16773         return false;
16774       if (TREE_CODE (type) == ARRAY_TYPE)
16775         {
16776           HOST_WIDE_INT min_index;
16777           unsigned HOST_WIDE_INT cnt;
16778           int curpos = 0, fieldsize;
16779           constructor_elt *ce;
16780
16781           if (TYPE_DOMAIN (type) == NULL_TREE
16782               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16783             return false;
16784
16785           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16786           if (fieldsize <= 0)
16787             return false;
16788
16789           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16790           memset (array, '\0', size);
16791           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16792             {
16793               tree val = ce->value;
16794               tree index = ce->index;
16795               int pos = curpos;
16796               if (index && TREE_CODE (index) == RANGE_EXPR)
16797                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16798                       * fieldsize;
16799               else if (index)
16800                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16801
16802               if (val)
16803                 {
16804                   STRIP_NOPS (val);
16805                   if (!native_encode_initializer (val, array + pos, fieldsize))
16806                     return false;
16807                 }
16808               curpos = pos + fieldsize;
16809               if (index && TREE_CODE (index) == RANGE_EXPR)
16810                 {
16811                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16812                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16813                   while (count > 0)
16814                     {
16815                       if (val)
16816                         memcpy (array + curpos, array + pos, fieldsize);
16817                       curpos += fieldsize;
16818                     }
16819                 }
16820               gcc_assert (curpos <= size);
16821             }
16822           return true;
16823         }
16824       else if (TREE_CODE (type) == RECORD_TYPE
16825                || TREE_CODE (type) == UNION_TYPE)
16826         {
16827           tree field = NULL_TREE;
16828           unsigned HOST_WIDE_INT cnt;
16829           constructor_elt *ce;
16830
16831           if (int_size_in_bytes (type) != size)
16832             return false;
16833
16834           if (TREE_CODE (type) == RECORD_TYPE)
16835             field = TYPE_FIELDS (type);
16836
16837           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16838             {
16839               tree val = ce->value;
16840               int pos, fieldsize;
16841
16842               if (ce->index != 0)
16843                 field = ce->index;
16844
16845               if (val)
16846                 STRIP_NOPS (val);
16847
16848               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16849                 return false;
16850
16851               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16852                   && TYPE_DOMAIN (TREE_TYPE (field))
16853                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16854                 return false;
16855               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16856                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16857                 return false;
16858               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16859               pos = int_byte_position (field);
16860               gcc_assert (pos + fieldsize <= size);
16861               if (val
16862                   && !native_encode_initializer (val, array + pos, fieldsize))
16863                 return false;
16864             }
16865           return true;
16866         }
16867       return false;
16868     case VIEW_CONVERT_EXPR:
16869     case NON_LVALUE_EXPR:
16870       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16871     default:
16872       return native_encode_expr (init, array, size) == size;
16873     }
16874 }
16875
16876 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16877    attribute is the const value T.  */
16878
16879 static bool
16880 tree_add_const_value_attribute (dw_die_ref die, tree t)
16881 {
16882   tree init;
16883   tree type = TREE_TYPE (t);
16884   rtx rtl;
16885
16886   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16887     return false;
16888
16889   init = t;
16890   gcc_assert (!DECL_P (init));
16891
16892   rtl = rtl_for_decl_init (init, type);
16893   if (rtl)
16894     return add_const_value_attribute (die, rtl);
16895   /* If the host and target are sane, try harder.  */
16896   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16897            && initializer_constant_valid_p (init, type))
16898     {
16899       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16900       if (size > 0 && (int) size == size)
16901         {
16902           unsigned char *array = (unsigned char *)
16903             ggc_alloc_cleared_atomic (size);
16904
16905           if (native_encode_initializer (init, array, size))
16906             {
16907               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16908               return true;
16909             }
16910         }
16911     }
16912   return false;
16913 }
16914
16915 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16916    attribute is the const value of T, where T is an integral constant
16917    variable with static storage duration
16918    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16919
16920 static bool
16921 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16922 {
16923
16924   if (!decl
16925       || (TREE_CODE (decl) != VAR_DECL
16926           && TREE_CODE (decl) != CONST_DECL))
16927     return false;
16928
16929     if (TREE_READONLY (decl)
16930         && ! TREE_THIS_VOLATILE (decl)
16931         && DECL_INITIAL (decl))
16932       /* OK */;
16933     else
16934       return false;
16935
16936   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16937   if (get_AT (var_die, DW_AT_const_value))
16938     return false;
16939
16940   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16941 }
16942
16943 /* Convert the CFI instructions for the current function into a
16944    location list.  This is used for DW_AT_frame_base when we targeting
16945    a dwarf2 consumer that does not support the dwarf3
16946    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16947    expressions.  */
16948
16949 static dw_loc_list_ref
16950 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16951 {
16952   dw_fde_ref fde;
16953   dw_loc_list_ref list, *list_tail;
16954   dw_cfi_ref cfi;
16955   dw_cfa_location last_cfa, next_cfa;
16956   const char *start_label, *last_label, *section;
16957   dw_cfa_location remember;
16958
16959   fde = current_fde ();
16960   gcc_assert (fde != NULL);
16961
16962   section = secname_for_decl (current_function_decl);
16963   list_tail = &list;
16964   list = NULL;
16965
16966   memset (&next_cfa, 0, sizeof (next_cfa));
16967   next_cfa.reg = INVALID_REGNUM;
16968   remember = next_cfa;
16969
16970   start_label = fde->dw_fde_begin;
16971
16972   /* ??? Bald assumption that the CIE opcode list does not contain
16973      advance opcodes.  */
16974   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16975     lookup_cfa_1 (cfi, &next_cfa, &remember);
16976
16977   last_cfa = next_cfa;
16978   last_label = start_label;
16979
16980   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16981     switch (cfi->dw_cfi_opc)
16982       {
16983       case DW_CFA_set_loc:
16984       case DW_CFA_advance_loc1:
16985       case DW_CFA_advance_loc2:
16986       case DW_CFA_advance_loc4:
16987         if (!cfa_equal_p (&last_cfa, &next_cfa))
16988           {
16989             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16990                                        start_label, last_label, section);
16991
16992             list_tail = &(*list_tail)->dw_loc_next;
16993             last_cfa = next_cfa;
16994             start_label = last_label;
16995           }
16996         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16997         break;
16998
16999       case DW_CFA_advance_loc:
17000         /* The encoding is complex enough that we should never emit this.  */
17001         gcc_unreachable ();
17002
17003       default:
17004         lookup_cfa_1 (cfi, &next_cfa, &remember);
17005         break;
17006       }
17007
17008   if (!cfa_equal_p (&last_cfa, &next_cfa))
17009     {
17010       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17011                                  start_label, last_label, section);
17012       list_tail = &(*list_tail)->dw_loc_next;
17013       start_label = last_label;
17014     }
17015
17016   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17017                              start_label, fde->dw_fde_end, section);
17018
17019   if (list && list->dw_loc_next)
17020     gen_llsym (list);
17021
17022   return list;
17023 }
17024
17025 /* Compute a displacement from the "steady-state frame pointer" to the
17026    frame base (often the same as the CFA), and store it in
17027    frame_pointer_fb_offset.  OFFSET is added to the displacement
17028    before the latter is negated.  */
17029
17030 static void
17031 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17032 {
17033   rtx reg, elim;
17034
17035 #ifdef FRAME_POINTER_CFA_OFFSET
17036   reg = frame_pointer_rtx;
17037   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17038 #else
17039   reg = arg_pointer_rtx;
17040   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17041 #endif
17042
17043   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17044   if (GET_CODE (elim) == PLUS)
17045     {
17046       offset += INTVAL (XEXP (elim, 1));
17047       elim = XEXP (elim, 0);
17048     }
17049
17050   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17051                && (elim == hard_frame_pointer_rtx
17052                    || elim == stack_pointer_rtx))
17053               || elim == (frame_pointer_needed
17054                           ? hard_frame_pointer_rtx
17055                           : stack_pointer_rtx));
17056
17057   frame_pointer_fb_offset = -offset;
17058 }
17059
17060 /* Generate a DW_AT_name attribute given some string value to be included as
17061    the value of the attribute.  */
17062
17063 static void
17064 add_name_attribute (dw_die_ref die, const char *name_string)
17065 {
17066   if (name_string != NULL && *name_string != 0)
17067     {
17068       if (demangle_name_func)
17069         name_string = (*demangle_name_func) (name_string);
17070
17071       add_AT_string (die, DW_AT_name, name_string);
17072     }
17073 }
17074
17075 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17076
17077 static void
17078 add_comp_dir_attribute (dw_die_ref die)
17079 {
17080   const char *wd = get_src_pwd ();
17081   char *wd1;
17082
17083   if (wd == NULL)
17084     return;
17085
17086   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17087     {
17088       int wdlen;
17089
17090       wdlen = strlen (wd);
17091       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17092       strcpy (wd1, wd);
17093       wd1 [wdlen] = DIR_SEPARATOR;
17094       wd1 [wdlen + 1] = 0;
17095       wd = wd1;
17096     }
17097
17098     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17099 }
17100
17101 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17102    default.  */
17103
17104 static int
17105 lower_bound_default (void)
17106 {
17107   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17108     {
17109     case DW_LANG_C:
17110     case DW_LANG_C89:
17111     case DW_LANG_C99:
17112     case DW_LANG_C_plus_plus:
17113     case DW_LANG_ObjC:
17114     case DW_LANG_ObjC_plus_plus:
17115     case DW_LANG_Java:
17116       return 0;
17117     case DW_LANG_Fortran77:
17118     case DW_LANG_Fortran90:
17119     case DW_LANG_Fortran95:
17120       return 1;
17121     case DW_LANG_UPC:
17122     case DW_LANG_D:
17123     case DW_LANG_Python:
17124       return dwarf_version >= 4 ? 0 : -1;
17125     case DW_LANG_Ada95:
17126     case DW_LANG_Ada83:
17127     case DW_LANG_Cobol74:
17128     case DW_LANG_Cobol85:
17129     case DW_LANG_Pascal83:
17130     case DW_LANG_Modula2:
17131     case DW_LANG_PLI:
17132       return dwarf_version >= 4 ? 1 : -1;
17133     default:
17134       return -1;
17135     }
17136 }
17137
17138 /* Given a tree node describing an array bound (either lower or upper) output
17139    a representation for that bound.  */
17140
17141 static void
17142 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17143 {
17144   switch (TREE_CODE (bound))
17145     {
17146     case ERROR_MARK:
17147       return;
17148
17149     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17150     case INTEGER_CST:
17151       {
17152         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17153         int dflt;
17154
17155         /* Use the default if possible.  */
17156         if (bound_attr == DW_AT_lower_bound
17157             && host_integerp (bound, 0)
17158             && (dflt = lower_bound_default ()) != -1
17159             && tree_low_cst (bound, 0) == dflt)
17160           ;
17161
17162         /* Otherwise represent the bound as an unsigned value with the
17163            precision of its type.  The precision and signedness of the
17164            type will be necessary to re-interpret it unambiguously.  */
17165         else if (prec < HOST_BITS_PER_WIDE_INT)
17166           {
17167             unsigned HOST_WIDE_INT mask
17168               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17169             add_AT_unsigned (subrange_die, bound_attr,
17170                              TREE_INT_CST_LOW (bound) & mask);
17171           }
17172         else if (prec == HOST_BITS_PER_WIDE_INT
17173                  || TREE_INT_CST_HIGH (bound) == 0)
17174           add_AT_unsigned (subrange_die, bound_attr,
17175                            TREE_INT_CST_LOW (bound));
17176         else
17177           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17178                          TREE_INT_CST_LOW (bound));
17179       }
17180       break;
17181
17182     CASE_CONVERT:
17183     case VIEW_CONVERT_EXPR:
17184       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17185       break;
17186
17187     case SAVE_EXPR:
17188       break;
17189
17190     case VAR_DECL:
17191     case PARM_DECL:
17192     case RESULT_DECL:
17193       {
17194         dw_die_ref decl_die = lookup_decl_die (bound);
17195
17196         /* ??? Can this happen, or should the variable have been bound
17197            first?  Probably it can, since I imagine that we try to create
17198            the types of parameters in the order in which they exist in
17199            the list, and won't have created a forward reference to a
17200            later parameter.  */
17201         if (decl_die != NULL)
17202           {
17203             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17204             break;
17205           }
17206       }
17207       /* FALLTHRU */
17208
17209     default:
17210       {
17211         /* Otherwise try to create a stack operation procedure to
17212            evaluate the value of the array bound.  */
17213
17214         dw_die_ref ctx, decl_die;
17215         dw_loc_list_ref list;
17216
17217         list = loc_list_from_tree (bound, 2);
17218         if (list == NULL || single_element_loc_list_p (list))
17219           {
17220             /* If DW_AT_*bound is not a reference nor constant, it is
17221                a DWARF expression rather than location description.
17222                For that loc_list_from_tree (bound, 0) is needed.
17223                If that fails to give a single element list,
17224                fall back to outputting this as a reference anyway.  */
17225             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17226             if (list2 && single_element_loc_list_p (list2))
17227               {
17228                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17229                 break;
17230               }
17231           }
17232         if (list == NULL)
17233           break;
17234
17235         if (current_function_decl == 0)
17236           ctx = comp_unit_die;
17237         else
17238           ctx = lookup_decl_die (current_function_decl);
17239
17240         decl_die = new_die (DW_TAG_variable, ctx, bound);
17241         add_AT_flag (decl_die, DW_AT_artificial, 1);
17242         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17243         add_AT_location_description (decl_die, DW_AT_location, list);
17244         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17245         break;
17246       }
17247     }
17248 }
17249
17250 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17251    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17252    Note that the block of subscript information for an array type also
17253    includes information about the element type of the given array type.  */
17254
17255 static void
17256 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17257 {
17258   unsigned dimension_number;
17259   tree lower, upper;
17260   dw_die_ref subrange_die;
17261
17262   for (dimension_number = 0;
17263        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17264        type = TREE_TYPE (type), dimension_number++)
17265     {
17266       tree domain = TYPE_DOMAIN (type);
17267
17268       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17269         break;
17270
17271       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17272          and (in GNU C only) variable bounds.  Handle all three forms
17273          here.  */
17274       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17275       if (domain)
17276         {
17277           /* We have an array type with specified bounds.  */
17278           lower = TYPE_MIN_VALUE (domain);
17279           upper = TYPE_MAX_VALUE (domain);
17280
17281           /* Define the index type.  */
17282           if (TREE_TYPE (domain))
17283             {
17284               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17285                  TREE_TYPE field.  We can't emit debug info for this
17286                  because it is an unnamed integral type.  */
17287               if (TREE_CODE (domain) == INTEGER_TYPE
17288                   && TYPE_NAME (domain) == NULL_TREE
17289                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17290                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17291                 ;
17292               else
17293                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17294                                     type_die);
17295             }
17296
17297           /* ??? If upper is NULL, the array has unspecified length,
17298              but it does have a lower bound.  This happens with Fortran
17299                dimension arr(N:*)
17300              Since the debugger is definitely going to need to know N
17301              to produce useful results, go ahead and output the lower
17302              bound solo, and hope the debugger can cope.  */
17303
17304           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17305           if (upper)
17306             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17307         }
17308
17309       /* Otherwise we have an array type with an unspecified length.  The
17310          DWARF-2 spec does not say how to handle this; let's just leave out the
17311          bounds.  */
17312     }
17313 }
17314
17315 static void
17316 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17317 {
17318   unsigned size;
17319
17320   switch (TREE_CODE (tree_node))
17321     {
17322     case ERROR_MARK:
17323       size = 0;
17324       break;
17325     case ENUMERAL_TYPE:
17326     case RECORD_TYPE:
17327     case UNION_TYPE:
17328     case QUAL_UNION_TYPE:
17329       size = int_size_in_bytes (tree_node);
17330       break;
17331     case FIELD_DECL:
17332       /* For a data member of a struct or union, the DW_AT_byte_size is
17333          generally given as the number of bytes normally allocated for an
17334          object of the *declared* type of the member itself.  This is true
17335          even for bit-fields.  */
17336       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17337       break;
17338     default:
17339       gcc_unreachable ();
17340     }
17341
17342   /* Note that `size' might be -1 when we get to this point.  If it is, that
17343      indicates that the byte size of the entity in question is variable.  We
17344      have no good way of expressing this fact in Dwarf at the present time,
17345      so just let the -1 pass on through.  */
17346   add_AT_unsigned (die, DW_AT_byte_size, size);
17347 }
17348
17349 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17350    which specifies the distance in bits from the highest order bit of the
17351    "containing object" for the bit-field to the highest order bit of the
17352    bit-field itself.
17353
17354    For any given bit-field, the "containing object" is a hypothetical object
17355    (of some integral or enum type) within which the given bit-field lives.  The
17356    type of this hypothetical "containing object" is always the same as the
17357    declared type of the individual bit-field itself.  The determination of the
17358    exact location of the "containing object" for a bit-field is rather
17359    complicated.  It's handled by the `field_byte_offset' function (above).
17360
17361    Note that it is the size (in bytes) of the hypothetical "containing object"
17362    which will be given in the DW_AT_byte_size attribute for this bit-field.
17363    (See `byte_size_attribute' above).  */
17364
17365 static inline void
17366 add_bit_offset_attribute (dw_die_ref die, tree decl)
17367 {
17368   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17369   tree type = DECL_BIT_FIELD_TYPE (decl);
17370   HOST_WIDE_INT bitpos_int;
17371   HOST_WIDE_INT highest_order_object_bit_offset;
17372   HOST_WIDE_INT highest_order_field_bit_offset;
17373   HOST_WIDE_INT unsigned bit_offset;
17374
17375   /* Must be a field and a bit field.  */
17376   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17377
17378   /* We can't yet handle bit-fields whose offsets are variable, so if we
17379      encounter such things, just return without generating any attribute
17380      whatsoever.  Likewise for variable or too large size.  */
17381   if (! host_integerp (bit_position (decl), 0)
17382       || ! host_integerp (DECL_SIZE (decl), 1))
17383     return;
17384
17385   bitpos_int = int_bit_position (decl);
17386
17387   /* Note that the bit offset is always the distance (in bits) from the
17388      highest-order bit of the "containing object" to the highest-order bit of
17389      the bit-field itself.  Since the "high-order end" of any object or field
17390      is different on big-endian and little-endian machines, the computation
17391      below must take account of these differences.  */
17392   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17393   highest_order_field_bit_offset = bitpos_int;
17394
17395   if (! BYTES_BIG_ENDIAN)
17396     {
17397       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17398       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17399     }
17400
17401   bit_offset
17402     = (! BYTES_BIG_ENDIAN
17403        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17404        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17405
17406   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17407 }
17408
17409 /* For a FIELD_DECL node which represents a bit field, output an attribute
17410    which specifies the length in bits of the given field.  */
17411
17412 static inline void
17413 add_bit_size_attribute (dw_die_ref die, tree decl)
17414 {
17415   /* Must be a field and a bit field.  */
17416   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17417               && DECL_BIT_FIELD_TYPE (decl));
17418
17419   if (host_integerp (DECL_SIZE (decl), 1))
17420     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17421 }
17422
17423 /* If the compiled language is ANSI C, then add a 'prototyped'
17424    attribute, if arg types are given for the parameters of a function.  */
17425
17426 static inline void
17427 add_prototyped_attribute (dw_die_ref die, tree func_type)
17428 {
17429   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17430       && TYPE_ARG_TYPES (func_type) != NULL)
17431     add_AT_flag (die, DW_AT_prototyped, 1);
17432 }
17433
17434 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17435    by looking in either the type declaration or object declaration
17436    equate table.  */
17437
17438 static inline dw_die_ref
17439 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17440 {
17441   dw_die_ref origin_die = NULL;
17442
17443   if (TREE_CODE (origin) != FUNCTION_DECL)
17444     {
17445       /* We may have gotten separated from the block for the inlined
17446          function, if we're in an exception handler or some such; make
17447          sure that the abstract function has been written out.
17448
17449          Doing this for nested functions is wrong, however; functions are
17450          distinct units, and our context might not even be inline.  */
17451       tree fn = origin;
17452
17453       if (TYPE_P (fn))
17454         fn = TYPE_STUB_DECL (fn);
17455
17456       fn = decl_function_context (fn);
17457       if (fn)
17458         dwarf2out_abstract_function (fn);
17459     }
17460
17461   if (DECL_P (origin))
17462     origin_die = lookup_decl_die (origin);
17463   else if (TYPE_P (origin))
17464     origin_die = lookup_type_die (origin);
17465
17466   /* XXX: Functions that are never lowered don't always have correct block
17467      trees (in the case of java, they simply have no block tree, in some other
17468      languages).  For these functions, there is nothing we can really do to
17469      output correct debug info for inlined functions in all cases.  Rather
17470      than die, we'll just produce deficient debug info now, in that we will
17471      have variables without a proper abstract origin.  In the future, when all
17472      functions are lowered, we should re-add a gcc_assert (origin_die)
17473      here.  */
17474
17475   if (origin_die)
17476     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17477   return origin_die;
17478 }
17479
17480 /* We do not currently support the pure_virtual attribute.  */
17481
17482 static inline void
17483 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17484 {
17485   if (DECL_VINDEX (func_decl))
17486     {
17487       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17488
17489       if (host_integerp (DECL_VINDEX (func_decl), 0))
17490         add_AT_loc (die, DW_AT_vtable_elem_location,
17491                     new_loc_descr (DW_OP_constu,
17492                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17493                                    0));
17494
17495       /* GNU extension: Record what type this method came from originally.  */
17496       if (debug_info_level > DINFO_LEVEL_TERSE
17497           && DECL_CONTEXT (func_decl))
17498         add_AT_die_ref (die, DW_AT_containing_type,
17499                         lookup_type_die (DECL_CONTEXT (func_decl)));
17500     }
17501 }
17502 \f
17503 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17504    given decl.  This used to be a vendor extension until after DWARF 4
17505    standardized it.  */
17506
17507 static void
17508 add_linkage_attr (dw_die_ref die, tree decl)
17509 {
17510   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17511
17512   /* Mimic what assemble_name_raw does with a leading '*'.  */
17513   if (name[0] == '*')
17514     name = &name[1];
17515
17516   if (dwarf_version >= 4)
17517     add_AT_string (die, DW_AT_linkage_name, name);
17518   else
17519     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17520 }
17521
17522 /* Add source coordinate attributes for the given decl.  */
17523
17524 static void
17525 add_src_coords_attributes (dw_die_ref die, tree decl)
17526 {
17527   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17528
17529   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17530   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17531 }
17532
17533 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17534
17535 static void
17536 add_linkage_name (dw_die_ref die, tree decl)
17537 {
17538   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17539        && TREE_PUBLIC (decl)
17540        && !DECL_ABSTRACT (decl)
17541        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17542        && die->die_tag != DW_TAG_member)
17543     {
17544       /* Defer until we have an assembler name set.  */
17545       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17546         {
17547           limbo_die_node *asm_name;
17548
17549           asm_name = ggc_alloc_cleared_limbo_die_node ();
17550           asm_name->die = die;
17551           asm_name->created_for = decl;
17552           asm_name->next = deferred_asm_name;
17553           deferred_asm_name = asm_name;
17554         }
17555       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17556         add_linkage_attr (die, decl);
17557     }
17558 }
17559
17560 /* Add a DW_AT_name attribute and source coordinate attribute for the
17561    given decl, but only if it actually has a name.  */
17562
17563 static void
17564 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17565 {
17566   tree decl_name;
17567
17568   decl_name = DECL_NAME (decl);
17569   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17570     {
17571       const char *name = dwarf2_name (decl, 0);
17572       if (name)
17573         add_name_attribute (die, name);
17574       if (! DECL_ARTIFICIAL (decl))
17575         add_src_coords_attributes (die, decl);
17576
17577       add_linkage_name (die, decl);
17578     }
17579
17580 #ifdef VMS_DEBUGGING_INFO
17581   /* Get the function's name, as described by its RTL.  This may be different
17582      from the DECL_NAME name used in the source file.  */
17583   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17584     {
17585       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17586                    XEXP (DECL_RTL (decl), 0));
17587       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17588     }
17589 #endif /* VMS_DEBUGGING_INFO */
17590 }
17591
17592 #ifdef VMS_DEBUGGING_INFO
17593 /* Output the debug main pointer die for VMS */
17594
17595 void
17596 dwarf2out_vms_debug_main_pointer (void)
17597 {
17598   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17599   dw_die_ref die;
17600
17601   /* Allocate the VMS debug main subprogram die.  */
17602   die = ggc_alloc_cleared_die_node ();
17603   die->die_tag = DW_TAG_subprogram;
17604   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17605   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17606                                current_function_funcdef_no);
17607   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17608
17609   /* Make it the first child of comp_unit_die.  */
17610   die->die_parent = comp_unit_die;
17611   if (comp_unit_die->die_child)
17612     {
17613       die->die_sib = comp_unit_die->die_child->die_sib;
17614       comp_unit_die->die_child->die_sib = die;
17615     }
17616   else
17617     {
17618       die->die_sib = die;
17619       comp_unit_die->die_child = die;
17620     }
17621 }
17622 #endif /* VMS_DEBUGGING_INFO */
17623
17624 /* Push a new declaration scope.  */
17625
17626 static void
17627 push_decl_scope (tree scope)
17628 {
17629   VEC_safe_push (tree, gc, decl_scope_table, scope);
17630 }
17631
17632 /* Pop a declaration scope.  */
17633
17634 static inline void
17635 pop_decl_scope (void)
17636 {
17637   VEC_pop (tree, decl_scope_table);
17638 }
17639
17640 /* Return the DIE for the scope that immediately contains this type.
17641    Non-named types get global scope.  Named types nested in other
17642    types get their containing scope if it's open, or global scope
17643    otherwise.  All other types (i.e. function-local named types) get
17644    the current active scope.  */
17645
17646 static dw_die_ref
17647 scope_die_for (tree t, dw_die_ref context_die)
17648 {
17649   dw_die_ref scope_die = NULL;
17650   tree containing_scope;
17651   int i;
17652
17653   /* Non-types always go in the current scope.  */
17654   gcc_assert (TYPE_P (t));
17655
17656   containing_scope = TYPE_CONTEXT (t);
17657
17658   /* Use the containing namespace if it was passed in (for a declaration).  */
17659   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17660     {
17661       if (context_die == lookup_decl_die (containing_scope))
17662         /* OK */;
17663       else
17664         containing_scope = NULL_TREE;
17665     }
17666
17667   /* Ignore function type "scopes" from the C frontend.  They mean that
17668      a tagged type is local to a parmlist of a function declarator, but
17669      that isn't useful to DWARF.  */
17670   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17671     containing_scope = NULL_TREE;
17672
17673   if (containing_scope == NULL_TREE)
17674     scope_die = comp_unit_die;
17675   else if (TYPE_P (containing_scope))
17676     {
17677       /* For types, we can just look up the appropriate DIE.  But
17678          first we check to see if we're in the middle of emitting it
17679          so we know where the new DIE should go.  */
17680       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17681         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17682           break;
17683
17684       if (i < 0)
17685         {
17686           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17687                       || TREE_ASM_WRITTEN (containing_scope));
17688           /*We are not in the middle of emitting the type
17689             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17690           scope_die = lookup_type_die (containing_scope);
17691
17692           /* If none of the current dies are suitable, we get file scope.  */
17693           if (scope_die == NULL)
17694             scope_die = comp_unit_die;
17695         }
17696       else
17697         scope_die = lookup_type_die (containing_scope);
17698     }
17699   else
17700     scope_die = context_die;
17701
17702   return scope_die;
17703 }
17704
17705 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17706
17707 static inline int
17708 local_scope_p (dw_die_ref context_die)
17709 {
17710   for (; context_die; context_die = context_die->die_parent)
17711     if (context_die->die_tag == DW_TAG_inlined_subroutine
17712         || context_die->die_tag == DW_TAG_subprogram)
17713       return 1;
17714
17715   return 0;
17716 }
17717
17718 /* Returns nonzero if CONTEXT_DIE is a class.  */
17719
17720 static inline int
17721 class_scope_p (dw_die_ref context_die)
17722 {
17723   return (context_die
17724           && (context_die->die_tag == DW_TAG_structure_type
17725               || context_die->die_tag == DW_TAG_class_type
17726               || context_die->die_tag == DW_TAG_interface_type
17727               || context_die->die_tag == DW_TAG_union_type));
17728 }
17729
17730 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17731    whether or not to treat a DIE in this context as a declaration.  */
17732
17733 static inline int
17734 class_or_namespace_scope_p (dw_die_ref context_die)
17735 {
17736   return (class_scope_p (context_die)
17737           || (context_die && context_die->die_tag == DW_TAG_namespace));
17738 }
17739
17740 /* Many forms of DIEs require a "type description" attribute.  This
17741    routine locates the proper "type descriptor" die for the type given
17742    by 'type', and adds a DW_AT_type attribute below the given die.  */
17743
17744 static void
17745 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17746                     int decl_volatile, dw_die_ref context_die)
17747 {
17748   enum tree_code code  = TREE_CODE (type);
17749   dw_die_ref type_die  = NULL;
17750
17751   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17752      or fixed-point type, use the inner type.  This is because we have no
17753      support for unnamed types in base_type_die.  This can happen if this is
17754      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17755   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17756       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17757     type = TREE_TYPE (type), code = TREE_CODE (type);
17758
17759   if (code == ERROR_MARK
17760       /* Handle a special case.  For functions whose return type is void, we
17761          generate *no* type attribute.  (Note that no object may have type
17762          `void', so this only applies to function return types).  */
17763       || code == VOID_TYPE)
17764     return;
17765
17766   type_die = modified_type_die (type,
17767                                 decl_const || TYPE_READONLY (type),
17768                                 decl_volatile || TYPE_VOLATILE (type),
17769                                 context_die);
17770
17771   if (type_die != NULL)
17772     add_AT_die_ref (object_die, DW_AT_type, type_die);
17773 }
17774
17775 /* Given an object die, add the calling convention attribute for the
17776    function call type.  */
17777 static void
17778 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17779 {
17780   enum dwarf_calling_convention value = DW_CC_normal;
17781
17782   value = ((enum dwarf_calling_convention)
17783            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17784
17785   /* DWARF doesn't provide a way to identify a program's source-level
17786      entry point.  DW_AT_calling_convention attributes are only meant
17787      to describe functions' calling conventions.  However, lacking a
17788      better way to signal the Fortran main program, we use this for the
17789      time being, following existing custom.  */
17790   if (is_fortran ()
17791       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17792     value = DW_CC_program;
17793
17794   /* Only add the attribute if the backend requests it, and
17795      is not DW_CC_normal.  */
17796   if (value && (value != DW_CC_normal))
17797     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17798 }
17799
17800 /* Given a tree pointer to a struct, class, union, or enum type node, return
17801    a pointer to the (string) tag name for the given type, or zero if the type
17802    was declared without a tag.  */
17803
17804 static const char *
17805 type_tag (const_tree type)
17806 {
17807   const char *name = 0;
17808
17809   if (TYPE_NAME (type) != 0)
17810     {
17811       tree t = 0;
17812
17813       /* Find the IDENTIFIER_NODE for the type name.  */
17814       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17815           && !TYPE_NAMELESS (type))
17816         t = TYPE_NAME (type);
17817
17818       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17819          a TYPE_DECL node, regardless of whether or not a `typedef' was
17820          involved.  */
17821       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17822                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17823         {
17824           /* We want to be extra verbose.  Don't call dwarf_name if
17825              DECL_NAME isn't set.  The default hook for decl_printable_name
17826              doesn't like that, and in this context it's correct to return
17827              0, instead of "<anonymous>" or the like.  */
17828           if (DECL_NAME (TYPE_NAME (type))
17829               && !DECL_NAMELESS (TYPE_NAME (type)))
17830             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17831         }
17832
17833       /* Now get the name as a string, or invent one.  */
17834       if (!name && t != 0)
17835         name = IDENTIFIER_POINTER (t);
17836     }
17837
17838   return (name == 0 || *name == '\0') ? 0 : name;
17839 }
17840
17841 /* Return the type associated with a data member, make a special check
17842    for bit field types.  */
17843
17844 static inline tree
17845 member_declared_type (const_tree member)
17846 {
17847   return (DECL_BIT_FIELD_TYPE (member)
17848           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17849 }
17850
17851 /* Get the decl's label, as described by its RTL. This may be different
17852    from the DECL_NAME name used in the source file.  */
17853
17854 #if 0
17855 static const char *
17856 decl_start_label (tree decl)
17857 {
17858   rtx x;
17859   const char *fnname;
17860
17861   x = DECL_RTL (decl);
17862   gcc_assert (MEM_P (x));
17863
17864   x = XEXP (x, 0);
17865   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17866
17867   fnname = XSTR (x, 0);
17868   return fnname;
17869 }
17870 #endif
17871 \f
17872 /* These routines generate the internal representation of the DIE's for
17873    the compilation unit.  Debugging information is collected by walking
17874    the declaration trees passed in from dwarf2out_decl().  */
17875
17876 static void
17877 gen_array_type_die (tree type, dw_die_ref context_die)
17878 {
17879   dw_die_ref scope_die = scope_die_for (type, context_die);
17880   dw_die_ref array_die;
17881
17882   /* GNU compilers represent multidimensional array types as sequences of one
17883      dimensional array types whose element types are themselves array types.
17884      We sometimes squish that down to a single array_type DIE with multiple
17885      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17886      say that we are allowed to do this kind of compression in C, because
17887      there is no difference between an array of arrays and a multidimensional
17888      array.  We don't do this for Ada to remain as close as possible to the
17889      actual representation, which is especially important against the language
17890      flexibilty wrt arrays of variable size.  */
17891
17892   bool collapse_nested_arrays = !is_ada ();
17893   tree element_type;
17894
17895   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17896      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17897   if (TYPE_STRING_FLAG (type)
17898       && TREE_CODE (type) == ARRAY_TYPE
17899       && is_fortran ()
17900       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17901     {
17902       HOST_WIDE_INT size;
17903
17904       array_die = new_die (DW_TAG_string_type, scope_die, type);
17905       add_name_attribute (array_die, type_tag (type));
17906       equate_type_number_to_die (type, array_die);
17907       size = int_size_in_bytes (type);
17908       if (size >= 0)
17909         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17910       else if (TYPE_DOMAIN (type) != NULL_TREE
17911                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17912                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17913         {
17914           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17915           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17916
17917           size = int_size_in_bytes (TREE_TYPE (szdecl));
17918           if (loc && size > 0)
17919             {
17920               add_AT_location_description (array_die, DW_AT_string_length, loc);
17921               if (size != DWARF2_ADDR_SIZE)
17922                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17923             }
17924         }
17925       return;
17926     }
17927
17928   /* ??? The SGI dwarf reader fails for array of array of enum types
17929      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17930      array type comes before the outer array type.  We thus call gen_type_die
17931      before we new_die and must prevent nested array types collapsing for this
17932      target.  */
17933
17934 #ifdef MIPS_DEBUGGING_INFO
17935   gen_type_die (TREE_TYPE (type), context_die);
17936   collapse_nested_arrays = false;
17937 #endif
17938
17939   array_die = new_die (DW_TAG_array_type, scope_die, type);
17940   add_name_attribute (array_die, type_tag (type));
17941   equate_type_number_to_die (type, array_die);
17942
17943   if (TREE_CODE (type) == VECTOR_TYPE)
17944     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17945
17946   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17947   if (is_fortran ()
17948       && TREE_CODE (type) == ARRAY_TYPE
17949       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17950       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17951     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17952
17953 #if 0
17954   /* We default the array ordering.  SDB will probably do
17955      the right things even if DW_AT_ordering is not present.  It's not even
17956      an issue until we start to get into multidimensional arrays anyway.  If
17957      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17958      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17959      and when we find out that we need to put these in, we will only do so
17960      for multidimensional arrays.  */
17961   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17962 #endif
17963
17964 #ifdef MIPS_DEBUGGING_INFO
17965   /* The SGI compilers handle arrays of unknown bound by setting
17966      AT_declaration and not emitting any subrange DIEs.  */
17967   if (TREE_CODE (type) == ARRAY_TYPE
17968       && ! TYPE_DOMAIN (type))
17969     add_AT_flag (array_die, DW_AT_declaration, 1);
17970   else
17971 #endif
17972   if (TREE_CODE (type) == VECTOR_TYPE)
17973     {
17974       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
17975       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17976       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17977       add_bound_info (subrange_die, DW_AT_upper_bound,
17978                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17979     }
17980   else
17981     add_subscript_info (array_die, type, collapse_nested_arrays);
17982
17983   /* Add representation of the type of the elements of this array type and
17984      emit the corresponding DIE if we haven't done it already.  */
17985   element_type = TREE_TYPE (type);
17986   if (collapse_nested_arrays)
17987     while (TREE_CODE (element_type) == ARRAY_TYPE)
17988       {
17989         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17990           break;
17991         element_type = TREE_TYPE (element_type);
17992       }
17993
17994 #ifndef MIPS_DEBUGGING_INFO
17995   gen_type_die (element_type, context_die);
17996 #endif
17997
17998   add_type_attribute (array_die, element_type, 0, 0, context_die);
17999
18000   if (get_AT (array_die, DW_AT_name))
18001     add_pubtype (type, array_die);
18002 }
18003
18004 static dw_loc_descr_ref
18005 descr_info_loc (tree val, tree base_decl)
18006 {
18007   HOST_WIDE_INT size;
18008   dw_loc_descr_ref loc, loc2;
18009   enum dwarf_location_atom op;
18010
18011   if (val == base_decl)
18012     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18013
18014   switch (TREE_CODE (val))
18015     {
18016     CASE_CONVERT:
18017       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18018     case VAR_DECL:
18019       return loc_descriptor_from_tree (val, 0);
18020     case INTEGER_CST:
18021       if (host_integerp (val, 0))
18022         return int_loc_descriptor (tree_low_cst (val, 0));
18023       break;
18024     case INDIRECT_REF:
18025       size = int_size_in_bytes (TREE_TYPE (val));
18026       if (size < 0)
18027         break;
18028       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18029       if (!loc)
18030         break;
18031       if (size == DWARF2_ADDR_SIZE)
18032         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18033       else
18034         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18035       return loc;
18036     case POINTER_PLUS_EXPR:
18037     case PLUS_EXPR:
18038       if (host_integerp (TREE_OPERAND (val, 1), 1)
18039           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18040              < 16384)
18041         {
18042           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18043           if (!loc)
18044             break;
18045           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18046         }
18047       else
18048         {
18049           op = DW_OP_plus;
18050         do_binop:
18051           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18052           if (!loc)
18053             break;
18054           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18055           if (!loc2)
18056             break;
18057           add_loc_descr (&loc, loc2);
18058           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18059         }
18060       return loc;
18061     case MINUS_EXPR:
18062       op = DW_OP_minus;
18063       goto do_binop;
18064     case MULT_EXPR:
18065       op = DW_OP_mul;
18066       goto do_binop;
18067     case EQ_EXPR:
18068       op = DW_OP_eq;
18069       goto do_binop;
18070     case NE_EXPR:
18071       op = DW_OP_ne;
18072       goto do_binop;
18073     default:
18074       break;
18075     }
18076   return NULL;
18077 }
18078
18079 static void
18080 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18081                       tree val, tree base_decl)
18082 {
18083   dw_loc_descr_ref loc;
18084
18085   if (host_integerp (val, 0))
18086     {
18087       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18088       return;
18089     }
18090
18091   loc = descr_info_loc (val, base_decl);
18092   if (!loc)
18093     return;
18094
18095   add_AT_loc (die, attr, loc);
18096 }
18097
18098 /* This routine generates DIE for array with hidden descriptor, details
18099    are filled into *info by a langhook.  */
18100
18101 static void
18102 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18103                           dw_die_ref context_die)
18104 {
18105   dw_die_ref scope_die = scope_die_for (type, context_die);
18106   dw_die_ref array_die;
18107   int dim;
18108
18109   array_die = new_die (DW_TAG_array_type, scope_die, type);
18110   add_name_attribute (array_die, type_tag (type));
18111   equate_type_number_to_die (type, array_die);
18112
18113   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18114   if (is_fortran ()
18115       && info->ndimensions >= 2)
18116     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18117
18118   if (info->data_location)
18119     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18120                           info->base_decl);
18121   if (info->associated)
18122     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18123                           info->base_decl);
18124   if (info->allocated)
18125     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18126                           info->base_decl);
18127
18128   for (dim = 0; dim < info->ndimensions; dim++)
18129     {
18130       dw_die_ref subrange_die
18131         = new_die (DW_TAG_subrange_type, array_die, NULL);
18132
18133       if (info->dimen[dim].lower_bound)
18134         {
18135           /* If it is the default value, omit it.  */
18136           int dflt;
18137
18138           if (host_integerp (info->dimen[dim].lower_bound, 0)
18139               && (dflt = lower_bound_default ()) != -1
18140               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18141             ;
18142           else
18143             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18144                                   info->dimen[dim].lower_bound,
18145                                   info->base_decl);
18146         }
18147       if (info->dimen[dim].upper_bound)
18148         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18149                               info->dimen[dim].upper_bound,
18150                               info->base_decl);
18151       if (info->dimen[dim].stride)
18152         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18153                               info->dimen[dim].stride,
18154                               info->base_decl);
18155     }
18156
18157   gen_type_die (info->element_type, context_die);
18158   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18159
18160   if (get_AT (array_die, DW_AT_name))
18161     add_pubtype (type, array_die);
18162 }
18163
18164 #if 0
18165 static void
18166 gen_entry_point_die (tree decl, dw_die_ref context_die)
18167 {
18168   tree origin = decl_ultimate_origin (decl);
18169   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18170
18171   if (origin != NULL)
18172     add_abstract_origin_attribute (decl_die, origin);
18173   else
18174     {
18175       add_name_and_src_coords_attributes (decl_die, decl);
18176       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18177                           0, 0, context_die);
18178     }
18179
18180   if (DECL_ABSTRACT (decl))
18181     equate_decl_number_to_die (decl, decl_die);
18182   else
18183     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18184 }
18185 #endif
18186
18187 /* Walk through the list of incomplete types again, trying once more to
18188    emit full debugging info for them.  */
18189
18190 static void
18191 retry_incomplete_types (void)
18192 {
18193   int i;
18194
18195   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18196     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18197                                   DINFO_USAGE_DIR_USE))
18198       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18199 }
18200
18201 /* Determine what tag to use for a record type.  */
18202
18203 static enum dwarf_tag
18204 record_type_tag (tree type)
18205 {
18206   if (! lang_hooks.types.classify_record)
18207     return DW_TAG_structure_type;
18208
18209   switch (lang_hooks.types.classify_record (type))
18210     {
18211     case RECORD_IS_STRUCT:
18212       return DW_TAG_structure_type;
18213
18214     case RECORD_IS_CLASS:
18215       return DW_TAG_class_type;
18216
18217     case RECORD_IS_INTERFACE:
18218       if (dwarf_version >= 3 || !dwarf_strict)
18219         return DW_TAG_interface_type;
18220       return DW_TAG_structure_type;
18221
18222     default:
18223       gcc_unreachable ();
18224     }
18225 }
18226
18227 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18228    include all of the information about the enumeration values also. Each
18229    enumerated type name/value is listed as a child of the enumerated type
18230    DIE.  */
18231
18232 static dw_die_ref
18233 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18234 {
18235   dw_die_ref type_die = lookup_type_die (type);
18236
18237   if (type_die == NULL)
18238     {
18239       type_die = new_die (DW_TAG_enumeration_type,
18240                           scope_die_for (type, context_die), type);
18241       equate_type_number_to_die (type, type_die);
18242       add_name_attribute (type_die, type_tag (type));
18243       if ((dwarf_version >= 4 || !dwarf_strict)
18244           && ENUM_IS_SCOPED (type))
18245         add_AT_flag (type_die, DW_AT_enum_class, 1);
18246     }
18247   else if (! TYPE_SIZE (type))
18248     return type_die;
18249   else
18250     remove_AT (type_die, DW_AT_declaration);
18251
18252   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18253      given enum type is incomplete, do not generate the DW_AT_byte_size
18254      attribute or the DW_AT_element_list attribute.  */
18255   if (TYPE_SIZE (type))
18256     {
18257       tree link;
18258
18259       TREE_ASM_WRITTEN (type) = 1;
18260       add_byte_size_attribute (type_die, type);
18261       if (TYPE_STUB_DECL (type) != NULL_TREE)
18262         {
18263           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18264           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18265         }
18266
18267       /* If the first reference to this type was as the return type of an
18268          inline function, then it may not have a parent.  Fix this now.  */
18269       if (type_die->die_parent == NULL)
18270         add_child_die (scope_die_for (type, context_die), type_die);
18271
18272       for (link = TYPE_VALUES (type);
18273            link != NULL; link = TREE_CHAIN (link))
18274         {
18275           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18276           tree value = TREE_VALUE (link);
18277
18278           add_name_attribute (enum_die,
18279                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18280
18281           if (TREE_CODE (value) == CONST_DECL)
18282             value = DECL_INITIAL (value);
18283
18284           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18285             /* DWARF2 does not provide a way of indicating whether or
18286                not enumeration constants are signed or unsigned.  GDB
18287                always assumes the values are signed, so we output all
18288                values as if they were signed.  That means that
18289                enumeration constants with very large unsigned values
18290                will appear to have negative values in the debugger.  */
18291             add_AT_int (enum_die, DW_AT_const_value,
18292                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18293         }
18294     }
18295   else
18296     add_AT_flag (type_die, DW_AT_declaration, 1);
18297
18298   if (get_AT (type_die, DW_AT_name))
18299     add_pubtype (type, type_die);
18300
18301   return type_die;
18302 }
18303
18304 /* Generate a DIE to represent either a real live formal parameter decl or to
18305    represent just the type of some formal parameter position in some function
18306    type.
18307
18308    Note that this routine is a bit unusual because its argument may be a
18309    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18310    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18311    node.  If it's the former then this function is being called to output a
18312    DIE to represent a formal parameter object (or some inlining thereof).  If
18313    it's the latter, then this function is only being called to output a
18314    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18315    argument type of some subprogram type.
18316    If EMIT_NAME_P is true, name and source coordinate attributes
18317    are emitted.  */
18318
18319 static dw_die_ref
18320 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18321                           dw_die_ref context_die)
18322 {
18323   tree node_or_origin = node ? node : origin;
18324   tree ultimate_origin;
18325   dw_die_ref parm_die
18326     = new_die (DW_TAG_formal_parameter, context_die, node);
18327
18328   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18329     {
18330     case tcc_declaration:
18331       ultimate_origin = decl_ultimate_origin (node_or_origin);
18332       if (node || ultimate_origin)
18333         origin = ultimate_origin;
18334       if (origin != NULL)
18335         add_abstract_origin_attribute (parm_die, origin);
18336       else if (emit_name_p)
18337         add_name_and_src_coords_attributes (parm_die, node);
18338       if (origin == NULL
18339           || (! DECL_ABSTRACT (node_or_origin)
18340               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18341                                            decl_function_context
18342                                                             (node_or_origin))))
18343         {
18344           tree type = TREE_TYPE (node_or_origin);
18345           if (decl_by_reference_p (node_or_origin))
18346             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18347                                 context_die);
18348           else
18349             add_type_attribute (parm_die, type,
18350                                 TREE_READONLY (node_or_origin),
18351                                 TREE_THIS_VOLATILE (node_or_origin),
18352                                 context_die);
18353         }
18354       if (origin == NULL && DECL_ARTIFICIAL (node))
18355         add_AT_flag (parm_die, DW_AT_artificial, 1);
18356
18357       if (node && node != origin)
18358         equate_decl_number_to_die (node, parm_die);
18359       if (! DECL_ABSTRACT (node_or_origin))
18360         add_location_or_const_value_attribute (parm_die, node_or_origin,
18361                                                DW_AT_location);
18362
18363       break;
18364
18365     case tcc_type:
18366       /* We were called with some kind of a ..._TYPE node.  */
18367       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18368       break;
18369
18370     default:
18371       gcc_unreachable ();
18372     }
18373
18374   return parm_die;
18375 }
18376
18377 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18378    children DW_TAG_formal_parameter DIEs representing the arguments of the
18379    parameter pack.
18380
18381    PARM_PACK must be a function parameter pack.
18382    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18383    must point to the subsequent arguments of the function PACK_ARG belongs to.
18384    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18385    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18386    following the last one for which a DIE was generated.  */
18387
18388 static dw_die_ref
18389 gen_formal_parameter_pack_die  (tree parm_pack,
18390                                 tree pack_arg,
18391                                 dw_die_ref subr_die,
18392                                 tree *next_arg)
18393 {
18394   tree arg;
18395   dw_die_ref parm_pack_die;
18396
18397   gcc_assert (parm_pack
18398               && lang_hooks.function_parameter_pack_p (parm_pack)
18399               && subr_die);
18400
18401   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18402   add_src_coords_attributes (parm_pack_die, parm_pack);
18403
18404   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18405     {
18406       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18407                                                                  parm_pack))
18408         break;
18409       gen_formal_parameter_die (arg, NULL,
18410                                 false /* Don't emit name attribute.  */,
18411                                 parm_pack_die);
18412     }
18413   if (next_arg)
18414     *next_arg = arg;
18415   return parm_pack_die;
18416 }
18417
18418 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18419    at the end of an (ANSI prototyped) formal parameters list.  */
18420
18421 static void
18422 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18423 {
18424   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18425 }
18426
18427 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18428    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18429    parameters as specified in some function type specification (except for
18430    those which appear as part of a function *definition*).  */
18431
18432 static void
18433 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18434 {
18435   tree link;
18436   tree formal_type = NULL;
18437   tree first_parm_type;
18438   tree arg;
18439
18440   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18441     {
18442       arg = DECL_ARGUMENTS (function_or_method_type);
18443       function_or_method_type = TREE_TYPE (function_or_method_type);
18444     }
18445   else
18446     arg = NULL_TREE;
18447
18448   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18449
18450   /* Make our first pass over the list of formal parameter types and output a
18451      DW_TAG_formal_parameter DIE for each one.  */
18452   for (link = first_parm_type; link; )
18453     {
18454       dw_die_ref parm_die;
18455
18456       formal_type = TREE_VALUE (link);
18457       if (formal_type == void_type_node)
18458         break;
18459
18460       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18461       parm_die = gen_formal_parameter_die (formal_type, NULL,
18462                                            true /* Emit name attribute.  */,
18463                                            context_die);
18464       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18465           && link == first_parm_type)
18466         {
18467           add_AT_flag (parm_die, DW_AT_artificial, 1);
18468           if (dwarf_version >= 3 || !dwarf_strict)
18469             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18470         }
18471       else if (arg && DECL_ARTIFICIAL (arg))
18472         add_AT_flag (parm_die, DW_AT_artificial, 1);
18473
18474       link = TREE_CHAIN (link);
18475       if (arg)
18476         arg = DECL_CHAIN (arg);
18477     }
18478
18479   /* If this function type has an ellipsis, add a
18480      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18481   if (formal_type != void_type_node)
18482     gen_unspecified_parameters_die (function_or_method_type, context_die);
18483
18484   /* Make our second (and final) pass over the list of formal parameter types
18485      and output DIEs to represent those types (as necessary).  */
18486   for (link = TYPE_ARG_TYPES (function_or_method_type);
18487        link && TREE_VALUE (link);
18488        link = TREE_CHAIN (link))
18489     gen_type_die (TREE_VALUE (link), context_die);
18490 }
18491
18492 /* We want to generate the DIE for TYPE so that we can generate the
18493    die for MEMBER, which has been defined; we will need to refer back
18494    to the member declaration nested within TYPE.  If we're trying to
18495    generate minimal debug info for TYPE, processing TYPE won't do the
18496    trick; we need to attach the member declaration by hand.  */
18497
18498 static void
18499 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18500 {
18501   gen_type_die (type, context_die);
18502
18503   /* If we're trying to avoid duplicate debug info, we may not have
18504      emitted the member decl for this function.  Emit it now.  */
18505   if (TYPE_STUB_DECL (type)
18506       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18507       && ! lookup_decl_die (member))
18508     {
18509       dw_die_ref type_die;
18510       gcc_assert (!decl_ultimate_origin (member));
18511
18512       push_decl_scope (type);
18513       type_die = lookup_type_die (type);
18514       if (TREE_CODE (member) == FUNCTION_DECL)
18515         gen_subprogram_die (member, type_die);
18516       else if (TREE_CODE (member) == FIELD_DECL)
18517         {
18518           /* Ignore the nameless fields that are used to skip bits but handle
18519              C++ anonymous unions and structs.  */
18520           if (DECL_NAME (member) != NULL_TREE
18521               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18522               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18523             {
18524               gen_type_die (member_declared_type (member), type_die);
18525               gen_field_die (member, type_die);
18526             }
18527         }
18528       else
18529         gen_variable_die (member, NULL_TREE, type_die);
18530
18531       pop_decl_scope ();
18532     }
18533 }
18534
18535 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18536    may later generate inlined and/or out-of-line instances of.  */
18537
18538 static void
18539 dwarf2out_abstract_function (tree decl)
18540 {
18541   dw_die_ref old_die;
18542   tree save_fn;
18543   tree context;
18544   int was_abstract;
18545   htab_t old_decl_loc_table;
18546
18547   /* Make sure we have the actual abstract inline, not a clone.  */
18548   decl = DECL_ORIGIN (decl);
18549
18550   old_die = lookup_decl_die (decl);
18551   if (old_die && get_AT (old_die, DW_AT_inline))
18552     /* We've already generated the abstract instance.  */
18553     return;
18554
18555   /* We can be called while recursively when seeing block defining inlined subroutine
18556      DIE.  Be sure to not clobber the outer location table nor use it or we would
18557      get locations in abstract instantces.  */
18558   old_decl_loc_table = decl_loc_table;
18559   decl_loc_table = NULL;
18560
18561   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18562      we don't get confused by DECL_ABSTRACT.  */
18563   if (debug_info_level > DINFO_LEVEL_TERSE)
18564     {
18565       context = decl_class_context (decl);
18566       if (context)
18567         gen_type_die_for_member
18568           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18569     }
18570
18571   /* Pretend we've just finished compiling this function.  */
18572   save_fn = current_function_decl;
18573   current_function_decl = decl;
18574   push_cfun (DECL_STRUCT_FUNCTION (decl));
18575
18576   was_abstract = DECL_ABSTRACT (decl);
18577   set_decl_abstract_flags (decl, 1);
18578   dwarf2out_decl (decl);
18579   if (! was_abstract)
18580     set_decl_abstract_flags (decl, 0);
18581
18582   current_function_decl = save_fn;
18583   decl_loc_table = old_decl_loc_table;
18584   pop_cfun ();
18585 }
18586
18587 /* Helper function of premark_used_types() which gets called through
18588    htab_traverse.
18589
18590    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18591    marked as unused by prune_unused_types.  */
18592
18593 static int
18594 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18595 {
18596   tree type;
18597   dw_die_ref die;
18598
18599   type = (tree) *slot;
18600   die = lookup_type_die (type);
18601   if (die != NULL)
18602     die->die_perennial_p = 1;
18603   return 1;
18604 }
18605
18606 /* Helper function of premark_types_used_by_global_vars which gets called
18607    through htab_traverse.
18608
18609    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18610    marked as unused by prune_unused_types. The DIE of the type is marked
18611    only if the global variable using the type will actually be emitted.  */
18612
18613 static int
18614 premark_types_used_by_global_vars_helper (void **slot,
18615                                           void *data ATTRIBUTE_UNUSED)
18616 {
18617   struct types_used_by_vars_entry *entry;
18618   dw_die_ref die;
18619
18620   entry = (struct types_used_by_vars_entry *) *slot;
18621   gcc_assert (entry->type != NULL
18622               && entry->var_decl != NULL);
18623   die = lookup_type_die (entry->type);
18624   if (die)
18625     {
18626       /* Ask cgraph if the global variable really is to be emitted.
18627          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18628       struct varpool_node *node = varpool_get_node (entry->var_decl);
18629       if (node && node->needed)
18630         {
18631           die->die_perennial_p = 1;
18632           /* Keep the parent DIEs as well.  */
18633           while ((die = die->die_parent) && die->die_perennial_p == 0)
18634             die->die_perennial_p = 1;
18635         }
18636     }
18637   return 1;
18638 }
18639
18640 /* Mark all members of used_types_hash as perennial.  */
18641
18642 static void
18643 premark_used_types (void)
18644 {
18645   if (cfun && cfun->used_types_hash)
18646     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18647 }
18648
18649 /* Mark all members of types_used_by_vars_entry as perennial.  */
18650
18651 static void
18652 premark_types_used_by_global_vars (void)
18653 {
18654   if (types_used_by_vars_hash)
18655     htab_traverse (types_used_by_vars_hash,
18656                    premark_types_used_by_global_vars_helper, NULL);
18657 }
18658
18659 /* Generate a DIE to represent a declared function (either file-scope or
18660    block-local).  */
18661
18662 static void
18663 gen_subprogram_die (tree decl, dw_die_ref context_die)
18664 {
18665   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18666   tree origin = decl_ultimate_origin (decl);
18667   dw_die_ref subr_die;
18668   tree fn_arg_types;
18669   tree outer_scope;
18670   dw_die_ref old_die = lookup_decl_die (decl);
18671   int declaration = (current_function_decl != decl
18672                      || class_or_namespace_scope_p (context_die));
18673
18674   premark_used_types ();
18675
18676   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18677      started to generate the abstract instance of an inline, decided to output
18678      its containing class, and proceeded to emit the declaration of the inline
18679      from the member list for the class.  If so, DECLARATION takes priority;
18680      we'll get back to the abstract instance when done with the class.  */
18681
18682   /* The class-scope declaration DIE must be the primary DIE.  */
18683   if (origin && declaration && class_or_namespace_scope_p (context_die))
18684     {
18685       origin = NULL;
18686       gcc_assert (!old_die);
18687     }
18688
18689   /* Now that the C++ front end lazily declares artificial member fns, we
18690      might need to retrofit the declaration into its class.  */
18691   if (!declaration && !origin && !old_die
18692       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18693       && !class_or_namespace_scope_p (context_die)
18694       && debug_info_level > DINFO_LEVEL_TERSE)
18695     old_die = force_decl_die (decl);
18696
18697   if (origin != NULL)
18698     {
18699       gcc_assert (!declaration || local_scope_p (context_die));
18700
18701       /* Fixup die_parent for the abstract instance of a nested
18702          inline function.  */
18703       if (old_die && old_die->die_parent == NULL)
18704         add_child_die (context_die, old_die);
18705
18706       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18707       add_abstract_origin_attribute (subr_die, origin);
18708     }
18709   else if (old_die)
18710     {
18711       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18712       struct dwarf_file_data * file_index = lookup_filename (s.file);
18713
18714       if (!get_AT_flag (old_die, DW_AT_declaration)
18715           /* We can have a normal definition following an inline one in the
18716              case of redefinition of GNU C extern inlines.
18717              It seems reasonable to use AT_specification in this case.  */
18718           && !get_AT (old_die, DW_AT_inline))
18719         {
18720           /* Detect and ignore this case, where we are trying to output
18721              something we have already output.  */
18722           return;
18723         }
18724
18725       /* If the definition comes from the same place as the declaration,
18726          maybe use the old DIE.  We always want the DIE for this function
18727          that has the *_pc attributes to be under comp_unit_die so the
18728          debugger can find it.  We also need to do this for abstract
18729          instances of inlines, since the spec requires the out-of-line copy
18730          to have the same parent.  For local class methods, this doesn't
18731          apply; we just use the old DIE.  */
18732       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18733           && (DECL_ARTIFICIAL (decl)
18734               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18735                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18736                       == (unsigned) s.line))))
18737         {
18738           subr_die = old_die;
18739
18740           /* Clear out the declaration attribute and the formal parameters.
18741              Do not remove all children, because it is possible that this
18742              declaration die was forced using force_decl_die(). In such
18743              cases die that forced declaration die (e.g. TAG_imported_module)
18744              is one of the children that we do not want to remove.  */
18745           remove_AT (subr_die, DW_AT_declaration);
18746           remove_AT (subr_die, DW_AT_object_pointer);
18747           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18748         }
18749       else
18750         {
18751           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18752           add_AT_specification (subr_die, old_die);
18753           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18754             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18755           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18756             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18757         }
18758     }
18759   else
18760     {
18761       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18762
18763       if (TREE_PUBLIC (decl))
18764         add_AT_flag (subr_die, DW_AT_external, 1);
18765
18766       add_name_and_src_coords_attributes (subr_die, decl);
18767       if (debug_info_level > DINFO_LEVEL_TERSE)
18768         {
18769           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18770           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18771                               0, 0, context_die);
18772         }
18773
18774       add_pure_or_virtual_attribute (subr_die, decl);
18775       if (DECL_ARTIFICIAL (decl))
18776         add_AT_flag (subr_die, DW_AT_artificial, 1);
18777
18778       add_accessibility_attribute (subr_die, decl);
18779     }
18780
18781   if (declaration)
18782     {
18783       if (!old_die || !get_AT (old_die, DW_AT_inline))
18784         {
18785           add_AT_flag (subr_die, DW_AT_declaration, 1);
18786
18787           /* If this is an explicit function declaration then generate
18788              a DW_AT_explicit attribute.  */
18789           if (lang_hooks.decls.function_decl_explicit_p (decl)
18790               && (dwarf_version >= 3 || !dwarf_strict))
18791             add_AT_flag (subr_die, DW_AT_explicit, 1);
18792
18793           /* The first time we see a member function, it is in the context of
18794              the class to which it belongs.  We make sure of this by emitting
18795              the class first.  The next time is the definition, which is
18796              handled above.  The two may come from the same source text.
18797
18798              Note that force_decl_die() forces function declaration die. It is
18799              later reused to represent definition.  */
18800           equate_decl_number_to_die (decl, subr_die);
18801         }
18802     }
18803   else if (DECL_ABSTRACT (decl))
18804     {
18805       if (DECL_DECLARED_INLINE_P (decl))
18806         {
18807           if (cgraph_function_possibly_inlined_p (decl))
18808             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18809           else
18810             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18811         }
18812       else
18813         {
18814           if (cgraph_function_possibly_inlined_p (decl))
18815             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18816           else
18817             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18818         }
18819
18820       if (DECL_DECLARED_INLINE_P (decl)
18821           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18822         add_AT_flag (subr_die, DW_AT_artificial, 1);
18823
18824       equate_decl_number_to_die (decl, subr_die);
18825     }
18826   else if (!DECL_EXTERNAL (decl))
18827     {
18828       HOST_WIDE_INT cfa_fb_offset;
18829
18830       if (!old_die || !get_AT (old_die, DW_AT_inline))
18831         equate_decl_number_to_die (decl, subr_die);
18832
18833       if (!flag_reorder_blocks_and_partition)
18834         {
18835           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18836                                        current_function_funcdef_no);
18837           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18838           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18839                                        current_function_funcdef_no);
18840           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18841
18842 #if VMS_DEBUGGING_INFO
18843       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18844          Section 2.3 Prologue and Epilogue Attributes:
18845          When a breakpoint is set on entry to a function, it is generally
18846          desirable for execution to be suspended, not on the very first
18847          instruction of the function, but rather at a point after the
18848          function's frame has been set up, after any language defined local
18849          declaration processing has been completed, and before execution of
18850          the first statement of the function begins. Debuggers generally
18851          cannot properly determine where this point is.  Similarly for a
18852          breakpoint set on exit from a function. The prologue and epilogue
18853          attributes allow a compiler to communicate the location(s) to use.  */
18854
18855       {
18856         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18857
18858         if (fde->dw_fde_vms_end_prologue)
18859           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18860             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18861
18862         if (fde->dw_fde_vms_begin_epilogue)
18863           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18864             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18865       }
18866 #endif
18867
18868           add_pubname (decl, subr_die);
18869           add_arange (decl, subr_die);
18870         }
18871       else
18872         {  /* Do nothing for now; maybe need to duplicate die, one for
18873               hot section and one for cold section, then use the hot/cold
18874               section begin/end labels to generate the aranges...  */
18875           /*
18876             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18877             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18878             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18879             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18880
18881             add_pubname (decl, subr_die);
18882             add_arange (decl, subr_die);
18883             add_arange (decl, subr_die);
18884            */
18885         }
18886
18887 #ifdef MIPS_DEBUGGING_INFO
18888       /* Add a reference to the FDE for this routine.  */
18889       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18890 #endif
18891
18892       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18893
18894       /* We define the "frame base" as the function's CFA.  This is more
18895          convenient for several reasons: (1) It's stable across the prologue
18896          and epilogue, which makes it better than just a frame pointer,
18897          (2) With dwarf3, there exists a one-byte encoding that allows us
18898          to reference the .debug_frame data by proxy, but failing that,
18899          (3) We can at least reuse the code inspection and interpretation
18900          code that determines the CFA position at various points in the
18901          function.  */
18902       if (dwarf_version >= 3)
18903         {
18904           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18905           add_AT_loc (subr_die, DW_AT_frame_base, op);
18906         }
18907       else
18908         {
18909           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18910           if (list->dw_loc_next)
18911             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18912           else
18913             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18914         }
18915
18916       /* Compute a displacement from the "steady-state frame pointer" to
18917          the CFA.  The former is what all stack slots and argument slots
18918          will reference in the rtl; the later is what we've told the
18919          debugger about.  We'll need to adjust all frame_base references
18920          by this displacement.  */
18921       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18922
18923       if (cfun->static_chain_decl)
18924         add_AT_location_description (subr_die, DW_AT_static_link,
18925                  loc_list_from_tree (cfun->static_chain_decl, 2));
18926     }
18927
18928   /* Generate child dies for template paramaters.  */
18929   if (debug_info_level > DINFO_LEVEL_TERSE)
18930     gen_generic_params_dies (decl);
18931
18932   /* Now output descriptions of the arguments for this function. This gets
18933      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18934      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18935      `...' at the end of the formal parameter list.  In order to find out if
18936      there was a trailing ellipsis or not, we must instead look at the type
18937      associated with the FUNCTION_DECL.  This will be a node of type
18938      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18939      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18940      an ellipsis at the end.  */
18941
18942   /* In the case where we are describing a mere function declaration, all we
18943      need to do here (and all we *can* do here) is to describe the *types* of
18944      its formal parameters.  */
18945   if (debug_info_level <= DINFO_LEVEL_TERSE)
18946     ;
18947   else if (declaration)
18948     gen_formal_types_die (decl, subr_die);
18949   else
18950     {
18951       /* Generate DIEs to represent all known formal parameters.  */
18952       tree parm = DECL_ARGUMENTS (decl);
18953       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18954       tree generic_decl_parm = generic_decl
18955                                 ? DECL_ARGUMENTS (generic_decl)
18956                                 : NULL;
18957
18958       /* Now we want to walk the list of parameters of the function and
18959          emit their relevant DIEs.
18960
18961          We consider the case of DECL being an instance of a generic function
18962          as well as it being a normal function.
18963
18964          If DECL is an instance of a generic function we walk the
18965          parameters of the generic function declaration _and_ the parameters of
18966          DECL itself. This is useful because we want to emit specific DIEs for
18967          function parameter packs and those are declared as part of the
18968          generic function declaration. In that particular case,
18969          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18970          That DIE has children DIEs representing the set of arguments
18971          of the pack. Note that the set of pack arguments can be empty.
18972          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18973          children DIE.
18974
18975          Otherwise, we just consider the parameters of DECL.  */
18976       while (generic_decl_parm || parm)
18977         {
18978           if (generic_decl_parm
18979               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18980             gen_formal_parameter_pack_die (generic_decl_parm,
18981                                            parm, subr_die,
18982                                            &parm);
18983           else if (parm)
18984             {
18985               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18986
18987               if (parm == DECL_ARGUMENTS (decl)
18988                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18989                   && parm_die
18990                   && (dwarf_version >= 3 || !dwarf_strict))
18991                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18992
18993               parm = DECL_CHAIN (parm);
18994             }
18995
18996           if (generic_decl_parm)
18997             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18998         }
18999
19000       /* Decide whether we need an unspecified_parameters DIE at the end.
19001          There are 2 more cases to do this for: 1) the ansi ... declaration -
19002          this is detectable when the end of the arg list is not a
19003          void_type_node 2) an unprototyped function declaration (not a
19004          definition).  This just means that we have no info about the
19005          parameters at all.  */
19006       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19007       if (fn_arg_types != NULL)
19008         {
19009           /* This is the prototyped case, check for....  */
19010           if (stdarg_p (TREE_TYPE (decl)))
19011             gen_unspecified_parameters_die (decl, subr_die);
19012         }
19013       else if (DECL_INITIAL (decl) == NULL_TREE)
19014         gen_unspecified_parameters_die (decl, subr_die);
19015     }
19016
19017   /* Output Dwarf info for all of the stuff within the body of the function
19018      (if it has one - it may be just a declaration).  */
19019   outer_scope = DECL_INITIAL (decl);
19020
19021   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19022      a function.  This BLOCK actually represents the outermost binding contour
19023      for the function, i.e. the contour in which the function's formal
19024      parameters and labels get declared. Curiously, it appears that the front
19025      end doesn't actually put the PARM_DECL nodes for the current function onto
19026      the BLOCK_VARS list for this outer scope, but are strung off of the
19027      DECL_ARGUMENTS list for the function instead.
19028
19029      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19030      the LABEL_DECL nodes for the function however, and we output DWARF info
19031      for those in decls_for_scope.  Just within the `outer_scope' there will be
19032      a BLOCK node representing the function's outermost pair of curly braces,
19033      and any blocks used for the base and member initializers of a C++
19034      constructor function.  */
19035   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19036     {
19037       /* Emit a DW_TAG_variable DIE for a named return value.  */
19038       if (DECL_NAME (DECL_RESULT (decl)))
19039         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19040
19041       current_function_has_inlines = 0;
19042       decls_for_scope (outer_scope, subr_die, 0);
19043     }
19044   /* Add the calling convention attribute if requested.  */
19045   add_calling_convention_attribute (subr_die, decl);
19046
19047 }
19048
19049 /* Returns a hash value for X (which really is a die_struct).  */
19050
19051 static hashval_t
19052 common_block_die_table_hash (const void *x)
19053 {
19054   const_dw_die_ref d = (const_dw_die_ref) x;
19055   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19056 }
19057
19058 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19059    as decl_id and die_parent of die_struct Y.  */
19060
19061 static int
19062 common_block_die_table_eq (const void *x, const void *y)
19063 {
19064   const_dw_die_ref d = (const_dw_die_ref) x;
19065   const_dw_die_ref e = (const_dw_die_ref) y;
19066   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19067 }
19068
19069 /* Generate a DIE to represent a declared data object.
19070    Either DECL or ORIGIN must be non-null.  */
19071
19072 static void
19073 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19074 {
19075   HOST_WIDE_INT off;
19076   tree com_decl;
19077   tree decl_or_origin = decl ? decl : origin;
19078   tree ultimate_origin;
19079   dw_die_ref var_die;
19080   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19081   dw_die_ref origin_die;
19082   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19083                       || class_or_namespace_scope_p (context_die));
19084   bool specialization_p = false;
19085
19086   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19087   if (decl || ultimate_origin)
19088     origin = ultimate_origin;
19089   com_decl = fortran_common (decl_or_origin, &off);
19090
19091   /* Symbol in common gets emitted as a child of the common block, in the form
19092      of a data member.  */
19093   if (com_decl)
19094     {
19095       dw_die_ref com_die;
19096       dw_loc_list_ref loc;
19097       die_node com_die_arg;
19098
19099       var_die = lookup_decl_die (decl_or_origin);
19100       if (var_die)
19101         {
19102           if (get_AT (var_die, DW_AT_location) == NULL)
19103             {
19104               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19105               if (loc)
19106                 {
19107                   if (off)
19108                     {
19109                       /* Optimize the common case.  */
19110                       if (single_element_loc_list_p (loc)
19111                           && loc->expr->dw_loc_opc == DW_OP_addr
19112                           && loc->expr->dw_loc_next == NULL
19113                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19114                              == SYMBOL_REF)
19115                         loc->expr->dw_loc_oprnd1.v.val_addr
19116                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19117                         else
19118                           loc_list_plus_const (loc, off);
19119                     }
19120                   add_AT_location_description (var_die, DW_AT_location, loc);
19121                   remove_AT (var_die, DW_AT_declaration);
19122                 }
19123             }
19124           return;
19125         }
19126
19127       if (common_block_die_table == NULL)
19128         common_block_die_table
19129           = htab_create_ggc (10, common_block_die_table_hash,
19130                              common_block_die_table_eq, NULL);
19131
19132       com_die_arg.decl_id = DECL_UID (com_decl);
19133       com_die_arg.die_parent = context_die;
19134       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19135       loc = loc_list_from_tree (com_decl, 2);
19136       if (com_die == NULL)
19137         {
19138           const char *cnam
19139             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19140           void **slot;
19141
19142           com_die = new_die (DW_TAG_common_block, context_die, decl);
19143           add_name_and_src_coords_attributes (com_die, com_decl);
19144           if (loc)
19145             {
19146               add_AT_location_description (com_die, DW_AT_location, loc);
19147               /* Avoid sharing the same loc descriptor between
19148                  DW_TAG_common_block and DW_TAG_variable.  */
19149               loc = loc_list_from_tree (com_decl, 2);
19150             }
19151           else if (DECL_EXTERNAL (decl))
19152             add_AT_flag (com_die, DW_AT_declaration, 1);
19153           add_pubname_string (cnam, com_die); /* ??? needed? */
19154           com_die->decl_id = DECL_UID (com_decl);
19155           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19156           *slot = (void *) com_die;
19157         }
19158       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19159         {
19160           add_AT_location_description (com_die, DW_AT_location, loc);
19161           loc = loc_list_from_tree (com_decl, 2);
19162           remove_AT (com_die, DW_AT_declaration);
19163         }
19164       var_die = new_die (DW_TAG_variable, com_die, decl);
19165       add_name_and_src_coords_attributes (var_die, decl);
19166       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19167                           TREE_THIS_VOLATILE (decl), context_die);
19168       add_AT_flag (var_die, DW_AT_external, 1);
19169       if (loc)
19170         {
19171           if (off)
19172             {
19173               /* Optimize the common case.  */
19174               if (single_element_loc_list_p (loc)
19175                   && loc->expr->dw_loc_opc == DW_OP_addr
19176                   && loc->expr->dw_loc_next == NULL
19177                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19178                 loc->expr->dw_loc_oprnd1.v.val_addr
19179                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19180               else
19181                 loc_list_plus_const (loc, off);
19182             }
19183           add_AT_location_description (var_die, DW_AT_location, loc);
19184         }
19185       else if (DECL_EXTERNAL (decl))
19186         add_AT_flag (var_die, DW_AT_declaration, 1);
19187       equate_decl_number_to_die (decl, var_die);
19188       return;
19189     }
19190
19191   /* If the compiler emitted a definition for the DECL declaration
19192      and if we already emitted a DIE for it, don't emit a second
19193      DIE for it again. Allow re-declarations of DECLs that are
19194      inside functions, though.  */
19195   if (old_die && declaration && !local_scope_p (context_die))
19196     return;
19197
19198   /* For static data members, the declaration in the class is supposed
19199      to have DW_TAG_member tag; the specification should still be
19200      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19201   if (declaration && class_scope_p (context_die))
19202     var_die = new_die (DW_TAG_member, context_die, decl);
19203   else
19204     var_die = new_die (DW_TAG_variable, context_die, decl);
19205
19206   origin_die = NULL;
19207   if (origin != NULL)
19208     origin_die = add_abstract_origin_attribute (var_die, origin);
19209
19210   /* Loop unrolling can create multiple blocks that refer to the same
19211      static variable, so we must test for the DW_AT_declaration flag.
19212
19213      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19214      copy decls and set the DECL_ABSTRACT flag on them instead of
19215      sharing them.
19216
19217      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19218
19219      ??? The declare_in_namespace support causes us to get two DIEs for one
19220      variable, both of which are declarations.  We want to avoid considering
19221      one to be a specification, so we must test that this DIE is not a
19222      declaration.  */
19223   else if (old_die && TREE_STATIC (decl) && ! declaration
19224            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19225     {
19226       /* This is a definition of a C++ class level static.  */
19227       add_AT_specification (var_die, old_die);
19228       specialization_p = true;
19229       if (DECL_NAME (decl))
19230         {
19231           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19232           struct dwarf_file_data * file_index = lookup_filename (s.file);
19233
19234           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19235             add_AT_file (var_die, DW_AT_decl_file, file_index);
19236
19237           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19238             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19239
19240           if (old_die->die_tag == DW_TAG_member)
19241             add_linkage_name (var_die, decl);
19242         }
19243     }
19244   else
19245     add_name_and_src_coords_attributes (var_die, decl);
19246
19247   if ((origin == NULL && !specialization_p)
19248       || (origin != NULL
19249           && !DECL_ABSTRACT (decl_or_origin)
19250           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19251                                        decl_function_context
19252                                                         (decl_or_origin))))
19253     {
19254       tree type = TREE_TYPE (decl_or_origin);
19255
19256       if (decl_by_reference_p (decl_or_origin))
19257         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19258       else
19259         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19260                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19261     }
19262
19263   if (origin == NULL && !specialization_p)
19264     {
19265       if (TREE_PUBLIC (decl))
19266         add_AT_flag (var_die, DW_AT_external, 1);
19267
19268       if (DECL_ARTIFICIAL (decl))
19269         add_AT_flag (var_die, DW_AT_artificial, 1);
19270
19271       add_accessibility_attribute (var_die, decl);
19272     }
19273
19274   if (declaration)
19275     add_AT_flag (var_die, DW_AT_declaration, 1);
19276
19277   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19278     equate_decl_number_to_die (decl, var_die);
19279
19280   if (! declaration
19281       && (! DECL_ABSTRACT (decl_or_origin)
19282           /* Local static vars are shared between all clones/inlines,
19283              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19284              already set.  */
19285           || (TREE_CODE (decl_or_origin) == VAR_DECL
19286               && TREE_STATIC (decl_or_origin)
19287               && DECL_RTL_SET_P (decl_or_origin)))
19288       /* When abstract origin already has DW_AT_location attribute, no need
19289          to add it again.  */
19290       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19291     {
19292       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19293           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19294         defer_location (decl_or_origin, var_die);
19295       else
19296         add_location_or_const_value_attribute (var_die,
19297                                                decl_or_origin,
19298                                                DW_AT_location);
19299       add_pubname (decl_or_origin, var_die);
19300     }
19301   else
19302     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19303 }
19304
19305 /* Generate a DIE to represent a named constant.  */
19306
19307 static void
19308 gen_const_die (tree decl, dw_die_ref context_die)
19309 {
19310   dw_die_ref const_die;
19311   tree type = TREE_TYPE (decl);
19312
19313   const_die = new_die (DW_TAG_constant, context_die, decl);
19314   add_name_and_src_coords_attributes (const_die, decl);
19315   add_type_attribute (const_die, type, 1, 0, context_die);
19316   if (TREE_PUBLIC (decl))
19317     add_AT_flag (const_die, DW_AT_external, 1);
19318   if (DECL_ARTIFICIAL (decl))
19319     add_AT_flag (const_die, DW_AT_artificial, 1);
19320   tree_add_const_value_attribute_for_decl (const_die, decl);
19321 }
19322
19323 /* Generate a DIE to represent a label identifier.  */
19324
19325 static void
19326 gen_label_die (tree decl, dw_die_ref context_die)
19327 {
19328   tree origin = decl_ultimate_origin (decl);
19329   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19330   rtx insn;
19331   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19332
19333   if (origin != NULL)
19334     add_abstract_origin_attribute (lbl_die, origin);
19335   else
19336     add_name_and_src_coords_attributes (lbl_die, decl);
19337
19338   if (DECL_ABSTRACT (decl))
19339     equate_decl_number_to_die (decl, lbl_die);
19340   else
19341     {
19342       insn = DECL_RTL_IF_SET (decl);
19343
19344       /* Deleted labels are programmer specified labels which have been
19345          eliminated because of various optimizations.  We still emit them
19346          here so that it is possible to put breakpoints on them.  */
19347       if (insn
19348           && (LABEL_P (insn)
19349               || ((NOTE_P (insn)
19350                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19351         {
19352           /* When optimization is enabled (via -O) some parts of the compiler
19353              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19354              represent source-level labels which were explicitly declared by
19355              the user.  This really shouldn't be happening though, so catch
19356              it if it ever does happen.  */
19357           gcc_assert (!INSN_DELETED_P (insn));
19358
19359           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19360           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19361         }
19362     }
19363 }
19364
19365 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19366    attributes to the DIE for a block STMT, to describe where the inlined
19367    function was called from.  This is similar to add_src_coords_attributes.  */
19368
19369 static inline void
19370 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19371 {
19372   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19373
19374   if (dwarf_version >= 3 || !dwarf_strict)
19375     {
19376       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19377       add_AT_unsigned (die, DW_AT_call_line, s.line);
19378     }
19379 }
19380
19381
19382 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19383    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19384
19385 static inline void
19386 add_high_low_attributes (tree stmt, dw_die_ref die)
19387 {
19388   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19389
19390   if (BLOCK_FRAGMENT_CHAIN (stmt)
19391       && (dwarf_version >= 3 || !dwarf_strict))
19392     {
19393       tree chain;
19394
19395       if (inlined_function_outer_scope_p (stmt))
19396         {
19397           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19398                                        BLOCK_NUMBER (stmt));
19399           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19400         }
19401
19402       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19403
19404       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19405       do
19406         {
19407           add_ranges (chain);
19408           chain = BLOCK_FRAGMENT_CHAIN (chain);
19409         }
19410       while (chain);
19411       add_ranges (NULL);
19412     }
19413   else
19414     {
19415       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19416                                    BLOCK_NUMBER (stmt));
19417       add_AT_lbl_id (die, DW_AT_low_pc, label);
19418       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19419                                    BLOCK_NUMBER (stmt));
19420       add_AT_lbl_id (die, DW_AT_high_pc, label);
19421     }
19422 }
19423
19424 /* Generate a DIE for a lexical block.  */
19425
19426 static void
19427 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19428 {
19429   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19430
19431   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19432     add_high_low_attributes (stmt, stmt_die);
19433
19434   decls_for_scope (stmt, stmt_die, depth);
19435 }
19436
19437 /* Generate a DIE for an inlined subprogram.  */
19438
19439 static void
19440 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19441 {
19442   tree decl;
19443
19444   /* The instance of function that is effectively being inlined shall not
19445      be abstract.  */
19446   gcc_assert (! BLOCK_ABSTRACT (stmt));
19447
19448   decl = block_ultimate_origin (stmt);
19449
19450   /* Emit info for the abstract instance first, if we haven't yet.  We
19451      must emit this even if the block is abstract, otherwise when we
19452      emit the block below (or elsewhere), we may end up trying to emit
19453      a die whose origin die hasn't been emitted, and crashing.  */
19454   dwarf2out_abstract_function (decl);
19455
19456   if (! BLOCK_ABSTRACT (stmt))
19457     {
19458       dw_die_ref subr_die
19459         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19460
19461       add_abstract_origin_attribute (subr_die, decl);
19462       if (TREE_ASM_WRITTEN (stmt))
19463         add_high_low_attributes (stmt, subr_die);
19464       add_call_src_coords_attributes (stmt, subr_die);
19465
19466       decls_for_scope (stmt, subr_die, depth);
19467       current_function_has_inlines = 1;
19468     }
19469 }
19470
19471 /* Generate a DIE for a field in a record, or structure.  */
19472
19473 static void
19474 gen_field_die (tree decl, dw_die_ref context_die)
19475 {
19476   dw_die_ref decl_die;
19477
19478   if (TREE_TYPE (decl) == error_mark_node)
19479     return;
19480
19481   decl_die = new_die (DW_TAG_member, context_die, decl);
19482   add_name_and_src_coords_attributes (decl_die, decl);
19483   add_type_attribute (decl_die, member_declared_type (decl),
19484                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19485                       context_die);
19486
19487   if (DECL_BIT_FIELD_TYPE (decl))
19488     {
19489       add_byte_size_attribute (decl_die, decl);
19490       add_bit_size_attribute (decl_die, decl);
19491       add_bit_offset_attribute (decl_die, decl);
19492     }
19493
19494   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19495     add_data_member_location_attribute (decl_die, decl);
19496
19497   if (DECL_ARTIFICIAL (decl))
19498     add_AT_flag (decl_die, DW_AT_artificial, 1);
19499
19500   add_accessibility_attribute (decl_die, decl);
19501
19502   /* Equate decl number to die, so that we can look up this decl later on.  */
19503   equate_decl_number_to_die (decl, decl_die);
19504 }
19505
19506 #if 0
19507 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19508    Use modified_type_die instead.
19509    We keep this code here just in case these types of DIEs may be needed to
19510    represent certain things in other languages (e.g. Pascal) someday.  */
19511
19512 static void
19513 gen_pointer_type_die (tree type, dw_die_ref context_die)
19514 {
19515   dw_die_ref ptr_die
19516     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19517
19518   equate_type_number_to_die (type, ptr_die);
19519   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19520   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19521 }
19522
19523 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19524    Use modified_type_die instead.
19525    We keep this code here just in case these types of DIEs may be needed to
19526    represent certain things in other languages (e.g. Pascal) someday.  */
19527
19528 static void
19529 gen_reference_type_die (tree type, dw_die_ref context_die)
19530 {
19531   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19532
19533   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19534     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19535   else
19536     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19537
19538   equate_type_number_to_die (type, ref_die);
19539   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19540   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19541 }
19542 #endif
19543
19544 /* Generate a DIE for a pointer to a member type.  */
19545
19546 static void
19547 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19548 {
19549   dw_die_ref ptr_die
19550     = new_die (DW_TAG_ptr_to_member_type,
19551                scope_die_for (type, context_die), type);
19552
19553   equate_type_number_to_die (type, ptr_die);
19554   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19555                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19556   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19557 }
19558
19559 /* Generate the DIE for the compilation unit.  */
19560
19561 static dw_die_ref
19562 gen_compile_unit_die (const char *filename)
19563 {
19564   dw_die_ref die;
19565   char producer[250];
19566   const char *language_string = lang_hooks.name;
19567   int language;
19568
19569   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19570
19571   if (filename)
19572     {
19573       add_name_attribute (die, filename);
19574       /* Don't add cwd for <built-in>.  */
19575       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19576         add_comp_dir_attribute (die);
19577     }
19578
19579   sprintf (producer, "%s %s", language_string, version_string);
19580
19581 #ifdef MIPS_DEBUGGING_INFO
19582   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19583      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19584      not appear in the producer string, the debugger reaches the conclusion
19585      that the object file is stripped and has no debugging information.
19586      To get the MIPS/SGI debugger to believe that there is debugging
19587      information in the object file, we add a -g to the producer string.  */
19588   if (debug_info_level > DINFO_LEVEL_TERSE)
19589     strcat (producer, " -g");
19590 #endif
19591
19592   add_AT_string (die, DW_AT_producer, producer);
19593
19594   language = DW_LANG_C89;
19595   if (strcmp (language_string, "GNU C++") == 0)
19596     language = DW_LANG_C_plus_plus;
19597   else if (strcmp (language_string, "GNU F77") == 0)
19598     language = DW_LANG_Fortran77;
19599   else if (strcmp (language_string, "GNU Pascal") == 0)
19600     language = DW_LANG_Pascal83;
19601   else if (dwarf_version >= 3 || !dwarf_strict)
19602     {
19603       if (strcmp (language_string, "GNU Ada") == 0)
19604         language = DW_LANG_Ada95;
19605       else if (strcmp (language_string, "GNU Fortran") == 0)
19606         language = DW_LANG_Fortran95;
19607       else if (strcmp (language_string, "GNU Java") == 0)
19608         language = DW_LANG_Java;
19609       else if (strcmp (language_string, "GNU Objective-C") == 0)
19610         language = DW_LANG_ObjC;
19611       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19612         language = DW_LANG_ObjC_plus_plus;
19613     }
19614
19615   add_AT_unsigned (die, DW_AT_language, language);
19616
19617   switch (language)
19618     {
19619     case DW_LANG_Fortran77:
19620     case DW_LANG_Fortran90:
19621     case DW_LANG_Fortran95:
19622       /* Fortran has case insensitive identifiers and the front-end
19623          lowercases everything.  */
19624       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19625       break;
19626     default:
19627       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19628       break;
19629     }
19630   return die;
19631 }
19632
19633 /* Generate the DIE for a base class.  */
19634
19635 static void
19636 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19637 {
19638   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19639
19640   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19641   add_data_member_location_attribute (die, binfo);
19642
19643   if (BINFO_VIRTUAL_P (binfo))
19644     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19645
19646   if (access == access_public_node)
19647     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19648   else if (access == access_protected_node)
19649     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19650 }
19651
19652 /* Generate a DIE for a class member.  */
19653
19654 static void
19655 gen_member_die (tree type, dw_die_ref context_die)
19656 {
19657   tree member;
19658   tree binfo = TYPE_BINFO (type);
19659   dw_die_ref child;
19660
19661   /* If this is not an incomplete type, output descriptions of each of its
19662      members. Note that as we output the DIEs necessary to represent the
19663      members of this record or union type, we will also be trying to output
19664      DIEs to represent the *types* of those members. However the `type'
19665      function (above) will specifically avoid generating type DIEs for member
19666      types *within* the list of member DIEs for this (containing) type except
19667      for those types (of members) which are explicitly marked as also being
19668      members of this (containing) type themselves.  The g++ front- end can
19669      force any given type to be treated as a member of some other (containing)
19670      type by setting the TYPE_CONTEXT of the given (member) type to point to
19671      the TREE node representing the appropriate (containing) type.  */
19672
19673   /* First output info about the base classes.  */
19674   if (binfo)
19675     {
19676       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19677       int i;
19678       tree base;
19679
19680       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19681         gen_inheritance_die (base,
19682                              (accesses ? VEC_index (tree, accesses, i)
19683                               : access_public_node), context_die);
19684     }
19685
19686   /* Now output info about the data members and type members.  */
19687   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19688     {
19689       /* If we thought we were generating minimal debug info for TYPE
19690          and then changed our minds, some of the member declarations
19691          may have already been defined.  Don't define them again, but
19692          do put them in the right order.  */
19693
19694       child = lookup_decl_die (member);
19695       if (child)
19696         splice_child_die (context_die, child);
19697       else
19698         gen_decl_die (member, NULL, context_die);
19699     }
19700
19701   /* Now output info about the function members (if any).  */
19702   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19703     {
19704       /* Don't include clones in the member list.  */
19705       if (DECL_ABSTRACT_ORIGIN (member))
19706         continue;
19707
19708       child = lookup_decl_die (member);
19709       if (child)
19710         splice_child_die (context_die, child);
19711       else
19712         gen_decl_die (member, NULL, context_die);
19713     }
19714 }
19715
19716 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19717    is set, we pretend that the type was never defined, so we only get the
19718    member DIEs needed by later specification DIEs.  */
19719
19720 static void
19721 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19722                                 enum debug_info_usage usage)
19723 {
19724   dw_die_ref type_die = lookup_type_die (type);
19725   dw_die_ref scope_die = 0;
19726   int nested = 0;
19727   int complete = (TYPE_SIZE (type)
19728                   && (! TYPE_STUB_DECL (type)
19729                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19730   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19731   complete = complete && should_emit_struct_debug (type, usage);
19732
19733   if (type_die && ! complete)
19734     return;
19735
19736   if (TYPE_CONTEXT (type) != NULL_TREE
19737       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19738           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19739     nested = 1;
19740
19741   scope_die = scope_die_for (type, context_die);
19742
19743   if (! type_die || (nested && scope_die == comp_unit_die))
19744     /* First occurrence of type or toplevel definition of nested class.  */
19745     {
19746       dw_die_ref old_die = type_die;
19747
19748       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19749                           ? record_type_tag (type) : DW_TAG_union_type,
19750                           scope_die, type);
19751       equate_type_number_to_die (type, type_die);
19752       if (old_die)
19753         add_AT_specification (type_die, old_die);
19754       else
19755         add_name_attribute (type_die, type_tag (type));
19756     }
19757   else
19758     remove_AT (type_die, DW_AT_declaration);
19759
19760   /* Generate child dies for template paramaters.  */
19761   if (debug_info_level > DINFO_LEVEL_TERSE
19762       && COMPLETE_TYPE_P (type))
19763     gen_generic_params_dies (type);
19764
19765   /* If this type has been completed, then give it a byte_size attribute and
19766      then give a list of members.  */
19767   if (complete && !ns_decl)
19768     {
19769       /* Prevent infinite recursion in cases where the type of some member of
19770          this type is expressed in terms of this type itself.  */
19771       TREE_ASM_WRITTEN (type) = 1;
19772       add_byte_size_attribute (type_die, type);
19773       if (TYPE_STUB_DECL (type) != NULL_TREE)
19774         {
19775           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19776           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19777         }
19778
19779       /* If the first reference to this type was as the return type of an
19780          inline function, then it may not have a parent.  Fix this now.  */
19781       if (type_die->die_parent == NULL)
19782         add_child_die (scope_die, type_die);
19783
19784       push_decl_scope (type);
19785       gen_member_die (type, type_die);
19786       pop_decl_scope ();
19787
19788       /* GNU extension: Record what type our vtable lives in.  */
19789       if (TYPE_VFIELD (type))
19790         {
19791           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19792
19793           gen_type_die (vtype, context_die);
19794           add_AT_die_ref (type_die, DW_AT_containing_type,
19795                           lookup_type_die (vtype));
19796         }
19797     }
19798   else
19799     {
19800       add_AT_flag (type_die, DW_AT_declaration, 1);
19801
19802       /* We don't need to do this for function-local types.  */
19803       if (TYPE_STUB_DECL (type)
19804           && ! decl_function_context (TYPE_STUB_DECL (type)))
19805         VEC_safe_push (tree, gc, incomplete_types, type);
19806     }
19807
19808   if (get_AT (type_die, DW_AT_name))
19809     add_pubtype (type, type_die);
19810 }
19811
19812 /* Generate a DIE for a subroutine _type_.  */
19813
19814 static void
19815 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19816 {
19817   tree return_type = TREE_TYPE (type);
19818   dw_die_ref subr_die
19819     = new_die (DW_TAG_subroutine_type,
19820                scope_die_for (type, context_die), type);
19821
19822   equate_type_number_to_die (type, subr_die);
19823   add_prototyped_attribute (subr_die, type);
19824   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19825   gen_formal_types_die (type, subr_die);
19826
19827   if (get_AT (subr_die, DW_AT_name))
19828     add_pubtype (type, subr_die);
19829 }
19830
19831 /* Generate a DIE for a type definition.  */
19832
19833 static void
19834 gen_typedef_die (tree decl, dw_die_ref context_die)
19835 {
19836   dw_die_ref type_die;
19837   tree origin;
19838
19839   if (TREE_ASM_WRITTEN (decl))
19840     return;
19841
19842   TREE_ASM_WRITTEN (decl) = 1;
19843   type_die = new_die (DW_TAG_typedef, context_die, decl);
19844   origin = decl_ultimate_origin (decl);
19845   if (origin != NULL)
19846     add_abstract_origin_attribute (type_die, origin);
19847   else
19848     {
19849       tree type;
19850
19851       add_name_and_src_coords_attributes (type_die, decl);
19852       if (DECL_ORIGINAL_TYPE (decl))
19853         {
19854           type = DECL_ORIGINAL_TYPE (decl);
19855
19856           gcc_assert (type != TREE_TYPE (decl));
19857           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19858         }
19859       else
19860         {
19861           type = TREE_TYPE (decl);
19862
19863           if (is_naming_typedef_decl (TYPE_NAME (type)))
19864             {
19865               /* Here, we are in the case of decl being a typedef naming
19866                  an anonymous type, e.g:
19867                      typedef struct {...} foo;
19868                  In that case TREE_TYPE (decl) is not a typedef variant
19869                  type and TYPE_NAME of the anonymous type is set to the
19870                  TYPE_DECL of the typedef. This construct is emitted by
19871                  the C++ FE.
19872
19873                  TYPE is the anonymous struct named by the typedef
19874                  DECL. As we need the DW_AT_type attribute of the
19875                  DW_TAG_typedef to point to the DIE of TYPE, let's
19876                  generate that DIE right away. add_type_attribute
19877                  called below will then pick (via lookup_type_die) that
19878                  anonymous struct DIE.  */
19879               if (!TREE_ASM_WRITTEN (type))
19880                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19881             }
19882         }
19883
19884       add_type_attribute (type_die, type, TREE_READONLY (decl),
19885                           TREE_THIS_VOLATILE (decl), context_die);
19886
19887       if (is_naming_typedef_decl (decl))
19888         /* We want that all subsequent calls to lookup_type_die with
19889            TYPE in argument yield the DW_TAG_typedef we have just
19890            created.  */
19891         equate_type_number_to_die (type, type_die);
19892
19893       add_accessibility_attribute (type_die, decl);
19894     }
19895
19896   if (DECL_ABSTRACT (decl))
19897     equate_decl_number_to_die (decl, type_die);
19898
19899   if (get_AT (type_die, DW_AT_name))
19900     add_pubtype (decl, type_die);
19901 }
19902
19903 /* Generate a DIE for a struct, class, enum or union type.  */
19904
19905 static void
19906 gen_tagged_type_die (tree type,
19907                      dw_die_ref context_die,
19908                      enum debug_info_usage usage)
19909 {
19910   int need_pop;
19911
19912   if (type == NULL_TREE
19913       || !is_tagged_type (type))
19914     return;
19915
19916   /* If this is a nested type whose containing class hasn't been written
19917      out yet, writing it out will cover this one, too.  This does not apply
19918      to instantiations of member class templates; they need to be added to
19919      the containing class as they are generated.  FIXME: This hurts the
19920      idea of combining type decls from multiple TUs, since we can't predict
19921      what set of template instantiations we'll get.  */
19922   if (TYPE_CONTEXT (type)
19923       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19924       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19925     {
19926       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19927
19928       if (TREE_ASM_WRITTEN (type))
19929         return;
19930
19931       /* If that failed, attach ourselves to the stub.  */
19932       push_decl_scope (TYPE_CONTEXT (type));
19933       context_die = lookup_type_die (TYPE_CONTEXT (type));
19934       need_pop = 1;
19935     }
19936   else if (TYPE_CONTEXT (type) != NULL_TREE
19937            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19938     {
19939       /* If this type is local to a function that hasn't been written
19940          out yet, use a NULL context for now; it will be fixed up in
19941          decls_for_scope.  */
19942       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19943       need_pop = 0;
19944     }
19945   else
19946     {
19947       context_die = declare_in_namespace (type, context_die);
19948       need_pop = 0;
19949     }
19950
19951   if (TREE_CODE (type) == ENUMERAL_TYPE)
19952     {
19953       /* This might have been written out by the call to
19954          declare_in_namespace.  */
19955       if (!TREE_ASM_WRITTEN (type))
19956         gen_enumeration_type_die (type, context_die);
19957     }
19958   else
19959     gen_struct_or_union_type_die (type, context_die, usage);
19960
19961   if (need_pop)
19962     pop_decl_scope ();
19963
19964   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19965      it up if it is ever completed.  gen_*_type_die will set it for us
19966      when appropriate.  */
19967 }
19968
19969 /* Generate a type description DIE.  */
19970
19971 static void
19972 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19973                                 enum debug_info_usage usage)
19974 {
19975   struct array_descr_info info;
19976
19977   if (type == NULL_TREE || type == error_mark_node)
19978     return;
19979
19980   /* If TYPE is a typedef type variant, let's generate debug info
19981      for the parent typedef which TYPE is a type of.  */
19982   if (typedef_variant_p (type))
19983     {
19984       if (TREE_ASM_WRITTEN (type))
19985         return;
19986
19987       /* Prevent broken recursion; we can't hand off to the same type.  */
19988       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19989
19990       /* Use the DIE of the containing namespace as the parent DIE of
19991          the type description DIE we want to generate.  */
19992       if (DECL_CONTEXT (TYPE_NAME (type))
19993           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19994         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19995
19996       TREE_ASM_WRITTEN (type) = 1;
19997
19998       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19999       return;
20000     }
20001
20002   /* If type is an anonymous tagged type named by a typedef, let's
20003      generate debug info for the typedef.  */
20004   if (is_naming_typedef_decl (TYPE_NAME (type)))
20005     {
20006       /* Use the DIE of the containing namespace as the parent DIE of
20007          the type description DIE we want to generate.  */
20008       if (DECL_CONTEXT (TYPE_NAME (type))
20009           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20010         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20011       
20012       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20013       return;
20014     }
20015
20016   /* If this is an array type with hidden descriptor, handle it first.  */
20017   if (!TREE_ASM_WRITTEN (type)
20018       && lang_hooks.types.get_array_descr_info
20019       && lang_hooks.types.get_array_descr_info (type, &info)
20020       && (dwarf_version >= 3 || !dwarf_strict))
20021     {
20022       gen_descr_array_type_die (type, &info, context_die);
20023       TREE_ASM_WRITTEN (type) = 1;
20024       return;
20025     }
20026
20027   /* We are going to output a DIE to represent the unqualified version
20028      of this type (i.e. without any const or volatile qualifiers) so
20029      get the main variant (i.e. the unqualified version) of this type
20030      now.  (Vectors are special because the debugging info is in the
20031      cloned type itself).  */
20032   if (TREE_CODE (type) != VECTOR_TYPE)
20033     type = type_main_variant (type);
20034
20035   if (TREE_ASM_WRITTEN (type))
20036     return;
20037
20038   switch (TREE_CODE (type))
20039     {
20040     case ERROR_MARK:
20041       break;
20042
20043     case POINTER_TYPE:
20044     case REFERENCE_TYPE:
20045       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20046          ensures that the gen_type_die recursion will terminate even if the
20047          type is recursive.  Recursive types are possible in Ada.  */
20048       /* ??? We could perhaps do this for all types before the switch
20049          statement.  */
20050       TREE_ASM_WRITTEN (type) = 1;
20051
20052       /* For these types, all that is required is that we output a DIE (or a
20053          set of DIEs) to represent the "basis" type.  */
20054       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20055                                 DINFO_USAGE_IND_USE);
20056       break;
20057
20058     case OFFSET_TYPE:
20059       /* This code is used for C++ pointer-to-data-member types.
20060          Output a description of the relevant class type.  */
20061       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20062                                         DINFO_USAGE_IND_USE);
20063
20064       /* Output a description of the type of the object pointed to.  */
20065       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20066                                         DINFO_USAGE_IND_USE);
20067
20068       /* Now output a DIE to represent this pointer-to-data-member type
20069          itself.  */
20070       gen_ptr_to_mbr_type_die (type, context_die);
20071       break;
20072
20073     case FUNCTION_TYPE:
20074       /* Force out return type (in case it wasn't forced out already).  */
20075       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20076                                         DINFO_USAGE_DIR_USE);
20077       gen_subroutine_type_die (type, context_die);
20078       break;
20079
20080     case METHOD_TYPE:
20081       /* Force out return type (in case it wasn't forced out already).  */
20082       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20083                                         DINFO_USAGE_DIR_USE);
20084       gen_subroutine_type_die (type, context_die);
20085       break;
20086
20087     case ARRAY_TYPE:
20088       gen_array_type_die (type, context_die);
20089       break;
20090
20091     case VECTOR_TYPE:
20092       gen_array_type_die (type, context_die);
20093       break;
20094
20095     case ENUMERAL_TYPE:
20096     case RECORD_TYPE:
20097     case UNION_TYPE:
20098     case QUAL_UNION_TYPE:
20099       gen_tagged_type_die (type, context_die, usage);
20100       return;
20101
20102     case VOID_TYPE:
20103     case INTEGER_TYPE:
20104     case REAL_TYPE:
20105     case FIXED_POINT_TYPE:
20106     case COMPLEX_TYPE:
20107     case BOOLEAN_TYPE:
20108       /* No DIEs needed for fundamental types.  */
20109       break;
20110
20111     case LANG_TYPE:
20112       /* Just use DW_TAG_unspecified_type.  */
20113       {
20114         dw_die_ref type_die = lookup_type_die (type);
20115         if (type_die == NULL)
20116           {
20117             tree name = TYPE_NAME (type);
20118             if (TREE_CODE (name) == TYPE_DECL)
20119               name = DECL_NAME (name);
20120             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20121             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20122             equate_type_number_to_die (type, type_die);
20123           }
20124       }
20125       break;
20126
20127     default:
20128       gcc_unreachable ();
20129     }
20130
20131   TREE_ASM_WRITTEN (type) = 1;
20132 }
20133
20134 static void
20135 gen_type_die (tree type, dw_die_ref context_die)
20136 {
20137   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20138 }
20139
20140 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20141    things which are local to the given block.  */
20142
20143 static void
20144 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20145 {
20146   int must_output_die = 0;
20147   bool inlined_func;
20148
20149   /* Ignore blocks that are NULL.  */
20150   if (stmt == NULL_TREE)
20151     return;
20152
20153   inlined_func = inlined_function_outer_scope_p (stmt);
20154
20155   /* If the block is one fragment of a non-contiguous block, do not
20156      process the variables, since they will have been done by the
20157      origin block.  Do process subblocks.  */
20158   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20159     {
20160       tree sub;
20161
20162       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20163         gen_block_die (sub, context_die, depth + 1);
20164
20165       return;
20166     }
20167
20168   /* Determine if we need to output any Dwarf DIEs at all to represent this
20169      block.  */
20170   if (inlined_func)
20171     /* The outer scopes for inlinings *must* always be represented.  We
20172        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20173     must_output_die = 1;
20174   else
20175     {
20176       /* Determine if this block directly contains any "significant"
20177          local declarations which we will need to output DIEs for.  */
20178       if (debug_info_level > DINFO_LEVEL_TERSE)
20179         /* We are not in terse mode so *any* local declaration counts
20180            as being a "significant" one.  */
20181         must_output_die = ((BLOCK_VARS (stmt) != NULL
20182                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20183                            && (TREE_USED (stmt)
20184                                || TREE_ASM_WRITTEN (stmt)
20185                                || BLOCK_ABSTRACT (stmt)));
20186       else if ((TREE_USED (stmt)
20187                 || TREE_ASM_WRITTEN (stmt)
20188                 || BLOCK_ABSTRACT (stmt))
20189                && !dwarf2out_ignore_block (stmt))
20190         must_output_die = 1;
20191     }
20192
20193   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20194      DIE for any block which contains no significant local declarations at
20195      all.  Rather, in such cases we just call `decls_for_scope' so that any
20196      needed Dwarf info for any sub-blocks will get properly generated. Note
20197      that in terse mode, our definition of what constitutes a "significant"
20198      local declaration gets restricted to include only inlined function
20199      instances and local (nested) function definitions.  */
20200   if (must_output_die)
20201     {
20202       if (inlined_func)
20203         {
20204           /* If STMT block is abstract, that means we have been called
20205              indirectly from dwarf2out_abstract_function.
20206              That function rightfully marks the descendent blocks (of
20207              the abstract function it is dealing with) as being abstract,
20208              precisely to prevent us from emitting any
20209              DW_TAG_inlined_subroutine DIE as a descendent
20210              of an abstract function instance. So in that case, we should
20211              not call gen_inlined_subroutine_die.
20212
20213              Later though, when cgraph asks dwarf2out to emit info
20214              for the concrete instance of the function decl into which
20215              the concrete instance of STMT got inlined, the later will lead
20216              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20217           if (! BLOCK_ABSTRACT (stmt))
20218             gen_inlined_subroutine_die (stmt, context_die, depth);
20219         }
20220       else
20221         gen_lexical_block_die (stmt, context_die, depth);
20222     }
20223   else
20224     decls_for_scope (stmt, context_die, depth);
20225 }
20226
20227 /* Process variable DECL (or variable with origin ORIGIN) within
20228    block STMT and add it to CONTEXT_DIE.  */
20229 static void
20230 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20231 {
20232   dw_die_ref die;
20233   tree decl_or_origin = decl ? decl : origin;
20234
20235   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20236     die = lookup_decl_die (decl_or_origin);
20237   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20238            && TYPE_DECL_IS_STUB (decl_or_origin))
20239     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20240   else
20241     die = NULL;
20242
20243   if (die != NULL && die->die_parent == NULL)
20244     add_child_die (context_die, die);
20245   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20246     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20247                                          stmt, context_die);
20248   else
20249     gen_decl_die (decl, origin, context_die);
20250 }
20251
20252 /* Generate all of the decls declared within a given scope and (recursively)
20253    all of its sub-blocks.  */
20254
20255 static void
20256 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20257 {
20258   tree decl;
20259   unsigned int i;
20260   tree subblocks;
20261
20262   /* Ignore NULL blocks.  */
20263   if (stmt == NULL_TREE)
20264     return;
20265
20266   /* Output the DIEs to represent all of the data objects and typedefs
20267      declared directly within this block but not within any nested
20268      sub-blocks.  Also, nested function and tag DIEs have been
20269      generated with a parent of NULL; fix that up now.  */
20270   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20271     process_scope_var (stmt, decl, NULL_TREE, context_die);
20272   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20273     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20274                        context_die);
20275
20276   /* If we're at -g1, we're not interested in subblocks.  */
20277   if (debug_info_level <= DINFO_LEVEL_TERSE)
20278     return;
20279
20280   /* Output the DIEs to represent all sub-blocks (and the items declared
20281      therein) of this block.  */
20282   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20283        subblocks != NULL;
20284        subblocks = BLOCK_CHAIN (subblocks))
20285     gen_block_die (subblocks, context_die, depth + 1);
20286 }
20287
20288 /* Is this a typedef we can avoid emitting?  */
20289
20290 static inline int
20291 is_redundant_typedef (const_tree decl)
20292 {
20293   if (TYPE_DECL_IS_STUB (decl))
20294     return 1;
20295
20296   if (DECL_ARTIFICIAL (decl)
20297       && DECL_CONTEXT (decl)
20298       && is_tagged_type (DECL_CONTEXT (decl))
20299       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20300       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20301     /* Also ignore the artificial member typedef for the class name.  */
20302     return 1;
20303
20304   return 0;
20305 }
20306
20307 /* Return TRUE if TYPE is a typedef that names a type for linkage
20308    purposes. This kind of typedefs is produced by the C++ FE for
20309    constructs like:
20310
20311    typedef struct {...} foo;
20312
20313    In that case, there is no typedef variant type produced for foo.
20314    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20315    struct type.  */
20316
20317 static bool
20318 is_naming_typedef_decl (const_tree decl)
20319 {
20320   if (decl == NULL_TREE
20321       || TREE_CODE (decl) != TYPE_DECL
20322       || !is_tagged_type (TREE_TYPE (decl))
20323       || DECL_IS_BUILTIN (decl)
20324       || is_redundant_typedef (decl)
20325       /* It looks like Ada produces TYPE_DECLs that are very similar
20326          to C++ naming typedefs but that have different
20327          semantics. Let's be specific to c++ for now.  */
20328       || !is_cxx ())
20329     return FALSE;
20330
20331   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20332           && TYPE_NAME (TREE_TYPE (decl)) == decl
20333           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20334               != TYPE_NAME (TREE_TYPE (decl))));
20335 }
20336
20337 /* Returns the DIE for a context.  */
20338
20339 static inline dw_die_ref
20340 get_context_die (tree context)
20341 {
20342   if (context)
20343     {
20344       /* Find die that represents this context.  */
20345       if (TYPE_P (context))
20346         return force_type_die (TYPE_MAIN_VARIANT (context));
20347       else
20348         return force_decl_die (context);
20349     }
20350   return comp_unit_die;
20351 }
20352
20353 /* Returns the DIE for decl.  A DIE will always be returned.  */
20354
20355 static dw_die_ref
20356 force_decl_die (tree decl)
20357 {
20358   dw_die_ref decl_die;
20359   unsigned saved_external_flag;
20360   tree save_fn = NULL_TREE;
20361   decl_die = lookup_decl_die (decl);
20362   if (!decl_die)
20363     {
20364       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20365
20366       decl_die = lookup_decl_die (decl);
20367       if (decl_die)
20368         return decl_die;
20369
20370       switch (TREE_CODE (decl))
20371         {
20372         case FUNCTION_DECL:
20373           /* Clear current_function_decl, so that gen_subprogram_die thinks
20374              that this is a declaration. At this point, we just want to force
20375              declaration die.  */
20376           save_fn = current_function_decl;
20377           current_function_decl = NULL_TREE;
20378           gen_subprogram_die (decl, context_die);
20379           current_function_decl = save_fn;
20380           break;
20381
20382         case VAR_DECL:
20383           /* Set external flag to force declaration die. Restore it after
20384            gen_decl_die() call.  */
20385           saved_external_flag = DECL_EXTERNAL (decl);
20386           DECL_EXTERNAL (decl) = 1;
20387           gen_decl_die (decl, NULL, context_die);
20388           DECL_EXTERNAL (decl) = saved_external_flag;
20389           break;
20390
20391         case NAMESPACE_DECL:
20392           if (dwarf_version >= 3 || !dwarf_strict)
20393             dwarf2out_decl (decl);
20394           else
20395             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20396             decl_die = comp_unit_die;
20397           break;
20398
20399         default:
20400           gcc_unreachable ();
20401         }
20402
20403       /* We should be able to find the DIE now.  */
20404       if (!decl_die)
20405         decl_die = lookup_decl_die (decl);
20406       gcc_assert (decl_die);
20407     }
20408
20409   return decl_die;
20410 }
20411
20412 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20413    always returned.  */
20414
20415 static dw_die_ref
20416 force_type_die (tree type)
20417 {
20418   dw_die_ref type_die;
20419
20420   type_die = lookup_type_die (type);
20421   if (!type_die)
20422     {
20423       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20424
20425       type_die = modified_type_die (type, TYPE_READONLY (type),
20426                                     TYPE_VOLATILE (type), context_die);
20427       gcc_assert (type_die);
20428     }
20429   return type_die;
20430 }
20431
20432 /* Force out any required namespaces to be able to output DECL,
20433    and return the new context_die for it, if it's changed.  */
20434
20435 static dw_die_ref
20436 setup_namespace_context (tree thing, dw_die_ref context_die)
20437 {
20438   tree context = (DECL_P (thing)
20439                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20440   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20441     /* Force out the namespace.  */
20442     context_die = force_decl_die (context);
20443
20444   return context_die;
20445 }
20446
20447 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20448    type) within its namespace, if appropriate.
20449
20450    For compatibility with older debuggers, namespace DIEs only contain
20451    declarations; all definitions are emitted at CU scope.  */
20452
20453 static dw_die_ref
20454 declare_in_namespace (tree thing, dw_die_ref context_die)
20455 {
20456   dw_die_ref ns_context;
20457
20458   if (debug_info_level <= DINFO_LEVEL_TERSE)
20459     return context_die;
20460
20461   /* If this decl is from an inlined function, then don't try to emit it in its
20462      namespace, as we will get confused.  It would have already been emitted
20463      when the abstract instance of the inline function was emitted anyways.  */
20464   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20465     return context_die;
20466
20467   ns_context = setup_namespace_context (thing, context_die);
20468
20469   if (ns_context != context_die)
20470     {
20471       if (is_fortran ())
20472         return ns_context;
20473       if (DECL_P (thing))
20474         gen_decl_die (thing, NULL, ns_context);
20475       else
20476         gen_type_die (thing, ns_context);
20477     }
20478   return context_die;
20479 }
20480
20481 /* Generate a DIE for a namespace or namespace alias.  */
20482
20483 static void
20484 gen_namespace_die (tree decl, dw_die_ref context_die)
20485 {
20486   dw_die_ref namespace_die;
20487
20488   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20489      they are an alias of.  */
20490   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20491     {
20492       /* Output a real namespace or module.  */
20493       context_die = setup_namespace_context (decl, comp_unit_die);
20494       namespace_die = new_die (is_fortran ()
20495                                ? DW_TAG_module : DW_TAG_namespace,
20496                                context_die, decl);
20497       /* For Fortran modules defined in different CU don't add src coords.  */
20498       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20499         {
20500           const char *name = dwarf2_name (decl, 0);
20501           if (name)
20502             add_name_attribute (namespace_die, name);
20503         }
20504       else
20505         add_name_and_src_coords_attributes (namespace_die, decl);
20506       if (DECL_EXTERNAL (decl))
20507         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20508       equate_decl_number_to_die (decl, namespace_die);
20509     }
20510   else
20511     {
20512       /* Output a namespace alias.  */
20513
20514       /* Force out the namespace we are an alias of, if necessary.  */
20515       dw_die_ref origin_die
20516         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20517
20518       if (DECL_CONTEXT (decl) == NULL_TREE
20519           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20520         context_die = setup_namespace_context (decl, comp_unit_die);
20521       /* Now create the namespace alias DIE.  */
20522       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20523       add_name_and_src_coords_attributes (namespace_die, decl);
20524       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20525       equate_decl_number_to_die (decl, namespace_die);
20526     }
20527 }
20528
20529 /* Generate Dwarf debug information for a decl described by DECL.
20530    The return value is currently only meaningful for PARM_DECLs,
20531    for all other decls it returns NULL.  */
20532
20533 static dw_die_ref
20534 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20535 {
20536   tree decl_or_origin = decl ? decl : origin;
20537   tree class_origin = NULL, ultimate_origin;
20538
20539   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20540     return NULL;
20541
20542   switch (TREE_CODE (decl_or_origin))
20543     {
20544     case ERROR_MARK:
20545       break;
20546
20547     case CONST_DECL:
20548       if (!is_fortran () && !is_ada ())
20549         {
20550           /* The individual enumerators of an enum type get output when we output
20551              the Dwarf representation of the relevant enum type itself.  */
20552           break;
20553         }
20554
20555       /* Emit its type.  */
20556       gen_type_die (TREE_TYPE (decl), context_die);
20557
20558       /* And its containing namespace.  */
20559       context_die = declare_in_namespace (decl, context_die);
20560
20561       gen_const_die (decl, context_die);
20562       break;
20563
20564     case FUNCTION_DECL:
20565       /* Don't output any DIEs to represent mere function declarations,
20566          unless they are class members or explicit block externs.  */
20567       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20568           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20569           && (current_function_decl == NULL_TREE
20570               || DECL_ARTIFICIAL (decl_or_origin)))
20571         break;
20572
20573 #if 0
20574       /* FIXME */
20575       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20576          on local redeclarations of global functions.  That seems broken.  */
20577       if (current_function_decl != decl)
20578         /* This is only a declaration.  */;
20579 #endif
20580
20581       /* If we're emitting a clone, emit info for the abstract instance.  */
20582       if (origin || DECL_ORIGIN (decl) != decl)
20583         dwarf2out_abstract_function (origin
20584                                      ? DECL_ORIGIN (origin)
20585                                      : DECL_ABSTRACT_ORIGIN (decl));
20586
20587       /* If we're emitting an out-of-line copy of an inline function,
20588          emit info for the abstract instance and set up to refer to it.  */
20589       else if (cgraph_function_possibly_inlined_p (decl)
20590                && ! DECL_ABSTRACT (decl)
20591                && ! class_or_namespace_scope_p (context_die)
20592                /* dwarf2out_abstract_function won't emit a die if this is just
20593                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20594                   that case, because that works only if we have a die.  */
20595                && DECL_INITIAL (decl) != NULL_TREE)
20596         {
20597           dwarf2out_abstract_function (decl);
20598           set_decl_origin_self (decl);
20599         }
20600
20601       /* Otherwise we're emitting the primary DIE for this decl.  */
20602       else if (debug_info_level > DINFO_LEVEL_TERSE)
20603         {
20604           /* Before we describe the FUNCTION_DECL itself, make sure that we
20605              have its containing type.  */
20606           if (!origin)
20607             origin = decl_class_context (decl);
20608           if (origin != NULL_TREE)
20609             gen_type_die (origin, context_die);
20610
20611           /* And its return type.  */
20612           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20613
20614           /* And its virtual context.  */
20615           if (DECL_VINDEX (decl) != NULL_TREE)
20616             gen_type_die (DECL_CONTEXT (decl), context_die);
20617
20618           /* Make sure we have a member DIE for decl.  */
20619           if (origin != NULL_TREE)
20620             gen_type_die_for_member (origin, decl, context_die);
20621
20622           /* And its containing namespace.  */
20623           context_die = declare_in_namespace (decl, context_die);
20624         }
20625
20626       /* Now output a DIE to represent the function itself.  */
20627       if (decl)
20628         gen_subprogram_die (decl, context_die);
20629       break;
20630
20631     case TYPE_DECL:
20632       /* If we are in terse mode, don't generate any DIEs to represent any
20633          actual typedefs.  */
20634       if (debug_info_level <= DINFO_LEVEL_TERSE)
20635         break;
20636
20637       /* In the special case of a TYPE_DECL node representing the declaration
20638          of some type tag, if the given TYPE_DECL is marked as having been
20639          instantiated from some other (original) TYPE_DECL node (e.g. one which
20640          was generated within the original definition of an inline function) we
20641          used to generate a special (abbreviated) DW_TAG_structure_type,
20642          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20643          should be actually referencing those DIEs, as variable DIEs with that
20644          type would be emitted already in the abstract origin, so it was always
20645          removed during unused type prunning.  Don't add anything in this
20646          case.  */
20647       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20648         break;
20649
20650       if (is_redundant_typedef (decl))
20651         gen_type_die (TREE_TYPE (decl), context_die);
20652       else
20653         /* Output a DIE to represent the typedef itself.  */
20654         gen_typedef_die (decl, context_die);
20655       break;
20656
20657     case LABEL_DECL:
20658       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20659         gen_label_die (decl, context_die);
20660       break;
20661
20662     case VAR_DECL:
20663     case RESULT_DECL:
20664       /* If we are in terse mode, don't generate any DIEs to represent any
20665          variable declarations or definitions.  */
20666       if (debug_info_level <= DINFO_LEVEL_TERSE)
20667         break;
20668
20669       /* Output any DIEs that are needed to specify the type of this data
20670          object.  */
20671       if (decl_by_reference_p (decl_or_origin))
20672         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20673       else
20674         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20675
20676       /* And its containing type.  */
20677       class_origin = decl_class_context (decl_or_origin);
20678       if (class_origin != NULL_TREE)
20679         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20680
20681       /* And its containing namespace.  */
20682       context_die = declare_in_namespace (decl_or_origin, context_die);
20683
20684       /* Now output the DIE to represent the data object itself.  This gets
20685          complicated because of the possibility that the VAR_DECL really
20686          represents an inlined instance of a formal parameter for an inline
20687          function.  */
20688       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20689       if (ultimate_origin != NULL_TREE
20690           && TREE_CODE (ultimate_origin) == PARM_DECL)
20691         gen_formal_parameter_die (decl, origin,
20692                                   true /* Emit name attribute.  */,
20693                                   context_die);
20694       else
20695         gen_variable_die (decl, origin, context_die);
20696       break;
20697
20698     case FIELD_DECL:
20699       /* Ignore the nameless fields that are used to skip bits but handle C++
20700          anonymous unions and structs.  */
20701       if (DECL_NAME (decl) != NULL_TREE
20702           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20703           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20704         {
20705           gen_type_die (member_declared_type (decl), context_die);
20706           gen_field_die (decl, context_die);
20707         }
20708       break;
20709
20710     case PARM_DECL:
20711       if (DECL_BY_REFERENCE (decl_or_origin))
20712         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20713       else
20714         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20715       return gen_formal_parameter_die (decl, origin,
20716                                        true /* Emit name attribute.  */,
20717                                        context_die);
20718
20719     case NAMESPACE_DECL:
20720     case IMPORTED_DECL:
20721       if (dwarf_version >= 3 || !dwarf_strict)
20722         gen_namespace_die (decl, context_die);
20723       break;
20724
20725     default:
20726       /* Probably some frontend-internal decl.  Assume we don't care.  */
20727       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20728       break;
20729     }
20730
20731   return NULL;
20732 }
20733 \f
20734 /* Output debug information for global decl DECL.  Called from toplev.c after
20735    compilation proper has finished.  */
20736
20737 static void
20738 dwarf2out_global_decl (tree decl)
20739 {
20740   /* Output DWARF2 information for file-scope tentative data object
20741      declarations, file-scope (extern) function declarations (which
20742      had no corresponding body) and file-scope tagged type declarations
20743      and definitions which have not yet been forced out.  */
20744   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20745     dwarf2out_decl (decl);
20746 }
20747
20748 /* Output debug information for type decl DECL.  Called from toplev.c
20749    and from language front ends (to record built-in types).  */
20750 static void
20751 dwarf2out_type_decl (tree decl, int local)
20752 {
20753   if (!local)
20754     dwarf2out_decl (decl);
20755 }
20756
20757 /* Output debug information for imported module or decl DECL.
20758    NAME is non-NULL name in the lexical block if the decl has been renamed.
20759    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20760    that DECL belongs to.
20761    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20762 static void
20763 dwarf2out_imported_module_or_decl_1 (tree decl,
20764                                      tree name,
20765                                      tree lexical_block,
20766                                      dw_die_ref lexical_block_die)
20767 {
20768   expanded_location xloc;
20769   dw_die_ref imported_die = NULL;
20770   dw_die_ref at_import_die;
20771
20772   if (TREE_CODE (decl) == IMPORTED_DECL)
20773     {
20774       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20775       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20776       gcc_assert (decl);
20777     }
20778   else
20779     xloc = expand_location (input_location);
20780
20781   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20782     {
20783       at_import_die = force_type_die (TREE_TYPE (decl));
20784       /* For namespace N { typedef void T; } using N::T; base_type_die
20785          returns NULL, but DW_TAG_imported_declaration requires
20786          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20787       if (!at_import_die)
20788         {
20789           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20790           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20791           at_import_die = lookup_type_die (TREE_TYPE (decl));
20792           gcc_assert (at_import_die);
20793         }
20794     }
20795   else
20796     {
20797       at_import_die = lookup_decl_die (decl);
20798       if (!at_import_die)
20799         {
20800           /* If we're trying to avoid duplicate debug info, we may not have
20801              emitted the member decl for this field.  Emit it now.  */
20802           if (TREE_CODE (decl) == FIELD_DECL)
20803             {
20804               tree type = DECL_CONTEXT (decl);
20805
20806               if (TYPE_CONTEXT (type)
20807                   && TYPE_P (TYPE_CONTEXT (type))
20808                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20809                                                 DINFO_USAGE_DIR_USE))
20810                 return;
20811               gen_type_die_for_member (type, decl,
20812                                        get_context_die (TYPE_CONTEXT (type)));
20813             }
20814           at_import_die = force_decl_die (decl);
20815         }
20816     }
20817
20818   if (TREE_CODE (decl) == NAMESPACE_DECL)
20819     {
20820       if (dwarf_version >= 3 || !dwarf_strict)
20821         imported_die = new_die (DW_TAG_imported_module,
20822                                 lexical_block_die,
20823                                 lexical_block);
20824       else
20825         return;
20826     }
20827   else
20828     imported_die = new_die (DW_TAG_imported_declaration,
20829                             lexical_block_die,
20830                             lexical_block);
20831
20832   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20833   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20834   if (name)
20835     add_AT_string (imported_die, DW_AT_name,
20836                    IDENTIFIER_POINTER (name));
20837   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20838 }
20839
20840 /* Output debug information for imported module or decl DECL.
20841    NAME is non-NULL name in context if the decl has been renamed.
20842    CHILD is true if decl is one of the renamed decls as part of
20843    importing whole module.  */
20844
20845 static void
20846 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20847                                    bool child)
20848 {
20849   /* dw_die_ref at_import_die;  */
20850   dw_die_ref scope_die;
20851
20852   if (debug_info_level <= DINFO_LEVEL_TERSE)
20853     return;
20854
20855   gcc_assert (decl);
20856
20857   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20858      We need decl DIE for reference and scope die. First, get DIE for the decl
20859      itself.  */
20860
20861   /* Get the scope die for decl context. Use comp_unit_die for global module
20862      or decl. If die is not found for non globals, force new die.  */
20863   if (context
20864       && TYPE_P (context)
20865       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20866     return;
20867
20868   if (!(dwarf_version >= 3 || !dwarf_strict))
20869     return;
20870
20871   scope_die = get_context_die (context);
20872
20873   if (child)
20874     {
20875       gcc_assert (scope_die->die_child);
20876       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20877       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20878       scope_die = scope_die->die_child;
20879     }
20880
20881   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20882   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20883
20884 }
20885
20886 /* Write the debugging output for DECL.  */
20887
20888 void
20889 dwarf2out_decl (tree decl)
20890 {
20891   dw_die_ref context_die = comp_unit_die;
20892
20893   switch (TREE_CODE (decl))
20894     {
20895     case ERROR_MARK:
20896       return;
20897
20898     case FUNCTION_DECL:
20899       /* What we would really like to do here is to filter out all mere
20900          file-scope declarations of file-scope functions which are never
20901          referenced later within this translation unit (and keep all of ones
20902          that *are* referenced later on) but we aren't clairvoyant, so we have
20903          no idea which functions will be referenced in the future (i.e. later
20904          on within the current translation unit). So here we just ignore all
20905          file-scope function declarations which are not also definitions.  If
20906          and when the debugger needs to know something about these functions,
20907          it will have to hunt around and find the DWARF information associated
20908          with the definition of the function.
20909
20910          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20911          nodes represent definitions and which ones represent mere
20912          declarations.  We have to check DECL_INITIAL instead. That's because
20913          the C front-end supports some weird semantics for "extern inline"
20914          function definitions.  These can get inlined within the current
20915          translation unit (and thus, we need to generate Dwarf info for their
20916          abstract instances so that the Dwarf info for the concrete inlined
20917          instances can have something to refer to) but the compiler never
20918          generates any out-of-lines instances of such things (despite the fact
20919          that they *are* definitions).
20920
20921          The important point is that the C front-end marks these "extern
20922          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20923          them anyway. Note that the C++ front-end also plays some similar games
20924          for inline function definitions appearing within include files which
20925          also contain `#pragma interface' pragmas.  */
20926       if (DECL_INITIAL (decl) == NULL_TREE)
20927         return;
20928
20929       /* If we're a nested function, initially use a parent of NULL; if we're
20930          a plain function, this will be fixed up in decls_for_scope.  If
20931          we're a method, it will be ignored, since we already have a DIE.  */
20932       if (decl_function_context (decl)
20933           /* But if we're in terse mode, we don't care about scope.  */
20934           && debug_info_level > DINFO_LEVEL_TERSE)
20935         context_die = NULL;
20936       break;
20937
20938     case VAR_DECL:
20939       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20940          declaration and if the declaration was never even referenced from
20941          within this entire compilation unit.  We suppress these DIEs in
20942          order to save space in the .debug section (by eliminating entries
20943          which are probably useless).  Note that we must not suppress
20944          block-local extern declarations (whether used or not) because that
20945          would screw-up the debugger's name lookup mechanism and cause it to
20946          miss things which really ought to be in scope at a given point.  */
20947       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20948         return;
20949
20950       /* For local statics lookup proper context die.  */
20951       if (TREE_STATIC (decl) && decl_function_context (decl))
20952         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20953
20954       /* If we are in terse mode, don't generate any DIEs to represent any
20955          variable declarations or definitions.  */
20956       if (debug_info_level <= DINFO_LEVEL_TERSE)
20957         return;
20958       break;
20959
20960     case CONST_DECL:
20961       if (debug_info_level <= DINFO_LEVEL_TERSE)
20962         return;
20963       if (!is_fortran () && !is_ada ())
20964         return;
20965       if (TREE_STATIC (decl) && decl_function_context (decl))
20966         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20967       break;
20968
20969     case NAMESPACE_DECL:
20970     case IMPORTED_DECL:
20971       if (debug_info_level <= DINFO_LEVEL_TERSE)
20972         return;
20973       if (lookup_decl_die (decl) != NULL)
20974         return;
20975       break;
20976
20977     case TYPE_DECL:
20978       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20979       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20980         return;
20981
20982       /* Don't bother trying to generate any DIEs to represent any of the
20983          normal built-in types for the language we are compiling.  */
20984       if (DECL_IS_BUILTIN (decl))
20985         {
20986           /* OK, we need to generate one for `bool' so GDB knows what type
20987              comparisons have.  */
20988           if (is_cxx ()
20989               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20990               && ! DECL_IGNORED_P (decl))
20991             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20992
20993           return;
20994         }
20995
20996       /* If we are in terse mode, don't generate any DIEs for types.  */
20997       if (debug_info_level <= DINFO_LEVEL_TERSE)
20998         return;
20999
21000       /* If we're a function-scope tag, initially use a parent of NULL;
21001          this will be fixed up in decls_for_scope.  */
21002       if (decl_function_context (decl))
21003         context_die = NULL;
21004
21005       break;
21006
21007     default:
21008       return;
21009     }
21010
21011   gen_decl_die (decl, NULL, context_die);
21012 }
21013
21014 /* Write the debugging output for DECL.  */
21015
21016 static void
21017 dwarf2out_function_decl (tree decl)
21018 {
21019   dwarf2out_decl (decl);
21020
21021   htab_empty (decl_loc_table);
21022 }
21023
21024 /* Output a marker (i.e. a label) for the beginning of the generated code for
21025    a lexical block.  */
21026
21027 static void
21028 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21029                        unsigned int blocknum)
21030 {
21031   switch_to_section (current_function_section ());
21032   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21033 }
21034
21035 /* Output a marker (i.e. a label) for the end of the generated code for a
21036    lexical block.  */
21037
21038 static void
21039 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21040 {
21041   switch_to_section (current_function_section ());
21042   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21043 }
21044
21045 /* Returns nonzero if it is appropriate not to emit any debugging
21046    information for BLOCK, because it doesn't contain any instructions.
21047
21048    Don't allow this for blocks with nested functions or local classes
21049    as we would end up with orphans, and in the presence of scheduling
21050    we may end up calling them anyway.  */
21051
21052 static bool
21053 dwarf2out_ignore_block (const_tree block)
21054 {
21055   tree decl;
21056   unsigned int i;
21057
21058   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21059     if (TREE_CODE (decl) == FUNCTION_DECL
21060         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21061       return 0;
21062   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21063     {
21064       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21065       if (TREE_CODE (decl) == FUNCTION_DECL
21066           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21067       return 0;
21068     }
21069
21070   return 1;
21071 }
21072
21073 /* Hash table routines for file_hash.  */
21074
21075 static int
21076 file_table_eq (const void *p1_p, const void *p2_p)
21077 {
21078   const struct dwarf_file_data *const p1 =
21079     (const struct dwarf_file_data *) p1_p;
21080   const char *const p2 = (const char *) p2_p;
21081   return strcmp (p1->filename, p2) == 0;
21082 }
21083
21084 static hashval_t
21085 file_table_hash (const void *p_p)
21086 {
21087   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21088   return htab_hash_string (p->filename);
21089 }
21090
21091 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21092    dwarf2out.c) and return its "index".  The index of each (known) filename is
21093    just a unique number which is associated with only that one filename.  We
21094    need such numbers for the sake of generating labels (in the .debug_sfnames
21095    section) and references to those files numbers (in the .debug_srcinfo
21096    and.debug_macinfo sections).  If the filename given as an argument is not
21097    found in our current list, add it to the list and assign it the next
21098    available unique index number.  In order to speed up searches, we remember
21099    the index of the filename was looked up last.  This handles the majority of
21100    all searches.  */
21101
21102 static struct dwarf_file_data *
21103 lookup_filename (const char *file_name)
21104 {
21105   void ** slot;
21106   struct dwarf_file_data * created;
21107
21108   /* Check to see if the file name that was searched on the previous
21109      call matches this file name.  If so, return the index.  */
21110   if (file_table_last_lookup
21111       && (file_name == file_table_last_lookup->filename
21112           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21113     return file_table_last_lookup;
21114
21115   /* Didn't match the previous lookup, search the table.  */
21116   slot = htab_find_slot_with_hash (file_table, file_name,
21117                                    htab_hash_string (file_name), INSERT);
21118   if (*slot)
21119     return (struct dwarf_file_data *) *slot;
21120
21121   created = ggc_alloc_dwarf_file_data ();
21122   created->filename = file_name;
21123   created->emitted_number = 0;
21124   *slot = created;
21125   return created;
21126 }
21127
21128 /* If the assembler will construct the file table, then translate the compiler
21129    internal file table number into the assembler file table number, and emit
21130    a .file directive if we haven't already emitted one yet.  The file table
21131    numbers are different because we prune debug info for unused variables and
21132    types, which may include filenames.  */
21133
21134 static int
21135 maybe_emit_file (struct dwarf_file_data * fd)
21136 {
21137   if (! fd->emitted_number)
21138     {
21139       if (last_emitted_file)
21140         fd->emitted_number = last_emitted_file->emitted_number + 1;
21141       else
21142         fd->emitted_number = 1;
21143       last_emitted_file = fd;
21144
21145       if (DWARF2_ASM_LINE_DEBUG_INFO)
21146         {
21147           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21148           output_quoted_string (asm_out_file,
21149                                 remap_debug_filename (fd->filename));
21150           fputc ('\n', asm_out_file);
21151         }
21152     }
21153
21154   return fd->emitted_number;
21155 }
21156
21157 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21158    That generation should happen after function debug info has been
21159    generated. The value of the attribute is the constant value of ARG.  */
21160
21161 static void
21162 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21163 {
21164   die_arg_entry entry;
21165
21166   if (!die || !arg)
21167     return;
21168
21169   if (!tmpl_value_parm_die_table)
21170     tmpl_value_parm_die_table
21171       = VEC_alloc (die_arg_entry, gc, 32);
21172
21173   entry.die = die;
21174   entry.arg = arg;
21175   VEC_safe_push (die_arg_entry, gc,
21176                  tmpl_value_parm_die_table,
21177                  &entry);
21178 }
21179
21180 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21181    by append_entry_to_tmpl_value_parm_die_table. This function must
21182    be called after function DIEs have been generated.  */
21183
21184 static void
21185 gen_remaining_tmpl_value_param_die_attribute (void)
21186 {
21187   if (tmpl_value_parm_die_table)
21188     {
21189       unsigned i;
21190       die_arg_entry *e;
21191
21192       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21193         tree_add_const_value_attribute (e->die, e->arg);
21194     }
21195 }
21196
21197
21198 /* Replace DW_AT_name for the decl with name.  */
21199
21200 static void
21201 dwarf2out_set_name (tree decl, tree name)
21202 {
21203   dw_die_ref die;
21204   dw_attr_ref attr;
21205   const char *dname;
21206
21207   die = TYPE_SYMTAB_DIE (decl);
21208   if (!die)
21209     return;
21210
21211   dname = dwarf2_name (name, 0);
21212   if (!dname)
21213     return;
21214
21215   attr = get_AT (die, DW_AT_name);
21216   if (attr)
21217     {
21218       struct indirect_string_node *node;
21219
21220       node = find_AT_string (dname);
21221       /* replace the string.  */
21222       attr->dw_attr_val.v.val_str = node;
21223     }
21224
21225   else
21226     add_name_attribute (die, dname);
21227 }
21228
21229 /* Called by the final INSN scan whenever we see a direct function call.
21230    Make an entry into the direct call table, recording the point of call
21231    and a reference to the target function's debug entry.  */
21232
21233 static void
21234 dwarf2out_direct_call (tree targ)
21235 {
21236   dcall_entry e;
21237   tree origin = decl_ultimate_origin (targ);
21238
21239   /* If this is a clone, use the abstract origin as the target.  */
21240   if (origin)
21241     targ = origin;
21242
21243   e.poc_label_num = poc_label_num++;
21244   e.poc_decl = current_function_decl;
21245   e.targ_die = force_decl_die (targ);
21246   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21247
21248   /* Drop a label at the return point to mark the point of call.  */
21249   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21250 }
21251
21252 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21253
21254 static hashval_t
21255 vcall_insn_table_hash (const void *x)
21256 {
21257   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21258 }
21259
21260 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21261    insnd_uid of *Y.  */
21262
21263 static int
21264 vcall_insn_table_eq (const void *x, const void *y)
21265 {
21266   return (((const struct vcall_insn *) x)->insn_uid
21267           == ((const struct vcall_insn *) y)->insn_uid);
21268 }
21269
21270 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21271
21272 static void
21273 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21274 {
21275   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21276   struct vcall_insn **slot;
21277
21278   gcc_assert (item);
21279   item->insn_uid = insn_uid;
21280   item->vtable_slot = vtable_slot;
21281   slot = (struct vcall_insn **)
21282       htab_find_slot_with_hash (vcall_insn_table, &item,
21283                                 (hashval_t) insn_uid, INSERT);
21284   *slot = item;
21285 }
21286
21287 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21288
21289 static unsigned int
21290 lookup_vcall_insn (unsigned int insn_uid)
21291 {
21292   struct vcall_insn item;
21293   struct vcall_insn *p;
21294
21295   item.insn_uid = insn_uid;
21296   item.vtable_slot = 0;
21297   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21298                                                  (void *) &item,
21299                                                  (hashval_t) insn_uid);
21300   if (p == NULL)
21301     return (unsigned int) -1;
21302   return p->vtable_slot;
21303 }
21304
21305
21306 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21307    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21308    is the vtable slot index that we will need to put in the virtual call
21309    table later.  */
21310
21311 static void
21312 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21313 {
21314   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21315     {
21316       tree token = OBJ_TYPE_REF_TOKEN (addr);
21317       if (TREE_CODE (token) == INTEGER_CST)
21318         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21319     }
21320 }
21321
21322 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21323    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21324    with NEW_INSN.  */
21325
21326 static void
21327 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21328 {
21329   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21330
21331   if (vtable_slot != (unsigned int) -1)
21332     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21333 }
21334
21335 /* Called by the final INSN scan whenever we see a virtual function call.
21336    Make an entry into the virtual call table, recording the point of call
21337    and the slot index of the vtable entry used to call the virtual member
21338    function.  The slot index was associated with the INSN_UID during the
21339    lowering to RTL.  */
21340
21341 static void
21342 dwarf2out_virtual_call (int insn_uid)
21343 {
21344   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21345   vcall_entry e;
21346
21347   if (vtable_slot == (unsigned int) -1)
21348     return;
21349
21350   e.poc_label_num = poc_label_num++;
21351   e.vtable_slot = vtable_slot;
21352   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21353
21354   /* Drop a label at the return point to mark the point of call.  */
21355   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21356 }
21357
21358 /* Called by the final INSN scan whenever we see a var location.  We
21359    use it to drop labels in the right places, and throw the location in
21360    our lookup table.  */
21361
21362 static void
21363 dwarf2out_var_location (rtx loc_note)
21364 {
21365   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21366   struct var_loc_node *newloc;
21367   rtx next_real;
21368   static const char *last_label;
21369   static const char *last_postcall_label;
21370   static bool last_in_cold_section_p;
21371   tree decl;
21372
21373   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21374     return;
21375
21376   next_real = next_real_insn (loc_note);
21377   /* If there are no instructions which would be affected by this note,
21378      don't do anything.  */
21379   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21380     return;
21381
21382   /* If there were any real insns between note we processed last time
21383      and this note (or if it is the first note), clear
21384      last_{,postcall_}label so that they are not reused this time.  */
21385   if (last_var_location_insn == NULL_RTX
21386       || last_var_location_insn != next_real
21387       || last_in_cold_section_p != in_cold_section_p)
21388     {
21389       last_label = NULL;
21390       last_postcall_label = NULL;
21391     }
21392
21393   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21394   newloc = add_var_loc_to_decl (decl, loc_note,
21395                                 NOTE_DURING_CALL_P (loc_note)
21396                                 ? last_postcall_label : last_label);
21397   if (newloc == NULL)
21398     return;
21399
21400   /* If there were no real insns between note we processed last time
21401      and this note, use the label we emitted last time.  Otherwise
21402      create a new label and emit it.  */
21403   if (last_label == NULL)
21404     {
21405       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21406       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21407       loclabel_num++;
21408       last_label = ggc_strdup (loclabel);
21409     }
21410
21411   if (!NOTE_DURING_CALL_P (loc_note))
21412     newloc->label = last_label;
21413   else
21414     {
21415       if (!last_postcall_label)
21416         {
21417           sprintf (loclabel, "%s-1", last_label);
21418           last_postcall_label = ggc_strdup (loclabel);
21419         }
21420       newloc->label = last_postcall_label;
21421     }
21422
21423   last_var_location_insn = next_real;
21424   last_in_cold_section_p = in_cold_section_p;
21425 }
21426
21427 /* We need to reset the locations at the beginning of each
21428    function. We can't do this in the end_function hook, because the
21429    declarations that use the locations won't have been output when
21430    that hook is called.  Also compute have_multiple_function_sections here.  */
21431
21432 static void
21433 dwarf2out_begin_function (tree fun)
21434 {
21435   if (function_section (fun) != text_section)
21436     have_multiple_function_sections = true;
21437
21438   dwarf2out_note_section_used ();
21439 }
21440
21441 /* Output a label to mark the beginning of a source code line entry
21442    and record information relating to this source line, in
21443    'line_info_table' for later output of the .debug_line section.  */
21444
21445 static void
21446 dwarf2out_source_line (unsigned int line, const char *filename,
21447                        int discriminator, bool is_stmt)
21448 {
21449   static bool last_is_stmt = true;
21450
21451   if (debug_info_level >= DINFO_LEVEL_NORMAL
21452       && line != 0)
21453     {
21454       int file_num = maybe_emit_file (lookup_filename (filename));
21455
21456       switch_to_section (current_function_section ());
21457
21458       /* If requested, emit something human-readable.  */
21459       if (flag_debug_asm)
21460         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21461                  filename, line);
21462
21463       if (DWARF2_ASM_LINE_DEBUG_INFO)
21464         {
21465           /* Emit the .loc directive understood by GNU as.  */
21466           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21467           if (is_stmt != last_is_stmt)
21468             {
21469               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21470               last_is_stmt = is_stmt;
21471             }
21472           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21473             fprintf (asm_out_file, " discriminator %d", discriminator);
21474           fputc ('\n', asm_out_file);
21475
21476           /* Indicate that line number info exists.  */
21477           line_info_table_in_use++;
21478         }
21479       else if (function_section (current_function_decl) != text_section)
21480         {
21481           dw_separate_line_info_ref line_info;
21482           targetm.asm_out.internal_label (asm_out_file,
21483                                           SEPARATE_LINE_CODE_LABEL,
21484                                           separate_line_info_table_in_use);
21485
21486           /* Expand the line info table if necessary.  */
21487           if (separate_line_info_table_in_use
21488               == separate_line_info_table_allocated)
21489             {
21490               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21491               separate_line_info_table
21492                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21493                                  separate_line_info_table,
21494                                  separate_line_info_table_allocated);
21495               memset (separate_line_info_table
21496                        + separate_line_info_table_in_use,
21497                       0,
21498                       (LINE_INFO_TABLE_INCREMENT
21499                        * sizeof (dw_separate_line_info_entry)));
21500             }
21501
21502           /* Add the new entry at the end of the line_info_table.  */
21503           line_info
21504             = &separate_line_info_table[separate_line_info_table_in_use++];
21505           line_info->dw_file_num = file_num;
21506           line_info->dw_line_num = line;
21507           line_info->function = current_function_funcdef_no;
21508         }
21509       else
21510         {
21511           dw_line_info_ref line_info;
21512
21513           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21514                                      line_info_table_in_use);
21515
21516           /* Expand the line info table if necessary.  */
21517           if (line_info_table_in_use == line_info_table_allocated)
21518             {
21519               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21520               line_info_table
21521                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21522                                  line_info_table_allocated);
21523               memset (line_info_table + line_info_table_in_use, 0,
21524                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21525             }
21526
21527           /* Add the new entry at the end of the line_info_table.  */
21528           line_info = &line_info_table[line_info_table_in_use++];
21529           line_info->dw_file_num = file_num;
21530           line_info->dw_line_num = line;
21531         }
21532     }
21533 }
21534
21535 /* Record the beginning of a new source file.  */
21536
21537 static void
21538 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21539 {
21540   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21541     {
21542       /* Record the beginning of the file for break_out_includes.  */
21543       dw_die_ref bincl_die;
21544
21545       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21546       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21547     }
21548
21549   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21550     {
21551       int file_num = maybe_emit_file (lookup_filename (filename));
21552
21553       switch_to_section (debug_macinfo_section);
21554       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21555       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21556                                    lineno);
21557
21558       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21559     }
21560 }
21561
21562 /* Record the end of a source file.  */
21563
21564 static void
21565 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21566 {
21567   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21568     /* Record the end of the file for break_out_includes.  */
21569     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21570
21571   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21572     {
21573       switch_to_section (debug_macinfo_section);
21574       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21575     }
21576 }
21577
21578 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21579    the tail part of the directive line, i.e. the part which is past the
21580    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21581
21582 static void
21583 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21584                   const char *buffer ATTRIBUTE_UNUSED)
21585 {
21586   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21587     {
21588       switch_to_section (debug_macinfo_section);
21589       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21590       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21591       dw2_asm_output_nstring (buffer, -1, "The macro");
21592     }
21593 }
21594
21595 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21596    the tail part of the directive line, i.e. the part which is past the
21597    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21598
21599 static void
21600 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21601                  const char *buffer ATTRIBUTE_UNUSED)
21602 {
21603   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21604     {
21605       switch_to_section (debug_macinfo_section);
21606       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21607       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21608       dw2_asm_output_nstring (buffer, -1, "The macro");
21609     }
21610 }
21611
21612 /* Set up for Dwarf output at the start of compilation.  */
21613
21614 static void
21615 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21616 {
21617   /* Allocate the file_table.  */
21618   file_table = htab_create_ggc (50, file_table_hash,
21619                                 file_table_eq, NULL);
21620
21621   /* Allocate the decl_die_table.  */
21622   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21623                                     decl_die_table_eq, NULL);
21624
21625   /* Allocate the decl_loc_table.  */
21626   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21627                                     decl_loc_table_eq, NULL);
21628
21629   /* Allocate the initial hunk of the decl_scope_table.  */
21630   decl_scope_table = VEC_alloc (tree, gc, 256);
21631
21632   /* Allocate the initial hunk of the abbrev_die_table.  */
21633   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21634     (ABBREV_DIE_TABLE_INCREMENT);
21635   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21636   /* Zero-th entry is allocated, but unused.  */
21637   abbrev_die_table_in_use = 1;
21638
21639   /* Allocate the initial hunk of the line_info_table.  */
21640   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21641     (LINE_INFO_TABLE_INCREMENT);
21642   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21643
21644   /* Zero-th entry is allocated, but unused.  */
21645   line_info_table_in_use = 1;
21646
21647   /* Allocate the pubtypes and pubnames vectors.  */
21648   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21649   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21650
21651   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21652   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21653                                       vcall_insn_table_eq, NULL);
21654
21655   /* Generate the initial DIE for the .debug section.  Note that the (string)
21656      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21657      will (typically) be a relative pathname and that this pathname should be
21658      taken as being relative to the directory from which the compiler was
21659      invoked when the given (base) source file was compiled.  We will fill
21660      in this value in dwarf2out_finish.  */
21661   comp_unit_die = gen_compile_unit_die (NULL);
21662
21663   incomplete_types = VEC_alloc (tree, gc, 64);
21664
21665   used_rtx_array = VEC_alloc (rtx, gc, 32);
21666
21667   debug_info_section = get_section (DEBUG_INFO_SECTION,
21668                                     SECTION_DEBUG, NULL);
21669   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21670                                       SECTION_DEBUG, NULL);
21671   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21672                                        SECTION_DEBUG, NULL);
21673   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21674                                        SECTION_DEBUG, NULL);
21675   debug_line_section = get_section (DEBUG_LINE_SECTION,
21676                                     SECTION_DEBUG, NULL);
21677   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21678                                    SECTION_DEBUG, NULL);
21679   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21680                                         SECTION_DEBUG, NULL);
21681   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21682                                         SECTION_DEBUG, NULL);
21683   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21684                                      SECTION_DEBUG, NULL);
21685   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21686                                      SECTION_DEBUG, NULL);
21687   debug_str_section = get_section (DEBUG_STR_SECTION,
21688                                    DEBUG_STR_SECTION_FLAGS, NULL);
21689   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21690                                       SECTION_DEBUG, NULL);
21691   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21692                                      SECTION_DEBUG, NULL);
21693
21694   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21695   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21696                                DEBUG_ABBREV_SECTION_LABEL, 0);
21697   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21698   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21699                                COLD_TEXT_SECTION_LABEL, 0);
21700   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21701
21702   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21703                                DEBUG_INFO_SECTION_LABEL, 0);
21704   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21705                                DEBUG_LINE_SECTION_LABEL, 0);
21706   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21707                                DEBUG_RANGES_SECTION_LABEL, 0);
21708   switch_to_section (debug_abbrev_section);
21709   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21710   switch_to_section (debug_info_section);
21711   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21712   switch_to_section (debug_line_section);
21713   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21714
21715   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21716     {
21717       switch_to_section (debug_macinfo_section);
21718       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21719                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21720       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21721     }
21722
21723   switch_to_section (text_section);
21724   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21725   if (flag_reorder_blocks_and_partition)
21726     {
21727       cold_text_section = unlikely_text_section ();
21728       switch_to_section (cold_text_section);
21729       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21730     }
21731
21732 }
21733
21734 /* Called before cgraph_optimize starts outputtting functions, variables
21735    and toplevel asms into assembly.  */
21736
21737 static void
21738 dwarf2out_assembly_start (void)
21739 {
21740   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21741     {
21742 #ifndef TARGET_UNWIND_INFO
21743       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21744 #endif
21745         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21746     }
21747 }
21748
21749 /* A helper function for dwarf2out_finish called through
21750    htab_traverse.  Emit one queued .debug_str string.  */
21751
21752 static int
21753 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21754 {
21755   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21756
21757   if (node->label && node->refcount)
21758     {
21759       switch_to_section (debug_str_section);
21760       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21761       assemble_string (node->str, strlen (node->str) + 1);
21762     }
21763
21764   return 1;
21765 }
21766
21767 #if ENABLE_ASSERT_CHECKING
21768 /* Verify that all marks are clear.  */
21769
21770 static void
21771 verify_marks_clear (dw_die_ref die)
21772 {
21773   dw_die_ref c;
21774
21775   gcc_assert (! die->die_mark);
21776   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21777 }
21778 #endif /* ENABLE_ASSERT_CHECKING */
21779
21780 /* Clear the marks for a die and its children.
21781    Be cool if the mark isn't set.  */
21782
21783 static void
21784 prune_unmark_dies (dw_die_ref die)
21785 {
21786   dw_die_ref c;
21787
21788   if (die->die_mark)
21789     die->die_mark = 0;
21790   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21791 }
21792
21793 /* Given DIE that we're marking as used, find any other dies
21794    it references as attributes and mark them as used.  */
21795
21796 static void
21797 prune_unused_types_walk_attribs (dw_die_ref die)
21798 {
21799   dw_attr_ref a;
21800   unsigned ix;
21801
21802   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21803     {
21804       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21805         {
21806           /* A reference to another DIE.
21807              Make sure that it will get emitted.
21808              If it was broken out into a comdat group, don't follow it.  */
21809           if (dwarf_version < 4
21810               || a->dw_attr == DW_AT_specification
21811               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21812             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21813         }
21814       /* Set the string's refcount to 0 so that prune_unused_types_mark
21815          accounts properly for it.  */
21816       if (AT_class (a) == dw_val_class_str)
21817         a->dw_attr_val.v.val_str->refcount = 0;
21818     }
21819 }
21820
21821
21822 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21823    to DIE's children.  */
21824
21825 static void
21826 prune_unused_types_mark (dw_die_ref die, int dokids)
21827 {
21828   dw_die_ref c;
21829
21830   if (die->die_mark == 0)
21831     {
21832       /* We haven't done this node yet.  Mark it as used.  */
21833       die->die_mark = 1;
21834
21835       /* We also have to mark its parents as used.
21836          (But we don't want to mark our parents' kids due to this.)  */
21837       if (die->die_parent)
21838         prune_unused_types_mark (die->die_parent, 0);
21839
21840       /* Mark any referenced nodes.  */
21841       prune_unused_types_walk_attribs (die);
21842
21843       /* If this node is a specification,
21844          also mark the definition, if it exists.  */
21845       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21846         prune_unused_types_mark (die->die_definition, 1);
21847     }
21848
21849   if (dokids && die->die_mark != 2)
21850     {
21851       /* We need to walk the children, but haven't done so yet.
21852          Remember that we've walked the kids.  */
21853       die->die_mark = 2;
21854
21855       /* If this is an array type, we need to make sure our
21856          kids get marked, even if they're types.  If we're
21857          breaking out types into comdat sections, do this
21858          for all type definitions.  */
21859       if (die->die_tag == DW_TAG_array_type
21860           || (dwarf_version >= 4
21861               && is_type_die (die) && ! is_declaration_die (die)))
21862         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21863       else
21864         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21865     }
21866 }
21867
21868 /* For local classes, look if any static member functions were emitted
21869    and if so, mark them.  */
21870
21871 static void
21872 prune_unused_types_walk_local_classes (dw_die_ref die)
21873 {
21874   dw_die_ref c;
21875
21876   if (die->die_mark == 2)
21877     return;
21878
21879   switch (die->die_tag)
21880     {
21881     case DW_TAG_structure_type:
21882     case DW_TAG_union_type:
21883     case DW_TAG_class_type:
21884       break;
21885
21886     case DW_TAG_subprogram:
21887       if (!get_AT_flag (die, DW_AT_declaration)
21888           || die->die_definition != NULL)
21889         prune_unused_types_mark (die, 1);
21890       return;
21891
21892     default:
21893       return;
21894     }
21895
21896   /* Mark children.  */
21897   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21898 }
21899
21900 /* Walk the tree DIE and mark types that we actually use.  */
21901
21902 static void
21903 prune_unused_types_walk (dw_die_ref die)
21904 {
21905   dw_die_ref c;
21906
21907   /* Don't do anything if this node is already marked and
21908      children have been marked as well.  */
21909   if (die->die_mark == 2)
21910     return;
21911
21912   switch (die->die_tag)
21913     {
21914     case DW_TAG_structure_type:
21915     case DW_TAG_union_type:
21916     case DW_TAG_class_type:
21917       if (die->die_perennial_p)
21918         break;
21919
21920       for (c = die->die_parent; c; c = c->die_parent)
21921         if (c->die_tag == DW_TAG_subprogram)
21922           break;
21923
21924       /* Finding used static member functions inside of classes
21925          is needed just for local classes, because for other classes
21926          static member function DIEs with DW_AT_specification
21927          are emitted outside of the DW_TAG_*_type.  If we ever change
21928          it, we'd need to call this even for non-local classes.  */
21929       if (c)
21930         prune_unused_types_walk_local_classes (die);
21931
21932       /* It's a type node --- don't mark it.  */
21933       return;
21934
21935     case DW_TAG_const_type:
21936     case DW_TAG_packed_type:
21937     case DW_TAG_pointer_type:
21938     case DW_TAG_reference_type:
21939     case DW_TAG_rvalue_reference_type:
21940     case DW_TAG_volatile_type:
21941     case DW_TAG_typedef:
21942     case DW_TAG_array_type:
21943     case DW_TAG_interface_type:
21944     case DW_TAG_friend:
21945     case DW_TAG_variant_part:
21946     case DW_TAG_enumeration_type:
21947     case DW_TAG_subroutine_type:
21948     case DW_TAG_string_type:
21949     case DW_TAG_set_type:
21950     case DW_TAG_subrange_type:
21951     case DW_TAG_ptr_to_member_type:
21952     case DW_TAG_file_type:
21953       if (die->die_perennial_p)
21954         break;
21955
21956       /* It's a type node --- don't mark it.  */
21957       return;
21958
21959     default:
21960       /* Mark everything else.  */
21961       break;
21962   }
21963
21964   if (die->die_mark == 0)
21965     {
21966       die->die_mark = 1;
21967
21968       /* Now, mark any dies referenced from here.  */
21969       prune_unused_types_walk_attribs (die);
21970     }
21971
21972   die->die_mark = 2;
21973
21974   /* Mark children.  */
21975   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21976 }
21977
21978 /* Increment the string counts on strings referred to from DIE's
21979    attributes.  */
21980
21981 static void
21982 prune_unused_types_update_strings (dw_die_ref die)
21983 {
21984   dw_attr_ref a;
21985   unsigned ix;
21986
21987   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21988     if (AT_class (a) == dw_val_class_str)
21989       {
21990         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21991         s->refcount++;
21992         /* Avoid unnecessarily putting strings that are used less than
21993            twice in the hash table.  */
21994         if (s->refcount
21995             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21996           {
21997             void ** slot;
21998             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21999                                              htab_hash_string (s->str),
22000                                              INSERT);
22001             gcc_assert (*slot == NULL);
22002             *slot = s;
22003           }
22004       }
22005 }
22006
22007 /* Remove from the tree DIE any dies that aren't marked.  */
22008
22009 static void
22010 prune_unused_types_prune (dw_die_ref die)
22011 {
22012   dw_die_ref c;
22013
22014   gcc_assert (die->die_mark);
22015   prune_unused_types_update_strings (die);
22016
22017   if (! die->die_child)
22018     return;
22019
22020   c = die->die_child;
22021   do {
22022     dw_die_ref prev = c;
22023     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22024       if (c == die->die_child)
22025         {
22026           /* No marked children between 'prev' and the end of the list.  */
22027           if (prev == c)
22028             /* No marked children at all.  */
22029             die->die_child = NULL;
22030           else
22031             {
22032               prev->die_sib = c->die_sib;
22033               die->die_child = prev;
22034             }
22035           return;
22036         }
22037
22038     if (c != prev->die_sib)
22039       prev->die_sib = c;
22040     prune_unused_types_prune (c);
22041   } while (c != die->die_child);
22042 }
22043
22044 /* A helper function for dwarf2out_finish called through
22045    htab_traverse.  Clear .debug_str strings that we haven't already
22046    decided to emit.  */
22047
22048 static int
22049 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22050 {
22051   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22052
22053   if (!node->label || !node->refcount)
22054     htab_clear_slot (debug_str_hash, h);
22055
22056   return 1;
22057 }
22058
22059 /* Remove dies representing declarations that we never use.  */
22060
22061 static void
22062 prune_unused_types (void)
22063 {
22064   unsigned int i;
22065   limbo_die_node *node;
22066   comdat_type_node *ctnode;
22067   pubname_ref pub;
22068   dcall_entry *dcall;
22069
22070 #if ENABLE_ASSERT_CHECKING
22071   /* All the marks should already be clear.  */
22072   verify_marks_clear (comp_unit_die);
22073   for (node = limbo_die_list; node; node = node->next)
22074     verify_marks_clear (node->die);
22075   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22076     verify_marks_clear (ctnode->root_die);
22077 #endif /* ENABLE_ASSERT_CHECKING */
22078
22079   /* Mark types that are used in global variables.  */
22080   premark_types_used_by_global_vars ();
22081
22082   /* Set the mark on nodes that are actually used.  */
22083   prune_unused_types_walk (comp_unit_die);
22084   for (node = limbo_die_list; node; node = node->next)
22085     prune_unused_types_walk (node->die);
22086   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22087     {
22088       prune_unused_types_walk (ctnode->root_die);
22089       prune_unused_types_mark (ctnode->type_die, 1);
22090     }
22091
22092   /* Also set the mark on nodes referenced from the
22093      pubname_table or arange_table.  */
22094   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22095     prune_unused_types_mark (pub->die, 1);
22096   for (i = 0; i < arange_table_in_use; i++)
22097     prune_unused_types_mark (arange_table[i], 1);
22098
22099   /* Mark nodes referenced from the direct call table.  */
22100   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22101     prune_unused_types_mark (dcall->targ_die, 1);
22102
22103   /* Get rid of nodes that aren't marked; and update the string counts.  */
22104   if (debug_str_hash && debug_str_hash_forced)
22105     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22106   else if (debug_str_hash)
22107     htab_empty (debug_str_hash);
22108   prune_unused_types_prune (comp_unit_die);
22109   for (node = limbo_die_list; node; node = node->next)
22110     prune_unused_types_prune (node->die);
22111   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22112     prune_unused_types_prune (ctnode->root_die);
22113
22114   /* Leave the marks clear.  */
22115   prune_unmark_dies (comp_unit_die);
22116   for (node = limbo_die_list; node; node = node->next)
22117     prune_unmark_dies (node->die);
22118   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22119     prune_unmark_dies (ctnode->root_die);
22120 }
22121
22122 /* Set the parameter to true if there are any relative pathnames in
22123    the file table.  */
22124 static int
22125 file_table_relative_p (void ** slot, void *param)
22126 {
22127   bool *p = (bool *) param;
22128   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22129   if (!IS_ABSOLUTE_PATH (d->filename))
22130     {
22131       *p = true;
22132       return 0;
22133     }
22134   return 1;
22135 }
22136
22137 /* Routines to manipulate hash table of comdat type units.  */
22138
22139 static hashval_t
22140 htab_ct_hash (const void *of)
22141 {
22142   hashval_t h;
22143   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22144
22145   memcpy (&h, type_node->signature, sizeof (h));
22146   return h;
22147 }
22148
22149 static int
22150 htab_ct_eq (const void *of1, const void *of2)
22151 {
22152   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22153   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22154
22155   return (! memcmp (type_node_1->signature, type_node_2->signature,
22156                     DWARF_TYPE_SIGNATURE_SIZE));
22157 }
22158
22159 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22160    to the location it would have been added, should we know its
22161    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22162    probably improve compactness of debug info, removing equivalent
22163    abbrevs, and hide any differences caused by deferring the
22164    computation of the assembler name, triggered by e.g. PCH.  */
22165
22166 static inline void
22167 move_linkage_attr (dw_die_ref die)
22168 {
22169   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22170   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22171
22172   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22173               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22174
22175   while (--ix > 0)
22176     {
22177       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22178
22179       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22180         break;
22181     }
22182
22183   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22184     {
22185       VEC_pop (dw_attr_node, die->die_attr);
22186       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22187     }
22188 }
22189
22190 /* Helper function for resolve_addr, attempt to resolve
22191    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22192    SYMBOL_REFs refer to variables emitted in the current CU.  */
22193
22194 static int
22195 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22196 {
22197   rtx rtl = *addr;
22198
22199   if (GET_CODE (rtl) == CONST_STRING)
22200     {
22201       size_t len = strlen (XSTR (rtl, 0)) + 1;
22202       tree t = build_string (len, XSTR (rtl, 0));
22203       tree tlen = build_int_cst (NULL_TREE, len - 1);
22204       TREE_TYPE (t)
22205         = build_array_type (char_type_node, build_index_type (tlen));
22206       rtl = lookup_constant_def (t);
22207       if (!rtl || !MEM_P (rtl))
22208         return 1;
22209       rtl = XEXP (rtl, 0);
22210       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22211       *addr = rtl;
22212       return 0;
22213     }
22214
22215   if (GET_CODE (rtl) == SYMBOL_REF
22216       && SYMBOL_REF_DECL (rtl)
22217       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22218     return 1;
22219
22220   if (GET_CODE (rtl) == CONST
22221       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22222     return 1;
22223
22224   return 0;
22225 }
22226
22227 /* Helper function for resolve_addr, handle one location
22228    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22229    the location list couldn't be resolved.  */
22230
22231 static bool
22232 resolve_addr_in_expr (dw_loc_descr_ref loc)
22233 {
22234   for (; loc; loc = loc->dw_loc_next)
22235     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22236          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22237         || (loc->dw_loc_opc == DW_OP_implicit_value
22238             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22239             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22240       return false;
22241     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22242              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22243       {
22244         dw_die_ref ref
22245           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22246         if (ref == NULL)
22247           return false;
22248         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22249         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22250         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22251       }
22252   return true;
22253 }
22254
22255 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22256    an address in .rodata section if the string literal is emitted there,
22257    or remove the containing location list or replace DW_AT_const_value
22258    with DW_AT_location and empty location expression, if it isn't found
22259    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22260    to something that has been emitted in the current CU.  */
22261
22262 static void
22263 resolve_addr (dw_die_ref die)
22264 {
22265   dw_die_ref c;
22266   dw_attr_ref a;
22267   dw_loc_list_ref *curr;
22268   unsigned ix;
22269
22270   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22271     switch (AT_class (a))
22272       {
22273       case dw_val_class_loc_list:
22274         curr = AT_loc_list_ptr (a);
22275         while (*curr)
22276           {
22277             if (!resolve_addr_in_expr ((*curr)->expr))
22278               {
22279                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22280                 if (next && (*curr)->ll_symbol)
22281                   {
22282                     gcc_assert (!next->ll_symbol);
22283                     next->ll_symbol = (*curr)->ll_symbol;
22284                   }
22285                 *curr = next;
22286               }
22287             else
22288               curr = &(*curr)->dw_loc_next;
22289           }
22290         if (!AT_loc_list (a))
22291           {
22292             remove_AT (die, a->dw_attr);
22293             ix--;
22294           }
22295         break;
22296       case dw_val_class_loc:
22297         if (!resolve_addr_in_expr (AT_loc (a)))
22298           {
22299             remove_AT (die, a->dw_attr);
22300             ix--;
22301           }
22302         break;
22303       case dw_val_class_addr:
22304         if (a->dw_attr == DW_AT_const_value
22305             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22306           {
22307             remove_AT (die, a->dw_attr);
22308             ix--;
22309           }
22310         break;
22311       default:
22312         break;
22313       }
22314
22315   FOR_EACH_CHILD (die, c, resolve_addr (c));
22316 }
22317
22318 /* Output stuff that dwarf requires at the end of every file,
22319    and generate the DWARF-2 debugging info.  */
22320
22321 static void
22322 dwarf2out_finish (const char *filename)
22323 {
22324   limbo_die_node *node, *next_node;
22325   comdat_type_node *ctnode;
22326   htab_t comdat_type_table;
22327   dw_die_ref die = 0;
22328   unsigned int i;
22329
22330   gen_remaining_tmpl_value_param_die_attribute ();
22331
22332   /* Add the name for the main input file now.  We delayed this from
22333      dwarf2out_init to avoid complications with PCH.  */
22334   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22335   if (!IS_ABSOLUTE_PATH (filename))
22336     add_comp_dir_attribute (comp_unit_die);
22337   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22338     {
22339       bool p = false;
22340       htab_traverse (file_table, file_table_relative_p, &p);
22341       if (p)
22342         add_comp_dir_attribute (comp_unit_die);
22343     }
22344
22345   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22346     {
22347       add_location_or_const_value_attribute (
22348         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22349         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22350         DW_AT_location);
22351     }
22352
22353   /* Traverse the limbo die list, and add parent/child links.  The only
22354      dies without parents that should be here are concrete instances of
22355      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22356      For concrete instances, we can get the parent die from the abstract
22357      instance.  */
22358   for (node = limbo_die_list; node; node = next_node)
22359     {
22360       next_node = node->next;
22361       die = node->die;
22362
22363       if (die->die_parent == NULL)
22364         {
22365           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22366
22367           if (origin)
22368             add_child_die (origin->die_parent, die);
22369           else if (die == comp_unit_die)
22370             ;
22371           else if (seen_error ())
22372             /* It's OK to be confused by errors in the input.  */
22373             add_child_die (comp_unit_die, die);
22374           else
22375             {
22376               /* In certain situations, the lexical block containing a
22377                  nested function can be optimized away, which results
22378                  in the nested function die being orphaned.  Likewise
22379                  with the return type of that nested function.  Force
22380                  this to be a child of the containing function.
22381
22382                  It may happen that even the containing function got fully
22383                  inlined and optimized out.  In that case we are lost and
22384                  assign the empty child.  This should not be big issue as
22385                  the function is likely unreachable too.  */
22386               tree context = NULL_TREE;
22387
22388               gcc_assert (node->created_for);
22389
22390               if (DECL_P (node->created_for))
22391                 context = DECL_CONTEXT (node->created_for);
22392               else if (TYPE_P (node->created_for))
22393                 context = TYPE_CONTEXT (node->created_for);
22394
22395               gcc_assert (context
22396                           && (TREE_CODE (context) == FUNCTION_DECL
22397                               || TREE_CODE (context) == NAMESPACE_DECL));
22398
22399               origin = lookup_decl_die (context);
22400               if (origin)
22401                 add_child_die (origin, die);
22402               else
22403                 add_child_die (comp_unit_die, die);
22404             }
22405         }
22406     }
22407
22408   limbo_die_list = NULL;
22409
22410   resolve_addr (comp_unit_die);
22411
22412   for (node = deferred_asm_name; node; node = node->next)
22413     {
22414       tree decl = node->created_for;
22415       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22416         {
22417           add_linkage_attr (node->die, decl);
22418           move_linkage_attr (node->die);
22419         }
22420     }
22421
22422   deferred_asm_name = NULL;
22423
22424   /* Walk through the list of incomplete types again, trying once more to
22425      emit full debugging info for them.  */
22426   retry_incomplete_types ();
22427
22428   if (flag_eliminate_unused_debug_types)
22429     prune_unused_types ();
22430
22431   /* Generate separate CUs for each of the include files we've seen.
22432      They will go into limbo_die_list.  */
22433   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22434     break_out_includes (comp_unit_die);
22435
22436   /* Generate separate COMDAT sections for type DIEs. */
22437   if (dwarf_version >= 4)
22438     {
22439       break_out_comdat_types (comp_unit_die);
22440
22441       /* Each new type_unit DIE was added to the limbo die list when created.
22442          Since these have all been added to comdat_type_list, clear the
22443          limbo die list.  */
22444       limbo_die_list = NULL;
22445
22446       /* For each new comdat type unit, copy declarations for incomplete
22447          types to make the new unit self-contained (i.e., no direct
22448          references to the main compile unit).  */
22449       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22450         copy_decls_for_unworthy_types (ctnode->root_die);
22451       copy_decls_for_unworthy_types (comp_unit_die);
22452
22453       /* In the process of copying declarations from one unit to another,
22454          we may have left some declarations behind that are no longer
22455          referenced.  Prune them.  */
22456       prune_unused_types ();
22457     }
22458
22459   /* Traverse the DIE's and add add sibling attributes to those DIE's
22460      that have children.  */
22461   add_sibling_attributes (comp_unit_die);
22462   for (node = limbo_die_list; node; node = node->next)
22463     add_sibling_attributes (node->die);
22464   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22465     add_sibling_attributes (ctnode->root_die);
22466
22467   /* Output a terminator label for the .text section.  */
22468   switch_to_section (text_section);
22469   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22470   if (flag_reorder_blocks_and_partition)
22471     {
22472       switch_to_section (unlikely_text_section ());
22473       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22474     }
22475
22476   /* We can only use the low/high_pc attributes if all of the code was
22477      in .text.  */
22478   if (!have_multiple_function_sections
22479       || !(dwarf_version >= 3 || !dwarf_strict))
22480     {
22481       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22482       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22483     }
22484
22485   else
22486     {
22487       unsigned fde_idx = 0;
22488       bool range_list_added = false;
22489
22490       /* We need to give .debug_loc and .debug_ranges an appropriate
22491          "base address".  Use zero so that these addresses become
22492          absolute.  Historically, we've emitted the unexpected
22493          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22494          Emit both to give time for other tools to adapt.  */
22495       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22496       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22497
22498       if (text_section_used)
22499         add_ranges_by_labels (comp_unit_die, text_section_label,
22500                               text_end_label, &range_list_added);
22501       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22502         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22503                               cold_end_label, &range_list_added);
22504
22505       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22506         {
22507           dw_fde_ref fde = &fde_table[fde_idx];
22508
22509           if (fde->dw_fde_switched_sections)
22510             {
22511               if (!fde->in_std_section)
22512                 add_ranges_by_labels (comp_unit_die,
22513                                       fde->dw_fde_hot_section_label,
22514                                       fde->dw_fde_hot_section_end_label,
22515                                       &range_list_added);
22516               if (!fde->cold_in_std_section)
22517                 add_ranges_by_labels (comp_unit_die,
22518                                       fde->dw_fde_unlikely_section_label,
22519                                       fde->dw_fde_unlikely_section_end_label,
22520                                       &range_list_added);
22521             }
22522           else if (!fde->in_std_section)
22523             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22524                                   fde->dw_fde_end, &range_list_added);
22525         }
22526
22527       if (range_list_added)
22528         add_ranges (NULL);
22529     }
22530
22531   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22532     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22533                     debug_line_section_label);
22534
22535   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22536     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22537
22538   /* Output all of the compilation units.  We put the main one last so that
22539      the offsets are available to output_pubnames.  */
22540   for (node = limbo_die_list; node; node = node->next)
22541     output_comp_unit (node->die, 0);
22542
22543   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22544   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22545     {
22546       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22547
22548       /* Don't output duplicate types.  */
22549       if (*slot != HTAB_EMPTY_ENTRY)
22550         continue;
22551
22552       /* Add a pointer to the line table for the main compilation unit
22553          so that the debugger can make sense of DW_AT_decl_file
22554          attributes.  */
22555       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22556         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22557                         debug_line_section_label);
22558
22559       output_comdat_type_unit (ctnode);
22560       *slot = ctnode;
22561     }
22562   htab_delete (comdat_type_table);
22563
22564   /* Output the main compilation unit if non-empty or if .debug_macinfo
22565      has been emitted.  */
22566   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22567
22568   /* Output the abbreviation table.  */
22569   switch_to_section (debug_abbrev_section);
22570   output_abbrev_section ();
22571
22572   /* Output location list section if necessary.  */
22573   if (have_location_lists)
22574     {
22575       /* Output the location lists info.  */
22576       switch_to_section (debug_loc_section);
22577       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22578                                    DEBUG_LOC_SECTION_LABEL, 0);
22579       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22580       output_location_lists (die);
22581     }
22582
22583   /* Output public names table if necessary.  */
22584   if (!VEC_empty (pubname_entry, pubname_table))
22585     {
22586       switch_to_section (debug_pubnames_section);
22587       output_pubnames (pubname_table);
22588     }
22589
22590   /* Output public types table if necessary.  */
22591   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22592      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22593      simply won't look for the section.  */
22594   if (!VEC_empty (pubname_entry, pubtype_table))
22595     {
22596       switch_to_section (debug_pubtypes_section);
22597       output_pubnames (pubtype_table);
22598     }
22599
22600   /* Output direct and virtual call tables if necessary.  */
22601   if (!VEC_empty (dcall_entry, dcall_table))
22602     {
22603       switch_to_section (debug_dcall_section);
22604       output_dcall_table ();
22605     }
22606   if (!VEC_empty (vcall_entry, vcall_table))
22607     {
22608       switch_to_section (debug_vcall_section);
22609       output_vcall_table ();
22610     }
22611
22612   /* Output the address range information.  We only put functions in the arange
22613      table, so don't write it out if we don't have any.  */
22614   if (fde_table_in_use)
22615     {
22616       switch_to_section (debug_aranges_section);
22617       output_aranges ();
22618     }
22619
22620   /* Output ranges section if necessary.  */
22621   if (ranges_table_in_use)
22622     {
22623       switch_to_section (debug_ranges_section);
22624       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22625       output_ranges ();
22626     }
22627
22628   /* Output the source line correspondence table.  We must do this
22629      even if there is no line information.  Otherwise, on an empty
22630      translation unit, we will generate a present, but empty,
22631      .debug_info section.  IRIX 6.5 `nm' will then complain when
22632      examining the file.  This is done late so that any filenames
22633      used by the debug_info section are marked as 'used'.  */
22634   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22635     {
22636       switch_to_section (debug_line_section);
22637       output_line_info ();
22638     }
22639
22640   /* Have to end the macro section.  */
22641   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22642     {
22643       switch_to_section (debug_macinfo_section);
22644       dw2_asm_output_data (1, 0, "End compilation unit");
22645     }
22646
22647   /* If we emitted any DW_FORM_strp form attribute, output the string
22648      table too.  */
22649   if (debug_str_hash)
22650     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22651 }
22652
22653 #include "gt-dwarf2out.h"